test/testmessage.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Dec 2017 16:08:09 -0800
changeset 11730 ac6c607e065c
parent 11275 6dd70238edb7
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Enable building the Metal renderer by default, and weak link the Metal framework so the SDL library is safe to use on older Macs
Also generate iOS versions of the Metal shaders
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,
slouken@11275
    49
        NULL,/* 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;
slouken@11275
    55
    data.buttons = buttons;
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
    }
icculus@9694
    72
    SDL_Log("Pressed button: %d, %s\n", button, button == -1 ? "[closed]" : 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
philipp@9922
    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
icculus@9697
   129
    /* Google says this is Traditional Chinese for "beef with broccoli" */
icculus@9697
   130
    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
icculus@9697
   131
                "牛肉西蘭花",
icculus@9697
   132
                "Unicode text in the title.",
icculus@9697
   133
                NULL);
icculus@9697
   134
    if (success == -1) {
icculus@9697
   135
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
icculus@9697
   136
        quit(1);
icculus@9697
   137
    }
icculus@9697
   138
jorgen@7088
   139
    button_messagebox(NULL);
jorgen@7088
   140
slouken@7458
   141
    /* Test showing a message box from a background thread.
slouken@7458
   142
slouken@7458
   143
       On Mac OS X, the video subsystem needs to be initialized for this
slouken@7458
   144
       to work, since the message box events are dispatched by the Cocoa
slouken@7458
   145
       subsystem on the main thread.
slouken@7458
   146
     */
slouken@7458
   147
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
aschiffler@7639
   148
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video subsystem: %s\n", SDL_GetError());
slouken@7458
   149
        return (1);
slouken@7458
   150
    }
slouken@6614
   151
    {
jorgen@7088
   152
        int status = 0;
jorgen@7088
   153
        SDL_Event event;
jorgen@7088
   154
        intptr_t eventNumber = SDL_RegisterEvents(1);
jorgen@7088
   155
        SDL_Thread* thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void*)eventNumber);
slouken@6614
   156
jorgen@7088
   157
        while (SDL_WaitEvent(&event))
jorgen@7088
   158
        {
jorgen@7088
   159
            if (event.type == eventNumber) {
jorgen@7088
   160
                break;
jorgen@7088
   161
            }
jorgen@7088
   162
        }
slouken@6614
   163
jorgen@7088
   164
        SDL_WaitThread(thread, &status);
jorgen@7088
   165
aschiffler@7639
   166
        SDL_Log("Message box thread return %i\n", status);
slouken@6614
   167
    }
slouken@7456
   168
slouken@7456
   169
    /* Test showing a message box with a parent window */
slouken@7456
   170
    {
slouken@7456
   171
        SDL_Event event;
philipp@7478
   172
        SDL_Window *window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
slouken@7456
   173
slouken@7456
   174
        success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
slouken@7456
   175
                    "Simple MessageBox",
slouken@7456
   176
                    "This is a simple error MessageBox with a parent window",
slouken@7456
   177
                    window);
slouken@7456
   178
        if (success == -1) {
aschiffler@7639
   179
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
slouken@7456
   180
            quit(1);
slouken@7456
   181
        }
slouken@7456
   182
slouken@7456
   183
        while (SDL_WaitEvent(&event))
slouken@7456
   184
        {
slouken@7456
   185
            if (event.type == SDL_QUIT || event.type == SDL_KEYUP) {
slouken@7456
   186
                break;
slouken@7456
   187
            }
slouken@7456
   188
        }
slouken@7456
   189
    }
slouken@6614
   190
slouken@6614
   191
    SDL_Quit();
slouken@6614
   192
    return (0);
slouken@6614
   193
}