Atlas - testmessage.c

Home / ext / SDL / test Lines: 8 | Size: 14684 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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. */ 26static void quit(int rc) 27{ 28 SDL_Quit(); 29 /* Let 'main()' return normally */ 30 if (rc != 0) { 31 exit(rc); 32 } 33} 34 35static int SDLCALL button_messagebox(void *eventNumber) 36{ 37 int i; 38 const SDL_MessageBoxButtonData buttons[] = { 39 { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 40 0, 41 "OK" }, 42 { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 43 1, 44 "Cancel" }, 45 { 0, 46 2, 47 "Retry" } 48 }; 49 SDL_MessageBoxData data = { 50 SDL_MESSAGEBOX_INFORMATION, 51 NULL, /* no parent window */ 52 "Custom MessageBox", 53 "This is a custom messagebox", 54 SDL_arraysize(buttons), 55 NULL, /* buttons */ 56 NULL /* Default color scheme */ 57 }; 58 59 for (i = 0; ; ++i) { 60 SDL_MessageBoxColorScheme colorScheme; 61 if (i != 0) { 62 int j; 63 for (j = 0; j < SDL_MESSAGEBOX_COLOR_COUNT; ++j) { 64 colorScheme.colors[j].r = SDL_rand(256); 65 colorScheme.colors[j].g = SDL_rand(256); 66 colorScheme.colors[j].b = SDL_rand(256); 67 } 68 data.colorScheme = &colorScheme; 69 } else { 70 data.colorScheme = NULL; 71 } 72 73 int button = -1; 74 data.buttons = buttons; 75 if (eventNumber) { 76 data.message = "This is a custom messagebox from a background thread."; 77 } 78 79 if (!SDL_ShowMessageBox(&data, &button)) { 80 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 81 if (eventNumber) { 82 SDL_Event event; 83 event.type = (Uint32)(intptr_t)eventNumber; 84 SDL_PushEvent(&event); 85 return 1; 86 } else { 87 quit(2); 88 } 89 } 90 91 const char* text; 92 if (button == 1) { 93 text = "Cancel"; 94 } else if (button == 2) { 95 text = "Retry"; 96 } else { 97 text = "OK"; 98 } 99 SDL_Log("Pressed button: %d, %s", button, button == -1 ? "[closed]" : text); 100 101 if (button == 2) { 102 continue; 103 } 104 105 if (eventNumber) { 106 SDL_Event event; 107 event.type = (Uint32)(intptr_t)eventNumber; 108 SDL_PushEvent(&event); 109 } 110 111 return 0; 112 } 113} 114 115int main(int argc, char *argv[]) 116{ 117 bool success; 118 SDLTest_CommonState *state; 119 120 /* Initialize test framework */ 121 state = SDLTest_CommonCreateState(argv, 0); 122 if (!state) { 123 return 1; 124 } 125 126 /* Parse commandline */ 127 if (!SDLTest_CommonDefaultArgs(state, argc, argv)) { 128 return 1; 129 } 130 131 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 132 "Simple MessageBox", 133 "This is a simple error MessageBox", 134 NULL); 135 if (!success) { 136 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 137 quit(1); 138 } 139 140 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, 141 "Simple MessageBox", 142 "This is a simple MessageBox with a newline:\r\nHello world!", 143 NULL); 144 if (!success) { 145 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 146 quit(1); 147 } 148 149 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 150 NULL, 151 "NULL Title", 152 NULL); 153 if (!success) { 154 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 155 quit(1); 156 } 157 158 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 159 "NULL Message", 160 NULL, 161 NULL); 162 if (!success) { 163 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 164 quit(1); 165 } 166 167 success = SDL_ShowSimpleMessageBox(0, 168 "No icon", 169 "This is a MessageBox with no icon!", 170 NULL); 171 172 /* Google says this is Traditional Chinese for "beef with broccoli" */ 173 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 174 "UTF-8 Simple MessageBox", 175 "Unicode text: '牛肉西蘭花' ...", 176 NULL); 177 if (!success) { 178 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 179 quit(1); 180 } 181 182 /* Google says this is Traditional Chinese for "beef with broccoli" */ 183 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 184 "UTF-8 Simple MessageBox", 185 "Unicode text and newline:\r\n'牛肉西蘭花'\n'牛肉西蘭花'", 186 NULL); 187 if (!success) { 188 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 189 quit(1); 190 } 191 192 /* Google says this is Traditional Chinese for "beef with broccoli" */ 193 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 194 "牛肉西蘭花", 195 "Unicode text in the title.", 196 NULL); 197 if (!success) { 198 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 199 quit(1); 200 } 201 202 /* Various other Unicode tests */ 203 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 204 "Latin", 205 "Türkçe özellikle çok güzel bir dil.\nBjörn/Bjørn\nÆgypt", 206 NULL); 207 if (!success) { 208 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 209 quit(1); 210 } 211 212 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 213 "Arabic", 214 "يتم استخدام أنظمة الكتابة لتسجيل اللغة البشرية.", 215 NULL); 216 if (!success) { 217 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 218 quit(1); 219 } 220 221 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 222 "Arabic (multi line)", 223 "سطر طويل جدًا من النص\nخط قصير\nسطر طويل للغاية من النص مذهل بشكل لا يصدق في اللغة العربية التي يتم التحدث بها في منطقة الشرق الأوسط وشمال أفريقيا", 224 NULL); 225 if (!success) { 226 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 227 quit(1); 228 } 229 230 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 231 "Cyrillic (Ukranian)", 232 "Для запису людської мови використовуються системи письма.", 233 NULL); 234 if (!success) { 235 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 236 quit(1); 237 } 238 239 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 240 "Japanese", 241 "文字体系は人間の言語を記録するために使用されます。", 242 NULL); 243 if (!success) { 244 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 245 quit(1); 246 } 247 248 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 249 "Indian", 250 "लेखन प्रणालियों का उपयोग मानव भाषा को रिकॉर्ड करने के लिए किया जाता है।", 251 NULL); 252 if (!success) { 253 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 254 quit(1); 255 } 256 257 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 258 "Korean", 259 "문자 체계는 인간의 언어를 기록하는 데 사용됩니다.", 260 NULL); 261 if (!success) { 262 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 263 quit(1); 264 } 265 266 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 267 "Thai", 268 "ระบบการเขียนใช้เพื่อบันทึกภาษาของมนุษย์", 269 NULL); 270 if (!success) { 271 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 272 quit(1); 273 } 274 275 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 276 "Greek", 277 "Τα συστήματα γραφής χρησιμοποιούνται για την καταγραφή της ανθρώπινης γλώσσας.", 278 NULL); 279 if (!success) { 280 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 281 quit(1); 282 } 283 284 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 285 "Hebrew", 286 "מערכות כתיבה משמשות לרישום שפה אנושית.", 287 NULL); 288 if (!success) { 289 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 290 quit(1); 291 } 292 293 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 294 "Armenian", 295 "Գրային համակարգերը օգտագործվում են մարդկային լեզուն գրանցելու համար։", 296 NULL); 297 if (!success) { 298 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 299 quit(1); 300 } 301 302 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 303 "Georgian", 304 "წერის სისტემები გამოიყენება ადამიანის ენის ჩასაწერად.", 305 NULL); 306 if (!success) { 307 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 308 quit(1); 309 } 310 311 button_messagebox(NULL); 312 313 /* Test showing a message box from a background thread. 314 315 On macOS, the video subsystem needs to be initialized for this 316 to work, since the message box events are dispatched by the Cocoa 317 subsystem on the main thread. 318 */ 319 if (!SDL_Init(SDL_INIT_VIDEO)) { 320 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video subsystem: %s", SDL_GetError()); 321 return 1; 322 } 323 { 324 int status = 0; 325 SDL_Event event; 326 Uint32 eventNumber = SDL_RegisterEvents(1); 327 SDL_Thread *thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void *)(uintptr_t)eventNumber); 328 329 while (SDL_WaitEvent(&event)) { 330 if (event.type == eventNumber) { 331 break; 332 } 333 } 334 335 SDL_WaitThread(thread, &status); 336 337 SDL_Log("Message box thread return %i", status); 338 } 339 340 /* Test showing a message box with a parent window */ 341 { 342 SDL_Event event; 343 SDL_Window *window = SDL_CreateWindow("Test", 640, 480, 0); 344 345 /* On wayland, no window will actually show until something has 346 actually been displayed. 347 */ 348 SDL_Renderer *renderer = SDL_CreateRenderer(window, NULL); 349 SDL_RenderPresent(renderer); 350 351 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 352 "Simple MessageBox", 353 "This is a simple error MessageBox with a parent window. Press a key or close the window after dismissing this messagebox.", 354 window); 355 if (!success) { 356 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError()); 357 quit(1); 358 } 359 360 while (SDL_WaitEvent(&event)) { 361 if (event.type == SDL_EVENT_QUIT || 362 event.type == SDL_EVENT_KEY_UP || 363 event.type == SDL_EVENT_MOUSE_BUTTON_UP) { 364 break; 365 } 366 } 367 } 368 369 SDL_Quit(); 370 SDLTest_CommonDestroyState(state); 371 return 0; 372} 373
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.