The Battle for Wesnoth  1.15.0-dev
image.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include "gui/widgets/image.hpp"
18 
19 #include "picture.hpp" // We want the file in src/
20 
23 #include "gui/core/log.hpp"
25 
26 #include "utils/functional.hpp"
27 
28 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
29 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
30 
31 namespace gui2
32 {
33 
34 // ------------ WIDGET -----------{
35 
37 
38 image::image(const implementation::builder_image& builder)
39  : styled_widget(builder, type())
40 {
41 }
42 
44 {
46 
47  if(!image) {
48  DBG_GUI_L << LOG_HEADER << " empty image return default.\n";
49  return get_config_default_size();
50  }
51 
52  const point minimum = get_config_default_size();
53  const point maximum = get_config_maximum_size();
54 
55  point result {image->w, image->h};
56 
57  if(minimum.x > 0 && result.x < minimum.x) {
58  DBG_GUI_L << LOG_HEADER << " increase width to minimum.\n";
59  result.x = minimum.x;
60  } else if(maximum.x > 0 && result.x > maximum.x) {
61  DBG_GUI_L << LOG_HEADER << " decrease width to maximum.\n";
62  result.x = maximum.x;
63  }
64 
65  if(minimum.y > 0 && result.y < minimum.y) {
66  DBG_GUI_L << LOG_HEADER << " increase height to minimum.\n";
67  result.y = minimum.y;
68  } else if(maximum.y > 0 && result.y > maximum.y) {
69  DBG_GUI_L << LOG_HEADER << " decrease height to maximum.\n";
70  result.y = maximum.y;
71  }
72 
73  DBG_GUI_L << LOG_HEADER << " result " << result << ".\n";
74  return result;
75 }
76 
77 void image::set_active(const bool /*active*/)
78 {
79  /* DO NOTHING */
80 }
81 
82 bool image::get_active() const
83 {
84  return true;
85 }
86 
87 unsigned image::get_state() const
88 {
89  return ENABLED;
90 }
91 
93 {
94  return false;
95 }
96 
97 // }---------- DEFINITION ---------{
98 
101 {
102  DBG_GUI_P << "Parsing image " << id << '\n';
103 
104  load_resolutions<resolution>(cfg);
105 }
106 
107 /*WIKI
108  * @page = GUIWidgetDefinitionWML
109  * @order = 1_image
110  *
111  * == Image ==
112  *
113  * @macro = image_description
114  *
115  * The definition of an image. The label field of the widget is used as the
116  * name of file to show. The widget normally has no event interaction so only
117  * one state is defined.
118  *
119  * The following states exist:
120  * * state_enabled, the image is enabled.
121  * @begin{parent}{name="gui/"}
122  * @begin{tag}{name="image_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
123  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
124  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
125  * @end{tag}{name="state_enabled"}
126  * @end{tag}{name="resolution"}
127  * @end{tag}{name="image_definition"}
128  * @end{parent}{name="gui/"}
129  */
131  : resolution_definition(cfg)
132 {
133  // Note the order should be the same as the enum state_t in image.hpp.
134  state.emplace_back(cfg.child("state_enabled"));
135 }
136 
137 // }---------- BUILDER -----------{
138 
139 /*WIKI_MACRO
140  * @begin{macro}{image_description}
141  * An image shows a static image.
142  * @end{macro}
143  */
144 
145 /*WIKI
146  * @page = GUIWidgetInstanceWML
147  * @order = 2_image
148  *
149  * == Image ==
150  *
151  * @macro = image_description
152  *
153  * An image has no extra fields.
154  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
155  * @begin{tag}{name="image"}{min=0}{max=-1}{super="generic/widget_instance"}
156  * @end{tag}{name="image"}
157  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
158  */
159 
160 namespace implementation
161 {
162 
163 builder_image::builder_image(const config& cfg) : builder_styled_widget(cfg)
164 {
165 }
166 
168 {
169  auto widget = std::make_shared<image>(*this);
170 
171  DBG_GUI_G << "Window builder: placed image '" << id << "' with definition '"
172  << definition << "'.\n";
173 
174  return widget;
175 }
176 
177 } // namespace implementation
178 
179 // }------------ END --------------
180 
181 } // namespace gui2
Define the common log macros for the gui toolkit.
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:68
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:423
std::vector< state_definition > state
#define DBG_GUI_L
Definition: log.hpp:57
Base class for all widgets.
Definition: widget.hpp:48
t_string get_image() const
Wrapper for label.
Definition: image.hpp:58
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: image.cpp:77
int x
x coordinate.
Definition: point.hpp:44
Generic file dialog.
Definition: field-fwd.hpp:22
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: image.cpp:87
std::string definition
Parameters for the styled_widget.
std::shared_ptr< widget > widget_ptr
Definition: widget.hpp:732
virtual widget_ptr build() const override
Definition: image.cpp:167
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
image_definition(const config &cfg)
Definition: image.cpp:99
point get_config_default_size() const
Gets the default size as defined in the config.
#define LOG_HEADER
Definition: image.cpp:29
resolution(const config &cfg)
Definition: image.cpp:130
Holds a 2D point.
Definition: point.hpp:23
Base class for all visible items.
virtual bool get_active() const override
See styled_widget::get_active.
Definition: image.cpp:82
const t_string & get_label() const
this module manages the cache of images.
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: image.cpp:43
image(const implementation::builder_image &builder)
Definition: image.cpp:38
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
int y
y coordinate.
Definition: point.hpp:47
#define DBG_GUI_G
Definition: log.hpp:40
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
point get_config_maximum_size() const
Gets the best size as defined in the config.
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: image.cpp:92