updated year in copyright info
[xboard.git] / winboard / wchat.c
1 /*\r
2  * Chat window (PV)\r
3  *\r
4  * Author: H.G.Muller (August 2009)\r
5  *\r
6  * Copyright 2009, 2010 Free Software Foundation, Inc. \r
7  *\r
8  * ------------------------------------------------------------------------\r
9  *\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
14  *\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
19  *\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
22  *\r
23  *------------------------------------------------------------------------\r
24  ** See the file ChangeLog for a revision history.  */\r
25 \r
26 #include "config.h"\r
27 \r
28 #include <windows.h> /* required for all Windows applications */\r
29 #include <richedit.h>\r
30 #include <stdio.h>\r
31 #include <stdlib.h>\r
32 #include <malloc.h>\r
33 #include <commdlg.h>\r
34 #include <dlgs.h>\r
35 \r
36 #include "common.h"\r
37 #include "frontend.h"\r
38 #include "winboard.h"\r
39 #include "backend.h"\r
40 \r
41 #include "wsnap.h"\r
42 \r
43 int chatCount;\r
44 extern char chatPartner[MAX_CHAT][MSG_SIZ];\r
45 HANDLE chatHandle[MAX_CHAT];\r
46 \r
47 void SendToICS P((char *s));\r
48 void ChatPopUp();\r
49 void ChatPopDown();\r
50 \r
51 /* Imports from backend.c */\r
52 extern int opponentKibitzes;\r
53 \r
54 /* Imports from winboard.c */\r
55 extern HWND ChatDialog;\r
56 \r
57 extern HINSTANCE hInst;\r
58 extern HWND hwndMain;\r
59 \r
60 extern WindowPlacement wpChat[MAX_CHAT];\r
61 \r
62 extern BoardSize boardSize;\r
63 \r
64 /* Module variables */\r
65 #define H_MARGIN            5\r
66 #define V_MARGIN            5\r
67 \r
68 // front end, although we might make GetWindowRect front end instead\r
69 static int GetControlWidth( HWND hDlg, int id )\r
70 {\r
71     RECT rc;\r
72 \r
73     GetWindowRect( GetDlgItem( hDlg, id ), &rc );\r
74 \r
75     return rc.right - rc.left;\r
76 }\r
77 \r
78 // front end?\r
79 static int GetControlHeight( HWND hDlg, int id )\r
80 {\r
81     RECT rc;\r
82 \r
83     GetWindowRect( GetDlgItem( hDlg, id ), &rc );\r
84 \r
85     return rc.bottom - rc.top;\r
86 }\r
87 \r
88 static void SetControlPos( HWND hDlg, int id, int x, int y, int width, int height )\r
89 {\r
90     HWND hControl = GetDlgItem( hDlg, id );\r
91 \r
92     SetWindowPos( hControl, HWND_TOP, x, y, width, height, SWP_NOZORDER );\r
93 }\r
94 \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
97 {\r
98     RECT rc;\r
99     int clientWidth;\r
100     int clientHeight;\r
101     int maxControlWidth;\r
102     int buttonWidth, buttonHeight;\r
103 \r
104     /* Initialize variables */\r
105     GetClientRect( hDlg, &rc );\r
106 \r
107     clientWidth = rc.right - rc.left;\r
108     clientHeight = rc.bottom - rc.top;\r
109 \r
110     maxControlWidth = clientWidth - 2*H_MARGIN;\r
111     buttonWidth  = GetControlWidth(hDlg, IDC_Send);\r
112     buttonHeight = GetControlHeight(hDlg, IDC_Send);\r
113 \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
119 \r
120 //    InvalidateRect( GetDlgItem(hDlg,IDC_EngineMemo1), NULL, FALSE );\r
121 //    InvalidateRect( GetDlgItem(hDlg,IDC_EngineMemo2), NULL, FALSE );\r
122 }\r
123 \r
124 // front end. Actual printing of PV lines into the output field\r
125 static void InsertIntoMemo( HANDLE hDlg, char * text )\r
126 {\r
127     HANDLE hMemo = GetDlgItem(hDlg, IDC_ChatMemo);\r
128 \r
129     SendMessage( hMemo, EM_SETSEL, 1000000, 1000000 );\r
130 \r
131     SendMessage( hMemo, EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) text );\r
132     SendMessage( hMemo, EM_SCROLLCARET, 0, 0);\r
133 }\r
134 \r
135 // This seems pure front end\r
136 LRESULT CALLBACK ChatProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )\r
137 {\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
142 \r
143     for(i=0; i<MAX_CHAT; i++) if(hDlg == chatHandle[i]) { partner = i; break; }\r
144 \r
145     switch (message) {\r
146     case WM_INITDIALOG:\r
147         if(partner<0) {\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
152         }\r
153         chatPartner[partner][0] = 0;\r
154         filterHasFocus[partner] = FALSE;\r
155 \r
156         return FALSE;\r
157 \r
158     case WM_COMMAND:\r
159       /* \r
160         [AS]\r
161         If <Enter> is pressed while editing the filter, it's better to apply\r
162         the filter rather than selecting the current game.\r
163       */\r
164       if( LOWORD(wParam) == IDC_ChatPartner ) {\r
165           switch( HIWORD(wParam) ) {\r
166           case EN_SETFOCUS:\r
167               filterHasFocus[partner] = TRUE;\r
168               break;\r
169           case EN_KILLFOCUS:\r
170               filterHasFocus[partner] = FALSE;\r
171               break;\r
172           }\r
173       }\r
174 \r
175       if( filterHasFocus[partner] && (LOWORD(wParam) == IDC_Send) ) {\r
176           SetFocus(GetDlgItem(hDlg, OPT_ChatInput));\r
177           wParam = IDC_Change;\r
178       }\r
179       /* [AS] End command replacement */\r
180 \r
181         switch (LOWORD(wParam)) {\r
182 \r
183         case IDCANCEL:\r
184             chatHandle[partner] = 0;\r
185             chatPartner[partner][0] = 0;\r
186             ChatPopDown();\r
187             EndDialog(hDlg, TRUE);\r
188             break;\r
189 \r
190         case IDC_Clear:\r
191             SendMessage( GetDlgItem(hDlg, IDC_ChatMemo), WM_SETTEXT, 0, (LPARAM) "" );\r
192             break;\r
193 \r
194         case IDC_Change:\r
195             GetDlgItemText(hDlg, IDC_ChatPartner, chatPartner[partner], MSG_SIZ);\r
196             break;\r
197 \r
198         case IDC_Send:\r
199             GetDlgItemText(hDlg, OPT_ChatInput, mess, MSG_SIZ);\r
200             SetDlgItemText(hDlg, OPT_ChatInput, "");\r
201             // from here on it could be back-end\r
202             if(!strcmp("WHISPER", chatPartner[partner]))\r
203                 sprintf(buf, "whisper %s\n", mess); // WHISPER box uses "whisper" to send\r
204             else {\r
205                 if(!atoi(chatPartner[partner])) {\r
206                     sprintf(buf, "> %s\n", mess); // echo only tells to handle, not channel\r
207                 InsertIntoMemo(hDlg, buf);\r
208                 sprintf(buf, "xtell %s %s\n", chatPartner[partner], mess);\r
209                 } else\r
210                 sprintf(buf, "tell %s %s\n", chatPartner[partner], mess);\r
211             }\r
212             SendToICS(buf);\r
213             break;\r
214 \r
215         default:\r
216           break;\r
217         }\r
218 \r
219         break;\r
220 \r
221     case WM_CLOSE:\r
222         chatHandle[partner] = 0;\r
223         chatPartner[partner][0] = 0;\r
224         ChatPopDown();\r
225         EndDialog(hDlg, TRUE);\r
226         break;\r
227 \r
228     case WM_SIZE:\r
229         ResizeWindowControls( hDlg );\r
230         break;\r
231 \r
232     case WM_ENTERSIZEMOVE:\r
233         return OnEnterSizeMove( &sd, hDlg, wParam, lParam );\r
234 \r
235     case WM_SIZING:\r
236         return OnSizing( &sd, hDlg, wParam, lParam );\r
237 \r
238     case WM_MOVING:\r
239         return OnMoving( &sd, hDlg, wParam, lParam );\r
240 \r
241     case WM_EXITSIZEMOVE:\r
242         return OnExitSizeMove( &sd, hDlg, wParam, lParam );\r
243     }\r
244 \r
245     return FALSE;\r
246 }\r
247 \r
248 // front end\r
249 void ChatPopUp()\r
250 {\r
251   FARPROC lpProc;\r
252   \r
253   if(chatCount >= MAX_CHAT) return;\r
254 \r
255   CheckMenuItem(GetMenu(hwndMain), IDM_NewChat, MF_CHECKED);\r
256   chatCount++;\r
257 \r
258     lpProc = MakeProcInstance( (FARPROC) ChatProc, hInst );\r
259 \r
260     /* Note to self: dialog must have the WS_VISIBLE style set, otherwise it's not shown! */\r
261     CreateDialog( hInst, MAKEINTRESOURCE(DLG_Chat), hwndMain, (DLGPROC)lpProc );\r
262 \r
263     FreeProcInstance(lpProc);\r
264 \r
265 }\r
266 \r
267 // front end\r
268 void ChatPopDown()\r
269 {\r
270   if(--chatCount <= 0)\r
271         CheckMenuItem(GetMenu(hwndMain), IDM_NewChat, MF_UNCHECKED);\r
272 }\r
273 \r
274 \r
275 //------------------------ pure back-end routines -------------------------------\r
276 \r
277 void OutputChatMessage(int partner, char *text)\r
278 {\r
279         if(!chatHandle[partner]) return;\r
280 \r
281         InsertIntoMemo(chatHandle[partner], text);\r
282 }\r