The Battle for Wesnoth  1.17.0-dev
window_builder.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
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/grid.hpp"
21 #include "color.hpp"
22 
23 #include <functional>
24 
25 class config;
26 
27 namespace gui2
28 {
29 
30 class window;
31 
32 /** Contains the info needed to instantiate a widget. */
34 {
35 public:
36  /**
37  * The replacements type is used to define replacement types.
38  *
39  * Certain widgets need to build a part of themselves upon instantiation
40  * but at the time of the definition it's not yet known what exactly. By
41  * using and `[instance]' widget this decision can be postponed until
42  * instantiation.
43  */
44  typedef std::map<std::string, std::shared_ptr<builder_widget>> replacements_map;
45 
46  explicit builder_widget(const config& cfg);
47 
48  virtual ~builder_widget()
49  {
50  }
51 
52  virtual widget* build() const = 0;
53 
54  virtual widget* build(const replacements_map& replacements) const = 0;
55 
56  /** Parameters for the widget. */
57  std::string id;
58  std::string linked_group;
59 
62 };
63 
64 typedef std::shared_ptr<builder_widget> builder_widget_ptr;
65 typedef std::shared_ptr<const builder_widget> builder_widget_const_ptr;
66 
67 /**
68  * Create a widget builder.
69  *
70  * This object holds the instance builder for a single widget.
71  *
72  * @param cfg The config object holding the information
73  * regarding the widget instance.
74  *
75  * @returns The builder for the widget instance.
76  */
77 builder_widget_ptr create_widget_builder(const config& cfg);
78 
79 /**
80  * Helper function to implement @ref build_single_widget_instance. This keeps the main
81  * logic in the implementation despite said function being a template and therefor
82  * needing to be fully implemented in the declaration.
83  */
84 widget* build_single_widget_instance_helper(const std::string& type, const config& cfg);
85 
86 /**
87  * Builds a single widget instance of the given type with the specified attributes.
88  *
89  * This should be used in place of creating a widget object directly, as it
90  * allows the widget-specific builder code to be executed.
91  *
92  * @tparam T The final widget type. The widget pointer will be
93  * cast to this.
94  *
95  * @param cfg Data config to pass to the widget's builder.
96  */
97 template<typename T>
99 {
100  return dynamic_cast<T*>(build_single_widget_instance_helper(T::type(), cfg));
101 }
102 
104 {
105 public:
106  explicit builder_grid(const config& cfg);
107 
108  unsigned rows;
109  unsigned cols;
110 
111  /** The grow factor for the rows / columns. */
112  std::vector<unsigned> row_grow_factor;
113  std::vector<unsigned> col_grow_factor;
114 
115  /** The flags per grid cell. */
116  std::vector<unsigned> flags;
117 
118  /** The border size per grid cell. */
119  std::vector<unsigned> border_size;
120 
121  /** The widgets per grid cell. */
122  std::vector<builder_widget_ptr> widgets;
123 
124  virtual grid* build() const override;
125  virtual widget* build(const replacements_map& replacements) const override;
126 
127  grid* build(grid* grid) const;
128  void build(grid& grid, const replacements_map& replacements) const;
129 };
130 
131 typedef std::shared_ptr<builder_grid> builder_grid_ptr;
132 typedef std::shared_ptr<const builder_grid> builder_grid_const_ptr;
133 
135 {
136 public:
137  explicit builder_window(const config& cfg)
138  : resolutions()
139  , id_(cfg["id"])
140  , description_(cfg["description"])
141  {
142  read(cfg);
143  }
144 
145  /**
146  * Key |Type |Default |Description
147  * --------------------|----------------------------------------|---------|-------------
148  * window_width | @ref guivartype_unsigned "unsigned" |0 |Width of the application window.
149  * window_height | @ref guivartype_unsigned "unsigned" |0 |Height of the application window.
150  * automatic_placement | @ref guivartype_bool "bool" |true |Automatically calculate the best size for the window and place it. If automatically placed vertical_placement and horizontal_placement can be used to modify the final placement. If not automatically placed the width and height are mandatory.
151  * x | @ref guivartype_f_unsigned "f_unsigned"|0 |X coordinate of the window to show.
152  * y | @ref guivartype_f_unsigned "f_unsigned"|0 |Y coordinate of the window to show.
153  * width | @ref guivartype_f_unsigned "f_unsigned"|0 |Width of the window to show.
154  * height | @ref guivartype_f_unsigned "f_unsigned"|0 |Height of the window to show.
155  * reevaluate_best_size| @ref guivartype_f_bool "f_bool" |false |The foo
156  * functions | @ref guivartype_function "function" |"" |The function definitions s available for the formula fields in window.
157  * vertical_placement | @ref guivartype_v_align "v_align" |"" |The vertical placement of the window.
158  * horizontal_placement| @ref guivartype_h_align "h_align" |"" |The horizontal placement of the window.
159  * maximum_width | @ref guivartype_unsigned "unsigned" |0 |The maximum width of the window (only used for automatic placement).
160  * maximum_height | @ref guivartype_unsigned "unsigned" |0 |The maximum height of the window (only used for automatic placement).
161  * click_dismiss | @ref guivartype_bool "bool" |false |Does the window need click dismiss behavior? Click dismiss behavior means that any mouse click will close the dialog. Note certain widgets will automatically disable this behavior since they need to process the clicks as well, for example buttons do need a click and a misclick on button shouldn't close the dialog. NOTE with some widgets this behavior depends on their contents (like scrolling labels) so the behavior might get changed depending on the data in the dialog. NOTE the default behavior might be changed since it will be disabled when can't be used due to widgets which use the mouse, including buttons, so it might be wise to set the behavior explicitly when not wanted and no mouse using widgets are available. This means enter, escape or an external source needs to be used to close the dialog (which is valid).
162  * definition | @ref guivartype_string "string" |"default"|Definition of the window which we want to show.
163  * linked_group | sections |[] |A group of linked widget sections.
164  * tooltip | @ref guivartype_section "section" |mandatory|Information regarding the tooltip for this window.
165  * helptip | @ref guivartype_section "section" |mandatory|Information regarding the helptip for this window.
166  * grid | @ref guivartype_grid "grid" |mandatory|The grid with the widgets to show.
167  *
168  * A linked_group section has the following fields and needs to have at least one size fixed:
169  * Key |Type |Default |Description
170  * --------------------|----------------------------------------|---------|-------------
171  * id | @ref guivartype_string "string" |mandatory|The unique id of the group (unique in this window).
172  * fixed_width | @ref guivartype_bool "bool" |false |Should widget in this group have the same width.
173  * fixed_height | @ref guivartype_bool "bool" |false |Should widget in this group have the same height.
174  *
175  * A tooltip and helptip section have the following field; more fields will probably be added later on:
176  * Key |Type |Default |Description
177  * --------------------|----------------------------------------|---------|-------------
178  * id | @ref guivartype_string "string" |mandatory|The id of the tip to show.
179  */
181  {
182  public:
183  explicit window_resolution(const config& cfg);
184 
185  unsigned window_width;
186  unsigned window_height;
187 
189 
195 
197 
200 
203 
205 
206  std::string definition;
207 
208  std::vector<linked_group_definition> linked_groups;
209 
210  /** Helper struct to store information about the tips. */
212  {
213  tooltip_info(const config& cfg, const std::string& tagname);
214 
215  std::string id;
216  };
217 
220 
221  builder_grid_ptr grid;
222  };
223 
224  /**
225  * Resolution options for this window instance.
226  *
227  * The window widget handles resolution options differently from other widgets.
228  * Most specify their resolution options in their definitions. However, windows
229  * define different resolution options for each window *instance*. That enables
230  * each dialog to have its own set of options.
231  */
232  std::vector<window_resolution> resolutions;
233 
234 private:
235  void read(const config& cfg);
236 
237  std::string id_;
238  std::string description_;
239 };
240 
241 /**
242  * Builds a window.
243  *
244  * @param type The type id string of the window, this window
245  * must be registered at startup.
246  */
247 std::unique_ptr<window> build(const std::string& type);
248 
249 /**
250  * Builds a window.
251  */
252 std::unique_ptr<window> build(const builder_window::window_resolution& res);
253 
254 } // namespace gui2
Contains the info needed to instantiate a widget.
widget::debug_border debug_border_mode
virtual widget * build() const =0
Key Type Default Description window_width unsigned 0 Width of the application window.
Base class for all widgets.
Definition: widget.hpp:49
std::shared_ptr< builder_widget > builder_widget_ptr
Generic file dialog.
Definition: field-fwd.hpp:23
std::vector< linked_group_definition > linked_groups
Base container class.
Definition: grid.hpp:31
std::shared_ptr< const builder_widget > builder_widget_const_ptr
std::map< std::string, std::shared_ptr< builder_widget > > replacements_map
The replacements type is used to define replacement types.
typed_formula< unsigned > maximum_height
std::vector< unsigned > row_grow_factor
The grow factor for the rows / columns.
void read(config &cfg, std::istream &in, abstract_validator *validator)
Definition: parser.cpp:627
std::vector< unsigned > col_grow_factor
widget * build_single_widget_instance_helper(const std::string &type, const config &cfg)
Helper function to implement build_single_widget_instance.
builder_window(const config &cfg)
T * build_single_widget_instance(const config &cfg=config())
Builds a single widget instance of the given type with the specified attributes.
std::string id
Parameters for the widget.
std::vector< unsigned > flags
The flags per grid cell.
builder_widget(const config &cfg)
std::shared_ptr< const builder_grid > builder_grid_const_ptr
Helper struct to store information about the tips.
wfl::function_symbol_table functions
std::vector< builder_widget_ptr > widgets
The widgets per grid cell.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
std::shared_ptr< builder_grid > builder_grid_ptr
std::vector< window_resolution > resolutions
Resolution options for this window instance.
std::vector< unsigned > border_size
The border size per grid cell.
typed_formula< unsigned > maximum_width
builder_widget_ptr create_widget_builder(const config &cfg)
Create a widget builder.