SDL_dialog.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  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. Not all platforms support
  133. * this option, and platforms that do support it may allow the
  134. * user to ignore the filters. If non-NULL, it must remain
  135. * valid at least until the callback is invoked.
  136. * \param nfilters the number of filters. Ignored if filters is NULL.
  137. * \param default_location the default folder or file to start the dialog at,
  138. * may be NULL. Not all platforms support this option.
  139. * \param allow_many if non-zero, the user will be allowed to select multiple
  140. * entries. Not all platforms support this option.
  141. *
  142. * \threadsafety This function should be called only from the main thread. The
  143. * callback may be invoked from the same thread or from a
  144. * different one, depending on the OS's constraints.
  145. *
  146. * \since This function is available since SDL 3.2.0.
  147. *
  148. * \sa SDL_DialogFileCallback
  149. * \sa SDL_DialogFileFilter
  150. * \sa SDL_ShowSaveFileDialog
  151. * \sa SDL_ShowOpenFolderDialog
  152. * \sa SDL_ShowFileDialogWithProperties
  153. */
  154. 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);
  155. /**
  156. * Displays a dialog that lets the user choose a new or existing file on their
  157. * filesystem.
  158. *
  159. * This is an asynchronous function; it will return immediately, and the
  160. * result will be passed to the callback.
  161. *
  162. * The callback will be invoked with a null-terminated list of files the user
  163. * chose. The list will be empty if the user canceled the dialog, and it will
  164. * be NULL if an error occurred.
  165. *
  166. * Note that the callback may be called from a different thread than the one
  167. * the function was invoked on.
  168. *
  169. * The chosen file may or may not already exist.
  170. *
  171. * On Linux, dialogs may require XDG Portals, which requires DBus, which
  172. * requires an event-handling loop. Apps that do not use SDL to handle events
  173. * should add a call to SDL_PumpEvents in their main loop.
  174. *
  175. * \param callback a function pointer to be invoked when the user selects a
  176. * file and accepts, or cancels the dialog, or an error
  177. * occurs.
  178. * \param userdata an optional pointer to pass extra data to the callback when
  179. * it will be invoked.
  180. * \param window the window that the dialog should be modal for, may be NULL.
  181. * Not all platforms support this option.
  182. * \param filters a list of filters, may be NULL. Not all platforms support
  183. * this option, and platforms that do support it may allow the
  184. * user to ignore the filters. If non-NULL, it must remain
  185. * valid at least until the callback is invoked.
  186. * \param nfilters the number of filters. Ignored if filters is NULL.
  187. * \param default_location the default folder or file to start the dialog at,
  188. * may be NULL. Not all platforms support this option.
  189. *
  190. * \threadsafety This function should be called only from the main thread. The
  191. * callback may be invoked from the same thread or from a
  192. * different one, depending on the OS's constraints.
  193. *
  194. * \since This function is available since SDL 3.2.0.
  195. *
  196. * \sa SDL_DialogFileCallback
  197. * \sa SDL_DialogFileFilter
  198. * \sa SDL_ShowOpenFileDialog
  199. * \sa SDL_ShowOpenFolderDialog
  200. * \sa SDL_ShowFileDialogWithProperties
  201. */
  202. 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);
  203. /**
  204. * Displays a dialog that lets the user select a folder on their filesystem.
  205. *
  206. * This is an asynchronous function; it will return immediately, and the
  207. * result will be passed to the callback.
  208. *
  209. * The callback will be invoked with a null-terminated list of files the user
  210. * chose. The list will be empty if the user canceled the dialog, and it will
  211. * be NULL if an error occurred.
  212. *
  213. * Note that the callback may be called from a different thread than the one
  214. * the function was invoked on.
  215. *
  216. * Depending on the platform, the user may be allowed to input paths that
  217. * don't yet exist.
  218. *
  219. * On Linux, dialogs may require XDG Portals, which requires DBus, which
  220. * requires an event-handling loop. Apps that do not use SDL to handle events
  221. * should add a call to SDL_PumpEvents in their main loop.
  222. *
  223. * \param callback a function pointer to be invoked when the user selects a
  224. * file and accepts, or cancels the dialog, or an error
  225. * occurs.
  226. * \param userdata an optional pointer to pass extra data to the callback when
  227. * it will be invoked.
  228. * \param window the window that the dialog should be modal for, may be NULL.
  229. * Not all platforms support this option.
  230. * \param default_location the default folder or file to start the dialog at,
  231. * may be NULL. Not all platforms support this option.
  232. * \param allow_many if non-zero, the user will be allowed to select multiple
  233. * entries. Not all platforms support this option.
  234. *
  235. * \threadsafety This function should be called only from the main thread. The
  236. * callback may be invoked from the same thread or from a
  237. * different one, depending on the OS's constraints.
  238. *
  239. * \since This function is available since SDL 3.2.0.
  240. *
  241. * \sa SDL_DialogFileCallback
  242. * \sa SDL_ShowOpenFileDialog
  243. * \sa SDL_ShowSaveFileDialog
  244. * \sa SDL_ShowFileDialogWithProperties
  245. */
  246. extern SDL_DECLSPEC void SDLCALL SDL_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const char *default_location, bool allow_many);
  247. /**
  248. * Various types of file dialogs.
  249. *
  250. * This is used by SDL_ShowFileDialogWithProperties() to decide what kind of
  251. * dialog to present to the user.
  252. *
  253. * \since This enum is available since SDL 3.2.0.
  254. *
  255. * \sa SDL_ShowFileDialogWithProperties
  256. */
  257. typedef enum SDL_FileDialogType
  258. {
  259. SDL_FILEDIALOG_OPENFILE,
  260. SDL_FILEDIALOG_SAVEFILE,
  261. SDL_FILEDIALOG_OPENFOLDER
  262. } SDL_FileDialogType;
  263. /**
  264. * Create and launch a file dialog with the specified properties.
  265. *
  266. * These are the supported properties:
  267. *
  268. * - `SDL_PROP_FILE_DIALOG_FILTERS_POINTER`: a pointer to a list of
  269. * SDL_DialogFileFilter structs, which will be used as filters for
  270. * file-based selections. Ignored if the dialog is an "Open Folder" dialog.
  271. * If non-NULL, the array of filters must remain valid at least until the
  272. * callback is invoked.
  273. * - `SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER`: the number of filters in the
  274. * array of filters, if it exists.
  275. * - `SDL_PROP_FILE_DIALOG_WINDOW_POINTER`: the window that the dialog should
  276. * be modal for.
  277. * - `SDL_PROP_FILE_DIALOG_LOCATION_STRING`: the default folder or file to
  278. * start the dialog at.
  279. * - `SDL_PROP_FILE_DIALOG_MANY_BOOLEAN`: true to allow the user to select
  280. * more than one entry.
  281. * - `SDL_PROP_FILE_DIALOG_TITLE_STRING`: the title for the dialog.
  282. * - `SDL_PROP_FILE_DIALOG_ACCEPT_STRING`: the label that the accept button
  283. * should have.
  284. * - `SDL_PROP_FILE_DIALOG_CANCEL_STRING`: the label that the cancel button
  285. * should have.
  286. *
  287. * Note that each platform may or may not support any of the properties.
  288. *
  289. * \param type the type of file dialog.
  290. * \param callback a function pointer to be invoked when the user selects a
  291. * file and accepts, or cancels the dialog, or an error
  292. * occurs.
  293. * \param userdata an optional pointer to pass extra data to the callback when
  294. * it will be invoked.
  295. * \param props the properties to use.
  296. *
  297. * \threadsafety This function should be called only from the main thread. The
  298. * callback may be invoked from the same thread or from a
  299. * different one, depending on the OS's constraints.
  300. *
  301. * \since This function is available since SDL 3.2.0.
  302. *
  303. * \sa SDL_FileDialogType
  304. * \sa SDL_DialogFileCallback
  305. * \sa SDL_DialogFileFilter
  306. * \sa SDL_ShowOpenFileDialog
  307. * \sa SDL_ShowSaveFileDialog
  308. * \sa SDL_ShowOpenFolderDialog
  309. */
  310. extern SDL_DECLSPEC void SDLCALL SDL_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props);
  311. #define SDL_PROP_FILE_DIALOG_FILTERS_POINTER "SDL.filedialog.filters"
  312. #define SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER "SDL.filedialog.nfilters"
  313. #define SDL_PROP_FILE_DIALOG_WINDOW_POINTER "SDL.filedialog.window"
  314. #define SDL_PROP_FILE_DIALOG_LOCATION_STRING "SDL.filedialog.location"
  315. #define SDL_PROP_FILE_DIALOG_MANY_BOOLEAN "SDL.filedialog.many"
  316. #define SDL_PROP_FILE_DIALOG_TITLE_STRING "SDL.filedialog.title"
  317. #define SDL_PROP_FILE_DIALOG_ACCEPT_STRING "SDL.filedialog.accept"
  318. #define SDL_PROP_FILE_DIALOG_CANCEL_STRING "SDL.filedialog.cancel"
  319. /* Ends C function definitions when using C++ */
  320. #ifdef __cplusplus
  321. }
  322. #endif
  323. #include <SDL3/SDL_close_code.h>
  324. #endif /* SDL_dialog_h_ */