The Battle for Wesnoth  1.15.12+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 <functional>
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 
178 /**
179  * @todo Implement properly.
180  */
182 {
183  return nullptr;
184 }
185 
186 // }---------- DEFINITION ---------{
187 
190 {
191  DBG_GUI_P << "Parsing matrix " << id << '\n';
192 
193  load_resolutions<resolution>(cfg);
194 }
195 
197  : resolution_definition(cfg)
198  , content(new builder_grid(cfg.child("content", "[matrix_definition]")))
199 {
200  // Note the order should be the same as the enum state_t in matrix.hpp.
201  state.emplace_back(cfg.child("state_enabled"));
202  state.emplace_back(cfg.child("state_disabled"));
203 }
204 
205 // }---------- BUILDER -----------{
206 
207 namespace implementation
208 {
209 
210 builder_matrix::builder_matrix(const config& cfg)
211  : builder_styled_widget(cfg)
212  , vertical_scrollbar_mode(
213  get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
214  , horizontal_scrollbar_mode(
215  get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
216  , builder_top(nullptr)
217  , builder_bottom(nullptr)
218  , builder_left(nullptr)
219  , builder_right(nullptr)
220  , builder_main(create_widget_builder(cfg.child("main", "[matrix]")))
221 {
222  if(const config& top = cfg.child("top")) {
223  builder_top = std::make_shared<builder_grid>(top);
224  }
225 
226  if(const config& bottom = cfg.child("bottom")) {
227  builder_bottom = std::make_shared<builder_grid>(bottom);
228  }
229 
230  if(const config& left = cfg.child("left")) {
231  builder_left = std::make_shared<builder_grid>(left);
232  }
233 
234  if(const config& right = cfg.child("right")) {
235  builder_right = std::make_shared<builder_grid>(right);
236  }
237 }
238 
240 {
241  return matrix::build(*this);
242 }
243 
244 } // namespace implementation
245 
246 // }------------ END --------------
247 
248 } // 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:65
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:414
virtual void layout_children() override
See widget::layout_children.
Definition: matrix.cpp:120
void set_parent(widget *parent)
Definition: widget.cpp:154
std::vector< state_definition > state
resolution(const config &cfg)
Definition: matrix.cpp:196
virtual widget * find(const std::string &id, const bool must_be_active)
Returns a widget with the wanted id.
Definition: widget.cpp:583
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
List with the matrix specific variables: Key Type Default Description vertical_scrollbar_mode scroll...
Definition: matrix.hpp:107
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:125
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:188
Base class for all widgets.
Definition: widget.hpp:49
grid content_
The grid containing our children.
Definition: matrix.hpp:226
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:416
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:192
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:237
The walker abstract base class.
Definition: walker.hpp:26
static thread_local std::deque< std::string > call_stack
For printing error messages when WFL parsing or evaluation fails, this contains the names of the WFL ...
Definition: function.cpp:46
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
virtual widget * build() const override
Definition: matrix.cpp:239
#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:1227
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:281
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:181
lu_byte left
Definition: lparser.cpp:1226
pane * pane_
Contains the pane used for adding new items to the matrix.
Definition: matrix.hpp:233
void draw_children(surface &frame_buffer, int x_offset, int y_offset)
Draws the children of a widget.
Definition: widget.cpp:386
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:59
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:64
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.