The Battle for Wesnoth  1.17.8+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
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 
112 {
114 }
115 
117 {
119 }
120 
121 void matrix::request_reduce_width(const unsigned /*maximum_width*/)
122 {
123 }
124 
125 widget* matrix::find_at(const point& coordinate, const bool must_be_active)
126 {
127  return content_.find_at(coordinate, must_be_active);
128 }
129 
131  const bool must_be_active) const
132 {
133  return content_.find_at(coordinate, must_be_active);
134 }
135 
136 widget* matrix::find(const std::string& id, const bool must_be_active)
137 {
138  if(widget* result = widget::find(id, must_be_active)) {
139  return result;
140  } else {
141  return content_.find(id, must_be_active);
142  }
143 }
144 
145 const widget* matrix::find(const std::string& id, const bool must_be_active)
146  const
147 {
148  if(const widget* result = widget::find(id, must_be_active)) {
149  return result;
150  } else {
151  return content_.find(id, must_be_active);
152  }
153 }
154 
156 {
158 
159  return size;
160 }
161 
163 {
164  return false;
165 }
166 
167 /**
168  * @todo Implement properly.
169  */
171 {
172  return nullptr;
173 }
174 
175 // }---------- DEFINITION ---------{
176 
179 {
180  DBG_GUI_P << "Parsing matrix " << id;
181 
182  load_resolutions<resolution>(cfg);
183 }
184 
186  : resolution_definition(cfg)
187  , content(new builder_grid(cfg.child("content", "[matrix_definition]")))
188 {
189  // Note the order should be the same as the enum state_t in matrix.hpp.
190  state.emplace_back(cfg.child("state_enabled"));
191  state.emplace_back(cfg.child("state_disabled"));
192 }
193 
194 // }---------- BUILDER -----------{
195 
196 namespace implementation
197 {
198 
199 builder_matrix::builder_matrix(const config& cfg)
200  : builder_styled_widget(cfg)
201  , vertical_scrollbar_mode(
202  get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
203  , horizontal_scrollbar_mode(
204  get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
205  , builder_top(nullptr)
206  , builder_bottom(nullptr)
207  , builder_left(nullptr)
208  , builder_right(nullptr)
209  , builder_main(create_widget_builder(cfg.child("main", "[matrix]")))
210 {
211  if(const config& top = cfg.child("top")) {
212  builder_top = std::make_shared<builder_grid>(top);
213  }
214 
215  if(const config& bottom = cfg.child("bottom")) {
216  builder_bottom = std::make_shared<builder_grid>(bottom);
217  }
218 
219  if(const config& left = cfg.child("left")) {
220  builder_left = std::make_shared<builder_grid>(left);
221  }
222 
223  if(const config& right = cfg.child("right")) {
224  builder_right = std::make_shared<builder_grid>(right);
225  }
226 }
227 
228 std::unique_ptr<widget> builder_matrix::build() const
229 {
230  return std::make_unique<matrix>(*this);
231 }
232 
233 } // namespace implementation
234 
235 // }------------ END --------------
236 
237 } // 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
unsigned create_item(const widget_data &item_data, const std::map< std::string, std::string > &tags)
Definition: matrix.cpp:93
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:43
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:185
virtual widget * find(const std::string &id, const bool must_be_active)
Returns a widget with the wanted id.
Definition: widget.cpp:541
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: matrix.cpp:136
matrix_definition(const config &cfg)
Definition: matrix.cpp:177
Base class for all widgets.
Definition: widget.hpp:53
grid content_
The grid containing our children.
Definition: matrix.hpp:216
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:125
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: matrix.cpp:121
Generic file dialog.
matrix(const implementation::builder_matrix &builder)
Definition: matrix.cpp:62
virtual iteration::walker_ptr create_walker() override
See widget::create_walker.
Definition: matrix.cpp:170
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: grid.cpp:190
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
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:645
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: matrix.cpp:162
#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
void draw_children()
Draws the children of a widget.
Definition: widget.cpp:389
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:632
unsigned get_state() const
Definition: matrix.cpp:57
builder_widget_ptr builder_main
Definition: matrix.hpp:271
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: matrix.cpp:155
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:223
unsigned create_item(const widget_data &item_data, const std::map< std::string, std::string > &tags)
Creates a new item.
Definition: pane.cpp:118
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:35
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
virtual void impl_draw_children() override
See widget::impl_draw_children.
Definition: matrix.cpp:111
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:228
void set_active(const bool active)
Definition: matrix.cpp:45
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.