test/testmessage.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 13 Aug 2017 21:06:52 -0700
changeset 11272 d60ede5a0c2e
parent 11020 3b993dbe4cf9
child 11275 6dd70238edb7
permissions -rw-r--r--
Fixed bug 3744 - missing SDLCALL in several functions

Ozkan Sezer

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