|
@@ -19,6 +19,7 @@
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
*/
|
|
|
#include "SDL_internal.h"
|
|
|
+#include "../SDL_dialog.h"
|
|
|
#include "../SDL_dialog_utils.h"
|
|
|
|
|
|
#include <windows.h>
|
|
@@ -32,7 +33,7 @@
|
|
|
|
|
|
typedef struct
|
|
|
{
|
|
|
- int is_save;
|
|
|
+ bool is_save;
|
|
|
const SDL_DialogFileFilter *filters;
|
|
|
int nfilters;
|
|
|
const char* default_file;
|
|
@@ -40,6 +41,9 @@ typedef struct
|
|
|
DWORD flags;
|
|
|
SDL_DialogFileCallback callback;
|
|
|
void* userdata;
|
|
|
+ const char* title;
|
|
|
+ const char* accept;
|
|
|
+ const char* cancel;
|
|
|
} winArgs;
|
|
|
|
|
|
typedef struct
|
|
@@ -48,6 +52,9 @@ typedef struct
|
|
|
SDL_DialogFileCallback callback;
|
|
|
const char* default_folder;
|
|
|
void* userdata;
|
|
|
+ const char* title;
|
|
|
+ const char* accept;
|
|
|
+ const char* cancel;
|
|
|
} winFArgs;
|
|
|
|
|
|
/** Converts dialog.nFilterIndex to SDL-compatible value */
|
|
@@ -60,7 +67,7 @@ int getFilterIndex(int as_reported_by_windows)
|
|
|
void windows_ShowFileDialog(void *ptr)
|
|
|
{
|
|
|
winArgs *args = (winArgs *) ptr;
|
|
|
- int is_save = args->is_save;
|
|
|
+ bool is_save = args->is_save;
|
|
|
const SDL_DialogFileFilter *filters = args->filters;
|
|
|
int nfilters = args->nfilters;
|
|
|
const char* default_file = args->default_file;
|
|
@@ -68,6 +75,7 @@ void windows_ShowFileDialog(void *ptr)
|
|
|
DWORD flags = args->flags;
|
|
|
SDL_DialogFileCallback callback = args->callback;
|
|
|
void* userdata = args->userdata;
|
|
|
+ const char *title = args->title;
|
|
|
|
|
|
/* GetOpenFileName and GetSaveFileName have the same signature
|
|
|
(yes, LPOPENFILENAMEW even for the save dialog) */
|
|
@@ -185,6 +193,34 @@ void windows_ShowFileDialog(void *ptr)
|
|
|
SDL_free(filterlist);
|
|
|
}
|
|
|
|
|
|
+ wchar_t *title_w = NULL;
|
|
|
+
|
|
|
+ if (title) {
|
|
|
+ int title_len = (int) SDL_strlen(title);
|
|
|
+
|
|
|
+ /* If the title is longer than 2GB, it might be exploitable. */
|
|
|
+ if (title_len < 0) {
|
|
|
+ title_len = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ int title_wlen = MultiByteToWideChar(CP_UTF8, 0, title, title_len, NULL, 0);
|
|
|
+
|
|
|
+ if (title_wlen < 0) {
|
|
|
+ title_wlen = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ title_w = (wchar_t *)SDL_malloc(title_wlen * sizeof(wchar_t));
|
|
|
+
|
|
|
+ if (!title_w) {
|
|
|
+ SDL_free(filter_wchar);
|
|
|
+ SDL_free(filebuffer);
|
|
|
+ callback(userdata, NULL, -1);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ MultiByteToWideChar(CP_UTF8, 0, title, title_len, title_w, title_wlen);
|
|
|
+ }
|
|
|
+
|
|
|
OPENFILENAMEW dialog;
|
|
|
dialog.lStructSize = sizeof(OPENFILENAME);
|
|
|
dialog.hwndOwner = window;
|
|
@@ -197,7 +233,7 @@ void windows_ShowFileDialog(void *ptr)
|
|
|
dialog.nMaxFile = SELECTLIST_SIZE;
|
|
|
dialog.lpstrFileTitle = NULL;
|
|
|
dialog.lpstrInitialDir = *initfolder ? initfolder : NULL;
|
|
|
- dialog.lpstrTitle = NULL;
|
|
|
+ dialog.lpstrTitle = title_w;
|
|
|
dialog.Flags = flags | OFN_EXPLORER | OFN_HIDEREADONLY | OFN_NOCHANGEDIR;
|
|
|
dialog.nFileOffset = 0;
|
|
|
dialog.nFileExtension = 0;
|
|
@@ -211,6 +247,7 @@ void windows_ShowFileDialog(void *ptr)
|
|
|
BOOL result = pGetAnyFileName(&dialog);
|
|
|
|
|
|
SDL_free(filter_wchar);
|
|
|
+ SDL_free(title_w);
|
|
|
|
|
|
if (result) {
|
|
|
if (!(flags & OFN_ALLOWMULTISELECT)) {
|
|
@@ -388,25 +425,54 @@ void windows_ShowFolderDialog(void* ptr)
|
|
|
SDL_DialogFileCallback callback = args->callback;
|
|
|
void *userdata = args->userdata;
|
|
|
HWND parent = NULL;
|
|
|
+ const char *title = args->title;
|
|
|
|
|
|
if (window) {
|
|
|
parent = (HWND) SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
|
|
|
}
|
|
|
|
|
|
+ wchar_t *title_w = NULL;
|
|
|
+
|
|
|
+ if (title) {
|
|
|
+ int title_len = (int) SDL_strlen(title);
|
|
|
+
|
|
|
+ /* If the title is longer than 2GB, it might be exploitable. */
|
|
|
+ if (title_len < 0) {
|
|
|
+ title_len = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ int title_wlen = MultiByteToWideChar(CP_UTF8, 0, title, title_len, NULL, 0);
|
|
|
+
|
|
|
+ if (title_wlen < 0) {
|
|
|
+ title_wlen = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ title_w = (wchar_t *)SDL_malloc(title_wlen * sizeof(wchar_t));
|
|
|
+
|
|
|
+ if (!title_w) {
|
|
|
+ callback(userdata, NULL, -1);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ MultiByteToWideChar(CP_UTF8, 0, title, title_len, title_w, title_wlen);
|
|
|
+ }
|
|
|
+
|
|
|
wchar_t buffer[MAX_PATH];
|
|
|
|
|
|
BROWSEINFOW dialog;
|
|
|
dialog.hwndOwner = parent;
|
|
|
dialog.pidlRoot = NULL;
|
|
|
- // Windows docs say this is `LPTSTR` - apparently it's actually `LPWSTR`
|
|
|
dialog.pszDisplayName = buffer;
|
|
|
- dialog.lpszTitle = NULL;
|
|
|
+ dialog.lpszTitle = title_w;
|
|
|
dialog.ulFlags = BIF_USENEWUI;
|
|
|
dialog.lpfn = browse_callback_proc;
|
|
|
dialog.lParam = (LPARAM)args->default_folder;
|
|
|
dialog.iImage = 0;
|
|
|
|
|
|
LPITEMIDLIST lpItem = SHBrowseForFolderW(&dialog);
|
|
|
+
|
|
|
+ SDL_free(title_w);
|
|
|
+
|
|
|
if (lpItem != NULL) {
|
|
|
SHGetPathFromIDListW(lpItem, buffer);
|
|
|
char *chosen_file = WIN_StringToUTF8W(buffer);
|
|
@@ -426,13 +492,12 @@ int windows_folder_dialog_thread(void* ptr)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-void SDL_ShowOpenFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL_Window* window, const SDL_DialogFileFilter *filters, int nfilters, const char* default_location, bool allow_many)
|
|
|
+static void ShowFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL_Window* window, const SDL_DialogFileFilter *filters, int nfilters, const char* default_location, bool allow_many, bool is_save, const char* title, const char* accept, const char* cancel)
|
|
|
{
|
|
|
winArgs *args;
|
|
|
SDL_Thread *thread;
|
|
|
|
|
|
if (SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER) != NULL) {
|
|
|
- SDL_Log("%s", SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER));
|
|
|
SDL_SetError("File dialog driver unsupported");
|
|
|
callback(userdata, NULL, -1);
|
|
|
return;
|
|
@@ -444,16 +509,19 @@ void SDL_ShowOpenFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- args->is_save = 0;
|
|
|
+ args->is_save = is_save;
|
|
|
args->filters = filters;
|
|
|
args->nfilters = nfilters;
|
|
|
args->default_file = default_location;
|
|
|
args->parent = window;
|
|
|
- args->flags = (allow_many != false) ? OFN_ALLOWMULTISELECT : 0;
|
|
|
+ args->flags = allow_many ? OFN_ALLOWMULTISELECT : 0;
|
|
|
args->callback = callback;
|
|
|
args->userdata = userdata;
|
|
|
+ args->title = title;
|
|
|
+ args->accept = accept;
|
|
|
+ args->cancel = cancel;
|
|
|
|
|
|
- thread = SDL_CreateThread(windows_file_dialog_thread, "SDL_ShowOpenFileDialog", (void *) args);
|
|
|
+ thread = SDL_CreateThread(windows_file_dialog_thread, "SDL_Windows_ShowFileDialog", (void *) args);
|
|
|
|
|
|
if (thread == NULL) {
|
|
|
callback(userdata, NULL, -1);
|
|
@@ -464,9 +532,9 @@ void SDL_ShowOpenFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL
|
|
|
SDL_DetachThread(thread);
|
|
|
}
|
|
|
|
|
|
-void SDL_ShowSaveFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL_Window* window, const SDL_DialogFileFilter *filters, int nfilters, const char* default_location)
|
|
|
+void ShowFolderDialog(SDL_DialogFileCallback callback, void* userdata, SDL_Window* window, const char* default_location, bool allow_many, const char* title, const char* accept, const char* cancel)
|
|
|
{
|
|
|
- winArgs *args;
|
|
|
+ winFArgs *args;
|
|
|
SDL_Thread *thread;
|
|
|
|
|
|
if (SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER) != NULL) {
|
|
@@ -475,22 +543,21 @@ void SDL_ShowSaveFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- args = (winArgs *)SDL_malloc(sizeof(*args));
|
|
|
+ args = (winFArgs *)SDL_malloc(sizeof(*args));
|
|
|
if (args == NULL) {
|
|
|
callback(userdata, NULL, -1);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- args->is_save = 1;
|
|
|
- args->filters = filters;
|
|
|
- args->nfilters = nfilters;
|
|
|
- args->default_file = default_location;
|
|
|
args->parent = window;
|
|
|
- args->flags = 0;
|
|
|
args->callback = callback;
|
|
|
+ args->default_folder = default_location;
|
|
|
args->userdata = userdata;
|
|
|
+ args->title = title;
|
|
|
+ args->accept = accept;
|
|
|
+ args->cancel = cancel;
|
|
|
|
|
|
- thread = SDL_CreateThread(windows_file_dialog_thread, "SDL_ShowSaveFileDialog", (void *) args);
|
|
|
+ thread = SDL_CreateThread(windows_folder_dialog_thread, "SDL_Windows_ShowFolderDialog", (void *) args);
|
|
|
|
|
|
if (thread == NULL) {
|
|
|
callback(userdata, NULL, -1);
|
|
@@ -501,35 +568,30 @@ void SDL_ShowSaveFileDialog(SDL_DialogFileCallback callback, void* userdata, SDL
|
|
|
SDL_DetachThread(thread);
|
|
|
}
|
|
|
|
|
|
-void SDL_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void* userdata, SDL_Window* window, const char* default_location, bool allow_many)
|
|
|
+void SDL_SYS_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props)
|
|
|
{
|
|
|
- winFArgs *args;
|
|
|
- SDL_Thread *thread;
|
|
|
-
|
|
|
- if (SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER) != NULL) {
|
|
|
- SDL_SetError("File dialog driver unsupported");
|
|
|
- callback(userdata, NULL, -1);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- args = (winFArgs *)SDL_malloc(sizeof(*args));
|
|
|
- if (args == NULL) {
|
|
|
- callback(userdata, NULL, -1);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- args->parent = window;
|
|
|
- args->callback = callback;
|
|
|
- args->default_folder = default_location;
|
|
|
- args->userdata = userdata;
|
|
|
-
|
|
|
- thread = SDL_CreateThread(windows_folder_dialog_thread, "SDL_ShowOpenFolderDialog", (void *) args);
|
|
|
-
|
|
|
- if (thread == NULL) {
|
|
|
- callback(userdata, NULL, -1);
|
|
|
- SDL_free(args);
|
|
|
- return;
|
|
|
- }
|
|
|
+ /* The internal functions will start threads, and the properties may be freed as soon as this function returns.
|
|
|
+ Save a copy of what we need before invoking the functions and starting the threads. */
|
|
|
+ SDL_Window* window = SDL_GetPointerProperty(props, SDL_PROP_FILE_DIALOG_WINDOW_POINTER, NULL);
|
|
|
+ SDL_DialogFileFilter *filters = SDL_GetPointerProperty(props, SDL_PROP_FILE_DIALOG_FILTERS_POINTER, NULL);
|
|
|
+ int nfilters = (int) SDL_GetNumberProperty(props, SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER, 0);
|
|
|
+ bool allow_many = SDL_GetBooleanProperty(props, SDL_PROP_FILE_DIALOG_MANY_BOOLEAN, false);
|
|
|
+ const char* default_location = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_LOCATION_STRING, NULL);
|
|
|
+ const char* title = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING, NULL);
|
|
|
+ const char* accept = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_ACCEPT_STRING, NULL);
|
|
|
+ const char* cancel = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_CANCEL_STRING, NULL);
|
|
|
+ bool is_save = false;
|
|
|
+
|
|
|
+ switch (type) {
|
|
|
+ case SDL_FILEDIALOG_SAVEFILE:
|
|
|
+ is_save = true;
|
|
|
+ SDL_FALLTHROUGH;
|
|
|
+ case SDL_FILEDIALOG_OPENFILE:
|
|
|
+ ShowFileDialog(callback, userdata, window, filters, nfilters, default_location, allow_many, is_save, title, accept, cancel);
|
|
|
+ break;
|
|
|
|
|
|
- SDL_DetachThread(thread);
|
|
|
+ case SDL_FILEDIALOG_OPENFOLDER:
|
|
|
+ ShowFolderDialog(callback, userdata, window, default_location, allow_many, title, accept, cancel);
|
|
|
+ break;
|
|
|
+ };
|
|
|
}
|