The Battle for Wesnoth  1.17.12+dev
toggle_panel.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2022
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 
18 #include "gui/widgets/panel.hpp"
20 
21 namespace gui2
22 {
23 
24 namespace implementation
25 {
26 struct builder_toggle_panel;
27 }
28 
29 // ------------ WIDGET -----------{
30 
31 /**
32  * @ingroup GUIWidgetWML
33  *
34  * Class for a toggle button.
35  *
36  * Quite some code looks like toggle_button maybe we should inherit from that but let's test first.
37  * the problem is that the toggle_button has an icon we don't want, but maybe look at refactoring later.
38  * but maybe we should also ditch the icon, not sure however since it's handy for checkboxes...
39  *
40  * A toggle panel is an item which can hold multiple other items.
41  * The difference between a grid and a panel is that it's possible to define how a panel looks.
42  * A grid in an invisible container to just hold the items.
43  * The toggle panel is a combination of the panel and a toggle button, it allows a toggle button with its own grid.
44  *
45  * The resolution for a toggle panel also contains the following keys:
46  * Key |Type |Default|Description
47  * -------------|------------------------------------|-------|-------------
48  * top_border | @ref guivartype_unsigned "unsigned"|0 |The size which isn't used for the client area.
49  * bottom_border| @ref guivartype_unsigned "unsigned"|0 |The size which isn't used for the client area.
50  * left_border | @ref guivartype_unsigned "unsigned"|0 |The size which isn't used for the client area.
51  * right_border | @ref guivartype_unsigned "unsigned"|0 |The size which isn't used for the client area.
52  * The following states exist:
53  * * state_enabled - the button is enabled and not selected.
54  * * state_disabled - the button is disabled and not selected.
55  * * state_focussed - the mouse is over the button and not selected.
56  * * state_enabled_selected - the button is enabled and selected.
57  * * state_disabled_selected - the button is disabled and selected.
58  * * state_focussed_selected - the mouse is over the button and selected.
59  * Variables:
60  * Key |Type |Default |Description
61  * ---------------|--------------------------------|---------|-----------
62  * grid | @ref guivartype_grid "grid" |mandatory|Defines the grid with the widgets to place on the panel.
63  * return_value_id| @ref guivartype_string "string"|"" |The return value id.
64  * return_value | @ref guivartype_int "int" |0 |The return value.
65  */
66 class toggle_panel : public panel, public selectable_item
67 {
68 public:
69  explicit toggle_panel(const implementation::builder_toggle_panel& builder);
70 
71  /**
72  * Sets the members of the child controls.
73  *
74  * Sets the members for all controls which have the proper member id. See
75  * styled_widget::set_members for more info.
76  *
77  * @param data Map with the key value pairs to set the
78  * members.
79  */
80  void set_child_members(const widget_data& data);
81 
82  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
83 
84  /** See @ref widget::find_at. */
85  virtual widget* find_at(const point& coordinate,
86  const bool must_be_active) override;
87 
88  /** See @ref widget::find_at. */
89  virtual const widget* find_at(const point& coordinate,
90  const bool must_be_active) const override;
91 
92  /** See @ref styled_widget::set_active. */
93  virtual void set_active(const bool active) override;
94 
95  /** See @ref styled_widget::get_active. */
96  virtual bool get_active() const override;
97 
98  /** See @ref styled_widget::get_state. */
99  virtual unsigned get_state() const override;
100 
101  /**
102  * See @ref container_base::get_client_rect.
103  *
104  * @todo only due to the fact our definition is slightly different from
105  * panel_definition we need to override this function and do about the
106  * same, look at a way to 'fix' that.
107  */
108  virtual SDL_Rect get_client_rect() const override;
109 
110  /**
111  * See @ref container_base::border_space.
112  *
113  * @todo only due to the fact our definition is slightly different from
114  * panel_definition we need to override this function and do about the
115  * same, look at a way to 'fix' that.
116  */
117  virtual point border_space() const override;
118 
119  /** Inherited from selectable_item */
120  virtual unsigned get_value() const override
121  {
122  return state_num_;
123  }
124 
125  /** Inherited from selectable_item */
126  virtual void set_value(unsigned selected, bool fire_event = false) override;
127 
128  /** Inherited from selectable_item */
129  virtual unsigned num_states() const override;
130 
131  /***** ***** ***** setters / getters for members ***** ****** *****/
132 
133  void set_retval(const int retval);
134 
135 private:
136  /**
137  * Possible states of the widget.
138  *
139  * Note the order of the states must be the same as defined in settings.hpp.
140  * Also note the internals do assume the order for 'up' and 'down' to be the
141  * same and also that 'up' is before 'down'. 'up' has no suffix, 'down' has
142  * the SELECTED suffix.
143  */
144  enum state_t {
148  COUNT
149  };
150 
151  void set_state(const state_t state);
152 
153  /**
154  * Current state of the widget.
155  *
156  * The state of the widget determines what to render and how the widget
157  * reacts to certain 'events'.
158  */
160 
161  /**
162  * Usually 1 for selected and 0 for not selected, can also have higher values in tristate buttons.
163  */
164  unsigned state_num_;
165 
166  /**
167  * The return value of the button.
168  *
169  * If this value is not 0 and the button is double clicked it sets the
170  * retval of the window and the window closes itself.
171  */
172  int retval_;
173 
174  /** Mouse left double click callback */
175  std::function<void(widget&)> callback_mouse_left_double_click_;
176 
177  /** See @ref widget::impl_draw_background. */
178  virtual void impl_draw_background() override;
179 
180  /** See @ref widget::impl_draw_foreground. */
181  virtual void impl_draw_foreground() override;
182 
183 public:
184  /** Static type getter that does not rely on the widget being constructed. */
185  static const std::string& type();
186 
187 private:
188  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
189  virtual const std::string& get_control_type() const override;
190 
191  /***** ***** ***** signal handlers ***** ****** *****/
192 
193  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
194 
195  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
196 
197  void signal_handler_pre_left_button_click(const event::ui_event event);
198 
199  void signal_handler_left_button_click(const event::ui_event event,
200  bool& handled);
201 
202  void signal_handler_left_button_double_click(const event::ui_event event,
203  bool& handled);
204 };
205 
206 // }---------- DEFINITION ---------{
207 
209 {
210  explicit toggle_panel_definition(const config& cfg);
211 
213  {
214  explicit resolution(const config& cfg);
215 
216  unsigned top_border;
217  unsigned bottom_border;
218 
219  unsigned left_border;
220  unsigned right_border;
221  };
222 };
223 
224 // }---------- BUILDER -----------{
225 
226 namespace implementation
227 {
228 
230 {
231  explicit builder_toggle_panel(const config& cfg);
232 
234 
235  virtual std::unique_ptr<widget> build() const override;
236 
238 
239 private:
240  std::string retval_id_;
241  int retval_;
242 };
243 
244 } // namespace implementation
245 
246 // }------------ END --------------
247 
248 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
Class for a toggle button.
Small abstract helper class.
unsigned state_num_
Usually 1 for selected and 0 for not selected, can also have higher values in tristate buttons...
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
A panel is a visible container to hold multiple widgets.
Definition: panel.hpp:58
Base class for all widgets.
Definition: widget.hpp:53
std::string_view data
Definition: picture.cpp:206
Generic file dialog.
bool fire_event(const ui_event event, const std::vector< std::pair< widget *, ui_event >> &event_chain, widget *dispatcher, widget *w, F &&... params)
Helper function for fire_event.
std::string selected
virtual std::unique_ptr< widget > build() const =0
state_t state_
Current state of the widget.
int retval_
The return value of the button.
state_t
Possible states of the widget.
Holds a 2D point.
Definition: point.hpp:24
std::shared_ptr< builder_grid > builder_grid_ptr
point resolution()
Definition: general.cpp:392
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:35
retval
Default window/dialog return values.
Definition: retval.hpp:29
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
virtual unsigned get_value() const override
Inherited from selectable_item.
std::function< void(widget &)> callback_mouse_left_double_click_
Mouse left double click callback.
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.