The Battle for Wesnoth  1.19.0-dev
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 
19 
22 
23 namespace gui2
24 {
25 namespace implementation
26 {
27  struct builder_button;
28 }
29 
30 // ------------ WIDGET -----------{
31 
32 /**
33  * Simple push button.
34  */
35 class button : public styled_widget, public clickable_item
36 {
37 public:
38  explicit button(const implementation::builder_button& builder);
39 
40  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
41 
42  /** See @ref styled_widget::set_active. */
43  virtual void set_active(const bool active) override;
44 
45  /** See @ref styled_widget::get_active. */
46  virtual bool get_active() const override;
47 
48  /** See @ref styled_widget::get_state. */
49  virtual unsigned get_state() const override;
50 
51  /** Inherited from clickable_item. */
52  virtual void connect_click_handler(const event::signal& signal) override
53  {
55  }
56 
57  /** Inherited from clickable_item. */
58  virtual void disconnect_click_handler(const event::signal& signal) override
59  {
61  }
62 
63  /***** ***** ***** setters / getters for members ***** ****** *****/
64 
65  void set_retval(const int retval)
66  {
67  retval_ = retval;
68  }
69 
70 private:
71  /**
72  * Possible states of the widget.
73  *
74  * Note the order of the states must be the same as defined in settings.hpp.
75  */
76  enum state_t {
81  };
82 
83  void set_state(const state_t state);
84  /**
85  * Current state of the widget.
86  *
87  * The state of the widget determines what to render and how the widget
88  * reacts to certain 'events'.
89  */
91 
92  /**
93  * The return value of the button.
94  *
95  * If this value is not 0 and the button is clicked it sets the retval of
96  * the window and the window closes itself.
97  */
98  int retval_;
99 
100 public:
101  /** Static type getter that does not rely on the widget being constructed. */
102  static const std::string& type();
103 
104 private:
105  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
106  virtual const std::string& get_control_type() const override;
107 
108  /***** ***** ***** signal handlers ***** ****** *****/
109 
110  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
111 
112  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
113 
115  bool& handled);
116 
118  bool& handled);
119 
121  bool& handled);
122 };
123 
124 // }---------- DEFINITION ---------{
125 
127 {
128  explicit button_definition(const config& cfg);
129 
131  {
132  explicit resolution(const config& cfg);
133  };
134 };
135 
136 // }---------- BUILDER -----------{
137 
138 namespace implementation
139 {
140 
142 {
143 public:
144  explicit builder_button(const config& cfg);
145 
147 
148  virtual std::unique_ptr<widget> build() const override;
149 
150 private:
151  std::string retval_id_;
152  int retval_;
153 };
154 
155 } // namespace implementation
156 
157 // }------------ END --------------
158 
159 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
Simple push button.
Definition: button.hpp:36
void set_retval(const int retval)
Definition: button.hpp:65
int retval_
The return value of the button.
Definition: button.hpp:98
state_t state_
Current state of the widget.
Definition: button.hpp:90
virtual void disconnect_click_handler(const event::signal &signal) override
Inherited from clickable_item.
Definition: button.hpp:58
virtual void connect_click_handler(const event::signal &signal) override
Inherited from clickable_item.
Definition: button.hpp:52
virtual bool get_active() const override
See styled_widget::get_active.
Definition: button.cpp:70
void signal_handler_mouse_enter(const event::ui_event event, bool &handled)
Definition: button.cpp:88
state_t
Possible states of the widget.
Definition: button.hpp:76
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: button.cpp:75
void set_state(const state_t state)
Definition: button.cpp:80
void signal_handler_left_button_down(const event::ui_event event, bool &handled)
Definition: button.cpp:106
button(const implementation::builder_button &builder)
Definition: button.cpp:44
void signal_handler_left_button_up(const event::ui_event event, bool &handled)
Definition: button.cpp:120
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:63
void signal_handler_mouse_leave(const event::ui_event event, bool &handled)
Definition: button.cpp:97
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
void signal_handler_left_button_click(const event::ui_event event, bool &handled)
Definition: button.cpp:129
Small concept class.
Base class for all visible items.
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
void disconnect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Disconnects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:182
dispatcher_callback<> signal
Used for events in event_category::general.
Definition: dispatcher.hpp:56
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177
Generic file dialog.
retval
Default window/dialog return values.
Definition: retval.hpp:30
Contains the implementation details for lexical_cast and shouldn't be used directly.
resolution(const config &cfg)
Definition: button.cpp:158
button_definition(const config &cfg)
Definition: button.cpp:150
builder_button(const config &cfg)
Definition: button.cpp:173
virtual std::unique_ptr< widget > build() const override
Definition: button.cpp:180
virtual std::unique_ptr< widget > build() const=0
Base class of a resolution, contains the common keys for a resolution.