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