forked from irwir/eMule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTRichEditCtrl.cpp
1509 lines (1326 loc) · 39.7 KB
/
HTRichEditCtrl.cpp
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//this file is part of eMule
//Copyright (C)2002-2008 Merkur ( strEmail.Format("%s@%s", "devteam", "emule-project.net") / http://www.emule-project.net )
//
//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., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "stdafx.h"
#include <share.h>
#include "emule.h"
#include "HTRichEditCtrl.h"
#include "OtherFunctions.h"
#include "Preferences.h"
#include "MenuCmds.h"
#include "Log.h"
#include <richole.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int CHTRichEditCtrl::sm_iSmileyClients = 0;
CComPtr<IStorage> CHTRichEditCtrl::sm_pIStorageSmileys;
CMapStringToPtr CHTRichEditCtrl::sm_aSmileyBitmaps;
IMPLEMENT_DYNAMIC(CHTRichEditCtrl, CRichEditCtrl)
BEGIN_MESSAGE_MAP(CHTRichEditCtrl, CRichEditCtrl)
ON_WM_CONTEXTMENU()
ON_WM_KEYDOWN()
ON_CONTROL_REFLECT(EN_ERRSPACE, OnEnErrspace)
ON_CONTROL_REFLECT(EN_MAXTEXT, OnEnMaxtext)
ON_NOTIFY_REFLECT_EX(EN_LINK, OnEnLink)
ON_WM_CREATE()
ON_WM_SYSCOLORCHANGE()
ON_WM_SETCURSOR()
ON_WM_SIZE()
END_MESSAGE_MAP()
CHTRichEditCtrl::CHTRichEditCtrl()
{
m_bAutoScroll = true;
m_bNoPaint = false;
m_bEnErrSpace = false;
m_bRestoreFormat = false;
memset(&m_cfDefault, 0, sizeof m_cfDefault);
m_bForceArrowCursor = false;
m_hArrowCursor = ::LoadCursor(NULL, IDC_ARROW);
m_bEnableSmileys = false;
m_crForeground = CLR_DEFAULT;
m_crBackground = CLR_DEFAULT;
m_crDfltForeground = CLR_DEFAULT;
m_crDfltBackground = CLR_DEFAULT;
m_bDfltForeground = false;
m_bDfltBackground = false;
}
CHTRichEditCtrl::~CHTRichEditCtrl()
{
EnableSmileys(false);
}
void CHTRichEditCtrl::Localize()
{
}
void CHTRichEditCtrl::Init(LPCTSTR pszTitle, LPCTSTR pszSkinKey)
{
SetProfileSkinKey(pszSkinKey);
SetTitle(pszTitle);
VERIFY( SendMessage(EM_SETUNDOLIMIT, 0, 0) == 0 );
int iMaxLogBuff = thePrefs.GetMaxLogBuff();
if (afxIsWin95())
LimitText(iMaxLogBuff > 0xFFFF ? 0xFFFF : iMaxLogBuff);
else
LimitText(iMaxLogBuff ? iMaxLogBuff : 128*1024);
m_iLimitText = GetLimitText();
VERIFY( GetSelectionCharFormat(m_cfDefault) );
// prevent the RE control to change the font height within single log lines (may happen with some Unicode chars)
DWORD dwLangOpts = SendMessage(EM_GETLANGOPTIONS);
SendMessage(EM_SETLANGOPTIONS, 0, dwLangOpts & ~(IMF_AUTOFONT /*| IMF_AUTOFONTSIZEADJUST*/));
//SendMessage(EM_SETEDITSTYLE, SES_EMULATESYSEDIT, SES_EMULATESYSEDIT);
}
void CHTRichEditCtrl::EnableSmileys(bool bEnable)
{
if (bEnable)
{
if (!m_bEnableSmileys)
{
m_bEnableSmileys = true;
sm_iSmileyClients++;
}
}
else
{
if (m_bEnableSmileys)
{
m_bEnableSmileys = false;
sm_iSmileyClients--;
if (sm_iSmileyClients <= 0)
{
PurgeSmileyCaches();
sm_iSmileyClients = 0;
}
}
}
}
void CHTRichEditCtrl::PurgeSmileyCaches()
{
POSITION pos = sm_aSmileyBitmaps.GetStartPosition();
while (pos)
{
CString strKey;
void *pValue;
sm_aSmileyBitmaps.GetNextAssoc(pos, strKey, pValue);
#ifdef USE_METAFILE
VERIFY( DeleteEnhMetaFile((HENHMETAFILE)pValue) );
#else
VERIFY( DeleteObject((HBITMAP)pValue) );
#endif
}
sm_aSmileyBitmaps.RemoveAll();
sm_pIStorageSmileys.Release();
}
void CHTRichEditCtrl::SetProfileSkinKey(LPCTSTR pszSkinKey)
{
m_strSkinKey = pszSkinKey;
}
void CHTRichEditCtrl::SetTitle(LPCTSTR pszTitle)
{
m_strTitle = pszTitle;
}
int CHTRichEditCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CRichEditCtrl::OnCreate(lpCreateStruct) == -1)
return -1;
Init(NULL);
return 0;
}
LRESULT CHTRichEditCtrl::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_ERASEBKGND:
if (m_bNoPaint)
return TRUE;
case WM_PAINT:
if (m_bNoPaint)
return TRUE;
}
return CRichEditCtrl::WindowProc(message, wParam, lParam);
}
void CHTRichEditCtrl::OnSize(UINT nType, int cx, int cy)
{
// Use the 'ScrollInfo' only, if there is a scrollbar available, otherwise we would
// use a scrollinfo which points to the top and we would thus stay at the top.
bool bAtEndOfScroll;
SCROLLINFO si;
si.cbSize = sizeof si;
si.fMask = SIF_ALL;
if ((GetStyle() & WS_VSCROLL) && GetScrollInfo(SB_VERT, &si))
bAtEndOfScroll = (si.nPos >= (int)(si.nMax - si.nPage));
else
bAtEndOfScroll = true;
CRichEditCtrl::OnSize(nType, cx, cy);
if (bAtEndOfScroll)
ScrollToLastLine();
}
COLORREF GetLogLineColor(UINT eMsgType)
{
if (eMsgType == LOG_ERROR)
return thePrefs.m_crLogError;
if (eMsgType == LOG_WARNING)
return thePrefs.m_crLogWarning;
if (eMsgType == LOG_SUCCESS)
return thePrefs.m_crLogSuccess;
ASSERT( eMsgType == LOG_INFO );
return CLR_DEFAULT;
}
void CHTRichEditCtrl::FlushBuffer()
{
if (m_astrBuff.GetSize() > 0) // flush buffer
{
for (int i = 0; i < m_astrBuff.GetSize(); i++)
{
const CString& rstrLine = m_astrBuff[i];
if (!rstrLine.IsEmpty())
{
if ((_TUCHAR)rstrLine[0] < 8)
AddLine((LPCTSTR)rstrLine + 1, rstrLine.GetLength() - 1, false, GetLogLineColor((_TUCHAR)rstrLine[0]));
else
AddLine((LPCTSTR)rstrLine, rstrLine.GetLength());
}
}
m_astrBuff.RemoveAll();
}
}
void CHTRichEditCtrl::AddEntry(LPCTSTR pszMsg)
{
CString strLine(pszMsg);
strLine += _T("\n");
if (m_hWnd == NULL){
m_astrBuff.Add(strLine);
}
else{
FlushBuffer();
AddLine(strLine, strLine.GetLength());
}
}
void CHTRichEditCtrl::Add(LPCTSTR pszMsg, int iLen)
{
if (m_hWnd == NULL){
CString strLine(pszMsg);
m_astrBuff.Add(strLine);
}
else{
FlushBuffer();
AddLine(pszMsg, iLen);
}
}
void CHTRichEditCtrl::AddTyped(LPCTSTR pszMsg, int iLen, UINT eMsgType)
{
if (m_hWnd == NULL)
{
CString strLine;
strLine = (TCHAR)(eMsgType & LOGMSGTYPEMASK);
strLine += pszMsg;
m_astrBuff.Add(strLine);
}
else
{
FlushBuffer();
AddLine(pszMsg, iLen, false, GetLogLineColor(eMsgType & LOGMSGTYPEMASK));
}
}
void CHTRichEditCtrl::AddLine(LPCTSTR pszMsg, int iLen, bool bLink, COLORREF cr, COLORREF bk, DWORD mask)
{
int iMsgLen = (iLen == -1) ? _tcslen(pszMsg) : iLen;
if (iMsgLen == 0)
return;
// Get Edit contents dimensions and cursor position
long lStartChar, lEndChar;
GetSel(lStartChar, lEndChar);
int iSize = GetWindowTextLength();
// Get Auto-AutoScroll state depending on scrollbar position
bool bAutoAutoScroll = m_bAutoScroll;
// Use the 'ScrollInfo' only, if there is a scrollbar available, otherwise we would
// use a scrollinfo which points to the top and we would thus stay at the top.
bool bScrollInfo = false;
SCROLLINFO si;
si.cbSize = sizeof si;
si.fMask = SIF_ALL;
if ((GetStyle() & WS_VSCROLL) && GetScrollInfo(SB_VERT, &si)) {
bScrollInfo = true;
// use some threshold to determine if at end or "very near" at end, unfortunately
// this is needed to get around richedit specific stuff. this threshold (pixels)
// should somewhat reflect the font size used in the control.
bAutoAutoScroll = (si.nPos >= (int)(si.nMax - si.nPage - 20));
}
// Reduce flicker by ignoring WM_PAINT
m_bNoPaint = true;
BOOL bIsVisible = IsWindowVisible();
if (bIsVisible)
SetRedraw(FALSE);
// Remember where we are
//int iFirstLine = !bAutoAutoScroll ? GetFirstVisibleLine() : 0;
POINT ptScrollPos;
SendMessage(EM_GETSCROLLPOS, 0, (LPARAM)&ptScrollPos);
// Select at the end of text and replace the selection
SafeAddLine(iSize, pszMsg, iMsgLen, lStartChar, lEndChar, bLink, cr, bk, mask);
SetSel(lStartChar, lEndChar); // Restore previous selection
if (bAutoAutoScroll)
ScrollToLastLine();
else {
//LineScroll(iFirstLine - GetFirstVisibleLine());
SendMessage(EM_SETSCROLLPOS, 0, (LPARAM)&ptScrollPos);
}
m_bNoPaint = false;
if (bIsVisible) {
SetRedraw();
Invalidate();
}
}
void CHTRichEditCtrl::OnEnErrspace()
{
m_bEnErrSpace = true;
}
void CHTRichEditCtrl::OnEnMaxtext()
{
m_bEnErrSpace = true;
}
void CHTRichEditCtrl::ScrollToLastLine(bool bForceLastLineAtBottom)
{
if (bForceLastLineAtBottom)
{
int iFirstVisible = GetFirstVisibleLine();
if (iFirstVisible > 0)
LineScroll(-iFirstVisible);
}
// WM_VSCROLL does not work correctly under Win98 (or older version of comctl.dll)
SendMessage(WM_VSCROLL, SB_BOTTOM);
if (afxIsWin95())
{
// older version of comctl.dll seem to need this to properly update the display
int iPos = GetScrollPos(SB_VERT);
SendMessage(WM_VSCROLL, MAKELONG(SB_THUMBPOSITION, iPos));
SendMessage(WM_VSCROLL, SB_ENDSCROLL);
}
}
void CHTRichEditCtrl::ScrollToFirstLine()
{
// WM_VSCROLL does not work correctly under Win98 (or older version of comctl.dll)
SendMessage(WM_VSCROLL, SB_TOP);
if (afxIsWin95())
{
// older version of comctl.dll seem to need this to properly update the display
int iPos = GetScrollPos(SB_VERT);
SendMessage(WM_VSCROLL, MAKELONG(SB_THUMBPOSITION, iPos));
SendMessage(WM_VSCROLL, SB_ENDSCROLL);
}
}
void CHTRichEditCtrl::AddString(int nPos, LPCTSTR pszString, bool bLink, COLORREF cr, COLORREF bk, DWORD mask)
{
bool bRestoreFormat = false;
m_bEnErrSpace = false;
SetSel(nPos, nPos);
if (bLink)
{
CHARFORMAT2 cf;
memset(&cf, 0, sizeof cf);
GetSelectionCharFormat(cf);
cf.dwMask |= CFM_LINK;
cf.dwEffects |= CFE_LINK;
SetSelectionCharFormat(cf);
}
else if (cr != CLR_DEFAULT || bk != CLR_DEFAULT || (mask & (CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE)) != 0)
{
CHARFORMAT2 cf;
memset(&cf, 0, sizeof(cf));
GetSelectionCharFormat(cf);
cf.dwMask |= CFM_COLOR;
if (cr == CLR_DEFAULT)
{
if (m_bDfltForeground)
cf.dwEffects |= CFE_AUTOCOLOR;
else {
ASSERT( m_crForeground != CLR_DEFAULT );
cf.dwEffects &= ~CFE_AUTOCOLOR;
cf.crTextColor = m_crForeground;
}
}
else
{
cf.dwEffects &= ~CFE_AUTOCOLOR;
cf.crTextColor = cr;
}
if (bk == CLR_DEFAULT)
{
// Background color is a little different than foreground color. Even if the
// background color is set to a non-standard value (e.g. via skin), the
// CFE_AUTOBACKCOLOR can be used to get this value. The usage of this flag instead
// of the explicit color has the advantage that on a change of the skin or on a
// change of the windows system colors, the control can display the colored
// text in the new color scheme better (e.g. the text is at least readable and
// not shown as black-on-black or white-on-white).
//if (m_bDfltBackground) {
cf.dwMask |= CFM_BACKCOLOR;
cf.dwEffects |= CFE_AUTOBACKCOLOR;
//}
//else {
// ASSERT( m_crBackground != CLR_DEFAULT );
// cf.dwEffects &= ~CFE_AUTOBACKCOLOR;
// cf.crBackColor = m_crBackground;
//}
}
else
{
cf.dwMask |= CFM_BACKCOLOR;
cf.dwEffects &= ~CFE_AUTOBACKCOLOR;
cf.crBackColor = bk;
}
cf.dwMask |= mask;
if (mask & CFM_BOLD)
cf.dwEffects |= CFE_BOLD;
else if (cf.dwEffects & CFE_BOLD)
cf.dwEffects ^= CFE_BOLD;
if (mask & CFM_ITALIC)
cf.dwEffects |= CFE_ITALIC;
else if (cf.dwEffects & CFE_ITALIC)
cf.dwEffects ^= CFE_ITALIC;
if (mask & CFM_UNDERLINE)
cf.dwEffects |= CFE_UNDERLINE;
else if (cf.dwEffects & CFE_UNDERLINE)
cf.dwEffects ^= CFE_UNDERLINE;
SetSelectionCharFormat(cf);
bRestoreFormat = true;
}
else if (m_bRestoreFormat)
{
SetSelectionCharFormat(m_cfDefault);
}
if (m_bEnableSmileys)
AddSmileys(pszString);
else
ReplaceSel(pszString);
m_bRestoreFormat = bRestoreFormat;
}
void CHTRichEditCtrl::SafeAddLine(int nPos, LPCTSTR pszLine, int iLen, long& lStartChar, long& lEndChar, bool bLink, COLORREF cr, COLORREF bk, DWORD mask)
{
// EN_ERRSPACE and EN_MAXTEXT are not working for rich edit control (at least not same as for standard control),
// need to explicitly check the log buffer limit..
int iCurSize = nPos;
if (iCurSize + iLen >= m_iLimitText)
{
bool bOldNoPaint = m_bNoPaint;
m_bNoPaint = true;
BOOL bIsVisible = IsWindowVisible();
if (bIsVisible)
SetRedraw(FALSE);
while (iCurSize > 0 && iCurSize + iLen > m_iLimitText)
{
// delete 1st line
int iLine0Len = LineLength(0) + 1; // add NL character
SetSel(0, iLine0Len);
ReplaceSel(_T(""));
// update any possible available selection
lStartChar -= iLine0Len;
if (lStartChar < 0)
lStartChar = 0;
lEndChar -= iLine0Len;
if (lEndChar < 0)
lEndChar = 0;
iCurSize = GetWindowTextLength();
}
m_bNoPaint = bOldNoPaint;
if (bIsVisible && !m_bNoPaint)
{
SetRedraw();
Invalidate();
}
}
AddString(nPos, pszLine, bLink, cr, bk, mask);
if (m_bEnErrSpace)
{
bool bOldNoPaint = m_bNoPaint;
m_bNoPaint = true;
BOOL bIsVisible = IsWindowVisible();
if (bIsVisible)
SetRedraw(FALSE);
// remove the first line as long as we are capable of adding the new line
int iSafetyCounter = 0;
while (m_bEnErrSpace && iSafetyCounter < 10)
{
// delete the previous partially added line
SetSel(nPos, -1);
ReplaceSel(_T(""));
// delete 1st line
int iLine0Len = LineLength(0) + 1; // add NL character
SetSel(0, iLine0Len);
ReplaceSel(_T(""));
// update any possible available selection
lStartChar -= iLine0Len;
if (lStartChar < 0)
lStartChar = 0;
lEndChar -= iLine0Len;
if (lEndChar < 0)
lEndChar = 0;
// add the new line again
nPos = GetWindowTextLength();
AddString(nPos, pszLine, bLink, cr, bk, mask);
if (m_bEnErrSpace && nPos == 0){
// should never happen: if we tried to add the line another time in the 1st line, there
// will be no chance to add the line at all -> avoid endless loop!
break;
}
iSafetyCounter++; // never ever create an endless loop!
}
m_bNoPaint = bOldNoPaint;
if (bIsVisible && !m_bNoPaint)
{
SetRedraw();
Invalidate();
}
}
}
void CHTRichEditCtrl::Reset()
{
m_astrBuff.RemoveAll();
SetRedraw(FALSE);
SetWindowText(_T(""));
SetRedraw();
Invalidate();
}
void CHTRichEditCtrl::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
{
if (point.x != -1 || point.y != -1) {
CRect rcClient;
GetClientRect(&rcClient);
ClientToScreen(&rcClient);
if (!rcClient.PtInRect(point)) {
Default();
return;
}
}
long lSelStart, lSelEnd;
GetSel(lSelStart, lSelEnd);
// ugly, simulate a left click to get around the text cursor problem when right clicking.
if (point.x != -1 && point.y != -1 && lSelStart == lSelEnd)
{
ASSERT( GetStyle() & ES_NOHIDESEL ); // this works only if ES_NOHIDESEL is set
CPoint ptMouse(point);
ScreenToClient(&ptMouse);
SendMessage(WM_LBUTTONDOWN, MK_LBUTTON, MAKELONG(ptMouse.x, ptMouse.y));
SendMessage(WM_LBUTTONUP, MK_LBUTTON, MAKELONG(ptMouse.x, ptMouse.y));
}
int iTextLen = GetWindowTextLength();
CTitleMenu menu;
menu.CreatePopupMenu();
menu.AddMenuTitle(GetResString(IDS_LOGENTRY));
menu.AppendMenu(MF_STRING | (lSelEnd > lSelStart ? MF_ENABLED : MF_GRAYED), MP_COPYSELECTED, GetResString(IDS_COPY));
menu.AppendMenu(MF_SEPARATOR);
menu.AppendMenu(MF_STRING | (iTextLen > 0 ? MF_ENABLED : MF_GRAYED), MP_SELECTALL, GetResString(IDS_SELECTALL));
menu.AppendMenu(MF_STRING | (iTextLen > 0 ? MF_ENABLED : MF_GRAYED), MP_REMOVEALL , GetResString(IDS_PW_RESET));
menu.AppendMenu(MF_STRING | (iTextLen > 0 ? MF_ENABLED : MF_GRAYED), MP_SAVELOG, GetResString(IDS_SAVELOG) + _T("..."));
menu.AppendMenu(MF_SEPARATOR);
menu.AppendMenu(MF_STRING | (m_bAutoScroll ? MF_CHECKED : MF_UNCHECKED), MP_AUTOSCROLL, GetResString(IDS_AUTOSCROLL));
if (point.x == -1 && point.y == -1)
{
point.x = 16;
point.y = 32;
ClientToScreen(&point);
}
// Cheap workaround for the "Text cursor is showing while context menu is open" glitch. It could be solved properly
// with the RE's COM interface, but because the according messages are not routed with a unique control ID, it's not
// really useable (e.g. if there are more RE controls in one window). Would to envelope each RE window to get a unique ID..
m_bForceArrowCursor = true;
menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
m_bForceArrowCursor = false;
VERIFY( menu.DestroyMenu() );
}
BOOL CHTRichEditCtrl::OnCommand(WPARAM wParam, LPARAM /*lParam*/)
{
switch (wParam)
{
case MP_COPYSELECTED:
CopySelectedItems();
break;
case MP_SELECTALL:
SelectAllItems();
break;
case MP_REMOVEALL:
Reset();
break;
case MP_SAVELOG:
SaveLog();
break;
case MP_AUTOSCROLL:
m_bAutoScroll = !m_bAutoScroll;
break;
}
return TRUE;
}
bool CHTRichEditCtrl::SaveLog(LPCTSTR pszDefName)
{
bool bResult = false;
CFileDialog dlg(FALSE, _T("log"), pszDefName ? pszDefName : (LPCTSTR)m_strTitle, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Log Files (*.log)|*.log||"), this, 0);
if (dlg.DoModal() == IDOK)
{
FILE* fp = _tfsopen(dlg.GetPathName(), _T("wb"), _SH_DENYWR);
if (fp)
{
// write Unicode byte-order mark 0xFEFF
fputwc(0xFEFF, fp);
CString strText;
GetWindowText(strText);
fwrite(strText, sizeof(TCHAR), strText.GetLength(), fp);
if (ferror(fp)){
CString strError;
strError.Format(_T("Failed to write log file \"%s\" - %s"), dlg.GetPathName(), _tcserror(errno));
AfxMessageBox(strError, MB_ICONERROR);
}
else
bResult = true;
fclose(fp);
}
else{
CString strError;
strError.Format(_T("Failed to create log file \"%s\" - %s"), dlg.GetPathName(), _tcserror(errno));
AfxMessageBox(strError, MB_ICONERROR);
}
}
return bResult;
}
CString CHTRichEditCtrl::GetLastLogEntry()
{
CString strLog;
int iLastLine = GetLineCount() - 2;
if (iLastLine >= 0)
{
GetLine(iLastLine, strLog.GetBuffer(1024), 1024);
strLog.ReleaseBuffer();
}
return strLog;
}
CString CHTRichEditCtrl::GetAllLogEntries()
{
CString strLog;
GetWindowText(strLog);
return strLog;
}
void CHTRichEditCtrl::SelectAllItems()
{
SetSel(0, -1);
}
void CHTRichEditCtrl::CopySelectedItems()
{
Copy();
}
void CHTRichEditCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (nChar == 'A' && (GetKeyState(VK_CONTROL) & 0x8000))
{
//////////////////////////////////////////////////////////////////
// Ctrl+A: Select all items
SelectAllItems();
}
else if (nChar == 'C' && (GetKeyState(VK_CONTROL) & 0x8000))
{
//////////////////////////////////////////////////////////////////
// Ctrl+C: Copy listview items to clipboard
CopySelectedItems();
}
else if (nChar == VK_ESCAPE)
{
// dont minimize CHTRichEditCtrl
return ;
}
CRichEditCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
}
static const struct
{
LPCTSTR pszScheme;
int iLen;
} s_apszSchemes[] =
{
{ _T("ed2k://"), 7 },
{ _T("http://"), 7 },
{ _T("https://"), 8 },
{ _T("ftp://"), 6 },
{ _T("www."), 4 },
{ _T("ftp."), 4 },
{ _T("mailto:"), 7 }
};
void CHTRichEditCtrl::AppendText(const CString& sText)
{
LPCTSTR psz = sText;
LPCTSTR pszStart = psz;
while (*psz != _T('\0'))
{
bool bFoundScheme = false;
for (int i = 0; i < _countof(s_apszSchemes); i++)
{
if (_tcsncmp(psz, s_apszSchemes[i].pszScheme, s_apszSchemes[i].iLen) == 0)
{
// output everything before the URL
if (psz - pszStart > 0){
CString str(pszStart, psz - pszStart);
AddLine(str, str.GetLength());
}
// search next space or EOL
int iLen = _tcscspn(psz, _T(" \t\r\n"));
if (iLen == 0){
AddLine(psz, -1, true);
psz += _tcslen(psz);
}
else{
CString str(psz, iLen);
AddLine(str, str.GetLength(), true);
psz += iLen;
}
pszStart = psz;
bFoundScheme = true;
break;
}
}
if (!bFoundScheme)
psz = _tcsinc(psz);
}
if (*pszStart != _T('\0'))
AddLine(pszStart, -1);
}
void CHTRichEditCtrl::AppendHyperLink(const CString& sText, const CString& sTitle, const CString& sCommand, const CString& sDirectory)
{
UNREFERENCED_PARAMETER(sText);
UNREFERENCED_PARAMETER(sTitle);
UNREFERENCED_PARAMETER(sDirectory);
ASSERT( sText.IsEmpty() );
ASSERT( sTitle.IsEmpty() );
ASSERT( sDirectory.IsEmpty() );
AddLine(sCommand, sCommand.GetLength(), true);
}
void CHTRichEditCtrl::AppendColoredText(LPCTSTR pszText, COLORREF cr, COLORREF bk, DWORD mask)
{
AddLine(pszText, -1, false, cr, bk, mask);
}
void CHTRichEditCtrl::AppendKeyWord(const CString& str, COLORREF cr)
{
AppendColoredText(str, cr);
}
BOOL CHTRichEditCtrl::OnEnLink(NMHDR *pNMHDR, LRESULT *pResult)
{
BOOL bMsgHandled = FALSE;
*pResult = 0;
ENLINK* pEnLink = reinterpret_cast<ENLINK *>(pNMHDR);
if (pEnLink && pEnLink->msg == WM_LBUTTONDOWN)
{
CString strUrl;
GetTextRange(pEnLink->chrg.cpMin, pEnLink->chrg.cpMax, strUrl);
// check if that "URL" has a valid URL scheme. if it does not have, pass that notification up to the
// parent window which may interpret that "URL" in some other way.
for (int i = 0; i < _countof(s_apszSchemes); i++){
if (_tcsncmp(strUrl, s_apszSchemes[i].pszScheme, s_apszSchemes[i].iLen) == 0){
ShellExecute(NULL, NULL, strUrl, NULL, NULL, SW_SHOWDEFAULT);
*pResult = 1;
bMsgHandled = TRUE; // do not route this message to any parent
break;
}
}
}
return bMsgHandled;
}
CString CHTRichEditCtrl::GetText() const
{
CString strText;
GetWindowText(strText);
return strText;
}
void CHTRichEditCtrl::SetFont(CFont* pFont, BOOL bRedraw)
{
// Use the 'ScrollInfo' only, if there is a scrollbar available, otherwise we would
// use a scrollinfo which points to the top and we would thus stay at the top.
bool bAtEndOfScroll;
SCROLLINFO si;
si.cbSize = sizeof si;
si.fMask = SIF_ALL;
if ((GetStyle() & WS_VSCROLL) && GetScrollInfo(SB_VERT, &si))
bAtEndOfScroll = (si.nPos >= (int)(si.nMax - si.nPage));
else
bAtEndOfScroll = true;
LOGFONT lf = {0};
pFont->GetLogFont(&lf);
CHARFORMAT cf = {0};
cf.cbSize = sizeof cf;
cf.dwMask |= CFM_BOLD;
cf.dwEffects |= (lf.lfWeight == FW_BOLD) ? CFE_BOLD : 0;
cf.dwMask |= CFM_ITALIC;
cf.dwEffects |= (lf.lfItalic) ? CFE_ITALIC : 0;
cf.dwMask |= CFM_UNDERLINE;
cf.dwEffects |= (lf.lfUnderline) ? CFE_UNDERLINE : 0;
cf.dwMask |= CFM_STRIKEOUT;
cf.dwEffects |= (lf.lfStrikeOut) ? CFE_STRIKEOUT : 0;
cf.dwMask |= CFM_SIZE;
HDC hDC = ::GetDC(HWND_DESKTOP);
int iPointSize = -MulDiv(lf.lfHeight, 72, GetDeviceCaps(hDC, LOGPIXELSY));
cf.yHeight = iPointSize * 20;
::ReleaseDC(NULL, hDC);
cf.dwMask |= CFM_FACE;
cf.bPitchAndFamily = lf.lfPitchAndFamily;
_tcsncpy(cf.szFaceName, lf.lfFaceName, _countof(cf.szFaceName));
cf.szFaceName[_countof(cf.szFaceName) - 1] = _T('\0');
// although this should work correctly (according SDK) it may give false results (e.g. the "click here..." text
// which is shown in the server info window may not be entirely used as a hyperlink???)
// cf.dwMask |= CFM_CHARSET;
// cf.bCharSet = lf.lfCharSet;
cf.yOffset = 0;
VERIFY( SetDefaultCharFormat(cf) );
// copy everything except the color
m_cfDefault.dwMask = (cf.dwMask & ~CFM_COLOR) | (m_cfDefault.dwMask & CFM_COLOR);
m_cfDefault.dwEffects = cf.dwEffects;
m_cfDefault.yHeight = cf.yHeight;
m_cfDefault.yOffset = cf.yOffset;
//m_cfDefault.crTextColor = cf.crTextColor;
m_cfDefault.bCharSet = cf.bCharSet;
m_cfDefault.bPitchAndFamily = cf.bPitchAndFamily;
memcpy(m_cfDefault.szFaceName, cf.szFaceName, sizeof(m_cfDefault.szFaceName));
PurgeSmileyCaches();
if (bAtEndOfScroll)
ScrollToLastLine();
if (bRedraw)
{
Invalidate();
UpdateWindow();
}
}
CFont* CHTRichEditCtrl::GetFont() const
{
ASSERT(0);
return NULL;
}
void CHTRichEditCtrl::OnSysColorChange()
{
CRichEditCtrl::OnSysColorChange();
ApplySkin();
}
void CHTRichEditCtrl::ApplySkin()
{
if (!m_strSkinKey.IsEmpty())
{
// Use the 'ScrollInfo' only, if there is a scrollbar available, otherwise we would
// use a scrollinfo which points to the top and we would thus stay at the top.
bool bAtEndOfScroll;
SCROLLINFO si;
si.cbSize = sizeof si;
si.fMask = SIF_ALL;
if ((GetStyle() & WS_VSCROLL) && GetScrollInfo(SB_VERT, &si))
bAtEndOfScroll = (si.nPos >= (int)(si.nMax - si.nPage));
else
bAtEndOfScroll = true;
COLORREF cr;
if (theApp.LoadSkinColor(m_strSkinKey + _T("Fg"), cr)) {
m_bDfltForeground = false;
m_crForeground = cr;
}
else {
m_bDfltForeground = m_crDfltForeground == CLR_DEFAULT;
m_crForeground = m_bDfltForeground ? GetSysColor(COLOR_WINDOWTEXT) : m_crDfltForeground;
}
bool bSetCharFormat = false;
CHARFORMAT cf;
GetDefaultCharFormat(cf);
if (!m_bDfltForeground && (cf.dwEffects & CFE_AUTOCOLOR)) {
cf.dwEffects &= ~CFE_AUTOCOLOR;
bSetCharFormat = true;
}
else if (m_bDfltForeground && !(cf.dwEffects & CFE_AUTOCOLOR)) {
cf.dwEffects |= CFE_AUTOCOLOR;
bSetCharFormat = true;
}
if (bSetCharFormat) {
cf.dwMask |= CFM_COLOR;
cf.crTextColor = m_crForeground;
VERIFY( SetDefaultCharFormat(cf) );
VERIFY( GetSelectionCharFormat(m_cfDefault) );
}
if (theApp.LoadSkinColor(m_strSkinKey + _T("Bk"), cr)) {
m_bDfltBackground = false;
m_crBackground = cr;
SetBackgroundColor(FALSE, m_crBackground);
}
else {
m_bDfltBackground = m_crDfltBackground == CLR_DEFAULT;
m_crBackground = m_bDfltBackground ? GetSysColor(COLOR_WINDOW) : m_crDfltBackground;
SetBackgroundColor(m_bDfltBackground, m_crBackground);
}
if (bAtEndOfScroll)
ScrollToLastLine();
}
else
{
m_bDfltForeground = m_crDfltForeground == CLR_DEFAULT;
m_crForeground = m_bDfltForeground ? GetSysColor(COLOR_WINDOWTEXT) : m_crDfltForeground;
m_bDfltBackground = m_crDfltBackground == CLR_DEFAULT;
m_crBackground = m_bDfltBackground ? GetSysColor(COLOR_WINDOW) : m_crDfltBackground;
VERIFY( GetSelectionCharFormat(m_cfDefault) );
}
PurgeSmileyCaches();
}
BOOL CHTRichEditCtrl::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// Cheap workaround for the "Text cursor is showing while context menu is open" glitch. It could be solved properly
// with the RE's COM interface, but because the according messages are not routed with a unique control ID, it's not
// really useable (e.g. if there are more RE controls in one window). Would to envelope each RE window to get a unique ID..
if (m_bForceArrowCursor && m_hArrowCursor)
{
::SetCursor(m_hArrowCursor);
return TRUE;
}
return CRichEditCtrl::OnSetCursor(pWnd, nHitTest, message);
}
class CBitmapDataObject : public CCmdTarget
{
public: