| 1 | /* GuiFileSelect.cpp |
| 2 | * |
| 3 | * Copyright (C) 2010-2026 Paul Boersma, 2013 Tom Naughton |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "GuiP.h"cxx . . $ `<file>/sys/GuiP.h`/ |
| 20 | #include <locale.h> |
| 21 | #if motifcxx . . $ `sys/Gui.h:34:10`! |
| 22 | #include <shlobj.h> |
| 23 | #endif |
| 24 | |
| 25 | autoStringSetcxx . . $ autoStringSet# GuiFileSelect_getInfileNamescxx . . $ GuiFileSelect_getInfileNames(5a0c946482488abf). (GuiWindowcxx . . $ GuiWindow# optionalParent, conststring32cxx . . $ conststring32# title, bool allowMultipleFiles) { |
| 26 | autoMelderSaveCurrentFoldercxx . . $ autoMelderSaveCurrentFolder# saveFoldercxx . . $ autoMelderSaveCurrentFolder#autoMelderSaveCurrentFolder(49f6e7a06ebc5aa8).; |
| 27 | autoStringSetcxx . . $ autoStringSet# me = StringSet_createcxx . . $ StringSet_create(3c0f0334e2cf88fa). (); |
| 28 | #if gtkcxx . . $ `sys/Gui.h:33:10`! |
| 29 | static structMelderFoldercxx . . $ structMelderFolder# folder { }; |
| 30 | GuiObjectcxx . . $ GuiObject# dialog = gtk_file_chooser_native_new (Melder_peek32to8 (title), nullptr, GTK_FILE_CHOOSER_ACTION_OPEN, |
| 31 | "_Open", "_Cancel"); |
| 32 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (dialog); |
| 33 | if (optionalParent) |
| 34 | gtk_native_dialog_set_transient_for (GTK_NATIVE_DIALOG (dialog), GTK_WINDOWcxx . . $ `<external>/3777575239456597881/gtkwindow.h:40:9`! (optionalParent -> d_gtkWindow)); |
| 35 | gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSERcxx . . $ `<external>/2609165349595818446/gtkfilechooser.h:32:9`! (dialog), allowMultipleFiles); |
| 36 | if (MelderFolder_isNullcxx . . $ MelderFolder_isNull(f2f3cbeb84a71ab3). (& folder)) // first time? |
| 37 | Melder_getCurrentFoldercxx . . $ Melder_getCurrentFolder(63f38d3b6b86d1dc). (& folder); |
| 38 | gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSERcxx . . $ `<external>/2609165349595818446/gtkfilechooser.h:32:9`! (dialog), MelderFolder_peekPath8cxx . . $ MelderFolder_peekPath8(31391b8863732c6e). (& folder)); |
| 39 | if (gtk_native_dialog_run (GTK_NATIVE_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) { |
| 40 | char *infolderName_utf8 = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSERcxx . . $ `<external>/2609165349595818446/gtkfilechooser.h:32:9`! (dialog)); |
| 41 | if (infolderName_utf8) { |
| 42 | conststring32cxx . . $ conststring32# infolderName = Melder_peek8to32_u (infolderName_utf8); // dangle |
| 43 | Melder_pathToFoldercxx . . $ Melder_pathToFolder(f9a4643b225086b8). (infolderName, & folder); |
| 44 | g_free (infolderName_utf8); |
| 45 | } |
| 46 | GSList *infileNames_list = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSERcxx . . $ `<external>/2609165349595818446/gtkfilechooser.h:32:9`! (dialog)); |
| 47 | for (GSList *element = infileNames_list; element != nullptr; element = g_slist_nextcxx . . $ `<external>/5193788326479635812/gslist.h:162:10`! (element)) { |
| 48 | char *infileName_utf8 = (char *) element -> datacxx . . $ _GSList#data.; |
| 49 | mycxx . . $ `melder/melder.h:44:9`! addString_copycxx . . $ structStringSet#addString_copy(ff00d524ab0355ec). (Melder_peek8to32_u (infileName_utf8)); |
| 50 | g_free (infileName_utf8); |
| 51 | } |
| 52 | g_slist_free (infileNames_list); |
| 53 | } |
| 54 | gtk_native_dialog_destroy (GTK_NATIVE_DIALOG (dialog)); |
| 55 | setlocale (LC_ALLcxx . . $ `<external>/3736975272097309885/locale.h:41:9`!, "C"); |
| 56 | #elif motif |
| 57 | static OPENFILENAMEW openFileName; // TODO: replace with Common Item Dialog API (since Vista) |
| 58 | constexpr integer MAXIMUM_SIZE = 3'000'000; |
| 59 | autostringW fullFileNameW (MAXIMUM_SIZE + 2); |
| 60 | ZeroMemory (& openFileName, sizeof (OPENFILENAMEW)); |
| 61 | openFileName. lStructSize = sizeof (OPENFILENAMEW); |
| 62 | openFileName. hwndOwner = ( optionalParent && optionalParent -> d_xmShell ? (HWND) XtWindow (optionalParent -> d_xmShell) : nullptr ); |
| 63 | openFileName. hInstance = nullptr; |
| 64 | openFileName. lpstrFilter = L"All Files\0*.*\0"; |
| 65 | ZeroMemory (fullFileNameW.get(), (MAXIMUM_SIZE+2) * sizeof (WCHAR)); |
| 66 | openFileName. lpstrCustomFilter = nullptr; |
| 67 | openFileName. nMaxCustFilter = 0; |
| 68 | openFileName. lpstrFile = fullFileNameW.get(); |
| 69 | openFileName. nMaxFile = MAXIMUM_SIZE; |
| 70 | openFileName. lpstrFileTitle = nullptr; |
| 71 | openFileName. nMaxFileTitle = 0; |
| 72 | openFileName. lpstrInitialDir = nullptr; |
| 73 | openFileName. lpstrTitle = Melder_peek32toW_fileSystem (title); |
| 74 | openFileName. Flags = OFN_EXPLORER | OFN_LONGNAMES | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY |
| 75 | | (allowMultipleFiles ? OFN_ALLOWMULTISELECT : 0); |
| 76 | openFileName. lpstrDefExt = nullptr; |
| 77 | openFileName. lpfnHook = nullptr; |
| 78 | openFileName. lpTemplateName = nullptr; |
| 79 | openFileName. pvReserved = nullptr; |
| 80 | openFileName. dwReserved = 0; |
| 81 | openFileName. FlagsEx = 0; |
| 82 | OSVERSIONINFO osVersionInfo; |
| 83 | ZeroMemory (& osVersionInfo, sizeof (OSVERSIONINFO)); |
| 84 | osVersionInfo. dwOSVersionInfoSize = sizeof (OSVERSIONINFO); |
| 85 | GetVersionEx (& osVersionInfo); |
| 86 | if (GetOpenFileNameW (& openFileName)) { |
| 87 | const integer firstFileNameLength = wcslen (fullFileNameW.get()); |
| 88 | if (fullFileNameW [firstFileNameLength + 1] == L'\0') { |
| 89 | /* |
| 90 | The user selected one file. |
| 91 | */ |
| 92 | my addString_copy (Melder_peekWto32 (fullFileNameW.get())); |
| 93 | } else { |
| 94 | /* |
| 95 | The user selected multiple files. |
| 96 | 'fullFileNameW' is a folder name; the file names follow. |
| 97 | */ |
| 98 | structMelderFolder folder { }; |
| 99 | Melder_pathToFolder (Melder_peekWto32 (fullFileNameW.get()), & folder); |
| 100 | for (const WCHAR *p = & fullFileNameW [firstFileNameLength + 1]; *p != L'\0'; p += wcslen (p) + 1) { |
| 101 | structMelderFile file { }; |
| 102 | MelderFolder_getFile (& folder, Melder_peekWto32 (p), & file); |
| 103 | my addString_copy (MelderFile_peekPath (& file)); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | setlocale (LC_ALL, "C"); |
| 108 | #elif cocoa |
| 109 | (void) optionalParent; |
| 110 | NSOpenPanel *openPanel = [NSOpenPanel openPanel]; |
| 111 | [openPanel setTitle: [NSString stringWithUTF8String: Melder_peek32to8 (title)]]; |
| 112 | [openPanel setAllowsMultipleSelection: allowMultipleFiles]; |
| 113 | [openPanel setCanChooseDirectories: NO]; |
| 114 | if ([openPanel runModal] == NSFileHandlingPanelOKButton) { |
| 115 | for (NSURL *url in [openPanel URLs]) { |
| 116 | structMelderFile file { }; |
| 117 | Melder_8bitFileRepresentationToStr32_inplace ([[url path] UTF8String], file. path); // BUG: unsafe buffer |
| 118 | my addString_copy (MelderFile_peekPath (& file)); |
| 119 | } |
| 120 | } |
| 121 | setlocale (LC_ALL, "C"); |
| 122 | #endif |
| 123 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 124 | } |
| 125 | |
| 126 | autostring32cxx . . $ autostring32# GuiFileSelect_getOutfileNamecxx . . $ GuiFileSelect_getOutfileName(575be6c40acafffe). (GuiWindowcxx . . $ GuiWindow# optionalParent, conststring32cxx . . $ conststring32# title, conststring32cxx . . $ conststring32# defaultName) { |
| 127 | autoMelderSaveCurrentFoldercxx . . $ autoMelderSaveCurrentFolder# saveFoldercxx . . $ autoMelderSaveCurrentFolder#autoMelderSaveCurrentFolder(49f6e7a06ebc5aa8).; |
| 128 | autostring32cxx . . $ autostring32# outfileNamecxx . . $ _autostring#_autostring(49f6e7a06ebc5aa8).; |
| 129 | #if gtkcxx . . $ `sys/Gui.h:33:10`! |
| 130 | static structMelderFilecxx . . $ structMelderFile# file; |
| 131 | GuiObjectcxx . . $ GuiObject# dialog = gtk_file_chooser_native_new (Melder_peek32to8 (title), nullptr, GTK_FILE_CHOOSER_ACTION_SAVE, |
| 132 | "_Save", "_Cancel"); |
| 133 | if (optionalParent) |
| 134 | gtk_native_dialog_set_transient_for (GTK_NATIVE_DIALOG (dialog), GTK_WINDOWcxx . . $ `<external>/3777575239456597881/gtkwindow.h:40:9`! (optionalParent -> d_gtkWindow)); |
| 135 | gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSERcxx . . $ `<external>/2609165349595818446/gtkfilechooser.h:32:9`! (dialog), true); |
| 136 | if (! MelderFile_isNullcxx . . $ MelderFile_isNull(8f92cdf95b27d908). (& file)) |
| 137 | gtk_file_chooser_set_filename (GTK_FILE_CHOOSERcxx . . $ `<external>/2609165349595818446/gtkfilechooser.h:32:9`! (dialog), MelderFile_peekPath8cxx . . $ MelderFile_peekPath8(3a3888509ce82e67). (& file)); |
| 138 | gtk_file_chooser_set_current_name (GTK_FILE_CHOOSERcxx . . $ `<external>/2609165349595818446/gtkfilechooser.h:32:9`! (dialog), Melder_peek32to8 (defaultName)); |
| 139 | if (gtk_native_dialog_run (GTK_NATIVE_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) { |
| 140 | char *outfileName_utf8 = gtk_file_chooser_get_filename (GTK_FILE_CHOOSERcxx . . $ `<external>/2609165349595818446/gtkfilechooser.h:32:9`! (dialog)); |
| 141 | outfileName =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_8to32_ecxx . . $ Melder_8to32_e(f01fe69d6ed2498b). (outfileName_utf8); |
| 142 | g_free (outfileName_utf8); |
| 143 | Melder_pathToFilecxx . . $ Melder_pathToFile(82691f2ee21fbb13). (outfileName.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), & file); |
| 144 | } |
| 145 | gtk_native_dialog_destroy (GTK_NATIVE_DIALOG (dialog)); |
| 146 | setlocale (LC_ALLcxx . . $ `<external>/3736975272097309885/locale.h:41:9`!, "C"); |
| 147 | #elif motif |
| 148 | OPENFILENAMEW openFileName; |
| 149 | ZeroMemory (& openFileName, sizeof (OPENFILENAMEW)); |
| 150 | static WCHAR customFilter [100+2]; |
| 151 | static WCHAR fullFileNameW [300+2]; |
| 152 | wcsncpy (fullFileNameW, Melder_peek32toW_fileSystem (defaultName), 300+2); |
| 153 | fullFileNameW [300+1] = L'\0'; |
| 154 | openFileName. lStructSize = sizeof (OPENFILENAMEW); |
| 155 | openFileName. hwndOwner = ( optionalParent && optionalParent -> d_xmShell ? (HWND) XtWindow (optionalParent -> d_xmShell) : nullptr ); |
| 156 | openFileName. lpstrFilter = nullptr; // like *.txt |
| 157 | openFileName. lpstrCustomFilter = customFilter; |
| 158 | openFileName. nMaxCustFilter = 100; |
| 159 | openFileName. lpstrFile = fullFileNameW; |
| 160 | openFileName. nMaxFile = 300; |
| 161 | openFileName. lpstrFileTitle = nullptr; |
| 162 | openFileName. lpstrInitialDir = nullptr; |
| 163 | openFileName. lpstrTitle = Melder_peek32toW_fileSystem (title); |
| 164 | openFileName. Flags = OFN_LONGNAMES | OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_HIDEREADONLY; |
| 165 | openFileName. lpstrDefExt = nullptr; |
| 166 | if (GetSaveFileNameW (& openFileName)) |
| 167 | outfileName = Melder_Wto32 (fullFileNameW); |
| 168 | setlocale (LC_ALL, "C"); |
| 169 | #elif cocoa |
| 170 | (void) optionalParent; |
| 171 | NSSavePanel *savePanel = [NSSavePanel savePanel]; // will be autoreleased (release will crash; 2020-11-12) |
| 172 | [savePanel setTitle: [NSString stringWithUTF8String: Melder_peek32to8 (title)]]; |
| 173 | [savePanel setNameFieldStringValue: [NSString stringWithUTF8String: Melder_peek32to8_fileSystem (defaultName)]]; |
| 174 | if ([savePanel runModal] == NSFileHandlingPanelOKButton) { |
| 175 | NSString *path = [[savePanel URL] path]; |
| 176 | if (path == nil) |
| 177 | Melder_throw (U"Don't understand where you want to save (1)."); |
| 178 | const char *outfileName_utf8 = [path UTF8String]; |
| 179 | if (! outfileName_utf8) |
| 180 | Melder_throw (U"Don't understand where you want to save (2)."); |
| 181 | structMelderFile file { }; |
| 182 | Melder_8bitFileRepresentationToStr32_inplace (outfileName_utf8, file. path); // BUG: unsafe buffer |
| 183 | outfileName = Melder_dup (MelderFile_peekPath (& file)); |
| 184 | } |
| 185 | setlocale (LC_ALL, "C"); |
| 186 | #endif |
| 187 | return outfileNamecxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 188 | } |
| 189 | |
| 190 | autostring32cxx . . $ autostring32# GuiFileSelect_getFolderNamecxx . . $ GuiFileSelect_getFolderName(5af1513ecf60daff). (GuiWindowcxx . . $ GuiWindow# optionalParent, conststring32cxx . . $ conststring32# title) { |
| 191 | autoMelderSaveCurrentFoldercxx . . $ autoMelderSaveCurrentFolder# saveFoldercxx . . $ autoMelderSaveCurrentFolder#autoMelderSaveCurrentFolder(49f6e7a06ebc5aa8).; |
| 192 | autostring32cxx . . $ autostring32# folderNamecxx . . $ _autostring#_autostring(49f6e7a06ebc5aa8).; |
| 193 | #if gtkcxx . . $ `sys/Gui.h:33:10`! |
| 194 | static structMelderFilecxx . . $ structMelderFile# file; |
| 195 | GuiObjectcxx . . $ GuiObject# dialog = gtk_file_chooser_native_new (Melder_peek32to8 (title), nullptr, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, |
| 196 | "Ch_oose", "_Cancel"); |
| 197 | if (optionalParent) |
| 198 | gtk_native_dialog_set_transient_for (GTK_NATIVE_DIALOG (dialog), GTK_WINDOWcxx . . $ `<external>/3777575239456597881/gtkwindow.h:40:9`! (optionalParent -> d_gtkWindow)); |
| 199 | if (! MelderFile_isNullcxx . . $ MelderFile_isNull(8f92cdf95b27d908). (& file)) |
| 200 | gtk_file_chooser_set_filename (GTK_FILE_CHOOSERcxx . . $ `<external>/2609165349595818446/gtkfilechooser.h:32:9`! (dialog), MelderFile_peekPath8cxx . . $ MelderFile_peekPath8(3a3888509ce82e67). (& file)); |
| 201 | if (gtk_native_dialog_run (GTK_NATIVE_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) { |
| 202 | char *folderName_utf8 = gtk_file_chooser_get_filename (GTK_FILE_CHOOSERcxx . . $ `<external>/2609165349595818446/gtkfilechooser.h:32:9`! (dialog)); |
| 203 | folderName =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_8to32_ecxx . . $ Melder_8to32_e(f01fe69d6ed2498b). (folderName_utf8); |
| 204 | g_free (folderName_utf8); |
| 205 | Melder_pathToFilecxx . . $ Melder_pathToFile(82691f2ee21fbb13). (folderName.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), & file); |
| 206 | } |
| 207 | gtk_native_dialog_destroy (GTK_NATIVE_DIALOG (dialog)); |
| 208 | setlocale (LC_ALLcxx . . $ `<external>/3736975272097309885/locale.h:41:9`!, "C"); |
| 209 | #elif motif |
| 210 | static WCHAR fullFileNameW [3000+2]; |
| 211 | static bool comInited = false; |
| 212 | if (! comInited) { |
| 213 | CoInitializeEx (nullptr, COINIT_APARTMENTTHREADED); |
| 214 | comInited = true; |
| 215 | } |
| 216 | static BROWSEINFO info; |
| 217 | ZeroMemory (& info, sizeof (BROWSEINFO)); |
| 218 | info. hwndOwner = ( optionalParent && optionalParent -> d_xmShell ? (HWND) XtWindow (optionalParent -> d_xmShell) : nullptr ); |
| 219 | info. ulFlags = BIF_USENEWUI; |
| 220 | info. pidlRoot = nullptr; // everything on the computer should be browsable |
| 221 | info. pszDisplayName = nullptr; // this would only give the bare folder name, not the full path |
| 222 | info. lpszTitle = Melder_peek32toW_fileSystem (title); |
| 223 | LPITEMIDLIST idList = SHBrowseForFolder (& info); |
| 224 | SHGetPathFromIDList (idList, fullFileNameW); |
| 225 | CoTaskMemFree (idList); |
| 226 | folderName = Melder_Wto32 (fullFileNameW); |
| 227 | setlocale (LC_ALL, "C"); |
| 228 | #elif cocoa |
| 229 | (void) optionalParent; |
| 230 | NSOpenPanel *openPanel = [NSOpenPanel openPanel]; |
| 231 | [openPanel setTitle: [NSString stringWithUTF8String: Melder_peek32to8 (title)]]; |
| 232 | [openPanel setAllowsMultipleSelection: NO]; |
| 233 | [openPanel setCanChooseDirectories: YES]; |
| 234 | [openPanel setCanChooseFiles: NO]; |
| 235 | [openPanel setPrompt: @"Choose"]; |
| 236 | if ([openPanel runModal] == NSFileHandlingPanelOKButton) { |
| 237 | for (NSURL *url in [openPanel URLs]) { |
| 238 | const char *folderName_utf8 = [[url path] UTF8String]; |
| 239 | structMelderFolder folder { }; |
| 240 | Melder_8bitFileRepresentationToStr32_inplace (folderName_utf8, folder. path); // BUG: unsafe buffer |
| 241 | folderName = Melder_dup (MelderFolder_peekPath (& folder)); |
| 242 | } |
| 243 | } |
| 244 | setlocale (LC_ALL, "C"); |
| 245 | #endif |
| 246 | return folderNamecxx . . $ _autostring#_autostring(39021ecceedcc7d2).; |
| 247 | } |
| 248 | |
| 249 | /* End of file GuiFileSelect.cpp */ |