Fix multi-leg promotions
[xboard.git] / winboard / wchat.c
index ac75819..3be1e7f 100644 (file)
@@ -3,7 +3,8 @@
  *\r
  * Author: H.G.Muller (August 2009)\r
  *\r
- * Copyright 2009, 2010 Free Software Foundation, Inc. \r
+ * Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free\r
+ * Software Foundation, Inc.\r
  *\r
  * ------------------------------------------------------------------------\r
  *\r
@@ -61,7 +62,8 @@ char *NextInHistory();
 extern HWND ChatDialog;\r
 \r
 extern HINSTANCE hInst;\r
-extern HWND hwndMain;\r
+extern HWND hwndConsole;\r
+extern char ics_handle[];\r
 \r
 extern WindowPlacement wpChat[MAX_CHAT];\r
 extern WindowPlacement wpConsole;\r
@@ -180,7 +182,7 @@ LRESULT CALLBACK ChatProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam
 {\r
     static SnapData sd;\r
     char buf[MSG_SIZ], mess[MSG_SIZ];\r
-    int partner = -1, i;\r
+    int partner = -1, i, x, y;\r
     static BOOL filterHasFocus[MAX_CHAT];\r
     WORD wMask;\r
     HWND hMemo;\r
@@ -189,24 +191,28 @@ LRESULT CALLBACK ChatProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam
 \r
     switch (message) {\r
     case WM_INITDIALOG:\r
+        Translate(hDlg, DLG_Chat);\r
        if(partner<0) {\r
                for(i=0; i<MAX_CHAT; i++) if(chatHandle[i] == NULL) { partner = i; break; }\r
                chatHandle[partner] = hDlg;\r
-               sprintf(buf, "Chat Window %s", first.tidy);\r
+               snprintf(buf, MSG_SIZ, T_("Chat Window %s"), ics_handle[0] ? ics_handle : first.tidy);\r
                SetWindowText(hDlg, buf);\r
         }\r
-       for(i=0; i<MAX_CHAT; i++) if(chatHandle[i] && i != partner) {\r
+       for(i=0; i<MAX_CHAT; i++) if(chatHandle[i]) {\r
+           if(i == partner) continue;\r
            // set our button in other open chats\r
            SetDlgItemText(chatHandle[i], IDC_Focus1+partner-(i<partner), chatPartner[partner]);\r
            EnableWindow( GetDlgItem(chatHandle[i], IDC_Focus1+partner-(i<partner)), 1 );\r
            // and buttons for other chats in ours\r
            SetDlgItemText(hDlg, IDC_Focus1+i-(i>partner), chatPartner[i]);\r
-       } else EnableWindow( GetDlgItem(hDlg, IDC_Focus1+i-(i>partner)), 1 );\r
+       } else EnableWindow( GetDlgItem(hDlg, IDC_Focus1+i-(i>partner)), 0 );\r
        for(i=0; i<MAX_CHAT-1; i++) { Button_SetStyle(GetDlgItem(hDlg, IDC_Focus1+i), BS_PUSHBUTTON|BS_LEFT, TRUE); }\r
-        SetWindowPos(hDlg, NULL, wpConsole.x, wpConsole.y, 0, 0, SWP_NOZORDER|SWP_NOSIZE);\r
+        x = wpConsole.x; y = wpConsole.y; EnsureOnScreen(&x, &y, 0, 0);\r
+        SetWindowPos(hDlg, NULL, x, y, 0, 0, SWP_NOZORDER|SWP_NOSIZE);\r
        SendMessage( GetDlgItem(hDlg, IDC_ChatPartner), // [HGM] clickbox: initialize with requested handle\r
                        WM_SETTEXT, 0, (LPARAM) chatPartner[partner] );\r
        filterHasFocus[partner] = TRUE;\r
+       onTop = partner; // a newly opened box becomes top one\r
        if(chatPartner[partner][0]) {\r
            filterHasFocus[partner] = FALSE;\r
            SetFocus( GetDlgItem(hDlg, OPT_ChatInput) );\r
@@ -216,7 +222,7 @@ LRESULT CALLBACK ChatProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam
        SendMessage(hMemo, EM_SETEVENTMASK, 0, wMask | ENM_LINK);\r
        SendMessage(hMemo, EM_AUTOURLDETECT, TRUE, 0L);\r
        chatInputWindowProc = (WNDPROC) // cloned from ConsoleWndProc(). Assume they all share same proc.\r
-             SetWindowLong(GetDlgItem(hDlg, OPT_ChatInput), GWL_WNDPROC, (LONG) InterceptArrowKeys);\r
+             SetWindowLongPtr(GetDlgItem(hDlg, OPT_ChatInput), GWLP_WNDPROC, (LONG_PTR) InterceptArrowKeys);\r
         return FALSE;\r
 \r
     case WM_NOTIFY:\r
@@ -237,7 +243,7 @@ LRESULT CALLBACK ChatProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam
     break;\r
 \r
     case WM_COMMAND:\r
-      /* \r
+      /*\r
         [AS]\r
         If <Enter> is pressed while editing the filter, it's better to apply\r
         the filter rather than selecting the current game.\r
@@ -261,12 +267,9 @@ LRESULT CALLBACK ChatProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam
 \r
         switch (LOWORD(wParam)) {\r
 \r
-       case IDCANCEL:\r
-           chatHandle[partner] = 0;\r
-           chatPartner[partner][0] = 0;\r
-            ChatPopDown();\r
-           EndDialog(hDlg, TRUE);\r
-            break;\r
+       case IDCANCEL: /* let Esc key switch focus back to console */\r
+           SetFocus(GetDlgItem(hwndConsole, OPT_ConsoleInput));\r
+           break;\r
 \r
        case IDC_Clear:\r
            SendMessage( GetDlgItem(hDlg, IDC_ChatMemo), WM_SETTEXT, 0, (LPARAM) "" );\r
@@ -286,16 +289,16 @@ LRESULT CALLBACK ChatProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam
            // from here on it could be back-end\r
            SaveInHistory(mess);\r
            if(!strcmp("whispers", chatPartner[partner]))\r
-               sprintf(buf, "whisper %s\n", mess); // WHISPER box uses "whisper" to send\r
+             snprintf(buf, MSG_SIZ, "whisper %s\n", mess); // WHISPER box uses "whisper" to send\r
            else if(!strcmp("shouts", chatPartner[partner]))\r
-               sprintf(buf, "shout %s\n", mess); // SHOUT box uses "shout" to send\r
+             snprintf(buf, MSG_SIZ, "shout %s\n", mess); // SHOUT box uses "shout" to send\r
            else {\r
                if(!atoi(chatPartner[partner])) {\r
-                   sprintf(buf, "> %s\r\n", mess); // echo only tells to handle, not channel\r
+                 snprintf(buf, MSG_SIZ, "> %s\r\n", mess); // echo only tells to handle, not channel\r
                InsertIntoMemo(hDlg, buf);\r
-               sprintf(buf, "xtell %s %s\n", chatPartner[partner], mess);\r
+               snprintf(buf, MSG_SIZ, "xtell %s %s\n", chatPartner[partner], mess);\r
                } else\r
-               sprintf(buf, "tell %s %s\n", chatPartner[partner], mess);\r
+                 snprintf(buf, MSG_SIZ, "tell %s %s\n", chatPartner[partner], mess);\r
            }\r
            SendToICS(buf);\r
            break;\r
@@ -359,19 +362,27 @@ void ChatPopUp(char *icsHandle)
 {\r
   FARPROC lpProc;\r
   int i, partner = -1;\r
-  \r
+  char buf[MSG_SIZ];\r
+  static int first = 1;\r
+\r
   CheckMenuItem(GetMenu(hwndMain), IDM_NewChat, MF_CHECKED);\r
   for(i=0; i<MAX_CHAT; i++) if(chatHandle[i] == NULL) { partner = i; break; }\r
   if(partner == -1) { DisplayError("You first have to close a Chat Box\nbefore you can open a new one", 0); return; }\r
-  if(icsHandle) // [HGM] clickbox set handle in advance\r
-       strcpy(chatPartner[partner], icsHandle);\r
-  else chatPartner[partner][0] = NULLCHAR;\r
+  if(icsHandle) { // [HGM] clickbox set handle in advance\r
+    safeStrCpy(chatPartner[partner], icsHandle,\r
+              sizeof(chatPartner[partner])/sizeof(chatPartner[partner][0]) );\r
+    if(sscanf(icsHandle, "%d", &i) == 1) { // make sure channel is on\r
+       snprintf(buf, MSG_SIZ, "addlist ch %d\n", i);\r
+       SendToICS(buf);\r
+       if(first) first=0, SendToICS(buf); // work-around for weirdness: On public FICS code first attempt on login is completely ignored\r
+    }\r
+  } else chatPartner[partner][0] = NULLCHAR;\r
   chatCount++;\r
 \r
     lpProc = MakeProcInstance( (FARPROC) ChatProc, hInst );\r
 \r
     /* Note to self: dialog must have the WS_VISIBLE style set, otherwise it's not shown! */\r
-    CreateDialog( hInst, MAKEINTRESOURCE(DLG_Chat), hwndMain, (DLGPROC)lpProc );\r
+    CreateDialog( hInst, MAKEINTRESOURCE(DLG_Chat), hwndConsole, (DLGPROC)lpProc );\r
 \r
     FreeProcInstance(lpProc);\r
 \r
@@ -389,10 +400,9 @@ void ChatPopDown()
 \r
 void OutputChatMessage(int partner, char *text)\r
 {\r
-       int j;\r
-       if(!chatHandle[partner]) return;\r
+       int j, n = strlen(text);\r
 \r
-       int n = strlen(text);\r
+       if(!chatHandle[partner]) return;\r
        text[n+1] = 0; text[n] = '\n'; text[n-1] = '\r'; // Needs CR to not lose line breaks on copy-paste\r
        InsertIntoMemo(chatHandle[partner], text);\r
        if(partner != onTop) for(j=0; j<MAX_CHAT; j++) if(j != partner && chatHandle[j])\r