# HG changeset patch # User Olaf Wintermann # Date 1707650970 -3600 # Node ID d2068517fbdd668e7e2704b3d5575c777e8fbaa6 # Parent 4df7c366cff79bc8aec095642cde355598a6aea0 add function for creating dialogs (WinUI3) diff -r 4df7c366cff7 -r d2068517fbdd make/vs/testapp/main.c --- a/make/vs/testapp/main.c Fri Feb 09 16:52:11 2024 +0100 +++ b/make/vs/testapp/main.c Sun Feb 11 12:29:30 2024 +0100 @@ -184,6 +184,15 @@ } +void dialog_result(UiEvent *evt, void *data) { + printf("dialog: %d\n", (int)evt->intval); +} + +void btn_dialog(UiEvent *evt, void *data) { + ui_dialog(evt->obj, .title = "Title", .content = "Hello World", .button1_label = "Yes", .button2_label = "No", .closebutton_label = "Close", .result = dialog_result); +} + + void application_startup(UiEvent* event, void* data) { @@ -268,7 +277,7 @@ ui_button(obj, .icon = "Forward", .onclick = action1, .onclickdata = "action3", .hexpand = true); ui_newline(obj); - ui_button(obj, .label = "Button4", .onclick = action1, .onclickdata = "action4"); + ui_button(obj, .label = "Dialog Test", .onclick = btn_dialog, .onclickdata = "action4"); ui_button(obj, .label = "Button5", .onclick = action1, .onclickdata = "action5", .colspan = 2); ui_newline(obj); diff -r 4df7c366cff7 -r d2068517fbdd ui/ui/window.h --- a/ui/ui/window.h Fri Feb 09 16:52:11 2024 +0100 +++ b/ui/ui/window.h Sun Feb 11 12:29:30 2024 +0100 @@ -39,11 +39,25 @@ #define UI_FILEDIALOG_SELECT_MULTI 1 #define UI_FILEDIALOG_SELECT_FOLDER 2 +typedef struct UiDialogArgs { + const char *title; + const char *content; + const char *button1_label; + const char *button2_label; + const char *closebutton_label; + ui_callback result; + void *resultdata; +} UiDialogArgs; + UIEXPORT UiObject* ui_window(const char *title, void *window_data); UIEXPORT UiObject* ui_simple_window(const char *title, void *window_data); UIEXPORT void ui_window_size(UiObject *obj, int width, int height); +#define ui_dialog(parent, ...) ui_dialog_create(parent, (UiDialogArgs){ __VA_ARGS__ } ) + +UIEXPORT void ui_dialog_create(UiObject *parent, UiDialogArgs args); + UIEXPORT void ui_openfiledialog(UiObject *obj, unsigned int mode, ui_callback file_selected_callback, void *cbdata); UIEXPORT void ui_savefiledialog(UiObject *obj, unsigned int mode, ui_callback file_selected_callback, void *cbdata); diff -r 4df7c366cff7 -r d2068517fbdd ui/winui/util.h --- a/ui/winui/util.h Fri Feb 09 16:52:11 2024 +0100 +++ b/ui/winui/util.h Sun Feb 11 12:29:30 2024 +0100 @@ -1,5 +1,7 @@ #pragma once +#include + wchar_t* str2wstr(const char* str, int* newlen); wchar_t* str2wstr_len(const char* str, size_t len, int* newlen); diff -r 4df7c366cff7 -r d2068517fbdd ui/winui/window.cpp --- a/ui/winui/window.cpp Fri Feb 09 16:52:11 2024 +0100 +++ b/ui/winui/window.cpp Sun Feb 11 12:29:30 2024 +0100 @@ -37,6 +37,7 @@ #include "util.h" #include "../common/context.h" +#include "../common/object.h" #include @@ -204,6 +205,70 @@ } } + + + +static Windows::Foundation::IAsyncAction create_dialog_async(UiObject *obj, UiDialogArgs args) { + UiObject* current = uic_current_obj(obj); + Window parentWindow = current->wobj->window; + + ContentDialog dialog = ContentDialog(); + dialog.XamlRoot(parentWindow.Content().XamlRoot()); + + if (args.title) { + wchar_t *str = str2wstr(args.title, nullptr); + dialog.Title(winrt::box_value(str)); + free(str); + } + if (args.content) { + wchar_t *str = str2wstr(args.content, nullptr); + dialog.Content(winrt::box_value(str)); + free(str); + } + + if (args.button1_label) { + wchar_t *str = str2wstr(args.button1_label, nullptr); + dialog.PrimaryButtonText(winrt::hstring(str)); + free(str); + } + if (args.button2_label) { + wchar_t *str = str2wstr(args.button2_label, nullptr); + dialog.SecondaryButtonText(winrt::hstring(str)); + free(str); + } + if (args.closebutton_label) { + wchar_t *str = str2wstr(args.closebutton_label, nullptr); + dialog.CloseButtonText(winrt::hstring(str)); + free(str); + } + + ContentDialogResult result = co_await dialog.ShowAsync(); + + if (args.result) { + UiEvent evt; + evt.obj = current; + evt.document = current->ctx->document; + evt.window = current->window; + evt.eventdata = NULL; + evt.intval = 0; + if (result == ContentDialogResult::Primary) { + evt.intval = 1; + } else if (result == ContentDialogResult::Secondary) { + evt.intval = 2; + } + + args.result(&evt, args.resultdata); + } +} + +UIEXPORT void ui_dialog_create(UiObject *obj, UiDialogArgs args) { + create_dialog_async(obj, args); +} + + + +// --------------------------------------- File Dialog --------------------------------------- + static void filedialog_callback( UiObject *obj, ui_callback file_selected_callback,