SDL_dialog.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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_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 should not be freed; it will automatically be freed
  71. * 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 a function pointer to be invoked when the user selects a
  112. * file and accepts, or cancels the dialog, or an error
  113. * occurs.
  114. * \param userdata an optional pointer to pass extra data to the callback when
  115. * it will be invoked.
  116. * \param window the window that the dialog should be modal for, may be NULL.
  117. * Not all platforms support this option.
  118. * \param filters a list of SDL_DialogFileFilter's, may be NULL. Not all
  119. * platforms support this option, and platforms that do support
  120. * it may allow the user to ignore the filters.
  121. * \param nfilters the number of filters. Ignored if filters is NULL.
  122. * \param default_location the default folder or file to start the dialog at,
  123. * may be NULL. Not all platforms support this option.
  124. * \param allow_many if non-zero, the user will be allowed to select multiple
  125. * entries. Not all platforms support this option.
  126. *
  127. * \since This function is available since SDL 3.1.3.
  128. *
  129. * \sa SDL_DialogFileCallback
  130. * \sa SDL_DialogFileFilter
  131. * \sa SDL_ShowSaveFileDialog
  132. * \sa SDL_ShowOpenFolderDialog
  133. */
  134. 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);
  135. /**
  136. * Displays a dialog that lets the user choose a new or existing file on their
  137. * filesystem.
  138. *
  139. * This function should only be invoked from the main thread.
  140. *
  141. * This is an asynchronous function; it will return immediately, and the
  142. * result will be passed to the callback.
  143. *
  144. * The callback will be invoked with a null-terminated list of files the user
  145. * chose. The list will be empty if the user canceled the dialog, and it will
  146. * be NULL if an error occurred.
  147. *
  148. * Note that the callback may be called from a different thread than the one
  149. * the function was invoked on.
  150. *
  151. * The chosen file may or may not already exist.
  152. *
  153. * On Linux, dialogs may require XDG Portals, which requires DBus, which
  154. * requires an event-handling loop. Apps that do not use SDL to handle events
  155. * should add a call to SDL_PumpEvents in their main loop.
  156. *
  157. * \param callback a function pointer to be invoked when the user selects a
  158. * file and accepts, or cancels the dialog, or an error
  159. * occurs.
  160. * \param userdata an optional pointer to pass extra data to the callback when
  161. * it will be invoked.
  162. * \param window the window that the dialog should be modal for, may be NULL.
  163. * Not all platforms support this option.
  164. * \param filters a list of SDL_DialogFileFilter's, may be NULL. Not all
  165. * platforms support this option, and platforms that do support
  166. * it may allow the user to ignore the filters.
  167. * \param nfilters the number of filters. Ignored if filters is NULL.
  168. * \param default_location the default folder or file to start the dialog at,
  169. * may be NULL. Not all platforms support this option.
  170. *
  171. * \since This function is available since SDL 3.1.3.
  172. *
  173. * \sa SDL_DialogFileCallback
  174. * \sa SDL_DialogFileFilter
  175. * \sa SDL_ShowOpenFileDialog
  176. * \sa SDL_ShowOpenFolderDialog
  177. */
  178. 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);
  179. /**
  180. * Displays a dialog that lets the user select a folder on their filesystem.
  181. *
  182. * This function should only be invoked from the main thread.
  183. *
  184. * This is an asynchronous function; it will return immediately, and the
  185. * result will be passed to the callback.
  186. *
  187. * The callback will be invoked with a null-terminated list of files the user
  188. * chose. The list will be empty if the user canceled the dialog, and it will
  189. * be NULL if an error occurred.
  190. *
  191. * Note that the callback may be called from a different thread than the one
  192. * the function was invoked on.
  193. *
  194. * Depending on the platform, the user may be allowed to input paths that
  195. * don't yet exist.
  196. *
  197. * On Linux, dialogs may require XDG Portals, which requires DBus, which
  198. * requires an event-handling loop. Apps that do not use SDL to handle events
  199. * should add a call to SDL_PumpEvents in their main loop.
  200. *
  201. * \param callback a function pointer to be invoked when the user selects a
  202. * file and accepts, or cancels the dialog, or an error
  203. * occurs.
  204. * \param userdata an optional pointer to pass extra data to the callback when
  205. * it will be invoked.
  206. * \param window the window that the dialog should be modal for, may be NULL.
  207. * Not all platforms support this option.
  208. * \param default_location the default folder or file to start the dialog at,
  209. * may be NULL. Not all platforms support this option.
  210. * \param allow_many if non-zero, the user will be allowed to select multiple
  211. * entries. Not all platforms support this option.
  212. *
  213. * \since This function is available since SDL 3.1.3.
  214. *
  215. * \sa SDL_DialogFileCallback
  216. * \sa SDL_ShowOpenFileDialog
  217. * \sa SDL_ShowSaveFileDialog
  218. */
  219. extern SDL_DECLSPEC void SDLCALL SDL_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const char *default_location, bool allow_many);
  220. /* Ends C function definitions when using C++ */
  221. #ifdef __cplusplus
  222. }
  223. #endif
  224. #include <SDL3/SDL_close_code.h>
  225. #endif /* SDL_dialog_h_ */