The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
toggle_panel.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #ifndef GUI_WIDGETS_TOGGLE_PANEL_HPP_INCLUDED
16 #define GUI_WIDGETS_TOGGLE_PANEL_HPP_INCLUDED
17 
18 #include "gui/widgets/panel.hpp"
20 
21 namespace gui2
22 {
23 
24 // ------------ WIDGET -----------{
25 
26 /**
27  * Class for a toggle button.
28  *
29  * Quite some code looks like ttoggle_button maybe we should inherit from that
30  * but let's test first. the problem is that the toggle_button has an icon we
31  * don't want, but maybe look at refactoring later. but maybe we should also
32  * ditch the icon, not sure however since it's handy for checkboxes...
33  */
34 class ttoggle_panel : public tpanel, public tselectable_
35 {
36 public:
37  ttoggle_panel();
38 
39  /**
40  * Sets the members of the child controls.
41  *
42  * Sets the members for all controls which have the proper member id. See
43  * tcontrol::set_members for more info.
44  *
45  * @param data Map with the key value pairs to set the
46  * members.
47  */
48  void set_child_members(
49  const std::map<std::string /* widget id */, string_map>& data);
50 
51  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
52 
53  /** See @ref twidget::find_at. */
54  virtual twidget* find_at(const tpoint& coordinate,
55  const bool must_be_active) override;
56 
57  /** See @ref twidget::find_at. */
58  virtual const twidget* find_at(const tpoint& coordinate,
59  const bool must_be_active) const override;
60 
61  /** See @ref tcontrol::set_active. */
62  virtual void set_active(const bool active) override;
63 
64  /** See @ref tcontrol::get_active. */
65  virtual bool get_active() const override;
66 
67  /** See @ref tcontrol::get_state. */
68  virtual unsigned get_state() const override;
69 
70  /**
71  * See @ref tcontainer_::get_client_rect.
72  *
73  * @todo only due to the fact our definition is slightly different from
74  * tpanel_definition we need to override this function and do about the
75  * same, look at a way to 'fix' that.
76  */
77  virtual SDL_Rect get_client_rect() const override;
78 
79  /**
80  * See @ref tcontainer_::border_space.
81  *
82  * @todo only due to the fact our definition is slightly different from
83  * tpanel_definition we need to override this function and do about the
84  * same, look at a way to 'fix' that.
85  */
86  virtual tpoint border_space() const override;
87 
88  /** Inherited from tselectable_ */
89  unsigned get_value() const override
90  {
91  return state_num_;;
92  }
93 
94  /** Inherited from tselectable_ */
95  void set_value(const unsigned selected);
96 
97  /** Inherited from tselectable_ */
98  unsigned num_states() const override;
99 
100  /***** ***** ***** setters / getters for members ***** ****** *****/
101 
102  void set_retval(const int retval);
103 
104  /** Inherited from tselectable_. */
105  void set_callback_state_change(std::function<void(twidget&)> callback)
106  {
107  callback_state_change_ = callback;
108  }
109 
111  std::function<void(twidget&)> callback)
112  {
114  }
115 
116 private:
117  /**
118  * Possible states of the widget.
119  *
120  * Note the order of the states must be the same as defined in settings.hpp.
121  * Also note the internals do assume the order for 'up' and 'down' to be the
122  * same and also that 'up' is before 'down'. 'up' has no suffix, 'down' has
123  * the SELECTED suffix.
124  */
125  enum tstate {
130  };
131 
132  void set_state(const tstate state);
133 
134  /**
135  * Current state of the widget.
136  *
137  * The state of the widget determines what to render and how the widget
138  * reacts to certain 'events'.
139  */
141 
142  /**
143  * Usually 1 for selected and 0 for not selected, can also have higher values in tristate buttons.
144  */
145  unsigned state_num_;
146 
147  /**
148  * The return value of the button.
149  *
150  * If this value is not 0 and the button is double clicked it sets the
151  * retval of the window and the window closes itself.
152  */
153  int retval_;
154 
155  /** See tselectable_::set_callback_state_change. */
156  std::function<void(twidget&)> callback_state_change_;
157 
158  /** Mouse left double click callback */
159  std::function<void(twidget&)> callback_mouse_left_double_click_;
160 
161  /** See @ref twidget::impl_draw_background. */
162  virtual void impl_draw_background(surface& frame_buffer,
163  int x_offset,
164  int y_offset) override;
165 
166  /** See @ref twidget::impl_draw_foreground. */
167  virtual void impl_draw_foreground(surface& frame_buffer,
168  int x_offset,
169  int y_offset) override;
170 
171  /** See @ref tcontrol::get_control_type. */
172  virtual const std::string& get_control_type() const override;
173 
174  /***** ***** ***** signal handlers ***** ****** *****/
175 
176  void signal_handler_mouse_enter(const event::tevent event, bool& handled);
177 
178  void signal_handler_mouse_leave(const event::tevent event, bool& handled);
179 
181 
183  bool& handled);
184 
186  bool& handled);
187 };
188 
189 // }---------- DEFINITION ---------{
190 
192 {
193  explicit ttoggle_panel_definition(const config& cfg);
194 
196  {
197  explicit tresolution(const config& cfg);
198 
199  unsigned top_border;
200  unsigned bottom_border;
201 
202  unsigned left_border;
203  unsigned right_border;
204  };
205 };
206 
207 // }---------- BUILDER -----------{
208 
209 namespace implementation
210 {
211 
213 {
214  explicit tbuilder_toggle_panel(const config& cfg);
215 
217 
218  twidget* build() const;
219 
221 
222 private:
224  int retval_;
225 };
226 
227 } // namespace implementation
228 
229 // }------------ END --------------
230 
231 } // namespace gui2
232 
233 #endif
void signal_handler_mouse_enter(const event::tevent event, bool &handled)
void signal_handler_left_button_click(const event::tevent event, bool &handled)
void signal_handler_pre_left_button_click(const event::tevent event)
virtual unsigned get_state() const override
See tcontrol::get_state.
tstate state_
Current state of the widget.
void set_state(const tstate state)
virtual void impl_draw_foreground(surface &frame_buffer, int x_offset, int y_offset) override
See twidget::impl_draw_foreground.
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
Base class of a resolution, contains the common keys for a resolution.
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
void set_callback_mouse_left_double_click(std::function< void(twidget &)> callback)
virtual twidget * build() const =0
tstate
Possible states of the widget.
void signal_handler_mouse_leave(const event::tevent event, bool &handled)
std::string selected
Definition: game_config.cpp:84
tevent
The event send to the dispatcher.
Definition: handler.hpp:54
void set_retval(const int retval)
Visible container to hold multiple widgets.
Definition: panel.hpp:34
int retval_
The return value of the button.
Small abstract helper class.
Definition: selectable.hpp:32
virtual tpoint border_space() const override
See tcontainer_::border_space.
unsigned state_num_
Usually 1 for selected and 0 for not selected, can also have higher values in tristate buttons...
Holds a 2D point.
Definition: point.hpp:24
void set_value(const unsigned selected)
Inherited from tselectable_.
virtual void impl_draw_background(surface &frame_buffer, int x_offset, int y_offset) override
See twidget::impl_draw_background.
unsigned get_value() const override
Inherited from tselectable_.
void set_child_members(const std::map< std::string, string_map > &data)
Sets the members of the child controls.
unsigned num_states() const override
Inherited from tselectable_.
cl_event event
Definition: glew.h:3070
Base class for all widgets.
Definition: widget.hpp:49
void set_callback_state_change(std::function< void(twidget &)> callback)
Inherited from tselectable_.
virtual twidget * find_at(const tpoint &coordinate, const bool must_be_active) override
See twidget::find_at.
Class for a toggle button.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
virtual void set_active(const bool active) override
See tcontrol::set_active.
virtual bool get_active() const override
See tcontrol::get_active.
virtual SDL_Rect get_client_rect() const override
See tcontainer_::get_client_rect.
void signal_handler_left_button_double_click(const event::tevent event, bool &handled)
GLsizei const GLcharARB ** string
Definition: glew.h:4503
ttoggle_panel_definition(const config &cfg)
Contains the implementation details for lexical_cast and shouldn't be used directly.
virtual const std::string & get_control_type() const override
See tcontrol::get_control_type.
std::function< void(twidget &)> callback_mouse_left_double_click_
Mouse left double click callback.
std::function< void(twidget &)> callback_state_change_
See tselectable_::set_callback_state_change.