# HG changeset patch # User Olaf Wintermann # Date 1695476483 -7200 # Node ID 8c9b4b28aaa90b6de147aa6fa653186cd02692c0 # Parent 3ce2eb11913bbd2230afdc64b7039a4f555caaf9 implement ui_button (WinUI) diff -r 3ce2eb11913b -r 8c9b4b28aaa9 make/vs/testapp/main.c --- 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 +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); } diff -r 3ce2eb11913b -r 8c9b4b28aaa9 ui/ui/toolkit.h --- 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* diff -r 3ce2eb11913b -r 8c9b4b28aaa9 ui/winui/button.cpp --- /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; +} diff -r 3ce2eb11913b -r 8c9b4b28aaa9 ui/winui/button.h --- /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 diff -r 3ce2eb11913b -r 8c9b4b28aaa9 ui/winui/toolkit.cpp --- 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 #include +#include 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: +} diff -r 3ce2eb11913b -r 8c9b4b28aaa9 ui/winui/toolkit.h --- 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); diff -r 3ce2eb11913b -r 8c9b4b28aaa9 ui/winui/window.cpp --- 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; } diff -r 3ce2eb11913b -r 8c9b4b28aaa9 ui/winui/winui.cpp --- /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 diff -r 3ce2eb11913b -r 8c9b4b28aaa9 ui/winui/winui.h --- /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 +#undef GetCurrentTime +#include +#include +#include \ No newline at end of file diff -r 3ce2eb11913b -r 8c9b4b28aaa9 ui/winui/winui.vcxproj --- 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 @@ + @@ -168,6 +169,7 @@ + diff -r 3ce2eb11913b -r 8c9b4b28aaa9 ui/winui/winui.vcxproj.filters --- 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 @@ Quelldateien + + Quelldateien + @@ -98,5 +101,8 @@ Headerdateien + + Headerdateien + \ No newline at end of file