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