The Battle for Wesnoth  1.15.2+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 /** The multi page class. */
35 class multi_page : public container_base
36 {
38  friend class debug_layout_graph;
39 
40 public:
41  explicit multi_page(const implementation::builder_multi_page& builder);
42 
43  /***** ***** ***** ***** Page handling. ***** ***** ****** *****/
44 
45  /**
46  * Adds single page to the grid.
47  *
48  * This function expect a page to one multiple widget.
49  *
50  * @param item The data to send to the set_members of the
51  * widget.
52  *
53  * @returns The grid of the newly added page.
54  */
55  grid& add_page(const string_map& item);
56  /**
57  * Adds single page to the grid.
58  *
59  * This function expect a page to one multiple widget.
60  *
61  * @param item The data to send to the set_members of the
62  * widget.
63  *
64  * @param type the id of the [page_definition] that shoduol be used
65  *
66  * @param insert_pos the position where th new page is inserted, usually
67  * -1 for 'at end'
68  *
69  * @returns The grid of the newly added page.
70  */
71  grid& add_page(const std::string& type, int insert_pos, const string_map& item);
72 
73  /**
74  * Adds single page to the grid.
75  *
76  * This function expect a page to have multiple widgets (either multiple
77  * columns or one column with multiple widgets).
78  *
79  *
80  * @param data The data to send to the set_members of the
81  * widgets. If the member id is not an empty
82  * string it is only send to the widget that
83  * has the wanted id (if any). If the member
84  * id is an empty string, it is send to all
85  * members. Having both empty and non-empty
86  * id's gives undefined behavior.
87  *
88  * @returns The grid of the newly added page.
89  */
90  grid& add_page(const std::map<std::string /* widget id */, string_map>& data);
91  /**
92  * Adds single page to the grid.
93  *
94  * This function expect a page to have multiple widgets (either multiple
95  * columns or one column with multiple widgets).
96  *
97  *
98  * @param data The data to send to the set_members of the
99  * widgets. If the member id is not an empty
100  * string it is only send to the widget that
101  * has the wanted id (if any). If the member
102  * id is an empty string, it is send to all
103  * members. Having both empty and non-empty
104  * id's gives undefined behavior.
105  *
106  * @param type the id of the [page_definition] that shoduol be used
107  *
108  * @param insert_pos the position where th new page is inserted, usually
109  * -1 for 'at end'
110  *
111  * @returns The grid of the newly added page.
112  */
113  grid& add_page(const std::string& type, int insert_pos, const std::map<std::string /* widget id */, string_map>& data);
114 
115  /**
116  * Removes a page in the multi page.
117  *
118  * @param page The page to remove, when not in
119  * range the function is ignored.
120  * @param count The number of pages to remove, 0 means all
121  * pages (starting from page).
122  */
123  void remove_page(const unsigned page, unsigned count = 1);
124 
125  /** Removes all pages in the multi page, clearing it. */
126  void clear();
127 
128  /** Returns the number of pages. */
129  unsigned get_page_count() const;
130 
131  /**
132  * Selectes a page.
133  *
134  * @param page The page to select.
135  * @param select Select or deselect the page.
136  */
137  void select_page(const unsigned page, const bool select = true);
138 
139  /**
140  * Returns the selected page.
141  *
142  * @returns The selected page.
143  * @retval -1 No page selected.
144  */
145  int get_selected_page() const;
146 
147  /**
148  * Returns the grid for the page.
149  *
150  * @param page The page to get the grid from, the caller
151  * has to make sure the page is a valid page.
152  *
153  * @returns The grid of the wanted page.
154  */
155  const grid& page_grid(const unsigned page) const;
156 
157  /**
158  * Returns the grid for the page.
159  *
160  * @param page The page to get the grid from, the caller
161  * has to make sure the page is a valid page.
162  *
163  * @returns The grid of the wanted page.
164  */
165  grid& page_grid(const unsigned page);
166 
167  /***** ***** ***** inherited ***** ****** *****/
168 
169  /** See @ref styled_widget::get_active. */
170  virtual bool get_active() const override;
171 
172  /** See @ref styled_widget::get_state. */
173  virtual unsigned get_state() const override;
174 
175  /***** ***** ***** setters / getters for members ***** ****** *****/
176 
177  void set_page_builders(const std::map<std::string, builder_grid_const_ptr>& page_builders)
178  {
179  assert(!page_builders.empty());
180  page_builders_ = page_builders;
181  }
182 
183 private:
184  /**
185  * Finishes the building initialization of the widget.
186  *
187  * @param page_data The initial data to fill the widget with.
188  */
189  void finalize(const std::vector<string_map>& page_data);
190 
191  /**
192  * Contains a pointer to the generator.
193  *
194  * The pointer is not owned by this class, it's stored in the content_grid_
195  * of the scrollbar_container super class and freed when it's grid is
196  * freed.
197  */
199 
200  /** Contains the builder for the new items. */
201  std::map<std::string, builder_grid_const_ptr> page_builders_;
202 
203  /** See @ref widget::impl_draw_background. */
204  virtual void impl_draw_background(surface& frame_buffer,
205  int x_offset,
206  int y_offset) override;
207 
208 public:
209  /** Static type getter that does not rely on the widget being constructed. */
210  static const std::string& type();
211 
212 private:
213  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
214  virtual const std::string& get_control_type() const override;
215 
216  /** See @ref container_base::set_self_active. */
217  virtual void set_self_active(const bool active) override;
218 };
219 
220 // }---------- DEFINITION ---------{
221 
223 {
224  explicit multi_page_definition(const config& cfg);
225 
227  {
228  explicit resolution(const config& cfg);
229 
231  };
232 };
233 
234 // }---------- BUILDER -----------{
235 
236 namespace implementation
237 {
238 
240 {
241  explicit builder_multi_page(const config& cfg);
242 
244 
245  widget* build() const;
246 
247  std::map<std::string, builder_grid_const_ptr> builders;
248 
249  /**
250  * Multi page data.
251  *
252  * Contains a vector with the data to set in every cell, it's used to
253  * serialize the data in the config, so the config is no longer required.
254  */
255  std::vector<std::map<std::string, t_string>> data;
256 };
257 
258 } // namespace implementation
259 
260 // }------------ END --------------
261 
262 } // 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:177
Base class for all widgets.
Definition: widget.hpp:47
std::map< std::string, builder_grid_const_ptr > builders
Definition: multi_page.hpp:247
void clear(const std::string &key)
Definition: general.cpp:205
std::vector< std::map< std::string, t_string > > data
Multi page data.
Definition: multi_page.hpp:255
Generic file dialog.
Definition: field-fwd.hpp:22
Base container class.
Definition: grid.hpp:30
Abstract base class for the generator.
Definition: generator.hpp:39
generator_base * generator_
Contains a pointer to the generator.
Definition: multi_page.hpp:198
window * build(const builder_window::window_resolution *definition)
Builds a window.
std::map< std::string, t_string > string_map
Definition: widget.hpp:24
A generic container base class.
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
point resolution()
Definition: general.cpp:373
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
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:371