Debian Codegraph @

hiro/gtk/widget/tree-view-item.cpp

raw
1 #if defined(Hiro_TreeView)
2
3 namespace hiro {
4
5 auto pTreeViewItem::construct() -> void {
6 if(auto parentWidget = _parentWidget()) {
7 if(auto parentItem = _parentItem()) {
8 gtk_tree_store_append(parentWidget->gtkTreeStore, &gtkIter, &parentItem->gtkIter);
9 } else {
10 gtk_tree_store_append(parentWidget->gtkTreeStore, &gtkIter, nullptr);
11 }
12 setChecked(state().checked);
13 setIcon(state().icon);
14 setText(state().text);
15 }
16 }
17
18 auto pTreeViewItem::destruct() -> void {
19 if(auto parent = _parentWidget()) {
20 gtk_tree_store_remove(parent->gtkTreeStore, &gtkIter);
21 }
22 }
23
24 //
25
26 auto pTreeViewItem::append(sTreeViewItem item) -> void {
27 }
28
29 auto pTreeViewItem::remove(sTreeViewItem item) -> void {
30 }
31
32 auto pTreeViewItem::setBackgroundColor(Color color) -> void {
33 }
34
35 auto pTreeViewItem::setCheckable(bool checkable) -> void {
36 _updateWidth();
37 }
38
39 auto pTreeViewItem::setChecked(bool checked) -> void {
40 if(auto parentWidget = _parentWidget()) {
41 gtk_tree_store_set(parentWidget->gtkTreeStore, &gtkIter, 0, checked, -1);
42 }
43 }
44
45 auto pTreeViewItem::setExpanded(bool expanded) -> void {
46 if(auto parentWidget = _parentWidget()) {
47 auto path = gtk_tree_model_get_path(parentWidget->gtkTreeModel, &gtkIter);
48 if(expanded) {
49 gtk_tree_view_expand_row(parentWidget->gtkTreeView, path, false);
50 //if you collapse a parent node, GTK collapses all child nodes
51 //this isn't very desirable, so restore any child expansions recursively here
52 for(auto& item : state().items) {
53 item->setExpanded(item->expanded());
54 }
55 } else {
56 gtk_tree_view_collapse_row(parentWidget->gtkTreeView, path);
57 }
58 gtk_tree_path_free(path);
59 }
60 }
61
62 auto pTreeViewItem::setFocused() -> void {
63 if(auto parentWidget = _parentWidget()) {
64 GtkTreePath* path = gtk_tree_path_new_from_string(self().path().transform("/", ":"));
65 gtk_tree_view_set_cursor(parentWidget->gtkTreeView, path, nullptr, false);
66 gtk_tree_view_scroll_to_cell(parentWidget->gtkTreeView, path, nullptr, true, 0.5, 0.0);
67 gtk_tree_path_free(path);
68 }
69 }
70
71 auto pTreeViewItem::setForegroundColor(Color color) -> void {
72 }
73
74 auto pTreeViewItem::setIcon(const image& icon) -> void {
75 if(auto parentWidget = _parentWidget()) {
76 if(icon) {
77 auto pixbuf = CreatePixbuf(icon);
78 gtk_tree_store_set(parentWidget->gtkTreeStore, &gtkIter, 1, pixbuf, -1);
79 } else {
80 gtk_tree_store_set(parentWidget->gtkTreeStore, &gtkIter, 1, nullptr, -1);
81 }
82 }
83 _updateWidth();
84 }
85
86 auto pTreeViewItem::setSelected() -> void {
87 if(auto parentWidget = _parentWidget()) {
88 parentWidget->lock();
89 //in order to select an item, it must first be visible
90 auto gtkPath = gtk_tree_model_get_path(parentWidget->gtkTreeModel, &gtkIter);
91 gtk_tree_view_expand_to_path(parentWidget->gtkTreeView, gtkPath);
92 gtk_tree_path_free(gtkPath);
93 gtk_tree_selection_select_iter(parentWidget->gtkTreeSelection, &gtkIter);
94 parentWidget->unlock();
95 }
96 }
97
98 auto pTreeViewItem::setText(const string& text) -> void {
99 if(auto parentWidget = _parentWidget()) {
100 gtk_tree_store_set(parentWidget->gtkTreeStore, &gtkIter, 2, (const char*)text, -1);
101 }
102 _updateWidth();
103 }
104
105 //
106
107 //recursive function to find the minimum (pre-computed / cached) width of a TreeViewItem tree
108 auto pTreeViewItem::_minimumWidth(u32 depth) -> u32 {
109 u32 width = TreeViewIndentation * depth + _width;
110 for(auto& item : state().items) {
111 if(auto self = item->self()) {
112 width = max(width, self->_minimumWidth(depth + 1));
113 }
114 }
115 return width;
116 }
117
118 auto pTreeViewItem::_parentItem() -> pTreeViewItem* {
119 if(auto parentItem = self().parentTreeViewItem()) return parentItem->self();
120 return nullptr;
121 }
122
123 auto pTreeViewItem::_parentWidget() -> pTreeView* {
124 if(auto parentWidget = self().parentTreeView(true)) return parentWidget->self();
125 return nullptr;
126 }
127
128 //this is called any time a TreeViewItem's checkability, icon, or text is updated
129 //in other words, whenever the width of the item might have changed
130 //it may change the requirement of the TreeView needing a scrollbar, so notify the TreeView as well
131 auto pTreeViewItem::_updateWidth() -> void {
132 _width = 4;
133 if(state().checkable) _width += 16 + (state().icon || state().text ? 4 : 0);
134 if(auto& icon = state().icon) _width += icon.width() + 2;
135 if(auto& text = state().text) _width += pFont::size(self().font(true), text).width();
136 if(auto parent = _parentWidget()) parent->_updateScrollBars();
137 }
138
139 }
140
141 #endif