The Battle for Wesnoth  1.15.0-dev
multi_page.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
21 #include "gui/widgets/settings.hpp"
24 
25 #include "gettext.hpp"
26 
27 #include "utils/functional.hpp"
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, get_control_type())
38  , generator_(generator_base::build(true, true, generator_base::independent, false))
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 string_map& 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  const std::map<std::string /* widget id */, string_map>& data)
61 {
62  assert(generator_);
63  grid& page = generator_->create_item(-1, page_builders_.begin()->second, data, nullptr);
64 
65  return page;
66 }
67 
69  const std::string& type, int insert_pos, const std::map<std::string /* widget id */, string_map>& data)
70 {
71  assert(generator_);
72  auto it_builder = page_builders_.find(type);
73  VALIDATE(it_builder != page_builders_.end(), "invalid page type '" + type + "'");
74  return generator_->create_item(insert_pos, it_builder->second, data, nullptr);
75 }
76 
77 void multi_page::remove_page(const unsigned page, unsigned count)
78 {
79  assert(generator_);
80 
81  if(page >= get_page_count()) {
82  return;
83  }
84 
85  if(!count || count > get_page_count()) {
86  count = get_page_count();
87  }
88 
89  for(; count; --count) {
90  generator_->delete_item(page);
91  }
92 }
93 
95 {
96  assert(generator_);
97  generator_->clear();
98 }
99 
101 {
102  assert(generator_);
103  return generator_->get_item_count();
104 }
105 
106 void multi_page::select_page(const unsigned page, const bool select)
107 {
108  assert(generator_);
109  generator_->select_item(page, select);
110 }
111 
113 {
114  assert(generator_);
115  return generator_->get_selected_item();
116 }
117 
118 const grid& multi_page::page_grid(const unsigned page) const
119 {
120  assert(generator_);
121  return generator_->item(page);
122 }
123 
124 grid& multi_page::page_grid(const unsigned page)
125 {
126  assert(generator_);
127  return generator_->item(page);
128 }
129 
131 {
132  return true;
133 }
134 
135 unsigned multi_page::get_state() const
136 {
137  return 0;
138 }
139 
140 void multi_page::finalize(const std::vector<string_map>& page_data)
141 {
142  assert(generator_);
143  generator_->create_items(-1, page_builders_.begin()->second, page_data, nullptr);
144  swap_grid(nullptr, &get_grid(), generator_, "_content_grid");
145 }
146 
148 {
149  /* DO NOTHING */
150 }
151 
152 void multi_page::set_self_active(const bool /*active*/)
153 {
154  /* DO NOTHING */
155 }
156 
157 // }---------- DEFINITION ---------{
158 
161 {
162  DBG_GUI_P << "Parsing multipage " << id << '\n';
163 
164  load_resolutions<resolution>(cfg);
165 }
166 
167 /*WIKI
168  * @page = GUIWidgetDefinitionWML
169  * @order = 1_multi_page
170  *
171  * == Multi page ==
172  *
173  * @begin{parent}{name="gui/"}
174  * @begin{tag}{name="multi_page_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
175  * @macro = multi_page_description
176  *
177  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
178  * @begin{table}{config}
179  * grid & grid & & A grid containing the widgets for main
180  * widget. $
181  * @end{table}
182  * @allow{link}{name="gui/window/resolution/grid"}
183  * @end{tag}{name="resolution"}
184  * @end{tag}{name="multi_page_definition"}
185  * @end{parent}{name="gui/"}
186  * A multipage has no states.
187  */
189  : resolution_definition(cfg), grid(nullptr)
190 {
191  const config& child = cfg.child("grid");
192  VALIDATE(child, _("No grid defined."));
193 
194  grid = std::make_shared<builder_grid>(child);
195 }
196 
197 // }---------- BUILDER -----------{
198 
199 /*WIKI_MACRO
200  * @begin{macro}{multi_page_description}
201  *
202  * A multi page is a styled_widget that contains several 'pages' of which
203  * only one is visible. The pages can contain the same widgets containing
204  * the same 'kind' of data or look completely different.
205  * @end{macro}
206  */
207 
208 /*WIKI
209  * @page = GUIWidgetInstanceWML
210  * @order = 2_multi_page
211  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
212  * @begin{tag}{name="multi_page"}{min=0}{max=-1}{super="generic/widget_instance"}
213  * == Multi page ==
214  *
215  * @macro = multi_page_description
216  *
217  * List with the multi page specific variables:
218  * @begin{table}{config}
219  * page_definition & section & & This defines how a multi page item
220  * looks. It must contain the grid
221  * definition for at least one page. $
222  *
223  * page_data & section & [] & A grid alike section which stores the
224  * initial data for the multi page. Every
225  * row must have the same number of columns
226  * as the 'page_definition'. $
227  * horizontal_scrollbar_mode & scrollbar_mode & initial_auto &
228  * Determines whether or not to show the
229  * scrollbar.
230  * vertical_scrollbar_mode & scrollbar_mode & initial_auto &
231  * Determines whether or not to show the
232  * scrollbar.
233  * @end{table}
234  * @begin{tag}{name="page_definition"}{min=0}{max=1}{super="gui/window/resolution/grid"}
235  * @end{tag}{name="page_definition"}
236  * @begin{tag}{name="page_data"}{min=0}{max=1}{super="gui/window/resolution/grid"}
237  * @end{tag}{name="page_data"}
238  * @end{tag}{name="multi_page"}
239  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
240  */
241 
242 namespace implementation
243 {
244 
245 builder_multi_page::builder_multi_page(const config& cfg)
246  : implementation::builder_styled_widget(cfg), builders(), data()
247 {
248  for (const config& page : cfg.child_range("page_definition"))
249  {
250  auto builder = std::make_shared<builder_grid>(page);
251  assert(builder);
252  builders[page["id"]] = builder;
253  }
254  VALIDATE(!builders.empty(), _("No page defined."));
255 
256  /** @todo This part is untested. */
257  const config& d = cfg.child("page_data");
258  if(!d) {
259  return;
260  }
261 
262  auto builder = builders.begin()->second;
263  for(const auto & row : d.child_range("row"))
264  {
265  unsigned col = 0;
266 
267  for(const auto & column : row.child_range("column"))
268  {
269  data.emplace_back();
270  for(const auto & i : column.attribute_range())
271  {
272  data.back()[i.first] = i.second;
273  }
274  ++col;
275  }
276 
277  VALIDATE(col == builder->cols,
278  _("'list_data' must have "
279  "the same number of columns as the 'list_definition'."));
280  }
281 }
282 
284 {
285  multi_page* widget = new multi_page(*this);
286 
287  widget->set_page_builders(builders);
288 
289  DBG_GUI_G << "Window builder: placed multi_page '" << id
290  << "' with definition '" << definition << "'.\n";
291 
292  const auto conf = widget->cast_config_to<multi_page_definition>();
293  assert(conf);
294 
295  widget->init_grid(conf->grid);
296 
297  widget->finalize(data);
298 
299  return widget;
300 }
301 
302 } // namespace implementation
303 
304 // }------------ END --------------
305 
306 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:68
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:423
virtual unsigned get_item_count() const =0
Returns the number of items.
virtual grid & create_item(const int index, builder_grid_const_ptr list_builder, const string_map &item_data, const std::function< void(widget &)> &callback)=0
Creates a new item.
void set_page_builders(const std::map< std::string, builder_grid_const_ptr > &page_builders)
Definition: multi_page.hpp:177
const grid & get_grid() const
grid & add_page(const string_map &item)
Adds single page to the grid.
Definition: multi_page.cpp:43
child_itors child_range(config_key_type key)
Definition: config.cpp:366
void clear()
Removes all pages in the multi page, clearing it.
Definition: multi_page.cpp:94
Base class for all widgets.
Definition: widget.hpp:47
std::map< std::string, builder_grid_const_ptr > builders
Definition: multi_page.hpp:240
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition: multi_page.cpp:152
void remove_page(const unsigned page, unsigned count=1)
Removes a page in the multi page.
Definition: multi_page.cpp:77
void finalize(const std::vector< string_map > &page_data)
Finishes the building initialization of the widget.
Definition: multi_page.cpp:140
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:248
#define d
int get_selected_page() const
Returns the selected page.
Definition: multi_page.cpp:112
Generic file dialog.
Definition: field-fwd.hpp:22
Base container class.
Definition: grid.hpp:30
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
std::string definition
Parameters for the styled_widget.
Abstract base class for the generator.
Definition: generator.hpp:39
#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:118
virtual void create_items(const int index, builder_grid_const_ptr list_builder, const std::vector< string_map > &data, const std::function< void(widget &)> &callback)=0
Creates one or more new item(s).
void init_grid(const std::shared_ptr< builder_grid > &grid_builder)
Initializes and builds the grid.
#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:198
std::size_t i
Definition: function.cpp:933
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...
void select_page(const unsigned page, const bool select=true)
Selectes a page.
Definition: multi_page.cpp:106
virtual bool get_active() const override
See styled_widget::get_active.
Definition: multi_page.cpp:130
std::map< std::string, t_string > string_map
Definition: widget.hpp:24
A generic container base class.
virtual void impl_draw_background() override
See widget::impl_draw_background.
Definition: multi_page.cpp:147
multi_page_definition(const config &cfg)
Definition: multi_page.cpp:159
unsigned get_page_count() const
Returns the number of pages.
Definition: multi_page.cpp:100
virtual void delete_item(const unsigned index)=0
Deletes an item.
The multi page class.
Definition: multi_page.hpp:35
std::map< std::string, builder_grid_const_ptr > page_builders_
Contains the builder for the new items.
Definition: multi_page.hpp:201
virtual void select_item(const unsigned index, const bool select)=0
(De)selects an item.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
#define DBG_GUI_G
Definition: log.hpp:40
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: multi_page.cpp:135