implement textfield (WinUI3) newapi

Sun, 01 Oct 2023 18:54:23 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 01 Oct 2023 18:54:23 +0200
branch
newapi
changeset 192
bcacd00ea955
parent 191
6113ed66d258
child 193
74c688cc1839

implement textfield (WinUI3)

make/vs/testapp/main.c file | annotate | diff | comparison | revisions
ui/common/context.c file | annotate | diff | comparison | revisions
ui/common/context.h file | annotate | diff | comparison | revisions
ui/common/object.h file | annotate | diff | comparison | revisions
ui/ui/text.h file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
ui/winui/button.cpp file | annotate | diff | comparison | revisions
ui/winui/text.cpp file | annotate | diff | comparison | revisions
ui/winui/text.h file | annotate | diff | comparison | revisions
ui/winui/util.cpp file | annotate | diff | comparison | revisions
ui/winui/util.h file | annotate | diff | comparison | revisions
ui/winui/winui.vcxproj file | annotate | diff | comparison | revisions
ui/winui/winui.vcxproj.filters file | annotate | diff | comparison | revisions
--- a/make/vs/testapp/main.c	Sun Oct 01 17:22:17 2023 +0200
+++ b/make/vs/testapp/main.c	Sun Oct 01 18:54:23 2023 +0200
@@ -38,6 +38,8 @@
     UiInteger* check;
     UiInteger* toggle;
     UiInteger* radio;
+    UiString* text;
+    UiString* password;
 } WindowData;
 
 void action1(UiEvent* event, void* data) {
@@ -72,6 +74,8 @@
     wdata->check = ui_int_new(obj->ctx, "check");
     wdata->toggle = ui_int_new(obj->ctx, "toggle");
     wdata->radio = ui_int_new(obj->ctx, "radio");
+    wdata->text = ui_string_new(obj->ctx, "text");
+    wdata->password = ui_string_new(obj->ctx, "password");
 
     ui_grid(obj, .margin=10, .columnspacing=5, .rowspacing=20) {
         ui_button(obj, .label="Button1", .onclick=action1, .onclickdata="action1");
@@ -98,6 +102,10 @@
         ui_newline(obj);
         ui_radiobutton(obj, .label = "Radio 4", .value = wdata->radio);
         ui_switch(obj, .label = "test", .onchange=action_switch);
+        ui_newline(obj);
+
+        ui_textfield(obj, .value = wdata->text);
+        ui_passwordfield(obj, .value = wdata->password);
     }
 
     ui_show(obj);
--- a/ui/common/context.c	Sun Oct 01 17:22:17 2023 +0200
+++ b/ui/common/context.c	Sun Oct 01 18:54:23 2023 +0200
@@ -247,6 +247,18 @@
     return val;
 }
 
+
+UiVar* uic_widget_var(UiContext* toplevel, UiContext* current, void* value, const char* varname, UiVarType type) {
+    if (value) {
+        return uic_create_value_var(current, value);
+    }
+    if (varname) {
+        return uic_create_var(toplevel, varname, type);
+    }
+    return NULL;
+}
+
+
 void uic_copy_binding(UiVar *from, UiVar *to, UiBool copytodoc) {
     // check type
     if(from->type != to->type) {
--- a/ui/common/context.h	Sun Oct 01 17:22:17 2023 +0200
+++ b/ui/common/context.h	Sun Oct 01 18:54:23 2023 +0200
@@ -117,6 +117,8 @@
 UiVar* uic_create_value_var(UiContext *ctx, void *value);
 void* uic_create_value(UiContext *ctx, UiVarType type);
 
+UiVar* uic_widget_var(UiContext* toplevel, UiContext* current, void* value, const char* varname, UiVarType type);
+
 void uic_copy_binding(UiVar *from, UiVar *to, UiBool copytodoc);
 void uic_save_var2(UiVar *var);
 void uic_unbind_var(UiVar *var);
--- a/ui/common/object.h	Sun Oct 01 17:22:17 2023 +0200
+++ b/ui/common/object.h	Sun Oct 01 18:54:23 2023 +0200
@@ -39,7 +39,7 @@
 void uic_obj_add(UiObject *toplevel, UiObject *ctobj);
 UiObject* uic_current_obj(UiObject *toplevel);
 
-UiContainer* uic_get_current_container(UiObject *obj);
+UiContainer* uic_get_current_container(UiObject *obj);;
 
 
 #ifdef	__cplusplus
--- a/ui/ui/text.h	Sun Oct 01 17:22:17 2023 +0200
+++ b/ui/ui/text.h	Sun Oct 01 18:54:23 2023 +0200
@@ -35,6 +35,20 @@
 extern "C" {
 #endif
 
+typedef struct UiTextFieldArgs {
+    UiTri fill;
+    UiBool hexpand;
+    UiBool vexpand;
+    int colspan;
+    int rowspan;
+    int width;
+
+    UiString* value;
+    const char* varname;
+    ui_callback onchange;
+    void* onchangedata;
+} UiTextFieldArgs;
+
 UIWIDGET ui_textarea(UiObject *obj, UiText *value);
 UIWIDGET ui_textarea_nv(UiObject *obj, char *varname);
 
@@ -43,20 +57,13 @@
 void ui_text_undo(UiText *value);
 void ui_text_redo(UiText *value);
 
-UIWIDGET ui_textfield(UiObject *obj, UiString *value);
-UIWIDGET ui_textfield_nv(UiObject *obj, char *varname);
-
-UIWIDGET ui_textfield_w(UiObject *obj, int width, UiString *value);
-UIWIDGET ui_textfield_wnv(UiObject *obj, int width, char *varname);
+#define ui_textfield(obj, ...) ui_textfield_create(obj, (UiTextFieldArgs) { __VA_ARGS__ })
+#define ui_frameless_textfield(obj, ...) ui_frameless_field_create(obj, (UiTextFieldArgs) { __VA_ARGS__ })
+#define ui_passwordfield(obj, ...) ui_passwordfield_create(obj, (UiTextFieldArgs) { __VA_ARGS__ })
 
-UIWIDGET ui_frameless_textfield(UiObject *obj, UiString *value);
-UIWIDGET ui_frameless_textfield_nv(UiObject *obj, char *varname);
-
-UIWIDGET ui_passwordfield(UiObject *obj, UiString *value);
-UIWIDGET ui_passwordfield_nv(UiObject *obj, char *varname);
-UIWIDGET ui_passwordfield_w(UiObject *obj, int width, UiString *value);
-UIWIDGET ui_passwordfield_wnv(UiObject *obj, int width, char *varname);
-
+UIWIDGET ui_textfield_create(UiObject *obj, UiTextFieldArgs args);
+UIWIDGET ui_frameless_textfield_create(UiObject* obj, UiTextFieldArgs args);
+UIWIDGET ui_passwordfield_create(UiObject* obj, UiTextFieldArgs args);
         
 #ifdef	__cplusplus
 }
--- a/ui/ui/toolkit.h	Sun Oct 01 17:22:17 2023 +0200
+++ b/ui/ui/toolkit.h	Sun Oct 01 18:54:23 2023 +0200
@@ -276,7 +276,7 @@
 
 struct UiString {
     char* (*get)(UiString*);
-    void  (*set)(UiString*, char*);
+    void  (*set)(UiString*, const char*);
     void  *obj;
     
     UiStr value;
--- a/ui/winui/button.cpp	Sun Oct 01 17:22:17 2023 +0200
+++ b/ui/winui/button.cpp	Sun Oct 01 18:54:23 2023 +0200
@@ -159,13 +159,7 @@
 	ui_context_add_widget_destructor(current->ctx, widget);
 
 	// bind variable
-	UiVar* var = nullptr;
-	if (args.value) {
-		var = uic_create_value_var(current->ctx, args.value);
-	}
-	else if (args.varname) {
-		var = uic_create_var(obj->ctx, args.varname, UI_VAR_INTEGER);
-	}
+	UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_INTEGER);
 	if (var) {
 		UiInteger* value = (UiInteger*)var->value;
 		value->obj = widget;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/winui/text.cpp	Sun Oct 01 18:54:23 2023 +0200
@@ -0,0 +1,160 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2023 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "text.h"
+
+#include "../common/context.h"
+#include "../common/object.h"
+
+#include "util.h"
+#include "container.h"
+
+using namespace winrt;
+using namespace Microsoft::UI::Xaml;
+using namespace Microsoft::UI::Xaml::Controls;
+using namespace Windows::UI::Xaml::Interop;
+using namespace winrt::Windows::Foundation;
+using namespace winrt::Microsoft::UI::Xaml::Controls::Primitives;
+
+
+UIWIDGET ui_textfield_create(UiObject* obj, UiTextFieldArgs args) {
+    UiObject* current = uic_current_obj(obj);
+
+    // create textbox and toolkit wrapper
+    TextBox textfield = TextBox();
+    UIElement elm = textfield;
+    UiWidget* widget = new UiWidget(elm);
+    ui_context_add_widget_destructor(current->ctx, widget);
+
+    UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_STRING);
+    if (var) {
+        UiString* value = (UiString*)var->value;
+        value->obj = widget;
+        value->get = ui_textfield_get;
+        value->set = ui_textfield_set;
+
+        // listener for notifying observers
+        // TODO:
+    }
+    
+    // add button to current container
+    UI_APPLY_LAYOUT1(current, args);
+
+    current->container->Add(textfield, false);
+
+    return widget;
+}
+
+UIWIDGET ui_frameless_textfield_create(UiObject* obj, UiTextFieldArgs args) {
+    return ui_textfield_create(obj, args);
+}
+
+UIWIDGET ui_passwordfield_create(UiObject* obj, UiTextFieldArgs args) {
+    UiObject* current = uic_current_obj(obj);
+
+    // create textbox and toolkit wrapper
+    PasswordBox textfield = PasswordBox();
+    UIElement elm = textfield;
+    UiWidget* widget = new UiWidget(elm);
+    ui_context_add_widget_destructor(current->ctx, widget);
+
+    UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_STRING);
+    if (var) {
+        UiString* value = (UiString*)var->value;
+        value->obj = widget;
+        value->get = ui_passwordfield_get;
+        value->set = ui_passwordfield_set;
+
+        // listener for notifying observers
+        // TODO:
+    }
+
+    // add button to current container
+    UI_APPLY_LAYOUT1(current, args);
+
+    current->container->Add(textfield, false);
+
+    return widget;
+}
+
+
+// -------------------------- getter/setter for textfield UiString --------------------------
+
+static char* tf_get(UiString* str, std::wstring &value) {
+    if (str->value.ptr) {
+        str->value.free(str->value.ptr);
+    }
+
+    str->value.ptr = wchar2utf8(value.c_str(), value.length());
+    str->value.free = free;
+
+    return str->value.ptr;
+}
+
+static std::wstring tf_set(UiString* str, const char* value) {
+    if (str->value.ptr) {
+        str->value.free(str->value.ptr);
+    }
+
+    str->value.ptr = _strdup(value);
+    str->value.free = free;
+
+    int len;
+    wchar_t* wstr = str2wstr(value, &len);
+    std::wstring s(wstr);
+    free(wstr);
+
+    return s;
+}
+
+extern "C" char* ui_textfield_get(UiString * str) {
+    UiWidget* widget = (UiWidget*)str->obj;
+    TextBox box = widget->uielement.as<TextBox>();
+    std::wstring wstr(box.Text());
+    return tf_get(str, wstr);
+}
+
+extern "C" void  ui_textfield_set(UiString * str, const char* newvalue) {
+    UiWidget* widget = (UiWidget*)str->obj;
+    TextBox box = widget->uielement.as<TextBox>();
+    box.Text(tf_set(str, newvalue));
+}
+
+
+extern "C" char* ui_passwordfield_get(UiString * str) {
+    UiWidget* widget = (UiWidget*)str->obj;
+    PasswordBox box = widget->uielement.as<PasswordBox>();
+    std::wstring wstr(box.Password());
+    return tf_get(str, wstr);
+}
+
+extern "C" void  ui_passwordfield_set(UiString * str, const char* newvalue) {
+    UiWidget* widget = (UiWidget*)str->obj;
+    PasswordBox box = widget->uielement.as<PasswordBox>();
+    box.Password(tf_set(str, newvalue));
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/winui/text.h	Sun Oct 01 18:54:23 2023 +0200
@@ -0,0 +1,40 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2023 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include "../ui/text.h"
+#include "toolkit.h"
+
+#include "../ui/container.h"
+
+extern "C" char* ui_textfield_get(UiString *str);
+extern "C" void  ui_textfield_set(UiString *str, const char *newvalue);
+
+extern "C" char* ui_passwordfield_get(UiString * str);
+extern "C" void  ui_passwordfield_set(UiString * str, const char* newvalue);
--- a/ui/winui/util.cpp	Sun Oct 01 17:22:17 2023 +0200
+++ b/ui/winui/util.cpp	Sun Oct 01 18:54:23 2023 +0200
@@ -26,4 +26,20 @@
     wstr[wlen] = 0;
 
     return wstr;
-}
\ No newline at end of file
+}
+
+char* wchar2utf8(const wchar_t* wstr, size_t wlen) {
+    size_t maxlen = wlen * 4;
+    char* ret = (char*)malloc(maxlen + 1);
+    int ret_len = WideCharToMultiByte(
+        CP_UTF8,
+        0,
+        wstr,
+        wlen,
+        ret,
+        maxlen,
+        NULL,
+        NULL);
+    ret[ret_len] = 0;
+    return ret;
+}
--- a/ui/winui/util.h	Sun Oct 01 17:22:17 2023 +0200
+++ b/ui/winui/util.h	Sun Oct 01 18:54:23 2023 +0200
@@ -1,3 +1,5 @@
 #pragma once
 
 wchar_t* str2wstr(const char* str, int* newlen);
+
+char* wchar2utf8(const wchar_t* wstr, size_t wlen);
--- a/ui/winui/winui.vcxproj	Sun Oct 01 17:22:17 2023 +0200
+++ b/ui/winui/winui.vcxproj	Sun Oct 01 18:54:23 2023 +0200
@@ -153,6 +153,7 @@
     <ClCompile Include="appmenu.cpp" />
     <ClCompile Include="button.cpp" />
     <ClCompile Include="container.cpp" />
+    <ClCompile Include="text.cpp" />
     <ClCompile Include="toolkit.cpp" />
     <ClCompile Include="util.cpp" />
     <ClCompile Include="window.cpp" />
@@ -188,6 +189,7 @@
     <ClInclude Include="appmenu.h" />
     <ClInclude Include="button.h" />
     <ClInclude Include="container.h" />
+    <ClInclude Include="text.h" />
     <ClInclude Include="toolkit.h" />
     <ClInclude Include="util.h" />
     <ClInclude Include="window.h" />
--- a/ui/winui/winui.vcxproj.filters	Sun Oct 01 17:22:17 2023 +0200
+++ b/ui/winui/winui.vcxproj.filters	Sun Oct 01 18:54:23 2023 +0200
@@ -63,6 +63,9 @@
     <ClCompile Include="button.cpp">
       <Filter>Quelldateien</Filter>
     </ClCompile>
+    <ClCompile Include="text.cpp">
+      <Filter>Quelldateien</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <None Include="packages.config" />
@@ -158,5 +161,8 @@
     <ClInclude Include="..\ui\window.h">
       <Filter>public</Filter>
     </ClInclude>
+    <ClInclude Include="text.h">
+      <Filter>Headerdateien</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>
\ No newline at end of file

mercurial