Debian Codegraph @

test/testmessage.c

raw
1 /*
2 Copyright (C) 1997-2026 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
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 #include "SDL.h"
19
20 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
21 static void
22 quit(int rc)
23 {
24 SDL_Quit();
25 exit(rc);
26 }
27
28 static int SDLCALL
29 button_messagebox(void *eventNumber)
30 {
31 const SDL_MessageBoxButtonData buttons[] = {
32 { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
33 0,
34 "OK" },
35 { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
36 1,
37 "Cancel" },
38 };
39
40 SDL_MessageBoxData data = {
41 SDL_MESSAGEBOX_INFORMATION,
42 NULL, /* no parent window */
43 "Custom MessageBox",
44 "This is a custom messagebox",
45 2,
46 NULL, /* buttons */
47 NULL /* Default color scheme */
48 };
49
50 int button = -1;
51 int success = 0;
52 data.buttons = buttons;
53 if (eventNumber) {
54 data.message = "This is a custom messagebox from a background thread.";
55 }
56
57 success = SDL_ShowMessageBox(&data, &button);
58 if (success == -1) {
59 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
60 if (eventNumber) {
61 SDL_Event event;
62 event.type = (Uint32)(intptr_t)eventNumber;
63 SDL_PushEvent(&event);
64 return 1;
65 } else {
66 quit(2);
67 }
68 }
69 SDL_Log("Pressed button: %d, %s\n", button, button == -1 ? "[closed]" : button == 1 ? "Cancel"
70 : "OK");
71
72 if (eventNumber) {
73 SDL_Event event;
74 event.type = (Uint32)(intptr_t)eventNumber;
75 SDL_PushEvent(&event);
76 }
77
78 return 0;
79 }
80
81 int main(int argc, char *argv[])
82 {
83 int success;
84
85 /* Enable standard application logging */
86 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
87
88 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
89 "Simple MessageBox",
90 "This is a simple error MessageBox",
91 NULL);
92 if (success == -1) {
93 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
94 quit(1);
95 }
96
97 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
98 "Simple MessageBox",
99 "This is a simple MessageBox with a newline:\r\nHello world!",
100 NULL);
101 if (success == -1) {
102 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
103 quit(1);
104 }
105
106 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
107 NULL,
108 "NULL Title",
109 NULL);
110 if (success == -1) {
111 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
112 quit(1);
113 }
114
115 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
116 "NULL Message",
117 NULL,
118 NULL);
119 if (success == -1) {
120 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
121 quit(1);
122 }
123
124 /* Google says this is Traditional Chinese for "beef with broccoli" */
125 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
126 "UTF-8 Simple MessageBox",
127 "Unicode text: '牛肉西蘭花' ...",
128 NULL);
129 if (success == -1) {
130 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
131 quit(1);
132 }
133
134 /* Google says this is Traditional Chinese for "beef with broccoli" */
135 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
136 "UTF-8 Simple MessageBox",
137 "Unicode text and newline:\r\n'牛肉西蘭花'\n'牛肉西蘭花'",
138 NULL);
139 if (success == -1) {
140 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
141 quit(1);
142 }
143
144 /* Google says this is Traditional Chinese for "beef with broccoli" */
145 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
146 "牛肉西蘭花",
147 "Unicode text in the title.",
148 NULL);
149 if (success == -1) {
150 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
151 quit(1);
152 }
153
154 button_messagebox(NULL);
155
156 /* Test showing a message box from a background thread.
157
158 On Mac OS X, the video subsystem needs to be initialized for this
159 to work, since the message box events are dispatched by the Cocoa
160 subsystem on the main thread.
161 */
162 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
163 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video subsystem: %s\n", SDL_GetError());
164 return 1;
165 }
166 {
167 int status = 0;
168 SDL_Event event;
169 intptr_t eventNumber = SDL_RegisterEvents(1);
170 SDL_Thread *thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void *)eventNumber);
171
172 while (SDL_WaitEvent(&event)) {
173 if (event.type == eventNumber) {
174 break;
175 }
176 }
177
178 SDL_WaitThread(thread, &status);
179
180 SDL_Log("Message box thread return %i\n", status);
181 }
182
183 /* Test showing a message box with a parent window */
184 {
185 SDL_Event event;
186 SDL_Window *window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
187
188 /* On wayland, no window will actually show until something has
189 actually been displayed.
190 */
191 SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
192 SDL_RenderPresent(renderer);
193
194 success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
195 "Simple MessageBox",
196 "This is a simple error MessageBox with a parent window. Press a key or close the window after dismissing this messagebox.",
197 window);
198 if (success == -1) {
199 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
200 quit(1);
201 }
202
203 while (SDL_WaitEvent(&event)) {
204 if (event.type == SDL_QUIT || event.type == SDL_KEYUP) {
205 break;
206 }
207 }
208 }
209
210 SDL_Quit();
211 return 0;
212 }