The Battle for Wesnoth  1.19.0-dev
button.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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 
24 
26 #include "gui/widgets/settings.hpp"
27 #include "gui/widgets/window.hpp"
28 #include "wml_exception.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  queue_redraw();
85  }
86 }
87 
89  bool& handled)
90 {
91  DBG_GUI_E << LOG_HEADER << ' ' << event << ".";
92 
94  handled = true;
95 }
96 
98  bool& handled)
99 {
100  DBG_GUI_E << LOG_HEADER << ' ' << event << ".";
101 
103  handled = true;
104 }
105 
107  bool& handled)
108 {
109  DBG_GUI_E << LOG_HEADER << ' ' << event << ".";
110 
111  window* window = get_window();
112  if(window) {
114  }
115 
117  handled = true;
118 }
119 
121  bool& handled)
122 {
123  DBG_GUI_E << LOG_HEADER << ' ' << event << ".";
124 
126  handled = true;
127 }
128 
130  bool& handled)
131 {
132  DBG_GUI_E << LOG_HEADER << ' ' << event << ".";
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) {
141  return;
142  }
143  }
144 
145  handled = true;
146 }
147 
148 // }---------- DEFINITION ---------{
149 
152 {
153  DBG_GUI_P << "Parsing button " << id;
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(VALIDATE_WML_CHILD(cfg, "state_enabled", missing_mandatory_wml_tag("button_definition][resolution", "state_enabled")));
163  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_disabled", missing_mandatory_wml_tag("button_definition][resolution", "state_disabled")));
164  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_pressed", missing_mandatory_wml_tag("button_definition][resolution", "state_pressed")));
165  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_focused", missing_mandatory_wml_tag("button_definition][resolution", "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 
180 std::unique_ptr<widget> builder_button::build() const
181 {
182  auto widget = std::make_unique<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 << "'.";
188 
189  return widget;
190 }
191 
192 } // namespace implementation
193 
194 // }------------ END --------------
195 
196 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
Simple push button.
Definition: button.hpp:36
int retval_
The return value of the button.
Definition: button.hpp:98
state_t state_
Current state of the widget.
Definition: button.hpp:90
virtual bool get_active() const override
See styled_widget::get_active.
Definition: button.cpp:70
void signal_handler_mouse_enter(const event::ui_event event, bool &handled)
Definition: button.cpp:88
state_t
Possible states of the widget.
Definition: button.hpp:76
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: button.cpp:75
void set_state(const state_t state)
Definition: button.cpp:80
void signal_handler_left_button_down(const event::ui_event event, bool &handled)
Definition: button.cpp:106
void signal_handler_left_button_up(const event::ui_event event, bool &handled)
Definition: button.cpp:120
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:63
void signal_handler_mouse_leave(const event::ui_event event, bool &handled)
Definition: button.cpp:97
void signal_handler_left_button_click(const event::ui_event event, bool &handled)
Definition: button.cpp:129
Small concept class.
Base class for all visible items.
Base class for all widgets.
Definition: widget.hpp:53
void queue_redraw()
Indicates that this widget should be redrawn.
Definition: widget.cpp:455
window * get_window()
Get the parent window.
Definition: widget.cpp:117
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition: window.hpp:399
void mouse_capture(const bool capture=true)
Definition: window.cpp:1215
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:35
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:1064
#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:158
button_definition(const config &cfg)
Definition: button.cpp:150
virtual std::unique_ptr< widget > build() const override
Definition: button.cpp:180
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
std::string missing_mandatory_wml_tag(const std::string &section, const std::string &tag)
Returns a standard message for a missing wml child (tag).
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)