The Battle for Wesnoth  1.19.4+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  const std::string& id, const widget_data& data, const int index = -1);
60 
61  /**
62  * Removes the given node as a child of its parent node.
63  *
64  * @param node A pointer to the node to remove.
65  *
66  * @returns A pair consisting of a smart pointer managing the removed
67  * node, and its position before removal.
68  */
69  std::pair<std::shared_ptr<tree_view_node>, int> remove_node(tree_view_node* node);
70 
71  void clear();
72 
73  /** See @ref container_base::set_self_active. */
74  virtual void set_self_active(const bool active) override;
75 
76  bool empty() const;
77 
78  /** See @ref widget::layout_children. */
79  virtual void layout_children() override;
80 
81  /***** ***** ***** setters / getters for members ***** ****** *****/
82 
83  void set_indentation_step_size(const unsigned indentation_step_size)
84  {
85  indentation_step_size_ = indentation_step_size;
86  }
87 
89  {
90  return selected_item_;
91  }
92 
94  {
95  return selected_item_;
96  }
97 
98  const std::vector<node_definition>& get_node_definitions() const
99  {
100  return node_definitions_;
101  }
102 
103 protected:
104  /***** ***** ***** ***** keyboard functions ***** ***** ***** *****/
105 
106  /** Inherited from scrollbar_container. */
107  void handle_key_up_arrow(SDL_Keymod modifier, bool& handled) override;
108 
109  /** Inherited from scrollbar_container. */
110  void handle_key_down_arrow(SDL_Keymod modifier, bool& handled) override;
111 
112  /** Inherited from scrollbar_container. */
113  void handle_key_left_arrow(SDL_Keymod modifier, bool& handled) override;
114 
115  /** Inherited from scrollbar_container. */
116  void handle_key_right_arrow(SDL_Keymod modifier, bool& handled) override;
117 
118 private:
119  static inline const std::string root_node_id = "root";
120 
121  /**
122  * @todo evaluate which way the dependency should go.
123  *
124  * We no depend on the implementation, maybe the implementation should
125  * depend on us instead.
126  */
127  const std::vector<node_definition> node_definitions_;
128 
130 
132 
134 
136 
137  /**
138  * Resizes the content.
139  *
140  * The resize either happens due to resizing the content or invalidate the
141  * layout of the window.
142  *
143  * @param width_modification The wanted modification to the width:
144  * * negative values reduce width.
145  * * zero leave width as is.
146  * * positive values increase width.
147  * @param height_modification The wanted modification to the height:
148  * * negative values reduce height.
149  * * zero leave height as is.
150  * * positive values increase height.
151  * @param width_modification_pos
152  * @param height_modification_pos
153  */
154  void resize_content(const int width_modification,
155  const int height_modification,
156  const int width_modification_pos = -1,
157  const int height_modification_pos = -1);
158 
159  /** Layouts the children if needed. */
160  void layout_children(const bool force);
161 
162  /** Inherited from container_base. */
163  virtual void finalize_setup();
164 
165 public:
166  /** Static type getter that does not rely on the widget being constructed. */
167  static const std::string& type();
168 
169  /** Optionally returns the node definition with the given id, or nullopt if not found. */
170  utils::optional<decltype(node_definitions_)::const_iterator> get_node_definition(const std::string& id) const
171  {
172  const auto def = std::find_if(
173  node_definitions_.begin(), node_definitions_.end(), [&id](const auto& d) { return d.id == id; });
174  return def != node_definitions_.end() ? utils::make_optional(def) : utils::nullopt;
175  }
176 
177 private:
178  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
179  virtual const std::string& get_control_type() const override;
180 
181  /***** ***** ***** signal handlers ***** ****** *****/
182 
184 
185  template<tree_view_node* (tree_view_node::*func) ()>
187 
188  template<tree_view_node* (tree_view_node::*func) ()>
189  bool handle_up_down_arrow();
190 };
191 
192 // }---------- DEFINITION ---------{
193 
195 {
196 
197  explicit tree_view_definition(const config& cfg);
198 
200  {
201  explicit resolution(const config& cfg);
202 
204  };
205 };
206 
207 // }---------- BUILDER -----------{
208 
209 namespace implementation
210 {
211 
213 {
214  explicit builder_tree_view(const config& cfg);
215 
217 
218  virtual std::unique_ptr<widget> build() const override;
219 
222 
224 
225  /**
226  * The types of nodes in the tree view.
227  *
228  * Since we expect the amount of nodes to remain low it's stored in a
229  * vector and not in a map.
230  */
231  std::vector<tree_node> nodes;
232 
233  /*
234  * NOTE this class doesn't have a data section, so it can only be filled
235  * with data by the engine. I think this poses no limit on the usage since
236  * I don't foresee that somebody wants to pre-fill a tree view. If the need
237  * arises the data part can be added.
238  */
239 };
240 
241 } // namespace implementation
242 
243 // }------------ END --------------
244 
245 } // 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:83
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:129
const tree_view_node * selected_item() const
Definition: tree_view.hpp:93
tree_view_node * selected_item_
Definition: tree_view.hpp:135
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:119
tree_view_node * get_next_node()
Definition: tree_view.cpp:179
tree_view_node & get_root_node()
Definition: tree_view.hpp:53
tree_view_node * root_node_
Definition: tree_view.hpp:133
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:170
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:127
implementation::tree_node node_definition
Definition: tree_view.hpp:45
tree_view_node * selected_item()
Definition: tree_view.hpp:88
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:98
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:231
scrollbar_container::scrollbar_mode vertical_scrollbar_mode
Definition: tree_view.hpp:220
scrollbar_container::scrollbar_mode horizontal_scrollbar_mode
Definition: tree_view.hpp:221
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