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
|
/********************************************************************
*
* Copyright (c) 2002 Sven Wiegand <mail@sven-wiegand.de>
*
* You can use this and modify this in any way you want,
* BUT LEAVE THIS HEADER INTACT.
*
* Redistribution is appreciated.
*
* $Workfile:$
* $Revision: 126 $
* $Modtime:$
* $Author: gabest $
*
* Revision History:
* $History:$
*
*********************************************************************/
#include "stdafx.h"
#include "TreePropSheet.h"
#include "PropPageFrameDefault.h"
namespace TreePropSheet
{
//-------------------------------------------------------------------
// class CTreePropSheet
//-------------------------------------------------------------------
BEGIN_MESSAGE_MAP(CTreePropSheet, CPropertySheet)
//{{AFX_MSG_MAP(CTreePropSheet)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
ON_MESSAGE(PSM_ADDPAGE, OnAddPage)
ON_MESSAGE(PSM_REMOVEPAGE, OnRemovePage)
ON_MESSAGE(PSM_SETCURSEL, OnSetCurSel)
ON_MESSAGE(PSM_SETCURSELID, OnSetCurSelId)
ON_MESSAGE(PSM_ISDIALOGMESSAGE, OnIsDialogMessage)
ON_NOTIFY(TVN_SELCHANGINGA, s_unPageTreeId, OnPageTreeSelChanging)
ON_NOTIFY(TVN_SELCHANGINGW, s_unPageTreeId, OnPageTreeSelChanging)
ON_NOTIFY(TVN_SELCHANGEDA, s_unPageTreeId, OnPageTreeSelChanged)
ON_NOTIFY(TVN_SELCHANGEDW, s_unPageTreeId, OnPageTreeSelChanged)
END_MESSAGE_MAP()
IMPLEMENT_DYNAMIC(CTreePropSheet, CPropertySheet)
const UINT CTreePropSheet::s_unPageTreeId = 0x7EEE;
CTreePropSheet::CTreePropSheet()
: m_bPageTreeSelChangedActive(FALSE),
m_bTreeViewMode(TRUE),
m_bPageCaption(FALSE),
m_bTreeImages(FALSE),
m_nPageTreeWidth(150),
m_pwndPageTree(NULL),
m_pFrame(NULL)
{}
CTreePropSheet::CTreePropSheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
: CPropertySheet(nIDCaption, pParentWnd, iSelectPage),
m_bPageTreeSelChangedActive(FALSE),
m_bTreeViewMode(TRUE),
m_bPageCaption(FALSE),
m_bTreeImages(FALSE),
m_nPageTreeWidth(150),
m_pwndPageTree(NULL),
m_pFrame(NULL)
{
}
CTreePropSheet::CTreePropSheet(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
: CPropertySheet(pszCaption, pParentWnd, iSelectPage),
m_bPageTreeSelChangedActive(FALSE),
m_bTreeViewMode(TRUE),
m_bPageCaption(FALSE),
m_bTreeImages(FALSE),
m_nPageTreeWidth(150),
m_pwndPageTree(NULL),
m_pFrame(NULL)
{
}
CTreePropSheet::~CTreePropSheet()
{
if(m_Images.GetSafeHandle()) m_Images.DeleteImageList();
delete m_pwndPageTree, m_pwndPageTree = NULL;
delete m_pFrame, m_pFrame = NULL;
}
/////////////////////////////////////////////////////////////////////
// Operationen
BOOL CTreePropSheet::SetTreeViewMode(BOOL bTreeViewMode /* = TRUE */, BOOL bPageCaption /* = FALSE */, BOOL bTreeImages /* = FALSE */)
{
if(IsWindow(m_hWnd))
{
// needs to becalled, before the window has been created
ASSERT(FALSE);
return FALSE;
}
m_bTreeViewMode = bTreeViewMode;
if(m_bTreeViewMode)
{
m_bPageCaption = bPageCaption;
m_bTreeImages = bTreeImages;
}
return TRUE;
}
BOOL CTreePropSheet::SetTreeWidth(int nWidth)
{
if(IsWindow(m_hWnd))
{
// needs to be called, before the window is created.
ASSERT(FALSE);
return FALSE;
}
m_nPageTreeWidth = nWidth;
return TRUE;
}
void CTreePropSheet::SetEmptyPageText(LPCTSTR lpszEmptyPageText)
{
m_strEmptyPageMessage = lpszEmptyPageText;
}
DWORD CTreePropSheet::SetEmptyPageTextFormat(DWORD dwFormat)
{
DWORD dwPrevFormat = m_pFrame->GetMsgFormat();
m_pFrame->SetMsgFormat(dwFormat);
return dwPrevFormat;
}
BOOL CTreePropSheet::SetTreeDefaultImages(CImageList *pImages)
{
if(pImages->GetImageCount() != 2)
{
ASSERT(FALSE);
return FALSE;
}
if(m_DefaultImages.GetSafeHandle())
m_DefaultImages.DeleteImageList();
m_DefaultImages.Create(pImages);
// update, if necessary
if(IsWindow(m_hWnd))
RefillPageTree();
return TRUE;
}
BOOL CTreePropSheet::SetTreeDefaultImages(UINT unBitmapID, int cx, COLORREF crMask)
{
if(m_DefaultImages.GetSafeHandle())
m_DefaultImages.DeleteImageList();
if(!m_DefaultImages.Create(unBitmapID, cx, 0, crMask))
return FALSE;
if(m_DefaultImages.GetImageCount() != 2)
{
m_DefaultImages.DeleteImageList();
return FALSE;
}
return TRUE;
}
CTreeCtrl* CTreePropSheet::GetPageTreeControl()
{
return m_pwndPageTree;
}
/////////////////////////////////////////////////////////////////////
// public helpers
BOOL CTreePropSheet::SetPageIcon(CPropertyPage *pPage, HICON hIcon)
{
pPage->m_psp.dwFlags |= PSP_USEHICON;
pPage->m_psp.hIcon = hIcon;
return TRUE;
}
BOOL CTreePropSheet::SetPageIcon(CPropertyPage *pPage, UINT unIconId)
{
HICON hIcon = AfxGetApp()->LoadIcon(unIconId);
if(!hIcon)
return FALSE;
return SetPageIcon(pPage, hIcon);
}
BOOL CTreePropSheet::SetPageIcon(CPropertyPage *pPage, CImageList &Images, int nImage)
{
HICON hIcon = Images.ExtractIcon(nImage);
if(!hIcon)
return FALSE;
return SetPageIcon(pPage, hIcon);
}
BOOL CTreePropSheet::DestroyPageIcon(CPropertyPage *pPage)
{
if(!pPage || !(pPage->m_psp.dwFlags & PSP_USEHICON) || !pPage->m_psp.hIcon)
return FALSE;
DestroyIcon(pPage->m_psp.hIcon);
pPage->m_psp.dwFlags &= ~PSP_USEHICON;
pPage->m_psp.hIcon = NULL;
return TRUE;
}
/////////////////////////////////////////////////////////////////////
// Overridable implementation helpers
CString CTreePropSheet::GenerateEmptyPageMessage(LPCTSTR lpszEmptyPageMessage, LPCTSTR lpszCaption)
{
CString strMsg;
strMsg.Format(lpszEmptyPageMessage, lpszCaption);
return strMsg;
}
CTreeCtrl* CTreePropSheet::CreatePageTreeObject()
{
return DNew CTreeCtrl;
}
CPropPageFrame* CTreePropSheet::CreatePageFrame()
{
return DNew CPropPageFrameDefault;
}
/////////////////////////////////////////////////////////////////////
// Implementation helpers
void CTreePropSheet::MoveChildWindows(int nDx, int nDy)
{
CWnd *pWnd = GetWindow(GW_CHILD);
while(pWnd)
{
CRect rect;
pWnd->GetWindowRect(rect);
rect.OffsetRect(nDx, nDy);
ScreenToClient(rect);
pWnd->MoveWindow(rect);
pWnd = pWnd->GetNextWindow();
}
}
void CTreePropSheet::RefillPageTree()
{
if(!IsWindow(m_hWnd))
return;
m_pwndPageTree->DeleteAllItems();
CTabCtrl *pTabCtrl = GetTabControl();
if(!IsWindow(pTabCtrl->GetSafeHwnd()))
{
ASSERT(FALSE);
return;
}
const int nPageCount = pTabCtrl->GetItemCount();
// rebuild image list
if(m_bTreeImages)
{
for(int i = m_Images.GetImageCount() - 1; i >= 0; --i)
m_Images.Remove(i);
// add page images
CImageList *pPageImages = pTabCtrl->GetImageList();
if(pPageImages)
{
for(int nImage = 0; nImage < pPageImages->GetImageCount(); ++nImage)
{
HICON hIcon = pPageImages->ExtractIcon(nImage);
m_Images.Add(hIcon);
DestroyIcon(hIcon);
}
}
// add default images
if(m_DefaultImages.GetSafeHandle())
{
HICON hIcon;
// add default images
hIcon = m_DefaultImages.ExtractIcon(0);
if(hIcon)
{
m_Images.Add(hIcon);
DestroyIcon(hIcon);
}
hIcon = m_DefaultImages.ExtractIcon(1);
{
m_Images.Add(hIcon);
DestroyIcon(hIcon);
}
}
}
// insert tree items
for(int nPage = 0; nPage < nPageCount; ++nPage)
{
// Get title and image of the page
CString strPagePath;
TCITEM ti;
ZeroMemory(&ti, sizeof(ti));
ti.mask = TCIF_TEXT | TCIF_IMAGE;
ti.cchTextMax = MAX_PATH;
ti.pszText = strPagePath.GetBuffer(ti.cchTextMax);
ASSERT(ti.pszText);
if(!ti.pszText)
return;
pTabCtrl->GetItem(nPage, &ti);
strPagePath.ReleaseBuffer();
// Create an item in the tree for the page
HTREEITEM hItem = CreatePageTreeItem(ti.pszText);
ASSERT(hItem);
if(hItem)
{
m_pwndPageTree->SetItemData(hItem, nPage);
// set image
if(m_bTreeImages)
{
int nImage = ti.iImage;
if(nImage < 0 || nImage >= m_Images.GetImageCount())
nImage = m_DefaultImages.GetSafeHandle() ? m_Images.GetImageCount() - 1 : -1;
m_pwndPageTree->SetItemImage(hItem, nImage, nImage);
}
}
}
}
HTREEITEM CTreePropSheet::CreatePageTreeItem(LPCTSTR lpszPath, HTREEITEM hParent /* = TVI_ROOT */)
{
CString strPath(lpszPath);
CString strTopMostItem(SplitPageTreePath(strPath));
// Check if an item with the given text does already exist
HTREEITEM hItem = NULL;
HTREEITEM hChild = m_pwndPageTree->GetChildItem(hParent);
while(hChild)
{
if(m_pwndPageTree->GetItemText(hChild) == strTopMostItem)
{
hItem = hChild;
break;
}
hChild = m_pwndPageTree->GetNextItem(hChild, TVGN_NEXT);
}
// If item with that text does not already exist, create a new one
if(!hItem)
{
hItem = m_pwndPageTree->InsertItem(strTopMostItem, hParent);
m_pwndPageTree->SetItemData(hItem, -1);
if(!strPath.IsEmpty() && m_bTreeImages && m_DefaultImages.GetSafeHandle())
// set folder image
m_pwndPageTree->SetItemImage(hItem, m_Images.GetImageCount() - 2, m_Images.GetImageCount() - 2);
}
if(!hItem)
{
ASSERT(FALSE);
return NULL;
}
if(strPath.IsEmpty())
return hItem;
else
return CreatePageTreeItem(strPath, hItem);
}
CString CTreePropSheet::SplitPageTreePath(CString &strRest)
{
int nSeperatorPos = 0;
while(TRUE)
{
nSeperatorPos = strRest.Find(_T("::"), nSeperatorPos);
if(nSeperatorPos == -1)
{
CString strItem(strRest);
strRest.Empty();
return strItem;
}
else if(nSeperatorPos > 0)
{
// if there is an odd number of backslashes infront of the
// seperator, than do not interpret it as separator
int nBackslashCount = 0;
for(int nPos = nSeperatorPos - 1; nPos >= 0 && strRest[nPos] == _T('\\'); --nPos, ++nBackslashCount);
if(nBackslashCount % 2 == 0)
break;
else
++nSeperatorPos;
}
}
CString strItem(strRest.Left(nSeperatorPos));
strItem.Replace(_T("\\::"), _T("::"));
strItem.Replace(_T("\\\\"), _T("\\"));
strRest = strRest.Mid(nSeperatorPos + 2);
return strItem;
}
BOOL CTreePropSheet::KillActiveCurrentPage()
{
HWND hCurrentPage = PropSheet_GetCurrentPageHwnd(m_hWnd);
if(!IsWindow(hCurrentPage))
{
ASSERT(FALSE);
return TRUE;
}
// Check if the current page is really active (if page is invisible
// an virtual empty page is the active one.
if(!::IsWindowVisible(hCurrentPage))
return TRUE;
// Try to deactivate current page
PSHNOTIFY pshn;
pshn.hdr.code = PSN_KILLACTIVE;
pshn.hdr.hwndFrom = m_hWnd;
pshn.hdr.idFrom = GetDlgCtrlID();
pshn.lParam = 0;
if(::SendMessage(hCurrentPage, WM_NOTIFY, pshn.hdr.idFrom, (LPARAM)&pshn))
// current page does not allow page change
return FALSE;
// Hide the page
::ShowWindow(hCurrentPage, SW_HIDE);
return TRUE;
}
HTREEITEM CTreePropSheet::GetPageTreeItem(int nPage, HTREEITEM hRoot /* = TVI_ROOT */)
{
// Special handling for root case
if(hRoot == TVI_ROOT)
hRoot = m_pwndPageTree->GetNextItem(NULL, TVGN_ROOT);
// Check parameters
if(nPage < 0 || nPage >= GetPageCount())
{
ASSERT(FALSE);
return NULL;
}
if(hRoot == NULL)
{
ASSERT(FALSE);
return NULL;
}
// we are performing a simple linear search here, because we are
// expecting only little data
HTREEITEM hItem = hRoot;
while(hItem)
{
if((signed)m_pwndPageTree->GetItemData(hItem) == nPage)
return hItem;
if(m_pwndPageTree->ItemHasChildren(hItem))
{
HTREEITEM hResult = GetPageTreeItem(nPage, m_pwndPageTree->GetNextItem(hItem, TVGN_CHILD));
if(hResult)
return hResult;
}
hItem = m_pwndPageTree->GetNextItem(hItem, TVGN_NEXT);
}
// we've found nothing, if we arrive here
return hItem;
}
BOOL CTreePropSheet::SelectPageTreeItem(int nPage)
{
HTREEITEM hItem = GetPageTreeItem(nPage);
if(!hItem)
return FALSE;
return m_pwndPageTree->SelectItem(hItem);
}
BOOL CTreePropSheet::SelectCurrentPageTreeItem()
{
CTabCtrl *pTab = GetTabControl();
if(!IsWindow(pTab->GetSafeHwnd()))
return FALSE;
return SelectPageTreeItem(pTab->GetCurSel());
}
void CTreePropSheet::UpdateCaption()
{
HWND hPage = PropSheet_GetCurrentPageHwnd(GetSafeHwnd());
BOOL bRealPage = IsWindow(hPage) && ::IsWindowVisible(hPage);
HTREEITEM hItem = m_pwndPageTree->GetSelectedItem();
if(!hItem)
return;
CString strCaption = m_pwndPageTree->GetItemText(hItem);
// if empty page, then update empty page message
if(!bRealPage)
m_pFrame->SetMsgText(GenerateEmptyPageMessage(m_strEmptyPageMessage, strCaption));
// if no captions are displayed, cancel here
if(!m_pFrame->GetShowCaption())
return;
// get tab control, to the the images from
CTabCtrl *pTabCtrl = GetTabControl();
if(!IsWindow(pTabCtrl->GetSafeHwnd()))
{
ASSERT(FALSE);
return;
}
if(m_bTreeImages)
{
// get image from tree
int nImage;
m_pwndPageTree->GetItemImage(hItem, nImage, nImage);
HICON hIcon = m_Images.ExtractIcon(nImage);
m_pFrame->SetCaption(strCaption, hIcon);
if(hIcon)
DestroyIcon(hIcon);
}
else if(bRealPage)
{
// get image from hidden (original) tab provided by the original
// implementation
CImageList *pImages = pTabCtrl->GetImageList();
if(pImages)
{
TCITEM ti;
ZeroMemory(&ti, sizeof(ti));
ti.mask = TCIF_IMAGE;
HICON hIcon = NULL;
if(pTabCtrl->GetItem((int)m_pwndPageTree->GetItemData(hItem), &ti))
hIcon = pImages->ExtractIcon(ti.iImage);
m_pFrame->SetCaption(strCaption, hIcon);
if(hIcon)
DestroyIcon(hIcon);
}
else
m_pFrame->SetCaption(strCaption);
}
else
m_pFrame->SetCaption(strCaption);
}
void CTreePropSheet::ActivatePreviousPage()
{
if(!IsWindow(m_hWnd))
return;
if(!IsWindow(m_pwndPageTree->GetSafeHwnd()))
{
// normal tab property sheet. Simply use page index
int nPageIndex = GetActiveIndex();
if(nPageIndex < 0 || nPageIndex >= GetPageCount())
return;
int nPrevIndex = (nPageIndex == 0) ? GetPageCount() - 1 : nPageIndex - 1;
SetActivePage(nPrevIndex);
}
else
{
// property sheet with page tree.
// we need a more sophisticated handling here, than simply using
// the page index, because we won't skip empty pages.
// so we have to walk the page tree
HTREEITEM hItem = m_pwndPageTree->GetSelectedItem();
ASSERT(hItem);
if(!hItem)
return;
HTREEITEM hPrevItem = NULL;
if(hPrevItem = m_pwndPageTree->GetPrevSiblingItem(hItem))
{
while(m_pwndPageTree->ItemHasChildren(hPrevItem))
{
hPrevItem = m_pwndPageTree->GetChildItem(hPrevItem);
while(m_pwndPageTree->GetNextSiblingItem(hPrevItem))
hPrevItem = m_pwndPageTree->GetNextSiblingItem(hPrevItem);
}
}
else
hPrevItem = m_pwndPageTree->GetParentItem(hItem);
if(!hPrevItem)
{
// no prev item, so cycle to the last item
hPrevItem = m_pwndPageTree->GetRootItem();
while(TRUE)
{
while(m_pwndPageTree->GetNextSiblingItem(hPrevItem))
hPrevItem = m_pwndPageTree->GetNextSiblingItem(hPrevItem);
if(m_pwndPageTree->ItemHasChildren(hPrevItem))
hPrevItem = m_pwndPageTree->GetChildItem(hPrevItem);
else
break;
}
}
if(hPrevItem)
m_pwndPageTree->SelectItem(hPrevItem);
}
}
void CTreePropSheet::ActivateNextPage()
{
if(!IsWindow(m_hWnd))
return;
if(!IsWindow(m_pwndPageTree->GetSafeHwnd()))
{
// normal tab property sheet. Simply use page index
int nPageIndex = GetActiveIndex();
if(nPageIndex < 0 || nPageIndex >= GetPageCount())
return;
int nNextIndex = (nPageIndex == GetPageCount() - 1) ? 0 : nPageIndex + 1;
SetActivePage(nNextIndex);
}
else
{
// property sheet with page tree.
// we need a more sophisticated handling here, than simply using
// the page index, because we won't skip empty pages.
// so we have to walk the page tree
HTREEITEM hItem = m_pwndPageTree->GetSelectedItem();
ASSERT(hItem);
if(!hItem)
return;
HTREEITEM hNextItem = NULL;
if(hNextItem = m_pwndPageTree->GetChildItem(hItem))
;
else if(hNextItem = m_pwndPageTree->GetNextSiblingItem(hItem))
;
else if(m_pwndPageTree->GetParentItem(hItem))
{
while(!hNextItem)
{
hItem = m_pwndPageTree->GetParentItem(hItem);
if(!hItem)
break;
hNextItem = m_pwndPageTree->GetNextSiblingItem(hItem);
}
}
if(!hNextItem)
// no next item -- so cycle to the first item
hNextItem = m_pwndPageTree->GetRootItem();
if(hNextItem)
m_pwndPageTree->SelectItem(hNextItem);
}
}
/////////////////////////////////////////////////////////////////////
// Overridings
BOOL CTreePropSheet::OnInitDialog()
{
if(m_bTreeViewMode)
{
// be sure, there are no stacked tabs, because otherwise the
// page caption will be to large in tree view mode
EnableStackedTabs(FALSE);
// Initialize image list.
if(m_DefaultImages.GetSafeHandle())
{
IMAGEINFO ii;
m_DefaultImages.GetImageInfo(0, &ii);
if(ii.hbmImage) DeleteObject(ii.hbmImage);
if(ii.hbmMask) DeleteObject(ii.hbmMask);
m_Images.Create(ii.rcImage.right - ii.rcImage.left, ii.rcImage.bottom - ii.rcImage.top, ILC_COLOR32 | ILC_MASK, 0, 1);
}
else
m_Images.Create(16, 16, ILC_COLOR32 | ILC_MASK, 0, 1);
}
// perform default implementation
BOOL bResult = __super::OnInitDialog();
if(!m_bTreeViewMode)
// stop here, if we would like to use tabs
return bResult;
// Get tab control...
CTabCtrl *pTab = GetTabControl();
if(!IsWindow(pTab->GetSafeHwnd()))
{
ASSERT(FALSE);
return bResult;
}
// ... and hide it
pTab->ShowWindow(SW_HIDE);
pTab->EnableWindow(FALSE);
// Place another (empty) tab ctrl, to get a frame instead
CRect rectFrame;
pTab->GetWindowRect(rectFrame);
ScreenToClient(rectFrame);
m_pFrame = CreatePageFrame();
if(!m_pFrame)
{
ASSERT(FALSE);
AfxThrowMemoryException();
}
m_pFrame->Create(WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, rectFrame, this, 0xFFFF);
m_pFrame->ShowCaption(m_bPageCaption);
// Lets make place for the tree ctrl
const int nTreeWidth = m_nPageTreeWidth;
const int nTreeSpace = 5;
CRect rectSheet;
GetWindowRect(rectSheet);
rectSheet.right += nTreeWidth;
SetWindowPos(NULL, -1, -1, rectSheet.Width(), rectSheet.Height(), SWP_NOZORDER | SWP_NOMOVE);
CenterWindow();
MoveChildWindows(nTreeWidth, 0);
// Lets calculate the rectangle for the tree ctrl
CRect rectTree(rectFrame);
rectTree.right = rectTree.left + nTreeWidth - nTreeSpace;
// calculate caption height
CTabCtrl wndTabCtrl;
wndTabCtrl.Create(WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, rectFrame, this, 0x1234);
wndTabCtrl.InsertItem(0, _T(""));
CRect rectFrameCaption;
wndTabCtrl.GetItemRect(0, rectFrameCaption);
wndTabCtrl.DestroyWindow();
m_pFrame->SetCaptionHeight(rectFrameCaption.Height());
// if no caption should be displayed, make the window smaller in
// height
if(!m_bPageCaption)
{
// make frame smaller
m_pFrame->GetWnd()->GetWindowRect(rectFrame);
ScreenToClient(rectFrame);
rectFrame.top += rectFrameCaption.Height();
m_pFrame->GetWnd()->MoveWindow(rectFrame);
// move all child windows up
MoveChildWindows(0, -rectFrameCaption.Height());
// modify rectangle for the tree ctrl
rectTree.bottom -= rectFrameCaption.Height();
// make us smaller
CRect rect;
GetWindowRect(rect);
rect.top += rectFrameCaption.Height() / 2;
rect.bottom -= rectFrameCaption.Height() - rectFrameCaption.Height() / 2;
// if (GetParent())
// GetParent()->ScreenToClient(rect);
MoveWindow(rect);
}
// finally create tht tree control
const DWORD dwTreeStyle = TVS_SHOWSELALWAYS | TVS_TRACKSELECT | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS;
m_pwndPageTree = CreatePageTreeObject();
if(!m_pwndPageTree)
{
ASSERT(FALSE);
AfxThrowMemoryException();
}
// MFC7-support here (Thanks to Rainer Wollgarten)
#if _MFC_VER >= 0x0700
{
m_pwndPageTree->CreateEx(
WS_EX_CLIENTEDGE | WS_EX_NOPARENTNOTIFY,
WS_TABSTOP | WS_CHILD | WS_VISIBLE | dwTreeStyle,
rectTree, this, s_unPageTreeId);
}
#else
{
m_pwndPageTree->CreateEx(
WS_EX_CLIENTEDGE | WS_EX_NOPARENTNOTIFY,
_T("SysTreeView32"), _T("PageTree"),
WS_TABSTOP | WS_CHILD | WS_VISIBLE | dwTreeStyle,
rectTree, this, s_unPageTreeId);
}
#endif
if(m_bTreeImages)
{
m_pwndPageTree->SetImageList(&m_Images, TVSIL_NORMAL);
m_pwndPageTree->SetImageList(&m_Images, TVSIL_STATE);
}
// Fill the tree ctrl
RefillPageTree();
// Select item for the current page
if(pTab->GetCurSel() > -1)
SelectPageTreeItem(pTab->GetCurSel());
return bResult;
}
void CTreePropSheet::OnDestroy()
{
__super::OnDestroy();
if(m_pwndPageTree && m_pwndPageTree->m_hWnd)
m_pwndPageTree->DestroyWindow();
if(m_pFrame && m_pFrame->GetWnd()->m_hWnd)
m_pFrame->GetWnd()->DestroyWindow();
}
LRESULT CTreePropSheet::OnAddPage(WPARAM wParam, LPARAM lParam)
{
LRESULT lResult = DefWindowProc(PSM_ADDPAGE, wParam, lParam);
if(!m_bTreeViewMode)
return lResult;
RefillPageTree();
SelectCurrentPageTreeItem();
return lResult;
}
LRESULT CTreePropSheet::OnRemovePage(WPARAM wParam, LPARAM lParam)
{
LRESULT lResult = DefWindowProc(PSM_REMOVEPAGE, wParam, lParam);
if(!m_bTreeViewMode)
return lResult;
RefillPageTree();
SelectCurrentPageTreeItem();
return lResult;
}
LRESULT CTreePropSheet::OnSetCurSel(WPARAM wParam, LPARAM lParam)
{
LRESULT lResult = DefWindowProc(PSM_SETCURSEL, wParam, lParam);
if(!m_bTreeViewMode)
return lResult;
SelectCurrentPageTreeItem();
UpdateCaption();
return lResult;
}
LRESULT CTreePropSheet::OnSetCurSelId(WPARAM wParam, LPARAM lParam)
{
LRESULT lResult = DefWindowProc(PSM_SETCURSEL, wParam, lParam);
if(!m_bTreeViewMode)
return lResult;
SelectCurrentPageTreeItem();
UpdateCaption();
return lResult;
}
void CTreePropSheet::OnPageTreeSelChanging(NMHDR *pNotifyStruct, LRESULT *plResult)
{
*plResult = 0;
if(m_bPageTreeSelChangedActive)
return;
else
m_bPageTreeSelChangedActive = TRUE;
NMTREEVIEW *pTvn = reinterpret_cast<NMTREEVIEW*>(pNotifyStruct);
int nPage = m_pwndPageTree->GetItemData(pTvn->itemNew.hItem);
BOOL bResult;
if(nPage < 0 || (unsigned)nPage >= m_pwndPageTree->GetCount())
bResult = KillActiveCurrentPage();
else
bResult = SetActivePage(nPage);
if(!bResult)
// prevent selection to change
*plResult = TRUE;
// Set focus to tree ctrl (I guess that's what the user expects)
m_pwndPageTree->SetFocus();
m_bPageTreeSelChangedActive = FALSE;
return;
}
void CTreePropSheet::OnPageTreeSelChanged(NMHDR *pNotifyStruct, LRESULT *plResult)
{
*plResult = 0;
UpdateCaption();
return;
}
LRESULT CTreePropSheet::OnIsDialogMessage(WPARAM wParam, LPARAM lParam)
{
MSG *pMsg = reinterpret_cast<MSG*>(lParam);
if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_TAB && GetKeyState(VK_CONTROL) & 0x8000)
{
if(GetKeyState(VK_SHIFT) & 0x8000)
ActivatePreviousPage();
else
ActivateNextPage();
return TRUE;
}
return __super::DefWindowProc(PSM_ISDIALOGMESSAGE, wParam, lParam);
}
} //namespace TreePropSheet
|