new mousehandler to correct for premove and promotion popup
[xboard.git] / winboard / winboard.c
index aaf6150..4ab4f93 100644 (file)
@@ -4839,8 +4839,8 @@ PaintProc(HWND hwnd)
  * The offset boardRect.left or boardRect.top must already have been\r
  *   subtracted from x.\r
  */\r
-int\r
-EventToSquare(int x)\r
+int EventToSquare(x, limit)\r
+     int x;\r
 {\r
   if (x <= 0)\r
     return -2;\r
@@ -4850,7 +4850,7 @@ EventToSquare(int x)
   if ((x % (squareSize + lineGap)) >= squareSize)\r
     return -1;\r
   x /= (squareSize + lineGap);\r
-  if (x >= BOARD_SIZE)\r
+    if (x >= limit)\r
     return -2;\r
   return x;\r
 }\r
@@ -4892,6 +4892,24 @@ SetupDropMenu(HMENU hmenu)
   }\r
 }\r
 \r
+void DragPieceBegin(int x, int y)\r
+{\r
+      dragInfo.lastpos.x = boardRect.left + x;\r
+      dragInfo.lastpos.y = boardRect.top + y;\r
+      dragInfo.from.x = fromX;\r
+      dragInfo.from.y = fromY;\r
+      dragInfo.start = dragInfo.from;\r
+      SetCapture(hwndMain);\r
+}\r
+\r
+void DragPieceEnd(int x, int y)\r
+{\r
+    ReleaseCapture();\r
+    dragInfo.start.x = dragInfo.start.y = -1;\r
+    dragInfo.from = dragInfo.start;\r
+    dragInfo.pos = dragInfo.lastpos = dragInfo.start;\r
+}\r
+\r
 /* Event handler for mouse messages */\r
 VOID\r
 MouseEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)\r
@@ -4900,11 +4918,7 @@ MouseEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
   POINT pt;\r
   static int recursive = 0;\r
   HMENU hmenu;\r
-//  BOOLEAN needsRedraw = FALSE;\r
-  BOOLEAN saveAnimate;\r
   BOOLEAN forceFullRepaint = IsFullRepaintPreferrable(); /* [AS] */\r
-  static BOOLEAN sameAgain = FALSE, promotionChoice = FALSE;\r
-  ChessMove moveType;\r
 \r
   if (recursive) {\r
     if (message == WM_MBUTTONUP) {\r
@@ -4920,8 +4934,8 @@ MouseEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
   \r
   pt.x = LOWORD(lParam);\r
   pt.y = HIWORD(lParam);\r
-  x = EventToSquare(pt.x - boardRect.left);\r
-  y = EventToSquare(pt.y - boardRect.top);\r
+  x = EventToSquare(pt.x - boardRect.left, BOARD_WIDTH);\r
+  y = EventToSquare(pt.y - boardRect.top, BOARD_HEIGHT);\r
   if (!flipView && y >= 0) {\r
     y = BOARD_HEIGHT - 1 - y;\r
   }\r
@@ -4931,29 +4945,6 @@ MouseEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 \r
   switch (message) {\r
   case WM_LBUTTONDOWN:\r
-    if(promotionChoice) { // we are waiting for a click to indicate promotion piece\r
-       promotionChoice = FALSE; // only one chance: if click not OK it is interpreted as cancel\r
-       if(appData.debugMode) fprintf(debugFP, "promotion click, x=%d, y=%d\n", x, y);\r
-       if(gameInfo.holdingsWidth && \r
-               (WhiteOnMove(currentMove) \r
-                       ? x == BOARD_WIDTH-1 && y < gameInfo.holdingsSize && y > 0\r
-                       : x == 0 && y >= BOARD_HEIGHT - gameInfo.holdingsSize && y < BOARD_HEIGHT-1) ) {\r
-           // click in right holdings, for determining promotion piece\r
-           ChessSquare p = boards[currentMove][y][x];\r
-           if(appData.debugMode) fprintf(debugFP, "square contains %d\n", (int)p);\r
-           if(p != EmptySquare) {\r
-               FinishMove(WhitePromotionQueen, fromX, fromY, toX, toY, ToLower(PieceToChar(p)));\r
-               fromX = fromY = -1;\r
-               break;\r
-           }\r
-       }\r
-       DrawPosition(FALSE, boards[currentMove]);\r
-       break;\r
-    }\r
-    ErrorPopDown();\r
-    sameAgain = FALSE;\r
-    if (y == -2) {\r
-      /* Downclick vertically off board; check if on clock */\r
       if (PtInRect((LPRECT) &whiteRect, pt)) {\r
         if (gameMode == EditPosition) {\r
          SetWhiteToPlayEvent();\r
@@ -4973,198 +4964,20 @@ MouseEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
           AdjustClock(!flipClock, -1);\r
        }\r
       }\r
-      if (!appData.highlightLastMove) {\r
-        ClearHighlights();\r
-       DrawPosition((int) (forceFullRepaint || FALSE), NULL);\r
-      }\r
-      fromX = fromY = -1;\r
       dragInfo.start.x = dragInfo.start.y = -1;\r
       dragInfo.from = dragInfo.start;\r
-      break;\r
-    } else if (x < 0 || y < 0\r
-      /* [HGM] block clicks between board and holdings */\r
-              || x == BOARD_LEFT-1 || x == BOARD_RGHT\r
-              || (x == BOARD_LEFT-2 && y < BOARD_HEIGHT-gameInfo.holdingsSize)\r
-              || (x == BOARD_RGHT+1 && y >= gameInfo.holdingsSize)\r
-       /* EditPosition, empty square, or different color piece;\r
-          click-click move is possible */\r
-                               ) {\r
-      break;\r
-    } else if (fromX == x && fromY == y) {\r
-      /* Downclick on same square again */\r
-      ClearHighlights();\r
-      DrawPosition(forceFullRepaint || FALSE, NULL);\r
-      sameAgain = TRUE;  \r
-    } else if (fromX != -1 &&\r
-               x != BOARD_LEFT-2 && x != BOARD_RGHT+1 \r
-                                                                        ) {\r
-      /* Downclick on different square. */\r
-      /* [HGM] if on holdings file, should count as new first click ! */\r
-      /* [HGM] <sameColor> now always do UserMoveTest(), and check colors there */\r
-       toX = x;\r
-       toY = y;\r
-        /* [HGM] <popupFix> UserMoveEvent requires two calls now,\r
-           to make sure move is legal before showing promotion popup */\r
-        moveType = UserMoveTest(fromX, fromY, toX, toY, NULLCHAR, FALSE);\r
-       if(moveType == AmbiguousMove) { /* [HGM] Edit-Position move executed */\r
+    if(fromX == -1 && frozen) { // not sure where this is for\r
                fromX = fromY = -1; \r
-               ClearHighlights();\r
-               DrawPosition(FALSE, boards[currentMove]);\r
-               break; \r
-       } else \r
-        if(moveType != ImpossibleMove && moveType != Comment) {\r
-          /* [HGM] We use PromotionToKnight in Shogi to indicate frorced promotion */\r
-          if (moveType == WhitePromotionKnight || moveType == BlackPromotionKnight ||\r
-            ((moveType == WhitePromotionQueen || moveType == BlackPromotionQueen) &&\r
-              appData.alwaysPromoteToQueen)) {\r
-                  FinishMove(moveType, fromX, fromY, toX, toY, 'q');\r
-                  if (!appData.highlightLastMove) {\r
-                      ClearHighlights();\r
-                      DrawPosition(forceFullRepaint || FALSE, NULL);\r
-                  }\r
-          } else\r
-          if (moveType == WhitePromotionQueen || moveType == BlackPromotionQueen ) {\r
-                  SetHighlights(fromX, fromY, toX, toY);\r
-                  DrawPosition(forceFullRepaint || FALSE, NULL);\r
-                  /* [HGM] <popupFix> Popup calls FinishMove now.\r
-                     If promotion to Q is legal, all are legal! */\r
-                 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat)\r
-                 { ChessSquare p = boards[currentMove][fromY][fromX], q = boards[currentMove][toY][toX];\r
-                   // kludge to temporarily execute move on display, without promoting yet\r
-                   promotionChoice = TRUE;\r
-                   boards[currentMove][fromY][fromX] = EmptySquare; // move Pawn to 8th rank\r
-                   boards[currentMove][toY][toX] = p;\r
-                   DrawPosition(FALSE, boards[currentMove]);\r
-                   boards[currentMove][fromY][fromX] = p; // take back, but display stays\r
-                   boards[currentMove][toY][toX] = q;\r
-                   DisplayMessage("Select piece from holdings", "");\r
-                 } else\r
-                  PromotionPopup(hwnd);\r
-                 goto noClear;\r
-          } else { // not a promotion. Move can be illegal if testLegality off, and should be made then.\r
-             if (appData.animate || appData.highlightLastMove) {\r
-                 SetHighlights(fromX, fromY, toX, toY);\r
-             } else {\r
-                 ClearHighlights();\r
-             }\r
-             FinishMove(moveType, fromX, fromY, toX, toY, NULLCHAR);\r
-             if (appData.animate && !appData.highlightLastMove) {\r
-                  ClearHighlights();\r
-                  DrawPosition(forceFullRepaint || FALSE, NULL);\r
-             }\r
-          }\r
-          fromX = fromY = -1;\r
-       noClear:\r
-         break;\r
-        }\r
-        if (gotPremove && moveType != Comment) {\r
-           SetPremoveHighlights(fromX, fromY, toX, toY);\r
-//            DrawPosition(forceFullRepaint || FALSE, NULL);\r
-       } else ClearHighlights();\r
-        fromX = fromY = -1;\r
-        DrawPosition(forceFullRepaint || FALSE, NULL);\r
-       if(moveType != Comment) break;\r
-    }\r
-    /* First downclick, or restart on a square with same color piece */\r
-    if (!frozen && OKToStartUserMove(x, y)) {\r
-      fromX = x;\r
-      fromY = y;\r
-      dragInfo.lastpos = pt;\r
-      dragInfo.from.x = fromX;\r
-      dragInfo.from.y = fromY;\r
-      dragInfo.start = dragInfo.from;\r
-      SetCapture(hwndMain);\r
-    } else {\r
-      fromX = fromY = -1;\r
-      dragInfo.start.x = dragInfo.start.y = -1;\r
-      dragInfo.from = dragInfo.start;\r
       DrawPosition(forceFullRepaint || FALSE, NULL); /* [AS] */\r
+      break;\r
     }\r
+      LeftClick(Press, pt.x - boardRect.left, pt.y - boardRect.top);\r
+      DrawPosition(TRUE, NULL);\r
     break;\r
 \r
   case WM_LBUTTONUP:\r
-    ReleaseCapture();\r
-    if (fromX == -1) break;\r
-    if (x == fromX && y == fromY) {\r
-      dragInfo.from.x = dragInfo.from.y = -1;\r
-      /* Upclick on same square */\r
-      if (sameAgain) {\r
-       /* Clicked same square twice: abort click-click move */\r
-       fromX = fromY = -1;\r
-       gotPremove = 0;\r
-       ClearPremoveHighlights();\r
-      } else {\r
-       /* First square clicked: start click-click move */\r
-       SetHighlights(fromX, fromY, -1, -1);\r
-      }\r
-      DrawPosition(forceFullRepaint || FALSE, NULL);\r
-    } else if (dragInfo.from.x < 0 || dragInfo.from.y < 0) {\r
-      /* Errant click; ignore */\r
-      break;\r
-    } else {\r
-      /* Finish drag move. */\r
-    if (appData.debugMode) {\r
-        fprintf(debugFP, "release\n");\r
-    }\r
-      dragInfo.from.x = dragInfo.from.y = -1;\r
-      toX = x;\r
-      toY = y;\r
-      saveAnimate = appData.animate; /* sorry, Hawk :) */\r
-      appData.animate = appData.animate && !appData.animateDragging;\r
-      moveType = UserMoveTest(fromX, fromY, toX, toY, NULLCHAR, TRUE);\r
-      if(moveType == AmbiguousMove) { /* [HGM] Edit-Position move executed */\r
-               fromX = fromY = -1; \r
-               ClearHighlights();\r
-               DrawPosition(FALSE, boards[currentMove]);\r
-               appData.animate = saveAnimate;\r
-               break; \r
-      } else \r
-      if(moveType != ImpossibleMove) {\r
-          /* [HGM] use move type to determine if move is promotion.\r
-             Knight is Shogi kludge for mandatory promotion, Queen means choice */\r
-          if (moveType == WhitePromotionKnight || moveType == BlackPromotionKnight ||\r
-            ((moveType == WhitePromotionQueen || moveType == BlackPromotionQueen) &&\r
-              appData.alwaysPromoteToQueen)) \r
-               FinishMove(moveType, fromX, fromY, toX, toY, 'q');\r
-          else \r
-          if (moveType == WhitePromotionQueen || moveType == BlackPromotionQueen ) {\r
-               DrawPosition(forceFullRepaint || FALSE, NULL);\r
-                 if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat)\r
-                 { ChessSquare p = boards[currentMove][fromY][fromX], q = boards[currentMove][toY][toX];\r
-                   // kludge to temporarily execute move on display, wthout promotng yet\r
-                   promotionChoice = TRUE;\r
-                   boards[currentMove][fromY][fromX] = EmptySquare; // move Pawn to 8th rank\r
-                   boards[currentMove][toY][toX] = p;\r
-                   DrawPosition(FALSE, boards[currentMove]);\r
-                   boards[currentMove][fromY][fromX] = p; // take back, but display stays\r
-                   boards[currentMove][toY][toX] = q;\r
-                   appData.animate = saveAnimate;\r
-                   DisplayMessage("Select piece from holdings", "");\r
-                   break;\r
-                 } else\r
-               PromotionPopup(hwnd); /* [HGM] Popup now calls FinishMove */\r
-          } else {\r
-           if(saveAnimate /* ^$!%@#$!$ */  && gameInfo.variant == VariantAtomic \r
-                         && (boards[currentMove][toY][toX] != EmptySquare || \r
-                                       moveType == WhiteCapturesEnPassant || \r
-                                       moveType == BlackCapturesEnPassant   ) )\r
-               AnimateAtomicCapture(fromX, fromY, toX, toY, 20);\r
-           FinishMove(moveType, fromX, fromY, toX, toY, NULLCHAR);\r
-         }\r
-      }\r
-      if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);\r
-      appData.animate = saveAnimate;\r
-      fromX = fromY = -1;\r
-      if (appData.highlightDragging && !appData.highlightLastMove) {\r
-       ClearHighlights();\r
-      }\r
-      if (appData.animate || appData.animateDragging ||\r
-         appData.highlightDragging || gotPremove) {\r
-       DrawPosition(forceFullRepaint || FALSE, NULL);\r
-      }\r
-    }\r
-    dragInfo.start.x = dragInfo.start.y = -1; \r
-    dragInfo.pos = dragInfo.lastpos = dragInfo.start;\r
+      LeftClick(Release, pt.x - boardRect.left, pt.y - boardRect.top);\r
+      DrawPosition(TRUE, NULL);\r
     break;\r
 \r
   case WM_MOUSEMOVE:\r
@@ -5174,7 +4987,6 @@ MouseEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
     {\r
       BOOL full_repaint = FALSE;\r
 \r
-      sameAgain = FALSE; /* [HGM] if we drag something around, do keep square selected */\r
       if (appData.animateDragging) {\r
        dragInfo.pos = pt;\r
       }\r
@@ -5399,7 +5211,8 @@ Promotion(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
        only show the popup when we are already sure the move is valid or\r
        legal. We pass a faulty move type, but the kludge is that FinishMove\r
        will figure out it is a promotion from the promoChar. */\r
-    FinishMove(NormalMove, fromX, fromY, toX, toY, promoChar);\r
+    UserMoveEvent(fromX, fromY, toX, toY, promoChar);\r
+    fromX = fromY = -1;\r
     if (!appData.highlightLastMove) {\r
       ClearHighlights();\r
       DrawPosition(FALSE, NULL);\r
@@ -5421,6 +5234,13 @@ PromotionPopup(HWND hwnd)
   FreeProcInstance(lpProc);\r
 }\r
 \r
+void\r
+PromotionPopUp()\r
+{\r
+  DrawPosition(TRUE, NULL);\r
+  PromotionPopup(hwndMain);\r
+}\r
+\r
 /* Toggle ShowThinking */\r
 VOID\r
 ToggleShowThinking()\r