ui/gtk/tree.c

changeset 46
31bc86844659
parent 45
ab71409644b0
child 47
3ac472683246
equal deleted inserted replaced
45:ab71409644b0 46:31bc86844659
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdarg.h>
33
34 #include "../common/context.h"
35 #include "../common/object.h"
36 #include "container.h"
37
38 #include "tree.h"
39 #include "image.h"
40
41
42 void* ui_strmodel_getvalue(void *elm, int column) {
43 return column == 0 ? elm : NULL;
44 }
45
46 static GtkListStore* create_list_store(UiList *list, UiModel *model) {
47 int columns = model->columns;
48 GType types[2*columns];
49 int c = 0;
50 for(int i=0;i<columns;i++,c++) {
51 switch(model->types[i]) {
52 case UI_STRING:
53 case UI_STRING_FREE: types[c] = G_TYPE_STRING; break;
54 case UI_INTEGER: types[c] = G_TYPE_INT; break;
55 case UI_ICON: types[c] = G_TYPE_OBJECT; break;
56 case UI_ICON_TEXT:
57 case UI_ICON_TEXT_FREE: {
58 types[c] = G_TYPE_OBJECT;
59 types[++c] = G_TYPE_STRING;
60 }
61 }
62 }
63
64 GtkListStore *store = gtk_list_store_newv(c, types);
65
66 if(list) {
67 void *elm = list->first(list);
68 while(elm) {
69 // insert new row
70 GtkTreeIter iter;
71 gtk_list_store_insert (store, &iter, -1);
72
73 // set column values
74 int c = 0;
75 for(int i=0;i<columns;i++,c++) {
76 void *data = model->getvalue(elm, c);
77
78 GValue value = G_VALUE_INIT;
79 switch(model->types[i]) {
80 case UI_STRING:
81 case UI_STRING_FREE: {
82 g_value_init(&value, G_TYPE_STRING);
83 g_value_set_string(&value, data);
84 if(model->types[i] == UI_STRING_FREE) {
85 free(data);
86 }
87 break;
88 }
89 case UI_INTEGER: {
90 g_value_init(&value, G_TYPE_INT);
91 int *intptr = data;
92 g_value_set_int(&value, *intptr);
93 break;
94 }
95 case UI_ICON: {
96 g_value_init(&value, G_TYPE_OBJECT);
97 UiIcon *icon = data;
98 #if GTK_MAJOR_VERSION >= 4
99 g_value_set_object(&value, icon->info); // TODO: does this work?
100 #else
101 if(!icon->pixbuf && icon->info) {
102 GError *error = NULL;
103 GdkPixbuf *pixbuf = gtk_icon_info_load_icon(icon->info, &error);
104 icon->pixbuf = pixbuf;
105 }
106
107 if(icon->pixbuf) {
108 g_value_set_object(&value, icon->pixbuf);
109 }
110 #endif
111 break;
112 }
113 case UI_ICON_TEXT:
114 case UI_ICON_TEXT_FREE: {
115 UiIcon *icon = data;
116 #if GTK_MAJOR_VERSION >= 4
117 GValue iconvalue = G_VALUE_INIT;
118 g_value_init(&iconvalue, G_TYPE_OBJECT);
119 g_value_set_object(&iconvalue, ui_icon_pixbuf(icon));
120 gtk_list_store_set_value(store, &iter, c, &iconvalue);
121 #else
122 GValue pixbufvalue = G_VALUE_INIT;
123 if(!icon->pixbuf && icon->info) {
124 GError *error = NULL;
125 GdkPixbuf *pixbuf = gtk_icon_info_load_icon(icon->info, &error);
126 icon->pixbuf = pixbuf;
127 }
128 g_value_init(&pixbufvalue, G_TYPE_OBJECT);
129 g_value_set_object(&pixbufvalue, icon->pixbuf);
130 gtk_list_store_set_value(store, &iter, c, &pixbufvalue);
131 #endif
132 c++;
133
134 char *str = model->getvalue(elm, c);
135 g_value_init(&value, G_TYPE_STRING);
136 g_value_set_string(&value, str);
137 if(model->types[i] == UI_ICON_TEXT_FREE) {
138 free(str);
139 }
140 break;
141 }
142 }
143
144 gtk_list_store_set_value(store, &iter, c, &value);
145 }
146
147 // next row
148 elm = list->next(list);
149 }
150 }
151
152 return store;
153 }
154
155
156 UIWIDGET ui_listview_create(UiObject *obj, UiListArgs args) {
157 UiObject* current = uic_current_obj(obj);
158
159 // create treeview
160 GtkWidget *view = gtk_tree_view_new();
161 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
162 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes(NULL, renderer, "text", 0, NULL);
163 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
164
165 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
166 #ifdef UI_GTK3
167 #if GTK_MINOR_VERSION >= 8
168 gtk_tree_view_set_activate_on_single_click(GTK_TREE_VIEW(view), TRUE);
169 #else
170 // TODO: implement for older gtk3
171 #endif
172 #else
173 // TODO: implement for gtk2
174 #endif
175
176 UiModel *model = ui_model(obj->ctx, UI_STRING, "", -1);
177 model->getvalue = args.getvalue;
178
179 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST);
180
181 UiList *list = var ? var->value : NULL;
182 GtkListStore *listmodel = create_list_store(list, model);
183 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(listmodel));
184
185 UiListView *listview = malloc(sizeof(UiListView));
186 listview->obj = obj;
187 listview->widget = view;
188 listview->var = var;
189 listview->model = model;
190 g_signal_connect(
191 view,
192 "destroy",
193 G_CALLBACK(ui_listview_destroy),
194 listview);
195
196 // bind var
197 list->update = ui_listview_update;
198 list->getselection = ui_listview_getselection;
199 list->obj = listview;
200
201 // add callback
202 if(args.onactivate) {
203 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData));
204 event->obj = obj;
205 event->activatedata = args.onactivatedata;
206 event->activate = args.onactivate;
207 event->selection = NULL;
208
209 g_signal_connect(
210 view,
211 "row-activated",
212 G_CALLBACK(ui_listview_activate_event),
213 event);
214 }
215
216 // add widget to the current container
217 GtkWidget *scroll_area = SCROLLEDWINDOW_NEW();
218 gtk_scrolled_window_set_policy(
219 GTK_SCROLLED_WINDOW(scroll_area),
220 GTK_POLICY_AUTOMATIC,
221 GTK_POLICY_AUTOMATIC); // GTK_POLICY_ALWAYS
222 SCROLLEDWINDOW_SET_CHILD(scroll_area, view);
223
224 UI_APPLY_LAYOUT1(current, args);
225 current->container->add(current->container, scroll_area, FALSE);
226
227 // ct->current should point to view, not scroll_area, to make it possible
228 // to add a context menu
229 current->container->current = view;
230
231 return scroll_area;
232 }
233
234 /*
235 static void drag_begin(GtkWidget *widget, GdkDragContext *context, gpointer udata) {
236 printf("drag begin\n");
237
238 }
239
240 static void drag_end(
241 GtkWidget *widget,
242 GdkDragContext *context,
243 guint time,
244 gpointer udata)
245 {
246 printf("drag end\n");
247
248 }
249 */
250
251 /*
252 static GtkTargetEntry targetentries[] =
253 {
254 { "STRING", 0, 0 },
255 { "text/plain", 0, 1 },
256 { "text/uri-list", 0, 2 },
257 };
258 */
259
260 UIWIDGET ui_table_create(UiObject *obj, UiListArgs args) {
261 UiObject* current = uic_current_obj(obj);
262
263 // create treeview
264 GtkWidget *view = gtk_tree_view_new();
265
266 UiModel *model = args.model;
267 int columns = model ? model->columns : 0;
268
269 int addi = 0;
270 for(int i=0;i<columns;i++) {
271 GtkTreeViewColumn *column = NULL;
272 if(model->types[i] == UI_ICON_TEXT) {
273 column = gtk_tree_view_column_new();
274 gtk_tree_view_column_set_title(column, model->titles[i]);
275
276 GtkCellRenderer *iconrenderer = gtk_cell_renderer_pixbuf_new();
277 GtkCellRenderer *textrenderer = gtk_cell_renderer_text_new();
278
279 gtk_tree_view_column_pack_end(column, textrenderer, TRUE);
280 gtk_tree_view_column_pack_start(column, iconrenderer, FALSE);
281
282
283 gtk_tree_view_column_add_attribute(column, iconrenderer, "pixbuf", i);
284 gtk_tree_view_column_add_attribute(column, textrenderer, "text", i+1);
285
286 addi++;
287 } else if (model->types[i] == UI_ICON) {
288 GtkCellRenderer *iconrenderer = gtk_cell_renderer_pixbuf_new();
289 column = gtk_tree_view_column_new_with_attributes(
290 model->titles[i],
291 iconrenderer,
292 "pixbuf",
293 i + addi,
294 NULL);
295 } else {
296 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
297 column = gtk_tree_view_column_new_with_attributes(
298 model->titles[i],
299 renderer,
300 "text",
301 i + addi,
302 NULL);
303 }
304 gtk_tree_view_column_set_resizable(column, TRUE);
305 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
306 }
307
308 //gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
309 #ifdef UI_GTK3
310 //gtk_tree_view_set_activate_on_single_click(GTK_TREE_VIEW(view), TRUE);
311 #else
312
313 #endif
314
315 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST);
316
317 UiList *list = var ? var->value : NULL;
318 GtkListStore *listmodel = create_list_store(list, model);
319 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(listmodel));
320
321 //g_signal_connect(view, "drag-begin", G_CALLBACK(drag_begin), NULL);
322 //g_signal_connect(view, "drag-end", G_CALLBACK(drag_end), NULL);
323
324 // add TreeView as observer to the UiList to update the TreeView if the
325 // data changes
326 UiListView *tableview = malloc(sizeof(UiListView));
327 tableview->obj = obj;
328 tableview->widget = view;
329 tableview->var = var;
330 tableview->model = model;
331 g_signal_connect(
332 view,
333 "destroy",
334 G_CALLBACK(ui_listview_destroy),
335 tableview);
336
337 // bind var
338 list->update = ui_listview_update;
339 list->getselection = ui_listview_getselection;
340 list->obj = tableview;
341
342 // add callback
343 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData));
344 event->obj = obj;
345 event->activate = args.onactivate;
346 event->selection = args.onselection;
347 event->activatedata = args.onactivatedata;
348 event->selectiondata = args.onselectiondata;
349 if(args.onactivate) {
350 g_signal_connect(
351 view,
352 "row-activated",
353 G_CALLBACK(ui_listview_activate_event),
354 event);
355 }
356 if(args.onselection) {
357 GtkTreeSelection *selection = gtk_tree_view_get_selection(
358 GTK_TREE_VIEW(view));
359 g_signal_connect(
360 selection,
361 "changed",
362 G_CALLBACK(ui_listview_selection_event),
363 event);
364 }
365 // TODO: destroy callback
366
367
368 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(view));
369 gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
370
371 // add widget to the current container
372 GtkWidget *scroll_area = SCROLLEDWINDOW_NEW();
373 gtk_scrolled_window_set_policy(
374 GTK_SCROLLED_WINDOW(scroll_area),
375 GTK_POLICY_AUTOMATIC,
376 GTK_POLICY_AUTOMATIC); // GTK_POLICY_ALWAYS
377 SCROLLEDWINDOW_SET_CHILD(scroll_area, view);
378
379 UI_APPLY_LAYOUT1(current, args);
380 current->container->add(current->container, scroll_area, FALSE);
381
382 // ct->current should point to view, not scroll_area, to make it possible
383 // to add a context menu
384 current->container->current = view;
385
386 return scroll_area;
387 }
388
389
390 GtkWidget* ui_get_tree_widget(UIWIDGET widget) {
391 return SCROLLEDWINDOW_GET_CHILD(widget);
392 }
393
394 static char** targets2array(char *target0, va_list ap, int *nelm) {
395 int al = 16;
396 char **targets = calloc(16, sizeof(char*));
397 targets[0] = target0;
398
399 int i = 1;
400 char *target;
401 while((target = va_arg(ap, char*)) != NULL) {
402 if(i >= al) {
403 al *= 2;
404 targets = realloc(targets, al*sizeof(char*));
405 }
406 targets[i] = target;
407 i++;
408 }
409
410 *nelm = i;
411 return targets;
412 }
413
414 /*
415 static GtkTargetEntry* targetstr2gtktargets(char **str, int nelm) {
416 GtkTargetEntry *targets = calloc(nelm, sizeof(GtkTargetEntry));
417 for(int i=0;i<nelm;i++) {
418 targets[i].target = str[i];
419 }
420 return targets;
421 }
422 */
423
424 void ui_table_dragsource(UIWIDGET tablewidget, int actions, char *target0, ...) {
425 va_list ap;
426 va_start(ap, target0);
427 int nelm;
428 char **targets = targets2array(target0, ap, &nelm);
429 va_end(ap);
430
431 // disabled
432 //ui_table_dragsource_a(tablewidget, actions, targets, nelm);
433
434 free(targets);
435 }
436
437 /*
438 void ui_table_dragsource_a(UIWIDGET tablewidget, int actions, char **targets, int nelm) {
439 GtkTargetEntry* t = targetstr2gtktargets(targets, nelm);
440 gtk_tree_view_enable_model_drag_source(
441 GTK_TREE_VIEW(ui_get_tree_widget(tablewidget)),
442 GDK_BUTTON1_MASK,
443 t,
444 nelm,
445 GDK_ACTION_COPY|GDK_ACTION_MOVE|GDK_ACTION_LINK);
446 free(t);
447 }
448
449
450 void ui_table_dragdest(UIWIDGET tablewidget, int actions, char *target0, ...) {
451 va_list ap;
452 va_start(ap, target0);
453 int nelm;
454 char **targets = targets2array(target0, ap, &nelm);
455 va_end(ap);
456 ui_table_dragdest_a(tablewidget, actions, targets, nelm);
457 free(targets);
458 }
459
460 void ui_table_dragdest_a(UIWIDGET tablewidget, int actions, char **targets, int nelm) {
461 GtkTargetEntry* t = targetstr2gtktargets(targets, nelm);
462 gtk_tree_view_enable_model_drag_dest(
463 GTK_TREE_VIEW(ui_get_tree_widget(tablewidget)),
464 t,
465 nelm,
466 GDK_ACTION_COPY|GDK_ACTION_MOVE|GDK_ACTION_LINK);
467 free(t);
468 }
469 */
470
471 void ui_listview_update(UiList *list, int i) {
472 UiListView *view = list->obj;
473 GtkListStore *store = create_list_store(list, view->model);
474 gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(store));
475 g_object_unref(G_OBJECT(store));
476 // TODO: free old model
477 }
478
479 UiListSelection ui_listview_getselection(UiList *list) {
480 UiListView *view = list->obj;
481 UiListSelection selection = ui_listview_selection(
482 gtk_tree_view_get_selection(GTK_TREE_VIEW(view->widget)),
483 NULL);
484 return selection;
485 }
486
487 void ui_listview_destroy(GtkWidget *w, UiListView *v) {
488 gtk_tree_view_set_model(GTK_TREE_VIEW(w), NULL);
489 ui_destroy_boundvar(v->obj->ctx, v->var);
490 // TODO: destroy model?
491 free(v);
492 }
493
494 void ui_combobox_destroy(GtkWidget *w, UiListView *v) {
495 gtk_combo_box_set_model(GTK_COMBO_BOX(w), NULL);
496 ui_destroy_boundvar(v->obj->ctx, v->var);
497 // TODO: destroy model?
498 free(v);
499 }
500
501
502 void ui_listview_activate_event(
503 GtkTreeView *treeview,
504 GtkTreePath *path,
505 GtkTreeViewColumn *column,
506 UiTreeEventData *event)
507 {
508 UiListSelection selection = ui_listview_selection(
509 gtk_tree_view_get_selection(treeview),
510 event);
511
512 UiEvent e;
513 e.obj = event->obj;
514 e.window = event->obj->window;
515 e.document = event->obj->ctx->document;
516 e.eventdata = &selection;
517 e.intval = selection.count > 0 ? selection.rows[0] : -1;
518 event->activate(&e, event->activatedata);
519
520 if(selection.count > 0) {
521 free(selection.rows);
522 }
523 }
524
525 void ui_listview_selection_event(
526 GtkTreeSelection *treeselection,
527 UiTreeEventData *event)
528 {
529 UiListSelection selection = ui_listview_selection(treeselection, event);
530
531 UiEvent e;
532 e.obj = event->obj;
533 e.window = event->obj->window;
534 e.document = event->obj->ctx->document;
535 e.eventdata = &selection;
536 e.intval = selection.count > 0 ? selection.rows[0] : -1;
537 event->selection(&e, event->selectiondata);
538
539 if(selection.count > 0) {
540 free(selection.rows);
541 }
542 }
543
544 UiListSelection ui_listview_selection(
545 GtkTreeSelection *selection,
546 UiTreeEventData *event)
547 {
548 GList *rows = gtk_tree_selection_get_selected_rows(selection, NULL);
549
550 UiListSelection ls;
551 ls.count = g_list_length(rows);
552 ls.rows = calloc(ls.count, sizeof(int));
553 GList *r = rows;
554 int i = 0;
555 while(r) {
556 GtkTreePath *path = r->data;
557 ls.rows[i] = ui_tree_path_list_index(path);
558 r = r->next;
559 i++;
560 }
561 return ls;
562 }
563
564 int ui_tree_path_list_index(GtkTreePath *path) {
565 int depth = gtk_tree_path_get_depth(path);
566 if(depth == 0) {
567 fprintf(stderr, "UiError: treeview selection: depth == 0\n");
568 return -1;
569 }
570 int *indices = gtk_tree_path_get_indices(path);
571 return indices[depth - 1];
572 }
573
574
575 /* --------------------------- ComboBox --------------------------- */
576
577 UIWIDGET ui_combobox_create(UiObject *obj, UiListArgs args) {
578 UiObject* current = uic_current_obj(obj);
579
580 UiModel *model = ui_model(obj->ctx, UI_STRING, "", -1);
581 model->getvalue = args.getvalue;
582
583 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST);
584
585 GtkWidget *combobox = ui_create_combobox(obj, model, var, args.onactivate, args.onactivatedata);
586 UI_APPLY_LAYOUT1(current, args);
587 current->container->add(current->container, combobox, FALSE);
588 current->container->current = combobox;
589 return combobox;
590 }
591
592 GtkWidget* ui_create_combobox(UiObject *obj, UiModel *model, UiVar *var, ui_callback f, void *udata) {
593 GtkWidget *combobox = gtk_combo_box_new();
594
595 UiListView *uicbox = malloc(sizeof(UiListView));
596 uicbox->obj = obj;
597 uicbox->widget = combobox;
598
599 UiList *list = var ? var->value : NULL;
600 GtkListStore *listmodel = create_list_store(list, model);
601
602 if(listmodel) {
603 gtk_combo_box_set_model(GTK_COMBO_BOX(combobox), GTK_TREE_MODEL(listmodel));
604 }
605
606 uicbox->var = var;
607 uicbox->model = model;
608
609 g_signal_connect(
610 combobox,
611 "destroy",
612 G_CALLBACK(ui_combobox_destroy),
613 uicbox);
614
615 // bind var
616 if(list) {
617 list->update = ui_combobox_modelupdate;
618 // TODO: combobox getselection
619 list->obj = uicbox;
620 }
621
622 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
623 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), renderer, TRUE);
624 gtk_cell_layout_set_attributes(
625 GTK_CELL_LAYOUT(combobox),
626 renderer,
627 "text",
628 0,
629 NULL);
630 gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), 0);
631
632 // add callback
633 if(f) {
634 UiEventData *event = ui_malloc(obj->ctx, sizeof(UiEventData));
635 event->obj = obj;
636 event->userdata = udata;
637 event->callback = f;
638 event->value = 0;
639 event->customdata = NULL;
640
641 g_signal_connect(
642 combobox,
643 "changed",
644 G_CALLBACK(ui_combobox_change_event),
645 event);
646 }
647
648 return combobox;
649 }
650
651 void ui_combobox_change_event(GtkComboBox *widget, UiEventData *e) {
652 UiEvent event;
653 event.obj = e->obj;
654 event.window = event.obj->window;
655 event.document = event.obj->ctx->document;
656 event.eventdata = NULL;
657 event.intval = gtk_combo_box_get_active(widget);
658 e->callback(&event, e->userdata);
659 }
660
661 void ui_combobox_modelupdate(UiList *list, int i) {
662 UiListView *view = list->obj;
663 GtkListStore *store = create_list_store(view->var->value, view->model);
664 gtk_combo_box_set_model(GTK_COMBO_BOX(view->widget), GTK_TREE_MODEL(store));
665 }
666

mercurial