The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
matrix.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 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/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 
38 
39 
40 tstate_default::tstate_default() : state_(ENABLED)
41 {
42 }
43 void tstate_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 tstate_default::get_state() const
56 {
57  return state_;
58 }
59 
61  : tbase(builder, get_control_type()), content_(), pane_(nullptr)
62 {
64  cfg = boost::dynamic_pointer_cast<const tmatrix_definition::tresolution>(
65  config());
66 
67  tbuilder_widget::treplacements replacements;
68  replacements.insert(std::make_pair("_main", builder.builder_main));
69 
70  if(builder.builder_top) {
71  replacements.insert(std::make_pair("_top", builder.builder_top));
72  }
73 
74  if(builder.builder_left) {
75  replacements.insert(std::make_pair("_left", builder.builder_left));
76  }
77 
78  if(builder.builder_right) {
79  replacements.insert(std::make_pair("_right", builder.builder_right));
80  }
81 
82  if(builder.builder_bottom) {
83  replacements.insert(std::make_pair("_bottom", builder.builder_bottom));
84  }
85 
86  cfg->content->build(content_, replacements);
87  content_.set_parent(this);
88 
89  pane_ = find_widget<tpane>(&content_, "pane", false, true);
90 }
91 
93 {
94  return new tmatrix(builder);
95 }
96 
97 unsigned
98 tmatrix::create_item(const std::map<std::string, string_map>& item_data,
99  const std::map<std::string, std::string>& tags)
100 {
101  return pane_->create_item(item_data, tags);
102 }
103 
104 void tmatrix::place(const tpoint& origin, const tpoint& size)
105 {
106  twidget::place(origin, size);
107 
108  content_.place(origin, size);
109 }
110 
111 void tmatrix::layout_initialise(const bool full_initialisation)
112 {
113  content_.layout_initialise(full_initialisation);
114 }
115 
116 void
117 tmatrix::impl_draw_children(surface& frame_buffer, int x_offset, int y_offset)
118 {
119  content_.draw_children(frame_buffer, x_offset, y_offset);
120 }
121 
123 {
125 }
126 
128  const std::vector<twidget*>& call_stack)
129 {
130  std::vector<twidget*> child_call_stack = call_stack;
131  content_.populate_dirty_list(caller, child_call_stack);
132 }
133 
134 void tmatrix::request_reduce_width(const unsigned /*maximum_width*/)
135 {
136 }
137 
138 twidget* tmatrix::find_at(const tpoint& coordinate, const bool must_be_active)
139 {
140  return content_.find_at(coordinate, must_be_active);
141 }
142 
143 const twidget* tmatrix::find_at(const tpoint& coordinate,
144  const bool must_be_active) const
145 {
146  return content_.find_at(coordinate, must_be_active);
147 }
148 
149 twidget* tmatrix::find(const std::string& id, const bool must_be_active)
150 {
151  if(twidget* result = twidget::find(id, must_be_active)) {
152  return result;
153  } else {
154  return content_.find(id, must_be_active);
155  }
156 }
157 
158 const twidget* tmatrix::find(const std::string& id, const bool must_be_active)
159  const
160 {
161  if(const twidget* result = twidget::find(id, must_be_active)) {
162  return result;
163  } else {
164  return content_.find(id, must_be_active);
165  }
166 }
167 
169 {
171 
172  return size;
173 }
174 
176 {
177  return false;
178 }
179 
181 {
182  /**
183  * @todo Implement properly.
184  */
185  return nullptr;
186 }
187 
189 {
190  static const std::string type = "matrix";
191  return type;
192 }
193 
194 // }---------- DEFINITION ---------{
195 
196 /*WIKI
197  * @page = GUIWidgetDefinitionWML
198  * @order = 1_matrix
199  * @begin{parent}{name="gui/"}
200  * @begin{tag}{name="matrix_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
201  * == Listbox ==
202  *
203  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super=generic/widget_definition/resolution}
204  *
205  *
206  * @begin{tag}{name="state_enabled"}{min=1}{max=1}{super="generic/state"}
207  * @end{tag}{name="state_enabled"}
208  * @begin{tag}{name="state_disabled"}{min=1}{max=1}{super="generic/state"}
209  * @end{tag}{name="state_disabled"}
210  * @begin{tag}{name="content"}{min=1}{max=1}{super="gui/window/resolution/grid"}
211  * @end{tag}{name="content"}
212  * @end{tag}{name="resolution"}
213  * @end{tag}{name="matrix_definition"}
214  * @end{parent}{name="gui/"}
215  */
216 
218  : tcontrol_definition(cfg)
219 {
220  DBG_GUI_P << "Parsing matrix " << id << '\n';
221 
222  load_resolutions<tresolution>(cfg);
223 }
224 
227  , content(new tbuilder_grid(cfg.child("content", "[matrix_definition]")))
228 {
229  // Note the order should be the same as the enum tstate in matrix.hpp.
230  state.push_back(tstate_definition(cfg.child("state_enabled")));
231  state.push_back(tstate_definition(cfg.child("state_disabled")));
232 }
233 
234 // }---------- BUILDER -----------{
235 
236 /*WIKI
237  * @page = GUIWidgetInstanceWML
238  * @order = 2_matrix
239  *
240  * == Listbox ==
241  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
242  * @begin{tag}{name="matrix"}{min=0}{max=-1}{super="generic/widget_instance"}
243  *
244  *
245  * List with the matrix specific variables:
246  * @begin{table}{config}
247  * vertical_scrollbar_mode & scrollbar_mode & initial_auto &
248  * Determines whether or not to show the
249  * scrollbar. $
250  * horizontal_scrollbar_mode & scrollbar_mode & initial_auto &
251  * Determines whether or not to show the
252  * scrollbar. $
253  * @end{table}
254  *
255  *
256  * @begin{tag}{name="top"}{min=0}{max=1}{super="gui/window/resolution/grid"}
257  * @end{tag}{name="top"}
258  * @begin{tag}{name="bottom"}{min=0}{max=1}{super="gui/window/resolution/grid"}
259  * @end{tag}{name="bottom"}
260  *
261  * @begin{tag}{name="left"}{min=0}{max=1}{super="gui/window/resolution/grid"}
262  * @end{tag}{name="left"}
263  * @begin{tag}{name="right"}{min=0}{max=1}{super="gui/window/resolution/grid"}
264  * @end{tag}{name="right"}
265  *
266  * @begin{tag}{name="main"}{min="1"}{max="1"}{super="gui/window/resolution/grid/row/column"}
267  * @end{tag}{name="main"}
268  * @end{tag}{name="matrix"}
269  *
270  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
271  */
272 
273 namespace implementation
274 {
275 
276 tbuilder_matrix::tbuilder_matrix(const config& cfg)
277  : tbuilder_control(cfg)
278  , vertical_scrollbar_mode(
279  get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
280  , horizontal_scrollbar_mode(
281  get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
282  , builder_top(nullptr)
283  , builder_bottom(nullptr)
284  , builder_left(nullptr)
285  , builder_right(nullptr)
286  , builder_main(create_builder_widget(cfg.child("main", "[matrix]")))
287 {
288  if(const config& top = cfg.child("top")) {
289  builder_top = new tbuilder_grid(top);
290  }
291 
292  if(const config& bottom = cfg.child("bottom")) {
294  }
295 
296  if(const config& left = cfg.child("left")) {
298  }
299 
300  if(const config& right = cfg.child("right")) {
302  }
303 }
304 
306 {
307  return tmatrix::build(*this);
308 }
309 
310 } // namespace implementation
311 
312 // }------------ END --------------
313 
314 } // namespace gui2
Define the common log macros for the gui toolkit.
#define DBG_GUI_P
Definition: log.hpp:69
virtual void request_reduce_width(const unsigned maximum_width) override
See twidget::request_reduce_width.
Definition: matrix.cpp:134
virtual void layout_children() override
See twidget::layout_children.
Definition: matrix.cpp:122
void set_parent(twidget *parent)
Definition: widget.cpp:150
virtual void layout_children() override
See twidget::layout_children.
Definition: grid.cpp:575
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
virtual void layout_initialise(const bool full_initialisation) override
See twidget::layout_initialise.
Definition: matrix.cpp:111
tresolution_definition_ptr config()
Definition: control.hpp:299
virtual const std::string & get_control_type() const override
See tcontrol::get_control_type.
Definition: matrix.cpp:188
virtual void place(const tpoint &origin, const tpoint &size) override
See twidget::place.
Definition: grid.cpp:435
virtual twidget * find_at(const tpoint &coordinate, const bool must_be_active) override
See twidget::find_at.
Definition: matrix.cpp:138
bool disable_click_dismiss() const override
See twidget::disable_click_dismiss.
Definition: matrix.cpp:175
virtual tpoint calculate_best_size() const override
See twidget::calculate_best_size.
Definition: matrix.cpp:168
tscrollbar_container::tscrollbar_mode get_scrollbar_mode(const std::string &scrollbar_mode)
Returns the scrollbar mode flags.
Definition: helper.cpp:114
tpoint get_best_size() const
Gets the best size for the widget.
Definition: widget.cpp:188
twidget * find(const std::string &id, const bool must_be_active) override
See twidget::find.
Definition: matrix.cpp:149
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:142
Base class of a resolution, contains the common keys for a resolution.
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
unsigned get_state() const
Definition: matrix.cpp:55
bool get_active() const
Definition: matrix.cpp:50
GLuint64EXT * result
Definition: glew.h:10727
The matrix class.
Definition: matrix.hpp:101
This file contains the settings handling of the widget library.
void populate_dirty_list(twindow &caller, std::vector< twidget * > &call_stack)
Adds a widget to the dirty list if it is dirty.
Definition: widget.cpp:386
void draw_children(surface &frame_buffer, int x_offset, int y_offset)
Draws the children of a widget.
Definition: widget.cpp:356
virtual void impl_draw_children(surface &frame_buffer, int x_offset, int y_offset) override
See twidget::impl_draw_children.
Definition: matrix.cpp:117
virtual void place(const tpoint &origin, const tpoint &size)
Places the widget.
Definition: widget.cpp:233
Contains the state info for a resolution.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
unsigned create_item(const std::map< std::string, string_map > &item_data, const std::map< std::string, std::string > &tags)
Definition: matrix.cpp:98
lu_byte right
Definition: lparser.cpp:1020
tmatrix_definition(const config &cfg)
Definition: matrix.cpp:217
tmatrix(const implementation::tbuilder_matrix &builder)
Definition: matrix.cpp:60
GLint left
Definition: glew.h:5907
GLuint GLenum matrix
Definition: glew.h:11418
Holds a 2D point.
Definition: point.hpp:24
void set_active(const bool active)
Definition: matrix.cpp:43
tbuilder_widget_ptr create_builder_widget(const config &cfg)
Create a widget builder.
GLsizeiptr size
Definition: glew.h:1649
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
tgrid content_
The grid containing our children.
Definition: matrix.hpp:220
Base class for all widgets.
Definition: widget.hpp:49
tstate state_
Current state of the widget.
Definition: matrix.hpp:65
GLint GLint bottom
Definition: glew.h:5907
virtual void layout_initialise(const bool full_initialisation) override
See twidget::layout_initialise.
Definition: grid.cpp:191
twidget * find(const std::string &id, const bool must_be_active) override
See twidget::find.
Definition: grid.cpp:612
tpane * pane_
Contains the pane used for adding new items to the matrix.
Definition: matrix.hpp:227
The walker abstract base class.
Definition: walker.hpp:27
std::map< std::string, boost::intrusive_ptr< tbuilder_widget > > treplacements
The replacements type is used to define replacement types.
virtual void place(const tpoint &origin, const tpoint &size) override
See twidget::place.
Definition: matrix.cpp:104
static tmatrix * build(const implementation::tbuilder_matrix &builder)
Definition: matrix.cpp:92
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
virtual twidget * find(const std::string &id, const bool must_be_active)
Returns a widget with the wanted id.
Definition: widget.cpp:558
GLsizei const GLcharARB ** string
Definition: glew.h:4503
virtual twidget * find_at(const tpoint &coordinate, const bool must_be_active) override
See twidget::find_at.
Definition: grid.cpp:599
virtual iterator::twalker_ * create_walker() override
See twidget::create_walker.
Definition: matrix.cpp:180
Contains the implementation details for lexical_cast and shouldn't be used directly.
virtual void child_populate_dirty_list(twindow &caller, const std::vector< twidget * > &call_stack) override
See twidget::child_populate_dirty_list.
Definition: matrix.cpp:127
tbuilder_widget_ptr builder_main
Definition: matrix.hpp:270