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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
/* $XConsortium: popup.c,v 2.38 94/08/26 18:04:22 swick Exp $
*
*
* COPYRIGHT 1989
* DIGITAL EQUIPMENT CORPORATION
* MAYNARD, MASSACHUSETTS
* ALL RIGHTS RESERVED.
*
* THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
* SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
* DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR
* ANY PURPOSE. IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
*
* IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT
* RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN
* ADDITION TO THAT SET FORTH ABOVE.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Digital Equipment Corporation not be
* used in advertising or publicity pertaining to distribution of the software
* without specific, written prior permission.
*/
/* $XFree86$ */
/* popup.c -- Handle pop-up widgets. */
#include "xmh.h"
#include "actions.h"
#include <X11/Xaw/Cardinals.h>
typedef struct _PopupStatus {
Widget popup; /* order of fields same as CommandStatusRec */
struct _LastInput lastInput;
char* shell_command; /* NULL, or contains sh -c command */
} PopupStatusRec, *PopupStatus;
/* these are just strings which are used more than one place in the code */
static String XmhNconfirm = "confirm";
static String XmhNdialog = "dialog";
static String XmhNerror = "error";
static String XmhNnotice = "notice";
static String XmhNokay = "okay";
static String XmhNprompt = "prompt";
static String XmhNvalue = "value";
/* The popups were originally parented from toplevel and neglected the
* transientFor resource. In order not to break existing user resource
* settings for the popups, transientFor is set independent of the parent,
* which remains the toplevel widget.
*/
static void DeterminePopupPosition(
Position *x_ptr,
Position *y_ptr,
Widget *transFor_return) /* return a suitable top level shell */
{
if (lastInput.win != (Window) -1) {
if (transFor_return) {
Widget source;
source = XtWindowToWidget(XtDisplay(toplevel), lastInput.win);
while (source && !XtIsWMShell(source))
source = XtParent(source);
*transFor_return = source;
}
/* use the site of the last KeyPress or ButtonPress */
*x_ptr = lastInput.x;
*y_ptr = lastInput.y;
} else {
Widget source;
int i = 0;
Dimension width, height;
Arg args[2];
/* %%% need to keep track of last screen */
/* guess which screen and use the the center of it */
while (i < numScrns && !scrnList[i]->mapped)
i++;
source = ((i < numScrns) ? scrnList[i]->parent : toplevel);
XtSetArg(args[0], XtNwidth, &width);
XtSetArg(args[1], XtNheight, &height);
XtGetValues(source, args, TWO);
XtTranslateCoords(source, (Position) (width / 2),
(Position) (height / 2), x_ptr, y_ptr);
if (transFor_return) *transFor_return = source;
}
}
static Boolean PositionThePopup(
Widget popup,
Position x,
Position y)
{
/* Hack. Fix up the position of the popup. The xmh app defaults file
* contains an Xmh*Geometry specification; the effects of that on
* popups, and the lack of any user-supplied geometry specification for
* popups, are mitigated here, by giving the popup shell a position.
* (Xmh*Geometry is needed in case there is no user-supplied default.)
* Returns True if an explicit geometry was inferred; false if the
* widget was repositioned to (x,y).
*/
Arg args[4];
String top_geom, pop_geom;
XtSetArg( args[0], XtNgeometry, &top_geom );
XtGetValues( toplevel, args, ONE );
XtSetArg( args[0], XtNgeometry, &pop_geom );
XtGetValues( popup, args, ONE );
if (pop_geom == NULL || pop_geom == top_geom) {
/* if same db entry, then ... */
XtSetArg( args[0], XtNgeometry, (String) NULL);
XtSetArg( args[1], XtNx, x);
XtSetArg( args[2], XtNy, y);
XtSetArg( args[3], XtNwinGravity, SouthWestGravity);
XtSetValues( popup, args, FOUR);
return False;
}
return True;
}
static void CenterPopupPosition(
Widget widget,
Widget popup,
Position px,
Position py)
{
Position x, y;
Position nx, ny;
Arg args[3];
if (widget == NULL) return;
XtSetArg(args[0], XtNx, &x);
XtSetArg(args[1], XtNy, &y);
XtGetValues(popup, args, TWO);
if (x == px && y == py) {
/* Program sets geometry. Correct our earlier calculations. */
nx = (GetWidth(widget) - GetWidth(popup)) / 2;
ny = (GetHeight(widget) - GetHeight(popup)) / 2;
if (nx < 0) nx = 0;
if (ny < 0) ny = 0;
XtTranslateCoords(widget, nx, ny, &x, &y);
XtSetArg(args[0], XtNx, x);
XtSetArg(args[1], XtNy, y);
XtSetArg(args[2], XtNwinGravity, CenterGravity);
XtSetValues(popup, args, THREE);
}
}
/* Insure that the popup is wholly showing on the screen.
Optionally center the widget horizontally and/or vertically
on current position.
*/
static void InsureVisibility(
Widget popup,
Widget popup_child,
Position x, /* assert: current position = (x,y) */
Position y,
Boolean centerX,
Boolean centerY)
{
Position root_x, root_y;
Dimension width, height, border;
Arg args[3];
XtSetArg( args[0], XtNwidth, &width );
XtSetArg( args[1], XtNheight, &height );
XtSetArg( args[2], XtNborderWidth, &border );
XtGetValues( popup, args, THREE );
XtTranslateCoords(popup_child, (Position)0, (Position)0, &root_x, &root_y);
if (centerX) root_x -= width/2 + border;
if (centerY) root_y -= height/2 + border;
if (root_x < 0) root_x = 0;
if (root_y < 0) root_y = 0;
border <<= 1;
if ((int)(root_x + width + border) > WidthOfScreen(XtScreen(toplevel))) {
root_x = WidthOfScreen(XtScreen(toplevel)) - width - border;
}
if ((int)(root_y + height + border) > HeightOfScreen(XtScreen(toplevel))) {
root_y = HeightOfScreen(XtScreen(toplevel)) - height - border;
}
if (root_x != x || root_y != y) {
XtSetArg( args[0], XtNx, root_x );
XtSetArg( args[1], XtNy, root_y );
XtSetValues( popup, args, TWO );
}
}
/*ARGSUSED*/
void DestroyPopup(
Widget widget, /* unused */
XtPointer client_data,
XtPointer call_data) /* unused */
{
Widget popup = (Widget) client_data;
XtPopdown(popup);
XtDestroyWidget(popup);
}
void WMDeletePopup(
Widget popup, /* transient shell */
XEvent* event)
{
String shellName;
String buttonName;
Widget button;
shellName = XtName(popup);
if (strcmp(shellName, XmhNconfirm) == 0)
buttonName = "*no";
else if (strcmp(shellName, XmhNprompt) == 0)
buttonName = "*cancel";
else if (strcmp(shellName, XmhNnotice) == 0)
buttonName = "*confirm";
else if (strcmp(shellName, XmhNerror) == 0)
buttonName = "*OK";
else
return; /* WM may kill us */
button = XtNameToWidget(popup, buttonName);
if (! button) return;
XtCallActionProc(button, "set", event, (String*)NULL, ZERO);
XtCallActionProc(button, "notify", event, (String*)NULL, ZERO);
XtCallActionProc(button, "unset", event, (String*)NULL, ZERO);
}
static void TheUsual(
Widget popup) /* shell */
{
XtInstallAllAccelerators(popup, popup);
XtAugmentTranslations(popup, app_resources.wm_protocols_translations);
XtRealizeWidget(popup);
XDefineCursor(XtDisplay(popup), XtWindow(popup), app_resources.cursor);
(void) XSetWMProtocols(XtDisplay(popup), XtWindow(popup),
protocolList, XtNumber(protocolList));
}
/*ARGSUSED*/
void XmhPromptOkayAction(
Widget w, /* the "value" widget in the Dialog box */
XEvent *event, /* unused */
String *params, /* unused */
Cardinal *num_params) /* unused */
{
XtCallCallbacks(XtNameToWidget(XtParent(w), XmhNokay), XtNcallback,
(XtPointer)XtParent(w));
}
void PopupPrompt(
Widget transientFor, /* required to be a top-level shell */
String question, /* the prompting string */
XtCallbackProc okayCallback) /* CreateFolder() */
{
Widget popup;
Widget dialog;
Widget value;
Position x, y;
Boolean positioned;
Arg args[3];
static XtTranslations PromptTextTranslations = NULL;
DeterminePopupPosition(&x, &y, (Widget*)NULL);
XtSetArg(args[0], XtNallowShellResize, True);
XtSetArg(args[1], XtNinput, True);
XtSetArg(args[2], XtNtransientFor, transientFor);
popup = XtCreatePopupShell(XmhNprompt, transientShellWidgetClass, toplevel,
args, THREE);
positioned = PositionThePopup(popup, x, y);
XtSetArg(args[0], XtNlabel, question);
XtSetArg(args[1], XtNvalue, "");
dialog = XtCreateManagedWidget(XmhNdialog, dialogWidgetClass, popup, args,
TWO);
XtSetArg(args[0], XtNresizable, True);
XtSetValues( XtNameToWidget(dialog, "label"), args, ONE);
value = XtNameToWidget(dialog, XmhNvalue);
XtSetValues( value, args, ONE);
if (! PromptTextTranslations)
PromptTextTranslations = XtParseTranslationTable
("<Key>Return: XmhPromptOkayAction()\n\
Ctrl<Key>R: no-op(RingBell)\n\
Ctrl<Key>S: no-op(RingBell)\n");
XtOverrideTranslations(value, PromptTextTranslations);
XawDialogAddButton(dialog, XmhNokay, okayCallback, (XtPointer) dialog);
XawDialogAddButton(dialog, "cancel", DestroyPopup, (XtPointer) popup);
TheUsual(popup);
InsureVisibility(popup, dialog, x, y, !positioned, False);
XtPopup(popup, XtGrabNone);
}
/* ARGSUSED */
static void FreePopupStatus(
Widget w, /* unused */
XtPointer closure,
XtPointer call_data) /* unused */
{
PopupStatus popup = (PopupStatus)closure;
XtPopdown(popup->popup);
XtDestroyWidget(popup->popup);
if (popup->shell_command)
XtFree(popup->shell_command);
XtFree((char *) closure);
}
void PopupNotice(
String message,
XtCallbackProc callback,
XtPointer closure)
{
PopupStatus popup_status = (PopupStatus)closure;
Widget transientFor;
Widget dialog;
Widget value;
Position x, y;
Arg args[3];
char command[65], label[128];
if (popup_status == (PopupStatus)NULL) {
popup_status = XtNew(PopupStatusRec);
popup_status->lastInput = lastInput;
popup_status->shell_command = (char*)NULL;
}
if (! popup_status->shell_command) {
/* MH command */
if (sscanf( message, "%64s", command ) != 1)
(void) strcpy( command, "system" );
else {
int l = strlen(command);
if (l && command[--l] == ':')
command[l] = '\0';
}
snprintf(label, sizeof(label), "%.64s command returned:", command);
} else {
/* arbitrary shell command */
int len = strlen(popup_status->shell_command);
snprintf(label, sizeof(label), "%.88s %s\nshell command returned:",
popup_status->shell_command,
((len > 88) ? "[truncated]" : ""));
}
DeterminePopupPosition(&x, &y, &transientFor);
XtSetArg( args[0], XtNallowShellResize, True );
XtSetArg( args[1], XtNinput, True );
XtSetArg( args[2], XtNtransientFor, transientFor);
popup_status->popup = XtCreatePopupShell(XmhNnotice,
transientShellWidgetClass, toplevel, args, THREE);
PositionThePopup(popup_status->popup, x, y);
XtSetArg( args[0], XtNlabel, label );
XtSetArg( args[1], XtNvalue, message );
dialog = XtCreateManagedWidget(XmhNdialog, dialogWidgetClass,
popup_status->popup, args, TWO);
/* The text area of the dialog box will not be editable. */
value = XtNameToWidget(dialog, XmhNvalue);
XtSetArg( args[0], XtNeditType, XawtextRead);
XtSetArg( args[1], XtNdisplayCaret, False);
XtSetValues( value, args, TWO);
XtOverrideTranslations(value, NoTextSearchAndReplace);
XawDialogAddButton( dialog, XmhNconfirm,
((callback != (XtCallbackProc) NULL)
? callback : (XtCallbackProc) FreePopupStatus),
(XtPointer) popup_status
);
TheUsual(popup_status->popup);
InsureVisibility(popup_status->popup, dialog, x, y, False, False);
XtPopup(popup_status->popup, XtGrabNone);
}
void PopupConfirm(
Widget center_widget, /* where to center; may be NULL */
String question,
XtCallbackList affirm_callbacks,
XtCallbackList negate_callbacks)
{
Widget popup;
Widget dialog;
Widget button;
Widget transientFor;
Position x, y;
Arg args[3];
static XtCallbackRec callbacks[] = {
{DestroyPopup, (XtPointer) NULL},
{(XtCallbackProc) NULL, (XtPointer) NULL}
};
DeterminePopupPosition(&x, &y, &transientFor);
XtSetArg(args[0], XtNinput, True);
XtSetArg(args[1], XtNallowShellResize, True);
XtSetArg(args[2], XtNtransientFor, transientFor);
popup = XtCreatePopupShell(XmhNconfirm, transientShellWidgetClass,
toplevel, args, THREE);
PositionThePopup(popup, x, y);
XtSetArg(args[0], XtNlabel, question);
dialog = XtCreateManagedWidget(XmhNdialog, dialogWidgetClass, popup, args,
ONE);
callbacks[0].closure = (XtPointer) popup;
XtSetArg(args[0], XtNcallback, callbacks);
button = XtCreateManagedWidget("yes", commandWidgetClass, dialog,
args, ONE);
if (affirm_callbacks)
XtAddCallbacks(button, XtNcallback, affirm_callbacks);
button = XtCreateManagedWidget("no", commandWidgetClass, dialog,
args, ZERO);
XtAddCallback(button, XtNcallback, DestroyPopup, (XtPointer) popup);
if (negate_callbacks)
XtAddCallbacks(button, XtNcallback, negate_callbacks);
TheUsual(popup);
CenterPopupPosition(center_widget ? center_widget : transientFor,
popup, x, y);
InsureVisibility(popup, dialog, x, y, False, False);
XtPopup(popup, XtGrabNone);
}
void PopupError(
Widget widget, /* transient for this top-level shell, or NULL */
String message)
{
Widget transFor, error_popup, dialog;
Position x, y;
Boolean positioned;
Arg args[3];
static XtCallbackRec callbacks[] = {
{DestroyPopup, (XtPointer) NULL},
{(XtCallbackProc) NULL, (XtPointer) NULL}
};
transFor = widget;
DeterminePopupPosition(&x, &y, transFor ? (Widget*)NULL : &transFor);
XtSetArg(args[0], XtNallowShellResize, True);
XtSetArg(args[1], XtNinput, True);
XtSetArg(args[2], XtNtransientFor, transFor);
error_popup = XtCreatePopupShell(XmhNerror, transientShellWidgetClass,
toplevel, args, THREE);
positioned = PositionThePopup(error_popup, x, y);
XtSetArg(args[0], XtNlabel, message);
dialog = XtCreateManagedWidget(XmhNdialog, dialogWidgetClass, error_popup,
args, ONE);
callbacks[0].closure = (XtPointer) error_popup;
XtSetArg(args[0], XtNcallback, callbacks);
XawDialogAddButton(dialog, "OK", DestroyPopup, (XtPointer) error_popup);
TheUsual(error_popup);
InsureVisibility(error_popup, dialog, x, y, !positioned, !positioned);
XtPopup(error_popup, XtGrabNone);
}
/*ARGSUSED*/
void PopupWarningHandler(
String name,
String type,
String class,
String msg,
String *params,
Cardinal *num)
{
char *ptr;
int i;
String par[10];
char message[500];
char buffer[500];
static Boolean allowPopup = True; /* protect against recursion */
XtGetErrorDatabaseText(name, type, class, msg, buffer, 500);
if (params && num && *num) {
i = (*num <= 10) ? *num : 10;
memmove( (char*)par, (char*)params, i * sizeof(String));
bzero( &par[i], (10-i) * sizeof(String));
if (*num > 10)
par[9] = "(truncated)";
snprintf(message, sizeof(message), buffer, par[0], par[1], par[2],
par[3], par[4], par[5], par[6], par[7], par[8], par[9]);
ptr = message;
} else {
ptr = buffer;
}
if (allowPopup) {
allowPopup = False;
PopupError((Widget)NULL, ptr);
allowPopup = True;
} else {
fprintf(stderr, ptr);
}
}
|