The Battle for Wesnoth  1.15.2+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  const std::vector<node_definition>& get_node_definitions() const
106  {
107  return node_definitions_;
108  }
109 
110 protected:
111  /***** ***** ***** ***** keyboard functions ***** ***** ***** *****/
112 
113  /** Inherited from scrollbar_container. */
114  void handle_key_up_arrow(SDL_Keymod modifier, bool& handled) override;
115 
116  /** Inherited from scrollbar_container. */
117  void handle_key_down_arrow(SDL_Keymod modifier, bool& handled) override;
118 
119  /** Inherited from scrollbar_container. */
120  void handle_key_left_arrow(SDL_Keymod modifier, bool& handled) override;
121 
122  /** Inherited from scrollbar_container. */
123  void handle_key_right_arrow(SDL_Keymod modifier, bool& handled) override;
124 private:
125  /**
126  * @todo evaluate which way the dependency should go.
127  *
128  * We no depend on the implementation, maybe the implementation should
129  * depend on us instead.
130  */
131  const std::vector<node_definition> node_definitions_;
132 
134 
136 
138 
140 
141  /**
142  * Resizes the content.
143  *
144  * The resize either happens due to resizing the content or invalidate the
145  * layout of the window.
146  *
147  * @param width_modification The wanted modification to the width:
148  * * negative values reduce width.
149  * * zero leave width as is.
150  * * positive values increase width.
151  * @param height_modification The wanted modification to the height:
152  * * negative values reduce height.
153  * * zero leave height as is.
154  * * positive values increase height.
155  */
156  void resize_content(const int width_modification,
157  const int height_modification,
158  const int width__modification_pos = -1,
159  const int height_modification_pos = -1);
160 
161  /** Layouts the children if needed. */
162  void layout_children(const bool force);
163 
164  /** Inherited from container_base. */
165  virtual void finalize_setup();
166 
167 public:
168  /** Static type getter that does not rely on the widget being constructed. */
169  static const std::string& type();
170 
171 private:
172  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
173  virtual const std::string& get_control_type() const override;
174 
175  /***** ***** ***** signal handlers ***** ****** *****/
176 
177  void signal_handler_left_button_down(const event::ui_event event);
178 
179  template<tree_view_node* (tree_view_node::*func) ()>
180  tree_view_node* get_next_node();
181 
182  template<tree_view_node* (tree_view_node::*func) ()>
183  bool handle_up_down_arrow();
184 };
185 
186 // }---------- DEFINITION ---------{
187 
189 {
190 
191  explicit tree_view_definition(const config& cfg);
192 
194  {
195  explicit resolution(const config& cfg);
196 
198  };
199 };
200 
201 // }---------- BUILDER -----------{
202 
203 namespace implementation
204 {
205 
207 {
208  explicit builder_tree_view(const config& cfg);
209 
211 
212  widget* build() const;
213 
216 
218 
219  /**
220  * The types of nodes in the tree view.
221  *
222  * Since we expect the amount of nodes to remain low it's stored in a
223  * vector and not in a map.
224  */
225  std::vector<tree_node> nodes;
226 
227  /*
228  * NOTE this class doesn't have a data section, so it can only be filled
229  * with data by the engine. I think this poses no limit on the usage since
230  * I don't foresee that somebody wants to pre-fill a tree view. If the need
231  * arises the data part can be added.
232  */
233 };
234 
235 } // namespace implementation
236 
237 // }------------ END --------------
238 
239 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
virtual widget * build() const =0
void finalize_setup()
The builder needs to call us so we do our setup.
unsigned indentation_step_size_
Definition: tree_view.hpp:133
tree_view_node & get_root_node()
Definition: tree_view.hpp:53
Base class for all widgets.
Definition: widget.hpp:47
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:214
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:139
tree_view_node * root_node_
Definition: tree_view.hpp:137
std::vector< tree_node > nodes
The types of nodes in the tree view.
Definition: tree_view.hpp:225
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:215
point resolution()
Definition: general.cpp:373
const std::vector< node_definition > node_definitions_
Definition: tree_view.hpp:131
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
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
const std::vector< node_definition > & get_node_definitions() const
Definition: tree_view.hpp:105
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:404