add combobox (WinUI3) newapi

Mon, 02 Oct 2023 19:56:18 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 02 Oct 2023 19:56:18 +0200
branch
newapi
changeset 197
8a82ebe23822
parent 196
320d85f3cd14
child 198
f2332d0d3318

add combobox (WinUI3)

make/vs/testapp/main.c file | annotate | diff | comparison | revisions
ui/winui/list.cpp file | annotate | diff | comparison | revisions
--- a/make/vs/testapp/main.c	Mon Oct 02 19:31:06 2023 +0200
+++ b/make/vs/testapp/main.c	Mon Oct 02 19:56:18 2023 +0200
@@ -149,7 +149,7 @@
             }
             */
 
-            ui_listview(obj, .list = wdata->list, .hexpand = true, .vexpand = true, .multiselection = true, .onselection= action_listselection_changed, .onactivate= action_onactivate);
+            ui_combobox(obj, .list = wdata->list, .onselection= action_listselection_changed, .onactivate= action_onactivate);
         }
     }   
 
--- a/ui/winui/list.cpp	Mon Oct 02 19:31:06 2023 +0200
+++ b/ui/winui/list.cpp	Mon Oct 02 19:56:18 2023 +0200
@@ -116,6 +116,55 @@
     return widget;
 }
 
+
+UIWIDGET ui_combobox_create(UiObject* obj, UiListArgs args) {
+    UiObject* current = uic_current_obj(obj);
+
+    // create listview and toolkit wrapper
+    ComboBox combobox = ComboBox();
+
+    UIElement elm = combobox;
+    UiWidget* widget = new UiWidget(elm);
+    widget->data1 = args.model;
+    widget->data2 = args.getvalue;
+    ui_context_add_widget_destructor(current->ctx, widget);
+
+    // bind var
+    UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST);
+    if (var) {
+        UiList* list = (UiList*)var->value;
+        list->update = ui_simple_list_update;
+        list->obj = widget;
+        ui_simple_list_update(list, 0);
+    }
+
+    if (args.onactivate) {
+        ui_callback cb = args.onactivate;
+        void* cbdata = args.onactivatedata;
+        combobox.SelectionChanged([cb, cbdata, obj](IInspectable const& sender, RoutedEventArgs evtargs) {
+            int selectedrow = sender.as<ComboBox>().SelectedIndex();
+            UiListSelection selection;
+            selection.count = 1;
+            selection.rows = &selectedrow;
+
+            UiEvent evt;
+            evt.obj = obj;
+            evt.window = obj->window;
+            evt.document = obj->ctx->document;
+            evt.eventdata = &selection;
+            evt.intval = selectedrow;
+            cb(&evt, cbdata);
+            });
+    }
+
+    // add listview to current container
+    UI_APPLY_LAYOUT1(current, args);
+
+    current->container->Add(combobox, false);
+
+    return widget;
+}
+
 static void* getstrvalue(void* elm, int ignore) {
     return elm;
 }
@@ -124,7 +173,7 @@
     UiWidget* widget = (UiWidget*)list->obj;
     UiModel* model = (UiModel*)widget->data1;
     ui_getvaluefunc getvalue = (ui_getvaluefunc)widget->data2;
-    ListView listview = widget->uielement.as<ListView>();
+    ItemsControl listview = widget->uielement.as<ItemsControl>();
     auto items = listview.Items();
 
     // priority: getvalue, model.getvalue, getstrvalue (fallback)

mercurial