The Battle for Wesnoth  1.15.9+dev
toggle_panel.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 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 
17 #include "gui/widgets/panel.hpp"
19 
20 namespace gui2
21 {
22 
23 namespace implementation
24 {
25 struct builder_toggle_panel;
26 }
27 
28 // ------------ WIDGET -----------{
29 
30 /**
31  * @ingroup GUIWidgetWML
32  *
33  * Class for a toggle button.
34  *
35  * Quite some code looks like toggle_button maybe we should inherit from that but let's test first.
36  * the problem is that the toggle_button has an icon we don't want, but maybe look at refactoring later.
37  * but maybe we should also ditch the icon, not sure however since it's handy for checkboxes...
38  *
39  * A toggle panel is an item which can hold multiple other items.
40  * The difference between a grid and a panel is that it's possible to define how a panel looks.
41  * A grid in an invisible container to just hold the items.
42  * The toggle panel is a combination of the panel and a toggle button, it allows a toggle button with its own grid.
43  *
44  * The resolution for a toggle panel also contains the following keys:
45  * Key |Type |Default|Description
46  * -------------|------------------------------------|-------|-------------
47  * top_border | @ref guivartype_unsigned "unsigned"|0 |The size which isn't used for the client area.
48  * bottom_border| @ref guivartype_unsigned "unsigned"|0 |The size which isn't used for the client area.
49  * left_border | @ref guivartype_unsigned "unsigned"|0 |The size which isn't used for the client area.
50  * right_border | @ref guivartype_unsigned "unsigned"|0 |The size which isn't used for the client area.
51  * The following states exist:
52  * * state_enabled - the button is enabled and not selected.
53  * * state_disabled - the button is disabled and not selected.
54  * * state_focussed - the mouse is over the button and not selected.
55  * * state_enabled_selected - the button is enabled and selected.
56  * * state_disabled_selected - the button is disabled and selected.
57  * * state_focussed_selected - the mouse is over the button and selected.
58  * Variables:
59  * Key |Type |Default |Description
60  * ---------------|--------------------------------|---------|-----------
61  * grid | @ref guivartype_grid "grid" |mandatory|Defines the grid with the widgets to place on the panel.
62  * return_value_id| @ref guivartype_string "string"|"" |The return value id.
63  * return_value | @ref guivartype_int "int" |0 |The return value.
64  */
65 class toggle_panel : public panel, public selectable_item
66 {
67 public:
68  explicit toggle_panel(const implementation::builder_toggle_panel& builder);
69 
70  /**
71  * Sets the members of the child controls.
72  *
73  * Sets the members for all controls which have the proper member id. See
74  * styled_widget::set_members for more info.
75  *
76  * @param data Map with the key value pairs to set the
77  * members.
78  */
79  void set_child_members(
80  const std::map<std::string /* widget id */, string_map>& 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(surface& frame_buffer,
179  int x_offset,
180  int y_offset) override;
181 
182  /** See @ref widget::impl_draw_foreground. */
183  virtual void impl_draw_foreground(surface& frame_buffer,
184  int x_offset,
185  int y_offset) override;
186 
187 public:
188  /** Static type getter that does not rely on the widget being constructed. */
189  static const std::string& type();
190 
191 private:
192  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
193  virtual const std::string& get_control_type() const override;
194 
195  /***** ***** ***** signal handlers ***** ****** *****/
196 
197  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
198 
199  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
200 
201  void signal_handler_pre_left_button_click(const event::ui_event event);
202 
203  void signal_handler_left_button_click(const event::ui_event event,
204  bool& handled);
205 
206  void signal_handler_left_button_double_click(const event::ui_event event,
207  bool& handled);
208 };
209 
210 // }---------- DEFINITION ---------{
211 
213 {
214  explicit toggle_panel_definition(const config& cfg);
215 
217  {
218  explicit resolution(const config& cfg);
219 
220  unsigned top_border;
221  unsigned bottom_border;
222 
223  unsigned left_border;
224  unsigned right_border;
225  };
226 };
227 
228 // }---------- BUILDER -----------{
229 
230 namespace implementation
231 {
232 
234 {
235  explicit builder_toggle_panel(const config& cfg);
236 
238 
239  virtual widget* build() const override;
240 
242 
243 private:
244  std::string retval_id_;
245  int retval_;
246 };
247 
248 } // namespace implementation
249 
250 // }------------ END --------------
251 
252 } // 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:57
Base class for all widgets.
Definition: widget.hpp:49
Generic file dialog.
Definition: field-fwd.hpp:22
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:23
point resolution()
Definition: general.cpp:371
retval
Default window/dialog return values.
Definition: retval.hpp:28
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:59
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:47