The Battle for Wesnoth  1.15.12+dev
multi_page.hpp
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 https://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 #pragma once
16 
18 
21 
22 namespace gui2
23 {
24 
25 // ------------ WIDGET -----------{
26 
27 namespace implementation
28 {
29 struct builder_multi_page;
30 }
31 
32 class generator_base;
33 
34 /**
35  * @ingroup GUIWidgetWML
36  *
37  * A multi page is a control that contains several 'pages' of which only one is visible.
38  * The pages can contain the same widgets containing the same 'kind' of data or look completely different.
39  * Key |Type |Default |Description
40  * -------------|----------------------------|---------|-----------
41  * grid | @ref guivartype_grid "grid"|mandatory|Defines the grid with the widgets to place on the page.
42  * A multipage has no states. List with the multi page specific variables:
43  * Key |Type |Default |Description
44  * ---------------|----------------------------------|---------|-----------
45  * page_definition| @ref guivartype_section "section"|mandatory|This defines how a multi page item looks. It must contain the grid definition for at least one page.
46  * page_data | @ref guivartype_section "section"|[] |A grid alike section which stores the initial data for the multi page. Every row must have the same number of columns as the 'page_definition'.
47  */
48 class multi_page : public container_base
49 {
51  friend class debug_layout_graph;
52 
53 public:
54  explicit multi_page(const implementation::builder_multi_page& builder);
55 
56  /***** ***** ***** ***** Page handling. ***** ***** ****** *****/
57 
58  /**
59  * Adds single page to the grid.
60  *
61  * This function expect a page to one multiple widget.
62  *
63  * @param item The data to send to the set_members of the
64  * widget.
65  *
66  * @returns The grid of the newly added page.
67  */
68  grid& add_page(const string_map& item);
69 
70  /**
71  * Adds single page to the grid.
72  *
73  * This function expect a page to one multiple widget.
74  *
75  * @param item The data to send to the set_members of the
76  * widget.
77  *
78  * @param type the id of the [page_definition] that shoduol be used
79  *
80  * @param insert_pos the position where th new page is inserted, usually
81  * -1 for 'at end'
82  *
83  * @returns The grid of the newly added page.
84  */
85  grid& add_page(const std::string& type, int insert_pos, const string_map& item);
86 
87  /**
88  * Adds single page to the grid.
89  *
90  * This function expect a page to have multiple widgets (either multiple
91  * columns or one column with multiple widgets).
92  *
93  *
94  * @param data The data to send to the set_members of the
95  * widgets. If the member id is not an empty
96  * string it is only send to the widget that
97  * has the wanted id (if any). If the member
98  * id is an empty string, it is send to all
99  * members. Having both empty and non-empty
100  * id's gives undefined behavior.
101  *
102  * @returns The grid of the newly added page.
103  */
104  grid& add_page(const std::map<std::string /* widget id */, string_map>& data);
105 
106  /**
107  * Adds single page to the grid.
108  *
109  * This function expect a page to have multiple widgets (either multiple
110  * columns or one column with multiple widgets).
111  *
112  *
113  * @param data The data to send to the set_members of the
114  * widgets. If the member id is not an empty
115  * string it is only send to the widget that
116  * has the wanted id (if any). If the member
117  * id is an empty string, it is send to all
118  * members. Having both empty and non-empty
119  * id's gives undefined behavior.
120  *
121  * @param type the id of the [page_definition] that should be used
122  *
123  * @param insert_pos the position where th new page is inserted, usually
124  * -1 for 'at end'
125  *
126  * @returns The grid of the newly added page.
127  */
128  grid& add_page(const std::string& type, int insert_pos, const std::map<std::string /* widget id */, string_map>& data);
129 
130  /**
131  * Removes a page in the multi page.
132  *
133  * @param page The page to remove, when not in
134  * range the function is ignored.
135  * @param count The number of pages to remove, 0 means all
136  * pages (starting from page).
137  */
138  void remove_page(const unsigned page, unsigned count = 1);
139 
140  /** Removes all pages in the multi page, clearing it. */
141  void clear();
142 
143  /** Returns the number of pages. */
144  unsigned get_page_count() const;
145 
146  /**
147  * Selects a page.
148  *
149  * @param page The page to select.
150  * @param select Select or deselect the page.
151  */
152  void select_page(const unsigned page, const bool select = true);
153 
154  /**
155  * Returns the selected page.
156  *
157  * @returns The selected page.
158  * @retval -1 No page selected.
159  */
160  int get_selected_page() const;
161 
162  /**
163  * Returns the grid for the page.
164  *
165  * @param page The page to get the grid from, the caller
166  * has to make sure the page is a valid page.
167  *
168  * @returns The grid of the wanted page.
169  */
170  const grid& page_grid(const unsigned page) const;
171 
172  /**
173  * Returns the grid for the page.
174  *
175  * @param page The page to get the grid from, the caller
176  * has to make sure the page is a valid page.
177  *
178  * @returns The grid of the wanted page.
179  */
180  grid& page_grid(const unsigned page);
181 
182  /***** ***** ***** inherited ***** ****** *****/
183 
184  /** See @ref styled_widget::get_active. */
185  virtual bool get_active() const override;
186 
187  /** See @ref styled_widget::get_state. */
188  virtual unsigned get_state() const override;
189 
190 private:
191  /***** ***** ***** setters / getters for members ***** ****** *****/
192 
193  void set_page_builders(const std::map<std::string, builder_grid_const_ptr>& page_builders)
194  {
195  assert(!page_builders.empty());
196  page_builders_ = page_builders;
197  }
198 
199  /**
200  * Finishes the building initialization of the widget.
201  *
202  * @param page_data The initial data to fill the widget with.
203  */
204  void finalize(const std::vector<string_map>& page_data);
205 
206  /**
207  * Contains a pointer to the generator.
208  *
209  * The pointer is not owned by this class, it's stored in the content_grid_
210  * of the scrollbar_container super class and freed when it's grid is
211  * freed.
212  */
214 
215  /** Contains the builder for the new items. */
216  std::map<std::string, builder_grid_const_ptr> page_builders_;
217 
218  /** See @ref widget::impl_draw_background. */
219  virtual void impl_draw_background(surface& frame_buffer,
220  int x_offset,
221  int y_offset) override;
222 
223 public:
224  /** Static type getter that does not rely on the widget being constructed. */
225  static const std::string& type();
226 
227 private:
228  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
229  virtual const std::string& get_control_type() const override;
230 
231  /** See @ref container_base::set_self_active. */
232  virtual void set_self_active(const bool active) override;
233 };
234 
235 // }---------- DEFINITION ---------{
236 
238 {
239  explicit multi_page_definition(const config& cfg);
240 
242  {
243  explicit resolution(const config& cfg);
244 
246  };
247 };
248 
249 // }---------- BUILDER -----------{
250 
251 namespace implementation
252 {
253 
255 {
256  explicit builder_multi_page(const config& cfg);
257 
259 
260  virtual widget* build() const override;
261 
262  std::map<std::string, builder_grid_const_ptr> builders;
263 
264  /**
265  * Multi page data.
266  *
267  * Contains a vector with the data to set in every cell, it's used to
268  * serialize the data in the config, so the config is no longer required.
269  */
270  std::vector<std::map<std::string, t_string>> data;
271 };
272 
273 } // namespace implementation
274 
275 // }------------ END --------------
276 
277 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
virtual widget * build() const =0
void set_page_builders(const std::map< std::string, builder_grid_const_ptr > &page_builders)
Definition: multi_page.hpp:193
Base class for all widgets.
Definition: widget.hpp:49
std::map< std::string, builder_grid_const_ptr > builders
Definition: multi_page.hpp:262
void clear(const std::string &key)
Definition: general.cpp:203
std::vector< std::map< std::string, t_string > > data
Multi page data.
Definition: multi_page.hpp:270
Generic file dialog.
Definition: field-fwd.hpp:22
Base container class.
Definition: grid.hpp:30
Abstract base class for the generator.
Definition: generator.hpp:37
generator_base * generator_
Contains a pointer to the generator.
Definition: multi_page.hpp:213
std::map< std::string, t_string > string_map
Definition: widget.hpp:26
A generic container base class.
A multi page is a control that contains several &#39;pages&#39; of which only one is visible.
Definition: multi_page.hpp:48
std::map< std::string, builder_grid_const_ptr > page_builders_
Contains the builder for the new items.
Definition: multi_page.hpp:216
point resolution()
Definition: general.cpp:387
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:59
std::shared_ptr< builder_grid > builder_grid_ptr
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:409