Fix OpenFolder dialog behavior on Windows

main
Mykola Rubets 2024-03-24 18:20:28 +02:00 committed by Sam Lantinga
parent 13933222ee
commit fc053fb499
1 changed files with 32 additions and 3 deletions

View File

@ -44,6 +44,7 @@ typedef struct
{ {
SDL_Window* parent; SDL_Window* parent;
SDL_DialogFileCallback callback; SDL_DialogFileCallback callback;
const char* default_folder;
void* userdata; void* userdata;
} winFArgs; } winFArgs;
@ -327,6 +328,31 @@ int windows_file_dialog_thread(void* ptr)
return 0; return 0;
} }
int CALLBACK browse_callback_proc(
HWND hwnd,
UINT uMsg,
LPARAM lParam,
LPARAM lpData)
{
switch (uMsg)
{
case BFFM_INITIALIZED :
if(lpData)
{
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
}
break;
case BFFM_SELCHANGED :
break;
case BFFM_VALIDATEFAILED :
break;
default:
break;
}
return 0;
}
void windows_ShowFolderDialog(void* ptr) void windows_ShowFolderDialog(void* ptr)
{ {
winFArgs *args = (winFArgs *) ptr; winFArgs *args = (winFArgs *) ptr;
@ -349,11 +375,13 @@ void windows_ShowFolderDialog(void* ptr)
dialog.pszDisplayName = buffer; dialog.pszDisplayName = buffer;
dialog.lpszTitle = NULL; dialog.lpszTitle = NULL;
dialog.ulFlags = BIF_USENEWUI; dialog.ulFlags = BIF_USENEWUI;
dialog.lpfn = NULL; dialog.lpfn = browse_callback_proc;
dialog.lParam = 0; dialog.lParam = (LPARAM)args->default_folder;
dialog.iImage = 0; dialog.iImage = 0;
if (SHBrowseForFolderW(&dialog)) { LPITEMIDLIST lpItem = SHBrowseForFolderW(&dialog);
if (lpItem != NULL) {
SHGetPathFromIDListW(lpItem, buffer);
char *chosen_file = WIN_StringToUTF8W(buffer); char *chosen_file = WIN_StringToUTF8W(buffer);
const char *files[2] = { chosen_file, NULL }; const char *files[2] = { chosen_file, NULL };
callback(userdata, (const char * const*) files, -1); callback(userdata, (const char * const*) files, -1);
@ -447,6 +475,7 @@ void SDL_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void* userdata, S
args->parent = window; args->parent = window;
args->callback = callback; args->callback = callback;
args->default_folder = default_location;
args->userdata = userdata; args->userdata = userdata;
thread = SDL_CreateThreadInternal(windows_folder_dialog_thread, "SDL_ShowOpenFolderDialog", 0, (void *) args); thread = SDL_CreateThreadInternal(windows_folder_dialog_thread, "SDL_ShowOpenFolderDialog", 0, (void *) args);