The Battle for Wesnoth  1.19.5+dev
modal_dialog.hpp
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 #pragma once
17 
20 #include "gui/widgets/window.hpp"
21 
22 #include <functional>
23 #include <string>
24 #include <vector>
25 
26 namespace 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  */
145 class 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 
155 public:
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  /** Returns the cached window exit code. */
176  int get_retval() const
177  {
178  return retval_;
179  }
180 
181  void set_always_save_fields(const bool always_save_fields)
182  {
183  always_save_fields_ = always_save_fields;
184  }
185 
186  void set_allow_plugin_skip(const bool allow_plugin_skip)
187  {
188  allow_plugin_skip_ = allow_plugin_skip;
189  }
190 
191  void set_show_even_without_video(const bool show_even_without_video)
192  {
193  show_even_without_video_ = show_even_without_video;
194  }
195 
196 protected:
197  /**
198  * Creates a new field of given type with given arguments.
199  *
200  * The field created is owned by modal_dialog, the returned pointer can be used
201  * in the child classes as access to a field.
202  *
203  * @param args Arguments to forward to the field constructor.
204  */
205  template<typename T, typename... Args>
206  T* register_field(Args&&... args);
207 
208  /**
209  * Creates a new boolean field.
210  *
211  * The field created is owned by modal_dialog, the returned pointer can be used
212  * in the child classes as access to a field.
213  *
214  * @param id Id of the widget, same value as in WML.
215  * @param mandatory Is the widget mandatory or mandatory.
216  * @param callback_load_value The callback function to set the initial value
217  * of the widget.
218  * @param callback_save_value The callback function to write the resulting
219  * value of the widget. Saving will only happen
220  * if the widget is enabled and the window closed
221  * with ok.
222  * @param callback_change When the value of the widget changes this
223  * callback is called.
224  * @param initial_fire
225  *
226  * @returns Pointer to the created widget.
227  */
228  field_bool*
229  register_bool(const std::string& id,
230  const bool mandatory,
231  const std::function<bool()> callback_load_value = nullptr,
232  const std::function<void(bool)> callback_save_value = nullptr,
233  const std::function<void(widget&)> callback_change = nullptr,
234  const bool initial_fire = false);
235 
236  /**
237  * Creates a new boolean field.
238  *
239  * The field created is owned by modal_dialog, the returned pointer can be used
240  * in the child classes as access to a field.
241  *
242  * @param id Id of the widget, same value as in WML.
243  * @param mandatory Is the widget mandatory or mandatory.
244  * @param linked_variable The variable the widget is linked to. See
245  * @ref field::field for more information.
246  * @param callback_change When the value of the widget changes this
247  * callback is called.
248  * @param initial_fire
249  *
250  * @returns Pointer to the created widget.
251  */
252  field_bool*
253  register_bool(const std::string& id,
254  const bool mandatory,
255  bool& linked_variable,
256  const std::function<void(widget&)> callback_change = nullptr,
257  const bool initial_fire = false);
258 
259  /**
260  * Creates a new integer field.
261  *
262  * See @ref register_bool for more info.
263  */
265  register_integer(const std::string& id,
266  const bool mandatory,
267  const std::function<int()> callback_load_value = nullptr,
268  const std::function<void(int)> callback_save_value = nullptr);
269 
270  /**
271  * Creates a new integer field.
272  *
273  * See @ref register_bool for more info.
274  */
275  field_integer* register_integer(const std::string& id,
276  const bool mandatory,
277  int& linked_variable);
278  /**
279  * Creates a new text field.
280  *
281  * See @ref register_bool for more info.
282  */
284  const std::string& id,
285  const bool mandatory,
286  const std::function<std::string()> callback_load_value = nullptr,
287  const std::function<void(const std::string&)> callback_save_value = nullptr,
288  const bool capture_focus = false);
289 
290  /**
291  * Creates a new text field.
292  *
293  * See @ref register_bool for more info.
294  */
295  field_text* register_text(const std::string& id,
296  const bool mandatory,
297  std::string& linked_variable,
298  const bool capture_focus = false);
299 
300  /**
301  * Registers a new styled_widget as a label.
302  *
303  * The label is used for a styled_widget to set the 'label' since it calls the
304  * @ref styled_widget::set_label it can also be used for the @ref image since
305  * there this sets the filename. (The @p use_markup makes no sense in an
306  * image but that's a detail.)
307  *
308  * @note In general it's preferred a widget sets its markup flag in WML, but
309  * some generic windows (like messages) may need different versions
310  * depending on where used.
311  *
312  * @param id Id of the widget, same value as in WML.
313  * @param mandatory Is the widget mandatory or optional.
314  * @param text The text for the label.
315  * @param use_markup Whether or not use markup for the label.
316  */
317  field_label* register_label(const std::string& id,
318  const bool mandatory,
319  const std::string& text,
320  const bool use_markup = false);
321 
322  /** Registers a new styled_widget as image. */
323  field_label* register_image(const std::string& id,
324  const bool mandatory,
325  const std::string& filename)
326  {
327  return register_label(id, mandatory, filename);
328  }
329 
330 private:
331  /**
332  * The window's exit code (return value).
333  *
334  * We keep a copy here so it may be accessed even after the dialog is closed and
335  * the window object is destroyed.
336  *
337  * This value is initially set to 0 (retval::NONE) meaning the dialog was not
338  * shown. After @ref show returns, it will hold the most recent retval of the
339  * window object, including any modifications made in @ref post_show.
340  */
341  int retval_;
342 
343  /**
344  * Always save the fields upon closing.
345  *
346  * Normally fields are only saved when the retval::OK button is pressed.
347  * With this flag set is always saves. Be careful with the flag since it
348  * also updates upon canceling, which can be a problem when the field sets
349  * a preference.
350  */
352 
353  /**
354  * Contains the automatically managed fields.
355  *
356  * Since the fields are automatically managed and there are no search
357  * functions defined we don't offer access to the vector. If access is
358  * needed the creator should store a copy of the pointer.
359  */
360  std::vector<std::unique_ptr<class field_base>> fields_;
361 
362  /**
363  * Contains the widget that should get the focus when the window is shown.
364  */
365  std::string focus_;
366 
367  /**
368  * Allow plugins to skip through the dialog?
369  * Most dialogs install a plugins context to allow plugins to accept whatever the dialog is offering
370  * and continue. Some dialogs, especially those that install their own plugins context, may want to
371  * disable this.
372  */
374 
375  /**
376  * Show the dialog even with --nogui?
377  * Some dialogs need to be shown even when --nogui is specified if the game is being driven by a plugin.
378  * Those dialogs allow the plugin to styled_widget them by creating a plugin context in pre_show().
379  */
381 
382  /** The ID of the window to build. Usually set by REGISTER_DIALOG. */
383  virtual const std::string& window_id() const = 0;
384 
385  /**
386  * Actions to be taken before showing the window.
387  *
388  * At this point the registered fields are registered and initialized with
389  * their initial values.
390  */
391  virtual void pre_show();
392 
393  /**
394  * Actions to be taken after the window has been shown.
395  *
396  * At this point the registered fields already stored their values
397  * (if the button with id 'ok' has been pressed).
398  */
399  virtual void post_show();
400 
401  /**
402  * Initializes all fields in the dialog and set the keyboard focus.
403  */
404  void init_fields();
405 
406  /**
407  * When the dialog is closed with the OK status saves all fields.
408  *
409  * Saving only happens if a callback handler is installed.
410  *
411  * @param save_fields Does the value in the fields need to be saved?
412  */
413  void finalize_fields(const bool save_fields);
414 };
415 
416 } // 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)
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.
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.
virtual const std::string & window_id() const =0
The ID of the window to build.
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.
modal_dialog(const std::string &window_id)
friend std::string get_modal_dialog_id(const modal_dialog &dialog)
Special helper function to get the id of the window.
Definition: test_gui2.cpp:179
virtual void pre_show()
Actions to be taken before showing the window.
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.
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)
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 ...
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.
bool show(const unsigned auto_close_time=0)
Shows the window.
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.
int get_retval() const
Returns the cached window exit code.
int retval_
The window's exit code (return value).
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:61
dialogs::modal_dialog * dialog()
Inherited from widget.
Definition: window.hpp:268
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.