ui/gtk/tree.c

changeset 147
2e384acc89a6
parent 146
dd0ae1c62a72
child 150
5cee4cb5ad79
equal deleted inserted replaced
146:dd0ae1c62a72 147:2e384acc89a6
27 */ 27 */
28 28
29 #include <stdio.h> 29 #include <stdio.h>
30 #include <stdlib.h> 30 #include <stdlib.h>
31 #include <string.h> 31 #include <string.h>
32 #include <stdarg.h>
32 33
33 #include "../common/context.h" 34 #include "../common/context.h"
34 #include "../common/object.h" 35 #include "../common/object.h"
35 #include "container.h" 36 #include "container.h"
36 37
65 #endif 66 #endif
66 67
67 UiModel *model = ui_model(obj->ctx, UI_STRING, "", -1); 68 UiModel *model = ui_model(obj->ctx, UI_STRING, "", -1);
68 model->getvalue = getvalue; 69 model->getvalue = getvalue;
69 UiList *list = var->value; 70 UiList *list = var->value;
70 UiListModel *listmodel = ui_list_model_new(var, model); 71 UiListModel *listmodel = ui_list_model_new(obj, var, model);
71 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(listmodel)); 72 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(listmodel));
72 73
73 UiListView *listview = malloc(sizeof(UiListView)); 74 UiListView *listview = malloc(sizeof(UiListView));
74 listview->ctx = obj->ctx; 75 listview->obj = obj;
75 listview->widget = view; 76 listview->widget = view;
76 listview->var = var; 77 listview->var = var;
77 listview->model = model; 78 listview->model = model;
78 g_signal_connect( 79 g_signal_connect(
79 view, 80 view,
133 // TODO: error 134 // TODO: error
134 } 135 }
135 return NULL; 136 return NULL;
136 } 137 }
137 138
139 static void drag_begin(GtkWidget *widget, GdkDragContext *context, gpointer udata) {
140 printf("drag begin\n");
141
142 }
143
144 static void drag_end(
145 GtkWidget *widget,
146 GdkDragContext *context,
147 guint time,
148 gpointer udata)
149 {
150 printf("drag end\n");
151
152 }
153
154 static gboolean drag_failed(
155 GtkWidget *widget,
156 GdkDragContext *context,
157 GtkDragResult result,
158 gpointer udata)
159 {
160 printf("drag failed: %d\n", result);
161
162 }
163
164 static GtkTargetEntry targetentries[] =
165 {
166 { "STRING", 0, 0 },
167 { "text/plain", 0, 1 },
168 { "text/uri-list", 0, 2 },
169 };
138 170
139 UIWIDGET ui_table_var(UiObject *obj, UiVar *var, UiModel *model, UiListCallbacks cb) { 171 UIWIDGET ui_table_var(UiObject *obj, UiVar *var, UiModel *model, UiListCallbacks cb) {
140 // create treeview 172 // create treeview
141 GtkWidget *view = gtk_tree_view_new(); 173 GtkWidget *view = gtk_tree_view_new();
174
142 int addi = 0; 175 int addi = 0;
143 for(int i=0;i<model->columns;i++) { 176 for(int i=0;i<model->columns;i++) {
144 GtkTreeViewColumn *column = NULL; 177 GtkTreeViewColumn *column = NULL;
145 if(model->types[i] == UI_ICON_TEXT) { 178 if(model->types[i] == UI_ICON_TEXT) {
146 column = gtk_tree_view_column_new(); 179 column = gtk_tree_view_column_new();
176 #else 209 #else
177 210
178 #endif 211 #endif
179 212
180 UiList *list = var->value; 213 UiList *list = var->value;
181 UiListModel *listmodel = ui_list_model_new(var, model); 214 UiListModel *listmodel = ui_list_model_new(obj, var, model);
182 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(listmodel)); 215 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(listmodel));
183 216
217 //g_signal_connect(view, "drag-begin", G_CALLBACK(drag_begin), NULL);
218 //g_signal_connect(view, "drag-end", G_CALLBACK(drag_end), NULL);
219 //g_signal_connect(view, "drag-failed", G_CALLBACK(drag_failed), NULL);
220
184 // add TreeView as observer to the UiList to update the TreeView if the 221 // add TreeView as observer to the UiList to update the TreeView if the
185 // data changes 222 // data changes
186 UiListView *tableview = malloc(sizeof(UiListView)); 223 UiListView *tableview = malloc(sizeof(UiListView));
187 tableview->ctx = obj->ctx; 224 tableview->obj = obj;
188 tableview->widget = view; 225 tableview->widget = view;
189 tableview->var = var; 226 tableview->var = var;
190 tableview->model = model; 227 tableview->model = model;
191 g_signal_connect( 228 g_signal_connect(
192 view, 229 view,
258 // TODO: error 295 // TODO: error
259 } 296 }
260 return NULL; 297 return NULL;
261 } 298 }
262 299
300 GtkWidget* ui_get_tree_widget(UIWIDGET widget) {
301 GList *c = gtk_container_get_children(GTK_CONTAINER(widget));
302 if(c) {
303 return c->data;
304 }
305 return NULL;
306 }
307
308 static char** targets2array(char *target0, va_list ap, int *nelm) {
309 int al = 16;
310 char **targets = calloc(16, sizeof(char*));
311 targets[0] = target0;
312
313 int i = 1;
314 char *target;
315 while((target = va_arg(ap, char*)) != NULL) {
316 if(i >= al) {
317 al *= 2;
318 targets = realloc(targets, al*sizeof(char*));
319 }
320 targets[i] = target;
321 i++;
322 }
323
324 *nelm = i;
325 return targets;
326 }
327
328 static GtkTargetEntry* targetstr2gtktargets(char **str, int nelm) {
329 GtkTargetEntry *targets = calloc(nelm, sizeof(GtkTargetEntry));
330 for(int i=0;i<nelm;i++) {
331 targets[i].target = str[i];
332 }
333 return targets;
334 }
335
336 void ui_table_dragsource(UIWIDGET tablewidget, int actions, char *target0, ...) {
337 va_list ap;
338 int nelm;
339 char **targets = targets2array(target0, ap, &nelm);
340 va_end(ap);
341 ui_table_dragsource_a(tablewidget, actions, targets, nelm);
342 free(targets);
343 }
344
345 void ui_table_dragsource_a(UIWIDGET tablewidget, int actions, char **targets, int nelm) {
346 GtkTargetEntry* t = targetstr2gtktargets(targets, nelm);
347 gtk_tree_view_enable_model_drag_source(
348 GTK_TREE_VIEW(ui_get_tree_widget(tablewidget)),
349 GDK_BUTTON1_MASK,
350 t,
351 nelm,
352 GDK_ACTION_COPY|GDK_ACTION_MOVE|GDK_ACTION_LINK);
353 free(t);
354 }
355
356 void ui_table_dragdest(UIWIDGET tablewidget, int actions, char *target0, ...) {
357 va_list ap;
358 int nelm;
359 char **targets = targets2array(target0, ap, &nelm);
360 va_end(ap);
361 ui_table_dragdest_a(tablewidget, actions, targets, nelm);
362 free(targets);
363 }
364
365 void ui_table_dragdest_a(UIWIDGET tablewidget, int actions, char **targets, int nelm) {
366 GtkTargetEntry* t = targetstr2gtktargets(targets, nelm);
367 gtk_tree_view_enable_model_drag_dest(
368 GTK_TREE_VIEW(ui_get_tree_widget(tablewidget)),
369 t,
370 nelm,
371 GDK_ACTION_COPY|GDK_ACTION_MOVE|GDK_ACTION_LINK);
372 free(t);
373 }
374
263 void ui_listview_update(UiList *list, int i) { 375 void ui_listview_update(UiList *list, int i) {
264 UiListView *view = list->obj; 376 UiListView *view = list->obj;
265 UiListModel *model = ui_list_model_new(view->var, view->model); 377 UiListModel *model = ui_list_model_new(view->obj, view->var, view->model);
266 gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(model)); 378 gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(model));
267 // TODO: free old model 379 // TODO: free old model
268 } 380 }
269 381
270 void ui_listview_destroy(GtkWidget *w, UiListView *v) { 382 void ui_listview_destroy(GtkWidget *w, UiListView *v) {
271 ui_destroy_boundvar(v->ctx, v->var); 383 ui_destroy_boundvar(v->obj->ctx, v->var);
272 // TODO: destroy model? 384 // TODO: destroy model?
273 free(v); 385 free(v);
274 } 386 }
275 387
276 388
373 } 485 }
374 486
375 UIWIDGET ui_combobox_var(UiObject *obj, UiVar *var, ui_getvaluefunc getvalue, ui_callback f, void *udata) { 487 UIWIDGET ui_combobox_var(UiObject *obj, UiVar *var, ui_getvaluefunc getvalue, ui_callback f, void *udata) {
376 UiModel *model = ui_model(obj->ctx, UI_STRING, "", -1); 488 UiModel *model = ui_model(obj->ctx, UI_STRING, "", -1);
377 model->getvalue = getvalue; 489 model->getvalue = getvalue;
378 UiListModel *listmodel = ui_list_model_new(var, model); 490 UiListModel *listmodel = ui_list_model_new(obj, var, model);
379 491
380 GtkWidget *combobox = ui_create_combobox(obj, listmodel, f, udata); 492 GtkWidget *combobox = ui_create_combobox(obj, listmodel, f, udata);
381 UiContainer *ct = uic_get_current_container(obj); 493 UiContainer *ct = uic_get_current_container(obj);
382 ct->add(ct, combobox, FALSE); 494 ct->add(ct, combobox, FALSE);
383 } 495 }
384 496
385 GtkWidget* ui_create_combobox(UiObject *obj, UiListModel *model, ui_callback f, void *udata) { 497 GtkWidget* ui_create_combobox(UiObject *obj, UiListModel *model, ui_callback f, void *udata) {
386 GtkWidget *combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model)); 498 GtkWidget *combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
387 499
388 UiListView *uicbox = malloc(sizeof(UiListView)); 500 UiListView *uicbox = malloc(sizeof(UiListView));
389 uicbox->ctx = obj->ctx; 501 uicbox->obj = obj;
390 uicbox->widget = combobox; 502 uicbox->widget = combobox;
391 uicbox->var = model->var; 503 uicbox->var = model->var;
392 uicbox->model = model->info; 504 uicbox->model = model->model;
393 505
394 g_signal_connect( 506 g_signal_connect(
395 combobox, 507 combobox,
396 "destroy", 508 "destroy",
397 G_CALLBACK(ui_listview_destroy), 509 G_CALLBACK(ui_listview_destroy),
440 e->callback(&event, e->userdata); 552 e->callback(&event, e->userdata);
441 } 553 }
442 554
443 void ui_combobox_modelupdate(UiList *list, int i) { 555 void ui_combobox_modelupdate(UiList *list, int i) {
444 UiListView *view = list->obj; 556 UiListView *view = list->obj;
445 UiListModel *model = ui_list_model_new(view->var, view->model); 557 UiListModel *model = ui_list_model_new(view->obj, view->var, view->model);
446 gtk_combo_box_set_model(GTK_COMBO_BOX(view->widget), GTK_TREE_MODEL(model)); 558 gtk_combo_box_set_model(GTK_COMBO_BOX(view->widget), GTK_TREE_MODEL(model));
447 } 559 }
448 560

mercurial