test/testmessage.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 20 Jul 2013 21:47:16 +0200
changeset 7478 0d02f6a323f5
parent 7458 648067e9073a
child 7517 965d57022c01
permissions -rw-r--r--
Removed not needed SDL_WINDOW_SHOWN from test programs.
See bug #1667.
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 /* Simple test of the SDL MessageBox API*/
    14 
    15 #include <stdio.h>
    16 #include <stdlib.h>
    17 #include <signal.h>
    18 
    19 #include "SDL.h"
    20 #include "SDL_thread.h"
    21 
    22 static int alive = 0;
    23 
    24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    25 static void
    26 quit(int rc)
    27 {
    28     SDL_Quit();
    29     exit(rc);
    30 }
    31 
    32 static int
    33 button_messagebox(void *eventNumber)
    34 {
    35     const SDL_MessageBoxButtonData buttons[] = {
    36         {
    37             SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
    38             0,
    39             "OK"
    40         },{
    41             SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
    42             1,
    43             "Cancel"
    44         },
    45     };
    46 
    47     SDL_MessageBoxData data = {
    48         SDL_MESSAGEBOX_INFORMATION,
    49         NULL, // no parent window
    50         "Custom MessageBox",
    51         "This is a custom messagebox",
    52         2,
    53         buttons,
    54         NULL // Default color scheme
    55     };
    56 
    57     int button = -1;
    58     int success = 0;
    59     if (eventNumber) {
    60         data.message = "This is a custom messagebox from a background thread.";
    61     }
    62 
    63     success = SDL_ShowMessageBox(&data, &button);
    64     if (success == -1) {
    65         printf("Error Presenting MessageBox: %s\n", SDL_GetError());
    66         if (eventNumber) {
    67             SDL_UserEvent event;
    68             event.type = (intptr_t)eventNumber;
    69             SDL_PushEvent((SDL_Event*)&event);
    70             return 1;
    71         } else {
    72             quit(2);
    73         }
    74     }
    75     printf("Pressed button: %d, %s\n", button, button == 1 ? "Cancel" : "OK");
    76 
    77     if (eventNumber) {
    78         SDL_UserEvent event;
    79         event.type = (intptr_t)eventNumber;
    80         SDL_PushEvent((SDL_Event*)&event);
    81     }
    82 
    83     return 0;
    84 }
    85 
    86 int
    87 main(int argc, char *argv[])
    88 {
    89     int success;
    90 
    91     success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
    92                 "Simple MessageBox",
    93                 "This is a simple error MessageBox",
    94                 NULL);
    95     if (success == -1) {
    96         printf("Error Presenting MessageBox: %s\n", SDL_GetError());
    97         quit(1);
    98     }
    99 
   100     success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
   101                 "Simple MessageBox",
   102                 "This is a simple MessageBox with a newline:\r\nHello world!",
   103                 NULL);
   104     if (success == -1) {
   105         printf("Error Presenting MessageBox: %s\n", SDL_GetError());
   106         quit(1);
   107     }
   108 
   109     /* Google says this is Traditional Chinese for "beef with broccoli" */
   110     success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
   111                 "UTF-8 Simple MessageBox",
   112                 "Unicode text: '牛肉西蘭花' ...",
   113                 NULL);
   114     if (success == -1) {
   115         printf("Error Presenting MessageBox: %s\n", SDL_GetError());
   116         quit(1);
   117     }
   118 
   119     /* Google says this is Traditional Chinese for "beef with broccoli" */
   120     success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
   121                 "UTF-8 Simple MessageBox",
   122                 "Unicode text and newline:\r\n'牛肉西蘭花'\n'牛肉西蘭花'",
   123                 NULL);
   124     if (success == -1) {
   125         printf("Error Presenting MessageBox: %s\n", SDL_GetError());
   126         quit(1);
   127     }
   128 
   129     button_messagebox(NULL);
   130 
   131     /* Test showing a message box from a background thread.
   132 
   133        On Mac OS X, the video subsystem needs to be initialized for this
   134        to work, since the message box events are dispatched by the Cocoa
   135        subsystem on the main thread.
   136      */
   137     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   138         fprintf(stderr, "Couldn't initialize SDL video subsystem: %s\n", SDL_GetError());
   139         return (1);
   140     }
   141     {
   142         int status = 0;
   143         SDL_Event event;
   144         intptr_t eventNumber = SDL_RegisterEvents(1);
   145         SDL_Thread* thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void*)eventNumber);
   146 
   147         while (SDL_WaitEvent(&event))
   148         {
   149             if (event.type == eventNumber) {
   150                 break;
   151             }
   152         }
   153 
   154         SDL_WaitThread(thread, &status);
   155 
   156         printf("Message box thread return %i\n", status);
   157     }
   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 
   164         success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
   165                     "Simple MessageBox",
   166                     "This is a simple error MessageBox with a parent window",
   167                     window);
   168         if (success == -1) {
   169             printf("Error Presenting MessageBox: %s\n", SDL_GetError());
   170             quit(1);
   171         }
   172 
   173         while (SDL_WaitEvent(&event))
   174         {
   175             if (event.type == SDL_QUIT || event.type == SDL_KEYUP) {
   176                 break;
   177             }
   178         }
   179     }
   180 
   181     SDL_Quit();
   182     return (0);
   183 }