Skip to content

Commit

Permalink
SDL_ShowFileDialogWithProperties with more options
Browse files Browse the repository at this point in the history
  • Loading branch information
Semphris authored and slouken committed Dec 4, 2024
1 parent dc5a2dd commit a4852f3
Show file tree
Hide file tree
Showing 19 changed files with 598 additions and 287 deletions.
1 change: 1 addition & 0 deletions Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ LOCAL_SRC_FILES := \
$(wildcard $(LOCAL_PATH)/src/core/*.c) \
$(wildcard $(LOCAL_PATH)/src/core/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/cpuinfo/*.c) \
$(LOCAL_PATH)/src/dialog/SDL_dialog.c \
$(LOCAL_PATH)/src/dialog/SDL_dialog_utils.c \
$(LOCAL_PATH)/src/dialog/android/SDL_androiddialog.c \
$(wildcard $(LOCAL_PATH)/src/dynapi/*.c) \
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2874,6 +2874,7 @@ endif()

if (SDL_DIALOG)
sdl_sources(${SDL3_SOURCE_DIR}/src/dialog/SDL_dialog_utils.c)
sdl_sources(${SDL3_SOURCE_DIR}/src/dialog/SDL_dialog.c)
if(ANDROID)
sdl_sources(${SDL3_SOURCE_DIR}/src/dialog/android/SDL_androiddialog.c)
set(HAVE_SDL_DIALOG TRUE)
Expand Down
1 change: 1 addition & 0 deletions VisualC-GDK/SDL/SDL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@
</ClCompile>
<ClCompile Include="..\..\src\camera\dummy\SDL_camera_dummy.c" />
<ClCompile Include="..\..\src\camera\SDL_camera.c" />
<ClCompile Include="..\..\src\dialog\SDL_dialog.c" />
<ClCompile Include="..\..\src\dialog\SDL_dialog_utils.c" />
<ClCompile Include="..\..\src\filesystem\SDL_filesystem.c" />
<ClCompile Include="..\..\src\filesystem\windows\SDL_sysfsops.c" />
Expand Down
3 changes: 3 additions & 0 deletions VisualC-GDK/SDL/SDL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<ClCompile Include="..\..\src\dialog\SDL_dialog_utils.c">
<Filter>dialog</Filter>
</ClCompile>
<ClCompile Include="..\..\src\dialog\SDL_dialog.c">
<Filter>dialog</Filter>
</ClCompile>
<ClCompile Include="..\..\src\filesystem\SDL_filesystem.c">
<Filter>filesystem</Filter>
</ClCompile>
Expand Down
1 change: 1 addition & 0 deletions VisualC/SDL/SDL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@
<ClCompile Include="..\..\src\camera\dummy\SDL_camera_dummy.c" />
<ClCompile Include="..\..\src\camera\mediafoundation\SDL_camera_mediafoundation.c" />
<ClCompile Include="..\..\src\camera\SDL_camera.c" />
<ClCompile Include="..\..\src\dialog\SDL_dialog.c" />
<ClCompile Include="..\..\src\dialog\SDL_dialog_utils.c" />
<ClCompile Include="..\..\src\filesystem\SDL_filesystem.c" />
<ClCompile Include="..\..\src\filesystem\windows\SDL_sysfsops.c" />
Expand Down
3 changes: 3 additions & 0 deletions VisualC/SDL/SDL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,9 @@
<ClCompile Include="..\..\src\camera\SDL_camera.c">
<Filter>camera</Filter>
</ClCompile>
<ClCompile Include="..\..\src\dialog\SDL_dialog.c">
<Filter>dialog</Filter>
</ClCompile>
<ClCompile Include="..\..\src\dialog\SDL_dialog_utils.c">
<Filter>dialog</Filter>
</ClCompile>
Expand Down
94 changes: 86 additions & 8 deletions include/SDL3/SDL_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include <SDL3/SDL_stdinc.h>
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_properties.h>
#include <SDL3/SDL_video.h>

#include <SDL3/SDL_begin_code.h>
Expand All @@ -55,6 +56,7 @@ extern "C" {
* \sa SDL_ShowOpenFileDialog
* \sa SDL_ShowSaveFileDialog
* \sa SDL_ShowOpenFolderDialog
* \sa SDL_ShowFileDialogWithProperties
*/
typedef struct SDL_DialogFileFilter
{
Expand Down Expand Up @@ -93,14 +95,13 @@ typedef struct SDL_DialogFileFilter
* \sa SDL_ShowOpenFileDialog
* \sa SDL_ShowSaveFileDialog
* \sa SDL_ShowOpenFolderDialog
* \sa SDL_ShowFileDialogWithProperties
*/
typedef void (SDLCALL *SDL_DialogFileCallback)(void *userdata, const char * const *filelist, int filter);

/**
* Displays a dialog that lets the user select a file on their filesystem.
*
* This function should only be invoked from the main thread.
*
* This is an asynchronous function; it will return immediately, and the
* result will be passed to the callback.
*
Expand All @@ -127,28 +128,32 @@ typedef void (SDLCALL *SDL_DialogFileCallback)(void *userdata, const char * cons
* Not all platforms support this option.
* \param filters a list of filters, may be NULL. Not all platforms support
* this option, and platforms that do support it may allow the
* user to ignore the filters.
* user to ignore the filters. If non-NULL, it must remain valid
* at least until the callback is invoked.
* \param nfilters the number of filters. Ignored if filters is NULL.
* \param default_location the default folder or file to start the dialog at,
* may be NULL. Not all platforms support this option.
* \param allow_many if non-zero, the user will be allowed to select multiple
* entries. Not all platforms support this option.
*
* \threadsafety This function should be called only from the main thread. The
* callback may be invoked from the same thread or from a
* different one, depending on the OS's constraints.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_DialogFileCallback
* \sa SDL_DialogFileFilter
* \sa SDL_ShowSaveFileDialog
* \sa SDL_ShowOpenFolderDialog
* \sa SDL_ShowFileDialogWithProperties
*/
extern SDL_DECLSPEC void SDLCALL SDL_ShowOpenFileDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const SDL_DialogFileFilter *filters, int nfilters, const char *default_location, bool allow_many);

/**
* Displays a dialog that lets the user choose a new or existing file on their
* filesystem.
*
* This function should only be invoked from the main thread.
*
* This is an asynchronous function; it will return immediately, and the
* result will be passed to the callback.
*
Expand All @@ -174,25 +179,29 @@ extern SDL_DECLSPEC void SDLCALL SDL_ShowOpenFileDialog(SDL_DialogFileCallback c
* Not all platforms support this option.
* \param filters a list of filters, may be NULL. Not all platforms support
* this option, and platforms that do support it may allow the
* user to ignore the filters.
* user to ignore the filters. If non-NULL, it must remain valid
* at least until the callback is invoked.
* \param nfilters the number of filters. Ignored if filters is NULL.
* \param default_location the default folder or file to start the dialog at,
* may be NULL. Not all platforms support this option.
*
* \threadsafety This function should be called only from the main thread. The
* callback may be invoked from the same thread or from a
* different one, depending on the OS's constraints.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_DialogFileCallback
* \sa SDL_DialogFileFilter
* \sa SDL_ShowOpenFileDialog
* \sa SDL_ShowOpenFolderDialog
* \sa SDL_ShowFileDialogWithProperties
*/
extern SDL_DECLSPEC void SDLCALL SDL_ShowSaveFileDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const SDL_DialogFileFilter *filters, int nfilters, const char *default_location);

/**
* Displays a dialog that lets the user select a folder on their filesystem.
*
* This function should only be invoked from the main thread.
*
* This is an asynchronous function; it will return immediately, and the
* result will be passed to the callback.
*
Expand Down Expand Up @@ -222,14 +231,83 @@ extern SDL_DECLSPEC void SDLCALL SDL_ShowSaveFileDialog(SDL_DialogFileCallback c
* \param allow_many if non-zero, the user will be allowed to select multiple
* entries. Not all platforms support this option.
*
* \threadsafety This function should be called only from the main thread. The
* callback may be invoked from the same thread or from a
* different one, depending on the OS's constraints.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_DialogFileCallback
* \sa SDL_ShowOpenFileDialog
* \sa SDL_ShowSaveFileDialog
* \sa SDL_ShowFileDialogWithProperties
*/
extern SDL_DECLSPEC void SDLCALL SDL_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const char *default_location, bool allow_many);

typedef enum SDL_FileDialogType {
SDL_FILEDIALOG_OPENFILE,
SDL_FILEDIALOG_SAVEFILE,
SDL_FILEDIALOG_OPENFOLDER
} SDL_FileDialogType;

#define SDL_PROP_FILE_DIALOG_FILTERS_POINTER "SDL.filedialog.filters"
#define SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER "SDL.filedialog.nfilters"
#define SDL_PROP_FILE_DIALOG_WINDOW_POINTER "SDL.filedialog.window"
#define SDL_PROP_FILE_DIALOG_LOCATION_STRING "SDL.filedialog.location"
#define SDL_PROP_FILE_DIALOG_MANY_BOOLEAN "SDL.filedialog.many"
#define SDL_PROP_FILE_DIALOG_TITLE_STRING "SDL.filedialog.title"
#define SDL_PROP_FILE_DIALOG_ACCEPT_STRING "SDL.filedialog.accept"
#define SDL_PROP_FILE_DIALOG_CANCEL_STRING "SDL.filedialog.cancel"

/**
* Create and launch a file dialog with the specified properties.
*
* These are the supported properties:
*
* - `SDL_PROP_FILE_DIALOG_FILTERS_POINTER`: a pointer to a list of
* SDL_DialogFileFilter's, which will be used as filters for file-based
* selections. Ignored if the dialog is an "Open Folder" dialog. If non-NULL,
* the array of filters must remain valid at least until the callback is
* invoked.
* - `SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER`: the number of filters in the array
* of filters, if it exists.
* - `SDL_PROP_FILE_DIALOG_WINDOW_POINTER`: the window that the dialog should
* be modal for.
* - `SDL_PROP_FILE_DIALOG_LOCATION_STRING`: the default folder or file to
* start the dialog at.
* - `SDL_PROP_FILE_DIALOG_MANY_BOOLEAN`: true to allow the user to select more
* than one entry.
* - `SDL_PROP_FILE_DIALOG_TITLE_STRING`: the title for the dialog.
* - `SDL_PROP_FILE_DIALOG_ACCEPT_STRING`: the label that the accept button
* should have.
* - `SDL_PROP_FILE_DIALOG_CANCEL_STRING`: the label that the cancel button
* should have.
*
* Note that each platform may or may not support any of the properties.
*
* \param type the type of file dialog.
* \param callback a function pointer to be invoked when the user selects a
* file and accepts, or cancels the dialog, or an error
* occurs.
* \param userdata an optional pointer to pass extra data to the callback when
* it will be invoked.
* \param props the properties to use.
*
* \threadsafety This function should be called only from the main thread. The
* callback may be invoked from the same thread or from a
* different one, depending on the OS's constraints.
*
* \since This function is available since SDL 3.2.0.
*
* \sa SDL_FileDialogType
* \sa SDL_DialogFileCallback
* \sa SDL_DialogFileFilter
* \sa SDL_ShowOpenFileDialog
* \sa SDL_ShowSaveFileDialog
* \sa SDL_ShowOpenFolderDialog
*/
extern SDL_DECLSPEC void SDLCALL SDL_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props);

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
}
Expand Down
103 changes: 103 additions & 0 deletions src/dialog/SDL_dialog.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
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"

void SDL_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props)
{
if (!callback) {
return;
}

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, -1);

if (filters && nfilters == -1) {
SDL_SetError("Set filter pointers, but didn't set number of filters (SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER)");
callback(userdata, NULL, -1);
return;
}

const char *msg = validate_filters(filters, nfilters);

if (msg) {
SDL_SetError("Invalid dialog file filters: %s", msg);
callback(userdata, NULL, -1);
return;
}

switch (type) {
case SDL_FILEDIALOG_OPENFILE:
case SDL_FILEDIALOG_SAVEFILE:
case SDL_FILEDIALOG_OPENFOLDER:
SDL_SYS_ShowFileDialogWithProperties(type, callback, userdata, props);
break;

default:
SDL_SetError("Unsupported file dialog type: %d", (int) type);
callback(userdata, NULL, -1);
break;
};
}

void SDL_ShowOpenFileDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const SDL_DialogFileFilter *filters, int nfilters, const char *default_location, bool allow_many)
{
SDL_PropertiesID props = SDL_CreateProperties();

SDL_SetPointerProperty(props, SDL_PROP_FILE_DIALOG_FILTERS_POINTER, (void *) filters);
SDL_SetNumberProperty(props, SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER, nfilters);
SDL_SetPointerProperty(props, SDL_PROP_FILE_DIALOG_WINDOW_POINTER, window);
SDL_SetStringProperty(props, SDL_PROP_FILE_DIALOG_LOCATION_STRING, default_location);
SDL_SetBooleanProperty(props, SDL_PROP_FILE_DIALOG_MANY_BOOLEAN, allow_many);

SDL_ShowFileDialogWithProperties(SDL_FILEDIALOG_OPENFILE, callback, userdata, props);

SDL_DestroyProperties(props);
}

void SDL_ShowSaveFileDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const SDL_DialogFileFilter *filters, int nfilters, const char *default_location)
{
SDL_PropertiesID props = SDL_CreateProperties();

SDL_SetPointerProperty(props, SDL_PROP_FILE_DIALOG_FILTERS_POINTER, (void *) filters);
SDL_SetNumberProperty(props, SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER, nfilters);
SDL_SetPointerProperty(props, SDL_PROP_FILE_DIALOG_WINDOW_POINTER, window);
SDL_SetStringProperty(props, SDL_PROP_FILE_DIALOG_LOCATION_STRING, default_location);

SDL_ShowFileDialogWithProperties(SDL_FILEDIALOG_SAVEFILE, callback, userdata, props);

SDL_DestroyProperties(props);
}

void SDL_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const char *default_location, bool allow_many)
{
SDL_PropertiesID props = SDL_CreateProperties();

SDL_SetPointerProperty(props, SDL_PROP_FILE_DIALOG_WINDOW_POINTER, window);
SDL_SetStringProperty(props, SDL_PROP_FILE_DIALOG_LOCATION_STRING, default_location);
SDL_SetBooleanProperty(props, SDL_PROP_FILE_DIALOG_MANY_BOOLEAN, allow_many);

SDL_ShowFileDialogWithProperties(SDL_FILEDIALOG_OPENFOLDER, callback, userdata, props);

SDL_DestroyProperties(props);
}
22 changes: 22 additions & 0 deletions src/dialog/SDL_dialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

void SDL_SYS_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props);
Loading

0 comments on commit a4852f3

Please sign in to comment.