The Battle for Wesnoth  1.15.12+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 <functional>
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 /**
38  * @ingroup GUIWidgetWML
39  *
40  * A pane is a container where new members can be added and removed during run-time.
41  */
42 class pane : public widget
43 {
44  friend struct pane_implementation;
45 
46 public:
47  struct item
48  {
49 
50  unsigned id;
51  std::map<std::string, std::string> tags;
52 
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 private:
61  explicit pane(const implementation::builder_pane& builder);
62 
63 public:
64  static pane* build(const implementation::builder_pane& builder);
65 
66  /**
67  * Creates a new item.
68  */
69  unsigned create_item(const std::map<std::string, string_map>& item_data,
70  const std::map<std::string, std::string>& tags);
71 
72  /** See @ref widget::place. */
73  virtual void place(const point& origin, const point& size) override;
74 
75  /** See @ref widget::layout_initialize. */
76  virtual void layout_initialize(const bool full_initialization) override;
77 
78  /** See @ref widget::impl_draw_children. */
79  virtual void impl_draw_children(surface& frame_buffer,
80  int x_offset,
81  int y_offset) override;
82 
83  /** See @ref widget::child_populate_dirty_list. */
84  virtual void
85  child_populate_dirty_list(window& caller,
86  const std::vector<widget*>& call_stack) override;
87 
88  /** See @ref widget::request_reduce_width. */
89  virtual void request_reduce_width(const unsigned maximum_width) override;
90 
91  /** See @ref widget::find_at. */
92  virtual widget* find_at(const point& coordinate,
93  const bool must_be_active) override;
94 
95  /** See @ref widget::find_at. */
96  virtual const widget* find_at(const point& coordinate,
97  const bool must_be_active) const override;
98 
99  /**
100  * Sorts the contents of the pane.
101  *
102  * @param compare_functor The functor to use to sort the items.
103  */
104  void sort(const compare_functor_t& compare_functor);
105 
106  /**
107  * Filters the contents of the pane.
108  *
109  * if the @p filter_functor returns @c true the item shown, else it's
110  * hidden.
111  *
112  * @param filter_functor The functor to determine whether an item
113  * should be shown or hidden.
114  */
115  void filter(const filter_functor_t& filter_functor);
116 
117 private:
118  /** See @ref widget::calculate_best_size. */
119  virtual point calculate_best_size() const override;
120 
121 public:
122  /** See @ref widget::disable_click_dismiss. */
123  bool disable_click_dismiss() const override;
124 
125  /** See @ref widget::create_walker. */
126  virtual iteration::walker_base* create_walker() override;
127 
128  /**
129  * Returns a grid in the pane.
130  *
131  * @param id The id of the item whose grid to return. The
132  * id is the value returned by
133  * @ref create_item().
134  *
135  * @returns The wanted grid.
136  * @retval nullptr The id isn't associated with an item.
137  */
138  grid* get_grid(const unsigned id);
139 
140  /**
141  * Returns a grid in the pane.
142  *
143  * @param id The id of the item whose grid to return. The
144  * id is the value returned by
145  * @ref create_item().
146  *
147  * @returns The wanted grid.
148  * @retval nullptr The id isn't associated with an item.
149  */
150  const grid* get_grid(const unsigned id) const;
151 
152 private:
153  /** The items in the pane. */
154  std::list<item> items_;
155 
156  /** The builer for the items in the list. */
158 
159  /** The id generator for the items. */
161 
162  /** Helper to do the placement. */
163  std::unique_ptr<placer_base> placer_;
164 
165  /** Places the children on the pane. */
166  void place_children();
167 
168  /**
169  * Moves the children on the pane.
170  *
171  * After certain operations, e.g. sorting the child widgets need to be
172  * placed again. This function does so, but avoids dirtying the widget so
173  * redrawing doesn't involve re-rendering the entire widget.
174  */
175  void set_origin_children();
176 
177  /**
178  * Places or moves the children on the pane.
179  *
180  * If the child has its best size it's move else placed.
181  *
182  * @note It would probably be possible to merge all three placement
183  * routines into one and using a flag for what to do: place, set_origin or
184  * place_or_set_origin.
185  */
186  void place_or_set_origin_children();
187 
188  /** Updates the placement for the child items. */
189  void prepare_placement() const;
190 
191  /***** ***** ***** signal handlers ***** ****** *****/
192 
193  void signal_handler_request_placement(dispatcher& dispatcher,
194  const event::ui_event event,
195  bool& handled);
196 };
197 
198 // }---------- BUILDER -----------{
199 
200 namespace implementation
201 {
202 
204 {
205  explicit builder_pane(const config& cfg);
206 
207  virtual widget* build() const override;
208 
209  virtual widget* build(const replacements_map& replacements) const override;
210 
211  placer_base::grow_direction grow_direction;
212 
213  unsigned parallel_items;
214 
216 };
217 
218 } // namespace implementation
219 
220 // }------------ END --------------
221 
222 } // 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:51
placer_base::grow_direction grow_direction
Definition: pane.hpp:211
std::list< item > items_
The items in the pane.
Definition: pane.hpp:154
Base class for all widgets.
Definition: widget.hpp:49
std::function< bool(const item &)> filter_functor_t
Definition: pane.hpp:58
std::unique_ptr< placer_base > placer_
Helper to do the placement.
Definition: pane.hpp:163
builder_grid_ptr item_definition
Definition: pane.hpp:215
Generic file dialog.
Definition: field-fwd.hpp:22
grid * item_grid
Definition: pane.hpp:53
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
static thread_local std::deque< std::string > call_stack
For printing error messages when WFL parsing or evaluation fails, this contains the names of the WFL ...
Definition: function.cpp:46
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:50
A pane is a container where new members can be added and removed during run-time. ...
Definition: pane.hpp:42
Holds a 2D point.
Definition: point.hpp:23
std::function< bool(const item &, const item &)> compare_functor_t
Definition: pane.hpp:56
bool grid()
Definition: general.cpp:519
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:157
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
unsigned item_id_generator_
The id generator for the items.
Definition: pane.hpp:160
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:59
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
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