The Battle for Wesnoth  1.19.2+dev
multi_page.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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 #pragma once
17 
19 
22 
23 namespace gui2
24 {
25 
26 // ------------ WIDGET -----------{
27 
28 namespace implementation
29 {
30 struct builder_multi_page;
31 }
32 
33 class generator_base;
34 
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 widget_item& item);
56 
57  /**
58  * Adds single page to the grid.
59  *
60  * This function expect a page to one multiple widget.
61  *
62  * @param item The data to send to the set_members of the
63  * widget.
64  *
65  * @param type the id of the [page_definition] that shoduol be used
66  *
67  * @param insert_pos the position where th new page is inserted, usually
68  * -1 for 'at end'
69  *
70  * @returns The grid of the newly added page.
71  */
72  grid& add_page(const std::string& type, int insert_pos, const widget_item& item);
73 
74  /**
75  * Adds single page to the grid.
76  *
77  * This function expect a page to have multiple widgets (either multiple
78  * columns or one column with multiple widgets).
79  *
80  *
81  * @param data The data to send to the set_members of the
82  * widgets. If the member id is not an empty
83  * string it is only send to the widget that
84  * has the wanted id (if any). If the member
85  * id is an empty string, it is send to all
86  * members. Having both empty and non-empty
87  * id's gives undefined behavior.
88  *
89  * @returns The grid of the newly added page.
90  */
91  grid& add_page(const widget_data& data);
92 
93  /**
94  * Adds single page to the grid.
95  *
96  * This function expect a page to have multiple widgets (either multiple
97  * columns or one column with multiple widgets).
98  *
99  *
100  * @param data The data to send to the set_members of the
101  * widgets. If the member id is not an empty
102  * string it is only send to the widget that
103  * has the wanted id (if any). If the member
104  * id is an empty string, it is send to all
105  * members. Having both empty and non-empty
106  * id's gives undefined behavior.
107  *
108  * @param type the id of the [page_definition] that should be used
109  *
110  * @param insert_pos the position where th new page is inserted, usually
111  * -1 for 'at end'
112  *
113  * @returns The grid of the newly added page.
114  */
115  grid& add_page(const std::string& type, int insert_pos, const widget_data& data);
116 
117  /**
118  * Removes a page in the multi page.
119  *
120  * @param page The page to remove, when not in
121  * range the function is ignored.
122  * @param count The number of pages to remove, 0 means all
123  * pages (starting from page).
124  */
125  void remove_page(const unsigned page, unsigned count = 1);
126 
127  /** Removes all pages in the multi page, clearing it. */
128  void clear();
129 
130  /** Returns the number of pages. */
131  unsigned get_page_count() const;
132 
133  /**
134  * Selects a page.
135  *
136  * @param page The page to select.
137  * @param select Select or deselect the page.
138  */
139  void select_page(const unsigned page, const bool select = true);
140 
141  /**
142  * Returns the selected page.
143  *
144  * @returns The selected page.
145  * @retval -1 No page selected.
146  */
147  int get_selected_page() const;
148 
149  /**
150  * Returns the grid for the page.
151  *
152  * @param page The page to get the grid from, the caller
153  * has to make sure the page is a valid page.
154  *
155  * @returns The grid of the wanted page.
156  */
157  const grid& page_grid(const unsigned page) const;
158 
159  /**
160  * Returns the grid for the page.
161  *
162  * @param page The page to get the grid from, the caller
163  * has to make sure the page is a valid page.
164  *
165  * @returns The grid of the wanted page.
166  */
167  grid& page_grid(const unsigned page);
168 
169  /***** ***** ***** inherited ***** ****** *****/
170 
171  /** See @ref styled_widget::get_active. */
172  virtual bool get_active() const override;
173 
174  /** See @ref styled_widget::get_state. */
175  virtual unsigned get_state() const override;
176 
177 private:
178  /***** ***** ***** setters / getters for members ***** ****** *****/
179 
180  void set_page_builders(const builder_grid_map& page_builders)
181  {
182  assert(!page_builders.empty());
183  page_builders_ = page_builders;
184  }
185 
186  /**
187  * Finishes the building initialization of the widget.
188  *
189  * @param generator Generator for the list
190  * @param page_data The initial data to fill the widget with.
191  */
192  void finalize(std::unique_ptr<generator_base> generator, const std::vector<widget_item>& page_data);
193 
194  /**
195  * Contains a pointer to the generator.
196  *
197  * The pointer is not owned by this class, it's stored in the content_grid_
198  * of the scrollbar_container super class and freed when it's grid is freed.
199  */
201 
202  /** Contains the builder for the new items. */
204 
205  /** See @ref widget::impl_draw_background. */
206  virtual bool impl_draw_background() 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  virtual std::unique_ptr<widget> build() const override;
246 
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<widget_item> data;
256 };
257 
258 } // namespace implementation
259 
260 // }------------ END --------------
261 
262 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
A generic container base class.
Abstract base class for the generator.
Definition: generator.hpp:39
Basic template class to generate new items.
Base container class.
Definition: grid.hpp:32
const grid & page_grid(const unsigned page) const
Returns the grid for the page.
Definition: multi_page.cpp:119
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: multi_page.cpp:136
generator_base * generator_
Contains a pointer to the generator.
Definition: multi_page.hpp:200
void remove_page(const unsigned page, unsigned count=1)
Removes a page in the multi page.
Definition: multi_page.cpp:75
grid & add_page(const widget_item &item)
Adds single page to the grid.
Definition: multi_page.cpp:43
builder_grid_map page_builders_
Contains the builder for the new items.
Definition: multi_page.hpp:203
friend class debug_layout_graph
Definition: multi_page.hpp:38
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
virtual bool get_active() const override
See styled_widget::get_active.
Definition: multi_page.cpp:131
unsigned get_page_count() const
Returns the number of pages.
Definition: multi_page.cpp:98
int get_selected_page() const
Returns the selected page.
Definition: multi_page.cpp:113
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
void set_page_builders(const builder_grid_map &page_builders)
Definition: multi_page.hpp:180
multi_page(const implementation::builder_multi_page &builder)
Definition: multi_page.cpp:36
virtual bool impl_draw_background() override
See widget::impl_draw_background.
Definition: multi_page.cpp:151
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition: multi_page.cpp:157
void finalize(std::unique_ptr< generator_base > generator, const std::vector< widget_item > &page_data)
Finishes the building initialization of the widget.
Definition: multi_page.cpp:141
void select_page(const unsigned page, const bool select=true)
Selects a page.
Definition: multi_page.cpp:104
void clear()
Removes all pages in the multi page, clearing it.
Definition: multi_page.cpp:92
Generic file dialog.
std::shared_ptr< builder_grid > builder_grid_ptr
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
std::map< std::string, builder_grid_const_ptr > builder_grid_map
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:411
Contains the implementation details for lexical_cast and shouldn't be used directly.
std::string_view data
Definition: picture.cpp:194
virtual std::unique_ptr< widget > build() const override
Definition: multi_page.cpp:224
std::vector< widget_item > data
Multi page data.
Definition: multi_page.hpp:255
virtual std::unique_ptr< widget > build() const=0
multi_page_definition(const config &cfg)
Definition: multi_page.cpp:164