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