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