The Battle for Wesnoth  1.19.0-dev
window_builder.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/grid.hpp"
21 #include "color.hpp"
23 
24 
25 
26 namespace gui2
27 {
28 
29 class window;
30 
31 /** Contains the info needed to instantiate a widget. */
33 {
34  /**
35  * The replacements type is used to define replacement types.
36  *
37  * Certain widgets need to build a part of themselves upon instantiation
38  * but at the time of the definition it's not yet known what exactly. By
39  * using and `[instance]' widget this decision can be postponed until
40  * instantiation.
41  */
42  using replacements_map = std::map<std::string, std::shared_ptr<builder_widget>>;
43 
44  /**
45  * @todo: evaluate whether to combine the two @ref build() functions with this
46  * as the sole parameter.
47  */
49 
50  explicit builder_widget(const config& cfg);
51 
52  virtual ~builder_widget()
53  {
54  }
55 
56  virtual std::unique_ptr<widget> build() const = 0;
57 
58  virtual std::unique_ptr<widget> build(const replacements_map& replacements) const = 0;
59 
60  /** Parameters for the widget. */
61  std::string id;
62  std::string linked_group;
63 
66 };
67 
68 using builder_widget_ptr = std::shared_ptr<builder_widget>;
69 using builder_widget_const_ptr = std::shared_ptr<const builder_widget>;
70 
71 /**
72  * Create a widget builder.
73  *
74  * This object holds the instance builder for a single widget.
75  *
76  * @param cfg The config object holding the information
77  * regarding the widget instance.
78  *
79  * @returns The builder for the widget instance.
80  */
82 
83 /**
84  * Implementation detail for @ref build_single_widget_instance. Do not use directly!
85  *
86  * @param type String ID of the widget type.
87  * @param cfg Data config to pass to the widget's builder.
88  *
89  * @returns A shared_ptr of the base widget type containing
90  * the newly built widget.
91  */
92 std::unique_ptr<widget> build_single_widget_instance_helper(const std::string& type, const config& cfg);
93 
94 /**
95  * Builds a single widget instance of the given type with the specified attributes.
96  *
97  * This should be used in place of creating a widget object directly, as it
98  * allows the widget-specific builder code to be executed.
99  *
100  * This is equivalent to calling @c build() on the result of @ref create_widget_builder.
101  *
102  * @tparam T The final widget type. The widget pointer will be
103  * cast to this.
104  *
105  * @param cfg Data config to pass to the widget's builder.
106  *
107  * @returns A shared_ptr of the given type containing the
108  * newly build widget.
109  */
110 template<typename T>
111 std::unique_ptr<T> build_single_widget_instance(const config& cfg = {})
112 {
113  static_assert(std::is_base_of_v<widget, T>, "Type is not a widget type");
114  return std::unique_ptr<T>{ static_cast<T*>(build_single_widget_instance_helper(T::type(), cfg).release()) };
115 }
116 
118 {
119  explicit builder_grid(const config& cfg);
120 
121  unsigned rows;
122  unsigned cols;
123 
124  /** The grow factor for the rows / columns. */
125  std::vector<unsigned> row_grow_factor;
126  std::vector<unsigned> col_grow_factor;
127 
128  /** The flags per grid cell. */
129  std::vector<unsigned> flags;
130 
131  /** The border size per grid cell. */
132  std::vector<unsigned> border_size;
133 
134  /** The widgets per grid cell. */
135  std::vector<builder_widget_ptr> widgets;
136 
137  /** Inherited from @ref builder_widget. */
138  virtual std::unique_ptr<widget> build() const override;
139 
140  /** Inherited from @ref builder_widget. */
141  virtual std::unique_ptr<widget> build(const replacements_map& replacements) const override;
142 
143  void build(grid& grid, optional_replacements replacements = std::nullopt) const;
144 };
145 
146 using builder_grid_ptr = std::shared_ptr<builder_grid>;
147 using builder_grid_const_ptr = std::shared_ptr<const builder_grid>;
148 using builder_grid_map = std::map<std::string, builder_grid_const_ptr>;
149 
151 {
152 public:
153  explicit builder_window(const config& cfg)
154  : resolutions()
155  , id_(cfg["id"])
156  , description_(cfg["description"])
157  {
158  read(cfg);
159  }
160 
161  /**
162  * Key |Type |Default |Description
163  * --------------------|----------------------------------------|---------|-------------
164  * window_width | @ref guivartype_unsigned "unsigned" |0 |Width of the application window.
165  * window_height | @ref guivartype_unsigned "unsigned" |0 |Height of the application window.
166  * 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.
167  * x | @ref guivartype_f_unsigned "f_unsigned"|0 |X coordinate of the window to show.
168  * y | @ref guivartype_f_unsigned "f_unsigned"|0 |Y coordinate of the window to show.
169  * width | @ref guivartype_f_unsigned "f_unsigned"|0 |Width of the window to show.
170  * height | @ref guivartype_f_unsigned "f_unsigned"|0 |Height of the window to show.
171  * reevaluate_best_size| @ref guivartype_f_bool "f_bool" |false |The foo
172  * functions | @ref guivartype_function "function" |"" |The function definitions s available for the formula fields in window.
173  * vertical_placement | @ref guivartype_v_align "v_align" |"" |The vertical placement of the window.
174  * horizontal_placement| @ref guivartype_h_align "h_align" |"" |The horizontal placement of the window.
175  * maximum_width | @ref guivartype_unsigned "unsigned" |0 |The maximum width of the window (only used for automatic placement).
176  * maximum_height | @ref guivartype_unsigned "unsigned" |0 |The maximum height of the window (only used for automatic placement).
177  * 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).
178  * definition | @ref guivartype_string "string" |"default"|Definition of the window which we want to show.
179  * linked_group | sections |[] |A group of linked widget sections.
180  * tooltip | @ref guivartype_section "section" |mandatory|Information regarding the tooltip for this window.
181  * helptip | @ref guivartype_section "section" |mandatory|Information regarding the helptip for this window.
182  * grid | @ref guivartype_grid "grid" |mandatory|The grid with the widgets to show.
183  *
184  * A linked_group section has the following fields and needs to have at least one size fixed:
185  * Key |Type |Default |Description
186  * --------------------|----------------------------------------|---------|-------------
187  * id | @ref guivartype_string "string" |mandatory|The unique id of the group (unique in this window).
188  * fixed_width | @ref guivartype_bool "bool" |false |Should widget in this group have the same width.
189  * fixed_height | @ref guivartype_bool "bool" |false |Should widget in this group have the same height.
190  *
191  * A tooltip and helptip section have the following field; more fields will probably be added later on:
192  * Key |Type |Default |Description
193  * --------------------|----------------------------------------|---------|-------------
194  * id | @ref guivartype_string "string" |mandatory|The id of the tip to show.
195  */
197  {
198  explicit window_resolution(const config& cfg);
199 
200  unsigned window_width;
201  unsigned window_height;
202 
204 
210 
212 
215 
218 
220 
221  std::string definition;
222 
223  std::vector<linked_group_definition> linked_groups;
224 
225  /** Helper struct to store information about the tips. */
227  {
228  tooltip_info(const config& cfg, const std::string& tagname);
229 
230  std::string id;
231  };
232 
235 
237  };
238 
239  /**
240  * Resolution options for this window instance.
241  *
242  * The window widget handles resolution options differently from other widgets.
243  * Most specify their resolution options in their definitions. However, windows
244  * define different resolution options for each window *instance*. That enables
245  * each dialog to have its own set of options.
246  */
247  std::vector<window_resolution> resolutions;
248 
249 private:
250  void read(const config& cfg);
251 
252  std::string id_;
253  std::string description_;
254 };
255 
256 /**
257  * Builds a window.
258  *
259  * @param type The type id string of the window, this window
260  * must be registered at startup.
261  */
262 std::unique_ptr<window> build(const std::string& type);
263 
264 /**
265  * Builds a window.
266  */
267 std::unique_ptr<window> build(const builder_window::window_resolution& res);
268 
269 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
builder_window(const config &cfg)
std::vector< window_resolution > resolutions
Resolution options for this window instance.
void read(const config &cfg)
Base container class.
Definition: grid.hpp:32
A simple wrapper class for optional reference types.
Generic file dialog.
std::shared_ptr< builder_grid > builder_grid_ptr
std::shared_ptr< builder_widget > builder_widget_ptr
std::unique_ptr< T > build_single_widget_instance(const config &cfg={})
Builds a single widget instance of the given type with the specified attributes.
std::unique_ptr< widget > build_single_widget_instance_helper(const std::string &type, const config &cfg)
Implementation detail for build_single_widget_instance.
builder_widget_ptr create_widget_builder(const config &cfg)
Create a widget builder.
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
std::map< std::string, builder_grid_const_ptr > builder_grid_map
std::shared_ptr< const builder_widget > builder_widget_const_ptr
std::shared_ptr< const builder_grid > builder_grid_const_ptr
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
std::vector< unsigned > col_grow_factor
std::vector< unsigned > flags
The flags per grid cell.
std::vector< builder_widget_ptr > widgets
The widgets per grid cell.
builder_grid(const config &cfg)
std::vector< unsigned > row_grow_factor
The grow factor for the rows / columns.
virtual std::unique_ptr< widget > build() const override
Inherited from builder_widget.
std::vector< unsigned > border_size
The border size per grid cell.
Contains the info needed to instantiate a widget.
virtual std::unique_ptr< widget > build(const replacements_map &replacements) const =0
widget::debug_border debug_border_mode
virtual std::unique_ptr< widget > build() const =0
builder_widget(const config &cfg)
std::string id
Parameters for the widget.
std::map< std::string, std::shared_ptr< builder_widget > > replacements_map
The replacements type is used to define replacement types.
Helper struct to store information about the tips.
tooltip_info(const config &cfg, const std::string &tagname)
std::vector< linked_group_definition > linked_groups
wfl::function_symbol_table functions
typed_formula< unsigned > maximum_height
typed_formula< unsigned > maximum_width