slouken@6614
|
1 |
/*
|
slouken@7517
|
2 |
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
|
slouken@6614
|
3 |
|
slouken@6614
|
4 |
This software is provided 'as-is', without any express or implied
|
slouken@6614
|
5 |
warranty. In no event will the authors be held liable for any damages
|
slouken@6614
|
6 |
arising from the use of this software.
|
slouken@6614
|
7 |
|
slouken@6614
|
8 |
Permission is granted to anyone to use this software for any purpose,
|
slouken@6614
|
9 |
including commercial applications, and to alter it and redistribute it
|
slouken@6614
|
10 |
freely.
|
slouken@6614
|
11 |
*/
|
slouken@6614
|
12 |
|
slouken@6614
|
13 |
/* Simple test of the SDL MessageBox API*/
|
slouken@6614
|
14 |
|
slouken@6614
|
15 |
#include <stdio.h>
|
slouken@6614
|
16 |
#include <stdlib.h>
|
slouken@6614
|
17 |
#include <signal.h>
|
slouken@6614
|
18 |
|
slouken@6614
|
19 |
#include "SDL.h"
|
slouken@6614
|
20 |
#include "SDL_thread.h"
|
slouken@6614
|
21 |
|
slouken@6614
|
22 |
static int alive = 0;
|
slouken@6614
|
23 |
|
slouken@6614
|
24 |
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
slouken@6614
|
25 |
static void
|
slouken@6614
|
26 |
quit(int rc)
|
slouken@6614
|
27 |
{
|
slouken@6614
|
28 |
SDL_Quit();
|
slouken@6614
|
29 |
exit(rc);
|
slouken@6614
|
30 |
}
|
slouken@6614
|
31 |
|
jorgen@7088
|
32 |
static int
|
jorgen@7088
|
33 |
button_messagebox(void *eventNumber)
|
jorgen@7088
|
34 |
{
|
jorgen@7088
|
35 |
const SDL_MessageBoxButtonData buttons[] = {
|
jorgen@7088
|
36 |
{
|
jorgen@7088
|
37 |
SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
|
jorgen@7088
|
38 |
0,
|
jorgen@7088
|
39 |
"OK"
|
jorgen@7088
|
40 |
},{
|
jorgen@7088
|
41 |
SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
|
jorgen@7088
|
42 |
1,
|
jorgen@7088
|
43 |
"Cancel"
|
jorgen@7088
|
44 |
},
|
jorgen@7088
|
45 |
};
|
jorgen@7088
|
46 |
|
jorgen@7088
|
47 |
SDL_MessageBoxData data = {
|
jorgen@7088
|
48 |
SDL_MESSAGEBOX_INFORMATION,
|
jorgen@7088
|
49 |
NULL, // no parent window
|
jorgen@7088
|
50 |
"Custom MessageBox",
|
jorgen@7088
|
51 |
"This is a custom messagebox",
|
jorgen@7088
|
52 |
2,
|
jorgen@7088
|
53 |
buttons,
|
jorgen@7088
|
54 |
NULL // Default color scheme
|
jorgen@7088
|
55 |
};
|
jorgen@7088
|
56 |
|
jorgen@7088
|
57 |
int button = -1;
|
jorgen@7088
|
58 |
int success = 0;
|
jorgen@7088
|
59 |
if (eventNumber) {
|
jorgen@7088
|
60 |
data.message = "This is a custom messagebox from a background thread.";
|
jorgen@7088
|
61 |
}
|
jorgen@7088
|
62 |
|
slouken@7458
|
63 |
success = SDL_ShowMessageBox(&data, &button);
|
jorgen@7088
|
64 |
if (success == -1) {
|
aschiffler@7639
|
65 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
|
jorgen@7088
|
66 |
if (eventNumber) {
|
jorgen@7088
|
67 |
SDL_UserEvent event;
|
jorgen@7088
|
68 |
event.type = (intptr_t)eventNumber;
|
jorgen@7088
|
69 |
SDL_PushEvent((SDL_Event*)&event);
|
jorgen@7088
|
70 |
return 1;
|
jorgen@7088
|
71 |
} else {
|
jorgen@7088
|
72 |
quit(2);
|
jorgen@7088
|
73 |
}
|
jorgen@7088
|
74 |
}
|
aschiffler@7639
|
75 |
SDL_Log("Pressed button: %d, %s\n", button, button == 1 ? "Cancel" : "OK");
|
jorgen@7088
|
76 |
|
jorgen@7088
|
77 |
if (eventNumber) {
|
jorgen@7088
|
78 |
SDL_UserEvent event;
|
jorgen@7088
|
79 |
event.type = (intptr_t)eventNumber;
|
jorgen@7088
|
80 |
SDL_PushEvent((SDL_Event*)&event);
|
jorgen@7088
|
81 |
}
|
jorgen@7088
|
82 |
|
jorgen@7088
|
83 |
return 0;
|
jorgen@7088
|
84 |
}
|
jorgen@7088
|
85 |
|
slouken@6614
|
86 |
int
|
slouken@6614
|
87 |
main(int argc, char *argv[])
|
slouken@6614
|
88 |
{
|
slouken@6614
|
89 |
int success;
|
slouken@6614
|
90 |
|
aschiffler@7639
|
91 |
/* Enable standard application logging */
|
aschiffler@7639
|
92 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
aschiffler@7639
|
93 |
|
slouken@6614
|
94 |
success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
slouken@6614
|
95 |
"Simple MessageBox",
|
slouken@6614
|
96 |
"This is a simple error MessageBox",
|
slouken@6614
|
97 |
NULL);
|
slouken@6614
|
98 |
if (success == -1) {
|
aschiffler@7639
|
99 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
|
slouken@6614
|
100 |
quit(1);
|
slouken@6614
|
101 |
}
|
slouken@6614
|
102 |
|
icculus@6758
|
103 |
success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
icculus@6758
|
104 |
"Simple MessageBox",
|
icculus@6758
|
105 |
"This is a simple MessageBox with a newline:\r\nHello world!",
|
icculus@6758
|
106 |
NULL);
|
icculus@6758
|
107 |
if (success == -1) {
|
aschiffler@7639
|
108 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
|
icculus@6758
|
109 |
quit(1);
|
icculus@6758
|
110 |
}
|
icculus@6758
|
111 |
|
icculus@6722
|
112 |
/* Google says this is Traditional Chinese for "beef with broccoli" */
|
icculus@6722
|
113 |
success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
icculus@6722
|
114 |
"UTF-8 Simple MessageBox",
|
icculus@6722
|
115 |
"Unicode text: '牛肉西蘭花' ...",
|
icculus@6722
|
116 |
NULL);
|
icculus@6722
|
117 |
if (success == -1) {
|
aschiffler@7639
|
118 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
|
icculus@6722
|
119 |
quit(1);
|
icculus@6722
|
120 |
}
|
icculus@6722
|
121 |
|
icculus@6758
|
122 |
/* Google says this is Traditional Chinese for "beef with broccoli" */
|
icculus@6758
|
123 |
success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
icculus@6758
|
124 |
"UTF-8 Simple MessageBox",
|
icculus@6758
|
125 |
"Unicode text and newline:\r\n'牛肉西蘭花'\n'牛肉西蘭花'",
|
icculus@6758
|
126 |
NULL);
|
icculus@6758
|
127 |
if (success == -1) {
|
aschiffler@7639
|
128 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
|
icculus@6758
|
129 |
quit(1);
|
icculus@6758
|
130 |
}
|
icculus@6758
|
131 |
|
jorgen@7088
|
132 |
button_messagebox(NULL);
|
jorgen@7088
|
133 |
|
slouken@7458
|
134 |
/* Test showing a message box from a background thread.
|
slouken@7458
|
135 |
|
slouken@7458
|
136 |
On Mac OS X, the video subsystem needs to be initialized for this
|
slouken@7458
|
137 |
to work, since the message box events are dispatched by the Cocoa
|
slouken@7458
|
138 |
subsystem on the main thread.
|
slouken@7458
|
139 |
*/
|
slouken@7458
|
140 |
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
aschiffler@7639
|
141 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video subsystem: %s\n", SDL_GetError());
|
slouken@7458
|
142 |
return (1);
|
slouken@7458
|
143 |
}
|
slouken@6614
|
144 |
{
|
jorgen@7088
|
145 |
int status = 0;
|
jorgen@7088
|
146 |
SDL_Event event;
|
jorgen@7088
|
147 |
intptr_t eventNumber = SDL_RegisterEvents(1);
|
jorgen@7088
|
148 |
SDL_Thread* thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void*)eventNumber);
|
slouken@6614
|
149 |
|
jorgen@7088
|
150 |
while (SDL_WaitEvent(&event))
|
jorgen@7088
|
151 |
{
|
jorgen@7088
|
152 |
if (event.type == eventNumber) {
|
jorgen@7088
|
153 |
break;
|
jorgen@7088
|
154 |
}
|
jorgen@7088
|
155 |
}
|
slouken@6614
|
156 |
|
jorgen@7088
|
157 |
SDL_WaitThread(thread, &status);
|
jorgen@7088
|
158 |
|
aschiffler@7639
|
159 |
SDL_Log("Message box thread return %i\n", status);
|
slouken@6614
|
160 |
}
|
slouken@7456
|
161 |
|
slouken@7456
|
162 |
/* Test showing a message box with a parent window */
|
slouken@7456
|
163 |
{
|
slouken@7456
|
164 |
SDL_Event event;
|
philipp@7478
|
165 |
SDL_Window *window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
|
slouken@7456
|
166 |
|
slouken@7456
|
167 |
success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
slouken@7456
|
168 |
"Simple MessageBox",
|
slouken@7456
|
169 |
"This is a simple error MessageBox with a parent window",
|
slouken@7456
|
170 |
window);
|
slouken@7456
|
171 |
if (success == -1) {
|
aschiffler@7639
|
172 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
|
slouken@7456
|
173 |
quit(1);
|
slouken@7456
|
174 |
}
|
slouken@7456
|
175 |
|
slouken@7456
|
176 |
while (SDL_WaitEvent(&event))
|
slouken@7456
|
177 |
{
|
slouken@7456
|
178 |
if (event.type == SDL_QUIT || event.type == SDL_KEYUP) {
|
slouken@7456
|
179 |
break;
|
slouken@7456
|
180 |
}
|
slouken@7456
|
181 |
}
|
slouken@7456
|
182 |
}
|
slouken@6614
|
183 |
|
slouken@6614
|
184 |
SDL_Quit();
|
slouken@6614
|
185 |
return (0);
|
slouken@6614
|
186 |
}
|