/
nativeui.cpp
165 lines (147 loc) · 5.13 KB
/
nativeui.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/** @file nativeui.cpp Native GUI functionality.
* @ingroup base
*
* @authors Copyright © 2012-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "dd_share.h"
#include "sys_system.h"
#include "ui/nativeui.h"
#include "ui/clientwindow.h"
#include <de/App>
#include <de/ByteArrayFile>
#include <de/FileSystem>
#include <SDL_messagebox.h>
#include <stdarg.h>
void Sys_MessageBox(messageboxtype_t type,
const char * title,
const char * msg,
const char * detailedMsg)
{
Sys_MessageBox2(type, title, msg, 0, detailedMsg);
}
void Sys_MessageBox2(messageboxtype_t type,
const char * title,
const char * msg,
const char * informativeMsg,
const char * detailedMsg)
{
Sys_MessageBox3(type, title, msg, informativeMsg, detailedMsg, 0);
}
int Sys_MessageBox3(messageboxtype_t type,
const char * title,
const char * msg,
const char * informativeMsg,
const char * detailedMsg,
const char ** buttons)
{
if (novideo)
{
// There's no GUI...
de::warning("%s", msg);
return 0;
}
if (ClientWindow::mainExists())
{
ClientWindow::main().hide();
}
SDL_MessageBoxData box{};
box.title = title;
switch (type)
{
case MBT_INFORMATION: box.flags = SDL_MESSAGEBOX_INFORMATION; break;
case MBT_QUESTION: box.flags = SDL_MESSAGEBOX_INFORMATION; break;
case MBT_WARNING: box.flags = SDL_MESSAGEBOX_WARNING; break;
case MBT_ERROR: box.flags = SDL_MESSAGEBOX_ERROR; break;
default:
break;
}
de::String text = msg;
if (detailedMsg)
{
/// @todo Making the dialog a bit wider would be nice, but it seems one has to
/// derive a new message box class for that -- the default one has a fixed size.
text += "\n\n";
text += detailedMsg;
}
if (informativeMsg)
{
text += "\n\n";
text += informativeMsg;
}
box.message = text;
std::vector<SDL_MessageBoxButtonData> buttonData;
if (buttons)
{
for (int i = 0; buttons[i]; ++i)
{
buttonData.push_back(SDL_MessageBoxButtonData{
Uint32(i == 0 ? SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT
: i == 1 ? SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT : 0),
i,
buttons[i]});
}
}
else
{
buttonData.push_back(
SDL_MessageBoxButtonData{SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 0, "OK"});
}
box.buttons = buttonData.data();
box.numbuttons = int(buttonData.size());
int rc;
SDL_ShowMessageBox(&box, &rc);
return rc;
}
void Sys_MessageBoxf(messageboxtype_t type, const char* title, const char* format, ...)
{
char buffer[1024];
va_list args;
va_start(args, format);
dd_vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
Sys_MessageBox(type, title, buffer, 0);
}
int Sys_MessageBoxWithButtons(messageboxtype_t type,
const char * title,
const char * msg,
const char * informativeMsg,
const char ** buttons)
{
return Sys_MessageBox3(type, title, msg, informativeMsg, 0, buttons);
}
void Sys_MessageBoxWithDetailsFromFile(messageboxtype_t type,
const char * title,
const char * msg,
const char * informativeMsg,
const char * detailsFileName)
{
try
{
de::String details;
de::App::rootFolder().locate<const de::File>(detailsFileName) >> details;
// This will be used as a null-terminated string.
details.append('\0');
Sys_MessageBox2(type, title, msg, informativeMsg, details);
}
catch (const de::Error &er)
{
de::warning("Could not read \"%s\": %s", detailsFileName, er.asText().c_str());
// Show it without the details, then.
Sys_MessageBox2(type, title, msg, informativeMsg, 0);
}
}