SDL

Форк
0
/
testmessage.c 
229 строк · 7.6 Кб
1
/*
2
  Copyright (C) 1997-2024 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 <stdlib.h>
16

17
#include <SDL3/SDL.h>
18
#include <SDL3/SDL_main.h>
19
#include <SDL3/SDL_test.h>
20

21
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
22
static void
23
quit(int rc)
24
{
25
    SDL_Quit();
26
    /* Let 'main()' return normally */
27
    if (rc != 0) {
28
        exit(rc);
29
    }
30
}
31

32
static int SDLCALL
33
button_messagebox(void *eventNumber)
34
{
35
    const SDL_MessageBoxButtonData buttons[] = {
36
        { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
37
          0,
38
          "OK" },
39
        { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
40
          1,
41
          "Cancel" },
42
    };
43

44
    SDL_MessageBoxData data = {
45
        SDL_MESSAGEBOX_INFORMATION,
46
        NULL, /* no parent window */
47
        "Custom MessageBox",
48
        "This is a custom messagebox",
49
        2,
50
        NULL, /* buttons */
51
        NULL  /* Default color scheme */
52
    };
53

54
    int button = -1;
55
    int success = 0;
56
    data.buttons = buttons;
57
    if (eventNumber) {
58
        data.message = "This is a custom messagebox from a background thread.";
59
    }
60

61
    success = SDL_ShowMessageBox(&data, &button);
62
    if (success == -1) {
63
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
64
        if (eventNumber) {
65
            SDL_Event event;
66
            event.type = (Uint32)(intptr_t)eventNumber;
67
            SDL_PushEvent(&event);
68
            return 1;
69
        } else {
70
            quit(2);
71
        }
72
    }
73
    SDL_Log("Pressed button: %d, %s\n", button, button == -1 ? "[closed]" : button == 1 ? "Cancel"
74
                                                                                        : "OK");
75

76
    if (eventNumber) {
77
        SDL_Event event;
78
        event.type = (Uint32)(intptr_t)eventNumber;
79
        SDL_PushEvent(&event);
80
    }
81

82
    return 0;
83
}
84

85
int main(int argc, char *argv[])
86
{
87
    int success;
88
    SDLTest_CommonState *state;
89

90
    /* Initialize test framework */
91
    state = SDLTest_CommonCreateState(argv, 0);
92
    if (!state) {
93
        return 1;
94
    }
95

96
    /* Enable standard application logging */
97
    SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
98

99
    /* Parse commandline */
100
    if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
101
        return 1;
102
    }
103

104
    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
105
                                       "Simple MessageBox",
106
                                       "This is a simple error MessageBox",
107
                                       NULL);
108
    if (success == -1) {
109
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
110
        quit(1);
111
    }
112

113
    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
114
                                       "Simple MessageBox",
115
                                       "This is a simple MessageBox with a newline:\r\nHello world!",
116
                                       NULL);
117
    if (success == -1) {
118
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
119
        quit(1);
120
    }
121

122
    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
123
                                       NULL,
124
                                       "NULL Title",
125
                                       NULL);
126
    if (success == -1) {
127
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
128
        quit(1);
129
    }
130

131
    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
132
                                       "NULL Message",
133
                                       NULL,
134
                                       NULL);
135
    if (success == -1) {
136
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
137
        quit(1);
138
    }
139

140
    /* Google says this is Traditional Chinese for "beef with broccoli" */
141
    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
142
                                       "UTF-8 Simple MessageBox",
143
                                       "Unicode text: '牛肉西蘭花' ...",
144
                                       NULL);
145
    if (success == -1) {
146
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
147
        quit(1);
148
    }
149

150
    /* Google says this is Traditional Chinese for "beef with broccoli" */
151
    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
152
                                       "UTF-8 Simple MessageBox",
153
                                       "Unicode text and newline:\r\n'牛肉西蘭花'\n'牛肉西蘭花'",
154
                                       NULL);
155
    if (success == -1) {
156
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
157
        quit(1);
158
    }
159

160
    /* Google says this is Traditional Chinese for "beef with broccoli" */
161
    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
162
                                       "牛肉西蘭花",
163
                                       "Unicode text in the title.",
164
                                       NULL);
165
    if (success == -1) {
166
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
167
        quit(1);
168
    }
169

170
    button_messagebox(NULL);
171

172
    /* Test showing a message box from a background thread.
173

174
       On macOS, the video subsystem needs to be initialized for this
175
       to work, since the message box events are dispatched by the Cocoa
176
       subsystem on the main thread.
177
     */
178
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
179
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video subsystem: %s\n", SDL_GetError());
180
        return 1;
181
    }
182
    {
183
        int status = 0;
184
        SDL_Event event;
185
        Uint32 eventNumber = SDL_RegisterEvents(1);
186
        SDL_Thread *thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void *)(uintptr_t)eventNumber);
187

188
        while (SDL_WaitEvent(&event)) {
189
            if (event.type == eventNumber) {
190
                break;
191
            }
192
        }
193

194
        SDL_WaitThread(thread, &status);
195

196
        SDL_Log("Message box thread return %i\n", status);
197
    }
198

199
    /* Test showing a message box with a parent window */
200
    {
201
        SDL_Event event;
202
        SDL_Window *window = SDL_CreateWindow("Test", 640, 480, 0);
203

204
        /* On wayland, no window will actually show until something has
205
           actually been displayed.
206
        */
207
        SDL_Renderer *renderer = SDL_CreateRenderer(window, NULL);
208
        SDL_RenderPresent(renderer);
209

210
        success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
211
                                           "Simple MessageBox",
212
                                           "This is a simple error MessageBox with a parent window. Press a key or close the window after dismissing this messagebox.",
213
                                           window);
214
        if (success == -1) {
215
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
216
            quit(1);
217
        }
218

219
        while (SDL_WaitEvent(&event)) {
220
            if (event.type == SDL_EVENT_QUIT || event.type == SDL_EVENT_KEY_UP) {
221
                break;
222
            }
223
        }
224
    }
225

226
    SDL_Quit();
227
    SDLTest_CommonDestroyState(state);
228
    return 0;
229
}
230

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.