The Battle for Wesnoth  1.17.4+dev
matrix.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2022
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 
22 #include "gui/core/log.hpp"
27 #include "gui/widgets/settings.hpp"
28 
29 #include <functional>
30 
31 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
32 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
33 
34 namespace gui2
35 {
36 
37 // ------------ WIDGET -----------{
38 
39 REGISTER_WIDGET(matrix)
40 
41 
42 state_default::state_default() : state_(ENABLED)
43 {
44 }
45 void state_default::set_active(const bool active)
46 {
47  if(get_active() != active) {
48  state_ = active ? ENABLED : DISABLED;
49  }
50 }
51 
53 {
54  return state_ != DISABLED;
55 }
56 
57 unsigned state_default::get_state() const
58 {
59  return state_;
60 }
61 
63  : tbase(builder, "matrix"), content_(), pane_(nullptr)
64 {
65  const auto cfg = cast_config_to<matrix_definition>();
66 
68  replacements.emplace("_main", builder.builder_main);
69 
70  if(builder.builder_top) {
71  replacements.emplace("_top", builder.builder_top);
72  }
73 
74  if(builder.builder_left) {
75  replacements.emplace("_left", builder.builder_left);
76  }
77 
78  if(builder.builder_right) {
79  replacements.emplace("_right", builder.builder_right);
80  }
81 
82  if(builder.builder_bottom) {
83  replacements.emplace("_bottom", builder.builder_bottom);
84  }
85 
86  cfg->content->build(content_, replacements);
87  content_.set_parent(this);
88 
89  pane_ = find_widget<pane>(&content_, "pane", false, true);
90 }
91 
92 unsigned
93 matrix::create_item(const std::map<std::string, string_map>& item_data,
94  const std::map<std::string, std::string>& tags)
95 {
96  return pane_->create_item(item_data, tags);
97 }
98 
99 void matrix::place(const point& origin, const point& size)
100 {
101  widget::place(origin, size);
102 
103  content_.place(origin, size);
104 }
105 
106 void matrix::layout_initialize(const bool full_initialization)
107 {
108  content_.layout_initialize(full_initialization);
109 }
110 
111 void matrix::impl_draw_children(int x_offset, int y_offset)
112 {
113  content_.draw_children(x_offset, y_offset);
114 }
115 
117 {
119 }
120 
122  const std::vector<widget*>& call_stack)
123 {
124  std::vector<widget*> child_call_stack = call_stack;
125  content_.populate_dirty_list(caller, child_call_stack);
126 }
127 
128 void matrix::request_reduce_width(const unsigned /*maximum_width*/)
129 {
130 }
131 
132 widget* matrix::find_at(const point& coordinate, const bool must_be_active)
133 {
134  return content_.find_at(coordinate, must_be_active);
135 }
136 
138  const bool must_be_active) const
139 {
140  return content_.find_at(coordinate, must_be_active);
141 }
142 
143 widget* matrix::find(const std::string& id, const bool must_be_active)
144 {
145  if(widget* result = widget::find(id, must_be_active)) {
146  return result;
147  } else {
148  return content_.find(id, must_be_active);
149  }
150 }
151 
152 const widget* matrix::find(const std::string& id, const bool must_be_active)
153  const
154 {
155  if(const widget* result = widget::find(id, must_be_active)) {
156  return result;
157  } else {
158  return content_.find(id, must_be_active);
159  }
160 }
161 
163 {
165 
166  return size;
167 }
168 
170 {
171  return false;
172 }
173 
174 /**
175  * @todo Implement properly.
176  */
178 {
179  return nullptr;
180 }
181 
182 // }---------- DEFINITION ---------{
183 
186 {
187  DBG_GUI_P << "Parsing matrix " << id << '\n';
188 
189  load_resolutions<resolution>(cfg);
190 }
191 
193  : resolution_definition(cfg)
194  , content(new builder_grid(cfg.child("content", "[matrix_definition]")))
195 {
196  // Note the order should be the same as the enum state_t in matrix.hpp.
197  state.emplace_back(cfg.child("state_enabled"));
198  state.emplace_back(cfg.child("state_disabled"));
199 }
200 
201 // }---------- BUILDER -----------{
202 
203 namespace implementation
204 {
205 
206 builder_matrix::builder_matrix(const config& cfg)
207  : builder_styled_widget(cfg)
208  , vertical_scrollbar_mode(
209  get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
210  , horizontal_scrollbar_mode(
211  get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
212  , builder_top(nullptr)
213  , builder_bottom(nullptr)
214  , builder_left(nullptr)
215  , builder_right(nullptr)
216  , builder_main(create_widget_builder(cfg.child("main", "[matrix]")))
217 {
218  if(const config& top = cfg.child("top")) {
219  builder_top = std::make_shared<builder_grid>(top);
220  }
221 
222  if(const config& bottom = cfg.child("bottom")) {
223  builder_bottom = std::make_shared<builder_grid>(bottom);
224  }
225 
226  if(const config& left = cfg.child("left")) {
227  builder_left = std::make_shared<builder_grid>(left);
228  }
229 
230  if(const config& right = cfg.child("right")) {
231  builder_right = std::make_shared<builder_grid>(right);
232  }
233 }
234 
235 std::unique_ptr<widget> builder_matrix::build() const
236 {
237  return std::make_unique<matrix>(*this);
238 }
239 
240 } // namespace implementation
241 
242 // }------------ END --------------
243 
244 } // 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
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
std::unique_ptr< class walker_base > walker_ptr
Definition: widget.hpp:39
virtual void layout_children() override
See widget::layout_children.
Definition: matrix.cpp:116
void set_parent(widget *parent)
Definition: widget.cpp:156
std::vector< state_definition > state
resolution(const config &cfg)
Definition: matrix.cpp:192
virtual widget * find(const std::string &id, const bool must_be_active)
Returns a widget with the wanted id.
Definition: widget.cpp:582
virtual void child_populate_dirty_list(window &caller, const std::vector< widget *> &call_stack) override
See widget::child_populate_dirty_list.
Definition: matrix.cpp:121
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:118
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: matrix.cpp:143
matrix_definition(const config &cfg)
Definition: matrix.cpp:184
Base class for all widgets.
Definition: widget.hpp:49
grid content_
The grid containing our children.
Definition: matrix.hpp:221
std::map< std::string, std::shared_ptr< builder_widget > > replacements_map
The replacements type is used to define replacement types.
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
Definition: matrix.cpp:132
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: matrix.cpp:128
unsigned create_item(const std::map< std::string, string_map > &item_data, const std::map< std::string, std::string > &tags)
Definition: matrix.cpp:93
Generic file dialog.
Definition: field-fwd.hpp:23
matrix(const implementation::builder_matrix &builder)
Definition: matrix.cpp:62
virtual iteration::walker_ptr create_walker() override
See widget::create_walker.
Definition: matrix.cpp:177
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: grid.cpp:190
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:415
virtual void impl_draw_children(int x_offset, int y_offset) override
See widget::impl_draw_children.
Definition: matrix.cpp:111
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: matrix.cpp:106
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
point get_best_size() const
Gets the best size for the widget.
Definition: widget.cpp:194
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:239
void draw_children(int x_offset, int y_offset)
Draws the children of a widget.
Definition: widget.cpp:387
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:660
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: matrix.cpp:169
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: grid.cpp:484
state_t state_
Current state of the widget.
Definition: matrix.hpp:63
bool get_active() const
Definition: matrix.cpp:52
Holds a 2D point.
Definition: point.hpp:24
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: matrix.cpp:99
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:647
unsigned get_state() const
Definition: matrix.cpp:57
builder_widget_ptr builder_main
Definition: matrix.hpp:276
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: matrix.cpp:162
virtual void layout_children() override
See widget::layout_children.
Definition: grid.cpp:623
pane * pane_
Contains the pane used for adding new items to the matrix.
Definition: matrix.hpp:228
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:60
virtual std::unique_ptr< widget > build() const override
Definition: matrix.cpp:235
void set_active(const bool active)
Definition: matrix.cpp:45
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:66
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.