-
Notifications
You must be signed in to change notification settings - Fork 2
/
dialog.c
423 lines (371 loc) · 12.8 KB
/
dialog.c
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
/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 Deutches Elektronen-Synchrotron in der Helmholtz-
* Gemelnschaft (DESY).
* Copyright (c) 2002 Berliner Speicherring-Gesellschaft fuer Synchrotron-
* Strahlung mbH (BESSY).
* Copyright (c) 2002 Southeastern Universities Research Association, as
* Operator of Thomas Jefferson National Accelerator Facility.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution.
\*************************************************************************/
/* dialog.c */
/************************DESCRIPTION***********************************
Creates alh dialogs
**********************************************************************/
#include <stdio.h>
#include <time.h>
#include <Xm/Xm.h>
#include <Xm/MessageB.h>
#include <Xm/FileSB.h>
#include <Xm/Protocols.h>
#include <Xm/AtomMgr.h>
#include "ax.h"
#define TIME_SIZE 18
extern Display *display;
extern int _no_error_popup;
static Widget warningbox = NULL;
/* function prototypes */
static void killWidget(Widget w, XtPointer clientdata, XtPointer calldata);
static void killDialog(Widget w, XtPointer clientdata, XtPointer calldata);
#if 0
static void logMessageString(Widget w, XtPointer clientdata, XtPointer calldata);
#endif
/******************************************************
Create a fileSelectionBox
******************************************************/
Widget createFileDialog(Widget parent,void *okCallback,XtPointer okParm,
void *cancelCallback,XtPointer cancelParm,XtPointer userParm,
String title,String pattern,String directory)
{
XmString Xtitle;
XmString Xpattern;
XmString Xdirectory;
XmString Xcurrentdir=0;
static Widget fileselectdialog = 0; /* make it static for reuse */
static void *oldOk= NULL;
static void *oldCancel=NULL;
static XtPointer oldOkParm = 0;
static XtPointer oldCancelParm = 0;
static Atom WM_DELETE_WINDOW = 0;
char file_sel[]="file_sel";
/* parent = 0 means we want to unmanage the fileSelectdialog */
if (!parent){
if (fileselectdialog && XtIsManaged(fileselectdialog))
XtUnmanageChild(fileselectdialog);
return(fileselectdialog);
}
/* destroy runtimeToplevel fileselectdialog
so we will not have exposure problems */
if ( parent && fileselectdialog &&
XtParent(XtParent(fileselectdialog)) != parent) {
XtDestroyWidget(fileselectdialog);
fileselectdialog = 0;
}
/* "Open" was selected. Create a Motif FileSelectionDialog w/callback */
if (!fileselectdialog) {
fileselectdialog = XmCreateFileSelectionDialog(parent,
file_sel, NULL, 0);
XtVaSetValues(fileselectdialog,
XmNallowShellResize, FALSE,
NULL);
WM_DELETE_WINDOW = XmInternAtom(XtDisplay(fileselectdialog),
"WM_DELETE_WINDOW", False);
XtAddCallback(fileselectdialog,XmNhelpCallback,
(XtCallbackProc)helpCallback,(XtPointer)NULL);
} else {
XtVaGetValues(fileselectdialog, XmNdirectory, &Xcurrentdir, NULL);
if (oldOk) XtRemoveCallback(fileselectdialog,XmNokCallback,
(XtCallbackProc)oldOk ,(XtPointer)oldOkParm);
if (oldCancel) XtRemoveCallback(fileselectdialog,XmNcancelCallback,
(XtCallbackProc)oldCancel ,(XtPointer)oldCancelParm);
if (oldCancel) XmRemoveWMProtocolCallback(XtParent(fileselectdialog),
WM_DELETE_WINDOW,(XtCallbackProc)oldCancel,(XtPointer)oldCancelParm );
}
Xtitle=XmStringCreateLtoR(title,XmSTRING_DEFAULT_CHARSET);
Xpattern=XmStringCreateLtoR(pattern,XmSTRING_DEFAULT_CHARSET);
if ( !directory && Xcurrentdir ) Xdirectory = Xcurrentdir;
else Xdirectory = XmStringCreateLtoR(directory,XmSTRING_DEFAULT_CHARSET);
XtVaSetValues(fileselectdialog,
XmNuserData, userParm,
XmNdialogTitle, Xtitle,
XmNdirectory, Xdirectory,
XmNpattern, Xpattern,
(XtPointer)NULL);
XmStringFree(Xtitle);
XmStringFree(Xpattern);
XmStringFree(Xdirectory);
XtAddCallback(fileselectdialog,XmNokCallback,
(XtCallbackProc)okCallback, (XtPointer)okParm);
XtAddCallback(fileselectdialog,XmNcancelCallback,
(XtCallbackProc)cancelCallback,(XtPointer)cancelParm);
XmAddWMProtocolCallback(XtParent(fileselectdialog),WM_DELETE_WINDOW,
(XtCallbackProc)cancelCallback,(XtPointer)cancelParm );
oldOk = okCallback;
oldCancel = cancelCallback;
oldOkParm = okParm;
oldCancelParm = cancelParm;
XtManageChild(fileselectdialog);
XFlush(display);
/*
XtPopup(XtParent(fileselectdialog), XtGrabNone);
*/
return(fileselectdialog);
}
/******************************************************
createDialog
******************************************************/
void createDialog(Widget parent,int dialogType,char *message1,char *message2)
{
XmString str,str1,str2,str3,string,string2;
Widget dialog = 0;
dialog = XmCreateMessageDialog(parent, "Dialog", NULL, 0);
XtUnmanageChild(XmMessageBoxGetChild(dialog,XmDIALOG_CANCEL_BUTTON));
XtUnmanageChild(XmMessageBoxGetChild(dialog,XmDIALOG_HELP_BUTTON));
XtSetSensitive(XmMessageBoxGetChild(dialog,XmDIALOG_HELP_BUTTON),FALSE);
XtAddCallback(dialog,XmNokCallback,killDialog,NULL);
switch(dialogType) {
case XmDIALOG_WARNING:
str=XmStringCreateLtoR("WarningDialog",XmSTRING_DEFAULT_CHARSET);
break;
case XmDIALOG_ERROR:
str=XmStringCreateLtoR("ErrorDialog",XmSTRING_DEFAULT_CHARSET);
break;
case XmDIALOG_INFORMATION:
str=XmStringCreateLtoR("InformationDialog",XmSTRING_DEFAULT_CHARSET);
break;
case XmDIALOG_MESSAGE:
str=XmStringCreateLtoR("MessageDialog",XmSTRING_DEFAULT_CHARSET);
break;
case XmDIALOG_QUESTION:
str=XmStringCreateLtoR("QuestionDialog",XmSTRING_DEFAULT_CHARSET);
break;
case XmDIALOG_WORKING:
str=XmStringCreateLtoR("WorkDialog",XmSTRING_DEFAULT_CHARSET);
break;
default:
str=XmStringCreateLtoR("Dialog",XmSTRING_DEFAULT_CHARSET);
break;
}
str1 = XmStringCreateLtoR(message1,XmFONTLIST_DEFAULT_TAG);
str2 = XmStringCreateLtoR(message2,XmFONTLIST_DEFAULT_TAG);
string = XmStringConcat(str1,str2);
str3 = XmStringCreateLtoR("ALH ",XmFONTLIST_DEFAULT_TAG);
string2 = XmStringConcat(str3,str);
XtVaSetValues(dialog,
#ifndef WIN32
/* 6/2012 Xming hangs when dialogType is set */
XmNdialogType, dialogType,
#endif
XmNdialogTitle, string2,
XmNmessageString, string,
(XtPointer)NULL);
XmStringFree(str);
XmStringFree(str1);
XmStringFree(str2);
XmStringFree(string);
XtManageChild(dialog);
XFlush(display);
}
/******************************************************
createActionDialog
******************************************************/
void createActionDialog(Widget parent,int dialogType,char *message1,
XtCallbackProc okCallback,XtPointer okParm,XtPointer userParm)
{
static Widget dialog = 0; /* make it static for reuse */
XmString str,str2;
static XtCallbackProc oldOkCallback = 0;
static XtPointer oldOkParm = 0;
if (dialog && XtIsManaged(dialog)) XtUnmanageChild(dialog);
if (!dialogType ) return;
/* destroy runtimeToplevel dialog so dialog is positioned properly */
if ( parent && dialog &&
XtParent(XtParent(dialog)) != parent) {
XtDestroyWidget(dialog);
dialog = 0;
}
if (!dialog) {
dialog = XmCreateMessageDialog(parent, "Dialog", NULL, 0);
XtSetSensitive(XmMessageBoxGetChild(dialog,XmDIALOG_HELP_BUTTON),FALSE);
XtAddCallback(dialog,XmNcancelCallback,(XtCallbackProc) XtUnmanageChild,NULL);
} else {
XtRemoveCallback(dialog,XmNokCallback,oldOkCallback,(XtPointer)oldOkParm);
}
switch(dialogType) {
case XmDIALOG_WARNING:
str = XmStringCreateSimple("ALH WarningDialog");
break;
case XmDIALOG_ERROR:
str = XmStringCreateSimple("ALH ErrorDialog");
break;
case XmDIALOG_INFORMATION:
str = XmStringCreateSimple("ALH InformationDialog");
break;
case XmDIALOG_MESSAGE:
str = XmStringCreateSimple("ALH MessageDialog");
break;
case XmDIALOG_QUESTION:
str = XmStringCreateSimple("ALH QuestionDialog");
break;
case XmDIALOG_WORKING:
str = XmStringCreateSimple("ALH WorkingDialog");
break;
default:
str = XmStringCreateSimple("ALH InformationDialog");
break;
}
str2=XmStringCreateLtoR(message1,XmSTRING_DEFAULT_CHARSET);
XtVaSetValues(dialog,
XmNuserData, userParm,
#ifndef WIN32
/* 6/2012 Xming hangs when dialogType is set */
XmNdialogType, dialogType,
#endif
XmNdialogTitle, str,
XmNmessageString, str2,
(XtPointer)NULL);
XmStringFree(str);
XmStringFree(str2);
XtAddCallback(dialog,XmNokCallback,okCallback,okParm);
oldOkCallback = okCallback;
oldOkParm = okParm;
XtManageChild(dialog);
XFlush(display);
/*
XmUpdateDisplay(dialog);
*/
return;
}
/******************************************************
Error message popup
******************************************************/
void errMsg(const char *fmt, ...)
{
XmString cstring,cstringOld,cstringNew;
va_list vargs;
static char lstring[1024]; /* DANGER: Fixed buffer size */
static int warningboxMessages = 0;
int nargs=10;
Arg args[10];
size_t len;
struct tm * tms;
time_t timeofday;
timeofday = time(0L);
tms = localtime(&timeofday);
len = strftime(lstring,TIME_SIZE,"%Y/%m/%d %H:%M ",tms);
va_start(vargs,fmt);
vsprintf(&lstring[TIME_SIZE-1],fmt,vargs);
va_end(vargs);
if(lstring[TIME_SIZE-1] == '\0') return;
alLogOpModMessage(0,0,&lstring[TIME_SIZE-1]);
if (_no_error_popup) {
return;
}
if (warningbox) {
if (warningboxMessages > 30) return;
cstring=XmStringCreateLtoR(lstring,XmSTRING_DEFAULT_CHARSET);
XtVaGetValues(warningbox, XmNmessageString, &cstringOld, NULL);
cstringNew = XmStringConcat(cstringOld,cstring);
XmStringFree(cstring);
XmStringFree(cstringOld);
if (warningboxMessages == 30){
cstring=XmStringCreateLtoR(
"\nOnly first 30 messages are displayed and logged\n",
XmSTRING_DEFAULT_CHARSET);
cstringOld = cstringNew;
cstringNew = XmStringConcat(cstringOld,cstring);
XmStringFree(cstring);
XmStringFree(cstringOld);
}
XtVaSetValues(warningbox, XmNmessageString, cstringNew, NULL);
XmStringFree(cstringNew);
warningboxMessages += 1;
XtManageChild(warningbox);
} else {
alBeep(display);
alBeep(display);
alBeep(display);
cstring=XmStringCreateLtoR(lstring,XmSTRING_DEFAULT_CHARSET);
nargs=0;
XtSetArg(args[nargs],XmNtitle,"ALH Warning");
nargs++;
XtSetArg(args[nargs],XmNmessageString,cstring);
nargs++;
#ifndef WIN32
warningbox=XmCreateWarningDialog(topLevelShell,"warningMessage",
#else
warningbox=XmCreateMessageDialog(topLevelShell,"warningMessage",
#endif
args,nargs);
XmStringFree(cstring);
XtDestroyWidget(XmMessageBoxGetChild(warningbox,XmDIALOG_CANCEL_BUTTON));
XtDestroyWidget(XmMessageBoxGetChild(warningbox,XmDIALOG_HELP_BUTTON));
/*XtAddCallback(warningbox,XmNokCallback,logMessageString,NULL);*/
XtAddCallback(warningbox,XmNokCallback,killWidget,NULL);
warningboxMessages = 1;
XtManageChild(warningbox);
}
}
/******************************************************
Fatal error message popup
******************************************************/
void fatalErrMsg(const char *fmt, ...)
{
va_list vargs;
static char lstring[1024]; /* DANGER: Fixed buffer size */
va_start(vargs,fmt);
vsprintf(lstring,fmt,vargs);
va_end(vargs);
if(lstring[0] == '\0') return;
errMsg(lstring);
if (warningbox) XtAddCallback(warningbox,XmNokCallback,exit_quit,NULL);
}
#if 0
/******************************************************
Static callback routine for logging a widget's messageString to opMod log file
******************************************************/
static void logMessageString(Widget w, XtPointer clientdata, XtPointer calldata)
{
XmString cstring;
XmStringContext context;
char *lstring;
XmStringCharSet tag;
XmStringDirection direction;
Boolean separator;
XtVaGetValues(w, XmNmessageString, &cstring, NULL);
if (!XmStringInitContext(&context,cstring)) {
XmStringFree(cstring);
return;
}
while (XmStringGetNextSegment(context,&lstring,&tag,&direction,&separator)){
alLogOpModMessage(0,0,lstring);
XtFree(lstring);
lstring=0;
}
if (lstring) XtFree(lstring);
XmStringFree(cstring);
XmStringFreeContext(context);
XtFree(tag);
}
#endif
/******************************************************
Static callback routine for destroying a widget
******************************************************/
static void killWidget(Widget w, XtPointer clientdata, XtPointer calldata)
{
XtDestroyWidget(w);
if (w == warningbox) {
warningbox = NULL;
}
}
/******************************************************
Static callback routine for destroying a widget
******************************************************/
static void killDialog(Widget w, XtPointer clientdata, XtPointer calldata)
{
XtDestroyWidget(w);
}