SDL  2.0
testmessage.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
+ Include dependency graph for testmessage.c:

Go to the source code of this file.

Functions

static void quit (int rc)
 
static int button_messagebox (void *eventNumber)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ button_messagebox()

static int button_messagebox ( void eventNumber)
static

Definition at line 29 of file testmessage.c.

30 {
31  const SDL_MessageBoxButtonData buttons[] = {
32  {
34  0,
35  "OK"
36  },{
38  1,
39  "Cancel"
40  },
41  };
42 
45  NULL, /* no parent window */
46  "Custom MessageBox",
47  "This is a custom messagebox",
48  2,
49  NULL,/* buttons */
50  NULL /* Default color scheme */
51  };
52 
53  int button = -1;
54  int success = 0;
55  data.buttons = buttons;
56  if (eventNumber) {
57  data.message = "This is a custom messagebox from a background thread.";
58  }
59 
60  success = SDL_ShowMessageBox(&data, &button);
61  if (success == -1) {
62  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
63  if (eventNumber) {
65  event.type = (intptr_t)eventNumber;
67  return 1;
68  } else {
69  quit(2);
70  }
71  }
72  SDL_Log("Pressed button: %d, %s\n", button, button == -1 ? "[closed]" : button == 1 ? "Cancel" : "OK");
73 
74  if (eventNumber) {
76  event.type = (intptr_t)eventNumber;
78  }
79 
80  return 0;
81 }

References button, NULL, quit(), SDL_GetError, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LogError, SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, SDL_MESSAGEBOX_INFORMATION, SDL_PushEvent, and SDL_ShowMessageBox.

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 84 of file testmessage.c.

85 {
86  int success;
87 
88  /* Enable standard application logging */
90 
92  "Simple MessageBox",
93  "This is a simple error MessageBox",
94  NULL);
95  if (success == -1) {
96  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
97  quit(1);
98  }
99 
101  "Simple MessageBox",
102  "This is a simple MessageBox with a newline:\r\nHello world!",
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 
109  /* Google says this is Traditional Chinese for "beef with broccoli" */
111  "UTF-8 Simple MessageBox",
112  "Unicode text: '牛肉西蘭花' ...",
113  NULL);
114  if (success == -1) {
115  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
116  quit(1);
117  }
118 
119  /* Google says this is Traditional Chinese for "beef with broccoli" */
121  "UTF-8 Simple MessageBox",
122  "Unicode text and newline:\r\n'牛肉西蘭花'\n'牛肉西蘭花'",
123  NULL);
124  if (success == -1) {
125  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
126  quit(1);
127  }
128 
129  /* Google says this is Traditional Chinese for "beef with broccoli" */
131  "牛肉西蘭花",
132  "Unicode text in the title.",
133  NULL);
134  if (success == -1) {
135  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
136  quit(1);
137  }
138 
140 
141  /* Test showing a message box from a background thread.
142 
143  On Mac OS X, the video subsystem needs to be initialized for this
144  to work, since the message box events are dispatched by the Cocoa
145  subsystem on the main thread.
146  */
147  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
148  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video subsystem: %s\n", SDL_GetError());
149  return (1);
150  }
151  {
152  int status = 0;
154  intptr_t eventNumber = SDL_RegisterEvents(1);
155  SDL_Thread* thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void*)eventNumber);
156 
157  while (SDL_WaitEvent(&event))
158  {
159  if (event.type == eventNumber) {
160  break;
161  }
162  }
163 
164  SDL_WaitThread(thread, &status);
165 
166  SDL_Log("Message box thread return %i\n", status);
167  }
168 
169  /* Test showing a message box with a parent window */
170  {
173 
175  "Simple MessageBox",
176  "This is a simple error MessageBox with a parent window",
177  window);
178  if (success == -1) {
179  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
180  quit(1);
181  }
182 
183  while (SDL_WaitEvent(&event))
184  {
185  if (event.type == SDL_QUIT || event.type == SDL_KEYUP) {
186  break;
187  }
188  }
189  }
190 
191  SDL_Quit();
192  return (0);
193 }

References button_messagebox(), NULL, quit(), SDL_CreateThread, SDL_CreateWindow, SDL_GetError, SDL_Init, SDL_INIT_VIDEO, SDL_KEYUP, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_MESSAGEBOX_ERROR, SDL_QUIT, SDL_Quit, SDL_RegisterEvents, SDL_ShowSimpleMessageBox, SDL_WaitEvent, SDL_WaitThread, and SDL_WINDOWPOS_CENTERED.

◆ quit()

static void quit ( int  rc)
static

Definition at line 22 of file testmessage.c.

23 {
24  SDL_Quit();
25  exit(rc);
26 }

References SDL_Quit.

Referenced by button_messagebox(), and main().

SDL_GetError
#define SDL_GetError
Definition: SDL_dynapi_overrides.h:113
SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT
@ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT
Definition: SDL_messagebox.h:50
SDL_WINDOWPOS_CENTERED
#define SDL_WINDOWPOS_CENTERED
Definition: SDL_video.h:139
SDL_LOG_CATEGORY_APPLICATION
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
SDL_UserEvent
A user-defined event type (event.user.*)
Definition: SDL_events.h:528
NULL
#define NULL
Definition: begin_code.h:164
SDL_KEYUP
@ SDL_KEYUP
Definition: SDL_events.h:97
button_messagebox
static int button_messagebox(void *eventNumber)
Definition: testmessage.c:29
SDL_Thread
Definition: SDL_thread_c.h:55
SDL_CreateWindow
#define SDL_CreateWindow
Definition: SDL_dynapi_overrides.h:514
SDL_RegisterEvents
#define SDL_RegisterEvents
Definition: SDL_dynapi_overrides.h:132
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:74
SDL_ShowSimpleMessageBox
#define SDL_ShowSimpleMessageBox
Definition: SDL_dynapi_overrides.h:244
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_PushEvent
#define SDL_PushEvent
Definition: SDL_dynapi_overrides.h:125
SDL_QUIT
@ SDL_QUIT
Definition: SDL_events.h:60
SDL_MessageBoxData
MessageBox structure containing title, text, window, etc.
Definition: SDL_messagebox.h:93
SDL_Quit
#define SDL_Quit
Definition: SDL_dynapi_overrides.h:58
SDL_MESSAGEBOX_ERROR
@ SDL_MESSAGEBOX_ERROR
Definition: SDL_messagebox.h:39
SDL_MessageBoxButtonData
Individual button data.
Definition: SDL_messagebox.h:57
SDL_CreateThread
#define SDL_CreateThread
Definition: SDL_dynapi_overrides.h:41
SDL_MESSAGEBOX_INFORMATION
@ SDL_MESSAGEBOX_INFORMATION
Definition: SDL_messagebox.h:41
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:79
SDL_LOG_PRIORITY_INFO
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT
@ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT
Definition: SDL_messagebox.h:49
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
SDL_ShowMessageBox
#define SDL_ShowMessageBox
Definition: SDL_dynapi_overrides.h:243
SDL_WaitEvent
#define SDL_WaitEvent
Definition: SDL_dynapi_overrides.h:123
SDL_Event
General event structure.
Definition: SDL_events.h:558
quit
static void quit(int rc)
Definition: testmessage.c:22
SDL_Init
#define SDL_Init
Definition: SDL_dynapi_overrides.h:54
SDL_WaitThread
#define SDL_WaitThread
Definition: SDL_dynapi_overrides.h:478
button
SDL_Texture * button
Definition: testgamecontroller.c:67