The Battle for Wesnoth  1.13.11+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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&)> tcompare_functor;
52 
53  typedef std::function<bool(const item&)> tfilter_functor;
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(surface& frame_buffer,
78  int x_offset,
79  int y_offset) override;
80 
81  /** See @ref widget::child_populate_dirty_list. */
82  virtual void
84  const std::vector<widget*>& call_stack) override;
85 
86  /** See @ref widget::request_reduce_width. */
87  virtual void request_reduce_width(const unsigned maximum_width) override;
88 
89  /** See @ref widget::find_at. */
90  virtual widget* find_at(const point& coordinate,
91  const bool must_be_active) override;
92 
93  /** See @ref widget::find_at. */
94  virtual const widget* find_at(const point& coordinate,
95  const bool must_be_active) const override;
96 
97  /**
98  * Sorts the contents of the pane.
99  *
100  * @param compare_functor The functor to use to sort the items.
101  */
102  void sort(const tcompare_functor& compare_functor);
103 
104  /**
105  * Filters the contents of the pane.
106  *
107  * if the @p filter_functor returns @c true the item shown, else it's
108  * hidden.
109  *
110  * @param filter_functor The functor to determine whether an item
111  * should be shown or hidden.
112  */
113  void filter(const tfilter_functor& filter_functor);
114 
115 private:
116  /** See @ref widget::calculate_best_size. */
117  virtual point calculate_best_size() const override;
118 
119 public:
120  /** See @ref widget::disable_click_dismiss. */
121  bool disable_click_dismiss() const override;
122 
123  /** See @ref widget::create_walker. */
124  virtual iteration::walker_base* create_walker() override;
125 
126  /**
127  * Returns a grid in the pane.
128  *
129  * @param id The id of the item whose grid to return. The
130  * id is the value returned by
131  * @ref create_item().
132  *
133  * @returns The wanted grid.
134  * @retval nullptr The id isn't associated with an item.
135  */
136  grid* get_grid(const unsigned id);
137 
138  /**
139  * Returns a grid in the pane.
140  *
141  * @param id The id of the item whose grid to return. The
142  * id is the value returned by
143  * @ref create_item().
144  *
145  * @returns The wanted grid.
146  * @retval nullptr The id isn't associated with an item.
147  */
148  const grid* get_grid(const unsigned id) const;
149 
150 private:
151  /** The items in the pane. */
152  std::list<item> items_;
153 
154  /** The builer for the items in the list. */
156 
157  /** The id generator for the items. */
159 
160  /** Helper to do the placement. */
161  std::unique_ptr<placer_base> placer_;
162 
163  /** Places the children on the pane. */
164  void place_children();
165 
166  /**
167  * Moves the children on the pane.
168  *
169  * After certain operations, e.g. sorting the child widgets need to be
170  * placed again. This function does so, but avoids dirtying the widget so
171  * redrawing doesn't involve re-rendering the entire widget.
172  */
173  void set_origin_children();
174 
175  /**
176  * Places or moves the children on the pane.
177  *
178  * If the child has its best size it's move else placed.
179  *
180  * @note It would probably be possible to merge all three placement
181  * routines into one and using a flag for what to do: place, set_origin or
182  * place_or_set_origin.
183  */
185 
186  /** Updates the placement for the child items. */
187  void prepare_placement() const;
188 
189  /***** ***** ***** signal handlers ***** ****** *****/
190 
192  const event::ui_event event,
193  bool& handled);
194 };
195 
196 // }---------- BUILDER -----------{
197 
198 namespace implementation
199 {
200 
202 {
203  explicit builder_pane(const config& cfg);
204 
205  widget* build() const;
206 
207  widget* build(const replacements_map& replacements) const;
208 
209  placer_base::tgrow_direction grow_direction;
210 
211  unsigned parallel_items;
212 
214 };
215 
216 } // namespace implementation
217 
218 // }------------ END --------------
219 
220 } // namespace gui2
Base class for the placement helper.
Contains the info needed to instantiate a widget.
void sort(const tcompare_functor &compare_functor)
Sorts the contents of the pane.
Definition: pane.cpp:211
grid * get_grid(const unsigned id)
Returns a grid in the pane.
Definition: pane.cpp:264
std::function< bool(const item &)> tfilter_functor
Definition: pane.hpp:53
std::map< std::string, std::string > tags
Definition: pane.hpp:46
std::list< item > items_
The items in the pane.
Definition: pane.hpp:152
unsigned create_item(const std::map< std::string, string_map > &item_data, const std::map< std::string, std::string > &tags)
Creates a new item.
Definition: pane.cpp:138
Base class for all widgets.
Definition: widget.hpp:48
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: pane.cpp:174
std::function< bool(const item &, const item &)> tcompare_functor
Definition: pane.hpp:51
std::unique_ptr< placer_base > placer_
Helper to do the placement.
Definition: pane.hpp:161
void signal_handler_request_placement(dispatcher &dispatcher, const event::ui_event event, bool &handled)
Definition: pane.cpp:341
builder_grid_ptr item_definition
Definition: pane.hpp:213
void set_origin_children()
Moves the children on the pane.
Definition: pane.cpp:290
void place_children()
Places the children on the pane.
Definition: pane.cpp:274
Generic file dialog.
Definition: field-fwd.hpp:22
grid * item_grid
Definition: pane.hpp:48
Base container class.
Definition: grid.hpp:30
std::map< std::string, std::shared_ptr< builder_widget > > replacements_map
The replacements type is used to define replacement types.
virtual void child_populate_dirty_list(window &caller, const std::vector< widget * > &call_stack) override
See widget::child_populate_dirty_list.
Definition: pane.cpp:201
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: pane.cpp:251
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: pane.cpp:245
pane(const builder_grid_ptr item_builder)
Definition: pane.cpp:107
The walker abstract base class.
Definition: walker.hpp:26
builder_pane(const config &cfg)
Definition: pane.cpp:422
virtual iteration::walker_base * create_walker() override
See widget::create_walker.
Definition: pane.cpp:256
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: pane.cpp:230
unsigned id
Definition: pane.hpp:45
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
static pane * build(const implementation::builder_pane &builder)
Definition: pane.cpp:133
Holds a 2D point.
Definition: point.hpp:23
virtual void impl_draw_children(surface &frame_buffer, int x_offset, int y_offset) override
See widget::impl_draw_children.
Definition: pane.cpp:189
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: pane.cpp:163
void filter(const tfilter_functor &filter_functor)
Filters the contents of the pane.
Definition: pane.cpp:218
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:155
unsigned item_id_generator_
The id generator for the items.
Definition: pane.hpp:158
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
placer_base::tgrow_direction grow_direction
Definition: pane.hpp:209
void prepare_placement() const
Updates the placement for the child items.
Definition: pane.cpp:326
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
Definition: pane.cpp:234
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
std::shared_ptr< builder_grid > builder_grid_ptr
static std::deque< std::string > call_stack
Definition: function.cpp:39
void place_or_set_origin_children()
Places or moves the children on the pane.
Definition: pane.cpp:306
Contains the implementation details for lexical_cast and shouldn't be used directly.
ui_event
The event send to the dispatcher.
Definition: handler.hpp:55