SDL 3.0
SDL_dialog.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/**
23 * # CategoryDialog
24 *
25 * File dialog support.
26 *
27 * SDL offers file dialogs, to let users select files with native GUI
28 * interfaces. There are "open" dialogs, "save" dialogs, and folder selection
29 * dialogs. The app can control some details, such as filtering to specific
30 * files, or whether multiple files can be selected by the user.
31 *
32 * Note that launching a file dialog is a non-blocking operation; control
33 * returns to the app immediately, and a callback is called later (possibly in
34 * another thread) when the user makes a choice.
35 */
36
37#ifndef SDL_dialog_h_
38#define SDL_dialog_h_
39
40#include <SDL3/SDL_stdinc.h>
41#include <SDL3/SDL_error.h>
42#include <SDL3/SDL_properties.h>
43#include <SDL3/SDL_video.h>
44
45#include <SDL3/SDL_begin_code.h>
46/* Set up for C function definitions, even when using C++ */
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51/**
52 * An entry for filters for file dialogs.
53 *
54 * `name` is a user-readable label for the filter (for example, "Office
55 * document").
56 *
57 * `pattern` is a semicolon-separated list of file extensions (for example,
58 * "doc;docx"). File extensions may only contain alphanumeric characters,
59 * hyphens, underscores and periods. Alternatively, the whole string can be a
60 * single asterisk ("*"), which serves as an "All files" filter.
61 *
62 * \since This struct is available since SDL 3.2.0.
63 *
64 * \sa SDL_DialogFileCallback
65 * \sa SDL_ShowOpenFileDialog
66 * \sa SDL_ShowSaveFileDialog
67 * \sa SDL_ShowOpenFolderDialog
68 * \sa SDL_ShowFileDialogWithProperties
69 */
71{
72 const char *name;
73 const char *pattern;
75
76/**
77 * Callback used by file dialog functions.
78 *
79 * The specific usage is described in each function.
80 *
81 * If `filelist` is:
82 *
83 * - NULL, an error occurred. Details can be obtained with SDL_GetError().
84 * - A pointer to NULL, the user either didn't choose any file or canceled the
85 * dialog.
86 * - A pointer to non-`NULL`, the user chose one or more files. The argument
87 * is a null-terminated array of pointers to UTF-8 encoded strings, each
88 * containing a path.
89 *
90 * The filelist argument should not be freed; it will automatically be freed
91 * when the callback returns.
92 *
93 * The filter argument is the index of the filter that was selected, or -1 if
94 * no filter was selected, the dialog was canceled, or if the platform or
95 * method doesn't support fetching the selected filter.
96 *
97 * In Android, the `filelist` are `content://` URIs. They should be opened
98 * using SDL_IOFromFile() with appropriate modes. This applies both to open
99 * and save file dialog.
100 *
101 * \param userdata an app-provided pointer, for the callback's use.
102 * \param filelist the file(s) chosen by the user.
103 * \param filter index of the selected filter.
104 *
105 * \since This datatype is available since SDL 3.2.0.
106 *
107 * \sa SDL_DialogFileFilter
108 * \sa SDL_ShowOpenFileDialog
109 * \sa SDL_ShowSaveFileDialog
110 * \sa SDL_ShowOpenFolderDialog
111 * \sa SDL_ShowFileDialogWithProperties
112 */
113typedef void (SDLCALL *SDL_DialogFileCallback)(void *userdata, const char * const *filelist, int filter);
114
115/**
116 * Displays a dialog that lets the user select a file on their filesystem.
117 *
118 * This is an asynchronous function; it will return immediately, and the
119 * result will be passed to the callback.
120 *
121 * The callback will be invoked with a null-terminated list of files the user
122 * chose. The list will be empty if the user canceled the dialog, and it will
123 * be NULL if an error occurred.
124 *
125 * Note that the callback may be called from a different thread than the one
126 * the function was invoked on.
127 *
128 * Depending on the platform, the user may be allowed to input paths that
129 * don't yet exist.
130 *
131 * On Linux, dialogs may require XDG Portals, which requires DBus, which
132 * requires an event-handling loop. Apps that do not use SDL to handle events
133 * should add a call to SDL_PumpEvents in their main loop.
134 *
135 * \param callback a function pointer to be invoked when the user selects a
136 * file and accepts, or cancels the dialog, or an error
137 * occurs.
138 * \param userdata an optional pointer to pass extra data to the callback when
139 * it will be invoked.
140 * \param window the window that the dialog should be modal for, may be NULL.
141 * Not all platforms support this option.
142 * \param filters a list of filters, may be NULL. Not all platforms support
143 * this option, and platforms that do support it may allow the
144 * user to ignore the filters. If non-NULL, it must remain
145 * valid at least until the callback is invoked.
146 * \param nfilters the number of filters. Ignored if filters is NULL.
147 * \param default_location the default folder or file to start the dialog at,
148 * may be NULL to use the platform default. A location
149 * ending in a path separator is a folder; otherwise
150 * the dialog starts in the folder before the last
151 * separator, with the rest as the default file name.
152 * Not all platforms support this option.
153 * \param allow_many if non-zero, the user will be allowed to select multiple
154 * entries. Not all platforms support this option.
155 *
156 * \threadsafety This function should be called only from the main thread. The
157 * callback may be invoked from the same thread or from a
158 * different one, depending on the OS's constraints.
159 *
160 * \since This function is available since SDL 3.2.0.
161 *
162 * \sa SDL_DialogFileCallback
163 * \sa SDL_DialogFileFilter
164 * \sa SDL_ShowSaveFileDialog
165 * \sa SDL_ShowOpenFolderDialog
166 * \sa SDL_ShowFileDialogWithProperties
167 */
168extern 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);
169
170/**
171 * Displays a dialog that lets the user choose a new or existing file on their
172 * filesystem.
173 *
174 * This is an asynchronous function; it will return immediately, and the
175 * result will be passed to the callback.
176 *
177 * The callback will be invoked with a null-terminated list of files the user
178 * chose. The list will be empty if the user canceled the dialog, and it will
179 * be NULL if an error occurred.
180 *
181 * Note that the callback may be called from a different thread than the one
182 * the function was invoked on.
183 *
184 * The chosen file may or may not already exist.
185 *
186 * On Linux, dialogs may require XDG Portals, which requires DBus, which
187 * requires an event-handling loop. Apps that do not use SDL to handle events
188 * should add a call to SDL_PumpEvents in their main loop.
189 *
190 * \param callback a function pointer to be invoked when the user selects a
191 * file and accepts, or cancels the dialog, or an error
192 * occurs.
193 * \param userdata an optional pointer to pass extra data to the callback when
194 * it will be invoked.
195 * \param window the window that the dialog should be modal for, may be NULL.
196 * Not all platforms support this option.
197 * \param filters a list of filters, may be NULL. Not all platforms support
198 * this option, and platforms that do support it may allow the
199 * user to ignore the filters. If non-NULL, it must remain
200 * valid at least until the callback is invoked.
201 * \param nfilters the number of filters. Ignored if filters is NULL.
202 * \param default_location the default folder or file to start the dialog at,
203 * may be NULL to use the platform default. A location
204 * ending in a path separator is a folder; otherwise
205 * the dialog starts in the folder before the last
206 * separator, with the rest as the default file name.
207 * Not all platforms support this option.
208 *
209 * \threadsafety This function should be called only from the main thread. The
210 * callback may be invoked from the same thread or from a
211 * different one, depending on the OS's constraints.
212 *
213 * \since This function is available since SDL 3.2.0.
214 *
215 * \sa SDL_DialogFileCallback
216 * \sa SDL_DialogFileFilter
217 * \sa SDL_ShowOpenFileDialog
218 * \sa SDL_ShowOpenFolderDialog
219 * \sa SDL_ShowFileDialogWithProperties
220 */
221extern SDL_DECLSPEC void SDLCALL SDL_ShowSaveFileDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const SDL_DialogFileFilter *filters, int nfilters, const char *default_location);
222
223/**
224 * Displays a dialog that lets the user select a folder on their filesystem.
225 *
226 * This is an asynchronous function; it will return immediately, and the
227 * result will be passed to the callback.
228 *
229 * The callback will be invoked with a null-terminated list of files the user
230 * chose. The list will be empty if the user canceled the dialog, and it will
231 * be NULL if an error occurred.
232 *
233 * Note that the callback may be called from a different thread than the one
234 * the function was invoked on.
235 *
236 * Depending on the platform, the user may be allowed to input paths that
237 * don't yet exist.
238 *
239 * On Linux, dialogs may require XDG Portals, which requires DBus, which
240 * requires an event-handling loop. Apps that do not use SDL to handle events
241 * should add a call to SDL_PumpEvents in their main loop.
242 *
243 * \param callback a function pointer to be invoked when the user selects a
244 * file and accepts, or cancels the dialog, or an error
245 * occurs.
246 * \param userdata an optional pointer to pass extra data to the callback when
247 * it will be invoked.
248 * \param window the window that the dialog should be modal for, may be NULL.
249 * Not all platforms support this option.
250 * \param default_location the default folder to start the dialog at, may be
251 * NULL to use the platform default. Not all platforms
252 * support this option.
253 * \param allow_many if non-zero, the user will be allowed to select multiple
254 * entries. Not all platforms support this option.
255 *
256 * \threadsafety This function should be called only from the main thread. The
257 * callback may be invoked from the same thread or from a
258 * different one, depending on the OS's constraints.
259 *
260 * \since This function is available since SDL 3.2.0.
261 *
262 * \sa SDL_DialogFileCallback
263 * \sa SDL_ShowOpenFileDialog
264 * \sa SDL_ShowSaveFileDialog
265 * \sa SDL_ShowFileDialogWithProperties
266 */
267extern SDL_DECLSPEC void SDLCALL SDL_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const char *default_location, bool allow_many);
268
269/**
270 * Various types of file dialogs.
271 *
272 * This is used by SDL_ShowFileDialogWithProperties() to decide what kind of
273 * dialog to present to the user.
274 *
275 * \since This enum is available since SDL 3.2.0.
276 *
277 * \sa SDL_ShowFileDialogWithProperties
278 */
285
286/**
287 * Create and launch a file dialog with the specified properties.
288 *
289 * These are the supported properties:
290 *
291 * - `SDL_PROP_FILE_DIALOG_FILTERS_POINTER`: a pointer to a list of
292 * SDL_DialogFileFilter structs, which will be used as filters for
293 * file-based selections. Ignored if the dialog is an "Open Folder" dialog.
294 * If non-NULL, the array of filters must remain valid at least until the
295 * callback is invoked.
296 * - `SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER`: the number of filters in the
297 * array of filters, if it exists.
298 * - `SDL_PROP_FILE_DIALOG_WINDOW_POINTER`: the window that the dialog should
299 * be modal for.
300 * - `SDL_PROP_FILE_DIALOG_LOCATION_STRING`: the default folder or file to
301 * start the dialog at. A location ending in a path separator is a folder;
302 * otherwise the dialog starts in the folder before the last separator, with
303 * the rest as the default file name. For SDL_FILEDIALOG_OPENFOLDER, the
304 * location is always a folder.
305 * - `SDL_PROP_FILE_DIALOG_MANY_BOOLEAN`: true to allow the user to select
306 * more than one entry.
307 * - `SDL_PROP_FILE_DIALOG_TITLE_STRING`: the title for the dialog.
308 * - `SDL_PROP_FILE_DIALOG_ACCEPT_STRING`: the label that the accept button
309 * should have.
310 * - `SDL_PROP_FILE_DIALOG_CANCEL_STRING`: the label that the cancel button
311 * should have.
312 *
313 * Note that each platform may or may not support any of the properties.
314 *
315 * \param type the type of file dialog.
316 * \param callback a function pointer to be invoked when the user selects a
317 * file and accepts, or cancels the dialog, or an error
318 * occurs.
319 * \param userdata an optional pointer to pass extra data to the callback when
320 * it will be invoked.
321 * \param props the properties to use.
322 *
323 * \threadsafety This function should be called only from the main thread. The
324 * callback may be invoked from the same thread or from a
325 * different one, depending on the OS's constraints.
326 *
327 * \since This function is available since SDL 3.2.0.
328 *
329 * \sa SDL_FileDialogType
330 * \sa SDL_DialogFileCallback
331 * \sa SDL_DialogFileFilter
332 * \sa SDL_ShowOpenFileDialog
333 * \sa SDL_ShowSaveFileDialog
334 * \sa SDL_ShowOpenFolderDialog
335 */
336extern SDL_DECLSPEC void SDLCALL SDL_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props);
337
338#define SDL_PROP_FILE_DIALOG_FILTERS_POINTER "SDL.filedialog.filters"
339#define SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER "SDL.filedialog.nfilters"
340#define SDL_PROP_FILE_DIALOG_WINDOW_POINTER "SDL.filedialog.window"
341#define SDL_PROP_FILE_DIALOG_LOCATION_STRING "SDL.filedialog.location"
342#define SDL_PROP_FILE_DIALOG_MANY_BOOLEAN "SDL.filedialog.many"
343#define SDL_PROP_FILE_DIALOG_TITLE_STRING "SDL.filedialog.title"
344#define SDL_PROP_FILE_DIALOG_ACCEPT_STRING "SDL.filedialog.accept"
345#define SDL_PROP_FILE_DIALOG_CANCEL_STRING "SDL.filedialog.cancel"
346
347/* Ends C function definitions when using C++ */
348#ifdef __cplusplus
349}
350#endif
351#include <SDL3/SDL_close_code.h>
352
353#endif /* SDL_dialog_h_ */
void SDL_ShowSaveFileDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const SDL_DialogFileFilter *filters, int nfilters, const char *default_location)
void(* SDL_DialogFileCallback)(void *userdata, const char *const *filelist, int filter)
Definition SDL_dialog.h:113
void SDL_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const char *default_location, bool allow_many)
SDL_FileDialogType
Definition SDL_dialog.h:280
@ SDL_FILEDIALOG_SAVEFILE
Definition SDL_dialog.h:282
@ SDL_FILEDIALOG_OPENFILE
Definition SDL_dialog.h:281
@ SDL_FILEDIALOG_OPENFOLDER
Definition SDL_dialog.h:283
void SDL_ShowOpenFileDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const SDL_DialogFileFilter *filters, int nfilters, const char *default_location, bool allow_many)
void SDL_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props)
Uint32 SDL_PropertiesID
struct SDL_Window SDL_Window
Definition SDL_video.h:210
static SDL_Window * window
Definition hello.c:16
const char * name
Definition SDL_dialog.h:72
const char * pattern
Definition SDL_dialog.h:73