The Battle for Wesnoth  1.17.0-dev
pane.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 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 
18 #include "gui/widgets/widget.hpp"
20 #include "gui/core/placer.hpp"
21 
22 #include <functional>
23 
24 #include <list>
25 
26 namespace gui2
27 {
28 
29 // ------------ WIDGET -----------{
30 
31 namespace implementation
32 {
33 struct builder_pane;
34 } // namespace implementation
35 
36 class grid;
37 
38 /**
39  * @ingroup GUIWidgetWML
40  *
41  * A pane is a container where new members can be added and removed during run-time.
42  */
43 class pane : public widget
44 {
45  friend struct pane_implementation;
46 
47 public:
48  struct item
49  {
50 
51  unsigned id;
52  std::map<std::string, std::string> tags;
53 
55  };
56 
57  typedef std::function<bool(const item&, const item&)> compare_functor_t;
58 
59  typedef std::function<bool(const item&)> filter_functor_t;
60 
61 private:
62  explicit pane(const implementation::builder_pane& builder);
63 
64 public:
65  static pane* build(const implementation::builder_pane& builder);
66 
67  /**
68  * Creates a new item.
69  */
70  unsigned create_item(const std::map<std::string, string_map>& item_data,
71  const std::map<std::string, std::string>& tags);
72 
73  /** See @ref widget::place. */
74  virtual void place(const point& origin, const point& size) override;
75 
76  /** See @ref widget::layout_initialize. */
77  virtual void layout_initialize(const bool full_initialization) override;
78 
79  /** See @ref widget::impl_draw_children. */
80  virtual void impl_draw_children(surface& frame_buffer,
81  int x_offset,
82  int y_offset) override;
83 
84  /** See @ref widget::child_populate_dirty_list. */
85  virtual void
86  child_populate_dirty_list(window& caller,
87  const std::vector<widget*>& call_stack) override;
88 
89  /** See @ref widget::request_reduce_width. */
90  virtual void request_reduce_width(const unsigned maximum_width) override;
91 
92  /** See @ref widget::find_at. */
93  virtual widget* find_at(const point& coordinate,
94  const bool must_be_active) override;
95 
96  /** See @ref widget::find_at. */
97  virtual const widget* find_at(const point& coordinate,
98  const bool must_be_active) const override;
99 
100  /**
101  * Sorts the contents of the pane.
102  *
103  * @param compare_functor The functor to use to sort the items.
104  */
105  void sort(const compare_functor_t& compare_functor);
106 
107  /**
108  * Filters the contents of the pane.
109  *
110  * if the @p filter_functor returns @c true the item shown, else it's
111  * hidden.
112  *
113  * @param filter_functor The functor to determine whether an item
114  * should be shown or hidden.
115  */
116  void filter(const filter_functor_t& filter_functor);
117 
118 private:
119  /** See @ref widget::calculate_best_size. */
120  virtual point calculate_best_size() const override;
121 
122 public:
123  /** See @ref widget::disable_click_dismiss. */
124  bool disable_click_dismiss() const override;
125 
126  /** See @ref widget::create_walker. */
127  virtual iteration::walker_base* create_walker() override;
128 
129  /**
130  * Returns a grid in the pane.
131  *
132  * @param id The id of the item whose grid to return. The
133  * id is the value returned by
134  * @ref create_item().
135  *
136  * @returns The wanted grid.
137  * @retval nullptr The id isn't associated with an item.
138  */
139  grid* get_grid(const unsigned id);
140 
141  /**
142  * Returns a grid in the pane.
143  *
144  * @param id The id of the item whose grid to return. The
145  * id is the value returned by
146  * @ref create_item().
147  *
148  * @returns The wanted grid.
149  * @retval nullptr The id isn't associated with an item.
150  */
151  const grid* get_grid(const unsigned id) const;
152 
153 private:
154  /** The items in the pane. */
155  std::list<item> items_;
156 
157  /** The builer for the items in the list. */
159 
160  /** The id generator for the items. */
162 
163  /** Helper to do the placement. */
164  std::unique_ptr<placer_base> placer_;
165 
166  /** Places the children on the pane. */
167  void place_children();
168 
169  /**
170  * Moves the children on the pane.
171  *
172  * After certain operations, e.g. sorting the child widgets need to be
173  * placed again. This function does so, but avoids dirtying the widget so
174  * redrawing doesn't involve re-rendering the entire widget.
175  */
176  void set_origin_children();
177 
178  /**
179  * Places or moves the children on the pane.
180  *
181  * If the child has its best size it's move else placed.
182  *
183  * @note It would probably be possible to merge all three placement
184  * routines into one and using a flag for what to do: place, set_origin or
185  * place_or_set_origin.
186  */
187  void place_or_set_origin_children();
188 
189  /** Updates the placement for the child items. */
190  void prepare_placement() const;
191 
192  /***** ***** ***** signal handlers ***** ****** *****/
193 
194  void signal_handler_request_placement(dispatcher& dispatcher,
195  const event::ui_event event,
196  bool& handled);
197 };
198 
199 // }---------- BUILDER -----------{
200 
201 namespace implementation
202 {
203 
205 {
206  explicit builder_pane(const config& cfg);
207 
208  virtual widget* build() const override;
209 
210  virtual widget* build(const replacements_map& replacements) const override;
211 
212  placer_base::grow_direction grow_direction;
213 
214  unsigned parallel_items;
215 
217 };
218 
219 } // namespace implementation
220 
221 // }------------ END --------------
222 
223 } // namespace gui2
Base class for the placement helper.
Contains the info needed to instantiate a widget.
std::map< std::string, std::string > tags
Definition: pane.hpp:52
placer_base::grow_direction grow_direction
Definition: pane.hpp:212
std::list< item > items_
The items in the pane.
Definition: pane.hpp:155
Base class for all widgets.
Definition: widget.hpp:49
std::function< bool(const item &)> filter_functor_t
Definition: pane.hpp:59
std::unique_ptr< placer_base > placer_
Helper to do the placement.
Definition: pane.hpp:164
builder_grid_ptr item_definition
Definition: pane.hpp:216
Generic file dialog.
Definition: field-fwd.hpp:23
grid * item_grid
Definition: pane.hpp:54
Base container class.
Definition: grid.hpp:31
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
std::map< std::string, std::shared_ptr< builder_widget > > replacements_map
The replacements type is used to define replacement types.
The walker abstract base class.
Definition: walker.hpp:27
static thread_local std::deque< std::string > call_stack
For printing error messages when WFL parsing or evaluation fails, this contains the names of the WFL ...
Definition: function.cpp:47
bool sort(const pane::item &lhs, const pane::item &rhs, const std::string &tag, const bool ascending)
Definition: filter.hpp:31
unsigned id
Definition: pane.hpp:51
A pane is a container where new members can be added and removed during run-time. ...
Definition: pane.hpp:43
Holds a 2D point.
Definition: point.hpp:24
std::function< bool(const item &, const item &)> compare_functor_t
Definition: pane.hpp:57
bool grid()
Definition: general.cpp:525
Helper to implement private functions without modifying the header.
Definition: pane.cpp:47
builder_grid_ptr item_builder_
The builer for the items in the list.
Definition: pane.hpp:158
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
unsigned item_id_generator_
The id generator for the items.
Definition: pane.hpp:161
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:65
std::shared_ptr< builder_grid > builder_grid_ptr
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
ui_event
The event send to the dispatcher.
Definition: handler.hpp:48