summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/test/testpopup.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
committer3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
commit5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch)
tree8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/test/testpopup.c
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/test/testpopup.c')
-rw-r--r--contrib/SDL-3.2.8/test/testpopup.c279
1 files changed, 279 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/test/testpopup.c b/contrib/SDL-3.2.8/test/testpopup.c
new file mode 100644
index 0000000..786f03c
--- /dev/null
+++ b/contrib/SDL-3.2.8/test/testpopup.c
@@ -0,0 +1,279 @@
1/*
2Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
3
4This software is provided 'as-is', without any express or implied
5warranty. In no event will the authors be held liable for any damages
6arising from the use of this software.
7
8Permission is granted to anyone to use this software for any purpose,
9including commercial applications, and to alter it and redistribute it
10freely.
11*/
12/* Simple program: Move N sprites around on the screen as fast as possible */
13
14#include <SDL3/SDL_main.h>
15#include <SDL3/SDL_test_common.h>
16#include <SDL3/SDL_test_font.h>
17
18#ifdef SDL_PLATFORM_EMSCRIPTEN
19#include <emscripten/emscripten.h>
20#endif
21
22#include <stdlib.h>
23
24#define MENU_WIDTH 120
25#define MENU_HEIGHT 300
26
27#define TOOLTIP_DELAY 500
28#define TOOLTIP_WIDTH 175
29#define TOOLTIP_HEIGHT 32
30
31static SDLTest_CommonState *state;
32static int num_menus;
33static Uint64 tooltip_timer;
34static int done;
35static const SDL_Color colors[] = {
36 { 0x7F, 0x00, 0x00, 0xFF },
37 { 0x00, 0x7F, 0x00, 0xFF },
38 { 0x00, 0x00, 0x7F, 0xFF }
39};
40
41struct PopupWindow
42{
43 SDL_Window *win;
44 SDL_Window *parent;
45 SDL_Renderer *renderer;
46 int idx;
47};
48
49static struct PopupWindow *menus;
50static struct PopupWindow tooltip;
51
52/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
53static void quit(int rc)
54{
55 SDL_free(menus);
56 menus = NULL;
57
58 SDLTest_CleanupTextDrawing();
59 SDLTest_CommonQuit(state);
60 /* Let 'main()' return normally */
61 if (rc != 0) {
62 exit(rc);
63 }
64}
65
66static int get_menu_index_by_window(SDL_Window *window)
67{
68 int i;
69 for (i = 0; i < num_menus; ++i) {
70 if (menus[i].win == window) {
71 return i;
72 }
73 }
74
75 return -1;
76}
77
78static bool window_is_root(SDL_Window *window)
79{
80 int i;
81 for (i = 0; i < state->num_windows; ++i) {
82 if (window == state->windows[i]) {
83 return true;
84 }
85 }
86
87 return false;
88}
89
90static bool create_popup(struct PopupWindow *new_popup, bool is_menu)
91{
92 SDL_Window *focus;
93 SDL_Window *new_win;
94 SDL_Renderer *new_renderer;
95 const int w = is_menu ? MENU_WIDTH : TOOLTIP_WIDTH;
96 const int h = is_menu ? MENU_HEIGHT : TOOLTIP_HEIGHT;
97 const int v_off = is_menu ? 0 : 32;
98 const SDL_WindowFlags flags = is_menu ? SDL_WINDOW_POPUP_MENU : SDL_WINDOW_TOOLTIP;
99 float x, y;
100
101 focus = SDL_GetMouseFocus();
102
103 SDL_GetMouseState(&x, &y);
104 new_win = SDL_CreatePopupWindow(focus,
105 (int)x, (int)y + v_off, w, h, flags);
106
107 if (new_win) {
108 new_renderer = SDL_CreateRenderer(new_win, state->renderdriver);
109
110 new_popup->win = new_win;
111 new_popup->renderer = new_renderer;
112 new_popup->parent = focus;
113
114 return true;
115 }
116
117 SDL_zerop(new_popup);
118 return false;
119}
120
121static void close_popups(void)
122{
123 int i;
124
125 for (i = 0; i < num_menus; ++i) {
126 /* Destroying the lowest level window recursively destroys the child windows */
127 if (window_is_root(menus[i].parent)) {
128 SDL_DestroyWindow(menus[i].win);
129 }
130 }
131 SDL_free(menus);
132 menus = NULL;
133 num_menus = 0;
134
135 /* If the tooltip was a child of a popup, it was recursively destroyed with the popup */
136 if (!window_is_root(tooltip.parent)) {
137 SDL_zero(tooltip);
138 }
139}
140
141static void loop(void)
142{
143 int i;
144 char fmt_str[128];
145 SDL_Event event;
146
147 /* Check for events */
148 while (SDL_PollEvent(&event)) {
149 if (event.type == SDL_EVENT_MOUSE_MOTION) {
150 /* Hide the tooltip and restart the timer if the mouse is moved */
151 if (tooltip.win) {
152 SDL_DestroyWindow(tooltip.win);
153 SDL_zero(tooltip);
154 }
155 tooltip_timer = SDL_GetTicks() + TOOLTIP_DELAY;
156
157 if (num_menus > 0 && event.motion.windowID == SDL_GetWindowID(menus[0].parent)) {
158 int x = (int)event.motion.x;
159 int y = (int)event.motion.y;
160
161 SDL_SetWindowPosition(menus[0].win, x, y);
162 }
163 } else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
164 /* Left click closes the popup menus */
165 if (event.button.button == SDL_BUTTON_LEFT) {
166 close_popups();
167 } else if (event.button.button == SDL_BUTTON_RIGHT) {
168 /* Create a new popup menu */
169 menus = SDL_realloc(menus, sizeof(struct PopupWindow) * (num_menus + 1));
170 if (create_popup(&menus[num_menus], true)) {
171 ++num_menus;
172 }
173 }
174 } else if (event.type == SDL_EVENT_KEY_DOWN) {
175 if (event.key.key == SDLK_SPACE) {
176 for (i = 0; i < num_menus; ++i) {
177 if (SDL_GetWindowFlags(menus[i].win) & SDL_WINDOW_HIDDEN) {
178 SDL_ShowWindow(menus[i].win);
179 } else {
180 SDL_HideWindow(menus[i].win);
181 }
182 }
183 /* Don't process this event in SDLTest_CommonEvent() */
184 continue;
185 }
186 }
187
188 SDLTest_CommonEvent(state, &event, &done);
189 }
190
191 if (done) {
192 return;
193 }
194
195 /* Show the tooltip if the delay period has elapsed */
196 if (SDL_GetTicks() > tooltip_timer) {
197 if (!tooltip.win) {
198 create_popup(&tooltip, false);
199 }
200 }
201
202 /* Draw the window */
203 for (i = 0; i < state->num_windows; ++i) {
204 SDL_Renderer *renderer = state->renderers[i];
205 SDL_RenderClear(renderer);
206 SDL_RenderPresent(renderer);
207 }
208
209 /* Draw the menus in alternating colors */
210 for (i = 0; i < num_menus; ++i) {
211 const SDL_Color *c = &colors[i % SDL_arraysize(colors)];
212 SDL_Renderer *renderer = menus[i].renderer;
213
214 SDL_SetRenderDrawColor(renderer, c->r, c->g, c->b, c->a);
215 SDL_RenderClear(renderer);
216 SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
217 SDL_snprintf(fmt_str, sizeof(fmt_str), "Popup Menu %i", i);
218 SDLTest_DrawString(renderer, 10.0f, 10.0f, fmt_str);
219 SDL_RenderPresent(renderer);
220 }
221
222 /* Draw the tooltip */
223 if (tooltip.win) {
224 int menu_idx;
225
226 SDL_SetRenderDrawColor(tooltip.renderer, 0x00, 0x00, 0x00, 0xFF);
227 SDL_RenderClear(tooltip.renderer);
228 SDL_SetRenderDrawColor(tooltip.renderer, 0xFF, 0xFF, 0xFF, 0xFF);
229
230 menu_idx = get_menu_index_by_window(tooltip.parent);
231 if (menu_idx >= 0) {
232 SDL_snprintf(fmt_str, sizeof(fmt_str), "Tooltip for popup %i", menu_idx);
233 } else {
234 SDL_snprintf(fmt_str, sizeof(fmt_str), "Toplevel tooltip");
235 }
236 SDLTest_DrawString(tooltip.renderer, 10.0f, TOOLTIP_HEIGHT / 2, fmt_str);
237 SDL_RenderPresent(tooltip.renderer);
238 }
239}
240
241int main(int argc, char *argv[])
242{
243 int i;
244
245 /* Initialize test framework */
246 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
247 if (!state) {
248 return 1;
249 }
250
251 /* Parse commandline */
252 if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
253 return 1;
254 }
255
256 if (!SDLTest_CommonInit(state)) {
257 SDLTest_CommonQuit(state);
258 quit(2);
259 }
260
261 for (i = 0; i < state->num_windows; ++i) {
262 SDL_Renderer *renderer = state->renderers[i];
263 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
264 SDL_RenderClear(renderer);
265 }
266
267 /* Main render loop */
268 done = 0;
269#ifdef SDL_PLATFORM_EMSCRIPTEN
270 emscripten_set_main_loop(loop, 0, 1);
271#else
272 while (!done) {
273 loop();
274 }
275#endif
276 quit(0);
277 /* keep the compiler happy ... */
278 return 0;
279}