/
CustomTabCtrl.h
3378 lines (2978 loc) · 92 KB
/
CustomTabCtrl.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
#ifndef __CUSTOMTABCTRL_H__
#define __CUSTOMTABCTRL_H__
#pragma once
/////////////////////////////////////////////////////////////////////////////
// CCustomTabCtrl - A base class to help implement
// Tab Controls with different appearances
//
// Original work by Bjarke Viksoe (bjarke@viksoe.dk)
// Revised version by Daniel Bowen (dbowen@es.com).
// Copyright (c) 2001-2002 Bjarke Viksoe.
// Copyright (c) 2002-2004 Daniel Bowen.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to you or your
// computer whatsoever. It's free, so don't hassle me about it.
//
// Beware of bugs.
//
// History (Date/Author/Description):
// ----------------------------------
//
// 2005/07/13: Daniel Bowen
// - Namespace qualify the use of more ATL and WTL classes.
//
// 2005/03/14: Daniel Bowen
// - Fix warnings when compiling for 64-bit.
//
// 2004/06/28: Daniel Bowen
// - More ATLASSERTs
// - Clean up warnings on level 4
// - Fix "FindItem" so you can search on more than 1 criteria at a time
// (like it was intended to work). Use #define for flags now
// instead of enumeration (CTFI_TABVIEW instead of eCustomTabItem_TabView).
//
// 2004/06/21: Peter Carlson
// - "CanClose" for items.
// - HighlightItem - "SetHighlighted" on item, then InvalidateRect for the item
//
// 2004/05/10: Peter Carlson
// - Middle mouse button notifications
//
// 2004/04/29: Daniel Bowen
// - Suport for a new CTCS_DRAGREARRANGE style. If you set this style,
// a tab item can be dragged to another position within the
// same tab control. Along with this are the new notifications
// CTCN_BEGINITEMDRAG
// CTCN_ACCEPTITEMDRAG
// CTCN_CANCELITEMDRAG
// If CTCS_SCROLL is also set, then the tabs will scroll when
// you get near the left or the right edge of the tab control.
// The drag rearrange methods are all overrideable, so a more
// derived class could do a different UI for the drag and drop.
// The current implementation roughly mimics dragging
// MDI tabs in Visual Studio (with the exceptions of
// supporting scrolling, and not moving the tab until the
// cursor is past the half-way point of an adjacent tab).
// - Remove m_idDlgCtrl, and just use GetDlgCtrlID where needed.
// This way, you can change the ID after the window is created.
// - Move shutdown code that was in OnDestroy to "Uninitialize".
// Call Uninitialize from both OnDestroy and UnsubclassWindow
// (UnsubclassWindow wasn't previously being overriden like it should).
// - With "Mouse Down" state flags, specify Left or Right to be
// more specific. Currently, the right mouse button state flags
// are not being set.
// - On a left and right button down, don't take the focus when
// clicking on a tab item.
// - You can now change the definition of the scroll speed specified by
// CTCSR_NONE
// CTCSR_SLOW
// CTCSR_NORMAL
// CTCSR_FAST
// As long as you #define these before including this header file.
// - Support for highlighting items.
// Add CCustomTabItem::IsHighlighted/SetHighlighted.
// Uses the custom draw state CDIS_MARKED.
//
// 2003/06/27: Daniel Bowen
// - Update comment referencing DECLARE_WND_CLASS to be DECLARE_WND_CLASS_EX instead
// - If the creation of the window fails, don't try to initialize.
//
// 2003/06/03: Daniel Bowen
// - Fix compile errors for VC 7.1
//
// 2003/01/07: Daniel Bowen
// - Destroy or detach tooltip control when handling WM_DESTROY
//
// 2002/12/05: Daniel Bowen
// - Handle WM_SYSCOLORCHANGE in case its broadcast to us
// from a top-level window. Call OnSettingChange.
//
// 2002/11/13: Daniel Bowen
// - New CTCS_FLATEDGE style. Tab controls derived from
// CCustomTabCtrl can use this style to determine whether
// to draw the outline of the control with a flat look.
// - New CalcSize_NonClient. UpdateLayout will now call
// this overrideable method before calling any other
// CalcSize_* methods, so that you can adjust the client
// RECT to account for non-client areas.
//
// 2002/10/21: Daniel Bowen
// - NMCTCITEM and NMCTC2ITEMS actually have "pt" in client
// coordinates, not screen coordinates. Change the comment.
// - Notifications using NMCTCITEM and NMCTC2ITEMS
// (NM_CLICK, et. al) were incorrectly initialing "pt".
// - Remove some some unnecessary ATLASSERT(::IsWindow(m_hWnd))
// (if the method doesn't depend on a valid m_hWnd)
// - Add some additional casting when dealing with current selection
// - DeleteItem - after sending CTCN_DELETEITEM, re-get
// the count of items in case its changed
// - Change a couple of ASSERTs and parameter checks that deal
// with size_t variables to not needlessly check for < 0
// - Change SetCurSel to take an int instead of a size_t.
// Passing an int < 0 will clear the current selection
// - CCustomTabCtrl::SetImageList - Should be
// CImageList imageListOld = m_imageList;
// instead of
// CImageList& imageListOld = m_imageList;
//
// 2002/07/16: Daniel Bowen
// - Ensure that any place doing anything with m_tooltip
// first checks if(m_tooltip.IsWindow()).
// - Update DeleteAllItems to turn off redrawing while
// the delete happens.
// - DeleteAllItems now takes an optional boolean to
// specify whether or not to redraw after the deletion
// - Handle WM_SETREDRAW, but still allow the default handling.
// We'll track WM_SETREDRAW in our own state variable
// so that we can avoid doing UpdateLayout if
// someone has called WM_SETREDRAW with FALSE.
// When they call WM_SETREDRAW with TRUE to turn
// it back on, we'll UpdateLayout to be ready for the caller
// to do an InvalidateRect or RedrawWindow.
//
// 2002/06/20: Daniel Bowen
// - SetCurSel -
// * Added a new optional parameter "bNotify" that allows
// you to specify whether or not you want the parent
// to receive the notifications CTCN_SELCHANGING and
// CTCN_SELCHANGE.
// * Even if the newly requested selection index is
// the same index as the previous selection index,
// go through the whole SetCurSel process.
// Even though the index is the same, the item
// might be different (as in the case of
// InsertItem inserting a new item where the old
// selection used to be). EnsureVisible is also called,
// which in the CTCS_SCROLL case, you really want
// every time even if the item is the same.
//
// This is a change from previous versions,
// where if the index was the same as the current selection,
// the method returned immediately.
//
// - DeleteItem -
// * When bUpdateSelection is true:
// Now, when you delete the selected item, instead of
// selecting the 0-index item, it tries to leave the index
// of the selected item the same. If the selected item
// was the last item, the new last item is selected.
// If the selected item was the only remaining item,
// the selection is cleared.
//
//
// 2002/06/13: Daniel Bowen
// - Fix small bug with scroll-repeat when scrolling right.
//
// 2002/06/12: Daniel Bowen
// - Publish codeproject article. For history prior
// to the release of the article, please see the article
// and the section "Note to previous users"
#ifndef __cplusplus
#error ATL requires C++ compilation (use a .cpp suffix)
#endif
#ifndef __ATLAPP_H__
#error CustomTabCtrl.h requires atlapp.h to be included first
#endif
#ifndef __ATLCTRLS_H__
#error CustomTabCtrl.h requires atlctrls.h to be included first
#endif
#ifndef __ATLGDIX_H__
#error CustomTabCtrl.h requires atlgdix.h to be included first
#endif
// There are slightly different dependencies under VC 7 (ATL 7) vs. VC 6 (ATL 3)
#if (_ATL_VER >= 0x0700)
#if !defined(__ATLCOLL_H__)
#error CustomTabCtrl.h requires atlcoll.h (under VC 7).
#endif
#if !defined(__ATLSTR_H__)
#error CustomTabCtrl.h requires CString. In VC 7, include atlstr.h
#endif
#if defined(__ATLMISC_H__) && !defined(_WTL_NO_CSTRING)
#error In VC 7, please define _WTL_NO_CSTRING if you include atlmisc.h
#endif
#else
#if !defined(_WTL_USE_CSTRING)
#error CustomTabCtrl.h requires CString. In VC 6, be sure to include atlmisc.h.
#endif
#endif
#if (_WIN32_IE < 0x0400)
#error CustomTabCtrl.h requires _WIN32_IE >= 0x0400
#endif
// Window styles:
// NOTE: "CTCS" stands for "Custom tab control style"
#define CTCS_SCROLL 0x0001 // TCS_SCROLLOPPOSITE
#define CTCS_BOTTOM 0x0002 // TCS_BOTTOM
//#define CTCS_RIGHT 0x0002 // TCS_RIGHT
//#define CTCS_MULTISELECT 0x0004 // TCS_MULTISELECT
#define CTCS_CLOSEBUTTON 0x0008 // TCS_FLATBUTTONS
//#define CTCS_FORCEICONLEFT 0x0010 // TCS_FORCEICONLEFT
//#define CTCS_FORCELABELLEFT 0x0020 // TCS_FORCELABELLEFT
#define CTCS_HOTTRACK 0x0040 // TCS_HOTTRACK
//#define CTCS_VERTICAL 0x0080 // TCS_VERTICAL
//#define CTCS_TABS 0x0000 // TCS_TABS
#define CTCS_FLATEDGE 0x0100 // TCS_BUTTONS
//#define CTCS_SINGLELINE 0x0000 // TCS_SINGLELINE
//#define CTCS_MULTILINE 0x0200 // TCS_MULTILINE
//#define CTCS_RIGHTJUSTIFY 0x0000 // TCS_RIGHTJUSTIFY
#define CTCS_DRAGREARRANGE 0x0400 // TCS_FIXEDWIDTH
//#define CTCS_OLEDRAGDROP 0x0800 // TCS_RAGGEDRIGHT
//#define CTCS_FOCUSONBUTTONDOWN 0x1000 // TCS_FOCUSONBUTTONDOWN
#define CTCS_BOLDSELECTEDTAB 0x2000 // TCS_OWNERDRAWFIXED
#define CTCS_TOOLTIPS 0x4000 // TCS_TOOLTIPS
//#define CTCS_FOCUSNEVER 0x8000 // TCS_FOCUSNEVER
// Notifications:
#define CTCN_FIRST (0U-550U) // TCN_FIRST
#define CTCN_LAST (0U-580U) // TCN_LAST
#define CTCN_SELCHANGE (TCN_FIRST - 1) // TCN_SELCHANGE
#define CTCN_SELCHANGING (TCN_FIRST - 2) // TCN_SELCHANGING
//#define CTCN_GETOBJECT (TCN_FIRST - 3) // TCN_GETOBJECT
//#define CTCN_FOCUSCHANGE (TCN_FIRST - 4) // TCN_FOCUSCHANGE
//#define CTCN_INITIALIZE (TCN_FIRST - 10) // obsolete for now
#define CTCN_INSERTITEM (TCN_FIRST - 11)
#define CTCN_DELETEITEM (TCN_FIRST - 12)
#define CTCN_MOVEITEM (TCN_FIRST - 13)
#define CTCN_SWAPITEMPOSITIONS (TCN_FIRST - 14)
#define CTCN_CLOSE (TCN_FIRST - 15)
#define CTCN_BEGINITEMDRAG (TCN_FIRST - 21)
#define CTCN_ACCEPTITEMDRAG (TCN_FIRST - 22)
#define CTCN_CANCELITEMDRAG (TCN_FIRST - 23)
#define CTCN_MCLICK (TCN_FIRST - 24)
#define CTCN_MDBLCLK (TCN_FIRST - 25)
// Hit Test codes
#define CTCHT_NOWHERE 0x0001 // TCHT_NOWHERE
#define CTCHT_ONITEMICON 0x0002 // TCHT_ONITEMICON
#define CTCHT_ONITEMLABEL 0x0004 // TCHT_ONITEMLABEL
#define CTCHT_ONITEM (CTCHT_ONITEMICON | CTCHT_ONITEMLABEL)
#define CTCHT_ONCLOSEBTN 0x0010
#define CTCHT_ONSCROLLRIGHTBTN 0x0020
#define CTCHT_ONSCROLLLEFTBTN 0x0040
// Find Item flags
#define CTFI_NONE 0x0000
#define CTFI_RECT 0x0001
#define CTFI_IMAGE 0x0002
#define CTFI_TEXT 0x0004
#define CTFI_TOOLTIP 0x0008
#define CTFI_TABVIEW 0x0010
#define CTFI_HIGHLIGHTED 0x0020
#define CTFI_CANCLOSE 0x0040
#define CTFI_LAST CTFI_CANCLOSE
#define CTFI_ALL 0xFFFF
// Number of milliseconds for scroll repeat
#ifndef CTCSR_NONE
#define CTCSR_NONE 0
#endif
#ifndef CTCSR_SLOW
#define CTCSR_SLOW 100
#endif
#ifndef CTCSR_NORMAL
#define CTCSR_NORMAL 25
#endif
#ifndef CTCSR_FAST
#define CTCSR_FAST 10
#endif
// Drag and drop related constant
#ifndef CTCD_SCROLLZONEWIDTH
#define CTCD_SCROLLZONEWIDTH 20
#endif
// Structures
typedef struct tagNMCTCITEM
{
NMHDR hdr;
int iItem; // Item Index
POINT pt; // Client Coordinates
} NMCTCITEM, *LPNMCTCITEM;
typedef struct tagNMCTC2ITEMS
{
NMHDR hdr;
int iItem1; // First Item Index
int iItem2; // Second Item Index
POINT pt; // Client Coordinates
} NMCTC2ITEMS, *LPNMCTC2ITEMS;
typedef struct tagCTCHITTESTINFO
{
POINT pt; // Client Coordinates of point to test
UINT flags;
} CTCHITTESTINFO, *LPCTCHITTESTINFO;
typedef struct tagNMCTCCUSTOMDRAW
{
NMCUSTOMDRAW nmcd;
HFONT hFontInactive;
HFONT hFontSelected;
HBRUSH hBrushBackground;
COLORREF clrTextInactive;
COLORREF clrTextSelected;
COLORREF clrSelectedTab;
COLORREF clrBtnFace;
COLORREF clrBtnShadow;
COLORREF clrBtnHighlight;
COLORREF clrBtnText;
COLORREF clrHighlight;
COLORREF clrHighlightHotTrack;
COLORREF clrHighlightText;
} NMCTCCUSTOMDRAW, FAR * LPNMCTCCUSTOMDRAW;
typedef struct tagCTCSETTINGS
{
signed char iPadding;
signed char iMargin;
signed char iSelMargin;
signed char iIndent;
} CTCSETTINGS;
// Tab Item classes
class CCustomTabItem
{
// Member variables
protected:
RECT m_rcItem;
int m_nImage;
_CSTRING_NS::CString m_sText;
_CSTRING_NS::CString m_sToolTip;
bool m_bHighlighted;
bool m_bCanClose;
public:
// NOTE: These are here for backwards compatibility.
// Use the new CTFI_NONE, CTFI_RECT, etc.
typedef enum FieldFlags
{
eCustomTabItem_None = CTFI_NONE,
eCustomTabItem_Rect = CTFI_RECT,
eCustomTabItem_Image = CTFI_IMAGE,
eCustomTabItem_Text = CTFI_TEXT,
eCustomTabItem_ToolTip = CTFI_TOOLTIP,
eCustomTabItem_All = CTFI_ALL,
};
#if (_MSC_VER >= 1300)
#pragma deprecated(eCustomTabItem_None)
#pragma deprecated(eCustomTabItem_Rect)
#pragma deprecated(eCustomTabItem_Image)
#pragma deprecated(eCustomTabItem_Text)
#pragma deprecated(eCustomTabItem_ToolTip)
#pragma deprecated(eCustomTabItem_All)
#endif
// Constructors/Destructors
public:
CCustomTabItem() :
m_nImage(-1),
m_bHighlighted(false),
m_bCanClose(true)
{
::SetRectEmpty(&m_rcItem);
}
CCustomTabItem(const CCustomTabItem& rhs)
{
*this = rhs;
}
virtual ~CCustomTabItem()
{
}
const CCustomTabItem& operator=(const CCustomTabItem& rhs)
{
if(&rhs != this)
{
m_rcItem = rhs.m_rcItem;
m_nImage = rhs.m_nImage;
m_sText = rhs.m_sText;
m_sToolTip = rhs.m_sToolTip;
m_bHighlighted = rhs.m_bHighlighted;
m_bCanClose = rhs.m_bCanClose;
}
return *this;
}
// Accessors
public:
RECT GetRect() const
{
return m_rcItem;
}
LPCRECT GetRectRef() const
{
return &m_rcItem;
}
bool SetRect(RECT rcItem)
{
m_rcItem = rcItem;
return true;
}
int GetImageIndex() const
{
return m_nImage;
}
bool SetImageIndex(int nImage = -1)
{
m_nImage = nImage;
return true;
}
_CSTRING_NS::CString GetText() const
{
return m_sText;
}
LPCTSTR GetTextRef() const
{
return (LPCTSTR)m_sText;
}
bool SetText(LPCTSTR sNewText)
{
m_sText = sNewText;
return true;
}
_CSTRING_NS::CString GetToolTip() const
{
return m_sToolTip;
}
LPCTSTR GetToolTipRef() const
{
return (LPCTSTR)m_sToolTip;
}
bool SetToolTip(LPCTSTR sNewText)
{
m_sToolTip = sNewText;
return true;
}
bool IsHighlighted() const
{
return m_bHighlighted;
}
bool SetHighlighted(bool bHighlighted)
{
m_bHighlighted = bHighlighted;
return true;
}
bool CanClose() const
{
return m_bCanClose;
}
bool SetCanClose(bool bCanClose)
{
m_bCanClose = bCanClose;
return true;
}
// Methods:
public:
bool UsingImage() const
{
return (m_nImage >= 0);
}
bool UsingText() const
{
return (m_sText.GetLength() > 0);
}
bool UsingToolTip() const
{
return (m_sToolTip.GetLength() > 0);
}
BOOL InflateRect(int dx, int dy)
{
return ::InflateRect(&m_rcItem, dx, dy);
}
bool MatchItem(CCustomTabItem* pItem, DWORD eFlags) const
{
bool bMatch = true;
if(bMatch && (eFlags & CTFI_RECT) == CTFI_RECT)
{
bMatch = (TRUE == ::EqualRect(&m_rcItem, &pItem->m_rcItem));
}
if(bMatch && (eFlags & CTFI_IMAGE) == CTFI_IMAGE)
{
bMatch = (m_nImage == pItem->m_nImage);
}
if(bMatch && (eFlags & CTFI_TEXT) == CTFI_TEXT)
{
bMatch = (m_sText == pItem->m_sText);
}
if(bMatch && (eFlags & CTFI_TOOLTIP) == CTFI_TOOLTIP)
{
bMatch = (m_sToolTip == pItem->m_sToolTip);
}
if(bMatch && (eFlags & CTFI_HIGHLIGHTED) == CTFI_HIGHLIGHTED)
{
bMatch = (m_bHighlighted == pItem->m_bHighlighted);
}
if(bMatch && (eFlags & CTFI_CANCLOSE) == CTFI_CANCLOSE)
{
bMatch = (m_bCanClose == pItem->m_bCanClose);
}
if(bMatch)
{
*pItem = *this;
}
return bMatch;
}
};
// Derived Tab Item class that supports an HWND identifying a "tab view"
class CTabViewTabItem : public CCustomTabItem
{
protected:
typedef CCustomTabItem baseClass;
// Member variables (in addition to CCustomTabItem ones)
protected:
HWND m_hWndTabView;
public:
// NOTE: This is here for backwards compatibility.
// Use the new CTFI_TABVIEW instead
typedef enum FieldFlags
{
eCustomTabItem_TabView = CTFI_TABVIEW,
};
// Use CTFI_TABVIEW instead
#if (_MSC_VER >= 1300)
#pragma deprecated(eCustomTabItem_TabView)
#endif
// Constructors/Destructors
public:
CTabViewTabItem() :
m_hWndTabView(NULL)
{
}
CTabViewTabItem(const CTabViewTabItem& rhs)
{
*this = rhs;
}
virtual ~CTabViewTabItem()
{
}
const CTabViewTabItem& operator=(const CTabViewTabItem& rhs)
{
if(&rhs != this)
{
m_rcItem = rhs.m_rcItem;
m_nImage = rhs.m_nImage;
m_sText = rhs.m_sText;
m_sToolTip = rhs.m_sToolTip;
m_bHighlighted = rhs.m_bHighlighted;
m_bCanClose = rhs.m_bCanClose;
m_hWndTabView = rhs.m_hWndTabView;
}
return *this;
}
// Accessors
public:
HWND GetTabView() const
{
return m_hWndTabView;
}
bool SetTabView(HWND hWnd = NULL)
{
m_hWndTabView = hWnd;
return true;
}
// Methods:
public:
bool UsingTabView() const
{
return (m_hWndTabView != NULL);
}
bool MatchItem(CTabViewTabItem* pItem, DWORD eFlags) const
{
bool bMatch = true;
if(eFlags == CTFI_TABVIEW)
{
// Make the common case a little faster
// (searching only for a match to the "tab view" HWND)
bMatch = (m_hWndTabView == pItem->m_hWndTabView);
}
else
{
// Do an extensive comparison
bMatch = baseClass::MatchItem(pItem, eFlags);
if(bMatch && (eFlags & CTFI_TABVIEW) == CTFI_TABVIEW)
{
bMatch = (m_hWndTabView == pItem->m_hWndTabView);
}
}
if(bMatch)
{
*pItem = *this;
}
return bMatch;
}
};
#if (_ATL_VER < 0x0700)
// With ATL 7, CAtlArray was introduced which is better than
// CSimpleArray. Among other things, it supports inserting
// items in any place. If this code is compiled under ATL 7,
// we'll use the real CAtlArray. If this is compiled under ATL 3,
// we'll use a "fake" CAtlArray where we implement the
// functionality we're using that the real CAtlArray provides.
//
// Important! This isn't the real ATL 7 CAtlArray.
// We inherit from CSimpleArray as "protected", so that you
// can't call its versions of functions (so you have
// to use the CAtlArray style of functions)
namespace ATL {
template<typename E>
class CAtlArray : protected ATL::CSimpleArray<E>
{
protected:
typedef CAtlArray thisClass;
typedef ATL::CSimpleArray<E> baseClass;
public:
//Real CAtlArray: size_t GetCount() const throw();
size_t GetCount() const
{
return m_nSize;
}
//Real CAtlArray: void InsertAt( size_t iElement, INARGTYPE element, size_t nCount = 1 );
void InsertAt( size_t nIndex, E& element )
{
if(m_nSize == m_nAllocSize)
{
E* aT;
int nNewAllocSize = (m_nAllocSize == 0) ? 1 : (m_nSize * 2);
aT = (E*)realloc(m_aT, nNewAllocSize * sizeof(E));
if(aT == NULL)
return; // FALSE;
m_nAllocSize = nNewAllocSize;
m_aT = aT;
}
memmove((void*)&m_aT[nIndex+1], (void*)&m_aT[nIndex], (m_nSize - nIndex ) * sizeof(E));
m_nSize++;
SetAtIndex(nIndex, element);
//return TRUE;
}
//Real CAtlArray: void RemoveAt( size_t iElement, size_t nCount = 1 );
void RemoveAt( size_t nIndex )
{
// This is an improvement over CSimpleArray::RemoveAt suggested
// by Jim Springfield on the ATL discussion list
m_aT[nIndex].~E();
if((int)nIndex != (m_nSize - 1))
{
memmove((void*)&m_aT[nIndex], (void*)&m_aT[nIndex + 1], (m_nSize - (nIndex + 1)) * sizeof(E));
}
m_nSize--;
//return TRUE;
}
//Real CAtlArray: const E& operator[]( size_t iElement ) const throw();
const E& operator[]( size_t iElement ) const
{
ATLASSERT( iElement < (size_t)m_nSize );
return( m_aT[iElement] );
}
//Real CAtlArray: E& operator[]( size_t iElement ) throw();
E& operator[]( size_t iElement )
{
ATLASSERT( iElement < (size_t)m_nSize );
return( m_aT[iElement] );
}
};
}; // namespace ATL
#endif // (_ATL_VER < 0x0700)
typedef ATL::CWinTraits<WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | CTCS_TOOLTIPS, 0> CCustomTabCtrlWinTraits;
template <class T, class TItem = CCustomTabItem, class TBase = ATL::CWindow, class TWinTraits = CCustomTabCtrlWinTraits>
class ATL_NO_VTABLE CCustomTabCtrl :
public ATL::CWindowImpl< T, TBase, TWinTraits >,
public COffscreenDrawRect< T >
{
public:
// Expose the item type (that's a template parameter to this base class)
typedef typename TItem TItem;
protected:
typedef ATL::CWindowImpl< T, TBase, TWinTraits > baseClass;
typedef COffscreenDrawRect< T > offscreenDrawClass;
// Member variables
protected:
int m_iCurSel;
int m_iHotItem;
CTCSETTINGS m_settings;
ATL::CAtlArray< TItem* > m_Items;
WTL::CFont m_font;
WTL::CFont m_fontSel;
WTL::CImageList m_imageList;
WTL::CToolTipCtrl m_tooltip;
RECT m_rcTabItemArea;
RECT m_rcScrollLeft;
RECT m_rcScrollRight;
RECT m_rcCloseButton;
int m_iDragItem;
int m_iDragItemOriginal;
POINT m_ptDragOrigin;
HCURSOR m_hCursorMove;
HCURSOR m_hCursorNoDrop;
int m_iScrollOffset;
// Flags, internal state, etc.
//
// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
// +-------+-------+-------+-------+---+-----------+---------------+
// | FUT | MO | MD | HT |SR | SD | FLAGS |
// +-------+-------+-------+-------+---+-----------+---------------+
//
// FLAGS - boolean flags
// SD - Scroll delta. The number of pixels to move in a single scroll.
// Valid values are 0-63 (the value is bit shifted to/from position).
// SR - Scroll repeat speed. Valid values are no-repeat,
// slow repeat, normal repeat and fast repeat
// HT - Current hot tracked item (if its a tab, then m_iHotItem is the hot tab item)
// MD - Item under mouse when mouse button down message was sent
// but before mouse button up message is sent
// MO - Item current under mouse cursor
// FUT - Not used at this time, but reserved for the future.
DWORD m_dwState;
enum StateBits
{
// Flags
// bits = 0x000000ff
ectcMouseInWindow = 0x00000001,
ectcOverflowLeft = 0x00000002,
ectcOverflowRight = 0x00000004,
//ectcOverflowBottom = 0x00000002, // alias for vertical mode
//ectcOverflowTop = 0x00000004, // alias for vertical mode
ectcEnableRedraw = 0x00000008,
ectcDraggingItem = 0x00000010,
//ectcFlag20 = 0x00000020,
//ectcFlag40 = 0x00000040,
//ectcFlag80 = 0x00000080,
// Scroll
// bits = 0x0000ff00
ectcScrollDeltaMask = 0x00003f00, //0011 1111
ectcScrollDeltaShift = 8,
// We have to publicly expose these:
ectcScrollRepeat = 0x0000c000, //1100 0000
//ectcScrollRepeat_None = 0x00000000,
//ectcScrollRepeat_Slow = 0x00004000, //0100 0000
//ectcScrollRepeat_Normal = 0x00008000, //1000 0000
//ectcScrollRepeat_Fast = 0x0000c000, //1100 0000
// Hot Tracking
// bits = 0x000f0000
ectcHotTrack = 0x000f0000,
ectcHotTrack_CloseButton = 0x00010000,
ectcHotTrack_ScrollRight = 0x00020000,
ectcHotTrack_ScrollLeft = 0x00030000,
ectcHotTrack_TabItem = 0x00040000,
// Mouse Down
// bits = 0x00f00000
ectcMouseDown = 0x00f00000,
ectcMouseDownL_CloseButton = 0x00100000,
ectcMouseDownL_ScrollRight = 0x00200000,
ectcMouseDownL_ScrollLeft = 0x00300000,
ectcMouseDownL_TabItem = 0x00400000,
ectcMouseDownR_CloseButton = 0x00900000,
ectcMouseDownR_ScrollRight = 0x00a00000,
ectcMouseDownR_ScrollLeft = 0x00b00000,
ectcMouseDownR_TabItem = 0x00c00000,
// Mouse Over
// bits = 0x0f000000
ectcMouseOver = 0x0f000000,
ectcMouseOver_CloseButton = 0x01000000,
ectcMouseOver_ScrollRight = 0x02000000,
ectcMouseOver_ScrollLeft = 0x03000000,
ectcMouseOver_TabItem = 0x04000000,
};
enum ButtonToolTipIDs
{
ectcToolTip_Close = 0xFFFFFFF0,
ectcToolTip_ScrollRight = 0xFFFFFFF1,
ectcToolTip_ScrollLeft = 0xFFFFFFF2,
};
enum TimerIDs
{
ectcTimer_ScrollLeft = 0x00000010,
ectcTimer_ScrollRight = 0x00000020,
};
// Public enumerations
public:
enum ScrollRepeat
{
ectcScrollRepeat_None = 0x00000000,
ectcScrollRepeat_Slow = 0x00004000,
ectcScrollRepeat_Normal = 0x00008000,
ectcScrollRepeat_Fast = 0x0000c000,
};
// Constructors
public:
CCustomTabCtrl() :
m_iCurSel(-1),
m_iHotItem(-1),
m_dwState(0),
m_iDragItem(-1),
m_iDragItemOriginal(-1),
m_hCursorMove(NULL),
m_hCursorNoDrop(NULL),
m_iScrollOffset(0)
{
::ZeroMemory(&m_settings, sizeof(CTCSETTINGS));
::ZeroMemory(&m_ptDragOrigin, sizeof(POINT));
::SetRectEmpty(&m_rcTabItemArea);
::SetRectEmpty(&m_rcCloseButton);
::SetRectEmpty(&m_rcScrollLeft);
::SetRectEmpty(&m_rcScrollRight);
m_dwState |= ((40 << ectcScrollDeltaShift) & ectcScrollDeltaMask);
m_dwState |= ectcScrollRepeat_Normal;
m_dwState |= ectcEnableRedraw;
}
// Implementation
protected:
void InitializeTooltips(void)
{
ATLASSERT(!m_tooltip.IsWindow());
if(!m_tooltip.IsWindow())
{
// Be sure InitCommonControlsEx is called before this,
// with one of the flags that includes the tooltip control
m_tooltip.Create(m_hWnd, NULL, NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP /* | TTS_BALLOON */, WS_EX_TOOLWINDOW);
if(m_tooltip.IsWindow())
{
m_tooltip.SetWindowPos(HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
m_tooltip.SetDelayTime(TTDT_INITIAL, ::GetDoubleClickTime());
m_tooltip.SetDelayTime(TTDT_AUTOPOP, ::GetDoubleClickTime() * 20);
m_tooltip.SetDelayTime(TTDT_RESHOW, ::GetDoubleClickTime() / 5);
}
}
}
void ActivateTooltips(BOOL bActivate = TRUE)
{
ATLASSERT(m_tooltip.IsWindow());
if(m_tooltip.IsWindow())
{
m_tooltip.Activate(bActivate);
}
}
void ClearCurrentHotTracking(bool bRedrawEffectedArea = true)
{
switch(m_dwState & ectcHotTrack)
{
case ectcHotTrack_CloseButton:
m_dwState &= ~ectcHotTrack;
if(bRedrawEffectedArea)
{
this->InvalidateRect(&m_rcCloseButton);
}
break;
case ectcHotTrack_ScrollRight:
m_dwState &= ~ectcHotTrack;
if(bRedrawEffectedArea)
{
this->InvalidateRect(&m_rcScrollRight);
}
break;
case ectcHotTrack_ScrollLeft:
m_dwState &= ~ectcHotTrack;
if(bRedrawEffectedArea)
{
this->InvalidateRect(&m_rcScrollLeft);
}
break;
case ectcHotTrack_TabItem:
m_dwState &= ~ectcHotTrack;
m_iHotItem = -1;
if(bRedrawEffectedArea)
{
// In case the derived class actually changes the width
// of a hot tracked tab, invalidate the whole tab window
this->Invalidate();
}
break;
default:
m_dwState &= ~ectcHotTrack;
m_iHotItem = -1;
if(bRedrawEffectedArea)
{
this->Invalidate();
}
break;
}
}
void ClearCurrentMouseDownTracking(bool bRedrawEffectedArea = true)
{
switch(m_dwState & ectcMouseDown)
{
case ectcMouseDownL_CloseButton:
case ectcMouseDownR_CloseButton:
m_dwState &= ~ectcMouseDown;
if(bRedrawEffectedArea)
{
this->InvalidateRect(&m_rcCloseButton);
}
break;
case ectcMouseDownL_ScrollRight:
case ectcMouseDownR_ScrollRight:
m_dwState &= ~ectcMouseDown;
if(bRedrawEffectedArea)
{
this->InvalidateRect(&m_rcScrollRight);
}
break;
case ectcMouseDownL_ScrollLeft: