The Battle for Wesnoth  1.17.0-dev
toggle_panel.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
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(surface& frame_buffer,
180  int x_offset,
181  int y_offset) override;
182 
183  /** See @ref widget::impl_draw_foreground. */
184  virtual void impl_draw_foreground(surface& frame_buffer,
185  int x_offset,
186  int y_offset) override;
187 
188 public:
189  /** Static type getter that does not rely on the widget being constructed. */
190  static const std::string& type();
191 
192 private:
193  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
194  virtual const std::string& get_control_type() const override;
195 
196  /***** ***** ***** signal handlers ***** ****** *****/
197 
198  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
199 
200  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
201 
202  void signal_handler_pre_left_button_click(const event::ui_event event);
203 
204  void signal_handler_left_button_click(const event::ui_event event,
205  bool& handled);
206 
207  void signal_handler_left_button_double_click(const event::ui_event event,
208  bool& handled);
209 };
210 
211 // }---------- DEFINITION ---------{
212 
214 {
215  explicit toggle_panel_definition(const config& cfg);
216 
218  {
219  explicit resolution(const config& cfg);
220 
221  unsigned top_border;
222  unsigned bottom_border;
223 
224  unsigned left_border;
225  unsigned right_border;
226  };
227 };
228 
229 // }---------- BUILDER -----------{
230 
231 namespace implementation
232 {
233 
235 {
236  explicit builder_toggle_panel(const config& cfg);
237 
239 
240  virtual widget* build() const override;
241 
243 
244 private:
245  std::string retval_id_;
246  int retval_;
247 };
248 
249 } // namespace implementation
250 
251 // }------------ END --------------
252 
253 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
Class for a toggle button.
Small abstract helper class.
virtual widget * build() const =0
unsigned state_num_
Usually 1 for selected and 0 for not selected, can also have higher values in tristate buttons...
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
std::string selected
state_t state_
Current state of the widget.
int retval_
The return value of the button.
bool fire_event(const ui_event event, std::vector< std::pair< widget *, ui_event >> &event_chain, widget *dispatcher, widget *w, F &&... params)
Helper function for fire_event.
state_t
Possible states of the widget.
Holds a 2D point.
Definition: point.hpp:24
point resolution()
Definition: general.cpp:393
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.
std::shared_ptr< builder_grid > builder_grid_ptr
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
ui_event
The event send to the dispatcher.
Definition: handler.hpp:48