The Battle for Wesnoth  1.17.5+dev
multi_page.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 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 
19 
22 #include "gui/widgets/settings.hpp"
25 
26 #include "gettext.hpp"
27 
28 #include <functional>
29 
30 namespace gui2
31 {
32 
33 // ------------ WIDGET -----------{
34 
35 REGISTER_WIDGET(multi_page)
36 
37 multi_page::multi_page(const implementation::builder_multi_page& builder)
38  : container_base(builder, type())
39  , generator_(nullptr)
40  , page_builders_()
41 {
42 }
43 
45 {
46  assert(generator_);
47  grid& page = generator_->create_item(-1, *page_builders_.begin()->second, item, nullptr);
48 
49  return page;
50 }
51 
52 grid& multi_page::add_page(const std::string& type, int insert_pos, const widget_item& item)
53 {
54  assert(generator_);
55  auto it_builder = page_builders_.find(type);
56  VALIDATE(it_builder != page_builders_.end(), "invalid page type '" + type + "'");
57  return generator_->create_item(insert_pos, *it_builder->second, item, nullptr);
58 }
59 
61 {
62  assert(generator_);
63  grid& page = generator_->create_item(-1, *page_builders_.begin()->second, data, nullptr);
64 
65  return page;
66 }
67 
68 grid& multi_page::add_page(const std::string& type, int insert_pos, const widget_data& data)
69 {
70  assert(generator_);
71  auto it_builder = page_builders_.find(type);
72  VALIDATE(it_builder != page_builders_.end(), "invalid page type '" + type + "'");
73  return generator_->create_item(insert_pos, *it_builder->second, data, nullptr);
74 }
75 
76 void multi_page::remove_page(const unsigned page, unsigned count)
77 {
78  assert(generator_);
79 
80  if(page >= get_page_count()) {
81  return;
82  }
83 
84  if(!count || count > get_page_count()) {
85  count = get_page_count();
86  }
87 
88  for(; count; --count) {
89  generator_->delete_item(page);
90  }
91 }
92 
94 {
95  assert(generator_);
96  generator_->clear();
97 }
98 
100 {
101  assert(generator_);
102  return generator_->get_item_count();
103 }
104 
105 void multi_page::select_page(const unsigned page, const bool select)
106 {
107  if(page >= get_page_count()) {
108  throw std::invalid_argument("invalid page index");
109  }
110  assert(generator_);
111  generator_->select_item(page, select);
112 }
113 
115 {
116  assert(generator_);
117  return generator_->get_selected_item();
118 }
119 
120 const grid& multi_page::page_grid(const unsigned page) const
121 {
122  assert(generator_);
123  return generator_->item(page);
124 }
125 
126 grid& multi_page::page_grid(const unsigned page)
127 {
128  assert(generator_);
129  return generator_->item(page);
130 }
131 
133 {
134  return true;
135 }
136 
137 unsigned multi_page::get_state() const
138 {
139  return 0;
140 }
141 
142 void multi_page::finalize(std::unique_ptr<generator_base> generator, const std::vector<widget_item>& page_data)
143 {
144  // Save our *non-owning* pointer before this gets moved into the grid.
145  generator_ = generator.get();
146  assert(generator_);
147 
148  generator->create_items(-1, *page_builders_.begin()->second, page_data, nullptr);
149  swap_grid(nullptr, &get_grid(), std::move(generator), "_content_grid");
150 }
151 
153 {
154  /* DO NOTHING */
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 << '\n';
168 
169  load_resolutions<resolution>(cfg);
170 }
171 
173  : resolution_definition(cfg), grid(nullptr)
174 {
175  const config& child = cfg.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  const config& d = cfg.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 << "'.\n";
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
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:66
std::vector< widget_item > data
Multi page data.
Definition: multi_page.hpp:269
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
virtual unsigned get_item_count() const =0
Returns the number of items.
const grid & get_grid() const
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:142
builder_grid_map page_builders_
Contains the builder for the new items.
Definition: multi_page.hpp:217
child_itors child_range(config_key_type key)
Definition: config.cpp:344
void clear()
Removes all pages in the multi page, clearing it.
Definition: multi_page.cpp:93
Base class for all widgets.
Definition: widget.hpp:52
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition: multi_page.cpp:157
std::string_view data
Definition: picture.cpp:208
void remove_page(const unsigned page, unsigned count=1)
Removes a page in the multi page.
Definition: multi_page.cpp:76
virtual grid & item(const unsigned index)=0
Gets the grid of an item.
#define d
int get_selected_page() const
Returns the selected page.
Definition: multi_page.cpp:114
static std::string _(const char *str)
Definition: gettext.hpp:93
Generic file dialog.
Definition: field-fwd.hpp:23
Base container class.
Definition: grid.hpp:31
std::string definition
Parameters for the styled_widget.
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
This file contains the settings handling of the widget library.
const grid & page_grid(const unsigned page) const
Returns the grid for the page.
Definition: multi_page.cpp:120
Basic template class to generate new items.
virtual std::unique_ptr< widget > build() const override
Definition: multi_page.cpp:224
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.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
virtual int get_selected_item() const =0
Returns the selected item.
generator_base * generator_
Contains a pointer to the generator.
Definition: multi_page.hpp:214
std::size_t i
Definition: function.cpp:967
virtual void clear()=0
Deletes all items.
void select_page(const unsigned page, const bool select=true)
Selects a page.
Definition: multi_page.cpp:105
grid & add_page(const widget_item &item)
Adds single page to the grid.
Definition: multi_page.cpp:44
virtual bool get_active() const override
See styled_widget::get_active.
Definition: multi_page.cpp:132
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.
A generic container base class.
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:1166
virtual void impl_draw_background() override
See widget::impl_draw_background.
Definition: multi_page.cpp:152
multi_page_definition(const config &cfg)
Definition: multi_page.cpp:164
unsigned get_page_count() const
Returns the number of pages.
Definition: multi_page.cpp:99
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
virtual void delete_item(const unsigned index)=0
Deletes an item.
A multi page is a control that contains several &#39;pages&#39; of which only one is visible.
Definition: multi_page.hpp:49
virtual void select_item(const unsigned index, const bool select)=0
(De)selects an item.
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
#define DBG_GUI_G
Definition: log.hpp:41
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:414
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: multi_page.cpp:137