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