This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
UIManager.cpp
2365 lines (2156 loc) · 78.2 KB
/
UIManager.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
#include "StdAfx.h"
#include <zmouse.h>
#include <GdiPlus.h>
#pragma comment( lib, "GdiPlus.lib" )
DECLARE_HANDLE(HZIP); // An HZIP identifies a zip file that has been opened
typedef DWORD ZRESULT;
#define OpenZip OpenZipU
#define CloseZip(hz) CloseZipU(hz)
extern HZIP OpenZipU(void *z,unsigned int len,DWORD flags);
extern ZRESULT CloseZipU(HZIP hz);
//女孩不哭 注:GdiPlus定义
// ULONG_PTR CPaintManagerUI::m_gdiplusToken;
// GdiplusStartupInput CPaintManagerUI::m_gdiplusStartupInput;
ULONG_PTR g_gdiplusToken;
Gdiplus::GdiplusStartupInput g_gdiplusStartupInput;
namespace DuiLib {
/////////////////////////////////////////////////////////////////////////////////////
//
//
static UINT MapKeyState()
{
UINT uState = 0;
if( ::GetKeyState(VK_CONTROL) < 0 ) uState |= MK_CONTROL;
if( ::GetKeyState(VK_RBUTTON) < 0 ) uState |= MK_LBUTTON;
if( ::GetKeyState(VK_LBUTTON) < 0 ) uState |= MK_RBUTTON;
if( ::GetKeyState(VK_SHIFT) < 0 ) uState |= MK_SHIFT;
if( ::GetKeyState(VK_MENU) < 0 ) uState |= MK_ALT;
return uState;
}
typedef struct tagFINDTABINFO
{
CControlUI* pFocus;
CControlUI* pLast;
bool bForward;
bool bNextIsIt;
} FINDTABINFO;
typedef struct tagFINDSHORTCUT
{
TCHAR ch;
bool bPickNext;
} FINDSHORTCUT;
typedef struct tagTIMERINFO
{
CControlUI* pSender;
UINT nLocalID;
HWND hWnd;
UINT uWinTimer;
bool bKilled;
} TIMERINFO;
/////////////////////////////////////////////////////////////////////////////////////
HPEN m_hUpdateRectPen = NULL;
HINSTANCE CPaintManagerUI::m_hInstance = NULL;
HINSTANCE CPaintManagerUI::m_hResourceInstance = NULL;
CDuiString CPaintManagerUI::m_pStrDefaultFontName;//added by cddjr at 05/18/2012
CDuiString CPaintManagerUI::m_pStrResourcePath;
CDuiString CPaintManagerUI::m_pStrResourceZip;
bool CPaintManagerUI::m_bCachedResourceZip = false;
HANDLE CPaintManagerUI::m_hResourceZip = NULL;
short CPaintManagerUI::m_H = 180;
short CPaintManagerUI::m_S = 100;
short CPaintManagerUI::m_L = 100;
CStdPtrArray CPaintManagerUI::m_aPreMessages;
CStdPtrArray CPaintManagerUI::m_aPlugins;
CPaintManagerUI::CPaintManagerUI() :
m_hWndPaint(NULL),
m_hDcPaint(NULL),
m_hDcOffscreen(NULL),
m_hDcBackground(NULL),
m_hbmpOffscreen(NULL),
m_hbmpBackground(NULL),
m_hwndTooltip(NULL),
m_bShowUpdateRect(false),
m_uTimerID(0x1000),
m_pRoot(NULL),
m_pFocus(NULL),
m_pEventHover(NULL),
m_pEventClick(NULL),
m_pEventKey(NULL),
m_pCapturedUI(NULL),
m_bFirstLayout(true),
m_bFocusNeeded(false),
m_bUpdateNeeded(false),
m_bMouseTracking(false),
m_bMouseCapture(false),
m_bOffscreenPaint(true),
m_bAlphaBackground(false),
m_bUsedVirtualWnd(false),
m_nOpacity(255),
m_pParentResourcePM(NULL)
{
m_dwDefaultDisabledColor = 0xFFA7A6AA;
m_dwDefaultFontColor = 0xFF000000;
m_dwDefaultLinkFontColor = 0xFF0000FF;
m_dwDefaultLinkHoverFontColor = 0xFFD3215F;
m_dwDefaultSelectedBkColor = 0xFFBAE4FF;
LOGFONT lf = { 0 };
::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
lf.lfCharSet = DEFAULT_CHARSET;
if (CPaintManagerUI::m_pStrDefaultFontName.GetLength()>0)
{
_tcscpy_s(lf.lfFaceName, LF_FACESIZE, CPaintManagerUI::m_pStrDefaultFontName.GetData());
}
HFONT hDefaultFont = ::CreateFontIndirect(&lf);
m_DefaultFontInfo.hFont = hDefaultFont;
m_DefaultFontInfo.sFontName = lf.lfFaceName;
m_DefaultFontInfo.iSize = -lf.lfHeight;
m_DefaultFontInfo.bBold = (lf.lfWeight >= FW_BOLD);
m_DefaultFontInfo.bUnderline = (lf.lfUnderline == TRUE);
m_DefaultFontInfo.bItalic = (lf.lfItalic == TRUE);
::ZeroMemory(&m_DefaultFontInfo.tm, sizeof(m_DefaultFontInfo.tm));
if( m_hUpdateRectPen == NULL ) {
m_hUpdateRectPen = ::CreatePen(PS_SOLID, 1, RGB(220, 0, 0));
// Boot Windows Common Controls (for the ToolTip control)
::InitCommonControls();
::LoadLibrary(_T("msimg32.dll"));
}
m_szMinWindow.cx = 0;
m_szMinWindow.cy = 0;
m_szMaxWindow.cx = 0;
m_szMaxWindow.cy = 0;
m_szInitWindowSize.cx = 0;
m_szInitWindowSize.cy = 0;
m_szRoundCorner.cx = m_szRoundCorner.cy = 0;
::ZeroMemory(&m_rcSizeBox, sizeof(m_rcSizeBox));
::ZeroMemory(&m_rcCaption, sizeof(m_rcCaption));
m_ptLastMousePos.x = m_ptLastMousePos.y = -1;
}
CPaintManagerUI::~CPaintManagerUI()
{
// Delete the control-tree structures
for( int i = 0; i < m_aDelayedCleanup.GetSize(); i++ ) delete static_cast<CControlUI*>(m_aDelayedCleanup[i]);
for( int i = 0; i < m_aAsyncNotify.GetSize(); i++ ) delete static_cast<TNotifyUI*>(m_aAsyncNotify[i]);
m_mNameHash.Resize(0);
delete m_pRoot;
::DeleteObject(m_DefaultFontInfo.hFont);
RemoveAllFonts();
RemoveAllImages();
RemoveAllDefaultAttributeList();
RemoveAllOptionGroups();
RemoveAllTimers();
// Reset other parts...
if( m_hwndTooltip != NULL ) ::DestroyWindow(m_hwndTooltip);
if( m_hDcOffscreen != NULL ) ::DeleteDC(m_hDcOffscreen);
if( m_hDcBackground != NULL ) ::DeleteDC(m_hDcBackground);
if( m_hbmpOffscreen != NULL ) ::DeleteObject(m_hbmpOffscreen);
if( m_hbmpBackground != NULL ) ::DeleteObject(m_hbmpBackground);
if( m_hDcPaint != NULL ) ::ReleaseDC(m_hWndPaint, m_hDcPaint);
m_aPreMessages.Remove(m_aPreMessages.Find(this));
}
void CPaintManagerUI::Init(HWND hWnd)
{
ASSERT(::IsWindow(hWnd));
// Remember the window context we came from
m_hWndPaint = hWnd;
m_hDcPaint = ::GetDC(hWnd);
// We'll want to filter messages globally too
m_aPreMessages.Add(this);
}
HINSTANCE CPaintManagerUI::GetInstance()
{
return m_hInstance;
}
CDuiString CPaintManagerUI::GetInstancePath()
{
if( m_hInstance == NULL ) return _T('\0');
TCHAR tszModule[MAX_PATH + 1] = { 0 };
::GetModuleFileName(m_hInstance, tszModule, MAX_PATH);
CDuiString sInstancePath = tszModule;
int pos = sInstancePath.ReverseFind(_T('\\'));
if( pos >= 0 ) sInstancePath = sInstancePath.Left(pos + 1);
return sInstancePath;
}
CDuiString CPaintManagerUI::GetCurrentPath()
{
TCHAR tszModule[MAX_PATH + 1] = { 0 };
::GetCurrentDirectory(MAX_PATH, tszModule);
return tszModule;
}
HINSTANCE CPaintManagerUI::GetResourceDll()
{
if( m_hResourceInstance == NULL ) return m_hInstance;
return m_hResourceInstance;
}
const CDuiString& CPaintManagerUI::GetResourcePath()
{
return m_pStrResourcePath;
}
const CDuiString& CPaintManagerUI::GetResourceZip()
{
return m_pStrResourceZip;
}
bool CPaintManagerUI::IsCachedResourceZip()
{
return m_bCachedResourceZip;
}
HANDLE CPaintManagerUI::GetResourceZipHandle()
{
return m_hResourceZip;
}
void CPaintManagerUI::SetInstance(HINSTANCE hInst)
{
m_hInstance = hInst;
}
void CPaintManagerUI::SetCurrentPath(LPCTSTR pStrPath)
{
::SetCurrentDirectory(pStrPath);
}
void CPaintManagerUI::SetResourceDll(HINSTANCE hInst)
{
m_hResourceInstance = hInst;
}
void CPaintManagerUI::SetResourcePath(LPCTSTR pStrPath)
{
m_pStrResourcePath = pStrPath;
if( m_pStrResourcePath.IsEmpty() ) return;
TCHAR cEnd = m_pStrResourcePath.GetAt(m_pStrResourcePath.GetLength() - 1);
if( cEnd != _T('\\') && cEnd != _T('/') ) m_pStrResourcePath += _T('\\');
}
void CPaintManagerUI::SetResourceZip(LPVOID pVoid, unsigned int len)
{
if( m_pStrResourceZip == _T("membuffer") ) return;
if( m_bCachedResourceZip && m_hResourceZip != NULL ) {
CloseZip((HZIP)m_hResourceZip);
m_hResourceZip = NULL;
}
m_pStrResourceZip = _T("membuffer");
m_bCachedResourceZip = true;
if( m_bCachedResourceZip )
m_hResourceZip = (HANDLE)OpenZip(pVoid, len, 3);
}
void CPaintManagerUI::SetResourceZip(LPCTSTR pStrPath, bool bCachedResourceZip)
{
if( m_pStrResourceZip == pStrPath && m_bCachedResourceZip == bCachedResourceZip ) return;
if( m_bCachedResourceZip && m_hResourceZip != NULL ) {
CloseZip((HZIP)m_hResourceZip);
m_hResourceZip = NULL;
}
m_pStrResourceZip = pStrPath;
m_bCachedResourceZip = bCachedResourceZip;
if( m_bCachedResourceZip ) {
CDuiString sFile = CPaintManagerUI::GetResourcePath();
sFile += CPaintManagerUI::GetResourceZip();
m_hResourceZip = (HANDLE)OpenZip((void*)sFile.GetData(), 0, 2);
}
}
void CPaintManagerUI::GetHSL(short* H, short* S, short* L)
{
*H = m_H;
*S = m_S;
*L = m_L;
}
void CPaintManagerUI::SetHSL(bool bUseHSL, short H, short S, short L)
{
if( H == m_H && S == m_S && L == m_L ) return;
m_H = CLAMP(H, 0, 360);
m_S = CLAMP(S, 0, 200);
m_L = CLAMP(L, 0, 200);
for( int i = 0; i < m_aPreMessages.GetSize(); i++ ) {
CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
if( pManager != NULL && pManager->GetRoot() != NULL )
pManager->GetRoot()->Invalidate();
}
}
void CPaintManagerUI::ReloadSkin()
{
for( int i = 0; i < m_aPreMessages.GetSize(); i++ ) {
CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
pManager->ReloadAllImages();
}
}
bool CPaintManagerUI::LoadPlugin(LPCTSTR pstrModuleName)
{
ASSERT( !::IsBadStringPtr(pstrModuleName,-1) || pstrModuleName == NULL );
if( pstrModuleName == NULL ) return false;
HMODULE hModule = ::LoadLibrary(pstrModuleName);
if( hModule != NULL ) {
LPCREATECONTROL lpCreateControl = (LPCREATECONTROL)::GetProcAddress(hModule, "CreateControl");
if( lpCreateControl != NULL ) {
if( m_aPlugins.Find(lpCreateControl) >= 0 ) return true;
m_aPlugins.Add(lpCreateControl);
return true;
}
}
return false;
}
CStdPtrArray* CPaintManagerUI::GetPlugins()
{
return &m_aPlugins;
}
bool CPaintManagerUI::StartupGdiPlus()
{
return Gdiplus::GdiplusStartup(&g_gdiplusToken, &g_gdiplusStartupInput, nullptr)==Gdiplus::Ok;
}
bool CPaintManagerUI::ShutdownGdiPlus()
{
Gdiplus::GdiplusShutdown(g_gdiplusToken);
g_gdiplusToken = 0;
return true;
}
HWND CPaintManagerUI::GetPaintWindow() const
{
return m_hWndPaint;
}
HWND CPaintManagerUI::GetTooltipWindow() const
{
return m_hwndTooltip;
}
HDC CPaintManagerUI::GetPaintDC() const
{
return m_hDcPaint;
}
POINT CPaintManagerUI::GetMousePos() const
{
return m_ptLastMousePos;
}
SIZE CPaintManagerUI::GetClientSize() const
{
RECT rcClient = { 0 };
::GetClientRect(m_hWndPaint, &rcClient);
return CSize(rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
}
SIZE CPaintManagerUI::GetInitSize()
{
return m_szInitWindowSize;
}
void CPaintManagerUI::SetInitSize(int cx, int cy)
{
m_szInitWindowSize.cx = cx;
m_szInitWindowSize.cy = cy;
if( m_pRoot == NULL && m_hWndPaint != NULL ) {
::SetWindowPos(m_hWndPaint, NULL, 0, 0, cx, cy, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
}
}
RECT& CPaintManagerUI::GetSizeBox()
{
return m_rcSizeBox;
}
void CPaintManagerUI::SetSizeBox(RECT& rcSizeBox)
{
m_rcSizeBox = rcSizeBox;
}
RECT& CPaintManagerUI::GetCaptionRect()
{
return m_rcCaption;
}
void CPaintManagerUI::SetCaptionRect(RECT& rcCaption)
{
m_rcCaption = rcCaption;
}
SIZE CPaintManagerUI::GetRoundCorner() const
{
return m_szRoundCorner;
}
void CPaintManagerUI::SetRoundCorner(int cx, int cy)
{
m_szRoundCorner.cx = cx;
m_szRoundCorner.cy = cy;
}
SIZE CPaintManagerUI::GetMinInfo() const
{
return m_szMinWindow;
}
void CPaintManagerUI::SetMinInfo(int cx, int cy)
{
ASSERT(cx>=0 && cy>=0);
m_szMinWindow.cx = cx;
m_szMinWindow.cy = cy;
}
SIZE CPaintManagerUI::GetMaxInfo() const
{
return m_szMaxWindow;
}
void CPaintManagerUI::SetMaxInfo(int cx, int cy)
{
ASSERT(cx>=0 && cy>=0);
m_szMaxWindow.cx = cx;
m_szMaxWindow.cy = cy;
}
int CPaintManagerUI::GetTransparent() const
{
return m_nOpacity;
}
void CPaintManagerUI::SetTransparent(int nOpacity)
{
if (nOpacity<0)
m_nOpacity = 0;
else if (nOpacity>255)
m_nOpacity = 255;
else
m_nOpacity = nOpacity;
if( m_hWndPaint != NULL ) {
typedef BOOL (__stdcall *PFUNCSETLAYEREDWINDOWATTR)(HWND, COLORREF, BYTE, DWORD);
PFUNCSETLAYEREDWINDOWATTR fSetLayeredWindowAttributes;
HMODULE hUser32 = ::GetModuleHandle(_T("User32.dll"));
// 女孩不哭 注:可以忽略吗?后面直接使用了(最后一行) @[C]
// if (hUser32)
// {
// fSetLayeredWindowAttributes =
// (PFUNCSETLAYEREDWINDOWATTR)::GetProcAddress(hUser32, "SetLayeredWindowAttributes");
// if( fSetLayeredWindowAttributes == NULL ) return;
// }
fSetLayeredWindowAttributes = (PFUNCSETLAYEREDWINDOWATTR)::GetProcAddress(hUser32, "SetLayeredWindowAttributes");
DWORD dwStyle = ::GetWindowLong(m_hWndPaint, GWL_EXSTYLE);
DWORD dwNewStyle = dwStyle;
if( nOpacity >= 0 && nOpacity < 256 ) dwNewStyle |= WS_EX_LAYERED;
else dwNewStyle &= ~WS_EX_LAYERED;
if(dwStyle != dwNewStyle) ::SetWindowLong(m_hWndPaint, GWL_EXSTYLE, dwNewStyle);
fSetLayeredWindowAttributes(m_hWndPaint, 0, nOpacity, LWA_ALPHA); //[C]
}
}
void CPaintManagerUI::SetBackgroundTransparent(bool bTrans)
{
m_bAlphaBackground = bTrans;
}
bool CPaintManagerUI::IsShowUpdateRect() const
{
return m_bShowUpdateRect;
}
void CPaintManagerUI::SetShowUpdateRect(bool show)
{
m_bShowUpdateRect = show;
}
bool CPaintManagerUI::PreMessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& /*lRes*/)
{
for( int i = 0; i < m_aPreMessageFilters.GetSize(); i++ )
{
bool bHandled = false;
LRESULT lResult = static_cast<IMessageFilterUI*>(m_aPreMessageFilters[i])->MessageHandler(uMsg, wParam, lParam, bHandled);
if( bHandled ) {
return true;
}
}
switch( uMsg ) {
case WM_KEYDOWN:
{
// Tabbing between controls
if( wParam == VK_TAB ) {
if( m_pFocus && m_pFocus->IsVisible() && m_pFocus->IsEnabled() ){
if(m_pFocus->IsWantTab()){
return false;
}
}
SetNextTabControl(::GetKeyState(VK_SHIFT) >= 0);
return true;
}
}
break;
case WM_SYSCHAR:
{
// Handle ALT-shortcut key-combinations
FINDSHORTCUT fs = { 0 };
fs.ch = toupper((int)wParam);
CControlUI* pControl = m_pRoot->FindControl(__FindControlFromShortcut, &fs, UIFIND_ENABLED | UIFIND_ME_FIRST | UIFIND_TOP_FIRST);
if( pControl != NULL ) {
pControl->SetFocus();
pControl->Activate();
return true;
}
}
break;
case WM_SYSKEYDOWN:
{
if( m_pFocus != NULL ) {
TEventUI event = { 0 };
event.Type = UIEVENT_SYSKEY;
event.chKey = (TCHAR)wParam;
event.ptMouse = m_ptLastMousePos;
event.wKeyState = MapKeyState();
event.dwTimestamp = ::GetTickCount();
m_pFocus->Event(event);
}
}
break;
}
return false;
}
bool CPaintManagerUI::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lRes)
{
//#ifdef _DEBUG
// switch( uMsg ) {
// case WM_NCPAINT:
// case WM_NCHITTEST:
// case WM_SETCURSOR:
// break;
// default:
// DUITRACE(_T("MSG: %-20s (%08ld)"), DUITRACEMSG(uMsg), ::GetTickCount());
// }
//#endif
// Not ready yet?
if( m_hWndPaint == NULL ) return false;
TNotifyUI* pMsg = NULL;
while( pMsg = static_cast<TNotifyUI*>(m_aAsyncNotify.GetAt(0)) ) {
m_aAsyncNotify.Remove(0);
if( pMsg->pSender != NULL ) {
if( pMsg->pSender->OnNotify ) pMsg->pSender->OnNotify(pMsg);
}
for( int j = 0; j < m_aNotifiers.GetSize(); j++ ) {
static_cast<INotifyUI*>(m_aNotifiers[j])->Notify(*pMsg);
}
delete pMsg;
}
// Cycle through listeners
for( int i = 0; i < m_aMessageFilters.GetSize(); i++ )
{
bool bHandled = false;
LRESULT lResult = static_cast<IMessageFilterUI*>(m_aMessageFilters[i])->MessageHandler(uMsg, wParam, lParam, bHandled);
if( bHandled ) {
lRes = lResult;
return true;
}
}
// Custom handling of events
switch( uMsg ) {
case WM_APP + 1:
{
for( int i = 0; i < m_aDelayedCleanup.GetSize(); i++ )
delete static_cast<CControlUI*>(m_aDelayedCleanup[i]);
m_aDelayedCleanup.Empty();
}
break;
case WM_CLOSE:
{
// Make sure all matching "closing" events are sent
TEventUI event = { 0 };
event.ptMouse = m_ptLastMousePos;
event.dwTimestamp = ::GetTickCount();
if( m_pEventHover != NULL ) {
event.Type = UIEVENT_MOUSELEAVE;
event.pSender = m_pEventHover;
m_pEventHover->Event(event);
}
if( m_pEventClick != NULL ) {
event.Type = UIEVENT_BUTTONUP;
event.pSender = m_pEventClick;
m_pEventClick->Event(event);
}
SetFocus(NULL);
// Hmmph, the usual Windows tricks to avoid
// focus loss...
HWND hwndParent = GetWindowOwner(m_hWndPaint);
if( hwndParent != NULL ) ::SetFocus(hwndParent);
}
break;
case WM_ERASEBKGND:
{
// We'll do the painting here...
lRes = 1;
}
return true;
case WM_PAINT:
{
// Should we paint?
RECT rcPaint = { 0 };
if( !::GetUpdateRect(m_hWndPaint, &rcPaint, FALSE) ) return true;
if( m_pRoot == NULL ) {
PAINTSTRUCT ps = { 0 };
::BeginPaint(m_hWndPaint, &ps);
::EndPaint(m_hWndPaint, &ps);
return true;
}
// Do we need to resize anything?
// This is the time where we layout the controls on the form.
// We delay this even from the WM_SIZE messages since resizing can be
// a very expensize operation.
if( m_bUpdateNeeded ) {
m_bUpdateNeeded = false;
RECT rcClient = { 0 };
::GetClientRect(m_hWndPaint, &rcClient);
if( !::IsRectEmpty(&rcClient) ) {
if( m_pRoot->IsUpdateNeeded() ) {
m_pRoot->SetPos(rcClient);
if( m_hDcOffscreen != NULL ) ::DeleteDC(m_hDcOffscreen);
if( m_hDcBackground != NULL ) ::DeleteDC(m_hDcBackground);
if( m_hbmpOffscreen != NULL ) ::DeleteObject(m_hbmpOffscreen);
if( m_hbmpBackground != NULL ) ::DeleteObject(m_hbmpBackground);
m_hDcOffscreen = NULL;
m_hDcBackground = NULL;
m_hbmpOffscreen = NULL;
m_hbmpBackground = NULL;
}
else {
CControlUI* pControl = NULL;
while( pControl = m_pRoot->FindControl(__FindControlFromUpdate, NULL, UIFIND_VISIBLE | UIFIND_ME_FIRST) ) {
pControl->SetPos( pControl->GetPos() );
}
}
// We'll want to notify the window when it is first initialized
// with the correct layout. The window form would take the time
// to submit swipes/animations.
if( m_bFirstLayout ) {
m_bFirstLayout = false;
SendNotify(m_pRoot, DUI_MSGTYPE_WINDOWINIT, 0, 0, false);
}
}
}
// Set focus to first control?
if( m_bFocusNeeded ) {
SetNextTabControl();
}
//
// Render screen
//
// Prepare offscreen bitmap?
if( m_bOffscreenPaint && m_hbmpOffscreen == NULL )
{
RECT rcClient = { 0 };
::GetClientRect(m_hWndPaint, &rcClient);
m_hDcOffscreen = ::CreateCompatibleDC(m_hDcPaint);
m_hbmpOffscreen = ::CreateCompatibleBitmap(m_hDcPaint, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
ASSERT(m_hDcOffscreen);
ASSERT(m_hbmpOffscreen);
}
// Begin Windows paint
PAINTSTRUCT ps = { 0 };
::BeginPaint(m_hWndPaint, &ps);
if( m_bOffscreenPaint )
{
HBITMAP hOldBitmap = (HBITMAP) ::SelectObject(m_hDcOffscreen, m_hbmpOffscreen);
int iSaveDC = ::SaveDC(m_hDcOffscreen);
if( m_bAlphaBackground ) {
if( m_hbmpBackground == NULL ) {
RECT rcClient = { 0 };
::GetClientRect(m_hWndPaint, &rcClient);
m_hDcBackground = ::CreateCompatibleDC(m_hDcPaint);;
m_hbmpBackground = ::CreateCompatibleBitmap(m_hDcPaint, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
ASSERT(m_hDcBackground);
ASSERT(m_hbmpBackground);
::SelectObject(m_hDcBackground, m_hbmpBackground);
::BitBlt(m_hDcBackground, ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right - ps.rcPaint.left,
ps.rcPaint.bottom - ps.rcPaint.top, ps.hdc, ps.rcPaint.left, ps.rcPaint.top, SRCCOPY);
}
else
::SelectObject(m_hDcBackground, m_hbmpBackground);
::BitBlt(m_hDcOffscreen, ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right - ps.rcPaint.left,
ps.rcPaint.bottom - ps.rcPaint.top, m_hDcBackground, ps.rcPaint.left, ps.rcPaint.top, SRCCOPY);
}
m_pRoot->DoPaint(m_hDcOffscreen, ps.rcPaint);
for( int i = 0; i < m_aPostPaintControls.GetSize(); i++ ) {
CControlUI* pPostPaintControl = static_cast<CControlUI*>(m_aPostPaintControls[i]);
pPostPaintControl->DoPostPaint(m_hDcOffscreen, ps.rcPaint);
}
::RestoreDC(m_hDcOffscreen, iSaveDC);
::BitBlt(ps.hdc, ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right - ps.rcPaint.left,
ps.rcPaint.bottom - ps.rcPaint.top, m_hDcOffscreen, ps.rcPaint.left, ps.rcPaint.top, SRCCOPY);
::SelectObject(m_hDcOffscreen, hOldBitmap);
if( m_bShowUpdateRect ) {
HPEN hOldPen = (HPEN)::SelectObject(ps.hdc, m_hUpdateRectPen);
::SelectObject(ps.hdc, ::GetStockObject(HOLLOW_BRUSH));
::Rectangle(ps.hdc, rcPaint.left, rcPaint.top, rcPaint.right, rcPaint.bottom);
::SelectObject(ps.hdc, hOldPen);
}
}
else
{
// A standard paint job
int iSaveDC = ::SaveDC(ps.hdc);
m_pRoot->DoPaint(ps.hdc, ps.rcPaint);
::RestoreDC(ps.hdc, iSaveDC);
}
// All Done!
::EndPaint(m_hWndPaint, &ps);
}
// If any of the painting requested a resize again, we'll need
// to invalidate the entire window once more.
if( m_bUpdateNeeded ) {
::InvalidateRect(m_hWndPaint, NULL, FALSE);
}
return true;
case WM_PRINTCLIENT:
{
RECT rcClient;
::GetClientRect(m_hWndPaint, &rcClient);
HDC hDC = (HDC) wParam;
int save = ::SaveDC(hDC);
m_pRoot->DoPaint(hDC, rcClient);
// Check for traversing children. The crux is that WM_PRINT will assume
// that the DC is positioned at frame coordinates and will paint the child
// control at the wrong position. We'll simulate the entire thing instead.
if( (lParam & PRF_CHILDREN) != 0 ) {
HWND hWndChild = ::GetWindow(m_hWndPaint, GW_CHILD);
while( hWndChild != NULL ) {
RECT rcPos = { 0 };
::GetWindowRect(hWndChild, &rcPos);
::MapWindowPoints(HWND_DESKTOP, m_hWndPaint, reinterpret_cast<LPPOINT>(&rcPos), 2);
::SetWindowOrgEx(hDC, -rcPos.left, -rcPos.top, NULL);
// NOTE: We use WM_PRINT here rather than the expected WM_PRINTCLIENT
// since the latter will not print the nonclient correctly for
// EDIT controls.
::SendMessage(hWndChild, WM_PRINT, wParam, lParam | PRF_NONCLIENT);
hWndChild = ::GetWindow(hWndChild, GW_HWNDNEXT);
}
}
::RestoreDC(hDC, save);
}
break;
case WM_GETMINMAXINFO:
{
LPMINMAXINFO lpMMI = (LPMINMAXINFO) lParam;
if( m_szMinWindow.cx > 0 ) lpMMI->ptMinTrackSize.x = m_szMinWindow.cx;
if( m_szMinWindow.cy > 0 ) lpMMI->ptMinTrackSize.y = m_szMinWindow.cy;
if( m_szMaxWindow.cx > 0 ) lpMMI->ptMaxTrackSize.x = m_szMaxWindow.cx;
if( m_szMaxWindow.cy > 0 ) lpMMI->ptMaxTrackSize.y = m_szMaxWindow.cy;
}
break;
case WM_SIZE:
{
if( m_pFocus != NULL ) {
TEventUI event = { 0 };
event.Type = UIEVENT_WINDOWSIZE;
event.pSender = m_pFocus;
event.dwTimestamp = ::GetTickCount();
m_pFocus->Event(event);
}
if( m_pRoot != NULL ) m_pRoot->NeedUpdate();
}
return true;
case WM_TIMER:
{
for( int i = 0; i < m_aTimers.GetSize(); i++ ) {
const TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i]);
if( pTimer->hWnd == m_hWndPaint && pTimer->uWinTimer == LOWORD(wParam) && pTimer->bKilled == false) {
TEventUI event = { 0 };
event.Type = UIEVENT_TIMER;
event.pSender = pTimer->pSender;
event.wParam = pTimer->nLocalID;
event.dwTimestamp = ::GetTickCount();
pTimer->pSender->Event(event);
break;
}
}
}
break;
case WM_MOUSEHOVER:
{
m_bMouseTracking = false;
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
CControlUI* pHover = FindControl(pt);
if( pHover == NULL ) break;
if(m_pCapturedUI && m_pCapturedUI!=pHover) break;
// Generate mouse hover event
if( m_pEventHover != NULL ) {
TEventUI event = { 0 };
event.ptMouse = pt;
event.Type = UIEVENT_MOUSEHOVER;
event.pSender = m_pEventHover;
event.dwTimestamp = ::GetTickCount();
m_pEventHover->Event(event);
}
// Create tooltip information
CDuiString sToolTip = pHover->GetToolTip();
if( sToolTip.IsEmpty() ) return true;
::ZeroMemory(&m_ToolTip, sizeof(TOOLINFO));
m_ToolTip.cbSize = sizeof(TOOLINFO);
m_ToolTip.uFlags = TTF_IDISHWND;
m_ToolTip.hwnd = m_hWndPaint;
m_ToolTip.uId = (UINT_PTR) m_hWndPaint;
m_ToolTip.hinst = m_hInstance;
m_ToolTip.lpszText = const_cast<LPTSTR>( (LPCTSTR) sToolTip );
m_ToolTip.rect = pHover->GetPos();
if( m_hwndTooltip == NULL ) {
m_hwndTooltip = ::CreateWindowEx(0, TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, m_hWndPaint, NULL, m_hInstance, NULL);
::SendMessage(m_hwndTooltip, TTM_ADDTOOL, 0, (LPARAM) &m_ToolTip);
}
::SendMessage( m_hwndTooltip,TTM_SETMAXTIPWIDTH,0, pHover->GetToolTipWidth());
::SendMessage(m_hwndTooltip, TTM_SETTOOLINFO, 0, (LPARAM) &m_ToolTip);
::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, TRUE, (LPARAM) &m_ToolTip);
}
return true;
case WM_MOUSELEAVE:
{
if( m_hwndTooltip != NULL ) ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, FALSE, (LPARAM) &m_ToolTip);
if( m_bMouseTracking ) ::SendMessage(m_hWndPaint, WM_MOUSEMOVE, 0, (LPARAM) -1);
m_bMouseTracking = false;
}
break;
case WM_MOUSEMOVE:
{
// Start tracking this entire window again...
if( !m_bMouseTracking ) {
TRACKMOUSEEVENT tme = { 0 };
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_HOVER | TME_LEAVE;
tme.hwndTrack = m_hWndPaint;
tme.dwHoverTime = m_hwndTooltip == NULL ? 400UL : (DWORD) ::SendMessage(m_hwndTooltip, TTM_GETDELAYTIME, TTDT_INITIAL, 0L);
_TrackMouseEvent(&tme);
m_bMouseTracking = true;
}
// Generate the appropriate mouse messages
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
m_ptLastMousePos = pt;
TEventUI event = { 0 };
if(m_pCapturedUI){
event.pSender = m_pCapturedUI;
event.ptMouse = pt;
event.Type = UIEVENT_MOUSEMOVE;
m_pCapturedUI->Event(event);
break;
}
CControlUI* pNewHover = FindControl(pt);
if( pNewHover != NULL && pNewHover->GetManager() != this ) break;
event.ptMouse = pt;
event.dwTimestamp = ::GetTickCount();
if( pNewHover != m_pEventHover && m_pEventHover != NULL ) {
event.Type = UIEVENT_MOUSELEAVE;
event.pSender = m_pEventHover;
m_pEventHover->Event(event);
m_pEventHover = NULL;
if( m_hwndTooltip != NULL ) ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, FALSE, (LPARAM) &m_ToolTip);
}
if( pNewHover != m_pEventHover && pNewHover != NULL ) {
event.Type = UIEVENT_MOUSEENTER;
event.pSender = pNewHover;
pNewHover->Event(event);
m_pEventHover = pNewHover;
}
if( m_pEventClick != NULL ) {
event.Type = UIEVENT_MOUSEMOVE;
event.pSender = m_pEventClick;
m_pEventClick->Event(event);
}
else if( pNewHover != NULL ) {
event.Type = UIEVENT_MOUSEMOVE;
event.pSender = pNewHover;
pNewHover->Event(event);
}
}
break;
case WM_LBUTTONDOWN:
{
// We alway set focus back to our app (this helps
// when Win32 child windows are placed on the dialog
// and we need to remove them on focus change).
::SetFocus(m_hWndPaint);
if(::GetCapture()) ::ReleaseCapture();
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
m_ptLastMousePos = pt;
CControlUI* pControl = FindControl(pt);
if( pControl == NULL ) break;
if( pControl->GetManager() != this ) break;
m_pEventClick = pControl;
pControl->SetFocus();
SetCapture();
TEventUI event = { 0 };
event.Type = UIEVENT_BUTTONDOWN;
event.pSender = pControl;
event.wParam = wParam;
event.lParam = lParam;
event.ptMouse = pt;
event.wKeyState = (WORD)wParam;
event.dwTimestamp = ::GetTickCount();
pControl->Event(event);
}
break;
case WM_LBUTTONDBLCLK:
{
::SetFocus(m_hWndPaint);
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
m_ptLastMousePos = pt;
CControlUI* pControl = FindControl(pt);
if( pControl == NULL ) break;
if( pControl->GetManager() != this ) break;
SetCapture();
TEventUI event = { 0 };
event.Type = UIEVENT_DBLCLICK;
event.pSender = pControl;
event.ptMouse = pt;
event.wKeyState = (WORD)wParam;
event.dwTimestamp = ::GetTickCount();
pControl->Event(event);
m_pEventClick = pControl;
}
break;
case WM_LBUTTONUP:
{
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
m_ptLastMousePos = pt;
if( m_pEventClick == NULL ) break;
TEventUI event = { 0 };
event.Type = UIEVENT_BUTTONUP;
event.pSender = m_pEventClick;
event.wParam = wParam;
event.lParam = lParam;
event.ptMouse = pt;
event.wKeyState = (WORD)wParam;
event.dwTimestamp = ::GetTickCount();
m_pEventClick->Event(event);
m_pEventClick = NULL;
ReleaseCapture();
}
break;
case WM_RBUTTONDOWN:
{
::SetFocus(m_hWndPaint);
if(::GetCapture()) ::ReleaseCapture();
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
m_ptLastMousePos = pt;
CControlUI* pControl = FindControl(pt);
if( pControl == NULL ) break;
if( pControl->GetManager() != this ) break;
pControl->SetFocus();
SetCapture();
TEventUI event = { 0 };
event.Type = UIEVENT_RBUTTONDOWN;
event.pSender = pControl;
event.wParam = wParam;
event.lParam = lParam;
event.ptMouse = pt;
event.wKeyState = (WORD)wParam;
event.dwTimestamp = ::GetTickCount();
pControl->Event(event);
m_pEventClick = pControl;
}
break;
case WM_RBUTTONUP:
{
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
m_ptLastMousePos = pt;
if( m_pEventClick == NULL ) break;
TEventUI event = { 0 };
event.Type = UIEVENT_RBUTTONUP;
event.pSender = m_pEventClick;
event.wParam = wParam;
event.lParam = lParam;
event.ptMouse = pt;
event.wKeyState = (WORD)wParam;
event.dwTimestamp = ::GetTickCount();