The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
dialog.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/dialogs/dialog.hpp"
18 
19 #include "gui/auxiliary/field.hpp"
21 #include "video.hpp"
22 
23 namespace gui2
24 {
25 
27 {
28  for(auto field : fields_)
29  {
30  delete field;
31  }
32 }
33 
34 bool tdialog::show(CVideo& video, const unsigned auto_close_time)
35 {
36  if(video.faked()) {
37  return false;
38  }
39 
40  std::unique_ptr<twindow> window(build_window(video));
41  assert(window.get());
42 
43  post_build(*window);
44 
45  window->set_owner(this);
46 
47  init_fields(*window);
48 
49  pre_show(*window);
50 
51  retval_ = window->show(restore_, auto_close_time);
52 
53  /*
54  * It can happen that when two clicks follow each other fast that the event
55  * handling code in events.cpp generates a DOUBLE_CLICK_EVENT. For some
56  * reason it can happen that this event gets pushed in the queue when the
57  * window is shown, but processed after the window is closed. This causes
58  * the next window to get this pending event.
59  *
60  * This caused a bug where double clicking in the campaign selection dialog
61  * directly selected a difficulty level and started the campaign. In order
62  * to avoid that problem, filter all pending DOUBLE_CLICK_EVENT events after
63  * the window is closed.
64  */
65  SDL_FlushEvent(DOUBLE_CLICK_EVENT);
66 
68 
69  post_show(*window);
70 
71  return retval_ == twindow::OK;
72 }
73 
75  const std::string& id,
76  const bool mandatory,
77  const std::function<bool()>& callback_load_value,
78  const std::function<void(const bool)>& callback_save_value,
79  const std::function<void(twidget&)>& callback_change)
80 {
81  tfield_bool* field = new tfield_bool(id,
82  mandatory,
83  callback_load_value,
84  callback_save_value,
85  callback_change);
86 
87  fields_.push_back(field);
88  return field;
89 }
90 
93  const bool mandatory,
94  bool& linked_variable,
95  const std::function<void(twidget&)>& callback_change)
96 {
98  = new tfield_bool(id, mandatory, linked_variable, callback_change);
99 
100  fields_.push_back(field);
101  return field;
102 }
103 
105  const std::string& id,
106  const bool mandatory,
107  const std::function<int()>& callback_load_value,
108  const std::function<void(const int)>& callback_save_value)
109 {
111  id, mandatory, callback_load_value, callback_save_value);
112 
113  fields_.push_back(field);
114  return field;
115 }
116 
118  const bool mandatory,
119  int& linked_variable)
120 {
121  tfield_integer* field = new tfield_integer(id, mandatory, linked_variable);
122 
123  fields_.push_back(field);
124  return field;
125 }
126 
128  const std::string& id,
129  const bool mandatory,
130  const std::function<std::string()>& callback_load_value,
131  const std::function<void(const std::string&)>& callback_save_value,
132  const bool capture_focus)
133 {
135  id, mandatory, callback_load_value, callback_save_value);
136 
137  if(capture_focus) {
138  focus_ = id;
139  }
140 
141  fields_.push_back(field);
142  return field;
143 }
144 
146  const bool mandatory,
147  std::string& linked_variable,
148  const bool capture_focus)
149 {
150  tfield_text* field = new tfield_text(id, mandatory, linked_variable);
151 
152  if(capture_focus) {
153  focus_ = id;
154  }
155 
156  fields_.push_back(field);
157  return field;
158 }
159 
161  const bool mandatory,
162  const std::string& text,
163  const bool use_markup)
164 {
165  tfield_label* field = new tfield_label(id, mandatory, text, use_markup);
166 
167  fields_.push_back(field);
168  return field;
169 }
170 
172 {
173  return build(video, window_id());
174 }
175 
176 void tdialog::post_build(twindow& /*window*/)
177 {
178  /* DO NOTHING */
179 }
180 
181 void tdialog::pre_show(twindow& /*window*/)
182 {
183  /* DO NOTHING */
184 }
185 
186 void tdialog::post_show(twindow& /*window*/)
187 {
188  /* DO NOTHING */
189 }
190 
192 {
193  for(auto field : fields_)
194  {
195  field->attach_to_window(window);
196  field->widget_init(window);
197  }
198 
199  if(!focus_.empty()) {
200  if(twidget* widget = window.find(focus_, false)) {
201  window.keyboard_capture(widget);
202  }
203  }
204 }
205 
206 void tdialog::finalize_fields(twindow& window, const bool save_fields)
207 {
208  for(auto field : fields_)
209  {
210  if(save_fields) {
211  field->widget_finalize(window);
212  }
213  field->detach_from_window();
214  }
215 }
216 
217 } // namespace gui2
218 
219 
220 /*WIKI
221  * @page = GUIWindowDefinitionWML
222  * @order = 1
223  *
224  * {{Autogenerated}}
225  *
226  * = Window definition =
227  *
228  * The window definition define how the windows shown in the dialog look.
229  */
230 
231 /*WIKI
232  * @page = GUIWindowDefinitionWML
233  * @order = ZZZZZZ_footer
234  *
235  * [[Category: WML Reference]]
236  * [[Category: GUI WML Reference]]
237  */
twidget * find(const std::string &id, const bool must_be_active) override
See twidget::find.
Definition: window.cpp:958
virtual void pre_show(twindow &window)
Actions to be taken before showing the window.
Definition: dialog.cpp:181
bool always_save_fields_
Always save the fields upon closing.
Definition: dialog.hpp:315
twindow * build(CVideo &video, const twindow_builder::tresolution *definition)
Builds a window.
virtual void post_build(twindow &window)
Actions to be taken directly after the window is build.
Definition: dialog.cpp:176
int retval_
Returns the window exit status, 0 means not shown.
Definition: dialog.hpp:305
bool show(CVideo &video, const unsigned auto_close_time=0)
Shows the window.
Definition: dialog.cpp:34
Template class to implement the generic field implementation.
Definition: field-fwd.hpp:36
Definition: video.hpp:58
Implements some helper classes to ease adding fields to a dialog and hide the synchronization needed...
-file util.hpp
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
Dialog is closed with ok button.
Definition: window.hpp:125
virtual void post_show(twindow &window)
Actions to be taken after the window has been shown.
Definition: dialog.cpp:186
tfield_integer * register_integer(const std::string &id, const bool mandatory, const std::function< int()> &callback_load_value=std::function< int()>(), const std::function< void(const int)> &callback_save_value=std::function< void(const int)>())
Creates a new integer field.
Definition: dialog.cpp:104
static void field(LexState *ls, struct ConsControl *cc)
Definition: lparser.cpp:707
static size_t id
Ids for the timers.
Definition: timer.cpp:39
#define DOUBLE_CLICK_EVENT
Definition: events.hpp:23
bool restore_
Restore the screen after showing?
Definition: dialog.hpp:338
virtual ~tdialog()
Definition: dialog.cpp:26
virtual void init_fields(twindow &window)
Initializes all fields in the dialog and set the keyboard focus.
Definition: dialog.cpp:191
tfield_label * register_label(const std::string &id, const bool mandatory, const std::string &text, const bool use_markup=false)
Registers a new control as a label.
Definition: dialog.cpp:160
bool faked() const
Definition: video.hpp:166
twindow * build_window(CVideo &video) const
Builds the window.
Definition: dialog.cpp:171
Base class for all widgets.
Definition: widget.hpp:49
std::vector< tfield_ * > fields_
Contains the automatically managed fields.
Definition: dialog.hpp:324
tfield_bool * register_bool(const std::string &id, const bool mandatory, const std::function< bool()> &callback_load_value=std::function< bool()>(), const std::function< void(const bool)> &callback_save_value=std::function< void(const bool)>(), const std::function< void(twidget &)> &callback_change=std::function< void(twidget &)>())
Creates a new boolean field.
Definition: dialog.cpp:74
virtual void finalize_fields(twindow &window, const bool save_fields)
When the dialog is closed with the OK status saves all fields.
Definition: dialog.cpp:206
std::string focus_
Contains the widget that should get the focus when the window is shown.
Definition: dialog.hpp:329
void keyboard_capture(twidget *widget)
Definition: window.cpp:1394
virtual const std::string & window_id() const =0
The id of the window to build.
Specialized field class for boolean.
Definition: field.hpp:547
tfield_text * register_text(const std::string &id, const bool mandatory, const std::function< std::string()> &callback_load_value=std::function< std::string()>(), const std::function< void(const std::string &)> &callback_save_value=std::function< void(const std::string &)>(), const bool capture_focus=false)
Creates a new text field.
Definition: dialog.cpp:127
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Specialized field class for a control, used for labels and images.
Definition: field.hpp:622
Specialized field class for text.
Definition: field.hpp:588
tfield< int, tinteger_selector_ > tfield_integer
Definition: field-fwd.hpp:37