The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tree_view_node.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 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_TREE_VIEW_NODE_HPP_INCLUDED
16 #define GUI_WIDGETS_TREE_VIEW_NODE_HPP_INCLUDED
17 
18 #include "gui/widgets/widget.hpp"
19 #include "gui/widgets/grid.hpp"
20 
21 #include <boost/ptr_container/ptr_vector.hpp>
22 
23 namespace gui2
24 {
25 
26 namespace implementation {
27  struct ttree_node;
28 }
29 
30 class tselectable_;
31 class ttree_view;
32 
33 class ttree_view_node : public twidget
34 {
36  friend class ttree_view;
37 
38 public:
41  const std::string& id,
42  const std::vector<tnode_definition>& node_definitions,
44  ttree_view& parent_tree_view,
45  const std::map<std::string /* widget id */, string_map>& data);
46 
48 
49  /**
50  * Adds a child item to the list of child nodes.
51  *
52  * @param id The id of the node definition to use for the
53  * new node.
54  * @param data The data to send to the set_members of the
55  * widgets. If the member id is not an empty
56  * string it is only send to the widget that has
57  * the wanted id (if any). If the member id is an
58  * empty string, it is send to all members.
59  * Having both empty and non-empty id's gives
60  * undefined behavior.
61  * @param index The item before which to add the new item,
62  * 0 == begin, -1 == end.
63  */
65  add_child(const std::string& id,
66  const std::map<std::string /* widget id */, string_map>& data,
67  const int index = -1);
68 
69  /**
70  * Adds a sibbling for a node at the end of the list.
71  *
72  * @param id The id of the node definition to use for the
73  * new node.
74  * @param data The data to send to the set_members of the
75  * widgets. If the member id is not an empty
76  * string it is only send to the widget that has
77  * the wanted id (if any). If the member id is an
78  * empty string, it is send to all members.
79  * Having both empty and non-empty id's gives
80  * undefined behavior.
81  */
84  const std::map<std::string /* widget id */, string_map>& data)
85  {
86  assert(!is_root_node());
87  return parent_node().add_child(id, data);
88  }
89 
90  /**
91  * Is this node the root node?
92  *
93  * When the parent tree view is created it adds one special node, the root
94  * node. This node has no parent node and some other special features so
95  * several code paths need to check whether they are the parent node.
96  */
97  bool is_root_node() const
98  {
99  return parent_node_ == nullptr;
100  }
101 
102  /**
103  * The indention level of the node.
104  *
105  * The root node starts at level 0.
106  */
107  unsigned get_indention_level() const;
108 
109  /** Does the node have children? */
110  bool empty() const
111  {
112  return children_.empty();
113  }
114 
115  /** Is the node folded? */
116  bool is_folded() const;
117 
118 #if 0
119  enum texpand_mode
120  {
121  recursive_restore // recursively restores collapse mode
122  , recursive_expand // recursively expands the children
123  , not_recursive
124  };
125 #endif
126 
127  // If recursive all children will be closed recursively causing
128  // restore expaning not to expand anything
129  // TODO: ^ implement
130  void fold(/*const bool recursive*/);
131  void unfold(/*const texpand_mode mode*/);
132 
133  /**
134  * See @ref twidget::create_walker.
135  *
136  * @todo Implement properly.
137  */
138  virtual iterator::twalker_* create_walker() override
139  {
140  return nullptr;
141  }
142 
143  /** See @ref twidget::find_at. */
144  virtual twidget* find_at(const tpoint& coordinate,
145  const bool must_be_active) override;
146 
147  /** See @ref twidget::find_at. */
148  virtual const twidget* find_at(const tpoint& coordinate,
149  const bool must_be_active) const override;
150 
151  /** See @ref twidget::find. */
152  twidget* find(const std::string& id, const bool must_be_active) override;
153 
154  /** See @ref twidget::find. */
155  const twidget* find(const std::string& id,
156  const bool must_be_active) const override;
157 
158  /**
159  * The "size" of the widget.
160  *
161  * @todo Rename this function, names to close to the size of the widget.
162  */
163  size_t size() const
164  {
165  return children_.size();
166  }
167 
168  /**
169  * Removes all child items from the widget.
170  */
171  void clear();
172 
173  /***** ***** ***** setters / getters for members ***** ****** *****/
174 
175  /**
176  * Returns the parent node.
177  *
178  * @pre is_root_node() == false.
179  */
181 
182  /** The const version of @ref parent_node. */
183  const ttree_view_node& parent_node() const;
184 
186 
187  const ttree_view& tree_view() const;
188 
190  /**
191  calculates the node indicies that we need to get from the root node to this node.
192  */
193  std::vector<int> describe_path();
194 
195  /** Inherited from tselectable_.
196  *
197  * @param scope Specifies the scope of the callback event
198  * 0 : on both fold and unfold
199  * 1 : on unfolded to folded
200  * 2 : on folded to unfolded
201  */
203  const int scope, std::function<void(twidget&)> callback)
204  {
205  switch (scope) {
206  case 0:
207  callback_state_change_ = callback;
208  break;
209  case 1:
210  callback_state_to_folded_ = callback;
211  break;
212  case 2:
213  callback_state_to_unfolded_ = callback;
214  break;
215  }
216  }
222  void select_node();
223  tgrid& get_grid() { return grid_; }
224  void layout_initialise(const bool full_initialisation);
225 private:
226 
227  int calculate_ypos();
228  /** See @ref twidget::request_reduce_width. */
229  virtual void request_reduce_width(const unsigned maximum_width) override;
230 
231  /**
232  * Our parent node.
233  *
234  * All nodes except the root node have a parent node.
235  */
237 
238  /** The tree view that owns us. */
240 
241  /** Grid holding our contents. */
243 
244  /**
245  * Our children.
246  *
247  * We want the returned child nodes to remain stable so store pointers.
248  */
249  boost::ptr_vector<ttree_view_node> children_;
250 
251  /**
252  * The node definitions known to use.
253  *
254  * This list is needed to create new nodes.
255  *
256  * @todo Maybe store this list in the tree_view to avoid copying the
257  * reference.
258  */
259  const std::vector<tnode_definition>& node_definitions_;
260 
261  /** The toggle for the folded state. */
263 
264  /** The label to show our selected state. */
266 
267  bool unfolded_;
268  void fold_internal();
269  void unfold_internal();
270 
271  /**
272  * "Inherited" from twidget.
273  *
274  * This version needs to call its children, which are it's child nodes.
275  */
276  void impl_populate_dirty_list(twindow& caller,
277  const std::vector<twidget*>& call_stack);
278 
279  /** See @ref twidget::calculate_best_size. */
280  virtual tpoint calculate_best_size() const override;
281 
282  /** See @ref twidget::disable_click_dismiss. */
283  bool disable_click_dismiss() const override;
284 
285  tpoint calculate_best_size(const int indention_level,
286  const unsigned indention_step_size) const;
287  /** @param assume_visible: if false (default) it will return 0 if the parent node is folded*/
288  tpoint get_current_size(bool assume_visible = false) const;
289  tpoint get_folded_size() const;
290  tpoint get_unfolded_size() const;
291 
292  /** See @ref twidget::set_origin. */
293  virtual void set_origin(const tpoint& origin) override;
294 
295  /** See @ref twidget::place. */
296  virtual void place(const tpoint& origin, const tpoint& size) override;
297 
298  unsigned
299  place(const unsigned indention_step_size, tpoint origin, unsigned width);
300 
301  /** See @ref twidget::set_visible_rectangle. */
302  virtual void set_visible_rectangle(const SDL_Rect& rectangle) override;
303 
304  /** See @ref twidget::impl_draw_children. */
305  virtual void impl_draw_children(surface& frame_buffer,
306  int x_offset,
307  int y_offset) override;
308 
309  /** See tselectable_::set_callback_state_change. */
310  std::function<void(twidget&)> callback_state_change_;
311 
312  /** See tselectable_::set_callback_state_change. */
313  std::function<void(twidget&)> callback_state_to_folded_;
314 
315  /** See tselectable_::set_callback_state_change. */
316  std::function<void(twidget&)> callback_state_to_unfolded_;
317 
318  // FIXME rename to icon
320 
322  bool& handled,
323  bool& halt);
324 
325  void
327  const std::map<std::string /* widget id */, string_map>& data);
328 
329  /**
330  * Returns the control_type of the @ref ttree_view_node.
331  *
332  * This class does not derive from @ref tcontrol but the function behaves
333  * similar as @ref tcontrol::get_control_type.
334  */
335  const std::string& get_control_type() const;
336 };
337 
338 } // namespace gui2
339 
340 #endif
std::vector< int > describe_path()
calculates the node indicies that we need to get from the root node to this node. ...
tgrid grid_
Grid holding our contents.
ttree_view_node * get_selectable_node_above()
ttree_view_node & add_sibling(const std::string &id, const std::map< std::string, string_map > &data)
Adds a sibbling for a node at the end of the list.
void signal_handler_label_left_button_click(const event::tevent event, bool &handled, bool &halt)
size_t size() const
The "size" of the widget.
ttree_view_node & parent_node()
Returns the parent node.
tselectable_ * toggle_
The toggle for the folded state.
const std::vector< tnode_definition > & node_definitions_
The node definitions known to use.
bool empty() const
Does the node have children?
scope
Available hotkey scopes.
bool is_folded() const
Is the node folded?
virtual void request_reduce_width(const unsigned maximum_width) override
See twidget::request_reduce_width.
Base container class.
Definition: grid.hpp:29
void set_callback_state_change(const int scope, std::function< void(twidget &)> callback)
Inherited from tselectable_.
virtual iterator::twalker_ * create_walker() override
See twidget::create_walker.
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
tpoint get_current_size(bool assume_visible=false) const
ttree_view_node & add_child(const std::string &id, const std::map< std::string, string_map > &data, const int index=-1)
Adds a child item to the list of child nodes.
tpoint get_unfolded_size() const
ttree_view_node * get_selectable_node_below()
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
virtual tpoint calculate_best_size() const override
See twidget::calculate_best_size.
ttree_view & tree_view()
virtual void set_visible_rectangle(const SDL_Rect &rectangle) override
See twidget::set_visible_rectangle.
boost::ptr_vector< ttree_view_node > children_
Our children.
void signal_handler_left_button_click(const event::tevent event)
void impl_populate_dirty_list(twindow &caller, const std::vector< twidget * > &call_stack)
"Inherited" from twidget.
std::function< void(twidget &)> callback_state_to_unfolded_
See tselectable_::set_callback_state_change.
tevent
The event send to the dispatcher.
Definition: handler.hpp:54
implementation::ttree_node tnode_definition
std::function< void(twidget &)> callback_state_change_
See tselectable_::set_callback_state_change.
ttree_view_node * get_node_above()
Small abstract helper class.
Definition: selectable.hpp:32
void init_grid(tgrid *grid, const std::map< std::string, string_map > &data)
ttree_view_node * get_node_below()
tselectable_ * label_
The label to show our selected state.
unsigned get_indention_level() const
The indention level of the node.
GLuint index
Definition: glew.h:1782
Holds a 2D point.
Definition: point.hpp:24
virtual twidget * find_at(const tpoint &coordinate, const bool must_be_active) override
See twidget::find_at.
bool is_root_node() const
Is this node the root node?
virtual void place(const tpoint &origin, const tpoint &size) override
See twidget::place.
twidget * find(const std::string &id, const bool must_be_active) override
See twidget::find.
GLsizeiptr size
Definition: glew.h:1649
ttree_view & tree_view_
The tree view that owns us.
tpoint get_folded_size() const
cl_event event
Definition: glew.h:3070
bool disable_click_dismiss() const override
See twidget::disable_click_dismiss.
Base class for all widgets.
Definition: widget.hpp:49
const std::string & get_control_type() const
Returns the control_type of the ttree_view_node.
virtual void set_origin(const tpoint &origin) override
See twidget::set_origin.
std::function< void(twidget &)> callback_state_to_folded_
See tselectable_::set_callback_state_change.
ttree_view_node * parent_node_
Our parent node.
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1220
The walker abstract base class.
Definition: walker.hpp:27
ttree_view_node & get_child_at(int index)
ttree_view_node * get_last_visible_parent_node()
ttree_view_node(const std::string &id, const std::vector< tnode_definition > &node_definitions, ttree_view_node *parent_node, ttree_view &parent_tree_view, const std::map< std::string, string_map > &data)
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Contains the implementation details for lexical_cast and shouldn't be used directly.
virtual void impl_draw_children(surface &frame_buffer, int x_offset, int y_offset) override
See twidget::impl_draw_children.
void clear()
Removes all child items from the widget.
void layout_initialise(const bool full_initialisation)
How the layout engine works.