ui/gtk/tree.c

changeset 140
c03c338a7dcf
parent 129
5babf09f5f19
child 142
46448d38885c
--- a/ui/gtk/tree.c	Tue Jan 24 18:46:47 2017 +0100
+++ b/ui/gtk/tree.c	Fri Nov 10 17:17:14 2017 +0100
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright 2014 Olaf Wintermann. All rights reserved.
+ * Copyright 2017 Olaf Wintermann. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -46,7 +46,7 @@
     return ui_listview(obj, list, ui_strmodel_getvalue, f, udata);
 }
 
-UIWIDGET ui_listview_var(UiObject *obj, UiListPtr *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
+UIWIDGET ui_listview_var(UiObject *obj, UiVar *var, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
     // create treeview
     GtkWidget *view = gtk_tree_view_new();
     GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
@@ -66,19 +66,24 @@
     
     UiModelInfo *modelinfo = ui_model_info(obj->ctx, UI_STRING, "", -1);
     modelinfo->getvalue = getvalue;
-    UiListModel *model = ui_list_model_new(list, modelinfo);
+    UiList *list = var->value;
+    UiListModel *model = ui_list_model_new(var, modelinfo);
     gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(model));
     
-    // add TreeView as observer to the UiList to update the TreeView if the
-    // data changes
-    UiTableView *listview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiTableView));
+    UiListView *listview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListView));
+    listview->ctx = obj->ctx;
     listview->widget = view;
-    listview->list = list;
+    listview->var = var;
     listview->modelinfo = modelinfo;
-    list->list->observers = ui_add_observer(
-            list->list->observers,
-            (ui_callback)ui_listview_update,
-            listview);
+    g_signal_connect(
+                view,
+                "destroy",
+                G_CALLBACK(ui_listview_destroy),
+                listview);
+    
+    // bind var
+    list->update = ui_listview_update;
+    list->obj = listview;
     
     // add callback
     if(f) {
@@ -114,16 +119,16 @@
 }
 
 UIWIDGET ui_listview(UiObject *obj, UiList *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
-    UiListPtr *listptr = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr));
-    listptr->list = list;
-    return ui_listview_var(obj, listptr, getvalue, f, udata);
+    UiVar *var = malloc(sizeof(UiVar));
+    var->value = list;
+    var->type = UI_VAR_SPECIAL;
+    return ui_listview_var(obj, var, getvalue, f, udata);
 }
 
 UIWIDGET ui_listview_nv(UiObject *obj, char *varname, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
-    UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST);
+    UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_LIST);
     if(var) {
-        UiListVar *value = var->value;
-        return ui_listview_var(obj, value->listptr, getvalue, f, udata);
+        return ui_listview_var(obj, var, getvalue, f, udata);
     } else {
         // TODO: error
     }
@@ -131,7 +136,7 @@
 }
 
 
-UIWIDGET ui_table_var(UiObject *obj, UiListPtr *list, UiModelInfo *modelinfo) {
+UIWIDGET ui_table_var(UiObject *obj, UiVar *var, UiModelInfo *modelinfo) {
     // create treeview
     GtkWidget *view = gtk_tree_view_new();
     int addi = 0;
@@ -172,19 +177,26 @@
     
 #endif
     
-    UiListModel *model = ui_list_model_new(list, modelinfo);
+    UiList *list = var->value;
+    UiListModel *model = ui_list_model_new(var, modelinfo);
     gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(model));
     
     // add TreeView as observer to the UiList to update the TreeView if the
     // data changes
-    UiTableView *tableview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiTableView));
+    UiListView *tableview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListView));
+    tableview->ctx = obj->ctx;
     tableview->widget = view;
-    tableview->list = list;
+    tableview->var = var;
     tableview->modelinfo = modelinfo;
-    list->list->observers = ui_add_observer(
-            list->list->observers,
-            (ui_callback)ui_listview_update,
-            tableview);
+    g_signal_connect(
+                view,
+                "destroy",
+                G_CALLBACK(ui_listview_destroy),
+                tableview);
+    
+    // bind var
+    list->update = ui_listview_update;
+    list->obj = tableview;
     
     // add callback
     UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData));
@@ -232,31 +244,36 @@
 }
 
 UIWIDGET ui_table(UiObject *obj, UiList *list, UiModelInfo *modelinfo) {
-    UiListPtr *listptr = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr));
-    listptr->list = list;
-    return ui_table_var(obj, listptr, modelinfo);
+    UiVar *var = malloc(sizeof(UiVar));
+    var->value = list;
+    var->type = UI_VAR_SPECIAL;
+    return ui_table_var(obj, var, modelinfo);
 }
 
 UIWIDGET ui_table_nv(UiObject *obj, char *varname, UiModelInfo *modelinfo) {
-    UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST);
+    UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_LIST);
     if(var) {
-        UiListVar *value = var->value;
-        return ui_table_var(obj, value->listptr, modelinfo);
+        return ui_table_var(obj, var, modelinfo);
     } else {
         // TODO: error
     }
     return NULL;
 }
 
-
-
-void ui_listview_update(UiEvent *event, UiTableView *view) {
-    UiListModel *model = ui_list_model_new(view->list, view->modelinfo);
-    gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(model));   
-    
+void ui_listview_update(UiList *list, int i) {
+    UiListView *view = list->obj;
+    UiListModel *model = ui_list_model_new(view->var, view->modelinfo);
+    gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(model));
     // TODO: free old model
 }
 
+void ui_listview_destroy(GtkWidget *w, UiListView *v) {
+    ui_destroy_boundvar(v->ctx, v->var);
+    // TODO: destroy model?
+    free(v);
+}
+
+
 void ui_listview_activate_event(
         GtkTreeView *treeview,
         GtkTreePath *path,
@@ -339,26 +356,27 @@
 }
 
 UIWIDGET ui_combobox(UiObject *obj, UiList *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
-    UiListPtr *listptr = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr));
-    listptr->list = list;
-    return ui_combobox_var(obj, listptr, getvalue, f, udata);
+    UiVar *var = malloc(sizeof(UiVar));
+    var->value = list;
+    var->type = UI_VAR_SPECIAL;
+    return ui_combobox_var(obj, var, getvalue, f, udata);
 }
 
 UIWIDGET ui_combobox_nv(UiObject *obj, char *varname, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
-    UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST);
+    UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_LIST);
     if(var) {
-        UiListVar *value = var->value;
-        return ui_combobox_var(obj, value->listptr, getvalue, f, udata);
+        return ui_combobox_var(obj, var, getvalue, f, udata);
     } else {
         // TODO: error
     }
     return NULL;
 }
 
-UIWIDGET ui_combobox_var(UiObject *obj, UiListPtr *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
+UIWIDGET ui_combobox_var(UiObject *obj, UiVar *var, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
     UiModelInfo *modelinfo = ui_model_info(obj->ctx, UI_STRING, "", -1);
     modelinfo->getvalue = getvalue;
-    UiListModel *model = ui_list_model_new(list, modelinfo);
+    UiList *list = var->value;
+    UiListModel *model = ui_list_model_new(var, modelinfo);
     
     GtkWidget *combobox = ui_create_combobox(obj, model, f, udata);
     UiContainer *ct = uic_get_current_container(obj);
@@ -368,11 +386,22 @@
 GtkWidget* ui_create_combobox(UiObject *obj, UiListModel *model, ui_callback f, void *udata) {
     GtkWidget *combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
     
-    UiList *list = model->list->list;
-    list->observers = ui_add_observer(
-            list->observers,
-            (ui_callback)ui_listview_update,
-            combobox);
+    UiListView *uicbox = malloc(sizeof(UiListView));
+    uicbox->ctx = obj->ctx;
+    uicbox->widget = combobox;
+    uicbox->var = model->var;
+    uicbox->modelinfo = model->info;
+    
+    g_signal_connect(
+                combobox,
+                "destroy",
+                G_CALLBACK(ui_listview_destroy),
+                uicbox);
+    
+    // bind var
+    UiList *list = model->var->value;
+    list->update = ui_combobox_modelupdate;
+    list->obj = uicbox;
     
     GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
     gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), renderer, TRUE);
@@ -412,8 +441,9 @@
     e->callback(&event, e->userdata);
 }
 
-void ui_combobox_update(UiEvent *event, void *combobox) {
-    printf("ui_combobox_update\n");
-    printf("TODO: implement\n");
+void ui_combobox_modelupdate(UiList *list, int i) {
+    UiListView *view = list->obj;
+    UiListModel *model = ui_list_model_new(view->var, view->modelinfo);
+    gtk_combo_box_set_model(GTK_COMBO_BOX(view->widget), GTK_TREE_MODEL(model));
 }
 

mercurial