The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
pane.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2012 - 2025
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
20#include "gui/core/placer.hpp"
21
22#include <functional>
23
24#include <list>
25
26namespace gui2
27{
28
29// ------------ WIDGET -----------{
30
31namespace implementation
32{
33struct 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 */
40class pane : public widget
41{
42 friend struct pane_implementation;
43
44public:
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
57public:
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
104private:
105 /** See @ref widget::calculate_best_size. */
106 virtual point calculate_best_size() const override;
107
108public:
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
139private:
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
187namespace 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
198 grow_direction::type grow_dir;
199
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:157
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:272
void signal_handler_request_placement(dispatcher &dispatcher, const event::ui_event event, bool &handled)
Definition pane.cpp:307
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:230
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition pane.cpp:217
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:200
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:222
void sort(const compare_functor_t &compare_functor)
Sorts the contents of the pane.
Definition pane.cpp:179
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:211
virtual void impl_draw_children() override
See widget::impl_draw_children.
Definition pane.cpp:167
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition pane.cpp:142
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:196
void place_children()
Places the children on the pane.
Definition pane.cpp:240
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:153
void filter(const filter_functor_t &filter_functor)
Filters the contents of the pane.
Definition pane.cpp:186
void set_origin_children()
Moves the children on the pane.
Definition pane.cpp:256
void prepare_placement() const
Updates the placement for the child items.
Definition pane.cpp:292
Base class for all widgets.
Definition widget.hpp:55
const config * cfg
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.
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.
grow_direction::type grow_dir
Definition pane.hpp:198
virtual std::unique_ptr< widget > build() const override
Definition pane.cpp:363
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