The Battle for Wesnoth  1.15.1+dev
tree_view.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 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 
19 
20 namespace gui2
21 {
22 
23 namespace implementation {
24  struct builder_tree_view;
25  struct tree_node
26  {
27  explicit tree_node(const config& cfg);
28 
29  std::string id;
30  bool unfolded;
32  };
33 }
34 
35 // ------------ WIDGET -----------{
36 
37 class tree_view_node;
38 
40 {
42  friend class tree_view_node;
43 
44 public:
46 
48 
49  ~tree_view();
50 
52 
54  {
55  return *root_node_;
56  }
57 
59  add_node(const std::string& id,
60  const std::map<std::string /* widget id */, string_map>& data,
61  const int index = -1);
62 
63  /**
64  * Removes the given node as a child of its parent node.
65  *
66  * @param node A pointer to the node to remove.
67  *
68  * @returns A pair consisting of a smart pointer managing the removed
69  * node, and its position before removal.
70  */
71  std::pair<tree_view_node::ptr_t, int> remove_node(tree_view_node* node);
72 
73  void clear();
74 
75  /** See @ref widget::child_populate_dirty_list. */
76  virtual void
77  child_populate_dirty_list(window& caller,
78  const std::vector<widget*>& call_stack) override;
79 
80  /** See @ref container_base::set_self_active. */
81  virtual void set_self_active(const bool active) override;
82 
83  bool empty() const;
84 
85  /** See @ref widget::layout_children. */
86  virtual void layout_children() override;
87 
88  /***** ***** ***** setters / getters for members ***** ****** *****/
89 
90  void set_indentation_step_size(const unsigned indentation_step_size)
91  {
92  indentation_step_size_ = indentation_step_size;
93  }
94 
96  {
97  return selected_item_;
98  }
99 
101  {
102  return selected_item_;
103  }
104 
105  void set_selection_change_callback(std::function<void(widget&)> callback)
106  {
107  selection_change_callback_ = callback;
108  }
109 
110  const std::vector<node_definition>& get_node_definitions() const
111  {
112  return node_definitions_;
113  }
114 
115 protected:
116  /***** ***** ***** ***** keyboard functions ***** ***** ***** *****/
117 
118  /** Inherited from scrollbar_container. */
119  void handle_key_up_arrow(SDL_Keymod modifier, bool& handled) override;
120 
121  /** Inherited from scrollbar_container. */
122  void handle_key_down_arrow(SDL_Keymod modifier, bool& handled) override;
123 
124  /** Inherited from scrollbar_container. */
125  void handle_key_left_arrow(SDL_Keymod modifier, bool& handled) override;
126 
127  /** Inherited from scrollbar_container. */
128  void handle_key_right_arrow(SDL_Keymod modifier, bool& handled) override;
129 private:
130  /**
131  * @todo evaluate which way the dependency should go.
132  *
133  * We no depend on the implementation, maybe the implementation should
134  * depend on us instead.
135  */
136  const std::vector<node_definition> node_definitions_;
137 
139 
141 
143 
145 
146  std::function<void(widget&)> selection_change_callback_;
147 
148  /**
149  * Resizes the content.
150  *
151  * The resize either happens due to resizing the content or invalidate the
152  * layout of the window.
153  *
154  * @param width_modification The wanted modification to the width:
155  * * negative values reduce width.
156  * * zero leave width as is.
157  * * positive values increase width.
158  * @param height_modification The wanted modification to the height:
159  * * negative values reduce height.
160  * * zero leave height as is.
161  * * positive values increase height.
162  */
163  void resize_content(const int width_modification,
164  const int height_modification,
165  const int width__modification_pos = -1,
166  const int height_modification_pos = -1);
167 
168  /** Layouts the children if needed. */
169  void layout_children(const bool force);
170 
171  /** Inherited from container_base. */
172  virtual void finalize_setup();
173 
174 public:
175  /** Static type getter that does not rely on the widget being constructed. */
176  static const std::string& type();
177 
178 private:
179  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
180  virtual const std::string& get_control_type() const override;
181 
182  /***** ***** ***** signal handlers ***** ****** *****/
183 
184  void signal_handler_left_button_down(const event::ui_event event);
185 
186  template<tree_view_node* (tree_view_node::*func) ()>
187  tree_view_node* get_next_node();
188 
189  template<tree_view_node* (tree_view_node::*func) ()>
190  bool handle_up_down_arrow();
191 };
192 
193 // }---------- DEFINITION ---------{
194 
196 {
197 
198  explicit tree_view_definition(const config& cfg);
199 
201  {
202  explicit resolution(const config& cfg);
203 
205  };
206 };
207 
208 // }---------- BUILDER -----------{
209 
210 namespace implementation
211 {
212 
214 {
215  explicit builder_tree_view(const config& cfg);
216 
218 
219  widget* build() const;
220 
223 
225 
226  /**
227  * The types of nodes in the tree view.
228  *
229  * Since we expect the amount of nodes to remain low it's stored in a
230  * vector and not in a map.
231  */
232  std::vector<tree_node> nodes;
233 
234  /*
235  * NOTE this class doesn't have a data section, so it can only be filled
236  * with data by the engine. I think this poses no limit on the usage since
237  * I don't foresee that somebody wants to pre-fill a tree view. If the need
238  * arises the data part can be added.
239  */
240 };
241 
242 } // namespace implementation
243 
244 // }------------ END --------------
245 
246 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
virtual widget * build() const =0
void set_selection_change_callback(std::function< void(widget &)> callback)
Definition: tree_view.hpp:105
void finalize_setup()
The builder needs to call us so we do our setup.
unsigned indentation_step_size_
Definition: tree_view.hpp:138
tree_view_node & get_root_node()
Definition: tree_view.hpp:53
Base class for all widgets.
Definition: widget.hpp:47
std::function< void(widget &)> selection_change_callback_
Definition: tree_view.hpp:146
void clear(const std::string &key)
Definition: general.cpp:205
tree_view_node * selected_item()
Definition: tree_view.hpp:95
scrollbar_container::scrollbar_mode vertical_scrollbar_mode
Definition: tree_view.hpp:221
const tree_view_node * selected_item() const
Definition: tree_view.hpp:100
Generic file dialog.
Definition: field-fwd.hpp:22
tree_view_node * selected_item_
Definition: tree_view.hpp:144
tree_view_node * root_node_
Definition: tree_view.hpp:142
std::vector< tree_node > nodes
The types of nodes in the tree view.
Definition: tree_view.hpp:232
void set_indentation_step_size(const unsigned indentation_step_size)
Definition: tree_view.hpp:90
Base class for creating containers with one or two scrollbar(s).
implementation::tree_node node_definition
Definition: tree_view.hpp:45
window * build(const builder_window::window_resolution *definition)
Builds a window.
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
scrollbar_mode
The way to handle the showing or hiding of the scrollbar.
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
Definition: tree_view.hpp:222
point resolution()
Definition: general.cpp:373
const std::vector< node_definition > node_definitions_
Definition: tree_view.hpp:136
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
std::shared_ptr< builder_grid > builder_grid_ptr
const std::vector< node_definition > & get_node_definitions() const
Definition: tree_view.hpp:110
static std::deque< std::string > call_stack
Definition: function.cpp:39
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:55
tree_node(const config &cfg)
Definition: tree_view.cpp:419