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
37 #include "frontend.h"
\r
38 #include "winboard.h"
\r
39 #include "backend.h"
\r
44 extern char chatPartner[MAX_CHAT][MSG_SIZ];
\r
45 HANDLE chatHandle[MAX_CHAT];
\r
47 void SendToICS P((char *s));
\r
48 void ChatPopUp P((char *s));
\r
51 /* Imports from backend.c */
\r
52 extern int opponentKibitzes;
\r
54 /* Imports from winboard.c */
\r
55 extern HWND ChatDialog;
\r
57 extern HINSTANCE hInst;
\r
58 extern HWND hwndMain;
\r
60 extern WindowPlacement wpChat[MAX_CHAT];
\r
62 extern BoardSize boardSize;
\r
64 /* Module variables */
\r
68 // front end, although we might make GetWindowRect front end instead
\r
69 static int GetControlWidth( HWND hDlg, int id )
\r
73 GetWindowRect( GetDlgItem( hDlg, id ), &rc );
\r
75 return rc.right - rc.left;
\r
79 static int GetControlHeight( HWND hDlg, int id )
\r
83 GetWindowRect( GetDlgItem( hDlg, id ), &rc );
\r
85 return rc.bottom - rc.top;
\r
88 static void SetControlPos( HWND hDlg, int id, int x, int y, int width, int height )
\r
90 HWND hControl = GetDlgItem( hDlg, id );
\r
92 SetWindowPos( hControl, HWND_TOP, x, y, width, height, SWP_NOZORDER );
\r
95 // Also here some of the size calculations should go to the back end, and their actual application to a front-end routine
\r
96 static void ResizeWindowControls( HWND hDlg )
\r
101 int maxControlWidth;
\r
102 int buttonWidth, buttonHeight;
\r
104 /* Initialize variables */
\r
105 GetClientRect( hDlg, &rc );
\r
107 clientWidth = rc.right - rc.left;
\r
108 clientHeight = rc.bottom - rc.top;
\r
110 maxControlWidth = clientWidth - 2*H_MARGIN;
\r
111 buttonWidth = GetControlWidth(hDlg, IDC_Send);
\r
112 buttonHeight = GetControlHeight(hDlg, IDC_Send);
\r
114 /* Resize controls */
\r
115 SetControlPos( hDlg, IDC_Clear, maxControlWidth+H_MARGIN-2*buttonWidth-5, V_MARGIN, buttonWidth, buttonHeight );
\r
116 SetControlPos( hDlg, IDC_Send, maxControlWidth+H_MARGIN-buttonWidth, V_MARGIN, buttonWidth, buttonHeight );
\r
117 SetControlPos( hDlg, IDC_ChatMemo, H_MARGIN, 2*V_MARGIN+buttonHeight, maxControlWidth, clientHeight-3*V_MARGIN-2*buttonHeight );
\r
118 SetControlPos( hDlg, OPT_ChatInput, H_MARGIN, clientHeight-V_MARGIN-buttonHeight, maxControlWidth, buttonHeight );
\r
120 // InvalidateRect( GetDlgItem(hDlg,IDC_EngineMemo1), NULL, FALSE );
\r
121 // InvalidateRect( GetDlgItem(hDlg,IDC_EngineMemo2), NULL, FALSE );
\r
124 // front end. Actual printing of PV lines into the output field
\r
125 static void InsertIntoMemo( HANDLE hDlg, char * text )
\r
127 HANDLE hMemo = GetDlgItem(hDlg, IDC_ChatMemo);
\r
129 SendMessage( hMemo, EM_SETSEL, 1000000, 1000000 );
\r
131 SendMessage( hMemo, EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) text );
\r
132 SendMessage( hMemo, EM_SCROLLCARET, 0, 0);
\r
135 // This seems pure front end
\r
136 LRESULT CALLBACK ChatProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
\r
138 static SnapData sd;
\r
139 char buf[MSG_SIZ], mess[MSG_SIZ];
\r
140 int partner = -1, i;
\r
141 static BOOL filterHasFocus[MAX_CHAT];
\r
143 for(i=0; i<MAX_CHAT; i++) if(hDlg == chatHandle[i]) { partner = i; break; }
\r
146 case WM_INITDIALOG:
\r
148 for(i=0; i<MAX_CHAT; i++) if(chatHandle[i] == NULL) { partner = i; break; }
\r
149 chatHandle[partner] = hDlg;
\r
150 sprintf(buf, "Chat Window %s", first.tidy);
\r
151 SetWindowText(hDlg, buf);
\r
153 // chatPartner[partner][0] = 0;
\r
154 SendMessage( GetDlgItem(hDlg, IDC_ChatPartner), // [HGM] clickbox: initialize with requested handle
\r
155 WM_SETTEXT, 0, (LPARAM) chatPartner[partner] );
\r
156 filterHasFocus[partner] = TRUE;
\r
157 if(chatPartner[partner][0]) {
\r
158 filterHasFocus[partner] = FALSE;
\r
159 SetFocus( GetDlgItem(hDlg, OPT_ChatInput) );
\r
166 If <Enter> is pressed while editing the filter, it's better to apply
\r
167 the filter rather than selecting the current game.
\r
169 if( LOWORD(wParam) == IDC_ChatPartner ) {
\r
170 switch( HIWORD(wParam) ) {
\r
172 filterHasFocus[partner] = TRUE;
\r
175 filterHasFocus[partner] = FALSE;
\r
180 if( filterHasFocus[partner] && (LOWORD(wParam) == IDC_Send) ) {
\r
181 SetFocus(GetDlgItem(hDlg, OPT_ChatInput));
\r
182 wParam = IDC_Change;
\r
184 /* [AS] End command replacement */
\r
186 switch (LOWORD(wParam)) {
\r
189 chatHandle[partner] = 0;
\r
190 chatPartner[partner][0] = 0;
\r
192 EndDialog(hDlg, TRUE);
\r
196 SendMessage( GetDlgItem(hDlg, IDC_ChatMemo), WM_SETTEXT, 0, (LPARAM) "" );
\r
200 GetDlgItemText(hDlg, IDC_ChatPartner, chatPartner[partner], MSG_SIZ);
\r
204 GetDlgItemText(hDlg, OPT_ChatInput, mess, MSG_SIZ);
\r
205 SetDlgItemText(hDlg, OPT_ChatInput, "");
\r
206 // from here on it could be back-end
\r
207 if(!strcmp("WHISPER", chatPartner[partner]))
\r
208 sprintf(buf, "whisper %s\n", mess); // WHISPER box uses "whisper" to send
\r
210 if(!atoi(chatPartner[partner])) {
\r
211 sprintf(buf, "> %s\r\n", mess); // echo only tells to handle, not channel
\r
212 InsertIntoMemo(hDlg, buf);
\r
213 sprintf(buf, "xtell %s %s\n", chatPartner[partner], mess);
\r
215 sprintf(buf, "tell %s %s\n", chatPartner[partner], mess);
\r
227 chatHandle[partner] = 0;
\r
228 chatPartner[partner][0] = 0;
\r
230 EndDialog(hDlg, TRUE);
\r
234 ResizeWindowControls( hDlg );
\r
237 case WM_ENTERSIZEMOVE:
\r
238 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
241 return OnSizing( &sd, hDlg, wParam, lParam );
\r
244 return OnMoving( &sd, hDlg, wParam, lParam );
\r
246 case WM_EXITSIZEMOVE:
\r
247 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
254 void ChatPopUp(char *icsHandle)
\r
257 int i, partner = -1;
\r
259 CheckMenuItem(GetMenu(hwndMain), IDM_NewChat, MF_CHECKED);
\r
260 for(i=0; i<MAX_CHAT; i++) if(chatHandle[i] == NULL) { partner = i; break; }
\r
261 if(partner == -1) { DisplayError("You first have to close a Chat Box\nbefore you can open a new one", 0); return; }
\r
262 if(icsHandle) // [HGM] clickbox set handle in advance
\r
263 strcpy(chatPartner[partner], icsHandle);
\r
264 else chatPartner[partner][0] = NULLCHAR;
\r
267 lpProc = MakeProcInstance( (FARPROC) ChatProc, hInst );
\r
269 /* Note to self: dialog must have the WS_VISIBLE style set, otherwise it's not shown! */
\r
270 CreateDialog( hInst, MAKEINTRESOURCE(DLG_Chat), hwndMain, (DLGPROC)lpProc );
\r
272 FreeProcInstance(lpProc);
\r
279 if(--chatCount <= 0)
\r
280 CheckMenuItem(GetMenu(hwndMain), IDM_NewChat, MF_UNCHECKED);
\r
284 //------------------------ pure back-end routines -------------------------------
\r
286 void OutputChatMessage(int partner, char *text)
\r
288 if(!chatHandle[partner]) return;
\r
290 int n = strlen(text);
\r
291 text[n+1] = 0; text[n] = '\n'; text[n-1] = '\r'; // Needs CR to not lose line breaks on copy-paste
\r
292 InsertIntoMemo(chatHandle[partner], text);
\r