The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
toggle_button.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_BUTTON_HPP_INCLUDED
16 #define GUI_WIDGETS_TOGGLE_BUTTON_HPP_INCLUDED
17 
18 #include "gui/widgets/control.hpp"
20 
21 namespace gui2
22 {
23 
24 // ------------ WIDGET -----------{
25 
26 /**
27  * Class for a toggle button.
28  *
29  * A toggle button is a button with two states 'up' and 'down' or 'selected' and
30  * 'deselected'. When the mouse is pressed on it the state changes.
31  */
32 class ttoggle_button : public tcontrol, public tselectable_
33 {
34 public:
36 
37  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
38 
39  /** See @ref tcontrol::set_members. */
40  void set_members(const string_map& data);
41 
42  /** See @ref tcontrol::set_active. */
43  virtual void set_active(const bool active) override;
44 
45  /** See @ref tcontrol::get_active. */
46  virtual bool get_active() const override;
47 
48  /** See @ref tcontrol::get_state. */
49  virtual unsigned get_state() const override;
50 
51  /** Inherited from tcontrol. */
52  void update_canvas();
53 
54  /** Inherited from tselectable_ */
55  unsigned get_value() const override
56  {
57  return state_num_;
58  }
59  /** Inherited from tselectable_ */
60  unsigned num_states() const override;
61  /** Inherited from tselectable_ */
62  void set_value(const unsigned selected);
63 
64  /***** ***** ***** setters / getters for members ***** ****** *****/
65 
66  void set_retval(const int retval);
67 
68  /** Inherited from tselectable_. */
69  void set_callback_state_change(std::function<void(twidget&)> callback)
70  {
71  callback_state_change_ = callback;
72  }
73 
75  {
77  update_canvas();
78  }
79  const std::string& icon_name() const
80  {
81  return icon_name_;
82  }
83 
84 private:
85  /**
86  * Possible states of the widget.
87  *
88  * Note the order of the states must be the same as defined in settings.hpp.
89  * Also note the internals do assume the order for 'up' and 'down' to be the
90  * same and also that 'up' is before 'down'. 'up' has no suffix, 'down' has
91  * the SELECTED suffix.
92  */
93  enum tstate {
98  };
99 
100  void set_state(const tstate state);
101 
102  /**
103  * Current state of the widget.
104  *
105  * The state of the widget determines what to render and how the widget
106  * reacts to certain 'events'.
107  */
109  /**
110  * Usually 1 for selected and 0 for not selected, can also have higher values in tristate buttons.
111  */
112  unsigned state_num_;
113  /**
114  * The return value of the button.
115  *
116  * If this value is not 0 and the button is double clicked it sets the
117  * retval of the window and the window closes itself.
118  */
119  int retval_;
120 
121  /** See tselectable_::set_callback_state_change. */
122  std::function<void(twidget&)> callback_state_change_;
123 
124  /**
125  * The toggle button can contain an icon next to the text.
126  * Maybe this will move the the tcontrol class if deemed needed.
127  */
129 
130  /** See @ref tcontrol::get_control_type. */
131  virtual const std::string& get_control_type() const override;
132 
133  /***** ***** ***** signal handlers ***** ****** *****/
134 
135  void signal_handler_mouse_enter(const event::tevent event, bool& handled);
136 
137  void signal_handler_mouse_leave(const event::tevent event, bool& handled);
138 
140  bool& handled);
141 
143  bool& handled);
144 };
145 
146 // }---------- DEFINITION ---------{
147 
149 {
150  explicit ttoggle_button_definition(const config& cfg);
151 
153  {
154  explicit tresolution(const config& cfg);
155  };
156 };
157 
158 // }---------- BUILDER -----------{
159 
160 namespace implementation
161 {
162 
164 {
165  explicit tbuilder_toggle_button(const config& cfg);
166 
168 
169  twidget* build() const;
170 
171 private:
174  int retval_;
175 };
176 
177 } // namespace implementation
178 
179 // }------------ END --------------
180 
181 } // namespace gui2
182 
183 #endif
virtual void set_active(const bool active) override
See tcontrol::set_active.
virtual unsigned get_state() const override
See tcontrol::get_state.
unsigned num_states() const override
Inherited from tselectable_.
void set_icon_name(const std::string &icon_name)
virtual const std::string & get_control_type() const override
See tcontrol::get_control_type.
void signal_handler_left_button_double_click(const event::tevent event, bool &handled)
const std::string & icon_name() const
tstate state_
Current state of the widget.
void signal_handler_mouse_leave(const event::tevent event, bool &handled)
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
Base class of a resolution, contains the common keys for a resolution.
Class for a toggle button.
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
void set_state(const tstate state)
tstate
Possible states of the widget.
virtual twidget * build() const =0
void update_canvas()
Inherited from tcontrol.
std::string icon_name_
The toggle button can contain an icon next to the text.
unsigned state_num_
Usually 1 for selected and 0 for not selected, can also have higher values in tristate buttons...
void set_callback_state_change(std::function< void(twidget &)> callback)
Inherited from tselectable_.
std::string selected
Definition: game_config.cpp:84
tevent
The event send to the dispatcher.
Definition: handler.hpp:54
unsigned get_value() const override
Inherited from tselectable_.
std::function< void(twidget &)> callback_state_change_
See tselectable_::set_callback_state_change.
std::map< std::string, t_string > string_map
Definition: generator.hpp:23
Small abstract helper class.
Definition: selectable.hpp:32
void signal_handler_left_button_click(const event::tevent event, bool &handled)
void set_members(const string_map &data)
See tcontrol::set_members.
void set_retval(const int retval)
virtual bool get_active() const override
See tcontrol::get_active.
Base class for all visible items.
Definition: control.hpp:34
cl_event event
Definition: glew.h:3070
void signal_handler_mouse_enter(const event::tevent event, bool &handled)
ttoggle_button_definition(const config &cfg)
Base class for all widgets.
Definition: widget.hpp:49
int retval_
The return value of the button.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
void set_value(const unsigned selected)
Inherited from tselectable_.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Contains the implementation details for lexical_cast and shouldn't be used directly.