The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
combobox.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://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/combobox.hpp"
18 
19 #include "gui/core/log.hpp"
24 #include "gui/widgets/settings.hpp"
25 #include "gui/widgets/window.hpp"
27 #include "sound.hpp"
28 
29 #include "utils/functional.hpp"
30 
31 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
32 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
33 
34 namespace gui2
35 {
36 
37 // ------------ WIDGET -----------{
38 
39 REGISTER_WIDGET(combobox)
40 
42  : tcontrol(COUNT)
43  , tselectable_()
44  , state_(ENABLED)
45  , retval_(0)
46  , values_()
47  , selected_()
48 {
49  values_.push_back(this->label());
50 
51  connect_signal<event::MOUSE_ENTER>(
52  std::bind(&tcombobox::signal_handler_mouse_enter, this, _2, _3));
53  connect_signal<event::MOUSE_LEAVE>(
54  std::bind(&tcombobox::signal_handler_mouse_leave, this, _2, _3));
55 
56  connect_signal<event::LEFT_BUTTON_DOWN>(std::bind(
58  connect_signal<event::LEFT_BUTTON_UP>(
59  std::bind(&tcombobox::signal_handler_left_button_up, this, _2, _3));
60  connect_signal<event::LEFT_BUTTON_CLICK>(std::bind(
62 }
63 
64 void tcombobox::set_active(const bool active)
65 {
66  if(get_active() != active) {
67  set_state(active ? ENABLED : DISABLED);
68  }
69 }
70 
72 {
73  return state_ != DISABLED;
74 }
75 
76 unsigned tcombobox::get_state() const
77 {
78  return state_;
79 }
80 
81 void tcombobox::set_state(const tstate state)
82 {
83  if(state != state_) {
84  state_ = state;
85  set_is_dirty(true);
86  }
87 }
88 
90 {
91  static const std::string type = "combobox";
92  return type;
93 }
94 
96  bool& handled)
97 {
98  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
99 
101  handled = true;
102 }
103 
105  bool& handled)
106 {
107  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
108 
110  handled = true;
111 }
112 
114  bool& handled)
115 {
116  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
117 
118  twindow* window = get_window();
119  if(window) {
120  window->mouse_capture();
121  }
122 
124  handled = true;
125 }
126 
128  bool& handled)
129 {
130  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
131 
133  handled = true;
134 }
135 
137  bool& handled)
138 {
139  assert(get_window());
140  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
141 
143 
144  // If a button has a retval do the default handling.
145  tdrop_down_list droplist(this->get_rectangle(), this->values_, this->selected_, this->get_use_markup());
146 
147  if(droplist.show(get_window()->video())) {
148  selected_ = droplist.selected_item();
149  this->set_label(values_[selected_]);
150  if(selected_callback_) {
151  selected_callback_(*this);
152  }
153  if(retval_ != 0) {
154  twindow* window = get_window();
155  if(window) {
156  window->set_retval(retval_);
157  return;
158  }
159  }
160  }
161 
162  handled = true;
163 }
164 
165 void tcombobox::set_values(const std::vector<std::string>& values, int selected)
166 {
167  assert(static_cast<size_t>(selected) < values.size());
168  assert(static_cast<size_t>(selected_) < values_.size());
169  if(values[selected] != values_[selected_]) {
170  set_is_dirty(true);
171  }
172  values_ = values;
173  selected_ = selected;
174  set_label(values_[selected_]);
175 
176 }
178 {
179  assert(static_cast<size_t>(selected) < values_.size());
180  assert(static_cast<size_t>(selected_) < values_.size());
181  if(selected != selected_) {
182  set_is_dirty(true);
183  }
186 }
187 
188 // }---------- DEFINITION ---------{
189 
191  : tcontrol_definition(cfg)
192 {
193  DBG_GUI_P << "Parsing combobox " << id << '\n';
194 
195  load_resolutions<tresolution>(cfg);
196 }
197 
198 /*WIKI
199  * @page = GUIWidgetDefinitionWML
200  * @order = 1_combobox
201  *
202  * == combobox ==
203  *
204  * @macro = combobox_description
205  *
206  * The following states exist:
207  * * state_enabled, the combobox is enabled.
208  * * state_disabled, the combobox is disabled.
209  * * state_pressed, the left mouse combobox is down.
210  * * state_focused, the mouse is over the combobox.
211  * @begin{parent}{name="gui/"}
212  * @begin{tag}{name="combobox_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
213  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
214  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
215  * @end{tag}{name="state_enabled"}
216  * @begin{tag}{name="state_disabled"}{min=0}{max=1}{super="generic/state"}
217  * @end{tag}{name="state_disabled"}
218  * @begin{tag}{name="state_pressed"}{min=0}{max=1}{super="generic/state"}
219  * @end{tag}{name="state_pressed"}
220  * @begin{tag}{name="state_focused"}{min=0}{max=1}{super="generic/state"}
221  * @end{tag}{name="state_focused"}
222  * @end{tag}{name="resolution"}
223  * @end{tag}{name="combobox_definition"}
224  * @end{parent}{name="gui/"}
225  */
228 {
229  // Note the order should be the same as the enum tstate in combobox.hpp.
230  state.push_back(tstate_definition(cfg.child("state_enabled")));
231  state.push_back(tstate_definition(cfg.child("state_disabled")));
232  state.push_back(tstate_definition(cfg.child("state_pressed")));
233  state.push_back(tstate_definition(cfg.child("state_focused")));
234 }
235 
236 // }---------- BUILDER -----------{
237 
238 /*WIKI_MACRO
239  * @begin{macro}{combobox_description}
240  *
241  * A combobox is a control to choose an element from a list of elements.
242  * @end{macro}
243  */
244 
245 /*WIKI
246  * @page = GUIWidgetInstanceWML
247  * @order = 2_combobox
248  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
249  * @begin{tag}{name="combobox"}{min=0}{max=-1}{super="generic/widget_instance"}
250  * == combobox ==
251  *
252  * @macro = combobox_description
253  *
254  * Instance of a combobox. When a combobox has a return value it sets the
255  * return value for the window. Normally this closes the window and returns
256  * this value to the caller. The return value can either be defined by the
257  * user or determined from the id of the combobox. The return value has a
258  * higher precedence as the one defined by the id. (Of course it's weird to
259  * give a combobox an id and then override its return value.)
260  *
261  * When the combobox doesn't have a standard id, but you still want to use the
262  * return value of that id, use return_value_id instead. This has a higher
263  * precedence as return_value.
264  *
265  * List with the combobox specific variables:
266  * @begin{table}{config}
267  * return_value_id & string & "" & The return value id. $
268  * return_value & int & 0 & The return value. $
269  *
270  * @end{table}
271  * @end{tag}{name="combobox"}
272  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
273  */
274 
275 namespace implementation
276 {
277 
278 tbuilder_combobox::tbuilder_combobox(const config& cfg)
279  : tbuilder_control(cfg)
280  , retval_id_(cfg["return_value_id"])
281  , retval_(cfg["return_value"])
282  , options_()
283 {
284  for(const auto& option : cfg.child_range("option")) {
285  options_.push_back(option["label"]);
286  }
287 }
288 
290 {
291  tcombobox* widget = new tcombobox();
292 
293  init_control(widget);
294 
295  widget->set_retval(get_retval(retval_id_, retval_, id));
296  if(!options_.empty()) {
297  widget->set_values(options_);
298  }
299  DBG_GUI_G << "Window builder: placed combobox '" << id
300  << "' with definition '" << definition << "'.\n";
301 
302  return widget;
303 }
304 
305 } // namespace implementation
306 
307 // }------------ END --------------
308 
309 } // namespace gui2
Define the common log macros for the gui toolkit.
#define DBG_GUI_P
Definition: log.hpp:69
void signal_handler_mouse_leave(const event::tevent event, bool &handled)
Definition: combobox.cpp:104
child_itors child_range(const std::string &key)
Definition: config.cpp:613
virtual unsigned get_state() const override
See tcontrol::get_state.
Definition: combobox.cpp:76
void signal_handler_left_button_up(const event::tevent event, bool &handled)
Definition: combobox.cpp:127
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
bool show(CVideo &video, const unsigned auto_close_time=0)
Shows the window.
Definition: dialog.cpp:34
This file contains the window object, this object is a top level container which has the event manage...
std::string sound_button_click
Definition: settings.cpp:58
virtual void set_label(const t_string &label)
Definition: control.cpp:330
Used by the combobox widget.
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:131
void set_state(const tstate state)
Definition: combobox.cpp:81
GLuint GLenum option
Definition: glew.h:2815
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:435
Base class of a resolution, contains the common keys for a resolution.
GLboolean GLenum GLenum GLvoid * values
Definition: glew.h:3799
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
void init_control(tcontrol *control) const
Definition: control.cpp:675
std::string definition
Parameters for the control.
Definition: control.hpp:530
This file contains the settings handling of the widget library.
#define LOG_HEADER
Definition: combobox.cpp:32
std::function< void(twidget &)> selected_callback_
Definition: combobox.hpp:126
int retval_
The return value of the button.
Definition: combobox.hpp:118
std::string selected
Definition: game_config.cpp:84
virtual void set_active(const bool active) override
See tcontrol::set_active.
Definition: combobox.cpp:64
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition: window.hpp:422
void signal_handler_mouse_enter(const event::tevent event, bool &handled)
Definition: combobox.cpp:95
tevent
The event send to the dispatcher.
Definition: handler.hpp:54
bool get_use_markup() const
Definition: control.hpp:255
void signal_handler_left_button_down(const event::tevent event, bool &handled)
Definition: combobox.cpp:113
Contains the state info for a resolution.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
Small abstract helper class.
Definition: selectable.hpp:32
virtual const std::string & get_control_type() const override
See tcontrol::get_control_type.
Definition: combobox.cpp:89
void signal_handler_left_button_click(const event::tevent event, bool &handled)
Definition: combobox.cpp:136
std::vector< std::string > options_
Definition: combobox.hpp:178
#define DBG_GUI_E
Definition: log.hpp:35
void set_values(const std::vector< std::string > &values, int selected=0)
Definition: combobox.cpp:165
tstate
Possible states of the widget.
Definition: combobox.hpp:95
void set_retval(const int retval)
Definition: combobox.hpp:61
tcombobox_definition(const config &cfg)
Definition: combobox.cpp:190
std::vector< std::string > values_
Definition: combobox.hpp:121
Base class for all visible items.
Definition: control.hpp:34
Simple push button.
Definition: combobox.hpp:31
tstate state_
Current state of the widget.
Definition: combobox.hpp:110
std::vector< tstate_definition > state
cl_event event
Definition: glew.h:3070
config & child(const std::string &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:658
Base class for all widgets.
Definition: widget.hpp:49
void play_UI_sound(const std::string &files)
Definition: sound.cpp:842
void mouse_capture(const bool capture=true)
Definition: window.cpp:1388
void set_selected(int selected)
Definition: combobox.cpp:177
int selected_item() const
twindow * get_window()
Get the parent window.
Definition: widget.cpp:116
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
SDL_Rect get_rectangle() const
Gets the bounding rectangle of the widget on the screen.
Definition: widget.cpp:279
#define DBG_GUI_G
Definition: log.hpp:41
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Contains the implementation details for lexical_cast and shouldn't be used directly.
virtual bool get_active() const override
See tcontrol::get_active.
Definition: combobox.cpp:71