The Battle for Wesnoth  1.13.11+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  int /*x_offset*/
150  ,
151  int /*y_offset*/)
152 {
153  /* DO NOTHING */
154 }
155 
156 void multi_page::set_self_active(const bool /*active*/)
157 {
158  /* DO NOTHING */
159 }
160 
161 // }---------- DEFINITION ---------{
162 
165 {
166  DBG_GUI_P << "Parsing multipage " << id << '\n';
167 
168  load_resolutions<resolution>(cfg);
169 }
170 
171 /*WIKI
172  * @page = GUIWidgetDefinitionWML
173  * @order = 1_multi_page
174  *
175  * == Multi page ==
176  *
177  * @begin{parent}{name="gui/"}
178  * @begin{tag}{name="multi_page_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
179  * @macro = multi_page_description
180  *
181  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
182  * @begin{table}{config}
183  * grid & grid & & A grid containing the widgets for main
184  * widget. $
185  * @end{table}
186  * @allow{link}{name="gui/window/resolution/grid"}
187  * @end{tag}{name="resolution"}
188  * @end{tag}{name="multi_page_definition"}
189  * @end{parent}{name="gui/"}
190  * A multipage has no states.
191  */
193  : resolution_definition(cfg), grid(nullptr)
194 {
195  const config& child = cfg.child("grid");
196  VALIDATE(child, _("No grid defined."));
197 
198  grid = std::make_shared<builder_grid>(child);
199 }
200 
201 // }---------- BUILDER -----------{
202 
203 /*WIKI_MACRO
204  * @begin{macro}{multi_page_description}
205  *
206  * A multi page is a styled_widget that contains several 'pages' of which
207  * only one is visible. The pages can contain the same widgets containing
208  * the same 'kind' of data or look completely different.
209  * @end{macro}
210  */
211 
212 /*WIKI
213  * @page = GUIWidgetInstanceWML
214  * @order = 2_multi_page
215  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
216  * @begin{tag}{name="multi_page"}{min=0}{max=-1}{super="generic/widget_instance"}
217  * == Multi page ==
218  *
219  * @macro = multi_page_description
220  *
221  * List with the multi page specific variables:
222  * @begin{table}{config}
223  * page_definition & section & & This defines how a multi page item
224  * looks. It must contain the grid
225  * definition for at least one page. $
226  *
227  * page_data & section & [] & A grid alike section which stores the
228  * initial data for the multi page. Every
229  * row must have the same number of columns
230  * as the 'page_definition'. $
231  * horizontal_scrollbar_mode & scrollbar_mode & initial_auto &
232  * Determines whether or not to show the
233  * scrollbar.
234  * vertical_scrollbar_mode & scrollbar_mode & initial_auto &
235  * Determines whether or not to show the
236  * scrollbar.
237  * @end{table}
238  * @begin{tag}{name="page_definition"}{min=0}{max=1}{super="gui/window/resolution/grid"}
239  * @end{tag}{name="page_definition"}
240  * @begin{tag}{name="page_data"}{min=0}{max=1}{super="gui/window/resolution/grid"}
241  * @end{tag}{name="page_data"}
242  * @end{tag}{name="multi_page"}
243  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
244  */
245 
246 namespace implementation
247 {
248 
249 builder_multi_page::builder_multi_page(const config& cfg)
250  : implementation::builder_styled_widget(cfg), builders(), data()
251 {
252  for (const config& page : cfg.child_range("page_definition"))
253  {
254  auto builder = std::make_shared<builder_grid>(page);
255  assert(builder);
256  builders[page["id"]] = builder;
257  }
258  VALIDATE(!builders.empty(), _("No page defined."));
259 
260  /** @todo This part is untested. */
261  const config& d = cfg.child("page_data");
262  if(!d) {
263  return;
264  }
265 
266  auto builder = builders.begin()->second;
267  for(const auto & row : d.child_range("row"))
268  {
269  unsigned col = 0;
270 
271  for(const auto & column : row.child_range("column"))
272  {
273  data.emplace_back();
274  for(const auto & i : column.attribute_range())
275  {
276  data.back()[i.first] = i.second;
277  }
278  ++col;
279  }
280 
281  VALIDATE(col == builder->cols,
282  _("'list_data' must have "
283  "the same number of columns as the 'list_definition'."));
284  }
285 }
286 
288 {
289  multi_page* widget = new multi_page(*this);
290 
291  widget->set_page_builders(builders);
292 
293  DBG_GUI_G << "Window builder: placed multi_page '" << id
294  << "' with definition '" << definition << "'.\n";
295 
296  const auto conf = widget->cast_config_to<multi_page_definition>();
297  assert(conf);
298 
299  widget->init_grid(conf->grid);
300 
301  widget->finalize(data);
302 
303  return widget;
304 }
305 
306 } // namespace implementation
307 
308 // }------------ END --------------
309 
310 } // 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:417
std::vector< char_t > string
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:360
void clear()
Removes all pages in the multi page, clearing it.
Definition: multi_page.cpp:94
unsigned get_page_count() const
Returns the number of pages.
Definition: multi_page.cpp:100
Base class for all widgets.
Definition: widget.hpp:48
std::map< std::string, builder_grid_const_ptr > builders
Definition: multi_page.hpp:242
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition: multi_page.cpp:156
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:250
#define d
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:40
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
This file contains the settings handling of the widget library.
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
virtual void clear()=0
Deletes all items.
void select_page(const unsigned page, const bool select=true)
Selectes a page.
Definition: multi_page.cpp:106
window * build(const builder_window::window_resolution *definition)
Builds a window.
virtual bool get_active() const override
See styled_widget::get_active.
Definition: multi_page.cpp:130
size_t i
Definition: function.cpp:933
std::map< std::string, t_string > string_map
Definition: widget.hpp:24
int get_selected_page() const
Returns the selected page.
Definition: multi_page.cpp:112
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:147
multi_page_definition(const config &cfg)
Definition: multi_page.cpp:163
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 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.
const grid & page_grid(const unsigned page) const
Returns the grid for the page.
Definition: multi_page.cpp:118
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
#define DBG_GUI_G
Definition: log.hpp:40
Contains the implementation details for lexical_cast and shouldn't be used directly.
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: multi_page.cpp:135