diff options
Diffstat (limited to 'contrib/SDL-3.2.8/test/testmessage.c')
| -rw-r--r-- | contrib/SDL-3.2.8/test/testmessage.c | 230 |
1 files changed, 230 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/test/testmessage.c b/contrib/SDL-3.2.8/test/testmessage.c new file mode 100644 index 0000000..1e5dbc9 --- /dev/null +++ b/contrib/SDL-3.2.8/test/testmessage.c | |||
| @@ -0,0 +1,230 @@ | |||
| 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 | |||
| 13 | /* Simple test of the SDL MessageBox API */ | ||
| 14 | #include <stdlib.h> | ||
| 15 | |||
| 16 | #include <SDL3/SDL.h> | ||
| 17 | #include <SDL3/SDL_main.h> | ||
| 18 | #include <SDL3/SDL_test.h> | ||
| 19 | |||
| 20 | /* This enables themed Windows dialogs when building with Visual Studio */ | ||
| 21 | #if defined(SDL_PLATFORM_WINDOWS) && defined(_MSC_VER) | ||
| 22 | #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") | ||
| 23 | #endif | ||
| 24 | |||
| 25 | /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ | ||
| 26 | static void | ||
| 27 | quit(int rc) | ||
| 28 | { | ||
| 29 | SDL_Quit(); | ||
| 30 | /* Let 'main()' return normally */ | ||
| 31 | if (rc != 0) { | ||
| 32 | exit(rc); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | static int SDLCALL | ||
| 37 | button_messagebox(void *eventNumber) | ||
| 38 | { | ||
| 39 | const SDL_MessageBoxButtonData buttons[] = { | ||
| 40 | { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, | ||
| 41 | 0, | ||
| 42 | "OK" }, | ||
| 43 | { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, | ||
| 44 | 1, | ||
| 45 | "Cancel" }, | ||
| 46 | }; | ||
| 47 | |||
| 48 | SDL_MessageBoxData data = { | ||
| 49 | SDL_MESSAGEBOX_INFORMATION, | ||
| 50 | NULL, /* no parent window */ | ||
| 51 | "Custom MessageBox", | ||
| 52 | "This is a custom messagebox", | ||
| 53 | 2, | ||
| 54 | NULL, /* buttons */ | ||
| 55 | NULL /* Default color scheme */ | ||
| 56 | }; | ||
| 57 | |||
| 58 | int button = -1; | ||
| 59 | int success = 0; | ||
| 60 | data.buttons = buttons; | ||
| 61 | if (eventNumber) { | ||
| 62 | data.message = "This is a custom messagebox from a background thread."; | ||
| 63 | } | ||
| 64 | |||
| 65 | success = SDL_ShowMessageBox(&data, &button); | ||
| 66 | if (success == -1) { | ||
| 67 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); | ||
| 68 | if (eventNumber) { | ||
| 69 | SDL_Event event; | ||
| 70 | event.type = (Uint32)(intptr_t)eventNumber; | ||
| 71 | SDL_PushEvent(&event); | ||
| 72 | return 1; | ||
| 73 | } else { | ||
| 74 | quit(2); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | SDL_Log("Pressed button: %d, %s", button, button == -1 ? "[closed]" : button == 1 ? "Cancel" | ||
| 78 | : "OK"); | ||
| 79 | |||
| 80 | if (eventNumber) { | ||
| 81 | SDL_Event event; | ||
| 82 | event.type = (Uint32)(intptr_t)eventNumber; | ||
| 83 | SDL_PushEvent(&event); | ||
| 84 | } | ||
| 85 | |||
| 86 | return 0; | ||
| 87 | } | ||
| 88 | |||
| 89 | int main(int argc, char *argv[]) | ||
| 90 | { | ||
| 91 | int success; | ||
| 92 | SDLTest_CommonState *state; | ||
| 93 | |||
| 94 | /* Initialize test framework */ | ||
| 95 | state = SDLTest_CommonCreateState(argv, 0); | ||
| 96 | if (!state) { | ||
| 97 | return 1; | ||
| 98 | } | ||
| 99 | |||
| 100 | /* Parse commandline */ | ||
| 101 | if (!SDLTest_CommonDefaultArgs(state, argc, argv)) { | ||
| 102 | return 1; | ||
| 103 | } | ||
| 104 | |||
| 105 | success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, | ||
| 106 | "Simple MessageBox", | ||
| 107 | "This is a simple error MessageBox", | ||
| 108 | NULL); | ||
| 109 | if (!success) { | ||
| 110 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); | ||
| 111 | quit(1); | ||
| 112 | } | ||
| 113 | |||
| 114 | success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, | ||
| 115 | "Simple MessageBox", | ||
| 116 | "This is a simple MessageBox with a newline:\r\nHello world!", | ||
| 117 | NULL); | ||
| 118 | if (!success) { | ||
| 119 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); | ||
| 120 | quit(1); | ||
| 121 | } | ||
| 122 | |||
| 123 | success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, | ||
| 124 | NULL, | ||
| 125 | "NULL Title", | ||
| 126 | NULL); | ||
| 127 | if (!success) { | ||
| 128 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); | ||
| 129 | quit(1); | ||
| 130 | } | ||
| 131 | |||
| 132 | success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, | ||
| 133 | "NULL Message", | ||
| 134 | NULL, | ||
| 135 | NULL); | ||
| 136 | if (!success) { | ||
| 137 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); | ||
| 138 | quit(1); | ||
| 139 | } | ||
| 140 | |||
| 141 | /* Google says this is Traditional Chinese for "beef with broccoli" */ | ||
| 142 | success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, | ||
| 143 | "UTF-8 Simple MessageBox", | ||
| 144 | "Unicode text: '牛肉西蘭花' ...", | ||
| 145 | NULL); | ||
| 146 | if (!success) { | ||
| 147 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); | ||
| 148 | quit(1); | ||
| 149 | } | ||
| 150 | |||
| 151 | /* Google says this is Traditional Chinese for "beef with broccoli" */ | ||
| 152 | success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, | ||
| 153 | "UTF-8 Simple MessageBox", | ||
| 154 | "Unicode text and newline:\r\n'牛肉西蘭花'\n'牛肉西蘭花'", | ||
| 155 | NULL); | ||
| 156 | if (!success) { | ||
| 157 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); | ||
| 158 | quit(1); | ||
| 159 | } | ||
| 160 | |||
| 161 | /* Google says this is Traditional Chinese for "beef with broccoli" */ | ||
| 162 | success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, | ||
| 163 | "牛肉西蘭花", | ||
| 164 | "Unicode text in the title.", | ||
| 165 | NULL); | ||
| 166 | if (!success) { | ||
| 167 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); | ||
| 168 | quit(1); | ||
| 169 | } | ||
| 170 | |||
| 171 | button_messagebox(NULL); | ||
| 172 | |||
| 173 | /* Test showing a message box from a background thread. | ||
| 174 | |||
| 175 | On macOS, the video subsystem needs to be initialized for this | ||
| 176 | to work, since the message box events are dispatched by the Cocoa | ||
| 177 | subsystem on the main thread. | ||
| 178 | */ | ||
| 179 | if (!SDL_Init(SDL_INIT_VIDEO)) { | ||
| 180 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video subsystem: %s", SDL_GetError()); | ||
| 181 | return 1; | ||
| 182 | } | ||
| 183 | { | ||
| 184 | int status = 0; | ||
| 185 | SDL_Event event; | ||
| 186 | Uint32 eventNumber = SDL_RegisterEvents(1); | ||
| 187 | SDL_Thread *thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void *)(uintptr_t)eventNumber); | ||
| 188 | |||
| 189 | while (SDL_WaitEvent(&event)) { | ||
| 190 | if (event.type == eventNumber) { | ||
| 191 | break; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | SDL_WaitThread(thread, &status); | ||
| 196 | |||
| 197 | SDL_Log("Message box thread return %i", status); | ||
| 198 | } | ||
| 199 | |||
| 200 | /* Test showing a message box with a parent window */ | ||
| 201 | { | ||
| 202 | SDL_Event event; | ||
| 203 | SDL_Window *window = SDL_CreateWindow("Test", 640, 480, 0); | ||
| 204 | |||
| 205 | /* On wayland, no window will actually show until something has | ||
| 206 | actually been displayed. | ||
| 207 | */ | ||
| 208 | SDL_Renderer *renderer = SDL_CreateRenderer(window, NULL); | ||
| 209 | SDL_RenderPresent(renderer); | ||
| 210 | |||
| 211 | success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, | ||
| 212 | "Simple MessageBox", | ||
| 213 | "This is a simple error MessageBox with a parent window. Press a key or close the window after dismissing this messagebox.", | ||
| 214 | window); | ||
| 215 | if (!success) { | ||
| 216 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); | ||
| 217 | quit(1); | ||
| 218 | } | ||
| 219 | |||
| 220 | while (SDL_WaitEvent(&event)) { | ||
| 221 | if (event.type == SDL_EVENT_QUIT || event.type == SDL_EVENT_KEY_UP) { | ||
| 222 | break; | ||
| 223 | } | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | SDL_Quit(); | ||
| 228 | SDLTest_CommonDestroyState(state); | ||
| 229 | return 0; | ||
| 230 | } | ||
