summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testdialog.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testdialog.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/testdialog.c154
1 files changed, 154 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testdialog.c b/src/contrib/SDL-3.2.20/test/testdialog.c
new file mode 100644
index 0000000..dcff5bd
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/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
18const SDL_DialogFileFilter filters[] = {
19 { "All files", "*" },
20 { "SVI Session Indexes", "index;svi-index;index.pb" },
21 { "JPG images", "jpg;jpeg" },
22 { "PNG images", "png" }
23};
24
25static void SDLCALL callback(void *userdata, const char * const *files, int filter) {
26 if (files) {
27 const char* filter_name = "(filter fetching unsupported)";
28
29 if (filter != -1) {
30 if (filter < sizeof(filters) / sizeof(*filters)) {
31 filter_name = filters[filter].name;
32 } else {
33 filter_name = "(No filter was selected)";
34 }
35 }
36
37 SDL_Log("Filter used: '%s'", filter_name);
38
39 while (*files) {
40 SDL_Log("'%s'", *files);
41 files++;
42 }
43 } else {
44 SDL_Log("Error: %s", SDL_GetError());
45 }
46}
47
48int main(int argc, char *argv[])
49{
50 SDL_Window *w;
51 SDL_Renderer *r;
52 SDLTest_CommonState *state;
53 const SDL_FRect open_file_rect = { 50, 50, 220, 140 };
54 const SDL_FRect save_file_rect = { 50, 290, 220, 140 };
55 const SDL_FRect open_folder_rect = { 370, 50, 220, 140 };
56 int i;
57 const char *initial_path = NULL;
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, SDL_arraysize(filters), 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, SDL_arraysize(filters), 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}