forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeView.cpp
2856 lines (2458 loc) · 66.4 KB
/
TreeView.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include <assert.h>
#define PROTECTED_THINGS_DISABLE
#include <vgui/Cursor.h>
#include <vgui/IScheme.h>
#include <vgui/IInput.h>
#include <vgui/IPanel.h>
#include <vgui/ISurface.h>
#include <vgui/ISystem.h>
#include <vgui/IVGui.h>
#include <vgui/KeyCode.h>
#include <KeyValues.h>
#include <vgui/MouseCode.h>
#include <vgui_controls/TreeView.h>
#include <vgui_controls/ScrollBar.h>
#include <vgui_controls/TextEntry.h>
#include <vgui_controls/Label.h>
#include <vgui_controls/Button.h>
#include <vgui_controls/TextImage.h>
#include <vgui_controls/ImageList.h>
#include <vgui_controls/ImagePanel.h>
#include "tier1/utlstring.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
using namespace vgui;
enum
{
WINDOW_BORDER_WIDTH=2 // the width of the window's border
};
#define TREE_INDENT_AMOUNT 20
namespace vgui
{
//-----------------------------------------------------------------------------
// Purpose: Displays an editable text field for the text control
//-----------------------------------------------------------------------------
class TreeNodeText : public TextEntry
{
DECLARE_CLASS_SIMPLE( TreeNodeText, TextEntry );
public:
TreeNodeText(Panel *parent, const char *panelName, TreeView *tree) : BaseClass(parent, panelName), m_pTree( tree )
{
m_bEditingInPlace = false;
m_bLabelEditingAllowed = false;
SetDragEnabled( false );
SetDropEnabled( false );
AddActionSignalTarget( this );
m_bArmForEditing = false;
m_bWaitingForRelease = false;
m_lArmingTime = 0L;
SetAllowKeyBindingChainToParent( true );
}
MESSAGE_FUNC( OnTextChanged, "TextChanged" )
{
GetParent()->InvalidateLayout();
}
bool IsKeyRebound( KeyCode code, int modifiers )
{
// If in editing mode, don't try and chain keypresses
if ( m_bEditingInPlace )
{
return false;
}
return BaseClass::IsKeyRebound( code, modifiers );
}
virtual void PaintBackground()
{
BaseClass::PaintBackground();
if ( !m_bLabelEditingAllowed )
return;
if ( !m_bEditingInPlace )
return;
int w, h;
GetSize( w, h );
surface()->DrawSetColor( GetFgColor() );
surface()->DrawOutlinedRect( 0, 0, w, h );
}
virtual void ApplySchemeSettings(IScheme *pScheme)
{
TextEntry::ApplySchemeSettings(pScheme);
SetBorder(NULL);
SetCursor(dc_arrow);
}
virtual void OnKeyCodeTyped(KeyCode code)
{
if ( m_bEditingInPlace )
{
if ( code == KEY_ENTER )
{
FinishEditingInPlace();
}
else if ( code == KEY_ESCAPE )
{
FinishEditingInPlace( true );
}
else
{
BaseClass::OnKeyCodeTyped( code );
}
return;
}
else if ( code == KEY_ENTER && IsLabelEditingAllowed() )
{
EnterEditingInPlace();
}
else
{
// let parent deal with it (don't chain back to TextEntry)
CallParentFunction(new KeyValues("KeyCodeTyped", "code", code));
}
}
#define CLICK_TO_EDIT_DELAY_MSEC 500
virtual void OnTick()
{
BaseClass::OnTick();
if ( m_bArmForEditing )
{
long msecSinceArming = system()->GetTimeMillis() - m_lArmingTime;
if ( msecSinceArming > CLICK_TO_EDIT_DELAY_MSEC )
{
m_bArmForEditing = false;
m_bWaitingForRelease = false;
ivgui()->RemoveTickSignal( GetVPanel() );
EnterEditingInPlace();
}
}
}
virtual void OnMouseReleased( MouseCode code )
{
if ( m_bEditingInPlace )
{
BaseClass::OnMouseReleased( code );
return;
}
else
{
if ( m_bWaitingForRelease && !IsBeingDragged() )
{
m_bArmForEditing = true;
m_bWaitingForRelease = false;
m_lArmingTime = system()->GetTimeMillis();
ivgui()->AddTickSignal( GetVPanel() );
}
else
{
m_bWaitingForRelease = false;
}
}
// let parent deal with it
CallParentFunction(new KeyValues("MouseReleased", "code", code));
}
virtual void OnCursorMoved( int x, int y )
{
// let parent deal with it
CallParentFunction(new KeyValues("OnCursorMoved", "x", x, "y", y));
}
virtual void OnMousePressed(MouseCode code)
{
if ( m_bEditingInPlace )
{
BaseClass::OnMousePressed( code );
return;
}
else
{
bool shift = (input()->IsKeyDown(KEY_LSHIFT) || input()->IsKeyDown(KEY_RSHIFT));
bool ctrl = (input()->IsKeyDown(KEY_LCONTROL) || input()->IsKeyDown(KEY_RCONTROL));
// make sure there is only one item selected
// before "WaitingForRelease" which leads to label editing.
CUtlVector< int > list;
m_pTree->GetSelectedItems( list );
bool bIsOnlyOneItemSelected = ( list.Count() == 1 );
if ( !shift &&
!ctrl &&
!m_bArmForEditing &&
IsLabelEditingAllowed() &&
bIsOnlyOneItemSelected &&
IsTextFullySelected() &&
!IsBeingDragged() )
{
m_bWaitingForRelease = true;
}
}
// let parent deal with it
CallParentFunction(new KeyValues("MousePressed", "code", code));
}
void SetLabelEditingAllowed( bool state )
{
m_bLabelEditingAllowed = state;
}
bool IsLabelEditingAllowed()
{
return m_bLabelEditingAllowed;
}
virtual void OnMouseDoublePressed(MouseCode code)
{
// Once we are editing, double pressing shouldn't chain up
if ( m_bEditingInPlace )
{
BaseClass::OnMouseDoublePressed( code );
return;
}
if ( m_bArmForEditing )
{
m_bArmForEditing = false;
m_bWaitingForRelease = false;
ivgui()->RemoveTickSignal( GetVPanel() );
}
CallParentFunction(new KeyValues("MouseDoublePressed", "code", code));
}
void EnterEditingInPlace()
{
if ( m_bEditingInPlace )
return;
m_bEditingInPlace = true;
char buf[ 1024 ];
GetText( buf, sizeof( buf ) );
m_OriginalText = buf;
SetCursor(dc_ibeam);
SetEditable( true );
SelectNone();
GotoTextEnd();
RequestFocus();
SelectAllText(false);
m_pTree->SetLabelBeingEdited( true );
}
void FinishEditingInPlace( bool revert = false )
{
if ( !m_bEditingInPlace )
return;
m_pTree->SetLabelBeingEdited( false );
SetEditable( false );
SetCursor(dc_arrow);
m_bEditingInPlace = false;
char buf[ 1024 ];
GetText( buf, sizeof( buf ) );
// Not actually changed...
if ( !Q_strcmp( buf, m_OriginalText.Get() ) )
return;
if ( revert )
{
SetText( m_OriginalText.Get() );
GetParent()->InvalidateLayout();
}
else
{
KeyValues *kv = new KeyValues( "LabelChanged", "original", m_OriginalText.Get(), "changed", buf );
PostActionSignal( kv );
}
}
virtual void OnKillFocus()
{
BaseClass::OnKillFocus();
FinishEditingInPlace();
}
virtual void OnMouseWheeled(int delta)
{
if ( m_bEditingInPlace )
{
BaseClass::OnMouseWheeled( delta );
return;
}
CallParentFunction(new KeyValues("MouseWheeled", "delta", delta));
}
// editable - cursor normal, and ability to edit text
bool IsBeingEdited() const
{
return m_bEditingInPlace;
}
private:
bool m_bEditingInPlace;
CUtlString m_OriginalText;
bool m_bLabelEditingAllowed;
bool m_bArmForEditing;
bool m_bWaitingForRelease;
long m_lArmingTime;
TreeView *m_pTree;
};
//-----------------------------------------------------------------------------
// Purpose: icon for the tree node (folder icon, file icon, etc.)
//-----------------------------------------------------------------------------
class TreeNodeImage : public ImagePanel
{
public:
TreeNodeImage(Panel *parent, const char *name) : ImagePanel(parent, name)
{
SetBlockDragChaining( true );
}
virtual ~TreeNodeImage() {}
//!! this could possibly be changed to just disallow mouse input on the image panel
virtual void OnMousePressed(MouseCode code)
{
// let parent deal with it
CallParentFunction(new KeyValues("MousePressed", "code", code));
}
virtual void OnMouseDoublePressed(MouseCode code)
{
// let parent deal with it
CallParentFunction(new KeyValues("MouseDoublePressed", "code", code));
}
virtual void OnMouseWheeled(int delta)
{
// let parent deal with it
CallParentFunction(new KeyValues("MouseWheeled", "delta", delta));
}
virtual void OnCursorMoved( int x, int y )
{
// let parent deal with it
CallParentFunction(new KeyValues("OnCursorMoved", "x", x, "y", y));
}
};
//-----------------------------------------------------------------------------
// Purpose: Scrollable area of the tree control, holds the tree itself only
//-----------------------------------------------------------------------------
class TreeViewSubPanel : public Panel
{
public:
TreeViewSubPanel(Panel *parent) : Panel(parent) {}
virtual ~TreeViewSubPanel() {}
virtual void ApplySchemeSettings(IScheme *pScheme)
{
Panel::ApplySchemeSettings(pScheme);
SetBorder(NULL);
}
virtual void OnMouseWheeled(int delta)
{
// let parent deal with it
CallParentFunction(new KeyValues("MouseWheeled", "delta", delta));
}
virtual void OnMousePressed(MouseCode code)
{
// let parent deal with it
CallParentFunction(new KeyValues("MousePressed", "code", code));
}
virtual void OnMouseDoublePressed(MouseCode code)
{
// let parent deal with it
CallParentFunction(new KeyValues("MouseDoublePressed", "code", code));
}
virtual void OnCursorMoved( int x, int y )
{
// let parent deal with it
CallParentFunction(new KeyValues("OnCursorMoved", "x", x, "y", y));
}
};
//-----------------------------------------------------------------------------
// Purpose: A single entry in the tree
//-----------------------------------------------------------------------------
class TreeNode : public Panel
{
DECLARE_CLASS_SIMPLE( TreeNode, Panel );
public:
TreeNode(Panel *parent, TreeView *pTreeView);
~TreeNode();
void SetText(const char *pszText);
void SetFont(HFont font);
void SetKeyValues(KeyValues *data);
bool IsSelected();
// currently unused, could be re-used if necessary
// bool IsInFocus();
virtual void PaintBackground();
virtual void PerformLayout();
TreeNode *GetParentNode();
int GetChildrenCount();
void ClearChildren();
int ComputeInsertionPosition( TreeNode *pChild );
int FindChild( TreeNode *pChild );
void AddChild(TreeNode *pChild);
void SetNodeExpanded(bool bExpanded);
bool IsExpanded();
int CountVisibleNodes();
void CalculateVisibleMaxWidth();
void OnChildWidthChange();
int GetMaxChildrenWidth();
int GetVisibleMaxWidth();
int GetDepth();
bool HasParent(TreeNode *pTreeNode);
bool IsBeingDisplayed();
virtual void SetVisible(bool state);
virtual void Paint();
virtual void ApplySchemeSettings(IScheme *pScheme);
virtual void SetBgColor( Color color );
virtual void SetFgColor( Color color );
virtual void OnSetFocus();
void SelectPrevChild(TreeNode *pCurrentChild);
void SelectNextChild(TreeNode *pCurrentChild);
int GetPrevChildItemIndex( TreeNode *pCurrentChild );
int GetNextChildItemIndex( TreeNode *pCurrentChild );
virtual void ClosePreviousParents( TreeNode *pPreviousParent );
virtual void StepInto( bool bClosePrevious=true );
virtual void StepOut( bool bClosePrevious=true );
virtual void StepOver( bool bClosePrevious=true );
virtual void OnKeyCodeTyped(KeyCode code);
virtual void OnMouseWheeled(int delta);
virtual void OnMousePressed( MouseCode code);
virtual void OnMouseReleased( MouseCode code);
virtual void OnCursorMoved( int x, int y );
virtual bool IsDragEnabled() const;
void PositionAndSetVisibleNodes(int &nStart, int &nCount, int x, int &y);
// counts items above this item including itself
int CountVisibleIndex();
virtual void OnCreateDragData( KeyValues *msg );
// For handling multiple selections...
virtual void OnGetAdditionalDragPanels( CUtlVector< Panel * >& dragabbles );
virtual void OnMouseDoublePressed( MouseCode code );
TreeNode *FindItemUnderMouse( int &nStart, int& nCount, int x, int &y, int mx, int my );
MESSAGE_FUNC_PARAMS( OnLabelChanged, "LabelChanged", data );
void EditLabel();
void SetLabelEditingAllowed( bool state );
bool IsLabelEditingAllowed() const;
virtual bool IsDroppable( CUtlVector< KeyValues * >& msglist );
virtual void OnPanelDropped( CUtlVector< KeyValues * >& msglist );
virtual HCursor GetDropCursor( CUtlVector< KeyValues * >& msglist );
virtual bool GetDropContextMenu( Menu *menu, CUtlVector< KeyValues * >& msglist );
void FindNodesInRange( CUtlVector< TreeNode * >& list, int startIndex, int endIndex );
void RemoveChildren();
void SetSelectionTextColor( const Color& clr );
void SetSelectionBgColor( const Color& clr );
void SetSelectionUnfocusedBgColor( const Color& clr );
public:
int m_ItemIndex;
int m_ParentIndex;
KeyValues *m_pData;
CUtlVector<TreeNode *> m_Children;
bool m_bExpand;
private:
void FindNodesInRange_R( CUtlVector< TreeNode * >& list, bool& finished, bool& foundStart, int startIndex, int endIndex );
int m_iNodeWidth;
int m_iMaxVisibleWidth;
TreeNodeText *m_pText;
TextImage *m_pExpandImage;
TreeNodeImage *m_pImagePanel;
bool m_bExpandableWithoutChildren;
TreeView *m_pTreeView;
int m_nClickedItem;
bool m_bClickedSelected;
};
TreeNode::TreeNode(Panel *parent, TreeView *pTreeView) :
BaseClass(parent, "TreeNode" ),
m_nClickedItem( 0 ),
m_bClickedSelected( false )
{
m_pData = NULL;
m_pTreeView = pTreeView;
m_ItemIndex = -1;
m_iNodeWidth = 0;
m_iMaxVisibleWidth = 0;
m_pExpandImage = new TextImage("+");
m_pExpandImage->SetPos(3, 1);
m_pImagePanel = new TreeNodeImage(this, "TreeImage");
m_pImagePanel->SetPos(TREE_INDENT_AMOUNT, 3);
m_pText = new TreeNodeText(this, "TreeNodeText",pTreeView);
m_pText->SetMultiline(false);
m_pText->SetEditable(false);
m_pText->SetPos(TREE_INDENT_AMOUNT*2, 0);
m_pText->AddActionSignalTarget( this );
m_bExpand = false;
m_bExpandableWithoutChildren = false;
}
TreeNode::~TreeNode()
{
delete m_pExpandImage;
if ( m_pData )
{
m_pData->deleteThis();
}
}
void TreeNode::SetText(const char *pszText)
{
m_pText->SetText(pszText);
InvalidateLayout();
}
void TreeNode::SetLabelEditingAllowed( bool state )
{
Assert( m_pTreeView->IsLabelEditingAllowed() );
m_pText->SetLabelEditingAllowed( state );
}
bool TreeNode::IsLabelEditingAllowed() const
{
return m_pText->IsLabelEditingAllowed();
}
bool TreeNode::GetDropContextMenu( Menu *menu, CUtlVector< KeyValues * >& msglist )
{
return m_pTreeView->GetItemDropContextMenu( m_ItemIndex, menu, msglist );
}
bool TreeNode::IsDroppable( CUtlVector< KeyValues * >& msglist )
{
return m_pTreeView->IsItemDroppable( m_ItemIndex, msglist );
}
void TreeNode::OnPanelDropped( CUtlVector< KeyValues * >& msglist )
{
m_pTreeView->OnItemDropped( m_ItemIndex, msglist );
}
HCursor TreeNode::GetDropCursor( CUtlVector< KeyValues * >& msglist )
{
return m_pTreeView->GetItemDropCursor( m_ItemIndex, msglist );
}
void TreeNode::OnCreateDragData( KeyValues *msg )
{
// make sure the dragged item appears selected,
// on the off chance it appears deselected by a cntl mousedown
m_pTreeView->AddSelectedItem( m_ItemIndex, false );
m_pTreeView->GenerateDragDataForItem( m_ItemIndex, msg );
}
// For handling multiple selections...
void TreeNode::OnGetAdditionalDragPanels( CUtlVector< Panel * >& dragabbles )
{
CUtlVector< int > list;
m_pTreeView->GetSelectedItems( list );
int c = list.Count();
// walk this in reverse order so that panels are in order of selection
// even though GetSelectedItems returns items in reverse selection order
for ( int i = c - 1; i >= 0; --i )
{
int itemIndex = list[ i ];
// Skip self
if ( itemIndex == m_ItemIndex )
continue;
dragabbles.AddToTail( ( Panel * )m_pTreeView->GetItem( itemIndex ) );
}
}
void TreeNode::OnLabelChanged( KeyValues *data )
{
char const *oldString = data->GetString( "original" );
char const *newString = data->GetString( "changed" );
if ( m_pTreeView->IsLabelEditingAllowed() )
{
m_pTreeView->OnLabelChanged( m_ItemIndex, oldString, newString );
}
}
void TreeNode::EditLabel()
{
if ( m_pText->IsLabelEditingAllowed() &&
!m_pText->IsBeingEdited() )
{
m_pText->EnterEditingInPlace();
}
}
void TreeNode::SetFont(HFont font)
{
Assert( font );
if ( !font )
return;
m_pText->SetFont(font);
m_pExpandImage->SetFont(font);
InvalidateLayout();
int i;
for (i=0;i<GetChildrenCount();i++)
{
m_Children[i]->SetFont(font);
}
}
void TreeNode::SetKeyValues(KeyValues *data)
{
if ( m_pData != data )
{
if (m_pData)
{
m_pData->deleteThis();
}
m_pData = data->MakeCopy();
}
// set text
m_pText->SetText(data->GetString("Text", ""));
m_bExpandableWithoutChildren = data->GetInt("Expand");
InvalidateLayout();
}
bool TreeNode::IsSelected()
{
return m_pTreeView->IsItemSelected( m_ItemIndex );
}
void TreeNode::PaintBackground()
{
if ( !m_pText->IsBeingEdited() )
{
// setup panel drawing
if ( IsSelected() )
{
m_pText->SelectAllText(false);
}
else
{
m_pText->SelectNoText();
}
}
BaseClass::PaintBackground();
}
// currently unused, could be re-used if necessary
/*
bool TreeNode::IsInFocus()
{
// check if our parent or one of it's children has focus
VPANEL focus = input()->GetFocus();
return (HasFocus() || (focus && ipanel()->HasParent(focus, GetVParent())));
}
*/
void TreeNode::PerformLayout()
{
BaseClass::PerformLayout();
int width = 0;
if (m_pData->GetInt("SelectedImage", 0) == 0 &&
m_pData->GetInt("Image", 0) == 0)
{
width = TREE_INDENT_AMOUNT;
}
else
{
width = TREE_INDENT_AMOUNT * 2;
}
m_pText->SetPos(width, 0);
int contentWide, contentTall;
m_pText->SetToFullWidth();
m_pText->GetSize(contentWide, contentTall);
contentWide += 10;
m_pText->SetSize( contentWide, m_pTreeView->GetRowHeight() );
width += contentWide;
SetSize(width, m_pTreeView->GetRowHeight());
m_iNodeWidth = width;
CalculateVisibleMaxWidth();
}
TreeNode *TreeNode::GetParentNode()
{
if (m_pTreeView->m_NodeList.IsValidIndex(m_ParentIndex))
{
return m_pTreeView->m_NodeList[m_ParentIndex];
}
return NULL;
}
int TreeNode::GetChildrenCount()
{
return m_Children.Count();
}
int TreeNode::ComputeInsertionPosition( TreeNode *pChild )
{
if ( !m_pTreeView->m_pSortFunc )
{
return GetChildrenCount() - 1;
}
int start = 0, end = GetChildrenCount() - 1;
while (start <= end)
{
int mid = (start + end) >> 1;
if ( m_pTreeView->m_pSortFunc( m_Children[mid]->m_pData, pChild->m_pData ) )
{
start = mid + 1;
}
else if ( m_pTreeView->m_pSortFunc( pChild->m_pData, m_Children[mid]->m_pData ) )
{
end = mid - 1;
}
else
{
return mid;
}
}
return end;
}
int TreeNode::FindChild( TreeNode *pChild )
{
if ( !m_pTreeView->m_pSortFunc )
{
AssertMsg( 0, "This code has never been tested. Is it correct?" );
for ( int i = 0; i < GetChildrenCount(); ++i )
{
if ( m_Children[i] == pChild )
return i;
}
return -1;
}
// Find the first entry <= to the child
int start = 0, end = GetChildrenCount() - 1;
while (start <= end)
{
int mid = (start + end) >> 1;
if ( m_Children[mid] == pChild )
return mid;
if ( m_pTreeView->m_pSortFunc( m_Children[mid]->m_pData, pChild->m_pData ) )
{
start = mid + 1;
}
else
{
end = mid - 1;
}
}
int nMax = GetChildrenCount();
while( end < nMax )
{
// Stop when we reach a child that has a different value
if ( m_pTreeView->m_pSortFunc( pChild->m_pData, m_Children[end]->m_pData ) )
return -1;
if ( m_Children[end] == pChild )
return end;
++end;
}
return -1;
}
void TreeNode::AddChild(TreeNode *pChild)
{
int i = ComputeInsertionPosition( pChild );
m_Children.InsertAfter( i, pChild );
}
void TreeNode::SetNodeExpanded(bool bExpanded)
{
m_bExpand = bExpanded;
if (m_bExpand)
{
// see if we have any child nodes
if (GetChildrenCount() < 1)
{
// we need to get our children from the control
m_pTreeView->GenerateChildrenOfNode(m_ItemIndex);
// if we still don't have any children, then hide the expand button
if (GetChildrenCount() < 1)
{
m_bExpand = false;
m_bExpandableWithoutChildren = false;
m_pTreeView->InvalidateLayout();
return;
}
}
m_pExpandImage->SetText("-");
}
else
{
m_pExpandImage->SetText("+");
if ( m_bExpandableWithoutChildren && GetChildrenCount() > 0 )
{
m_pTreeView->RemoveChildrenOfNode( m_ItemIndex );
}
// check if we've closed down on one of our children, if so, we get the focus
int selectedItem = m_pTreeView->GetFirstSelectedItem();
if (selectedItem != -1 && m_pTreeView->m_NodeList[selectedItem]->HasParent(this))
{
m_pTreeView->AddSelectedItem( m_ItemIndex, true );
}
}
CalculateVisibleMaxWidth();
m_pTreeView->InvalidateLayout();
}
bool TreeNode::IsExpanded()
{
return m_bExpand;
}
int TreeNode::CountVisibleNodes()
{
int count = 1; // count myself
if (m_bExpand)
{
int i;
for (i=0;i<m_Children.Count();i++)
{
count += m_Children[i]->CountVisibleNodes();
}
}
return count;
}
void TreeNode::CalculateVisibleMaxWidth()
{
int width;
if (m_bExpand)
{
int childMaxWidth = GetMaxChildrenWidth();
childMaxWidth += TREE_INDENT_AMOUNT;
width = max(childMaxWidth, m_iNodeWidth);
}
else
{
width = m_iNodeWidth;
}
if (width != m_iMaxVisibleWidth)
{
m_iMaxVisibleWidth = width;
if (GetParentNode())
{
GetParentNode()->OnChildWidthChange();
}
else
{
m_pTreeView->InvalidateLayout();
}
}
}
void TreeNode::OnChildWidthChange()
{
CalculateVisibleMaxWidth();
}
int TreeNode::GetMaxChildrenWidth()
{
int maxWidth = 0;
int i;
for (i=0;i<GetChildrenCount();i++)
{
int childWidth = m_Children[i]->GetVisibleMaxWidth();
if (childWidth > maxWidth)
{
maxWidth = childWidth;
}
}
return maxWidth;
}
int TreeNode::GetVisibleMaxWidth()
{
return m_iMaxVisibleWidth;
}
int TreeNode::GetDepth()
{
int depth = 0;
TreeNode *pParent = GetParentNode();
while (pParent)
{
depth++;
pParent = pParent->GetParentNode();
}
return depth;
}
bool TreeNode::HasParent(TreeNode *pTreeNode)
{
TreeNode *pParent = GetParentNode();
while (pParent)
{
if (pParent == pTreeNode)
return true;
pParent = pParent->GetParentNode();
}
return false;
}
bool TreeNode::IsBeingDisplayed()
{
TreeNode *pParent = GetParentNode();
while (pParent)
{
// our parents aren't showing us
if (!pParent->m_bExpand)
return false;
pParent = pParent->GetParentNode();
}
return true;
}
void TreeNode::SetVisible(bool state)
{
BaseClass::SetVisible(state);
bool bChildrenVisible = state && m_bExpand;
int i;
for (i=0;i<GetChildrenCount();i++)
{
m_Children[i]->SetVisible(bChildrenVisible);
}
}