Debian Codegraph @

hiro/gtk/widget/table-view.cpp

raw
1 #if defined(Hiro_TableView)
2
3 namespace hiro {
4
5 static auto TableView_activate(GtkTreeView*, GtkTreePath* gtkRow, GtkTreeViewColumn* gtkColumn, pTableView* p) -> void { return p->_doActivate(gtkRow, gtkColumn); }
6 static auto TableView_buttonEvent(GtkTreeView* treeView, GdkEventButton* event, pTableView* p) -> s32 { return p->_doEvent(event); }
7 static auto TableView_change(GtkTreeSelection*, pTableView* p) -> void { return p->_doChange(); }
8 static auto TableView_edit(GtkCellRendererText* renderer, const char* path, const char* text, pTableView* p) -> void { return p->_doEdit(renderer, path, text); }
9 static auto TableView_headerActivate(GtkTreeViewColumn* column, pTableView* p) -> void { return p->_doHeaderActivate(column); }
10 static auto TableView_keyPressEvent(GtkTreeView* treeView, GdkEventKey* event, pTableView* p) -> bool { return p->_doKeyPress(event); }
11 static auto TableView_mouseMoveEvent(GtkWidget*, GdkEvent*, pTableView* p) -> s32 { return p->_doMouseMove(); }
12 static auto TableView_popup(GtkTreeView*, pTableView* p) -> void { return p->_doContext(); }
13
14 static auto TableView_dataFunc(GtkTreeViewColumn* column, GtkCellRenderer* renderer, GtkTreeModel* model, GtkTreeIter* iter, pTableView* p) -> void { return p->_doDataFunc(column, renderer, iter); }
15 static auto TableView_toggle(GtkCellRendererToggle* toggle, const char* path, pTableView* p) -> void { return p->_doToggle(toggle, path); }
16
17 static auto TableView_realize(GtkTreeView*, pTableView* p) -> void {
18 //the initial geometry for column sizing is most likely wrong at this point:
19 //having to call processEvents() twice is very heavy-handed, but necessary here.
20 for(u32 repeat : range(2)) {
21 Application::processEvents();
22 p->resizeColumns();
23 }
24 }
25
26 auto pTableView::construct() -> void {
27 gtkWidget = gtk_scrolled_window_new(0, 0);
28 gtkScrolledWindow = GTK_SCROLLED_WINDOW(gtkWidget);
29 gtk_scrolled_window_set_policy(gtkScrolledWindow, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
30 gtk_scrolled_window_set_shadow_type(gtkScrolledWindow, GTK_SHADOW_ETCHED_IN);
31
32 gtkWidgetChild = gtk_tree_view_new();
33 gtkTreeView = GTK_TREE_VIEW(gtkWidgetChild);
34 gtkTreeSelection = gtk_tree_view_get_selection(gtkTreeView);
35 gtk_container_add(GTK_CONTAINER(gtkWidget), gtkWidgetChild);
36 gtk_tree_view_set_rubber_banding(gtkTreeView, true);
37
38 gtk_widget_show(gtkWidgetChild);
39
40 setBackgroundColor(state().backgroundColor);
41 setBatchable(state().batchable);
42 setBordered(state().bordered);
43 setFont(self().font(true));
44 setForegroundColor(state().foregroundColor);
45 setHeadered(state().headered);
46 setSortable(state().sortable);
47
48 g_signal_connect(G_OBJECT(gtkTreeView), "button-press-event", G_CALLBACK(TableView_buttonEvent), (gpointer)this);
49 g_signal_connect(G_OBJECT(gtkTreeView), "button-release-event", G_CALLBACK(TableView_buttonEvent), (gpointer)this);
50 g_signal_connect(G_OBJECT(gtkTreeView), "key-press-event", G_CALLBACK(TableView_keyPressEvent), (gpointer)this);
51 g_signal_connect(G_OBJECT(gtkTreeView), "motion-notify-event", G_CALLBACK(TableView_mouseMoveEvent), (gpointer)this);
52 g_signal_connect(G_OBJECT(gtkTreeView), "popup-menu", G_CALLBACK(TableView_popup), (gpointer)this);
53 g_signal_connect(G_OBJECT(gtkTreeView), "realize", G_CALLBACK(TableView_realize), (gpointer)this);
54 g_signal_connect(G_OBJECT(gtkTreeView), "row-activated", G_CALLBACK(TableView_activate), (gpointer)this);
55 g_signal_connect(G_OBJECT(gtkTreeSelection), "changed", G_CALLBACK(TableView_change), (gpointer)this);
56
57 //searching doesn't currently work anyway ...
58 gtkEntry = (GtkEntry*)gtk_entry_new();
59 gtk_tree_view_set_search_entry(gtkTreeView, gtkEntry);
60
61 pWidget::construct();
62 }
63
64 auto pTableView::destruct() -> void {
65 gtk_widget_destroy(GTK_WIDGET(gtkEntry));
66 gtk_widget_destroy(gtkWidgetChild);
67 gtk_widget_destroy(gtkWidget);
68 }
69
70 auto pTableView::append(sTableViewColumn column) -> void {
71 _updateRulesHint();
72 }
73
74 auto pTableView::append(sTableViewItem item) -> void {
75 }
76
77 auto pTableView::focused() const -> bool {
78 return gtk_widget_has_focus(GTK_WIDGET(gtkTreeView));
79 }
80
81 auto pTableView::remove(sTableViewColumn column) -> void {
82 _updateRulesHint();
83 }
84
85 auto pTableView::remove(sTableViewItem item) -> void {
86 }
87
88 auto pTableView::resizeColumns() -> void {
89 auto lock = acquire();
90
91 std::vector<s32> widths;
92 s32 minimumWidth = 0;
93 s32 expandable = 0;
94 for(u32 position : range(self().columnCount())) {
95 s32 width = _width(position);
96 widths.push_back(width);
97 minimumWidth += width;
98 if(self().column(position).expandable()) expandable++;
99 }
100
101 s32 maximumWidth = self().geometry().width() - 6;
102 if(auto scrollBar = gtk_scrolled_window_get_vscrollbar(gtkScrolledWindow)) {
103 GtkAllocation allocation;
104 gtk_widget_get_allocation(scrollBar, &allocation);
105 if(gtk_widget_get_visible(scrollBar)) maximumWidth -= allocation.width;
106 }
107
108 s32 expandWidth = 0;
109 if(expandable && maximumWidth > minimumWidth) {
110 expandWidth = (maximumWidth - minimumWidth) / expandable;
111 }
112
113 for(u32 position : range(self().columnCount())) {
114 if(auto column = self().column(position)->self()) {
115 s32 width = widths[position];
116 if(column->state().expandable) width += expandWidth;
117 gtk_tree_view_column_set_fixed_width(column->gtkColumn, width);
118 }
119 }
120 }
121
122 auto pTableView::setAlignment(Alignment alignment) -> void {
123 _updateRulesHint();
124 }
125
126 auto pTableView::setBackgroundColor(Color color) -> void {
127 GdkColor gdkColor = CreateColor(color);
128 gtk_widget_modify_base(gtkWidgetChild, GTK_STATE_NORMAL, color ? &gdkColor : nullptr);
129 _updateRulesHint();
130 }
131
132 auto pTableView::setBatchable(bool batchable) -> void {
133 gtk_tree_selection_set_mode(gtkTreeSelection, batchable ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE);
134 }
135
136 auto pTableView::setBordered(bool bordered) -> void {
137 gtk_tree_view_set_grid_lines(gtkTreeView, bordered ? GTK_TREE_VIEW_GRID_LINES_BOTH : GTK_TREE_VIEW_GRID_LINES_NONE);
138 }
139
140 auto pTableView::setFocused() -> void {
141 lock();
142 //gtk_widget_grab_focus() will select the first item if nothing is currently selected
143 //this behavior is undesirable. detect selection state first, and restore if required
144 if(!state().batchable) {
145 //gtk_tree_selection_get_selected() will throw a critical exception in batchable mode
146 bool selected = gtk_tree_selection_get_selected(gtkTreeSelection, nullptr, nullptr);
147 gtk_widget_grab_focus(gtkWidgetChild);
148 if(!selected) gtk_tree_selection_unselect_all(gtkTreeSelection);
149 } else {
150 gtk_widget_grab_focus(gtkWidgetChild);
151 }
152 unlock();
153 }
154
155 auto pTableView::setFont(const Font& font) -> void {
156 }
157
158 auto pTableView::setForegroundColor(Color color) -> void {
159 GdkColor gdkColor = CreateColor(color);
160 gtk_widget_modify_text(gtkWidgetChild, GTK_STATE_NORMAL, color ? &gdkColor : nullptr);
161 _updateRulesHint();
162 }
163
164 auto pTableView::setGeometry(Geometry geometry) -> void {
165 pWidget::setGeometry(geometry);
166 for(auto& column : state().columns) {
167 if(column->expandable()) {
168 Application::processEvents();
169 return resizeColumns();
170 }
171 }
172 }
173
174 auto pTableView::setHeadered(bool headered) -> void {
175 gtk_tree_view_set_headers_visible(gtkTreeView, headered);
176 }
177
178 auto pTableView::setSortable(bool sortable) -> void {
179 for(auto& column : state().columns) {
180 if(auto self = column->self()) gtk_tree_view_column_set_clickable(self->gtkColumn, sortable);
181 }
182 }
183
184 auto pTableView::_cellWidth(u32 _row, u32 _column) -> u32 {
185 u32 width = 8;
186 if(auto item = self().item(_row)) {
187 if(auto cell = item->cell(_column)) {
188 if(cell->state.checkable) {
189 width += 16 + (cell->state.icon || cell->state.text ? 4 : 0);
190 }
191 if(auto& icon = cell->state.icon) {
192 width += icon.width() + 2;
193 }
194 if(auto& text = cell->state.text) {
195 width += pFont::size(cell->font(true), text).width();
196 }
197 }
198 }
199 return width;
200 }
201
202 auto pTableView::_columnWidth(u32 _column) -> u32 {
203 u32 width = 6;
204 if(auto column = self().column(_column)) {
205 if(auto& icon = column->state.icon) {
206 width += icon.width() + 2;
207 }
208 if(auto& text = column->state.text) {
209 width += pFont::size(column->font(true), text).width() + 8;
210 }
211 if(column->state.sorting != Sort::None) {
212 width += 20;
213 }
214 }
215 return width;
216 }
217
218 auto pTableView::_createModel() -> void {
219 auto lock = acquire();
220
221 gtk_tree_view_set_model(gtkTreeView, nullptr);
222 gtkListStore = nullptr;
223 gtkTreeModel = nullptr;
224
225 std::vector<GType> types;
226 for(auto& column : state().columns) {
227 if(auto self = column->self()) {
228 if(!self->gtkColumn) continue; //may not have been created yet; or recently destroyed
229 types.push_back(G_TYPE_BOOLEAN);
230 types.push_back(GDK_TYPE_PIXBUF);
231 types.push_back(G_TYPE_STRING);
232 }
233 }
234 if(!types.size()) return; //no columns available
235
236 gtkListStore = gtk_list_store_newv(types.size(), types.data());
237 gtkTreeModel = GTK_TREE_MODEL(gtkListStore);
238 gtk_tree_view_set_model(gtkTreeView, gtkTreeModel);
239 }
240
241 auto pTableView::_doActivate(GtkTreePath* gtkRow, GtkTreeViewColumn* gtkColumn) -> void {
242 if(locked()) return;
243
244 if(gtkRow && gtkColumn) {
245 auto path = gtk_tree_path_to_string(gtkRow);
246 auto item = self().item(toNatural(path));
247 auto cell = item.cell(0);
248 for(auto& column : state().columns) {
249 if(auto self = column->self()) {
250 if(self->gtkColumn == gtkColumn) {
251 cell = item.cell(column->offset());
252 break;
253 }
254 }
255 }
256 g_free(path);
257 self().doActivate(cell);
258 } else {
259 self().doActivate({});
260 }
261 }
262
263 auto pTableView::_doChange() -> void {
264 if(!locked()) _updateSelected();
265 }
266
267 auto pTableView::_doContext() -> void {
268 if(locked()) return;
269
270 if(auto item = self().selected()) {
271 //keyboard context menu cannot report the currently selected cell; so return first cell rather than nothing
272 self().doContext(item.cell(0));
273 } else {
274 self().doContext(TableViewCell());
275 }
276 }
277
278 auto pTableView::_doDataFunc(GtkTreeViewColumn* gtkColumn, GtkCellRenderer* renderer, GtkTreeIter* iter) -> void {
279 auto path = gtk_tree_model_get_string_from_iter(gtkTreeModel, iter);
280 auto row = toNatural(path);
281 g_free(path);
282
283 for(auto& column : state().columns) {
284 if(auto p = column->self()) {
285 if(renderer != GTK_CELL_RENDERER(p->gtkCellToggle)
286 && renderer != GTK_CELL_RENDERER(p->gtkCellIcon)
287 && renderer != GTK_CELL_RENDERER(p->gtkCellText)
288 ) continue;
289 if(auto item = self().item(row)) {
290 if(auto cell = item->cell(column->offset())) {
291 if(renderer == GTK_CELL_RENDERER(p->gtkCellToggle)) {
292 gtk_cell_renderer_set_visible(renderer, cell->state.checkable);
293 } else if(renderer == GTK_CELL_RENDERER(p->gtkCellText)) {
294 auto alignment = cell->alignment(true);
295 if(!alignment) alignment = {0.0, 0.5};
296 //note: below line will center column header text; but causes strange glitches
297 //(specifically, windows fail to respond to the close button ... some kind of heap corruption inside GTK+)
298 //gtk_tree_view_column_set_alignment(gtkColumn, alignment.horizontal());
299 gtk_cell_renderer_set_alignment(renderer, alignment.horizontal(), alignment.vertical());
300 auto pangoAlignment = PANGO_ALIGN_CENTER;
301 if(alignment.horizontal() < 0.333) pangoAlignment = PANGO_ALIGN_LEFT;
302 if(alignment.horizontal() > 0.666) pangoAlignment = PANGO_ALIGN_RIGHT;
303 g_object_set(G_OBJECT(renderer), "alignment", pangoAlignment, nullptr);
304 auto font = pFont::create(cell->font(true));
305 g_object_set(G_OBJECT(renderer), "font-desc", font, nullptr);
306 pango_font_description_free(font);
307 if(auto color = cell->foregroundColor(true)) {
308 auto gdkColor = CreateColor(color);
309 if(settings.theme.widgetColors) g_object_set(G_OBJECT(renderer), "foreground-gdk", &gdkColor, nullptr);
310 } else {
311 g_object_set(G_OBJECT(renderer), "foreground-set", false, nullptr);
312 }
313 }
314 if(auto color = cell->backgroundColor(true)) {
315 auto gdkColor = CreateColor(color);
316 if(settings.theme.widgetColors) g_object_set(G_OBJECT(renderer), "cell-background-gdk", &gdkColor, nullptr);
317 } else {
318 g_object_set(G_OBJECT(renderer), "cell-background-set", false, nullptr);
319 }
320 }
321 }
322 }
323 }
324 }
325
326 auto pTableView::_doEdit(GtkCellRendererText* gtkCellRendererText, const char* path, const char* text) -> void {
327 for(auto& column : state().columns) {
328 if(auto delegate = column->self()) {
329 if(gtkCellRendererText == GTK_CELL_RENDERER_TEXT(delegate->gtkCellText)) {
330 auto row = toNatural(path);
331 if(auto item = self().item(row)) {
332 if(auto cell = item->cell(column->offset())) {
333 if(string{text} != cell->state.text) {
334 cell->setText(text);
335 if(!locked()) self().doEdit(cell);
336 }
337 return;
338 }
339 }
340 }
341 }
342 }
343 }
344
345 auto pTableView::_doEvent(GdkEventButton* gdkEvent) -> s32 {
346 GtkTreePath* gtkRow = nullptr;
347 GtkTreeViewColumn* gtkColumn = nullptr;
348 gtk_tree_view_get_path_at_pos(gtkTreeView, gdkEvent->x, gdkEvent->y, &gtkRow, &gtkColumn, nullptr, nullptr);
349
350 if(gdkEvent->type == GDK_BUTTON_PRESS) {
351 //detect when the empty space of the GtkTreeView is clicked; and clear the selection
352 if(!gtkRow) {
353 //the first time a GtkTreeView widget is clicked, even if the empty space of the widget is clicked,
354 //a "changed" signal will be sent after the "button-press-event", to activate the first item in the tree
355 //this is undesirable, so set a flag to undo the next selection change during the "changed" signal
356 suppressChange = true;
357 if(gtk_tree_selection_count_selected_rows(gtkTreeSelection) > 0) {
358 gtk_tree_selection_unselect_all(gtkTreeSelection);
359 for(auto& item : state().items) item->setSelected(false);
360 self().doChange();
361 return true;
362 }
363 }
364
365 if(gdkEvent->button == 3) {
366 //multi-selection mode:
367 //if multiple items are selected, and one item is right-clicked on (for a context menu), GTK clears selection on all other items
368 //block this behavior so that onContext() handler can work on more than one selected item at a time
369 if(gtkRow && gtk_tree_selection_path_is_selected(gtkTreeSelection, gtkRow)) return true;
370 }
371 }
372
373 if(gdkEvent->type == GDK_BUTTON_RELEASE) {
374 suppressChange = false;
375 if(gdkEvent->button == 3) {
376 //handle action during right-click release; as button-press-event is sent prior to selection update
377 //without this, the callback handler would see the previous selection state instead
378 if(gtkRow && gtkColumn) {
379 auto path = gtk_tree_path_to_string(gtkRow);
380 auto item = self().item(toNatural(path));
381 auto cell = item.cell(0);
382 for(auto& column : state().columns) {
383 if(auto self = column->self()) {
384 if(self->gtkColumn == gtkColumn) {
385 cell = item.cell(column->offset());
386 break;
387 }
388 }
389 }
390 g_free(path);
391 self().doContext(cell);
392 } else {
393 self().doContext(TableViewCell());
394 }
395 return false;
396 }
397 }
398
399 return false;
400 }
401
402 auto pTableView::_doHeaderActivate(GtkTreeViewColumn* gtkTreeViewColumn) -> void {
403 for(auto& column : state().columns) {
404 if(auto delegate = column->self()) {
405 if(gtkTreeViewColumn == delegate->gtkColumn) {
406 if(!locked()) self().doSort(column);
407 return;
408 }
409 }
410 }
411 }
412
413 auto pTableView::_doKeyPress(GdkEventKey* event) -> bool {
414 //TODO: GCC 8.2.0 on FreeBSD 12.0 (amd64) seems to miscompile this function periodically to always return true.
415 return false;
416
417 if(state().batchable && event->type == GDK_KEY_PRESS) {
418 //when using keyboard to activate tree view items in GTK_SELECTION_MULTIPLE mode, GTK will deselect all but the last item
419 //this code detects said case, blocks the key from being propagated, and calls the activate callback directly
420 //the result is that the enter key can be used to activate multiple selected items at a time
421 //there are four ways to activate items via the keyboard in GTK, so we have to detect all of them here
422 u32 modifiers = event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_SUPER_MASK); //ignore other modifiers (eg mouse buttons)
423 if((event->keyval == GDK_KEY_Return && !modifiers)
424 || (event->keyval == GDK_KEY_KP_Enter && !modifiers)
425 || (event->keyval == GDK_KEY_space && !modifiers)
426 || (event->keyval == GDK_KEY_space && modifiers == GDK_SHIFT_MASK)
427 ) {
428 _doActivate();
429 return true; //block GTK from handling this keypress
430 }
431 }
432 return false; //allow GTK to handle this keypress
433 }
434
435 //GtkTreeView::cursor-changed and GtkTreeSelection::changed do not send signals for changes during rubber-banding selection
436 //so here we capture motion-notify-event, and if the selections have changed, invoke TableView::onChange
437 auto pTableView::_doMouseMove() -> s32 {
438 if(gtk_tree_view_is_rubber_banding_active(gtkTreeView)) {
439 if(!locked()) _updateSelected();
440 }
441 return false;
442 }
443
444 auto pTableView::_doToggle(GtkCellRendererToggle* gtkCellRendererToggle, const char* path) -> void {
445 for(auto& column : state().columns) {
446 if(auto delegate = column->self()) {
447 if(gtkCellRendererToggle == GTK_CELL_RENDERER_TOGGLE(delegate->gtkCellToggle)) {
448 auto row = toNatural(path);
449 if(auto item = self().item(row)) {
450 if(auto cell = item->cell(column->offset())) {
451 //GTK+ sends the "toggled" signal *before* changing the currently selected item
452 //if TableView::doToggle calls TableView::selected(), it will retrieve the old state instead
453 //gtk_tree_selection_get_selected_rows() will also report incorrectly, so _updateSelected() cannot be used here
454 //note: this hack here also works correctly in batchable (multi-selection) mode, so it seems safe in practice
455 for(auto& item : state().items) item->state.selected = item->offset() == row;
456
457 cell->setChecked(!cell->checked());
458 if(!locked()) self().doToggle(cell);
459 return;
460 }
461 }
462 }
463 }
464 }
465 }
466
467 //the rules hint draws each row with alternating background colors
468 //this isn't currently exposed as a hiro API call, so try and determine if we should apply it here
469 //basically, if there's two or more columns and no custom colors applied, then we do so
470 auto pTableView::_updateRulesHint() -> void {
471 bool rules = false; //true
472 if(state().backgroundColor) rules = false;
473 if(state().foregroundColor) rules = false;
474 if(state().columns.size() <= 1) rules = false;
475 gtk_tree_view_set_rules_hint(gtkTreeView, rules);
476 }
477
478 //compare currently selected items to previously selected items
479 //if different, invoke the onChange callback unless locked, and cache current selection
480 //this prevents firing an onChange event when the actual selection has not changed
481 //this is particularly important for the motion-notify-event binding
482 auto pTableView::_updateSelected() -> void {
483 if(suppressChange) {
484 suppressChange = false;
485 gtk_tree_selection_unselect_all(gtkTreeSelection);
486 return;
487 }
488
489 std::vector<u32> selected;
490
491 GList* list = gtk_tree_selection_get_selected_rows(gtkTreeSelection, &gtkTreeModel);
492 GList* p = list;
493
494 while(p) {
495 GtkTreeIter iter;
496 if(gtk_tree_model_get_iter(gtkTreeModel, &iter, (GtkTreePath*)p->data)) {
497 char* pathname = gtk_tree_model_get_string_from_iter(gtkTreeModel, &iter);
498 u32 selection = toNatural(pathname);
499 g_free(pathname);
500 selected.push_back(selection);
501 }
502 p = p->next;
503 }
504
505 g_list_foreach(list, (GFunc)gtk_tree_path_free, nullptr);
506 g_list_free(list);
507
508 bool identical = selected.size() == currentSelection.size();
509 if(identical) {
510 for(auto n : range(selected.size())) {
511 if(selected[n] != currentSelection[n]) {
512 identical = false;
513 break;
514 }
515 }
516 }
517 if(identical) return;
518
519 currentSelection = selected;
520 for(auto& item : state().items) item->state.selected = false;
521 for(auto& position : currentSelection) {
522 if(position >= self().itemCount()) continue;
523 self().item(position)->state.selected = true;
524 }
525
526 if(!locked()) self().doChange();
527 }
528
529 auto pTableView::_width(u32 column) -> u32 {
530 if(auto width = self().column(column).width()) return width;
531 u32 width = 1;
532 if(!self().column(column).visible()) return width;
533 if(self().headered()) width = max(width, _columnWidth(column));
534 for(auto row : range(self().itemCount())) {
535 width = max(width, _cellWidth(row, column));
536 }
537 return width;
538 }
539
540 }
541
542 #endif