The Battle for Wesnoth  1.19.0-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 
24 
25 #include "gettext.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_()
40 {
41 }
42 
44 {
45  assert(generator_);
46  grid& page = generator_->create_item(-1, *page_builders_.begin()->second, item, nullptr);
47 
48  return page;
49 }
50 
51 grid& multi_page::add_page(const std::string& type, int insert_pos, const widget_item& item)
52 {
53  assert(generator_);
54  auto it_builder = page_builders_.find(type);
55  VALIDATE(it_builder != page_builders_.end(), "invalid page type '" + type + "'");
56  return generator_->create_item(insert_pos, *it_builder->second, item, nullptr);
57 }
58 
60 {
61  assert(generator_);
62  grid& page = generator_->create_item(-1, *page_builders_.begin()->second, data, nullptr);
63 
64  return page;
65 }
66 
67 grid& multi_page::add_page(const std::string& type, int insert_pos, const widget_data& data)
68 {
69  assert(generator_);
70  auto it_builder = page_builders_.find(type);
71  VALIDATE(it_builder != page_builders_.end(), "invalid page type '" + type + "'");
72  return generator_->create_item(insert_pos, *it_builder->second, data, nullptr);
73 }
74 
75 void multi_page::remove_page(const unsigned page, unsigned count)
76 {
77  assert(generator_);
78 
79  if(page >= get_page_count()) {
80  return;
81  }
82 
83  if(!count || count > get_page_count()) {
84  count = get_page_count();
85  }
86 
87  for(; count; --count) {
88  generator_->delete_item(page);
89  }
90 }
91 
93 {
94  assert(generator_);
95  generator_->clear();
96 }
97 
99 {
100  assert(generator_);
101  return generator_->get_item_count();
102 }
103 
104 void multi_page::select_page(const unsigned page, const bool select)
105 {
106  if(page >= get_page_count()) {
107  throw std::invalid_argument("invalid page index");
108  }
109  assert(generator_);
110  generator_->select_item(page, select);
111 }
112 
114 {
115  assert(generator_);
116  return generator_->get_selected_item();
117 }
118 
119 const grid& multi_page::page_grid(const unsigned page) const
120 {
121  assert(generator_);
122  return generator_->item(page);
123 }
124 
125 grid& multi_page::page_grid(const unsigned page)
126 {
127  assert(generator_);
128  return generator_->item(page);
129 }
130 
132 {
133  return true;
134 }
135 
136 unsigned multi_page::get_state() const
137 {
138  return 0;
139 }
140 
141 void multi_page::finalize(std::unique_ptr<generator_base> generator, const std::vector<widget_item>& page_data)
142 {
143  // Save our *non-owning* pointer before this gets moved into the grid.
144  generator_ = generator.get();
145  assert(generator_);
146 
147  generator->create_items(-1, *page_builders_.begin()->second, page_data, nullptr);
148  swap_grid(nullptr, &get_grid(), std::move(generator), "_content_grid");
149 }
150 
152 {
153  /* DO NOTHING */
154  return true;
155 }
156 
157 void multi_page::set_self_active(const bool /*active*/)
158 {
159  /* DO NOTHING */
160 }
161 
162 // }---------- DEFINITION ---------{
163 
166 {
167  DBG_GUI_P << "Parsing multipage " << id;
168 
169  load_resolutions<resolution>(cfg);
170 }
171 
173  : resolution_definition(cfg), grid(nullptr)
174 {
175  auto child = cfg.optional_child("grid");
176  VALIDATE(child, _("No grid defined."));
177 
178  grid = std::make_shared<builder_grid>(*child);
179 }
180 
181 // }---------- BUILDER -----------{
182 
183 namespace implementation
184 {
185 
186 builder_multi_page::builder_multi_page(const config& cfg)
187  : implementation::builder_styled_widget(cfg), builders(), data()
188 {
189  for (const config& page : cfg.child_range("page_definition"))
190  {
191  auto builder = std::make_shared<builder_grid>(page);
192  assert(builder);
193  builders[page["id"]] = builder;
194  }
195  VALIDATE(!builders.empty(), _("No page defined."));
196 
197  /** @todo This part is untested. */
198  auto d = cfg.optional_child("page_data");
199  if(!d) {
200  return;
201  }
202 
203  auto builder = builders.begin()->second;
204  for(const auto & row : d->child_range("row"))
205  {
206  unsigned col = 0;
207 
208  for(const auto & column : row.child_range("column"))
209  {
210  data.emplace_back();
211  for(const auto & i : column.attribute_range())
212  {
213  data.back()[i.first] = i.second;
214  }
215  ++col;
216  }
217 
218  VALIDATE(col == builder->cols,
219  _("'list_data' must have "
220  "the same number of columns as the 'list_definition'."));
221  }
222 }
223 
224 std::unique_ptr<widget> builder_multi_page::build() const
225 {
226  auto widget = std::make_unique<multi_page>(*this);
227 
228  widget->set_page_builders(builders);
229 
230  DBG_GUI_G << "Window builder: placed multi_page '" << id
231  << "' with definition '" << definition << "'.";
232 
233  const auto conf = widget->cast_config_to<multi_page_definition>();
234  assert(conf);
235 
236  widget->init_grid(*conf->grid);
237 
239  widget->finalize(std::move(generator), data);
240 
241  return widget;
242 }
243 
244 } // namespace implementation
245 
246 // }------------ END --------------
247 
248 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
child_itors child_range(config_key_type key)
Definition: config.cpp:273
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
A generic container base class.
const grid & get_grid() const
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:1162
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
A multi page is a control that contains several 'pages' of which only one is visible.
Definition: multi_page.hpp:50
const grid & page_grid(const unsigned page) const
Returns the grid for the page.
Definition: multi_page.cpp:119
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: multi_page.cpp:136
generator_base * generator_
Contains a pointer to the generator.
Definition: multi_page.hpp:214
void remove_page(const unsigned page, unsigned count=1)
Removes a page in the multi page.
Definition: multi_page.cpp:75
grid & add_page(const widget_item &item)
Adds single page to the grid.
Definition: multi_page.cpp:43
builder_grid_map page_builders_
Contains the builder for the new items.
Definition: multi_page.hpp:217
virtual bool get_active() const override
See styled_widget::get_active.
Definition: multi_page.cpp:131
unsigned get_page_count() const
Returns the number of pages.
Definition: multi_page.cpp:98
int get_selected_page() const
Returns the selected page.
Definition: multi_page.cpp:113
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:151
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition: multi_page.cpp:157
void finalize(std::unique_ptr< generator_base > generator, const std::vector< widget_item > &page_data)
Finishes the building initialization of the widget.
Definition: multi_page.cpp:141
void select_page(const unsigned page, const bool select=true)
Selects a page.
Definition: multi_page.cpp:104
void clear()
Removes all pages in the multi page, clearing it.
Definition: multi_page.cpp:92
Base class for all widgets.
Definition: widget.hpp:53
std::size_t i
Definition: function.cpp:968
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:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
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.
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:412
Contains the implementation details for lexical_cast and shouldn't be used directly.
std::string_view data
Definition: picture.cpp:194
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
virtual std::unique_ptr< widget > build() const override
Definition: multi_page.cpp:224
std::vector< widget_item > data
Multi page data.
Definition: multi_page.hpp:269
std::string definition
Parameters for the styled_widget.
multi_page_definition(const config &cfg)
Definition: multi_page.cpp:164
Base class of a resolution, contains the common keys for a resolution.
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
#define d