The Battle for Wesnoth  1.15.1+dev
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 
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_function& signal) override
53  {
54  connect_signal_mouse_left_click(*this, signal);
55  }
56 
57  /** Inherited from clickable_item. */
58  virtual void disconnect_click_handler(const event::signal_function& 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 
114  void signal_handler_left_button_down(const event::ui_event event,
115  bool& handled);
116 
117  void signal_handler_left_button_up(const event::ui_event event,
118  bool& handled);
119 
120  void signal_handler_left_button_click(const event::ui_event event,
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 class styled_widget;
139 
140 namespace implementation
141 {
142 
144 {
145 public:
146  explicit builder_button(const config& cfg);
147 
149 
150  widget* build() const;
151 
152 private:
153  std::string retval_id_;
154  int retval_;
155 };
156 
157 } // namespace implementation
158 
159 // }------------ END --------------
160 
161 } // 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:65
Base class for all widgets.
Definition: widget.hpp:47
state_t
Possible states of the widget.
Definition: button.hpp:76
Generic file dialog.
Definition: field-fwd.hpp:22
virtual void connect_click_handler(const event::signal_function &signal) override
Inherited from clickable_item.
Definition: button.hpp:52
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:233
window * build(const builder_window::window_resolution *definition)
Builds a window.
Base class for all visible items.
std::function< void(widget &dispatcher, const ui_event event, bool &handled, bool &halt)> signal_function
Callback function signature.
Definition: dispatcher.hpp:42
int retval_
The return value of the button.
Definition: button.hpp:98
state_t state_
Current state of the widget.
Definition: button.hpp:90
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:238
Simple push button.
Definition: button.hpp:35
Small concept class.
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
virtual void disconnect_click_handler(const event::signal_function &signal) override
Inherited from clickable_item.
Definition: button.hpp:58
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