implement new api style for ui_button (WinUI3) newapi

Sun, 01 Oct 2023 09:23:47 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 01 Oct 2023 09:23:47 +0200
branch
newapi
changeset 186
5db4979bf482
parent 185
4a8b1a748f09
child 187
24ce2c326d85

implement new api style for ui_button (WinUI3)

make/vs/testapp/main.c file | annotate | diff | comparison | revisions
ui/ui/button.h file | annotate | diff | comparison | revisions
ui/ui/container.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/container.cpp file | annotate | diff | comparison | revisions
ui/winui/container.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 Sep 24 12:03:57 2023 +0200
+++ b/make/vs/testapp/main.c	Sun Oct 01 09:23:47 2023 +0200
@@ -30,38 +30,30 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 
 #include <ui/ui.h>
 
 
 void action1(UiEvent* event, void* data) {
     char* action = data;
-    printf("hello world!\n");
+    printf("data: %s!\n", data);
 }
 
 void application_startup(UiEvent* event, void* data) {
     UiObject* obj = ui_window("Test", NULL);
 
-    /*
-    ui_button(obj, "Button1", action1, "action1");
-    ui_button(obj, "Button2", action1, "action2");
-    ui_layout_fill(obj, TRUE);
-    ui_button(obj, "Button3", action1, "action3");
-    */
-
     UI_GRID_SP(obj, 10, 5, 20) {
-        ui_button(obj, "Button1", action1, "action1");
-        ui_button(obj, "Button2", action1, "action2");
-        ui_layout_hexpand(obj, 1);
-        ui_button(obj, "Button3", action1, "action3");
+        ui_button(obj, .label="Button1", .onclick=action1, .onclickdata="action1");
+        ui_button(obj, .label="Button2", .onclick=action1, .onclickdata="action2");
+        ui_button(obj, .label="Button3", .onclick=action1, .onclickdata="action3", .hexpand=true);
         ui_newline(obj);
 
-        ui_button(obj, "Button4", action1, "action4");
-        ui_layout_gridwidth(obj, 2);
-        ui_button(obj, "Button5_", action1, "action5");
+        ui_button(obj, .label="Button4", .onclick=action1, .onclickdata="action4");
+        ui_button(obj, .label="Button5", .onclick=action1, .onclickdata="action5", .colspan=2);
         ui_newline(obj);
 
-        ui_button(obj, "Very Long Button Label Text ____________ Test", action1, "test");
+        ui_button(obj, .label="Very Long Button Label Text ____________ Test", .onclick=action1, .onclickdata="test");
     }
 
     ui_show(obj);
--- a/ui/ui/button.h	Sun Sep 24 12:03:57 2023 +0200
+++ b/ui/ui/button.h	Sun Oct 01 09:23:47 2023 +0200
@@ -34,14 +34,42 @@
 #ifdef	__cplusplus
 extern "C" {
 #endif
-    
-UIWIDGET ui_button(UiObject *obj, char *label, ui_callback f, void *data);
+
+typedef struct UiButtonArgs {
+    UiTri fill;
+    UiBool hexpand;
+    UiBool vexpand;
+    int colspan;
+    int rowspan;
+
+    char* label;
+    char* stockid;
+    ui_callback onclick;
+    void* onclickdata;
+} UiButtonArgs;
 
-UIWIDGET ui_checkbox(UiObject *obj, char *label, UiInteger *value);
-UIWIDGET ui_checkbox_nv(UiObject *obj, char *label, char *varname);
+typedef struct UiToggleArgs {
+    char* label;
+    char* stockid;
+    UiInteger* value;
+    char* varname;
+} UiToggleArgs;
+   
+#define ui_button(obj, ...) ui_button_create(obj, (UiButtonArgs){ __VA_ARGS__ } )
+#define ui_checkbox(obj, ...) ui_checkbox_create(obj, (UiToggleArgs){ __VA_ARGS__ } )
+#define ui_radiobutton(obj, ...) ui_checkbox_create(obj, (UiToggleArgs){ __VA_ARGS__ } )
 
-UIWIDGET ui_radiobutton(UiObject *obj, char *label, UiInteger *rgroup);
-UIWIDGET ui_radiobutton_nv(UiObject *obj, char *label, char *varname);
+UIWIDGET ui_button_create(UiObject* obj, UiButtonArgs args);
+UIWIDGET ui_checkbox_create(UiObject* obj, UiToggleArgs args);
+UIWIDGET ui_radiobutton_create(UiObject* obj, UiToggleArgs);
+
+UIWIDGET deprecated_ui_button(UiObject *obj, char *label, ui_callback f, void *data);
+
+UIWIDGET deprecated_ui_checkbox(UiObject *obj, char *label, UiInteger *value);
+UIWIDGET deprecated_ui_checkbox_nv(UiObject *obj, char *label, char *varname);
+
+UIWIDGET deprecated_ui_radiobutton(UiObject *obj, char *label, UiInteger *rgroup);
+UIWIDGET deprecated_ui_radiobutton_nv(UiObject *obj, char *label, char *varname);
 
 
 #ifdef	__cplusplus
--- a/ui/ui/container.h	Sun Sep 24 12:03:57 2023 +0200
+++ b/ui/ui/container.h	Sun Oct 01 09:23:47 2023 +0200
@@ -34,7 +34,8 @@
 #ifdef __cplusplus
 extern "C" {
 #endif
-    
+   
+
 #define UI_CTN(obj, ctn) for(ctn;ui_container_finish(obj);ui_container_begin_close(obj))
 #define UI_VBOX(obj) for(ui_vbox(obj);ui_container_finish(obj);ui_container_begin_close(obj))
 #define UI_HBOX(obj) for(ui_hbox(obj);ui_container_finish(obj);ui_container_begin_close(obj))
@@ -43,6 +44,7 @@
 #define UI_GRID(obj) for(ui_grid(obj);ui_container_finish(obj);ui_container_begin_close(obj))
 #define UI_GRID_SP(obj, margin, columnspacing, rowspacing) for(ui_grid_sp(obj,margin,columnspacing,rowspacing);ui_container_finish(obj);ui_container_begin_close(obj))
     
+
 void ui_end(UiObject *obj);
     
 UIWIDGET ui_vbox(UiObject *obj);
@@ -70,7 +72,9 @@
 void ui_layout_hexpand(UiObject *obj, UiBool expand);
 void ui_layout_vexpand(UiObject *obj, UiBool expand);
 void ui_layout_width(UiObject *obj, int width);
-void ui_layout_gridwidth(UiObject *obj, int width);
+void ui_layout_height(UiObject* obj, int width);
+void ui_layout_colspan(UiObject *obj, int cols);
+void ui_layout_rowspan(UiObject* obj, int rows);
 void ui_newline(UiObject *obj);
 
 
@@ -83,6 +87,13 @@
 void ui_container_begin_close(UiObject *obj);
 int ui_container_finish(UiObject *obj);
 
+#define UI_APPLY_LAYOUT1(obj, args) \
+    if(args.fill != UI_DEFAULT) ui_layout_fill(obj, args.fill == UI_ON ? 1 : 0 ); \
+    if(args.hexpand) ui_layout_hexpand(obj, 1); \
+    if(args.vexpand) ui_layout_vexpand(obj, 1); \
+    if(args.colspan > 0) ui_layout_colspan(obj, args.colspan); \
+    if(args.rowspan > 0) ui_layout_rowspan(obj, args.rowspan);
+
 
 #ifdef __cplusplus
 }
--- a/ui/ui/toolkit.h	Sun Sep 24 12:03:57 2023 +0200
+++ b/ui/ui/toolkit.h	Sun Oct 01 09:23:47 2023 +0200
@@ -163,6 +163,8 @@
 
 typedef struct UiTabbedPane UiTabbedPane;
 
+typedef enum UiTri UiTri;
+
 enum UiMouseEventType { UI_PRESS = 0, UI_PRESS2 };
 
 
@@ -339,6 +341,12 @@
     UiObserver *observers;
 };
 
+enum UiTri {
+    UI_DEFAULT = 0,
+    UI_ON,
+    UI_OFF
+};
+
 
 void ui_init(const char *appname, int argc, char **argv);
 const char* ui_appname();
--- a/ui/winui/button.cpp	Sun Sep 24 12:03:57 2023 +0200
+++ b/ui/winui/button.cpp	Sun Oct 01 09:23:47 2023 +0200
@@ -41,13 +41,13 @@
 using namespace Windows::UI::Xaml::Interop;
 using namespace winrt::Windows::Foundation;
 
-UIWIDGET ui_button(UiObject* obj, char* label, ui_callback f, void* data) {
+UIWIDGET ui_button_create(UiObject* obj, UiButtonArgs args) {
 	UiObject* current = uic_current_obj(obj);
 
 	// create button with label
 	Button button = Button();
-	if (label) {
-		wchar_t *wlabel = str2wstr(label, nullptr);
+	if (args.label) {
+		wchar_t* wlabel = str2wstr(args.label, nullptr);
 		button.Content(box_value(wlabel));
 		free(wlabel);
 	}
@@ -58,10 +58,10 @@
 	ui_context_add_widget_destructor(current->ctx, widget);
 
 	// register callback
-	if (f) {
+	if (args.onclick) {
 		widget->obj = obj;
-		widget->event_func = (ui_eventfunc)f;
-		widget->event_data = data;
+		widget->event_func = (ui_eventfunc)args.onclick;
+		widget->event_data = args.onclickdata;
 		button.Click([widget](IInspectable const& sender, RoutedEventArgs) {
 			ui_callback cb = (ui_callback)widget->event_func;
 
@@ -72,11 +72,20 @@
 			evt.eventdata = nullptr;
 			evt.intval = 0;
 			cb(&evt, widget->event_data);
-		} );
+			});
 	}
-	
+
 	// add button to current container
+	UI_APPLY_LAYOUT1(current, args);
 	current->container->Add(button, false);
 
 	return widget;
 }
+
+UIWIDGET ui_checkbox_create(UiObject* obj, UiToggleArgs args) {
+	return nullptr;
+}
+
+UIWIDGET ui_radiobutton_create(UiObject* obj, UiToggleArgs) {
+	return nullptr;
+}
--- a/ui/winui/container.cpp	Sun Sep 24 12:03:57 2023 +0200
+++ b/ui/winui/container.cpp	Sun Oct 01 09:23:47 2023 +0200
@@ -189,8 +189,11 @@
 	control.HorizontalAlignment(HorizontalAlignment::Stretch);
 	control.VerticalAlignment(VerticalAlignment::Stretch);
 
-	if (layout.gridwidth > 0) {
-		grid.SetColumnSpan(control, layout.gridwidth);
+	if (layout.colspan > 0) {
+		grid.SetColumnSpan(control, layout.colspan);
+	}
+	if (layout.rowspan > 0) {
+		grid.SetRowSpan(control, layout.rowspan);
 	}
 
 	grid.SetRow(control, y);
@@ -229,9 +232,19 @@
 	ct->layout.width = width;
 }
 
-void ui_layout_gridwidth(UiObject* obj, int width) {
+void ui_layout_height(UiObject* obj, int height) {
+	UiContainer* ct = uic_get_current_container(obj);
+	ct->layout.height = height;
+}
+
+void ui_layout_colspan(UiObject* obj, int cols) {
 	UiContainer* ct = uic_get_current_container(obj);
-	ct->layout.gridwidth = width;
+	ct->layout.colspan = cols;
+}
+
+void ui_layout_rowspan(UiObject* obj, int rows) {
+	UiContainer* ct = uic_get_current_container(obj);
+	ct->layout.rowspan = rows;
 }
 
 void ui_newline(UiObject* obj) {
--- a/ui/winui/container.h	Sun Sep 24 12:03:57 2023 +0200
+++ b/ui/winui/container.h	Sun Oct 01 09:23:47 2023 +0200
@@ -68,7 +68,9 @@
     UiBool       hexpand;
     UiBool       vexpand;
     int          width;
-    int          gridwidth;
+    int          height;
+    int          colspan;
+    int          rowspan;
 };
 
 struct UiContainer {
--- a/ui/winui/winui.vcxproj	Sun Sep 24 12:03:57 2023 +0200
+++ b/ui/winui/winui.vcxproj	Sun Oct 01 09:23:47 2023 +0200
@@ -168,6 +168,23 @@
     <ClInclude Include="..\common\properties.h" />
     <ClInclude Include="..\common\types.h" />
     <ClInclude Include="..\common\ucx_properties.h" />
+    <ClInclude Include="..\ui\button.h" />
+    <ClInclude Include="..\ui\container.h" />
+    <ClInclude Include="..\ui\display.h" />
+    <ClInclude Include="..\ui\dnd.h" />
+    <ClInclude Include="..\ui\entry.h" />
+    <ClInclude Include="..\ui\graphics.h" />
+    <ClInclude Include="..\ui\image.h" />
+    <ClInclude Include="..\ui\menu.h" />
+    <ClInclude Include="..\ui\properties.h" />
+    <ClInclude Include="..\ui\range.h" />
+    <ClInclude Include="..\ui\stock.h" />
+    <ClInclude Include="..\ui\text.h" />
+    <ClInclude Include="..\ui\toolbar.h" />
+    <ClInclude Include="..\ui\toolkit.h" />
+    <ClInclude Include="..\ui\tree.h" />
+    <ClInclude Include="..\ui\ui.h" />
+    <ClInclude Include="..\ui\window.h" />
     <ClInclude Include="appmenu.h" />
     <ClInclude Include="button.h" />
     <ClInclude Include="container.h" />
--- a/ui/winui/winui.vcxproj.filters	Sun Sep 24 12:03:57 2023 +0200
+++ b/ui/winui/winui.vcxproj.filters	Sun Oct 01 09:23:47 2023 +0200
@@ -19,6 +19,9 @@
     <Filter Include="Headerdateien\common">
       <UniqueIdentifier>{e701397e-2637-4a23-a260-1d3259292fa8}</UniqueIdentifier>
     </Filter>
+    <Filter Include="public">
+      <UniqueIdentifier>{135ba0dc-0817-4d23-b816-1ac0fa785bb9}</UniqueIdentifier>
+    </Filter>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="toolkit.cpp">
@@ -104,5 +107,56 @@
     <ClInclude Include="button.h">
       <Filter>Headerdateien</Filter>
     </ClInclude>
+    <ClInclude Include="..\ui\button.h">
+      <Filter>public</Filter>
+    </ClInclude>
+    <ClInclude Include="..\ui\container.h">
+      <Filter>public</Filter>
+    </ClInclude>
+    <ClInclude Include="..\ui\display.h">
+      <Filter>public</Filter>
+    </ClInclude>
+    <ClInclude Include="..\ui\dnd.h">
+      <Filter>public</Filter>
+    </ClInclude>
+    <ClInclude Include="..\ui\entry.h">
+      <Filter>public</Filter>
+    </ClInclude>
+    <ClInclude Include="..\ui\graphics.h">
+      <Filter>public</Filter>
+    </ClInclude>
+    <ClInclude Include="..\ui\image.h">
+      <Filter>public</Filter>
+    </ClInclude>
+    <ClInclude Include="..\ui\menu.h">
+      <Filter>public</Filter>
+    </ClInclude>
+    <ClInclude Include="..\ui\properties.h">
+      <Filter>public</Filter>
+    </ClInclude>
+    <ClInclude Include="..\ui\range.h">
+      <Filter>public</Filter>
+    </ClInclude>
+    <ClInclude Include="..\ui\stock.h">
+      <Filter>public</Filter>
+    </ClInclude>
+    <ClInclude Include="..\ui\text.h">
+      <Filter>public</Filter>
+    </ClInclude>
+    <ClInclude Include="..\ui\toolbar.h">
+      <Filter>public</Filter>
+    </ClInclude>
+    <ClInclude Include="..\ui\toolkit.h">
+      <Filter>public</Filter>
+    </ClInclude>
+    <ClInclude Include="..\ui\tree.h">
+      <Filter>public</Filter>
+    </ClInclude>
+    <ClInclude Include="..\ui\ui.h">
+      <Filter>public</Filter>
+    </ClInclude>
+    <ClInclude Include="..\ui\window.h">
+      <Filter>public</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>
\ No newline at end of file

mercurial