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 }
#define SDL_GetError
#define SDL_PushEvent
#define SDL_LogError
#define SDL_ShowMessageBox
#define SDL_Log
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
@ SDL_MESSAGEBOX_INFORMATION
@ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT
@ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
struct _cl_event * event
#define NULL
Definition: begin_code.h:163
Individual button data.
MessageBox structure containing title, text, window, etc.
A user-defined event type (event.user.*)
Definition: SDL_events.h:562
SDL_Texture * button
static void quit(int rc)
Definition: testmessage.c:22
General event structure.
Definition: SDL_events.h:592

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 
110  NULL,
111  "NULL Title",
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 
119  "NULL Message",
120  NULL,
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 
127  /* Google says this is Traditional Chinese for "beef with broccoli" */
129  "UTF-8 Simple MessageBox",
130  "Unicode text: '牛肉西蘭花' ...",
131  NULL);
132  if (success == -1) {
133  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
134  quit(1);
135  }
136 
137  /* Google says this is Traditional Chinese for "beef with broccoli" */
139  "UTF-8 Simple MessageBox",
140  "Unicode text and newline:\r\n'牛肉西蘭花'\n'牛肉西蘭花'",
141  NULL);
142  if (success == -1) {
143  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
144  quit(1);
145  }
146 
147  /* Google says this is Traditional Chinese for "beef with broccoli" */
149  "牛肉西蘭花",
150  "Unicode text in the title.",
151  NULL);
152  if (success == -1) {
153  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
154  quit(1);
155  }
156 
158 
159  /* Test showing a message box from a background thread.
160 
161  On Mac OS X, the video subsystem needs to be initialized for this
162  to work, since the message box events are dispatched by the Cocoa
163  subsystem on the main thread.
164  */
165  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
166  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video subsystem: %s\n", SDL_GetError());
167  return (1);
168  }
169  {
170  int status = 0;
172  intptr_t eventNumber = SDL_RegisterEvents(1);
173  SDL_Thread* thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void*)eventNumber);
174 
175  while (SDL_WaitEvent(&event))
176  {
177  if (event.type == eventNumber) {
178  break;
179  }
180  }
181 
182  SDL_WaitThread(thread, &status);
183 
184  SDL_Log("Message box thread return %i\n", status);
185  }
186 
187  /* Test showing a message box with a parent window */
188  {
191 
193  "Simple MessageBox",
194  "This is a simple error MessageBox with a parent window",
195  window);
196  if (success == -1) {
197  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
198  quit(1);
199  }
200 
201  while (SDL_WaitEvent(&event))
202  {
203  if (event.type == SDL_QUIT || event.type == SDL_KEYUP) {
204  break;
205  }
206  }
207  }
208 
209  SDL_Quit();
210  return (0);
211 }
#define SDL_INIT_VIDEO
Definition: SDL.h:82
#define SDL_CreateWindow
#define SDL_RegisterEvents
#define SDL_LogSetPriority
#define SDL_ShowSimpleMessageBox
#define SDL_WaitThread
#define SDL_Quit
#define SDL_Init
#define SDL_WaitEvent
@ SDL_QUIT
Definition: SDL_events.h:60
@ SDL_KEYUP
Definition: SDL_events.h:99
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
@ SDL_MESSAGEBOX_ERROR
#define SDL_CreateThread(fn, name, data)
Definition: SDL_thread.h:137
#define SDL_WINDOWPOS_CENTERED
Definition: SDL_video.h:139
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
The type used to identify a window.
Definition: SDL_sysvideo.h:75
static int button_messagebox(void *eventNumber)
Definition: testmessage.c:29

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().