The Battle for Wesnoth  1.15.7+dev
menu_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 
18 
19 #include "gui/core/log.hpp"
24 #include "gui/widgets/settings.hpp"
25 #include "gui/widgets/window.hpp"
26 #include "sound.hpp"
27 
28 #include "utils/functional.hpp"
29 
30 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
31 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
32 
33 namespace gui2
34 {
35 
36 // ------------ WIDGET -----------{
37 
38 REGISTER_WIDGET(menu_button)
39 
40 menu_button::menu_button(const implementation::builder_menu_button& builder)
41  : styled_widget(builder, type())
42  , selectable_item()
43  , state_(ENABLED)
44  , values_()
45  , selected_(0)
46  , keep_open_(false)
47 {
48  values_.emplace_back("label", this->get_label());
49 
50  connect_signal<event::MOUSE_ENTER>(
51  std::bind(&menu_button::signal_handler_mouse_enter, this, _2, _3));
52 
53  connect_signal<event::MOUSE_LEAVE>(
54  std::bind(&menu_button::signal_handler_mouse_leave, this, _2, _3));
55 
56  connect_signal<event::LEFT_BUTTON_DOWN>(
57  std::bind(&menu_button::signal_handler_left_button_down, this, _2, _3));
58 
59  connect_signal<event::LEFT_BUTTON_UP>(
60  std::bind(&menu_button::signal_handler_left_button_up, this, _2, _3));
61 
62  connect_signal<event::LEFT_BUTTON_CLICK>(
63  std::bind(&menu_button::signal_handler_left_button_click, this, _2, _3));
64 
65  connect_signal<event::SDL_WHEEL_UP>(
66  std::bind(&menu_button::signal_handler_sdl_wheel_up, this, _2, _3),
68 
69  connect_signal<event::SDL_WHEEL_DOWN>(
70  std::bind(&menu_button::signal_handler_sdl_wheel_down, this, _2, _3),
72 }
73 
74 void menu_button::set_active(const bool active)
75 {
76  if(get_active() != active) {
77  set_state(active ? ENABLED : DISABLED);
78  }
79 }
80 
82 {
83  return state_ != DISABLED;
84 }
85 
86 unsigned menu_button::get_state() const
87 {
88  return state_;
89 }
90 
92 {
93  if(state != state_) {
94  state_ = state;
95  set_is_dirty(true);
96  }
97 }
98 
100 {
101  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
102 
104  handled = true;
105 }
106 
108 {
109  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
110 
112  handled = true;
113 }
114 
116 {
117  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
118 
119  window* window = get_window();
120  if(window) {
121  window->mouse_capture();
122  }
123 
125  handled = true;
126 }
127 
129 {
130  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
131 
133  handled = true;
134 }
135 
137 {
138  assert(get_window());
139  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
140 
142 
143  // If a button has a retval do the default handling.
145 
146  if(droplist.show()) {
147  const int selected = droplist.selected_item();
148 
149  // Safety check. If the user clicks a selection in the dropdown and moves their mouse away too
150  // quickly, selected_ could be set to -1. This returns in that case, preventing crashes.
151  if(selected < 0) {
152  return;
153  }
154 
155  set_selected(selected, true);
156  }
157 
158  handled = true;
159 }
160 
162 {
163  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
164 
165  // TODO: should values wrap?
166  if(selected_ > 0) {
167  set_selected(selected_ - 1);
168  }
169 
170  handled = true;
171 }
172 
174 {
175  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
176 
177  // TODO: should values wrap?
178  if(selected_ < values_.size() - 1) {
179  set_selected(selected_ + 1);
180  }
181 
182  handled = true;
183 }
184 
185 void menu_button::set_values(const std::vector<::config>& values, unsigned selected)
186 {
187  assert(selected < values.size());
188  assert(selected_ < values_.size());
189 
190  if(values[selected]["label"] != values_[selected_]["label"]) {
191  set_is_dirty(true);
192  }
193 
194  values_ = values;
195  selected_ = selected;
196 
197  set_label(values_[selected_]["label"]);
198 }
199 
201 {
202  assert(selected < values_.size());
203  assert(selected_ < values_.size());
204 
205  if(selected != selected_) {
206  set_is_dirty(true);
207  }
208 
210 
211  set_label(values_[selected_]["label"]);
212  if (fire_event) {
213  fire(event::NOTIFY_MODIFIED, *this, nullptr);
214  }
215 }
216 
217 // }---------- DEFINITION ---------{
218 
221 {
222  DBG_GUI_P << "Parsing menu_button " << id << '\n';
223 
224  load_resolutions<resolution>(cfg);
225 }
226 
227 /*WIKI
228  * @page = GUIWidgetDefinitionWML
229  * @order = 1_menu_button
230  *
231  * == menu_button ==
232  *
233  * @macro = menu_button_description
234  *
235  * The following states exist:
236  * * state_enabled, the menu_button is enabled.
237  * * state_disabled, the menu_button is disabled.
238  * * state_pressed, the left mouse menu_button is down.
239  * * state_focused, the mouse is over the menu_button.
240  * @begin{parent}{name="gui/"}
241  * @begin{tag}{name="menu_button_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
242  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
243  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
244  * @end{tag}{name="state_enabled"}
245  * @begin{tag}{name="state_disabled"}{min=0}{max=1}{super="generic/state"}
246  * @end{tag}{name="state_disabled"}
247  * @begin{tag}{name="state_pressed"}{min=0}{max=1}{super="generic/state"}
248  * @end{tag}{name="state_pressed"}
249  * @begin{tag}{name="state_focused"}{min=0}{max=1}{super="generic/state"}
250  * @end{tag}{name="state_focused"}
251  * @end{tag}{name="resolution"}
252  * @end{tag}{name="menu_button_definition"}
253  * @end{parent}{name="gui/"}
254  */
256  : resolution_definition(cfg)
257 {
258  // Note the order should be the same as the enum state_t in menu_button.hpp.
259  state.emplace_back(cfg.child("state_enabled"));
260  state.emplace_back(cfg.child("state_disabled"));
261  state.emplace_back(cfg.child("state_pressed"));
262  state.emplace_back(cfg.child("state_focused"));
263 }
264 
265 // }---------- BUILDER -----------{
266 
267 /*WIKI_MACRO
268  * @begin{macro}{menu_button_description}
269  *
270  * A menu_button is a styled_widget to choose an element from a list of elements.
271  * @end{macro}
272  */
273 
274 /*WIKI
275  * @page = GUIWidgetInstanceWML
276  * @order = 2_menu_button
277  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
278  * @begin{tag}{name="menu_button"}{min=0}{max=-1}{super="generic/widget_instance"}
279  * == menu_button ==
280  *
281  * @macro = menu_button_description
282  *
283  * Instance of a menu_button. When a menu_button has a return value it sets the
284  * return value for the window. Normally this closes the window and returns
285  * this value to the caller. The return value can either be defined by the
286  * user or determined from the id of the menu_button. The return value has a
287  * higher precedence as the one defined by the id. (Of course it's weird to
288  * give a menu_button an id and then override its return value.)
289  *
290  * When the menu_button doesn't have a standard id, but you still want to use the
291  * return value of that id, use return_value_id instead. This has a higher
292  * precedence as return_value.
293  *
294  * List with the menu_button specific variables:
295  * @begin{table}{config}
296  * return_value_id & string & "" & The return value id. $
297  * return_value & int & 0 & The return value. $
298  *
299  * @end{table}
300  * @end{tag}{name="menu_button"}
301  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
302  */
303 
304 namespace implementation
305 {
306 
307 builder_menu_button::builder_menu_button(const config& cfg)
308  : builder_styled_widget(cfg)
309  , options_()
310 {
311  for(const auto& option : cfg.child_range("option")) {
312  options_.push_back(option);
313  }
314 }
315 
317 {
318  menu_button* widget = new menu_button(*this);
319 
320  if(!options_.empty()) {
321  widget->set_values(options_);
322  }
323 
324  DBG_GUI_G << "Window builder: placed menu_button '" << id
325  << "' with definition '" << definition << "'.\n";
326 
327  return widget;
328 }
329 
330 } // namespace implementation
331 
332 // }------------ END --------------
333 
334 } // 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
Small abstract helper class.
void set_selected(unsigned selected, bool fire_event=true)
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:420
void set_state(const state_t state)
Definition: menu_button.cpp:91
std::vector< state_definition > state
Simple push button.
Definition: menu_button.hpp:41
menu_button_definition(const config &cfg)
This file contains the window object, this object is a top level container which has the event manage...
child_itors child_range(config_key_type key)
Definition: config.cpp:362
Base class for all widgets.
Definition: widget.hpp:49
std::string sound_button_click
Definition: settings.cpp:42
void signal_handler_left_button_click(const event::ui_event event, bool &handled)
bool show(const unsigned auto_close_time=0)
Shows the window.
void signal_handler_left_button_up(const event::ui_event event, bool &handled)
void signal_handler_left_button_down(const event::ui_event event, bool &handled)
void signal_handler_sdl_wheel_up(const event::ui_event event, bool &handled)
Generic file dialog.
Definition: field-fwd.hpp:22
Sent by a widget to notify others its contents or state are modified.
Definition: handler.hpp:88
virtual void set_label(const t_string &label)
std::string definition
Parameters for the styled_widget.
void signal_handler_mouse_leave(const event::ui_event event, bool &handled)
This file contains the settings handling of the widget library.
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:465
Used by the menu_button widget.
std::string selected
state_t
Possible states of the widget.
Definition: menu_button.hpp:95
void set_values(const std::vector<::config > &values, unsigned selected=0)
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
state_t state_
Current state of the widget.
bool fire_event(const ui_event event, std::vector< std::pair< widget *, ui_event >> &event_chain, widget *dispatcher, widget *w, F &&... params)
Helper function for fire_event.
#define DBG_GUI_E
Definition: log.hpp:34
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: menu_button.cpp:86
window * get_window()
Get the parent window.
Definition: widget.cpp:116
std::vector<::config > values_
Base class for all visible items.
void signal_handler_mouse_enter(const event::ui_event event, bool &handled)
Definition: menu_button.cpp:99
void mouse_capture(const bool capture=true)
Definition: window.cpp:1276
void play_UI_sound(const std::string &files)
Definition: sound.cpp:1051
bool fire(const ui_event event, widget &target)
Fires an event which has no extra parameters.
Definition: dispatcher.cpp:68
void signal_handler_sdl_wheel_down(const event::ui_event event, bool &handled)
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: menu_button.cpp:74
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
#define DBG_GUI_G
Definition: log.hpp:40
virtual bool get_active() const override
See styled_widget::get_active.
Definition: menu_button.cpp:81
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