Add -useBoardTexture and -usePieceFont options
[xboard.git] / xboard.c
index f9fe68f..033c53b 100644 (file)
--- a/xboard.c
+++ b/xboard.c
@@ -142,6 +142,10 @@ extern char *getenv();
 # endif
 #endif
 
+#if ENABLE_NLS
+#include <locale.h>
+#endif
+
 #include <X11/Intrinsic.h>
 #include <X11/StringDefs.h>
 #include <X11/Shell.h>
@@ -469,9 +473,8 @@ void BoardOptionsProc P((Widget w, XEvent *event, String *prms, Cardinal *nprms)
 void LoadOptionsProc P((Widget w, XEvent *event, String *prms, Cardinal *nprms));
 void SaveOptionsProc P((Widget w, XEvent *event, String *prms, Cardinal *nprms));
 void EditBookProc P((Widget w, XEvent *event, String *prms, Cardinal *nprms));
+void SelectMove P((Widget w, XEvent * event, String * params, Cardinal * nParams));
 void GameListOptionsPopDown P(());
-void ShufflePopDown P(());
-void TimeControlPopDown P(());
 void GenericPopDown P(());
 void update_ics_width P(());
 int get_term_width P(());
@@ -814,7 +817,7 @@ String pieceMenuStrings[2][PIECE_MENU_SIZE] = {
       N_("Archbishop"), N_("Chancellor"), "----", N_("Promote"), N_("Demote"),
       N_("Empty square"), N_("Clear board") }
 };
-/* must be in same order as PieceMenuStrings! */
+/* must be in same order as pieceMenuStrings! */
 ChessSquare pieceMenuTranslation[2][PIECE_MENU_SIZE] = {
     { WhitePlay, (ChessSquare) 0, WhitePawn, WhiteKnight, WhiteBishop,
        WhiteRook, WhiteQueen, WhiteKing, (ChessSquare) 0, WhiteAlfil,
@@ -830,7 +833,7 @@ ChessSquare pieceMenuTranslation[2][PIECE_MENU_SIZE] = {
 String dropMenuStrings[DROP_MENU_SIZE] = {
     "----", N_("Pawn"), N_("Knight"), N_("Bishop"), N_("Rook"), N_("Queen")
   };
-/* must be in same order as PieceMenuStrings! */
+/* must be in same order as dropMenuStrings! */
 ChessSquare dropMenuTranslation[DROP_MENU_SIZE] = {
     (ChessSquare) 0, WhitePawn, WhiteKnight, WhiteBishop,
     WhiteRook, WhiteQueen
@@ -1034,13 +1037,11 @@ XtActionsRec boardActions[] = {
     { "GameListPopDown", (XtActionProc) GameListPopDown },
     { "GameListOptionsPopDown", (XtActionProc) GameListOptionsPopDown },
     { "PromotionPopDown", (XtActionProc) PromotionPopDown },
-    { "HistoryPopDown", (XtActionProc) HistoryPopDown },
     { "EngineOutputPopDown", (XtActionProc) EngineOutputPopDown },
     { "EvalGraphPopDown", (XtActionProc) EvalGraphPopDown },
-    { "ShufflePopDown", (XtActionProc) ShufflePopDown },
-    { "TimeControlPopDown", (XtActionProc) TimeControlPopDown },
     { "GenericPopDown", (XtActionProc) GenericPopDown },
     { "CopyMemoProc", (XtActionProc) CopyMemoProc },
+    { "SelectMove", (XtActionProc) SelectMove },
 };
 
 char globalTranslations[] =
@@ -1050,6 +1051,8 @@ char globalTranslations[] =
    :Ctrl<Key>o: LoadGameProc() \n \
    :Meta<Key>Next: LoadNextGameProc() \n \
    :Meta<Key>Prior: LoadPrevGameProc() \n \
+   :Ctrl<Key>Down: LoadSelectedProc(3) \n \
+   :Ctrl<Key>Up: LoadSelectedProc(-3) \n \
    :Ctrl<Key>s: SaveGameProc() \n \
    :Ctrl<Key>c: CopyGameProc() \n \
    :Ctrl<Key>v: PasteGameProc() \n \
@@ -1086,6 +1089,8 @@ char globalTranslations[] =
    :Meta<Key>Right: ForwardProc() \n \
    :Meta<Key>Home: ToStartProc() \n \
    :Meta<Key>Left: BackwardProc() \n \
+   :<Key>Left: BackwardProc() \n \
+   :<Key>Right: ForwardProc() \n \
    :<Key>Home: RevertProc() \n \
    :<Key>End: TruncateGameProc() \n \
    :Ctrl<Key>m: MoveNowProc() \n \
@@ -1129,8 +1134,12 @@ char boardTranslations[] =
    Any<Btn3Down>: XawPositionSimpleMenu(menuB) XawPositionSimpleMenu(menuD) \
                  PieceMenuPopup(menuB) \n";
 
-char whiteTranslations[] = "<BtnDown>: WhiteClock()\n";
-char blackTranslations[] = "<BtnDown>: BlackClock()\n";
+char whiteTranslations[] =
+   "Shift<BtnDown>: WhiteClock(1)\n \
+   <BtnDown>: WhiteClock(0)\n";
+char blackTranslations[] =
+   "Shift<BtnDown>: BlackClock(1)\n \
+   <BtnDown>: BlackClock(0)\n";
 
 char ICSInputTranslations[] =
     "<Key>Up: UpKeyProc() \n "
@@ -1588,8 +1597,8 @@ GetWindowCoords()
 { // wrapper to shield use of window handles from back-end (make addressible by number?)
   // In XBoard this will have to wait until awareness of window parameters is implemented
   GetActualPlacement(shellWidget, &wpMain);
-  if(EngineOutputIsUp()) GetActualPlacement(engineOutputShell, &wpEngineOutput); else
-  if(MoveHistoryIsUp()) GetActualPlacement(historyShell, &wpMoveHistory);
+  if(EngineOutputIsUp()) GetActualPlacement(engineOutputShell, &wpEngineOutput);
+  if(MoveHistoryIsUp()) GetActualPlacement(shells[7], &wpMoveHistory);
   if(EvalGraphIsUp()) GetActualPlacement(evalGraphShell, &wpEvalGraph);
   if(GameListIsUp()) GetActualPlacement(gameListShell, &wpGameList);
   if(shellUp[1]) GetActualPlacement(shells[1], &wpComment);
@@ -1647,7 +1656,7 @@ ConvertToLine(int argc, char **argv)
   line[0] = NULLCHAR;
   for(i=1; i<argc; i++)
     {
-      if( (strchr(argv[i], ' ') || strchr(argv[i], '\n') ||strchr(argv[i], '\t') )
+      if( (strchr(argv[i], ' ') || strchr(argv[i], '\n') ||strchr(argv[i], '\t') || argv[i][0] == NULLCHAR)
          && argv[i][0] != '{' )
        snprintf(buf, sizeof(buf)/sizeof(buf[0]), "{%s} ", argv[i]);
       else
@@ -2025,6 +2034,12 @@ main(argc, argv)
         setbuf(debugFP, NULL);
     }
 
+#if ENABLE_NLS
+    if (appData.debugMode) {
+      fprintf(debugFP, "locale = %s\n", setlocale(LC_ALL, NULL));
+    }
+#endif
+
     /* [HGM,HR] make sure board size is acceptable */
     if(appData.NrFiles > BOARD_FILES ||
        appData.NrRanks > BOARD_RANKS   )
@@ -2158,17 +2173,26 @@ XBoard square size (hint): %d\n\
     appData.coordFont = InsertPxlSize(appData.coordFont, coordFontPxlSize);
     fontSet = CreateFontSet(appData.font);
     clockFontSet = CreateFontSet(appData.clockFont);
+    {
+      /* For the coordFont, use the 0th font of the fontset. */
+      XFontSet coordFontSet = CreateFontSet(appData.coordFont);
+      XFontStruct **font_struct_list;
+      char **font_name_list;
+      XFontsOfFontSet(coordFontSet, &font_struct_list, &font_name_list);
+      coordFontID = XLoadFont(xDisplay, font_name_list[0]);
+      coordFontStruct = XQueryFont(xDisplay, coordFontID);
+    }
 #else
     appData.font = FindFont(appData.font, fontPxlSize);
     appData.clockFont = FindFont(appData.clockFont, clockFontPxlSize);
     appData.coordFont = FindFont(appData.coordFont, coordFontPxlSize);
     clockFontID = XLoadFont(xDisplay, appData.clockFont);
     clockFontStruct = XQueryFont(xDisplay, clockFontID);
-#endif
     coordFontID = XLoadFont(xDisplay, appData.coordFont);
     coordFontStruct = XQueryFont(xDisplay, coordFontID);
-    countFontID = XLoadFont(xDisplay, appData.coordFont); // [HGM] holdings
-    countFontStruct = XQueryFont(xDisplay, countFontID);
+#endif
+    countFontID = coordFontID;  // [HGM] holdings
+    countFontStruct = coordFontStruct;
 
     xdb = XtDatabase(xDisplay);
 #if ENABLE_NLS
@@ -2677,6 +2701,8 @@ XBoard square size (hint): %d\n\
     return 0;
 }
 
+static Boolean noEcho;
+
 void
 ShutDownFrontEnd()
 {
@@ -2686,6 +2712,7 @@ ShutDownFrontEnd()
     if (saveSettingsOnExit) SaveSettings(settingsFileName);
     unlink(gameCopyFilename);
     unlink(gamePasteFilename);
+    if(noEcho) EchoOn();
 }
 
 RETSIGTYPE TermSizeSigHandler(int sig)
@@ -3111,18 +3138,51 @@ InsertPxlSize(pattern, targetPxlSize)
      char *pattern;
      int targetPxlSize;
 {
-    char *base_fnt_lst, strInt[3], *p;
+    char *base_fnt_lst, strInt[12], *p, *q;
+    int alternatives, i, len, strIntLen;
 
-    base_fnt_lst = calloc(1, strlen(pattern) + 3);
-    snprintf(strInt, sizeof(strInt)/sizeof(strInt[0]), "%d", targetPxlSize);
-    p = strstr(pattern, "--");
-    if (p == NULL) {
-      /* Can't insert size; use string as-is */
-      return pattern;
+    /*
+     * Replace the "*" (if present) in the pixel-size slot of each
+     * alternative with the targetPxlSize.
+     */
+    p = pattern;
+    alternatives = 1;
+    while ((p = strchr(p, ',')) != NULL) {
+      alternatives++;
+      p++;
+    }
+    snprintf(strInt, sizeof(strInt), "%d", targetPxlSize);
+    strIntLen = strlen(strInt);
+    base_fnt_lst = calloc(1, strlen(pattern) + strIntLen * alternatives + 1);
+
+    p = pattern;
+    q = base_fnt_lst;
+    while (alternatives--) {
+      char *comma = strchr(p, ',');
+      for (i=0; i<14; i++) {
+       char *hyphen = strchr(p, '-');
+       if (!hyphen) break;
+       if (comma && hyphen > comma) break;
+       len = hyphen + 1 - p;
+       if (i == 7 && *p == '*' && len == 2) {
+         p += len;
+         memcpy(q, strInt, strIntLen);
+         q += strIntLen;
+         *q++ = '-';
+       } else {
+         memcpy(q, p, len);
+         p += len;
+         q += len;
+       }
+      }
+      if (!comma) break;
+      len = comma + 1 - p;
+      memcpy(q, p, len);
+      p += len;
+      q += len;
     }
-    strncpy(base_fnt_lst, pattern, p - pattern + 2);
-    strcat(base_fnt_lst, strInt);
-    strcat(base_fnt_lst, strchr(p + 2, '-'));
+    strcpy(q, p);
+
     return base_fnt_lst;
 }
 
@@ -3137,11 +3197,22 @@ CreateFontSet(base_fnt_lst)
 
     fntSet = XCreateFontSet(xDisplay, base_fnt_lst,
                            &missing_list, &missing_count, &def_string);
-    if (missing_count > 0 && appData.debugMode) {
-      int i;
+    if (appData.debugMode) {
+      int i, count;
+      XFontStruct **font_struct_list;
+      char **font_name_list;
+      fprintf(debugFP, "Requested font set for list %s\n", base_fnt_lst);
+      if (fntSet) {
+       fprintf(debugFP, " got list %s, locale %s\n",
+               XBaseFontNameListOfFontSet(fntSet),
+               XLocaleOfFontSet(fntSet));
+       count = XFontsOfFontSet(fntSet, &font_struct_list, &font_name_list);
+       for (i = 0; i < count; i++) {
+         fprintf(debugFP, " got charset %s\n", font_name_list[i]);
+       }
+      }
       for (i = 0; i < missing_count; i++) {
-       fprintf(debugFP, _("Missing charset %s for %s (usually harmless)\n"),
-               missing_list[i], base_fnt_lst);
+       fprintf(debugFP, " missing charset %s\n", missing_list[i]);
       }
     }
     if (fntSet == NULL) {
@@ -3530,7 +3601,7 @@ void CreateXPMBoard(char *s, int kind)
 {
     XpmAttributes attr;
     attr.valuemask = 0;
-    if(s == NULL || *s == 0 || *s == '*') { useTexture &= ~(kind+1); return; }
+    if(!appData.useBitmaps || s == NULL || *s == 0 || *s == '*') { useTexture &= ~(kind+1); return; }
     if (XpmReadFileToPixmap(xDisplay, xBoardWindow, s, &(xpmBoardBitmap[kind]), NULL, &attr) == 0) {
        useTexture |= kind + 1; textureW[kind] = attr.width; textureH[kind] = attr.height;
     }
@@ -4078,6 +4149,7 @@ void WhiteClock(w, event, prms, nprms)
      String *prms;
      Cardinal *nprms;
 {
+    shiftKey = prms[0][0] & 1;
     ClockClick(0);
 }
 
@@ -4087,6 +4159,7 @@ void BlackClock(w, event, prms, nprms)
      String *prms;
      Cardinal *nprms;
 {
+    shiftKey = prms[0][0] & 1;
     ClockClick(1);
 }
 
@@ -4930,6 +5003,14 @@ void CommentPopDown()
     PopDown(1);
 }
 
+static char *openName;
+FILE *openFP;
+
+void DelayedLoad()
+{
+  (void) (*fileProc)(openFP, 0, openName);
+}
+
 void FileNamePopUp(label, def, filter, proc, openMode)
      char *label;
      char *def;
@@ -4940,12 +5021,11 @@ void FileNamePopUp(label, def, filter, proc, openMode)
     fileProc = proc;           /* I can't see a way not */
     fileOpenMode = openMode;   /*   to use globals here */
     {   // [HGM] use file-selector dialog stolen from Ghostview
-       char *name;
        int index; // this is not supported yet
-       FILE *f;
-       if(f = XsraSelFile(shellWidget, label, NULL, NULL, "could not open: ",
-                          (def[0] ? def : NULL), filter, openMode, NULL, &name))
-         (void) (*fileProc)(f, index=0, name);
+       if(openFP = XsraSelFile(shellWidget, label, NULL, NULL, "could not open: ",
+                          (def[0] ? def : NULL), filter, openMode, NULL, &openName))
+         // [HGM] delay to give expose event opportunity to redraw board after browser-dialog popdown before lengthy load starts
+         ScheduleDelayedEvent(&DelayedLoad, 50);
     }
 }
 
@@ -5063,7 +5143,7 @@ void PromotionPopUp()
                         (XtPointer) dialog);
       XawDialogAddButton(dialog, _("Captain"), PromotionCallback,
                         (XtPointer) dialog);
-    } else {\r
+    } else {
     XawDialogAddButton(dialog, _("Queen"), PromotionCallback,
                       (XtPointer) dialog);
     XawDialogAddButton(dialog, _("Rook"), PromotionCallback,
@@ -5074,7 +5154,7 @@ void PromotionPopUp()
                       (XtPointer) dialog);
     }
     if (!appData.testLegality || gameInfo.variant == VariantSuicide ||
-        gameInfo.variant == VariantSpartan && !WhiteOnMove(currentMove) ||\r
+        gameInfo.variant == VariantSpartan && !WhiteOnMove(currentMove) ||
         gameInfo.variant == VariantGiveaway) {
       XawDialogAddButton(dialog, _("King"), PromotionCallback,
                         (XtPointer) dialog);
@@ -5673,8 +5753,6 @@ SendGameSelection(Widget w, Atom *selection, Atom *target,
 
 void CopySomething()
 {
-  int ret;
-
   /*
    * Set both PRIMARY (the selection) and CLIPBOARD, since we don't
    * have a notion of a game that is selected but not copied.
@@ -5886,7 +5964,7 @@ void MatchProc(w, event, prms, nprms)
      String *prms;
      Cardinal *nprms;
 {
-    MatchEvent(2);\r
+    MatchEvent(2);
 }
 
 void IcsClientProc(w, event, prms, nprms)
@@ -6876,6 +6954,7 @@ void DisplayTitle(text)
     XtSetArg(args[i], XtNiconName, (XtArgVal) icon);    i++;
     XtSetArg(args[i], XtNtitle, (XtArgVal) title);      i++;
     XtSetValues(shellWidget, args, i);
+    XSync(xDisplay, False);
 }
 
 
@@ -7164,15 +7243,23 @@ PlayAlarmSound()
 }
 
 void
+PlayTellSound()
+{
+  PlaySound(appData.soundTell);
+}
+
+void
 EchoOn()
 {
     system("stty echo");
+    noEcho = False;
 }
 
 void
 EchoOff()
 {
     system("stty -echo");
+    noEcho = True;
 }
 
 void
@@ -7675,7 +7762,7 @@ int OpenTCP(host, port, pr)
     struct addrinfo hints;
     struct addrinfo *ais, *ai;
     int error;
-    int s;
+    int s=0;
     ChildProc *cp;
 
     memset(&hints, 0, sizeof(hints));
@@ -8573,8 +8660,8 @@ AnimateMove(board, fromX, fromY, toX, toY)
 }
 
 void
-DragPieceBegin(x, y)
-     int x; int y;
+DragPieceBegin(x, y, instantly)
+     int x; int y; Boolean instantly;
 {
     int         boardX, boardY, color;
     XPoint corner;
@@ -8938,3 +9025,9 @@ void DrawArrowHighlight(int fromX, int fromY, int toX,int toY)
     if( IsDrawArrowEnabled() && fromX >= 0 && fromY >= 0 && toX >= 0 && toY >= 0)
         DrawArrowBetweenSquares(fromX, fromY, toX, toY);
 }
+
+void UpdateLogos(int displ)
+{
+    return; // no logos in XBoard yet
+}
+