-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreditsCtrl.cpp
executable file
·1191 lines (1066 loc) · 38.4 KB
/
CreditsCtrl.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
//////////////////////////////////////////////////////////////////////
// Calques 3D - a 3D Dynamic Geometry Learning Environment
// Copyright (c) 1997-2007 Nicolas Van Labeke
//////////////////////////////////////////////////////////////////////
// This file is part of Calques 3D.
//
// Calques 3D 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.
//
// Calques 3D 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 Calques 3D; if not, write to The Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//////////////////////////////////////////////////////////////////////
/// @file CreditsCtrl.cpp
/// @brief Implementation of the CCreditsCtrl class.
///
/// @author Marc Richarme (http://www.codeproject.com/dialog/ccreditsctrl.asp)
/// $Date: 2007-11-25 13:37:20+00 $
/// $Revision: 1.8 $
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CreditsCtrl.h"
#include <afxtempl.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define CCREDITCTRL_TIMER1 100
// use transparent BitBlts if supported? ( UNTESTED!! )
//#define CCREDITCTRL_USE_TRANSPARENT_BITBLT
// stuff that _should_ have been defined in some header :-/
#ifndef C1_TRANSPARENT
#define C1_TRANSPARENT 0x0001
#endif
#ifndef CAPS1
#define CAPS1 94
#endif
#ifndef NEWTRANSPARENT
#define NEWTRANSPARENT 3
#endif
/////////////////////////////////////////////////////////////////////////////
// CCreditsCtrl
LPCTSTR CCreditsCtrl::m_lpszClassName = NULL;
CCreditsCtrl::CCreditsCtrl()
{
m_nTimerSpeed = 40;
m_nCurBitmapOffset = 0;
m_crInternalTransparentColor = RGB(255,0,255);
m_pBackgroundPaint = CCreditsCtrl::DrawBackground;
m_dwBackgroundPaintLParam = GetSysColor(COLOR_BTNFACE);//(DWORD)m_crBackgroundColor;
m_hLinkCursor = NULL;
m_hDefaultCursor = NULL;
m_bCanScroll = TRUE;
m_bIsScrolling = FALSE;
}
CCreditsCtrl::~CCreditsCtrl()
{
}
BEGIN_MESSAGE_MAP(CCreditsCtrl, CWnd)
//{{AFX_MSG_MAP(CCreditsCtrl)
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_SIZE()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CCreditsCtrl::Create(DWORD dwExStyle, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, UINT nDefaultCursorID, UINT nLinkCursorID)
{
m_hDefaultCursor = nDefaultCursorID == 0 ? NULL : AfxGetApp()->LoadCursor(nDefaultCursorID);
if(nLinkCursorID == 0)
SetDefaultLinkCursor();
else
m_hLinkCursor = AfxGetApp()->LoadCursor(nLinkCursorID);
// register window class & create CWnd object
if (m_lpszClassName == NULL)
m_lpszClassName = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW);
BOOL bResult = CreateEx(dwExStyle, m_lpszClassName, _T(""), dwStyle,
rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
pParentWnd->GetSafeHwnd(), (HMENU)nID, NULL );
// start scrolling
if(bResult)
SetTimer(CCREDITCTRL_TIMER1,m_nTimerSpeed*10,NULL);
return bResult;
}
BOOL CCreditsCtrl::Create(DWORD dwExStyle, DWORD dwStyle, UINT nPlaceholderID, CWnd* pParentWnd, UINT nID, UINT nDefaultCursorID, UINT nLinkCursorID)
{
// get rect from placeholder and call create with the found rect
RECT rect;
pParentWnd->GetDlgItem(nPlaceholderID)->GetWindowRect(&rect);
pParentWnd->ScreenToClient(&rect);
return Create(dwExStyle, dwStyle, rect, pParentWnd, nID, nDefaultCursorID, nLinkCursorID);
}
/////////////////////////////////////////////////////////////////////////////
// CCreditsCtrl message handlers
void CCreditsCtrl::OnPaint()
{
static BOOL bFirstDraw = TRUE;
CPaintDC dc(this); // device context for painting
// init memory DC
CDC memDC;
memDC.CreateCompatibleDC(&dc);
int nMemDCSave = memDC.SaveDC();
CBitmap memBmp;
memBmp.CreateCompatibleBitmap(&dc,m_rcClient.Width(),m_rcClient.Height());
memDC.SelectObject(&memBmp);
// draw backgorund
// if(m_pBackgroundPaint!=NULL)
/// (*m_pBackgroundPaint)(&memDC,m_rcClient,m_bIsScrolling||bFirstDraw,m_dwBackgroundPaintLParam);
globalData.DrawParentBackground (this, &memDC, m_rcClient);
// calculate hot rectagle position and save background at that location
CDC hotBgDC;
CBitmap hotbgBmp,*pOldHBgBmp;
CRect rcHotRect;
if(m_rcHotRect != CRect(0,0,0,0))
{
hotBgDC.CreateCompatibleDC(&memDC);
hotbgBmp.CreateCompatibleBitmap(&memDC,m_rcHotRect.Width(),m_rcHotRect.Height());
pOldHBgBmp = hotBgDC.SelectObject(&hotbgBmp);
if(m_nBitmapHeight <= m_rcClient.bottom)
rcHotRect = m_rcHotRect;
else if(m_nBitmapHeight-m_nCurBitmapOffset+m_rcHotRect.top < m_rcClient.bottom)
rcHotRect.SetRect(m_rcHotRect.left,m_nBitmapHeight-m_nCurBitmapOffset+m_rcHotRect.top,m_rcHotRect.right,m_nBitmapHeight-m_nCurBitmapOffset+m_rcHotRect.bottom);
else
rcHotRect.SetRect(m_rcHotRect.left,m_rcHotRect.top-m_nCurBitmapOffset,m_rcHotRect.right,m_rcHotRect.bottom-m_nCurBitmapOffset);
hotBgDC.BitBlt(0,0,m_rcHotRect.Width(),m_rcHotRect.Height(),&memDC,rcHotRect.left,rcHotRect.top,SRCCOPY);
}
// draw normal bitmap
if(m_nBitmapHeight <= m_rcClient.bottom)
{
CRect rect = m_rcClient;
rect.bottom = m_nBitmapHeight;
DrawTransparentBitmap(&m_bmpNormal,&memDC,m_crInternalTransparentColor,rect,rect);
} else
{
DrawTransparentBitmap(&m_bmpNormal,&memDC,m_crInternalTransparentColor,CRect(0,0,m_rcClient.right,min(m_nBitmapHeight-m_nCurBitmapOffset,m_rcClient.bottom)),CRect(0,m_nCurBitmapOffset,0,0/*the two last values are not taken into account by DrawTransparentBitmap anyway*/));
if(m_nBitmapHeight-m_nCurBitmapOffset < m_rcClient.bottom)
DrawTransparentBitmap(&m_bmpNormal,&memDC,m_crInternalTransparentColor,CRect(0,m_nBitmapHeight-m_nCurBitmapOffset,m_rcClient.right,m_rcClient.bottom),CRect(0,0,0,0/*the two last values are not taken into account by DrawTransparentBitmap anyway*/));
}
// draw hot rect onto generic background
if(m_rcHotRect != CRect(0,0,0,0))
{
memDC.BitBlt(rcHotRect.left,rcHotRect.top,rcHotRect.Width(),rcHotRect.Height(),&hotBgDC,0,0,SRCCOPY);
DrawTransparentBitmap(&m_bmpHot,&memDC,m_crInternalTransparentColor,rcHotRect,m_rcHotRect);
hotBgDC.SelectObject(pOldHBgBmp);
}
// copy memory DC to screen
dc.BitBlt(0,0,m_rcClient.Width(),m_rcClient.Height(),&memDC,0,0,SRCCOPY);
memDC.RestoreDC(nMemDCSave);
if(bFirstDraw)
bFirstDraw = FALSE;
}
CString CCreditsCtrl::SetDataString(LPCTSTR lpszNewString)
{
CString sOldString = m_sData;
m_sData = lpszNewString;
if(IsWindow(m_hWnd))
Initialize();
return sOldString;
}
CString CCreditsCtrl::SetDataString(UINT nStringResourceID)
{
CString sOldString = m_sData;
m_sData.LoadString(nStringResourceID);
if(IsWindow(m_hWnd))
Initialize();
return sOldString;
}
CString CCreditsCtrl::FormatDataString(LPCTSTR lpszFormat, ...)
{
ASSERT(AfxIsValidString(lpszFormat));
CString sOldString = m_sData; // store old string
// let CString do the formatting
va_list argList;
va_start(argList, lpszFormat);
m_sData.FormatV(lpszFormat, argList);
va_end(argList);
if(IsWindow(m_hWnd)) // Initialize bitmaps if we have already been Create()d
Initialize();
return sOldString;
}
CString CCreditsCtrl::FormatDataString(UINT nFormatID, ...)
{
CString strFormat;
VERIFY(strFormat.LoadString(nFormatID) != 0); // load resource string
CString sOldString = m_sData; // store old string
// let CString do the formatting
va_list argList;
va_start(argList, nFormatID);
m_sData.FormatV(strFormat, argList);
va_end(argList);
if(IsWindow(m_hWnd)) // Initialize bitmaps if we have already been Create()d
Initialize();
return sOldString;
}
CString CCreditsCtrl::GetDataString()
{
return m_sData;
}
void CCreditsCtrl::SetDefaultLinkCursor()
{
// following code is taken from Chris Maunders hyperlink control (http://www.codeproject.com) - tnx
if (m_hLinkCursor == NULL) // No cursor handle - load our own
{
// Get the windows directory
CString strWndDir;
GetWindowsDirectory(strWndDir.GetBuffer(MAX_PATH), MAX_PATH);
strWndDir.ReleaseBuffer();
strWndDir += _T("\\winhlp32.exe");
// This retrieves cursor #106 from winhlp32.exe, which is a hand pointer
HMODULE hModule = LoadLibrary(strWndDir);
if (hModule) {
HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
if (hHandCursor)
m_hLinkCursor = CopyCursor(hHandCursor);
}
FreeLibrary(hModule);
}
}
void CCreditsCtrl::OnTimer(UINT nIDEvent)
{
if(nIDEvent == CCREDITCTRL_TIMER1)
{
if(IsWindowVisible())
{
// increment bitmap offset
if(++m_nCurBitmapOffset > m_nBitmapHeight)
m_nCurBitmapOffset = 1;
// update cursor
CPoint point,pt;
GetCursorPos(&point);
pt = point;
ScreenToClient(&point);
if(m_rcClient.PtInRect(point) && WindowFromPoint(pt)==this)
{
CRect rect;
int n;
if((n = HitTest(point)) != -1)
{
rect = m_HotRects[n];
SetCursor(m_hLinkCursor);
}
else
{
rect = CRect(0,0,0,0);
SetCursor(m_hDefaultCursor);
}
if(rect != m_rcHotRect)
m_rcHotRect = rect;
}
// update window
Invalidate(FALSE);
UpdateWindow();
// set timer
SetTimer(CCREDITCTRL_TIMER1,m_nTimerSpeed,NULL);
}
} else
CWnd::OnTimer(nIDEvent);
}
void CCreditsCtrl::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
if(IsWindow(m_hWnd))
GetClientRect(m_rcClient);
Initialize();
}
void CCreditsCtrl::TransparentBlt(CDC *pSrcDC, CDC* pDestDC,COLORREF crTrans,const CRect& rcDest,const CRect& rcSrc)
{
int SaveDestDC = pDestDC->SaveDC();
int SaveSrcDC = pSrcDC->SaveDC();
#ifdef CCREDITCTRL_USE_TRANSPARENT_BITBLT // use transparent BitBlts if supported?
// Only attempt this if device supports functionality. ( untested!! )
if(pDestDC->GetDeviceCaps(CAPS1) & C1_TRANSPARENT)
{
// Special transparency background mode
pDestDC->SetBkMode(NEWTRANSPARENT);
pDestDC->SetBkColor(crTrans);
// Actual blt is a simple source copy; transparency is automatic.
pDestDC->BitBlt(rcDest.left, rcDest.top, rcDest.Width(), rcDest.Height(), pSrcDC, rcSrc.left, rcSrc.top, SRCCOPY);
} else // if driver doesn't support transparent BitBlts, do it the hard way
{
#endif
// initialize memory DC and monochrome mask DC
CDC tmpDC, maskDC;
CBitmap bmpTmp, bmpMask;
int SaveTmpDC, SaveMaskDC;
tmpDC.CreateCompatibleDC(pDestDC);
maskDC.CreateCompatibleDC(pDestDC);
SaveTmpDC = tmpDC.SaveDC();
SaveMaskDC = maskDC.SaveDC();
bmpTmp.CreateCompatibleBitmap(pDestDC,rcDest.Width(),rcDest.Height());
bmpMask.CreateBitmap(rcDest.Width(),rcDest.Height(),1,1,NULL);
tmpDC.SelectObject(&bmpTmp);
maskDC.SelectObject(&bmpMask);
// copy existing data from destination dc to memory dc
tmpDC.BitBlt(0,0,rcDest.Width(),rcDest.Height(),pDestDC,rcDest.left,rcDest.top,SRCCOPY);
// create mask
pSrcDC->SetBkColor(crTrans);
maskDC.BitBlt(0,0,rcDest.Width(),rcDest.Height(),pSrcDC,rcSrc.left,rcSrc.top,SRCCOPY);
// do some BitBlt magic
tmpDC.SetBkColor(RGB(255,255,255));
tmpDC.SetTextColor(RGB(0,0,0));
tmpDC.BitBlt(0,0,rcDest.Width(),rcDest.Height(),pSrcDC,rcSrc.left,rcSrc.top,SRCINVERT);
tmpDC.BitBlt(0,0,rcDest.Width(),rcDest.Height(),&maskDC,0,0,SRCAND);
tmpDC.BitBlt(0,0,rcDest.Width(),rcDest.Height(),pSrcDC,rcSrc.left,rcSrc.top,SRCINVERT);
// copy what we have in our memory DC to the destination DC
pDestDC->BitBlt(rcDest.left,rcDest.top,rcDest.Width(),rcDest.Height(),&tmpDC,0,0,SRCCOPY);
// clean up
tmpDC.RestoreDC(SaveTmpDC);
maskDC.RestoreDC(SaveMaskDC);
#ifdef CCREDITCTRL_USE_TRANSPARENT_BITBLT
}
#endif
pDestDC->RestoreDC(SaveDestDC);
pSrcDC->RestoreDC(SaveSrcDC);
}
void CCreditsCtrl::DrawTransparentBitmap(CBitmap *pBitmap, CDC* pDC,COLORREF crTrans,const CRect& rcDest,const CRect& rcSrc)
{
int SaveImageDC;
// initialize image DC
CDC imageDC;
imageDC.CreateCompatibleDC(pDC);
SaveImageDC = imageDC.SaveDC();
imageDC.SelectObject(pBitmap);
TransparentBlt(&imageDC,pDC,crTrans,rcDest,rcSrc);
// clean up
imageDC.RestoreDC(SaveImageDC);
}
void CCreditsCtrl::Initialize()
{
//// [Initialize] ///////////////////////////////////////
// //
// Create bitmaps and calc hot regions from m_sData //
// //
/////////////////////////////////////////////////////////
int nMaxHeight = 5000;
// initialize normal and hot DCs
CDC *pDC = GetDC();
CDC normalDC, hotDC;
normalDC.CreateCompatibleDC(pDC);
hotDC.CreateCompatibleDC(pDC);
int nSaveDCNormal = normalDC.SaveDC();
int nSaveDCHot = hotDC.SaveDC();
// initialize bitmaps
if(m_bmpNormal.m_hObject)
m_bmpNormal.DeleteObject();
m_bmpNormal.CreateCompatibleBitmap(pDC,m_rcClient.Width(),nMaxHeight);
if(m_bmpHot.m_hObject)
m_bmpHot.DeleteObject();
m_bmpHot.CreateCompatibleBitmap(pDC,m_rcClient.Width(),nMaxHeight);
// select bitmaps into DCs
normalDC.SelectObject(&m_bmpNormal);
hotDC.SelectObject(&m_bmpHot);
// fill with transparent color
normalDC.FillSolidRect(0,0,m_rcClient.right,nMaxHeight,m_crInternalTransparentColor);
hotDC.FillSolidRect(0,0,m_rcClient.right,nMaxHeight,m_crInternalTransparentColor);
CString sData = m_sData;
// substitute line break tags with newline characters
// sData.Remove('\n');
// sData.Replace("<br>","\n");
// sData.Replace("<p>","\n\n");
// make sure we get the last line displayed
sData += '\n';
// variables used for parsing
CList<font_attribs,font_attribs&> font_attribs_tree;
font_attribs fa;
fa.bBold = FALSE;
fa.bItalic = FALSE;
fa.bUnderline = FALSE;
fa.bStrikeout = FALSE;
fa.crBkColor = CLR_NONE;
fa.crColor = RGB(0,0,0);
fa.nSize = 12;
strcpy(fa.szName,"Arial");
font_attribs_tree.AddTail(fa); // default font
CList<general_attribs,general_attribs&> general_attribs_tree;
general_attribs ga;
ga.nAlign = 1;
ga.nVAlign = 1;
ga.nMaxWidth = m_rcClient.Width();
ga.nMaxHeight = nMaxHeight;
general_attribs_tree.AddTail(ga); // default alignment
font_attribs link;
BOOL bInsideTag = FALSE;
CString sCurTagName;
CString sCurElement;
CString sCurOption;
int nCurHPos = 0;
int nCurVPos = 0;
int nCurLineHeight = 0;
CArray<line_rect,line_rect&> arcLineRects; // list containg information about the elements in the current line. used for vertical alignment of these element at line break.
BOOL bIsLineEmpty = TRUE;
BOOL bIsOption = FALSE;
TCHAR cTmp;
COLORREF crHrColor;
int nHrWidth;
int nHrSize;
int nHrAlign;
CString sCurLink;
COLORREF crBitmap;
int nBitmapBorder;
CString sBitmap;
CDC lineDC;
lineDC.CreateCompatibleDC(&normalDC);
CBitmap lineBmp;
lineBmp.CreateCompatibleBitmap(&normalDC,ga.nMaxWidth,ga.nMaxHeight);
CBitmap *pOldBmp = lineDC.SelectObject(&lineBmp);
CDC hover_lineDC;
hover_lineDC.CreateCompatibleDC(&hotDC);
CBitmap hover_lineBmp;
hover_lineBmp.CreateCompatibleBitmap(&hotDC,ga.nMaxWidth,ga.nMaxHeight);
CBitmap *pOldHoverBmp = hover_lineDC.SelectObject(&hover_lineBmp);
// main parsing loop... processing character by character
// (don't even _try_ to understand what's going on here :)
for(int i = 0; i < sData.GetLength() && i >= 0; i++)
{
if(!bInsideTag)
{
if(sData[i] == '<')
{
if(sCurElement != "")
{
Parse_AppendText(&lineDC,&hover_lineDC,&nCurHPos,&nCurVPos,&nCurLineHeight,&arcLineRects,&general_attribs_tree.GetTail(),&font_attribs_tree.GetTail(),sCurElement, sCurLink, link);
bIsLineEmpty = FALSE;
}
sCurTagName = "";
sCurElement = "";
bInsideTag = TRUE;
continue;
}
if(sData[i] == '\n') // line break
{
if(bIsLineEmpty) // if line is empty add the height of a space with the current font
{
fa = font_attribs_tree.GetTail();
CFont font;
font.CreateFont(-fa.nSize,0,0,0,fa.bBold?FW_BOLD:0,fa.bItalic,fa.bUnderline,fa.bStrikeout,0,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,PROOF_QUALITY,DEFAULT_PITCH|FF_DONTCARE,&fa.szName[0]);
CFont *pOldFont = lineDC.SelectObject(&font);
CRect rect(0,0,ga.nMaxWidth,ga.nMaxHeight);
lineDC.DrawText(" ",rect,DT_CALCRECT);
lineDC.SelectObject(pOldFont);
nCurVPos += rect.Height();
} else
{
if(sCurElement != "")
Parse_AppendText(&lineDC,&hover_lineDC,&nCurHPos,&nCurVPos,&nCurLineHeight,&arcLineRects,&general_attribs_tree.GetTail(),&font_attribs_tree.GetTail(),sCurElement,sCurLink,link);
Parse_VAlignLine(&normalDC,&hotDC,&lineDC,&hover_lineDC,nCurHPos,nCurVPos,nCurLineHeight,&arcLineRects,&general_attribs_tree.GetTail());
nCurVPos += nCurLineHeight;
bIsLineEmpty = TRUE;
}
arcLineRects.RemoveAll();
nCurLineHeight = 0;
nCurHPos = 0;
sCurElement = "";
continue;
}
sCurElement += sData[i];
bIsLineEmpty = FALSE;
} else
{
if(sData[i] == '>')
{
if(sCurTagName == "font") // <font face="s" size="n" style="[-b|b][-i|i][-u|u][-s|s]" color="n,n,n" background="n,n,n">
{
font_attribs_tree.AddTail(fa);
general_attribs_tree.AddTail(ga);
} else if(sCurTagName == "" && sCurElement == "/font") // closing font tag.. revove the last attributes from the lists
{
if(font_attribs_tree.GetCount() > 1)
font_attribs_tree.RemoveTail();
if(general_attribs_tree.GetCount() > 1)
general_attribs_tree.RemoveTail();
} else if(sCurTagName == "" && sCurElement == "hr") // no parameters specified for the hr tag.. use the defaults
{
crHrColor = GetSysColor(COLOR_BTNSHADOW); // default color
nHrWidth = ga.nMaxWidth-100; // default width
nHrSize = 2; // default height
nHrAlign = 1; // center by default
sCurTagName = "hr";
}
if(sCurTagName == "hr") // wrap line is needed and draw rect
{
if(!bIsLineEmpty)
{
Parse_VAlignLine(&normalDC,&hotDC,&lineDC,&hover_lineDC,nCurHPos,nCurVPos,nCurLineHeight,&arcLineRects,&general_attribs_tree.GetTail());
nCurVPos += nCurLineHeight;
bIsLineEmpty = TRUE;
}
arcLineRects.RemoveAll();
nCurLineHeight = 0;
nCurHPos = 0;
CRect rect;
rect.left = nHrAlign == 0 ? 0 : (nHrAlign == 2 ? ga.nMaxWidth-nHrWidth : ga.nMaxWidth/2-nHrWidth/2);
rect.right = rect.left + nHrWidth;
rect.top = nCurVPos + 2;
rect.bottom = rect.top + nHrSize;
normalDC.FillSolidRect(rect,crHrColor);
nCurVPos += 4+nHrSize;
} else if(sCurTagName == "" && sCurElement== "/a" && sCurLink != "") // if we have an ending link tag AND valid link action and link region...
sCurLink = "";
else if(sCurTagName == "img" && sBitmap != "")
{
if(sBitmap[0]=='#') // only resource bitmaps allowed at this time
{
CBitmap bmp;
bmp.LoadBitmap(atoi(sBitmap.Mid(1)));
Parse_AppendBitmap(&lineDC,&hover_lineDC,&nCurHPos,&nCurVPos,&nCurLineHeight,&arcLineRects,&general_attribs_tree.GetTail(),&bmp, crBitmap, nBitmapBorder, sCurLink, link);
bIsLineEmpty = FALSE;
}
crBitmap = CLR_NONE;
nBitmapBorder = 0;
sBitmap = "";
} else if(sCurTagName == "br" || (sCurTagName == "" && sCurElement== "br")) // just substitute with newline character
{
sData.SetAt(i,'\n');
i--;
} else if(sCurTagName == "p" || (sCurTagName == "" && sCurElement== "p")) // just substitute with 2 newline characters
{
sData.SetAt(i,'\n');
sData.SetAt(i-1,'\n');
i-= 2;
}
sCurElement = "";
bInsideTag = FALSE;
continue;
}
if(sData[i] == ' ' && !bIsOption)
{
if(sCurElement != "")
{
if(sCurTagName == "")
{
sCurTagName = sCurElement;
sCurTagName.MakeLower();
if(sCurTagName == "font") // store latest font attributes. these are the ones that are modified by the font tags parameters
{
fa = font_attribs_tree.GetTail();
ga = general_attribs_tree.GetTail();
} else if(sCurTagName == "hr") // set default hr options...
{
crHrColor = GetSysColor(COLOR_BTNSHADOW);
nHrWidth = ga.nMaxWidth-10;
nHrSize = 2;
nHrAlign = 1;
} else if(sCurTagName == "a") // init link hot attributes
{
link = font_attribs_tree.GetTail();
link.crColor = 0xeeffffff;
link.crBkColor = 0xeeffffff;
link.bBold = -10;
link.bItalic = -10;
link.bUnderline = -10;
link.bStrikeout = -10;
link.nSize = 0;
link.szName[0] = '\0';
} else if(sCurTagName == "img")
{
nBitmapBorder = 2;
crBitmap = CLR_NONE;
sBitmap = "";
}
} else
{
sCurOption = sCurTagName;
sCurOption.MakeLower();
}
}
sCurElement = "";
continue;
}
if(sData[i] == '"' || sData[i] == '\'') // this happens when we have a new parameter value to parse
{
if(bIsOption && sData[i]==cTmp) // "sData[i]==cTmp" : closing (double)quote has to match opening quote
{
if(sCurTagName == "font") // parse font tag paramaters
{
if(sCurOption == "size") // font size
{
int nSize = atoi(sCurElement);
if(nSize > 0 && nSize < 2000) // let's be reasonable
fa.nSize = nSize;
} else if(sCurOption == "face") // font face
{
strcpy(fa.szName,sCurElement.Left(MAX_PATH-1));
} else if(sCurOption == "style") // font style (bold (b) ,italic (i) ,underline (u) ,strikeout (s) )
{
if(sCurElement.Find("-b")!=-1 || sCurElement.Find("-B")!=-1)
fa.bBold = FALSE;
else if(sCurElement.FindOneOf("bB")!=-1)
fa.bBold = TRUE;
if(sCurElement.Find("-i")!=-1 || sCurElement.Find("-I")!=-1)
fa.bItalic = FALSE;
else if(sCurElement.FindOneOf("iI")!=-1)
fa.bItalic = TRUE;
if(sCurElement.Find("-u")!=-1 || sCurElement.Find("-U")!=-1)
fa.bUnderline = FALSE;
else if(sCurElement.FindOneOf("uU")!=-1)
fa.bUnderline = TRUE;
if(sCurElement.Find("-s")!=-1 || sCurElement.Find("-S")!=-1)
fa.bStrikeout = FALSE;
else if(sCurElement.FindOneOf("sS")!=-1)
fa.bStrikeout = TRUE;
} else if(sCurOption == "color") // font color
StringToColor(sCurElement,fa.crColor);
else if(sCurOption == "background") // font background-color
StringToColor(sCurElement,fa.crBkColor);
else if(sCurOption == "align") // horisontal font alignment. here we change the "general_attribs"
{ // only the latest open font tag with this parameter takes effect at a line break!!
sCurElement.MakeLower();
if(sCurElement == "left")
ga.nAlign = 0;
else if(sCurElement == "center")
ga.nAlign = 1;
else if(sCurElement == "right")
ga.nAlign = 2;
} else if(sCurOption == "valign") // vertical font alignment. here we change the "general_attribs"
{
sCurElement.MakeLower();
if(sCurElement == "top")
ga.nVAlign = 0;
else if(sCurElement == "middle")
ga.nVAlign = 1;
else if(sCurElement == "bottom")
ga.nVAlign = 2;
}
} else if(sCurTagName == "a")
{
if(sCurOption == "href") // what to do
sCurLink = sCurElement;
else if(sCurOption == "size") // font size
{
int nSize = atoi(sCurElement);
if(nSize > 0 && nSize < 2000) // let's be reasonable
link.nSize = nSize;
} else if(sCurOption == "face") // font face
{
strcpy(link.szName,sCurElement.Left(MAX_PATH-1));
} else if(sCurOption == "style") // font style (bold (b) ,italic (i) ,underline (u) ,strikeout (s) )
{
if(sCurElement.Find("-b")!=-1 || sCurElement.Find("-B")!=-1)
link.bBold = FALSE;
else if(sCurElement.FindOneOf("bB")!=-1)
link.bBold = TRUE;
if(sCurElement.Find("-i")!=-1 || sCurElement.Find("-I")!=-1)
link.bItalic = FALSE;
else if(sCurElement.FindOneOf("iI")!=-1)
link.bItalic = TRUE;
if(sCurElement.Find("-u")!=-1 || sCurElement.Find("-U")!=-1)
link.bUnderline = FALSE;
else if(sCurElement.FindOneOf("uU")!=-1)
link.bUnderline = TRUE;
if(sCurElement.Find("-s")!=-1 || sCurElement.Find("-S")!=-1)
link.bStrikeout = FALSE;
else if(sCurElement.FindOneOf("sS")!=-1)
link.bStrikeout = TRUE;
} else if(sCurOption == "color") // font color
StringToColor(sCurElement,link.crColor);
else if(sCurOption == "background") // font background-color
StringToColor(sCurElement,link.crBkColor);
} else if(sCurTagName == "img") // image tag: <img src="#resourceID">
{
// TODO: alow usage of filenames in <img> tag
if(sCurOption == "src" && sCurElement != "")
sBitmap = sCurElement;
if(sCurOption == "color")
StringToColor(sCurElement,crBitmap);
if(sCurOption == "border" && sCurElement != "")
nBitmapBorder = atoi(sCurElement);
} else if(sCurTagName == "hr") // horisontal ruler
{
if(sCurElement != "")
{
if(sCurOption == "color") // color
StringToColor(sCurElement,crHrColor);
else if(sCurOption == "width") // width
nHrWidth = atoi(sCurElement);
else if(sCurOption == "size") // height
nHrSize = atoi(sCurElement);
else if(sCurOption == "align") // horz alignment
{
sCurElement.MakeLower();
if(sCurElement=="left")
nHrAlign = 0;
else if(sCurElement=="right")
nHrAlign = 2;
else
nHrAlign = 1;
}
}
} else if((sCurTagName == "vspace") && (sCurOption == "size")) // vertical space
{
if(!bIsLineEmpty) // insert linebreak only is line isn't empty
{
Parse_VAlignLine(&normalDC,&hotDC,&lineDC,&hover_lineDC,nCurHPos,nCurVPos,nCurLineHeight,&arcLineRects,&general_attribs_tree.GetTail());
nCurVPos += nCurLineHeight;
bIsLineEmpty = TRUE;
}
arcLineRects.RemoveAll();
nCurLineHeight = 0;
nCurHPos = 0;
nCurVPos += atoi(sCurElement); // add "size" parameters value to vertical offset
} else if((sCurTagName == "hspace") && (sCurOption == "size")) // horisontal space
nCurHPos += atoi(sCurElement); // add "size" parameters value to horisontal offset
sCurElement = "";
bIsOption = FALSE;
} else if(sData[i-1] == '=') // parameter is beginning
{
sCurOption = sCurElement;
sCurOption = sCurOption.Left(sCurOption.GetLength()-1); // remove trailing "=";
sCurOption.MakeLower();
sCurOption.TrimRight();
sCurElement = "";
cTmp = sData[i];
bIsOption = TRUE;
}
continue;
}
sCurElement += sData[i]; // append non-formatting-significant character to curent element
}
}
lineDC.SelectObject(pOldBmp);
hover_lineDC.SelectObject(pOldHoverBmp);
//... finished parsing
m_nBitmapHeight = nCurVPos;
// clean up
normalDC.RestoreDC(nSaveDCNormal);
hotDC.RestoreDC(nSaveDCHot);
}
void CCreditsCtrl::DrawBackground(CDC *pDC, RECT rect, BOOL bAnimate, DWORD lParam)
{
pDC->FillSolidRect(&rect,(COLORREF)lParam);
}
void CCreditsCtrl::SetDefaultBkColor(COLORREF crColor)
{
m_dwBackgroundPaintLParam = (DWORD)crColor; // default background color
}
void CCreditsCtrl::Parse_AppendText(CDC *pDC, CDC *pHoverDC, int *pnCurHPos, int *pnCurVPos, int *pnCurHeight, CArray<line_rect,line_rect&>* parcLineRects, general_attribs *pga, font_attribs *pfa, CString sText, CString sCurLink, font_attribs &link)
{
CRect rect(0,0,pga->nMaxWidth,pga->nMaxHeight);
CDC dc,hoverDC;
CBitmap hoverBmp,bmp,*pOldHBmp;
dc.CreateCompatibleDC(pDC);
CFont font,hover_font,*pOldHFont;
font.CreateFont(-pfa->nSize,0,0,0,pfa->bBold?FW_BOLD:0,pfa->bItalic,pfa->bUnderline,pfa->bStrikeout,0,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,PROOF_QUALITY,DEFAULT_PITCH|FF_DONTCARE,pfa->szName);
CFont *pOldFont = dc.SelectObject(&font);
dc.SetTextColor(pfa->crColor == CLR_NONE ? m_crInternalTransparentColor : pfa->crColor);
dc.SetBkColor(pfa->crBkColor == CLR_NONE ? m_crInternalTransparentColor : pfa->crBkColor);
dc.SetBkMode(OPAQUE);
dc.DrawText(sText,rect,DT_CALCRECT|DT_SINGLELINE);
if(sCurLink != "")
{
if(link.bBold == -10) link.bBold = pfa->bBold;
if(link.bItalic == -10) link.bItalic = pfa->bItalic;
if(link.bUnderline == -10) link.bUnderline = pfa->bUnderline;
if(link.bStrikeout == -10) link.bStrikeout = pfa->bStrikeout;
if(link.crColor == 0xeeffffff) link.crColor = pfa->crColor;
if(link.crBkColor == 0xeeffffff) link.crBkColor = pfa->crBkColor;
if(link.nSize == 0) link.nSize = pfa->nSize;
if(link.szName[0] == '\0') strcpy(link.szName,pfa->szName);
CRect rect2(0,0,pga->nMaxWidth,pga->nMaxHeight);
hoverDC.CreateCompatibleDC(pDC);
hover_font.CreateFont(-link.nSize,0,0,0,link.bBold?FW_BOLD:0,link.bItalic,link.bUnderline,link.bStrikeout,0,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,PROOF_QUALITY,DEFAULT_PITCH|FF_DONTCARE,link.szName);
pOldHFont = hoverDC.SelectObject(&hover_font);
hoverDC.DrawText(sText,rect2,DT_CALCRECT|DT_SINGLELINE);
if(rect.Width() < rect2.Width())
rect = rect2;
hoverBmp.CreateCompatibleBitmap(pDC,rect.right,rect.bottom);
pOldHBmp = hoverDC.SelectObject(&hoverBmp);
hoverDC.FillSolidRect(rect,m_crInternalTransparentColor);
hoverDC.SetTextColor(link.crColor == CLR_NONE ? m_crInternalTransparentColor : link.crColor);
hoverDC.SetBkColor(link.crBkColor == CLR_NONE ? m_crInternalTransparentColor : link.crBkColor);
hoverDC.SetBkMode(OPAQUE);
hoverDC.DrawText(sText,rect,DT_SINGLELINE);
}
bmp.CreateCompatibleBitmap(pDC,rect.right,rect.bottom);
CBitmap *pOldBmp = dc.SelectObject(&bmp);
dc.FillSolidRect(rect,m_crInternalTransparentColor);
dc.SetBkColor(pfa->crBkColor == CLR_NONE ? m_crInternalTransparentColor : pfa->crBkColor);
dc.DrawText(sText,rect,DT_SINGLELINE);
if(sCurLink != "")
{
Parse_AppendElement(pDC,pHoverDC,pnCurHPos,pnCurVPos,pnCurHeight,parcLineRects,pga,rect.Width(),rect.Height(),&dc,&hoverDC,sCurLink);
hoverDC.SelectObject(pOldHBmp);
hoverDC.SelectObject(pOldHFont);
}
else
Parse_AppendElement(pDC,pHoverDC,pnCurHPos,pnCurVPos,pnCurHeight,parcLineRects,pga,rect.Width(),rect.Height(),&dc,&dc,sCurLink);
// clean up
dc.SelectObject(pOldBmp);
dc.SelectObject(pOldFont);
}
void CCreditsCtrl::Parse_AppendBitmap(CDC *pDC, CDC *pHoverDC, int *pnCurHPos, int *pnCurVPos, int *pnCurHeight, CArray<line_rect,line_rect&>* parcLineRects, general_attribs *pga, CBitmap *pBitmap, COLORREF crBorder, int nBorder, CString sCurLink, font_attribs &link)
{
BITMAP bm;
pBitmap->GetBitmap(&bm);
CDC bmpDC;
bmpDC.CreateCompatibleDC(pDC);
CBitmap *pOldBmp1 = bmpDC.SelectObject(pBitmap);
CDC dc;
dc.CreateCompatibleDC(pDC);
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDC,bm.bmWidth+nBorder*2,bm.bmHeight+nBorder*2);
CBitmap *pOldBmp2 = dc.SelectObject(&bmp);
dc.FillSolidRect(0,0,bm.bmWidth+nBorder*2,bm.bmHeight+nBorder*2,crBorder==CLR_NONE?m_crInternalTransparentColor:crBorder);
dc.BitBlt(nBorder,nBorder,bm.bmWidth,bm.bmHeight,&bmpDC,0,0,SRCCOPY);
if(sCurLink == "")
Parse_AppendElement(pDC,pHoverDC,pnCurHPos,pnCurVPos,pnCurHeight,parcLineRects,pga,bm.bmWidth+nBorder*2,bm.bmHeight+nBorder*2,&dc,&dc,sCurLink);
else
{
CDC hoverDC;
hoverDC.CreateCompatibleDC(pDC);
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDC,bm.bmWidth+nBorder*2,bm.bmHeight+nBorder*2);
CBitmap *pOldBmp = hoverDC.SelectObject(&bmp);
hoverDC.FillSolidRect(0,0,bm.bmWidth+nBorder*2,bm.bmHeight+nBorder*2,link.crColor==CLR_NONE?m_crInternalTransparentColor:link.crColor==0xeeffffff?crBorder:link.crColor);
hoverDC.BitBlt(nBorder,nBorder,bm.bmWidth,bm.bmHeight,&bmpDC,0,0,SRCCOPY);
Parse_AppendElement(pDC,pHoverDC,pnCurHPos,pnCurVPos,pnCurHeight,parcLineRects,pga,bm.bmWidth+nBorder*2,bm.bmHeight+nBorder*2,&dc,&hoverDC,sCurLink);
hoverDC.SelectObject(pOldBmp);
}
// clean up
dc.SelectObject(pOldBmp2);
bmpDC.SelectObject(pOldBmp1);
}
void CCreditsCtrl::Parse_AppendElement(CDC *pDC, CDC *pHoverDC, int *pnCurHPos, int *pnCurVPos, int *pnCurHeight, CArray<line_rect,line_rect&>* parcLineRects, general_attribs *pga, int nElementWidth, int nElementHeight, CDC *pElementDC, CDC *pHoverElementDC, CString sCurLink)
{
if(*pnCurHeight < nElementHeight)
*pnCurHeight = nElementHeight;
CRect rect;
rect.left = *pnCurHPos;
rect.top = 0;
rect.right = rect.left+nElementWidth;
rect.bottom = nElementHeight;
line_rect lr;
lr.rcRect = rect;
lr.nVAlign = pga->nVAlign;
lr.sLink = sCurLink;
parcLineRects->Add(lr);
pDC->BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),pElementDC,0,0,SRCCOPY);
pHoverDC->BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),pHoverElementDC,0,0,SRCCOPY);
*pnCurHPos += nElementWidth;
}
void CCreditsCtrl::Parse_VAlignLine(CDC *pDestDC, CDC *pHoverDestDC, CDC *pLineDC, CDC *pHoverLineDC, int nCurHPos, int nCurVPos, int nCurHeight, CArray<line_rect,line_rect&>* parcLineRects, general_attribs *pga)
{
{
CArray<line_rect,line_rect&> LinkElements;
CRect rect;
CDC memDC;
memDC.CreateCompatibleDC(pDestDC);
CBitmap memBmp;
memBmp.CreateCompatibleBitmap(pDestDC,nCurHPos,nCurHeight);
CBitmap *pOldBmp = memDC.SelectObject(&memBmp);
memDC.FillSolidRect(0,0,nCurHPos,nCurHeight,m_crInternalTransparentColor);
for(int i = 0; i < parcLineRects->GetSize(); i++)
{
rect.left = (*parcLineRects)[i].rcRect.left;
// calculate elements vertical position
if((*parcLineRects)[i].nVAlign == 0) // top align
rect.top = 0;
else if((*parcLineRects)[i].nVAlign == 1) // middle align
rect.top = nCurHeight/2-(*parcLineRects)[i].rcRect.bottom/2;
else // bottom align
rect.top = nCurHeight - (*parcLineRects)[i].rcRect.bottom;
rect.bottom = rect.top + (*parcLineRects)[i].rcRect.bottom;
// don't touch horz alignment
rect.left = (*parcLineRects)[i].rcRect.left;
rect.right = (*parcLineRects)[i].rcRect.right;
// draw element
memDC.BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),pLineDC,(*parcLineRects)[i].rcRect.left,(*parcLineRects)[i].rcRect.top,SRCCOPY);
// add link to list(if necessary)
if((*parcLineRects)[i].sLink != "")
{
line_rect lr;
lr.sLink = (*parcLineRects)[i].sLink;
lr.rcRect = rect;
LinkElements.Add(lr);
}
}
rect.top = nCurVPos;
rect.bottom = rect.top + nCurHeight;
if(pga->nAlign == 0) // left align