SDL_dialog.h 13 KB

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