Merge branch 'v4.7.x' into master
[xboard.git] / xaw / xboard.c
index 69079be..d3d6ba2 100644 (file)
@@ -5,7 +5,7 @@
  * Massachusetts.
  *
  * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
- * 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
+ * 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
  *
  * The following terms apply to Digital Equipment Corporation's copyright
  * interest in XBoard:
@@ -183,13 +183,6 @@ extern char *getenv();
 // [HGM] bitmaps: put before incuding the bitmaps / pixmaps, to know how many piece types there are.
 #include "common.h"
 
-#if HAVE_LIBXPM
-#include <X11/xpm.h>
-#define IMAGE_EXT "xpm"
-#else
-#define IMAGE_EXT "xim"
-#endif
-
 #include "bitmaps/icon_white.bm"
 #include "bitmaps/icon_black.bm"
 #include "bitmaps/checkmark.bm"
@@ -202,8 +195,6 @@ extern char *getenv();
 #include "childio.h"
 #include "xgamelist.h"
 #include "xhistory.h"
-#include "xevalgraph.h"
-#include "xedittags.h"
 #include "menus.h"
 #include "board.h"
 #include "dialogs.h"
@@ -212,6 +203,7 @@ extern char *getenv();
 #include "gettext.h"
 #include "draw.h"
 
+#define SLASH '/'
 
 #ifdef __EMX__
 #ifndef HAVE_USLEEP
@@ -262,8 +254,6 @@ void TempForwardProc P((Widget w, XEvent *event, String *prms, Cardinal *nprms))
 Boolean TempBackwardActive = False;
 void ManInner P((Widget w, XEvent *event, String *prms, Cardinal *nprms));
 void DisplayMove P((int moveNumber));
-void ICSInitScript P((void));
-void SelectMove P((Widget w, XEvent * event, String * params, Cardinal * nParams));
 void update_ics_width P(());
 int CopyMemoProc P(());
 
@@ -321,6 +311,7 @@ WindowPlacement wpEvalGraph;
 WindowPlacement wpEngineOutput;
 WindowPlacement wpGameList;
 WindowPlacement wpTags;
+WindowPlacement wpDualBoard;
 
 
 /* This magic number is the number of intermediate frames used
@@ -378,7 +369,7 @@ XtActionsRec boardActions[] = {
     { "GenericPopDown", (XtActionProc) GenericPopDown },
     { "ErrorPopDown", (XtActionProc) ErrorPopDown },
     { "CopyMemoProc", (XtActionProc) CopyMemoProc },
-    { "SelectMove", (XtActionProc) SelectMove },
+    { "SelectMove", (XtActionProc) SelectMoveX },
     { "LoadSelectedProc", LoadSelectedProc },
     { "SetFilterProc", SetFilterProc },
     { "TypeInProc", TypeInProc },
@@ -390,60 +381,17 @@ XtActionsRec boardActions[] = {
 };
 
 char globalTranslations[] =
-  ":<Key>F9: MenuItem(Actions.Resign) \n \
-   :Ctrl<Key>n: MenuItem(File.NewGame) \n \
-   :Meta<Key>V: MenuItem(File.NewVariant) \n \
-   :Ctrl<Key>o: MenuItem(File.LoadGame) \n \
-   :Meta<Key>Next: MenuItem(LoadNextGameProc) \n \
+  ":Meta<Key>Next: MenuItem(LoadNextGameProc) \n \
    :Meta<Key>Prior: MenuItem(LoadPrevGameProc) \n \
    :Ctrl<Key>Down: LoadSelectedProc(3) \n \
    :Ctrl<Key>Up: LoadSelectedProc(-3) \n \
-   :Ctrl<Key>s: MenuItem(File.SaveGame) \n \
-   :Ctrl<Key>c: MenuItem(Edit.CopyGame) \n \
-   :Ctrl<Key>v: MenuItem(Edit.PasteGame) \n \
-   :Ctrl<Key>O: MenuItem(File.LoadPosition) \n \
    :Shift<Key>Next: MenuItem(LoadNextPositionProc) \n \
    :Shift<Key>Prior: MenuItem(LoadPrevPositionProc) \n \
-   :Ctrl<Key>S: MenuItem(File.SavePosition) \n \
-   :Ctrl<Key>C: MenuItem(Edit.CopyPosition) \n \
-   :Ctrl<Key>V: MenuItem(Edit.PastePosition) \n \
-   :Ctrl<Key>q: MenuItem(File.Quit) \n \
-   :Ctrl<Key>w: MenuItem(Mode.MachineWhite) \n \
-   :Ctrl<Key>b: MenuItem(Mode.MachineBlack) \n \
-   :Ctrl<Key>t: MenuItem(Mode.TwoMachines) \n \
-   :Ctrl<Key>a: MenuItem(Mode.AnalysisMode) \n \
-   :Ctrl<Key>g: MenuItem(Mode.AnalyzeFile) \n \
-   :Ctrl<Key>e: MenuItem(Mode.EditGame) \n \
-   :Ctrl<Key>E: MenuItem(Mode.EditPosition) \n \
-   :Meta<Key>O: MenuItem(View.EngineOutput) \n \
-   :Meta<Key>E: MenuItem(View.EvaluationGraph) \n \
-   :Meta<Key>G: MenuItem(View.GameList) \n \
-   :Meta<Key>H: MenuItem(View.MoveHistory) \n \
    :<Key>Pause: MenuItem(Mode.Pause) \n \
-   :<Key>F3: MenuItem(Action.Accept) \n \
-   :<Key>F4: MenuItem(Action.Decline) \n \
-   :<Key>F12: MenuItem(Action.Rematch) \n \
-   :<Key>F5: MenuItem(Action.CallFlag) \n \
-   :<Key>F6: MenuItem(Action.Draw) \n \
-   :<Key>F7: MenuItem(Action.Adjourn) \n \
-   :<Key>F8: MenuItem(Action.Abort) \n \
-   :<Key>F10: MenuItem(Action.StopObserving) \n \
-   :<Key>F11: MenuItem(Action.StopExamining) \n \
    :Ctrl<Key>d: MenuItem(DebugProc) \n \
    :Meta Ctrl<Key>F12: MenuItem(DebugProc) \n \
-   :Meta<Key>End: MenuItem(Edit.ForwardtoEnd) \n \
-   :Meta<Key>Right: MenuItem(Edit.Forward) \n \
-   :Meta<Key>Home: MenuItem(Edit.BacktoStart) \n \
-   :Meta<Key>Left: MenuItem(Edit.Backward) \n \
    :<Key>Left: MenuItem(Edit.Backward) \n \
    :<Key>Right: MenuItem(Edit.Forward) \n \
-   :<Key>Home: MenuItem(Edit.Revert) \n \
-   :<Key>End: MenuItem(Edit.TruncateGame) \n \
-   :Ctrl<Key>m: MenuItem(Engine.MoveNow) \n \
-   :Ctrl<Key>x: MenuItem(Engine.RetractMove) \n \
-   :Meta<Key>J: MenuItem(Options.Adjudications) \n \
-   :Meta<Key>U: MenuItem(Options.CommonEngine) \n \
-   :Meta<Key>T: MenuItem(Options.TimeControl) \n \
    :Ctrl<Key>P: MenuItem(PonderNextMove) \n "
 #ifndef OPTIONSDIALOG
     "\
@@ -454,8 +402,6 @@ char globalTranslations[] =
    :Ctrl<Key>H: MenuItem(HideThinkingProc) \n "
 #endif
    "\
-   :<Key>F1: MenuItem(Help.ManXBoard) \n \
-   :<Key>F2: MenuItem(View.FlipView) \n \
    :<KeyDown>Return: TempBackwardProc() \n \
    :<KeyUp>Return: TempForwardProc() \n";
 
@@ -466,7 +412,7 @@ char ICSInputTranslations[] =
 
 // [HGM] vari: another hideous kludge: call extend-end first so we can be sure select-start works,
 //             as the widget is destroyed before the up-click can call extend-end
-char commentTranslations[] = "<Btn3Down>: extend-end() select-start() CommentClick() \n";
+char commentTranslations[] = "<Btn3Down>: extend-end(PRIMARY) select-start() CommentClick() \n";
 
 String xboardResources[] = {
     "*Error*translations: #override\\n <Key>Return: ErrorPopDown()",
@@ -477,128 +423,6 @@ String xboardResources[] = {
 /* Max possible square size */
 #define MAXSQSIZE 256
 
-static int xpm_avail[MAXSQSIZE];
-
-#ifdef HAVE_DIR_STRUCT
-
-/* Extract piece size from filename */
-static int
-xpm_getsize (char *name, int len, char *ext)
-{
-    char *p, *d;
-    char buf[10];
-
-    if (len < 4)
-      return 0;
-
-    if ((p=strchr(name, '.')) == NULL ||
-       StrCaseCmp(p+1, ext) != 0)
-      return 0;
-
-    p = name + 3;
-    d = buf;
-
-    while (*p && isdigit(*p))
-      *(d++) = *(p++);
-
-    *d = 0;
-    return atoi(buf);
-}
-
-/* Setup xpm_avail */
-static int
-xpm_getavail (char *dirname, char *ext)
-{
-    DIR *dir;
-    struct dirent *ent;
-    int  i;
-
-    for (i=0; i<MAXSQSIZE; ++i)
-      xpm_avail[i] = 0;
-
-    if (appData.debugMode)
-      fprintf(stderr, "XPM dir:%s:ext:%s:\n", dirname, ext);
-
-    dir = opendir(dirname);
-    if (!dir)
-      {
-         fprintf(stderr, _("%s: Can't access XPM directory %s\n"),
-                 programName, dirname);
-         exit(1);
-      }
-
-    while ((ent=readdir(dir)) != NULL) {
-       i = xpm_getsize(ent->d_name, NAMLEN(ent), ext);
-       if (i > 0 && i < MAXSQSIZE)
-         xpm_avail[i] = 1;
-    }
-
-    closedir(dir);
-
-    return 0;
-}
-
-void
-xpm_print_avail (FILE *fp, char *ext)
-{
-    int i;
-
-    fprintf(fp, _("Available `%s' sizes:\n"), ext);
-    for (i=1; i<MAXSQSIZE; ++i) {
-       if (xpm_avail[i])
-         printf("%d\n", i);
-    }
-}
-
-/* Return XPM piecesize closest to size */
-int
-xpm_closest_to (char *dirname, int size, char *ext)
-{
-    int i;
-    int sm_diff = MAXSQSIZE;
-    int sm_index = 0;
-    int diff;
-
-    xpm_getavail(dirname, ext);
-
-    if (appData.debugMode)
-      xpm_print_avail(stderr, ext);
-
-    for (i=1; i<MAXSQSIZE; ++i) {
-       if (xpm_avail[i]) {
-           diff = size - i;
-           diff = (diff<0) ? -diff : diff;
-           if (diff < sm_diff) {
-               sm_diff = diff;
-               sm_index = i;
-           }
-       }
-    }
-
-    if (!sm_index) {
-       fprintf(stderr, _("Error: No `%s' files!\n"), ext);
-       exit(1);
-    }
-
-    return sm_index;
-}
-#else  /* !HAVE_DIR_STRUCT */
-/* If we are on a system without a DIR struct, we can't
-   read the directory, so we can't collect a list of
-   filenames, etc., so we can't do any size-fitting. */
-int
-xpm_closest_to (char *dirname, int size, char *ext)
-{
-    fprintf(stderr, _("\
-Warning: No DIR structure found on this system --\n\
-         Unable to autosize for XPM/XIM pieces.\n\
-   Please report this error to %s.\n\
-   Include system type & operating system in message.\n"), PACKAGE_BUGREPORT););
-    return size;
-}
-#endif /* HAVE_DIR_STRUCT */
-
-
 /* Arrange to catch delete-window events */
 Atom wm_delete_window;
 void
@@ -982,6 +806,7 @@ PrintArg (ArgType t)
     case ArgString:   p = " STR"; break;
     case ArgBoolean:  p = " TF"; break;
     case ArgSettingsFilename:
+    case ArgBackupSettingsFile:
     case ArgFilename: p = " FILE"; break;
     case ArgX:        p = " Nx"; break;
     case ArgY:        p = " Ny"; break;
@@ -995,11 +820,116 @@ PrintArg (ArgType t)
     case ArgTwo:
     case ArgNone:
     case ArgCommSettings:
+    case ArgMaster:
+    case ArgInstall:
       break;
   }
   return p;
 }
 
+char *
+GenerateGlobalTranslationTable (void)
+{
+  /* go through all menu items and extract the keyboard shortcuts, so that X11 can load them */
+  char *output;
+
+  int i,j;
+  MenuItem *mi;
+
+  output = strdup("");
+
+  /* loop over all menu entries */
+  for( i=0; menuBar[i].mi ; i++)
+    {
+      mi = menuBar[i].mi;
+      for(j=0; mi[j].proc; j++)
+       {
+         if (mi[j].accel)
+           {
+             int ctrl  = 0;
+             int shift = 0;
+             int alt   = 0;
+
+             char *key,*test, *mods;
+
+             /* check for Ctrl/Alt */
+             if( strstr(mi[j].accel, "<Ctrl>")  ) ctrl  = 1;
+             if( strstr(mi[j].accel, "<Shift>") ) shift = 1;
+             if( strstr(mi[j].accel, "<Alt>")   ) alt   = 1;
+
+             /* remove all <...> */
+             test = strrchr(mi[j].accel, '>');
+             if ( test==NULL )
+               key = strdup(mi[j].accel);
+             else
+               key = strdup(++test); // remove ">"
+
+             /* instead of shift X11 uses the uppercase letter directly*/
+             if (shift && strlen(key)==1 )
+               {
+                 *key  = toupper(*key);
+                 shift = 0;
+               }
+
+             /* handle some special cases which have different names in X11 */
+             if ( strncmp(key, "Page_Down", 9) == 0 )
+               {
+                 free(key);
+                 key=strdup("Next");
+               }
+             else if ( strncmp(key, "Page_Up", 7) == 0 )
+               {
+                 free(key);
+                 key=strdup("Prior");
+               };
+
+             /* create string of mods */
+             if (ctrl)
+               mods = strdup("Ctrl ");
+             else
+               mods = strdup("");
+
+             if(alt)
+               {
+                 mods = realloc(mods, strlen(mods) + strlen("Meta ")+1);
+                 strncat(mods, "Meta ", 5);
+               };
+
+             if(shift)
+               {
+                 mods = realloc(mods, strlen(mods) + strlen("Shift ")+1);
+                 strncat(mods, "Shift ", 6);
+               };
+
+             // remove trailing space
+             if( isspace(mods[strlen(mods)-1]) )
+               mods[strlen(mods)-1]='\0';
+
+             /* get the name for the callback, we can use MenuItem() here that will call KeyBindingProc */
+             size_t namesize = snprintf(NULL, 0, "%s.%s", menuBar[i].ref, mi[j].ref);
+             char *name = malloc(namesize+1);
+             snprintf(name, namesize+1, "%s.%s", menuBar[i].ref, mi[j].ref);
+
+             size_t buffersize = snprintf(NULL, 0, ":%s<Key>%s: MenuItem(%s) \n ", mods, key, name);
+             char *buffer = malloc(buffersize+1);
+             snprintf(buffer, buffersize+1, ":%s<Key>%s: MenuItem(%s) \n ", mods, key, name);
+
+             /* add string to the output */
+             output = realloc(output, strlen(output) + strlen(buffer)+1);
+             strncat(output, buffer, strlen(buffer));
+
+             /* clean up */
+             free(key);
+             free(buffer);
+             free(name);
+             free(mods);
+           }
+       }
+    }
+  return output;
+}
+
+
 void
 PrintOptions ()
 {
@@ -1029,6 +959,11 @@ PrintOptions ()
   if(len) buf[len] = NULLCHAR, printf("%s\n", buf);
 }
 
+void
+SlaveResize (Option *opt)
+{
+}
+
 int
 main (int argc, char **argv)
 {
@@ -1046,7 +981,7 @@ main (int argc, char **argv)
     debugFP = stderr;
 
     if(argc > 1 && (!strcmp(argv[1], "-v" ) || !strcmp(argv[1], "--version" ))) {
-       printf("%s version %s\n", PACKAGE_NAME, PACKAGE_VERSION);
+      printf("%s version %s\n\n  configure options: %s\n", PACKAGE_NAME, PACKAGE_VERSION, CONFIGURE_OPTIONS);
        exit(0);
     }
 
@@ -1268,7 +1203,6 @@ main (int argc, char **argv)
       XtSetArg(args[1], XtNforeground, &buttonForegroundPixel);
       XtGetValues(optList[W_PAUSE].handle, args, 2);
     }
-    AppendEnginesToMenu(appData.recentEngineList);
 
     xBoardWindow = XtWindow(boardWidget);
 
@@ -1328,8 +1262,13 @@ main (int argc, char **argv)
     if (appData.animate || appData.animateDragging)
       CreateAnimVars();
 
+
+    char *TranslationsTableMenus=GenerateGlobalTranslationTable ();
+
     XtAugmentTranslations(formWidget,
                          XtParseTranslationTable(globalTranslations));
+    XtAugmentTranslations(formWidget,
+                         XtParseTranslationTable(TranslationsTableMenus));
 
     XtAddEventHandler(formWidget, KeyPressMask, False,
                      (XtEventHandler) MoveTypeInProc, NULL);
@@ -1564,7 +1503,7 @@ FindFont (char *pattern, int targetPxlSize)
         safeStrCpy(p, best, strlen(best)+1 );
     }
     if (appData.debugMode) {
-        fprintf(debugFP, _("resolved %s at pixel size %d\n  to %s\n"),
+        fprintf(debugFP, "resolved %s at pixel size %d\n  to %s\n",
                pattern, targetPxlSize, p);
     }
     XFreeFontNames(fonts);
@@ -1626,18 +1565,6 @@ KeyBindingProc (Widget w, XEvent *event, String *prms, Cardinal *nprms)
     if(item) ((MenuProc *) item->proc) ();
 }
 
-static void
-MenuEngineSelect (Widget w, caddr_t addr, caddr_t index)
-{
-    RecentEngineEvent((int) (intptr_t) addr);
-}
-
-void
-AppendMenuItem (char *msg, int n)
-{
-    CreateMenuItem((Widget) optList[W_ENGIN].textValue, msg, (XtCallbackProc) MenuEngineSelect, n);
-}
-
 void
 SetupDropMenu ()
 {
@@ -1797,7 +1724,7 @@ HandlePV (Widget w, XEvent * event, String * params, Cardinal * nParams)
     MovePV(event->xmotion.x, event->xmotion.y, lineGap + BOARD_HEIGHT * (squareSize + lineGap));
 }
 
-static int savedIndex;  /* gross that this is global */
+extern int savedIndex;  /* gross that this is global */
 
 void
 CommentClick (Widget w, XEvent * event, String * params, Cardinal * nParams)
@@ -1814,27 +1741,6 @@ CommentClick (Widget w, XEvent * event, String * params, Cardinal * nParams)
        LoadVariation( index, val ); // [HGM] also does the actual moving to it, now
 }
 
-void
-EditCommentPopUp (int index, char *title, char *text)
-{
-    savedIndex = index;
-    if (text == NULL) text = "";
-    NewCommentPopup(title, text, index);
-}
-
-void
-CommentPopUp (char *title, char *text)
-{
-    savedIndex = currentMove; // [HGM] vari
-    NewCommentPopup(title, text, currentMove);
-}
-
-void
-CommentPopDown ()
-{
-    PopDown(CommentDlg);
-}
-
 
 /* Disable all user input other than deleting the window */
 static int frozen = 0;
@@ -2404,6 +2310,19 @@ SetClockIcon (int color)
     }
 }
 
+#define INPUT_SOURCE_BUF_SIZE 8192
+
+typedef struct {
+    CPKind kind;
+    int fd;
+    int lineByLine;
+    char *unused;
+    InputCallback func;
+    XtInputId xid;
+    char buf[INPUT_SOURCE_BUF_SIZE];
+    VOIDSTAR closure;
+} InputSource;
+
 void
 DoInputCallback (caddr_t closure, int *source, XtInputId *xid)
 {