The Battle for Wesnoth  1.17.0-dev
multi_page.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
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_(generator_base::build(true, true, generator_base::independent, false))
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 string_map& 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  const std::map<std::string /* widget id */, string_map>& data)
62 {
63  assert(generator_);
64  grid& page = generator_->create_item(-1, *page_builders_.begin()->second, data, nullptr);
65 
66  return page;
67 }
68 
70  const std::string& type, int insert_pos, const std::map<std::string /* widget id */, string_map>& data)
71 {
72  assert(generator_);
73  auto it_builder = page_builders_.find(type);
74  VALIDATE(it_builder != page_builders_.end(), "invalid page type '" + type + "'");
75  return generator_->create_item(insert_pos, *it_builder->second, data, nullptr);
76 }
77 
78 void multi_page::remove_page(const unsigned page, unsigned count)
79 {
80  assert(generator_);
81 
82  if(page >= get_page_count()) {
83  return;
84  }
85 
86  if(!count || count > get_page_count()) {
87  count = get_page_count();
88  }
89 
90  for(; count; --count) {
91  generator_->delete_item(page);
92  }
93 }
94 
96 {
97  assert(generator_);
98  generator_->clear();
99 }
100 
102 {
103  assert(generator_);
104  return generator_->get_item_count();
105 }
106 
107 void multi_page::select_page(const unsigned page, const bool select)
108 {
109  if(page >= get_page_count()) {
110  throw std::invalid_argument("invalid page index");
111  }
112  assert(generator_);
113  generator_->select_item(page, select);
114 }
115 
117 {
118  assert(generator_);
119  return generator_->get_selected_item();
120 }
121 
122 const grid& multi_page::page_grid(const unsigned page) const
123 {
124  assert(generator_);
125  return generator_->item(page);
126 }
127 
128 grid& multi_page::page_grid(const unsigned page)
129 {
130  assert(generator_);
131  return generator_->item(page);
132 }
133 
135 {
136  return true;
137 }
138 
139 unsigned multi_page::get_state() const
140 {
141  return 0;
142 }
143 
144 void multi_page::finalize(const std::vector<string_map>& page_data)
145 {
146  assert(generator_);
147  generator_->create_items(-1, *page_builders_.begin()->second, page_data, nullptr);
148  swap_grid(nullptr, &get_grid(), generator_, "_content_grid");
149 }
150 
152  ,
153  int /*x_offset*/
154  ,
155  int /*y_offset*/)
156 {
157  /* DO NOTHING */
158 }
159 
160 void multi_page::set_self_active(const bool /*active*/)
161 {
162  /* DO NOTHING */
163 }
164 
165 // }---------- DEFINITION ---------{
166 
169 {
170  DBG_GUI_P << "Parsing multipage " << id << '\n';
171 
172  load_resolutions<resolution>(cfg);
173 }
174 
176  : resolution_definition(cfg), grid(nullptr)
177 {
178  const config& child = cfg.child("grid");
179  VALIDATE(child, _("No grid defined."));
180 
181  grid = std::make_shared<builder_grid>(child);
182 }
183 
184 // }---------- BUILDER -----------{
185 
186 namespace implementation
187 {
188 
189 builder_multi_page::builder_multi_page(const config& cfg)
190  : implementation::builder_styled_widget(cfg), builders(), data()
191 {
192  for (const config& page : cfg.child_range("page_definition"))
193  {
194  auto builder = std::make_shared<builder_grid>(page);
195  assert(builder);
196  builders[page["id"]] = builder;
197  }
198  VALIDATE(!builders.empty(), _("No page defined."));
199 
200  /** @todo This part is untested. */
201  const config& d = cfg.child("page_data");
202  if(!d) {
203  return;
204  }
205 
206  auto builder = builders.begin()->second;
207  for(const auto & row : d.child_range("row"))
208  {
209  unsigned col = 0;
210 
211  for(const auto & column : row.child_range("column"))
212  {
213  data.emplace_back();
214  for(const auto & i : column.attribute_range())
215  {
216  data.back()[i.first] = i.second;
217  }
218  ++col;
219  }
220 
221  VALIDATE(col == builder->cols,
222  _("'list_data' must have "
223  "the same number of columns as the 'list_definition'."));
224  }
225 }
226 
228 {
229  multi_page* widget = new multi_page(*this);
230 
231  widget->set_page_builders(builders);
232 
233  DBG_GUI_G << "Window builder: placed multi_page '" << id
234  << "' with definition '" << definition << "'.\n";
235 
236  const auto conf = widget->cast_config_to<multi_page_definition>();
237  assert(conf);
238 
239  widget->init_grid(*conf->grid);
240 
241  widget->finalize(data);
242 
243  return widget;
244 }
245 
246 } // namespace implementation
247 
248 // }------------ END --------------
249 
250 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:66
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.
void set_page_builders(const std::map< std::string, builder_grid_const_ptr > &page_builders)
Definition: multi_page.hpp:194
const grid & get_grid() const
grid & add_page(const string_map &item)
Adds single page to the grid.
Definition: multi_page.cpp:44
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:95
Base class for all widgets.
Definition: widget.hpp:49
std::map< std::string, builder_grid_const_ptr > builders
Definition: multi_page.hpp:263
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition: multi_page.cpp:160
void remove_page(const unsigned page, unsigned count=1)
Removes a page in the multi page.
Definition: multi_page.cpp:78
void finalize(const std::vector< string_map > &page_data)
Finishes the building initialization of the widget.
Definition: multi_page.cpp:144
virtual grid & item(const unsigned index)=0
Gets the grid of an item.
void swap_grid(grid *g, grid *content_grid, widget *widget, const std::string &id)
Swaps an item in a grid for another one.
std::vector< std::map< std::string, t_string > > data
Multi page data.
Definition: multi_page.hpp:271
#define d
int get_selected_page() const
Returns the selected page.
Definition: multi_page.cpp:116
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.
Abstract base class for the generator.
Definition: generator.hpp:38
#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:122
void init_grid(const builder_grid &grid_builder)
Initializes and builds the grid.
virtual void create_items(const int index, const builder_grid &list_builder, const std::vector< string_map > &data, const std::function< void(widget &)> &callback)=0
Creates one or more new item(s).
#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.
std::shared_ptr< const typename T::resolution > cast_config_to() const
Casts the current resolution definition config to the respective type of a derived widget...
virtual grid & create_item(const int index, const builder_grid &list_builder, const string_map &item_data, const std::function< void(widget &)> &callback)=0
Creates a new item.
void select_page(const unsigned page, const bool select=true)
Selects a page.
Definition: multi_page.cpp:107
virtual bool get_active() const override
See styled_widget::get_active.
Definition: multi_page.cpp:134
std::map< std::string, t_string > string_map
Definition: widget.hpp:26
A generic container base class.
virtual void impl_draw_background(surface &frame_buffer, int x_offset, int y_offset) override
See widget::impl_draw_background.
Definition: multi_page.cpp:151
multi_page_definition(const config &cfg)
Definition: multi_page.cpp:167
unsigned get_page_count() const
Returns the number of pages.
Definition: multi_page.cpp:101
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
std::map< std::string, builder_grid_const_ptr > page_builders_
Contains the builder for the new items.
Definition: multi_page.hpp:217
virtual void select_item(const unsigned index, const bool select)=0
(De)selects an item.
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
#define DBG_GUI_G
Definition: log.hpp:41
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
virtual widget * build() const override
Definition: multi_page.cpp:227
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:410
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: multi_page.cpp:139