2 * Copyright 1989 Software Research Associates, Inc., Tokyo, Japan
4 * Permission to use, copy, modify, and distribute this software and its
5 * documentation for any purpose and without fee is hereby granted, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Software Research Associates not be used
9 * in advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. Software Research Associates
11 * makes no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * SOFTWARE RESEARCH ASSOCIATES DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
16 * IN NO EVENT SHALL SOFTWARE RESEARCH ASSOCIATES BE LIABLE FOR ANY SPECIAL,
17 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
18 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
19 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
22 * Author: Erik M. van der Poel
23 * Software Research Associates, Inc., Tokyo, Japan
32 * erik%sra.co.jp@uunet.uu.net
34 * erik%sra.co.jp@mcvax.uucp
36 * try junet instead of co.jp
38 * Erik M. van der Poel
39 * Software Research Associates, Inc.
40 * 1-1-1 Hirakawa-cho, Chiyoda-ku
41 * Tokyo 102 Japan. TEL +81-3-234-2692
46 /* BSD 4.3 errno.h does not declare errno */
48 //extern int sys_nerr;
49 //extern char *sys_errlist[]; // [HGM] this produced a compile error in Ubuntu 8.04
51 #include <sys/param.h>
52 #include <X11/cursorfont.h>
53 #include <X11/Intrinsic.h>
54 #include <X11/StringDefs.h>
55 #include <X11/Composite.h>
56 #include <X11/Shell.h>
57 #include <X11/Xaw/Form.h>
58 #include <X11/Xaw/Command.h>
59 #include <X11/Xaw/Scrollbar.h>
60 #include <X11/Xaw/Label.h>
61 #include <X11/Xaw/Cardinals.h>
67 #define MAXPATHLEN 1024
68 #endif /* ndef MAXPATHLEN */
70 #if !defined(SVR4) && !defined(SYSV) && !defined(USG)
72 #endif /* !defined(SVR4) && !defined(SYSV) && !defined(USG) */
74 int SFstatus = SEL_FILE_NULL;
77 SFstartDir[MAXPATHLEN+1],
78 SFcurrentPath[MAXPATHLEN+1],
79 SFlastPath[MAXPATHLEN+1],
80 SFcurrentDir[MAXPATHLEN+1];
100 Atom SFwmDeleteWindow;
102 XSegment SFsegs[2], SFcompletionSegs[2];
104 XawTextPosition SFtextPos;
106 int SFupperX, SFlowerY, SFupperY;
108 int SFtextX, SFtextYoffset;
110 int SFentryWidth, SFentryHeight;
112 int SFlineToTextH = 3;
114 int SFlineToTextV = 3;
116 int SFbesideText = 3;
118 int SFaboveAndBelowText = 2;
120 int SFcharsPerEntry = 15;
124 int SFworkProcAdded = 0;
128 int SFpathScrollWidth, SFvScrollHeight, SFhScrollWidth;
130 char SFtextBuffer[MAXPATHLEN+1];
132 char SFfilterBuffer[MAXPATHLEN+1];
134 XtIntervalId SFdirModTimerId;
138 Boolean SFpathFlag; // [HGM]
140 static char *oneLineTextEditTranslations = "\
141 <Key>Return: redraw-display()\n\
142 Ctrl<Key>M: redraw-display()\n\
147 SFexposeList(w, n, event, cont)
153 if ((event->type == NoExpose) || event->xexpose.count) {
157 SFdrawList((int)(intptr_t)n, SF_DO_NOT_SCROLL);
163 if(SFdirs) XtFree((XtPointer) SFdirs);
164 SFdirs = NULL; // kludge to throw away all cached info
169 SFmodVerifyCallback(w, client_data, event, cont)
171 XtPointer client_data;
178 (XLookupString(&(event->xkey), buf, 2, NULL, NULL) == 1) &&
179 ((*buf) == '\r' || *buf == 033)
182 Arg args[10]; char *p;
183 if(*buf == 033) { // [HGM] esc in filter: restore and give focus to path
184 XtSetArg(args[0], XtNstring, SFfilterBuffer);
185 XtSetValues(filterField, args, 1);
186 XtSetKeyboardFocus(selFileForm, selFileField);
187 SFstatus = SEL_FILE_TEXT;
190 if(!SFpathFlag) // [HGM] cr: fetch current extenson filter
192 XtSetArg(args[0], XtNstring, &p);
193 XtGetValues(filterField, args, 1);
194 if(strcmp(SFfilterBuffer, p)) SFpurge();
195 strncpy(SFfilterBuffer, p, 40);
196 SFstatus = SEL_FILE_TEXT;
200 SFstatus = (*buf == 033 ? SEL_FILE_CANCEL : SEL_FILE_OK);
202 if(!client_data) SFstatus = SEL_FILE_TEXT;
208 SFokCallback(w, cl, cd)
212 SFstatus = SEL_FILE_OK;
215 static XtCallbackRec SFokSelect[] = {
216 { SFokCallback, (XtPointer) NULL },
217 { NULL, (XtPointer) NULL },
222 SFcancelCallback(w, cl, cd)
226 SFstatus = SEL_FILE_CANCEL;
229 static XtCallbackRec SFcancelSelect[] = {
230 { SFcancelCallback, (XtPointer) NULL },
231 { NULL, (XtPointer) NULL },
236 SFdismissAction(w, event, params, num_params)
240 Cardinal *num_params;
242 if (event->type == ClientMessage &&
243 event->xclient.data.l[0] != SFwmDeleteWindow) return;
245 SFstatus = SEL_FILE_CANCEL;
248 static char *wmDeleteWindowTranslation = "\
249 <Message>WM_PROTOCOLS: SelFileDismiss()\n\
252 static XtActionsRec actions[] = {
253 {"SelFileDismiss", SFdismissAction},
256 void SFsetFocus(Widget w, XtPointer data, XEvent *event, Boolean *b)
258 XtSetKeyboardFocus((Widget) data, w);
261 void SFwheelProc(Widget w, XtPointer data, XEvent *event, Boolean *b)
262 { // [HGM] mouse-wheel callback scrolls lists
263 int dir, n = (intptr_t) data;
264 if(event->xbutton.button == Button4) dir = -2; // kludge to indicate relative motion
265 if(event->xbutton.button == Button5) dir = -1;
266 SFvSliderMovedCallback(w, n, dir);
270 SFcreateWidgets(toplevel, prompt, ok, cancel)
277 int listWidth, listHeight;
278 int listSpacing = 10;
279 int scrollThickness = 15;
280 int hScrollX, hScrollY;
281 int vScrollX, vScrollY;
289 XtSetArg(arglist[i], XtNtransientFor, toplevel); i++;
291 selFile = XtAppCreateShell("Browse", "SelFile",
292 transientShellWidgetClass, SFdisplay, arglist, i);
294 /* Add WM_DELETE_WINDOW protocol */
295 XtAppAddActions(XtWidgetToApplicationContext(selFile),
296 actions, XtNumber(actions));
297 XtOverrideTranslations(selFile,
298 XtParseTranslationTable(wmDeleteWindowTranslation));
301 XtSetArg(arglist[i], XtNdefaultDistance, 30); i++;
302 selFileForm = XtCreateManagedWidget("selFileForm",
303 formWidgetClass, selFile, arglist, i);
306 XtSetArg(arglist[i], XtNlabel, prompt); i++;
307 XtSetArg(arglist[i], XtNresizable, True); i++;
308 XtSetArg(arglist[i], XtNtop, XtChainTop); i++;
309 XtSetArg(arglist[i], XtNbottom, XtChainTop); i++;
310 XtSetArg(arglist[i], XtNleft, XtChainLeft); i++;
311 XtSetArg(arglist[i], XtNright, XtChainLeft); i++;
312 XtSetArg(arglist[i], XtNborderWidth, 0); i++;
313 selFilePrompt = XtCreateManagedWidget("selFilePrompt",
314 labelWidgetClass, selFileForm, arglist, i);
317 XtSetArg(arglist[i], XtNforeground, &SFfore); i++;
318 XtSetArg(arglist[i], XtNbackground, &SFback); i++;
319 XtGetValues(selFilePrompt, arglist, i);
323 SFentryWidth = SFbesideText + SFcharsPerEntry * SFcharWidth +
325 SFentryHeight = SFaboveAndBelowText + SFcharHeight +
328 listWidth = SFlineToTextH + SFentryWidth + SFlineToTextH + 1 +
330 listHeight = SFlineToTextV + SFentryHeight + SFlineToTextV + 1 +
331 SFlineToTextV + SFlistSize * SFentryHeight +
332 SFlineToTextV + 1 + scrollThickness;
334 SFpathScrollWidth = NR * listWidth + (NR-1) * listSpacing + 4;
337 hScrollY = SFlineToTextV + SFentryHeight + SFlineToTextV + 1 +
338 SFlineToTextV + SFlistSize * SFentryHeight +
340 SFhScrollWidth = SFlineToTextH + SFentryWidth + SFlineToTextH;
342 vScrollX = SFlineToTextH + SFentryWidth + SFlineToTextH;
343 vScrollY = SFlineToTextV + SFentryHeight + SFlineToTextV;
344 SFvScrollHeight = SFlineToTextV + SFlistSize * SFentryHeight +
347 SFupperX = SFlineToTextH + SFentryWidth + SFlineToTextH - 1;
348 SFlowerY = SFlineToTextV + SFentryHeight + SFlineToTextV + 1 +
350 SFupperY = SFlineToTextV + SFentryHeight + SFlineToTextV + 1 +
351 SFlineToTextV + SFlistSize * SFentryHeight - 1;
353 SFtextX = SFlineToTextH + SFbesideText;
354 SFtextYoffset = SFlowerY + SFaboveAndBelowText + SFcharAscent;
357 SFsegs[0].y1 = vScrollY;
358 SFsegs[0].x2 = vScrollX - 1;
359 SFsegs[0].y2 = vScrollY;
360 SFsegs[1].x1 = vScrollX;
362 SFsegs[1].x2 = vScrollX;
363 SFsegs[1].y2 = vScrollY - 1;
365 SFcompletionSegs[0].x1 = SFcompletionSegs[0].x2 = SFlineToTextH;
366 SFcompletionSegs[1].x1 = SFcompletionSegs[1].x2 =
367 SFlineToTextH + SFentryWidth - 1;
370 XtSetArg(arglist[i], XtNwidth, NR * listWidth + (NR - 1) * listSpacing + 4);
372 XtSetArg(arglist[i], XtNborderColor, SFfore); i++;
374 XtSetArg(arglist[i], XtNfromVert, selFilePrompt); i++;
375 XtSetArg(arglist[i], XtNvertDistance, 5); i++;
376 XtSetArg(arglist[i], XtNresizable, True); i++;
377 XtSetArg(arglist[i], XtNtop, XtChainTop); i++;
378 XtSetArg(arglist[i], XtNbottom, XtChainTop); i++;
379 XtSetArg(arglist[i], XtNleft, XtChainLeft); i++;
380 XtSetArg(arglist[i], XtNright, XtChainLeft); i++;
381 XtSetArg(arglist[i], XtNstring, SFtextBuffer); i++;
382 XtSetArg(arglist[i], XtNlength, MAXPATHLEN); i++;
383 XtSetArg(arglist[i], XtNeditType, XawtextEdit); i++;
384 XtSetArg(arglist[i], XtNwrap, XawtextWrapWord); i++;
385 XtSetArg(arglist[i], XtNresize, XawtextResizeHeight); i++;
386 selFileField = XtCreateManagedWidget("selFileField",
387 asciiTextWidgetClass, selFileForm, arglist, i);
389 XtOverrideTranslations(selFileField,
390 XtParseTranslationTable(oneLineTextEditTranslations));
391 XtAddEventHandler(selFileField, ButtonPressMask, False, SFsetFocus, (XtPointer) selFileForm);
394 XtSetArg(arglist[i], XtNlabel, "Filter on extensions:"); i++;
395 XtSetArg(arglist[i], XtNvertDistance, 5); i++;
396 XtSetArg(arglist[i], XtNfromVert, selFileField); i++;
397 XtSetArg(arglist[i], XtNresizable, True); i++;
398 XtSetArg(arglist[i], XtNtop, XtChainTop); i++;
399 XtSetArg(arglist[i], XtNbottom, XtChainTop); i++;
400 XtSetArg(arglist[i], XtNleft, XtChainLeft); i++;
401 XtSetArg(arglist[i], XtNright, XtChainLeft); i++;
402 XtSetArg(arglist[i], XtNborderWidth, 0); i++;
403 selFileMess = XtCreateManagedWidget("selFileMess",
404 labelWidgetClass, selFileForm, arglist, i);
407 XtSetArg(arglist[i], XtNwidth, NR * listWidth + (NR - 1) * listSpacing + 4);
409 XtSetArg(arglist[i], XtNborderColor, SFfore); i++;
410 XtSetArg(arglist[i], XtNvertDistance, 5); i++;
411 XtSetArg(arglist[i], XtNfromVert, selFileMess); i++;
412 XtSetArg(arglist[i], XtNresizable, True); i++;
413 XtSetArg(arglist[i], XtNtop, XtChainTop); i++;
414 XtSetArg(arglist[i], XtNbottom, XtChainTop); i++;
415 XtSetArg(arglist[i], XtNleft, XtChainLeft); i++;
416 XtSetArg(arglist[i], XtNright, XtChainLeft); i++;
417 XtSetArg(arglist[i], XtNlength, MAXPATHLEN); i++;
418 XtSetArg(arglist[i], XtNeditType, XawtextEdit); i++;
419 XtSetArg(arglist[i], XtNwrap, XawtextWrapWord); i++;
420 XtSetArg(arglist[i], XtNresize, XawtextResizeHeight); i++;
421 XtSetArg(arglist[i], XtNuseStringInPlace, False); i++;
422 filterField = XtCreateManagedWidget("filterField",
423 asciiTextWidgetClass, selFileForm, arglist, i);
425 XtOverrideTranslations(filterField,
426 XtParseTranslationTable(oneLineTextEditTranslations));
427 XtAddEventHandler(filterField, ButtonPressMask, False, SFsetFocus, (XtPointer) selFileForm);
430 XtSetArg(arglist[i], XtNorientation, XtorientHorizontal); i++;
431 XtSetArg(arglist[i], XtNwidth, SFpathScrollWidth); i++;
432 XtSetArg(arglist[i], XtNheight, scrollThickness); i++;
433 XtSetArg(arglist[i], XtNborderColor, SFfore); i++;
434 XtSetArg(arglist[i], XtNfromVert, filterField); i++;
435 XtSetArg(arglist[i], XtNvertDistance, 10); i++;
436 XtSetArg(arglist[i], XtNtop, XtChainTop); i++;
437 XtSetArg(arglist[i], XtNbottom, XtChainTop); i++;
438 XtSetArg(arglist[i], XtNleft, XtChainLeft); i++;
439 XtSetArg(arglist[i], XtNright, XtChainLeft); i++;
440 selFileHScroll = XtCreateManagedWidget("selFileHScroll",
441 scrollbarWidgetClass, selFileForm, arglist, i);
443 XtAddCallback(selFileHScroll, XtNjumpProc,
444 SFpathSliderMovedCallback, (XtPointer) NULL);
445 XtAddCallback(selFileHScroll, XtNscrollProc,
446 SFpathAreaSelectedCallback, (XtPointer) NULL);
449 XtSetArg(arglist[i], XtNwidth, listWidth); i++;
450 XtSetArg(arglist[i], XtNheight, listHeight); i++;
451 XtSetArg(arglist[i], XtNborderColor, SFfore); i++;
452 XtSetArg(arglist[i], XtNfromVert, selFileHScroll); i++;
453 XtSetArg(arglist[i], XtNvertDistance, 10); i++;
454 XtSetArg(arglist[i], XtNtop, XtChainTop); i++;
455 XtSetArg(arglist[i], XtNbottom, XtChainTop); i++;
456 XtSetArg(arglist[i], XtNleft, XtChainLeft); i++;
457 XtSetArg(arglist[i], XtNright, XtChainLeft); i++;
458 selFileLists[0] = XtCreateManagedWidget("selFileList1",
459 compositeWidgetClass, selFileForm, arglist, i);
462 XtSetArg(arglist[i], XtNwidth, listWidth); i++;
463 XtSetArg(arglist[i], XtNheight, listHeight); i++;
464 XtSetArg(arglist[i], XtNborderColor, SFfore); i++;
465 XtSetArg(arglist[i], XtNfromHoriz, selFileLists[0]); i++;
466 XtSetArg(arglist[i], XtNfromVert, selFileHScroll); i++;
467 XtSetArg(arglist[i], XtNhorizDistance, listSpacing); i++;
468 XtSetArg(arglist[i], XtNvertDistance, 10); i++;
469 XtSetArg(arglist[i], XtNtop, XtChainTop); i++;
470 XtSetArg(arglist[i], XtNbottom, XtChainTop); i++;
471 XtSetArg(arglist[i], XtNleft, XtChainLeft); i++;
472 XtSetArg(arglist[i], XtNright, XtChainLeft); i++;
473 selFileLists[1] = XtCreateManagedWidget("selFileList2",
474 compositeWidgetClass, selFileForm, arglist, i);
477 XtSetArg(arglist[i], XtNwidth, listWidth); i++;
478 XtSetArg(arglist[i], XtNheight, listHeight); i++;
479 XtSetArg(arglist[i], XtNborderColor, SFfore); i++;
480 XtSetArg(arglist[i], XtNfromHoriz, selFileLists[1]); i++;
481 XtSetArg(arglist[i], XtNfromVert, selFileHScroll); i++;
482 XtSetArg(arglist[i], XtNhorizDistance, listSpacing); i++;
483 XtSetArg(arglist[i], XtNvertDistance, 10); i++;
484 XtSetArg(arglist[i], XtNtop, XtChainTop); i++;
485 XtSetArg(arglist[i], XtNbottom, XtChainTop); i++;
486 XtSetArg(arglist[i], XtNleft, XtChainLeft); i++;
487 XtSetArg(arglist[i], XtNright, XtChainLeft); i++;
488 selFileLists[2] = XtCreateManagedWidget("selFileList3",
489 compositeWidgetClass, selFileForm, arglist, i);
491 for (n = 0; n < NR; n++) {
494 XtSetArg(arglist[i], XtNx, vScrollX); i++;
495 XtSetArg(arglist[i], XtNy, vScrollY); i++;
496 XtSetArg(arglist[i], XtNwidth, scrollThickness); i++;
497 XtSetArg(arglist[i], XtNheight, SFvScrollHeight); i++;
498 XtSetArg(arglist[i], XtNborderColor, SFfore); i++;
499 selFileVScrolls[n] = XtCreateManagedWidget("selFileVScroll",
500 scrollbarWidgetClass, selFileLists[n], arglist, i);
502 XtAddCallback(selFileVScrolls[n], XtNjumpProc,
503 SFvFloatSliderMovedCallback, (XtPointer)(intptr_t) n);
504 XtAddCallback(selFileVScrolls[n], XtNscrollProc,
505 SFvAreaSelectedCallback, (XtPointer)(intptr_t) n);
509 XtSetArg(arglist[i], XtNorientation, XtorientHorizontal);
511 XtSetArg(arglist[i], XtNx, hScrollX); i++;
512 XtSetArg(arglist[i], XtNy, hScrollY); i++;
513 XtSetArg(arglist[i], XtNwidth, SFhScrollWidth); i++;
514 XtSetArg(arglist[i], XtNheight, scrollThickness); i++;
515 XtSetArg(arglist[i], XtNborderColor, SFfore); i++;
516 selFileHScrolls[n] = XtCreateManagedWidget("selFileHScroll",
517 scrollbarWidgetClass, selFileLists[n], arglist, i);
519 XtAddCallback(selFileHScrolls[n], XtNjumpProc,
520 SFhSliderMovedCallback, (XtPointer)(intptr_t) n);
521 XtAddCallback(selFileHScrolls[n], XtNscrollProc,
522 SFhAreaSelectedCallback, (XtPointer)(intptr_t) n);
524 XtAddEventHandler(selFileVScrolls[n], ButtonPressMask, False,
525 SFwheelProc, (XtPointer)(intptr_t) n); // [HGM] couplemouse wheel to v-scroll
529 XtSetArg(arglist[i], XtNlabel, ok); i++;
530 XtSetArg(arglist[i], XtNresizable, True); i++;
531 XtSetArg(arglist[i], XtNcallback, SFokSelect); i++;
532 XtSetArg(arglist[i], XtNborderColor, SFfore); i++;
533 XtSetArg(arglist[i], XtNfromVert, selFileLists[0]); i++;
534 XtSetArg(arglist[i], XtNvertDistance, 30); i++;
535 XtSetArg(arglist[i], XtNtop, XtChainTop); i++;
536 XtSetArg(arglist[i], XtNbottom, XtChainTop); i++;
537 XtSetArg(arglist[i], XtNleft, XtChainLeft); i++;
538 XtSetArg(arglist[i], XtNright, XtChainLeft); i++;
539 selFileOK = XtCreateManagedWidget("selFileOK", commandWidgetClass,
540 selFileForm, arglist, i);
543 XtSetArg(arglist[i], XtNlabel, cancel); i++;
544 XtSetArg(arglist[i], XtNresizable, True); i++;
545 XtSetArg(arglist[i], XtNcallback, SFcancelSelect); i++;
546 XtSetArg(arglist[i], XtNborderColor, SFfore); i++;
547 XtSetArg(arglist[i], XtNfromHoriz, selFileOK); i++;
548 XtSetArg(arglist[i], XtNfromVert, selFileLists[0]); i++;
549 XtSetArg(arglist[i], XtNhorizDistance, 30); i++;
550 XtSetArg(arglist[i], XtNvertDistance, 30); i++;
551 XtSetArg(arglist[i], XtNtop, XtChainTop); i++;
552 XtSetArg(arglist[i], XtNbottom, XtChainTop); i++;
553 XtSetArg(arglist[i], XtNleft, XtChainLeft); i++;
554 XtSetArg(arglist[i], XtNright, XtChainLeft); i++;
555 selFileCancel = XtCreateManagedWidget("selFileCancel",
556 commandWidgetClass, selFileForm, arglist, i);
558 XtSetMappedWhenManaged(selFile, False);
559 XtRealizeWidget(selFile);
561 /* Add WM_DELETE_WINDOW protocol */
562 SFwmDeleteWindow = XInternAtom(SFdisplay, "WM_DELETE_WINDOW", False);
563 XSetWMProtocols(SFdisplay, XtWindow(selFile), &SFwmDeleteWindow, 1);
567 xtermCursor = XCreateFontCursor(SFdisplay, XC_xterm);
569 sbRightArrowCursor = XCreateFontCursor(SFdisplay, XC_sb_right_arrow);
570 dotCursor = XCreateFontCursor(SFdisplay, XC_dot);
572 XDefineCursor(SFdisplay, XtWindow(selFileForm), xtermCursor);
573 XDefineCursor(SFdisplay, XtWindow(selFileField), xtermCursor);
574 XDefineCursor(SFdisplay, XtWindow(filterField), xtermCursor);
576 for (n = 0; n < NR; n++) {
577 XDefineCursor(SFdisplay, XtWindow(selFileLists[n]),
580 XDefineCursor(SFdisplay, XtWindow(selFileOK), dotCursor);
581 XDefineCursor(SFdisplay, XtWindow(selFileCancel), dotCursor);
583 for (n = 0; n < NR; n++) {
584 XtAddEventHandler(selFileLists[n], ExposureMask, True,
585 SFexposeList, (XtPointer)(intptr_t) n);
586 XtAddEventHandler(selFileLists[n], EnterWindowMask, False,
587 SFenterList, (XtPointer)(intptr_t) n);
588 XtAddEventHandler(selFileLists[n], LeaveWindowMask, False,
589 SFleaveList, (XtPointer)(intptr_t) n);
590 XtAddEventHandler(selFileLists[n], PointerMotionMask, False,
591 (XtEventHandler) SFmotionList, (XtPointer)(intptr_t) n);
592 XtAddEventHandler(selFileLists[n], ButtonPressMask, False,
593 SFbuttonPressList, (XtPointer)(intptr_t) n);
594 XtAddEventHandler(selFileLists[n], ButtonReleaseMask, False,
595 SFbuttonReleaseList, (XtPointer)(intptr_t) n);
598 XtAddEventHandler(selFileField, KeyPressMask, False,
599 SFmodVerifyCallback, (XtPointer) NULL);
600 XtAddEventHandler(filterField, KeyReleaseMask, False,
601 SFmodVerifyCallback, (XtPointer) 1);
602 XtSetKeyboardFocus(selFileForm, selFileField);
604 SFapp = XtWidgetToApplicationContext(selFile);
608 /* position widget under the cursor */
615 Dimension width, height, b_width;
616 int x, y, max_x, max_y;
619 unsigned int dummymask;
621 XQueryPointer(XtDisplay(w), XtWindow(w), &root, &child, &x, &y,
622 &dummyx, &dummyy, &dummymask);
624 XtSetArg(args[num_args], XtNwidth, &width); num_args++;
625 XtSetArg(args[num_args], XtNheight, &height); num_args++;
626 XtSetArg(args[num_args], XtNborderWidth, &b_width); num_args++;
627 XtGetValues(w, args, num_args);
629 width += 2 * b_width;
630 height += 2 * b_width;
632 x -= ( (Position) width/2 );
634 if ( x > (max_x = (Position) (XtScreen(w)->width - width)) ) x = max_x;
636 y -= ( (Position) height/2 );
638 if ( y > (max_y = (Position) (XtScreen(w)->height - height)) ) y = max_y;
641 XtSetArg(args[num_args], XtNx, x); num_args++;
642 XtSetArg(args[num_args], XtNy, y); num_args++;
643 XtSetValues(w, args, num_args);
647 SFopenFile(name, mode, prompt, failed)
657 if ((fp = fopen(name, mode)) == NULL) {
659 if (1) { // [HGM] always use strerror
660 buf = XtMalloc(strlen(failed) + strlen(strerror(errno)) +
663 strcat(buf, strerror(errno));
667 buf = XtMalloc(strlen(failed) + strlen(prompt) + 2);
672 XtSetArg(args[0], XtNlabel, buf);
673 XtSetValues(selFilePrompt, args, ONE);
688 XtSetArg(arglist[i], XtNstring, &v); i++;
689 XtGetValues(selFileField, arglist, i);
690 strncpy(SFtextBuffer, v, MAXPATHLEN);
701 XtSetArg(arglist[i], XtNstring, path); i++;
702 XtSetValues(selFileField, arglist, i);
703 XawTextSetInsertionPoint(selFileField, strlen(path));
709 SFupdateTextBuffer();
711 if ((SFtextBuffer[0] == '/') || (SFtextBuffer[0] == '~')) {
712 (void) strncpy(SFcurrentPath, SFtextBuffer, MAXPATHLEN);
714 SFtextPos = XawTextGetInsertionPoint(selFileField);
716 (void) strcat(strncpy(SFcurrentPath, SFstartDir, MAXPATHLEN), SFtextBuffer);
718 SFtextPos = XawTextGetInsertionPoint(selFileField) +
722 if (!SFworkProcAdded) {
723 (void) XtAppAddWorkProc(SFapp, SFworkProc, NULL);
734 return strcpy(XtMalloc((unsigned) (strlen(SFtextBuffer) + 1)),
741 SFstatus = SEL_FILE_NULL;
742 XtRemoveGrab(selFile);
743 XtUnmapWidget(selFile);
744 XtRemoveTimeOut(SFdirModTimerId);
745 if (SFchdir(SFstartDir)) {
748 "XsraSelFile: can't return to current directory"
755 XsraSelFile(toplevel, prompt, ok, cancel, failed,
756 init_path, filter, mode, show_entry, name_return)
768 static int firstTime = 1;
775 prompt = "Pathname:";
786 if(SFpathFlag != (mode && mode[0] == 'p') || strcmp(SFfilterBuffer, filter)) {
788 SFpathFlag = (mode && mode[0] == 'p'); // [HGM] ignore everything that is not a directory
793 SFdisplay = XtDisplay(toplevel);
794 SFcreateWidgets(toplevel, prompt, ok, cancel);
798 XtSetArg(arglist[i], XtNlabel, prompt); i++;
799 XtSetValues(selFilePrompt, arglist, i);
802 XtSetArg(arglist[i], XtNlabel, ok); i++;
803 XtSetValues(selFileOK, arglist, i);
806 XtSetArg(arglist[i], XtNlabel, cancel); i++;
807 XtSetValues(selFileCancel, arglist, i);
811 XtSetArg(arglist[i], XtNstring, filter); i++;
812 XtSetValues(filterField, arglist, i);
814 strncpy(SFfilterBuffer, filter, MAXPATHLEN-1);
815 SFupdateTextBuffer();
816 strncpy(SFlastPath, SFtextBuffer, MAXPATHLEN-1); // remember for cancel
818 SFpositionWidget(selFile);
819 XtMapWidget(selFile);
821 #if defined(SVR4) || defined(SYSV) || defined(USG) || 1
822 if (!getcwd(SFstartDir, MAXPATHLEN)) { // [HGM] always do this, as I do not know when exactly to do it
823 #else /* defined(SVR4) || defined(SYSV) || defined(USG) */
824 if (!getwd(SFstartDir)) {
825 #endif /* defined(SVR4) || defined(SYSV) || defined(USG) */
827 XtAppError(SFapp, "XsraSelFile: can't get current directory");
829 (void) strcat(SFstartDir, "/");
830 (void) strncpy(SFcurrentDir, SFstartDir, MAXPATHLEN);
833 if (init_path[0] == '/') {
834 (void) strncpy(SFcurrentPath, init_path, MAXPATHLEN);
840 SFsetText(SFcurrentPath);
842 SFsetText(&(SFcurrentPath[strlen(SFstartDir)]));
845 (void) strcat(strncpy(SFcurrentPath, SFstartDir, MAXPATHLEN),
847 SFsetText(&(SFcurrentPath[strlen(SFstartDir)]));
850 (void) strncpy(SFcurrentPath, SFstartDir, MAXPATHLEN);
857 XtAddGrab(selFile, True, True);
859 SFdirModTimerId = XtAppAddTimeOut(SFapp, (unsigned long) 1000,
860 SFdirModTimer, (XtPointer) NULL);
863 XtAppNextEvent(SFapp, &event);
864 XtDispatchEvent(&event);
867 SFstatus = SEL_FILE_NULL;
871 *name_return = SFgetText();
872 if(mode && (mode[0] == 'p' || mode[0] == 'f')) { // [HGM] for use in file-option browse button
876 if ((*name_return)[strlen(*name_return)-1] != '/' && // [HGM] refuse directories
877 (fp = SFopenFile(*name_return, mode, prompt, failed))) {
881 SFstatus = SEL_FILE_NULL;
883 case SEL_FILE_CANCEL:
885 SFsetText(SFlastPath);