The Battle for Wesnoth  1.19.7+dev
multi_page.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 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 
19 
23 
24 #include "gettext.hpp"
25 #include "wml_exception.hpp"
26 
27 #include <functional>
28 
29 namespace gui2
30 {
31 
32 // ------------ WIDGET -----------{
33 
34 REGISTER_WIDGET(multi_page)
35 
36 multi_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 
66 grid& 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 
82 grid& 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 
90 void 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 
119 void 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_);
131  return generator_->get_selected_item();
132 }
133 
134 const grid& multi_page::page_grid(const unsigned page) const
135 {
136  assert(generator_);
137  return generator_->item(page);
138 }
139 
140 grid& multi_page::page_grid(const unsigned page)
141 {
142  assert(generator_);
143  return generator_->item(page);
144 }
145 
147 {
148  return true;
149 }
150 
151 unsigned multi_page::get_state() const
152 {
153  return 0;
154 }
155 
157 {
158  /* DO NOTHING */
159  return true;
160 }
161 
162 void 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 
188 namespace implementation
189 {
190 
191 builder_multi_page::builder_multi_page(const config& cfg)
192  : implementation::builder_styled_widget(cfg), builders(), data()
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;
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 
229 std::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:172
child_itors child_range(config_key_type key)
Definition: config.cpp:272
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Equivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Definition: config.cpp:384
A generic container base class.
virtual void delete_item(const unsigned index)=0
Deletes 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 grid & item(const unsigned index)=0
Gets the grid of 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.
Definition: generator.cpp:1160
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.
Definition: multi_page.cpp:134
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: multi_page.cpp:151
generator_base * generator_
Contains a pointer to the generator.
Definition: multi_page.hpp:186
void remove_page(const unsigned page, unsigned count=1)
Removes a page in the multi page.
Definition: multi_page.cpp:90
grid & add_page(const widget_item &item)
Adds single page to the grid.
Definition: multi_page.cpp:58
builder_grid_map page_builders_
Contains the builder for the new items.
Definition: multi_page.hpp:189
virtual bool get_active() const override
See styled_widget::get_active.
Definition: multi_page.cpp:146
unsigned get_page_count() const
Returns the number of pages.
Definition: multi_page.cpp:113
int get_selected_page() const
Returns the selected page.
Definition: multi_page.cpp:128
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
virtual bool impl_draw_background() override
See widget::impl_draw_background.
Definition: multi_page.cpp:156
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition: multi_page.cpp:162
void select_page(const unsigned page, const bool select=true)
Selects a page.
Definition: multi_page.cpp:119
void clear()
Removes all pages in the multi page, clearing it.
Definition: multi_page.cpp:107
Base class for all widgets.
Definition: widget.hpp:55
static std::string _(const char *str)
Definition: gettext.hpp:93
#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:178
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
virtual std::unique_ptr< widget > build() const override
Definition: multi_page.cpp:229
std::vector< widget_item > data
Multi page data.
Definition: multi_page.hpp:241
std::string definition
Parameters for the styled_widget.
multi_page_definition(const config &cfg)
Definition: multi_page.cpp:169
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