port combobox to new API (GTK) newapi

Sat, 24 Feb 2024 20:20:34 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 24 Feb 2024 20:20:34 +0100
branch
newapi
changeset 270
9389313ac00f
parent 269
3380100e20f5
child 271
94d44bdcad3e

port combobox to new API (GTK)

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/tree.c file | annotate | diff | comparison | revisions
ui/gtk/tree.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Thu Feb 22 22:25:53 2024 +0100
+++ b/application/main.c	Sat Feb 24 20:20:34 2024 +0100
@@ -83,7 +83,7 @@
     */
     
     char *str = elm;
-    return col == 1 ? str : "x";
+    return col == 0 ? str : "x";
 }
 
 void application_startup(UiEvent *event, void *data) {
@@ -120,7 +120,7 @@
         
         //UiModel *model = ui_model(obj->ctx, UI_ICON_TEXT, "Col 1", UI_STRING, "Col 2", -1);
         //model->getvalue = list_getvalue;
-        ui_listview(obj, .hexpand = true, .vexpand = true, .colspan = 2, .varname = "list", .getvalue = list_getvalue);
+        ui_combobox(obj, .hexpand = true, .vexpand = false, .colspan = 2, .varname = "list", .getvalue = list_getvalue);
     }
     
     ui_show(obj);
--- a/ui/gtk/tree.c	Thu Feb 22 22:25:53 2024 +0100
+++ b/ui/gtk/tree.c	Sat Feb 24 20:20:34 2024 +0100
@@ -555,47 +555,37 @@
 
 /* --------------------------- ComboBox ---------------------------  */
 
-UIWIDGET ui_combobox_deprecated(UiObject *obj, UiList *list, ui_getvaluefunc getvalue, ui_callback f, void *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_getvaluefunc getvalue, ui_callback f, void *udata) {
-    UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_LIST);
-    if(var) {
-        return ui_combobox_var(obj, var, getvalue, f, udata);
-    } else {
-        // TODO: error
-    }
-    return NULL;
+UIWIDGET ui_combobox_create(UiObject *obj, UiListArgs args) {
+    UiObject* current = uic_current_obj(obj);
+    
+    UiModel *model = ui_model(obj->ctx, UI_STRING, "", -1);
+    model->getvalue = args.getvalue;
+    
+    UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST);
+    
+    GtkWidget *combobox = ui_create_combobox(obj, model, var, args.onactivate, args.onactivatedata);
+    UI_APPLY_LAYOUT1(current, args);
+    current->container->add(current->container, combobox, FALSE);
+    current->container->current = combobox;
+    return combobox;
 }
 
-UIWIDGET ui_combobox_var(UiObject *obj, UiVar *var, ui_getvaluefunc getvalue, ui_callback f, void *udata) {
-    /*
-    UiModel *model = ui_model(obj->ctx, UI_STRING, "", -1);
-    model->getvalue = getvalue;
-    UiList *list = var ? var->value : NULL;
-    GtkListStore *store = create_list_store(list, model);
-    
-    //GtkWidget *combobox = ui_create_combobox(obj, listmodel, f, udata);
-    UiContainer *ct = uic_get_current_container(obj);
-    ct->add(ct, combobox, FALSE);
-    return combobox;
-    */
-    return NULL;
-}
-
-GtkWidget* ui_create_combobox(UiObject *obj, GtkListStore *store, ui_callback f, void *udata) {
-    GtkWidget *combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
-    
+GtkWidget* ui_create_combobox(UiObject *obj, UiModel *model, UiVar *var, ui_callback f, void *udata) {
+    GtkWidget *combobox = gtk_combo_box_new();
+     
     UiListView *uicbox = malloc(sizeof(UiListView));
     uicbox->obj = obj;
     uicbox->widget = combobox;
-    // TODO
-    //uicbox->var = model->var;
-    //uicbox->model = model->model;
+    
+    UiList *list = var ? var->value : NULL;
+    GtkListStore *listmodel = create_list_store(list, model);
+    
+    if(listmodel) {
+        gtk_combo_box_set_model(GTK_COMBO_BOX(combobox), GTK_TREE_MODEL(listmodel));
+    }
+    
+    uicbox->var = var;
+    uicbox->model = model;
     
     g_signal_connect(
                 combobox,
@@ -604,9 +594,10 @@
                 uicbox);
     
     // bind var
-    //UiList *list = model->var->value;
-    //list->update = ui_combobox_modelupdate;
-    //list->obj = uicbox;
+    if(list) {
+        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);
--- a/ui/gtk/tree.h	Thu Feb 22 22:25:53 2024 +0100
+++ b/ui/gtk/tree.h	Sat Feb 24 20:20:34 2024 +0100
@@ -76,7 +76,7 @@
 int ui_tree_path_list_index(GtkTreePath *path);
 
 UIWIDGET ui_combobox_var(UiObject *obj, UiVar *var, ui_getvaluefunc getvalue, ui_callback f, void *udata);
-GtkWidget* ui_create_combobox(UiObject *obj, GtkListStore *store, ui_callback f, void *udata);
+GtkWidget* ui_create_combobox(UiObject *obj, UiModel *model, UiVar *var, ui_callback f, void *udata);
 void ui_combobox_change_event(GtkComboBox *widget, UiEventData *e);
 void ui_combobox_modelupdate(UiList *list, int i);
         

mercurial