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