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