-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatlctrlw.h
3997 lines (3540 loc) · 111 KB
/
atlctrlw.h
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
// Windows Template Library - WTL version 10.0
// Copyright (C) Microsoft Corporation, WTL Team. All rights reserved.
//
// This file is a part of the Windows Template Library.
// The use and distribution terms for this software are covered by the
// Microsoft Public License (http://opensource.org/licenses/MS-PL)
// which can be found in the file MS-PL.txt at the root folder.
#ifndef __ATLCTRLW_H__
#define __ATLCTRLW_H__
#pragma once
#ifndef __ATLAPP_H__
#error atlctrlw.h requires atlapp.h to be included first
#endif
#ifndef __ATLCTRLS_H__
#error atlctrlw.h requires atlctrls.h to be included first
#endif
// Define _WTL_CMDBAR_VISTA_MENUS as 0 to exclude Vista menus support
#ifndef _WTL_CMDBAR_VISTA_MENUS
#define _WTL_CMDBAR_VISTA_MENUS 1
#endif
// Note: Define _WTL_CMDBAR_VISTA_STD_MENUBAR to use Vista standard menubar look with Vista menus
///////////////////////////////////////////////////////////////////////////////
// Classes in this file:
//
// CCommandBarCtrlImpl<T, TBase, TWinTraits>
// CCommandBarCtrl
// CMDICommandBarCtrlImpl<T, TBase, TWinTraits>
// CMDICommandBarCtrl
namespace WTL
{
///////////////////////////////////////////////////////////////////////////////
// Command Bars
// Window Styles:
#define CBRWS_TOP CCS_TOP
#define CBRWS_BOTTOM CCS_BOTTOM
#define CBRWS_NORESIZE CCS_NORESIZE
#define CBRWS_NOPARENTALIGN CCS_NOPARENTALIGN
#define CBRWS_NODIVIDER CCS_NODIVIDER
// Extended styles
#define CBR_EX_TRANSPARENT 0x00000001L
#define CBR_EX_SHAREMENU 0x00000002L
#define CBR_EX_ALTFOCUSMODE 0x00000004L
#define CBR_EX_TRACKALWAYS 0x00000008L
#define CBR_EX_NOVISTAMENUS 0x00000010L
// standard command bar styles
#define ATL_SIMPLE_CMDBAR_PANE_STYLE \
(WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | CBRWS_NODIVIDER | CBRWS_NORESIZE | CBRWS_NOPARENTALIGN)
// Messages - support chevrons for frame windows
#define CBRM_GETCMDBAR (WM_USER + 301) // returns command bar HWND
#define CBRM_GETMENU (WM_USER + 302) // returns loaded or attached menu
#define CBRM_TRACKPOPUPMENU (WM_USER + 303) // displays a popup menu
typedef struct tagCBRPOPUPMENU
{
int cbSize;
HMENU hMenu; // popup menu do display
UINT uFlags; // TPM_* flags for ::TrackPopupMenuEx
int x;
int y;
LPTPMPARAMS lptpm; // ptr to TPMPARAMS for ::TrackPopupMenuEx
} CBRPOPUPMENU, *LPCBRPOPUPMENU;
// helper class
template <class T>
class CSimpleStack : public ATL::CSimpleArray< T >
{
public:
BOOL Push(T t)
{
#ifdef _CMDBAR_EXTRA_TRACE
ATLTRACE2(atlTraceUI, 0, _T("CmdBar - STACK-PUSH (%8.8X) size = %i\n"), t, GetSize());
#endif
return this->Add(t);
}
T Pop()
{
int nLast = this->GetSize() - 1;
if(nLast < 0)
return NULL; // must be able to convert to NULL
T t = this->m_aT[nLast];
#ifdef _CMDBAR_EXTRA_TRACE
ATLTRACE2(atlTraceUI, 0, _T("CmdBar - STACK-POP (%8.8X) size = %i\n"), t, GetSize());
#endif
if(!this->RemoveAt(nLast))
return NULL;
return t;
}
T GetCurrent()
{
int nLast = this->GetSize() - 1;
if(nLast < 0)
return NULL; // must be able to convert to NULL
return this->m_aT[nLast];
}
};
///////////////////////////////////////////////////////////////////////////////
// CCommandBarCtrlBase - base class for the Command Bar implementation
class CCommandBarCtrlBase : public CToolBarCtrl
{
public:
struct _MsgHookData
{
HHOOK hMsgHook;
DWORD dwUsage;
_MsgHookData() : hMsgHook(NULL), dwUsage(0)
{ }
};
typedef ATL::CSimpleMap<DWORD, _MsgHookData*> CMsgHookMap;
static CMsgHookMap* s_pmapMsgHook;
static HHOOK s_hCreateHook;
static CCommandBarCtrlBase* s_pCurrentBar;
static bool s_bStaticInit;
CSimpleStack<HWND> m_stackMenuWnd;
CSimpleStack<HMENU> m_stackMenuHandle;
HWND m_hWndHook;
DWORD m_dwMagic;
CCommandBarCtrlBase() : m_hWndHook(NULL), m_dwMagic(1314)
{
// init static variables
if(!s_bStaticInit)
{
CStaticDataInitCriticalSectionLock lock;
if(FAILED(lock.Lock()))
{
ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CCommandBarCtrlBase::CCommandBarCtrlBase.\n"));
ATLASSERT(FALSE);
return;
}
if(!s_bStaticInit)
{
// Just in case...
AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES);
// done
s_bStaticInit = true;
}
lock.Unlock();
}
}
bool IsCommandBarBase() const { return m_dwMagic == 1314; }
};
__declspec(selectany) CCommandBarCtrlBase::CMsgHookMap* CCommandBarCtrlBase::s_pmapMsgHook = NULL;
__declspec(selectany) HHOOK CCommandBarCtrlBase::s_hCreateHook = NULL;
__declspec(selectany) CCommandBarCtrlBase* CCommandBarCtrlBase::s_pCurrentBar = NULL;
__declspec(selectany) bool CCommandBarCtrlBase::s_bStaticInit = false;
///////////////////////////////////////////////////////////////////////////////
// CCommandBarCtrl - ATL implementation of Command Bars
template <class T, class TBase = CCommandBarCtrlBase, class TWinTraits = ATL::CControlWinTraits>
class ATL_NO_VTABLE CCommandBarCtrlImpl : public ATL::CWindowImpl< T, TBase, TWinTraits >
{
public:
DECLARE_WND_SUPERCLASS2(NULL, T, TBase::GetWndClassName())
// Declarations
struct _MenuItemData // menu item data
{
DWORD dwMagic;
LPTSTR lpstrText;
UINT fType;
UINT fState;
int iButton;
_MenuItemData() : dwMagic(0x1313), lpstrText(NULL), fType(0U), fState(0U), iButton(0)
{ }
bool IsCmdBarMenuItem() { return (dwMagic == 0x1313); }
};
struct _ToolBarData // toolbar resource data
{
WORD wVersion;
WORD wWidth;
WORD wHeight;
WORD wItemCount;
//WORD aItems[wItemCount]
WORD* items()
{ return (WORD*)(this+1); }
};
// Constants
enum _CmdBarDrawConstants
{
s_kcxGap = 1,
s_kcxTextMargin = 2,
s_kcxButtonMargin = 3,
s_kcyButtonMargin = 3
};
enum
{
_nMaxMenuItemTextLength = 100,
_chChevronShortcut = _T('/')
};
// Data members
HMENU m_hMenu;
HIMAGELIST m_hImageList;
ATL::CSimpleValArray<WORD> m_arrCommand;
DWORD m_dwExtendedStyle; // Command Bar specific extended styles
ATL::CContainedWindow m_wndParent;
bool m_bMenuActive:1;
bool m_bAttachedMenu:1;
bool m_bImagesVisible:1;
bool m_bPopupItem:1;
bool m_bContextMenu:1;
bool m_bEscapePressed:1;
bool m_bSkipMsg:1;
bool m_bParentActive:1;
bool m_bFlatMenus:1;
bool m_bUseKeyboardCues:1;
bool m_bShowKeyboardCues:1;
bool m_bAllowKeyboardCues:1;
bool m_bKeyboardInput:1;
bool m_bAlphaImages:1;
bool m_bLayoutRTL:1;
bool m_bSkipPostDown:1;
bool m_bVistaMenus:1;
int m_nPopBtn;
int m_nNextPopBtn;
SIZE m_szBitmap;
SIZE m_szButton;
COLORREF m_clrMask;
CFont m_fontMenu; // used internally, only to measure text
UINT m_uSysKey;
HWND m_hWndFocus; // Alternate focus mode
int m_cxExtraSpacing;
#if _WTL_CMDBAR_VISTA_MENUS
ATL::CSimpleValArray<HBITMAP> m_arrVistaBitmap; // Bitmaps for Vista menus
#endif // _WTL_CMDBAR_VISTA_MENUS
// Constructor/destructor
CCommandBarCtrlImpl() :
m_hMenu(NULL),
m_hImageList(NULL),
m_dwExtendedStyle(CBR_EX_TRANSPARENT | CBR_EX_SHAREMENU | CBR_EX_TRACKALWAYS),
m_wndParent(this, 1),
m_bMenuActive(false),
m_bAttachedMenu(false),
m_bImagesVisible(true),
m_bPopupItem(false),
m_bContextMenu(false),
m_bEscapePressed(false),
m_bSkipMsg(false),
m_bParentActive(true),
m_bFlatMenus(false),
m_bUseKeyboardCues(false),
m_bShowKeyboardCues(false),
m_bAllowKeyboardCues(true),
m_bKeyboardInput(false),
m_bAlphaImages(false),
m_bLayoutRTL(false),
m_bSkipPostDown(false),
m_bVistaMenus(false),
m_nPopBtn(-1),
m_nNextPopBtn(-1),
m_clrMask(RGB(192, 192, 192)),
m_uSysKey(0),
m_hWndFocus(NULL),
m_cxExtraSpacing(0)
{
SetImageSize(16, 15); // default
}
~CCommandBarCtrlImpl()
{
if(m_wndParent.IsWindow())
/*scary!*/ m_wndParent.UnsubclassWindow();
if((m_hMenu != NULL) && ((m_dwExtendedStyle & CBR_EX_SHAREMENU) == 0))
::DestroyMenu(m_hMenu);
if(m_hImageList != NULL)
::ImageList_Destroy(m_hImageList);
}
// Attributes
DWORD GetCommandBarExtendedStyle() const
{
return m_dwExtendedStyle;
}
DWORD SetCommandBarExtendedStyle(DWORD dwExtendedStyle, DWORD dwMask = 0)
{
DWORD dwPrevStyle = m_dwExtendedStyle;
if(dwMask == 0)
m_dwExtendedStyle = dwExtendedStyle;
else
m_dwExtendedStyle = (m_dwExtendedStyle & ~dwMask) | (dwExtendedStyle & dwMask);
return dwPrevStyle;
}
CMenuHandle GetMenu() const
{
ATLASSERT(::IsWindow(this->m_hWnd));
return m_hMenu;
}
COLORREF GetImageMaskColor() const
{
return m_clrMask;
}
COLORREF SetImageMaskColor(COLORREF clrMask)
{
COLORREF clrOld = m_clrMask;
m_clrMask = clrMask;
return clrOld;
}
bool GetImagesVisible() const
{
return m_bImagesVisible;
}
bool SetImagesVisible(bool bVisible)
{
bool bOld = m_bImagesVisible;
m_bImagesVisible = bVisible;
return bOld;
}
void GetImageSize(SIZE& size) const
{
size = m_szBitmap;
}
bool SetImageSize(SIZE& size)
{
return SetImageSize(size.cx, size.cy);
}
bool SetImageSize(int cx, int cy)
{
if(m_hImageList != NULL)
{
if(::ImageList_GetImageCount(m_hImageList) == 0) // empty
{
::ImageList_Destroy(m_hImageList);
m_hImageList = NULL;
}
else
{
return false; // can't set, image list exists
}
}
if((cx == 0) || (cy == 0))
return false;
m_szBitmap.cx = cx;
m_szBitmap.cy = cy;
m_szButton.cx = m_szBitmap.cx + 2 * s_kcxButtonMargin;
m_szButton.cy = m_szBitmap.cy + 2 * s_kcyButtonMargin;
return true;
}
bool GetAlphaImages() const
{
return m_bAlphaImages;
}
bool SetAlphaImages(bool bAlphaImages)
{
if(m_hImageList != NULL)
{
if(::ImageList_GetImageCount(m_hImageList) == 0) // empty
{
::ImageList_Destroy(m_hImageList);
m_hImageList = NULL;
}
else
{
return false; // can't set, image list exists
}
}
m_bAlphaImages = bAlphaImages;
return true;
}
HWND GetCmdBar() const
{
ATLASSERT(::IsWindow(this->m_hWnd));
return (HWND)::SendMessage(this->m_hWnd, CBRM_GETCMDBAR, 0, 0L);
}
// Methods
HWND Create(HWND hWndParent, RECT& rcPos, LPCTSTR szWindowName = NULL,
DWORD dwStyle = 0, DWORD dwExStyle = 0,
UINT nID = 0, LPVOID lpCreateParam = NULL)
{
// These styles are required for command bars
dwStyle |= TBSTYLE_LIST | TBSTYLE_FLAT;
return ATL::CWindowImpl< T, TBase, TWinTraits >::Create(hWndParent, rcPos, szWindowName, dwStyle, dwExStyle, nID, lpCreateParam);
}
BOOL AttachToWindow(HWND hWnd)
{
ATLASSERT(this->m_hWnd == NULL);
ATLASSERT(::IsWindow(hWnd));
BOOL bRet = this->SubclassWindow(hWnd);
if(bRet)
{
m_bAttachedMenu = true;
T* pT = static_cast<T*>(this);
pT->GetSystemSettings();
}
return bRet;
}
BOOL LoadMenu(ATL::_U_STRINGorID menu)
{
ATLASSERT(::IsWindow(this->m_hWnd));
if(m_bAttachedMenu) // doesn't work in this mode
return FALSE;
if(menu.m_lpstr == NULL)
return FALSE;
HMENU hMenu = ::LoadMenu(ModuleHelper::GetResourceInstance(), menu.m_lpstr);
if(hMenu == NULL)
return FALSE;
return AttachMenu(hMenu);
}
BOOL AttachMenu(HMENU hMenu)
{
ATLASSERT(::IsWindow(this->m_hWnd));
ATLASSERT((hMenu == NULL) || ::IsMenu(hMenu));
if((hMenu != NULL) && !::IsMenu(hMenu))
return FALSE;
#if _WTL_CMDBAR_VISTA_MENUS
// remove Vista bitmaps if used
if(m_bVistaMenus && (m_hMenu != NULL))
{
T* pT = static_cast<T*>(this);
pT->_RemoveVistaBitmapsFromMenu();
}
#endif // _WTL_CMDBAR_VISTA_MENUS
// destroy old menu, if needed, and set new one
if((m_hMenu != NULL) && ((m_dwExtendedStyle & CBR_EX_SHAREMENU) == 0))
::DestroyMenu(m_hMenu);
m_hMenu = hMenu;
if(m_bAttachedMenu) // Nothing else in this mode
return TRUE;
// Build buttons according to menu
this->SetRedraw(FALSE);
// Clear all buttons
int nCount = this->GetButtonCount();
for(int i = 0; i < nCount; i++)
ATLVERIFY(this->DeleteButton(0) != FALSE);
// Add buttons for each menu item
if(m_hMenu != NULL)
{
int nItems = ::GetMenuItemCount(m_hMenu);
T* pT = static_cast<T*>(this);
(void)pT; // avoid level 4 warning
TCHAR szString[pT->_nMaxMenuItemTextLength] = {};
for(int i = 0; i < nItems; i++)
{
CMenuItemInfo mii;
mii.fMask = MIIM_TYPE | MIIM_STATE | MIIM_SUBMENU;
mii.fType = MFT_STRING;
mii.dwTypeData = szString;
mii.cch = pT->_nMaxMenuItemTextLength;
BOOL bRet = ::GetMenuItemInfo(m_hMenu, i, TRUE, &mii);
ATLASSERT(bRet);
// If we have more than the buffer, we assume we have bitmaps bits
if(lstrlen(szString) > pT->_nMaxMenuItemTextLength - 1)
{
mii.fType = MFT_BITMAP;
::SetMenuItemInfo(m_hMenu, i, TRUE, &mii);
szString[0] = 0;
}
// NOTE: Command Bar currently supports only drop-down menu items
ATLASSERT(mii.hSubMenu != NULL);
TBBUTTON btn = {};
btn.iBitmap = 0;
btn.idCommand = i;
btn.fsState = (BYTE)(((mii.fState & MFS_DISABLED) == 0) ? TBSTATE_ENABLED : 0);
btn.fsStyle = BTNS_BUTTON | BTNS_AUTOSIZE | BTNS_DROPDOWN;
btn.dwData = 0;
btn.iString = 0;
bRet = this->InsertButton(-1, &btn);
ATLASSERT(bRet);
TBBUTTONINFO bi = {};
bi.cbSize = sizeof(TBBUTTONINFO);
bi.dwMask = TBIF_TEXT;
bi.pszText = szString;
bRet = this->SetButtonInfo(i, &bi);
ATLASSERT(bRet);
}
}
this->SetRedraw(TRUE);
this->Invalidate();
this->UpdateWindow();
return TRUE;
}
BOOL LoadImages(ATL::_U_STRINGorID image)
{
return _LoadImagesHelper(image, false);
}
BOOL LoadMappedImages(UINT nIDImage, UINT nFlags = 0, LPCOLORMAP lpColorMap = NULL, int nMapSize = 0)
{
return _LoadImagesHelper(nIDImage, true, nFlags , lpColorMap, nMapSize);
}
BOOL _LoadImagesHelper(ATL::_U_STRINGorID image, bool bMapped, UINT nFlags = 0, LPCOLORMAP lpColorMap = NULL, int nMapSize = 0)
{
ATLASSERT(::IsWindow(this->m_hWnd));
HINSTANCE hInstance = ModuleHelper::GetResourceInstance();
HRSRC hRsrc = ::FindResource(hInstance, image.m_lpstr, (LPTSTR)RT_TOOLBAR);
if(hRsrc == NULL)
return FALSE;
HGLOBAL hGlobal = ::LoadResource(hInstance, hRsrc);
if(hGlobal == NULL)
return FALSE;
_ToolBarData* pData = (_ToolBarData*)::LockResource(hGlobal);
if(pData == NULL)
return FALSE;
ATLASSERT(pData->wVersion == 1);
WORD* pItems = pData->items();
int nItems = pData->wItemCount;
// Set internal data
SetImageSize(pData->wWidth, pData->wHeight);
// Create image list if needed
if(m_hImageList == NULL)
{
// Check if the bitmap is 32-bit (alpha channel) bitmap (valid for Windows XP only)
T* pT = static_cast<T*>(this);
m_bAlphaImages = AtlIsAlphaBitmapResource(image);
if(!pT->CreateInternalImageList(pData->wItemCount))
return FALSE;
}
#if _WTL_CMDBAR_VISTA_MENUS
int nOldImageCount = ::ImageList_GetImageCount(m_hImageList);
#endif // _WTL_CMDBAR_VISTA_MENUS
// Add bitmap to our image list
CBitmap bmp;
if(bMapped)
{
ATLASSERT(HIWORD(PtrToUlong(image.m_lpstr)) == 0); // if mapped, must be a numeric ID
int nIDImage = (int)(short)LOWORD(PtrToUlong(image.m_lpstr));
bmp.LoadMappedBitmap(nIDImage, (WORD)nFlags, lpColorMap, nMapSize);
}
else
{
if(m_bAlphaImages)
bmp = (HBITMAP)::LoadImage(ModuleHelper::GetResourceInstance(), image.m_lpstr, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
else
bmp.LoadBitmap(image.m_lpstr);
}
ATLASSERT(bmp.m_hBitmap != NULL);
if(bmp.m_hBitmap == NULL)
return FALSE;
if(::ImageList_AddMasked(m_hImageList, bmp, m_clrMask) == -1)
return FALSE;
// Fill the array with command IDs
for(int i = 0; i < nItems; i++)
{
if(pItems[i] != 0)
m_arrCommand.Add(pItems[i]);
}
int nImageCount = ::ImageList_GetImageCount(m_hImageList);
ATLASSERT(nImageCount == m_arrCommand.GetSize());
if(nImageCount != m_arrCommand.GetSize())
return FALSE;
#if _WTL_CMDBAR_VISTA_MENUS
if(RunTimeHelper::IsVista())
{
T* pT = static_cast<T*>(this);
pT->_AddVistaBitmapsFromImageList(nOldImageCount, nImageCount - nOldImageCount);
ATLASSERT(nImageCount == m_arrVistaBitmap.GetSize());
}
#endif // _WTL_CMDBAR_VISTA_MENUS
return TRUE;
}
BOOL AddBitmap(ATL::_U_STRINGorID bitmap, int nCommandID)
{
ATLASSERT(::IsWindow(this->m_hWnd));
CBitmap bmp;
bmp.LoadBitmap(bitmap.m_lpstr);
if(bmp.m_hBitmap == NULL)
return FALSE;
return AddBitmap(bmp, nCommandID);
}
BOOL AddBitmap(HBITMAP hBitmap, UINT nCommandID)
{
ATLASSERT(::IsWindow(this->m_hWnd));
T* pT = static_cast<T*>(this);
// Create image list if it doesn't exist
if(m_hImageList == NULL)
{
if(!pT->CreateInternalImageList(1))
return FALSE;
}
// check bitmap size
CBitmapHandle bmp = hBitmap;
SIZE size = {};
bmp.GetSize(size);
if((size.cx != m_szBitmap.cx) || (size.cy != m_szBitmap.cy))
{
ATLASSERT(FALSE); // must match size!
return FALSE;
}
// add bitmap
int nRet = ::ImageList_AddMasked(m_hImageList, hBitmap, m_clrMask);
if(nRet == -1)
return FALSE;
BOOL bRet = m_arrCommand.Add((WORD)nCommandID);
ATLASSERT(::ImageList_GetImageCount(m_hImageList) == m_arrCommand.GetSize());
#if _WTL_CMDBAR_VISTA_MENUS
if(RunTimeHelper::IsVista())
{
pT->_AddVistaBitmapFromImageList(m_arrCommand.GetSize() - 1);
ATLASSERT(m_arrVistaBitmap.GetSize() == m_arrCommand.GetSize());
}
#endif // _WTL_CMDBAR_VISTA_MENUS
return bRet;
}
BOOL AddIcon(ATL::_U_STRINGorID icon, UINT nCommandID)
{
ATLASSERT(::IsWindow(this->m_hWnd));
HICON hIcon = ::LoadIcon(ModuleHelper::GetResourceInstance(), icon.m_lpstr);
if(hIcon == NULL)
return FALSE;
return AddIcon(hIcon, nCommandID);
}
BOOL AddIcon(HICON hIcon, UINT nCommandID)
{
ATLASSERT(::IsWindow(this->m_hWnd));
T* pT = static_cast<T*>(this);
// create image list if it doesn't exist
if(m_hImageList == NULL)
{
if(!pT->CreateInternalImageList(1))
return FALSE;
}
int nRet = ::ImageList_AddIcon(m_hImageList, hIcon);
if(nRet == -1)
return FALSE;
BOOL bRet = m_arrCommand.Add((WORD)nCommandID);
ATLASSERT(::ImageList_GetImageCount(m_hImageList) == m_arrCommand.GetSize());
#if _WTL_CMDBAR_VISTA_MENUS
if(RunTimeHelper::IsVista())
{
pT->_AddVistaBitmapFromImageList(m_arrCommand.GetSize() - 1);
ATLASSERT(m_arrVistaBitmap.GetSize() == m_arrCommand.GetSize());
}
#endif // _WTL_CMDBAR_VISTA_MENUS
return bRet;
}
BOOL ReplaceBitmap(ATL::_U_STRINGorID bitmap, int nCommandID)
{
ATLASSERT(::IsWindow(this->m_hWnd));
CBitmap bmp;
bmp.LoadBitmap(bitmap.m_lpstr);
if(bmp.m_hBitmap == NULL)
return FALSE;
return ReplaceBitmap(bmp, nCommandID);
}
BOOL ReplaceBitmap(HBITMAP hBitmap, UINT nCommandID)
{
ATLASSERT(::IsWindow(this->m_hWnd));
BOOL bRet = FALSE;
for(int i = 0; i < m_arrCommand.GetSize(); i++)
{
if(m_arrCommand[i] == nCommandID)
{
bRet = ::ImageList_Remove(m_hImageList, i);
if(bRet)
{
m_arrCommand.RemoveAt(i);
#if _WTL_CMDBAR_VISTA_MENUS
if(RunTimeHelper::IsVista())
{
if(m_arrVistaBitmap[i] != NULL)
::DeleteObject(m_arrVistaBitmap[i]);
m_arrVistaBitmap.RemoveAt(i);
}
#endif // _WTL_CMDBAR_VISTA_MENUS
}
break;
}
}
if(bRet)
bRet = AddBitmap(hBitmap, nCommandID);
return bRet;
}
BOOL ReplaceIcon(ATL::_U_STRINGorID icon, UINT nCommandID)
{
ATLASSERT(::IsWindow(this->m_hWnd));
HICON hIcon = ::LoadIcon(ModuleHelper::GetResourceInstance(), icon.m_lpstr);
if(hIcon == NULL)
return FALSE;
return ReplaceIcon(hIcon, nCommandID);
}
BOOL ReplaceIcon(HICON hIcon, UINT nCommandID)
{
ATLASSERT(::IsWindow(this->m_hWnd));
BOOL bRet = FALSE;
for(int i = 0; i < m_arrCommand.GetSize(); i++)
{
if(m_arrCommand[i] == nCommandID)
{
bRet = (::ImageList_ReplaceIcon(m_hImageList, i, hIcon) != -1);
#if _WTL_CMDBAR_VISTA_MENUS
if(RunTimeHelper::IsVista() && (bRet != FALSE))
{
T* pT = static_cast<T*>(this);
pT->_ReplaceVistaBitmapFromImageList(i);
}
#endif // _WTL_CMDBAR_VISTA_MENUS
break;
}
}
return bRet;
}
BOOL RemoveImage(int nCommandID)
{
ATLASSERT(::IsWindow(this->m_hWnd));
BOOL bRet = FALSE;
for(int i = 0; i < m_arrCommand.GetSize(); i++)
{
if(m_arrCommand[i] == nCommandID)
{
bRet = ::ImageList_Remove(m_hImageList, i);
if(bRet)
{
m_arrCommand.RemoveAt(i);
#if _WTL_CMDBAR_VISTA_MENUS
if(RunTimeHelper::IsVista())
{
if(m_arrVistaBitmap[i] != NULL)
::DeleteObject(m_arrVistaBitmap[i]);
m_arrVistaBitmap.RemoveAt(i);
}
#endif // _WTL_CMDBAR_VISTA_MENUS
}
break;
}
}
return bRet;
}
BOOL RemoveAllImages()
{
ATLASSERT(::IsWindow(this->m_hWnd));
ATLTRACE2(atlTraceUI, 0, _T("CmdBar - Removing all images\n"));
BOOL bRet = ::ImageList_RemoveAll(m_hImageList);
if(bRet)
{
m_arrCommand.RemoveAll();
#if _WTL_CMDBAR_VISTA_MENUS
for(int i = 0; i < m_arrVistaBitmap.GetSize(); i++)
{
if(m_arrVistaBitmap[i] != NULL)
::DeleteObject(m_arrVistaBitmap[i]);
}
m_arrVistaBitmap.RemoveAll();
#endif // _WTL_CMDBAR_VISTA_MENUS
}
return bRet;
}
BOOL TrackPopupMenu(HMENU hMenu, UINT uFlags, int x, int y, LPTPMPARAMS lpParams = NULL)
{
ATLASSERT(::IsWindow(this->m_hWnd));
ATLASSERT(::IsMenu(hMenu));
if(!::IsMenu(hMenu))
return FALSE;
m_bContextMenu = true;
if(m_bUseKeyboardCues)
m_bShowKeyboardCues = m_bKeyboardInput;
T* pT = static_cast<T*>(this);
return pT->DoTrackPopupMenu(hMenu, uFlags, x, y, lpParams);
}
BOOL SetMDIClient(HWND /*hWndMDIClient*/)
{
// Use CMDICommandBarCtrl for MDI support
ATLASSERT(FALSE);
return FALSE;
}
// Message map and handlers
BEGIN_MSG_MAP(CCommandBarCtrlImpl)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBackground)
MESSAGE_HANDLER(WM_INITMENU, OnInitMenu)
MESSAGE_HANDLER(WM_INITMENUPOPUP, OnInitMenuPopup)
MESSAGE_HANDLER(WM_MENUSELECT, OnMenuSelect)
MESSAGE_HANDLER(GetAutoPopupMessage(), OnInternalAutoPopup)
MESSAGE_HANDLER(GetGetBarMessage(), OnInternalGetBar)
MESSAGE_HANDLER(WM_SETTINGCHANGE, OnSettingChange)
MESSAGE_HANDLER(WM_MENUCHAR, OnMenuChar)
MESSAGE_HANDLER(WM_KILLFOCUS, OnKillFocus)
MESSAGE_HANDLER(WM_KEYDOWN, OnKeyDown)
MESSAGE_HANDLER(WM_KEYUP, OnKeyUp)
MESSAGE_HANDLER(WM_CHAR, OnChar)
MESSAGE_HANDLER(WM_SYSKEYDOWN, OnSysKeyDown)
MESSAGE_HANDLER(WM_SYSKEYUP, OnSysKeyUp)
MESSAGE_HANDLER(WM_SYSCHAR, OnSysChar)
// public API handlers - these stay to support chevrons in atlframe.h
MESSAGE_HANDLER(CBRM_GETMENU, OnAPIGetMenu)
MESSAGE_HANDLER(CBRM_TRACKPOPUPMENU, OnAPITrackPopupMenu)
MESSAGE_HANDLER(CBRM_GETCMDBAR, OnAPIGetCmdBar)
MESSAGE_HANDLER(WM_DRAWITEM, OnDrawItem)
MESSAGE_HANDLER(WM_MEASUREITEM, OnMeasureItem)
MESSAGE_HANDLER(WM_FORWARDMSG, OnForwardMsg)
ALT_MSG_MAP(1) // Parent window messages
NOTIFY_CODE_HANDLER(TBN_HOTITEMCHANGE, OnParentHotItemChange)
NOTIFY_CODE_HANDLER(TBN_DROPDOWN, OnParentDropDown)
MESSAGE_HANDLER(WM_INITMENUPOPUP, OnParentInitMenuPopup)
MESSAGE_HANDLER(GetGetBarMessage(), OnParentInternalGetBar)
MESSAGE_HANDLER(WM_SYSCOMMAND, OnParentSysCommand)
MESSAGE_HANDLER(CBRM_GETMENU, OnParentAPIGetMenu)
MESSAGE_HANDLER(WM_MENUCHAR, OnParentMenuChar)
MESSAGE_HANDLER(CBRM_TRACKPOPUPMENU, OnParentAPITrackPopupMenu)
MESSAGE_HANDLER(CBRM_GETCMDBAR, OnParentAPIGetCmdBar)
MESSAGE_HANDLER(WM_SETTINGCHANGE, OnParentSettingChange)
MESSAGE_HANDLER(WM_DRAWITEM, OnParentDrawItem)
MESSAGE_HANDLER(WM_MEASUREITEM, OnParentMeasureItem)
MESSAGE_HANDLER(WM_ACTIVATE, OnParentActivate)
NOTIFY_CODE_HANDLER(NM_CUSTOMDRAW, OnParentCustomDraw)
ALT_MSG_MAP(2) // MDI client window messages
// Use CMDICommandBarCtrl for MDI support
ALT_MSG_MAP(3) // Message hook messages
MESSAGE_HANDLER(WM_MOUSEMOVE, OnHookMouseMove)
MESSAGE_HANDLER(WM_SYSKEYDOWN, OnHookSysKeyDown)
MESSAGE_HANDLER(WM_SYSKEYUP, OnHookSysKeyUp)
MESSAGE_HANDLER(WM_SYSCHAR, OnHookSysChar)
MESSAGE_HANDLER(WM_KEYDOWN, OnHookKeyDown)
MESSAGE_HANDLER(WM_NEXTMENU, OnHookNextMenu)
MESSAGE_HANDLER(WM_CHAR, OnHookChar)
END_MSG_MAP()
LRESULT OnForwardMsg(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
LPMSG pMsg = (LPMSG)lParam;
if((pMsg->message >= WM_MOUSEFIRST) && (pMsg->message <= WM_MOUSELAST))
m_bKeyboardInput = false;
else if((pMsg->message >= WM_KEYFIRST) && (pMsg->message <= WM_KEYLAST))
m_bKeyboardInput = true;
LRESULT lRet = 0;
ProcessWindowMessage(pMsg->hwnd, pMsg->message, pMsg->wParam, pMsg->lParam, lRet, 3);
return lRet;
}
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
// Let the toolbar initialize itself
LRESULT lRet = this->DefWindowProc(uMsg, wParam, lParam);
// get and use system settings
T* pT = static_cast<T*>(this);
pT->GetSystemSettings();
// Parent init
ATL::CWindow wndParent = this->GetParent();
ATL::CWindow wndTopLevelParent = wndParent.GetTopLevelParent();
m_wndParent.SubclassWindow(wndTopLevelParent);
// Toolbar Init
this->SetButtonStructSize();
this->SetImageList(NULL);
// Create message hook if needed
CWindowCreateCriticalSectionLock lock;
if(FAILED(lock.Lock()))
{
ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CCommandBarCtrlImpl::OnCreate.\n"));
ATLASSERT(FALSE);
return -1;
}
if(this->s_pmapMsgHook == NULL)
{
ATLTRY(this->s_pmapMsgHook = new CCommandBarCtrlBase::CMsgHookMap);
ATLASSERT(this->s_pmapMsgHook != NULL);
}
if(this->s_pmapMsgHook != NULL)
{
DWORD dwThreadID = ::GetCurrentThreadId();
CCommandBarCtrlBase::_MsgHookData* pData = this->s_pmapMsgHook->Lookup(dwThreadID);
if(pData == NULL)
{
ATLTRY(pData = new CCommandBarCtrlBase::_MsgHookData);
ATLASSERT(pData != NULL);
HHOOK hMsgHook = ::SetWindowsHookEx(WH_GETMESSAGE, MessageHookProc, ModuleHelper::GetModuleInstance(), dwThreadID);
ATLASSERT(hMsgHook != NULL);
if((pData != NULL) && (hMsgHook != NULL))
{
pData->hMsgHook = hMsgHook;
pData->dwUsage = 1;
BOOL bRet = this->s_pmapMsgHook->Add(dwThreadID, pData);
(void)bRet; // avoid level 4 warning
ATLASSERT(bRet);
}
}
else
{
(pData->dwUsage)++;
}
}
lock.Unlock();
// Get layout