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