diff options
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testmodal.c')
-rw-r--r-- | src/contrib/SDL-3.2.20/test/testmodal.c | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testmodal.c b/src/contrib/SDL-3.2.20/test/testmodal.c new file mode 100644 index 0000000..d898b89 --- /dev/null +++ b/src/contrib/SDL-3.2.20/test/testmodal.c | |||
@@ -0,0 +1,176 @@ | |||
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 a parent window and a modal child window. */ | ||
13 | |||
14 | #include <SDL3/SDL.h> | ||
15 | #include <SDL3/SDL_main.h> | ||
16 | #include <SDL3/SDL_test.h> | ||
17 | #include <stdlib.h> | ||
18 | |||
19 | int main(int argc, char *argv[]) | ||
20 | { | ||
21 | SDL_Window *w1 = NULL, *w2 = NULL; | ||
22 | SDL_Renderer *r1 = NULL, *r2 = NULL; | ||
23 | SDLTest_CommonState *state = NULL; | ||
24 | Uint64 show_deadline = 0; | ||
25 | int i; | ||
26 | int exit_code = 0; | ||
27 | |||
28 | /* Initialize test framework */ | ||
29 | state = SDLTest_CommonCreateState(argv, 0); | ||
30 | if (state == NULL) { | ||
31 | return 1; | ||
32 | } | ||
33 | |||
34 | /* Parse commandline */ | ||
35 | for (i = 1; i < argc;) { | ||
36 | int consumed; | ||
37 | |||
38 | consumed = SDLTest_CommonArg(state, i); | ||
39 | |||
40 | if (consumed <= 0) { | ||
41 | static const char *options[] = { NULL }; | ||
42 | SDLTest_CommonLogUsage(state, argv[0], options); | ||
43 | return 1; | ||
44 | } | ||
45 | |||
46 | i += consumed; | ||
47 | } | ||
48 | |||
49 | if (!SDL_Init(SDL_INIT_VIDEO)) { | ||
50 | SDL_Log("SDL_Init failed (%s)", SDL_GetError()); | ||
51 | return 1; | ||
52 | } | ||
53 | |||
54 | if (!SDL_CreateWindowAndRenderer("Parent Window", 640, 480, 0, &w1, &r1)) { | ||
55 | SDL_Log("Failed to create parent window and/or renderer: %s", SDL_GetError()); | ||
56 | exit_code = 1; | ||
57 | goto sdl_quit; | ||
58 | } | ||
59 | |||
60 | if (!SDL_CreateWindowAndRenderer("Non-Modal Window", 320, 200, 0, &w2, &r2)) { | ||
61 | SDL_Log("Failed to create parent window and/or renderer: %s", SDL_GetError()); | ||
62 | exit_code = 1; | ||
63 | goto sdl_quit; | ||
64 | } | ||
65 | |||
66 | if (SDL_SetWindowParent(w2, w1)) { | ||
67 | if (SDL_SetWindowModal(w2, true)) { | ||
68 | SDL_SetWindowTitle(w2, "Modal Window"); | ||
69 | } | ||
70 | } | ||
71 | |||
72 | while (1) { | ||
73 | int quit = 0; | ||
74 | SDL_Event e; | ||
75 | while (SDL_PollEvent(&e)) { | ||
76 | if (e.type == SDL_EVENT_QUIT) { | ||
77 | quit = 1; | ||
78 | break; | ||
79 | } else if (e.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) { | ||
80 | if (e.window.windowID == SDL_GetWindowID(w2)) { | ||
81 | SDL_DestroyRenderer(r2); | ||
82 | SDL_DestroyWindow(w2); | ||
83 | r2 = NULL; | ||
84 | w2 = NULL; | ||
85 | } else if (e.window.windowID == SDL_GetWindowID(w1)) { | ||
86 | SDL_DestroyRenderer(r1); | ||
87 | SDL_DestroyWindow(w1); | ||
88 | r1 = NULL; | ||
89 | w1 = NULL; | ||
90 | } | ||
91 | } else if (e.type == SDL_EVENT_KEY_DOWN) { | ||
92 | if ((e.key.key == SDLK_M || e.key.key == SDLK_N) && !w2) { | ||
93 | if (!SDL_CreateWindowAndRenderer("Non-Modal Window", 320, 200, SDL_WINDOW_HIDDEN, &w2, &r2)) { | ||
94 | SDL_Log("Failed to create modal window and/or renderer: %s", SDL_GetError()); | ||
95 | exit_code = 1; | ||
96 | goto sdl_quit; | ||
97 | } | ||
98 | |||
99 | if (e.key.key == SDLK_M) { | ||
100 | if (SDL_SetWindowParent(w2, w1)) { | ||
101 | if (SDL_SetWindowModal(w2, true)) { | ||
102 | SDL_SetWindowTitle(w2, "Modal Window"); | ||
103 | } | ||
104 | } | ||
105 | } | ||
106 | SDL_ShowWindow(w2); | ||
107 | } else if (e.key.key == SDLK_ESCAPE && w2) { | ||
108 | SDL_DestroyWindow(w2); | ||
109 | r2 = NULL; | ||
110 | w2 = NULL; | ||
111 | } else if (e.key.key == SDLK_H) { | ||
112 | if (e.key.mod & SDL_KMOD_CTRL) { | ||
113 | /* Hide the parent, which should hide the modal too. */ | ||
114 | show_deadline = SDL_GetTicksNS() + SDL_SECONDS_TO_NS(3); | ||
115 | SDL_HideWindow(w1); | ||
116 | } else if (w2) { | ||
117 | /* Show/hide the modal window */ | ||
118 | if (SDL_GetWindowFlags(w2) & SDL_WINDOW_HIDDEN) { | ||
119 | SDL_ShowWindow(w2); | ||
120 | } else { | ||
121 | SDL_HideWindow(w2); | ||
122 | } | ||
123 | } | ||
124 | } else if (e.key.key == SDLK_P && w2) { | ||
125 | if (SDL_GetWindowFlags(w2) & SDL_WINDOW_MODAL) { | ||
126 | /* Unparent the window */ | ||
127 | if (SDL_SetWindowModal(w2, false)) { | ||
128 | if (SDL_SetWindowParent(w2, NULL)) { | ||
129 | SDL_SetWindowTitle(w2, "Non-Modal Window"); | ||
130 | } | ||
131 | } | ||
132 | } else { | ||
133 | /* Reparent the window */ | ||
134 | if (SDL_SetWindowParent(w2, w1)) { | ||
135 | if (SDL_SetWindowModal(w2, true)) { | ||
136 | SDL_SetWindowTitle(w2, "Modal Window"); | ||
137 | } | ||
138 | } | ||
139 | } | ||
140 | } | ||
141 | } | ||
142 | } | ||
143 | if (quit) { | ||
144 | break; | ||
145 | } | ||
146 | SDL_Delay(100); | ||
147 | |||
148 | if (show_deadline && show_deadline <= SDL_GetTicksNS()) { | ||
149 | SDL_ShowWindow(w1); | ||
150 | } | ||
151 | |||
152 | /* Parent window is red */ | ||
153 | if (r1) { | ||
154 | SDL_SetRenderDrawColor(r1, 224, 48, 12, SDL_ALPHA_OPAQUE); | ||
155 | SDL_RenderClear(r1); | ||
156 | SDL_RenderPresent(r1); | ||
157 | } | ||
158 | |||
159 | /* Child window is blue */ | ||
160 | if (r2) { | ||
161 | SDL_SetRenderDrawColor(r2, 6, 76, 255, SDL_ALPHA_OPAQUE); | ||
162 | SDL_RenderClear(r2); | ||
163 | SDL_RenderPresent(r2); | ||
164 | } | ||
165 | } | ||
166 | |||
167 | sdl_quit: | ||
168 | if (w1) { | ||
169 | /* The child window and renderer will be cleaned up automatically. */ | ||
170 | SDL_DestroyWindow(w1); | ||
171 | } | ||
172 | |||
173 | SDL_Quit(); | ||
174 | SDLTest_CommonDestroyState(state); | ||
175 | return exit_code; | ||
176 | } | ||