The Battle for Wesnoth  1.15.1+dev
spacer.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/spacer.hpp"
18 
20 #include "gui/widgets/settings.hpp"
21 
22 #include "utils/functional.hpp"
23 
24 namespace gui2
25 {
26 
27 // ------------ WIDGET -----------{
28 
29 REGISTER_WIDGET(spacer)
30 
31 spacer::spacer(const implementation::builder_spacer& builder, const std::string& w, const std::string& h)
32  : styled_widget(builder, type())
33  , width_(w)
34  , height_(h)
35 {
36 }
37 
39 {
40  return (!width_.has_formula() && width_() == 0) && (!height_.has_formula() && height_() == 0);
41 }
42 
43 void spacer::request_reduce_width(const unsigned maximum_width)
44 {
45  // Do nothing unless this widget fills all available space (has non-size size).
46  if(fills_available_space()) {
48  }
49 }
50 
51 void spacer::request_reduce_height(const unsigned maximum_height)
52 {
53  // Do nothing unless this widget fills all available space (has non-size size).
54  if(fills_available_space()) {
56  }
57 }
58 
60 {
62 
63  unsigned width = width_(size);
64  unsigned height = height_(size);
65 
66  point best_size;
67 
68  if(width || height) {
69  best_size = point(width, height);
70  }
71 
72  if(best_size != point()) {
73  return best_size;
74  }
75 
77 }
78 
79 void spacer::set_active(const bool /*active*/)
80 {
81  /* DO NOTHING */
82 }
83 
84 bool spacer::get_active() const
85 {
86  return true;
87 }
88 
89 unsigned spacer::get_state() const
90 {
91  return 0;
92 }
93 
95 {
96  return false;
97 }
98 
99 void spacer::impl_draw_background(surface& /*frame_buffer*/
100  ,
101  int /*x_offset*/
102  ,
103  int /*y_offset*/)
104 {
105  /* DO NOTHING */
106 }
107 
108 // }---------- DEFINITION ---------{
109 
112 {
113  DBG_GUI_P << "Parsing spacer " << id << '\n';
114 
115  load_resolutions<resolution>(cfg);
116 }
117 
118 /*WIKI
119  * @page = GUIWidgetDefinitionWML
120  * @order = 1_spacer
121  *
122  * == Spacer ==
123  *
124  * @macro = spacer_description
125  *
126  * A spacer has no states so nothing to load.
127  * @begin{parent}{name="gui/"}
128  * @begin{tag}{name="spacer_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
129  * @end{tag}{name="spacer_definition"}
130  * @end{parent}{name="gui/"}
131  */
133  : resolution_definition(cfg)
134 {
135 }
136 
137 // }---------- BUILDER -----------{
138 
139 /*WIKI_MACRO
140  * @begin{macro}{spacer_description}
141  *
142  * A spacer is a dummy item to either fill in a widget since no empty
143  * items are allowed or to reserve a fixed space.
144  * @end{macro}
145  */
146 
147 
148 /*WIKI
149  * @page = GUIWidgetInstanceWML
150  * @order = 2_spacer
151  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
152  * @begin{tag}{name="spacer"}{min=0}{max=-1}{super="generic/widget_instance"}
153  * == Spacer ==
154  *
155  * @macro = spacer_description
156  *
157  * If either the width or the height is non-zero the spacer functions as a
158  * fixed size spacer.
159  *
160  * @begin{table}{config}
161  * width & f_unsigned & 0 & The width of the spacer. $
162  * height & f_unsigned & 0 & The height of the spacer. $
163  * @end{table}
164  *
165  * The variable available are the same as for the window resolution see
166  * https://www.wesnoth.org/wiki/GUIToolkitWML#Resolution_2 for the list of
167  * items.
168  * @end{tag}{name="spacer"}
169  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
170  */
171 
172 namespace implementation
173 {
174 
175 builder_spacer::builder_spacer(const config& cfg)
176  : builder_styled_widget(cfg), width_(cfg["width"]), height_(cfg["height"])
177 {
178 }
179 
181 {
182  spacer* widget = new spacer(*this, width_, height_);
183 
184  DBG_GUI_G << "Window builder: placed spacer '" << id
185  << "' with definition '" << definition << "'.\n";
186 
187  return widget;
188 }
189 
190 } // namespace implementation
191 
192 // }------------ END --------------
193 
194 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:68
virtual bool get_active() const override
See styled_widget::get_active.
Definition: spacer.cpp:84
Base class for all widgets.
Definition: widget.hpp:47
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: spacer.cpp:59
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: spacer.cpp:89
STL namespace.
#define h
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
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: spacer.cpp:43
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.
bool fills_available_space()
Definition: spacer.cpp:38
resolution(const config &cfg)
Definition: spacer.cpp:132
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: spacer.cpp:94
spacer_definition(const config &cfg)
Definition: spacer.cpp:110
Holds a 2D point.
Definition: point.hpp:23
typed_formula< unsigned > width_
Definition: spacer.hpp:76
int w
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Base class for all visible items.
virtual void request_reduce_height(const unsigned maximum_height) override
See widget::request_reduce_height.
Definition: spacer.cpp:51
typed_formula< unsigned > height_
Definition: spacer.hpp:77
An empty widget.
Definition: spacer.hpp:41
bool has_formula() const
Determine whether the class contains a formula.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
#define DBG_GUI_G
Definition: log.hpp:40
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: spacer.cpp:79
virtual void impl_draw_background(surface &frame_buffer, int x_offset, int y_offset) override
See widget::impl_draw_background.
Definition: spacer.cpp:99
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.