testmessage.c 5.4 KB

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