New browser
[xboard.git] / filebrowser / selfile.c
diff --git a/filebrowser/selfile.c b/filebrowser/selfile.c
deleted file mode 100644 (file)
index 729453b..0000000
+++ /dev/null
@@ -1,902 +0,0 @@
-/*
- * Copyright 1989 Software Research Associates, Inc., Tokyo, Japan
- *
- * 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 Software Research Associates not be used
- * in advertising or publicity pertaining to distribution of the software
- * without specific, written prior permission.  Software Research Associates
- * makes no representations about the suitability of this software for any
- * purpose.  It is provided "as is" without express or implied warranty.
- *
- * SOFTWARE RESEARCH ASSOCIATES DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
- * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
- * IN NO EVENT SHALL SOFTWARE RESEARCH ASSOCIATES BE LIABLE FOR ANY SPECIAL,
- * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- * PERFORMANCE OF THIS SOFTWARE.
- *
- * Author: Erik M. van der Poel
- *         Software Research Associates, Inc., Tokyo, Japan
- *         erik@sra.co.jp
- */
-
-/*
- * Author's address:
- *
- *     erik@sra.co.jp
- *                                            OR
- *     erik%sra.co.jp@uunet.uu.net
- *                                            OR
- *     erik%sra.co.jp@mcvax.uucp
- *                                            OR
- *     try junet instead of co.jp
- *                                            OR
- *     Erik M. van der Poel
- *     Software Research Associates, Inc.
- *     1-1-1 Hirakawa-cho, Chiyoda-ku
- *     Tokyo 102 Japan. TEL +81-3-234-2692
- */
-
-#include <stdio.h>
-#include <errno.h>
-/* BSD 4.3 errno.h does not declare errno */
-extern int errno;
-//extern int sys_nerr;
-//extern char *sys_errlist[]; // [HGM] this produced a compile error in Ubuntu 8.04
-
-#include <sys/param.h>
-#include <X11/cursorfont.h>
-#include <X11/Intrinsic.h>
-#include <X11/StringDefs.h>
-#include <X11/Composite.h>
-#include <X11/Shell.h>
-#include <X11/Xaw/Form.h>
-#include <X11/Xaw/Command.h>
-#include <X11/Xaw/Scrollbar.h>
-#include <X11/Xaw/Label.h>
-#include <X11/Xaw/Cardinals.h>
-
-#include "xstat.h"
-#include "selfile.h"
-#include "../gettext.h"
-
-#ifndef MAXPATHLEN
-#define MAXPATHLEN 1024
-#endif /* ndef MAXPATHLEN */
-
-#if !defined(SVR4) && !defined(SYSV) && !defined(USG)
-extern char *getwd();
-#endif /* !defined(SVR4) && !defined(SYSV) && !defined(USG) */
-
-#ifdef ENABLE_NLS
-# define  _(s) gettext (s)
-# define N_(s) gettext_noop (s)
-#else
-# define  _(s) (s)
-# define N_(s)  s
-#endif
-
-
-int SFstatus = SEL_FILE_NULL;
-
-char
-       SFstartDir[MAXPATHLEN+1],
-       SFcurrentPath[MAXPATHLEN+1],
-       SFlastPath[MAXPATHLEN+1],
-       SFcurrentDir[MAXPATHLEN+1];
-
-Widget
-       selFile,
-       selFileCancel,
-       selFileField,
-       selFileMess,
-       filterField,
-       selFileForm,
-       selFileHScroll,
-       selFileHScrolls[3],
-       selFileLists[3],
-       selFileOK,
-       selFilePrompt,
-       selFileVScrolls[3];
-
-Display *SFdisplay;
-
-Pixel SFfore, SFback;
-
-Atom   SFwmDeleteWindow;
-
-XSegment SFsegs[2], SFcompletionSegs[2];
-
-XawTextPosition SFtextPos;
-
-int SFupperX, SFlowerY, SFupperY;
-
-int SFtextX, SFtextYoffset;
-
-int SFentryWidth, SFentryHeight;
-
-int SFlineToTextH = 3;
-
-int SFlineToTextV = 3;
-
-int SFbesideText = 3;
-
-int SFaboveAndBelowText = 2;
-
-int SFcharsPerEntry = 15;
-
-int SFlistSize = 10;
-
-int SFworkProcAdded = 0;
-
-XtAppContext SFapp;
-
-int SFpathScrollWidth, SFvScrollHeight, SFhScrollWidth;
-
-char SFtextBuffer[MAXPATHLEN+1];
-
-char SFfilterBuffer[MAXPATHLEN+1];
-
-XtIntervalId SFdirModTimerId;
-
-int (*SFfunc)();
-
-Boolean SFpathFlag; // [HGM]
-
-static char *oneLineTextEditTranslations = "\
-       <Key>Return:    redraw-display()\n\
-       Ctrl<Key>M:     redraw-display()\n\
-";
-
-/* ARGSUSED */
-static void
-SFexposeList(w, n, event, cont)
-       Widget          w;
-       XtPointer       n;
-        XEvent         *event;
-        Boolean         *cont;
-{
-       if ((event->type == NoExpose) || event->xexpose.count) {
-               return;
-       }
-
-       SFdrawList((int)(intptr_t)n, SF_DO_NOT_SCROLL);
-}
-
-void
-SFpurge()
-{
-       if(SFdirs) XtFree((XtPointer) SFdirs);
-       SFdirs = NULL; // kludge to throw away all cached info
-}
-
-/* ARGSUSED */
-static void
-SFmodVerifyCallback(w, client_data, event, cont)
-       Widget                  w;
-       XtPointer               client_data;
-        XEvent                 *event;
-        Boolean                 *cont;
-{
-       char    buf[2];
-
-       if (
-               (XLookupString(&(event->xkey), buf, 2, NULL, NULL) == 1) &&
-               ((*buf) == '\r' || *buf == 033)
-       ) {
-               if(client_data) {
-                   Arg args[10]; char *p;
-                   if(*buf == 033) { // [HGM] esc in filter: restore and give focus to path
-                       XtSetArg(args[0], XtNstring, SFfilterBuffer);
-                       XtSetValues(filterField, args, 1);
-                       XtSetKeyboardFocus(selFileForm, selFileField);
-                       SFstatus = SEL_FILE_TEXT;
-                       return;
-                   } else
-                   if(!SFpathFlag) // [HGM] cr: fetch current extenson filter
-                   {   
-                       XtSetArg(args[0], XtNstring, &p);
-                       XtGetValues(filterField, args, 1);
-                       if(strcmp(SFfilterBuffer, p)) SFpurge();
-                       strncpy(SFfilterBuffer, p, 40);
-                       SFstatus = SEL_FILE_TEXT;
-                   }
-                   return;
-               }
-               SFstatus = (*buf == 033 ? SEL_FILE_CANCEL : SEL_FILE_OK);
-       } else {
-               if(!client_data) SFstatus = SEL_FILE_TEXT;
-       }
-}
-
-/* ARGSUSED */
-static void
-SFokCallback(w, cl, cd)
-       Widget  w;
-        XtPointer cl, cd;
-{
-       SFstatus = SEL_FILE_OK;
-}
-
-static XtCallbackRec SFokSelect[] = {
-       { SFokCallback, (XtPointer) NULL },
-       { NULL, (XtPointer) NULL },
-};
-
-/* ARGSUSED */
-static void
-SFcancelCallback(w, cl, cd)
-       Widget  w;
-        XtPointer cl, cd;
-{
-       SFstatus = SEL_FILE_CANCEL;
-}
-
-static XtCallbackRec SFcancelSelect[] = {
-       { SFcancelCallback, (XtPointer) NULL },
-       { NULL, (XtPointer) NULL },
-};
-
-/* ARGSUSED */
-static void
-SFdismissAction(w, event, params, num_params)
-       Widget  w;
-       XEvent *event;
-       String *params;
-       Cardinal *num_params;
-{
-       if (event->type == ClientMessage &&
-           event->xclient.data.l[0] != SFwmDeleteWindow) return;
-
-       SFstatus = SEL_FILE_CANCEL;
-}
-
-static char *wmDeleteWindowTranslation = "\
-       <Message>WM_PROTOCOLS:  SelFileDismiss()\n\
-";
-
-static XtActionsRec actions[] = {
-       {"SelFileDismiss",      SFdismissAction},
-};
-
-void SFsetFocus(Widget w, XtPointer data, XEvent *event, Boolean *b)
-{
-    XtSetKeyboardFocus((Widget) data, w);
-}
-
-void SFwheelProc(Widget w, XtPointer data, XEvent *event, Boolean *b)
-{   // [HGM] mouse-wheel callback scrolls lists
-    int dir, n = (intptr_t) data;
-    if(event->xbutton.button == Button4) dir = -2; // kludge to indicate relative motion
-    if(event->xbutton.button == Button5) dir = -1;
-    SFvSliderMovedCallback(w, n, dir);
-}
-
-static void
-SFcreateWidgets(toplevel, prompt, ok, cancel)
-       Widget  toplevel;
-       char    *prompt;
-       char    *ok;
-       char    *cancel;
-{
-       Cardinal        i, n;
-       int             listWidth, listHeight;
-       int             listSpacing = 10;
-       int             scrollThickness = 15;
-       int             hScrollX, hScrollY;
-       int             vScrollX, vScrollY;
-       Cursor
-                       xtermCursor,
-                       sbRightArrowCursor,
-                       dotCursor;
-       Arg             arglist[20];
-
-       i = 0;
-       XtSetArg(arglist[i], XtNtransientFor, toplevel);                i++;
-
-       selFile = XtAppCreateShell(_("Browse"), "SelFile",
-               transientShellWidgetClass, SFdisplay, arglist, i);
-
-       /* Add WM_DELETE_WINDOW protocol */
-       XtAppAddActions(XtWidgetToApplicationContext(selFile),
-               actions, XtNumber(actions));
-       XtOverrideTranslations(selFile,
-               XtParseTranslationTable(wmDeleteWindowTranslation));
-
-       i = 0;
-       XtSetArg(arglist[i], XtNdefaultDistance, 30);                   i++;
-       selFileForm = XtCreateManagedWidget("selFileForm",
-               formWidgetClass, selFile, arglist, i);
-
-       i = 0;
-       XtSetArg(arglist[i], XtNlabel, prompt);                         i++;
-       XtSetArg(arglist[i], XtNresizable, True);                       i++;
-       XtSetArg(arglist[i], XtNtop, XtChainTop);                       i++;
-       XtSetArg(arglist[i], XtNbottom, XtChainTop);                    i++;
-       XtSetArg(arglist[i], XtNleft, XtChainLeft);                     i++;
-       XtSetArg(arglist[i], XtNright, XtChainLeft);                    i++;
-       XtSetArg(arglist[i], XtNborderWidth, 0);                        i++;
-       selFilePrompt = XtCreateManagedWidget("selFilePrompt",
-               labelWidgetClass, selFileForm, arglist, i);
-
-       i = 0;
-       XtSetArg(arglist[i], XtNforeground, &SFfore);                   i++;
-       XtSetArg(arglist[i], XtNbackground, &SFback);                   i++;
-       XtGetValues(selFilePrompt, arglist, i);
-
-       SFinitFont();
-
-       SFentryWidth = SFbesideText + SFcharsPerEntry * SFcharWidth +
-                       SFbesideText;
-       SFentryHeight = SFaboveAndBelowText + SFcharHeight +
-                       SFaboveAndBelowText;
-
-       listWidth = SFlineToTextH + SFentryWidth + SFlineToTextH + 1 +
-                       scrollThickness;
-       listHeight = SFlineToTextV + SFentryHeight + SFlineToTextV + 1 +
-                       SFlineToTextV + SFlistSize * SFentryHeight +
-                       SFlineToTextV + 1 + scrollThickness;
-
-       SFpathScrollWidth = NR * listWidth + (NR-1) * listSpacing + 4;
-
-       hScrollX = -1;
-       hScrollY = SFlineToTextV + SFentryHeight + SFlineToTextV + 1 +
-                       SFlineToTextV + SFlistSize * SFentryHeight +
-                       SFlineToTextV;
-       SFhScrollWidth = SFlineToTextH + SFentryWidth + SFlineToTextH;
-
-       vScrollX = SFlineToTextH + SFentryWidth + SFlineToTextH;
-       vScrollY = SFlineToTextV + SFentryHeight + SFlineToTextV;
-       SFvScrollHeight = SFlineToTextV + SFlistSize * SFentryHeight +
-                       SFlineToTextV;
-
-       SFupperX = SFlineToTextH + SFentryWidth + SFlineToTextH - 1;
-       SFlowerY = SFlineToTextV + SFentryHeight + SFlineToTextV + 1 +
-                       SFlineToTextV;
-       SFupperY = SFlineToTextV + SFentryHeight + SFlineToTextV + 1 +
-                       SFlineToTextV + SFlistSize * SFentryHeight - 1;
-
-       SFtextX = SFlineToTextH + SFbesideText;
-       SFtextYoffset = SFlowerY + SFaboveAndBelowText + SFcharAscent;
-
-       SFsegs[0].x1 = 0;
-       SFsegs[0].y1 = vScrollY;
-       SFsegs[0].x2 = vScrollX - 1;
-       SFsegs[0].y2 = vScrollY;
-       SFsegs[1].x1 = vScrollX;
-       SFsegs[1].y1 = 0;
-       SFsegs[1].x2 = vScrollX;
-       SFsegs[1].y2 = vScrollY - 1;
-
-       SFcompletionSegs[0].x1 = SFcompletionSegs[0].x2 = SFlineToTextH;
-       SFcompletionSegs[1].x1 = SFcompletionSegs[1].x2 =
-               SFlineToTextH + SFentryWidth - 1;
-
-       i = 0;
-       XtSetArg(arglist[i], XtNwidth, NR * listWidth + (NR - 1) * listSpacing + 4);
-                                                                       i++;
-       XtSetArg(arglist[i], XtNborderColor, SFfore);                   i++;
-
-       XtSetArg(arglist[i], XtNfromVert, selFilePrompt);               i++;
-       XtSetArg(arglist[i], XtNvertDistance, 5);                       i++;
-       XtSetArg(arglist[i], XtNresizable, True);                       i++;
-       XtSetArg(arglist[i], XtNtop, XtChainTop);                       i++;
-       XtSetArg(arglist[i], XtNbottom, XtChainTop);                    i++;
-       XtSetArg(arglist[i], XtNleft, XtChainLeft);                     i++;
-       XtSetArg(arglist[i], XtNright, XtChainLeft);                    i++;
-       XtSetArg(arglist[i], XtNstring, SFtextBuffer);                  i++;
-       XtSetArg(arglist[i], XtNlength, MAXPATHLEN);                    i++;
-       XtSetArg(arglist[i], XtNeditType, XawtextEdit);                 i++;
-       XtSetArg(arglist[i], XtNwrap, XawtextWrapWord);                 i++;
-       XtSetArg(arglist[i], XtNresize, XawtextResizeHeight);           i++;
-       selFileField = XtCreateManagedWidget("selFileField",
-               asciiTextWidgetClass, selFileForm, arglist, i);
-
-       XtOverrideTranslations(selFileField,
-               XtParseTranslationTable(oneLineTextEditTranslations));
-       XtAddEventHandler(selFileField, ButtonPressMask, False, SFsetFocus, (XtPointer) selFileForm);
-
-       i = 0;
-       XtSetArg(arglist[i], XtNlabel, _("Filter on extensions:"));     i++;
-       XtSetArg(arglist[i], XtNvertDistance, 5);                       i++;
-       XtSetArg(arglist[i], XtNfromVert, selFileField);                i++;
-       XtSetArg(arglist[i], XtNresizable, True);                       i++;
-       XtSetArg(arglist[i], XtNtop, XtChainTop);                       i++;
-       XtSetArg(arglist[i], XtNbottom, XtChainTop);                    i++;
-       XtSetArg(arglist[i], XtNleft, XtChainLeft);                     i++;
-       XtSetArg(arglist[i], XtNright, XtChainLeft);                    i++;
-       XtSetArg(arglist[i], XtNborderWidth, 0);                        i++;
-       selFileMess = XtCreateManagedWidget("selFileMess",
-               labelWidgetClass, selFileForm, arglist, i);
-
-       i = 0;
-       XtSetArg(arglist[i], XtNwidth, NR * listWidth + (NR - 1) * listSpacing + 4);
-                                                                       i++;
-       XtSetArg(arglist[i], XtNborderColor, SFfore);                   i++;
-       XtSetArg(arglist[i], XtNvertDistance, 5);                       i++;
-       XtSetArg(arglist[i], XtNfromVert, selFileMess);                 i++;
-       XtSetArg(arglist[i], XtNresizable, True);                       i++;
-       XtSetArg(arglist[i], XtNtop, XtChainTop);                       i++;
-       XtSetArg(arglist[i], XtNbottom, XtChainTop);                    i++;
-       XtSetArg(arglist[i], XtNleft, XtChainLeft);                     i++;
-       XtSetArg(arglist[i], XtNright, XtChainLeft);                    i++;
-       XtSetArg(arglist[i], XtNlength, MAXPATHLEN);                    i++;
-       XtSetArg(arglist[i], XtNeditType, XawtextEdit);                 i++;
-       XtSetArg(arglist[i], XtNwrap, XawtextWrapWord);                 i++;
-       XtSetArg(arglist[i], XtNresize, XawtextResizeHeight);           i++;
-       XtSetArg(arglist[i], XtNuseStringInPlace, False);               i++;
-       filterField = XtCreateManagedWidget("filterField",
-               asciiTextWidgetClass, selFileForm, arglist, i);
-
-       XtOverrideTranslations(filterField,
-               XtParseTranslationTable(oneLineTextEditTranslations));
-       XtAddEventHandler(filterField, ButtonPressMask, False, SFsetFocus, (XtPointer) selFileForm);
-
-       i = 0;
-       XtSetArg(arglist[i], XtNorientation, XtorientHorizontal);       i++;
-       XtSetArg(arglist[i], XtNwidth, SFpathScrollWidth);              i++;
-       XtSetArg(arglist[i], XtNheight, scrollThickness);               i++;
-       XtSetArg(arglist[i], XtNborderColor, SFfore);                   i++;
-       XtSetArg(arglist[i], XtNfromVert, filterField);                 i++;
-       XtSetArg(arglist[i], XtNvertDistance, 10);                      i++;
-       XtSetArg(arglist[i], XtNtop, XtChainTop);                       i++;
-       XtSetArg(arglist[i], XtNbottom, XtChainTop);                    i++;
-       XtSetArg(arglist[i], XtNleft, XtChainLeft);                     i++;
-       XtSetArg(arglist[i], XtNright, XtChainLeft);                    i++;
-       selFileHScroll = XtCreateManagedWidget("selFileHScroll",
-               scrollbarWidgetClass, selFileForm, arglist, i);
-
-       XtAddCallback(selFileHScroll, XtNjumpProc,
-               SFpathSliderMovedCallback, (XtPointer) NULL);
-       XtAddCallback(selFileHScroll, XtNscrollProc,
-               SFpathAreaSelectedCallback, (XtPointer) NULL);
-
-       i = 0;
-       XtSetArg(arglist[i], XtNwidth, listWidth);                      i++;
-       XtSetArg(arglist[i], XtNheight, listHeight);                    i++;
-       XtSetArg(arglist[i], XtNborderColor, SFfore);                   i++;
-       XtSetArg(arglist[i], XtNfromVert, selFileHScroll);              i++;
-       XtSetArg(arglist[i], XtNvertDistance, 10);                      i++;
-       XtSetArg(arglist[i], XtNtop, XtChainTop);                       i++;
-       XtSetArg(arglist[i], XtNbottom, XtChainTop);                    i++;
-       XtSetArg(arglist[i], XtNleft, XtChainLeft);                     i++;
-       XtSetArg(arglist[i], XtNright, XtChainLeft);                    i++;
-       selFileLists[0] = XtCreateManagedWidget("selFileList1",
-               compositeWidgetClass, selFileForm, arglist, i);
-#if (NR == 3)
-       i = 0;
-       XtSetArg(arglist[i], XtNwidth, listWidth);                      i++;
-       XtSetArg(arglist[i], XtNheight, listHeight);                    i++;
-       XtSetArg(arglist[i], XtNborderColor, SFfore);                   i++;
-       XtSetArg(arglist[i], XtNfromHoriz, selFileLists[0]);            i++;
-       XtSetArg(arglist[i], XtNfromVert, selFileHScroll);              i++;
-       XtSetArg(arglist[i], XtNhorizDistance, listSpacing);            i++;
-       XtSetArg(arglist[i], XtNvertDistance, 10);                      i++;
-       XtSetArg(arglist[i], XtNtop, XtChainTop);                       i++;
-       XtSetArg(arglist[i], XtNbottom, XtChainTop);                    i++;
-       XtSetArg(arglist[i], XtNleft, XtChainLeft);                     i++;
-       XtSetArg(arglist[i], XtNright, XtChainLeft);                    i++;
-       selFileLists[1] = XtCreateManagedWidget("selFileList2",
-               compositeWidgetClass, selFileForm, arglist, i);
-
-       i = 0;
-       XtSetArg(arglist[i], XtNwidth, listWidth);                      i++;
-       XtSetArg(arglist[i], XtNheight, listHeight);                    i++;
-       XtSetArg(arglist[i], XtNborderColor, SFfore);                   i++;
-       XtSetArg(arglist[i], XtNfromHoriz, selFileLists[1]);            i++;
-       XtSetArg(arglist[i], XtNfromVert, selFileHScroll);              i++;
-       XtSetArg(arglist[i], XtNhorizDistance, listSpacing);            i++;
-       XtSetArg(arglist[i], XtNvertDistance, 10);                      i++;
-       XtSetArg(arglist[i], XtNtop, XtChainTop);                       i++;
-       XtSetArg(arglist[i], XtNbottom, XtChainTop);                    i++;
-       XtSetArg(arglist[i], XtNleft, XtChainLeft);                     i++;
-       XtSetArg(arglist[i], XtNright, XtChainLeft);                    i++;
-       selFileLists[2] = XtCreateManagedWidget("selFileList3",
-               compositeWidgetClass, selFileForm, arglist, i);
-#endif
-       for (n = 0; n < NR; n++) {
-
-               i = 0;
-               XtSetArg(arglist[i], XtNx, vScrollX);                   i++;
-               XtSetArg(arglist[i], XtNy, vScrollY);                   i++;
-               XtSetArg(arglist[i], XtNwidth, scrollThickness);        i++;
-               XtSetArg(arglist[i], XtNheight, SFvScrollHeight);       i++;
-               XtSetArg(arglist[i], XtNborderColor, SFfore);           i++;
-               selFileVScrolls[n] = XtCreateManagedWidget("selFileVScroll",
-                       scrollbarWidgetClass, selFileLists[n], arglist, i);
-
-               XtAddCallback(selFileVScrolls[n], XtNjumpProc,
-                       SFvFloatSliderMovedCallback, (XtPointer)(intptr_t) n);
-               XtAddCallback(selFileVScrolls[n], XtNscrollProc,
-                       SFvAreaSelectedCallback, (XtPointer)(intptr_t) n);
-
-               i = 0;
-
-               XtSetArg(arglist[i], XtNorientation, XtorientHorizontal);
-                                                                       i++;
-               XtSetArg(arglist[i], XtNx, hScrollX);                   i++;
-               XtSetArg(arglist[i], XtNy, hScrollY);                   i++;
-               XtSetArg(arglist[i], XtNwidth, SFhScrollWidth);         i++;
-               XtSetArg(arglist[i], XtNheight, scrollThickness);       i++;
-               XtSetArg(arglist[i], XtNborderColor, SFfore);           i++;
-               selFileHScrolls[n] = XtCreateManagedWidget("selFileHScroll",
-                       scrollbarWidgetClass, selFileLists[n], arglist, i);
-
-               XtAddCallback(selFileHScrolls[n], XtNjumpProc,
-                       SFhSliderMovedCallback, (XtPointer)(intptr_t) n);
-               XtAddCallback(selFileHScrolls[n], XtNscrollProc,
-                       SFhAreaSelectedCallback, (XtPointer)(intptr_t) n);
-
-               XtAddEventHandler(selFileVScrolls[n], ButtonPressMask, False,
-                       SFwheelProc, (XtPointer)(intptr_t) n); // [HGM] couplemouse wheel to v-scroll
-       }
-
-       i = 0;
-       XtSetArg(arglist[i], XtNlabel, ok);                             i++;
-       XtSetArg(arglist[i], XtNresizable, True);                       i++;
-       XtSetArg(arglist[i], XtNcallback, SFokSelect);                  i++;
-       XtSetArg(arglist[i], XtNborderColor, SFfore);                   i++;
-       XtSetArg(arglist[i], XtNfromVert, selFileLists[0]);             i++;
-       XtSetArg(arglist[i], XtNvertDistance, 30);                      i++;
-       XtSetArg(arglist[i], XtNtop, XtChainTop);                       i++;
-       XtSetArg(arglist[i], XtNbottom, XtChainTop);                    i++;
-       XtSetArg(arglist[i], XtNleft, XtChainLeft);                     i++;
-       XtSetArg(arglist[i], XtNright, XtChainLeft);                    i++;
-       selFileOK = XtCreateManagedWidget("selFileOK", commandWidgetClass,
-               selFileForm, arglist, i);
-
-       i = 0;
-       XtSetArg(arglist[i], XtNlabel, cancel);                         i++;
-       XtSetArg(arglist[i], XtNresizable, True);                       i++;
-       XtSetArg(arglist[i], XtNcallback, SFcancelSelect);              i++;
-       XtSetArg(arglist[i], XtNborderColor, SFfore);                   i++;
-       XtSetArg(arglist[i], XtNfromHoriz, selFileOK);                  i++;
-       XtSetArg(arglist[i], XtNfromVert, selFileLists[0]);             i++;
-       XtSetArg(arglist[i], XtNhorizDistance, 30);                     i++;
-       XtSetArg(arglist[i], XtNvertDistance, 30);                      i++;
-       XtSetArg(arglist[i], XtNtop, XtChainTop);                       i++;
-       XtSetArg(arglist[i], XtNbottom, XtChainTop);                    i++;
-       XtSetArg(arglist[i], XtNleft, XtChainLeft);                     i++;
-       XtSetArg(arglist[i], XtNright, XtChainLeft);                    i++;
-       selFileCancel = XtCreateManagedWidget("selFileCancel",
-               commandWidgetClass, selFileForm, arglist, i);
-
-       XtSetMappedWhenManaged(selFile, False);
-       XtRealizeWidget(selFile);
-
-       /* Add WM_DELETE_WINDOW protocol */
-       SFwmDeleteWindow = XInternAtom(SFdisplay, "WM_DELETE_WINDOW", False);
-       XSetWMProtocols(SFdisplay, XtWindow(selFile), &SFwmDeleteWindow, 1);
-
-       SFcreateGC();
-
-       xtermCursor = XCreateFontCursor(SFdisplay, XC_xterm);
-
-       sbRightArrowCursor = XCreateFontCursor(SFdisplay, XC_sb_right_arrow);
-       dotCursor = XCreateFontCursor(SFdisplay, XC_dot);
-
-       XDefineCursor(SFdisplay, XtWindow(selFileForm), xtermCursor);
-       XDefineCursor(SFdisplay, XtWindow(selFileField), xtermCursor);
-       XDefineCursor(SFdisplay, XtWindow(filterField), xtermCursor);
-
-       for (n = 0; n < NR; n++) {
-               XDefineCursor(SFdisplay, XtWindow(selFileLists[n]),
-                       sbRightArrowCursor);
-       }
-       XDefineCursor(SFdisplay, XtWindow(selFileOK), dotCursor);
-       XDefineCursor(SFdisplay, XtWindow(selFileCancel), dotCursor);
-
-       for (n = 0; n < NR; n++) {
-               XtAddEventHandler(selFileLists[n], ExposureMask, True,
-                       SFexposeList, (XtPointer)(intptr_t) n);
-               XtAddEventHandler(selFileLists[n], EnterWindowMask, False,
-                       SFenterList, (XtPointer)(intptr_t) n);
-               XtAddEventHandler(selFileLists[n], LeaveWindowMask, False,
-                       SFleaveList, (XtPointer)(intptr_t) n);
-               XtAddEventHandler(selFileLists[n], PointerMotionMask, False,
-                       (XtEventHandler) SFmotionList, (XtPointer)(intptr_t) n);
-               XtAddEventHandler(selFileLists[n], ButtonPressMask, False,
-                       SFbuttonPressList, (XtPointer)(intptr_t) n);
-               XtAddEventHandler(selFileLists[n], ButtonReleaseMask, False,
-                       SFbuttonReleaseList, (XtPointer)(intptr_t) n);
-       }
-
-       XtAddEventHandler(selFileField, KeyPressMask, False,
-               SFmodVerifyCallback, (XtPointer) NULL);
-       XtAddEventHandler(filterField, KeyReleaseMask, False,
-               SFmodVerifyCallback, (XtPointer) 1);
-       XtSetKeyboardFocus(selFileForm, selFileField);
-
-       SFapp = XtWidgetToApplicationContext(selFile);
-
-}
-
-/* position widget under the cursor */
-void
-SFpositionWidget(w)
-    Widget w;
-{
-    Arg args[3];
-    Cardinal num_args;
-    Dimension width, height, b_width;
-    int x, y, max_x, max_y;
-    Window root, child;
-    int dummyx, dummyy;
-    unsigned int dummymask;
-    
-    XQueryPointer(XtDisplay(w), XtWindow(w), &root, &child, &x, &y,
-                 &dummyx, &dummyy, &dummymask);
-    num_args = 0;
-    XtSetArg(args[num_args], XtNwidth, &width); num_args++;
-    XtSetArg(args[num_args], XtNheight, &height); num_args++;
-    XtSetArg(args[num_args], XtNborderWidth, &b_width); num_args++;
-    XtGetValues(w, args, num_args);
-
-    width += 2 * b_width;
-    height += 2 * b_width;
-
-    x -= ( (Position) width/2 );
-    if (x < 0) x = 0;
-    if ( x > (max_x = (Position) (XtScreen(w)->width - width)) ) x = max_x;
-
-    y -= ( (Position) height/2 );
-    if (y < 0) y = 0;
-    if ( y > (max_y = (Position) (XtScreen(w)->height - height)) ) y = max_y;
-    
-    num_args = 0;
-    XtSetArg(args[num_args], XtNx, x); num_args++;
-    XtSetArg(args[num_args], XtNy, y); num_args++;
-    XtSetValues(w, args, num_args);
-}
-
-FILE *
-SFopenFile(name, mode, prompt, failed)
-    char *name;
-    char *mode;
-    char *prompt;
-    char *failed;
-{
-    Arg args[1];
-    FILE *fp;
-
-    SFchdir(SFstartDir);
-    if ((fp = fopen(name, mode)) == NULL) {
-       char *buf;
-       if (1) { // [HGM] always use strerror
-           buf = XtMalloc(strlen(failed) + strlen(strerror(errno)) + 
-                          strlen(prompt) + 2);
-           strcpy(buf, failed);
-           strcat(buf, strerror(errno));
-           strcat(buf, "\n");
-           strcat(buf, prompt);
-       } else {
-           buf = XtMalloc(strlen(failed) + strlen(prompt) + 2);
-           strcpy(buf, failed);
-           strcat(buf, "\n");
-           strcat(buf, prompt);
-       }
-       XtSetArg(args[0], XtNlabel, buf);
-       XtSetValues(selFilePrompt, args, ONE);
-       XtFree(buf);
-       return NULL;
-    }
-    return fp;
-}
-
-void
-SFupdateTextBuffer()
-{
-       Arg arglist[2];
-       int i;
-       char *v;
-
-       i = 0;
-       XtSetArg(arglist[i], XtNstring, &v);  i++;
-       XtGetValues(selFileField, arglist, i);
-       strncpy(SFtextBuffer, v, MAXPATHLEN);
-}
-
-void
-SFsetText(path)
-       char    *path;
-{
-       Arg arglist[2];
-       int i;
-
-       i = 0;
-       XtSetArg(arglist[i], XtNstring, path);  i++;
-       XtSetValues(selFileField, arglist, i);
-       XawTextSetInsertionPoint(selFileField, strlen(path));
-}
-
-void
-SFtextChanged()
-{
-       SFupdateTextBuffer();
-
-       if ((SFtextBuffer[0] == '/') || (SFtextBuffer[0] == '~')) {
-         (void) strncpy(SFcurrentPath, SFtextBuffer, MAXPATHLEN);
-
-               SFtextPos = XawTextGetInsertionPoint(selFileField);
-       } else {
-         (void) strcat(strncpy(SFcurrentPath, SFstartDir, MAXPATHLEN), SFtextBuffer);
-
-               SFtextPos = XawTextGetInsertionPoint(selFileField) +
-                       strlen(SFstartDir);
-       }
-
-       if (!SFworkProcAdded) {
-               (void) XtAppAddWorkProc(SFapp, SFworkProc, NULL);
-               SFworkProcAdded = 1;
-       }
-
-       SFupdatePath();
-       return;
-}
-
-static char *
-SFgetText()
-{
-       return strcpy(XtMalloc((unsigned) (strlen(SFtextBuffer) + 1)),
-               SFtextBuffer);
-}
-
-static void
-SFprepareToReturn()
-{
-       SFstatus = SEL_FILE_NULL;
-       XtRemoveGrab(selFile);
-       XtUnmapWidget(selFile);
-       XtRemoveTimeOut(SFdirModTimerId);
-       if (SFchdir(SFstartDir)) {
-               XtAppError(
-                       SFapp,
-                       "XsraSelFile: can't return to current directory"
-               );
-       }
-       return;
-}
-
-FILE *
-XsraSelFile(toplevel, prompt, ok, cancel, failed,
-           init_path, filter, mode, show_entry, name_return)
-       Widget          toplevel;
-       char            *prompt;
-       char            *ok;
-       char            *cancel;
-       char            *failed;
-       char            *init_path;
-       char            *filter;
-       char            *mode;
-       int             (*show_entry)();
-       char            **name_return;
-{
-       static int      firstTime = 1;
-       Cardinal        i;
-       Arg             arglist[20];
-       XEvent          event;
-       FILE            *fp;
-
-       if (!prompt) {
-               prompt = _("Pathname:");
-       }
-
-       if (!ok) {
-               ok = _("OK");
-       }
-
-       if (!cancel) {
-               cancel = _("Cancel");
-       }
-
-       if(SFpathFlag != (mode && mode[0] == 'p') || strcmp(SFfilterBuffer, filter)) {
-               SFpurge();
-               SFpathFlag = (mode && mode[0] == 'p'); // [HGM] ignore everything that is not a directory
-       }
-
-       if (firstTime) {
-               firstTime = 0;
-               SFdisplay = XtDisplay(toplevel);
-               SFcreateWidgets(toplevel, prompt, ok, cancel);
-       } else {
-               i = 0;
-
-               XtSetArg(arglist[i], XtNlabel, prompt);                 i++;
-               XtSetValues(selFilePrompt, arglist, i);
-
-               i = 0;
-               XtSetArg(arglist[i], XtNlabel, ok);                     i++;
-               XtSetValues(selFileOK, arglist, i);
-
-               i = 0;
-               XtSetArg(arglist[i], XtNlabel, cancel);                 i++;
-               XtSetValues(selFileCancel, arglist, i);
-       }
-
-       i = 0;
-       XtSetArg(arglist[i], XtNstring, filter);                        i++;
-       XtSetValues(filterField, arglist, i);
-
-       strncpy(SFfilterBuffer, filter, MAXPATHLEN-1);
-       SFupdateTextBuffer();
-       strncpy(SFlastPath, SFtextBuffer, MAXPATHLEN-1); // remember for cancel
-
-       SFpositionWidget(selFile);
-       XtMapWidget(selFile);
-
-#if defined(SVR4) || defined(SYSV) || defined(USG) || 1
-       if (!getcwd(SFstartDir, MAXPATHLEN)) { // [HGM] always do this, as I do not know when exactly to do it
-#else /* defined(SVR4) || defined(SYSV) || defined(USG) */
-       if (!getwd(SFstartDir)) {
-#endif /* defined(SVR4) || defined(SYSV) || defined(USG) */
-
-         XtAppError(SFapp, _("XsraSelFile: can't get current directory"));
-       }
-       (void) strcat(SFstartDir, "/");
-       (void) strncpy(SFcurrentDir, SFstartDir, MAXPATHLEN);
-
-       if (init_path) {
-               if (init_path[0] == '/') {
-                 (void) strncpy(SFcurrentPath, init_path, MAXPATHLEN);
-                       if (strncmp(
-                               SFcurrentPath,
-                               SFstartDir,
-                               strlen(SFstartDir)
-                       )) {
-                               SFsetText(SFcurrentPath);
-                       } else {
-                               SFsetText(&(SFcurrentPath[strlen(SFstartDir)]));
-                       }
-               } else {
-                 (void) strcat(strncpy(SFcurrentPath, SFstartDir, MAXPATHLEN),
-                               init_path);
-                       SFsetText(&(SFcurrentPath[strlen(SFstartDir)]));
-               }
-       } else {
-         (void) strncpy(SFcurrentPath, SFstartDir, MAXPATHLEN);
-       }
-
-       SFfunc = show_entry;
-
-       SFtextChanged();
-
-       XtAddGrab(selFile, True, True);
-
-       SFdirModTimerId = XtAppAddTimeOut(SFapp, (unsigned long) 1000,
-               SFdirModTimer, (XtPointer) NULL);
-
-       while (1) {
-               XtAppNextEvent(SFapp, &event);
-               XtDispatchEvent(&event);
-               switch (SFstatus) {
-               case SEL_FILE_TEXT:
-                       SFstatus = SEL_FILE_NULL;
-                       SFtextChanged();
-                       break;
-               case SEL_FILE_OK:
-                       *name_return = SFgetText();
-                       if(mode && (mode[0] == 'p' || mode[0] == 'f')) { // [HGM] for use in file-option browse button
-                               SFprepareToReturn();
-                               return stderr;
-                       }
-                       if ((!(*name_return)[0] || (*name_return)[strlen(*name_return)-1] != '/') &&      // [HGM] refuse directories
-                           (fp = SFopenFile(*name_return, mode, prompt, failed))) {
-                               SFprepareToReturn();
-                               return fp;
-                       }
-                       SFstatus = SEL_FILE_NULL;
-                       break;
-               case SEL_FILE_CANCEL:
-                       SFprepareToReturn();
-                       SFsetText(SFlastPath);
-                       return NULL;
-               case SEL_FILE_NULL:
-                       break;
-               }
-       }
-
-}