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