add functions for getting/setting a listview/dropdown selection (Win32)

Sat, 13 Dec 2025 12:47:36 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 13 Dec 2025 12:47:36 +0100
changeset 998
55508508ba4d
parent 997
287b20e2a564
child 1000
56faeb1772da

add functions for getting/setting a listview/dropdown selection (Win32)

ui/ui/list.h file | annotate | diff | comparison | revisions
ui/win32/list.c file | annotate | diff | comparison | revisions
ui/win32/list.h file | annotate | diff | comparison | revisions
--- a/ui/ui/list.h	Sat Dec 13 12:37:41 2025 +0100
+++ b/ui/ui/list.h	Sat Dec 13 12:47:36 2025 +0100
@@ -332,7 +332,9 @@
 UIEXPORT UIWIDGET ui_breadcrumbbar_create(UiObject *obj, UiListArgs *args);
 
 UIEXPORT void ui_listview_select(UIWIDGET listview, int index);
+UIEXPORT int ui_listview_selection(UIWIDGET listview);
 UIEXPORT void ui_dropdown_select(UIWIDGET dropdown, int index);
+UIEXPORT int ui_dropdown_selection(UIWIDGET dropdown);
 
 UIEXPORT UIWIDGET ui_sourcelist_create(UiObject *obj, UiSourceListArgs *args);
 
--- a/ui/win32/list.c	Sat Dec 13 12:37:41 2025 +0100
+++ b/ui/win32/list.c	Sat Dec 13 12:47:36 2025 +0100
@@ -166,8 +166,8 @@
         UiList *list = listview->var->value;
         list->obj = listview;
         list->update = ui_listview_update;
-        list->getselection = ui_listview_getselection;
-        list->setselection = ui_listview_setselection;
+        list->getselection = ui_listview_getselection_impl;
+        list->setselection = ui_listview_setselection_impl;
 
         ui_listview_update(list, -1);
     } else if (!table && args->static_elements && args->static_nelm > 0) {
@@ -188,9 +188,8 @@
     return (W32Widget*)listview;
 }
 
-static UiListSelection listview_get_selection(UiListView *listview) {
+static UiListSelection listview_get_selection2(HWND hwnd) {
     UiListSelection sel = { 0, NULL };
-    HWND hwnd = listview->widget.hwnd;
 
     CX_ARRAY_DECLARE(int, indices);
     cx_array_initialize(indices, 8);
@@ -208,6 +207,11 @@
     return sel;
 }
 
+static UiListSelection listview_get_selection(UiListView *listview) {
+    HWND hwnd = listview->widget.hwnd;
+    return listview_get_selection2(hwnd);
+}
+
 // listview class event proc
 int ui_listview_eventproc(W32Widget *widget, HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
     UiListView *listview = (UiListView*)widget;
@@ -340,12 +344,12 @@
     }
 }
 
-UiListSelection ui_listview_getselection(UiList *list) {
+UiListSelection ui_listview_getselection_impl(UiList *list) {
     UiListView *listview = (UiListView*)list->obj;
     return listview_get_selection(listview);
 }
 
-void ui_listview_setselection(UiList *list, UiListSelection selection) {
+void ui_listview_setselection_impl(UiList *list, UiListSelection selection) {
 
 }
 
@@ -359,6 +363,20 @@
     return listview_create(obj, args, TRUE);
 }
 
+void ui_listview_select(UIWIDGET listview, int index) {
+
+}
+
+int ui_listview_selection(UIWIDGET listview) {
+    W32Widget *w = (W32Widget*)listview;
+    UiListSelection sel = listview_get_selection2(w->hwnd);
+    int index = -1;
+    if (sel.count > 0) {
+        index = sel.rows[0];
+    }
+    free(sel.rows);
+    return index;
+}
 
 /* ------------------------------------ DropDown ------------------------------------*/
 
@@ -396,8 +414,8 @@
         UiList *list = dropdown->var->value;
         list->obj = dropdown;
         list->update = ui_dropdown_update;
-        list->getselection = ui_dropdown_getselection;
-        list->setselection = ui_dropdown_setselection;
+        list->getselection = ui_dropdown_getselection_impl;
+        list->setselection = ui_dropdown_setselection_impl;
 
         ui_dropdown_update(list, -1);
     } else if (args->static_elements && args->static_nelm > 0) {
@@ -457,7 +475,7 @@
     }
 }
 
-UiListSelection ui_dropdown_getselection(UiList *list) {
+UiListSelection ui_dropdown_getselection_impl(UiList *list) {
     UiListSelection sel = { 0, NULL };
     UiListView *listview = (UiListView*)list->obj;
     int index = (int)SendMessage(listview->widget.hwnd, CB_GETCURSEL, 0, 0);
@@ -469,7 +487,15 @@
     return sel;
 }
 
-void ui_dropdown_setselection(UiList *list, UiListSelection selection) {
+void ui_dropdown_setselection_impl(UiList *list, UiListSelection selection) {
     UiListView *listview = (UiListView*)list->obj;
     SendMessage(listview->widget.hwnd, CB_SETCURSEL, 0, 0);
 }
+
+void ui_dropdown_select(UIWIDGET dropdown, int index) {
+    SendMessage(dropdown->hwnd, CB_SETCURSEL, 0, 0);
+}
+
+int ui_dropdown_selection(UIWIDGET dropdown) {
+    return SendMessage(dropdown->hwnd, CB_GETCURSEL, 0, 0);
+}
--- a/ui/win32/list.h	Sat Dec 13 12:37:41 2025 +0100
+++ b/ui/win32/list.h	Sat Dec 13 12:47:36 2025 +0100
@@ -66,15 +66,15 @@
 W32Size ui_listview_get_preferred_size(W32Widget *widget);
 
 void ui_listview_update(UiList *list, int row);
-UiListSelection ui_listview_getselection(UiList *list);
-void ui_listview_setselection(UiList *list, UiListSelection selection);
+UiListSelection ui_listview_getselection_impl(UiList *list);
+void ui_listview_setselection_impl(UiList *list, UiListSelection selection);
 
 int ui_dropdown_eventproc(W32Widget *widget, HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
 W32Size ui_dropdown_get_preferred_size(W32Widget *widget);
 
 void ui_dropdown_update(UiList *list, int row);
-UiListSelection ui_dropdown_getselection(UiList *list);
-void ui_dropdown_setselection(UiList *list, UiListSelection selection);
+UiListSelection ui_dropdown_getselection_impl(UiList *list);
+void ui_dropdown_setselection_impl(UiList *list, UiListSelection selection);
 
 #ifdef __cplusplus
 }

mercurial