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