test/testmessage.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 19 Feb 2015 23:52:10 -0500
changeset 9370 795d4866e4db
parent 9356 e87d6e1e812a
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Mac OS X: Better way to stop duplicate joystick reports (thanks, Kyungdahm!).

Fixes Bugzilla #2822.
slouken@6614
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 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
gabomdq@7677
    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
slouken@6614
    21
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@6614
    22
static void
slouken@6614
    23
quit(int rc)
slouken@6614
    24
{
slouken@6614
    25
    SDL_Quit();
slouken@6614
    26
    exit(rc);
slouken@6614
    27
}
slouken@6614
    28
jorgen@7088
    29
static int
jorgen@7088
    30
button_messagebox(void *eventNumber)
jorgen@7088
    31
{
jorgen@7088
    32
    const SDL_MessageBoxButtonData buttons[] = {
jorgen@7088
    33
        {
jorgen@7088
    34
            SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
jorgen@7088
    35
            0,
jorgen@7088
    36
            "OK"
jorgen@7088
    37
        },{
jorgen@7088
    38
            SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
jorgen@7088
    39
            1,
jorgen@7088
    40
            "Cancel"
jorgen@7088
    41
        },
jorgen@7088
    42
    };
jorgen@7088
    43
jorgen@7088
    44
    SDL_MessageBoxData data = {
jorgen@7088
    45
        SDL_MESSAGEBOX_INFORMATION,
gabomdq@7663
    46
        NULL, /* no parent window */
jorgen@7088
    47
        "Custom MessageBox",
jorgen@7088
    48
        "This is a custom messagebox",
jorgen@7088
    49
        2,
jorgen@7088
    50
        buttons,
gabomdq@7663
    51
        NULL /* Default color scheme */
jorgen@7088
    52
    };
jorgen@7088
    53
jorgen@7088
    54
    int button = -1;
jorgen@7088
    55
    int success = 0;
jorgen@7088
    56
    if (eventNumber) {
jorgen@7088
    57
        data.message = "This is a custom messagebox from a background thread.";
jorgen@7088
    58
    }
jorgen@7088
    59
slouken@7458
    60
    success = SDL_ShowMessageBox(&data, &button);
jorgen@7088
    61
    if (success == -1) {
aschiffler@7639
    62
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
jorgen@7088
    63
        if (eventNumber) {
jorgen@7088
    64
            SDL_UserEvent event;
jorgen@7088
    65
            event.type = (intptr_t)eventNumber;
jorgen@7088
    66
            SDL_PushEvent((SDL_Event*)&event);
jorgen@7088
    67
            return 1;
jorgen@7088
    68
        } else {
jorgen@7088
    69
            quit(2);
jorgen@7088
    70
        }
jorgen@7088
    71
    }
aschiffler@7639
    72
    SDL_Log("Pressed button: %d, %s\n", button, button == 1 ? "Cancel" : "OK");
jorgen@7088
    73
jorgen@7088
    74
    if (eventNumber) {
jorgen@7088
    75
        SDL_UserEvent event;
jorgen@7088
    76
        event.type = (intptr_t)eventNumber;
jorgen@7088
    77
        SDL_PushEvent((SDL_Event*)&event);
jorgen@7088
    78
    }
jorgen@7088
    79
jorgen@7088
    80
    return 0;
jorgen@7088
    81
}
jorgen@7088
    82
slouken@6614
    83
int
slouken@6614
    84
main(int argc, char *argv[])
slouken@6614
    85
{
slouken@6614
    86
    int success;
slouken@6614
    87
aschiffler@7639
    88
	/* Enable standard application logging */
aschiffler@7639
    89
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    90
slouken@6614
    91
    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
slouken@6614
    92
                "Simple MessageBox",
slouken@6614
    93
                "This is a simple error MessageBox",
slouken@6614
    94
                NULL);
slouken@6614
    95
    if (success == -1) {
aschiffler@7639
    96
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
slouken@6614
    97
        quit(1);
slouken@6614
    98
    }
slouken@6614
    99
icculus@6758
   100
    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
icculus@6758
   101
                "Simple MessageBox",
icculus@6758
   102
                "This is a simple MessageBox with a newline:\r\nHello world!",
icculus@6758
   103
                NULL);
icculus@6758
   104
    if (success == -1) {
aschiffler@7639
   105
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
icculus@6758
   106
        quit(1);
icculus@6758
   107
    }
icculus@6758
   108
icculus@6722
   109
    /* Google says this is Traditional Chinese for "beef with broccoli" */
icculus@6722
   110
    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
icculus@6722
   111
                "UTF-8 Simple MessageBox",
icculus@6722
   112
                "Unicode text: '牛肉西蘭花' ...",
icculus@6722
   113
                NULL);
icculus@6722
   114
    if (success == -1) {
aschiffler@7639
   115
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
icculus@6722
   116
        quit(1);
icculus@6722
   117
    }
icculus@6722
   118
icculus@6758
   119
    /* Google says this is Traditional Chinese for "beef with broccoli" */
icculus@6758
   120
    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
icculus@6758
   121
                "UTF-8 Simple MessageBox",
icculus@6758
   122
                "Unicode text and newline:\r\n'牛肉西蘭花'\n'牛肉西蘭花'",
icculus@6758
   123
                NULL);
icculus@6758
   124
    if (success == -1) {
aschiffler@7639
   125
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
icculus@6758
   126
        quit(1);
icculus@6758
   127
    }
icculus@6758
   128
jorgen@7088
   129
    button_messagebox(NULL);
jorgen@7088
   130
slouken@7458
   131
    /* Test showing a message box from a background thread.
slouken@7458
   132
slouken@7458
   133
       On Mac OS X, the video subsystem needs to be initialized for this
slouken@7458
   134
       to work, since the message box events are dispatched by the Cocoa
slouken@7458
   135
       subsystem on the main thread.
slouken@7458
   136
     */
slouken@7458
   137
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
aschiffler@7639
   138
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video subsystem: %s\n", SDL_GetError());
slouken@7458
   139
        return (1);
slouken@7458
   140
    }
slouken@6614
   141
    {
jorgen@7088
   142
        int status = 0;
jorgen@7088
   143
        SDL_Event event;
jorgen@7088
   144
        intptr_t eventNumber = SDL_RegisterEvents(1);
jorgen@7088
   145
        SDL_Thread* thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void*)eventNumber);
slouken@6614
   146
jorgen@7088
   147
        while (SDL_WaitEvent(&event))
jorgen@7088
   148
        {
jorgen@7088
   149
            if (event.type == eventNumber) {
jorgen@7088
   150
                break;
jorgen@7088
   151
            }
jorgen@7088
   152
        }
slouken@6614
   153
jorgen@7088
   154
        SDL_WaitThread(thread, &status);
jorgen@7088
   155
aschiffler@7639
   156
        SDL_Log("Message box thread return %i\n", status);
slouken@6614
   157
    }
slouken@7456
   158
slouken@7456
   159
    /* Test showing a message box with a parent window */
slouken@7456
   160
    {
slouken@7456
   161
        SDL_Event event;
philipp@7478
   162
        SDL_Window *window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
slouken@7456
   163
slouken@7456
   164
        success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
slouken@7456
   165
                    "Simple MessageBox",
slouken@7456
   166
                    "This is a simple error MessageBox with a parent window",
slouken@7456
   167
                    window);
slouken@7456
   168
        if (success == -1) {
aschiffler@7639
   169
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
slouken@7456
   170
            quit(1);
slouken@7456
   171
        }
slouken@7456
   172
slouken@7456
   173
        while (SDL_WaitEvent(&event))
slouken@7456
   174
        {
slouken@7456
   175
            if (event.type == SDL_QUIT || event.type == SDL_KEYUP) {
slouken@7456
   176
                break;
slouken@7456
   177
            }
slouken@7456
   178
        }
slouken@7456
   179
    }
slouken@6614
   180
slouken@6614
   181
    SDL_Quit();
slouken@6614
   182
    return (0);
slouken@6614
   183
}