The Battle for Wesnoth  1.17.17+dev
button.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2023
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 #include "wml_exception.hpp"
30 #include "gettext.hpp"
31 
32 #include "sound.hpp"
33 
34 #include <functional>
35 
36 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
37 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
38 
39 namespace gui2
40 {
41 
42 // ------------ WIDGET -----------{
43 
44 REGISTER_WIDGET(button)
45 
46 button::button(const implementation::builder_button& builder)
47  : styled_widget(builder, type())
48  , clickable_item()
49  , state_(ENABLED)
50  , retval_(retval::NONE)
51 {
52  connect_signal<event::MOUSE_ENTER>(
53  std::bind(&button::signal_handler_mouse_enter, this, std::placeholders::_2, std::placeholders::_3));
54  connect_signal<event::MOUSE_LEAVE>(
55  std::bind(&button::signal_handler_mouse_leave, this, std::placeholders::_2, std::placeholders::_3));
56 
57  connect_signal<event::LEFT_BUTTON_DOWN>(std::bind(
58  &button::signal_handler_left_button_down, this, std::placeholders::_2, std::placeholders::_3));
59  connect_signal<event::LEFT_BUTTON_UP>(
60  std::bind(&button::signal_handler_left_button_up, this, std::placeholders::_2, std::placeholders::_3));
61  connect_signal<event::LEFT_BUTTON_CLICK>(std::bind(
62  &button::signal_handler_left_button_click, this, std::placeholders::_2, std::placeholders::_3));
63 }
64 
65 void button::set_active(const bool active)
66 {
67  if(get_active() != active) {
68  set_state(active ? ENABLED : DISABLED);
69  }
70 }
71 
72 bool button::get_active() const
73 {
74  return state_ != DISABLED;
75 }
76 
77 unsigned button::get_state() const
78 {
79  return state_;
80 }
81 
82 void button::set_state(const state_t state)
83 {
84  if(state != state_) {
85  state_ = state;
86  queue_redraw();
87  }
88 }
89 
91  bool& handled)
92 {
93  DBG_GUI_E << LOG_HEADER << ' ' << event << ".";
94 
96  handled = true;
97 }
98 
100  bool& handled)
101 {
102  DBG_GUI_E << LOG_HEADER << ' ' << event << ".";
103 
105  handled = true;
106 }
107 
109  bool& handled)
110 {
111  DBG_GUI_E << LOG_HEADER << ' ' << event << ".";
112 
113  window* window = get_window();
114  if(window) {
116  }
117 
119  handled = true;
120 }
121 
123  bool& handled)
124 {
125  DBG_GUI_E << LOG_HEADER << ' ' << event << ".";
126 
128  handled = true;
129 }
130 
132  bool& handled)
133 {
134  DBG_GUI_E << LOG_HEADER << ' ' << event << ".";
135 
137 
138  // If a button has a retval do the default handling.
139  if(retval_ != retval::NONE) {
140  window* window = get_window();
141  if(window) {
143  return;
144  }
145  }
146 
147  handled = true;
148 }
149 
150 // }---------- DEFINITION ---------{
151 
154 {
155  DBG_GUI_P << "Parsing button " << id;
156 
157  load_resolutions<resolution>(cfg);
158 }
159 
161  : resolution_definition(cfg)
162 {
163  // Note the order should be the same as the enum state_t in button.hpp.
164  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_enabled", _("Missing required state for button")));
165  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_disabled", _("Missing required state for button")));
166  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_pressed", _("Missing required state for button")));
167  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_focused", _("Missing required state for button")));
168 }
169 
170 // }---------- BUILDER -----------{
171 
172 namespace implementation
173 {
174 
175 builder_button::builder_button(const config& cfg)
176  : builder_styled_widget(cfg)
177  , retval_id_(cfg["return_value_id"])
178  , retval_(cfg["return_value"])
179 {
180 }
181 
182 std::unique_ptr<widget> builder_button::build() const
183 {
184  auto widget = std::make_unique<button>(*this);
185 
186  widget->set_retval(get_retval(retval_id_, retval_, id));
187 
188  DBG_GUI_G << "Window builder: placed button '" << id
189  << "' with definition '" << definition << "'.";
190 
191  return widget;
192 }
193 
194 } // namespace implementation
195 
196 // }------------ END --------------
197 
198 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
Simple push button.
Definition: button.hpp:37
int retval_
The return value of the button.
Definition: button.hpp:99
state_t state_
Current state of the widget.
Definition: button.hpp:91
virtual bool get_active() const override
See styled_widget::get_active.
Definition: button.cpp:72
void signal_handler_mouse_enter(const event::ui_event event, bool &handled)
Definition: button.cpp:90
state_t
Possible states of the widget.
Definition: button.hpp:77
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: button.cpp:77
void set_state(const state_t state)
Definition: button.cpp:82
void signal_handler_left_button_down(const event::ui_event event, bool &handled)
Definition: button.cpp:108
void signal_handler_left_button_up(const event::ui_event event, bool &handled)
Definition: button.cpp:122
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:65
void signal_handler_mouse_leave(const event::ui_event event, bool &handled)
Definition: button.cpp:99
void signal_handler_left_button_click(const event::ui_event event, bool &handled)
Definition: button.cpp:131
Small concept class.
Base class for all visible items.
Base class for all widgets.
Definition: widget.hpp:54
void queue_redraw()
Indicates that this widget should be redrawn.
Definition: widget.cpp:442
window * get_window()
Get the parent window.
Definition: widget.cpp:118
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:67
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition: window.hpp:358
void mouse_capture(const bool capture=true)
Definition: window.cpp:1124
static std::string _(const char *str)
Definition: gettext.hpp:93
Define the common log macros for the gui toolkit.
#define DBG_GUI_G
Definition: log.hpp:41
#define DBG_GUI_P
Definition: log.hpp:66
#define DBG_GUI_E
Definition: log.hpp:35
#define LOG_HEADER
Definition: button.cpp:37
This file contains the window object, this object is a top level container which has the event manage...
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
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
std::string sound_button_click
Definition: settings.cpp:48
Generic file dialog.
retval
Default window/dialog return values.
Definition: retval.hpp:30
@ NONE
Default, unset return value.
Definition: retval.hpp:32
Contains the implementation details for lexical_cast and shouldn't be used directly.
void play_UI_sound(const std::string &files)
Definition: sound.cpp:1066
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
This file contains the settings handling of the widget library.
resolution(const config &cfg)
Definition: button.cpp:160
button_definition(const config &cfg)
Definition: button.cpp:152
virtual std::unique_ptr< widget > build() const override
Definition: button.cpp:182
std::string definition
Parameters for the styled_widget.
Base class of a resolution, contains the common keys for a resolution.
std::vector< state_definition > state
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE_WML_CHILD(cfg, key, message)