The Battle for Wesnoth  1.15.12+dev
button.cpp
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 #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 <functional>
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 button::button(const implementation::builder_button& builder)
44  : styled_widget(builder, type())
45  , clickable_item()
46  , state_(ENABLED)
47  , retval_(retval::NONE)
48 {
49  connect_signal<event::MOUSE_ENTER>(
50  std::bind(&button::signal_handler_mouse_enter, this, std::placeholders::_2, std::placeholders::_3));
51  connect_signal<event::MOUSE_LEAVE>(
52  std::bind(&button::signal_handler_mouse_leave, this, std::placeholders::_2, std::placeholders::_3));
53 
54  connect_signal<event::LEFT_BUTTON_DOWN>(std::bind(
55  &button::signal_handler_left_button_down, this, std::placeholders::_2, std::placeholders::_3));
56  connect_signal<event::LEFT_BUTTON_UP>(
57  std::bind(&button::signal_handler_left_button_up, this, std::placeholders::_2, std::placeholders::_3));
58  connect_signal<event::LEFT_BUTTON_CLICK>(std::bind(
59  &button::signal_handler_left_button_click, this, std::placeholders::_2, std::placeholders::_3));
60 }
61 
62 void button::set_active(const bool active)
63 {
64  if(get_active() != active) {
65  set_state(active ? ENABLED : DISABLED);
66  }
67 }
68 
69 bool button::get_active() const
70 {
71  return state_ != DISABLED;
72 }
73 
74 unsigned button::get_state() const
75 {
76  return state_;
77 }
78 
79 void button::set_state(const state_t state)
80 {
81  if(state != state_) {
82  state_ = state;
83  set_is_dirty(true);
84  }
85 }
86 
88  bool& handled)
89 {
90  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
91 
93  handled = true;
94 }
95 
97  bool& handled)
98 {
99  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
100 
102  handled = true;
103 }
104 
106  bool& handled)
107 {
108  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
109 
110  window* window = get_window();
111  if(window) {
112  window->mouse_capture();
113  }
114 
116  handled = true;
117 }
118 
120  bool& handled)
121 {
122  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
123 
125  handled = true;
126 }
127 
129  bool& handled)
130 {
131  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
132 
134 
135  // If a button has a retval do the default handling.
136  if(retval_ != retval::NONE) {
137  window* window = get_window();
138  if(window) {
139  window->set_retval(retval_);
140  return;
141  }
142  }
143 
144  handled = true;
145 }
146 
147 // }---------- DEFINITION ---------{
148 
151 {
152  DBG_GUI_P << "Parsing button " << id << '\n';
153 
154  load_resolutions<resolution>(cfg);
155 }
156 
158  : resolution_definition(cfg)
159 {
160  // Note the order should be the same as the enum state_t in button.hpp.
161  state.emplace_back(cfg.child("state_enabled"));
162  state.emplace_back(cfg.child("state_disabled"));
163  state.emplace_back(cfg.child("state_pressed"));
164  state.emplace_back(cfg.child("state_focused"));
165 }
166 
167 // }---------- BUILDER -----------{
168 
169 namespace implementation
170 {
171 
172 builder_button::builder_button(const config& cfg)
173  : builder_styled_widget(cfg)
174  , retval_id_(cfg["return_value_id"])
175  , retval_(cfg["return_value"])
176 {
177 }
178 
180 {
181  button* widget = new button(*this);
182 
183  widget->set_retval(get_retval(retval_id_, retval_, id));
184 
185  DBG_GUI_G << "Window builder: placed button '" << id
186  << "' with definition '" << definition << "'.\n";
187 
188  return widget;
189 }
190 
191 } // namespace implementation
192 
193 // }------------ END --------------
194 
195 } // namespace gui2
Define the common log macros for the gui toolkit.
Base class of a resolution, contains the common keys for a resolution.
#define DBG_GUI_P
Definition: log.hpp:65
config & child(config_key_type 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:414
std::vector< state_definition > state
void signal_handler_mouse_enter(const event::ui_event event, bool &handled)
Definition: button.cpp:87
void set_retval(const int retval)
Definition: button.hpp:65
void signal_handler_left_button_down(const event::ui_event event, bool &handled)
Definition: button.cpp:105
This file contains the window object, this object is a top level container which has the event manage...
Base class for all widgets.
Definition: widget.hpp:49
std::string sound_button_click
Definition: settings.cpp:42
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:136
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: button.cpp:74
void signal_handler_left_button_click(const event::ui_event event, bool &handled)
Definition: button.cpp:128
void set_state(const state_t state)
Definition: button.cpp:79
state_t
Possible states of the widget.
Definition: button.hpp:76
Generic file dialog.
Definition: field-fwd.hpp:22
std::string definition
Parameters for the styled_widget.
This file contains the settings handling of the widget library.
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:465
virtual widget * build() const override
Definition: button.cpp:179
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
void signal_handler_left_button_up(const event::ui_event event, bool &handled)
Definition: button.cpp:119
#define DBG_GUI_E
Definition: log.hpp:34
Default, unset return value.
Definition: retval.hpp:31
window * get_window()
Get the parent window.
Definition: widget.cpp:116
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition: window.hpp:357
Base class for all visible items.
virtual bool get_active() const override
See styled_widget::get_active.
Definition: button.cpp:69
int retval_
The return value of the button.
Definition: button.hpp:98
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:62
state_t state_
Current state of the widget.
Definition: button.hpp:90
void mouse_capture(const bool capture=true)
Definition: window.cpp:1267
void play_UI_sound(const std::string &files)
Definition: sound.cpp:1051
Simple push button.
Definition: button.hpp:35
Small concept class.
void signal_handler_mouse_leave(const event::ui_event event, bool &handled)
Definition: button.cpp:96
retval
Default window/dialog return values.
Definition: retval.hpp:28
#define LOG_HEADER
Definition: button.cpp:34
resolution(const config &cfg)
Definition: button.cpp:157
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
#define DBG_GUI_G
Definition: log.hpp:40
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:47
button_definition(const config &cfg)
Definition: button.cpp:149