implement ui_list_getselection (GTK)

Fri, 14 Jun 2024 18:24:31 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 14 Jun 2024 18:24:31 +0200
changeset 41
7ada3db4cfa8
parent 40
af5c2bfe0f21
child 42
9af327d0e0e4

implement ui_list_getselection (GTK)

ui/gtk/tree.c file | annotate | diff | comparison | revisions
ui/gtk/tree.h file | annotate | diff | comparison | revisions
--- a/ui/gtk/tree.c	Fri Jun 14 18:05:41 2024 +0200
+++ b/ui/gtk/tree.c	Fri Jun 14 18:24:31 2024 +0200
@@ -186,6 +186,7 @@
     
     // bind var
     list->update = ui_listview_update;
+    list->getselection = ui_listview_getselection;
     list->obj = listview;
     
     // add callback
@@ -322,6 +323,7 @@
     
     // bind var
     list->update = ui_listview_update;
+    list->getselection = ui_listview_getselection;
     list->obj = tableview;
     
     // add callback
@@ -463,6 +465,14 @@
     // TODO: free old model
 }
 
+UiListSelection ui_listview_getselection(UiList *list) {
+    UiListView *view = list->obj;
+    UiListSelection selection = ui_listview_selection(
+            gtk_tree_view_get_selection(GTK_TREE_VIEW(view->widget)),
+            NULL);
+    return selection;
+}
+
 void ui_listview_destroy(GtkWidget *w, UiListView *v) {
     gtk_tree_view_set_model(GTK_TREE_VIEW(w), NULL);
     ui_destroy_boundvar(v->obj->ctx, v->var);
@@ -484,7 +494,7 @@
         GtkTreeViewColumn *column,
         UiTreeEventData *event)
 {
-    UiListSelection *selection = ui_listview_selection(
+    UiListSelection selection = ui_listview_selection(
             gtk_tree_view_get_selection(treeview),
             event);
     
@@ -492,50 +502,48 @@
     e.obj = event->obj;
     e.window = event->obj->window;
     e.document = event->obj->ctx->document;
-    e.eventdata = selection;
-    e.intval = selection->count > 0 ? selection->rows[0] : -1;
+    e.eventdata = &selection;
+    e.intval = selection.count > 0 ? selection.rows[0] : -1;
     event->activate(&e, event->activatedata);
     
-    if(selection->count > 0) {
-        free(selection->rows);
+    if(selection.count > 0) {
+        free(selection.rows);
     }
-    free(selection);
 }
 
 void ui_listview_selection_event(
         GtkTreeSelection *treeselection,
         UiTreeEventData *event)
 {
-    UiListSelection *selection = ui_listview_selection(treeselection, event);
+    UiListSelection selection = ui_listview_selection(treeselection, event);
     
     UiEvent e;
     e.obj = event->obj;
     e.window = event->obj->window;
     e.document = event->obj->ctx->document;
-    e.eventdata = selection;
-    e.intval = selection->count > 0 ? selection->rows[0] : -1;
+    e.eventdata = &selection;
+    e.intval = selection.count > 0 ? selection.rows[0] : -1;
     event->selection(&e, event->selectiondata);
     
-    if(selection->count > 0) {
-        free(selection->rows);
+    if(selection.count > 0) {
+        free(selection.rows);
     }
-    free(selection);
 }
 
-UiListSelection* ui_listview_selection(
+UiListSelection ui_listview_selection(
         GtkTreeSelection *selection,
         UiTreeEventData *event)
 {
     GList *rows = gtk_tree_selection_get_selected_rows(selection, NULL);
     
-    UiListSelection *ls = malloc(sizeof(UiListSelection));
-    ls->count = g_list_length(rows);
-    ls->rows = calloc(ls->count, sizeof(int));
+    UiListSelection ls;
+    ls.count = g_list_length(rows);
+    ls.rows = calloc(ls.count, sizeof(int));
     GList *r = rows;
     int i = 0;
     while(r) {
         GtkTreePath *path = r->data;
-        ls->rows[i] = ui_tree_path_list_index(path);
+        ls.rows[i] = ui_tree_path_list_index(path);
         r = r->next;
         i++;
     }
@@ -596,6 +604,7 @@
     // bind var
     if(list) {
         list->update = ui_combobox_modelupdate;
+        // TODO: combobox getselection
         list->obj = uicbox;
     }
     
--- a/ui/gtk/tree.h	Fri Jun 14 18:05:41 2024 +0200
+++ b/ui/gtk/tree.h	Fri Jun 14 18:24:31 2024 +0200
@@ -59,6 +59,8 @@
 GtkWidget* ui_get_tree_widget(UIWIDGET widget);
 
 void ui_listview_update(UiList *list, int i);
+UiListSelection ui_listview_getselection(UiList *list);
+
 void ui_combobox_destroy(GtkWidget *w, UiListView *v);
 void ui_listview_destroy(GtkWidget *w, UiListView *v);
 
@@ -70,7 +72,7 @@
 void ui_listview_selection_event(
         GtkTreeSelection *treeselection,
         UiTreeEventData *event);
-UiListSelection* ui_listview_selection(
+UiListSelection ui_listview_selection(
         GtkTreeSelection *selection,
         UiTreeEventData *event);
 int ui_tree_path_list_index(GtkTreePath *path);

mercurial