Atlas - testdialog.c

Home / ext / SDL / test Lines: 1 | Size: 6474 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Copyright (C) 1997-2025 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 19const 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 58char *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 SDL_Window *w; 78 SDL_Renderer *r; 79 SDLTest_CommonState *state; 80 const SDL_FRect open_file_rect = { 50, 50, 220, 140 }; 81 const SDL_FRect save_file_rect = { 50, 290, 220, 140 }; 82 const SDL_FRect open_folder_rect = { 370, 50, 220, 140 }; 83 int i; 84 const char *default_filename = "Untitled.index"; 85 const char *initial_path = NULL; 86 char *last_saved_path = NULL; 87 88 /* Initialize test framework */ 89 state = SDLTest_CommonCreateState(argv, 0); 90 if (state == NULL) { 91 return 1; 92 } 93 94 /* Parse commandline */ 95 for (i = 1; i < argc;) { 96 int consumed; 97 98 consumed = SDLTest_CommonArg(state, i); 99 100 if (consumed <= 0) { 101 static const char *options[] = { NULL }; 102 SDLTest_CommonLogUsage(state, argv[0], options); 103 return 1; 104 } 105 106 i += consumed; 107 } 108 109 if (!SDL_Init(SDL_INIT_VIDEO)) { 110 SDL_Log("SDL_Init failed (%s)", SDL_GetError()); 111 return 1; 112 } 113 if (!SDL_CreateWindowAndRenderer("testdialog", 640, 480, 0, &w, &r)) { 114 SDL_Log("Failed to create window and/or renderer: %s", SDL_GetError()); 115 SDL_Quit(); 116 return 1; 117 } 118 119 initial_path = SDL_GetUserFolder(SDL_FOLDER_HOME); 120 121 if (!initial_path) { 122 SDL_Log("Will not use an initial path, couldn't get the home directory path: %s", SDL_GetError()); 123 } 124 125 while (1) { 126 int quit = 0; 127 SDL_Event e; 128 while (SDL_PollEvent(&e)) { 129 if (e.type == SDL_EVENT_QUIT) { 130 quit = 1; 131 break; 132 } else if (e.type == SDL_EVENT_MOUSE_BUTTON_UP) { 133 const SDL_FPoint p = { e.button.x, e.button.y }; 134 /* 135 * Arguments, in order: 136 * - A function to call when files are chosen (or dialog is canceled, or error happens) 137 * - A user-managed void pointer to pass to the function when it will be invoked 138 * - The window to bind the dialog to, or NULL if none 139 * - A list of filters for the files, see SDL_DialogFileFilter above (not for SDL_ShowOpenFolderDialog) 140 * - The path where the dialog should start. May be a folder or a file 141 * - Nonzero if the user is allowed to choose multiple entries (not for SDL_ShowSaveFileDialog) 142 */ 143 if (SDL_PointInRectFloat(&p, &open_file_rect)) { 144 SDL_ShowOpenFileDialog(callback, NULL, w, filters, SDL_arraysize(filters), initial_path, 1); 145 } else if (SDL_PointInRectFloat(&p, &open_folder_rect)) { 146 SDL_ShowOpenFolderDialog(callback, NULL, w, initial_path, 1); 147 } else if (SDL_PointInRectFloat(&p, &save_file_rect)) { 148 char *save_path = NULL; 149 if (last_saved_path) { 150 save_path = SDL_strdup(last_saved_path); 151 } else { 152 save_path = concat_strings(initial_path, default_filename); 153 } 154 SDL_ShowSaveFileDialog(callback, &last_saved_path, w, filters, SDL_arraysize(filters), save_path ? save_path : default_filename); 155 SDL_free(save_path); 156 } 157 } 158 } 159 if (quit) { 160 break; 161 } 162 SDL_Delay(100); 163 164 SDL_SetRenderDrawColor(r, 0, 0, 0, SDL_ALPHA_OPAQUE); 165 SDL_RenderClear(r); 166 167 SDL_SetRenderDrawColor(r, 255, 0, 0, SDL_ALPHA_OPAQUE); 168 SDL_RenderFillRect(r, &open_file_rect); 169 170 SDL_SetRenderDrawColor(r, 0, 255, 0, SDL_ALPHA_OPAQUE); 171 SDL_RenderFillRect(r, &save_file_rect); 172 173 SDL_SetRenderDrawColor(r, 0, 0, 255, SDL_ALPHA_OPAQUE); 174 SDL_RenderFillRect(r, &open_folder_rect); 175 176 SDL_SetRenderDrawColor(r, 0, 0, 0, SDL_ALPHA_OPAQUE); 177 SDLTest_DrawString(r, open_file_rect.x+5, open_file_rect.y+open_file_rect.h/2, "Open File..."); 178 SDLTest_DrawString(r, save_file_rect.x+5, save_file_rect.y+save_file_rect.h/2, "Save File..."); 179 SDLTest_DrawString(r, open_folder_rect.x+5, open_folder_rect.y+open_folder_rect.h/2, "Open Folder..."); 180 181 SDL_RenderPresent(r); 182 } 183 184 SDL_free(last_saved_path); 185 SDLTest_CleanupTextDrawing(); 186 SDL_DestroyRenderer(r); 187 SDL_DestroyWindow(w); 188 SDL_Quit(); 189 SDLTest_CommonDestroyState(state); 190 return 0; 191} 192
[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.