SDL_windowsdialog.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "../SDL_dialog_utils.h"
  20. #include <windows.h>
  21. #include <commdlg.h>
  22. #include <shlobj.h>
  23. #include "../../core/windows/SDL_windows.h"
  24. #include "../../thread/SDL_systhread.h"
  25. /* If this number is too small, selecting too many files will give an error */
  26. #define SELECTLIST_SIZE 65536
  27. typedef struct
  28. {
  29. int is_save;
  30. const SDL_DialogFileFilter *filters;
  31. const char* default_file;
  32. SDL_Window* parent;
  33. DWORD flags;
  34. SDL_DialogFileCallback callback;
  35. void* userdata;
  36. } winArgs;
  37. typedef struct
  38. {
  39. SDL_Window* parent;
  40. SDL_DialogFileCallback callback;
  41. const char* default_folder;
  42. void* userdata;
  43. } winFArgs;
  44. /** Converts dialog.nFilterIndex to SDL-compatible value */
  45. int getFilterIndex(int as_reported_by_windows, const SDL_DialogFileFilter *filters)
  46. {
  47. int filter_index = as_reported_by_windows - 1;
  48. if (filter_index < 0) {
  49. filter_index = 0;
  50. for (const SDL_DialogFileFilter *filter = filters; filter && filter->name && filter->pattern; filter++) {
  51. filter_index++;
  52. }
  53. }
  54. return filter_index;
  55. }
  56. /* TODO: The new version of file dialogs */
  57. void windows_ShowFileDialog(void *ptr)
  58. {
  59. winArgs *args = (winArgs *) ptr;
  60. int is_save = args->is_save;
  61. const SDL_DialogFileFilter *filters = args->filters;
  62. const char* default_file = args->default_file;
  63. SDL_Window* parent = args->parent;
  64. DWORD flags = args->flags;
  65. SDL_DialogFileCallback callback = args->callback;
  66. void* userdata = args->userdata;
  67. /* GetOpenFileName and GetSaveFileName have the same signature
  68. (yes, LPOPENFILENAMEW even for the save dialog) */
  69. typedef BOOL (WINAPI *pfnGetAnyFileNameW)(LPOPENFILENAMEW);
  70. typedef DWORD (WINAPI *pfnCommDlgExtendedError)(void);
  71. HMODULE lib = LoadLibraryW(L"Comdlg32.dll");
  72. pfnGetAnyFileNameW pGetAnyFileName = NULL;
  73. pfnCommDlgExtendedError pCommDlgExtendedError = NULL;
  74. if (lib) {
  75. pGetAnyFileName = (pfnGetAnyFileNameW) GetProcAddress(lib, is_save ? "GetSaveFileNameW" : "GetOpenFileNameW");
  76. pCommDlgExtendedError = (pfnCommDlgExtendedError) GetProcAddress(lib, "CommDlgExtendedError");
  77. } else {
  78. SDL_SetError("Couldn't load Comdlg32.dll");
  79. callback(userdata, NULL, -1);
  80. return;
  81. }
  82. if (!pGetAnyFileName) {
  83. SDL_SetError("Couldn't load GetOpenFileName/GetSaveFileName from library");
  84. callback(userdata, NULL, -1);
  85. return;
  86. }
  87. if (!pCommDlgExtendedError) {
  88. SDL_SetError("Couldn't load CommDlgExtendedError from library");
  89. callback(userdata, NULL, -1);
  90. return;
  91. }
  92. HWND window = NULL;
  93. if (parent) {
  94. window = (HWND) SDL_GetProperty(SDL_GetWindowProperties(parent), SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
  95. }
  96. wchar_t *filebuffer; /* lpstrFile */
  97. wchar_t initfolder[MAX_PATH] = L""; /* lpstrInitialDir */
  98. /* If SELECTLIST_SIZE is too large, putting filebuffer on the stack might
  99. cause an overflow */
  100. filebuffer = (wchar_t *) SDL_malloc(SELECTLIST_SIZE * sizeof(wchar_t));
  101. /* Necessary for the return code below */
  102. SDL_memset(filebuffer, 0, SELECTLIST_SIZE * sizeof(wchar_t));
  103. if (default_file) {
  104. /* On Windows 10, 11 and possibly others, lpstrFile can be initialized
  105. with a path and the dialog will start at that location, but *only if
  106. the path contains a filename*. If it ends with a folder (directory
  107. separator), it fails with 0x3002 (12290) FNERR_INVALIDFILENAME. For
  108. that specific case, lpstrInitialDir must be used instead, but just
  109. for that case, because lpstrInitialDir doesn't support file names.
  110. On top of that, lpstrInitialDir hides a special algorithm that
  111. decides which folder to actually use as starting point, which may or
  112. may not be the one provided, or some other unrelated folder. Also,
  113. the algorithm changes between platforms. Assuming the documentation
  114. is correct, the algorithm is there under 'lpstrInitialDir':
  115. https://learn.microsoft.com/en-us/windows/win32/api/commdlg/ns-commdlg-openfilenamew
  116. Finally, lpstrFile does not support forward slashes. lpstrInitialDir
  117. does, though. */
  118. char last_c = default_file[SDL_strlen(default_file) - 1];
  119. if (last_c == '\\' || last_c == '/') {
  120. MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, default_file, -1, initfolder, MAX_PATH);
  121. } else {
  122. MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, default_file, -1, filebuffer, MAX_PATH);
  123. for (int i = 0; i < SELECTLIST_SIZE; i++) {
  124. if (filebuffer[i] == L'/') {
  125. filebuffer[i] = L'\\';
  126. }
  127. }
  128. }
  129. }
  130. wchar_t *filter_wchar = NULL;
  131. if (filters) {
  132. /* '\x01' is used in place of a null byte */
  133. /* suffix needs two null bytes in case the filter list is empty */
  134. char *filterlist = convert_filters(filters, NULL, "", "", "\x01\x01", "",
  135. "\x01", "\x01", "*.", ";*.", "");
  136. if (!filterlist) {
  137. callback(userdata, NULL, -1);
  138. SDL_free(filebuffer);
  139. return;
  140. }
  141. int filter_len = (int)SDL_strlen(filterlist);
  142. for (char *c = filterlist; *c; c++) {
  143. if (*c == '\x01') {
  144. *c = '\0';
  145. }
  146. }
  147. int filter_wlen = MultiByteToWideChar(CP_UTF8, 0, filterlist, filter_len, NULL, 0);
  148. filter_wchar = SDL_malloc(filter_wlen * sizeof(wchar_t));
  149. if (!filter_wchar) {
  150. SDL_free(filterlist);
  151. callback(userdata, NULL, -1);
  152. SDL_free(filebuffer);
  153. return;
  154. }
  155. MultiByteToWideChar(CP_UTF8, 0, filterlist, filter_len, filter_wchar, filter_wlen);
  156. SDL_free(filterlist);
  157. }
  158. OPENFILENAMEW dialog;
  159. dialog.lStructSize = sizeof(OPENFILENAME);
  160. dialog.hwndOwner = window;
  161. dialog.hInstance = 0;
  162. dialog.lpstrFilter = filter_wchar;
  163. dialog.lpstrCustomFilter = NULL;
  164. dialog.nMaxCustFilter = 0;
  165. dialog.nFilterIndex = 0;
  166. dialog.lpstrFile = filebuffer;
  167. dialog.nMaxFile = SELECTLIST_SIZE;
  168. dialog.lpstrFileTitle = NULL;
  169. dialog.lpstrInitialDir = *initfolder ? initfolder : NULL;
  170. dialog.lpstrTitle = NULL;
  171. dialog.Flags = flags | OFN_EXPLORER | OFN_HIDEREADONLY | OFN_NOCHANGEDIR;
  172. dialog.nFileOffset = 0;
  173. dialog.nFileExtension = 0;
  174. dialog.lpstrDefExt = NULL;
  175. dialog.lCustData = 0;
  176. dialog.lpfnHook = NULL;
  177. dialog.lpTemplateName = NULL;
  178. /* Skipped many mac-exclusive and reserved members */
  179. dialog.FlagsEx = 0;
  180. BOOL result = pGetAnyFileName(&dialog);
  181. SDL_free(filter_wchar);
  182. if (result) {
  183. if (!(flags & OFN_ALLOWMULTISELECT)) {
  184. /* File is a C string stored in dialog.lpstrFile */
  185. char *chosen_file = WIN_StringToUTF8W(dialog.lpstrFile);
  186. const char* opts[2] = { chosen_file, NULL };
  187. callback(userdata, opts, getFilterIndex(dialog.nFilterIndex, filters));
  188. SDL_free(chosen_file);
  189. } else {
  190. /* File is either a C string if the user chose a single file, else
  191. it's a series of strings formatted like:
  192. "C:\\path\\to\\folder\0filename1.ext\0filename2.ext\0\0"
  193. The code below will only stop on a double NULL in all cases, so
  194. it is important that the rest of the buffer has been zeroed. */
  195. char chosen_folder[MAX_PATH];
  196. char chosen_file[MAX_PATH];
  197. wchar_t *file_ptr = dialog.lpstrFile;
  198. size_t nfiles = 0;
  199. size_t chosen_folder_size;
  200. char **chosen_files_list = (char **) SDL_malloc(sizeof(char *) * (nfiles + 1));
  201. if (!chosen_files_list) {
  202. callback(userdata, NULL, -1);
  203. SDL_free(filebuffer);
  204. return;
  205. }
  206. chosen_files_list[nfiles] = NULL;
  207. if (WideCharToMultiByte(CP_UTF8, 0, file_ptr, -1, chosen_folder, MAX_PATH, NULL, NULL) >= MAX_PATH) {
  208. SDL_SetError("Path too long or invalid character in path");
  209. SDL_free(chosen_files_list);
  210. callback(userdata, NULL, -1);
  211. SDL_free(filebuffer);
  212. return;
  213. }
  214. chosen_folder_size = SDL_strlen(chosen_folder);
  215. SDL_strlcpy(chosen_file, chosen_folder, MAX_PATH);
  216. chosen_file[chosen_folder_size] = '\\';
  217. file_ptr += SDL_strlen(chosen_folder) + 1;
  218. while (*file_ptr) {
  219. nfiles++;
  220. char **new_cfl = (char **) SDL_realloc(chosen_files_list, sizeof(char*) * (nfiles + 1));
  221. if (!new_cfl) {
  222. for (size_t i = 0; i < nfiles - 1; i++) {
  223. SDL_free(chosen_files_list[i]);
  224. }
  225. SDL_free(chosen_files_list);
  226. callback(userdata, NULL, -1);
  227. SDL_free(filebuffer);
  228. return;
  229. }
  230. chosen_files_list = new_cfl;
  231. chosen_files_list[nfiles] = NULL;
  232. int diff = ((int) chosen_folder_size) + 1;
  233. if (WideCharToMultiByte(CP_UTF8, 0, file_ptr, -1, chosen_file + diff, MAX_PATH - diff, NULL, NULL) >= MAX_PATH - diff) {
  234. SDL_SetError("Path too long or invalid character in path");
  235. for (size_t i = 0; i < nfiles - 1; i++) {
  236. SDL_free(chosen_files_list[i]);
  237. }
  238. SDL_free(chosen_files_list);
  239. callback(userdata, NULL, -1);
  240. SDL_free(filebuffer);
  241. return;
  242. }
  243. file_ptr += SDL_strlen(chosen_file) + 1 - diff;
  244. chosen_files_list[nfiles - 1] = SDL_strdup(chosen_file);
  245. if (!chosen_files_list[nfiles - 1]) {
  246. for (size_t i = 0; i < nfiles - 1; i++) {
  247. SDL_free(chosen_files_list[i]);
  248. }
  249. SDL_free(chosen_files_list);
  250. callback(userdata, NULL, -1);
  251. SDL_free(filebuffer);
  252. return;
  253. }
  254. }
  255. /* If the user chose only one file, it's all just one string */
  256. if (nfiles == 0) {
  257. nfiles++;
  258. char **new_cfl = (char **) SDL_realloc(chosen_files_list, sizeof(char*) * (nfiles + 1));
  259. if (!new_cfl) {
  260. SDL_free(chosen_files_list);
  261. callback(userdata, NULL, -1);
  262. SDL_free(filebuffer);
  263. return;
  264. }
  265. chosen_files_list = new_cfl;
  266. chosen_files_list[nfiles] = NULL;
  267. chosen_files_list[nfiles - 1] = SDL_strdup(chosen_folder);
  268. if (!chosen_files_list[nfiles - 1]) {
  269. SDL_free(chosen_files_list);
  270. callback(userdata, NULL, -1);
  271. SDL_free(filebuffer);
  272. return;
  273. }
  274. }
  275. callback(userdata, (const char * const*) chosen_files_list, getFilterIndex(dialog.nFilterIndex, filters));
  276. for (size_t i = 0; i < nfiles; i++) {
  277. SDL_free(chosen_files_list[i]);
  278. }
  279. SDL_free(chosen_files_list);
  280. }
  281. } else {
  282. DWORD error = pCommDlgExtendedError();
  283. /* Error code 0 means the user clicked the cancel button. */
  284. if (error == 0) {
  285. /* Unlike SDL's handling of errors, Windows does reset the error
  286. code to 0 after calling GetOpenFileName if another Windows
  287. function before set a different error code, so it's safe to
  288. check for success. */
  289. const char* opts[1] = { NULL };
  290. callback(userdata, opts, getFilterIndex(dialog.nFilterIndex, filters));
  291. } else {
  292. SDL_SetError("Windows error, CommDlgExtendedError: %ld", pCommDlgExtendedError());
  293. callback(userdata, NULL, -1);
  294. }
  295. }
  296. SDL_free(filebuffer);
  297. }
  298. int windows_file_dialog_thread(void* ptr)
  299. {
  300. windows_ShowFileDialog(ptr);
  301. SDL_free(ptr);
  302. return 0;
  303. }
  304. int CALLBACK browse_callback_proc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
  305. {
  306. switch (uMsg) {
  307. case BFFM_INITIALIZED:
  308. if (lpData) {
  309. SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
  310. }
  311. break;
  312. case BFFM_SELCHANGED:
  313. break;
  314. case BFFM_VALIDATEFAILED:
  315. break;
  316. default:
  317. break;
  318. }
  319. return 0;
  320. }
  321. void windows_ShowFolderDialog(void* ptr)
  322. {
  323. winFArgs *args = (winFArgs *) ptr;
  324. SDL_Window *window = args->parent;
  325. SDL_DialogFileCallback callback = args->callback;
  326. void *userdata = args->userdata;
  327. HWND parent = NULL;
  328. if (window) {
  329. parent = (HWND) SDL_GetProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
  330. }
  331. wchar_t buffer[MAX_PATH];
  332. BROWSEINFOW dialog;
  333. dialog.hwndOwner = parent;
  334. dialog.pidlRoot = NULL;
  335. /* Windows docs say this is `LPTSTR` - apparently it's actually `LPWSTR`*/
  336. dialog.pszDisplayName = buffer;
  337. dialog.lpszTitle = NULL;
  338. dialog.ulFlags = BIF_USENEWUI;
  339. dialog.lpfn = browse_callback_proc;
  340. dialog.lParam = (LPARAM)args->default_folder;
  341. dialog.iImage = 0;
  342. LPITEMIDLIST lpItem = SHBrowseForFolderW(&dialog);
  343. if (lpItem != NULL) {
  344. SHGetPathFromIDListW(lpItem, buffer);
  345. char *chosen_file = WIN_StringToUTF8W(buffer);
  346. const char *files[2] = { chosen_file, NULL };
  347. callback(userdata, (const char * const*) files, -1);
  348. SDL_free(chosen_file);
  349. } else {
  350. const char *files[1] = { NULL };
  351. callback(userdata, (const char * const*) files, -1);
  352. }
  353. }
  354. int windows_folder_dialog_thread(void* ptr)
  355. {
  356. windows_ShowFolderDialog(ptr);
  357. SDL_free(ptr);
  358. return 0;
  359. }
  360. void SDL_ShowOpenFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL_Window* window, const SDL_DialogFileFilter *filters, const char* default_location, SDL_bool allow_many)
  361. {
  362. winArgs *args;
  363. SDL_Thread *thread;
  364. if (SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER) != NULL) {
  365. SDL_Log("%s", SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER));
  366. SDL_SetError("File dialog driver unsupported");
  367. callback(userdata, NULL, -1);
  368. return;
  369. }
  370. args = SDL_malloc(sizeof(winArgs));
  371. if (args == NULL) {
  372. callback(userdata, NULL, -1);
  373. return;
  374. }
  375. args->is_save = 0;
  376. args->filters = filters;
  377. args->default_file = default_location;
  378. args->parent = window;
  379. args->flags = (allow_many == SDL_TRUE) ? OFN_ALLOWMULTISELECT : 0;
  380. args->callback = callback;
  381. args->userdata = userdata;
  382. thread = SDL_CreateThread(windows_file_dialog_thread, "SDL_ShowOpenFileDialog", (void *) args);
  383. if (thread == NULL) {
  384. callback(userdata, NULL, -1);
  385. SDL_free(args);
  386. return;
  387. }
  388. SDL_DetachThread(thread);
  389. }
  390. void SDL_ShowSaveFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL_Window* window, const SDL_DialogFileFilter *filters, const char* default_location)
  391. {
  392. winArgs *args;
  393. SDL_Thread *thread;
  394. if (SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER) != NULL) {
  395. SDL_SetError("File dialog driver unsupported");
  396. callback(userdata, NULL, -1);
  397. return;
  398. }
  399. args = SDL_malloc(sizeof(winArgs));
  400. if (args == NULL) {
  401. callback(userdata, NULL, -1);
  402. return;
  403. }
  404. args->is_save = 1;
  405. args->filters = filters;
  406. args->default_file = default_location;
  407. args->parent = window;
  408. args->flags = 0;
  409. args->callback = callback;
  410. args->userdata = userdata;
  411. thread = SDL_CreateThread(windows_file_dialog_thread, "SDL_ShowSaveFileDialog", (void *) args);
  412. if (thread == NULL) {
  413. callback(userdata, NULL, -1);
  414. SDL_free(args);
  415. return;
  416. }
  417. SDL_DetachThread(thread);
  418. }
  419. void SDL_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void* userdata, SDL_Window* window, const char* default_location, SDL_bool allow_many)
  420. {
  421. winFArgs *args;
  422. SDL_Thread *thread;
  423. if (SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER) != NULL) {
  424. SDL_SetError("File dialog driver unsupported");
  425. callback(userdata, NULL, -1);
  426. return;
  427. }
  428. args = SDL_malloc(sizeof(winFArgs));
  429. if (args == NULL) {
  430. callback(userdata, NULL, -1);
  431. return;
  432. }
  433. args->parent = window;
  434. args->callback = callback;
  435. args->default_folder = default_location;
  436. args->userdata = userdata;
  437. thread = SDL_CreateThread(windows_folder_dialog_thread, "SDL_ShowOpenFolderDialog", (void *) args);
  438. if (thread == NULL) {
  439. callback(userdata, NULL, -1);
  440. SDL_free(args);
  441. return;
  442. }
  443. SDL_DetachThread(thread);
  444. }