2015-06-21 18:33:46 +03:00
|
|
|
/*
|
2024-01-02 00:15:26 +03:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Simple test of the SDL MessageBox API */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-11-27 07:43:38 +03:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-15 07:58:20 +03:00
|
|
|
#include <SDL3/SDL_main.h>
|
2023-03-17 02:25:39 +03:00
|
|
|
#include <SDL3/SDL_test.h>
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
|
|
|
static void
|
|
|
|
quit(int rc)
|
|
|
|
{
|
|
|
|
SDL_Quit();
|
2023-04-12 12:07:28 +03:00
|
|
|
/* Let 'main()' return normally */
|
|
|
|
if (rc != 0) {
|
|
|
|
exit(rc);
|
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
2017-08-14 07:06:52 +03:00
|
|
|
static int SDLCALL
|
2015-06-21 18:33:46 +03:00
|
|
|
button_messagebox(void *eventNumber)
|
|
|
|
{
|
|
|
|
const SDL_MessageBoxButtonData buttons[] = {
|
2022-11-30 23:51:59 +03:00
|
|
|
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
|
|
|
|
0,
|
|
|
|
"OK" },
|
|
|
|
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
|
|
|
|
1,
|
|
|
|
"Cancel" },
|
2015-06-21 18:33:46 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
SDL_MessageBoxData data = {
|
|
|
|
SDL_MESSAGEBOX_INFORMATION,
|
|
|
|
NULL, /* no parent window */
|
|
|
|
"Custom MessageBox",
|
|
|
|
"This is a custom messagebox",
|
|
|
|
2,
|
2022-11-30 23:51:59 +03:00
|
|
|
NULL, /* buttons */
|
|
|
|
NULL /* Default color scheme */
|
2015-06-21 18:33:46 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
int button = -1;
|
|
|
|
int success = 0;
|
2017-08-14 07:15:44 +03:00
|
|
|
data.buttons = buttons;
|
2015-06-21 18:33:46 +03:00
|
|
|
if (eventNumber) {
|
|
|
|
data.message = "This is a custom messagebox from a background thread.";
|
|
|
|
}
|
|
|
|
|
|
|
|
success = SDL_ShowMessageBox(&data, &button);
|
|
|
|
if (success == -1) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
|
|
|
|
if (eventNumber) {
|
2021-11-13 22:05:02 +03:00
|
|
|
SDL_Event event;
|
2022-10-06 02:15:49 +03:00
|
|
|
event.type = (Uint32)(intptr_t)eventNumber;
|
2021-11-13 22:05:02 +03:00
|
|
|
SDL_PushEvent(&event);
|
2015-06-21 18:33:46 +03:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 23:51:59 +03:00
|
|
|
SDL_Log("Pressed button: %d, %s\n", button, button == -1 ? "[closed]" : button == 1 ? "Cancel"
|
|
|
|
: "OK");
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
if (eventNumber) {
|
2021-11-13 22:05:02 +03:00
|
|
|
SDL_Event event;
|
2022-10-06 02:15:49 +03:00
|
|
|
event.type = (Uint32)(intptr_t)eventNumber;
|
2021-11-13 22:05:02 +03:00
|
|
|
SDL_PushEvent(&event);
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
int main(int argc, char *argv[])
|
2015-06-21 18:33:46 +03:00
|
|
|
{
|
|
|
|
int success;
|
2023-03-17 02:25:39 +03:00
|
|
|
SDLTest_CommonState *state;
|
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, 0);
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!state) {
|
2023-03-17 02:25:39 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2023-03-17 02:25:39 +03:00
|
|
|
/* Parse commandline */
|
|
|
|
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
2022-11-30 23:51:59 +03:00
|
|
|
"Simple MessageBox",
|
|
|
|
"This is a simple error MessageBox",
|
|
|
|
NULL);
|
2024-08-23 03:33:49 +03:00
|
|
|
if (!success) {
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
|
|
|
|
quit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
2022-11-30 23:51:59 +03:00
|
|
|
"Simple MessageBox",
|
|
|
|
"This is a simple MessageBox with a newline:\r\nHello world!",
|
|
|
|
NULL);
|
2024-08-23 03:33:49 +03:00
|
|
|
if (!success) {
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
|
2020-12-10 11:20:56 +03:00
|
|
|
quit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
2022-11-30 23:51:59 +03:00
|
|
|
NULL,
|
|
|
|
"NULL Title",
|
|
|
|
NULL);
|
2024-08-23 03:33:49 +03:00
|
|
|
if (!success) {
|
2020-12-10 11:20:56 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
|
|
|
|
quit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
2022-11-30 23:51:59 +03:00
|
|
|
"NULL Message",
|
|
|
|
NULL,
|
|
|
|
NULL);
|
2024-08-23 03:33:49 +03:00
|
|
|
if (!success) {
|
2020-12-10 11:20:56 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
|
2015-06-21 18:33:46 +03:00
|
|
|
quit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Google says this is Traditional Chinese for "beef with broccoli" */
|
|
|
|
success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
2022-11-30 23:51:59 +03:00
|
|
|
"UTF-8 Simple MessageBox",
|
|
|
|
"Unicode text: '牛肉西蘭花' ...",
|
|
|
|
NULL);
|
2024-08-23 03:33:49 +03:00
|
|
|
if (!success) {
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
|
|
|
|
quit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Google says this is Traditional Chinese for "beef with broccoli" */
|
|
|
|
success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
2022-11-30 23:51:59 +03:00
|
|
|
"UTF-8 Simple MessageBox",
|
|
|
|
"Unicode text and newline:\r\n'牛肉西蘭花'\n'牛肉西蘭花'",
|
|
|
|
NULL);
|
2024-08-23 03:33:49 +03:00
|
|
|
if (!success) {
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
|
|
|
|
quit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Google says this is Traditional Chinese for "beef with broccoli" */
|
|
|
|
success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
2022-11-30 23:51:59 +03:00
|
|
|
"牛肉西蘭花",
|
|
|
|
"Unicode text in the title.",
|
|
|
|
NULL);
|
2024-08-23 03:33:49 +03:00
|
|
|
if (!success) {
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
|
|
|
|
quit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
button_messagebox(NULL);
|
|
|
|
|
|
|
|
/* Test showing a message box from a background thread.
|
|
|
|
|
2022-11-26 03:00:06 +03:00
|
|
|
On macOS, the video subsystem needs to be initialized for this
|
2015-06-21 18:33:46 +03:00
|
|
|
to work, since the message box events are dispatched by the Cocoa
|
|
|
|
subsystem on the main thread.
|
|
|
|
*/
|
2024-08-23 03:33:49 +03:00
|
|
|
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video subsystem: %s\n", SDL_GetError());
|
2022-11-27 19:38:43 +03:00
|
|
|
return 1;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
{
|
|
|
|
int status = 0;
|
|
|
|
SDL_Event event;
|
2024-03-06 20:51:05 +03:00
|
|
|
Uint32 eventNumber = SDL_RegisterEvents(1);
|
2024-03-06 20:57:27 +03:00
|
|
|
SDL_Thread *thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void *)(uintptr_t)eventNumber);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2022-11-27 19:38:43 +03:00
|
|
|
while (SDL_WaitEvent(&event)) {
|
2015-06-21 18:33:46 +03:00
|
|
|
if (event.type == eventNumber) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_WaitThread(thread, &status);
|
|
|
|
|
|
|
|
SDL_Log("Message box thread return %i\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test showing a message box with a parent window */
|
|
|
|
{
|
|
|
|
SDL_Event event;
|
2023-03-06 01:44:38 +03:00
|
|
|
SDL_Window *window = SDL_CreateWindow("Test", 640, 480, 0);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2021-03-29 13:12:33 +03:00
|
|
|
/* On wayland, no window will actually show until something has
|
|
|
|
actually been displayed.
|
|
|
|
*/
|
2024-05-13 20:31:55 +03:00
|
|
|
SDL_Renderer *renderer = SDL_CreateRenderer(window, NULL);
|
2021-03-29 13:12:33 +03:00
|
|
|
SDL_RenderPresent(renderer);
|
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
2022-11-30 23:51:59 +03:00
|
|
|
"Simple MessageBox",
|
|
|
|
"This is a simple error MessageBox with a parent window. Press a key or close the window after dismissing this messagebox.",
|
|
|
|
window);
|
2024-08-23 03:33:49 +03:00
|
|
|
if (!success) {
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
|
|
|
|
quit(1);
|
|
|
|
}
|
|
|
|
|
2022-11-27 19:38:43 +03:00
|
|
|
while (SDL_WaitEvent(&event)) {
|
2023-01-24 04:54:09 +03:00
|
|
|
if (event.type == SDL_EVENT_QUIT || event.type == SDL_EVENT_KEY_UP) {
|
2015-06-21 18:33:46 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Quit();
|
2023-03-17 02:25:39 +03:00
|
|
|
SDLTest_CommonDestroyState(state);
|
2022-11-27 19:38:43 +03:00
|
|
|
return 0;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|