| 1 |
#if defined(Hiro_TreeView) |
| 2 |
|
| 3 |
namespace hiro { |
| 4 |
|
| 5 |
static const u32 TreeViewIndentation = 20; |
| 6 |
|
| 7 |
//gtk_tree_view_collapse_all(gtkTreeView); |
| 8 |
//gtk_tree_view_expand_all(gtkTreeView); |
| 9 |
|
| 10 |
static auto TreeView_activate(GtkTreeView*, GtkTreePath* gtkPath, GtkTreeViewColumn*, pTreeView* p) -> void { p->_activatePath(gtkPath); } |
| 11 |
static auto TreeView_buttonEvent(GtkTreeView*, GdkEventButton* gdkEvent, pTreeView* p) -> s32 { return p->_buttonEvent(gdkEvent); } |
| 12 |
static auto TreeView_change(GtkTreeSelection*, pTreeView* p) -> void { p->_updateSelected(); } |
| 13 |
static auto TreeView_context(GtkTreeView*, pTreeView* p) -> void { p->self().doContext(); } |
| 14 |
static auto TreeView_dataFunc(GtkTreeViewColumn* column, GtkCellRenderer* renderer, GtkTreeModel* model, GtkTreeIter* iter, pTreeView* p) -> void { return p->_doDataFunc(column, renderer, iter); } |
| 15 |
static auto TreeView_keyPress(GtkWidget*, GdkEventKey*, pTreeView* p) -> s32 { p->suppressActivate = false; return false; } |
| 16 |
static auto TreeView_toggle(GtkCellRendererToggle*, char* path, pTreeView* p) -> void { p->_togglePath(path); } |
| 17 |
|
| 18 |
auto pTreeView::construct() -> void { |
| 19 |
gtkWidget = gtk_scrolled_window_new(0, 0); |
| 20 |
gtkScrolledWindow = GTK_SCROLLED_WINDOW(gtkWidget); |
| 21 |
gtk_scrolled_window_set_policy(gtkScrolledWindow, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); |
| 22 |
gtk_scrolled_window_set_shadow_type(gtkScrolledWindow, GTK_SHADOW_ETCHED_IN); |
| 23 |
|
| 24 |
gtkTreeStore = gtk_tree_store_new(3, G_TYPE_BOOLEAN, GDK_TYPE_PIXBUF, G_TYPE_STRING); |
| 25 |
gtkTreeModel = GTK_TREE_MODEL(gtkTreeStore); |
| 26 |
|
| 27 |
gtkWidgetChild = gtk_tree_view_new_with_model(gtkTreeModel); |
| 28 |
gtkTreeView = GTK_TREE_VIEW(gtkWidgetChild); |
| 29 |
gtkTreeSelection = gtk_tree_view_get_selection(gtkTreeView); |
| 30 |
gtk_tree_view_set_headers_visible(gtkTreeView, false); |
| 31 |
gtk_tree_view_set_show_expanders(gtkTreeView, false); |
| 32 |
gtk_tree_view_set_level_indentation(gtkTreeView, TreeViewIndentation); |
| 33 |
gtk_container_add(GTK_CONTAINER(gtkWidget), gtkWidgetChild); |
| 34 |
gtk_widget_show(gtkWidgetChild); |
| 35 |
|
| 36 |
gtkTreeViewColumn = gtk_tree_view_column_new(); |
| 37 |
|
| 38 |
gtkCellToggle = gtk_cell_renderer_toggle_new(); |
| 39 |
gtk_tree_view_column_pack_start(gtkTreeViewColumn, gtkCellToggle, false); |
| 40 |
gtk_tree_view_column_set_attributes(gtkTreeViewColumn, gtkCellToggle, "active", 0, nullptr); |
| 41 |
gtk_tree_view_column_set_cell_data_func(gtkTreeViewColumn, GTK_CELL_RENDERER(gtkCellToggle), (GtkTreeCellDataFunc)TreeView_dataFunc, (gpointer)this, nullptr); |
| 42 |
|
| 43 |
gtkCellPixbuf = gtk_cell_renderer_pixbuf_new(); |
| 44 |
gtk_tree_view_column_pack_start(gtkTreeViewColumn, gtkCellPixbuf, false); |
| 45 |
gtk_tree_view_column_set_attributes(gtkTreeViewColumn, gtkCellPixbuf, "pixbuf", 1, nullptr); |
| 46 |
gtk_tree_view_column_set_cell_data_func(gtkTreeViewColumn, GTK_CELL_RENDERER(gtkCellPixbuf), (GtkTreeCellDataFunc)TreeView_dataFunc, (gpointer)this, nullptr); |
| 47 |
|
| 48 |
gtkCellText = gtk_cell_renderer_text_new(); |
| 49 |
gtk_tree_view_column_pack_start(gtkTreeViewColumn, gtkCellText, true); |
| 50 |
gtk_tree_view_column_set_attributes(gtkTreeViewColumn, gtkCellText, "text", 2, nullptr); |
| 51 |
gtk_tree_view_column_set_cell_data_func(gtkTreeViewColumn, GTK_CELL_RENDERER(gtkCellText), (GtkTreeCellDataFunc)TreeView_dataFunc, (gpointer)this, nullptr); |
| 52 |
|
| 53 |
gtk_tree_view_append_column(gtkTreeView, gtkTreeViewColumn); |
| 54 |
gtk_tree_view_set_search_column(gtkTreeView, 2); |
| 55 |
|
| 56 |
setActivation(state().activation); |
| 57 |
setBackgroundColor(state().backgroundColor); |
| 58 |
setForegroundColor(state().foregroundColor); |
| 59 |
|
| 60 |
g_signal_connect(G_OBJECT(gtkWidgetChild), "button-press-event", G_CALLBACK(TreeView_buttonEvent), (gpointer)this); |
| 61 |
g_signal_connect(G_OBJECT(gtkWidgetChild), "button-release-event", G_CALLBACK(TreeView_buttonEvent), (gpointer)this); |
| 62 |
g_signal_connect(G_OBJECT(gtkWidgetChild), "key-press-event", G_CALLBACK(TreeView_keyPress), (gpointer)this); |
| 63 |
g_signal_connect(G_OBJECT(gtkWidgetChild), "popup-menu", G_CALLBACK(TreeView_context), (gpointer)this); |
| 64 |
g_signal_connect(G_OBJECT(gtkWidgetChild), "row-activated", G_CALLBACK(TreeView_activate), (gpointer)this); |
| 65 |
g_signal_connect(G_OBJECT(gtkTreeSelection), "changed", G_CALLBACK(TreeView_change), (gpointer)this); |
| 66 |
g_signal_connect(G_OBJECT(gtkCellToggle), "toggled", G_CALLBACK(TreeView_toggle), (gpointer)this); |
| 67 |
|
| 68 |
//Ctrl+F triggers a small popup window at the bottom of the GtkTreeView, which clears the currently selected item(s) |
| 69 |
//this is undesirable for amethyst, which uses the active item to display a document to edit, and binds Ctrl+F to a document find function |
| 70 |
//for now, disable GtkTreeView's interactive search: longer term, more thought will need to go into if this is ever desirable or not |
| 71 |
//gtk_tree_view_set_enable_search(gtkTreeView, false) does not work |
| 72 |
//gtk_tree_view_set_search_column(gtkTreeView, -1) does not work |
| 73 |
gtkEntry = (GtkEntry*)gtk_entry_new(); |
| 74 |
gtk_tree_view_set_search_entry(gtkTreeView, gtkEntry); |
| 75 |
|
| 76 |
pWidget::construct(); |
| 77 |
} |
| 78 |
|
| 79 |
auto pTreeView::destruct() -> void { |
| 80 |
gtk_widget_destroy(GTK_WIDGET(gtkEntry)); |
| 81 |
gtk_widget_destroy(gtkWidgetChild); |
| 82 |
gtk_widget_destroy(gtkWidget); |
| 83 |
} |
| 84 |
|
| 85 |
// |
| 86 |
|
| 87 |
auto pTreeView::append(sTreeViewItem item) -> void { |
| 88 |
} |
| 89 |
|
| 90 |
auto pTreeView::remove(sTreeViewItem item) -> void { |
| 91 |
} |
| 92 |
|
| 93 |
auto pTreeView::setActivation(Mouse::Click activation) -> void { |
| 94 |
//handled by callbacks |
| 95 |
} |
| 96 |
|
| 97 |
auto pTreeView::setBackgroundColor(Color color) -> void { |
| 98 |
auto gdkColor = CreateColor(color); |
| 99 |
gtk_widget_modify_base(gtkWidgetChild, GTK_STATE_NORMAL, color ? &gdkColor : nullptr); |
| 100 |
} |
| 101 |
|
| 102 |
auto pTreeView::setFocused() -> void { |
| 103 |
//gtk_widget_grab_focus() will select the first item if nothing is currently selected |
| 104 |
//this behavior is undesirable. detect selection state first, and restore if required |
| 105 |
lock(); |
| 106 |
bool selected = gtk_tree_selection_get_selected(gtkTreeSelection, nullptr, nullptr); |
| 107 |
gtk_widget_grab_focus(gtkWidgetChild); |
| 108 |
if(!selected) gtk_tree_selection_unselect_all(gtkTreeSelection); |
| 109 |
unlock(); |
| 110 |
} |
| 111 |
|
| 112 |
auto pTreeView::setForegroundColor(Color color) -> void { |
| 113 |
auto gdkColor = CreateColor(color); |
| 114 |
gtk_widget_modify_text(gtkWidgetChild, GTK_STATE_NORMAL, color ? &gdkColor : nullptr); |
| 115 |
} |
| 116 |
|
| 117 |
auto pTreeView::setGeometry(Geometry geometry) -> void { |
| 118 |
pWidget::setGeometry(geometry); |
| 119 |
_updateScrollBars(); |
| 120 |
} |
| 121 |
|
| 122 |
// |
| 123 |
|
| 124 |
auto pTreeView::_activatePath(GtkTreePath* gtkPath) -> void { |
| 125 |
if(suppressActivate) { |
| 126 |
suppressActivate = false; |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
char* path = gtk_tree_path_to_string(gtkPath); |
| 131 |
if(auto item = self().item(string{path}.transform(":", "/"))) { |
| 132 |
if(!locked()) self().doActivate(); |
| 133 |
} |
| 134 |
g_free(path); |
| 135 |
} |
| 136 |
|
| 137 |
auto pTreeView::_buttonEvent(GdkEventButton* gdkEvent) -> s32 { |
| 138 |
if(gdkEvent->type == GDK_BUTTON_PRESS) { |
| 139 |
//detect when the empty space of the GtkTreeView is clicked; and clear the selection |
| 140 |
GtkTreePath* gtkPath = nullptr; |
| 141 |
gtk_tree_view_get_path_at_pos(gtkTreeView, gdkEvent->x, gdkEvent->y, >kPath, nullptr, nullptr, nullptr); |
| 142 |
if(!gtkPath) { |
| 143 |
//the first time a GtkTreeView widget is clicked, even if the empty space of the widget is clicked, |
| 144 |
//a "changed" signal will be sent after the "button-press-event", to activate the first item in the tree |
| 145 |
//this is undesirable, so set a flag to undo the next selection change during the "changed" signal |
| 146 |
suppressChange = true; |
| 147 |
if(gtk_tree_selection_count_selected_rows(gtkTreeSelection) > 0) { |
| 148 |
gtk_tree_selection_unselect_all(gtkTreeSelection); |
| 149 |
state().selectedPath.reset(); |
| 150 |
self().doChange(); |
| 151 |
return true; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
if(gdkEvent->button == 1) { |
| 156 |
//emulate activate-on-single-click, which is only available in GTK+ 3.8 and later |
| 157 |
if(gtkPath && self().activation() == Mouse::Click::Single) { |
| 158 |
//selectedPath must be updated for TreeView::doActivate() to act on the correct TreeViewItem. |
| 159 |
//as this will then cause "changed" to not see that the path has changed, we must handle that case here as well. |
| 160 |
char* path = gtk_tree_path_to_string(gtkPath); |
| 161 |
string selectedPath = string{path}.transform(":", "/"); |
| 162 |
g_free(path); |
| 163 |
if(state().selectedPath != selectedPath) { |
| 164 |
state().selectedPath = selectedPath; |
| 165 |
self().doChange(); |
| 166 |
} |
| 167 |
self().doActivate(); |
| 168 |
//"row-activated" is sent before "button-press-event" (GDK_2BUTTON_PRESS); |
| 169 |
//so stop a double-click from calling TreeView::doActivate() twice by setting a flag after single-clicks |
| 170 |
suppressActivate = true; //key presses will clear this flag to allow key-activations to work correctly |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
if(gdkEvent->button == 3) { |
| 175 |
//multi-selection mode: (not implemented in TreeView yet ... but code is here anyway for future use) |
| 176 |
//if multiple items are selected, and one item is right-clicked on (for a context menu), GTK clears selection on all other items |
| 177 |
//block this behavior so that onContext() handler can work on more than one selected item at a time |
| 178 |
if(gtkPath && gtk_tree_selection_path_is_selected(gtkTreeSelection, gtkPath)) return true; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
if(gdkEvent->type == GDK_BUTTON_RELEASE) { |
| 183 |
suppressChange = false; |
| 184 |
if(gdkEvent->button == 3) { |
| 185 |
//handle action during right-click release; as button-press-event is sent prior to selection update |
| 186 |
//without this, the callback handler would see the previous selection state instead |
| 187 |
self().doContext(); |
| 188 |
return false; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
auto pTreeView::_doDataFunc(GtkTreeViewColumn* column, GtkCellRenderer* renderer, GtkTreeIter* iter) -> void { |
| 196 |
auto path = gtk_tree_model_get_string_from_iter(gtkTreeModel, iter); |
| 197 |
auto parts = nall::split((const char*)path, ":"); |
| 198 |
g_free(path); |
| 199 |
|
| 200 |
auto item = self().item(parts.empty() ? 0u : parts.front().natural()); |
| 201 |
if(!parts.empty()) parts.erase(parts.begin()); |
| 202 |
if(!item) return; |
| 203 |
while(!parts.empty()) { |
| 204 |
item = item.item(parts.front().natural()); |
| 205 |
parts.erase(parts.begin()); |
| 206 |
if(!item) return; |
| 207 |
} |
| 208 |
|
| 209 |
if(renderer == GTK_CELL_RENDERER(gtkCellToggle)) { |
| 210 |
gtk_cell_renderer_set_visible(renderer, item->state.checkable); |
| 211 |
} else if(renderer == GTK_CELL_RENDERER(gtkCellPixbuf)) { |
| 212 |
gtk_cell_renderer_set_visible(renderer, (bool)item->state.icon); |
| 213 |
} else if(renderer == GTK_CELL_RENDERER(gtkCellText)) { |
| 214 |
auto font = pFont::create(item->font(true)); |
| 215 |
g_object_set(G_OBJECT(renderer), "font-desc", font, nullptr); |
| 216 |
pango_font_description_free(font); |
| 217 |
if(auto color = item->foregroundColor(true)) { |
| 218 |
auto gdkColor = CreateColor(color); |
| 219 |
g_object_set(G_OBJECT(renderer), "foreground-gdk", &gdkColor, nullptr); |
| 220 |
} else { |
| 221 |
g_object_set(G_OBJECT(renderer), "foreground-set", false, nullptr); |
| 222 |
} |
| 223 |
} |
| 224 |
if(auto color = item->backgroundColor(true)) { |
| 225 |
auto gdkColor = CreateColor(color); |
| 226 |
g_object_set(G_OBJECT(renderer), "cell-background-gdk", &gdkColor, nullptr); |
| 227 |
} else { |
| 228 |
g_object_set(G_OBJECT(renderer), "cell-background-set", false, nullptr); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
auto pTreeView::_togglePath(string path) -> void { |
| 233 |
if(auto item = self().item(path.transform(":", "/"))) { |
| 234 |
bool checked = !item->checked(); |
| 235 |
gtk_tree_store_set(gtkTreeStore, &item->self()->gtkIter, 0, checked, -1); |
| 236 |
item->state.checked = checked; |
| 237 |
if(!locked()) self().doToggle(item); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
//it is necessary to compute the minimum width necessary to show all items in a tree, |
| 242 |
//before a horizontal scroll bar must be shown. this is because GTK2 (and possibly GTK3) |
| 243 |
//fail to subtract the tree view indentation level on items before determining if the |
| 244 |
//horizontal scroll bar is necessary. as a result, without this, the scroll bar shows up |
| 245 |
//far before it is necessary, and gets worse the more nested the tree is. |
| 246 |
// |
| 247 |
//this is called whenever the TreeView geometry changes, or whenever a TreeViewItem's |
| 248 |
//checkability, icon, or text is updated. in other words, whenever the need for a horizontal |
| 249 |
//scroll bar to show all items in the tree is necessary or not. |
| 250 |
auto pTreeView::_updateScrollBars() -> void { |
| 251 |
s32 maximumWidth = self().geometry().width() - 6; |
| 252 |
if(auto scrollBar = gtk_scrolled_window_get_vscrollbar(gtkScrolledWindow)) { |
| 253 |
GtkAllocation allocation; |
| 254 |
gtk_widget_get_allocation(scrollBar, &allocation); |
| 255 |
if(gtk_widget_get_visible(scrollBar)) maximumWidth -= allocation.width; |
| 256 |
} |
| 257 |
|
| 258 |
s32 minimumWidth = 0; |
| 259 |
for(auto& item : state().items) { |
| 260 |
if(auto self = item->self()) { |
| 261 |
minimumWidth = max(minimumWidth, self->_minimumWidth()); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
gtk_scrolled_window_set_policy(gtkScrolledWindow, |
| 266 |
minimumWidth >= maximumWidth ? GTK_POLICY_ALWAYS : GTK_POLICY_NEVER, |
| 267 |
GTK_POLICY_AUTOMATIC); |
| 268 |
} |
| 269 |
|
| 270 |
auto pTreeView::_updateSelected() -> void { |
| 271 |
if(suppressChange) { |
| 272 |
suppressChange = false; |
| 273 |
gtk_tree_selection_unselect_all(gtkTreeSelection); |
| 274 |
return; |
| 275 |
} |
| 276 |
|
| 277 |
GtkTreeIter iter; |
| 278 |
if(gtk_tree_selection_get_selected(gtkTreeSelection, >kTreeModel, &iter)) { |
| 279 |
char* gtkPath = gtk_tree_model_get_string_from_iter(gtkTreeModel, &iter); |
| 280 |
string path = string{gtkPath}.transform(":", "/"); |
| 281 |
g_free(gtkPath); |
| 282 |
if(state().selectedPath != path) { |
| 283 |
state().selectedPath = path; |
| 284 |
if(!locked()) self().doChange(); |
| 285 |
} |
| 286 |
} else if(state().selectedPath) { |
| 287 |
state().selectedPath.reset(); |
| 288 |
if(!locked()) self().doChange(); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
#endif |