The Battle for Wesnoth  1.17.0-dev
button.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
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_function& signal) override
54  {
55  connect_signal_mouse_left_click(*this, signal);
56  }
57 
58  /** Inherited from clickable_item. */
59  virtual void disconnect_click_handler(const event::signal_function& 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 
115  void signal_handler_left_button_down(const event::ui_event event,
116  bool& handled);
117 
118  void signal_handler_left_button_up(const event::ui_event event,
119  bool& handled);
120 
121  void signal_handler_left_button_click(const event::ui_event event,
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 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
Base class of a resolution, contains the common keys for a resolution.
virtual widget * build() const =0
void set_retval(const int retval)
Definition: button.hpp:66
Base class for all widgets.
Definition: widget.hpp:49
dispatcher_callback_func<> signal_function
Callback function signature.
Definition: dispatcher.hpp:198
state_t
Possible states of the widget.
Definition: button.hpp:77
Generic file dialog.
Definition: field-fwd.hpp:23
virtual void connect_click_handler(const event::signal_function &signal) override
Inherited from clickable_item.
Definition: button.hpp:53
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:172
Base class for all visible items.
int retval_
The return value of the button.
Definition: button.hpp:99
state_t state_
Current state of the widget.
Definition: button.hpp:91
void disconnect_signal_mouse_left_click(dispatcher &dispatcher, const signal_function &signal)
Disconnects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177
Simple push button.
Definition: button.hpp:36
Small concept class.
point resolution()
Definition: general.cpp:393
retval
Default window/dialog return values.
Definition: retval.hpp:29
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
virtual void disconnect_click_handler(const event::signal_function &signal) override
Inherited from clickable_item.
Definition: button.hpp:59
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:48