The Battle for Wesnoth  1.17.17+dev
pane.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2023
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  */
179 
180  /** Updates the placement for the child items. */
181  void prepare_placement() const;
182 
183  /***** ***** ***** signal handlers ***** ****** *****/
184 
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
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
Base container class.
Definition: grid.hpp:32
A pane is a container where new members can be added and removed during run-time.
Definition: pane.hpp:44
void place_or_set_origin_children()
Places or moves the children on the pane.
Definition: pane.cpp:275
void signal_handler_request_placement(dispatcher &dispatcher, const event::ui_event event, bool &handled)
Definition: pane.cpp:310
std::function< bool(const item &, const item &)> compare_functor_t
Definition: pane.hpp:58
grid * get_grid(const unsigned id)
Returns a grid in the pane.
Definition: pane.cpp:233
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: pane.cpp:220
std::function< bool(const item &)> filter_functor_t
Definition: pane.hpp:60
std::list< item > items_
The items in the pane.
Definition: pane.hpp:146
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
Definition: pane.cpp:203
pane(const implementation::builder_pane &builder)
Definition: pane.cpp:105
std::unique_ptr< placer_base > placer_
Helper to do the placement.
Definition: pane.hpp:155
virtual iteration::walker_ptr create_walker() override
See widget::create_walker.
Definition: pane.cpp:225
void sort(const compare_functor_t &compare_functor)
Sorts the contents of the pane.
Definition: pane.cpp:180
builder_grid_ptr item_builder_
The builer for the items in the list.
Definition: pane.hpp:149
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: pane.cpp:214
virtual void impl_draw_children() override
See widget::impl_draw_children.
Definition: pane.cpp:168
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: pane.cpp:143
unsigned create_item(const widget_data &item_data, const std::map< std::string, std::string > &tags)
Creates a new item.
Definition: pane.cpp:118
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: pane.cpp:199
void place_children()
Places the children on the pane.
Definition: pane.cpp:243
unsigned item_id_generator_
The id generator for the items.
Definition: pane.hpp:152
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: pane.cpp:154
void filter(const filter_functor_t &filter_functor)
Filters the contents of the pane.
Definition: pane.cpp:187
void set_origin_children()
Moves the children on the pane.
Definition: pane.cpp:259
void prepare_placement() const
Updates the placement for the child items.
Definition: pane.cpp:295
Base class for all widgets.
Definition: widget.hpp:54
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
std::unique_ptr< class walker_base > walker_ptr
Definition: widget.hpp:43
Generic file dialog.
std::shared_ptr< builder_grid > builder_grid_ptr
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:35
Contains the implementation details for lexical_cast and shouldn't be used directly.
bool grid()
Definition: general.cpp:565
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
Base class for the placement helper.
Contains the info needed to instantiate a widget.
std::map< std::string, std::shared_ptr< builder_widget > > replacements_map
The replacements type is used to define replacement types.
builder_pane(const config &cfg)
Definition: pane.cpp:357
grow_direction::type grow_dir
Definition: pane.hpp:203
virtual std::unique_ptr< widget > build() const override
Definition: pane.cpp:366
builder_grid_ptr item_definition
Definition: pane.hpp:207
std::map< std::string, std::string > tags
Definition: pane.hpp:53
item(item &&)=default
unsigned id
Definition: pane.hpp:52
std::unique_ptr< grid > item_grid
Definition: pane.hpp:55
Helper to implement private functions without modifying the header.
Definition: pane.cpp:49
Holds a 2D point.
Definition: point.hpp:25