SDL_dialog.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #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_video.h>
  28. #include <SDL3/SDL_begin_code.h>
  29. /* Set up for C function definitions, even when using C++ */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /**
  34. * An entry for filters for file dialogs.
  35. *
  36. * `name` is a user-readable label for the filter (for example, "Office
  37. * document").
  38. *
  39. * `pattern` is a semicolon-separated list of file extensions (for example,
  40. * "doc;docx"). File extensions may only contain alphanumeric characters,
  41. * hyphens, underscores and periods. Alternatively, the whole string can be a
  42. * single asterisk ("*"), which serves as an "All files" filter.
  43. *
  44. * \since This struct is available since SDL 3.1.3.
  45. *
  46. * \sa SDL_DialogFileCallback
  47. * \sa SDL_ShowOpenFileDialog
  48. * \sa SDL_ShowSaveFileDialog
  49. * \sa SDL_ShowOpenFolderDialog
  50. */
  51. typedef struct SDL_DialogFileFilter
  52. {
  53. const char *name;
  54. const char *pattern;
  55. } SDL_DialogFileFilter;
  56. /**
  57. * Callback used by file dialog functions.
  58. *
  59. * The specific usage is described in each function.
  60. *
  61. * If `filelist` is:
  62. *
  63. * - NULL, an error occurred. Details can be obtained with SDL_GetError().
  64. * - A pointer to NULL, the user either didn't choose any file or canceled the
  65. * dialog.
  66. * - A pointer to non-`NULL`, the user chose one or more files. The argument
  67. * is a null-terminated list of pointers to C strings, each containing a
  68. * path.
  69. *
  70. * The filelist argument does not need to be freed; it will automatically be
  71. * freed when the callback returns.
  72. *
  73. * The filter argument is the index of the filter that was selected, or -1 if
  74. * no filter was selected or if the platform or method doesn't support
  75. * fetching the selected filter.
  76. *
  77. * \param userdata an app-provided pointer, for the callback's use.
  78. * \param filelist the file(s) chosen by the user.
  79. * \param filter index of the selected filter.
  80. *
  81. * \since This datatype is available since SDL 3.1.3.
  82. *
  83. * \sa SDL_DialogFileFilter
  84. * \sa SDL_ShowOpenFileDialog
  85. * \sa SDL_ShowSaveFileDialog
  86. * \sa SDL_ShowOpenFolderDialog
  87. */
  88. typedef void (SDLCALL *SDL_DialogFileCallback)(void *userdata, const char * const *filelist, int filter);
  89. /**
  90. * Displays a dialog that lets the user select a file on their filesystem.
  91. *
  92. * This function should only be invoked from the main thread.
  93. *
  94. * This is an asynchronous function; it will return immediately, and the
  95. * result will be passed to the callback.
  96. *
  97. * The callback will be invoked with a null-terminated list of files the user
  98. * chose. The list will be empty if the user canceled the dialog, and it will
  99. * be NULL if an error occurred.
  100. *
  101. * Note that the callback may be called from a different thread than the one
  102. * the function was invoked on.
  103. *
  104. * Depending on the platform, the user may be allowed to input paths that
  105. * don't yet exist.
  106. *
  107. * On Linux, dialogs may require XDG Portals, which requires DBus, which
  108. * requires an event-handling loop. Apps that do not use SDL to handle events
  109. * should add a call to SDL_PumpEvents in their main loop.
  110. *
  111. * \param callback an SDL_DialogFileCallback to be invoked when the user
  112. * selects a file and accepts, or cancels the dialog, or an
  113. * error occurs. The first argument is a null-terminated list
  114. * of C strings, representing the paths chosen by the user.
  115. * The list will be empty if the user canceled the dialog, and
  116. * it will be NULL if an error occurred. If an error occurred,
  117. * it can be fetched with SDL_GetError(). The second argument
  118. * is the userdata pointer passed to the function. The third
  119. * argument is the index of the filter selected by the user,
  120. * or one past the index of the last filter (therefore the
  121. * index of the terminating NULL filter) if no filter was
  122. * chosen, or -1 if the platform does not support detecting
  123. * the selected filter.
  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 SDL_DialogFileFilter's, may be NULL. Not all
  129. * platforms support this option, and platforms that do support
  130. * it may allow the user to ignore the filters.
  131. * \param nfilters the number of filters. Ignored if filters is NULL.
  132. * \param default_location the default folder or file to start the dialog at,
  133. * may be NULL. Not all platforms support this option.
  134. * \param allow_many if non-zero, the user will be allowed to select multiple
  135. * entries. Not all platforms support this option.
  136. *
  137. * \since This function is available since SDL 3.1.3.
  138. *
  139. * \sa SDL_DialogFileCallback
  140. * \sa SDL_DialogFileFilter
  141. * \sa SDL_ShowSaveFileDialog
  142. * \sa SDL_ShowOpenFolderDialog
  143. */
  144. 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);
  145. /**
  146. * Displays a dialog that lets the user choose a new or existing file on their
  147. * filesystem.
  148. *
  149. * This function should only be invoked from the main thread.
  150. *
  151. * This is an asynchronous function; it will return immediately, and the
  152. * result will be passed to the callback.
  153. *
  154. * The callback will be invoked with a null-terminated list of files the user
  155. * chose. The list will be empty if the user canceled the dialog, and it will
  156. * be NULL if an error occurred.
  157. *
  158. * Note that the callback may be called from a different thread than the one
  159. * the function was invoked on.
  160. *
  161. * The chosen file may or may not already exist.
  162. *
  163. * On Linux, dialogs may require XDG Portals, which requires DBus, which
  164. * requires an event-handling loop. Apps that do not use SDL to handle events
  165. * should add a call to SDL_PumpEvents in their main loop.
  166. *
  167. * \param callback an SDL_DialogFileCallback to be invoked when the user
  168. * selects a file and accepts, or cancels the dialog, or an
  169. * error occurs. The first argument is a null-terminated list
  170. * of C strings, representing the paths chosen by the user.
  171. * The list will be empty if the user canceled the dialog, and
  172. * it will be NULL if an error occurred. If an error occurred,
  173. * it can be fetched with SDL_GetError(). The second argument
  174. * is the userdata pointer passed to the function. The third
  175. * argument is the index of the filter selected by the user,
  176. * or one past the index of the last filter (therefore the
  177. * index of the terminating NULL filter) if no filter was
  178. * chosen, or -1 if the platform does not support detecting
  179. * the selected filter.
  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 SDL_DialogFileFilter's, may be NULL. Not all
  185. * platforms support this option, and platforms that do support
  186. * it may allow the user to ignore the filters.
  187. * \param nfilters the number of filters. Ignored if filters is NULL.
  188. * \param default_location the default folder or file to start the dialog at,
  189. * may be NULL. Not all platforms support this option.
  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. */
  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 function should only be invoked from the main thread.
  203. *
  204. * This is an asynchronous function; it will return immediately, and the
  205. * result will be passed to the callback.
  206. *
  207. * The callback will be invoked with a null-terminated list of files the user
  208. * chose. The list will be empty if the user canceled the dialog, and it will
  209. * be NULL if an error occurred.
  210. *
  211. * Note that the callback may be called from a different thread than the one
  212. * the function was invoked on.
  213. *
  214. * Depending on the platform, the user may be allowed to input paths that
  215. * don't yet exist.
  216. *
  217. * On Linux, dialogs may require XDG Portals, which requires DBus, which
  218. * requires an event-handling loop. Apps that do not use SDL to handle events
  219. * should add a call to SDL_PumpEvents in their main loop.
  220. *
  221. * \param callback an SDL_DialogFileCallback to be invoked when the user
  222. * selects a file and accepts, or cancels the dialog, or an
  223. * error occurs. The first argument is a null-terminated list
  224. * of C strings, representing the paths chosen by the user.
  225. * The list will be empty if the user canceled the dialog, and
  226. * it will be NULL if an error occurred. If an error occurred,
  227. * it can be fetched with SDL_GetError(). The second argument
  228. * is the userdata pointer passed to the function. The third
  229. * argument is always -1 for SDL_ShowOpenFolderDialog.
  230. * \param userdata an optional pointer to pass extra data to the callback when
  231. * it will be invoked.
  232. * \param window the window that the dialog should be modal for, may be NULL.
  233. * Not all platforms support this option.
  234. * \param default_location the default folder or file to start the dialog at,
  235. * may be NULL. Not all platforms support this option.
  236. * \param allow_many if non-zero, the user will be allowed to select multiple
  237. * entries. Not all platforms support this option.
  238. *
  239. * \since This function is available since SDL 3.1.3.
  240. *
  241. * \sa SDL_DialogFileCallback
  242. * \sa SDL_ShowOpenFileDialog
  243. * \sa SDL_ShowSaveFileDialog
  244. */
  245. extern SDL_DECLSPEC void SDLCALL SDL_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const char *default_location, bool allow_many);
  246. /* Ends C function definitions when using C++ */
  247. #ifdef __cplusplus
  248. }
  249. #endif
  250. #include <SDL3/SDL_close_code.h>
  251. #endif /* SDL_dialog_h_ */