forked from sebkirche/treelist
/
TreeListCtrl.cpp
956 lines (750 loc) · 29.1 KB
/
TreeListCtrl.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
//*****************************************************************************
//*
//*
//* TreeListCtrl.cpp
//*
//*
//*****************************************************************************
//
// (C) Copyright Anton Zechner 2007
//
// This code creates a Tree-List-Control for MFC
//
#include <Afx.h>
#include <tchar.h>
#include "TreeListCtrl.h"
IMPLEMENT_DYNAMIC(CTreeListCtrl, CWnd)
//*****************************************************************************
//*
//* Create
//*
//*****************************************************************************
// Creates a new Tree-List-Control
// dwStyle : Style of the Window
// TVS_CHECKBOXES
// TVS_DISABLEDRAGDROP
// TVS_EDITLABELS
// TVS_FULLROWSELECT
// TVS_HASBUTTONS
// TVS_HASLINES
// TVS_INFOTIP
// TVS_LINESATROOT
// TVS_NONEVENHEIGHT
// TVS_NOSCROLL
// TVS_NOTOOLTIPS
// TVS_RTLREADING
// TVS_SHOWSELALWAYS
// TVS_SINGLEEXPAND
// TVS_TRACKSELECT
// sRect : Rectangle where the window should be shown
// pParentWnd : Pointer to parent window
// nId : Is the ID of the window
// Returns TRUE if the window was created or FALSE if an error occurs
BOOL CTreeListCtrl::Create(DWORD dwStyle, const RECT &sRect, CWnd *pParentWnd, UINT nId) {
TreeListRegister(AfxGetInstanceHandle());
return CWnd::Create(_T("TreeList"), NULL, dwStyle, sRect, pParentWnd, nId);
}
//*****************************************************************************
//*
//* Create
//*
//*****************************************************************************
// Creates a new Tree-List-Control over a dialog item
// dwExStyle : Is the exdended style of the window (WS_EX_????)
// dwStyle : Style of the Window
// TVS_CHECKBOXES
// TVS_DISABLEDRAGDROP
// TVS_EDITLABELS
// TVS_FULLROWSELECT
// TVS_HASBUTTONS
// TVS_HASLINES
// TVS_INFOTIP
// TVS_LINESATROOT
// TVS_NONEVENHEIGHT
// TVS_NOSCROLL
// TVS_NOTOOLTIPS
// TVS_RTLREADING
// TVS_SHOWSELALWAYS
// TVS_SINGLEEXPAND
// TVS_TRACKSELECT
// uItem : Is the ID of the dialog item
// pDlg : Pointer to parent dialog
// nId : Is the ID of the window
// Returns TRUE if the window was created or FALSE if an error occurs
BOOL CTreeListCtrl::Create(DWORD dwStyle, UINT uItem, CDialog *pDlg, UINT nId) {
return CreateEx(0, dwStyle, uItem, pDlg, nId);
}
//*****************************************************************************
//*
//* CreateEx
//*
//*****************************************************************************
// Creates a new Tree-List-Control
// dwExStyle : Is the exdended style of the window (WS_EX_????)
// dwStyle : Style of the Window
// TVS_CHECKBOXES
// TVS_DISABLEDRAGDROP
// TVS_EDITLABELS
// TVS_FULLROWSELECT
// TVS_HASBUTTONS
// TVS_HASLINES
// TVS_INFOTIP
// TVS_LINESATROOT
// TVS_NONEVENHEIGHT
// TVS_NOSCROLL
// TVS_NOTOOLTIPS
// TVS_RTLREADING
// TVS_SHOWSELALWAYS
// TVS_SINGLEEXPAND
// TVS_TRACKSELECT
// sRect : Rectangle where the window should be shown
// pParentWnd : Pointer to parent window
// nId : Is the ID of the window
// Returns TRUE if the window was created or FALSE if an error occurs
BOOL CTreeListCtrl::CreateEx(DWORD dwExStyle, DWORD dwStyle, const RECT &sRect, CWnd *pParentWnd, UINT nId) {
TreeListRegister(AfxGetInstanceHandle());
return CWnd::CreateEx(dwExStyle, _T("TreeList"), NULL, dwStyle, sRect, pParentWnd, nId);
}
//*****************************************************************************
//*
//* CreateEx
//*
//*****************************************************************************
// Creates a new Tree-List-Control over a dialog item
// dwExStyle : Is the exdended style of the window (WS_EX_????)
// dwStyle : Style of the Window
// TVS_CHECKBOXES
// TVS_DISABLEDRAGDROP
// TVS_EDITLABELS
// TVS_FULLROWSELECT
// TVS_HASBUTTONS
// TVS_HASLINES
// TVS_INFOTIP
// TVS_LINESATROOT
// TVS_NONEVENHEIGHT
// TVS_NOSCROLL
// TVS_NOTOOLTIPS
// TVS_RTLREADING
// TVS_SHOWSELALWAYS
// TVS_SINGLEEXPAND
// TVS_TRACKSELECT
// uItem : Is the ID of the dialog item
// pDlg : Pointer to parent dialog
// nId : Is the ID of the window
// Returns TRUE if the window was created or FALSE if an error occurs
BOOL CTreeListCtrl::CreateEx(DWORD dwExStyle, DWORD dwStyle, UINT uItem, CDialog *pDlg, UINT nId) {
CPoint sPoint(0, 0);
CRect sRect;
CWnd *pWnd;
pWnd = pDlg->GetDlgItem(uItem);
ASSERT(pWnd != 0);
pWnd->GetWindowRect(&sRect);
pDlg->ClientToScreen(&sPoint);
sRect.left -= sPoint.x;
sRect.right -= sPoint.x;
sRect.top -= sPoint.y;
sRect.bottom -= sPoint.y;
return CTreeListCtrl::CreateEx(dwExStyle, dwStyle | WS_CHILD | WS_VISIBLE, sRect, pDlg, nId);
}
//*****************************************************************************
//*
//* InsertColumn
//*
//*****************************************************************************
// Inserts a column in the header of the Tree-List-Control
// nCol : Position where the column should be inserted (use -1 for end)
// pColumnText : Text for the column
// nFormat : Ist the format for the text
// TVCFMT_CENTER
// TVCFMT_IMAGE
// TVCFMT_RIGHT
// TVCFMT_LEFT
// TVCFMT_MARK
// nWidth : Is the width for the column in pixels.
// A negative value means is a autoexpanting column.
// The value of abs(nWidth) is the weight of the collumn.
// nMin : Ist the minimal size for the column.
// A negative value means abs(nMin) the minimal size
// is only for dynamic size changing.
// Use 0x8000 for no minimal size
// Returns the position of the column or -1 if an error occurs
int CTreeListCtrl::InsertColumn(int nCol, LPCTSTR pColumnText, int nFormat, int nWidth, int nMin) {
TV_COLUMN sItem;
ASSERT(::IsWindow(m_hWnd));
sItem.mask = 0;
if(pColumnText) {
sItem.mask |= TVCF_TEXT;
sItem.pszText = (LPTSTR)pColumnText;
sItem.cchTextMax = 256;
}
if(nFormat >= 0) {
sItem.mask |= TVCF_FMT | TVCF_MARK;
sItem.fmt = nFormat;
}
if(nMin != 0x8000) {
sItem.mask |= TVCF_MIN;
sItem.iOrder = nMin;
}
if(nWidth >= 0) {
sItem.mask |= TVCF_WIDTH;
sItem.cx = nWidth;
} else {
sItem.mask |= TVCF_VWIDTH;
sItem.cx = -nWidth;
}
return InsertColumn(nCol, &sItem);
}
//*****************************************************************************
//*
//* SetItem
//*
//*****************************************************************************
// Changes some properties of an item
// hItem : is the handle of the item
// nCol : is the column for the text (0=tree column)
// nMask : defies which propaties should be set (TVIF_????)
// pText : is the new text of the item (add TVIF_TEXT to nMask)
// nImage : is the new image for the item (add TVIF_IMAGE to nMask)
// nSelectedImage : is the new image for the item (add TVIF_SELECTEDIMAGE to nMask)
// nState : is the new state for the item (add TVIF_STATE to nMask)
// nStateMask : is a mask for the states
// lParam : is the new LPARAM parameter for the item (add TVIF_PARAM to nMask)
// Returns TRUE if the text was changed or FALSE if an error occurs
BOOL CTreeListCtrl::SetItem(HTREEITEM hItem, int nCol, UINT nMask, LPCTSTR pText, int nImage, int nSelectedImage, UINT nState, UINT nStateMask, LPARAM lParam) {
TV_ITEM sItem;
ASSERT(::IsWindow(m_hWnd));
sItem.mask = nMask | TVIF_SUBITEM;
sItem.hItem = hItem;
sItem.state = nState;
sItem.stateMask = nStateMask;
sItem.pszText = (LPTSTR)pText;
sItem.cchTextMax = (pText) ? 256 : 0;
sItem.iImage = nImage;
sItem.iSelectedImage = nSelectedImage;
sItem.cChildren = nCol;
sItem.lParam = lParam;
return SetItem(&sItem);
}
//*****************************************************************************
//*
//* SetItemText
//*
//*****************************************************************************
// Set the text of an item
// hItem : Is the handle of the item
// pText : Is the new text of the item
// nCol : Is the column for the text (0=tree column)
// Returns TRUE if the text was changed or FALSE if an error occurs
BOOL CTreeListCtrl::SetItemText(HTREEITEM hItem, LPCTSTR pText, int nCol) {
TV_ITEM sItem;
ASSERT(::IsWindow(m_hWnd));
sItem.mask = TVIF_SUBITEM | TVIF_TEXT;
sItem.hItem = hItem;
sItem.stateMask = 0;
sItem.pszText = (LPTSTR)pText;
sItem.cchTextMax = (pText) ? 256 : 0;
sItem.cChildren = nCol;
return SetItem(&sItem);
}
//*****************************************************************************
//*
//* ListDeleteItem
//*
//*****************************************************************************
// Deletes an item in a Tree-List-Control which is used as List-Control
// iRow : Is the row of the item
// Returns TRUE if the item was deleted or FALSE if an error occurs
BOOL CTreeListCtrl::ListDeleteItem(int iRow) {
ASSERT(::IsWindow(m_hWnd));
return DeleteItem(GetItemOfRow(iRow));
}
//*****************************************************************************
//*
//* ListInsertItem
//*
//*****************************************************************************
// Inserts an item in a Tree-List-Control which is used as List-Control
// iRow : Is the row of the item
// pText : Is the text for the item
// nImage : Is the number for the icon (use TV_NOIMAGE for no icon)
// nState : Is the state of the image
// TVIS_BOLD = text is bolded
// TVIS_UNTERLINE = text is underlined
// TVIS_SELECTED = sItem is selected
// TVIS_OVERLAYMASK = overlay bits for image
// TVIS_STATEIMAGEMASK = image for state icons
// iMask : Is the mask of bits which are used in the nState parameter
// TVIS_BOLD = text is bolded
// TVIS_UNTERLINE = text is underlined
// TVIS_SELECTED = sItem is selected
// TVIS_OVERLAYMASK = overlay bits for image
// TVIS_STATEIMAGEMASK = image for state icons
// Returns the insert position of the item or -1 if an error occurs
int CTreeListCtrl::ListInsertItem(int iRow, LPCTSTR pText, int nImage, int nState, int iMask) {
HTREEITEM hItem;
TVINSERTSTRUCT sData;
ASSERT(::IsWindow(m_hWnd));
sData.item.mask = 0;
if(pText) {
sData.item.mask |= TVIF_TEXT;
sData.item.pszText = (LPTSTR)pText;
sData.item.cchTextMax = 256;
}
if(nImage >= 0) {
sData.item.mask |= (TVIF_SELECTEDIMAGE | TVIF_IMAGE) ;
sData.item.iImage = nImage;
sData.item.iSelectedImage = nImage;
}
if(nState >= 0) {
sData.item.mask |= TVIF_STATE;
sData.item.state = nState;
sData.item.stateMask = iMask & (TVIS_BOLD | TVIS_UNTERLINE | TVIS_SELECTED | TVIS_OVERLAYMASK | TVIS_STATEIMAGEMASK);
}
hItem = GetItemOfRow(iRow);
if(!hItem) {
sData.hInsertAfter = TVI_LAST;
sData.hParent = TVI_ROOT;
} else {
sData.hParent = hItem;
sData.hInsertAfter = TVI_BEFORE;
}
hItem = InsertItem(&sData);
return GetRowOfItem(hItem);
}
//*****************************************************************************
//*
//* ListSetItem
//*
//*****************************************************************************
// Changes an item in a Tree-List-Control which is used as List-Control
// iRow : Is the row of the item
// nCol : Is the column of the item
// pText : Is the text for the item
// nImage : Is the number for the icon (use TV_NOIMAGE for no icon)
// nState : Is the state of the image
// TVIS_BOLD = text is bolded
// TVIS_UNTERLINE = text is underlined
// TVIS_SELECTED = sItem is selected
// TVIS_OVERLAYMASK = overlay bits for image
// TVIS_STATEIMAGEMASK = image for state icons (only for column 0)
// iMask : Is the mask of bits which are used in the nState parameter
// TVIS_BOLD = text is bolded
// TVIS_UNTERLINE = text is underlined
// TVIS_SELECTED = sItem is selected
// TVIS_OVERLAYMASK = overlay bits for image
// TVIS_STATEIMAGEMASK = image for state icons (only for column 0)
// Returns TRUE if ok or FALSE if an error occurs
BOOL CTreeListCtrl::ListSetItem(int iRow, int nCol, LPCTSTR pText, int nImage, int nState, int iMask) {
TV_ITEM sData;
ASSERT(::IsWindow(m_hWnd));
sData.mask = TVIF_HANDLE | TVIF_SUBITEM;
sData.hItem = GetItemOfRow(iRow);
sData.cChildren = nCol;
if(!sData.hItem)
return FALSE;
if(pText) {
sData.mask |= TVIF_TEXT;
sData.pszText = (LPTSTR)pText;
sData.cchTextMax = 256;
}
if(nImage >= 0) {
sData.mask |= TVIF_SELECTEDIMAGE | TVIF_IMAGE ;
sData.iImage = nImage;
sData.iSelectedImage = nImage;
}
if(nState >= 0) {
sData.mask |= TVIF_STATE;
sData.state = nState;
sData.stateMask = iMask & (TVIS_BOLD | TVIS_UNTERLINE | TVIS_SELECTED);
}
return SetItem(&sData);
}
//*****************************************************************************
//*
//* ListSetColor
//*
//*****************************************************************************
// Changes the colors of an item in a Tree-List-Control which is used as List-Control
// iRow : Is the row of the item
// nCol : Is the column of the item
// uBkColor : Is the new background color (use TV_NOCOLOR for the default color)
// uTextColor : Is the new text color (use TV_NOCOLOR for the default color)
// Returns TRUE if ok or FALSE if an error occurs
BOOL CTreeListCtrl::ListSetColor(int iRow, int nCol, COLORREF uBkColor, COLORREF uTextColor) {
HTREEITEM hItem;
ASSERT(::IsWindow(m_hWnd));
hItem = GetItemOfRow(iRow);
if(!hItem)
return FALSE;
SetItemBkColor(hItem, nCol, uBkColor);
SetItemTextColor(hItem, nCol, uTextColor);
return TRUE;
}
//*****************************************************************************
//*
//* ListGetColor
//*
//*****************************************************************************
// Gets the colors of an item in a Tree-List-Control which is used as List-Control
// iRow : Is the row of the item
// nCol : Is the column of the item
// uBkColor : Is the new background color (TV_NOCOLOR for the default color)
// uTextColor : Is the new background color (TV_NOCOLOR for the default color)
// Returns TRUE if ok or FALSE if an error occurs
BOOL CTreeListCtrl::ListGetColor(int iRow, int nCol, COLORREF &uBkColor, COLORREF &uTextColor) {
HTREEITEM hItem;
hItem = GetItemOfRow(iRow);
if(!hItem) {
uBkColor = TV_NOCOLOR;
uTextColor = TV_NOCOLOR;
return FALSE;
}
uBkColor = GetItemBkColor(hItem, nCol);
uTextColor = GetItemTextColor(hItem, nCol);
return TRUE;
}
//*****************************************************************************
//*
//* GetItemState
//*
//*****************************************************************************
// Gets the state of an Item
// hItem : Is the handle of the item
// nStateMask : Is the state mask of the requested states
// nCol : Is the column of the item
// Returns the state bits of the item
UINT CTreeListCtrl::GetItemState(HTREEITEM hItem, UINT uMask, int nCol) {
TV_ITEM sItem;
ASSERT(::IsWindow(m_hWnd));
sItem.mask = TVIF_STATE | TVIF_HANDLE | TVIF_SUBITEM;
sItem.stateMask = uMask;
sItem.hItem = hItem;
sItem.cChildren = nCol;
if(!GetItem(&sItem))
return 0;
return sItem.state;
}
//*****************************************************************************
//*
//* SetItemState
//*
//*****************************************************************************
// Sets the state of an Item
// hItem : Is the handle of the item
// nCol : Is the column of the item
// nState : New state bits
// nStateMask : Mask of the bits which should be changed
// Returns the state bits of the item
BOOL CTreeListCtrl::SetItemState(HTREEITEM hItem, int nCol, UINT nState, UINT nStateMask) {
TV_ITEM sItem;
ASSERT(::IsWindow(m_hWnd));
sItem.mask = TVIF_STATE | TVIF_HANDLE | TVIF_SUBITEM;
sItem.hItem = hItem;
sItem.cChildren = nCol;
sItem.stateMask = nStateMask;
sItem.state = nState;
return SetItem(&sItem);
}
//*****************************************************************************
//*
//* GetItemText
//*
//*****************************************************************************
// Gets the text of an item
// hItem : Is the handle of the item
// nCol : Is the column of the item
// Returns a pointer to the text or NULL if an error occurs
LPCTSTR CTreeListCtrl::GetItemText(HTREEITEM hItem, int nCol) {
TV_ITEM sItem;
ASSERT(::IsWindow(m_hWnd));
sItem.mask = TVIF_TEXT | TVIF_SUBITEM | TVIF_TEXTPTR;
sItem.hItem = hItem;
sItem.cChildren = nCol;
if(!GetItem(&sItem))
return 0;
return sItem.pszText;
}
//*****************************************************************************
//*
//* GetItemText
//*
//*****************************************************************************
// Gets the text of an item and stores it in an buffer
// hItem : Is the handle of the item
// pBuffer : Is the text buffer where the text will be saved
// iMax : Is the size of the text buffer in chars
// nCol : Is the column of the item
// Returns a pointer to the text or NULL if an error occurs
BOOL CTreeListCtrl::GetItemText(HTREEITEM hItem, LPTSTR pBuffer, int iMax, int nCol) {
TV_ITEM sItem;
ASSERT(::IsWindow(m_hWnd));
if(iMax <= 0)
return FALSE;
pBuffer[0] = 0;
sItem.mask = TVIF_TEXT | TVIF_SUBITEM;
sItem.hItem = hItem;
sItem.pszText = pBuffer;
sItem.cchTextMax = iMax;
sItem.cChildren = nCol;
return GetItem(&sItem);
}
//*****************************************************************************
//*
//* GetItemImageEx
//*
//*****************************************************************************
// Reads the image of an entry
// hItem : Is the handle of the item
// nCol : Is the column of the item
// Returns the image number or -1 if an error occurs
int CTreeListCtrl::GetItemImageEx(HTREEITEM hItem, int nCol) {
TVITEM sItem;
ASSERT(::IsWindow(m_hWnd));
sItem.mask = TVIF_IMAGE | TVIF_SUBITEM;
sItem.hItem = hItem;
sItem.cChildren = nCol;
if(!GetItem(&sItem))
return -1;
return sItem.iImage;
}
//*****************************************************************************
//*
//* GetItemImageEx
//*
//*****************************************************************************
// Reads the image of an entry
// hItem : Is the handle of the item
// nImage : Here the image number will be stored
// nCol : Is the column of the item
// Returns TRUE if the image number was detected or FALSE if an error occurs
BOOL CTreeListCtrl::GetItemImageEx(HTREEITEM hItem, int *pImage, int nCol) {
TVITEM sItem;
ASSERT(::IsWindow(m_hWnd));
sItem.mask = TVIF_IMAGE | TVIF_SUBITEM;
sItem.hItem = hItem;
sItem.cChildren = nCol;
if(!GetItem(&sItem))
return FALSE;
pImage[0] = sItem.iImage;
return TRUE;
}
//*****************************************************************************
//*
//* SetItemImageEx
//*
//*****************************************************************************
// Changes the image of an entry
// hItem : Is the handle of the item
// nImage : Is the new the image number
// nCol : Is the column of the item
// Returns TRUE if the image number was detected or FALSE if an error occurs
BOOL CTreeListCtrl::SetItemImageEx(HTREEITEM hItem, int nImage, int nCol) {
TVITEM sItem;
ASSERT(::IsWindow(m_hWnd));
sItem.mask = TVIF_IMAGE | TVIF_SUBITEM;
sItem.hItem = hItem;
sItem.iImage = nImage;
sItem.cChildren = nCol;
return SetItem(&sItem);
}
//*****************************************************************************
//*
//* ListGetItemText
//*
//*****************************************************************************
// Gets the text of an item and stores it in an buffer
// iRow : Row index of the item whose text is to be retrieved.
// pBuffer : Is the text buffer where the text will be saved
// iMax : Is the size of the text buffer in chars
// nCol : Is the column of the item
// Returns a pointer to the text or NULL if an error occurs
BOOL CTreeListCtrl::ListGetItemText(int iRow, LPTSTR pBuffer, int iMax, int nCol) {
TV_ITEM sItem;
HTREEITEM hItem;
ASSERT(::IsWindow(m_hWnd));
hItem = GetItemOfRow(iRow);
if(!hItem || iMax <= 0)
return FALSE;
pBuffer[0] = 0;
sItem.mask = TVIF_TEXT | TVIF_SUBITEM;
sItem.hItem = hItem;
sItem.pszText = pBuffer;
sItem.cchTextMax = iMax;
sItem.cChildren = nCol;
return GetItem(&sItem);
}
//*****************************************************************************
//*
//* FindItem
//*
//*****************************************************************************
// Seaches for an Item in the control with several properties
// hItem : Is the item handle where the seach begins
// iFlags : This flag define the seach options
// TVIF_CHILD = begin at the first child of hItem to seach
// TVIF_NEXT = begin at the next item after hItem to seach
// TVIF_PARAM = compare the the lParam entry at the seach
// TVIF_TEXT = compare the the pText entry at the seach
// TVIF_STATE = compare the the uState entry at the seach
// TVIF_CASE = don't differ between lower and upper case
// nCol : Selects the column for the text comparison
// pText : Is the text option for the seach
// lParam : Is the param option for the seach
// uState : Are the state bits for the seach
// uStateMask : Only bits which are set in this mask will be comared
// Retuns the handle of the item which was found, or NULL if no item was found
HTREEITEM CTreeListCtrl::FindItem(HTREEITEM hItem, int iFlags, int nCol, LPCTSTR pText, LPARAM lParam, UINT uState, UINT uStateMask) {
TV_FIND sFind;
sFind.uFlags = iFlags;
sFind.uColumn = nCol;
sFind.pText = pText;
sFind.lParam = lParam;
sFind.uState = uState;
sFind.uStateMask = uStateMask;
return FindItem(hItem, &sFind);
}
//*****************************************************************************
//*
//* InsertItem
//*
//*****************************************************************************
// Call this function to insert a new item in a tree list control.
// nMask : Integer specifying which attributes to set. See theTVITEM structure in the Platform SDK.
// pText : Address of a string containing the items text.
// nImage : Index of the items image in the tree view controls image list.
// nSelImage : Index of the items selected image in the tree view controls image list.
// nState : Specifies values for the items states. SeeTree View Control Item States in the Platform SDK for a list of appropriate states.
// nStateMask : Specifies which states are to be set. See theTVITEM structure in the Platform SDK.
// lParam : A 32-bit application-specific value associated with the item.
// hParent : Handle of the inserted items parent.
// hInsertAfter : Handle of the item after which the new item is to be inserted.
// Returns the handle of the new item if successful, otherwise NULL.
HTREEITEM CTreeListCtrl::InsertItem(UINT nMask, LPCTSTR pText, int nImage, int nSelImage, UINT nState, UINT nStateMask, LPARAM lParam, HTREEITEM hParent, HTREEITEM hInsertAfter) {
TVINSERTSTRUCT sInsert;
ASSERT(::IsWindow(m_hWnd));
sInsert.hParent = hParent;
sInsert.hInsertAfter = hInsertAfter;
sInsert.item.mask = nMask;
sInsert.item.pszText = (LPTSTR)pText;
sInsert.item.iImage = nImage;
sInsert.item.iSelectedImage = nSelImage;
sInsert.item.state = nState;
sInsert.item.stateMask = nStateMask;
sInsert.item.lParam = lParam;
return (HTREEITEM)::SendMessage(m_hWnd, TVM_INSERTITEM, 0, (LPARAM)&sInsert);
}
//*****************************************************************************
//*
//* InsertItem
//*
//*****************************************************************************
// Call this function to insert a new item in a tree list control, with a sort fonction.
// nMask : Integer specifying which attributes to set. See theTVITEM structure in the Platform SDK.
// pText : Address of a string containing the items text.
// nImage : Index of the items image in the tree view controls image list.
// nSelImage : Index of the items selected image in the tree view controls image list.
// nState : Specifies values for the items states. SeeTree View Control Item States in the Platform SDK for a list of appropriate states.
// nStateMask : Specifies which states are to be set. See theTVITEM structure in the Platform SDK.
// lParam : A 32-bit application-specific value associated with the item.
// pCmpProc : Is the sort function which is used to find the insert position
// hParent : Handle of the inserted items parent.
// Returns the handle of the new item if successful, otherwise NULL.
HTREEITEM CTreeListCtrl::InsertItem(UINT nMask, LPCTSTR pText, int nImage, int nSelImage, UINT nState, UINT nStateMask, LPARAM lParam, PFNTVSORTEX pCmpProc, HTREEITEM hParent) {
TVINSERTSTRUCT sInsert;
ASSERT(::IsWindow(m_hWnd));
sInsert.hParent = hParent;
sInsert.hInsertAfter = TVI_SORTEX;
sInsert.item.hItem = (HTREEITEM)pCmpProc;
sInsert.item.pszText = (LPTSTR)pText;
sInsert.item.mask = nMask;
sInsert.item.iImage = nImage;
sInsert.item.iSelectedImage = nSelImage;
sInsert.item.state = nState;
sInsert.item.stateMask = nStateMask;
sInsert.item.lParam = lParam;
return (HTREEITEM)::SendMessage(m_hWnd, TVM_INSERTITEM, 0, (LPARAM)&sInsert);
}
//*****************************************************************************
//*
//* SetColumnText
//*
//*****************************************************************************
// Sets the text of an item in the column header
// nCol : is the number of the column
// pText : is the new text
// Retuns TRUE if succesfull
BOOL CTreeListCtrl::SetColumnText(int nCol, LPCTSTR pText) {
TV_COLUMN sData;
sData.pszText = (LPTSTR)pText;
sData.cchTextMax = (int)_tcslen(pText);
sData.mask = TVCF_TEXT;
return SetColumn(nCol, &sData);
}
//*****************************************************************************
//*
//* SetColumnImage
//*
//*****************************************************************************
// Sets the text of an item in the column header
// nCol : is the number of the column
// iImage : is the new image number
// Retuns TRUE if succesfull
BOOL CTreeListCtrl::SetColumnImage(int nCol, int iImage) {
TV_COLUMN sData;
sData.iImage = iImage;
sData.mask = TVCF_IMAGE;
return SetColumn(nCol, &sData);
}
//*****************************************************************************
//*
//* SetColumnMark
//*
//*****************************************************************************
// Sets the mark state of a column. A marked column will be drawn darker.
// nCol : is the number of the column
// bOn : is the mark state
// Returns TRUE if succesfull
BOOL CTreeListCtrl::SetColumnMark(int nCol, BOOL bOn) {
TV_COLUMN sData;
sData.fmt = (bOn) ? TVCFMT_MARK : 0;
sData.mask = TVCF_MARK;
return SetColumn(nCol, &sData);
}
//*****************************************************************************
//*
//* FixColumnSize
//*
//*****************************************************************************
// Fixes the size of a a column. The size of a fixed column can't be changed.
// nCol : is the number of the column
// bOn : is the fixed state
// iWidth : is the new width for the column (-1 means don't change width)
// TVCF_LASTSIZE restore precedent size
// Returns TRUE if succesfull
BOOL CTreeListCtrl::FixColumnSize(int nCol, BOOL bOn, int iWidth) {
LV_COLUMN sData;
sData.fmt = (bOn) ? TVCFMT_FIXED : 0;
sData.mask = TVCF_FIXED;
if(iWidth >= 0) { // set also the column width
sData.mask |= TVCF_WIDTH;
sData.cx = iWidth;
}
return SetColumn(nCol, &sData);
}
//*****************************************************************************
//*
//* CreateDragImage
//*
//*****************************************************************************
CImageList *CTreeListCtrl::CreateDragImage(HTREEITEM hItem, int nCol) {
CImageList *pImageList;
HIMAGELIST hImageList;
ASSERT(::IsWindow(m_hWnd));
hImageList = TreeList_CreateDragImageEx(m_hWnd, hItem, nCol);
if(!hImageList) {
return NULL;
}
pImageList = new CImageList;
VERIFY(pImageList->Attach(hImageList));
return pImageList;
}