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 #include "gui/widgets/settings.hpp"
26 
27 #include "utils/functional.hpp"
28 
29 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
30 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
31 
32 namespace gui2
33 {
34 
35 // ------------ WIDGET -----------{
36 
38 
39 image::image(const implementation::builder_image& builder)
40  : styled_widget(builder, type())
41 {
42 }
43 
45 {
47 
48  if(!image) {
49  DBG_GUI_L << LOG_HEADER << " empty image return default.\n";
50  return get_config_default_size();
51  }
52 
53  const point minimum = get_config_default_size();
54  const point maximum = get_config_maximum_size();
55 
56  point result {image->w, image->h};
57 
58  if(minimum.x > 0 && result.x < minimum.x) {
59  DBG_GUI_L << LOG_HEADER << " increase width to minimum.\n";
60  result.x = minimum.x;
61  } else if(maximum.x > 0 && result.x > maximum.x) {
62  DBG_GUI_L << LOG_HEADER << " decrease width to maximum.\n";
63  result.x = maximum.x;
64  }
65 
66  if(minimum.y > 0 && result.y < minimum.y) {
67  DBG_GUI_L << LOG_HEADER << " increase height to minimum.\n";
68  result.y = minimum.y;
69  } else if(maximum.y > 0 && result.y > maximum.y) {
70  DBG_GUI_L << LOG_HEADER << " decrease height to maximum.\n";
71  result.y = maximum.y;
72  }
73 
74  DBG_GUI_L << LOG_HEADER << " result " << result << ".\n";
75  return result;
76 }
77 
78 void image::set_active(const bool /*active*/)
79 {
80  /* DO NOTHING */
81 }
82 
83 bool image::get_active() const
84 {
85  return true;
86 }
87 
88 unsigned image::get_state() const
89 {
90  return ENABLED;
91 }
92 
94 {
95  return false;
96 }
97 
98 // }---------- DEFINITION ---------{
99 
102 {
103  DBG_GUI_P << "Parsing image " << id << '\n';
104 
105  load_resolutions<resolution>(cfg);
106 }
107 
108 /*WIKI
109  * @page = GUIWidgetDefinitionWML
110  * @order = 1_image
111  *
112  * == Image ==
113  *
114  * @macro = image_description
115  *
116  * The definition of an image. The label field of the widget is used as the
117  * name of file to show. The widget normally has no event interaction so only
118  * one state is defined.
119  *
120  * The following states exist:
121  * * state_enabled, the image is enabled.
122  * @begin{parent}{name="gui/"}
123  * @begin{tag}{name="image_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
124  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
125  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
126  * @end{tag}{name="state_enabled"}
127  * @end{tag}{name="resolution"}
128  * @end{tag}{name="image_definition"}
129  * @end{parent}{name="gui/"}
130  */
132  : resolution_definition(cfg)
133 {
134  // Note the order should be the same as the enum state_t in image.hpp.
135  state.emplace_back(cfg.child("state_enabled"));
136 }
137 
138 // }---------- BUILDER -----------{
139 
140 /*WIKI_MACRO
141  * @begin{macro}{image_description}
142  * An image shows a static image.
143  * @end{macro}
144  */
145 
146 /*WIKI
147  * @page = GUIWidgetInstanceWML
148  * @order = 2_image
149  *
150  * == Image ==
151  *
152  * @macro = image_description
153  *
154  * An image has no extra fields.
155  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
156  * @begin{tag}{name="image"}{min=0}{max=-1}{super="generic/widget_instance"}
157  * @end{tag}{name="image"}
158  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
159  */
160 
161 namespace implementation
162 {
163 
164 builder_image::builder_image(const config& cfg) : builder_styled_widget(cfg)
165 {
166 }
167 
169 {
170  image* widget = new image(*this);
171 
172  DBG_GUI_G << "Window builder: placed image '" << id << "' with definition '"
173  << definition << "'.\n";
174 
175  return widget;
176 }
177 
178 } // namespace implementation
179 
180 // }------------ END --------------
181 
182 } // 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:47
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:78
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:88
std::string definition
Parameters for the styled_widget.
This file contains the settings handling of the widget library.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
image_definition(const config &cfg)
Definition: image.cpp:100
point get_config_default_size() const
Gets the default size as defined in the config.
#define LOG_HEADER
Definition: image.cpp:30
resolution(const config &cfg)
Definition: image.cpp:131
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:83
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:44
image(const implementation::builder_image &builder)
Definition: image.cpp:39
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
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:93