The Battle for Wesnoth  1.17.4+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(
81  const std::map<std::string /* widget id */, string_map>& data);
82 
83  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
84 
85  /** See @ref widget::find_at. */
86  virtual widget* find_at(const point& coordinate,
87  const bool must_be_active) override;
88 
89  /** See @ref widget::find_at. */
90  virtual const widget* find_at(const point& coordinate,
91  const bool must_be_active) const override;
92 
93  /** See @ref styled_widget::set_active. */
94  virtual void set_active(const bool active) override;
95 
96  /** See @ref styled_widget::get_active. */
97  virtual bool get_active() const override;
98 
99  /** See @ref styled_widget::get_state. */
100  virtual unsigned get_state() const override;
101 
102  /**
103  * See @ref container_base::get_client_rect.
104  *
105  * @todo only due to the fact our definition is slightly different from
106  * panel_definition we need to override this function and do about the
107  * same, look at a way to 'fix' that.
108  */
109  virtual SDL_Rect get_client_rect() const override;
110 
111  /**
112  * See @ref container_base::border_space.
113  *
114  * @todo only due to the fact our definition is slightly different from
115  * panel_definition we need to override this function and do about the
116  * same, look at a way to 'fix' that.
117  */
118  virtual point border_space() const override;
119 
120  /** Inherited from selectable_item */
121  virtual unsigned get_value() const override
122  {
123  return state_num_;
124  }
125 
126  /** Inherited from selectable_item */
127  virtual void set_value(unsigned selected, bool fire_event = false) override;
128 
129  /** Inherited from selectable_item */
130  virtual unsigned num_states() const override;
131 
132  /***** ***** ***** setters / getters for members ***** ****** *****/
133 
134  void set_retval(const int retval);
135 
136 private:
137  /**
138  * Possible states of the widget.
139  *
140  * Note the order of the states must be the same as defined in settings.hpp.
141  * Also note the internals do assume the order for 'up' and 'down' to be the
142  * same and also that 'up' is before 'down'. 'up' has no suffix, 'down' has
143  * the SELECTED suffix.
144  */
145  enum state_t {
149  COUNT
150  };
151 
152  void set_state(const state_t state);
153 
154  /**
155  * Current state of the widget.
156  *
157  * The state of the widget determines what to render and how the widget
158  * reacts to certain 'events'.
159  */
161 
162  /**
163  * Usually 1 for selected and 0 for not selected, can also have higher values in tristate buttons.
164  */
165  unsigned state_num_;
166 
167  /**
168  * The return value of the button.
169  *
170  * If this value is not 0 and the button is double clicked it sets the
171  * retval of the window and the window closes itself.
172  */
173  int retval_;
174 
175  /** Mouse left double click callback */
176  std::function<void(widget&)> callback_mouse_left_double_click_;
177 
178  /** See @ref widget::impl_draw_background. */
179  virtual void impl_draw_background(int x_offset, int y_offset) override;
180 
181  /** See @ref widget::impl_draw_foreground. */
182  virtual void impl_draw_foreground(int x_offset, int y_offset) override;
183 
184 public:
185  /** Static type getter that does not rely on the widget being constructed. */
186  static const std::string& type();
187 
188 private:
189  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
190  virtual const std::string& get_control_type() const override;
191 
192  /***** ***** ***** signal handlers ***** ****** *****/
193 
194  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
195 
196  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
197 
198  void signal_handler_pre_left_button_click(const event::ui_event event);
199 
200  void signal_handler_left_button_click(const event::ui_event event,
201  bool& handled);
202 
203  void signal_handler_left_button_double_click(const event::ui_event event,
204  bool& handled);
205 };
206 
207 // }---------- DEFINITION ---------{
208 
210 {
211  explicit toggle_panel_definition(const config& cfg);
212 
214  {
215  explicit resolution(const config& cfg);
216 
217  unsigned top_border;
218  unsigned bottom_border;
219 
220  unsigned left_border;
221  unsigned right_border;
222  };
223 };
224 
225 // }---------- BUILDER -----------{
226 
227 namespace implementation
228 {
229 
231 {
232  explicit builder_toggle_panel(const config& cfg);
233 
235 
236  virtual std::unique_ptr<widget> build() const override;
237 
239 
240 private:
241  std::string retval_id_;
242  int retval_;
243 };
244 
245 } // namespace implementation
246 
247 // }------------ END --------------
248 
249 } // 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:49
Generic file dialog.
Definition: field-fwd.hpp:23
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:399
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.