The Battle for Wesnoth  1.17.4+dev
pane.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2022
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  item(item&&) = default;
51 
52  unsigned id;
53  std::map<std::string, std::string> tags;
54 
55  std::unique_ptr<grid> item_grid;
56  };
57 
58  typedef std::function<bool(const item&, const item&)> compare_functor_t;
59 
60  typedef std::function<bool(const item&)> filter_functor_t;
61 
62 public:
63  explicit pane(const implementation::builder_pane& builder);
64 
65  /**
66  * Creates a new item.
67  */
68  unsigned create_item(const std::map<std::string, string_map>& item_data,
69  const std::map<std::string, std::string>& tags);
70 
71  /** See @ref widget::place. */
72  virtual void place(const point& origin, const point& size) override;
73 
74  /** See @ref widget::layout_initialize. */
75  virtual void layout_initialize(const bool full_initialization) override;
76 
77  /** See @ref widget::impl_draw_children. */
78  virtual void impl_draw_children(int x_offset, int y_offset) override;
79 
80  /** See @ref widget::child_populate_dirty_list. */
81  virtual void
82  child_populate_dirty_list(window& caller,
83  const std::vector<widget*>& call_stack) override;
84 
85  /** See @ref widget::request_reduce_width. */
86  virtual void request_reduce_width(const unsigned maximum_width) override;
87 
88  /** See @ref widget::find_at. */
89  virtual widget* find_at(const point& coordinate,
90  const bool must_be_active) override;
91 
92  /** See @ref widget::find_at. */
93  virtual const widget* find_at(const point& coordinate,
94  const bool must_be_active) const override;
95 
96  /**
97  * Sorts the contents of the pane.
98  *
99  * @param compare_functor The functor to use to sort the items.
100  */
101  void sort(const compare_functor_t& compare_functor);
102 
103  /**
104  * Filters the contents of the pane.
105  *
106  * if the @p filter_functor returns @c true the item shown, else it's
107  * hidden.
108  *
109  * @param filter_functor The functor to determine whether an item
110  * should be shown or hidden.
111  */
112  void filter(const filter_functor_t& filter_functor);
113 
114 private:
115  /** See @ref widget::calculate_best_size. */
116  virtual point calculate_best_size() const override;
117 
118 public:
119  /** See @ref widget::disable_click_dismiss. */
120  bool disable_click_dismiss() const override;
121 
122  /** See @ref widget::create_walker. */
123  virtual iteration::walker_ptr create_walker() override;
124 
125  /**
126  * Returns a grid in the pane.
127  *
128  * @param id The id of the item whose grid to return. The
129  * id is the value returned by
130  * @ref create_item().
131  *
132  * @returns The wanted grid.
133  * @retval nullptr The id isn't associated with an item.
134  */
135  grid* get_grid(const unsigned id);
136 
137  /**
138  * Returns a grid in the pane.
139  *
140  * @param id The id of the item whose grid to return. The
141  * id is the value returned by
142  * @ref create_item().
143  *
144  * @returns The wanted grid.
145  * @retval nullptr The id isn't associated with an item.
146  */
147  const grid* get_grid(const unsigned id) const;
148 
149 private:
150  /** The items in the pane. */
151  std::list<item> items_;
152 
153  /** The builer for the items in the list. */
155 
156  /** The id generator for the items. */
158 
159  /** Helper to do the placement. */
160  std::unique_ptr<placer_base> placer_;
161 
162  /** Places the children on the pane. */
163  void place_children();
164 
165  /**
166  * Moves the children on the pane.
167  *
168  * After certain operations, e.g. sorting the child widgets need to be
169  * placed again. This function does so, but avoids dirtying the widget so
170  * redrawing doesn't involve re-rendering the entire widget.
171  */
172  void set_origin_children();
173 
174  /**
175  * Places or moves the children on the pane.
176  *
177  * If the child has its best size it's move else placed.
178  *
179  * @note It would probably be possible to merge all three placement
180  * routines into one and using a flag for what to do: place, set_origin or
181  * place_or_set_origin.
182  */
183  void place_or_set_origin_children();
184 
185  /** Updates the placement for the child items. */
186  void prepare_placement() const;
187 
188  /***** ***** ***** signal handlers ***** ****** *****/
189 
190  void signal_handler_request_placement(dispatcher& dispatcher,
191  const event::ui_event event,
192  bool& handled);
193 };
194 
195 // }---------- BUILDER -----------{
196 
197 namespace implementation
198 {
199 
201 {
202  explicit builder_pane(const config& cfg);
203 
204  virtual std::unique_ptr<widget> build() const override;
205 
206  virtual std::unique_ptr<widget> build(const replacements_map& replacements) const override;
207 
209 
210  unsigned parallel_items;
211 
213 };
214 
215 } // namespace implementation
216 
217 // }------------ END --------------
218 
219 } // namespace gui2
Base class for the placement helper.
Contains the info needed to instantiate a widget.
std::unique_ptr< class walker_base > walker_ptr
Definition: widget.hpp:39
std::unique_ptr< grid > item_grid
Definition: pane.hpp:55
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
std::map< std::string, std::string > tags
Definition: pane.hpp:53
std::list< item > items_
The items in the pane.
Definition: pane.hpp:151
Base class for all widgets.
Definition: widget.hpp:49
std::function< bool(const item &)> filter_functor_t
Definition: pane.hpp:60
std::map< std::string, std::shared_ptr< builder_widget > > replacements_map
The replacements type is used to define replacement types.
std::unique_ptr< placer_base > placer_
Helper to do the placement.
Definition: pane.hpp:160
builder_grid_ptr item_definition
Definition: pane.hpp:212
Generic file dialog.
Definition: field-fwd.hpp:23
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
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
unsigned id
Definition: pane.hpp:52
grow_direction::type grow_dir
Definition: pane.hpp:208
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:58
std::shared_ptr< builder_grid > builder_grid_ptr
bool grid()
Definition: general.cpp:562
Helper to implement private functions without modifying the header.
Definition: pane.cpp:48
builder_grid_ptr item_builder_
The builer for the items in the list.
Definition: pane.hpp:154
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:157
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:60
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:66
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:410