SDL 3.0
SDL_dialog.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 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
28#ifndef SDL_dialog_h_
29#define SDL_dialog_h_
30
31#include <SDL3/SDL_stdinc.h>
32#include <SDL3/SDL_error.h>
33#include <SDL3/SDL_video.h>
34
35#include <SDL3/SDL_begin_code.h>
36/* Set up for C function definitions, even when using C++ */
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41/**
42 * An entry for filters for file dialogs.
43 *
44 * `name` is a user-readable label for the filter (for example, "Office
45 * document").
46 *
47 * `pattern` is a semicolon-separated list of file extensions (for example,
48 * "doc;docx"). File extensions may only contain alphanumeric characters,
49 * hyphens, underscores and periods. Alternatively, the whole string can be a
50 * single asterisk ("*"), which serves as an "All files" filter.
51 *
52 * \since This struct is available since SDL 3.1.3.
53 *
54 * \sa SDL_DialogFileCallback
55 * \sa SDL_ShowOpenFileDialog
56 * \sa SDL_ShowSaveFileDialog
57 * \sa SDL_ShowOpenFolderDialog
58 */
60{
61 const char *name;
62 const char *pattern;
64
65/**
66 * Callback used by file dialog functions.
67 *
68 * The specific usage is described in each function.
69 *
70 * If `filelist` is:
71 *
72 * - NULL, an error occurred. Details can be obtained with SDL_GetError().
73 * - A pointer to NULL, the user either didn't choose any file or canceled the
74 * dialog.
75 * - A pointer to non-`NULL`, the user chose one or more files. The argument
76 * is a null-terminated list of pointers to C strings, each containing a
77 * path.
78 *
79 * The filelist argument does not need to be freed; it will automatically be
80 * freed when the callback returns.
81 *
82 * The filter argument is the index of the filter that was selected, or -1 if
83 * no filter was selected or if the platform or method doesn't support
84 * fetching the selected filter.
85 *
86 * \param userdata an app-provided pointer, for the callback's use.
87 * \param filelist the file(s) chosen by the user.
88 * \param filter index of the selected filter.
89 *
90 * \since This datatype is available since SDL 3.1.3.
91 *
92 * \sa SDL_DialogFileFilter
93 * \sa SDL_ShowOpenFileDialog
94 * \sa SDL_ShowSaveFileDialog
95 * \sa SDL_ShowOpenFolderDialog
96 */
97typedef void (SDLCALL *SDL_DialogFileCallback)(void *userdata, const char * const *filelist, int filter);
98
99/**
100 * Displays a dialog that lets the user select a file on their filesystem.
101 *
102 * This function should only be invoked from the main thread.
103 *
104 * This is an asynchronous function; it will return immediately, and the
105 * result will be passed to the callback.
106 *
107 * The callback will be invoked with a null-terminated list of files the user
108 * chose. The list will be empty if the user canceled the dialog, and it will
109 * be NULL if an error occurred.
110 *
111 * Note that the callback may be called from a different thread than the one
112 * the function was invoked on.
113 *
114 * Depending on the platform, the user may be allowed to input paths that
115 * don't yet exist.
116 *
117 * On Linux, dialogs may require XDG Portals, which requires DBus, which
118 * requires an event-handling loop. Apps that do not use SDL to handle events
119 * should add a call to SDL_PumpEvents in their main loop.
120 *
121 * \param callback an SDL_DialogFileCallback to be invoked when the user
122 * selects a file and accepts, or cancels the dialog, or an
123 * error occurs. The first argument is a null-terminated list
124 * of C strings, representing the paths chosen by the user.
125 * The list will be empty if the user canceled the dialog, and
126 * it will be NULL if an error occurred. If an error occurred,
127 * it can be fetched with SDL_GetError(). The second argument
128 * is the userdata pointer passed to the function. The third
129 * argument is the index of the filter selected by the user,
130 * or one past the index of the last filter (therefore the
131 * index of the terminating NULL filter) if no filter was
132 * chosen, or -1 if the platform does not support detecting
133 * the selected filter.
134 * \param userdata an optional pointer to pass extra data to the callback when
135 * it will be invoked.
136 * \param window the window that the dialog should be modal for, may be NULL.
137 * Not all platforms support this option.
138 * \param filters a list of SDL_DialogFileFilter's, may be NULL. Not all
139 * platforms support this option, and platforms that do support
140 * it may allow the user to ignore the filters.
141 * \param nfilters the number of filters. Ignored if filters is NULL.
142 * \param default_location the default folder or file to start the dialog at,
143 * may be NULL. Not all platforms support this option.
144 * \param allow_many if non-zero, the user will be allowed to select multiple
145 * entries. Not all platforms support this option.
146 *
147 * \since This function is available since SDL 3.1.3.
148 *
149 * \sa SDL_DialogFileCallback
150 * \sa SDL_DialogFileFilter
151 * \sa SDL_ShowSaveFileDialog
152 * \sa SDL_ShowOpenFolderDialog
153 */
154extern 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);
155
156/**
157 * Displays a dialog that lets the user choose a new or existing file on their
158 * filesystem.
159 *
160 * This function should only be invoked from the main thread.
161 *
162 * This is an asynchronous function; it will return immediately, and the
163 * result will be passed to the callback.
164 *
165 * The callback will be invoked with a null-terminated list of files the user
166 * chose. The list will be empty if the user canceled the dialog, and it will
167 * be NULL if an error occurred.
168 *
169 * Note that the callback may be called from a different thread than the one
170 * the function was invoked on.
171 *
172 * The chosen file may or may not already exist.
173 *
174 * On Linux, dialogs may require XDG Portals, which requires DBus, which
175 * requires an event-handling loop. Apps that do not use SDL to handle events
176 * should add a call to SDL_PumpEvents in their main loop.
177 *
178 * \param callback an SDL_DialogFileCallback to be invoked when the user
179 * selects a file and accepts, or cancels the dialog, or an
180 * error occurs. The first argument is a null-terminated list
181 * of C strings, representing the paths chosen by the user.
182 * The list will be empty if the user canceled the dialog, and
183 * it will be NULL if an error occurred. If an error occurred,
184 * it can be fetched with SDL_GetError(). The second argument
185 * is the userdata pointer passed to the function. The third
186 * argument is the index of the filter selected by the user,
187 * or one past the index of the last filter (therefore the
188 * index of the terminating NULL filter) if no filter was
189 * chosen, or -1 if the platform does not support detecting
190 * the selected filter.
191 * \param userdata an optional pointer to pass extra data to the callback when
192 * it will be invoked.
193 * \param window the window that the dialog should be modal for, may be NULL.
194 * Not all platforms support this option.
195 * \param filters a list of SDL_DialogFileFilter's, may be NULL. Not all
196 * platforms support this option, and platforms that do support
197 * it may allow the user to ignore the filters.
198 * \param nfilters the number of filters. Ignored if filters is NULL.
199 * \param default_location the default folder or file to start the dialog at,
200 * may be NULL. Not all platforms support this option.
201 *
202 * \since This function is available since SDL 3.1.3.
203 *
204 * \sa SDL_DialogFileCallback
205 * \sa SDL_DialogFileFilter
206 * \sa SDL_ShowOpenFileDialog
207 * \sa SDL_ShowOpenFolderDialog
208 */
209extern SDL_DECLSPEC void SDLCALL SDL_ShowSaveFileDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const SDL_DialogFileFilter *filters, int nfilters, const char *default_location);
210
211/**
212 * Displays a dialog that lets the user select a folder on their filesystem.
213 *
214 * This function should only be invoked from the main thread.
215 *
216 * This is an asynchronous function; it will return immediately, and the
217 * result will be passed to the callback.
218 *
219 * The callback will be invoked with a null-terminated list of files the user
220 * chose. The list will be empty if the user canceled the dialog, and it will
221 * be NULL if an error occurred.
222 *
223 * Note that the callback may be called from a different thread than the one
224 * the function was invoked on.
225 *
226 * Depending on the platform, the user may be allowed to input paths that
227 * don't yet exist.
228 *
229 * On Linux, dialogs may require XDG Portals, which requires DBus, which
230 * requires an event-handling loop. Apps that do not use SDL to handle events
231 * should add a call to SDL_PumpEvents in their main loop.
232 *
233 * \param callback an SDL_DialogFileCallback to be invoked when the user
234 * selects a file and accepts, or cancels the dialog, or an
235 * error occurs. The first argument is a null-terminated list
236 * of C strings, representing the paths chosen by the user.
237 * The list will be empty if the user canceled the dialog, and
238 * it will be NULL if an error occurred. If an error occurred,
239 * it can be fetched with SDL_GetError(). The second argument
240 * is the userdata pointer passed to the function. The third
241 * argument is always -1 for SDL_ShowOpenFolderDialog.
242 * \param userdata an optional pointer to pass extra data to the callback when
243 * it will be invoked.
244 * \param window the window that the dialog should be modal for, may be NULL.
245 * Not all platforms support this option.
246 * \param default_location the default folder or file to start the dialog at,
247 * may be NULL. Not all platforms support this option.
248 * \param allow_many if non-zero, the user will be allowed to select multiple
249 * entries. Not all platforms support this option.
250 *
251 * \since This function is available since SDL 3.1.3.
252 *
253 * \sa SDL_DialogFileCallback
254 * \sa SDL_ShowOpenFileDialog
255 * \sa SDL_ShowSaveFileDialog
256 */
257extern SDL_DECLSPEC void SDLCALL SDL_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const char *default_location, bool allow_many);
258
259/* Ends C function definitions when using C++ */
260#ifdef __cplusplus
261}
262#endif
263#include <SDL3/SDL_close_code.h>
264
265#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:97
void SDL_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const char *default_location, bool allow_many)
void SDL_ShowOpenFileDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const SDL_DialogFileFilter *filters, int nfilters, const char *default_location, bool allow_many)
struct SDL_Window SDL_Window
Definition SDL_video.h:165
const char * name
Definition SDL_dialog.h:61
const char * pattern
Definition SDL_dialog.h:62