tbx  0.7.6
treeview.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2020 Alan Buckley All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 /*
26  * treeview.h
27  *
28  * Created on: 11-Nov-2020
29  * Author: alanb
30  */
31 
32 #ifndef TBX_TREEVIEW_H_
33 #define TBX_TREEVIEW_H_
34 
35 #include "../gadget.h"
36 
37 namespace tbx {
38 namespace ext {
39 
40 class TreeViewNodeSelectedListener;
41 class TreeViewNodeExpandedListener;
42 class TreeViewNodeRenamedListener;
43 class TreeViewNodeDraggedListener;
44 class TreeViewScrollListener;
45 
46 
47 typedef int TreeNodeId;
48 class TreeViewCurrentNode;
49 
63 class TreeView: public tbx::Gadget {
64 public:
65  enum {TOOLBOX_CLASS = 0x4028};
66 
67  TreeView() {}
68 
74  ~TreeView() {}
75 
81  TreeView(const TreeView &other) : Gadget(other) {}
82 
92  TreeView(const Gadget &other) : Gadget(other) {check_toolbox_class(TreeView::TOOLBOX_CLASS);}
93 
101  TreeView(const Component &other) : Gadget(other) {}
102 
107  TreeView &operator=(const TreeView &other) {_handle = other.handle(); _id = other._id; return *this;}
108 
115  TreeView &operator=(const Gadget &other) {_handle = other.handle(); _id = other.id(); check_toolbox_class(TOOLBOX_CLASS); return *this;}
116 
124  TreeView &operator=(const Component &other) {Window check(other.handle()); _handle = other.handle(); _id = other.id(); check_toolbox_class(TOOLBOX_CLASS); return *this;}
125 
131  bool operator==(const Gadget &other) const {return (_handle == other.handle() && _id == other.id());}
132 
138  bool operator!=(const Gadget &other) const {return (_handle != other.handle() || _id != other.id());}
139 
140  unsigned int state() const {return int_property(0x4029);}
141  void state(unsigned int new_state) {int_property(0x4028, new_state);}
142  bool state_flag(unsigned int flag) const {return (state() & flag) != 0;}
143  void state_flag(unsigned int flag, bool set);
144 
145  bool allow_vscroll() const {return state_flag(1);}
146  void allow_vscroll(bool value) {state_flag(1, value);}
147  bool allow_hscroll() const {return state_flag(2);}
148  void allow_hscroll(bool value) {state_flag(2, value);}
149  bool auto_update() const {return state_flag(4);}
150  void auto_update(bool value) {state_flag(4, value);}
151  bool show_current_node() const {return state_flag(8);}
152  void show_current_node(bool value) {state_flag(8, value);}
153  bool plot_lines() const {return state_flag(16);}
154  void plot_lines(bool value) {state_flag(16, value);}
155  bool allow_selection() const {return state_flag(32);}
156  void allow_selection(bool value) {state_flag(32,value);}
157  bool notify_selection() const {return state_flag(64);}
158  void notify_selection(bool value) {state_flag(64, value);}
159  bool select_children() const {return state_flag(1u<<7);}
160  void select_children(bool value) {state_flag(1u<<7, value);}
161  bool allow_expand() const {return state_flag(1u<<8);}
162  void allow_expand(bool value) {state_flag(1u<<8, value);}
163  bool notify_expansion() const {return state_flag(1u<<9);}
164  void notify_expansion(bool value) {state_flag(1u<<9, value);}
165  bool expand_by_default() const {return state_flag(1u<<10);}
166  void expand_by_default(bool value) {state_flag(1u<<10, value);}
167  bool allow_rename() const {return state_flag(1u<<11);}
168  void allow_rename(bool value) {state_flag(1u<<11, value);}
169  bool notify_rename() const {return state_flag(1u<<12);}
170  void notify_rename(bool value) {state_flag(1u<<12, value);}
171  bool allow_drags() const {return state_flag(1u<<13);}
172  void allow_drags(bool value) {state_flag(1u<<13, value);}
173  bool notify_drag_end() const {return state_flag(1u<<14);}
174  void notify_drag_end(bool value) {state_flag(1u<<14, value);}
175  bool all_events() const {return state_flag(1u<<15);}
176  void all_events(bool value) {state_flag(1u<<15,value);}
177  bool double_click_expands() const {return state_flag(1u<<16);}
178  void double_click_expands(bool value) {state_flag(1u<<16,value);}
179  bool flat_mode() const {return state_flag(1u<<17);}
180  void flat_mode(bool value) {state_flag(1u<<17,value);}
181  bool text_below_sprite() const {return state_flag(1u<<18);}
182  void text_below_sprite(bool value) {state_flag(1u<<18,value);}
183  bool sort_display() const {return state_flag(1u<<19);}
184  void sort_display(bool value) {state_flag(1u<<19,value);}
185  bool sort_reversed() const {return state_flag(1u<<20);}
186  void sort_reversed(bool value) {state_flag(1u<<20,value);}
187  bool sort_by_sprite() const {return state_flag(1u<<21);}
188  void sort_by_sprite(bool value) {state_flag(1u<<21,value);}
189 
193  void menu(Menu menu) {int_property(0x403c, (int)menu.handle());}
194 
196  TreeViewCurrentNode current_node(TreeNodeId id);
197  TreeNodeId find_node(int x, int y);
198 
199  void clear();
200 
201  void update_display();
202 
203  Window underlying_window() const;
204 
205  // Listeners
210  /* Renaming is not currently supported
211  *void add_node_renamed_listener(TreeViewNodeRenamedListener *listener);
212  *void remove_node_renamed_listener(TreeViewNodeRenamedListener *listener);
213  */
218 };
219 
228 {
229 public:
231 
235  TreeViewCurrentNode(TreeView &tree_view);
241  TreeViewCurrentNode(const TreeViewCurrentNode &other) {_handle = other._handle; _id = other._id;}
247  TreeViewCurrentNode&operator=(const TreeViewCurrentNode &other) {_handle = other._handle; _id = other._id; return *this;}
253  bool operator==(const TreeViewCurrentNode &other) const {return (_handle == other._handle && _id == other._id);}
259  bool operator!=(const TreeViewCurrentNode &other) const {return (_handle != other._handle && _id != other._id);}
260 
264  TreeNodeId node_id() const {return (TreeNodeId)int_property(0x402b);}
265 
266  void text(const std::string &text) {string_property(0x402c, text);}
267  std::string text() const {return string_property(0x402f);}
268  void sprite(int sprite_area_id, const std::string &sprite_name, const std::string &expanded_sprite_name);
269  std::string sprite_name() const;
270  std::string expanded_sprite_name() const;
271 
272  void private_word(void *word) {int_property(0x402e, (int)word);}
273  void *private_word() const {return (void *)int_property(0x4031);}
274 
275  void move_to(TreeNodeId id);
276  bool move_next(bool sorted = false);
277  bool move_prev(bool sorted = false);
278  bool move_child(bool sorted = false);
279  bool move_parent();
280  bool move_first_selected();
281  bool move_next_selected();
282 
283  void expand(bool expand, bool recurse = false);
284  void select(bool select, bool add);
285  void make_visible();
286 
287  unsigned int state() const;
288  bool has_next_sibling() const {return (state() & (1u<<0)) != 0;}
289  bool has_prev_sibling() const {return (state() & (1u<<1)) != 0;}
290  bool has_child() const {return (state() & (1u<<2)) != 0;}
291  bool parent_is_root() const {return (state() & (1u<<3)) != 0;}
292  bool has_sprite() const {return (state() & (1u<<4)) != 0;}
293  bool has_expanded_sprite() const {return (state() & (1u<<5)) != 0;}
294  bool has_text() const {return (state() & (1u<<6)) != 0;}
295  bool expanded() const {return (state() & (1u<<7)) != 0;}
296  bool selected() const {return (state() & (1u<<8)) != 0;}
297 
298  TreeNodeId add_child(const std::string &text);
299  TreeNodeId add_sibling(const std::string &text);
300  void erase();
301 
302 };
303 
304 }
305 }
306 
307 #endif /* TBX_TREEVIEW_H_ */
tbx::Component::string_property
std::string string_property(int property_id) const
Get a string property from the toolbox object.
Definition: component.cc:261
tbx::ext::TreeView
TreeView wrapper class for an underlying toolbox TreeView gadget.
Definition: treeview.h:63
tbx::ext::TreeView::operator=
TreeView & operator=(const TreeView &other)
Assign a TreeView field to refer to the same underlying toolbox gadget as another.
Definition: treeview.h:107
tbx
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:35
tbx::ext::TreeViewCurrentNode::TreeViewCurrentNode
TreeViewCurrentNode(const TreeViewCurrentNode &other)
Construct from an existing tree view current node.
Definition: treeview.h:241
tbx::ext::TreeViewCurrentNode::erase
void erase()
Delete the current tree node.
Definition: treeview.cc:589
tbx::Component::handle
ObjectId handle() const
Return the object handle for his component.
Definition: component.h:95
tbx::ext::TreeView::underlying_window
Window underlying_window() const
Get underlying window used for the treeview.
Definition: treeview.cc:190
tbx::ext::TreeViewCurrentNode::operator=
TreeViewCurrentNode & operator=(const TreeViewCurrentNode &other)
Assign from an existing tree view current node.
Definition: treeview.h:247
tbx::Window
The Window object represents a toolbox window.
Definition: window.h:70
tbx::ext::TreeViewCurrentNode::expanded_sprite_name
std::string expanded_sprite_name() const
Get the name of the expanded sprite for the current node.
Definition: treeview.cc:352
tbx::ext::TreeView::TreeView
TreeView(const Component &other)
Construct a TreeView field from a Component.
Definition: treeview.h:101
tbx::ext::TreeViewCurrentNode::node_id
TreeNodeId node_id() const
Get the ID of the current node.
Definition: treeview.h:264
tbx::Component::int_property
int int_property(int property_id) const
Get an integer property from the toolbox Component.
Definition: component.cc:165
tbx::ext::TreeViewCurrentNode::move_next
bool move_next(bool sorted=false)
Move current node to next sibling.
Definition: treeview.cc:393
tbx::ext::TreeViewNodeSelectedListener
Listener for node selected events.
Definition: treeviewlisteners.h:75
tbx::ext::TreeViewCurrentNode::operator==
bool operator==(const TreeViewCurrentNode &other) const
Check if this current tree node item refers to the current node of the same tree view.
Definition: treeview.h:253
tbx::ext::TreeViewCurrentNode::move_first_selected
bool move_first_selected()
Move current node to first selected node.
Definition: treeview.cc:455
tbx::ext::TreeViewCurrentNode::move_child
bool move_child(bool sorted=false)
Move current node to first child.
Definition: treeview.cc:425
tbx::ext::TreeView::update_display
void update_display()
Update the display.
Definition: treeview.cc:175
tbx::Component
Base class for components in an object.
Definition: component.h:42
tbx::ext::TreeView::remove_scroll_listener
void remove_scroll_listener(TreeViewScrollListener *listener)
Remove a listener for when the tree view is scrolled.
Definition: treeview.cc:290
tbx::ext::TreeView::add_node_expanded_listener
void add_node_expanded_listener(TreeViewNodeExpandedListener *listener)
Add a listener for when a node is expanded or contracted.
Definition: treeview.cc:226
tbx::Component::_handle
ObjectId _handle
Underlying toolbox handle.
Definition: component.h:45
tbx::ext::TreeView::operator=
TreeView & operator=(const Gadget &other)
Assign a writable field to refer to the same underlying toolbox gadget as an existing Gadget.
Definition: treeview.h:115
tbx::Component::id
ComponentId id() const
Get the component ID of this component.
Definition: component.h:103
tbx::ext::TreeView::TreeView
TreeView(const Gadget &other)
Construct a TreeView from another gadget.
Definition: treeview.h:92
tbx::ext::TreeViewCurrentNode::TreeViewCurrentNode
TreeViewCurrentNode()
Construct an uninitialised TreeViewCurrentNode.
Definition: treeview.h:230
tbx::Menu
Class representing a toolbox menu.
Definition: menu.h:49
tbx::ext::TreeViewCurrentNode::sprite_name
std::string sprite_name() const
Get the name of the sprite for the current node.
Definition: treeview.cc:330
tbx::ext::TreeView::TreeView
TreeView(const TreeView &other)
Construct a TreeView from another TreeView.
Definition: treeview.h:81
tbx::ext::TreeViewCurrentNode::select
void select(bool select, bool add)
Select/deselect a node.
Definition: treeview.cc:503
tbx::ext::TreeViewCurrentNode::operator!=
bool operator!=(const TreeViewCurrentNode &other) const
Check if this current tree node item refers to the current node of the same tree view.
Definition: treeview.h:259
tbx::ext::TreeViewCurrentNode::add_child
TreeNodeId add_child(const std::string &text)
Add a new node as the child of the current node.
Definition: treeview.cc:550
tbx::ext::TreeViewCurrentNode::move_parent
bool move_parent()
Move current node to the parent.
Definition: treeview.cc:440
tbx::ext::TreeView::operator=
TreeView & operator=(const Component &other)
Assign a TreeView field to refer to the same underlying toolbox component as an existing Gadget.
Definition: treeview.h:124
tbx::ext::TreeViewCurrentNode::move_next_selected
bool move_next_selected()
Move current node to the next selected node.
Definition: treeview.cc:470
tbx::ext::TreeViewNodeDraggedListener
Listener for node dragged events.
Definition: treeviewlisteners.h:200
tbx::ext::TreeViewNodeExpandedListener
Listener for node expanded events.
Definition: treeviewlisteners.h:109
tbx::ext::TreeView::menu
void menu(Menu menu)
Set menu for treeview.
Definition: treeview.h:193
tbx::ext::TreeView::add_node_dragged_listener
void add_node_dragged_listener(TreeViewNodeDraggedListener *listener)
Add a listener for when a node is dragged.
Definition: treeview.cc:263
tbx::ext::TreeView::~TreeView
~TreeView()
Destroy a TreeView gadget.
Definition: treeview.h:74
tbx::ext::TreeViewCurrentNode::make_visible
void make_visible()
Make current node visible in the tree.
Definition: treeview.cc:518
tbx::ext::TreeView::add_scroll_listener
void add_scroll_listener(TreeViewScrollListener *listener)
Add a listener for when the tree view is scrolled.
Definition: treeview.cc:281
tbx::ext::TreeViewCurrentNode::move_prev
bool move_prev(bool sorted=false)
Move current node to previous sibling.
Definition: treeview.cc:409
tbx::ext::TreeView::operator!=
bool operator!=(const Gadget &other) const
Check if this TreeView refers to the same underlying toolbox gadget as another gadget.
Definition: treeview.h:138
tbx::ext::TreeView::clear
void clear()
Delete all nodes from the tree.
Definition: treeview.cc:158
tbx::ext::TreeViewCurrentNode
Class representing the current node in a tree view.
Definition: treeview.h:228
tbx::ext::TreeView::current_node
TreeViewCurrentNode current_node()
Get the current tree view node.
Definition: treeview.cc:112
tbx::ext::TreeView::add_node_selected_listener
void add_node_selected_listener(TreeViewNodeSelectedListener *listener)
Add a listener for when a node is selected or deselected.
Definition: treeview.cc:208
tbx::ext::TreeViewCurrentNode::sprite
void sprite(int sprite_area_id, const std::string &sprite_name, const std::string &expanded_sprite_name)
Set the sprites used for the current tree_view node.
Definition: treeview.cc:311
tbx::ext::TreeViewCurrentNode::expand
void expand(bool expand, bool recurse=false)
Expand or contract the current node.
Definition: treeview.cc:486
tbx::Component::_id
int _id
Underlying toolbox component id.
Definition: component.h:46
tbx::ext::TreeViewScrollListener
Listener for tree view scroll events.
Definition: treeviewlisteners.h:230
tbx::ext::TreeViewCurrentNode::move_to
void move_to(TreeNodeId id)
Move current node to a give node id.
Definition: treeview.cc:375
tbx::Gadget
This is the base class for all Gadgets.
Definition: gadget.h:49
tbx::ext::TreeView::remove_node_expanded_listener
void remove_node_expanded_listener(TreeViewNodeExpandedListener *listener)
Remove a listener for when a node is expanded or contracted.
Definition: treeview.cc:235
tbx::Gadget::check_toolbox_class
void check_toolbox_class(int class_id) const
Check the underlying gadget class for this object has the given class id.
Definition: gadget.cc:47
tbx::ext::TreeView::remove_node_selected_listener
void remove_node_selected_listener(TreeViewNodeSelectedListener *listener)
Remove a listener for when a node is selected or deselected.
Definition: treeview.cc:217
tbx::ext::TreeView::find_node
TreeNodeId find_node(int x, int y)
Find the tree node at the given location.
Definition: treeview.cc:138
tbx::ext::TreeView::remove_node_dragged_listener
void remove_node_dragged_listener(TreeViewNodeDraggedListener *listener)
Remove a listener for when a node is dragged.
Definition: treeview.cc:272
tbx::ext::TreeView::TreeView
TreeView()
Construct an uninitialised TreeView.
Definition: treeview.h:67
tbx::ext::TreeView::operator==
bool operator==(const Gadget &other) const
Check if this TreeView refers to the same underlying toolbox gadget as another gadget.
Definition: treeview.h:131
tbx::ext::TreeViewCurrentNode::add_sibling
TreeNodeId add_sibling(const std::string &text)
Add a new node as the child of the current node.
Definition: treeview.cc:571