SDL_dialog.h 14 KB

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