The Battle for Wesnoth  1.19.14+dev
tree_view.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2025
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 
39 {
42  friend class tree_view_node;
43 
44 public:
46 
47  explicit tree_view(const implementation::builder_tree_view& builder);
48 
49  ~tree_view();
50 
52 
54  {
55  return *root_node_;
56  }
57 
59  {
60  return *root_node_;
61  }
62 
64  const std::string& id, const widget_data& data, const int index = -1);
65 
66  /**
67  * Removes the given node as a child of its parent node.
68  *
69  * @param node A pointer to the node to remove.
70  *
71  * @returns A pair consisting of a smart pointer managing the removed
72  * node, and its position before removal.
73  */
74  std::pair<std::shared_ptr<tree_view_node>, int> remove_node(tree_view_node* node);
75 
76  void clear();
77 
78  /** See @ref container_base::set_self_active. */
79  virtual void set_self_active(const bool active) override;
80 
81  bool empty() const;
82 
83  /** See @ref widget::layout_children. */
84  virtual void layout_children() override;
85 
86  /***** ***** ***** setters / getters for members ***** ****** *****/
87 
88  void set_indentation_step_size(const unsigned indentation_step_size)
89  {
90  indentation_step_size_ = indentation_step_size;
91  }
92 
93  unsigned get_indentation_step_size() const
94  {
96  }
97 
99  {
100  return selected_item_;
101  }
102 
104  {
105  return selected_item_;
106  }
107 
108  const std::vector<node_definition>& get_node_definitions() const
109  {
110  return node_definitions_;
111  }
112 
113 protected:
114  /***** ***** ***** ***** keyboard functions ***** ***** ***** *****/
115 
116  /** Inherited from scrollbar_container. */
117  void handle_key_up_arrow(SDL_Keymod modifier, bool& handled) override;
118 
119  /** Inherited from scrollbar_container. */
120  void handle_key_down_arrow(SDL_Keymod modifier, bool& handled) override;
121 
122  /** Inherited from scrollbar_container. */
123  void handle_key_left_arrow(SDL_Keymod modifier, bool& handled) override;
124 
125  /** Inherited from scrollbar_container. */
126  void handle_key_right_arrow(SDL_Keymod modifier, bool& handled) override;
127 
128 private:
129  static inline const std::string root_node_id = "root";
130 
131  /**
132  * @todo evaluate which way the dependency should go.
133  *
134  * We no depend on the implementation, maybe the implementation should
135  * depend on us instead.
136  */
137  const std::vector<node_definition> node_definitions_;
138 
140 
142 
144 
146 
147  /**
148  * Resizes the content.
149  *
150  * The resize either happens due to resizing the content or invalidate the
151  * layout of the window.
152  *
153  * @param width_modification The wanted modification to the width:
154  * * negative values reduce width.
155  * * zero leave width as is.
156  * * positive values increase width.
157  * @param height_modification The wanted modification to the height:
158  * * negative values reduce height.
159  * * zero leave height as is.
160  * * positive values increase height.
161  * @param width_modification_pos
162  * @param height_modification_pos
163  */
164  void resize_content(const int width_modification,
165  const int height_modification,
166  const int width_modification_pos = -1,
167  const int height_modification_pos = -1);
168 
169  /** Layouts the children if needed. */
170  void layout_children(const bool force);
171 
172  /** Inherited from container_base. */
173  virtual void finalize_setup();
174 
175 public:
176  /** Static type getter that does not rely on the widget being constructed. */
177  static const std::string& type();
178 
179  /** Optionally returns the node definition with the given id, or nullopt if not found. */
180  utils::optional<decltype(node_definitions_)::const_iterator> get_node_definition(const std::string& id) const
181  {
183  return def != node_definitions_.end() ? utils::make_optional(def) : utils::nullopt;
184  }
185 
186 private:
187  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
188  virtual const std::string& get_control_type() const override;
189 
190  /***** ***** ***** signal handlers ***** ****** *****/
191 
193 
194  template<tree_view_node* (tree_view_node::*func) ()>
196 
197  template<tree_view_node* (tree_view_node::*func) ()>
198  bool handle_up_down_arrow();
199 };
200 
201 // }---------- DEFINITION ---------{
202 
204 {
205 
206  explicit tree_view_definition(const config& cfg);
207 
209  {
210  explicit resolution(const config& cfg);
211 
213  };
214 };
215 
216 // }---------- BUILDER -----------{
217 
218 namespace implementation
219 {
220 
222 {
223  explicit builder_tree_view(const config& cfg);
224 
226 
227  virtual std::unique_ptr<widget> build() const override;
228 
230 
231  /**
232  * The types of nodes in the tree view.
233  *
234  * Since we expect the amount of nodes to remain low it's stored in a
235  * vector and not in a map.
236  */
237  std::vector<tree_node> nodes;
238 
239  /*
240  * NOTE this class doesn't have a data section, so it can only be filled
241  * with data by the engine. I think this poses no limit on the usage since
242  * I don't foresee that somebody wants to pre-fill a tree view. If the need
243  * arises the data part can be added.
244  */
245 };
246 
247 } // namespace implementation
248 
249 // }------------ END --------------
250 
251 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
Base class for creating containers with one or two scrollbar(s).
void finalize_setup()
The builder needs to call us so we do our setup.
void set_indentation_step_size(const unsigned indentation_step_size)
Definition: tree_view.hpp:88
unsigned get_indentation_step_size() const
Definition: tree_view.hpp:93
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:108
const tree_view_node & get_root_node() const
Definition: tree_view.hpp:53
unsigned indentation_step_size_
Definition: tree_view.hpp:139
const tree_view_node * selected_item() const
Definition: tree_view.hpp:103
tree_view_node * selected_item_
Definition: tree_view.hpp:145
void handle_key_right_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
Definition: tree_view.cpp:239
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition: tree_view.cpp:93
static const std::string root_node_id
Definition: tree_view.hpp:129
tree_view_node * get_next_node()
Definition: tree_view.cpp:178
tree_view_node & get_root_node()
Definition: tree_view.hpp:58
tree_view_node * root_node_
Definition: tree_view.hpp:143
virtual void finalize_setup()
Inherited from container_base.
Definition: tree_view.cpp:156
bool empty() const
Definition: tree_view.cpp:98
void handle_key_up_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
Definition: tree_view.cpp:209
void handle_key_left_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
Definition: tree_view.cpp:227
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
utils::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:180
void signal_handler_left_button_down(const event::ui_event event)
Definition: tree_view.cpp:170
const std::vector< node_definition > node_definitions_
Definition: tree_view.hpp:137
implementation::tree_node node_definition
Definition: tree_view.hpp:45
tree_view_node * selected_item()
Definition: tree_view.hpp:98
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:218
virtual void layout_children() override
See widget::layout_children.
Definition: tree_view.cpp:103
bool handle_up_down_arrow()
Definition: tree_view.cpp:194
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:108
const std::string & id() const
Definition: widget.cpp:110
const config * cfg
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:36
Contains the implementation details for lexical_cast and shouldn't be used directly.
std::size_t index(std::string_view str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:70
auto find(Container &container, const Value &value, const Projection &projection={})
Definition: general.hpp:179
std::string_view data
Definition: picture.cpp:188
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:237
virtual std::unique_ptr< widget > build() const override
Definition: tree_view.cpp:290
tree_node(const config &cfg)
Definition: tree_view.cpp:309
tree_view_definition(const config &cfg)
Definition: tree_view.cpp:253