The Battle for Wesnoth  1.17.17+dev
matrix.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2023
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 #include "gui/widgets/settings.hpp"
29 
30 #include <functional>
31 
32 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
33 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
34 
35 namespace gui2
36 {
37 
38 // ------------ WIDGET -----------{
39 
40 REGISTER_WIDGET(matrix)
41 
42 
43 state_default::state_default() : state_(ENABLED)
44 {
45 }
46 void state_default::set_active(const bool active)
47 {
48  if(get_active() != active) {
49  state_ = active ? ENABLED : DISABLED;
50  }
51 }
52 
54 {
55  return state_ != DISABLED;
56 }
57 
58 unsigned state_default::get_state() const
59 {
60  return state_;
61 }
62 
64  : tbase(builder, "matrix"), content_(), pane_(nullptr)
65 {
66  const auto cfg = cast_config_to<matrix_definition>();
67 
69  replacements.emplace("_main", builder.builder_main);
70 
71  if(builder.builder_top) {
72  replacements.emplace("_top", builder.builder_top);
73  }
74 
75  if(builder.builder_left) {
76  replacements.emplace("_left", builder.builder_left);
77  }
78 
79  if(builder.builder_right) {
80  replacements.emplace("_right", builder.builder_right);
81  }
82 
83  if(builder.builder_bottom) {
84  replacements.emplace("_bottom", builder.builder_bottom);
85  }
86 
87  cfg->content->build(content_, replacements);
88  content_.set_parent(this);
89 
90  pane_ = find_widget<pane>(&content_, "pane", false, true);
91 }
92 
93 unsigned
95  const std::map<std::string, std::string>& tags)
96 {
97  return pane_->create_item(item_data, tags);
98 }
99 
100 void matrix::place(const point& origin, const point& size)
101 {
102  widget::place(origin, size);
103 
104  content_.place(origin, size);
105 }
106 
107 void matrix::layout_initialize(const bool full_initialization)
108 {
109  content_.layout_initialize(full_initialization);
110 }
111 
113 {
115 }
116 
118 {
120 }
121 
122 void matrix::request_reduce_width(const unsigned /*maximum_width*/)
123 {
124 }
125 
126 widget* matrix::find_at(const point& coordinate, const bool must_be_active)
127 {
128  return content_.find_at(coordinate, must_be_active);
129 }
130 
132  const bool must_be_active) const
133 {
134  return content_.find_at(coordinate, must_be_active);
135 }
136 
137 widget* matrix::find(const std::string& id, const bool must_be_active)
138 {
139  if(widget* result = widget::find(id, must_be_active)) {
140  return result;
141  } else {
142  return content_.find(id, must_be_active);
143  }
144 }
145 
146 const widget* matrix::find(const std::string& id, const bool must_be_active)
147  const
148 {
149  if(const widget* result = widget::find(id, must_be_active)) {
150  return result;
151  } else {
152  return content_.find(id, must_be_active);
153  }
154 }
155 
157 {
159 
160  return size;
161 }
162 
164 {
165  return false;
166 }
167 
168 /**
169  * @todo Implement properly.
170  */
172 {
173  return nullptr;
174 }
175 
176 // }---------- DEFINITION ---------{
177 
180 {
181  DBG_GUI_P << "Parsing matrix " << id;
182 
183  load_resolutions<resolution>(cfg);
184 }
185 
187  : resolution_definition(cfg)
188  , content(new builder_grid(VALIDATE_WML_CHILD(cfg, "content", _("Missing [content] in [matrix_definition]"))))
189 {
190  // Note the order should be the same as the enum state_t in matrix.hpp.
191  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_enabled", _("Missing required state for matrix definition")));
192  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_disabled", _("Missing required state for matrix definition")));
193 }
194 
195 // }---------- BUILDER -----------{
196 
197 namespace implementation
198 {
199 
200 builder_matrix::builder_matrix(const config& cfg)
201  : builder_styled_widget(cfg)
202  , vertical_scrollbar_mode(
203  get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
204  , horizontal_scrollbar_mode(
205  get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
206  , builder_top(nullptr)
207  , builder_bottom(nullptr)
208  , builder_left(nullptr)
209  , builder_right(nullptr)
210  , builder_main(create_widget_builder(VALIDATE_WML_CHILD(cfg, "main", _("Missing [main] in [matrix]"))))
211 {
212  if(auto top = cfg.optional_child("top")) {
213  builder_top = std::make_shared<builder_grid>(*top);
214  }
215 
216  if(auto bottom = cfg.optional_child("bottom")) {
217  builder_bottom = std::make_shared<builder_grid>(*bottom);
218  }
219 
220  if(auto left = cfg.optional_child("left")) {
221  builder_left = std::make_shared<builder_grid>(*left);
222  }
223 
224  if(auto right = cfg.optional_child("right")) {
225  builder_right = std::make_shared<builder_grid>(*right);
226  }
227 }
228 
229 std::unique_ptr<widget> builder_matrix::build() const
230 {
231  return std::make_unique<matrix>(*this);
232 }
233 
234 } // namespace implementation
235 
236 // }------------ END --------------
237 
238 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
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:389
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:112
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: matrix.cpp:122
virtual void layout_children() override
See widget::layout_children.
Definition: matrix.cpp:117
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
Definition: matrix.cpp:126
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: matrix.cpp:156
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: matrix.cpp:100
pane * pane_
Contains the pane used for adding new items to the matrix.
Definition: matrix.hpp:223
matrix(const implementation::builder_matrix &builder)
Definition: matrix.cpp:63
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: matrix.cpp:107
virtual iteration::walker_ptr create_walker() override
See widget::create_walker.
Definition: matrix.cpp:171
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: matrix.cpp:137
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: matrix.cpp:163
grid content_
The grid containing our children.
Definition: matrix.hpp:216
unsigned create_item(const widget_data &item_data, const std::map< std::string, std::string > &tags)
Definition: matrix.cpp:94
unsigned create_item(const widget_data &item_data, const std::map< std::string, std::string > &tags)
Creates a new item.
Definition: pane.cpp:118
unsigned get_state() const
Definition: matrix.cpp:58
bool get_active() const
Definition: matrix.cpp:53
void set_active(const bool active)
Definition: matrix.cpp:46
state_t state_
Current state of the widget.
Definition: matrix.hpp:63
Base class for all widgets.
Definition: widget.hpp:54
point get_best_size() const
Gets the best size for the widget.
Definition: widget.cpp:194
virtual void place(const point &origin, const point &size)
Places the widget.
Definition: widget.cpp:239
virtual widget * find(const std::string &id, const bool must_be_active)
Returns a widget with the wanted id.
Definition: widget.cpp:541
void set_parent(widget *parent)
Definition: widget.cpp:156
void draw_children()
Draws the children of a widget.
Definition: widget.cpp:389
static std::string _(const char *str)
Definition: gettext.hpp:93
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:43
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:35
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:87
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
This file contains the settings handling of the widget library.
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:271
virtual std::unique_ptr< widget > build() const override
Definition: matrix.cpp:229
resolution(const config &cfg)
Definition: matrix.cpp:186
matrix_definition(const config &cfg)
Definition: matrix.cpp:178
Base class of a resolution, contains the common keys for a resolution.
std::vector< state_definition > state
Holds a 2D point.
Definition: point.hpp:25
#define VALIDATE_WML_CHILD(cfg, key, message)