The Battle for Wesnoth  1.19.0-dev
pane.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2024
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 
37 /**
38  * @ingroup GUIWidgetWML
39  *
40  * A pane is a container where new members can be added and removed during run-time.
41  */
42 class pane : public widget
43 {
44  friend struct pane_implementation;
45 
46 public:
47  struct item
48  {
49  unsigned id;
50  std::map<std::string, std::string> tags;
51 
52  std::unique_ptr<grid> item_grid;
53  };
54 
55  typedef std::function<bool(const item&, const item&)> compare_functor_t;
56 
57  typedef std::function<bool(const item&)> filter_functor_t;
58 
59 public:
60  explicit pane(const implementation::builder_pane& builder);
61 
62  /**
63  * Creates a new item.
64  */
65  unsigned create_item(const widget_data& item_data,
66  const std::map<std::string, std::string>& tags);
67 
68  /** See @ref widget::place. */
69  virtual void place(const point& origin, const point& size) override;
70 
71  /** See @ref widget::layout_initialize. */
72  virtual void layout_initialize(const bool full_initialization) override;
73 
74  /** See @ref widget::impl_draw_children. */
75  virtual void impl_draw_children() override;
76 
77  /** See @ref widget::request_reduce_width. */
78  virtual void request_reduce_width(const unsigned maximum_width) override;
79 
80  /** See @ref widget::find_at. */
81  virtual widget* find_at(const point& coordinate,
82  const bool must_be_active) override;
83 
84  /** See @ref widget::find_at. */
85  virtual const widget* find_at(const point& coordinate,
86  const bool must_be_active) const override;
87 
88  /**
89  * Sorts the contents of the pane.
90  *
91  * @param compare_functor The functor to use to sort the items.
92  */
93  void sort(const compare_functor_t& compare_functor);
94 
95  /**
96  * Filters the contents of the pane.
97  *
98  * if the @p filter_functor returns @c true the item shown, else it's
99  * hidden.
100  *
101  * @param filter_functor The functor to determine whether an item
102  * should be shown or hidden.
103  */
104  void filter(const filter_functor_t& filter_functor);
105 
106 private:
107  /** See @ref widget::calculate_best_size. */
108  virtual point calculate_best_size() const override;
109 
110 public:
111  /** See @ref widget::disable_click_dismiss. */
112  bool disable_click_dismiss() const override;
113 
114  /** See @ref widget::create_walker. */
115  virtual iteration::walker_ptr create_walker() override;
116 
117  /**
118  * Returns a grid in the pane.
119  *
120  * @param id The id of the item whose grid to return. The
121  * id is the value returned by
122  * @ref create_item().
123  *
124  * @returns The wanted grid.
125  * @retval nullptr The id isn't associated with an item.
126  */
127  grid* get_grid(const unsigned id);
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  const grid* get_grid(const unsigned id) const;
140 
141 private:
142  /** The items in the pane. */
143  std::list<item> items_;
144 
145  /** The builer for the items in the list. */
147 
148  /** The id generator for the items. */
150 
151  /** Helper to do the placement. */
152  std::unique_ptr<placer_base> placer_;
153 
154  /** Places the children on the pane. */
155  void place_children();
156 
157  /**
158  * Moves the children on the pane.
159  *
160  * After certain operations, e.g. sorting the child widgets need to be
161  * placed again. This function does so, but avoids dirtying the widget so
162  * redrawing doesn't involve re-rendering the entire widget.
163  */
164  void set_origin_children();
165 
166  /**
167  * Places or moves the children on the pane.
168  *
169  * If the child has its best size it's move else placed.
170  *
171  * @note It would probably be possible to merge all three placement
172  * routines into one and using a flag for what to do: place, set_origin or
173  * place_or_set_origin.
174  */
176 
177  /** Updates the placement for the child items. */
178  void prepare_placement() const;
179 
180  /***** ***** ***** signal handlers ***** ****** *****/
181 
183  const event::ui_event event,
184  bool& handled);
185 };
186 
187 // }---------- BUILDER -----------{
188 
189 namespace implementation
190 {
191 
193 {
194  explicit builder_pane(const config& cfg);
195 
196  virtual std::unique_ptr<widget> build() const override;
197 
198  virtual std::unique_ptr<widget> build(const replacements_map& replacements) const override;
199 
201 
202  unsigned parallel_items;
203 
205 };
206 
207 } // namespace implementation
208 
209 // }------------ END --------------
210 
211 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
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:43
void place_or_set_origin_children()
Places or moves the children on the pane.
Definition: pane.cpp:273
void signal_handler_request_placement(dispatcher &dispatcher, const event::ui_event event, bool &handled)
Definition: pane.cpp:308
std::function< bool(const item &, const item &)> compare_functor_t
Definition: pane.hpp:55
grid * get_grid(const unsigned id)
Returns a grid in the pane.
Definition: pane.cpp:231
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: pane.cpp:218
std::function< bool(const item &)> filter_functor_t
Definition: pane.hpp:57
std::list< item > items_
The items in the pane.
Definition: pane.hpp:143
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
Definition: pane.cpp:201
pane(const implementation::builder_pane &builder)
Definition: pane.cpp:103
std::unique_ptr< placer_base > placer_
Helper to do the placement.
Definition: pane.hpp:152
virtual iteration::walker_ptr create_walker() override
See widget::create_walker.
Definition: pane.cpp:223
void sort(const compare_functor_t &compare_functor)
Sorts the contents of the pane.
Definition: pane.cpp:178
builder_grid_ptr item_builder_
The builer for the items in the list.
Definition: pane.hpp:146
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: pane.cpp:212
virtual void impl_draw_children() override
See widget::impl_draw_children.
Definition: pane.cpp:166
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: pane.cpp:141
unsigned create_item(const widget_data &item_data, const std::map< std::string, std::string > &tags)
Creates a new item.
Definition: pane.cpp:116
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: pane.cpp:197
void place_children()
Places the children on the pane.
Definition: pane.cpp:241
unsigned item_id_generator_
The id generator for the items.
Definition: pane.hpp:149
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: pane.cpp:152
void filter(const filter_functor_t &filter_functor)
Filters the contents of the pane.
Definition: pane.cpp:185
void set_origin_children()
Moves the children on the pane.
Definition: pane.cpp:257
void prepare_placement() const
Updates the placement for the child items.
Definition: pane.cpp:293
Base class for all widgets.
Definition: widget.hpp:53
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
std::unique_ptr< class walker_base > walker_ptr
Definition: widget.hpp:42
Generic file dialog.
std::shared_ptr< builder_grid > builder_grid_ptr
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
Contains the implementation details for lexical_cast and shouldn't be used directly.
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:85
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:355
grow_direction::type grow_dir
Definition: pane.hpp:200
virtual std::unique_ptr< widget > build() const override
Definition: pane.cpp:364
builder_grid_ptr item_definition
Definition: pane.hpp:204
std::map< std::string, std::string > tags
Definition: pane.hpp:50
unsigned id
Definition: pane.hpp:49
std::unique_ptr< grid > item_grid
Definition: pane.hpp:52
Helper to implement private functions without modifying the header.
Definition: pane.cpp:47
Holds a 2D point.
Definition: point.hpp:25