The Battle for Wesnoth  1.17.12+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 widget_data& 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() override;
79 
80  /** See @ref widget::request_reduce_width. */
81  virtual void request_reduce_width(const unsigned maximum_width) override;
82 
83  /** See @ref widget::find_at. */
84  virtual widget* find_at(const point& coordinate,
85  const bool must_be_active) override;
86 
87  /** See @ref widget::find_at. */
88  virtual const widget* find_at(const point& coordinate,
89  const bool must_be_active) const override;
90 
91  /**
92  * Sorts the contents of the pane.
93  *
94  * @param compare_functor The functor to use to sort the items.
95  */
96  void sort(const compare_functor_t& compare_functor);
97 
98  /**
99  * Filters the contents of the pane.
100  *
101  * if the @p filter_functor returns @c true the item shown, else it's
102  * hidden.
103  *
104  * @param filter_functor The functor to determine whether an item
105  * should be shown or hidden.
106  */
107  void filter(const filter_functor_t& filter_functor);
108 
109 private:
110  /** See @ref widget::calculate_best_size. */
111  virtual point calculate_best_size() const override;
112 
113 public:
114  /** See @ref widget::disable_click_dismiss. */
115  bool disable_click_dismiss() const override;
116 
117  /** See @ref widget::create_walker. */
118  virtual iteration::walker_ptr create_walker() override;
119 
120  /**
121  * Returns a grid in the pane.
122  *
123  * @param id The id of the item whose grid to return. The
124  * id is the value returned by
125  * @ref create_item().
126  *
127  * @returns The wanted grid.
128  * @retval nullptr The id isn't associated with an item.
129  */
130  grid* get_grid(const unsigned id);
131 
132  /**
133  * Returns a grid in the pane.
134  *
135  * @param id The id of the item whose grid to return. The
136  * id is the value returned by
137  * @ref create_item().
138  *
139  * @returns The wanted grid.
140  * @retval nullptr The id isn't associated with an item.
141  */
142  const grid* get_grid(const unsigned id) const;
143 
144 private:
145  /** The items in the pane. */
146  std::list<item> items_;
147 
148  /** The builer for the items in the list. */
150 
151  /** The id generator for the items. */
153 
154  /** Helper to do the placement. */
155  std::unique_ptr<placer_base> placer_;
156 
157  /** Places the children on the pane. */
158  void place_children();
159 
160  /**
161  * Moves the children on the pane.
162  *
163  * After certain operations, e.g. sorting the child widgets need to be
164  * placed again. This function does so, but avoids dirtying the widget so
165  * redrawing doesn't involve re-rendering the entire widget.
166  */
167  void set_origin_children();
168 
169  /**
170  * Places or moves the children on the pane.
171  *
172  * If the child has its best size it's move else placed.
173  *
174  * @note It would probably be possible to merge all three placement
175  * routines into one and using a flag for what to do: place, set_origin or
176  * place_or_set_origin.
177  */
178  void place_or_set_origin_children();
179 
180  /** Updates the placement for the child items. */
181  void prepare_placement() const;
182 
183  /***** ***** ***** signal handlers ***** ****** *****/
184 
185  void signal_handler_request_placement(dispatcher& dispatcher,
186  const event::ui_event event,
187  bool& handled);
188 };
189 
190 // }---------- BUILDER -----------{
191 
192 namespace implementation
193 {
194 
196 {
197  explicit builder_pane(const config& cfg);
198 
199  virtual std::unique_ptr<widget> build() const override;
200 
201  virtual std::unique_ptr<widget> build(const replacements_map& replacements) const override;
202 
204 
205  unsigned parallel_items;
206 
208 };
209 
210 } // namespace implementation
211 
212 // }------------ END --------------
213 
214 } // 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:43
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:146
Base class for all widgets.
Definition: widget.hpp:53
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:155
builder_grid_ptr item_definition
Definition: pane.hpp:207
Generic file dialog.
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
unsigned id
Definition: pane.hpp:52
grow_direction::type grow_dir
Definition: pane.hpp:203
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:565
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:149
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:35
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:152
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
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:414