add getter/setter for ui number/string types newapi

Sun, 28 Jan 2024 10:06:54 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 28 Jan 2024 10:06:54 +0100
branch
newapi
changeset 231
e160bb392148
parent 230
4f5b32a2a60f
child 232
e2b33055113f

add getter/setter for ui number/string types

make/vs/testapp/main.c file | annotate | diff | comparison | revisions
make/vs/testapp/testapp.vcxproj file | annotate | diff | comparison | revisions
ui/common/types.c file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
ui/winui/label.cpp file | annotate | diff | comparison | revisions
ui/winui/text.cpp file | annotate | diff | comparison | revisions
ui/winui/text.h file | annotate | diff | comparison | revisions
--- a/make/vs/testapp/main.c	Sun Jan 28 09:35:44 2024 +0100
+++ b/make/vs/testapp/main.c	Sun Jan 28 10:06:54 2024 +0100
@@ -60,16 +60,16 @@
     char* action = data;
     
     WindowData* wdata = event->window;
-    int64_t is_checked = wdata->check->get(wdata->check);
-    int64_t radio = wdata->radio->get(wdata->radio);
+    int64_t is_checked = ui_get(wdata->check);
+    int64_t radio = ui_get(wdata->radio);
 
     printf("data: %s %d\n", data, is_checked);
 
-    double d = wdata->progress->get(wdata->progress);
-    wdata->progress->set(wdata->progress, d + 1);
+    double d = ui_get(wdata->progress);
+    ui_set(wdata->progress, d + 1);
 
-    int spinner_active = wdata->spinner->get(wdata->spinner);
-    wdata->spinner->set(wdata->spinner, !spinner_active);
+    int spinner_active = ui_get(wdata->spinner);
+    ui_set(wdata->spinner, !spinner_active);
 
     ui_list_append(menuList, "List Item X");
     ui_list_append(menuList, "List Item X");
--- a/make/vs/testapp/testapp.vcxproj	Sun Jan 28 09:35:44 2024 +0100
+++ b/make/vs/testapp/testapp.vcxproj	Sun Jan 28 10:06:54 2024 +0100
@@ -115,6 +115,7 @@
       <PreprocessorDefinitions>_DEBUG;_CONSOLE;UI_WINUI;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <ConformanceMode>true</ConformanceMode>
       <AdditionalIncludeDirectories>C:\Users\Olaf\Projekte\toolkit\ui;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+      <LanguageStandard_C>stdc17</LanguageStandard_C>
     </ClCompile>
     <Link>
       <SubSystem>Windows</SubSystem>
--- a/ui/common/types.c	Sun Jan 28 09:35:44 2024 +0100
+++ b/ui/common/types.c	Sun Jan 28 10:06:54 2024 +0100
@@ -277,6 +277,66 @@
 }
 
 
+void ui_int_set(UiInteger* i, int64_t value) {
+    if (i && i->set) {
+        i->set(i, value);
+    }
+}
+
+int64_t ui_int_get(UiInteger* i) {
+    if (i) {
+        return i->get ? i->get(i) : i->value;
+    } else {
+        return 0;
+    }
+}
+
+void ui_double_set(UiDouble* d, double value) {
+    if (d && d->set) {
+        d->set(d, value);
+    }
+}
+
+double ui_double_get(UiDouble* d) {
+    if (d) {
+        return d->get ? d->get(d) : d->value;
+    }
+    else {
+        return 0;
+    }
+}
+
+void ui_string_set(UiString* s, const char* value) {
+    if (s && s->set) {
+        s->set(s, value);
+    }
+}
+
+char* ui_string_get(UiString* s) {
+    if (s) {
+        return s->get ? s->get(s) : s->value.ptr;
+    }
+    else {
+        return 0;
+    }
+}
+
+void ui_text_set(UiText* s, const char* value) {
+    if (s && s->set) {
+        s->set(s, value);
+    }
+}
+
+char* ui_text_get(UiText* s) {
+    if (s) {
+        return s->get ? s->get(s) : s->value.ptr;
+    }
+    else {
+        return 0;
+    }
+}
+
+
 // private functions
 void uic_int_copy(UiInteger *from, UiInteger *to) {
     to->get = from->get;
--- a/ui/ui/toolkit.h	Sun Jan 28 09:35:44 2024 +0100
+++ b/ui/ui/toolkit.h	Sun Jan 28 10:06:54 2024 +0100
@@ -406,6 +406,28 @@
 UIEXPORT UiText* ui_text_new(UiContext *ctx, char *name);
 UIEXPORT UiRange* ui_range_new(UiContext *ctx, char *name);
 
+#define ui_get(v) _Generic(v, \
+    UiInteger*: ui_int_get, \
+    UiDouble*: ui_double_get, \
+    UiString*: ui_string_get, \
+    UiText*:ui_text_get) (v)
+
+#define ui_set(v, n) _Generic(v, \
+    UiInteger*: ui_int_set, \
+    UiDouble*: ui_double_set, \
+    UiString*: ui_string_set, \
+    UiText*:ui_text_set) (v, n)
+
+UIEXPORT void ui_int_set(UiInteger *i, int64_t value);
+UIEXPORT int64_t ui_int_get(UiInteger *i);
+UIEXPORT void ui_double_set(UiDouble *d, double value);
+UIEXPORT double ui_double_get(UiDouble *d);
+UIEXPORT void ui_string_set(UiString *s, const char *value);
+UIEXPORT char* ui_string_get(UiString *s);
+UIEXPORT void ui_text_set(UiText *s, const char* value);
+UIEXPORT char* ui_text_get(UiText *s);
+
+
 UIEXPORT UiObserver* ui_observer_new(ui_callback f, void *data);
 UIEXPORT UiObserver* ui_obsvlist_add(UiObserver *list, UiObserver *observer);
 UIEXPORT UiObserver* ui_add_observer(UiObserver *list, ui_callback f, void *data);
--- a/ui/winui/label.cpp	Sun Jan 28 09:35:44 2024 +0100
+++ b/ui/winui/label.cpp	Sun Jan 28 10:06:54 2024 +0100
@@ -94,13 +94,13 @@
     UiWidget* widget = (UiWidget*)str->obj;
     TextBlock box = widget->uielement.as<TextBlock>();
     std::wstring wstr(box.Text());
-    return ui_string_get(str, wstr);
+    return ui_wstring_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));
+    box.Text(ui_wstring_set(str, newvalue));
 }
 
 
--- a/ui/winui/text.cpp	Sun Jan 28 09:35:44 2024 +0100
+++ b/ui/winui/text.cpp	Sun Jan 28 10:06:54 2024 +0100
@@ -114,7 +114,7 @@
 
 // -------------------------- getter/setter for textfield UiString --------------------------
 
-char* ui_string_get(UiString* str, std::wstring &value) {
+char* ui_wstring_get(UiString* str, std::wstring &value) {
     if (str->value.ptr) {
         str->value.free(str->value.ptr);
     }
@@ -125,7 +125,7 @@
     return str->value.ptr;
 }
 
-std::wstring ui_string_set(UiString* str, const char* value) {
+std::wstring ui_wstring_set(UiString* str, const char* value) {
     if (str->value.ptr) {
         str->value.free(str->value.ptr);
     }
@@ -145,13 +145,13 @@
     UiWidget* widget = (UiWidget*)str->obj;
     TextBox box = widget->uielement.as<TextBox>();
     std::wstring wstr(box.Text());
-    return ui_string_get(str, wstr);
+    return ui_wstring_get(str, wstr);
 }
 
 void  ui_textfield_set(UiString * str, const char* newvalue) {
     UiWidget* widget = (UiWidget*)str->obj;
     TextBox box = widget->uielement.as<TextBox>();
-    box.Text(ui_string_set(str, newvalue));
+    box.Text(ui_wstring_set(str, newvalue));
 }
 
 
@@ -159,13 +159,13 @@
     UiWidget* widget = (UiWidget*)str->obj;
     PasswordBox box = widget->uielement.as<PasswordBox>();
     std::wstring wstr(box.Password());
-    return ui_string_get(str, wstr);
+    return ui_wstring_get(str, wstr);
 }
 
 void  ui_passwordfield_set(UiString * str, const char* newvalue) {
     UiWidget* widget = (UiWidget*)str->obj;
     PasswordBox box = widget->uielement.as<PasswordBox>();
-    box.Password(ui_string_set(str, newvalue));
+    box.Password(ui_wstring_set(str, newvalue));
 }
 
 
@@ -304,13 +304,13 @@
     UiPathTextField* widget = (UiPathTextField*)str->obj;
     TextBox box = widget->textbox;
     std::wstring wstr(box.Text());
-    return ui_string_get(str, wstr);
+    return ui_wstring_get(str, wstr);
 }
 
 void  ui_path_textfield_set(UiString* str, const char* newvalue) {
     UiPathTextField* widget = (UiPathTextField*)str->obj;
     TextBox box = widget->textbox;
-    box.Text(ui_string_set(str, newvalue));
+    box.Text(ui_wstring_set(str, newvalue));
     ui_pathtextfield_update(widget, newvalue);
 }
 
--- a/ui/winui/text.h	Sun Jan 28 09:35:44 2024 +0100
+++ b/ui/winui/text.h	Sun Jan 28 10:06:54 2024 +0100
@@ -60,8 +60,8 @@
     void* ondropdata;
 };
 
-char* ui_string_get(UiString* str, std::wstring& value);
-std::wstring ui_string_set(UiString* str, const char* value);
+char* ui_wstring_get(UiString* str, std::wstring& value);
+std::wstring ui_wstring_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);

mercurial