🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
7.2 KiB

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