The Battle for Wesnoth  1.13.4+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 - 2016 by Mark de Wever <[email protected]>
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 #ifndef GUI_WIDGETS_PANE_HPP_INCLUDED
16 #define GUI_WIDGETS_PANE_HPP_INCLUDED
17 
18 #include "gui/widgets/widget.hpp"
20 #include "gui/core/placer.hpp"
21 
22 #include "utils/functional.hpp"
23 
24 #include <list>
25 
26 typedef std::map<std::string, t_string> string_map;
27 
28 namespace gui2
29 {
30 
31 // ------------ WIDGET -----------{
32 
33 namespace implementation
34 {
35 struct tbuilder_pane;
36 } // namespace implementation
37 
38 class tgrid;
39 
40 class tpane : public twidget
41 {
42  friend struct tpane_implementation;
43 
44 public:
45  struct titem
46  {
47 
48  unsigned id;
49  std::map<std::string, std::string> tags;
50 
52  };
53 
54  typedef std::function<bool(const titem&, const titem&)> tcompare_functor;
55 
56  typedef std::function<bool(const titem&)> tfilter_functor;
57 
58  /** @deprecated Use the second overload. */
59  explicit tpane(const tbuilder_grid_ptr item_builder);
60 
61 private:
62  explicit tpane(const implementation::tbuilder_pane& builder);
63 
64 public:
65  static tpane* build(const implementation::tbuilder_pane& builder);
66 
67  /**
68  * Creates a new item.
69  */
70  unsigned create_item(const std::map<std::string, string_map>& item_data,
71  const std::map<std::string, std::string>& tags);
72 
73  /** See @ref twidget::place. */
74  virtual void place(const tpoint& origin, const tpoint& size) override;
75 
76  /** See @ref twidget::layout_initialise. */
77  virtual void layout_initialise(const bool full_initialisation) override;
78 
79  /** See @ref twidget::impl_draw_children. */
80  virtual void impl_draw_children(surface& frame_buffer,
81  int x_offset,
82  int y_offset) override;
83 
84  /** See @ref twidget::child_populate_dirty_list. */
85  virtual void
87  const std::vector<twidget*>& call_stack) override;
88 
89  /** See @ref twidget::request_reduce_width. */
90  virtual void request_reduce_width(const unsigned maximum_width) override;
91 
92  /** See @ref twidget::find_at. */
93  virtual twidget* find_at(const tpoint& coordinate,
94  const bool must_be_active) override;
95 
96  /** See @ref twidget::find_at. */
97  virtual const twidget* find_at(const tpoint& coordinate,
98  const bool must_be_active) const override;
99 
100  /**
101  * Sorts the contents of the pane.
102  *
103  * @param compare_functor The functor to use to sort the items.
104  */
105  void sort(const tcompare_functor& compare_functor);
106 
107  /**
108  * Filters the contents of the pane.
109  *
110  * if the @p filter_functor returns @c true the item shown, else it's
111  * hidden.
112  *
113  * @param filter_functor The functor to determine whether an item
114  * should be shown or hidden.
115  */
116  void filter(const tfilter_functor& filter_functor);
117 
118 private:
119  /** See @ref twidget::calculate_best_size. */
120  virtual tpoint calculate_best_size() const override;
121 
122 public:
123  /** See @ref twidget::disable_click_dismiss. */
124  bool disable_click_dismiss() const override;
125 
126  /** See @ref twidget::create_walker. */
127  virtual iterator::twalker_* create_walker() override;
128 
129  /**
130  * Returns a grid in the pane.
131  *
132  * @param id The id of the item whose grid to return. The
133  * id is the value returned by
134  * @ref create_item().
135  *
136  * @returns The wanted grid.
137  * @retval nullptr The id isn't associated with an item.
138  */
139  tgrid* grid(const unsigned id);
140 
141  /**
142  * Returns a grid in the pane.
143  *
144  * @param id The id of the item whose grid to return. The
145  * id is the value returned by
146  * @ref create_item().
147  *
148  * @returns The wanted grid.
149  * @retval nullptr The id isn't associated with an item.
150  */
151  const tgrid* grid(const unsigned id) const;
152 
153 private:
154  /** The items in the pane. */
155  std::list<titem> items_;
156 
157  /** The builer for the items in the list. */
159 
160  /** The id generator for the items. */
162 
163  /** Helper to do the placement. */
164  std::unique_ptr<tplacer_> placer_;
165 
166  /** Places the children on the pane. */
167  void place_children();
168 
169  /**
170  * Moves the children on the pane.
171  *
172  * After certain operations, e.g. sorting the child widgets need to be
173  * placed again. This function does so, but avoids dirtying the widget so
174  * redrawing doesn't involve re-rendering the entire widget.
175  */
176  void set_origin_children();
177 
178  /**
179  * Places or moves the children on the pane.
180  *
181  * If the child has its best size it's move else placed.
182  *
183  * @note It would probably be possible to merge all three placement
184  * routines into one and using a flag for what to do: place, set_origin or
185  * place_or_set_origin.
186  */
188 
189  /** Updates the placement for the child items. */
190  void prepare_placement() const;
191 
192  /***** ***** ***** signal handlers ***** ****** *****/
193 
195  const event::tevent event,
196  bool& handled);
197 };
198 
199 // }---------- BUILDER -----------{
200 
201 namespace implementation
202 {
203 
205 {
206  explicit tbuilder_pane(const config& cfg);
207 
208  twidget* build() const;
209 
210  twidget* build(const treplacements& replacements) const;
211 
212  tplacer_::tgrow_direction grow_direction;
213 
214  unsigned parallel_items;
215 
217 };
218 
219 } // namespace implementation
220 
221 // }------------ END --------------
222 
223 } // namespace gui2
224 
225 #endif
Base class for the placement helper.
std::map< std::string, t_string > string_map
Definition: pane.hpp:26
void filter(const tfilter_functor &filter_functor)
Filters the contents of the pane.
Definition: pane.cpp:222
static tpane * build(const implementation::tbuilder_pane &builder)
Definition: pane.cpp:137
virtual void place(const tpoint &origin, const tpoint &size) override
See twidget::place.
Definition: pane.cpp:167
virtual void impl_draw_children(surface &frame_buffer, int x_offset, int y_offset) override
See twidget::impl_draw_children.
Definition: pane.cpp:193
Helper to implement private functions without modifying the header.
Definition: pane.cpp:44
Contains the info needed to instantiate a widget.
void prepare_placement() const
Updates the placement for the child items.
Definition: pane.cpp:330
Base container class.
Definition: grid.hpp:29
std::function< bool(const titem &, const titem &)> tcompare_functor
Definition: pane.hpp:54
virtual iterator::twalker_ * create_walker() override
See twidget::create_walker.
Definition: pane.cpp:260
virtual tpoint calculate_best_size() const override
See twidget::calculate_best_size.
Definition: pane.cpp:249
void signal_handler_request_placement(tdispatcher &dispatcher, const event::tevent event, bool &handled)
Definition: pane.cpp:345
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:142
tgrid * grid(const unsigned id)
Returns a grid in the pane.
Definition: pane.cpp:268
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
tpane(const tbuilder_grid_ptr item_builder)
Definition: pane.cpp:111
tbuilder_grid_ptr item_builder_
The builer for the items in the list.
Definition: pane.hpp:158
virtual twidget * find_at(const tpoint &coordinate, const bool must_be_active) override
See twidget::find_at.
Definition: pane.cpp:238
std::list< titem > items_
The items in the pane.
Definition: pane.hpp:155
virtual void child_populate_dirty_list(twindow &caller, const std::vector< twidget * > &call_stack) override
See twidget::child_populate_dirty_list.
Definition: pane.cpp:205
tevent
The event send to the dispatcher.
Definition: handler.hpp:54
std::function< bool(const titem &)> tfilter_functor
Definition: pane.hpp:56
void set_origin_children()
Moves the children on the pane.
Definition: pane.cpp:294
unsigned id
Definition: pane.hpp:48
Holds a 2D point.
Definition: point.hpp:24
tgrid * grid
Definition: pane.hpp:51
twidget * build() const
Definition: pane.cpp:436
tbuilder_pane(const config &cfg)
Definition: pane.cpp:426
GLsizeiptr size
Definition: glew.h:1649
std::map< std::string, std::string > tags
Definition: pane.hpp:49
cl_event event
Definition: glew.h:3070
Base class for all widgets.
Definition: widget.hpp:49
std::unique_ptr< tplacer_ > placer_
Helper to do the placement.
Definition: pane.hpp:164
void place_or_set_origin_children()
Places or moves the children on the pane.
Definition: pane.cpp:310
The walker abstract base class.
Definition: walker.hpp:27
std::map< std::string, boost::intrusive_ptr< tbuilder_widget > > treplacements
The replacements type is used to define replacement types.
virtual void request_reduce_width(const unsigned maximum_width) override
See twidget::request_reduce_width.
Definition: pane.cpp:234
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
void place_children()
Places the children on the pane.
Definition: pane.cpp:278
bool disable_click_dismiss() const override
See twidget::disable_click_dismiss.
Definition: pane.cpp:255
Contains the implementation details for lexical_cast and shouldn't be used directly.
void sort(const tcompare_functor &compare_functor)
Sorts the contents of the pane.
Definition: pane.cpp:215
virtual void layout_initialise(const bool full_initialisation) override
See twidget::layout_initialise.
Definition: pane.cpp:178
tplacer_::tgrow_direction grow_direction
Definition: pane.hpp:212
unsigned item_id_generator_
The id generator for the items.
Definition: pane.hpp:161
tbuilder_grid_ptr item_definition
Definition: pane.hpp:216