adds new piece types in the promotion dialog and edit-position piece menu
[xboard.git] / xboard.c
index c4360fc..f9ed2d7 100644 (file)
--- a/xboard.c
+++ b/xboard.c
@@ -456,6 +456,7 @@ Display *xDisplay;
 Window xBoardWindow;
 Pixel lightSquareColor, darkSquareColor, whitePieceColor, blackPieceColor,
   jailSquareColor, highlightSquareColor, premoveHighlightColor;
+Pixel lowTimeWarningColor;
 GC lightSquareGC, darkSquareGC, jailSquareGC, lineGC, wdPieceGC, wlPieceGC,
   bdPieceGC, blPieceGC, wbPieceGC, bwPieceGC, coordGC, highlineGC,
   wjPieceGC, bjPieceGC, prelineGC, countGC;
@@ -695,21 +696,27 @@ MenuItem buttonBar[] = {
     {NULL, NULL}
 };
 
-#define PIECE_MENU_SIZE 11
+#define PIECE_MENU_SIZE 18
 String pieceMenuStrings[2][PIECE_MENU_SIZE] = {
     { N_("White"), "----", N_("Pawn"), N_("Knight"), N_("Bishop"), N_("Rook"),
-      N_("Queen"), N_("King"), "----", N_("Empty square"), N_("Clear board") },
+      N_("Queen"), N_("King"), "----", N_("Elephant"), N_("Cannon"), 
+      N_("Archbishop"), N_("Chancellor"), "----", N_("Promote"), N_("Demote"), 
+      N_("Empty square"), N_("Clear board") },
     { N_("Black"), "----", N_("Pawn"), N_("Knight"), N_("Bishop"), N_("Rook"),
-      N_("Queen"), N_("King"), "----", N_("Empty square"), N_("Clear board") },
+      N_("Queen"), N_("King"), "----", N_("Elephant"), N_("Cannon"), 
+      N_("Archbishop"), N_("Chancellor"), "----", N_("Promote"), N_("Demote"), 
+      N_("Empty square"), N_("Clear board") }
 };
 /* must be in same order as PieceMenuStrings! */
 ChessSquare pieceMenuTranslation[2][PIECE_MENU_SIZE] = {
     { WhitePlay, (ChessSquare) 0, WhitePawn, WhiteKnight, WhiteBishop,
-       WhiteRook, WhiteQueen, WhiteKing,
-       (ChessSquare) 0, EmptySquare, ClearBoard },
+       WhiteRook, WhiteQueen, WhiteKing, (ChessSquare) 0, WhiteAlfil,
+       WhiteCannon, WhiteAngel, WhiteMarshall, (ChessSquare) 0, 
+       PromotePiece, DemotePiece, EmptySquare, ClearBoard },
     { BlackPlay, (ChessSquare) 0, BlackPawn, BlackKnight, BlackBishop,
-       BlackRook, BlackQueen, BlackKing,
-       (ChessSquare) 0, EmptySquare, ClearBoard },
+       BlackRook, BlackQueen, BlackKing, (ChessSquare) 0, BlackAlfil,
+       BlackCannon, BlackAngel, BlackMarshall, (ChessSquare) 0, 
+       PromotePiece, DemotePiece, EmptySquare, ClearBoard },
 };
 
 #define DROP_MENU_SIZE 6
@@ -1059,6 +1066,9 @@ XtResource clientResources[] = {
     { "zippyReplayTimeout", "zippyReplayTimeout", XtRInt, sizeof(int),
        XtOffset(AppDataPtr, zippyReplayTimeout), XtRImmediate,
         (XtPointer) ZIPPY_REPLAY_TIMEOUT },
+    { "zippyShortGame", "zippyShortGame", XtRInt, sizeof(int),
+       XtOffset(AppDataPtr, zippyShortGame), XtRImmediate,
+        (XtPointer) 0 },
 #endif
     { "flashCount", "flashCount", XtRInt, sizeof(int),
        XtOffset(AppDataPtr, flashCount), XtRImmediate,
@@ -1192,6 +1202,12 @@ XtResource clientResources[] = {
     { "showButtonBar", "showButtonBar", XtRBoolean,
        sizeof(Boolean), XtOffset(AppDataPtr, showButtonBar),
        XtRImmediate, (XtPointer) True },
+    { "lowTimeWarningColor", "lowTimeWarningColor", XtRString,
+      sizeof(String), XtOffset(AppDataPtr, lowTimeWarningColor),
+      XtRString, COLOR_LOWTIMEWARNING },
+    { "lowTimeWarning", "lowTimeWarning", XtRBoolean,
+      sizeof(Boolean), XtOffset(AppDataPtr, lowTimeWarning),
+      XtRImmediate, (XtPointer) False },
     {"icsEngineAnalyze", "icsEngineAnalyze", XtRBoolean,        /* [DM] icsEngineAnalyze */
         sizeof(Boolean), XtOffset(AppDataPtr, icsEngineAnalyze),
         XtRImmediate, (XtPointer) False },
@@ -1601,6 +1617,7 @@ XrmOptionDescRec shellOptions[] = {
     { "-zippyVariants", "zippyVariants", XrmoptionSepArg, NULL },
     { "-zippyMaxGames", "zippyMaxGames", XrmoptionSepArg, NULL },
     { "-zippyReplayTimeout", "zippyReplayTimeout", XrmoptionSepArg, NULL },
+    { "-zippyShortGame", "zippyShortGame", XrmoptionSepArg, NULL },
 #endif
     { "-flashCount", "flashCount", XrmoptionSepArg, NULL },
     { "-flash", "flashCount", XrmoptionNoArg, "3" },
@@ -1669,6 +1686,8 @@ XrmOptionDescRec shellOptions[] = {
     { "-showButtonBar", "showButtonBar", XrmoptionSepArg, NULL },
     { "-buttons", "showButtonBar", XrmoptionNoArg, "True" },
     { "-xbuttons", "showButtonBar", XrmoptionNoArg, "False" },
+    { "-lowTimeWarningColor", "lowTimeWarningColor", XrmoptionSepArg, NULL },
+    { "-lowTimeWarning", "lowTimeWarning", XrmoptionSepArg, NULL },
     /* [AS,HR] New features */
     { "-firstScoreAbs", "firstScoreAbs", XrmoptionSepArg, NULL },
     { "-secondScoreAbs", "secondScoreAbs", XrmoptionSepArg, NULL },
@@ -2144,7 +2163,7 @@ CatchDeleteWindow(Widget w, String procname)
 {
   char buf[MSG_SIZ];
   XSetWMProtocols(xDisplay, XtWindow(w), &wm_delete_window, 1);
-  sprintf(buf, "<Message>WM_PROTOCOLS: %s() \n", procname);
+  snprintf(buf, sizeof(buf), "<Message>WM_PROTOCOLS: %s() \n", procname);
   XtAugmentTranslations(w, XtParseTranslationTable(buf));
 }
 
@@ -2316,6 +2335,20 @@ main(argc, argv)
     if (argc > 1) {
        fprintf(stderr, _("%s: unrecognized argument %s\n"),
                programName, argv[1]);
+       fprintf(stderr, "Recognized options:\n");
+       for(i = 0; i < XtNumber(shellOptions); i++) {
+           j = fprintf(stderr, "  %s%s", shellOptions[i].option,
+                       (shellOptions[i].argKind == XrmoptionSepArg
+                        ? " ARG" : ""));
+           if (i++ < XtNumber(shellOptions)) {
+               fprintf(stderr, "%*c%s%s\n", 40 - j, ' ',
+                       shellOptions[i].option,
+                       (shellOptions[i].argKind == XrmoptionSepArg
+                        ? " ARG" : ""));
+           } else {
+               fprintf(stderr, "\n");
+           }
+       }
        exit(2);
     }
 
@@ -2334,8 +2367,8 @@ main(argc, argv)
     i = strlen(p) + strlen("/.xboardXXXXXx.pgn") + 1;
     gameCopyFilename = (char*) malloc(i);
     gamePasteFilename = (char*) malloc(i);
-    sprintf(gameCopyFilename, "%s/.xboard%05uc.pgn", p, getpid());
-    sprintf(gamePasteFilename, "%s/.xboard%05up.pgn", p, getpid());
+    snprintf(gameCopyFilename,i, "%s/.xboard%05uc.pgn", p, getpid());
+    snprintf(gamePasteFilename,i, "%s/.xboard%05up.pgn", p, getpid());
 
     XtGetApplicationResources(shellWidget, (XtPointer) &appData,
                              clientResources, XtNumber(clientResources),
@@ -2573,6 +2606,20 @@ XBoard square size (hint): %d\n\
     if (forceMono) {
       fprintf(stderr, _("%s: too few colors available; trying monochrome mode\n"),
              programName);
+      
+      if (appData.bitmapDirectory == NULL ||
+             appData.bitmapDirectory[0] == NULLCHAR)
+           appData.bitmapDirectory = DEF_BITMAP_DIR;
+    }
+
+    if (appData.lowTimeWarning && !appData.monoMode) {
+      vFrom.addr = (caddr_t) appData.lowTimeWarningColor;
+      vFrom.size = strlen(appData.lowTimeWarningColor);
+      XtConvert(shellWidget, XtRString, &vFrom, XtRPixel, &vTo);
+      if (vTo.addr == NULL) 
+               appData.monoMode = True;
+      else
+               lowTimeWarningColor = *(Pixel *) vTo.addr;
     }
 
     if (appData.monoMode && appData.debugMode) {
@@ -3697,7 +3744,7 @@ void CreateXIMPieces()
            fprintf(stderr, "%d", piece+1);
            for (kind=0; kind<4; kind++) {
                fprintf(stderr, ".");
-               sprintf(buf, "%s/%c%s%u.xim",
+               snprintf(buf, sizeof(buf), "%s/%c%s%u.xim",
                        ExpandPathName(appData.pixmapDirectory),
                        ToLower(PieceToChar((ChessSquare)piece)),
                        ximkind[kind], ss);
@@ -3716,7 +3763,7 @@ void CreateXIMPieces()
        /* Load light and dark squares */
        /* If the LSQ and DSQ pieces don't exist, we will
           draw them with solid squares. */
-       sprintf(buf, "%s/lsq%u.xim", ExpandPathName(appData.pixmapDirectory), ss);
+       snprintf(buf,sizeof(buf), "%s/lsq%u.xim", ExpandPathName(appData.pixmapDirectory), ss);
        if (access(buf, 0) != 0) {
            useImageSqs = 0;
        } else {
@@ -3730,7 +3777,7 @@ void CreateXIMPieces()
 
            loadXIM(ximLightSquare, NULL, buf, &xpmLightSquare, NULL);
            fprintf(stderr, _("dark square "));
-           sprintf(buf, "%s/dsq%u.xim",
+           snprintf(buf,sizeof(buf), "%s/dsq%u.xim",
                    ExpandPathName(appData.pixmapDirectory), ss);
            if (appData.debugMode)
              fprintf(stderr, _("(File:%s:) "), buf);
@@ -3745,6 +3792,8 @@ void CreateXIMPieces()
     XSynchronize(xDisplay, False); /* Work-around for xlib/xt buffering bug */
 }
 
+char pieceBitmapNames[] = "pnbrqfeacwmohijgdvlsuk";
+
 #if HAVE_LIBXPM
 void CreateXPMPieces()
 {
@@ -3819,9 +3868,9 @@ void CreateXPMPieces()
        for (piece = (int) WhitePawn; piece <= (int) WhiteKing; piece++) {
            fprintf(stderr, "%d ", piece+1);
            for (kind=0; kind<4; kind++) {
-               sprintf(buf, "%s/%c%s%u.xpm",
+             snprintf(buf, sizeof(buf), "%s/%c%s%u.xpm",
                        ExpandPathName(appData.pixmapDirectory),
-                       ToLower(PieceToChar((ChessSquare)piece)),
+                       pieceBitmapNames[piece],
                        xpmkind[kind], ss);
                if (appData.debugMode) {
                    fprintf(stderr, _("(File:%s:) "), buf);
@@ -3839,7 +3888,7 @@ void CreateXPMPieces()
        /* If the LSQ and DSQ pieces don't exist, we will
           draw them with solid squares. */
        fprintf(stderr, _("light square "));
-       sprintf(buf, "%s/lsq%u.xpm", ExpandPathName(appData.pixmapDirectory), ss);
+       snprintf(buf, sizeof(buf), "%s/lsq%u.xpm", ExpandPathName(appData.pixmapDirectory), ss);
        if (access(buf, 0) != 0) {
            useImageSqs = 0;
        } else {
@@ -3853,7 +3902,7 @@ void CreateXPMPieces()
                exit(1);
            }
            fprintf(stderr, _("dark square "));
-           sprintf(buf, "%s/dsq%u.xpm",
+           snprintf(buf, sizeof(buf), "%s/dsq%u.xpm",
                    ExpandPathName(appData.pixmapDirectory), ss);
            if (appData.debugMode) {
                fprintf(stderr, _("(File:%s:) "), buf);
@@ -3885,7 +3934,7 @@ void CreatePieces()
 
     for (kind = SOLID; kind <= (appData.monoMode ? OUTLINE : SOLID); kind++) {
        for (piece = (int) WhitePawn; piece <= (int) WhiteKing; piece++) {
-           sprintf(buf, "%c%u%c.bm", ToLower(PieceToChar((ChessSquare)piece)),
+           sprintf(buf, "%c%u%c.bm", pieceBitmapNames[piece],
                    ss, kind == SOLID ? 's' : 'o');
            ReadBitmap(&pieceBitmap[kind][piece], buf, NULL, ss, ss);
        }
@@ -3910,7 +3959,7 @@ void CreatePieces()
 
     for (kind = SOLID; kind <= (appData.monoMode ? OUTLINE : SOLID); kind++) {
        for (piece = (int) WhitePawn; piece <= (int) WhiteKing; piece++) {
-           sprintf(buf, "%c%u%c.bm", ToLower(PieceToChar((ChessSquare)piece)),
+           sprintf(buf, "%c%u%c.bm", pieceBitmapNames[piece],
                    ss, kind == SOLID ? 's' : 'o');
            ReadBitmap(&pieceBitmap[kind][piece], buf,
                       bib->bits[kind][piece], ss, ss);
@@ -3939,20 +3988,21 @@ void ReadBitmap(pm, name, bits, wreq, hreq)
        strcat(fullname, name);
        errcode = XReadBitmapFile(xDisplay, xBoardWindow, fullname,
                                  &w, &h, pm, &x_hot, &y_hot);
+    fprintf(stderr, "load %s\n", name);
        if (errcode != BitmapSuccess) {
            switch (errcode) {
              case BitmapOpenFailed:
-               sprintf(msg, _("Can't open bitmap file %s"), fullname);
+               snprintf(msg, sizeof(msg), _("Can't open bitmap file %s"), fullname);
                break;
              case BitmapFileInvalid:
-               sprintf(msg, _("Invalid bitmap in file %s"), fullname);
+               snprintf(msg, sizeof(msg), _("Invalid bitmap in file %s"), fullname);
                break;
              case BitmapNoMemory:
-               sprintf(msg, _("Ran out of memory reading bitmap file %s"),
+               snprintf(msg, sizeof(msg), _("Ran out of memory reading bitmap file %s"),
                        fullname);
                break;
              default:
-               sprintf(msg, _("Unknown XReadBitmapFile error %d on file %s"),
+               snprintf(msg, sizeof(msg), _("Unknown XReadBitmapFile error %d on file %s"),
                        errcode, fullname);
                break;
            }
@@ -4208,7 +4258,7 @@ void SetupDropMenu()
                                       && !appData.icsActive));
        count = 0;
        while (p && *p++ == dmEnables[i].piece) count++;
-       sprintf(label, "%s  %d", dmEnables[i].widget, count);
+       snprintf(label, sizeof(label), "%s  %d", dmEnables[i].widget, count);
        j = 0;
        XtSetArg(args[j], XtNlabel, label); j++;
        XtSetValues(entry, args, j);
@@ -5792,11 +5842,12 @@ void PromotionPopUp()
                            layoutArgs, XtNumber(layoutArgs));
 
     j = 0;
-    XtSetArg(args[j], XtNlabel, _("Promote pawn to what?")); j++;
+    XtSetArg(args[j], XtNlabel, _("Promote to what?")); j++;
     XtSetArg(args[j], XtNborderWidth, 0); j++;
     dialog = XtCreateManagedWidget("promotion", dialogWidgetClass,
                                   layout, args, j);
 
+  if(gameInfo.variant != VariantShogi) {
     XawDialogAddButton(dialog, _("Queen"), PromotionCallback,
                       (XtPointer) dialog);
     XawDialogAddButton(dialog, _("Rook"), PromotionCallback,
@@ -5810,6 +5861,21 @@ void PromotionPopUp()
       XawDialogAddButton(dialog, _("King"), PromotionCallback,
                         (XtPointer) dialog);
     }
+    if(gameInfo.variant == VariantCapablanca || 
+       gameInfo.variant == VariantGothic || 
+       gameInfo.variant == VariantCapaRandom) {
+      XawDialogAddButton(dialog, _("Archbishop"), PromotionCallback,
+                        (XtPointer) dialog);
+      XawDialogAddButton(dialog, _("Chancellor"), PromotionCallback,
+                        (XtPointer) dialog);
+    }
+  } else // [HGM] shogi
+  {
+      XawDialogAddButton(dialog, _("Promote"), PromotionCallback,
+                        (XtPointer) dialog);
+      XawDialogAddButton(dialog, _("Defer"), PromotionCallback,
+                        (XtPointer) dialog);
+  }
     XawDialogAddButton(dialog, _("cancel"), PromotionCallback,
                       (XtPointer) dialog);
 
@@ -5864,6 +5930,10 @@ void PromotionCallback(w, client_data, call_data)
        return;
     } else if (strcmp(name, _("Knight")) == 0) {
        promoChar = 'n';
+    } else if (strcmp(name, _("Promote")) == 0) {
+       promoChar = '+';
+    } else if (strcmp(name, _("Defer")) == 0) {
+       promoChar = '=';
     } else {
        promoChar = ToLower(name[0]);
     }
@@ -6471,7 +6541,7 @@ void AnalyzeModeProc(w, event, prms, nprms)
     char buf[MSG_SIZ];
 
     if (!first.analysisSupport) {
-      sprintf(buf, _("%s does not support analysis"), first.tidy);
+      snprintf(buf, sizeof(buf), _("%s does not support analysis"), first.tidy);
       DisplayError(buf, 0);
       return;
     }
@@ -6513,7 +6583,7 @@ void AnalyzeFileProc(w, event, prms, nprms)
 {
     if (!first.analysisSupport) {
       char buf[MSG_SIZ];
-      sprintf(buf, _("%s does not support analysis"), first.tidy);
+      snprintf(buf, sizeof(buf), _("%s does not support analysis"), first.tidy);
       DisplayError(buf, 0);
       return;
     }
@@ -7349,7 +7419,7 @@ void InfoProc(w, event, prms, nprms)
      Cardinal *nprms;
 {
     char buf[MSG_SIZ];
-    sprintf(buf, "xterm -e info --directory %s --directory . -f %s &",
+    snprintf(buf, sizeof(buf), "xterm -e info --directory %s --directory . -f %s &",
            INFODIR, INFOFILE);
     system(buf);
 }
@@ -7366,7 +7436,7 @@ void ManProc(w, event, prms, nprms)
       name = prms[0];
     else
       name = "xboard";
-    sprintf(buf, "xterm -e man %s &", name);
+    snprintf(buf, sizeof(buf), "xterm -e man %s &", name);
     system(buf);
 }
 
@@ -7400,7 +7470,7 @@ void AboutProc(w, event, prms, nprms)
 #else
     char *zippy = "";
 #endif
-    sprintf(buf, "%s%s\n\n%s\n%s\n%s\n\n%s%s\n%s",
+    snprintf(buf, sizeof(buf), "%s%s\n\n%s\n%s\n%s\n\n%s%s\n%s",
            programVersion, zippy,
            "Copyright 1991 Digital Equipment Corporation",
            "Enhancements Copyright 1992-2009 Free Software Foundation",
@@ -7458,7 +7528,7 @@ void DisplayMessage(message, extMessage)
 
     if (extMessage) {
        if (*message) {
-           sprintf(buf, "%s  %s", message, extMessage);
+           snprintf(buf, sizeof(buf), "%s  %s", message, extMessage);
            message = buf;
        } else {
            message = extMessage;
@@ -7488,11 +7558,11 @@ void DisplayTitle(text)
        strcpy(icon, text);
        strcpy(title, text);
     } else if (appData.icsActive) {
-       sprintf(icon, "%s", appData.icsHost);
-       sprintf(title, "%s: %s", programName, appData.icsHost);
+        snprintf(icon, sizeof(icon), "%s", appData.icsHost);
+       snprintf(title, sizeof(title), "%s: %s", programName, appData.icsHost);
     } else if (appData.cmailGameName[0] != NULLCHAR) {
-       sprintf(icon, "%s", "CMail");
-       sprintf(title, "%s: %s", programName, "CMail");
+        snprintf(icon, sizeof(icon), "%s", "CMail");
+       snprintf(title,sizeof(title), "%s: %s", programName, "CMail");
 #ifdef GOTHIC
     // [HGM] license: This stuff should really be done in back-end, but WinBoard already had a pop-up for it
     } else if (gameInfo.variant == VariantGothic) {
@@ -7509,7 +7579,7 @@ void DisplayTitle(text)
        strcpy(title, programName);
     } else {
        strcpy(icon, first.tidy);
-       sprintf(title, "%s: %s", programName, first.tidy);
+       snprintf(title,sizeof(title), "%s: %s", programName, first.tidy);
     }
     i = 0;
     XtSetArg(args[i], XtNiconName, (XtArgVal) icon);    i++;
@@ -7533,7 +7603,7 @@ void DisplayError(message, error)
            fprintf(stderr, "%s: %s: %s\n",
                    programName, message, strerror(error));
        }
-       sprintf(buf, "%s: %s", message, strerror(error));
+       snprintf(buf, sizeof(buf), "%s: %s", message, strerror(error));
        message = buf;
     }
     ErrorPopUp(_("Error"), message, FALSE);
@@ -7569,7 +7639,7 @@ void DisplayFatalError(message, error, status)
     } else {
        fprintf(stderr, "%s: %s: %s\n",
                programName, message, strerror(error));
-       sprintf(buf, "%s: %s", message, strerror(error));
+       snprintf(buf, sizeof(buf), "%s: %s", message, strerror(error));
        message = buf;
     }
     if (appData.popupExitMessage && boardWidget && XtIsRealized(boardWidget)) {
@@ -7758,7 +7828,7 @@ PlaySound(name)
     putc(BELLCHAR, stderr);
   } else {
     char buf[2048];
-    sprintf(buf, "%s '%s' &", appData.soundProgram, name);
+    snprintf(buf, sizeof(buf), "%s '%s' &", appData.soundProgram, name);
     system(buf);
   }
 }
@@ -8088,6 +8158,14 @@ DisplayTimerLabel(w, color, timer, highlight)
     char buf[MSG_SIZ];
     Arg args[16];
 
+    Pixel foregroundOrWarningColor = timerForegroundPixel;
+
+    if (timer > 0
+       && appData.lowTimeWarning
+       && (timer / 1000) < appData.icsAlarmTime)
+
+      foregroundOrWarningColor = lowTimeWarningColor;
+
     if (appData.clockMode) {
        sprintf(buf, "%s: %s", color, TimeString(timer));
        XtSetArg(args[0], XtNlabel, buf);
@@ -8097,11 +8175,12 @@ DisplayTimerLabel(w, color, timer, highlight)
     }
 
     if (highlight) {
-       XtSetArg(args[1], XtNbackground, timerForegroundPixel);
+
+       XtSetArg(args[1], XtNbackground, foregroundOrWarningColor);
        XtSetArg(args[2], XtNforeground, timerBackgroundPixel);
     } else {
        XtSetArg(args[1], XtNbackground, timerBackgroundPixel);
-       XtSetArg(args[2], XtNforeground, timerForegroundPixel);
+       XtSetArg(args[2], XtNforeground, foregroundOrWarningColor);
     }
 
     XtSetValues(w, args, 3);
@@ -8277,9 +8356,9 @@ int OpenTelnet(host, port, pr)
     char cmdLine[MSG_SIZ];
 
     if (port[0] == NULLCHAR) {
-       sprintf(cmdLine, "%s %s", appData.telnetProgram, host);
+      snprintf(cmdLine, sizeof(cmdLine), "%s %s", appData.telnetProgram, host);
     } else {
-       sprintf(cmdLine, "%s %s %s", appData.telnetProgram, host, port);
+      snprintf(cmdLine,sizeof(cmdLine), "%s %s %s", appData.telnetProgram, host, port);
     }
     return StartChildProcess(cmdLine, "", pr);
 }