Atlas - testdialog.c
Home / ext / SDL / test Lines: 1 | Size: 6194 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 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_iostream.h> 16#include <SDL3/SDL_main.h> 17#include <SDL3/SDL_test.h> 18 19static const SDL_DialogFileFilter filters[] = { 20 { "All files", "*" }, 21 { "SVI Session Indexes", "index;svi-index;index.pb" }, 22 { "JPG images", "jpg;jpeg" }, 23 { "PNG images", "png" } 24}; 25 26static void SDLCALL callback(void *userdata, const char * const *files, int filter) { 27 char **saved_path = userdata; 28 29 if (files) { 30 const char *filter_name = "(filter fetching unsupported)"; 31 32 if (filter != -1) { 33 if (filter < sizeof(filters) / sizeof(*filters)) { 34 filter_name = filters[filter].name; 35 } else { 36 filter_name = "(No filter was selected)"; 37 } 38 } 39 40 SDL_Log("Filter used: '%s'", filter_name); 41 42 if (*files && saved_path) { 43 *saved_path = SDL_strdup(*files); 44 /* Create the file */ 45 SDL_IOStream *stream = SDL_IOFromFile(*saved_path, "w"); 46 SDL_CloseIO(stream); 47 } 48 49 while (*files) { 50 SDL_Log("'%s'", *files); 51 files++; 52 } 53 } else { 54 SDL_Log("Error: %s", SDL_GetError()); 55 } 56} 57 58static char *concat_strings(const char *a, const char *b) 59{ 60 char *out = NULL; 61 62 if (a != NULL && b != NULL) { 63 const size_t out_size = SDL_strlen(a) + SDL_strlen(b) + 1; 64 out = (char *)SDL_malloc(out_size); 65 if (out) { 66 *out = '\0'; 67 SDL_strlcat(out, a, out_size); 68 SDL_strlcat(out, b, out_size); 69 } 70 } 71 72 return out; 73} 74 75int main(int argc, char *argv[]) 76{ 77 static const SDL_FRect OPEN_FILE_RECT = { 50, 50, 220, 140 }; 78 static const SDL_FRect SAVE_FILE_RECT = { 50, 290, 220, 140 }; 79 static const SDL_FRect OPEN_FOLDER_RECT = { 370, 50, 220, 140 }; 80 static const char DEFAULT_FILENAME[] = "Untitled.index"; 81 const char *initial_path = NULL; 82 char *last_saved_path = NULL; 83 84 /* Initialize test framework */ 85 SDLTest_CommonState *state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); 86 if (state == NULL) { 87 return 1; 88 } 89 90 if (!SDLTest_CommonDefaultArgs(state, argc, argv)) { 91 SDL_Quit(); 92 SDLTest_CommonDestroyState(state); 93 return 1; 94 } 95 96 if (!SDLTest_CommonInit(state)) { 97 SDL_Log("SDL_Init failed (%s)", SDL_GetError()); 98 return 1; 99 } 100 101 initial_path = SDL_GetUserFolder(SDL_FOLDER_HOME); 102 103 if (!initial_path) { 104 SDL_Log("Will not use an initial path, couldn't get the home directory path: %s", SDL_GetError()); 105 } 106 107 while (1) { 108 int done = 0; 109 SDL_Event e; 110 while (SDL_PollEvent(&e)) { 111 SDLTest_CommonEvent(state, &e, &done); 112 if (e.type == SDL_EVENT_MOUSE_BUTTON_UP) { 113 const SDL_FPoint p = { e.button.x, e.button.y }; 114 SDL_Window *w = SDL_GetWindowFromID(e.button.windowID); 115 /* 116 * Arguments, in order: 117 * - A function to call when files are chosen (or dialog is canceled, or error happens) 118 * - A user-managed void pointer to pass to the function when it will be invoked 119 * - The window to bind the dialog to, or NULL if none 120 * - A list of filters for the files, see SDL_DialogFileFilter above (not for SDL_ShowOpenFolderDialog) 121 * - The path where the dialog should start. May be a folder or a file 122 * - Nonzero if the user is allowed to choose multiple entries (not for SDL_ShowSaveFileDialog) 123 */ 124 if (SDL_PointInRectFloat(&p, &OPEN_FILE_RECT)) { 125 SDL_ShowOpenFileDialog(callback, NULL, w, filters, SDL_arraysize(filters), initial_path, 1); 126 } else if (SDL_PointInRectFloat(&p, &OPEN_FOLDER_RECT)) { 127 SDL_ShowOpenFolderDialog(callback, NULL, w, initial_path, 1); 128 } else if (SDL_PointInRectFloat(&p, &SAVE_FILE_RECT)) { 129 char *save_path = NULL; 130 if (last_saved_path) { 131 save_path = SDL_strdup(last_saved_path); 132 } else { 133 save_path = concat_strings(initial_path, DEFAULT_FILENAME); 134 } 135 SDL_ShowSaveFileDialog(callback, &last_saved_path, w, filters, SDL_arraysize(filters), save_path ? save_path : DEFAULT_FILENAME); 136 SDL_free(save_path); 137 } 138 } 139 } 140 if (done) { 141 break; 142 } 143 SDL_Delay(100); 144 145 for (int i = 0; i < state->num_windows; i++) { 146 SDL_Renderer *r = state->renderers[i]; 147 148 SDL_SetRenderDrawColor(r, 0, 0, 0, SDL_ALPHA_OPAQUE); 149 SDL_RenderClear(r); 150 151 SDL_SetRenderDrawColor(r, 255, 0, 0, SDL_ALPHA_OPAQUE); 152 SDL_RenderFillRect(r, &OPEN_FILE_RECT); 153 154 SDL_SetRenderDrawColor(r, 0, 255, 0, SDL_ALPHA_OPAQUE); 155 SDL_RenderFillRect(r, &SAVE_FILE_RECT); 156 157 SDL_SetRenderDrawColor(r, 0, 0, 255, SDL_ALPHA_OPAQUE); 158 SDL_RenderFillRect(r, &OPEN_FOLDER_RECT); 159 160 SDL_SetRenderDrawColor(r, 0, 0, 0, SDL_ALPHA_OPAQUE); 161 SDLTest_DrawString(r, OPEN_FILE_RECT.x+5, OPEN_FILE_RECT.y+OPEN_FILE_RECT.h/2, "Open File..."); 162 SDLTest_DrawString(r, SAVE_FILE_RECT.x+5, SAVE_FILE_RECT.y+SAVE_FILE_RECT.h/2, "Save File..."); 163 SDLTest_DrawString(r, OPEN_FOLDER_RECT.x+5, OPEN_FOLDER_RECT.y+OPEN_FOLDER_RECT.h/2, "Open Folder..."); 164 165 SDL_RenderPresent(r); 166 } 167 } 168 169 SDL_free(last_saved_path); 170 SDLTest_CleanupTextDrawing(); 171 SDLTest_CommonQuit(state); 172 return 0; 173} 174[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.