ui/gtk/tree.c

changeset 42
29b2821d1262
parent 41
394f3b06dba1
child 52
25e5390cce41
equal deleted inserted replaced
41:394f3b06dba1 42:29b2821d1262
76 (ui_callback)ui_listview_update, 76 (ui_callback)ui_listview_update,
77 listview); 77 listview);
78 78
79 // add callback 79 // add callback
80 if(f) { 80 if(f) {
81 UiEventData *event = ucx_mempool_malloc( 81 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiEventData));
82 obj->ctx->mempool,
83 sizeof(UiEventData));
84 event->obj = obj; 82 event->obj = obj;
85 event->user_data = udata; 83 event->userdata = udata;
86 event->callback = f; 84 event->activate = f;
87 event->value = 0; 85 event->selection = NULL;
88 86
89 g_signal_connect( 87 g_signal_connect(
90 view, 88 view,
91 "row-activated", 89 "row-activated",
92 G_CALLBACK(ui_listview_selected), 90 G_CALLBACK(ui_listview_activate_event),
93 event); 91 event);
94 } 92 }
95 93
96 // add widget to the current container 94 // add widget to the current container
97 GtkWidget *scroll_area = gtk_scrolled_window_new(NULL, NULL); 95 GtkWidget *scroll_area = gtk_scrolled_window_new(NULL, NULL);
140 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column); 138 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
141 } 139 }
142 140
143 //gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE); 141 //gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
144 #ifdef UI_GTK3 142 #ifdef UI_GTK3
145 gtk_tree_view_set_activate_on_single_click(GTK_TREE_VIEW(view), TRUE); 143 //gtk_tree_view_set_activate_on_single_click(GTK_TREE_VIEW(view), TRUE);
146 #else 144 #else
147 145
148 #endif 146 #endif
149 147
150 UiListModel *model = ui_list_model_new(list, modelinfo); 148 UiListModel *model = ui_list_model_new(list, modelinfo);
160 list->list->observers, 158 list->list->observers,
161 (ui_callback)ui_listview_update, 159 (ui_callback)ui_listview_update,
162 tableview); 160 tableview);
163 161
164 // add callback 162 // add callback
165 if(modelinfo->callback) { 163 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData));
166 UiEventData *event = ucx_mempool_malloc( 164 event->obj = obj;
167 obj->ctx->mempool, 165 event->activate = modelinfo->activate;
168 sizeof(UiEventData)); 166 event->selection = modelinfo->selection;
169 event->obj = obj; 167 event->userdata = modelinfo->userdata;
170 event->user_data = modelinfo->userdata; 168 if(modelinfo->activate) {
171 event->callback = modelinfo->callback;
172 event->value = 0;
173
174 g_signal_connect( 169 g_signal_connect(
175 view, 170 view,
176 "row-activated", 171 "row-activated",
177 G_CALLBACK(ui_listview_selected), 172 G_CALLBACK(ui_listview_activate_event),
178 event); 173 event);
179 } 174 }
175 if(modelinfo->selection) {
176 GtkTreeSelection *selection = gtk_tree_view_get_selection(
177 GTK_TREE_VIEW(view));
178 g_signal_connect(
179 selection,
180 "changed",
181 G_CALLBACK(ui_listview_selection_event),
182 event);
183 }
184 // TODO: destroy callback
185
186 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(view));
187 gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
180 188
181 // add widget to the current container 189 // add widget to the current container
182 GtkWidget *scroll_area = gtk_scrolled_window_new(NULL, NULL); 190 GtkWidget *scroll_area = gtk_scrolled_window_new(NULL, NULL);
183 gtk_scrolled_window_set_policy( 191 gtk_scrolled_window_set_policy(
184 GTK_SCROLLED_WINDOW(scroll_area), 192 GTK_SCROLLED_WINDOW(scroll_area),
210 } 218 }
211 219
212 220
213 221
214 void ui_listview_update(UiEvent *event, UiTableView *view) { 222 void ui_listview_update(UiEvent *event, UiTableView *view) {
215 UiList *list = view->list->list;
216 UiListModel *model = ui_list_model_new(view->list, view->modelinfo); 223 UiListModel *model = ui_list_model_new(view->list, view->modelinfo);
217 gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(model)); 224 gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(model));
218 225
219 // TODO: free old model 226 // TODO: free old model
220 } 227 }
221 228
222 void ui_listview_selected( 229 void ui_listview_activate_event(
223 GtkTreeView *treeview, 230 GtkTreeView *treeview,
224 GtkTreePath *path, 231 GtkTreePath *path,
225 GtkTreeViewColumn *column, 232 GtkTreeViewColumn *column,
226 UiEventData *event) 233 UiTreeEventData *event)
227 { 234 {
228 int *indices = gtk_tree_path_get_indices(path); 235 UiListSelection *selection = ui_listview_selection(
229 int depth = gtk_tree_path_get_depth(path); 236 gtk_tree_view_get_selection(treeview),
230 if(depth == 0) { 237 event);
231 fprintf(stderr, "UiError: ui_treeview_selected: depth == 0\n");
232 return;
233 }
234 GtkTreeModel *model = gtk_tree_view_get_model(treeview);
235 GtkTreeIter iter;
236 gtk_tree_model_get_iter (model, &iter, path);
237 void *value = NULL;
238 gtk_tree_model_get(model, &iter, 0, &value, -1);
239 238
240 UiEvent e; 239 UiEvent e;
241 e.obj = event->obj; 240 e.obj = event->obj;
242 e.window = event->obj->window; 241 e.window = event->obj->window;
243 e.document = event->obj->document; 242 e.document = event->obj->document;
244 e.eventdata = value; 243 e.eventdata = selection;
245 e.intval = indices[depth-1]; 244 e.intval = selection->count > 0 ? selection->rows[0] : -1;
246 event->callback(&e, event->user_data); 245 event->activate(&e, event->userdata);
247 } 246
247 free(selection);
248 }
249
250 void ui_listview_selection_event(
251 GtkTreeSelection *treeselection,
252 UiTreeEventData *event)
253 {
254 UiListSelection *selection = ui_listview_selection(treeselection, event);
255
256 UiEvent e;
257 e.obj = event->obj;
258 e.window = event->obj->window;
259 e.document = event->obj->document;
260 e.eventdata = selection;
261 e.intval = selection->count > 0 ? selection->rows[0] : -1;
262 event->selection(&e, event->userdata);
263
264 free(selection);
265 }
266
267 UiListSelection* ui_listview_selection(
268 GtkTreeSelection *selection,
269 UiTreeEventData *event)
270 {
271 GList *rows = gtk_tree_selection_get_selected_rows(selection, NULL);
272
273 UiListSelection *ls = malloc(sizeof(UiListSelection));
274 ls->count = g_list_length(rows);
275 ls->rows = calloc(ls->count, sizeof(int));
276 GList *r = rows;
277 int i = 0;
278 while(r) {
279 GtkTreePath *path = r->data;
280 ls->rows[i] = ui_tree_path_list_index(path);
281 r = r->next;
282 i++;
283 }
284 return ls;
285 }
286
287 int ui_tree_path_list_index(GtkTreePath *path) {
288 int depth = gtk_tree_path_get_depth(path);
289 if(depth == 0) {
290 fprintf(stderr, "UiError: treeview selection: depth == 0\n");
291 return -1;
292 }
293 int *indices = gtk_tree_path_get_indices(path);
294 return indices[depth - 1];
295 }

mercurial