4 * Author: H.G.Muller (August 2009)
\r
6 * Copyright 2009, 2010 Free Software Foundation, Inc.
\r
8 * ------------------------------------------------------------------------
\r
10 * GNU XBoard is free software: you can redistribute it and/or modify
\r
11 * it under the terms of the GNU General Public License as published by
\r
12 * the Free Software Foundation, either version 3 of the License, or (at
\r
13 * your option) any later version.
\r
15 * GNU XBoard is distributed in the hope that it will be useful, but
\r
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
\r
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
18 * General Public License for more details.
\r
20 * You should have received a copy of the GNU General Public License
\r
21 * along with this program. If not, see http://www.gnu.org/licenses/. *
\r
23 *------------------------------------------------------------------------
\r
24 ** See the file ChangeLog for a revision history. */
\r
28 #include <windows.h> /* required for all Windows applications */
\r
29 #include <richedit.h>
\r
33 #include <commdlg.h>
\r
35 #include <Windowsx.h>
\r
38 #include "frontend.h"
\r
39 #include "winboard.h"
\r
40 #include "backend.h"
\r
46 extern char chatPartner[MAX_CHAT][MSG_SIZ];
\r
47 HANDLE chatHandle[MAX_CHAT];
\r
48 static WNDPROC chatInputWindowProc;
\r
50 void SendToICS P((char *s));
\r
51 void ChatPopUp P((char *s));
\r
54 /* Imports from backend.c */
\r
55 extern int opponentKibitzes;
\r
57 /* Imports from winboard.c */
\r
58 VOID SaveInHistory(char *cmd);
\r
59 char *PrevInHistory(char *cmd);
\r
60 char *NextInHistory();
\r
61 extern HWND ChatDialog;
\r
63 extern HINSTANCE hInst;
\r
64 extern HWND hwndConsole;
\r
66 extern WindowPlacement wpChat[MAX_CHAT];
\r
67 extern WindowPlacement wpConsole;
\r
69 extern BoardSize boardSize;
\r
71 /* Module variables */
\r
75 // front end, although we might make GetWindowRect front end instead
\r
76 static int GetControlWidth( HWND hDlg, int id )
\r
80 GetWindowRect( GetDlgItem( hDlg, id ), &rc );
\r
82 return rc.right - rc.left;
\r
86 static int GetControlHeight( HWND hDlg, int id )
\r
90 GetWindowRect( GetDlgItem( hDlg, id ), &rc );
\r
92 return rc.bottom - rc.top;
\r
95 static void SetControlPos( HWND hDlg, int id, int x, int y, int width, int height )
\r
97 HWND hControl = GetDlgItem( hDlg, id );
\r
99 SetWindowPos( hControl, HWND_TOP, x, y, width, height, SWP_NOZORDER );
\r
102 // Also here some of the size calculations should go to the back end, and their actual application to a front-end routine
\r
103 static void ResizeWindowControls( HWND hDlg )
\r
108 int maxControlWidth;
\r
109 int buttonWidth, buttonHeight;
\r
111 /* Initialize variables */
\r
112 GetClientRect( hDlg, &rc );
\r
114 clientWidth = rc.right - rc.left;
\r
115 clientHeight = rc.bottom - rc.top;
\r
117 maxControlWidth = clientWidth - 2*H_MARGIN;
\r
118 buttonWidth = GetControlWidth(hDlg, IDC_Send);
\r
119 buttonHeight = GetControlHeight(hDlg, IDC_Send);
\r
121 /* Resize controls */
\r
122 SetControlPos( hDlg, IDC_Clear, maxControlWidth+H_MARGIN-2*buttonWidth-5, V_MARGIN, buttonWidth, buttonHeight );
\r
123 SetControlPos( hDlg, IDC_Send, maxControlWidth+H_MARGIN-buttonWidth, V_MARGIN, buttonWidth, buttonHeight );
\r
124 SetControlPos( hDlg, IDC_ChatMemo, H_MARGIN, 2*V_MARGIN+buttonHeight, maxControlWidth, clientHeight-3*V_MARGIN-2*buttonHeight );
\r
125 SetControlPos( hDlg, OPT_ChatInput, H_MARGIN, clientHeight-V_MARGIN-buttonHeight, maxControlWidth, buttonHeight );
\r
127 // InvalidateRect( GetDlgItem(hDlg,IDC_EngineMemo1), NULL, FALSE );
\r
128 // InvalidateRect( GetDlgItem(hDlg,IDC_EngineMemo2), NULL, FALSE );
\r
131 // front end. Actual printing of PV lines into the output field
\r
132 static void InsertIntoMemo( HANDLE hDlg, char * text )
\r
134 HANDLE hMemo = GetDlgItem(hDlg, IDC_ChatMemo);
\r
136 SendMessage( hMemo, EM_SETSEL, 1000000, 1000000 );
\r
138 SendMessage( hMemo, EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) text );
\r
139 SendMessage( hMemo, EM_SCROLLCARET, 0, 0);
\r
143 InterceptArrowKeys(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
\r
150 case WM_KEYDOWN: // cloned from ConsoleInputSubClass()
\r
153 GetWindowText(hwnd, buf, MSG_SIZ);
\r
154 p = PrevInHistory(buf);
\r
156 SetWindowText(hwnd, p);
\r
157 sel.cpMin = 999999;
\r
158 sel.cpMax = 999999;
\r
159 SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&sel);
\r
164 p = NextInHistory();
\r
166 SetWindowText(hwnd, p);
\r
167 sel.cpMin = 999999;
\r
168 sel.cpMax = 999999;
\r
169 SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&sel);
\r
175 return (*chatInputWindowProc)(hwnd, message, wParam, lParam);
\r
178 // This seems pure front end
\r
179 LRESULT CALLBACK ChatProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
\r
181 static SnapData sd;
\r
182 char buf[MSG_SIZ], mess[MSG_SIZ];
\r
183 int partner = -1, i, x, y;
\r
184 static BOOL filterHasFocus[MAX_CHAT];
\r
188 for(i=0; i<MAX_CHAT; i++) if(hDlg == chatHandle[i]) { partner = i; break; }
\r
191 case WM_INITDIALOG:
\r
193 for(i=0; i<MAX_CHAT; i++) if(chatHandle[i] == NULL) { partner = i; break; }
\r
194 chatHandle[partner] = hDlg;
\r
195 sprintf(buf, "Chat Window %s", first.tidy);
\r
196 SetWindowText(hDlg, buf);
\r
198 for(i=0; i<MAX_CHAT; i++) if(chatHandle[i]) {
\r
199 if(i == partner) continue;
\r
200 // set our button in other open chats
\r
201 SetDlgItemText(chatHandle[i], IDC_Focus1+partner-(i<partner), chatPartner[partner]);
\r
202 EnableWindow( GetDlgItem(chatHandle[i], IDC_Focus1+partner-(i<partner)), 1 );
\r
203 // and buttons for other chats in ours
\r
204 SetDlgItemText(hDlg, IDC_Focus1+i-(i>partner), chatPartner[i]);
\r
205 } else EnableWindow( GetDlgItem(hDlg, IDC_Focus1+i-(i>partner)), 0 );
\r
206 for(i=0; i<MAX_CHAT-1; i++) { Button_SetStyle(GetDlgItem(hDlg, IDC_Focus1+i), BS_PUSHBUTTON|BS_LEFT, TRUE); }
\r
207 x = wpConsole.x; y = wpConsole.y; EnsureOnScreen(&x, &y, 0, 0);
\r
208 SetWindowPos(hDlg, NULL, x, y, 0, 0, SWP_NOZORDER|SWP_NOSIZE);
\r
209 SendMessage( GetDlgItem(hDlg, IDC_ChatPartner), // [HGM] clickbox: initialize with requested handle
\r
210 WM_SETTEXT, 0, (LPARAM) chatPartner[partner] );
\r
211 filterHasFocus[partner] = TRUE;
\r
212 onTop = partner; // a newly opened box becomes top one
\r
213 if(chatPartner[partner][0]) {
\r
214 filterHasFocus[partner] = FALSE;
\r
215 SetFocus( GetDlgItem(hDlg, OPT_ChatInput) );
\r
217 hMemo = GetDlgItem(hDlg, IDC_ChatMemo);
\r
218 wMask = (WORD) SendMessage(hMemo, EM_GETEVENTMASK, 0, 0L);
\r
219 SendMessage(hMemo, EM_SETEVENTMASK, 0, wMask | ENM_LINK);
\r
220 SendMessage(hMemo, EM_AUTOURLDETECT, TRUE, 0L);
\r
221 chatInputWindowProc = (WNDPROC) // cloned from ConsoleWndProc(). Assume they all share same proc.
\r
222 SetWindowLong(GetDlgItem(hDlg, OPT_ChatInput), GWL_WNDPROC, (LONG) InterceptArrowKeys);
\r
226 if (((NMHDR*)lParam)->code == EN_LINK)
\r
228 ENLINK *pLink = (ENLINK*)lParam;
\r
229 if (pLink->msg == WM_LBUTTONUP)
\r
233 tr.chrg = pLink->chrg;
\r
234 tr.lpstrText = malloc(1+tr.chrg.cpMax-tr.chrg.cpMin);
\r
235 SendMessage( GetDlgItem(hDlg, IDC_ChatMemo), EM_GETTEXTRANGE, 0, (LPARAM)&tr);
\r
236 ShellExecute(NULL, "open", tr.lpstrText, NULL, NULL, SW_SHOW);
\r
237 free(tr.lpstrText);
\r
245 If <Enter> is pressed while editing the filter, it's better to apply
\r
246 the filter rather than selecting the current game.
\r
248 if( LOWORD(wParam) == IDC_ChatPartner ) {
\r
249 switch( HIWORD(wParam) ) {
\r
251 filterHasFocus[partner] = TRUE;
\r
254 filterHasFocus[partner] = FALSE;
\r
259 if( filterHasFocus[partner] && (LOWORD(wParam) == IDC_Send) ) {
\r
260 SetFocus(GetDlgItem(hDlg, OPT_ChatInput));
\r
261 wParam = IDC_Change;
\r
263 /* [AS] End command replacement */
\r
265 switch (LOWORD(wParam)) {
\r
267 case IDCANCEL: /* let Esc key switch focus back to console */
\r
268 SetFocus(GetDlgItem(hwndConsole, OPT_ConsoleInput));
\r
272 SendMessage( GetDlgItem(hDlg, IDC_ChatMemo), WM_SETTEXT, 0, (LPARAM) "" );
\r
276 GetDlgItemText(hDlg, IDC_ChatPartner, chatPartner[partner], MSG_SIZ);
\r
277 for(i=0; i<MAX_CHAT; i++) if(chatHandle[i] && i != partner) {
\r
278 // set our button in other open chats
\r
279 SetDlgItemText(chatHandle[i], IDC_Focus1+partner-(i<partner), chatPartner[partner]);
\r
284 GetDlgItemText(hDlg, OPT_ChatInput, mess, MSG_SIZ);
\r
285 SetDlgItemText(hDlg, OPT_ChatInput, "");
\r
286 // from here on it could be back-end
\r
287 SaveInHistory(mess);
\r
288 if(!strcmp("whispers", chatPartner[partner]))
\r
289 sprintf(buf, "whisper %s\n", mess); // WHISPER box uses "whisper" to send
\r
290 else if(!strcmp("shouts", chatPartner[partner]))
\r
291 sprintf(buf, "shout %s\n", mess); // SHOUT box uses "shout" to send
\r
293 if(!atoi(chatPartner[partner])) {
\r
294 sprintf(buf, "> %s\r\n", mess); // echo only tells to handle, not channel
\r
295 InsertIntoMemo(hDlg, buf);
\r
296 sprintf(buf, "xtell %s %s\n", chatPartner[partner], mess);
\r
298 sprintf(buf, "tell %s %s\n", chatPartner[partner], mess);
\r
307 i = LOWORD(wParam) - IDC_Focus1;
\r
308 if(i >= partner) i++;
\r
310 SetFocus(GetDlgItem(hDlg, IDC_Send));
\r
311 if(chatHandle[i]) {
\r
313 for(j=0; j<MAX_CHAT; j++) if(i != j && chatHandle[j])
\r
314 Button_SetState(GetDlgItem(chatHandle[j], IDC_Focus1+i-(j<i)), FALSE);
\r
315 SetFocus(GetDlgItem(chatHandle[i], OPT_ChatInput));
\r
326 chatHandle[partner] = 0;
\r
327 chatPartner[partner][0] = 0;
\r
329 for(i=0; i<MAX_CHAT; i++) if(chatHandle[i] && i != partner) {
\r
330 // set our button in other open chats
\r
331 SetDlgItemText(chatHandle[i], IDC_Focus1+partner-(i<partner), "");
\r
332 EnableWindow( GetDlgItem(chatHandle[i], IDC_Focus1+partner-(i<partner)), 0 );
\r
334 EndDialog(hDlg, TRUE);
\r
338 ResizeWindowControls( hDlg );
\r
341 case WM_ENTERSIZEMOVE:
\r
342 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
345 return OnSizing( &sd, hDlg, wParam, lParam );
\r
348 return OnMoving( &sd, hDlg, wParam, lParam );
\r
350 case WM_EXITSIZEMOVE:
\r
351 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
358 void ChatPopUp(char *icsHandle)
\r
361 int i, partner = -1;
\r
363 CheckMenuItem(GetMenu(hwndMain), IDM_NewChat, MF_CHECKED);
\r
364 for(i=0; i<MAX_CHAT; i++) if(chatHandle[i] == NULL) { partner = i; break; }
\r
365 if(partner == -1) { DisplayError("You first have to close a Chat Box\nbefore you can open a new one", 0); return; }
\r
366 if(icsHandle) // [HGM] clickbox set handle in advance
\r
367 strcpy(chatPartner[partner], icsHandle);
\r
368 else chatPartner[partner][0] = NULLCHAR;
\r
371 lpProc = MakeProcInstance( (FARPROC) ChatProc, hInst );
\r
373 /* Note to self: dialog must have the WS_VISIBLE style set, otherwise it's not shown! */
\r
374 CreateDialog( hInst, MAKEINTRESOURCE(DLG_Chat), hwndConsole, (DLGPROC)lpProc );
\r
376 FreeProcInstance(lpProc);
\r
383 if(--chatCount <= 0)
\r
384 CheckMenuItem(GetMenu(hwndMain), IDM_NewChat, MF_UNCHECKED);
\r
388 //------------------------ pure back-end routines -------------------------------
\r
390 void OutputChatMessage(int partner, char *text)
\r
393 if(!chatHandle[partner]) return;
\r
395 int n = strlen(text);
\r
396 text[n+1] = 0; text[n] = '\n'; text[n-1] = '\r'; // Needs CR to not lose line breaks on copy-paste
\r
397 InsertIntoMemo(chatHandle[partner], text);
\r
398 if(partner != onTop) for(j=0; j<MAX_CHAT; j++) if(j != partner && chatHandle[j])
\r
399 Button_SetState(GetDlgItem(chatHandle[j], IDC_Focus1+partner-(j<partner)), TRUE);
\r