The Battle for Wesnoth  1.19.0-dev
toggle_button.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
20 
21 namespace gui2
22 {
23 namespace implementation
24 {
25  struct builder_toggle_button;
26 }
27 
28 // ------------ WIDGET -----------{
29 
30 /**
31  * @ingroup GUIWidgetWML
32  *
33  * Class for a toggle button.
34  *
35  * A toggle button is a button with two states 'up' and 'down' or 'selected' and 'deselected'.
36  * When the mouse is pressed on it the state changes.
37  *
38  * The following states exist:
39  * * state_enabled - the button is enabled and not selected.
40  * * state_disabled - the button is disabled and not selected.
41  * * state_focussed - the mouse is over the button and not selected.
42  * * state_enabled_selected - the button is enabled and selected.
43  * * state_disabled_selected - the button is disabled and selected.
44  * * state_focussed_selected - the mouse is over the button and selected.
45  *
46  * Variables:
47  * Key |Type |Default |Description
48  * --------------------|----------------------------------------|---------|-------------
49  * icon | @ref guivartype_string "string" |"" |The name of the icon file to show.
50  * return_value_id | @ref guivartype_string "string" |"" |The return value id.
51  * return_value | @ref guivartype_int "int" |0 |The return value.
52  */
54 {
55 public:
57 
58  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
59 
60  /** See @ref styled_widget::set_members. */
61  void set_members(const widget_item& data) override;
62 
63  /** See @ref styled_widget::set_active. */
64  virtual void set_active(const bool active) override;
65 
66  /** See @ref styled_widget::get_active. */
67  virtual bool get_active() const override;
68 
69  /** See @ref styled_widget::get_state. */
70  virtual unsigned get_state() const override;
71 
72  /** Inherited from styled_widget. */
73  virtual void update_canvas() override;
74 
75  /** Inherited from selectable_item */
76  virtual unsigned get_value() const override
77  {
78  return state_num_;
79  }
80 
81  /** Inherited from selectable_item */
82  virtual unsigned num_states() const override;
83 
84  /** Inherited from selectable_item */
85  virtual void set_value(unsigned selected, bool fire_event = false) override;
86 
87  /***** ***** ***** setters / getters for members ***** ****** *****/
88 
89  void set_retval(const int retval);
90 
91  void set_icon_name(const std::string& icon_name)
92  {
94  update_canvas();
95  }
96 
97  const std::string& icon_name() const
98  {
99  return icon_name_;
100  }
101 
102  /**
103  * Possible states of the widget.
104  *
105  * Note the order of the states must be the same as defined in settings.hpp.
106  * Also note the internals do assume the order for 'up' and 'down' to be the
107  * same and also that 'up' is before 'down'. 'up' has no suffix, 'down' has
108  * the SELECTED suffix.
109  */
110  enum state_t {
114  COUNT
115  };
116 
117 private:
118 
119  void set_state(const state_t state);
120 
121  /**
122  * Current state of the widget.
123  *
124  * The state of the widget determines what to render and how the widget
125  * reacts to certain 'events'.
126  */
128  /**
129  * Usually 1 for selected and 0 for not selected, can also have higher values in tristate buttons.
130  */
131  unsigned state_num_;
132  /**
133  * The return value of the button.
134  *
135  * If this value is not 0 and the button is double clicked it sets the
136  * retval of the window and the window closes itself.
137  */
138  int retval_;
139 
140  /**
141  * The toggle button can contain an icon next to the text.
142  * Maybe this will move the the styled_widget class if deemed needed.
143  */
144  std::string icon_name_;
145 
146 public:
147  /** Static type getter that does not rely on the widget being constructed. */
148  static const std::string& type();
149 
150 private:
151  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
152  virtual const std::string& get_control_type() const override;
153 
154  /***** ***** ***** signal handlers ***** ****** *****/
155 
156  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
157 
158  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
159 
161  bool& handled);
162 
164  bool& handled);
165 };
166 
167 // }---------- DEFINITION ---------{
168 
170 {
171  explicit toggle_button_definition(const config& cfg);
172 
174  {
175  explicit resolution(const config& cfg);
176  };
177 };
178 
179 // }---------- BUILDER -----------{
180 
181 namespace implementation
182 {
183 
185 {
186  explicit builder_toggle_button(const config& cfg);
187 
189 
190  virtual std::unique_ptr<widget> build() const override;
191 
192 private:
193  std::string icon_name_;
194  std::string retval_id_;
195  int retval_;
196 };
197 
198 } // namespace implementation
199 
200 // }------------ END --------------
201 
202 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
Small abstract helper class.
Base class for all visible items.
Class for a toggle button.
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
void set_state(const state_t state)
virtual void set_value(unsigned selected, bool fire_event=false) override
Inherited from selectable_item.
void signal_handler_mouse_enter(const event::ui_event event, bool &handled)
void signal_handler_mouse_leave(const event::ui_event event, bool &handled)
virtual void set_active(const bool active) override
See styled_widget::set_active.
virtual unsigned get_state() const override
See styled_widget::get_state.
void set_members(const widget_item &data) override
See styled_widget::set_members.
unsigned state_num_
Usually 1 for selected and 0 for not selected, can also have higher values in tristate buttons.
state_t
Possible states of the widget.
virtual void update_canvas() override
Inherited from styled_widget.
virtual unsigned get_value() const override
Inherited from selectable_item.
void signal_handler_left_button_click(const event::ui_event event, bool &handled)
std::string icon_name_
The toggle button can contain an icon next to the text.
int retval_
The return value of the button.
void signal_handler_left_button_double_click(const event::ui_event event, bool &handled)
virtual unsigned num_states() const override
Inherited from selectable_item.
void set_retval(const int retval)
toggle_button(const implementation::builder_toggle_button &builder)
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
virtual bool get_active() const override
See styled_widget::get_active.
state_t state_
Current state of the widget.
void set_icon_name(const std::string &icon_name)
const std::string & icon_name() const
std::string selected
bool fire_event(const ui_event event, const std::vector< std::pair< widget *, ui_event >> &event_chain, widget *dispatcher, widget *w, F &&... params)
Helper function for fire_event.
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
Generic file dialog.
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
retval
Default window/dialog return values.
Definition: retval.hpp:30
Contains the implementation details for lexical_cast and shouldn't be used directly.
std::string_view data
Definition: picture.cpp:194
virtual std::unique_ptr< widget > build() const=0
virtual std::unique_ptr< widget > build() const override
Base class of a resolution, contains the common keys for a resolution.
toggle_button_definition(const config &cfg)