-
Notifications
You must be signed in to change notification settings - Fork 1
/
XHTMLStatic.cpp
1577 lines (1374 loc) · 40.5 KB
/
XHTMLStatic.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
// XHTMLStatic.cpp Version 1.4
//
// Author: Hans Dietrich
// hdietrich@gmail.com
//
// History
// Version 1.4 - 2007 October 19
// - Fixed bug where text could be written outside of control's client
// rect, reported (with fix) by David Pritchard.
// - Expanded table of character entities.
// - Added VS2005 project.
// - Implemented memory DC to improve performance, suggested by conan.ks.
// - Switched from WM_TIMER to WM_MOUSEMOVE for display of hand cursor,
// suggested by rm2 and RichardC. This prevents hand cursor from
// appearing on overlapping windows.
//
// Version 1.3 - 2006 August 15
// - Added transparency support, suggested by Anna
// - Added support for WM_PRINT, suggested by beaus07
// - Added support for <center>, requested by several readers
// - Added support for tooltips for hyperlinks
// - Load hand cursor from IDC_HAND, suggested by kamnas
// - Fixed font object leak, reported by furbo
// - Fixed problem with SetWindowText() reported by Andro67;
// the background and text colors are no longer reset
// when SetWindowText() is called.
// - Fixed bug when control is hidden, reported by RichardC
//
// Version 1.2 - 2004 June 12
// - Changed APP: hyperlink to use HWND instead of GetParent();
// - Added wParam to XHTMLSTATIC_APP_COMMAND struct
// - Added function SetTextColor(LPCTSTR lpszColor)
// - Added function SetLogFont(const LOGFONT * pLogFont)
// - Added function SetWindowText() to call Init and RedrawWindow
// - Fixed bug with XNamedColors in handling of "255,0,0" style
// in SetColorFromString()
// - Fixed bug with descenders of large serif fonts
//
// Version 1.1 - 2004 May 20
// - Implemented SUB tag
// - Implemented SUP tag
// - Implemented BIG tag
// - Implemented SMALL tag
// - Implemented CODE tag
// - Implemented HR tag
// - Implemented APP: hyperlink
// - Implemented common character entities
// - Improved parsing performance
// - Bug fixes
//
// Version 1.0 - 2002 September 16
// - Initial public release
//
// Acknowledgements:
// Thanks to Charles Petzold for explaining how GetTextExtentPoint32()
// works, in his excellent "Programming Windows", Fifth Edition:
// http://www.amazon.com/gp/product/157231995X
//
// Thanks to Chris Maunder for showing how to set the cursor and receive
// mouse clicks for static controls, and for all the code that I used
// from his CHyperLink class:
// http://www.codeproject.com/miscctrl/hyperlink.asp
//
// License:
// This software is released into the public domain. You are free to use
// it in any way you like, except that you may not sell this source code.
//
// This software is provided "as is" with no expressed or implied warranty.
// I accept no liability for any damage or loss of business that this
// software may cause.
//
///////////////////////////////////////////////////////////////////////////////
#include "pch.h"
#include "XHTMLStatic.h"
#include "atlconv.h" // for Unicode conversion
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#pragma warning(disable : 4996) // disable bogus deprecation warning
#ifndef __noop
#if _MSC_VER < 1300
#define __noop ((void)0)
#endif
#endif
#undef TRACE
#define TRACE __noop
//=============================================================================
// if you want to see the TRACE output, uncomment this line:
//#include "XTrace.h"
//=============================================================================
//=============================================================================
// some common character entities
//=============================================================================
XHTMLSTATIC_CHAR_ENTITIES CXHTMLStatic::m_aCharEntities[] =
{
{ _T("&"), 0, _T('&') }, // ampersand
{ _T("•"), 0, _T('\x95') }, // bullet NOT IN MS SANS SERIF
{ _T("¢"), 0, _T('\xA2') }, // cent sign
{ _T("©"), 0, _T('\xA9') }, // copyright
{ _T("°"), 0, _T('\xB0') }, // degree sign
{ _T("€"), 0, _T('\x80') }, // euro sign
{ _T("½"), 0, _T('\xBD') }, // fraction one half
{ _T("¼"), 0, _T('\xBC') }, // fraction one quarter
{ _T(">"), 0, _T('>') }, // greater than
{ _T("¿"), 0, _T('\xBF') }, // inverted question mark
{ _T("<"), 0, _T('<') }, // less than
{ _T("µ"), 0, _T('\xB5') }, // micro sign
{ _T("·"), 0, _T('\xB7') }, // middle dot = Georgian comma
{ _T(" "), 0, _T(' ') }, // nonbreaking space
{ _T("¶"), 0, _T('\xB6') }, // pilcrow sign = paragraph sign
{ _T("±"), 0, _T('\xB1') }, // plus-minus sign
{ _T("£"), 0, _T('\xA3') }, // pound sign
{ _T("""), 0, _T('"') }, // quotation mark
{ _T("®"), 0, _T('\xAE') }, // registered trademark
{ _T("§"), 0, _T('\xA7') }, // section sign
{ _T("¹"), 0, _T('\xB9') }, // superscript one
{ _T("²"), 0, _T('\xB2') }, // superscript two
{ _T("×"), 0, _T('\xD7') }, // multiplication sign
{ _T("™"), 0, _T('\x99') }, // trademark NOT IN MS SANS SERIF
{ NULL, 0, 0 } // MUST BE LAST
};
//=============================================================================
BEGIN_MESSAGE_MAP(CXHTMLStatic, CStatic)
//=============================================================================
//{{AFX_MSG_MAP(CXHTMLStatic)
ON_WM_PAINT()
ON_WM_SETCURSOR()
ON_WM_DESTROY()
ON_WM_ERASEBKGND()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_PRINT, OnPrint)
ON_CONTROL_REFLECT(STN_CLICKED, OnClicked)
END_MESSAGE_MAP()
//=============================================================================
CXHTMLStatic::CXHTMLStatic()
//=============================================================================
{
memset(&m_lf, 0, sizeof(m_lf));
m_bRefresh = TRUE;
m_bLogFont = FALSE;
m_hLinkCursor = NULL;
m_paAppCommands = NULL;
m_nAppCommands = 0;
m_nLeftMargin = 0;
m_nRightMargin = 0;
m_bToolTip = FALSE; // TRUE = display tooltip
m_hMemDC = NULL;
m_hBitmap = NULL;
m_hOldBitmap = NULL;
m_AnchorRectPtrs.RemoveAll();
m_AnchorUrls.RemoveAll();
m_hLinkCursor = AfxGetApp()->LoadStandardCursor(IDC_HAND);
if (m_hLinkCursor == NULL) // Still no cursor handle -
// load the WinHelp hand cursor
{
// this probably necessary only for Win95
TRACE(_T("Loading hand cursor from winhlp32.exe\n"));
// This retrieves cursor #106 from winhlp32.exe, which is a hand pointer
// get hand cursor
CString strWndDir;
GetWindowsDirectory(strWndDir.GetBuffer(MAX_PATH), MAX_PATH);
strWndDir.ReleaseBuffer();
strWndDir += _T("\\winhlp32.exe");
HMODULE hModule = ::LoadLibrary(strWndDir);
if (hModule)
{
HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
if (hHandCursor)
m_hLinkCursor = CopyCursor(hHandCursor);
}
::FreeLibrary(hModule);
}
InitCharEntities();
ResetAll();
}
//=============================================================================
CXHTMLStatic::~CXHTMLStatic()
//=============================================================================
{
ResetAll();
if (m_hLinkCursor)
::DestroyCursor(m_hLinkCursor);
m_hLinkCursor = NULL;
if (m_hMemDC)
DeleteDC(m_hMemDC);
m_hMemDC = NULL;
}
//=============================================================================
BOOL CXHTMLStatic::PreTranslateMessage(MSG* pMsg)
//=============================================================================
{
if (m_bToolTip && ::IsWindow(m_ToolTip.m_hWnd))
m_ToolTip.RelayEvent(pMsg);
return CStatic::PreTranslateMessage(pMsg);
}
//=============================================================================
void CXHTMLStatic::InitCharEntities()
//=============================================================================
{
for (int i = 0; m_aCharEntities[i].pszName != NULL; i++)
{
m_aCharEntities[i].cCode = (TCHAR) (i + 2); // don't use 0 or 1
}
}
//=============================================================================
TCHAR CXHTMLStatic::GetCharEntity(TCHAR cCode)
//=============================================================================
{
TCHAR c = _T(' ');
for (int i = 0; m_aCharEntities[i].pszName != NULL; i++)
{
if (cCode == m_aCharEntities[i].cCode)
{
c = m_aCharEntities[i].cSymbol;
break;
}
}
return c;
}
//=============================================================================
void CXHTMLStatic::ResetAll()
//=============================================================================
{
TRACE(_T("in CXHTMLStatic::ResetAll\n"));
Reset();
m_crBackGround = ::GetSysColor(COLOR_WINDOW);
m_crText = ::GetSysColor(COLOR_WINDOWTEXT);
m_nLeftMargin = 0;
m_nRightMargin = 0;
int n = (int)m_AnchorRectPtrs.GetSize();
for (int i = 0; i < n; i++)
{
CRect *pRect = (CRect *) m_AnchorRectPtrs[i];
if (pRect)
delete pRect;
}
m_AnchorRectPtrs.RemoveAll();
m_AnchorUrls.RemoveAll();
if (m_paAppCommands)
delete [] m_paAppCommands;
m_paAppCommands = NULL;
m_nAppCommands = 0;
}
//=============================================================================
// Reset (now called by SetWindowText() instead of ResetAll())
void CXHTMLStatic::Reset()
//=============================================================================
{
TRACE(_T("in CXHTMLStatic::Reset\n"));
m_bRefresh = TRUE;
m_bUnderline = FALSE;
m_bBold = FALSE;
m_bItalic = FALSE;
m_bCenter = FALSE;
m_bStrikeThrough = FALSE;
m_bSubscript = FALSE;
m_bSuperscript = FALSE;
m_bHorizontalRule = FALSE;
m_nHorizontalRuleSize = 2;
m_bOnHyperlink = FALSE;
m_hPrevCursor = NULL;
m_bInAnchor = FALSE;
m_bGeneratedText = FALSE;
}
//=============================================================================
LRESULT CXHTMLStatic::OnPrint(WPARAM wParam, LPARAM /*lParam*/)
//=============================================================================
{
Draw((HDC) wParam);
return 0;
}
//=============================================================================
void CXHTMLStatic::OnPaint()
//=============================================================================
{
CPaintDC dc(this); // device context for painting
Draw(dc.m_ps.hdc);
}
//=============================================================================
void CXHTMLStatic::Draw(HDC hDC)
//=============================================================================
{
TRACE(_T("in CXHTMLStatic::Draw\n"));
ASSERT(hDC);
if (hDC == NULL)
return;
CRect rect;
GetClientRect(&rect);
int nRectWidth = rect.Width();
int nRectHeight = rect.Height();
if (m_bRefresh)
{
TRACE(_T("creating bitmap\n"));
if (m_hOldBitmap && m_hMemDC)
SelectObject(m_hMemDC, m_hOldBitmap);
m_hOldBitmap = NULL;
if (m_hBitmap)
DeleteObject(m_hBitmap);
m_hBitmap = NULL;
if (m_hMemDC)
DeleteDC(m_hMemDC);
m_hMemDC = CreateCompatibleDC(hDC);
ASSERT(m_hMemDC);
// create bitmap for entire client area
m_hBitmap = CreateCompatibleBitmap(hDC, nRectWidth, nRectHeight);
ASSERT(m_hBitmap);
m_hOldBitmap = (HBITMAP) SelectObject(m_hMemDC, m_hBitmap);
// copy background from parent
BitBlt(m_hMemDC, 0, 0, nRectWidth, nRectHeight, hDC, 0, 0, SRCCOPY);
m_bRefresh = FALSE;
}
else
{
TRACE(_T("restoring bitmap\n"));
// restore cached bitmap
ASSERT(m_hMemDC);
ASSERT(m_hBitmap);
BitBlt(hDC, 0, 0, nRectWidth, nRectHeight, m_hMemDC, 0, 0, SRCCOPY);
return;
}
int nAnchors = (int)m_AnchorRectPtrs.GetSize();
int i = 0;
for (i = 0; i < nAnchors; i++)
{
CRect *pRect = (CRect *) m_AnchorRectPtrs[i];
if (pRect)
delete pRect;
if (m_bToolTip && m_ToolTip.GetToolCount())
{
TRACE(_T("deleting tool %d -----\n"), i+1);
m_ToolTip.DelTool(this, i+1);
}
}
m_AnchorRectPtrs.RemoveAll();
m_AnchorUrls.RemoveAll();
int nToolId = 1; // next tool id
// get text from control
CString strText = _T("");
GetWindowText(strText);
// replace character entity names with codes
TCHAR ent[3] = { 0 };
ent[0] = _T('\001'); // each entity name is replaced with a two-character
// code that begins with \001
for (i = 0; m_aCharEntities[i].pszName != NULL; i++)
{
ent[1] = m_aCharEntities[i].cCode;
strText.Replace(m_aCharEntities[i].pszName, ent);
}
CString str1 = _T("");
int index = 0;
// set text and background colors
COLORREF crText = m_crText;
COLORREF prev_crText = crText;
COLORREF crBackground = m_crBackGround;
COLORREF prev_crBackground = crBackground;
if (!(GetExStyle() & WS_EX_TRANSPARENT))
{
HBRUSH hbrush = CreateSolidBrush(m_crBackGround);
ASSERT(hbrush);
FillRect(m_hMemDC, &rect, hbrush);
if (hbrush)
DeleteObject(hbrush);
}
// nothing to do if no text or not visible
if (strText.IsEmpty() || !IsWindowVisible())
return;
int n = strText.GetLength();
// allow for margins
rect.left += m_nLeftMargin;
rect.right -= m_nRightMargin;
int nInitialXOffset = 0;//m_nLeftMargin;
m_yStart = rect.top;
LOGFONT lf = { 0 };
LOGFONT prev_lf = { 0 };
if (m_bLogFont)
{
memcpy(&lf, &m_lf, sizeof(lf));
}
else
{
CFont* cf = GetFont();
if (cf != NULL)
{
VERIFY(cf->GetObject(sizeof(lf), &lf));
}
else
{
HFONT hFont = (HFONT)::GetCurrentObject(hDC, OBJ_FONT); //+++1.1
if (hFont)
GetObject(hFont, sizeof(lf), &lf);
else
GetObject(GetStockObject(SYSTEM_FONT), sizeof(lf), &lf);
}
}
lf.lfWeight = m_bBold ? FW_BOLD : FW_NORMAL;
lf.lfUnderline = (BYTE) m_bUnderline;
lf.lfItalic = (BYTE) m_bItalic;
lf.lfStrikeOut = (BYTE) m_bStrikeThrough;
memcpy(&prev_lf, &lf, sizeof(lf));
// create initial font
HFONT hNewFont = CreateFontIndirect(&lf);
ASSERT(hNewFont);
HFONT hOldFont = (HFONT) SelectObject(m_hMemDC, hNewFont);
CString strAnchorText = _T("");
BOOL bSizeChange = FALSE;
TEXTMETRIC tm = { 0 };
GetTextMetrics(m_hMemDC, &tm);
while (n > 0)
{
///////////////////////////////////////////////////////////////////////
if (_tcsnicmp(strText, _T("<B>"), 3) == 0) // check for <b> or <B>
{
n -= 3;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
m_bBold++;// = TRUE;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("</B>"), 4) == 0) // check for </B>
{
n -= 4;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
if (m_bBold)
m_bBold--;// = FALSE;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("<I>"), 3) == 0) // check for <I>
{
n -= 3;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
m_bItalic++;// = TRUE;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("</I>"), 4) == 0) // check for </I>
{
n -= 4;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
if (m_bItalic)
m_bItalic--;// = FALSE;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("<U>"), 3) == 0) // check for <U>
{
n -= 3;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
m_bUnderline++;// = TRUE;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("</U>"), 4) == 0) // check for </U>
{
n -= 4;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
if (m_bUnderline)
m_bUnderline--;// = FALSE;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("<CENTER>"), 8) == 0) // check for <CENTER>
{
n -= 8;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
m_bCenter++;// = TRUE;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("</CENTER>"), 9) == 0) // check for </CENTER>
{
n -= 9;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
if (m_bCenter)
m_bCenter--;// = FALSE;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("<STRIKE>"), 8) == 0) // check for <STRIKE>
{
n -= 8;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
m_bStrikeThrough++;// = TRUE;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("</STRIKE>"), 9) == 0) // check for </STRIKE>
{
n -= 9;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
if (m_bStrikeThrough)
m_bStrikeThrough--;// = FALSE;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("<BIG>"), 5) == 0) // check for <BIG>
{
n -= 5;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
if (lf.lfHeight > 0)
lf.lfHeight++;
else
lf.lfHeight--;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("</BIG>"), 6) == 0) // check for </BIG>
{
n -= 6;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
if (lf.lfHeight > 0)
lf.lfHeight--;
else
lf.lfHeight++;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("<SMALL>"), 7) == 0) // check for <SMALL>
{
n -= 7;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
if (lf.lfHeight > 0)
lf.lfHeight--;
else
lf.lfHeight++;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("</SMALL>"), 8) == 0) // check for </SMALL>
{
n -= 8;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
if (lf.lfHeight > 0)
lf.lfHeight++;
else
lf.lfHeight--;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("<SUB>"), 5) == 0) // check for <SUB>
{
n -= 5;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
m_bSubscript++;// = TRUE;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("</SUB>"), 6) == 0) // check for </SUB>
{
n -= 6;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
if (m_bSubscript)
m_bSubscript--;// = FALSE;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("<SUP>"), 5) == 0) // check for <SUP>
{
n -= 5;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
m_bSuperscript++;// = TRUE;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("</SUP>"), 6) == 0) // check for </SUP>
{
n -= 6;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
if (m_bSuperscript)
m_bSuperscript--;// = FALSE;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("<FONT"), 5) == 0) // check for <FONT
{
index = strText.Find(_T('>'));
if (index != -1)
{
CString strAttributes = strText.Mid(5, index-5);
int m = strAttributes.GetLength();
strText = strText.Mid(index+1);
// loop to parse FONT attributes
while (m > 0)
{
// trim left whitespace
if ((strAttributes.GetLength() > 0) &&
(strAttributes[0] == _T(' ')))
{
m--;
strAttributes = strAttributes.Mid(1);
continue;
}
///////////////////////////////////////////////////////////
if (_tcsnicmp(strAttributes, _T("COLOR"), 5) == 0)
{
int index2 = strAttributes.Find(_T('"'));
if (index2 != -1)
{
m -= index2 + 1;
strAttributes = strAttributes.Mid(index2+1);
index2 = strAttributes.Find(_T('"'));
if (index2 != -1)
{
CString strColor = strAttributes.Left(index2);
CXNamedColors nc(strColor);
crText = nc.GetRGB();
strAttributes = strAttributes.Mid(index2+1);
m = strAttributes.GetLength();
}
}
else
break;
}
///////////////////////////////////////////////////////////
else if (_tcsnicmp(strAttributes, _T("BGCOLOR"), 7) == 0)
{
int index2 = strAttributes.Find(_T('"'));
if (index2 != -1)
{
m -= index2 + 1;
strAttributes = strAttributes.Mid(index2+1);
index2 = strAttributes.Find(_T('"'));
if (index2 != -1)
{
CString strBgColor = strAttributes.Left(index2);
CXNamedColors nc(strBgColor);
crBackground = nc.GetRGB();
strAttributes = strAttributes.Mid(index2+1);
m = strAttributes.GetLength();
}
}
else
break;
}
///////////////////////////////////////////////////////////
else if (_tcsnicmp(strAttributes, _T("FACE"), 4) == 0)
{
int index2 = strAttributes.Find(_T('"'));
if (index2 != -1)
{
m -= index2 + 1;
strAttributes = strAttributes.Mid(index2+1);
index2 = strAttributes.Find(_T('"'));
if (index2 != -1)
{
memset(lf.lfFaceName, 0, sizeof(lf.lfFaceName));
_tcsncpy(lf.lfFaceName, strAttributes, index2);
m -= index2 + 1;
if (m > 0)
strAttributes = strAttributes.Mid(index2+1);
else
strAttributes = _T("");
m = strAttributes.GetLength();
}
}
else
break;
}
///////////////////////////////////////////////////////////
else if (_tcsnicmp(strAttributes, _T("SIZE"), 4) == 0)
{
int index2 = strAttributes.Find(_T('"'));
if (index2 != -1)
{
m -= index2 + 1;
strAttributes = strAttributes.Mid(index2+1);
index2 = strAttributes.Find(_T('"'));
if (index2 != -1)
{
int nSize = 0;
nSize = _ttoi(strAttributes);
lf.lfHeight -= nSize;
bSizeChange = TRUE;
m -= index2 + 1;
if (m > 0)
strAttributes = strAttributes.Mid(index2+1);
else
strAttributes = _T("");
m = strAttributes.GetLength();
}
}
else
break;
}
else
{
while ((strAttributes.GetLength() > 0) &&
(strAttributes[0] != _T(' ')))
{
m--;
strAttributes = strAttributes.Mid(1);
}
}
}
n -= index + 1;
}
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("</FONT>"), 7) == 0) // check for </FONT>
{
n -= 7;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
crText = prev_crText;
crBackground = prev_crBackground;
memcpy(&lf, &prev_lf, sizeof(lf));
if (bSizeChange)
m_yStart += tm.tmDescent;
bSizeChange = FALSE;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("<CODE>"), 6) == 0) // check for <CODE>
{
n -= 6;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
_tcscpy(lf.lfFaceName, _T("Courier New"));
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("</CODE>"), 7) == 0) // check for </CODE>
{
n -= 7;
index = strText.Find(_T('>'));
if (index != -1)
strText = strText.Mid(index+1);
memcpy(&lf, &prev_lf, sizeof(lf));
continue;
}
///////////////////////////////////////////////////////////////////////
// <a href=www.xyz.com>XYZ Web Site</a>
else if (_tcsnicmp(strText, _T("<A HREF="), 8) == 0) // check for <A HREF=
{
index = strText.Find(_T('>'));
if (index != -1)
{
strAnchorText = strText.Mid(9, index-10);
strText = strText.Mid(index+1);
n = strText.GetLength();
m_bInAnchor = TRUE;
continue;
}
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("</A>"), 4) == 0) // check for </A>
{
strText = strText.Mid(4);
n -= 4;
m_bInAnchor = FALSE;
continue;
}
///////////////////////////////////////////////////////////////////////
else if (_tcsnicmp(strText, _T("<HR"), 3) == 0) // check for <HR>
{
index = strText.Find(_T('>'));
if (index != -1)
{
CString strAttributes = strText.Mid(3); //, index-3);
int m = strAttributes.GetLength();
strText = strText.Mid(index+1);
// loop to parse attributes
while (m > 0)
{
// trim left whitespace
if ((strAttributes.GetLength() > 0) &&
(strAttributes[0] == _T(' ')))
{
m--;
strAttributes = strAttributes.Mid(1);
continue;
}
///////////////////////////////////////////////////////////
if (_tcsnicmp(strAttributes, _T("SIZE"), 4) == 0)
{
int index2 = strAttributes.Find(_T('='));
if (index2 != -1)
{
m -= index2 + 1;
strAttributes = strAttributes.Mid(index2+1);
index2 = strAttributes.Find(_T('>'));
if (index2 != -1)
{
CString strSize = strAttributes.Left(index2);
m_nHorizontalRuleSize = _ttoi(strSize);
strAttributes = strAttributes.Mid(index2+1);
m = strAttributes.GetLength();
}
}
else
break;
}
else
{
while ((strAttributes.GetLength() > 0) &&
(strAttributes[0] != _T(' ')))
{
m--;
strAttributes = strAttributes.Mid(1);
}
}
}
n -= index + 1;
}
m_bHorizontalRule++;// = TRUE;
str1 = _T("\r\n");
m_bGeneratedText = TRUE;
}
///////////////////////////////////////////////////////////////////////
// <br> or \r\n or plain text
else
{
str1 = strText;
index = str1.Find(_T('<'));
if (index != -1)
{
if (_tcsnicmp(strText, _T("<BR>"), 4) == 0) // check for <BR>
{
n -= 4;
str1 = _T("\r\n");
m_bGeneratedText = TRUE;
strText = strText.Mid(4);
}
else
{
str1 = strText.Left(index);
if (str1.GetLength() <= 0)
{
if (strText.GetLength() != 0)
{
str1 = strText[0];
index = 1;
n -= 1;
}
}
strText = strText.Mid(index);
}
}
else
{
str1 = strText;
strText = _T("");
}
}
lf.lfWeight = m_bBold ? FW_BOLD : FW_NORMAL;
lf.lfUnderline = (BYTE) m_bUnderline;
lf.lfItalic = (BYTE) m_bItalic;
lf.lfStrikeOut = (BYTE) m_bStrikeThrough;
// update font
if (hOldFont)
SelectObject(m_hMemDC, hOldFont);
if (hNewFont)
DeleteObject(hNewFont);
hNewFont = CreateFontIndirect(&lf);
ASSERT(hNewFont);
hOldFont = (HFONT) SelectObject(m_hMemDC, hNewFont);
::SetTextColor(m_hMemDC, crText);
if (GetExStyle() & WS_EX_TRANSPARENT)
::SetBkMode(m_hMemDC, TRANSPARENT);
else
::SetBkColor(m_hMemDC, crBackground);
GetTextMetrics(m_hMemDC, &tm);
int nBaselineAdjust = tm.tmAscent / 2;