test/testmessage.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 08 Jun 2017 13:27:58 -0400
changeset 11081 eea7f98a37e4
parent 11020 3b993dbe4cf9
child 11272 d60ede5a0c2e
permissions -rw-r--r--
jack: Initial shot at a JACK audio target.

http://jackaudio.org/

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