The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
button.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include "gui/widgets/button.hpp"
18 
19 #include "gui/core/log.hpp"
20 
24 
26 #include "gui/widgets/settings.hpp"
27 #include "gui/widgets/window.hpp"
28 
29 #include "sound.hpp"
30 
31 #include "utils/functional.hpp"
32 
33 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
34 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
35 
36 namespace gui2
37 {
38 
39 // ------------ WIDGET -----------{
40 
41 REGISTER_WIDGET(button)
42 
43 tbutton::tbutton() : tcontrol(COUNT), tclickable_(), state_(ENABLED), retval_(0)
44 {
45  connect_signal<event::MOUSE_ENTER>(
46  std::bind(&tbutton::signal_handler_mouse_enter, this, _2, _3));
47  connect_signal<event::MOUSE_LEAVE>(
48  std::bind(&tbutton::signal_handler_mouse_leave, this, _2, _3));
49 
50  connect_signal<event::LEFT_BUTTON_DOWN>(std::bind(
52  connect_signal<event::LEFT_BUTTON_UP>(
53  std::bind(&tbutton::signal_handler_left_button_up, this, _2, _3));
54  connect_signal<event::LEFT_BUTTON_CLICK>(std::bind(
56 }
57 
58 void tbutton::set_active(const bool active)
59 {
60  if(get_active() != active) {
61  set_state(active ? ENABLED : DISABLED);
62  }
63 }
64 
65 bool tbutton::get_active() const
66 {
67  return state_ != DISABLED;
68 }
69 
70 unsigned tbutton::get_state() const
71 {
72  return state_;
73 }
74 
75 void tbutton::set_state(const tstate state)
76 {
77  if(state != state_) {
78  state_ = state;
79  set_is_dirty(true);
80  }
81 }
82 
84 {
85  static const std::string type = "button";
86  return type;
87 }
88 
90  bool& handled)
91 {
92  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
93 
95  handled = true;
96 }
97 
99  bool& handled)
100 {
101  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
102 
104  handled = true;
105 }
106 
108  bool& handled)
109 {
110  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
111 
112  twindow* window = get_window();
113  if(window) {
114  window->mouse_capture();
115  }
116 
118  handled = true;
119 }
120 
122  bool& handled)
123 {
124  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
125 
127  handled = true;
128 }
129 
131  bool& handled)
132 {
133  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
134 
136 
137  // If a button has a retval do the default handling.
138  if(retval_ != 0) {
139  twindow* window = get_window();
140  if(window) {
141  window->set_retval(retval_);
142  return;
143  }
144  }
145 
146  handled = true;
147 }
148 
149 // }---------- DEFINITION ---------{
150 
152  : tcontrol_definition(cfg)
153 {
154  DBG_GUI_P << "Parsing button " << id << '\n';
155 
156  load_resolutions<tresolution>(cfg);
157 }
158 
159 /*WIKI
160  * @page = GUIWidgetDefinitionWML
161  * @order = 1_button
162  *
163  * == Button ==
164  *
165  * @macro = button_description
166  *
167  * The following states exist:
168  * * state_enabled, the button is enabled.
169  * * state_disabled, the button is disabled.
170  * * state_pressed, the left mouse button is down.
171  * * state_focused, the mouse is over the button.
172  * @begin{parent}{name="gui/"}
173  * @begin{tag}{name="button_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
174  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
175  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
176  * @end{tag}{name="state_enabled"}
177  * @begin{tag}{name="state_disabled"}{min=0}{max=1}{super="generic/state"}
178  * @end{tag}{name="state_disabled"}
179  * @begin{tag}{name="state_pressed"}{min=0}{max=1}{super="generic/state"}
180  * @end{tag}{name="state_pressed"}
181  * @begin{tag}{name="state_focused"}{min=0}{max=1}{super="generic/state"}
182  * @end{tag}{name="state_focused"}
183  * @end{tag}{name="resolution"}
184  * @end{tag}{name="button_definition"}
185  * @end{parent}{name="gui/"}
186  */
189 {
190  // Note the order should be the same as the enum tstate in button.hpp.
191  state.push_back(tstate_definition(cfg.child("state_enabled")));
192  state.push_back(tstate_definition(cfg.child("state_disabled")));
193  state.push_back(tstate_definition(cfg.child("state_pressed")));
194  state.push_back(tstate_definition(cfg.child("state_focused")));
195 }
196 
197 // }---------- BUILDER -----------{
198 
199 /*WIKI_MACRO
200  * @begin{macro}{button_description}
201  *
202  * A button is a control that can be pushed to start an action or close
203  * a dialog.
204  * @end{macro}
205  */
206 
207 /*WIKI
208  * @page = GUIWidgetInstanceWML
209  * @order = 2_button
210  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
211  * @begin{tag}{name="button"}{min=0}{max=-1}{super="generic/widget_instance"}
212  * == Button ==
213  *
214  * @macro = button_description
215  *
216  * Instance of a button. When a button has a return value it sets the
217  * return value for the window. Normally this closes the window and returns
218  * this value to the caller. The return value can either be defined by the
219  * user or determined from the id of the button. The return value has a
220  * higher precedence as the one defined by the id. (Of course it's weird to
221  * give a button an id and then override its return value.)
222  *
223  * When the button doesn't have a standard id, but you still want to use the
224  * return value of that id, use return_value_id instead. This has a higher
225  * precedence as return_value.
226  *
227  * List with the button specific variables:
228  * @begin{table}{config}
229  * return_value_id & string & "" & The return value id. $
230  * return_value & int & 0 & The return value. $
231  *
232  * @end{table}
233  * @end{tag}{name="button"}
234  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
235  */
236 
237 namespace implementation
238 {
239 
240 tbuilder_button::tbuilder_button(const config& cfg)
241  : tbuilder_control(cfg)
242  , retval_id_(cfg["return_value_id"])
243  , retval_(cfg["return_value"])
244 {
245 }
246 
248 {
249  tbutton* widget = new tbutton();
250 
251  init_control(widget);
252 
253  widget->set_retval(get_retval(retval_id_, retval_, id));
254 
255  DBG_GUI_G << "Window builder: placed button '" << id
256  << "' with definition '" << definition << "'.\n";
257 
258  return widget;
259 }
260 
261 } // namespace implementation
262 
263 // }------------ END --------------
264 
265 } // namespace gui2
Define the common log macros for the gui toolkit.
#define DBG_GUI_P
Definition: log.hpp:69
void signal_handler_mouse_enter(const event::tevent event, bool &handled)
Definition: button.cpp:89
virtual void set_active(const bool active) override
See tcontrol::set_active.
Definition: button.cpp:58
void signal_handler_left_button_down(const event::tevent event, bool &handled)
Definition: button.cpp:107
tstate
Possible states of the widget.
Definition: button.hpp:73
void signal_handler_left_button_up(const event::tevent event, bool &handled)
Definition: button.cpp:121
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
void set_retval(const int retval)
Definition: button.hpp:62
This file contains the window object, this object is a top level container which has the event manage...
std::string sound_button_click
Definition: settings.cpp:58
tstate state_
Current state of the widget.
Definition: button.hpp:88
int get_retval(const std::string &retval_id, const int retval, const std::string &id)
Returns the return value for a widget.
Definition: helper.cpp:131
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:435
Base class of a resolution, contains the common keys for a resolution.
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
void init_control(tcontrol *control) const
Definition: control.cpp:675
std::string definition
Parameters for the control.
Definition: control.hpp:530
Simple push button.
Definition: button.hpp:32
void signal_handler_mouse_leave(const event::tevent event, bool &handled)
Definition: button.cpp:98
This file contains the settings handling of the widget library.
void signal_handler_left_button_click(const event::tevent event, bool &handled)
Definition: button.cpp:130
virtual const std::string & get_control_type() const override
See tcontrol::get_control_type.
Definition: button.cpp:83
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition: window.hpp:422
tevent
The event send to the dispatcher.
Definition: handler.hpp:54
Contains the state info for a resolution.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
Small concept class.
Definition: clickable.hpp:39
#define DBG_GUI_E
Definition: log.hpp:35
int retval_
The return value of the button.
Definition: button.hpp:96
Base class for all visible items.
Definition: control.hpp:34
std::vector< tstate_definition > state
virtual unsigned get_state() const override
See tcontrol::get_state.
Definition: button.cpp:70
cl_event event
Definition: glew.h:3070
config & child(const std::string &key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:658
Base class for all widgets.
Definition: widget.hpp:49
void play_UI_sound(const std::string &files)
Definition: sound.cpp:842
void mouse_capture(const bool capture=true)
Definition: window.cpp:1388
#define LOG_HEADER
Definition: button.cpp:34
twindow * get_window()
Get the parent window.
Definition: widget.cpp:116
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
#define DBG_GUI_G
Definition: log.hpp:41
GLsizei const GLcharARB ** string
Definition: glew.h:4503
virtual bool get_active() const override
See tcontrol::get_active.
Definition: button.cpp:65
Contains the implementation details for lexical_cast and shouldn't be used directly.
void set_state(const tstate state)
Definition: button.cpp:75
tbutton_definition(const config &cfg)
Definition: button.cpp:151