The Battle for Wesnoth  1.19.2+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 
31 {
32 public:
34 
35  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
36 
37  /** See @ref styled_widget::set_members. */
38  void set_members(const widget_item& data) override;
39 
40  /** See @ref styled_widget::set_active. */
41  virtual void set_active(const bool active) override;
42 
43  /** See @ref styled_widget::get_active. */
44  virtual bool get_active() const override;
45 
46  /** See @ref styled_widget::get_state. */
47  virtual unsigned get_state() const override;
48 
49  /** Inherited from styled_widget. */
50  virtual void update_canvas() override;
51 
52  /** Inherited from selectable_item */
53  virtual unsigned get_value() const override
54  {
55  return state_num_;
56  }
57 
58  /** Inherited from selectable_item */
59  virtual unsigned num_states() const override;
60 
61  /** Inherited from selectable_item */
62  virtual void set_value(unsigned selected, bool fire_event = false) override;
63 
64  /***** ***** ***** setters / getters for members ***** ****** *****/
65 
66  void set_retval(const int retval);
67 
68  void set_icon_name(const std::string& icon_name)
69  {
71  update_canvas();
72  }
73 
74  const std::string& icon_name() const
75  {
76  return icon_name_;
77  }
78 
79  /**
80  * Possible states of the widget.
81  *
82  * Note the order of the states must be the same as defined in settings.hpp.
83  * Also note the internals do assume the order for 'up' and 'down' to be the
84  * same and also that 'up' is before 'down'. 'up' has no suffix, 'down' has
85  * the SELECTED suffix.
86  */
87  enum state_t {
91  COUNT
92  };
93 
94 private:
95 
96  void set_state(const state_t state);
97 
98  /**
99  * Current state of the widget.
100  *
101  * The state of the widget determines what to render and how the widget
102  * reacts to certain 'events'.
103  */
105  /**
106  * Usually 1 for selected and 0 for not selected, can also have higher values in tristate buttons.
107  */
108  unsigned state_num_;
109  /**
110  * The return value of the button.
111  *
112  * If this value is not 0 and the button is double clicked it sets the
113  * retval of the window and the window closes itself.
114  */
115  int retval_;
116 
117  /**
118  * The toggle button can contain an icon next to the text.
119  * Maybe this will move the the styled_widget class if deemed needed.
120  */
121  std::string icon_name_;
122 
123 public:
124  /** Static type getter that does not rely on the widget being constructed. */
125  static const std::string& type();
126 
127 private:
128  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
129  virtual const std::string& get_control_type() const override;
130 
131  /***** ***** ***** signal handlers ***** ****** *****/
132 
133  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
134 
135  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
136 
138  bool& handled);
139 
141  bool& handled);
142 };
143 
144 // }---------- DEFINITION ---------{
145 
147 {
148  explicit toggle_button_definition(const config& cfg);
149 
151  {
152  explicit resolution(const config& cfg);
153  };
154 };
155 
156 // }---------- BUILDER -----------{
157 
158 namespace implementation
159 {
160 
162 {
163  explicit builder_toggle_button(const config& cfg);
164 
166 
167  virtual std::unique_ptr<widget> build() const override;
168 
169 private:
170  std::string icon_name_;
171  std::string retval_id_;
172  int retval_;
173 };
174 
175 } // namespace implementation
176 
177 // }------------ END --------------
178 
179 } // 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.
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
toggle_button_definition(const config &cfg)