SDL_dialog.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. #include "SDL_internal.h"
  19. #include "SDL_dialog.h"
  20. #include "SDL_dialog_utils.h"
  21. void SDL_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props)
  22. {
  23. if (!callback) {
  24. return;
  25. }
  26. #ifdef SDL_DIALOG_DISABLED
  27. SDL_SetError("SDL not built with dialog support");
  28. callback(userdata, NULL, -1);
  29. #else
  30. SDL_DialogFileFilter *filters = SDL_GetPointerProperty(props, SDL_PROP_FILE_DIALOG_FILTERS_POINTER, NULL);
  31. int nfilters = (int) SDL_GetNumberProperty(props, SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER, -1);
  32. if (filters && nfilters == -1) {
  33. SDL_SetError("Set filter pointers, but didn't set number of filters (SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER)");
  34. callback(userdata, NULL, -1);
  35. return;
  36. }
  37. const char *msg = validate_filters(filters, nfilters);
  38. if (msg) {
  39. SDL_SetError("Invalid dialog file filters: %s", msg);
  40. callback(userdata, NULL, -1);
  41. return;
  42. }
  43. switch (type) {
  44. case SDL_FILEDIALOG_OPENFILE:
  45. case SDL_FILEDIALOG_SAVEFILE:
  46. case SDL_FILEDIALOG_OPENFOLDER:
  47. SDL_SYS_ShowFileDialogWithProperties(type, callback, userdata, props);
  48. break;
  49. default:
  50. SDL_SetError("Unsupported file dialog type: %d", (int) type);
  51. callback(userdata, NULL, -1);
  52. break;
  53. };
  54. #endif
  55. }
  56. void SDL_ShowOpenFileDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const SDL_DialogFileFilter *filters, int nfilters, const char *default_location, bool allow_many)
  57. {
  58. #ifdef SDL_DIALOG_DISABLED
  59. if (!callback) {
  60. return;
  61. }
  62. SDL_SetError("SDL not built with dialog support");
  63. callback(userdata, NULL, -1);
  64. #else
  65. SDL_PropertiesID props = SDL_CreateProperties();
  66. SDL_SetPointerProperty(props, SDL_PROP_FILE_DIALOG_FILTERS_POINTER, (void *) filters);
  67. SDL_SetNumberProperty(props, SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER, nfilters);
  68. SDL_SetPointerProperty(props, SDL_PROP_FILE_DIALOG_WINDOW_POINTER, window);
  69. SDL_SetStringProperty(props, SDL_PROP_FILE_DIALOG_LOCATION_STRING, default_location);
  70. SDL_SetBooleanProperty(props, SDL_PROP_FILE_DIALOG_MANY_BOOLEAN, allow_many);
  71. SDL_ShowFileDialogWithProperties(SDL_FILEDIALOG_OPENFILE, callback, userdata, props);
  72. SDL_DestroyProperties(props);
  73. #endif
  74. }
  75. void SDL_ShowSaveFileDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const SDL_DialogFileFilter *filters, int nfilters, const char *default_location)
  76. {
  77. #ifdef SDL_DIALOG_DISABLED
  78. if (!callback) {
  79. return;
  80. }
  81. SDL_SetError("SDL not built with dialog support");
  82. callback(userdata, NULL, -1);
  83. #else
  84. SDL_PropertiesID props = SDL_CreateProperties();
  85. SDL_SetPointerProperty(props, SDL_PROP_FILE_DIALOG_FILTERS_POINTER, (void *) filters);
  86. SDL_SetNumberProperty(props, SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER, nfilters);
  87. SDL_SetPointerProperty(props, SDL_PROP_FILE_DIALOG_WINDOW_POINTER, window);
  88. SDL_SetStringProperty(props, SDL_PROP_FILE_DIALOG_LOCATION_STRING, default_location);
  89. SDL_ShowFileDialogWithProperties(SDL_FILEDIALOG_SAVEFILE, callback, userdata, props);
  90. SDL_DestroyProperties(props);
  91. #endif
  92. }
  93. void SDL_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const char *default_location, bool allow_many)
  94. {
  95. #ifdef SDL_DIALOG_DISABLED
  96. if (!callback) {
  97. return;
  98. }
  99. SDL_SetError("SDL not built with dialog support");
  100. callback(userdata, NULL, -1);
  101. #else
  102. SDL_PropertiesID props = SDL_CreateProperties();
  103. SDL_SetPointerProperty(props, SDL_PROP_FILE_DIALOG_WINDOW_POINTER, window);
  104. SDL_SetStringProperty(props, SDL_PROP_FILE_DIALOG_LOCATION_STRING, default_location);
  105. SDL_SetBooleanProperty(props, SDL_PROP_FILE_DIALOG_MANY_BOOLEAN, allow_many);
  106. SDL_ShowFileDialogWithProperties(SDL_FILEDIALOG_OPENFOLDER, callback, userdata, props);
  107. SDL_DestroyProperties(props);
  108. #endif
  109. }