test/testmessage.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 7677 871d43c6968a
child 8692 715d401184a2
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

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