🛠️🐜 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.

217 lines
6.6 KiB

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