The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
multi_page.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2008 - 2025
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
19
23
24#include "gettext.hpp"
25#include "wml_exception.hpp"
26
27#include <functional>
28
29namespace gui2
30{
31
32// ------------ WIDGET -----------{
33
34REGISTER_WIDGET(multi_page)
35
36multi_page::multi_page(const implementation::builder_multi_page& builder)
37 : container_base(builder, type())
38 , generator_(nullptr)
39 , page_builders_(builder.builders)
40{
41 const auto conf = cast_config_to<multi_page_definition>();
42 assert(conf);
43
44 init_grid(*conf->grid);
45
47
48 // Save our *non-owning* pointer before this gets moved into the grid.
49 generator_ = generator.get();
50 assert(generator_);
51
52 generator->create_items(-1, *page_builders_.begin()->second, builder.data, nullptr);
53
54 // TODO: can we use the replacements system here?
55 swap_grid(nullptr, &get_grid(), std::move(generator), "_content_grid");
56}
57
59{
60 assert(generator_);
61 grid& page = generator_->create_item(-1, *page_builders_.begin()->second, item, nullptr);
62
63 return page;
64}
65
66grid& multi_page::add_page(const std::string& type, int insert_pos, const widget_item& item)
67{
68 assert(generator_);
69 auto it_builder = page_builders_.find(type);
70 VALIDATE(it_builder != page_builders_.end(), "invalid page type '" + type + "'");
71 return generator_->create_item(insert_pos, *it_builder->second, item, nullptr);
72}
73
75{
76 assert(generator_);
77 grid& page = generator_->create_item(-1, *page_builders_.begin()->second, data, nullptr);
78
79 return page;
80}
81
82grid& multi_page::add_page(const std::string& type, int insert_pos, const widget_data& data)
83{
84 assert(generator_);
85 auto it_builder = page_builders_.find(type);
86 VALIDATE(it_builder != page_builders_.end(), "invalid page type '" + type + "'");
87 return generator_->create_item(insert_pos, *it_builder->second, data, nullptr);
88}
89
90void multi_page::remove_page(const unsigned page, unsigned count)
91{
92 assert(generator_);
93
94 if(page >= get_page_count()) {
95 return;
96 }
97
98 if(!count || (page + count) > get_page_count()) {
99 count = get_page_count() - page;
100 }
101
102 for(; count; --count) {
103 generator_->delete_item(page);
104 }
105}
106
108{
109 assert(generator_);
110 generator_->clear();
111}
112
114{
115 assert(generator_);
116 return generator_->get_item_count();
117}
118
119void multi_page::select_page(const unsigned page, const bool select)
120{
121 if(page >= get_page_count()) {
122 throw std::invalid_argument("invalid page index");
123 }
124 assert(generator_);
125 generator_->select_item(page, select);
126}
127
129{
130 assert(generator_);
132}
133
134const grid& multi_page::page_grid(const unsigned page) const
135{
136 assert(generator_);
137 return generator_->item(page);
138}
139
140grid& multi_page::page_grid(const unsigned page)
141{
142 assert(generator_);
143 return generator_->item(page);
144}
145
147{
148 return true;
149}
150
151unsigned multi_page::get_state() const
152{
153 return 0;
154}
155
157{
158 /* DO NOTHING */
159 return true;
160}
161
162void multi_page::set_self_active(const bool /*active*/)
163{
164 /* DO NOTHING */
165}
166
167// }---------- DEFINITION ---------{
168
171{
172 DBG_GUI_P << "Parsing multipage " << id;
173
174 load_resolutions<resolution>(cfg);
175}
176
178 : resolution_definition(cfg), grid(nullptr)
179{
180 auto child = cfg.optional_child("grid");
181 VALIDATE(child, _("No grid defined."));
182
183 grid = std::make_shared<builder_grid>(*child);
184}
185
186// }---------- BUILDER -----------{
187
188namespace implementation
189{
190
191builder_multi_page::builder_multi_page(const config& cfg)
193{
194 for (const config& page : cfg.child_range("page_definition"))
195 {
196 auto builder = std::make_shared<builder_grid>(page);
197 assert(builder);
198 builders[page["id"]] = builder;
199 }
200 VALIDATE(!builders.empty(), _("No page defined."));
201
202 /** @todo This part is untested. */
203 auto d = cfg.optional_child("page_data");
204 if(!d) {
205 return;
206 }
207
208 auto builder = builders.begin()->second;
209 for(const auto & row : d->child_range("row"))
210 {
211 unsigned col = 0;
212
213 for(const auto & column : row.child_range("column"))
214 {
215 data.emplace_back();
216 for(const auto& [key, value] : column.attribute_range())
217 {
218 data.back()[key] = value.t_str();
219 }
220 ++col;
221 }
222
223 VALIDATE(col == builder->cols,
224 _("‘list_data’ must have "
225 "the same number of columns as the ‘list_definition’."));
226 }
227}
228
229std::unique_ptr<widget> builder_multi_page::build() const
230{
231 auto widget = std::make_unique<multi_page>(*this);
232 DBG_GUI_G << "Window builder: placed multi_page '" << id << "' with definition '" << definition << "'.";
233 return widget;
234}
235
236} // namespace implementation
237
238// }------------ END --------------
239
240} // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
optional_config_impl< config > optional_child(std::string_view key, int n=0)
Equivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Definition config.cpp:380
child_itors child_range(std::string_view key)
Definition config.cpp:268
A generic container base class.
virtual void delete_item(const unsigned index)=0
Deletes an item.
virtual grid & item(const unsigned index)=0
Gets the grid of an item.
virtual grid & create_item(const int index, const builder_grid &list_builder, const widget_item &item_data, const std::function< void(widget &)> &callback)=0
Creates a new item.
virtual void select_item(const unsigned index, const bool select)=0
(De)selects an item.
virtual unsigned get_item_count() const =0
Returns the number of items.
static std::unique_ptr< generator_base > build(const bool has_minimum, const bool has_maximum, const placement placement, const bool select)
Create a new generator.
virtual void clear()=0
Deletes all items.
virtual int get_selected_item() const =0
Returns the selected item.
Basic template class to generate new items.
virtual void create_items(const int index, const builder_grid &list_builder, const std::vector< widget_data > &data, const std::function< void(widget &)> &callback) override
Inherited from generator_base.
Base container class.
Definition grid.hpp:32
const grid & page_grid(const unsigned page) const
Returns the grid for the page.
virtual unsigned get_state() const override
See styled_widget::get_state.
generator_base * generator_
Contains a pointer to the generator.
void remove_page(const unsigned page, unsigned count=1)
Removes a page in the multi page.
grid & add_page(const widget_item &item)
Adds single page to the grid.
builder_grid_map page_builders_
Contains the builder for the new items.
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
virtual bool get_active() const override
See styled_widget::get_active.
unsigned get_page_count() const
Returns the number of pages.
int get_selected_page() const
Returns the selected page.
virtual bool impl_draw_background() override
See widget::impl_draw_background.
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
void select_page(const unsigned page, const bool select=true)
Selects a page.
void clear()
Removes all pages in the multi page, clearing it.
Base class for all widgets.
Definition widget.hpp:55
const config * cfg
static std::string _(const char *str)
Definition gettext.hpp:100
#define DBG_GUI_G
Definition log.hpp:41
#define DBG_GUI_P
Definition log.hpp:66
Generic file dialog.
std::map< std::string, widget_item > widget_data
Definition widget.hpp:36
std::map< std::string, t_string > widget_item
Definition widget.hpp:33
void swap_grid(grid *g, grid *content_grid, std::unique_ptr< widget > widget, const std::string &id)
Swaps an item in a grid for another one.
Contains the implementation details for lexical_cast and shouldn't be used directly.
std::string_view data
Definition picture.cpp:188
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
Definition sound.cpp:139
virtual std::unique_ptr< widget > build() const override
std::vector< widget_item > data
Multi page data.
std::string definition
Parameters for the styled_widget.
multi_page_definition(const config &cfg)
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
#define d