The Battle for Wesnoth  1.17.23+dev
image.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2023
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
18 #include "gui/widgets/image.hpp"
19 
20 #include "picture.hpp" // We want the file in src/
21 
24 #include "gui/core/log.hpp"
26 #include "gui/widgets/settings.hpp"
27 #include "wml_exception.hpp"
28 #include "gettext.hpp"
29 
30 #include <functional>
31 
32 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
33 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
34 
35 namespace gui2
36 {
37 
38 // ------------ WIDGET -----------{
39 
41 
42 image::image(const implementation::builder_image& builder)
43  : styled_widget(builder, type())
44 {
45 }
46 
48 {
50 
51  if(image_size.x == 0 || image_size.y == 0) {
52  DBG_GUI_L << LOG_HEADER << " empty image return default.";
53  return get_config_default_size();
54  }
55 
56  const point minimum = get_config_default_size();
57  const point maximum = get_config_maximum_size();
58 
59  point result {image_size.x, image_size.y};
60 
61  if(minimum.x > 0 && result.x < minimum.x) {
62  DBG_GUI_L << LOG_HEADER << " increase width to minimum.";
63  result.x = minimum.x;
64  } else if(maximum.x > 0 && result.x > maximum.x) {
65  DBG_GUI_L << LOG_HEADER << " decrease width to maximum.";
66  result.x = maximum.x;
67  }
68 
69  if(minimum.y > 0 && result.y < minimum.y) {
70  DBG_GUI_L << LOG_HEADER << " increase height to minimum.";
71  result.y = minimum.y;
72  } else if(maximum.y > 0 && result.y > maximum.y) {
73  DBG_GUI_L << LOG_HEADER << " decrease height to maximum.";
74  result.y = maximum.y;
75  }
76 
77  DBG_GUI_L << LOG_HEADER << " result " << result << ".";
78  return result;
79 }
80 
81 void image::set_active(const bool /*active*/)
82 {
83  /* DO NOTHING */
84 }
85 
86 bool image::get_active() const
87 {
88  return true;
89 }
90 
91 unsigned image::get_state() const
92 {
93  return ENABLED;
94 }
95 
97 {
98  return false;
99 }
100 
101 // }---------- DEFINITION ---------{
102 
105 {
106  DBG_GUI_P << "Parsing image " << id;
107 
108  load_resolutions<resolution>(cfg);
109 }
110 
112  : resolution_definition(cfg)
113 {
114  // Note the order should be the same as the enum state_t in image.hpp.
115  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_enabled", _("Missing required state for image control")));
116 }
117 
118 // }---------- BUILDER -----------{
119 
120 namespace implementation
121 {
122 
123 builder_image::builder_image(const config& cfg) : builder_styled_widget(cfg)
124 {
125 }
126 
127 std::unique_ptr<widget> builder_image::build() const
128 {
129  auto widget = std::make_unique<image>(*this);
130 
131  DBG_GUI_G << "Window builder: placed image '" << id << "' with definition '"
132  << definition << "'.";
133 
134  return widget;
135 }
136 
137 } // namespace implementation
138 
139 // }------------ END --------------
140 
141 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: image.cpp:81
virtual bool get_active() const override
See styled_widget::get_active.
Definition: image.cpp:86
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: image.cpp:47
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: image.cpp:96
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: image.cpp:91
Base class for all visible items.
const t_string & get_label() const
point get_config_maximum_size() const
Gets the best size as defined in the config.
point get_config_default_size() const
Gets the default size as defined in the config.
Base class for all widgets.
Definition: widget.hpp:54
Generic locator abstracting the location of an image.
Definition: picture.hpp:64
static std::string _(const char *str)
Definition: gettext.hpp:93
Define the common log macros for the gui toolkit.
#define DBG_GUI_L
Definition: log.hpp:55
#define DBG_GUI_G
Definition: log.hpp:41
#define DBG_GUI_P
Definition: log.hpp:66
#define LOG_HEADER
Definition: image.cpp:33
Generic file dialog.
Functions to load and save images from/to disk.
point get_size(const locator &i_locator, bool skip_cache)
Returns the width and height of an image.
Definition: picture.cpp:827
Contains the implementation details for lexical_cast and shouldn't be used directly.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
This file contains the settings handling of the widget library.
resolution(const config &cfg)
Definition: image.cpp:111
image_definition(const config &cfg)
Definition: image.cpp:103
virtual std::unique_ptr< widget > build() const override
Definition: image.cpp:127
std::string definition
Parameters for the styled_widget.
Base class of a resolution, contains the common keys for a resolution.
std::vector< state_definition > state
Holds a 2D point.
Definition: point.hpp:25
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE_WML_CHILD(cfg, key, message)