The Battle for Wesnoth  1.15.1+dev
window_builder.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://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 #pragma once
16 
19 #include "gui/widgets/grid.hpp"
20 #include "color.hpp"
21 
22 #include "utils/functional.hpp"
23 
24 class config;
25 
26 namespace gui2
27 {
28 
29 class window;
30 
31 /**
32  * Builds a window.
33  *
34  * @param type The type id string of the window, this window
35  * must be registered at startup.
36  */
37 window* build(const std::string& type);
38 
39 /** Contains the info needed to instantiate a widget. */
41 {
42 public:
43  /**
44  * The replacements type is used to define replacement types.
45  *
46  * Certain widgets need to build a part of themselves upon instantiation
47  * but at the time of the definition it's not yet known what exactly. By
48  * using and `[instance]' widget this decision can be postponed until
49  * instantiation.
50  */
51  typedef std::map<std::string, std::shared_ptr<builder_widget>> replacements_map;
52 
53  explicit builder_widget(const config& cfg);
54 
55  virtual ~builder_widget()
56  {
57  }
58 
59  virtual widget* build() const = 0;
60 
61  virtual widget* build(const replacements_map& replacements) const = 0;
62 
63  /** Parameters for the widget. */
64  std::string id;
65  std::string linked_group;
66 
69 };
70 
71 typedef std::shared_ptr<builder_widget> builder_widget_ptr;
72 typedef std::shared_ptr<const builder_widget> builder_widget_const_ptr;
73 
74 /**
75  * Create a widget builder.
76  *
77  * This object holds the instance builder for a single widget.
78  *
79  * @param cfg The config object holding the information
80  * regarding the widget instance.
81  *
82  * @returns The builder for the widget instance.
83  */
84 builder_widget_ptr create_widget_builder(const config& cfg);
85 
86 /**
87  * Helper function to implement @ref build_single_widget_instance. This keeps the main
88  * logic in the implementation despite said function being a template and therefor
89  * needing to be fully implemented in the declaration.
90  */
91 widget* build_single_widget_instance_helper(const std::string& type, const config& cfg);
92 
93 /**
94  * Builds a single widget instance of the given type with the specified attributes.
95  *
96  * This should be used in place of creating a widget object directly, as it
97  * allows the widget-specific builder code to be executed.
98  *
99  * @tparam T The final widget type. The widget pointer will be
100  * cast to this.
101  *
102  * @param cfg Data config to pass to the widget's builder.
103  */
104 template<typename T>
106 {
107  return dynamic_cast<T*>(build_single_widget_instance_helper(T::type(), cfg));
108 }
109 
111 {
112 public:
113  explicit builder_grid(const config& cfg);
114 
115  unsigned rows;
116  unsigned cols;
117 
118  /** The grow factor for the rows / columns. */
119  std::vector<unsigned> row_grow_factor;
120  std::vector<unsigned> col_grow_factor;
121 
122  /** The flags per grid cell. */
123  std::vector<unsigned> flags;
124 
125  /** The border size per grid cell. */
126  std::vector<unsigned> border_size;
127 
128  /** The widgets per grid cell. */
129  std::vector<builder_widget_ptr> widgets;
130 
131  grid* build() const;
132  widget* build(const replacements_map& replacements) const;
133 
134  grid* build(grid* grid) const;
135  void build(grid& grid, const replacements_map& replacements) const;
136 };
137 
138 typedef std::shared_ptr<builder_grid> builder_grid_ptr;
139 typedef std::shared_ptr<const builder_grid> builder_grid_const_ptr;
140 
142 {
143 public:
144  explicit builder_window(const config& cfg)
145  : resolutions()
146  , id_(cfg["id"])
147  , description_(cfg["description"])
148  {
149  read(cfg);
150  }
151 
153  {
154  public:
155  explicit window_resolution(const config& cfg);
156 
157  unsigned window_width;
158  unsigned window_height;
159 
161 
167 
169 
172 
173  unsigned maximum_width;
174  unsigned maximum_height;
175 
177 
178  std::string definition;
179 
180  std::vector<linked_group_definition> linked_groups;
181 
182  /** Helper struct to store information about the tips. */
184  {
185  tooltip_info(const config& cfg, const std::string& tagname);
186 
187  std::string id;
188  };
189 
192 
193  builder_grid_ptr grid;
194  };
195 
196  /**
197  * Resolution options for this window instance.
198  *
199  * The window widget handles resolution options differently from other widgets.
200  * Most specify their resolution options in their definitions. However, windows
201  * define different resolution options for each window *instance*. That enables
202  * each dialog to have its own set of options.
203  */
204  std::vector<window_resolution> resolutions;
205 
206 private:
207  void read(const config& cfg);
208 
209  std::string id_;
210  std::string description_;
211 };
212 
213 /**
214  * Builds a window.
215  */
217 
218 } // namespace gui2
Contains the info needed to instantiate a widget.
virtual widget * build() const =0
std::shared_ptr< const builder_grid > builder_grid_const_ptr
Base class for all widgets.
Definition: widget.hpp:47
std::shared_ptr< builder_widget > builder_widget_ptr
Generic file dialog.
Definition: field-fwd.hpp:22
std::vector< linked_group_definition > linked_groups
Base container class.
Definition: grid.hpp:30
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.
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:625
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)
window * build(const builder_window::window_resolution *definition)
Builds a window.
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:92
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
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.
builder_widget_ptr create_widget_builder(const config &cfg)
Create a widget builder.