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