The Battle for Wesnoth  1.19.1+dev
matrix.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2024
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 
20 #include "gettext.hpp"
23 #include "gui/core/log.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 
90 unsigned
92  const std::map<std::string, std::string>& tags)
93 {
94  return pane_->create_item(item_data, tags);
95 }
96 
97 void matrix::place(const point& origin, const point& size)
98 {
99  widget::place(origin, size);
100 
101  content_.place(origin, size);
102 }
103 
104 void matrix::layout_initialize(const bool full_initialization)
105 {
106  content_.layout_initialize(full_initialization);
107 }
108 
110 {
112 }
113 
115 {
117 }
118 
119 void matrix::request_reduce_width(const unsigned /*maximum_width*/)
120 {
121 }
122 
123 widget* matrix::find_at(const point& coordinate, const bool must_be_active)
124 {
125  return content_.find_at(coordinate, must_be_active);
126 }
127 
129  const bool must_be_active) const
130 {
131  return content_.find_at(coordinate, must_be_active);
132 }
133 
134 widget* matrix::find(const std::string& id, const bool must_be_active)
135 {
136  if(widget* result = widget::find(id, must_be_active)) {
137  return result;
138  } else {
139  return content_.find(id, must_be_active);
140  }
141 }
142 
143 const widget* matrix::find(const std::string& id, const bool must_be_active)
144  const
145 {
146  if(const widget* result = widget::find(id, must_be_active)) {
147  return result;
148  } else {
149  return content_.find(id, must_be_active);
150  }
151 }
152 
154 {
156 
157  return size;
158 }
159 
161 {
162  return false;
163 }
164 
165 /**
166  * @todo Implement properly.
167  */
169 {
170  return nullptr;
171 }
172 
173 // }---------- DEFINITION ---------{
174 
177 {
178  DBG_GUI_P << "Parsing matrix " << id;
179 
180  load_resolutions<resolution>(cfg);
181 }
182 
184  : resolution_definition(cfg)
185  , content(new builder_grid(VALIDATE_WML_CHILD(cfg, "content", missing_mandatory_wml_tag("matrix", "content"))))
186 {
187  // Note the order should be the same as the enum state_t in matrix.hpp.
188  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_enabled", missing_mandatory_wml_tag("matrix_definition][resolution", "state_enabled")));
189  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_disabled", missing_mandatory_wml_tag("matrix_definition][resolution", "state_disabled")));
190 }
191 
192 // }---------- BUILDER -----------{
193 
194 namespace implementation
195 {
196 
197 builder_matrix::builder_matrix(const config& cfg)
198  : builder_styled_widget(cfg)
199  , vertical_scrollbar_mode(
200  get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
201  , horizontal_scrollbar_mode(
202  get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
203  , builder_top(nullptr)
204  , builder_bottom(nullptr)
205  , builder_left(nullptr)
206  , builder_right(nullptr)
207  , builder_main(create_widget_builder(VALIDATE_WML_CHILD(cfg, "main", missing_mandatory_wml_tag("matrix", "main"))))
208 {
209  if(auto top = cfg.optional_child("top")) {
210  builder_top = std::make_shared<builder_grid>(*top);
211  }
212 
213  if(auto bottom = cfg.optional_child("bottom")) {
214  builder_bottom = std::make_shared<builder_grid>(*bottom);
215  }
216 
217  if(auto left = cfg.optional_child("left")) {
218  builder_left = std::make_shared<builder_grid>(*left);
219  }
220 
221  if(auto right = cfg.optional_child("right")) {
222  builder_right = std::make_shared<builder_grid>(*right);
223  }
224 }
225 
226 std::unique_ptr<widget> builder_matrix::build() const
227 {
228  return std::make_unique<matrix>(*this);
229 }
230 
231 } // namespace implementation
232 
233 // }------------ END --------------
234 
235 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Euivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Definition: config.cpp:385
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: grid.cpp:484
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
Definition: grid.cpp:632
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:645
virtual void layout_children() override
See widget::layout_children.
Definition: grid.cpp:623
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: grid.cpp:190
virtual void impl_draw_children() override
See widget::impl_draw_children.
Definition: matrix.cpp:109
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: matrix.cpp:119
virtual void layout_children() override
See widget::layout_children.
Definition: matrix.cpp:114
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
Definition: matrix.cpp:123
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: matrix.cpp:153
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: matrix.cpp:97
pane * pane_
Contains the pane used for adding new items to the matrix.
Definition: matrix.hpp:214
matrix(const implementation::builder_matrix &builder)
Definition: matrix.cpp:60
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: matrix.cpp:104
virtual iteration::walker_ptr create_walker() override
See widget::create_walker.
Definition: matrix.cpp:168
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: matrix.cpp:134
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: matrix.cpp:160
grid content_
The grid containing our children.
Definition: matrix.hpp:207
unsigned create_item(const widget_data &item_data, const std::map< std::string, std::string > &tags)
Definition: matrix.cpp:91
unsigned create_item(const widget_data &item_data, const std::map< std::string, std::string > &tags)
Creates a new item.
Definition: pane.cpp:116
unsigned get_state() const
Definition: matrix.cpp:55
bool get_active() const
Definition: matrix.cpp:50
void set_active(const bool active)
Definition: matrix.cpp:43
state_t state_
Current state of the widget.
Definition: matrix.hpp:63
Base class for all widgets.
Definition: widget.hpp:53
point get_best_size() const
Gets the best size for the widget.
Definition: widget.cpp:193
virtual void place(const point &origin, const point &size)
Places the widget.
Definition: widget.cpp:238
virtual widget * find(const std::string &id, const bool must_be_active)
Returns a widget with the wanted id.
Definition: widget.cpp:550
void set_parent(widget *parent)
Definition: widget.cpp:155
void draw_children()
Draws the children of a widget.
Definition: widget.cpp:392
Define the common log macros for the gui toolkit.
#define DBG_GUI_P
Definition: log.hpp:66
scrollbar_mode get_scrollbar_mode(const std::string &scrollbar_mode)
Returns the scrollbar mode flags.
Definition: helper.cpp:121
std::unique_ptr< class walker_base > walker_ptr
Definition: widget.hpp:42
Generic file dialog.
builder_widget_ptr create_widget_builder(const config &cfg)
Create a widget builder.
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
Contains the implementation details for lexical_cast and shouldn't be used directly.
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
std::map< std::string, std::shared_ptr< builder_widget > > replacements_map
The replacements type is used to define replacement types.
builder_widget_ptr builder_main
Definition: matrix.hpp:262
virtual std::unique_ptr< widget > build() const override
Definition: matrix.cpp:226
resolution(const config &cfg)
Definition: matrix.cpp:183
matrix_definition(const config &cfg)
Definition: matrix.cpp:175
std::vector< state_definition > state
Holds a 2D point.
Definition: point.hpp:25
std::string missing_mandatory_wml_tag(const std::string &section, const std::string &tag)
Returns a standard message for a missing wml child (tag).
#define VALIDATE_WML_CHILD(cfg, key, message)