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