1
0

testdialog.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Sample program: Create open and save dialogs. */
  11. #include <SDL3/SDL.h>
  12. #include <SDL3/SDL_main.h>
  13. #include <SDL3/SDL_test.h>
  14. const SDL_DialogFileFilter filters[3] = {
  15. { "All files", "*" },
  16. { "JPG images", "jpg;jpeg" },
  17. { "PNG images", "png" }
  18. };
  19. static void SDLCALL callback(void *userdata, const char * const *files, int filter) {
  20. if (files) {
  21. const char* filter_name = "(filter fetching unsupported)";
  22. if (filter != -1) {
  23. if (filter < sizeof(filters) / sizeof(*filters)) {
  24. filter_name = filters[filter].name;
  25. } else {
  26. filter_name = "(No filter was selected)";
  27. }
  28. }
  29. SDL_Log("Filter used: '%s'", filter_name);
  30. while (*files) {
  31. SDL_Log("'%s'", *files);
  32. files++;
  33. }
  34. } else {
  35. SDL_Log("Error: %s", SDL_GetError());
  36. }
  37. }
  38. int main(int argc, char *argv[])
  39. {
  40. SDL_Window *w;
  41. SDL_Renderer *r;
  42. SDLTest_CommonState *state;
  43. const SDL_FRect open_file_rect = { 50, 50, 220, 140 };
  44. const SDL_FRect save_file_rect = { 50, 290, 220, 140 };
  45. const SDL_FRect open_folder_rect = { 370, 50, 220, 140 };
  46. int i;
  47. const char *initial_path = NULL;
  48. const int nfilters = sizeof(filters) / sizeof(*filters);
  49. /* Initialize test framework */
  50. state = SDLTest_CommonCreateState(argv, 0);
  51. if (state == NULL) {
  52. return 1;
  53. }
  54. /* Parse commandline */
  55. for (i = 1; i < argc;) {
  56. int consumed;
  57. consumed = SDLTest_CommonArg(state, i);
  58. if (consumed <= 0) {
  59. static const char *options[] = { NULL };
  60. SDLTest_CommonLogUsage(state, argv[0], options);
  61. return 1;
  62. }
  63. i += consumed;
  64. }
  65. if (!SDL_Init(SDL_INIT_VIDEO)) {
  66. SDL_Log("SDL_Init failed (%s)", SDL_GetError());
  67. return 1;
  68. }
  69. if (!SDL_CreateWindowAndRenderer("testdialog", 640, 480, 0, &w, &r)) {
  70. SDL_Log("Failed to create window and/or renderer: %s", SDL_GetError());
  71. SDL_Quit();
  72. return 1;
  73. }
  74. initial_path = SDL_GetUserFolder(SDL_FOLDER_HOME);
  75. if (!initial_path) {
  76. SDL_Log("Will not use an initial path, couldn't get the home directory path: %s", SDL_GetError());
  77. }
  78. while (1) {
  79. int quit = 0;
  80. SDL_Event e;
  81. while (SDL_PollEvent(&e)) {
  82. if (e.type == SDL_EVENT_QUIT) {
  83. quit = 1;
  84. break;
  85. } else if (e.type == SDL_EVENT_MOUSE_BUTTON_UP) {
  86. const SDL_FPoint p = { e.button.x, e.button.y };
  87. /*
  88. * Arguments, in order:
  89. * - A function to call when files are chosen (or dialog is canceled, or error happens)
  90. * - A user-managed void pointer to pass to the function when it will be invoked
  91. * - The window to bind the dialog to, or NULL if none
  92. * - A list of filters for the files, see SDL_DialogFileFilter above (not for SDL_ShowOpenFolderDialog)
  93. * - The path where the dialog should start. May be a folder or a file
  94. * - Nonzero if the user is allowed to choose multiple entries (not for SDL_ShowSaveFileDialog)
  95. */
  96. if (SDL_PointInRectFloat(&p, &open_file_rect)) {
  97. SDL_ShowOpenFileDialog(callback, NULL, w, filters, nfilters, initial_path, 1);
  98. } else if (SDL_PointInRectFloat(&p, &open_folder_rect)) {
  99. SDL_ShowOpenFolderDialog(callback, NULL, w, initial_path, 1);
  100. } else if (SDL_PointInRectFloat(&p, &save_file_rect)) {
  101. SDL_ShowSaveFileDialog(callback, NULL, w, filters, nfilters, initial_path);
  102. }
  103. }
  104. }
  105. if (quit) {
  106. break;
  107. }
  108. SDL_Delay(100);
  109. SDL_SetRenderDrawColor(r, 0, 0, 0, SDL_ALPHA_OPAQUE);
  110. SDL_RenderClear(r);
  111. SDL_SetRenderDrawColor(r, 255, 0, 0, SDL_ALPHA_OPAQUE);
  112. SDL_RenderFillRect(r, &open_file_rect);
  113. SDL_SetRenderDrawColor(r, 0, 255, 0, SDL_ALPHA_OPAQUE);
  114. SDL_RenderFillRect(r, &save_file_rect);
  115. SDL_SetRenderDrawColor(r, 0, 0, 255, SDL_ALPHA_OPAQUE);
  116. SDL_RenderFillRect(r, &open_folder_rect);
  117. SDL_SetRenderDrawColor(r, 0, 0, 0, SDL_ALPHA_OPAQUE);
  118. SDLTest_DrawString(r, open_file_rect.x+5, open_file_rect.y+open_file_rect.h/2, "Open File...");
  119. SDLTest_DrawString(r, save_file_rect.x+5, save_file_rect.y+save_file_rect.h/2, "Save File...");
  120. SDLTest_DrawString(r, open_folder_rect.x+5, open_folder_rect.y+open_folder_rect.h/2, "Open Folder...");
  121. SDL_RenderPresent(r);
  122. }
  123. SDLTest_CleanupTextDrawing();
  124. SDL_DestroyRenderer(r);
  125. SDL_DestroyWindow(w);
  126. SDL_Quit();
  127. SDLTest_CommonDestroyState(state);
  128. return 0;
  129. }