SDL_dialog.h 14 KB

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