The Battle for Wesnoth  1.19.5+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  void set_success(bool success);
52 
53  /** Inherited from clickable_item. */
54  virtual void connect_click_handler(const event::signal& signal) override
55  {
57  }
58 
59  /** Inherited from clickable_item. */
60  virtual void disconnect_click_handler(const event::signal& signal) override
61  {
63  }
64 
65  /***** ***** ***** setters / getters for members ***** ****** *****/
66 
67  void set_retval(const int retval)
68  {
69  retval_ = retval;
70  }
71 
72 private:
73  /**
74  * Possible states of the widget.
75  *
76  * Note the order of the states must be the same as defined in settings.hpp.
77  */
78  enum state_t {
83  SUCCESS
84  };
85 
86  void set_state(const state_t state);
87  /**
88  * Current state of the widget.
89  *
90  * The state of the widget determines what to render and how the widget
91  * reacts to certain 'events'.
92  */
94 
95  /**
96  * The return value of the button.
97  *
98  * If this value is not 0 and the button is clicked it sets the retval of
99  * the window and the window closes itself.
100  */
101  int retval_;
102 
103  /**
104  * Action performed by this button succeeded.
105  */
106  bool success_;
107 
108 public:
109  /** Static type getter that does not rely on the widget being constructed. */
110  static const std::string& type();
111 
112 private:
113  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
114  virtual const std::string& get_control_type() const override;
115 
116  /***** ***** ***** signal handlers ***** ****** *****/
117 
118  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
119 
120  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
121 
123  bool& handled);
124 
126  bool& handled);
127 
129  bool& handled);
130 };
131 
132 // }---------- DEFINITION ---------{
133 
135 {
136  explicit button_definition(const config& cfg);
137 
139  {
140  explicit resolution(const config& cfg);
141  };
142 };
143 
144 // }---------- BUILDER -----------{
145 
146 namespace implementation
147 {
148 
150 {
151 public:
152  explicit builder_button(const config& cfg);
153 
155 
156  virtual std::unique_ptr<widget> build() const override;
157 
158 private:
159  std::string retval_id_;
160  int retval_;
161 };
162 
163 } // namespace implementation
164 
165 // }------------ END --------------
166 
167 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Simple push button.
Definition: button.hpp:36
void set_retval(const int retval)
Definition: button.hpp:67
int retval_
The return value of the button.
Definition: button.hpp:101
state_t state_
Current state of the widget.
Definition: button.hpp:93
bool success_
Action performed by this button succeeded.
Definition: button.hpp:106
virtual void disconnect_click_handler(const event::signal &signal) override
Inherited from clickable_item.
Definition: button.hpp:60
virtual void connect_click_handler(const event::signal &signal) override
Inherited from clickable_item.
Definition: button.hpp:54
virtual bool get_active() const override
See styled_widget::get_active.
Definition: button.cpp:71
void signal_handler_mouse_enter(const event::ui_event event, bool &handled)
Definition: button.cpp:96
state_t
Possible states of the widget.
Definition: button.hpp:78
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: button.cpp:76
void set_state(const state_t state)
Definition: button.cpp:81
void signal_handler_left_button_down(const event::ui_event event, bool &handled)
Definition: button.cpp:118
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:132
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:64
void signal_handler_mouse_leave(const event::ui_event event, bool &handled)
Definition: button.cpp:105
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
void set_success(bool success)
Definition: button.cpp:89
void signal_handler_left_button_click(const event::ui_event event, bool &handled)
Definition: button.cpp:141
Small concept class.
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:170
button_definition(const config &cfg)
Definition: button.cpp:162
builder_button(const config &cfg)
Definition: button.cpp:190
virtual std::unique_ptr< widget > build() const override
Definition: button.cpp:197
virtual std::unique_ptr< widget > build() const=0