The Battle for Wesnoth  1.15.12+dev
toggle_button.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 
19 
20 namespace gui2
21 {
22 namespace implementation
23 {
24  struct builder_toggle_button;
25 }
26 
27 // ------------ WIDGET -----------{
28 
29 /**
30  * @ingroup GUIWidgetWML
31  *
32  * Class for a toggle button.
33  *
34  * A toggle button is a button with two states 'up' and 'down' or 'selected' and 'deselected'.
35  * When the mouse is pressed on it the state changes.
36  *
37  * The following states exist:
38  * * state_enabled - the button is enabled and not selected.
39  * * state_disabled - the button is disabled and not selected.
40  * * state_focussed - the mouse is over the button and not selected.
41  * * state_enabled_selected - the button is enabled and selected.
42  * * state_disabled_selected - the button is disabled and selected.
43  * * state_focussed_selected - the mouse is over the button and selected.
44  *
45  * Variables:
46  * Key |Type |Default |Description
47  * --------------------|----------------------------------------|---------|-------------
48  * icon | @ref guivartype_string "string" |"" |The name of the icon file to show.
49  * return_value_id | @ref guivartype_string "string" |"" |The return value id.
50  * return_value | @ref guivartype_int "int" |0 |The return value.
51  */
53 {
54 public:
56 
57  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
58 
59  /** See @ref styled_widget::set_members. */
60  void set_members(const string_map& data) override;
61 
62  /** See @ref styled_widget::set_active. */
63  virtual void set_active(const bool active) override;
64 
65  /** See @ref styled_widget::get_active. */
66  virtual bool get_active() const override;
67 
68  /** See @ref styled_widget::get_state. */
69  virtual unsigned get_state() const override;
70 
71  /** Inherited from styled_widget. */
72  virtual void update_canvas() override;
73 
74  /** Inherited from selectable_item */
75  virtual unsigned get_value() const override
76  {
77  return state_num_;
78  }
79 
80  /** Inherited from selectable_item */
81  virtual unsigned num_states() const override;
82 
83  /** Inherited from selectable_item */
84  virtual void set_value(unsigned selected, bool fire_event = false) override;
85 
86  /***** ***** ***** setters / getters for members ***** ****** *****/
87 
88  void set_retval(const int retval);
89 
90  void set_icon_name(const std::string& icon_name)
91  {
92  icon_name_ = icon_name;
93  update_canvas();
94  }
95 
96  const std::string& icon_name() const
97  {
98  return icon_name_;
99  }
100 
101  /**
102  * Possible states of the widget.
103  *
104  * Note the order of the states must be the same as defined in settings.hpp.
105  * Also note the internals do assume the order for 'up' and 'down' to be the
106  * same and also that 'up' is before 'down'. 'up' has no suffix, 'down' has
107  * the SELECTED suffix.
108  */
109  enum state_t {
113  COUNT
114  };
115 
116 private:
117 
118  void set_state(const state_t state);
119 
120  /**
121  * Current state of the widget.
122  *
123  * The state of the widget determines what to render and how the widget
124  * reacts to certain 'events'.
125  */
127  /**
128  * Usually 1 for selected and 0 for not selected, can also have higher values in tristate buttons.
129  */
130  unsigned state_num_;
131  /**
132  * The return value of the button.
133  *
134  * If this value is not 0 and the button is double clicked it sets the
135  * retval of the window and the window closes itself.
136  */
137  int retval_;
138 
139  /**
140  * The toggle button can contain an icon next to the text.
141  * Maybe this will move the the styled_widget class if deemed needed.
142  */
143  std::string icon_name_;
144 
145 public:
146  /** Static type getter that does not rely on the widget being constructed. */
147  static const std::string& type();
148 
149 private:
150  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
151  virtual const std::string& get_control_type() const override;
152 
153  /***** ***** ***** signal handlers ***** ****** *****/
154 
155  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
156 
157  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
158 
159  void signal_handler_left_button_click(const event::ui_event event,
160  bool& handled);
161 
162  void signal_handler_left_button_double_click(const event::ui_event event,
163  bool& handled);
164 };
165 
166 // }---------- DEFINITION ---------{
167 
169 {
170  explicit toggle_button_definition(const config& cfg);
171 
173  {
174  explicit resolution(const config& cfg);
175  };
176 };
177 
178 // }---------- BUILDER -----------{
179 
180 namespace implementation
181 {
182 
184 {
185  explicit builder_toggle_button(const config& cfg);
186 
188 
189  virtual widget* build() const override;
190 
191 private:
192  std::string icon_name_;
193  std::string retval_id_;
194  int retval_;
195 };
196 
197 } // namespace implementation
198 
199 // }------------ END --------------
200 
201 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
Small abstract helper class.
virtual widget * build() const =0
Base class for all widgets.
Definition: widget.hpp:49
state_t state_
Current state of the widget.
Generic file dialog.
Definition: field-fwd.hpp:22
state_t
Possible states of the widget.
std::string selected
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.
unsigned state_num_
Usually 1 for selected and 0 for not selected, can also have higher values in tristate buttons...
const std::string & icon_name() const
std::string icon_name_
The toggle button can contain an icon next to the text.
std::map< std::string, t_string > string_map
Definition: widget.hpp:26
Base class for all visible items.
int retval_
The return value of the button.
point resolution()
Definition: general.cpp:387
retval
Default window/dialog return values.
Definition: retval.hpp:28
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
Class for a toggle button.
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
virtual unsigned get_value() const override
Inherited from selectable_item.
void set_icon_name(const std::string &icon_name)