ui/gtk/tree.c

changeset 140
c03c338a7dcf
parent 129
5babf09f5f19
child 142
46448d38885c
equal deleted inserted replaced
139:dbde25a5bc53 140:c03c338a7dcf
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2014 Olaf Wintermann. All rights reserved. 4 * Copyright 2017 Olaf Wintermann. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
44 44
45 UIWIDGET ui_listview_str(UiObject *obj, UiList *list, ui_callback f, void *udata) { 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); 46 return ui_listview(obj, list, ui_strmodel_getvalue, f, udata);
47 } 47 }
48 48
49 UIWIDGET ui_listview_var(UiObject *obj, UiListPtr *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { 49 UIWIDGET ui_listview_var(UiObject *obj, UiVar *var, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
50 // create treeview 50 // create treeview
51 GtkWidget *view = gtk_tree_view_new(); 51 GtkWidget *view = gtk_tree_view_new();
52 GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); 52 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
53 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes(NULL, renderer, "text", 0, NULL); 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); 54 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
64 // TODO: implement for gtk2 64 // TODO: implement for gtk2
65 #endif 65 #endif
66 66
67 UiModelInfo *modelinfo = ui_model_info(obj->ctx, UI_STRING, "", -1); 67 UiModelInfo *modelinfo = ui_model_info(obj->ctx, UI_STRING, "", -1);
68 modelinfo->getvalue = getvalue; 68 modelinfo->getvalue = getvalue;
69 UiListModel *model = ui_list_model_new(list, modelinfo); 69 UiList *list = var->value;
70 UiListModel *model = ui_list_model_new(var, modelinfo);
70 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(model)); 71 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(model));
71 72
72 // add TreeView as observer to the UiList to update the TreeView if the 73 UiListView *listview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListView));
73 // data changes 74 listview->ctx = obj->ctx;
74 UiTableView *listview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiTableView));
75 listview->widget = view; 75 listview->widget = view;
76 listview->list = list; 76 listview->var = var;
77 listview->modelinfo = modelinfo; 77 listview->modelinfo = modelinfo;
78 list->list->observers = ui_add_observer( 78 g_signal_connect(
79 list->list->observers, 79 view,
80 (ui_callback)ui_listview_update, 80 "destroy",
81 listview); 81 G_CALLBACK(ui_listview_destroy),
82 listview);
83
84 // bind var
85 list->update = ui_listview_update;
86 list->obj = listview;
82 87
83 // add callback 88 // add callback
84 if(f) { 89 if(f) {
85 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData)); 90 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData));
86 event->obj = obj; 91 event->obj = obj;
112 117
113 return scroll_area; 118 return scroll_area;
114 } 119 }
115 120
116 UIWIDGET ui_listview(UiObject *obj, UiList *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { 121 UIWIDGET ui_listview(UiObject *obj, UiList *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
117 UiListPtr *listptr = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr)); 122 UiVar *var = malloc(sizeof(UiVar));
118 listptr->list = list; 123 var->value = list;
119 return ui_listview_var(obj, listptr, getvalue, f, udata); 124 var->type = UI_VAR_SPECIAL;
125 return ui_listview_var(obj, var, getvalue, f, udata);
120 } 126 }
121 127
122 UIWIDGET ui_listview_nv(UiObject *obj, char *varname, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { 128 UIWIDGET ui_listview_nv(UiObject *obj, char *varname, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
123 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST); 129 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_LIST);
124 if(var) { 130 if(var) {
125 UiListVar *value = var->value; 131 return ui_listview_var(obj, var, getvalue, f, udata);
126 return ui_listview_var(obj, value->listptr, getvalue, f, udata);
127 } else { 132 } else {
128 // TODO: error 133 // TODO: error
129 } 134 }
130 return NULL; 135 return NULL;
131 } 136 }
132 137
133 138
134 UIWIDGET ui_table_var(UiObject *obj, UiListPtr *list, UiModelInfo *modelinfo) { 139 UIWIDGET ui_table_var(UiObject *obj, UiVar *var, UiModelInfo *modelinfo) {
135 // create treeview 140 // create treeview
136 GtkWidget *view = gtk_tree_view_new(); 141 GtkWidget *view = gtk_tree_view_new();
137 int addi = 0; 142 int addi = 0;
138 for(int i=0;i<modelinfo->columns;i++) { 143 for(int i=0;i<modelinfo->columns;i++) {
139 GtkTreeViewColumn *column = NULL; 144 GtkTreeViewColumn *column = NULL;
170 //gtk_tree_view_set_activate_on_single_click(GTK_TREE_VIEW(view), TRUE); 175 //gtk_tree_view_set_activate_on_single_click(GTK_TREE_VIEW(view), TRUE);
171 #else 176 #else
172 177
173 #endif 178 #endif
174 179
175 UiListModel *model = ui_list_model_new(list, modelinfo); 180 UiList *list = var->value;
181 UiListModel *model = ui_list_model_new(var, modelinfo);
176 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(model)); 182 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(model));
177 183
178 // add TreeView as observer to the UiList to update the TreeView if the 184 // add TreeView as observer to the UiList to update the TreeView if the
179 // data changes 185 // data changes
180 UiTableView *tableview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiTableView)); 186 UiListView *tableview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListView));
187 tableview->ctx = obj->ctx;
181 tableview->widget = view; 188 tableview->widget = view;
182 tableview->list = list; 189 tableview->var = var;
183 tableview->modelinfo = modelinfo; 190 tableview->modelinfo = modelinfo;
184 list->list->observers = ui_add_observer( 191 g_signal_connect(
185 list->list->observers, 192 view,
186 (ui_callback)ui_listview_update, 193 "destroy",
187 tableview); 194 G_CALLBACK(ui_listview_destroy),
195 tableview);
196
197 // bind var
198 list->update = ui_listview_update;
199 list->obj = tableview;
188 200
189 // add callback 201 // add callback
190 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData)); 202 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData));
191 event->obj = obj; 203 event->obj = obj;
192 event->activate = modelinfo->activate; 204 event->activate = modelinfo->activate;
230 242
231 return scroll_area; 243 return scroll_area;
232 } 244 }
233 245
234 UIWIDGET ui_table(UiObject *obj, UiList *list, UiModelInfo *modelinfo) { 246 UIWIDGET ui_table(UiObject *obj, UiList *list, UiModelInfo *modelinfo) {
235 UiListPtr *listptr = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr)); 247 UiVar *var = malloc(sizeof(UiVar));
236 listptr->list = list; 248 var->value = list;
237 return ui_table_var(obj, listptr, modelinfo); 249 var->type = UI_VAR_SPECIAL;
250 return ui_table_var(obj, var, modelinfo);
238 } 251 }
239 252
240 UIWIDGET ui_table_nv(UiObject *obj, char *varname, UiModelInfo *modelinfo) { 253 UIWIDGET ui_table_nv(UiObject *obj, char *varname, UiModelInfo *modelinfo) {
241 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST); 254 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_LIST);
242 if(var) { 255 if(var) {
243 UiListVar *value = var->value; 256 return ui_table_var(obj, var, modelinfo);
244 return ui_table_var(obj, value->listptr, modelinfo);
245 } else { 257 } else {
246 // TODO: error 258 // TODO: error
247 } 259 }
248 return NULL; 260 return NULL;
249 } 261 }
250 262
251 263 void ui_listview_update(UiList *list, int i) {
252 264 UiListView *view = list->obj;
253 void ui_listview_update(UiEvent *event, UiTableView *view) { 265 UiListModel *model = ui_list_model_new(view->var, view->modelinfo);
254 UiListModel *model = ui_list_model_new(view->list, view->modelinfo); 266 gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(model));
255 gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(model));
256
257 // TODO: free old model 267 // TODO: free old model
258 } 268 }
269
270 void ui_listview_destroy(GtkWidget *w, UiListView *v) {
271 ui_destroy_boundvar(v->ctx, v->var);
272 // TODO: destroy model?
273 free(v);
274 }
275
259 276
260 void ui_listview_activate_event( 277 void ui_listview_activate_event(
261 GtkTreeView *treeview, 278 GtkTreeView *treeview,
262 GtkTreePath *path, 279 GtkTreePath *path,
263 GtkTreeViewColumn *column, 280 GtkTreeViewColumn *column,
337 UIWIDGET ui_combobox_str(UiObject *obj, UiList *list, ui_callback f, void *udata) { 354 UIWIDGET ui_combobox_str(UiObject *obj, UiList *list, ui_callback f, void *udata) {
338 return ui_combobox(obj, list, ui_strmodel_getvalue, f, udata); 355 return ui_combobox(obj, list, ui_strmodel_getvalue, f, udata);
339 } 356 }
340 357
341 UIWIDGET ui_combobox(UiObject *obj, UiList *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { 358 UIWIDGET ui_combobox(UiObject *obj, UiList *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
342 UiListPtr *listptr = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr)); 359 UiVar *var = malloc(sizeof(UiVar));
343 listptr->list = list; 360 var->value = list;
344 return ui_combobox_var(obj, listptr, getvalue, f, udata); 361 var->type = UI_VAR_SPECIAL;
362 return ui_combobox_var(obj, var, getvalue, f, udata);
345 } 363 }
346 364
347 UIWIDGET ui_combobox_nv(UiObject *obj, char *varname, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { 365 UIWIDGET ui_combobox_nv(UiObject *obj, char *varname, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
348 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST); 366 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_LIST);
349 if(var) { 367 if(var) {
350 UiListVar *value = var->value; 368 return ui_combobox_var(obj, var, getvalue, f, udata);
351 return ui_combobox_var(obj, value->listptr, getvalue, f, udata);
352 } else { 369 } else {
353 // TODO: error 370 // TODO: error
354 } 371 }
355 return NULL; 372 return NULL;
356 } 373 }
357 374
358 UIWIDGET ui_combobox_var(UiObject *obj, UiListPtr *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { 375 UIWIDGET ui_combobox_var(UiObject *obj, UiVar *var, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
359 UiModelInfo *modelinfo = ui_model_info(obj->ctx, UI_STRING, "", -1); 376 UiModelInfo *modelinfo = ui_model_info(obj->ctx, UI_STRING, "", -1);
360 modelinfo->getvalue = getvalue; 377 modelinfo->getvalue = getvalue;
361 UiListModel *model = ui_list_model_new(list, modelinfo); 378 UiList *list = var->value;
379 UiListModel *model = ui_list_model_new(var, modelinfo);
362 380
363 GtkWidget *combobox = ui_create_combobox(obj, model, f, udata); 381 GtkWidget *combobox = ui_create_combobox(obj, model, f, udata);
364 UiContainer *ct = uic_get_current_container(obj); 382 UiContainer *ct = uic_get_current_container(obj);
365 ct->add(ct, combobox, FALSE); 383 ct->add(ct, combobox, FALSE);
366 } 384 }
367 385
368 GtkWidget* ui_create_combobox(UiObject *obj, UiListModel *model, ui_callback f, void *udata) { 386 GtkWidget* ui_create_combobox(UiObject *obj, UiListModel *model, ui_callback f, void *udata) {
369 GtkWidget *combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model)); 387 GtkWidget *combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
370 388
371 UiList *list = model->list->list; 389 UiListView *uicbox = malloc(sizeof(UiListView));
372 list->observers = ui_add_observer( 390 uicbox->ctx = obj->ctx;
373 list->observers, 391 uicbox->widget = combobox;
374 (ui_callback)ui_listview_update, 392 uicbox->var = model->var;
375 combobox); 393 uicbox->modelinfo = model->info;
394
395 g_signal_connect(
396 combobox,
397 "destroy",
398 G_CALLBACK(ui_listview_destroy),
399 uicbox);
400
401 // bind var
402 UiList *list = model->var->value;
403 list->update = ui_combobox_modelupdate;
404 list->obj = uicbox;
376 405
377 GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); 406 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
378 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), renderer, TRUE); 407 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), renderer, TRUE);
379 gtk_cell_layout_set_attributes( 408 gtk_cell_layout_set_attributes(
380 GTK_CELL_LAYOUT(combobox), 409 GTK_CELL_LAYOUT(combobox),
410 event.eventdata = NULL; 439 event.eventdata = NULL;
411 event.intval = gtk_combo_box_get_active(widget); 440 event.intval = gtk_combo_box_get_active(widget);
412 e->callback(&event, e->userdata); 441 e->callback(&event, e->userdata);
413 } 442 }
414 443
415 void ui_combobox_update(UiEvent *event, void *combobox) { 444 void ui_combobox_modelupdate(UiList *list, int i) {
416 printf("ui_combobox_update\n"); 445 UiListView *view = list->obj;
417 printf("TODO: implement\n"); 446 UiListModel *model = ui_list_model_new(view->var, view->modelinfo);
418 } 447 gtk_combo_box_set_model(GTK_COMBO_BOX(view->widget), GTK_TREE_MODEL(model));
419 448 }
449

mercurial