test/testmessage.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 14 Jul 2013 11:57:45 -0700
changeset 7456 193c9a1723ad
parent 7088 664d8532219b
child 7458 648067e9073a
permissions -rw-r--r--
Added testing of messagebox with a parent window
     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     /* Load the SDL library */
    92     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    93         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    94         return (1);
    95     }
    96 
    97 #if 0
    98     success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
    99                 "Simple MessageBox",
   100                 "This is a simple error MessageBox",
   101                 NULL);
   102     if (success == -1) {
   103         printf("Error Presenting MessageBox: %s\n", SDL_GetError());
   104         quit(1);
   105     }
   106 
   107     success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
   108                 "Simple MessageBox",
   109                 "This is a simple MessageBox with a newline:\r\nHello world!",
   110                 NULL);
   111     if (success == -1) {
   112         printf("Error Presenting MessageBox: %s\n", SDL_GetError());
   113         quit(1);
   114     }
   115 
   116     /* Google says this is Traditional Chinese for "beef with broccoli" */
   117     success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
   118                 "UTF-8 Simple MessageBox",
   119                 "Unicode text: '牛肉西蘭花' ...",
   120                 NULL);
   121     if (success == -1) {
   122         printf("Error Presenting MessageBox: %s\n", SDL_GetError());
   123         quit(1);
   124     }
   125 
   126     /* Google says this is Traditional Chinese for "beef with broccoli" */
   127     success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
   128                 "UTF-8 Simple MessageBox",
   129                 "Unicode text and newline:\r\n'牛肉西蘭花'\n'牛肉西蘭花'",
   130                 NULL);
   131     if (success == -1) {
   132         printf("Error Presenting MessageBox: %s\n", SDL_GetError());
   133         quit(1);
   134     }
   135 
   136     button_messagebox(NULL);
   137 
   138     /* Test showing a message box from a background thread */
   139     {
   140         int status = 0;
   141         SDL_Event event;
   142         intptr_t eventNumber = SDL_RegisterEvents(1);
   143         SDL_Thread* thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void*)eventNumber);
   144 
   145         while (SDL_WaitEvent(&event))
   146         {
   147             if (event.type == eventNumber) {
   148                 break;
   149             }
   150         }
   151 
   152         SDL_WaitThread(thread, &status);
   153 
   154         printf("Message box thread return %i\n", status);
   155     }
   156 #endif
   157 
   158     /* Test showing a message box with a parent window */
   159     {
   160         SDL_Event event;
   161         SDL_Window *window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
   162 
   163         success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
   164                     "Simple MessageBox",
   165                     "This is a simple error MessageBox with a parent window",
   166                     window);
   167         if (success == -1) {
   168             printf("Error Presenting MessageBox: %s\n", SDL_GetError());
   169             quit(1);
   170         }
   171 
   172         while (SDL_WaitEvent(&event))
   173         {
   174             if (event.type == SDL_QUIT || event.type == SDL_KEYUP) {
   175                 break;
   176             }
   177         }
   178     }
   179 
   180     SDL_Quit();
   181     return (0);
   182 }