The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
image.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 "../../image.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 tpoint timage::calculate_best_size() const
40 {
42 
43  if(!image) {
44  DBG_GUI_L << LOG_HEADER << " empty image return default.\n";
45  return get_config_default_size();
46  }
47 
48  const tpoint minimum = get_config_default_size();
49  const tpoint maximum = get_config_maximum_size();
50 
51  tpoint result = tpoint(image->w, image->h);
52 
53  if(minimum.x > 0 && result.x < minimum.x) {
54  DBG_GUI_L << LOG_HEADER << " increase width to minimum.\n";
55  result.x = minimum.x;
56  } else if(maximum.x > 0 && result.x > maximum.x) {
57  DBG_GUI_L << LOG_HEADER << " decrease width to maximum.\n";
58  result.x = maximum.x;
59  }
60 
61  if(minimum.y > 0 && result.y < minimum.y) {
62  DBG_GUI_L << LOG_HEADER << " increase height to minimum.\n";
63  result.y = minimum.y;
64  } else if(maximum.y > 0 && result.y > maximum.y) {
65  DBG_GUI_L << LOG_HEADER << " decrease height to maximum.\n";
66  result.y = maximum.y;
67  }
68 
69  DBG_GUI_L << LOG_HEADER << " result " << result << ".\n";
70  return result;
71 }
72 
73 void timage::set_active(const bool /*active*/)
74 {
75  /* DO NOTHING */
76 }
77 
78 bool timage::get_active() const
79 {
80  return true;
81 }
82 
83 unsigned timage::get_state() const
84 {
85  return ENABLED;
86 }
87 
88 bool timage::disable_click_dismiss() const
89 {
90  return false;
91 }
92 
93 const std::string& timage::get_control_type() const
94 {
95  static const std::string type = "image";
96  return type;
97 }
98 
99 // }---------- DEFINITION ---------{
100 
102  : tcontrol_definition(cfg)
103 {
104  DBG_GUI_P << "Parsing image " << id << '\n';
105 
106  load_resolutions<tresolution>(cfg);
107 }
108 
109 /*WIKI
110  * @page = GUIWidgetDefinitionWML
111  * @order = 1_image
112  *
113  * == Image ==
114  *
115  * @macro = image_description
116  *
117  * The definition of an image. The label field of the widget is used as the
118  * name of file to show. The widget normally has no event interaction so only
119  * one state is defined.
120  *
121  * The following states exist:
122  * * state_enabled, the image is enabled.
123  * @begin{parent}{name="gui/"}
124  * @begin{tag}{name="image_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
125  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
126  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
127  * @end{tag}{name="state_enabled"}
128  * @end{tag}{name="resolution"}
129  * @end{tag}{name="image_definition"}
130  * @end{parent}{name="gui/"}
131  */
134 {
135  // Note the order should be the same as the enum tstate in image.hpp.
136  state.push_back(tstate_definition(cfg.child("state_enabled")));
137 }
138 
139 // }---------- BUILDER -----------{
140 
141 /*WIKI_MACRO
142  * @begin{macro}{image_description}
143  * An image shows a static image.
144  * @end{macro}
145  */
146 
147 /*WIKI
148  * @page = GUIWidgetInstanceWML
149  * @order = 2_image
150  *
151  * == Image ==
152  *
153  * @macro = image_description
154  *
155  * An image has no extra fields.
156  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
157  * @begin{tag}{name="image"}{min=0}{max=-1}{super="generic/widget_instance"}
158  * @end{tag}{name="image"}
159  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
160  */
161 
162 namespace implementation
163 {
164 
165 tbuilder_image::tbuilder_image(const config& cfg) : tbuilder_control(cfg)
166 {
167 }
168 
170 {
171  timage* widget = new timage();
172 
173  init_control(widget);
174 
175  DBG_GUI_G << "Window builder: placed image '" << id << "' with definition '"
176  << definition << "'.\n";
177 
178  return widget;
179 }
180 
181 } // namespace implementation
182 
183 // }------------ END --------------
184 
185 } // namespace gui2
Define the common log macros for the gui toolkit.
#define DBG_GUI_P
Definition: log.hpp:69
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: image.cpp:878
#define DBG_GUI_L
Definition: log.hpp:58
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
GLenum GLsizei GLenum GLenum const GLvoid * image
Definition: glew.h:3783
tresolution(const config &cfg)
Definition: image.cpp:132
Base class of a resolution, contains the common keys for a resolution.
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
void init_control(tcontrol *control) const
Definition: control.cpp:675
std::string definition
Parameters for the control.
Definition: control.hpp:530
timage_definition(const config &cfg)
Definition: image.cpp:101
GLuint64EXT * result
Definition: glew.h:10727
This file contains the settings handling of the widget library.
int y
y coordinate.
Definition: point.hpp:34
Contains the state info for a resolution.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
int x
x coordinate.
Definition: point.hpp:31
Holds a 2D point.
Definition: point.hpp:24
#define LOG_HEADER
Definition: image.cpp:30
std::vector< tstate_definition > state
config & child(const std::string &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:658
Base class for all widgets.
Definition: widget.hpp:49
this module manages the cache of images.
Definition: image.cpp:75
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
#define DBG_GUI_G
Definition: log.hpp:41
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Contains the implementation details for lexical_cast and shouldn't be used directly.