The Battle for Wesnoth  1.15.1+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  * Class for a toggle button.
32  *
33  * Quite some code looks like toggle_button maybe we should inherit from that
34  * but let's test first. the problem is that the toggle_button has an icon we
35  * don't want, but maybe look at refactoring later. but maybe we should also
36  * ditch the icon, not sure however since it's handy for checkboxes...
37  */
38 class toggle_panel : public panel, public selectable_item
39 {
40 public:
41  explicit toggle_panel(const implementation::builder_toggle_panel& builder);
42 
43  /**
44  * Sets the members of the child controls.
45  *
46  * Sets the members for all controls which have the proper member id. See
47  * styled_widget::set_members for more info.
48  *
49  * @param data Map with the key value pairs to set the
50  * members.
51  */
52  void set_child_members(
53  const std::map<std::string /* widget id */, string_map>& data);
54 
55  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
56 
57  /** See @ref widget::find_at. */
58  virtual widget* find_at(const point& coordinate,
59  const bool must_be_active) override;
60 
61  /** See @ref widget::find_at. */
62  virtual const widget* find_at(const point& coordinate,
63  const bool must_be_active) const override;
64 
65  /** See @ref styled_widget::set_active. */
66  virtual void set_active(const bool active) override;
67 
68  /** See @ref styled_widget::get_active. */
69  virtual bool get_active() const override;
70 
71  /** See @ref styled_widget::get_state. */
72  virtual unsigned get_state() const override;
73 
74  /**
75  * See @ref container_base::get_client_rect.
76  *
77  * @todo only due to the fact our definition is slightly different from
78  * panel_definition we need to override this function and do about the
79  * same, look at a way to 'fix' that.
80  */
81  virtual SDL_Rect get_client_rect() const override;
82 
83  /**
84  * See @ref container_base::border_space.
85  *
86  * @todo only due to the fact our definition is slightly different from
87  * panel_definition we need to override this function and do about the
88  * same, look at a way to 'fix' that.
89  */
90  virtual point border_space() const override;
91 
92  /** Inherited from selectable_item */
93  virtual unsigned get_value() const override
94  {
95  return state_num_;
96  }
97 
98  /** Inherited from selectable_item */
99  virtual void set_value(unsigned selected, bool fire_event = false) override;
100 
101  /** Inherited from selectable_item */
102  virtual unsigned num_states() const override;
103 
104  /***** ***** ***** setters / getters for members ***** ****** *****/
105 
106  void set_retval(const int retval);
107 
108 private:
109  /**
110  * Possible states of the widget.
111  *
112  * Note the order of the states must be the same as defined in settings.hpp.
113  * Also note the internals do assume the order for 'up' and 'down' to be the
114  * same and also that 'up' is before 'down'. 'up' has no suffix, 'down' has
115  * the SELECTED suffix.
116  */
117  enum state_t {
121  COUNT
122  };
123 
124  void set_state(const state_t state);
125 
126  /**
127  * Current state of the widget.
128  *
129  * The state of the widget determines what to render and how the widget
130  * reacts to certain 'events'.
131  */
133 
134  /**
135  * Usually 1 for selected and 0 for not selected, can also have higher values in tristate buttons.
136  */
137  unsigned state_num_;
138 
139  /**
140  * The return value of the button.
141  *
142  * If this value is not 0 and the button is double clicked it sets the
143  * retval of the window and the window closes itself.
144  */
145  int retval_;
146 
147  /** Mouse left double click callback */
148  std::function<void(widget&)> callback_mouse_left_double_click_;
149 
150  /** See @ref widget::impl_draw_background. */
151  virtual void impl_draw_background(surface& frame_buffer,
152  int x_offset,
153  int y_offset) override;
154 
155  /** See @ref widget::impl_draw_foreground. */
156  virtual void impl_draw_foreground(surface& frame_buffer,
157  int x_offset,
158  int y_offset) override;
159 
160 public:
161  /** Static type getter that does not rely on the widget being constructed. */
162  static const std::string& type();
163 
164 private:
165  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
166  virtual const std::string& get_control_type() const override;
167 
168  /***** ***** ***** signal handlers ***** ****** *****/
169 
170  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
171 
172  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
173 
174  void signal_handler_pre_left_button_click(const event::ui_event event);
175 
176  void signal_handler_left_button_click(const event::ui_event event,
177  bool& handled);
178 
179  void signal_handler_left_button_double_click(const event::ui_event event,
180  bool& handled);
181 };
182 
183 // }---------- DEFINITION ---------{
184 
186 {
187  explicit toggle_panel_definition(const config& cfg);
188 
190  {
191  explicit resolution(const config& cfg);
192 
193  unsigned top_border;
194  unsigned bottom_border;
195 
196  unsigned left_border;
197  unsigned right_border;
198  };
199 };
200 
201 // }---------- BUILDER -----------{
202 
203 namespace implementation
204 {
205 
207 {
208  explicit builder_toggle_panel(const config& cfg);
209 
211 
212  widget* build() const;
213 
215 
216 private:
217  std::string retval_id_;
218  int retval_;
219 };
220 
221 } // namespace implementation
222 
223 // }------------ END --------------
224 
225 } // 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...
Visible container to hold multiple widgets.
Definition: panel.hpp:37
Base class for all widgets.
Definition: widget.hpp:47
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.
window * build(const builder_window::window_resolution *definition)
Builds a window.
Holds a 2D point.
Definition: point.hpp:23
point resolution()
Definition: general.cpp:373
retval
Default window/dialog return values.
Definition: retval.hpp:28
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:92
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:55