The Battle for Wesnoth  1.19.5+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 
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  {
182  const auto def = std::find_if(
183  node_definitions_.begin(), node_definitions_.end(), [&id](const auto& d) { return d.id == id; });
184  return def != node_definitions_.end() ? utils::make_optional(def) : utils::nullopt;
185  }
186 
187 private:
188  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
189  virtual const std::string& get_control_type() const override;
190 
191  /***** ***** ***** signal handlers ***** ****** *****/
192 
194 
195  template<tree_view_node* (tree_view_node::*func) ()>
197 
198  template<tree_view_node* (tree_view_node::*func) ()>
199  bool handle_up_down_arrow();
200 };
201 
202 // }---------- DEFINITION ---------{
203 
205 {
206 
207  explicit tree_view_definition(const config& cfg);
208 
210  {
211  explicit resolution(const config& cfg);
212 
214  };
215 };
216 
217 // }---------- BUILDER -----------{
218 
219 namespace implementation
220 {
221 
223 {
224  explicit builder_tree_view(const config& cfg);
225 
227 
228  virtual std::unique_ptr<widget> build() const override;
229 
232 
234 
235  /**
236  * The types of nodes in the tree view.
237  *
238  * Since we expect the amount of nodes to remain low it's stored in a
239  * vector and not in a map.
240  */
241  std::vector<tree_node> nodes;
242 
243  /*
244  * NOTE this class doesn't have a data section, so it can only be filled
245  * with data by the engine. I think this poses no limit on the usage since
246  * I don't foresee that somebody wants to pre-fill a tree view. If the need
247  * arises the data part can be added.
248  */
249 };
250 
251 } // namespace implementation
252 
253 // }------------ END --------------
254 
255 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
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.
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:109
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: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:129
tree_view_node * get_next_node()
Definition: tree_view.cpp:179
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: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
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:171
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:219
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:108
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:36
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:178
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:241
scrollbar_container::scrollbar_mode vertical_scrollbar_mode
Definition: tree_view.hpp:230
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
Definition: tree_view.hpp:231
virtual std::unique_ptr< widget > build() const override
Definition: tree_view.cpp:293
tree_node(const config &cfg)
Definition: tree_view.cpp:317
tree_view_definition(const config &cfg)
Definition: tree_view.cpp:254
#define d