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