The Battle for Wesnoth  1.15.2+dev
stacked_widget.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 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 #include <boost/dynamic_bitset.hpp>
23 
24 namespace gui2
25 {
26 
27 // ------------ WIDGET -----------{
28 
29 namespace implementation
30 {
31 struct builder_stacked_widget;
32 }
33 
34 class generator_base;
35 
37 {
40  friend class debug_layout_graph;
41 
42 public:
44 
45  /***** ***** ***** inherited ***** ****** *****/
46 
47  /** See @ref styled_widget::get_active. */
48  virtual bool get_active() const override;
49 
50  /** See @ref styled_widget::get_state. */
51  virtual unsigned get_state() const override;
52 
53  /** See @ref widget::layout_children. */
54  virtual void layout_children() override;
55 
56  /**
57  * Gets the current visible layer number.
58  *
59  * The current layer number will be -1 if all layers are currently visible.
60  * In this case, only the topmost (highest-numbered) layer will receive
61  * events.
62  *
63  * If more than one but not all layers are visible, this will be the number of
64  * the last one made visible.
65  *
66  * @returns The most recently shown layer
67  */
68  int current_layer() const { return selected_layer_; }
69 
70  /**
71  * Tests if the specified layer is selected (ie, visible).
72  *
73  * @param layer The layer to test
74  * @returns True if the specified layer is selected
75  */
76  bool layer_selected(const unsigned layer);
77 
78  /**
79  * Selects and displays a particular layer.
80  *
81  * If layer -1 is selected, all layers will be displayed but only the
82  * topmost (highest-numbered) layer will receive events.
83  *
84  * @param layer The layer to select
85  */
86  void select_layer(const int layer);
87 
88  /**
89  * Selects and displays multiple layers based on the state of the provided dynamic_bitset.
90  *
91  * @param mask A mask specifying which layers to select and deselect
92  */
93  void select_layers(const boost::dynamic_bitset<>& mask);
94 
95  /**
96  * Gets the total number of layers.
97  *
98  * @returns The total number of layers
99  */
100  unsigned int get_layer_count() const;
101 
102  /**
103  * Gets the grid for a specified layer.
104  * This can be used to search for widgets in a hidden layer.
105  *
106  * @param i The layer to retrieve
107  * @returns The grid for the specified layer.
108  */
109  grid* get_layer_grid(unsigned int i);
110 
111  /** Const overload for @ref get_layer_grid. */
112  const grid* get_layer_grid(unsigned int i) const;
113 
114  void set_find_in_all_layers(const bool do_find)
115  {
116  find_in_all_layers_ = do_find;
117  }
118 
119 private:
120  /**
121  * Finishes the building initialization of the widget.
122  *
123  * @param widget_builder The builder to build the contents of the
124  * widget.
125  */
126  void finalize(std::vector<builder_grid_const_ptr> widget_builder);
127 
128  /**
129  * Contains a pointer to the generator.
130  *
131  * The pointer is not owned by this class, it's stored in the content_grid_
132  * of the scrollbar_container super class and freed when its grid is
133  * 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  widget* build() const;
209 
210  /** The builders for all layers of the stack .*/
211  std::vector<builder_grid_const_ptr> stack;
212 };
213 
214 } // namespace implementation
215 
216 // }------------ END --------------
217 
218 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
virtual widget * build() const =0
bool find_in_all_layers_
If true, find will search all layers for widgets regardless of which one is visible.
Base class for all widgets.
Definition: widget.hpp:47
int current_layer() const
Gets the current visible layer number.
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
std::vector< builder_grid_const_ptr > stack
The builders for all layers of the stack .
generator_base * generator_
Contains a pointer to the generator.
std::size_t i
Definition: function.cpp:933
window * build(const builder_window::window_resolution *definition)
Builds a window.
A generic container base class.
int selected_layer_
The number of the current selected layer.
bool find(E event, F functor)
Tests whether an event handler is available.
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
void set_find_in_all_layers(const bool do_find)
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.