SDL_dialog.h 14 KB

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