-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessageBox.cpp
More file actions
306 lines (245 loc) · 12.6 KB
/
MessageBox.cpp
File metadata and controls
306 lines (245 loc) · 12.6 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Message box. Taken from the FOX library and slightly modified for translation purpose.
// Also added a SU button
#include "config.h"
#include "i18n.h"
#include <fx.h>
#include <fxkeys.h>
#include <FXPNGIcon.h>
#include "icons.h"
#include "xfedefs.h"
#include "xfeutils.h"
#include "MessageBox.h"
// Padding for message box buttons
#define HORZ_PAD 30
#define VERT_PAD 2
#define BOX_BUTTON_MASK (BOX_OK|BOX_OK_CANCEL|BOX_YES_NO|BOX_YES_NO_CANCEL|BOX_QUIT_CANCEL|BOX_QUIT_SAVE_CANCEL|BOX_OK_SU|BOX_YES_NO_ALL_CANCEL)
// Map
FXDEFMAP(MessageBox) MessageBoxMap[] =
{
FXMAPFUNC(SEL_COMMAND, MessageBox::ID_CANCEL, MessageBox::onCmdCancel),
FXMAPFUNCS(SEL_COMMAND, MessageBox::ID_CLICKED_YES, MessageBox::ID_CLICKED_ALL, MessageBox::onCmdClicked),
FXMAPFUNC(SEL_COMMAND, MessageBox::ID_CLICKED_SU, MessageBox::onCmdSu),
};
// Object implementation
FXIMPLEMENT(MessageBox, DialogBox, MessageBoxMap, ARRAYNUMBER(MessageBoxMap))
// Construct message box with given caption, icon, and message text
MessageBox::MessageBox(FXWindow* owner, const FXString& caption, const FXString& text, FXIcon* ic, FXuint opts, FXuint textopts, int x, int y) :
DialogBox(owner, caption, opts|DECOR_TITLE|DECOR_BORDER|DECOR_STRETCHABLE|DECOR_MAXIMIZE|DECOR_CLOSE, x, y, 0, 0, 0, 0, 0, 0, 4, 4)
{
initialize(text, ic, opts&BOX_BUTTON_MASK, textopts);
}
// Construct free floating message box with given caption, icon, and message text
MessageBox::MessageBox(FXApp* a, const FXString& caption, const FXString& text, FXIcon* ic, FXuint opts, FXuint textopts, int x, int y) :
DialogBox(a, caption, opts|DECOR_TITLE|DECOR_BORDER|DECOR_STRETCHABLE|DECOR_MINIMIZE|DECOR_MAXIMIZE|DECOR_CLOSE, x, y, 0, 0, 0, 0, 0, 0, 4, 4)
{
initialize(text, ic, opts&BOX_BUTTON_MASK, textopts);
}
// Build contents
void MessageBox::initialize(const FXString& text, FXIcon* ic, FXuint whichbuttons, FXuint textoptions)
{
FXButton* initial;
FXVerticalFrame* content = new FXVerticalFrame(this, LAYOUT_FILL_X|LAYOUT_FILL_Y);
FXHorizontalFrame* info = new FXHorizontalFrame(content, LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_FILL_X|LAYOUT_FILL_Y, 0, 0, 0, 0, 10, 10, 10, 10);
// Message text
msg = new FXLabel(info, FXString::null, ic, textoptions);
setText(text);
FXHorizontalFrame* buttons = new FXHorizontalFrame(content, LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_FILL_X|PACK_UNIFORM_WIDTH, 0, 0, 0, 0, 10, 10, 10, 10);
if (whichbuttons == BOX_OK)
{
initial = new FXButton(buttons, _("&OK"), NULL, this, ID_CLICKED_OK, BUTTON_INITIAL|BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
initial->setFocus();
}
else if (whichbuttons == BOX_OK_SU)
{
initial = new FXButton(buttons, _("&OK"), NULL, this, ID_CLICKED_OK, BUTTON_INITIAL|BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
// Su button only if allowed
FXbool root_mode = getApp()->reg().readUnsignedEntry("OPTIONS", "root_mode", TRUE);
if (root_mode)
{
FXString key = getApp()->reg().readStringEntry("KEYBINDINGS", "new_root_window", "Shift-F3");
// Space before tab is used to set the correct button height
FXButton* btn = new FXButton(buttons, " "+TAB+_("Launch Xfe as root")+PARS(key), minixferooticon, this, ID_CLICKED_SU, BUTTON_DEFAULT|ICON_AFTER_TEXT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
FXHotKey hotkey = _parseAccel(key);
btn->addHotKey(hotkey);
}
initial->setFocus();
}
else if (whichbuttons == BOX_OK_CANCEL)
{
initial = new FXButton(buttons, _("&Cancel"), NULL, this, ID_CLICKED_CANCEL, BUTTON_INITIAL|BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
new FXButton(buttons, _("&OK"), NULL, this, ID_CLICKED_OK, BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
initial->setFocus();
}
else if (whichbuttons == BOX_YES_NO)
{
initial = new FXButton(buttons, _("&No"), NULL, this, ID_CLICKED_NO, BUTTON_INITIAL|BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
new FXButton(buttons, _("&Yes"), NULL, this, ID_CLICKED_YES, BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
initial->setFocus();
}
else if (whichbuttons == BOX_YES_NO_CANCEL)
{
initial = new FXButton(buttons, _("&Cancel"), NULL, this, ID_CLICKED_CANCEL, BUTTON_INITIAL|BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
new FXButton(buttons, _("&Yes"), NULL, this, ID_CLICKED_YES, BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
new FXButton(buttons, _("&No"), NULL, this, ID_CLICKED_NO, BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
initial->setFocus();
}
else if (whichbuttons == BOX_QUIT_CANCEL)
{
initial = new FXButton(buttons, _("&Cancel"), NULL, this, ID_CLICKED_CANCEL, BUTTON_INITIAL|BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
new FXButton(buttons, _("&Quit"), NULL, this, ID_CLICKED_QUIT, BUTTON_DEFAULT|BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
initial->setFocus();
}
else if (whichbuttons == BOX_QUIT_SAVE_CANCEL)
{
initial = new FXButton(buttons, _("&Cancel"), NULL, this, ID_CLICKED_CANCEL, BUTTON_INITIAL|BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
new FXButton(buttons, _("&Quit"), NULL, this, ID_CLICKED_QUIT, BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
new FXButton(buttons, _("&Save"), NULL, this, ID_CLICKED_SAVE, BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
initial->setFocus();
}
else if (whichbuttons == BOX_YES_NO_ALL_CANCEL)
{
initial = new FXButton(buttons, _("&Cancel"), NULL, this, ID_CLICKED_CANCEL, BUTTON_INITIAL|BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
new FXButton(buttons, _("&Yes"), NULL, this, ID_CLICKED_YES, BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
new FXButton(buttons, _("&No"), NULL, this, ID_CLICKED_NO, BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
new FXButton(buttons, _("Yes for &All"), NULL, this, ID_CLICKED_ALL, BUTTON_DEFAULT|FRAME_RAISED|FRAME_THICK|LAYOUT_TOP|LAYOUT_LEFT|LAYOUT_CENTER_X, 0, 0, 0, 0, HORZ_PAD, HORZ_PAD, VERT_PAD, VERT_PAD);
initial->setFocus();
}
}
// Close dialog
long MessageBox::onCmdClicked(FXObject*, FXSelector sel, void*)
{
getApp()->stopModal(this, BOX_CLICKED_YES+(FXSELID(sel)-ID_CLICKED_YES));
hide();
return(1);
}
// Launch a root Xfe (su mode)
long MessageBox::onCmdSu(FXObject*, FXSelector sel, void*)
{
getApp()->stopModal(this, BOX_CLICKED_YES+(FXSELID(sel)-ID_CLICKED_YES));
hide();
// Wait cursor
getApp()->beginWaitCursor();
// Obtain preferred root mode
FXbool use_sudo = getApp()->reg().readUnsignedEntry("OPTIONS", "use_sudo", FALSE);
// Use sudo or su to launch xfe as root
FXString title, sucmd, command;
if (use_sudo)
{
title = _("Enter the user password:");
sucmd = SUDOCMD;
}
else
{
title = _("Enter the root password:");
sucmd = SUCMD;
}
// Get text font
FXString fontspec = getApp()->reg().readStringEntry("SETTINGS", "textfont", "Courier,100,normal,regular");
if (fontspec.empty())
{
command = "st -t " + ::quote(title) + sucmd;
}
else
{
FXchar fontsize[32];
FXFont* font = new FXFont(getApp(), fontspec);
font->create();
snprintf(fontsize, sizeof(fontsize), "%d",(int)(font->getSize()/10)); // Size is in deci-points, thus divide by 10
command = "st -t " + ::quote(title) + " -f '" + (font->getFamily()).text() + ":pixelsize=" + fontsize + "'" + sucmd;
}
// Execute su or sudo command in an internal st terminal
int status = runst(command);
// If error
if (status < 0)
{
MessageBox::error(getApp(), BOX_OK, _("Error"), _("An error has occurred!"));
getApp()->endWaitCursor();
return(0);
}
// Wait cursor
getApp()->endWaitCursor();
return(1);
}
// Close dialog with a cancel
long MessageBox::onCmdCancel(FXObject* sender, FXSelector, void* ptr)
{
return(MessageBox::onCmdClicked(sender, FXSEL(SEL_COMMAND, ID_CLICKED_CANCEL), ptr));
}
// Show a modal error message
FXuint MessageBox::error(FXWindow* owner, FXuint opts, const char* caption, const char* message, ...)
{
va_list arguments;
va_start(arguments, message);
MessageBox box(owner, caption, FXStringVFormat(message, arguments), errorbigicon, opts|DECOR_TITLE|DECOR_BORDER);
va_end(arguments);
return(box.execute());
}
// Show a modal error message, in free floating window
FXuint MessageBox::error(FXApp* app, FXuint opts, const char* caption, const char* message, ...)
{
va_list arguments;
va_start(arguments, message);
MessageBox box(app, caption, FXStringVFormat(message, arguments), errorbigicon, opts|DECOR_TITLE|DECOR_BORDER);
va_end(arguments);
return(box.execute());
}
// Show a modal warning message
FXuint MessageBox::warning(FXWindow* owner, FXuint opts, const char* caption, const char* message, ...)
{
va_list arguments;
va_start(arguments, message);
MessageBox box(owner, caption, FXStringVFormat(message, arguments), warningbigicon, opts|DECOR_TITLE|DECOR_BORDER);
va_end(arguments);
return(box.execute());
}
// Show a modal warning message, in free floating window
FXuint MessageBox::warning(FXApp* app, FXuint opts, const char* caption, const char* message, ...)
{
va_list arguments;
va_start(arguments, message);
MessageBox box(app, caption, FXStringVFormat(message, arguments), warningbigicon, opts|DECOR_TITLE|DECOR_BORDER);
va_end(arguments);
return(box.execute());
}
// Show a modal question dialog
FXuint MessageBox::question(FXWindow* owner, FXuint opts, const char* caption, const char* message, ...)
{
va_list arguments;
va_start(arguments, message);
MessageBox box(owner, caption, FXStringVFormat(message, arguments), questionbigicon, opts|DECOR_TITLE|DECOR_BORDER);
va_end(arguments);
return(box.execute());
}
// Show a modal question dialog, in free floating window
FXuint MessageBox::question(FXApp* app, FXuint opts, const char* caption, const char* message, ...)
{
va_list arguments;
va_start(arguments, message);
MessageBox box(app, caption, FXStringVFormat(message, arguments), questionbigicon, opts|DECOR_TITLE|DECOR_BORDER);
va_end(arguments);
return(box.execute());
}
// Show a modal information dialog
FXuint MessageBox::information(FXWindow* owner, FXuint opts, const char* caption, const char* message, ...)
{
va_list arguments;
va_start(arguments, message);
MessageBox box(owner, caption, FXStringVFormat(message, arguments), infobigicon, opts|DECOR_TITLE|DECOR_BORDER);
va_end(arguments);
return(box.execute());
}
// Show a modal information dialog, in free floating window
FXuint MessageBox::information(FXApp* app, FXuint opts, const char* caption, const char* message, ...)
{
va_list arguments;
va_start(arguments, message);
MessageBox box(app, caption, FXStringVFormat(message, arguments), infobigicon, opts|DECOR_TITLE|DECOR_BORDER);
va_end(arguments);
return(box.execute());
}
// Set message text
void MessageBox::setText(FXString text)
{
// Set message text with a maximum of MAX_MESSAGE_LENGTH characters per line
msg->setText(::multiLines(text, MAX_MESSAGE_LENGTH));
}