The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
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
21namespace gui2
22{
23
24namespace implementation {
25 struct builder_tree_view;
26 struct tree_node
27 {
28 explicit tree_node(const config& cfg);
29
30 std::string id;
33 };
34}
35
36// ------------ WIDGET -----------{
37
39{
42 friend class tree_view_node;
43
44public:
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
94 {
96 }
97
98 /**
99 * If true, adding an item when no item is selected will automatically select that item.
100 *
101 * Name chosen for similarity to list_view::has_minimum().
102 */
103 bool has_minimum() const
104 {
105 return has_minimum_;
106 }
107
109 {
110 return selected_item_;
111 }
112
114 {
115 return selected_item_;
116 }
117
118 const std::vector<node_definition>& get_node_definitions() const
119 {
120 return node_definitions_;
121 }
122
123protected:
124 /***** ***** ***** ***** keyboard functions ***** ***** ***** *****/
125
126 /** Inherited from scrollbar_container. */
127 void handle_key_up_arrow(SDL_Keymod modifier, bool& handled) override;
128
129 /** Inherited from scrollbar_container. */
130 void handle_key_down_arrow(SDL_Keymod modifier, bool& handled) override;
131
132 /** Inherited from scrollbar_container. */
133 void handle_key_left_arrow(SDL_Keymod modifier, bool& handled) override;
134
135 /** Inherited from scrollbar_container. */
136 void handle_key_right_arrow(SDL_Keymod modifier, bool& handled) override;
137
138private:
139 static inline const std::string root_node_id = "root";
140
141 /**
142 * @todo evaluate which way the dependency should go.
143 *
144 * We no depend on the implementation, maybe the implementation should
145 * depend on us instead.
146 */
147 const std::vector<node_definition> node_definitions_;
148
150
152
154
156
158
159 /**
160 * Resizes the content.
161 *
162 * The resize either happens due to resizing the content or invalidate the
163 * layout of the window.
164 *
165 * @param width_modification The wanted modification to the width:
166 * * negative values reduce width.
167 * * zero leave width as is.
168 * * positive values increase width.
169 * @param height_modification The wanted modification to the height:
170 * * negative values reduce height.
171 * * zero leave height as is.
172 * * positive values increase height.
173 * @param width_modification_pos
174 * @param height_modification_pos
175 */
176 void resize_content(const int width_modification,
177 const int height_modification,
178 const int width_modification_pos = -1,
179 const int height_modification_pos = -1);
180
181 /** Layouts the children if needed. */
182 void layout_children(const bool force);
183
184 /** Inherited from container_base. */
185 virtual void finalize_setup();
186
187public:
188 /** Static type getter that does not rely on the widget being constructed. */
189 static const std::string& type();
190
191 /** Optionally returns the node definition with the given id, or nullopt if not found. */
192 utils::optional<decltype(node_definitions_)::const_iterator> get_node_definition(const std::string& id) const
193 {
195 return def != node_definitions_.end() ? utils::make_optional(def) : utils::nullopt;
196 }
197
198private:
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) ()>
211};
212
213// }---------- DEFINITION ---------{
214
216{
217
218 explicit tree_view_definition(const config& cfg);
219
221 {
222 explicit resolution(const config& cfg);
223
226 };
227};
228
229// }---------- BUILDER -----------{
230
231namespace implementation
232{
233
235{
236 explicit builder_tree_view(const config& cfg);
237
239
240 virtual std::unique_ptr<widget> build() const override;
241
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:157
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
tree_view_node * selected_item()
const tree_view_node * selected_item() const
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.
unsigned indentation_step_size_
tree_view_node * selected_item_
void handle_key_right_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition tree_view.cpp:94
bool has_minimum() const
If true, adding an item when no item is selected will automatically select that item.
static const std::string root_node_id
tree_view_node * get_next_node()
tree_view_node & get_root_node()
Definition tree_view.hpp:58
tree_view_node * root_node_
virtual void finalize_setup()
Inherited from container_base.
bool empty() const
Definition tree_view.cpp:99
void handle_key_up_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
void handle_key_left_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
tree_view_node & add_node(const std::string &id, const widget_data &data, const int index=-1)
Definition tree_view.cpp:57
const std::vector< node_definition > & get_node_definitions() const
void signal_handler_left_button_down(const event::ui_event event)
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
const std::vector< node_definition > node_definitions_
implementation::tree_node node_definition
Definition tree_view.hpp:45
void handle_key_down_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from scrollbar_container.
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.
virtual void layout_children() override
See widget::layout_children.
bool handle_up_down_arrow()
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:63
const tree_view_node & get_root_node() const
Definition tree_view.hpp:53
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.
auto find(Container &container, const Value &value, const Projection &projection={})
Definition general.hpp:213
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.
virtual std::unique_ptr< widget > build() const override