The Battle for Wesnoth  1.15.0-dev
pane.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "gui/widgets/widget.hpp"
19 #include "gui/core/placer.hpp"
20 
21 #include "utils/functional.hpp"
22 
23 #include <list>
24 
25 namespace gui2
26 {
27 
28 // ------------ WIDGET -----------{
29 
30 namespace implementation
31 {
32 struct builder_pane;
33 } // namespace implementation
34 
35 class grid;
36 
37 class pane : public widget
38 {
39  friend struct pane_implementation;
40 
41 public:
42  struct item
43  {
44  unsigned id;
45  std::map<std::string, std::string> tags;
46 
48  };
49 
50  typedef std::function<bool(const item&, const item&)> compare_functor_t;
51 
52  typedef std::function<bool(const item&)> filter_functor_t;
53 
54 public:
55  explicit pane(const implementation::builder_pane& builder);
56 
57  /*
58  * Creates a new item.
59  */
60  unsigned create_item(const widget_data& item_data,
61  const std::map<std::string, std::string>& tags);
62 
63  /** See @ref widget::place. */
64  virtual void place(const point& origin, const point& size) override;
65 
66  /** See @ref widget::layout_initialize. */
67  virtual void layout_initialize(const bool full_initialization) override;
68 
69  /** See @ref widget::impl_draw_children. */
70  virtual void impl_draw_children() override;
71 
72  /** See @ref widget::request_reduce_width. */
73  virtual void request_reduce_width(const unsigned maximum_width) override;
74 
75  /** See @ref widget::find_at. */
76  virtual widget* find_at(const point& coordinate,
77  const bool must_be_active) override;
78 
79  /** See @ref widget::find_at. */
80  virtual const widget* find_at(const point& coordinate,
81  const bool must_be_active) const override;
82 
83  /**
84  * Sorts the contents of the pane.
85  *
86  * @param compare_functor The functor to use to sort the items.
87  */
88  void sort(const compare_functor_t& compare_functor);
89 
90  /**
91  * Filters the contents of the pane.
92  *
93  * if the @p filter_functor returns @c true the item shown, else it's
94  * hidden.
95  *
96  * @param filter_functor The functor to determine whether an item
97  * should be shown or hidden.
98  */
99  void filter(const filter_functor_t& filter_functor);
100 
101 private:
102  /** See @ref widget::calculate_best_size. */
103  virtual point calculate_best_size() const override;
104 
105 public:
106  /** See @ref widget::disable_click_dismiss. */
107  bool disable_click_dismiss() const override;
108 
109  /** See @ref widget::create_walker. */
110  virtual iteration::walker_ptr create_walker() override;
111 
112  /**
113  * Returns a grid in the pane.
114  *
115  * @param id The id of the item whose grid to return. The
116  * id is the value returned by
117  * @ref create_item().
118  *
119  * @returns The wanted grid.
120  * @retval nullptr The id isn't associated with an item.
121  */
122  grid* get_grid(const unsigned id);
123 
124  /**
125  * Returns a grid in the pane.
126  *
127  * @param id The id of the item whose grid to return. The
128  * id is the value returned by
129  * @ref create_item().
130  *
131  * @returns The wanted grid.
132  * @retval nullptr The id isn't associated with an item.
133  */
134  const grid* get_grid(const unsigned id) const;
135 
136 private:
137  /** The items in the pane. */
138  std::list<item> items_;
139 
140  /** The builer for the items in the list. */
142 
143  /** The id generator for the items. */
145 
146  /** Helper to do the placement. */
147  std::unique_ptr<placer_base> placer_;
148 
149  /** Places the children on the pane. */
150  void place_children();
151 
152  /**
153  * Moves the children on the pane.
154  *
155  * After certain operations, e.g. sorting the child widgets need to be
156  * placed again. This function does so, but avoids dirtying the widget so
157  * redrawing doesn't involve re-rendering the entire widget.
158  */
159  void set_origin_children();
160 
161  /**
162  * Places or moves the children on the pane.
163  *
164  * If the child has its best size it's move else placed.
165  *
166  * @note It would probably be possible to merge all three placement
167  * routines into one and using a flag for what to do: place, set_origin or
168  * place_or_set_origin.
169  */
170  void place_or_set_origin_children();
171 
172  /** Updates the placement for the child items. */
173  void prepare_placement() const;
174 
175  /***** ***** ***** signal handlers ***** ****** *****/
176 
177  void signal_handler_request_placement(dispatcher& dispatcher,
178  const event::ui_event event,
179  bool& handled);
180 };
181 
182 // }---------- BUILDER -----------{
183 
184 namespace implementation
185 {
186 
188 {
189  explicit builder_pane(const config& cfg);
190 
191  virtual widget_ptr build() const override;
192 
193  virtual widget_ptr build(const replacements_map& replacements) const override;
194 
195  placer_base::grow_direction grow_direction;
196 
197  unsigned parallel_items;
198 
200 };
201 
202 } // namespace implementation
203 
204 // }------------ END --------------
205 
206 } // namespace gui2
Base class for the placement helper.
Contains the info needed to instantiate a widget.
grid_ptr item_grid
Definition: pane.hpp:47
std::unique_ptr< class walker_base > walker_ptr
Definition: widget.hpp:38
std::map< std::string, std::string > tags
Definition: pane.hpp:45
placer_base::grow_direction grow_direction
Definition: pane.hpp:195
std::list< item > items_
The items in the pane.
Definition: pane.hpp:138
Base class for all widgets.
Definition: widget.hpp:48
std::shared_ptr< grid > grid_ptr
Definition: grid.hpp:557
std::function< bool(const item &)> filter_functor_t
Definition: pane.hpp:52
std::map< std::string, std::shared_ptr< builder_widget > > replacements_map
The replacements type is used to define replacement types.
std::unique_ptr< placer_base > placer_
Helper to do the placement.
Definition: pane.hpp:147
builder_grid_ptr item_definition
Definition: pane.hpp:199
Generic file dialog.
Definition: field-fwd.hpp:22
Base container class.
Definition: grid.hpp:30
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
std::shared_ptr< widget > widget_ptr
Definition: widget.hpp:732
bool sort(const pane::item &lhs, const pane::item &rhs, const std::string &tag, const bool ascending)
Definition: filter.hpp:30
unsigned id
Definition: pane.hpp:44
Holds a 2D point.
Definition: point.hpp:23
std::function< bool(const item &, const item &)> compare_functor_t
Definition: pane.hpp:50
std::shared_ptr< builder_grid > builder_grid_ptr
bool grid()
Definition: general.cpp:507
Helper to implement private functions without modifying the header.
Definition: pane.cpp:47
builder_grid_ptr item_builder_
The builer for the items in the list.
Definition: pane.hpp:141
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:30
unsigned item_id_generator_
The id generator for the items.
Definition: pane.hpp:144
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
ui_event
The event send to the dispatcher.
Definition: handler.hpp:47