The Battle for Wesnoth  1.15.0-dev
drawing.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 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/drawing.hpp"
18 
21 
23 
24 #include "utils/functional.hpp"
25 
26 namespace gui2
27 {
28 
29 // ------------ WIDGET -----------{
30 
31 REGISTER_WIDGET(drawing)
32 
33 drawing::drawing(const implementation::builder_drawing& builder)
34  : styled_widget(builder, type())
35  , best_size_(0, 0)
36 {
37 }
38 
40 {
41  return best_size_ != point() ? best_size_
43 }
44 
45 void drawing::request_reduce_width(const unsigned maximum_width)
46 {
47  if(best_size_ != point()) {
48  // This drawing is of fixed size, do nothing.
49  } else {
51  }
52 }
53 
54 void drawing::request_reduce_height(const unsigned maximum_height)
55 {
56  if(best_size_ != point()) {
57  // This drawing is of fixed size, do nothing.
58  } else {
60  }
61 }
62 
63 void drawing::set_active(const bool /*active*/)
64 {
65  /* DO NOTHING */
66 }
67 
68 bool drawing::get_active() const
69 {
70  return true;
71 }
72 
73 unsigned drawing::get_state() const
74 {
75  return 0;
76 }
77 
79 {
80  return false;
81 }
82 
83 // }---------- DEFINITION ---------{
84 
87 {
88  DBG_GUI_P << "Parsing drawing " << id << '\n';
89 
90  load_resolutions<resolution>(cfg);
91 }
92 
93 /*WIKI
94  * @page = GUIWidgetDefinitionWML
95  * @order = 1_drawing
96  *
97  * == Drawing ==
98  *
99  * @macro = drawing_description
100  *
101  * The definition of a drawing. The widget normally has no event interaction
102  * so only one state is defined.
103  *
104  * The following states exist:
105  * * state_enabled
106  * the drawing is enabled. The state is a dummy since the
107  * things really drawn are placed in the window instance.
108  * @begin{parent}{name="gui/"}
109  * @begin{tag}{name="drawing_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
110  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
111  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
112  * @end{tag}{name="state_enabled"}
113  * @end{tag}{name="resolution"}
114  * @end{tag}{name="drawing_definition"}
115  * @end{parent}{name="gui/"}
116  */
118  : resolution_definition(cfg)
119 {
120  /*
121  * Note the order should be the same as the enum state_t in drawing.hpp.
122  * Normally the [draw] section is in the config, but for this widget the
123  * original draw section is ignored, so send a dummy.
124  */
125  static const config dummy("draw");
126  state.emplace_back(dummy);
127 }
128 
129 // }---------- BUILDER -----------{
130 
131 /*WIKI_MACRO
132  * @begin{macro}{drawing_description}
133  *
134  * A drawing is widget with a fixed size and gives access to the
135  * canvas of the widget in the window instance. This allows special
136  * display only widgets.
137  * @end{macro}
138  */
139 
140 /*WIKI
141  * @page = GUIWidgetInstanceWML
142  * @order = 2_drawing
143  *
144  * == Spacer ==
145  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
146  * @begin{tag}{name="drawing"}{min=0}{max=-1}{super="generic/widget_instance"}
147  * @macro = drawing_description
148  *
149  * If either the width or the height is not zero the drawing functions as a
150  * fixed size drawing.
151  *
152  * @begin{table}{config}
153  * width & f_unsigned & 0 & The width of the drawing. $
154  * height & f_unsigned & 0 & The height of the drawing. $
155  * draw & config & & The config containing the drawing. $
156  * @end{table}
157  * @allow{link}{name="generic/state/draw"}
158  * @end{tag}{name="drawing"}
159  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
160  * The variable available are the same as for the window resolution see
161  * https://www.wesnoth.org/wiki/GUIToolkitWML#Resolution_2 for the list of
162  * items.
163  */
164 
165 namespace implementation
166 {
167 
168 builder_drawing::builder_drawing(const config& cfg)
169  : builder_styled_widget(cfg)
170  , width(cfg["width"])
171  , height(cfg["height"])
172  , draw(cfg.child("draw"))
173 {
174  assert(!draw.empty());
175 }
176 
178 {
179  auto widget = std::make_shared<drawing>(*this);
180 
182 
183  const unsigned w = width(size);
184  const unsigned h = height(size);
185 
186  if(w || h) {
187  widget->set_best_size(point(w, h));
188  }
189 
190  widget->set_drawing_data(draw);
191 
192  DBG_GUI_G << "Window builder: placed drawing '" << id
193  << "' with definition '" << definition << "'.\n";
194 
195  return widget;
196 }
197 
198 } // namespace implementation
199 
200 // }------------ END --------------
201 
202 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:68
std::vector< state_definition > state
int dummy
Definition: lstrlib.cpp:1125
virtual widget_ptr build() const override
Definition: drawing.cpp:177
Base class for all widgets.
Definition: widget.hpp:48
point best_size_
When we&#39;re used as a fixed size item, this holds the best size.
Definition: drawing.hpp:104
A widget to draw upon.
Definition: drawing.hpp:38
#define h
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: drawing.cpp:39
virtual point calculate_best_size() const override
See widget::calculate_best_size.
void get_screen_size_variables(wfl::map_formula_callable &variable)
Gets a formula object with the screen size.
Definition: helper.cpp:99
Generic file dialog.
Definition: field-fwd.hpp:22
std::string definition
Parameters for the styled_widget.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
std::shared_ptr< widget > widget_ptr
Definition: widget.hpp:732
virtual void request_reduce_height(const unsigned maximum_height) override
See widget::request_reduce_height.
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: drawing.cpp:45
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
typed_formula< unsigned > height
The height of the widget.
Definition: drawing.hpp:144
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: drawing.cpp:63
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: drawing.cpp:78
Holds a 2D point.
Definition: point.hpp:23
int w
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Base class for all visible items.
virtual bool get_active() const override
See styled_widget::get_active.
Definition: drawing.cpp:68
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: drawing.cpp:73
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
virtual void request_reduce_height(const unsigned maximum_height) override
See widget::request_reduce_height.
Definition: drawing.cpp:54
#define DBG_GUI_G
Definition: log.hpp:40
drawing_definition(const config &cfg)
Definition: drawing.cpp:85
bool empty() const
Definition: config.cpp:837
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
config draw
Config containing what to draw on the widgets canvas.
Definition: drawing.hpp:147
typed_formula< unsigned > width
The width of the widget.
Definition: drawing.hpp:141