TextChat.cpp
14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002 Ultr@VNC Team Members. All Rights Reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
//
// If the source code for the program is not available from the place from
// which you received this file, check
// http://ultravnc.sourceforge.net/
#include "stdhdrs.h"
#include "resource.h"
#include "vncclient.h"
#include "vncserver.h"
#include "TextChat.h"
#include "commctrl.h"
#include "richedit.h"
#include "localization.h" // Act : add localization on messages
#define TEXTMAXSIZE 8192
#define MAXNAMESIZE 128 // MAX_COMPUTERNAME_LENGTH+1 (32)
#define CHAT_OPEN -1 // Todo; put these codes in rfbproto.h
#define CHAT_CLOSE -2
#define CHAT_FINISHED -3
extern HINSTANCE hAppInstance;
//
//
//
TextChat::TextChat(vncClient *pCC)
{
m_hDlg=NULL;
m_pCC = pCC;
m_fTextChatRunning = false;
m_fPersistentTexts = false;
m_szLocalText = new char [TEXTMAXSIZE]; // Text (message) entered by local user
memset(m_szLocalText, 0, TEXTMAXSIZE);
m_szLastLocalText = new char [TEXTMAXSIZE]; // Last local text (no more necessary)
memset(m_szLastLocalText, 0, TEXTMAXSIZE);
m_szRemoteText = new char [TEXTMAXSIZE]; // Incoming remote text (remote message)
memset(m_szRemoteText, 0, TEXTMAXSIZE);
m_szRemoteName = new char[MAXNAMESIZE]; // Name of remote machine
memset(m_szRemoteName,0,MAXNAMESIZE);
m_szLocalName = new char[MAXNAMESIZE]; // Name of local machine
memset(m_szLocalName,0,MAXNAMESIZE);
m_szTextBoxBuffer = new char[TEXTMAXSIZE]; // History Text (containing all chat messages from everybody)
memset(m_szTextBoxBuffer,0,TEXTMAXSIZE);
// memset(m_szUserID, 0, 16); // Short User ID (name, pseudo...whatever) that replace
// the local computer name/IP
unsigned long pcSize=MAXNAMESIZE;
if (GetComputerName(m_szLocalName, &pcSize))
{
if( pcSize >= MAXNAMESIZE)
{
m_szLocalName[MAXNAMESIZE-4]='.';
m_szLocalName[MAXNAMESIZE-3]='.';
m_szLocalName[MAXNAMESIZE-2]='.';
m_szLocalName[MAXNAMESIZE-1]=0x00;
}
}
// Load the Rich Edit control DLL
m_hRichEdit = LoadLibrary( "RICHED32.DLL" );
if (!m_hRichEdit)
{
MessageBox( NULL, sz_ID_RICHED32_UNLOAD,
sz_ID_RICHED32_DLL_LD, MB_OK | MB_ICONEXCLAMATION );
// Todo: do normal edit instead (no colors)
}
}
//
//
//
TextChat::~TextChat()
{
if (m_szLocalText != NULL) delete [] m_szLocalText;
if (m_szLastLocalText != NULL) delete [] m_szLastLocalText;
if (m_szRemoteText != NULL) delete [] m_szRemoteText;
if (m_szRemoteName != NULL) delete [] m_szRemoteName;
if (m_szLocalName != NULL) delete [] m_szLocalName;
if (m_szTextBoxBuffer != NULL) delete [] m_szTextBoxBuffer;
m_fTextChatRunning = false;
SendMessage(m_hDlg, WM_COMMAND, IDCANCEL, 0L);
if (m_hRichEdit != NULL) FreeLibrary(m_hRichEdit);
}
//
//
//
void TextChat::ShowChatWindow(bool fVisible)
{
ShowWindow(m_hDlg, fVisible ? SW_RESTORE : SW_MINIMIZE);
SetForegroundWindow(m_hDlg);
//SetFocus(GetDlgItem(m_hDlg, IDC_INPUTAREA_EDIT));
}
//
// Set text format to a selection in the Chat area
//
void TextChat::SetTextFormat(bool bBold /*= false */, bool bItalic /*= false*/
, long nSize /*= 0x75*/, const char* szFaceName /*= "MS Sans Serif"*/, DWORD dwColor /*= BLACK*/)
{
if ( GetDlgItem( m_hDlg, IDC_CHATAREA_EDIT ) ) // Sanity Check
{
CHARFORMAT cf;
memset( &cf, 0, sizeof(CHARFORMAT) ); // Initialize structure
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM_COLOR | CFM_FACE | CFM_SIZE;
if (bBold) {
cf.dwMask |= CFM_BOLD;
cf.dwEffects |= CFE_BOLD;
}
if (bItalic) {
cf.dwMask |= CFM_ITALIC;
cf.dwEffects |= CFE_ITALIC;
}
cf.crTextColor = dwColor; // set color in AABBGGRR mode (alpha-RGB)
cf.yHeight = nSize; // set size in points
strcpy( cf.szFaceName, szFaceName);
SendDlgItemMessage( m_hDlg, IDC_CHATAREA_EDIT, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf );
}
}
//
//
//
void TextChat::ProcessTextChatMsg(int nTO)
{
rfbTextChatMsg tcm;
m_pCC->m_socket->ReadExact(((char *) &tcm) + nTO, sz_rfbTextChatMsg - nTO);
int len = Swap32IfLE(tcm.length);
if (len == CHAT_OPEN)
{
if (m_fTextChatRunning) return;
// PostMessage(m_pCC->m_server->GetDesktopPointer()->Window(), WM_USER+888, 0, (LPARAM)this);
DisplayTextChat();
return;
}
else if (len == CHAT_CLOSE)
{
// Close TextChat Dialog
if (!m_fTextChatRunning) return;
PostMessage(m_hDlg, WM_COMMAND, IDOK, 0);
return;
}
else if (len == CHAT_FINISHED)
{
// Close TextChat Dialog
if (!m_fTextChatRunning) return;
// m_fTextChatRunning = false;
// PostMessage(m_hDlg, WM_COMMAND, IDCANCEL, 0);
return;
}
else
{
// Read the incoming text
if (len > TEXTMAXSIZE) return; // Todo: Improve this...
if (len == 0)
{
SetDlgItemText(m_hDlg, IDC_CHATAREA_EDIT, "");
memset(m_szRemoteText, 0, TEXTMAXSIZE);
}
else
{
memset(m_szRemoteText, 0, TEXTMAXSIZE);
m_pCC->m_socket->ReadExact(m_szRemoteText, len);
ShowChatWindow(true); // Show the chat window on new message reception
PrintMessage(m_szRemoteText, m_szRemoteName, RED);
}
}
}
//
// Tell the client to initiate a TextChat
//
void TextChat::OrderTextChat()
{
SendTextChatRequest(CHAT_OPEN);
return;
}
//
//
//
void TextChat::SendTextChatRequest(int nMsg)
{
if (m_pCC->m_fFileTransferRunning) return; // Don't break a running file transfer please...
rfbTextChatMsg tcm;
tcm.type = rfbTextChat;
tcm.length = Swap32IfLE(nMsg);
m_pCC->m_socket->SendExact((char *)&tcm, sz_rfbTextChatMsg, rfbTextChat);
return;
}
//
// Output messages in the chat area
//
//
void TextChat::PrintMessage(const char* szMessage,const char* szSender,DWORD dwColor /*= BLACK*/)
{
// char szTextBoxBuffer[TEXTMAXSIZE];
// memset(szTextBoxBuffer,0,TEXTMAXSIZE);
CHARRANGE cr;
// Todo: test if chat text + sender + message > TEXTMAXSIZE -> Modulo display
if (szSender != NULL) //print the sender's name
{
SendDlgItemMessage(m_hDlg, IDC_CHATAREA_EDIT,WM_GETTEXT, TEXTMAXSIZE-1,(LONG)m_szTextBoxBuffer);
cr.cpMax = strlen(m_szTextBoxBuffer); // Select the last caracter to make the text insertion
cr.cpMin = cr.cpMax;
SendDlgItemMessage(m_hDlg, IDC_CHATAREA_EDIT,EM_EXSETSEL,0,(LONG) &cr);
/***/
SetTextFormat(false,false,0x75,"MS Sans Serif",dwColor);
_snprintf(m_szTextBoxBuffer,TEXTMAXSIZE-1,"<%s>: ",szSender);
SendDlgItemMessage(m_hDlg, IDC_CHATAREA_EDIT,EM_REPLACESEL,FALSE,(LONG)m_szTextBoxBuffer); // Replace the selection with the message
}
if (szMessage != NULL) //print the sender's message
{
SendDlgItemMessage(m_hDlg, IDC_CHATAREA_EDIT,WM_GETTEXT, TEXTMAXSIZE-1,(LONG)m_szTextBoxBuffer);
cr.cpMax = strlen(m_szTextBoxBuffer); // Select the last caracter to make the text insertion
cr.cpMin = cr.cpMax;
SendDlgItemMessage(m_hDlg, IDC_CHATAREA_EDIT,EM_EXSETSEL,0,(LONG) &cr);
/***/
SetTextFormat(false, false, 0x75, "MS Sans Serif", dwColor != GREY ? BLACK : GREY);
_snprintf(m_szTextBoxBuffer, TEXTMAXSIZE-1, "%s", szMessage);
SendDlgItemMessage(m_hDlg, IDC_CHATAREA_EDIT,EM_REPLACESEL,FALSE,(LONG)m_szTextBoxBuffer);
}
// Scroll down the chat window
// The following seems necessary under W9x & Me if we want the Edit to scroll to bottom...
SCROLLINFO si;
ZeroMemory(&si, sizeof(SCROLLINFO));
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_RANGE|SIF_PAGE;
GetScrollInfo(GetDlgItem(m_hDlg, IDC_CHATAREA_EDIT), SB_VERT, &si);
si.nPos = si.nMax - max(si.nPage - 1, 0);
SendDlgItemMessage(m_hDlg, IDC_CHATAREA_EDIT, WM_VSCROLL, MAKELONG(SB_THUMBPOSITION, si.nPos), 0L); // Scroll down the ch
// This line does the bottom scrolling correctly under NT4,W2K, XP...
// SendDlgItemMessage(m_hDlg, IDC_CHATAREA_EDIT, WM_VSCROLL, SB_BOTTOM, 0L);
}
//
// Send local text content
//
void TextChat::SendLocalText(void)
{
if (m_pCC->m_fFileTransferRunning) return; // Don't break a running file transfer please...
// We keep it because we could use it
// for future retype functionality. (F3)
memcpy(m_szLastLocalText, m_szLocalText, strlen(m_szLocalText));
PrintMessage(m_szLocalText, m_szLocalName, BLUE);
rfbTextChatMsg tcm;
tcm.type = rfbTextChat;
tcm.length = Swap32IfLE(strlen(m_szLocalText));
m_pCC->m_socket->SendExact((char *)&tcm, sz_rfbTextChatMsg, rfbTextChat);
m_pCC->m_socket->SendExact((char *)m_szLocalText, strlen(m_szLocalText));
//and we clear the input box
SetDlgItemText(m_hDlg, IDC_INPUTAREA_EDIT, "");
return;
}
LRESULT CALLBACK TextChat::DoDialogThread(LPVOID lpParameter)
{
TextChat* _this = (TextChat*)lpParameter;
_this->m_fTextChatRunning = true;
return DialogBoxParam(hAppInstance, MAKEINTRESOURCE(IDD_TEXTCHAT_DLG),
NULL, (DLGPROC) TextChatDlgProc, (LONG) _this);
}
int TextChat::DoDialog()
{
m_fTextChatRunning = true; // Here.Important.
return DialogBoxParam(hAppInstance, MAKEINTRESOURCE(IDD_TEXTCHAT_DLG),
NULL, (DLGPROC) TextChatDlgProc, (LONG) this);
}
HWND TextChat::DisplayTextChat()
{
DWORD threadID;
HANDLE m_Thread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(TextChat::DoDialogThread),(LPVOID)this, 0, &threadID);
ResumeThread(m_Thread);
return (HWND)0;
}
//
//
//
void TextChat::KillDialog()
{
// DestroyWindow(m_hDlg);
m_fTextChatRunning = false;
PostMessage(m_hDlg, WM_COMMAND, IDCANCEL, 0);
}
//
//
//
void TextChat::RefuseTextChat()
{
SendTextChatRequest(CHAT_CLOSE);
SendTextChatRequest(CHAT_FINISHED);
}
//
//
//
BOOL CALLBACK TextChat::TextChatDlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
TextChat *_this = (TextChat *) GetWindowLong(hWnd, GWL_USERDATA);
switch (uMsg)
{
case WM_INITDIALOG:
{
SetWindowLong(hWnd, GWL_USERDATA, lParam);
TextChat *_this = (TextChat *) lParam;
if (_this->m_szLocalText == NULL || _this->m_szRemoteText == NULL)
EndDialog(hWnd, FALSE);
_this->m_hWnd = hWnd;
_this->m_hDlg = hWnd;
if (_snprintf(_this->m_szRemoteName,MAXNAMESIZE-1,"%s", _this->m_pCC->GetClientName()) < 0 )
{
_this->m_szRemoteName[MAXNAMESIZE-4]='.';
_this->m_szRemoteName[MAXNAMESIZE-3]='.';
_this->m_szRemoteName[MAXNAMESIZE-2]='.';
_this->m_szRemoteName[MAXNAMESIZE-1]=0x00;
}
const long lTitleBufSize = 256;
char szTitle[lTitleBufSize];
_snprintf(szTitle,lTitleBufSize-1, sz_ID_CHAT_WITH_S_ULTRAVNC,_this->m_szRemoteName);
SetWindowText(hWnd, szTitle);
memset(_this->m_szLocalText, 0, TEXTMAXSIZE);
// Local message box
SetDlgItemText(hWnd, IDC_INPUTAREA_EDIT, _this->m_szLocalText);
// Chat area
_this->SetTextFormat(); // Set character formatting and background color
SendDlgItemMessage( hWnd, IDC_CHATAREA_EDIT, EM_SETBKGNDCOLOR, FALSE, 0xFFFFFF );
// if (!_this->m_fPersistentTexts)
{
memset(_this->m_szLastLocalText, 0, TEXTMAXSIZE); // Future retype functionnality
memset(_this->m_szTextBoxBuffer, 0, TEXTMAXSIZE); // Clear Chat area
// Load and display diclaimer message
if (LoadString(hAppInstance,IDS_WARNING,_this->m_szRemoteText, TEXTMAXSIZE -1) )
_this->PrintMessage(_this->m_szRemoteText, NULL ,GREY);
}
// else
// SendDlgItemMessage(hWnd,IDC_CHATAREA_EDIT, WM_SETTEXT, 0, (LONG)_this->m_szTextBoxBuffer);
// Scroll down the chat window
// The following seems necessary under W9x & Me if we want the Edit to scroll to bottom...
SCROLLINFO si;
ZeroMemory(&si, sizeof(SCROLLINFO));
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_RANGE|SIF_PAGE;
GetScrollInfo(GetDlgItem(hWnd, IDC_CHATAREA_EDIT), SB_VERT, &si);
si.nPos = si.nMax - max(si.nPage - 1, 0);
SendDlgItemMessage(hWnd, IDC_CHATAREA_EDIT, WM_VSCROLL, MAKELONG(SB_THUMBPOSITION, si.nPos), 0L);
// This line does the bottom scrolling correctly under NT4,W2K, XP...
// SendDlgItemMessage(m_hDlg, IDC_CHATAREA_EDIT, WM_VSCROLL, SB_BOTTOM, 0L);
// SendDlgItemMessage(hWnd, IDC_PERSISTENT_CHECK, BM_SETCHECK, _this->m_fPersistentTexts, 0);
//if (strlen(_this->m_szUserID) > 0)
// SetDlgItemText(hWnd, IDC_USERID_EDIT, _this->m_szUserID);
SetForegroundWindow(hWnd);
return TRUE;
}
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
/*
case IDC_PERSISTENT_CHECK:
_this->m_fPersistentTexts = (SendDlgItemMessage(hWnd, IDC_PERSISTENT_CHECK, BM_GETCHECK, 0, 0) == BST_CHECKED);
return TRUE;
*/
case IDOK:
// Client order to close TextChat
EndDialog(hWnd, FALSE);
_this->m_fTextChatRunning = false;
_this->SendTextChatRequest(CHAT_FINISHED);
return TRUE;
case IDCANCEL:
_this->SendTextChatRequest(CHAT_CLOSE); // Client must close TextChat
EndDialog(hWnd, FALSE);
_this->m_fTextChatRunning = false;
_this->SendTextChatRequest(CHAT_FINISHED);
return TRUE;
case IDC_SEND_B:
{
memset(_this->m_szLocalText,0,TEXTMAXSIZE);
UINT nRes = GetDlgItemText( hWnd, IDC_LOCALTEXT_EDIT, _this->m_szLocalText, TEXTMAXSIZE-1);
strcat(_this->m_szLocalText, "\n");
_this->SendLocalText();
SetFocus(GetDlgItem(hWnd, IDC_INPUTAREA_EDIT));
}
return TRUE;
case IDC_INPUTAREA_EDIT:
if(HIWORD(wParam) == EN_UPDATE)
{
UINT nRes = GetDlgItemText( hWnd, IDC_INPUTAREA_EDIT, _this->m_szLocalText, TEXTMAXSIZE);
if (strstr(_this->m_szLocalText, "\n") > 0 ) // Enter triggers the message transmission
{
// nRes = GetDlgItemText( hWnd, IDC_USERID_EDIT, _this->m_szUserID, 16);
_this->SendLocalText();
}
}
return TRUE;
case IDC_HIDE_B:
_this->ShowChatWindow(false);
return TRUE;
default:
return TRUE;
break;
}
break;
case WM_SYSCOMMAND:
switch (LOWORD(wParam))
{
case SC_RESTORE:
_this->ShowChatWindow(true);
//SetFocus(GetDlgItem(hWnd, IDC_INPUTAREA_EDIT));
return TRUE;
}
break;
case WM_DESTROY:
// _this->m_fTextChatRunning = false;
// _this->SendTextChatRequest(CHAT_FINISHED);
EndDialog(hWnd, FALSE);
return TRUE;
}
return 0;
}