ui/gtk/tree.c

changeset 33
458831c574f4
child 40
caa0df8ed095
equal deleted inserted replaced
32:9a5f47fbc5c3 33:458831c574f4
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2014 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
33 #include "../common/context.h"
34 #include "../common/object.h"
35 #include "container.h"
36
37 #include "tree.h"
38
39
40 void* ui_strmodel_getvalue(void *elm, int column) {
41 return column == 0 ? elm : NULL;
42 }
43
44
45 UIWIDGET ui_listview_str(UiObject *obj, UiList *list, ui_callback f, void *udata) {
46 return ui_listview(obj, list, ui_strmodel_getvalue, f, udata);
47 }
48
49 UIWIDGET ui_listview_var(UiObject *obj, UiListPtr *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
50 // create treeview
51 GtkWidget *view = gtk_tree_view_new();
52 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
53 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes(NULL, renderer, "text", 0, NULL);
54 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
55
56 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
57 #ifdef UI_GTK3
58 gtk_tree_view_set_activate_on_single_click(GTK_TREE_VIEW(view), TRUE);
59 #else
60
61 #endif
62
63 UiListModel *model = ui_list_model_new(list, getvalue);
64 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(model));
65
66 // add TreeView as observer to the UiList to update the TreeView if the
67 // data changes
68 UiListView *listview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListView));
69 listview->widget = view;
70 listview->list = list;
71 listview->getvalue = getvalue;
72 list->list->observers = ui_add_observer(
73 list->list->observers,
74 (ui_callback)ui_listview_update,
75 listview);
76
77 // add callback
78 if(f) {
79 UiEventData *event = ucx_mempool_malloc(
80 obj->ctx->mempool,
81 sizeof(UiEventData));
82 event->obj = obj;
83 event->user_data = udata;
84 event->callback = f;
85 event->value = 0;
86
87 g_signal_connect(
88 view,
89 "row-activated",
90 G_CALLBACK(ui_listview_selected),
91 event);
92 }
93
94 // add widget to the current container
95 GtkWidget *scroll_area = gtk_scrolled_window_new(NULL, NULL);
96 gtk_scrolled_window_set_policy(
97 GTK_SCROLLED_WINDOW(scroll_area),
98 GTK_POLICY_AUTOMATIC,
99 GTK_POLICY_AUTOMATIC); // GTK_POLICY_ALWAYS
100 gtk_container_add(GTK_CONTAINER(scroll_area), view);
101
102 UiContainer *ct = uic_get_current_container(obj);
103 ct->add(ct, scroll_area);
104
105 return scroll_area;
106 }
107
108 UIWIDGET ui_listview(UiObject *obj, UiList *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
109 UiListPtr *listptr = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr));
110 listptr->list = list;
111 return ui_listview_var(obj, listptr, getvalue, f, udata);
112 }
113
114 UIWIDGET ui_listview_nv(UiObject *obj, char *varname, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
115 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST);
116 if(var) {
117 UiListVar *value = var->value;
118 return ui_listview_var(obj, value->listptr, getvalue, f, udata);
119 } else {
120 // TODO: error
121 }
122 return NULL;
123 }
124
125
126 void ui_listview_update(UiEvent *event, UiListView *view) {
127 UiList *list = view->list->list;
128
129 UiListModel *model = ui_list_model_new(view->list, view->getvalue);
130 gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(model));
131 }
132
133 void ui_listview_selected(
134 GtkTreeView *treeview,
135 GtkTreePath *path,
136 GtkTreeViewColumn *column,
137 UiEventData *event)
138 {
139 int *indices = gtk_tree_path_get_indices(path);
140 int depth = gtk_tree_path_get_depth(path);
141 if(depth == 0) {
142 fprintf(stderr, "UiError: ui_treeview_selected: depth == 0\n");
143 return;
144 }
145 GtkTreeModel *model = gtk_tree_view_get_model(treeview);
146 GtkTreeIter iter;
147 gtk_tree_model_get_iter (model, &iter, path);
148 void *value = NULL;
149 gtk_tree_model_get(model, &iter, 0, &value, -1);
150
151 UiEvent e;
152 e.obj = event->obj;
153 e.window = event->obj->window;
154 e.document = event->obj->document;
155 e.eventdata = value;
156 e.intval = indices[depth-1];
157 event->callback(&e, event->user_data);
158 }

mercurial