# HG changeset patch # User Olaf Wintermann # Date 1684844346 -7200 # Node ID f34953bf4ac7f2f25819215a60cd04121c558180 # Parent 34f4d78647be0005683d1c800baf59d03ce2906e add basic winui menu implementation diff -r 34f4d78647be -r f34953bf4ac7 make/vs/testapp/main.c --- a/make/vs/testapp/main.c Tue May 23 11:42:39 2023 +0200 +++ b/make/vs/testapp/main.c Tue May 23 14:19:06 2023 +0200 @@ -47,6 +47,13 @@ ui_init("app1", 0, NULL); ui_onstartup(application_startup, NULL); + ui_menu("File"); + ui_menuitem("Test", NULL, NULL); + ui_submenu("Sub"); + ui_menuitem("subitem", NULL, NULL); + ui_submenu_end(); + ui_menuitem("Exit", NULL, NULL); + ui_main(); return (EXIT_SUCCESS); diff -r 34f4d78647be -r f34953bf4ac7 make/vs/ucx/ucx.vcxproj --- a/make/vs/ucx/ucx.vcxproj Tue May 23 11:42:39 2023 +0200 +++ b/make/vs/ucx/ucx.vcxproj Tue May 23 14:19:06 2023 +0200 @@ -40,7 +40,7 @@ Unicode - DynamicLibrary + StaticLibrary true v143 Unicode diff -r 34f4d78647be -r f34953bf4ac7 ui/winui/appmenu.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/winui/appmenu.cpp Tue May 23 14:19:06 2023 +0200 @@ -0,0 +1,162 @@ +/* + * 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 "appmenu.h" + +#include +#undef GetCurrentTime +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "util.h" + + +using namespace winrt; +using namespace Microsoft::UI::Xaml; +using namespace Microsoft::UI::Xaml::Controls; +using namespace Microsoft::UI::Xaml::XamlTypeInfo; +using namespace Microsoft::UI::Xaml::Markup; +using namespace Windows::UI::Xaml::Interop; + +static void add_top_menu_widget(MenuBar &parent, int i, UiMenuItemI* item, UiObject* obj); + +static void add_menu_widget(winrt::Windows::Foundation::Collections::IVector parent, int i, UiMenuItemI* item, UiObject* obj); +static void add_menuitem_widget(winrt::Windows::Foundation::Collections::IVector parent, int i, UiMenuItemI* item, UiObject* obj); +static void add_menuitem_st_widget(winrt::Windows::Foundation::Collections::IVector parent, int i, UiMenuItemI* item, UiObject* obj); +static void add_menuseparator_widget(winrt::Windows::Foundation::Collections::IVector parent, int i, UiMenuItemI* item, UiObject* obj); +static void add_checkitem_widget(winrt::Windows::Foundation::Collections::IVector parent, int i, UiMenuItemI* item, UiObject* obj); +static void add_checkitemnv_widget(winrt::Windows::Foundation::Collections::IVector parent, int i, UiMenuItemI* item, UiObject* obj); +static void add_menuitem_list_widget(winrt::Windows::Foundation::Collections::IVector parent, int i, UiMenuItemI* item, UiObject* obj); + +static ui_menu_add_f createMenuItem[] = { + /* UI_MENU */ add_menu_widget, + /* UI_MENU_SUBMENU */ add_menu_widget, + /* UI_MENU_ITEM */ add_menuitem_widget, + /* UI_MENU_STOCK_ITEM */ add_menuitem_st_widget, + /* UI_MENU_CHECK_ITEM */ add_checkitem_widget, + /* UI_MENU_CHECK_ITEM_NV */ add_checkitemnv_widget, + /* UI_MENU_ITEM_LIST */ add_menuitem_list_widget, + /* UI_MENU_ITEM_LIST_NV */ NULL, // TODO + /* UI_MENU_SEPARATOR */ add_menuseparator_widget +}; + +winrt::Microsoft::UI::Xaml::Controls::MenuBar ui_create_menubar(UiObject* obj) { + MenuBar mb = MenuBar(); + + UiMenu* menus_begin = uic_get_menu_list(); + + UiMenu* ls = menus_begin; + while (ls) { + UiMenu* menu = ls; + add_top_menu_widget(mb, 0, &menu->item, obj); + + ls = (UiMenu*)ls->item.next; + } + + return mb; +} + +static void add_top_menu_widget(MenuBar& parent, int i, UiMenuItemI* item, UiObject* obj) { + UiMenu* menu = (UiMenu*)item; + + MenuBarItem mi = MenuBarItem(); + wchar_t* wlabel = str2wstr(menu->label, NULL); + mi.Title(wlabel); + free(wlabel); + + UiMenuItemI* it = menu->items_begin; + int index = 0; + while (it) { + createMenuItem[it->type](mi.Items(), index, it, obj); + + it = it->next; + index++; + } + + parent.Items().Append(mi); +} + +static void add_menu_widget(winrt::Windows::Foundation::Collections::IVector parent, int i, UiMenuItemI* item, UiObject* obj) { + UiMenu* menu = (UiMenu*)item; + + MenuFlyoutSubItem mi = MenuFlyoutSubItem(); + wchar_t* wlabel = str2wstr(menu->label, NULL); + mi.Text(wlabel); + free(wlabel); + + parent.Append(mi); + + UiMenuItemI* it = menu->items_begin; + int index = 0; + while (it) { + createMenuItem[it->type](mi.Items(), index, it, obj); + + it = it->next; + index++; + } + + +} + +static void add_menuitem_widget(winrt::Windows::Foundation::Collections::IVector parent, int i, UiMenuItemI* item, UiObject* obj) { + UiMenuItem* it = (UiMenuItem*)item; + + MenuFlyoutItem mi = MenuFlyoutItem(); + wchar_t* wlabel = str2wstr(it->label, NULL); + mi.Text(wlabel); + free(wlabel); + + parent.Append(mi); +} + +static void add_menuitem_st_widget(winrt::Windows::Foundation::Collections::IVector parent, int i, UiMenuItemI* item, UiObject* obj) { + +} + +static void add_menuseparator_widget(winrt::Windows::Foundation::Collections::IVector parent, int i, UiMenuItemI* item, UiObject* obj) { + +} + +static void add_checkitem_widget(winrt::Windows::Foundation::Collections::IVector parent, int i, UiMenuItemI* item, UiObject* obj) { + +} + +static void add_checkitemnv_widget(winrt::Windows::Foundation::Collections::IVector parent, int i, UiMenuItemI* item, UiObject* obj) { + +} + +static void add_menuitem_list_widget(winrt::Windows::Foundation::Collections::IVector parent, int i, UiMenuItemI* item, UiObject* obj) { + +} diff -r 34f4d78647be -r f34953bf4ac7 ui/winui/appmenu.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/winui/appmenu.h Tue May 23 14:19:06 2023 +0200 @@ -0,0 +1,48 @@ +/* + * 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 "../common/menu.h" + +#include +#undef GetCurrentTime +#include +#include +#include +#include +#include +#include + + + +typedef void(*ui_menu_add_f)(winrt::Windows::Foundation::Collections::IVector parent, int, UiMenuItemI*, UiObject*); + +winrt::Microsoft::UI::Xaml::Controls::MenuBar ui_create_menubar(UiObject* obj); + diff -r 34f4d78647be -r f34953bf4ac7 ui/winui/util.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/winui/util.cpp Tue May 23 14:19:06 2023 +0200 @@ -0,0 +1,29 @@ + + +#include "util.h" + +#include + +#include + + +wchar_t* str2wstr(const char* str, int* newlen) { + size_t len = strlen(str); + + + wchar_t* wstr = (wchar_t*)calloc(len + 1, sizeof(wchar_t)); + int wlen = MultiByteToWideChar( + CP_UTF8, + 0, + str, + len, + wstr, + len + 1 + ); + if (newlen) { + *newlen = wlen; + } + wstr[wlen] = 0; + + return wstr; +} \ No newline at end of file diff -r 34f4d78647be -r f34953bf4ac7 ui/winui/util.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/winui/util.h Tue May 23 14:19:06 2023 +0200 @@ -0,0 +1,3 @@ +#pragma once + +wchar_t* str2wstr(const char* str, int* newlen); diff -r 34f4d78647be -r f34953bf4ac7 ui/winui/window.cpp --- a/ui/winui/window.cpp Tue May 23 11:42:39 2023 +0200 +++ b/ui/winui/window.cpp Tue May 23 14:19:06 2023 +0200 @@ -37,6 +37,8 @@ #include #include +#include "appmenu.h" + #include using namespace winrt; @@ -49,8 +51,17 @@ class UiWindow : UiWidget { public: - UiWindow(const char* title) { + UiWindow(const char* title, UiObject *obj) { window = Window(); + + grid = Grid(); + window.Content(grid); + + if (uic_get_menu_list()) { + MenuBar mb = ui_create_menubar(obj); + mb.VerticalAlignment(VerticalAlignment::Top); + grid.Children().Append(mb); + } } virtual void show() { @@ -58,12 +69,13 @@ } Window window{ nullptr }; + Grid grid; }; UiObject* ui_window(const char* title, void* window_data) { UiObject* obj = (UiObject*)malloc(sizeof(UiObject)); - UiWindow* window = new UiWindow(title); + UiWindow* window = new UiWindow(title, obj); obj->widget = window; return obj; diff -r 34f4d78647be -r f34953bf4ac7 ui/winui/winui.vcxproj --- a/ui/winui/winui.vcxproj Tue May 23 11:42:39 2023 +0200 +++ b/ui/winui/winui.vcxproj Tue May 23 14:19:06 2023 +0200 @@ -148,7 +148,9 @@ + + @@ -162,7 +164,9 @@ + + diff -r 34f4d78647be -r f34953bf4ac7 ui/winui/winui.vcxproj.filters --- a/ui/winui/winui.vcxproj.filters Tue May 23 11:42:39 2023 +0200 +++ b/ui/winui/winui.vcxproj.filters Tue May 23 14:19:06 2023 +0200 @@ -48,6 +48,12 @@ Quelldateien\common + + Quelldateien + + + Quelldateien + @@ -80,5 +86,11 @@ Headerdateien\common + + Headerdateien + + + Headerdateien + \ No newline at end of file diff -r 34f4d78647be -r f34953bf4ac7 ui/winui/winui.vcxproj.user --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/winui/winui.vcxproj.user Tue May 23 14:19:06 2023 +0200 @@ -0,0 +1,4 @@ + + + + \ No newline at end of file