The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
modal_dialog.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2008 - 2025
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#pragma once
17
21
22#include <functional>
23#include <string>
24#include <vector>
25
26namespace gui2::dialogs
27{
28/**
29 * Registers a window.
30 *
31 * This function registers a window. The registration is used to validate
32 * whether the config for the window exists when starting Wesnoth.
33 *
34 * @note Most of the time you want to call @ref REGISTER_DIALOG instead of this
35 * function. It also directly adds the code for the dialog's id function.
36 *
37 * @param id Id of the window, multiple dialogs can use
38 * the same window so the id doesn't need to be
39 * unique.
40 */
41#define REGISTER_WINDOW(id) \
42 namespace \
43 { \
44 namespace ns_##id \
45 { \
46 struct register_helper \
47 { \
48 register_helper() \
49 { \
50 register_window(#id); \
51 } \
52 }; \
53 \
54 struct register_helper register_helper; \
55 } \
56 }
57
58/**
59 * Registers a window for a dialog.
60 *
61 * Call this function to register a window. In the header of the class it adds
62 * the following code:
63 *@code
64 * virtual const std::string& id() const;
65 *@endcode
66 * Then use this macro in the implementation, inside the gui2 namespace.
67 *
68 * @note When the @p id is "foo" and the type tfoo it's easier to use
69 * REGISTER_DIALOG(foo).
70 *
71 * @param type Class type of the window to register.
72 * @param id Id of the window, multiple dialogs can use
73 * the same window so the id doesn't need to be
74 * unique.
75 */
76#define REGISTER_DIALOG2(type, id) \
77 REGISTER_WINDOW(id) \
78 const std::string& type::window_id() const \
79 { \
80 static const std::string result(#id); \
81 return result; \
82 }
83
84/**
85 * Wrapper for REGISTER_DIALOG2.
86 *
87 * "Calls" REGISTER_DIALOG2(window_id, window_id)
88 */
89#define REGISTER_DIALOG(window_id) REGISTER_DIALOG2(window_id, window_id)
90
91/**
92 * Adds a bare-bones static `display` function to a dialog class that immediately
93 * invokes the dialogs's modal_dialog::show function. If more complex behavior
94 * is desired, the function should be defined manually.
95 *
96 * See the modal_dialog documentation (below) for more info.
97 */
98#define DEFINE_SIMPLE_DISPLAY_WRAPPER(dialog) \
99 template<typename... T> \
100 static void display(T&&... args) \
101 { \
102 dialog(std::forward<T>(args)...).show(); \
103 }
104
105/**
106 * Adds a bare-bonesstatic `execute` function to a dialog class that immediately
107 * invokes and return the result of the dialogs's modal_dialog::show function.
108 * If more complex behavior is desired, the function should be defined manually.
109 *
110 * See the modal_dialog documentation (below) for more info.
111 */
112#define DEFINE_SIMPLE_EXECUTE_WRAPPER(dialog) \
113 template<typename... T> \
114 static bool execute(T&&... args) \
115 { \
116 return dialog(std::forward<T>(args)...).show(); \
117 }
118
119/**
120 * Abstract base class for all modal dialogs.
121 *
122 * A dialog shows a certain window instance to the user. The subclasses of this
123 * class will hold the parameters used for a certain window, eg a server
124 * connection dialog will hold the name of the selected server as parameter that
125 * way the caller doesn't need to know about the 'contents' of the window.
126 *
127 * @par Usage
128 *
129 * Simple dialogs that are shown to query user information it is recommended to
130 * add a static member called @p execute. The parameters to the function are:
131 * - references to in + out parameters by reference
132 * - references to the in parameters
133 * - the parameters for @ref modal_dialog::show.
134 *
135 * The 'in + out parameters' are used as initial value and final value when the
136 * OK button is pressed. The 'in parameters' are just extra parameters for
137 * showing.
138 *
139 * When a function only has 'in parameters' it should return a void value and
140 * the function should be called @p display, if it has 'in + out parameters' it
141 * must return a bool value. This value indicates whether or not the OK button
142 * was pressed to close the dialog. See editor_new_map::execute for an
143 * example.
144 */
145class modal_dialog : public window
146{
147 /**
148 * Special helper function to get the id of the window.
149 *
150 * This is used in the unit tests, but these implementation details
151 * shouldn't be used in the normal code.
152 */
153 friend std::string get_modal_dialog_id(const modal_dialog& dialog);
154
155public:
156 explicit modal_dialog(const std::string& window_id);
157
158 virtual ~modal_dialog();
159
160 /**
161 * Shows the window.
162 *
163 * @param auto_close_time The time in ms after which the dialog will
164 * automatically close, if 0 it doesn't close.
165 * @note the timeout is a minimum time and
166 * there's no guarantee about how fast it closes
167 * after the minimum.
168 *
169 * @returns Whether the final retval_ == retval::OK
170 */
171 bool show(const unsigned auto_close_time = 0);
172
173 /***** ***** ***** setters / getters for members ***** ****** *****/
174
175 void set_always_save_fields(const bool always_save_fields)
176 {
177 always_save_fields_ = always_save_fields;
178 }
179
180 void set_allow_plugin_skip(const bool allow_plugin_skip)
181 {
182 allow_plugin_skip_ = allow_plugin_skip;
183 }
184
185 void set_show_even_without_video(const bool show_even_without_video)
186 {
187 show_even_without_video_ = show_even_without_video;
188 }
189
190protected:
191 /**
192 * Creates a new field of given type with given arguments.
193 *
194 * The field created is owned by modal_dialog, the returned pointer can be used
195 * in the child classes as access to a field.
196 *
197 * @param args Arguments to forward to the field constructor.
198 */
199 template<typename T, typename... Args>
200 T* register_field(Args&&... args);
201
202 /**
203 * Creates a new boolean field.
204 *
205 * The field created is owned by modal_dialog, the returned pointer can be used
206 * in the child classes as access to a field.
207 *
208 * @param id Id of the widget, same value as in WML.
209 * @param mandatory Is the widget mandatory or optional?
210 * @param callback_load_value The callback function to set the initial value
211 * of the widget.
212 * @param callback_save_value The callback function to write the resulting
213 * value of the widget. Saving will only happen
214 * if the widget is enabled and the window closed
215 * with ok.
216 * @param callback_change When the value of the widget changes this
217 * callback is called.
218 * @param initial_fire
219 *
220 * @returns Pointer to the created widget.
221 */
223 register_bool(const std::string& id,
224 const bool mandatory,
225 const std::function<bool()>& callback_load_value = nullptr,
226 const std::function<void(bool)>& callback_save_value = nullptr,
227 const std::function<void(widget&)>& callback_change = nullptr,
228 const bool initial_fire = false);
229
230 /**
231 * Creates a new boolean field.
232 *
233 * The field created is owned by modal_dialog, the returned pointer can be used
234 * in the child classes as access to a field.
235 *
236 * @param id Id of the widget, same value as in WML.
237 * @param mandatory Is the widget mandatory or optional?
238 * @param linked_variable The variable the widget is linked to. See
239 * @ref field::field for more information.
240 * @param callback_change When the value of the widget changes this
241 * callback is called.
242 * @param initial_fire
243 *
244 * @returns Pointer to the created widget.
245 */
247 register_bool(const std::string& id,
248 const bool mandatory,
249 bool& linked_variable,
250 const std::function<void(widget&)>& callback_change = nullptr,
251 const bool initial_fire = false);
252
253 /**
254 * Creates a new integer field.
255 *
256 * See @ref register_bool for more info.
257 */
259 register_integer(const std::string& id,
260 const bool mandatory,
261 const std::function<int()>& callback_load_value = nullptr,
262 const std::function<void(int)>& callback_save_value = nullptr);
263
264 /**
265 * Creates a new integer field.
266 *
267 * See @ref register_bool for more info.
268 */
269 field_integer* register_integer(const std::string& id,
270 const bool mandatory,
271 int& linked_variable);
272 /**
273 * Creates a new text field.
274 *
275 * See @ref register_bool for more info.
276 */
278 const std::string& id,
279 const bool mandatory,
280 const std::function<std::string()>& callback_load_value = nullptr,
281 const std::function<void(const std::string&)>& callback_save_value = nullptr,
282 const bool capture_focus = false);
283
284 /**
285 * Creates a new text field.
286 *
287 * See @ref register_bool for more info.
288 */
289 field_text* register_text(const std::string& id,
290 const bool mandatory,
291 std::string& linked_variable,
292 const bool capture_focus = false);
293
294 /**
295 * Registers a new styled_widget as a label.
296 *
297 * The label is used for a styled_widget to set the 'label' since it calls the
298 * @ref styled_widget::set_label it can also be used for the @ref image since
299 * there this sets the filename. (The @p use_markup makes no sense in an
300 * image but that's a detail.)
301 *
302 * @note In general it's preferred a widget sets its markup flag in WML, but
303 * some generic windows (like messages) may need different versions
304 * depending on where used.
305 *
306 * @param id Id of the widget, same value as in WML.
307 * @param mandatory Is the widget mandatory or optional?
308 * @param text The text for the label.
309 * @param use_markup Whether or not use markup for the label.
310 */
311 field_label* register_label(const std::string& id,
312 const bool mandatory,
313 const std::string& text,
314 const bool use_markup = false);
315
316 /** Registers a new styled_widget as image. */
317 field_label* register_image(const std::string& id,
318 const bool mandatory,
319 const std::string& filename)
320 {
321 return register_label(id, mandatory, filename);
322 }
323
324private:
325 /**
326 * Always save the fields upon closing.
327 *
328 * Normally fields are only saved when the retval::OK button is pressed.
329 * With this flag set is always saves. Be careful with the flag since it
330 * also updates upon canceling, which can be a problem when the field sets
331 * a preference.
332 */
334
335 /**
336 * Contains the automatically managed fields.
337 *
338 * Since the fields are automatically managed and there are no search
339 * functions defined we don't offer access to the vector. If access is
340 * needed the creator should store a copy of the pointer.
341 */
342 std::vector<std::unique_ptr<class field_base>> fields_;
343
344 /**
345 * Contains the widget that should get the focus when the window is shown.
346 */
347 std::string focus_;
348
349 /**
350 * Allow plugins to skip through the dialog?
351 * Most dialogs install a plugins context to allow plugins to accept whatever the dialog is offering
352 * and continue. Some dialogs, especially those that install their own plugins context, may want to
353 * disable this.
354 */
356
357 /**
358 * Show the dialog even with --nogui?
359 * Some dialogs need to be shown even when --nogui is specified if the game is being driven by a plugin.
360 * Those dialogs allow the plugin to styled_widget them by creating a plugin context in pre_show().
361 */
363
364 /** The ID of the window to build. Usually set by REGISTER_DIALOG. */
365 virtual const std::string& window_id() const = 0;
366
367 /**
368 * Actions to be taken before showing the window.
369 *
370 * At this point the registered fields are registered and initialized with
371 * their initial values.
372 */
373 virtual void pre_show();
374
375 /**
376 * Actions to be taken after the window has been shown.
377 *
378 * At this point the registered fields already stored their values
379 * (if the button with id 'ok' has been pressed).
380 */
381 virtual void post_show();
382
383 /**
384 * Initializes all fields in the dialog and set the keyboard focus.
385 */
386 void init_fields();
387
388 /**
389 * When the dialog is closed with the OK status saves all fields.
390 *
391 * Saving only happens if a callback handler is installed.
392 *
393 * @param save_fields Does the value in the fields need to be saved?
394 */
395 void finalize_fields(const bool save_fields);
396};
397
398} // namespace dialogs
Abstract base class for all modal dialogs.
bool always_save_fields_
Always save the fields upon closing.
void init_fields()
Initializes all fields in the dialog and set the keyboard focus.
void set_show_even_without_video(const bool show_even_without_video)
field_bool * register_bool(const std::string &id, const bool mandatory, const std::function< bool()> &callback_load_value=nullptr, const std::function< void(bool)> &callback_save_value=nullptr, const std::function< void(widget &)> &callback_change=nullptr, const bool initial_fire=false)
Creates a new boolean field.
virtual void post_show()
Actions to be taken after the window has been shown.
void set_allow_plugin_skip(const bool allow_plugin_skip)
field_label * register_image(const std::string &id, const bool mandatory, const std::string &filename)
Registers a new styled_widget as image.
virtual const std::string & window_id() const =0
The ID of the window to build.
field_text * register_text(const std::string &id, const bool mandatory, const std::function< std::string()> &callback_load_value=nullptr, const std::function< void(const std::string &)> &callback_save_value=nullptr, const bool capture_focus=false)
Creates a new text field.
std::string focus_
Contains the widget that should get the focus when the window is shown.
std::vector< std::unique_ptr< class field_base > > fields_
Contains the automatically managed fields.
field_label * register_label(const std::string &id, const bool mandatory, const std::string &text, const bool use_markup=false)
Registers a new styled_widget as a label.
friend std::string get_modal_dialog_id(const modal_dialog &dialog)
Special helper function to get the id of the window.
virtual void pre_show()
Actions to be taken before showing the window.
bool allow_plugin_skip_
Allow plugins to skip through the dialog? Most dialogs install a plugins context to allow plugins to ...
void set_always_save_fields(const bool always_save_fields)
field_integer * register_integer(const std::string &id, const bool mandatory, const std::function< int()> &callback_load_value=nullptr, const std::function< void(int)> &callback_save_value=nullptr)
Creates a new integer field.
T * register_field(Args &&... args)
Creates a new field of given type with given arguments.
void finalize_fields(const bool save_fields)
When the dialog is closed with the OK status saves all fields.
bool show_even_without_video_
Show the dialog even with –nogui? Some dialogs need to be shown even when –nogui is specified if the ...
bool show(const unsigned auto_close_time=0)
Shows the window.
Specialized field class for boolean.
Definition field.hpp:488
Specialized field class for a styled_widget, used for labels and images.
Definition field.hpp:568
Specialized field class for text.
Definition field.hpp:536
Template class to implement the generic field implementation.
Definition field.hpp:245
Base class for all widgets.
Definition widget.hpp:55
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition window.hpp:59
Contains all forward declarations for field.hpp.
This file contains the window object, this object is a top level container which has the event manage...
std::string filename
Filename.