The Battle for Wesnoth  1.17.0-dev
matrix.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2021
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
18 #include "gui/widgets/matrix.hpp"
19 
21 #include "gui/core/log.hpp"
26 #include "gui/widgets/settings.hpp"
27 
28 #include <functional>
29 
30 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
31 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
32 
33 namespace gui2
34 {
35 
36 // ------------ WIDGET -----------{
37 
38 REGISTER_WIDGET(matrix)
39 
40 
41 state_default::state_default() : state_(ENABLED)
42 {
43 }
44 void state_default::set_active(const bool active)
45 {
46  if(get_active() != active) {
47  state_ = active ? ENABLED : DISABLED;
48  }
49 }
50 
52 {
53  return state_ != DISABLED;
54 }
55 
56 unsigned state_default::get_state() const
57 {
58  return state_;
59 }
60 
62  : tbase(builder, "matrix"), content_(), pane_(nullptr)
63 {
64  const auto cfg = cast_config_to<matrix_definition>();
65 
67  replacements.emplace("_main", builder.builder_main);
68 
69  if(builder.builder_top) {
70  replacements.emplace("_top", builder.builder_top);
71  }
72 
73  if(builder.builder_left) {
74  replacements.emplace("_left", builder.builder_left);
75  }
76 
77  if(builder.builder_right) {
78  replacements.emplace("_right", builder.builder_right);
79  }
80 
81  if(builder.builder_bottom) {
82  replacements.emplace("_bottom", builder.builder_bottom);
83  }
84 
85  cfg->content->build(content_, replacements);
86  content_.set_parent(this);
87 
88  pane_ = find_widget<pane>(&content_, "pane", false, true);
89 }
90 
92 {
93  return new matrix(builder);
94 }
95 
96 unsigned
97 matrix::create_item(const std::map<std::string, string_map>& item_data,
98  const std::map<std::string, std::string>& tags)
99 {
100  return pane_->create_item(item_data, tags);
101 }
102 
103 void matrix::place(const point& origin, const point& size)
104 {
105  widget::place(origin, size);
106 
107  content_.place(origin, size);
108 }
109 
110 void matrix::layout_initialize(const bool full_initialization)
111 {
112  content_.layout_initialize(full_initialization);
113 }
114 
115 void
116 matrix::impl_draw_children(surface& frame_buffer, int x_offset, int y_offset)
117 {
118  content_.draw_children(frame_buffer, x_offset, y_offset);
119 }
120 
122 {
124 }
125 
127  const std::vector<widget*>& call_stack)
128 {
129  std::vector<widget*> child_call_stack = call_stack;
130  content_.populate_dirty_list(caller, child_call_stack);
131 }
132 
133 void matrix::request_reduce_width(const unsigned /*maximum_width*/)
134 {
135 }
136 
137 widget* matrix::find_at(const point& coordinate, const bool must_be_active)
138 {
139  return content_.find_at(coordinate, must_be_active);
140 }
141 
143  const bool must_be_active) const
144 {
145  return content_.find_at(coordinate, must_be_active);
146 }
147 
148 widget* matrix::find(const std::string& id, const bool must_be_active)
149 {
150  if(widget* result = widget::find(id, must_be_active)) {
151  return result;
152  } else {
153  return content_.find(id, must_be_active);
154  }
155 }
156 
157 const widget* matrix::find(const std::string& id, const bool must_be_active)
158  const
159 {
160  if(const widget* result = widget::find(id, must_be_active)) {
161  return result;
162  } else {
163  return content_.find(id, must_be_active);
164  }
165 }
166 
168 {
170 
171  return size;
172 }
173 
175 {
176  return false;
177 }
178 
179 /**
180  * @todo Implement properly.
181  */
183 {
184  return nullptr;
185 }
186 
187 // }---------- DEFINITION ---------{
188 
191 {
192  DBG_GUI_P << "Parsing matrix " << id << '\n';
193 
194  load_resolutions<resolution>(cfg);
195 }
196 
198  : resolution_definition(cfg)
199  , content(new builder_grid(cfg.child("content", "[matrix_definition]")))
200 {
201  // Note the order should be the same as the enum state_t in matrix.hpp.
202  state.emplace_back(cfg.child("state_enabled"));
203  state.emplace_back(cfg.child("state_disabled"));
204 }
205 
206 // }---------- BUILDER -----------{
207 
208 namespace implementation
209 {
210 
211 builder_matrix::builder_matrix(const config& cfg)
212  : builder_styled_widget(cfg)
213  , vertical_scrollbar_mode(
214  get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
215  , horizontal_scrollbar_mode(
216  get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
217  , builder_top(nullptr)
218  , builder_bottom(nullptr)
219  , builder_left(nullptr)
220  , builder_right(nullptr)
221  , builder_main(create_widget_builder(cfg.child("main", "[matrix]")))
222 {
223  if(const config& top = cfg.child("top")) {
224  builder_top = std::make_shared<builder_grid>(top);
225  }
226 
227  if(const config& bottom = cfg.child("bottom")) {
228  builder_bottom = std::make_shared<builder_grid>(bottom);
229  }
230 
231  if(const config& left = cfg.child("left")) {
232  builder_left = std::make_shared<builder_grid>(left);
233  }
234 
235  if(const config& right = cfg.child("right")) {
236  builder_right = std::make_shared<builder_grid>(right);
237  }
238 }
239 
241 {
242  return matrix::build(*this);
243 }
244 
245 } // namespace implementation
246 
247 // }------------ END --------------
248 
249 } // 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:66
virtual void impl_draw_children(surface &frame_buffer, int x_offset, int y_offset) override
See widget::impl_draw_children.
Definition: matrix.cpp:116
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:402
virtual void layout_children() override
See widget::layout_children.
Definition: matrix.cpp:121
void set_parent(widget *parent)
Definition: widget.cpp:155
std::vector< state_definition > state
resolution(const config &cfg)
Definition: matrix.cpp:197
virtual widget * find(const std::string &id, const bool must_be_active)
Returns a widget with the wanted id.
Definition: widget.cpp:584
virtual void child_populate_dirty_list(window &caller, const std::vector< widget *> &call_stack) override
See widget::child_populate_dirty_list.
Definition: matrix.cpp:126
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:126
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: matrix.cpp:148
matrix_definition(const config &cfg)
Definition: matrix.cpp:189
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:137
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: matrix.cpp:133
unsigned create_item(const std::map< std::string, string_map > &item_data, const std::map< std::string, std::string > &tags)
Definition: matrix.cpp:97
Generic file dialog.
Definition: field-fwd.hpp:23
matrix(const implementation::builder_matrix &builder)
Definition: matrix.cpp:61
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: grid.cpp:186
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:417
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: matrix.cpp:110
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
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:193
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:238
The walker abstract base class.
Definition: walker.hpp:27
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:47
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:656
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: matrix.cpp:174
virtual widget * build() const override
Definition: matrix.cpp:240
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
static matrix * build(const implementation::builder_matrix &builder)
Definition: matrix.cpp:91
lu_byte right
Definition: lparser.cpp:1227
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: grid.cpp:480
state_t state_
Current state of the widget.
Definition: matrix.hpp:63
bool get_active() const
Definition: matrix.cpp:51
Holds a 2D point.
Definition: point.hpp:24
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: matrix.cpp:103
scrollbar_mode get_scrollbar_mode(const std::string &scrollbar_mode)
Returns the scrollbar mode flags.
Definition: helper.cpp:121
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
Definition: grid.cpp:643
unsigned get_state() const
Definition: matrix.cpp:56
builder_widget_ptr builder_main
Definition: matrix.hpp:281
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: matrix.cpp:167
virtual void layout_children() override
See widget::layout_children.
Definition: grid.cpp:619
virtual iteration::walker_base * create_walker() override
See widget::create_walker.
Definition: matrix.cpp:182
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:387
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:61
void set_active(const bool active)
Definition: matrix.cpp:44
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:65
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.