implement combobox get/set selection (GTK) newapi tip

Sun, 27 Oct 2024 12:08:10 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 27 Oct 2024 12:08:10 +0100
branch
newapi
changeset 360
681b930abe84
parent 359
c51e58359db8

implement combobox get/set selection (GTK)

ui/common/types.c file | annotate | diff | comparison | revisions
ui/gtk/list.c file | annotate | diff | comparison | revisions
ui/gtk/list.h file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/ui/common/types.c	Sun Oct 27 11:34:25 2024 +0100
+++ b/ui/common/types.c	Sun Oct 27 12:08:10 2024 +0100
@@ -501,6 +501,15 @@
     return (UiListSelection){ 0, NULL };
 }
 
+UIEXPORT void ui_list_setselection(UiList *list, int index) {
+    if (list->setselection && index >= 0) {
+        UiListSelection sel;
+        sel.count = 1;
+        sel.rows = &index;
+        list->setselection(list, sel);
+    }
+}
+
 UIEXPORT void ui_listselection_free(UiListSelection selection) {
     if (selection.rows) {
         free(selection.rows);
--- a/ui/gtk/list.c	Sun Oct 27 11:34:25 2024 +0100
+++ b/ui/gtk/list.c	Sun Oct 27 12:08:10 2024 +0100
@@ -180,7 +180,7 @@
 #endif
     
     UiModel *model = ui_model(obj->ctx, UI_STRING, "", -1);
-    model->getvalue = args.getvalue;
+    model->getvalue = args.getvalue ? args.getvalue : ui_strmodel_getvalue;
     
     UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST);
     
@@ -586,7 +586,7 @@
     UiObject* current = uic_current_obj(obj);
     
     UiModel *model = ui_model(obj->ctx, UI_STRING, "", -1);
-    model->getvalue = args.getvalue;
+    model->getvalue = args.getvalue ? args.getvalue : ui_strmodel_getvalue;
     
     UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST);
     
@@ -625,7 +625,8 @@
     // bind var
     if(list) {
         list->update = ui_combobox_modelupdate;
-        // TODO: combobox getselection
+        list->getselection = ui_combobox_getselection;
+        list->setselection = ui_combobox_setselection;
         list->obj = uicbox;
     }
     
@@ -674,3 +675,18 @@
     gtk_combo_box_set_model(GTK_COMBO_BOX(view->widget), GTK_TREE_MODEL(store));
 }
 
+UiListSelection ui_combobox_getselection(UiList *list) {
+    UiListView *combobox = list->obj;
+    UiListSelection ret;
+    ret.rows = malloc(sizeof(int*));
+    ret.count = 1;
+    ret.rows[0] = gtk_combo_box_get_active(GTK_COMBO_BOX(combobox->widget));
+    return ret;
+}
+
+void ui_combobox_setselection(UiList *list, UiListSelection selection) {
+    UiListView *combobox = list->obj;
+    if(selection.count > 0) {
+        gtk_combo_box_set_active(GTK_COMBO_BOX(combobox->widget), selection.rows[0]);
+    }
+}
--- a/ui/gtk/list.h	Sun Oct 27 11:34:25 2024 +0100
+++ b/ui/gtk/list.h	Sun Oct 27 12:08:10 2024 +0100
@@ -60,6 +60,7 @@
 
 void ui_listview_update(UiList *list, int i);
 UiListSelection ui_listview_getselection(UiList *list);
+void ui_listview_setselection(UiList *list, UiListSelection selection);
 
 void ui_combobox_destroy(GtkWidget *w, UiListView *v);
 void ui_listview_destroy(GtkWidget *w, UiListView *v);
@@ -81,6 +82,8 @@
 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);
+UiListSelection ui_combobox_getselection(UiList *list);
+void ui_combobox_setselection(UiList *list, UiListSelection selection);
         
 #ifdef	__cplusplus
 }
--- a/ui/ui/toolkit.h	Sun Oct 27 11:34:25 2024 +0100
+++ b/ui/ui/toolkit.h	Sun Oct 27 12:08:10 2024 +0100
@@ -357,6 +357,7 @@
     /* binding functions */
     void (*update)(UiList *list, int i);
     UiListSelection (*getselection)(UiList *list);
+    void (*setselection)(UiList *list, UiListSelection selection);
     /* binding object */
     void *obj;
     
@@ -518,6 +519,7 @@
 UIEXPORT void ui_list_notify(UiList *list);
 
 UIEXPORT UiListSelection ui_list_getselection(UiList *list);
+UIEXPORT void ui_list_setselection(UiList *list, int index);
 
 UIEXPORT UiFileList ui_filelist_copy(UiFileList list);
 UIEXPORT void ui_filelist_free(UiFileList list);

mercurial