Debian Codegraph @

sys/GuiFileSelect.cpp

raw symbols SCIP
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"
20 #include <locale.h>
21 #if motif
22 #include <shlobj.h>
23 #endif
24
25 autoStringSet GuiFileSelect_getInfileNames (GuiWindow optionalParent, conststring32 title, bool allowMultipleFiles) {
26 autoMelderSaveCurrentFolder saveFolder;
27 autoStringSet me = StringSet_create ();
28 #if gtk
29 static structMelderFolder folder { };
30 GuiObject dialog = gtk_file_chooser_native_new (Melder_peek32to8 (title), nullptr, GTK_FILE_CHOOSER_ACTION_OPEN,
31 "_Open", "_Cancel");
32 Melder_assert (dialog);
33 if (optionalParent)
34 gtk_native_dialog_set_transient_for (GTK_NATIVE_DIALOG (dialog), GTK_WINDOW (optionalParent -> d_gtkWindow));
35 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), allowMultipleFiles);
36 if (MelderFolder_isNull (& folder)) // first time?
37 Melder_getCurrentFolder (& folder);
38 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), MelderFolder_peekPath8 (& 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_CHOOSER (dialog));
41 if (infolderName_utf8) {
42 conststring32 infolderName = Melder_peek8to32_u (infolderName_utf8); // dangle
43 Melder_pathToFolder (infolderName, & folder);
44 g_free (infolderName_utf8);
45 }
46 GSList *infileNames_list = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (dialog));
47 for (GSList *element = infileNames_list; element != nullptr; element = g_slist_next (element)) {
48 char *infileName_utf8 = (char *) element -> data;
49 my addString_copy (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_ALL, "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 me;
124 }
125
126 autostring32 GuiFileSelect_getOutfileName (GuiWindow optionalParent, conststring32 title, conststring32 defaultName) {
127 autoMelderSaveCurrentFolder saveFolder;
128 autostring32 outfileName;
129 #if gtk
130 static structMelderFile file;
131 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_WINDOW (optionalParent -> d_gtkWindow));
135 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), true);
136 if (! MelderFile_isNull (& file))
137 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (dialog), MelderFile_peekPath8 (& file));
138 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (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_CHOOSER (dialog));
141 outfileName = Melder_8to32_e (outfileName_utf8);
142 g_free (outfileName_utf8);
143 Melder_pathToFile (outfileName.get(), & file);
144 }
145 gtk_native_dialog_destroy (GTK_NATIVE_DIALOG (dialog));
146 setlocale (LC_ALL, "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 outfileName;
188 }
189
190 autostring32 GuiFileSelect_getFolderName (GuiWindow optionalParent, conststring32 title) {
191 autoMelderSaveCurrentFolder saveFolder;
192 autostring32 folderName;
193 #if gtk
194 static structMelderFile file;
195 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_WINDOW (optionalParent -> d_gtkWindow));
199 if (! MelderFile_isNull (& file))
200 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (dialog), MelderFile_peekPath8 (& 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_CHOOSER (dialog));
203 folderName = Melder_8to32_e (folderName_utf8);
204 g_free (folderName_utf8);
205 Melder_pathToFile (folderName.get(), & file);
206 }
207 gtk_native_dialog_destroy (GTK_NATIVE_DIALOG (dialog));
208 setlocale (LC_ALL, "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 folderName;
247 }
248
249 /* End of file GuiFileSelect.cpp */