Implement -positionDir option GTK
[xboard.git] / backend.c
index dd8bbb1..6bf57ac 100644 (file)
--- a/backend.c
+++ b/backend.c
@@ -6948,7 +6948,7 @@ UserMoveEvent(int fromX, int fromY, int toX, int toY, int promoChar)
        /* EditPosition, empty square, or different color piece;
           click-click move is possible */
        if (toX == -2 || toY == -2) {
-           boards[0][fromY][fromX] = EmptySquare;
+           boards[0][fromY][fromX] = (boards[0][fromY][fromX] == EmptySquare ? DarkSquare : EmptySquare);
            DrawPosition(FALSE, boards[currentMove]);
            return;
        } else if (toX >= 0 && toY >= 0) {
@@ -7323,8 +7323,8 @@ CanPromote (ChessSquare piece, int y)
          gameInfo.variant == VariantMakruk   || gameInfo.variant == VariantASEAN) return FALSE;
        return (piece == BlackPawn && y <= zone ||
                piece == WhitePawn && y >= BOARD_HEIGHT-1-zone ||
-               piece == BlackLance && y == 1 ||
-               piece == WhiteLance && y == BOARD_HEIGHT-2 );
+               piece == BlackLance && y <= zone ||
+               piece == WhiteLance && y >= BOARD_HEIGHT-1-zone );
 }
 
 void
@@ -7367,6 +7367,8 @@ void ReportClick(char *action, int x, int y)
        SendToProgram(buf, &first);
 }
 
+Boolean right; // instructs front-end to use button-1 events as if they were button 3
+
 void
 LeftClick (ClickType clickType, int xPix, int yPix)
 {
@@ -7377,13 +7379,6 @@ LeftClick (ClickType clickType, int xPix, int yPix)
     ChessSquare piece;
     static TimeMark lastClickTime, prevClickTime;
 
-    if(SeekGraphClick(clickType, xPix, yPix, 0)) return;
-
-    prevClickTime = lastClickTime; GetTimeMark(&lastClickTime);
-
-    if (clickType == Press) ErrorPopDown();
-    lastClickType = clickType, lastLeftX = xPix, lastLeftY = yPix; // [HGM] alien: remember state
-
     x = EventToSquare(xPix, BOARD_WIDTH);
     y = EventToSquare(yPix, BOARD_HEIGHT);
     if (!flipView && y >= 0) {
@@ -7393,6 +7388,20 @@ LeftClick (ClickType clickType, int xPix, int yPix)
        x = BOARD_WIDTH - 1 - x;
     }
 
+    if(appData.monoMouse && gameMode == EditPosition && fromX < 0 && clickType == Press && boards[currentMove][y][x] == EmptySquare) {
+       static int dummy;
+       RightClick(clickType, xPix, yPix, &dummy, &dummy);
+       right = TRUE;
+       return;
+    }
+
+    if(SeekGraphClick(clickType, xPix, yPix, 0)) return;
+
+    prevClickTime = lastClickTime; GetTimeMark(&lastClickTime);
+
+    if (clickType == Press) ErrorPopDown();
+    lastClickType = clickType, lastLeftX = xPix, lastLeftY = yPix; // [HGM] alien: remember state
+
     if(promoSweep != EmptySquare) { // up-click during sweep-select of promo-piece
        defaultPromoChoice = promoSweep;
        promoSweep = EmptySquare;   // terminate sweep
@@ -7486,7 +7495,7 @@ LeftClick (ClickType clickType, int xPix, int yPix)
            return;
        }
     }
-
+printf("to click %d,%d\n",x,y);
     /* fromX != -1 */
     if (clickType == Press && gameMode != EditPosition) {
        ChessSquare fromP;
@@ -7549,6 +7558,12 @@ LeftClick (ClickType clickType, int xPix, int yPix)
        // ignore clicks on holdings
        if(x < BOARD_LEFT || x >= BOARD_RGHT) return;
     }
+printf("A type=%d\n",clickType);
+
+    if(x == fromX && y == fromY && gameMode == EditPosition && SubtractTimeMarks(&lastClickTime, &prevClickTime) < 200) {
+       gatingPiece = boards[currentMove][fromY][fromX]; // prepare to copy rather than move
+       return;
+    }
 
     if (clickType == Release && x == fromX && y == fromY && killX < 0) {
        DragPieceEnd(xPix, yPix); dragging = 0;
@@ -7582,7 +7597,7 @@ LeftClick (ClickType clickType, int xPix, int yPix)
     }
 
     clearFlag = 0;
-
+printf("B\n");
     if(gameMode != EditPosition && !appData.testLegality && !legal[y][x] &&
        fromX >= BOARD_LEFT && fromX < BOARD_RGHT && (x != killX || y != killY) && !sweepSelecting) {
        if(dragging) DragPieceEnd(xPix, yPix), dragging = 0;
@@ -7590,7 +7605,7 @@ LeftClick (ClickType clickType, int xPix, int yPix)
        DrawPosition(TRUE, NULL);
        return; // ignore to-click
     }
-
+printf("(%d,%d)-(%d,%d) %d %d\n",fromX,fromY,toX,toY,x,y);
     /* we now have a different from- and (possibly off-board) to-square */
     /* Completed move */
     if(!sweepSelecting) {
@@ -9890,14 +9905,14 @@ ParseGameHistory (char *game)
 void
 ApplyMove (int fromX, int fromY, int toX, int toY, int promoChar, Board board)
 {
-  ChessSquare captured = board[toY][toX], piece, king, killed; int p, rookX, oldEP = EP_NONE, berolina = 0;
+  ChessSquare captured = board[toY][toX], piece, pawn, king, killed; int p, rookX, oldEP, epRank, berolina = 0;
   int promoRank = gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand || gameInfo.variant == VariantChuChess ? 3 : 1;
 
     /* [HGM] compute & store e.p. status and castling rights for new position */
     /* we can always do that 'in place', now pointers to these rights are passed to ApplyMove */
 
       if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A;
-      oldEP = (signed char)board[EP_STATUS];
+      oldEP = (signed char)board[EP_FILE]; epRank = board[EP_RANK];
       board[EP_STATUS] = EP_NONE;
       board[EP_FILE] = board[EP_RANK] = 100;
 
@@ -9927,14 +9942,14 @@ ApplyMove (int fromX, int fromY, int toX, int toY, int promoChar, Board board)
            }
       }
 
-      piece = board[fromY][fromX];
-      if( piece == WhiteLance || piece == BlackLance ) {
+      pawn = board[fromY][fromX];
+      if( pawn == WhiteLance || pawn == BlackLance ) {
            if( gameInfo.variant != VariantSuper && gameInfo.variant != VariantChu ) {
                if(gameInfo.variant == VariantSpartan) board[EP_STATUS] = EP_PAWN_MOVE; // in Spartan no e.p. rights must be set
-               else piece += WhiteLance - WhitePawn; // Lance is Pawn-like in most variants, so let Pawn code treat it by this kludge
+               else pawn += WhitePawn - WhiteLance; // Lance is Pawn-like in most variants, so let Pawn code treat it by this kludge
            }
       }
-      if( piece == WhitePawn ) {
+      if( pawn == WhitePawn ) {
            if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
               board[EP_STATUS] = EP_PAWN_MOVE;
            if( toY-fromY>=2) {
@@ -9947,7 +9962,7 @@ ApplyMove (int fromX, int fromY, int toX, int toY, int promoChar, Board board)
                      board[EP_STATUS] = toX;
           }
       } else
-      if( piece == BlackPawn ) {
+      if( pawn == BlackPawn ) {
            if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
               board[EP_STATUS] = EP_PAWN_MOVE;
            if( toY-fromY<= -2) {
@@ -10046,11 +10061,11 @@ ApplyMove (int fromX, int fromY, int toX, int toY, int promoChar, Board board)
               && (toX != fromX)
                && gameInfo.variant != VariantXiangqi
                && gameInfo.variant != VariantBerolina
-              && (board[fromY][fromX] == WhitePawn)
+              && (pawn == WhitePawn)
               && (board[toY][toX] == EmptySquare)) {
        board[fromY][fromX] = EmptySquare;
-       board[toY][toX] = WhitePawn;
-       if(toY == board[EP_RANK] - 128 + 1)
+       board[toY][toX] = piece;
+       if(toY == epRank - 128 + 1)
            captured = board[toY - 2][toX], board[toY - 2][toX] = EmptySquare;
        else
            captured = board[toY - 1][toX], board[toY - 1][toX] = EmptySquare;
@@ -10111,11 +10126,11 @@ ApplyMove (int fromX, int fromY, int toX, int toY, int promoChar, Board board)
               && (toX != fromX)
                && gameInfo.variant != VariantXiangqi
                && gameInfo.variant != VariantBerolina
-              && (board[fromY][fromX] == BlackPawn)
+              && (pawn == BlackPawn)
               && (board[toY][toX] == EmptySquare)) {
        board[fromY][fromX] = EmptySquare;
-       board[toY][toX] = BlackPawn;
-       if(toY == board[EP_RANK] - 128 - 1)
+       board[toY][toX] = piece;
+       if(toY == epRank - 128 - 1)
            captured = board[toY + 2][toX], board[toY + 2][toX] = EmptySquare;
        else
            captured = board[toY + 1][toX], board[toY + 1][toX] = EmptySquare;
@@ -15121,7 +15136,7 @@ EditPositionMenuEvent (ChessSquare selection, int x, int y)
                                     AAA + x, ONE + y);
                            SendToICS(buf);
                        }
-                   } else {
+                   } else if(boards[0][y][x] != DarkSquare) {
                        if(boards[0][y][x] != p) nonEmpty++;
                        boards[0][y][x] = p;
                    }