add label (WinUI3) newapi

Tue, 17 Oct 2023 21:00:48 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 17 Oct 2023 21:00:48 +0200
branch
newapi
changeset 219
527a66c0afb2
parent 218
d06e7e8e53e1
child 220
e377456302df

add label (WinUI3)

make/vs/testapp/main.c file | annotate | diff | comparison | revisions
ui/ui/display.h file | annotate | diff | comparison | revisions
ui/winui/label.cpp file | annotate | diff | comparison | revisions
ui/winui/label.h 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/winui.vcxproj file | annotate | diff | comparison | revisions
ui/winui/winui.vcxproj.filters file | annotate | diff | comparison | revisions
--- a/make/vs/testapp/main.c	Sat Oct 14 10:55:11 2023 +0200
+++ b/make/vs/testapp/main.c	Tue Oct 17 21:00:48 2023 +0200
@@ -255,7 +255,14 @@
                 ui_tab(obj, "Tab 2") {
                     ui_button(obj, .label = "Tab 2 Button");
                 }
+                ui_tab(obj, "Tab 3") {
+
+                }
             }
+            ui_newline(obj);
+
+            ui_label(obj, .label = "Test Label");
+            ui_newline(obj);
 
             ui_newline(obj);
             ui_textfield(obj, .value = wdata->t1);
--- a/ui/ui/display.h	Sat Oct 14 10:55:11 2023 +0200
+++ b/ui/ui/display.h	Tue Oct 17 21:00:48 2023 +0200
@@ -39,10 +39,44 @@
 extern "C" {
 #endif
     
+enum UiAlignment {
+    UI_ALIGN_DEFAULT = 0,
+    UI_ALIGN_LEFT,
+    UI_ALIGN_RIGHT,
+    UI_ALIGN_CENTER
+};
+
+typedef enum UiAlignment UiAlignment;
+
+typedef struct UiLabelArgs {
+    UiTri fill;
+    UiBool hexpand;
+    UiBool vexpand;
+    int colspan;
+    int rowspan;
+    int width;
+
+    const char* label;
+    UiAlignment align;
+    UiString* value;
+    const char* varname;
+} UiLabelArgs;
+
 /* label widgets */
-UIWIDGET ui_label(UiObject *obj, char *label);
-UIWIDGET ui_llabel(UiObject *obj, char *label);
-UIWIDGET ui_rlabel(UiObject *obj, char *label);
+
+#define ui_label(obj, ...) ui_label_create(obj, (UiLabelArgs) { __VA_ARGS__ })
+#define ui_llabel(obj, ...) ui_llabel_create(obj, (UiLabelArgs) { __VA_ARGS__ })
+#define ui_rlabel(obj, ...) ui_rlabel_create(obj, (UiLabelArgs) { __VA_ARGS__ })
+
+
+UIEXPORT UIWIDGET ui_label_create(UiObject* obj, UiLabelArgs args);
+UIEXPORT UIWIDGET ui_llabel_create(UiObject* obj, UiLabelArgs args);
+UIEXPORT UIWIDGET ui_rlabel_create(UiObject* obj, UiLabelArgs args);
+
+UIWIDGET ui_label_deprecated(UiObject *obj, char *label);
+UIWIDGET ui_llabel_deprecated(UiObject *obj, char *label);
+UIWIDGET ui_rlabel_deprecated(UiObject *obj, char *label);
+
 UIWIDGET ui_space(UiObject *obj);
 UIWIDGET ui_separator(UiObject *obj);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/winui/label.cpp	Tue Oct 17 21:00:48 2023 +0200
@@ -0,0 +1,104 @@
+/*
+ * 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 "pch.h"
+
+#include "label.h"
+#include "text.h"
+#include "util.h"
+
+#include "toolkit.h"
+#include "container.h"
+#include "../common/object.h"
+#include "../common/context.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_label_create(UiObject* obj, UiLabelArgs args) {
+    UiObject* current = uic_current_obj(obj);
+
+    // create textbox and toolkit wrapper
+    TextBlock label = TextBlock();
+    if (args.label) {
+        wchar_t* wlabel = str2wstr(args.label, nullptr);
+        label.Text(wlabel);
+        free(wlabel);
+    }
+
+    UIElement elm = label;
+    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_label_get;
+        value->set = ui_label_set;
+
+        // listener for notifying observers
+        // TODO:
+    }
+
+    // add button to current container
+    UI_APPLY_LAYOUT1(current, args);
+
+    current->container->Add(label, false);
+
+    return widget;
+}
+
+UIWIDGET ui_llabel_create(UiObject* obj, UiLabelArgs args) {
+    args.align = UI_ALIGN_LEFT;
+    return ui_label_create(obj, args);
+}
+
+UIWIDGET ui_rlabel_create(UiObject* obj, UiLabelArgs args) {
+    args.align = UI_ALIGN_RIGHT;
+    return ui_label_create(obj, args);
+}
+
+
+
+char* ui_label_get(UiString* str) {
+    UiWidget* widget = (UiWidget*)str->obj;
+    TextBlock box = widget->uielement.as<TextBlock>();
+    std::wstring wstr(box.Text());
+    return ui_string_get(str, wstr);
+}
+
+void  ui_label_set(UiString* str, const char* newvalue) {
+    UiWidget* widget = (UiWidget*)str->obj;
+    TextBox box = widget->uielement.as<TextBox>();
+    box.Text(ui_string_set(str, newvalue));
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/winui/label.h	Tue Oct 17 21:00:48 2023 +0200
@@ -0,0 +1,36 @@
+/*
+ * 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/toolkit.h"
+#include "../ui/display.h"
+
+extern "C" char* ui_label_get(UiString * str);
+extern "C" void  ui_label_set(UiString * str, const char* newvalue);
\ No newline at end of file
--- a/ui/winui/text.cpp	Sat Oct 14 10:55:11 2023 +0200
+++ b/ui/winui/text.cpp	Tue Oct 17 21:00:48 2023 +0200
@@ -107,7 +107,7 @@
 
 // -------------------------- getter/setter for textfield UiString --------------------------
 
-static char* tf_get(UiString* str, std::wstring &value) {
+char* ui_string_get(UiString* str, std::wstring &value) {
     if (str->value.ptr) {
         str->value.free(str->value.ptr);
     }
@@ -118,7 +118,7 @@
     return str->value.ptr;
 }
 
-static std::wstring tf_set(UiString* str, const char* value) {
+std::wstring ui_string_set(UiString* str, const char* value) {
     if (str->value.ptr) {
         str->value.free(str->value.ptr);
     }
@@ -134,29 +134,29 @@
     return s;
 }
 
-extern "C" char* ui_textfield_get(UiString * str) {
+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);
+    return ui_string_get(str, wstr);
 }
 
-extern "C" void  ui_textfield_set(UiString * str, const char* newvalue) {
+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));
+    box.Text(ui_string_set(str, newvalue));
 }
 
 
-extern "C" char* ui_passwordfield_get(UiString * str) {
+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);
+    return ui_string_get(str, wstr);
 }
 
-extern "C" void  ui_passwordfield_set(UiString * str, const char* newvalue) {
+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));
+    box.Password(ui_string_set(str, newvalue));
 }
--- a/ui/winui/text.h	Sat Oct 14 10:55:11 2023 +0200
+++ b/ui/winui/text.h	Tue Oct 17 21:00:48 2023 +0200
@@ -33,6 +33,10 @@
 
 #include "../ui/container.h"
 
+
+char* ui_string_get(UiString* str, std::wstring& value);
+std::wstring ui_string_set(UiString* str, const char* value);
+
 extern "C" char* ui_textfield_get(UiString *str);
 extern "C" void  ui_textfield_set(UiString *str, const char *newvalue);
 
--- a/ui/winui/winui.vcxproj	Sat Oct 14 10:55:11 2023 +0200
+++ b/ui/winui/winui.vcxproj	Tue Oct 17 21:00:48 2023 +0200
@@ -135,6 +135,7 @@
     <ClInclude Include="commandbar.h" />
     <ClInclude Include="container.h" />
     <ClInclude Include="icons.h" />
+    <ClInclude Include="label.h" />
     <ClInclude Include="list.h" />
     <ClInclude Include="pch.h" />
     <ClInclude Include="App.xaml.h">
@@ -160,6 +161,7 @@
     <ClCompile Include="commandbar.cpp" />
     <ClCompile Include="container.cpp" />
     <ClCompile Include="icons.cpp" />
+    <ClCompile Include="label.cpp" />
     <ClCompile Include="list.cpp" />
     <ClCompile Include="pch.cpp">
       <PrecompiledHeader>Create</PrecompiledHeader>
--- a/ui/winui/winui.vcxproj.filters	Sat Oct 14 10:55:11 2023 +0200
+++ b/ui/winui/winui.vcxproj.filters	Tue Oct 17 21:00:48 2023 +0200
@@ -25,6 +25,7 @@
     <ClCompile Include="window.cpp" />
     <ClCompile Include="stock.cpp" />
     <ClCompile Include="icons.cpp" />
+    <ClCompile Include="label.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="pch.h" />
@@ -91,6 +92,7 @@
     </ClInclude>
     <ClInclude Include="stock.h" />
     <ClInclude Include="icons.h" />
+    <ClInclude Include="label.h" />
   </ItemGroup>
   <ItemGroup>
     <Image Include="Assets\Wide310x150Logo.scale-200.png">

mercurial