From 49de9d0290bcdbf6faab45f973c9dc64278934aa Mon Sep 17 00:00:00 2001 From: martinfouilleul Date: Tue, 23 May 2023 16:04:49 +0200 Subject: [PATCH] [win32] added basic mp_save_dialogue() implementation using COM IFileSaveDialog --- examples/ui/main.c | 8 ++++++-- src/win32_app.c | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/examples/ui/main.c b/examples/ui/main.c index ed521fe..21701c1 100644 --- a/examples/ui/main.c +++ b/examples/ui/main.c @@ -406,8 +406,6 @@ int main() UI_STYLE_SIZE); widget_view("Buttons") { - ui_button("Button 1"); - if(ui_button("Test Dialog").clicked) { char* options[] = {"Accept", "Reject"}; @@ -427,6 +425,12 @@ int main() str8 file = mp_open_dialog(mem_scratch(), "Open File", 0, 0, 0, true); printf("selected file %.*s\n", (int)file.len, file.ptr); } + + if(ui_button("Save").clicked) + { + str8 file = mp_save_dialog(mem_scratch(), "Save File", 0, 0, 0, true); + printf("selected file %.*s\n", (int)file.len, file.ptr); + } } ui_style_next(&(ui_style){.size.width = {UI_SIZE_PARENT, 0.5}, diff --git a/src/win32_app.c b/src/win32_app.c index a0d9ff0..e30b4e0 100644 --- a/src/win32_app.c +++ b/src/win32_app.c @@ -1148,7 +1148,45 @@ MP_API str8 mp_save_dialog(mem_arena* arena, const char* title, const char* defaultPath, int filterCount, - const char** filters); + const char** filters) +{ + str8 res = {0}; + HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); + if(SUCCEEDED(hr)) + { + IFileOpenDialog* dialog = 0; + hr = CoCreateInstance(&CLSID_FileSaveDialog, NULL, CLSCTX_ALL, &IID_IFileSaveDialog, (void**)&dialog); + if(SUCCEEDED(hr)) + { + hr = dialog->lpVtbl->Show(dialog, NULL); + if(SUCCEEDED(hr)) + { + IShellItem* item; + hr = dialog->lpVtbl->GetResult(dialog, &item); + if(SUCCEEDED(hr)) + { + PWSTR filePath; + hr = item->lpVtbl->GetDisplayName(item, SIGDN_FILESYSPATH, &filePath); + + if(SUCCEEDED(hr)) + { + int utf8Size = WideCharToMultiByte(CP_UTF8, 0, filePath, -1, NULL, 0, NULL, NULL); + if(utf8Size > 0) + { + res.ptr = mem_arena_alloc(arena, utf8Size); + res.len = utf8Size-1; + WideCharToMultiByte(CP_UTF8, 0, filePath, -1, res.ptr, utf8Size, NULL, NULL); + } + CoTaskMemFree(filePath); + } + item->lpVtbl->Release(item); + } + } + } + } + CoUninitialize(); + return(res); +} #include