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 
17 #include "gui/core/widget_definition.hpp"
18 #include "gui/core/window_builder.hpp"
19 
20 #include "gui/widgets/styled_widget.hpp"
21 #include "gui/widgets/clickable_item.hpp"
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. */
connect_click_handler(const event::signal_function & signal)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. */
disconnect_click_handler(const event::signal_function & signal)58 	virtual void disconnect_click_handler(const event::signal_function& signal) override
59 	{
60 		disconnect_signal_mouse_left_click(*this, signal);
61 	}
62 
63 	/***** ***** ***** setters / getters for members ***** ****** *****/
64 
set_retval(const int retval)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 {
77 		ENABLED,
78 		DISABLED,
79 		PRESSED,
80 		FOCUSED,
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 	 */
90 	state_t state_;
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 
126 struct button_definition : public styled_widget_definition
127 {
128 	explicit button_definition(const config& cfg);
129 
130 	struct resolution : public resolution_definition
131 	{
132 		explicit resolution(const config& cfg);
133 	};
134 };
135 
136 // }---------- BUILDER -----------{
137 
138 class styled_widget;
139 
140 namespace implementation
141 {
142 
143 struct builder_button : public builder_styled_widget
144 {
145 public:
146 	explicit builder_button(const config& cfg);
147 
148 	using builder_styled_widget::build;
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
162