2 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
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.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
13 /* Simple test of the SDL MessageBox API */
20 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
29 button_messagebox(void *eventNumber)
31 const SDL_MessageBoxButtonData buttons[] = {
33 SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
37 SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
43 SDL_MessageBoxData data = {
44 SDL_MESSAGEBOX_INFORMATION,
45 NULL, /* no parent window */
47 "This is a custom messagebox",
50 NULL /* Default color scheme */
55 data.buttons = buttons;
57 data.message = "This is a custom messagebox from a background thread.";
60 success = SDL_ShowMessageBox(&data, &button);
62 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
65 event.type = (intptr_t)eventNumber;
66 SDL_PushEvent((SDL_Event*)&event);
72 SDL_Log("Pressed button: %d, %s\n", button, button == -1 ? "[closed]" : button == 1 ? "Cancel" : "OK");
76 event.type = (intptr_t)eventNumber;
77 SDL_PushEvent((SDL_Event*)&event);
84 main(int argc, char *argv[])
88 /* Enable standard application logging */
89 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
91 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
93 "This is a simple error MessageBox",
96 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
100 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
102 "This is a simple MessageBox with a newline:\r\nHello world!",
105 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
109 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
114 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
118 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
123 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
127 /* Google says this is Traditional Chinese for "beef with broccoli" */
128 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
129 "UTF-8 Simple MessageBox",
130 "Unicode text: '牛肉西蘭花' ...",
133 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
137 /* Google says this is Traditional Chinese for "beef with broccoli" */
138 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
139 "UTF-8 Simple MessageBox",
140 "Unicode text and newline:\r\n'牛肉西蘭花'\n'牛肉西蘭花'",
143 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
147 /* Google says this is Traditional Chinese for "beef with broccoli" */
148 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
150 "Unicode text in the title.",
153 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
157 button_messagebox(NULL);
159 /* Test showing a message box from a background thread.
161 On Mac OS X, the video subsystem needs to be initialized for this
162 to work, since the message box events are dispatched by the Cocoa
163 subsystem on the main thread.
165 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
166 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video subsystem: %s\n", SDL_GetError());
172 intptr_t eventNumber = SDL_RegisterEvents(1);
173 SDL_Thread* thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void*)eventNumber);
175 while (SDL_WaitEvent(&event))
177 if (event.type == eventNumber) {
182 SDL_WaitThread(thread, &status);
184 SDL_Log("Message box thread return %i\n", status);
187 /* Test showing a message box with a parent window */
190 SDL_Window *window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
192 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
194 "This is a simple error MessageBox with a parent window",
197 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
201 while (SDL_WaitEvent(&event))
203 if (event.type == SDL_QUIT || event.type == SDL_KEYUP) {