The Battle for Wesnoth  1.15.1+dev
matrix.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 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/matrix.hpp"
18 
20 #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 
37 REGISTER_WIDGET(matrix)
38 
39 
40 state_default::state_default() : state_(ENABLED)
41 {
42 }
43 void state_default::set_active(const bool active)
44 {
45  if(get_active() != active) {
46  state_ = active ? ENABLED : DISABLED;
47  }
48 }
49 
51 {
52  return state_ != DISABLED;
53 }
54 
55 unsigned state_default::get_state() const
56 {
57  return state_;
58 }
59 
61  : tbase(builder, "matrix"), content_(), pane_(nullptr)
62 {
63  const auto cfg = cast_config_to<matrix_definition>();
64 
66  replacements.emplace("_main", builder.builder_main);
67 
68  if(builder.builder_top) {
69  replacements.emplace("_top", builder.builder_top);
70  }
71 
72  if(builder.builder_left) {
73  replacements.emplace("_left", builder.builder_left);
74  }
75 
76  if(builder.builder_right) {
77  replacements.emplace("_right", builder.builder_right);
78  }
79 
80  if(builder.builder_bottom) {
81  replacements.emplace("_bottom", builder.builder_bottom);
82  }
83 
84  cfg->content->build(content_, replacements);
85  content_.set_parent(this);
86 
87  pane_ = find_widget<pane>(&content_, "pane", false, true);
88 }
89 
91 {
92  return new matrix(builder);
93 }
94 
95 unsigned
96 matrix::create_item(const std::map<std::string, string_map>& item_data,
97  const std::map<std::string, std::string>& tags)
98 {
99  return pane_->create_item(item_data, tags);
100 }
101 
102 void matrix::place(const point& origin, const point& size)
103 {
104  widget::place(origin, size);
105 
106  content_.place(origin, size);
107 }
108 
109 void matrix::layout_initialize(const bool full_initialization)
110 {
111  content_.layout_initialize(full_initialization);
112 }
113 
114 void
115 matrix::impl_draw_children(surface& frame_buffer, int x_offset, int y_offset)
116 {
117  content_.draw_children(frame_buffer, x_offset, y_offset);
118 }
119 
121 {
123 }
124 
126  const std::vector<widget*>& call_stack)
127 {
128  std::vector<widget*> child_call_stack = call_stack;
129  content_.populate_dirty_list(caller, child_call_stack);
130 }
131 
132 void matrix::request_reduce_width(const unsigned /*maximum_width*/)
133 {
134 }
135 
136 widget* matrix::find_at(const point& coordinate, const bool must_be_active)
137 {
138  return content_.find_at(coordinate, must_be_active);
139 }
140 
142  const bool must_be_active) const
143 {
144  return content_.find_at(coordinate, must_be_active);
145 }
146 
147 widget* matrix::find(const std::string& id, const bool must_be_active)
148 {
149  if(widget* result = widget::find(id, must_be_active)) {
150  return result;
151  } else {
152  return content_.find(id, must_be_active);
153  }
154 }
155 
156 const widget* matrix::find(const std::string& id, const bool must_be_active)
157  const
158 {
159  if(const widget* result = widget::find(id, must_be_active)) {
160  return result;
161  } else {
162  return content_.find(id, must_be_active);
163  }
164 }
165 
167 {
169 
170  return size;
171 }
172 
174 {
175  return false;
176 }
177 
179 {
180  /**
181  * @todo Implement properly.
182  */
183  return nullptr;
184 }
185 
186 // }---------- DEFINITION ---------{
187 
188 /*WIKI
189  * @page = GUIWidgetDefinitionWML
190  * @order = 1_matrix
191  * @begin{parent}{name="gui/"}
192  * @begin{tag}{name="matrix_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
193  * == Listbox ==
194  *
195  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super=generic/widget_definition/resolution}
196  *
197  *
198  * @begin{tag}{name="state_enabled"}{min=1}{max=1}{super="generic/state"}
199  * @end{tag}{name="state_enabled"}
200  * @begin{tag}{name="state_disabled"}{min=1}{max=1}{super="generic/state"}
201  * @end{tag}{name="state_disabled"}
202  * @begin{tag}{name="content"}{min=1}{max=1}{super="gui/window/resolution/grid"}
203  * @end{tag}{name="content"}
204  * @end{tag}{name="resolution"}
205  * @end{tag}{name="matrix_definition"}
206  * @end{parent}{name="gui/"}
207  */
208 
211 {
212  DBG_GUI_P << "Parsing matrix " << id << '\n';
213 
214  load_resolutions<resolution>(cfg);
215 }
216 
218  : resolution_definition(cfg)
219  , content(new builder_grid(cfg.child("content", "[matrix_definition]")))
220 {
221  // Note the order should be the same as the enum state_t in matrix.hpp.
222  state.emplace_back(cfg.child("state_enabled"));
223  state.emplace_back(cfg.child("state_disabled"));
224 }
225 
226 // }---------- BUILDER -----------{
227 
228 /*WIKI
229  * @page = GUIWidgetInstanceWML
230  * @order = 2_matrix
231  *
232  * == Listbox ==
233  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
234  * @begin{tag}{name="matrix"}{min=0}{max=-1}{super="generic/widget_instance"}
235  *
236  *
237  * List with the matrix specific variables:
238  * @begin{table}{config}
239  * vertical_scrollbar_mode & scrollbar_mode & initial_auto &
240  * Determines whether or not to show the
241  * scrollbar. $
242  * horizontal_scrollbar_mode & scrollbar_mode & initial_auto &
243  * Determines whether or not to show the
244  * scrollbar. $
245  * @end{table}
246  *
247  *
248  * @begin{tag}{name="top"}{min=0}{max=1}{super="gui/window/resolution/grid"}
249  * @end{tag}{name="top"}
250  * @begin{tag}{name="bottom"}{min=0}{max=1}{super="gui/window/resolution/grid"}
251  * @end{tag}{name="bottom"}
252  *
253  * @begin{tag}{name="left"}{min=0}{max=1}{super="gui/window/resolution/grid"}
254  * @end{tag}{name="left"}
255  * @begin{tag}{name="right"}{min=0}{max=1}{super="gui/window/resolution/grid"}
256  * @end{tag}{name="right"}
257  *
258  * @begin{tag}{name="main"}{min="1"}{max="1"}{super="gui/window/resolution/grid/row/column"}
259  * @end{tag}{name="main"}
260  * @end{tag}{name="matrix"}
261  *
262  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
263  */
264 
265 namespace implementation
266 {
267 
268 builder_matrix::builder_matrix(const config& cfg)
269  : builder_styled_widget(cfg)
270  , vertical_scrollbar_mode(
271  get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
272  , horizontal_scrollbar_mode(
273  get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
274  , builder_top(nullptr)
275  , builder_bottom(nullptr)
276  , builder_left(nullptr)
277  , builder_right(nullptr)
278  , builder_main(create_widget_builder(cfg.child("main", "[matrix]")))
279 {
280  if(const config& top = cfg.child("top")) {
281  builder_top = std::make_shared<builder_grid>(top);
282  }
283 
284  if(const config& bottom = cfg.child("bottom")) {
285  builder_bottom = std::make_shared<builder_grid>(bottom);
286  }
287 
288  if(const config& left = cfg.child("left")) {
289  builder_left = std::make_shared<builder_grid>(left);
290  }
291 
292  if(const config& right = cfg.child("right")) {
293  builder_right = std::make_shared<builder_grid>(right);
294  }
295 }
296 
298 {
299  return matrix::build(*this);
300 }
301 
302 } // namespace implementation
303 
304 // }------------ END --------------
305 
306 } // 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
virtual void impl_draw_children(surface &frame_buffer, int x_offset, int y_offset) override
See widget::impl_draw_children.
Definition: matrix.cpp:115
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:420
virtual void layout_children() override
See widget::layout_children.
Definition: matrix.cpp:120
void set_parent(widget *parent)
Definition: widget.cpp:152
std::vector< state_definition > state
resolution(const config &cfg)
Definition: matrix.cpp:217
virtual widget * find(const std::string &id, const bool must_be_active)
Returns a widget with the wanted id.
Definition: widget.cpp:581
virtual void child_populate_dirty_list(window &caller, const std::vector< widget *> &call_stack) override
See widget::child_populate_dirty_list.
Definition: matrix.cpp:125
The matrix class.
Definition: matrix.hpp:99
unsigned create_item(const std::map< std::string, string_map > &item_data, const std::map< std::string, std::string > &tags)
Creates a new item.
Definition: pane.cpp:138
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: matrix.cpp:147
matrix_definition(const config &cfg)
Definition: matrix.cpp:209
Base class for all widgets.
Definition: widget.hpp:47
grid content_
The grid containing our children.
Definition: matrix.hpp:218
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
Definition: matrix.cpp:136
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: matrix.cpp:132
unsigned create_item(const std::map< std::string, string_map > &item_data, const std::map< std::string, std::string > &tags)
Definition: matrix.cpp:96
Generic file dialog.
Definition: field-fwd.hpp:22
matrix(const implementation::builder_matrix &builder)
Definition: matrix.cpp:60
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: grid.cpp:185
void populate_dirty_list(window &caller, std::vector< widget *> &call_stack)
Adds a widget to the dirty list if it is dirty.
Definition: widget.cpp:414
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: matrix.cpp:109
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
std::map< std::string, std::shared_ptr< builder_widget > > replacements_map
The replacements type is used to define replacement types.
point get_best_size() const
Gets the best size for the widget.
Definition: widget.cpp:190
This file contains the settings handling of the widget library.
virtual void place(const point &origin, const point &size)
Places the widget.
Definition: widget.cpp:235
The walker abstract base class.
Definition: walker.hpp:26
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:655
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: matrix.cpp:173
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
static matrix * build(const implementation::builder_matrix &builder)
Definition: matrix.cpp:90
lu_byte right
Definition: lparser.cpp:1027
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: grid.cpp:479
state_t state_
Current state of the widget.
Definition: matrix.hpp:63
bool get_active() const
Definition: matrix.cpp:50
Holds a 2D point.
Definition: point.hpp:23
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: matrix.cpp:102
scrollbar_mode get_scrollbar_mode(const std::string &scrollbar_mode)
Returns the scrollbar mode flags.
Definition: helper.cpp:120
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
Definition: grid.cpp:642
unsigned get_state() const
Definition: matrix.cpp:55
builder_widget_ptr builder_main
Definition: matrix.hpp:273
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: matrix.cpp:166
virtual void layout_children() override
See widget::layout_children.
Definition: grid.cpp:618
virtual iteration::walker_base * create_walker() override
See widget::create_walker.
Definition: matrix.cpp:178
lu_byte left
Definition: lparser.cpp:1026
pane * pane_
Contains the pane used for adding new items to the matrix.
Definition: matrix.hpp:225
void draw_children(surface &frame_buffer, int x_offset, int y_offset)
Draws the children of a widget.
Definition: widget.cpp:384
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
void set_active(const bool active)
Definition: matrix.cpp:43
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
static std::deque< std::string > call_stack
Definition: function.cpp:39
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
builder_widget_ptr create_widget_builder(const config &cfg)
Create a widget builder.