Sat, 23 Sep 2023 15:41:23 +0200
implement ui_button (WinUI)
make/vs/testapp/main.c | 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/button.h | file | annotate | diff | comparison | revisions | |
ui/winui/toolkit.cpp | file | annotate | diff | comparison | revisions | |
ui/winui/toolkit.h | file | annotate | diff | comparison | revisions | |
ui/winui/window.cpp | file | annotate | diff | comparison | revisions | |
ui/winui/winui.cpp | file | annotate | diff | comparison | revisions | |
ui/winui/winui.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 Fri Sep 22 21:32:45 2023 +0200 +++ b/make/vs/testapp/main.c Sat Sep 23 15:41:23 2023 +0200 @@ -34,10 +34,18 @@ #include <ui/ui.h> +void action1(UiEvent* event, void* data) { + char* action = data; + printf("hello world!\n"); +} 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_button(obj, "Button3", action1, "action3"); + ui_show(obj); }
--- a/ui/ui/toolkit.h Fri Sep 22 21:32:45 2023 +0200 +++ b/ui/ui/toolkit.h Sat Sep 23 15:41:23 2023 +0200 @@ -88,7 +88,12 @@ public: winrt::Microsoft::UI::Xaml::UIElement uielement; + void* obj; + void(*event_func)(void*, void*) = nullptr; + void* event_data = nullptr; + UiWidget(winrt::Microsoft::UI::Xaml::UIElement& elm); + }; #define UIWIDGET UiWidget*
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/winui/button.cpp Sat Sep 23 15:41:23 2023 +0200 @@ -0,0 +1,82 @@ +/* + * 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 "button.h" + +#include "util.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; + +UIWIDGET ui_button(UiObject* obj, char* label, ui_callback f, void* data) { + UiObject* current = uic_current_obj(obj); + + // create button with label + Button button = Button(); + if (label) { + wchar_t *wlabel = str2wstr(label, nullptr); + button.Content(box_value(wlabel)); + free(wlabel); + } + + // create toolkit wrapper object and register destructor + UIElement elm = button; + UiWidget* widget = new UiWidget(elm); + ui_context_add_widget_destructor(current->ctx, widget); + + // register callback + if (f) { + widget->obj = obj; + widget->event_func = (ui_eventfunc)f; + widget->event_data = data; + button.Click([widget](IInspectable const& sender, RoutedEventArgs) { + ui_callback cb = (ui_callback)widget->event_func; + + UiEvent evt; + evt.obj = (UiObject*)widget->obj; + evt.window = evt.obj->window; + evt.document = evt.obj->ctx->document; + evt.eventdata = nullptr; + evt.intval = 0; + cb(&evt, widget->event_data); + } ); + } + + // add button to current container + current->container->Add(button, false); + + return widget; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/winui/button.h Sat Sep 23 15:41:23 2023 +0200 @@ -0,0 +1,34 @@ +/* + * 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 "toolkit.h" +#include "../ui/container.h" + +#include "../ui/button.h" \ No newline at end of file
--- a/ui/winui/toolkit.cpp Fri Sep 22 21:32:45 2023 +0200 +++ b/ui/winui/toolkit.cpp Sat Sep 23 15:41:23 2023 +0200 @@ -38,6 +38,7 @@ #include <winrt/Microsoft.UI.Xaml.Markup.h> #include <cx/allocator.h> +#include <cx/mempool.h> using namespace winrt; @@ -46,6 +47,7 @@ using namespace Microsoft::UI::Xaml::XamlTypeInfo; using namespace Microsoft::UI::Xaml::Markup; using namespace Windows::UI::Xaml::Interop; +using namespace winrt::Windows::Foundation; static const char* application_name; @@ -87,10 +89,25 @@ XamlControlsXamlMetaDataProvider provider; }; -UiWindow::UiWindow(winrt::Microsoft::UI::Xaml::Window& win) : window(win) {}; - UiWidget::UiWidget(winrt::Microsoft::UI::Xaml::UIElement& elm) : uielement(elm) {} +extern "C" void destroy_ui_window_wrapper(void* ptr) { + UiWindow* win = (UiWindow*)ptr; + delete win; +} + +extern "C" void destroy_ui_widget_wrapper(void* ptr) { + UiWidget* widget = (UiWidget*)ptr; + delete widget; +} + +void ui_context_add_window_destructor(UiContext* ctx, UiWindow* win) { + // TODO: +} + +void ui_context_add_widget_destructor(UiContext* ctx, UiWidget* widget) { + // TODO: +}
--- a/ui/winui/toolkit.h Fri Sep 22 21:32:45 2023 +0200 +++ b/ui/winui/toolkit.h Sat Sep 23 15:41:23 2023 +0200 @@ -31,3 +31,10 @@ #define UIEXPORT extern "C" __declspec(dllexport) #include "../ui/toolkit.h" +typedef void(*ui_eventfunc)(void*, void*); + +extern "C" void destroy_ui_window_wrapper(void* ptr); +extern "C" void destroy_ui_widget_wrapper(void* ptr); + +void ui_context_add_window_destructor(UiContext* ctx, UiWindow *win); +void ui_context_add_widget_destructor(UiContext* ctx, UiWidget* widget);
--- a/ui/winui/window.cpp Fri Sep 22 21:32:45 2023 +0200 +++ b/ui/winui/window.cpp Sat Sep 23 15:41:23 2023 +0200 @@ -39,6 +39,7 @@ #include "appmenu.h" #include "container.h" +#include "util.h" #include "../common/context.h" @@ -54,6 +55,9 @@ using namespace Windows::UI::Xaml::Interop; using namespace winrt::Windows::Foundation; +UiWindow::UiWindow(winrt::Microsoft::UI::Xaml::Window& win) : window(win) {} + + UiObject* ui_window(const char* title, void* window_data) { CxMempool* mp = cxBasicMempoolCreate(256); UiObject* obj = (UiObject*)cxCalloc(mp->allocator, 1, sizeof(UiObject)); @@ -62,13 +66,22 @@ obj->window = window_data; Window window = Window(); + if (title) { + wchar_t *wtitle = str2wstr(title, nullptr); + window.Title(wtitle); + free(wtitle); + } Grid grid = Grid(); window.Content(grid); obj->wobj = new UiWindow(window); + - window.Closed([&](IInspectable const& sender, WindowEventArgs) { delete obj->wobj; } ); + window.Closed([obj](IInspectable const& sender, WindowEventArgs) { + // TODO: destroy UiObject*, context, ... + delete obj->wobj; + }); obj->container = new UiBoxContainer(grid, UI_CONTAINER_VBOX); @@ -79,21 +92,7 @@ obj->container->Add(mb, false); } - Button b1 = Button(); - Button b2 = Button(); - - b1.Content(box_value(L"Button 1")); - b2.Content(box_value(L"Button 2")); - - obj->container->Add(b1, false); - obj->container->Add(b2, false); - - obj->window = window_data; - //wptr2->Activate(); - //obj->wobj = wptr2; - - return obj; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/winui/winui.cpp Sat Sep 23 15:41:23 2023 +0200 @@ -0,0 +1,1 @@ +#include "winui.h" \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/winui/winui.h Sat Sep 23 15:41:23 2023 +0200 @@ -0,0 +1,7 @@ +#pragma once + +#include <Windows.h> +#undef GetCurrentTime +#include <winrt/Windows.Foundation.Collections.h> +#include <winrt/Windows.UI.Xaml.Interop.h> +#include <winrt/Microsoft.UI.Xaml.Controls.h> \ No newline at end of file
--- a/ui/winui/winui.vcxproj Fri Sep 22 21:32:45 2023 +0200 +++ b/ui/winui/winui.vcxproj Sat Sep 23 15:41:23 2023 +0200 @@ -151,6 +151,7 @@ <ClCompile Include="..\common\types.c" /> <ClCompile Include="..\common\ucx_properties.c" /> <ClCompile Include="appmenu.cpp" /> + <ClCompile Include="button.cpp" /> <ClCompile Include="container.cpp" /> <ClCompile Include="toolkit.cpp" /> <ClCompile Include="util.cpp" /> @@ -168,6 +169,7 @@ <ClInclude Include="..\common\types.h" /> <ClInclude Include="..\common\ucx_properties.h" /> <ClInclude Include="appmenu.h" /> + <ClInclude Include="button.h" /> <ClInclude Include="container.h" /> <ClInclude Include="toolkit.h" /> <ClInclude Include="util.h" />
--- a/ui/winui/winui.vcxproj.filters Fri Sep 22 21:32:45 2023 +0200 +++ b/ui/winui/winui.vcxproj.filters Sat Sep 23 15:41:23 2023 +0200 @@ -57,6 +57,9 @@ <ClCompile Include="container.cpp"> <Filter>Quelldateien</Filter> </ClCompile> + <ClCompile Include="button.cpp"> + <Filter>Quelldateien</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <None Include="packages.config" /> @@ -98,5 +101,8 @@ <ClInclude Include="container.h"> <Filter>Headerdateien</Filter> </ClInclude> + <ClInclude Include="button.h"> + <Filter>Headerdateien</Filter> + </ClInclude> </ItemGroup> </Project> \ No newline at end of file