The Battle for Wesnoth  1.19.5+dev
stacked_widget.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 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 #include <boost/dynamic_bitset.hpp>
24 
25 namespace gui2
26 {
27 
28 // ------------ WIDGET -----------{
29 
30 namespace implementation
31 {
32 struct builder_stacked_widget;
33 }
34 
35 class generator_base;
36 
38 {
41  friend class debug_layout_graph;
42 
43 public:
45 
46  /***** ***** ***** inherited ***** ****** *****/
47 
48  /** See @ref styled_widget::get_active. */
49  virtual bool get_active() const override;
50 
51  /** See @ref styled_widget::get_state. */
52  virtual unsigned get_state() const override;
53 
54  /** See @ref widget::layout_children. */
55  virtual void layout_children() override;
56 
57  /**
58  * Gets the current visible layer number.
59  *
60  * The current layer number will be -1 if all layers are currently visible.
61  * In this case, only the topmost (highest-numbered) layer will receive
62  * events.
63  *
64  * If more than one but not all layers are visible, this will be the number of
65  * the last one made visible.
66  *
67  * @returns The most recently shown layer
68  */
69  int current_layer() const { return selected_layer_; }
70 
71  /**
72  * Tests if the specified layer is selected (ie, visible).
73  *
74  * @param layer The layer to test
75  * @returns True if the specified layer is selected
76  */
77  bool layer_selected(const unsigned layer);
78 
79  /**
80  * Selects and displays a particular layer.
81  *
82  * If layer -1 is selected, all layers will be displayed but only the
83  * topmost (highest-numbered) layer will receive events.
84  *
85  * @param layer The layer to select
86  */
87  void select_layer(const int layer);
88 
89  /**
90  * Selects and displays multiple layers based on the state of the provided dynamic_bitset.
91  *
92  * @param mask A mask specifying which layers to select and deselect
93  */
94  void select_layers(const boost::dynamic_bitset<>& mask);
95 
96  /**
97  * Gets the total number of layers.
98  *
99  * @returns The total number of layers
100  */
101  unsigned int get_layer_count() const;
102 
103  /**
104  * Gets the grid for a specified layer.
105  * This can be used to search for widgets in a hidden layer.
106  *
107  * @param i The layer to retrieve
108  * @returns The grid for the specified layer.
109  */
110  grid* get_layer_grid(unsigned int i);
111 
112  /** Const overload for @ref get_layer_grid. */
113  const grid* get_layer_grid(unsigned int i) const;
114 
115  void set_find_in_all_layers(const bool do_find)
116  {
117  find_in_all_layers_ = do_find;
118  }
119 
120 private:
121  /**
122  * Finishes the building initialization of the widget.
123  *
124  * @param generator Generator for the list
125  * @param widget_builders The builder to build the contents of the widget.
126  */
127  void finalize(std::unique_ptr<generator_base> generator, const std::vector<builder_grid>& widget_builders);
128 
129  /**
130  * Contains a pointer to the generator.
131  *
132  * The pointer is not owned by this class, it's stored in the content_grid_
133  * of the scrollbar_container super class and freed when its grid is freed.
134  *
135  * NOTE: the generator is initialized with has_minimum (first arg) as false,
136  * which seems a little counter-intuitive at first. After all, shouldn't the
137  * stack always have at least one layer visible? However, this allows select_layer
138  * to function correctly.
139  *
140  * If has_minimum is true, the generator policy selected (one_item) can leave
141  * multiple layers selected when selecting a new one. This is most likely due to
142  * cases where the new chosen layer comes *after* the currently selected one.
143  * In that case, the generator would not allow the interim state where no layer
144  * before the new chosen layer is reached in the loop.
145  */
147 
148  /**
149  * The number of the current selected layer.
150  */
152 
153  /**
154  * If true, @ref find will search all layers for widgets regardless of which
155  * one is visible.
156  */
158 
159  void update_selected_layer_index(const int i);
160 
161  /** Internal implementation detail for selecting layers. */
162  void select_layer_impl(std::function<bool(unsigned int i)> display_condition);
163 
164 public:
165  /** Static type getter that does not rely on the widget being constructed. */
166  static const std::string& type();
167 
168 private:
169  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
170  virtual const std::string& get_control_type() const override;
171 
172  /** See @ref container_base::set_self_active. */
173  virtual void set_self_active(const bool active) override;
174 
175 public:
176  /** See @ref widget::find. */
177  virtual widget* find(const std::string& id, const bool must_be_active) override;
178 
179  /** See @ref widget::find. */
180  virtual const widget* find(const std::string& id, const bool must_be_active) const override;
181 };
182 
183 // }---------- DEFINITION ---------{
184 
186 {
187  explicit stacked_widget_definition(const config& cfg);
188 
190  {
191  explicit resolution(const config& cfg);
192 
194  };
195 };
196 
197 // }---------- BUILDER -----------{
198 
199 namespace implementation
200 {
201 
203 {
204  explicit builder_stacked_widget(const config& cfg);
205 
207 
208  virtual std::unique_ptr<widget> build() const override;
209 
210  /** The builders for all layers of the stack .*/
211  std::vector<builder_grid> stack;
212 };
213 
214 } // namespace implementation
215 
216 // }------------ END --------------
217 
218 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
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
virtual widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
generator_base * generator_
Contains a pointer to the generator.
int current_layer() const
Gets the current visible layer number.
virtual bool get_active() const override
See styled_widget::get_active.
void select_layer_impl(std::function< bool(unsigned int i)> display_condition)
Internal implementation detail for selecting layers.
void update_selected_layer_index(const int i)
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
grid * get_layer_grid(unsigned int i)
Gets the grid for a specified layer.
friend class debug_layout_graph
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
bool layer_selected(const unsigned layer)
Tests if the specified layer is selected (ie, visible).
bool find_in_all_layers_
If true, find will search all layers for widgets regardless of which one is visible.
int selected_layer_
The number of the current selected layer.
stacked_widget(const implementation::builder_stacked_widget &builder)
unsigned int get_layer_count() const
Gets the total number of layers.
void set_find_in_all_layers(const bool do_find)
virtual unsigned get_state() const override
See styled_widget::get_state.
void finalize(std::unique_ptr< generator_base > generator, const std::vector< builder_grid > &widget_builders)
Finishes the building initialization of the widget.
void select_layers(const boost::dynamic_bitset<> &mask)
Selects and displays multiple layers based on the state of the provided dynamic_bitset.
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
virtual void layout_children() override
See widget::layout_children.
void select_layer(const int layer)
Selects and displays a particular layer.
Base class for all widgets.
Definition: widget.hpp:55
std::size_t i
Definition: function.cpp:1028
Generic file dialog.
std::shared_ptr< builder_grid > builder_grid_ptr
Contains the implementation details for lexical_cast and shouldn't be used directly.
std::vector< builder_grid > stack
The builders for all layers of the stack .
virtual std::unique_ptr< widget > build() const override
virtual std::unique_ptr< widget > build() const=0
stacked_widget_definition(const config &cfg)