changes from H.G. Muller; version 4.3.16
[xboard.git] / winboard / winboard.c
index a70f16b..6ad5def 100644 (file)
@@ -140,7 +140,8 @@ BOOLEAN chessProgram;
 static int boardX, boardY, consoleX, consoleY, consoleW, consoleH;\r
 static int squareSize, lineGap, minorSize;\r
 static int winWidth, winHeight;\r
-static RECT messageRect, whiteRect, blackRect;\r
+static RECT messageRect, whiteRect, blackRect, leftLogoRect, rightLogoRect; // [HGM] logo\r
+static int logoHeight = 0;\r
 static char messageText[MESSAGE_TEXT_MAX];\r
 static int clockTimerEvent = 0;\r
 static int loadGameTimerEvent = 0;\r
@@ -613,6 +614,35 @@ InitInstance(HINSTANCE hInstance, int nCmdShow, LPSTR lpCmdLine)
     return (FALSE);\r
   }\r
 \r
+  /* [HGM] logo: Load logos if specified (must be done before InitDrawingSizes) */\r
+  if( appData.firstLogo && appData.firstLogo[0] != NULLCHAR) {\r
+      first.programLogo = LoadImage( 0, appData.firstLogo, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );\r
+\r
+      if (first.programLogo == NULL && appData.debugMode) {\r
+          fprintf( debugFP, "Unable to load logo bitmap '%s'\n", appData.firstLogo );\r
+      }\r
+  } else if(appData.autoLogo) {\r
+      if(appData.firstDirectory && appData.firstDirectory[0]) {\r
+       char buf[MSG_SIZ];\r
+       sprintf(buf, "%s/logo.bmp", appData.firstDirectory);\r
+       first.programLogo = LoadImage( 0, buf, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );   \r
+      }\r
+  }\r
+\r
+  if( appData.secondLogo && appData.secondLogo[0] != NULLCHAR) {\r
+      second.programLogo = LoadImage( 0, appData.secondLogo, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );\r
+\r
+      if (second.programLogo == NULL && appData.debugMode) {\r
+          fprintf( debugFP, "Unable to load logo bitmap '%s'\n", appData.secondLogo );\r
+      }\r
+  } else if(appData.autoLogo) {\r
+      if(appData.secondDirectory && appData.secondDirectory[0]) {\r
+       char buf[MSG_SIZ];\r
+       sprintf(buf, "%s\\logo.bmp", appData.secondDirectory);\r
+       second.programLogo = LoadImage( 0, buf, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );  \r
+      }\r
+  }\r
+\r
   iconWhite = LoadIcon(hInstance, "icon_white");\r
   iconBlack = LoadIcon(hInstance, "icon_black");\r
   iconCurrent = iconWhite;\r
@@ -630,6 +660,7 @@ InitInstance(HINSTANCE hInstance, int nCmdShow, LPSTR lpCmdLine)
       boardSize = (BoardSize)ibs;\r
     }\r
   }\r
+\r
   InitDrawingSizes(boardSize, 0);\r
   InitMenuChecks();\r
   buttonCount = GetSystemMetrics(SM_CMOUSEBUTTONS);\r
@@ -1188,6 +1219,10 @@ ArgDescriptor argDescriptors[] = {
   { "sameColorGames", ArgInt, (LPVOID) &appData.sameColorGames, FALSE },\r
   { "smpCores", ArgInt, (LPVOID) &appData.smpCores, TRUE },\r
   { "egtFormats", ArgString, (LPVOID) &appData.egtFormats, TRUE },\r
+  { "niceEngines", ArgInt, (LPVOID) &appData.niceEngines, TRUE },\r
+  { "firstLogo", ArgFilename, (LPVOID) &appData.firstLogo, FALSE },\r
+  { "secondLogo", ArgFilename, (LPVOID) &appData.secondLogo, FALSE },\r
+  { "autoLogo", ArgBoolean, (LPVOID) &appData.autoLogo, TRUE },\r
 \r
 #ifdef ZIPPY\r
   { "zippyTalk", ArgBoolean, (LPVOID) &appData.zippyTalk, FALSE },\r
@@ -2530,7 +2565,7 @@ static void CreatePieceMaskFromFont( HDC hdc_window, HDC hdc, int index )
     SetBkMode( hdc, TRANSPARENT );\r
     SetTextColor( hdc, chroma );\r
     /* Step 2: the piece has been drawn in purple, there are now black and purple in this bitmap */\r
-    TextOut( hdc, pt.x, pt.y, &pieceToFontChar[index], 1 );\r
+    TextOut( hdc, pt.x, pt.y, &pieceToFontChar[appData.allWhite && index >= (int)BlackPawn ? index - (int)BlackPawn : index], 1 );\r
 \r
     SelectObject( hdc, GetStockObject(WHITE_BRUSH) );\r
     /* Step 3: the area outside the piece is filled with white */\r
@@ -2569,7 +2604,7 @@ static void CreatePieceMaskFromFont( HDC hdc_window, HDC hdc, int index )
         Step 5: some fonts have "disconnected" areas that are skipped by the fill:\r
         draw the piece again in black for safety.\r
     */\r
-    TextOut( hdc, pt.x, pt.y, &pieceToFontChar[index], 1 );\r
+    TextOut( hdc, pt.x, pt.y, &pieceToFontChar[appData.allWhite && index >= (int)BlackPawn ? index - (int)BlackPawn : index], 1 );\r
 \r
     SelectObject( hdc, hbm_old );\r
 \r
@@ -2610,7 +2645,7 @@ static void CreatePieceMaskFromFont( HDC hdc_window, HDC hdc, int index )
     }\r
 \r
     SetTextColor( hdc, foreColor );\r
-    TextOut( hdc, pt.x, pt.y, &pieceToFontChar[index], 1 );\r
+    TextOut( hdc, pt.x, pt.y, &pieceToFontChar[appData.allWhite && index >= (int)BlackPawn ? index - (int)BlackPawn : index], 1 );\r
 \r
     SelectObject( hdc, hbm_old );\r
 \r
@@ -3059,27 +3094,52 @@ InitDrawingSizes(BoardSize boardSize, int flags)
   ReleaseDC(hwndMain, hdc);\r
 \r
   /* Compute where everything goes */\r
-  whiteRect.left = OUTER_MARGIN;\r
-  whiteRect.right = whiteRect.left + boardWidth/2 - INNER_MARGIN/2;\r
-  whiteRect.top = OUTER_MARGIN;\r
-  whiteRect.bottom = whiteRect.top + clockSize.cy;\r
+  if(first.programLogo || second.programLogo) {\r
+        /* [HGM] logo: if either logo is on, reserve space for it */\r
+       logoHeight =  2*clockSize.cy;\r
+       leftLogoRect.left   = OUTER_MARGIN;\r
+       leftLogoRect.right  = leftLogoRect.left + 4*clockSize.cy;\r
+       leftLogoRect.top    = OUTER_MARGIN;\r
+       leftLogoRect.bottom = OUTER_MARGIN + logoHeight;\r
+\r
+       rightLogoRect.right  = OUTER_MARGIN + boardWidth;\r
+       rightLogoRect.left   = rightLogoRect.right - 4*clockSize.cy;\r
+       rightLogoRect.top    = OUTER_MARGIN;\r
+       rightLogoRect.bottom = OUTER_MARGIN + logoHeight;\r
+\r
+\r
+    blackRect.left = leftLogoRect.right;\r
+    blackRect.right = rightLogoRect.left;\r
+    blackRect.top = OUTER_MARGIN;\r
+    blackRect.bottom = blackRect.top + clockSize.cy;\r
+\r
+    whiteRect.left = blackRect.left ;\r
+    whiteRect.right = blackRect.right;\r
+    whiteRect.top = blackRect.bottom;\r
+    whiteRect.bottom = leftLogoRect.bottom;\r
+  } else {\r
+    whiteRect.left = OUTER_MARGIN;\r
+    whiteRect.right = whiteRect.left + boardWidth/2 - INNER_MARGIN/2;\r
+    whiteRect.top = OUTER_MARGIN + logoHeight;\r
+    whiteRect.bottom = whiteRect.top + clockSize.cy;\r
 \r
-  blackRect.left = whiteRect.right + INNER_MARGIN;\r
-  blackRect.right = blackRect.left + boardWidth/2 - 1;\r
-  blackRect.top = whiteRect.top;\r
-  blackRect.bottom = whiteRect.bottom;\r
+    blackRect.left = whiteRect.right + INNER_MARGIN;\r
+    blackRect.right = blackRect.left + boardWidth/2 - 1;\r
+    blackRect.top = whiteRect.top;\r
+    blackRect.bottom = whiteRect.bottom;\r
+  }\r
 \r
-  messageRect.left = whiteRect.left + MESSAGE_LINE_LEFTMARGIN;\r
+  messageRect.left = OUTER_MARGIN + MESSAGE_LINE_LEFTMARGIN;\r
   if (appData.showButtonBar) {\r
-    messageRect.right = blackRect.right\r
+    messageRect.right = OUTER_MARGIN + boardWidth         // [HGM] logo: expressed independent of clock placement\r
       - N_BUTTONS*BUTTON_WIDTH - MESSAGE_LINE_LEFTMARGIN;\r
   } else {\r
-    messageRect.right = blackRect.right;\r
+    messageRect.right = OUTER_MARGIN + boardWidth;\r
   }\r
   messageRect.top = whiteRect.bottom + INNER_MARGIN;\r
   messageRect.bottom = messageRect.top + messageSize.cy;\r
 \r
-  boardRect.left = whiteRect.left;\r
+  boardRect.left = OUTER_MARGIN;\r
   boardRect.right = boardRect.left + boardWidth;\r
   boardRect.top = messageRect.bottom + INNER_MARGIN;\r
   boardRect.bottom = boardRect.top + boardHeight;\r
@@ -3565,7 +3625,7 @@ DrawPieceOnDC(HDC hdc, ChessSquare piece, int color, int sqcolor, int x, int y,
     CreatePiecesFromFont();\r
 \r
     if( fontBitmapSquareSize == squareSize ) {\r
-        int index = TranslatePieceToFontPiece( piece );\r
+        int index = TranslatePieceToFontPiece(piece);\r
 \r
         SelectObject( tmphdc, hPieceMask[ index ] );\r
 \r
@@ -3710,16 +3770,16 @@ VOID RebuildTextureSquareInfo()
             if( (col + row) & 1 ) {\r
                 /* Lite square */\r
                 if( lite_w >= squareSize && lite_h >= squareSize ) {\r
-                    backTextureSquareInfo[row][col].x = col * (lite_w - squareSize) / BOARD_WIDTH;\r
-                    backTextureSquareInfo[row][col].y = row * (lite_h - squareSize) / BOARD_HEIGHT;\r
+                    backTextureSquareInfo[row][col].x = col * (lite_w - squareSize) / (BOARD_WIDTH-1);  /* [HGM] divide by size-1 in stead of size! */\r
+                    backTextureSquareInfo[row][col].y = (BOARD_HEIGHT-1-row) * (lite_h - squareSize) / (BOARD_HEIGHT-1);\r
                     backTextureSquareInfo[row][col].mode = GetBackTextureMode(liteBackTextureMode);\r
                 }\r
             }\r
             else {\r
                 /* Dark square */\r
                 if( dark_w >= squareSize && dark_h >= squareSize ) {\r
-                    backTextureSquareInfo[row][col].x = col * (dark_w - squareSize) / BOARD_WIDTH;\r
-                    backTextureSquareInfo[row][col].y = row * (dark_h - squareSize) / BOARD_HEIGHT;\r
+                    backTextureSquareInfo[row][col].x = col * (dark_w - squareSize) / (BOARD_WIDTH-1);\r
+                    backTextureSquareInfo[row][col].y = (BOARD_HEIGHT-1-row) * (dark_h - squareSize) / (BOARD_HEIGHT-1);\r
                     backTextureSquareInfo[row][col].mode = GetBackTextureMode(darkBackTextureMode);\r
                 }\r
             }\r
@@ -4045,7 +4105,7 @@ DrawBoardOnDC(HDC hdc, Board board, HDC tmphdc)
       piece = board[row][column];\r
 \r
       square_color = ((column + row) % 2) == 1;\r
-      if(!strcmp(appData.variant, "xiangqi") ) {\r
+      if( gameInfo.variant == VariantXiangqi ) {\r
           square_color = !InPalace(row, column);\r
           if(BOARD_HEIGHT&1) { if(row==BOARD_HEIGHT/2) square_color ^= 1; }\r
           else if(row < BOARD_HEIGHT/2) square_color ^= 1;\r
@@ -4086,14 +4146,16 @@ DrawBoardOnDC(HDC hdc, Board board, HDC tmphdc)
       else if( backTextureSquareInfo[row][column].mode > 0 ) {\r
           /* [AS] Draw the square using a texture bitmap */\r
           HBITMAP hbm = SelectObject( texture_hdc, square_color ? liteBackTexture : darkBackTexture );\r
+         int r = row, c = column; // [HGM] do not flip board in flipView\r
+         if(flipView) { r = BOARD_HEIGHT-1 - r; c = BOARD_WIDTH-1 - c; }\r
 \r
           DrawTile( x, y, \r
               squareSize, squareSize, \r
               hdc, \r
               texture_hdc,\r
-              backTextureSquareInfo[row][column].mode,\r
-              backTextureSquareInfo[row][column].x,\r
-              backTextureSquareInfo[row][column].y );\r
+              backTextureSquareInfo[r][c].mode,\r
+              backTextureSquareInfo[r][c].x,\r
+              backTextureSquareInfo[r][c].y );\r
 \r
           SelectObject( texture_hdc, hbm );\r
 \r
@@ -4130,6 +4192,32 @@ void fputDW(FILE *f, int x)
 #define MAX_CLIPS 200   /* more than enough */\r
 \r
 VOID\r
+DrawLogoOnDC(HDC hdc, RECT logoRect, ChessProgramState *cps)\r
+{\r
+  HBITMAP bufferBitmap;\r
+  BITMAP bi;\r
+  RECT Rect;\r
+  HDC tmphdc;\r
+  HBITMAP hbm;\r
+  int w = 100, h = 50;\r
+\r
+  if(cps->programLogo == NULL) return;\r
+//  GetClientRect(hwndMain, &Rect);\r
+//  bufferBitmap = CreateCompatibleBitmap(hdc, Rect.right-Rect.left+1,\r
+//                                     Rect.bottom-Rect.top+1);\r
+  tmphdc = CreateCompatibleDC(hdc);\r
+  hbm = SelectObject(tmphdc, (HBITMAP) cps->programLogo);\r
+  if( GetObject( cps->programLogo, sizeof(bi), &bi ) > 0 ) {\r
+            w = bi.bmWidth;\r
+            h = bi.bmHeight;\r
+  }\r
+  StretchBlt(hdc, logoRect.left, logoRect.top, logoRect.right - logoRect.left, \r
+                  logoRect.bottom - logoRect.top, tmphdc, 0, 0, w, h, SRCCOPY);\r
+  SelectObject(tmphdc, hbm);\r
+  DeleteDC(tmphdc);\r
+}\r
+\r
+VOID\r
 HDCDrawPosition(HDC hdc, BOOLEAN repaint, Board board)\r
 {\r
   static Board lastReq, lastDrawn;\r
@@ -4383,6 +4471,11 @@ HDCDrawPosition(HDC hdc, BOOLEAN repaint, Board board)
   DrawHighlightsOnDC(hdcmem);\r
   DrawBoardOnDC(hdcmem, board, tmphdc);\r
 \r
+  if(logoHeight) {\r
+       DrawLogoOnDC(hdc, leftLogoRect, flipClock ? &second : &first);\r
+       DrawLogoOnDC(hdc, rightLogoRect, flipClock ? &first : &second);\r
+  }\r
+\r
   if( appData.highlightMoveWithArrow ) {\r
     DrawArrowHighlight(hdcmem);\r
   }\r
@@ -4727,7 +4820,7 @@ MouseEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
                   gameMode == MachinePlaysWhite) {\r
          CallFlagEvent();\r
         } else if (gameMode == EditGame) {\r
-          AdjustClock(flipClock, -1);\r
+          AdjustClock((logoHeight > 0 ? flipView: flipClock), -1);\r
         }\r
       } else if (PtInRect((LPRECT) &blackRect, pt)) {\r
        if (gameMode == EditPosition) {\r
@@ -4736,7 +4829,7 @@ MouseEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
                   gameMode == MachinePlaysBlack) {\r
          CallFlagEvent();\r
         } else if (gameMode == EditGame) {\r
-          AdjustClock(!flipClock, -1);\r
+          AdjustClock(!(logoHeight > 0 ? flipView: flipClock), -1);\r
        }\r
       }\r
       if (!appData.highlightLastMove) {\r
@@ -4794,7 +4887,7 @@ MouseEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
                   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)\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
@@ -4893,7 +4986,7 @@ MouseEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
           else \r
           if (moveType == WhitePromotionQueen || moveType == BlackPromotionQueen ) {\r
                DrawPosition(forceFullRepaint || FALSE, NULL);\r
-                 if(gameInfo.variant == VariantSuper)\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
@@ -4961,9 +5054,9 @@ MouseEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
     if(y == -2) {\r
       /* [HGM] right mouse button in clock area edit-game mode ups clock */\r
       if (PtInRect((LPRECT) &whiteRect, pt)) {\r
-          if (gameMode == EditGame) AdjustClock(flipClock, 1);\r
+          if (gameMode == EditGame) AdjustClock((logoHeight > 0 ? flipView: flipClock), 1);\r
       } else if (PtInRect((LPRECT) &blackRect, pt)) {\r
-          if (gameMode == EditGame) AdjustClock(!flipClock, 1);\r
+          if (gameMode == EditGame) AdjustClock(!(logoHeight > 0 ? flipView: flipClock), 1);\r
       }\r
     }\r
     DrawPosition(TRUE, NULL);\r
@@ -5192,7 +5285,7 @@ VOID
 ToggleShowThinking()\r
 {\r
   appData.showThinking = !appData.showThinking;\r
-  ShowThinkingEvent();
+  ShowThinkingEvent();\r
 }\r
 \r
 VOID\r
@@ -5506,7 +5599,7 @@ WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
         }\r
         else {\r
             EngineOutputPopUp();\r
-        }
+        }\r
         break;\r
 \r
     /* [AS] User adjudication */\r
@@ -9293,7 +9386,8 @@ DisplayWhiteClock(long timeRemaining, int highlight)
   char *flag = whiteFlag && gameMode == TwoMachinesPlay ? "(!)" : "";\r
 \r
   if (!IsIconic(hwndMain)) {\r
-    DisplayAClock(hdc, timeRemaining, highlight, flipClock ? &blackRect : &whiteRect, "White", flag);\r
+    DisplayAClock(hdc, timeRemaining, highlight, \r
+                       (logoHeight > 0 ? flipView: flipClock) ? &blackRect : &whiteRect, "White", flag);\r
   }\r
   if (highlight && iconCurrent == iconBlack) {\r
     iconCurrent = iconWhite;\r
@@ -9315,7 +9409,8 @@ DisplayBlackClock(long timeRemaining, int highlight)
 \r
   hdc = GetDC(hwndMain);\r
   if (!IsIconic(hwndMain)) {\r
-    DisplayAClock(hdc, timeRemaining, highlight, flipClock ? &whiteRect : &blackRect, "Black", flag);\r
+    DisplayAClock(hdc, timeRemaining, highlight, \r
+                       (logoHeight > 0 ? flipView: flipClock) ? &whiteRect : &blackRect, "Black", flag);\r
   }\r
   if (highlight && iconCurrent == iconWhite) {\r
     iconCurrent = iconBlack;\r
@@ -9406,6 +9501,23 @@ CancelDelayedEvent()
   }\r
 }\r
 \r
+DWORD GetWin32Priority(int nice)\r
+{ // [HGM] nice: translate Unix nice() value to indows priority class. (Code stolen from Polyglot 1.4w11)\r
+/*\r
+REALTIME_PRIORITY_CLASS     0x00000100\r
+HIGH_PRIORITY_CLASS         0x00000080\r
+ABOVE_NORMAL_PRIORITY_CLASS 0x00008000\r
+NORMAL_PRIORITY_CLASS       0x00000020\r
+BELOW_NORMAL_PRIORITY_CLASS 0x00004000\r
+IDLE_PRIORITY_CLASS         0x00000040\r
+*/\r
+        if (nice < -15) return 0x00000080;\r
+        if (nice < 0)   return 0x00008000;\r
+        if (nice == 0)  return 0x00000020;\r
+        if (nice < 15)  return 0x00004000;\r
+        return 0x00000040;\r
+}\r
+\r
 /* Start a child process running the given program.\r
    The process's standard output can be read from "from", and its\r
    standard input can be written to "to".\r
@@ -9521,6 +9633,11 @@ StartChildProcess(char *cmdLine, char *dir, ProcRef *pr)
     return err;\r
   }\r
 \r
+  if (appData.niceEngines){ // [HGM] nice: adjust engine proc priority\r
+    if(appData.debugMode) fprintf(debugFP, "nice engine proc to %d\n", appData.niceEngines);\r
+    SetPriorityClass(piProcInfo.hProcess, GetWin32Priority(appData.niceEngines));\r
+  }\r
+\r
   /* Close the handles we don't need in the parent */\r
   CloseHandle(piProcInfo.hThread);\r
   CloseHandle(hChildStdinRd);\r