Implement -autoCopyPV
[xboard.git] / filebrowser / selfile.c
index 5a4ceb4..729453b 100644 (file)
@@ -62,6 +62,7 @@ extern int errno;
 
 #include "xstat.h"
 #include "selfile.h"
+#include "../gettext.h"
 
 #ifndef MAXPATHLEN
 #define MAXPATHLEN 1024
@@ -71,13 +72,22 @@ extern int errno;
 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],
-       SFcurrentPath[MAXPATHLEN],
-       SFlastPath[MAXPATHLEN],
-       SFcurrentDir[MAXPATHLEN];
+       SFstartDir[MAXPATHLEN+1],
+       SFcurrentPath[MAXPATHLEN+1],
+       SFlastPath[MAXPATHLEN+1],
+       SFcurrentDir[MAXPATHLEN+1];
 
 Widget
        selFile,
@@ -127,9 +137,9 @@ XtAppContext SFapp;
 
 int SFpathScrollWidth, SFvScrollHeight, SFhScrollWidth;
 
-char SFtextBuffer[MAXPATHLEN];
+char SFtextBuffer[MAXPATHLEN+1];
 
-char SFfilterBuffer[MAXPATHLEN];
+char SFfilterBuffer[MAXPATHLEN+1];
 
 XtIntervalId SFdirModTimerId;
 
@@ -288,7 +298,7 @@ SFcreateWidgets(toplevel, prompt, ok, cancel)
        i = 0;
        XtSetArg(arglist[i], XtNtransientFor, toplevel);                i++;
 
-       selFile = XtAppCreateShell("Browse", "SelFile",
+       selFile = XtAppCreateShell(_("Browse"), "SelFile",
                transientShellWidgetClass, SFdisplay, arglist, i);
 
        /* Add WM_DELETE_WINDOW protocol */
@@ -383,7 +393,6 @@ SFcreateWidgets(toplevel, prompt, ok, cancel)
        XtSetArg(arglist[i], XtNeditType, XawtextEdit);                 i++;
        XtSetArg(arglist[i], XtNwrap, XawtextWrapWord);                 i++;
        XtSetArg(arglist[i], XtNresize, XawtextResizeHeight);           i++;
-       XtSetArg(arglist[i], XtNuseStringInPlace, True);                i++;
        selFileField = XtCreateManagedWidget("selFileField",
                asciiTextWidgetClass, selFileForm, arglist, i);
 
@@ -392,7 +401,7 @@ SFcreateWidgets(toplevel, prompt, ok, cancel)
        XtAddEventHandler(selFileField, ButtonPressMask, False, SFsetFocus, (XtPointer) selFileForm);
 
        i = 0;
-       XtSetArg(arglist[i], XtNlabel, "Filter on extensions:");        i++;
+       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++;
@@ -678,9 +687,36 @@ SFopenFile(name, mode, prompt, failed)
     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);
@@ -746,15 +782,15 @@ XsraSelFile(toplevel, prompt, ok, cancel, failed,
        FILE            *fp;
 
        if (!prompt) {
-               prompt = "Pathname:";
+               prompt = _("Pathname:");
        }
 
        if (!ok) {
-               ok = "OK";
+               ok = _("OK");
        }
 
        if (!cancel) {
-               cancel = "Cancel";
+               cancel = _("Cancel");
        }
 
        if(SFpathFlag != (mode && mode[0] == 'p') || strcmp(SFfilterBuffer, filter)) {
@@ -785,8 +821,9 @@ XsraSelFile(toplevel, prompt, ok, cancel, failed,
        XtSetArg(arglist[i], XtNstring, filter);                        i++;
        XtSetValues(filterField, arglist, i);
 
-       safeStrCpy(SFfilterBuffer, filter, MAXPATHLEN);
-       safeStrCpy(SFlastPath, SFtextBuffer, MAXPATHLEN); // remember for cancel
+       strncpy(SFfilterBuffer, filter, MAXPATHLEN-1);
+       SFupdateTextBuffer();
+       strncpy(SFlastPath, SFtextBuffer, MAXPATHLEN-1); // remember for cancel
 
        SFpositionWidget(selFile);
        XtMapWidget(selFile);
@@ -797,7 +834,7 @@ XsraSelFile(toplevel, prompt, ok, cancel, failed,
        if (!getwd(SFstartDir)) {
 #endif /* defined(SVR4) || defined(SYSV) || defined(USG) */
 
-               XtAppError(SFapp, "XsraSelFile: can't get current directory");
+         XtAppError(SFapp, _("XsraSelFile: can't get current directory"));
        }
        (void) strcat(SFstartDir, "/");
        (void) strncpy(SFcurrentDir, SFstartDir, MAXPATHLEN);
@@ -846,7 +883,7 @@ XsraSelFile(toplevel, prompt, ok, cancel, failed,
                                SFprepareToReturn();
                                return stderr;
                        }
-                       if ((*name_return)[strlen(*name_return)-1] != '/' &&         // [HGM] refuse directories
+                       if ((!(*name_return)[0] || (*name_return)[strlen(*name_return)-1] != '/') &&      // [HGM] refuse directories
                            (fp = SFopenFile(*name_return, mode, prompt, failed))) {
                                SFprepareToReturn();
                                return fp;
@@ -861,4 +898,5 @@ XsraSelFile(toplevel, prompt, ok, cancel, failed,
                        break;
                }
        }
+
 }