The Battle for Wesnoth  1.19.0-dev
tree_view.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2024
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
20 
21 namespace gui2
22 {
23 
24 namespace implementation {
25  struct builder_tree_view;
26  struct tree_node
27  {
28  explicit tree_node(const config& cfg);
29 
30  std::string id;
31  bool unfolded;
33  };
34 }
35 
36 // ------------ WIDGET -----------{
37 
38 
39 /**
40  * @ingroup GUIWidgetWML
41  *
42  * A tree view is a control that holds several items of the same or different types.
43  * The items shown are called tree view nodes and when a node has children, these can be shown or hidden.
44  * Nodes that contain children need to provide a clickable button in order to fold or unfold the children.
45  *
46  * The following states exist:
47  * * state_enabled - the listbox is enabled.
48  * * state_disabled - the listbox is disabled.
49  * List with the tree view specific variables:
50  * Key |Type |Default |Description
51  * -------------------------|------------------------------------------------|------------|-------------
52  * vertical_scrollbar_mode | @ref guivartype_scrollbar_mode "scrollbar_mode"|initial_auto|Determines whether or not to show the scrollbar.
53  * horizontal_scrollbar_mode| @ref guivartype_scrollbar_mode "scrollbar_mode"|initial_auto|Determines whether or not to show the scrollbar.
54  * indention_step_size | @ref guivartype_unsigned "unsigned" |0 |The number of pixels every level of nodes is indented from the previous level.
55  * node | @ref guivartype_unsigned "unsigned" |mandatory |The tree view can contain multiple node sections. This part needs more documentation.
56  * id | @ref guivartype_unsigned "unsigned" |"" |.
57  * return_value_id | @ref guivartype_unsigned "unsigned" |"" |.
58  */
60 {
63  friend class tree_view_node;
64 
65 public:
67 
68  explicit tree_view(const implementation::builder_tree_view& builder);
69 
70  ~tree_view();
71 
73 
75  {
76  return *root_node_;
77  }
78 
80  const std::string& id, const widget_data& data, const int index = -1);
81 
82  /**
83  * Removes the given node as a child of its parent node.
84  *
85  * @param node A pointer to the node to remove.
86  *
87  * @returns A pair consisting of a smart pointer managing the removed
88  * node, and its position before removal.
89  */
90  std::pair<std::shared_ptr<tree_view_node>, int> remove_node(tree_view_node* node);
91 
92  void clear();
93 
94  /** See @ref container_base::set_self_active. */
95  virtual void set_self_active(const bool active) override;
96 
97  bool empty() const;
98 
99  /** See @ref widget::layout_children. */
100  virtual void layout_children() override;
101 
102  /***** ***** ***** setters / getters for members ***** ****** *****/
103 
104  void set_indentation_step_size(const unsigned indentation_step_size)
105  {
106  indentation_step_size_ = indentation_step_size;
107  }
108 
110  {
111  return selected_item_;
112  }
113 
115  {
116  return selected_item_;
117  }
118 
119  const std::vector<node_definition>& get_node_definitions() const
120  {
121  return node_definitions_;
122  }
123 
124 protected:
125  /***** ***** ***** ***** keyboard functions ***** ***** ***** *****/
126 
127  /** Inherited from scrollbar_container. */
128  void handle_key_up_arrow(SDL_Keymod modifier, bool& handled) override;
129 
130  /** Inherited from scrollbar_container. */
131  void handle_key_down_arrow(SDL_Keymod modifier, bool& handled) override;
132 
133  /** Inherited from scrollbar_container. */
134  void handle_key_left_arrow(SDL_Keymod modifier, bool& handled) override;
135 
136  /** Inherited from scrollbar_container. */
137  void handle_key_right_arrow(SDL_Keymod modifier, bool& handled) override;
138 
139 private:
140  static inline const std::string root_node_id = "root";
141 
142  /**
143  * @todo evaluate which way the dependency should go.
144  *
145  * We no depend on the implementation, maybe the implementation should
146  * depend on us instead.
147  */
148  const std::vector<node_definition> node_definitions_;
149 
151 
153 
155 
157 
158  /**
159  * Resizes the content.
160  *
161  * The resize either happens due to resizing the content or invalidate the
162  * layout of the window.
163  *
164  * @param width_modification The wanted modification to the width:
165  * * negative values reduce width.
166  * * zero leave width as is.
167  * * positive values increase width.
168  * @param height_modification The wanted modification to the height:
169  * * negative values reduce height.
170  * * zero leave height as is.
171  * * positive values increase height.
172  * @param width_modification_pos
173  * @param height_modification_pos
174  */
175  void resize_content(const int width_modification,
176  const int height_modification,
177  const int width_modification_pos = -1,
178  const int height_modification_pos = -1);
179 
180  /** Layouts the children if needed. */
181  void layout_children(const bool force);
182 
183  /** Inherited from container_base. */
184  virtual void finalize_setup();
185 
186 public:
187  /** Static type getter that does not rely on the widget being constructed. */
188  static const std::string& type();
189 
190  /** Optionally returns the node definition with the given id, or nullopt if not found. */
191  std::optional<decltype(node_definitions_)::const_iterator> get_node_definition(const std::string& id) const
192  {
193  const auto def = std::find_if(
194  node_definitions_.begin(), node_definitions_.end(), [&id](const auto& d) { return d.id == id; });
195  return def != node_definitions_.end() ? std::optional{def} : std::nullopt;
196  }
197 
198 private:
199  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
200  virtual const std::string& get_control_type() const override;
201 
202  /***** ***** ***** signal handlers ***** ****** *****/
203 
205 
206  template<tree_view_node* (tree_view_node::*func) ()>
208 
209  template<tree_view_node* (tree_view_node::*func) ()>
210  bool handle_up_down_arrow();
211 };
212 
213 // }---------- DEFINITION ---------{
214 
216 {
217 
218  explicit tree_view_definition(const config& cfg);
219 
221  {
222  explicit resolution(const config& cfg);
223 
225  };
226 };
227 
228 // }---------- BUILDER -----------{
229 
230 namespace implementation
231 {
232 
234 {
235  explicit builder_tree_view(const config& cfg);
236 
238 
239  virtual std::unique_ptr<widget> build() const override;
240 
243 
245 
246  /**
247  * The types of nodes in the tree view.
248  *
249  * Since we expect the amount of nodes to remain low it's stored in a
250  * vector and not in a map.
251  */
252  std::vector<tree_node> nodes;
253 
254  /*
255  * NOTE this class doesn't have a data section, so it can only be filled
256  * with data by the engine. I think this poses no limit on the usage since
257  * I don't foresee that somebody wants to pre-fill a tree view. If the need
258  * arises the data part can be added.
259  */
260 };
261 
262 } // namespace implementation
263 
264 // }------------ END --------------
265 
266 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
Base class for creating containers with one or two scrollbar(s).
scrollbar_mode
The way to handle the showing or hiding of the scrollbar.
void finalize_setup()
The builder needs to call us so we do our setup.
A tree view is a control that holds several items of the same or different types.
Definition: tree_view.hpp:60
void set_indentation_step_size(const unsigned indentation_step_size)
Definition: tree_view.hpp:104
void resize_content(const int width_modification, const int height_modification, const int width_modification_pos=-1, const int height_modification_pos=-1)
Resizes the content.
Definition: tree_view.cpp:109
unsigned indentation_step_size_
Definition: tree_view.hpp:150
const tree_view_node * selected_item() const
Definition: tree_view.hpp:114
tree_view_node * selected_item_
Definition: tree_view.hpp:156
void handle_key_right_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
Definition: tree_view.cpp:240
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition: tree_view.cpp:94
static const std::string root_node_id
Definition: tree_view.hpp:140
tree_view_node * get_next_node()
Definition: tree_view.cpp:179
tree_view_node & get_root_node()
Definition: tree_view.hpp:74
tree_view_node * root_node_
Definition: tree_view.hpp:154
virtual void finalize_setup()
Inherited from container_base.
Definition: tree_view.cpp:157
bool empty() const
Definition: tree_view.cpp:99
void handle_key_up_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
Definition: tree_view.cpp:210
void handle_key_left_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
Definition: tree_view.cpp:228
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
tree_view_node & add_node(const std::string &id, const widget_data &data, const int index=-1)
Definition: tree_view.cpp:56
void signal_handler_left_button_down(const event::ui_event event)
Definition: tree_view.cpp:171
const std::vector< node_definition > node_definitions_
Definition: tree_view.hpp:148
implementation::tree_node node_definition
Definition: tree_view.hpp:66
tree_view_node * selected_item()
Definition: tree_view.hpp:109
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
void handle_key_down_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
Definition: tree_view.cpp:219
std::optional< decltype(node_definitions_)::const_iterator > get_node_definition(const std::string &id) const
Optionally returns the node definition with the given id, or nullopt if not found.
Definition: tree_view.hpp:191
virtual void layout_children() override
See widget::layout_children.
Definition: tree_view.cpp:104
bool handle_up_down_arrow()
Definition: tree_view.cpp:195
std::pair< std::shared_ptr< tree_view_node >, int > remove_node(tree_view_node *node)
Removes the given node as a child of its parent node.
Definition: tree_view.cpp:62
const std::vector< node_definition > & get_node_definitions() const
Definition: tree_view.hpp:119
const std::string & id() const
Definition: widget.cpp:110
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
Generic file dialog.
std::shared_ptr< builder_grid > builder_grid_ptr
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
Contains the implementation details for lexical_cast and shouldn't be used directly.
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:70
std::string_view data
Definition: picture.cpp:194
virtual std::unique_ptr< widget > build() const=0
std::vector< tree_node > nodes
The types of nodes in the tree view.
Definition: tree_view.hpp:252
scrollbar_container::scrollbar_mode vertical_scrollbar_mode
Definition: tree_view.hpp:241
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
Definition: tree_view.hpp:242
virtual std::unique_ptr< widget > build() const override
Definition: tree_view.cpp:293
tree_node(const config &cfg)
Definition: tree_view.cpp:317
Base class of a resolution, contains the common keys for a resolution.
tree_view_definition(const config &cfg)
Definition: tree_view.cpp:254
#define d