2 // System.Windows.Forms.TreeNode
5 // stubbed out by Jackson Harper (jackson@latitudegeo.com)
6 // Dennis Hayes (dennish@Raytek.com)
7 // Aleksey Ryabchuk (ryabchuk@yahoo.com)
9 // (C) 2002 Ximian, Inc
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System
.Runtime
.InteropServices
;
35 namespace System
.Windows
.Forms
{
41 public class TreeNode
: MarshalByRefObject
, ICloneable
{
43 TreeNodeCollection children
;
48 int selectedImageIndex
;
60 selectedImageIndex
= 0;
65 internal TreeNode ( IntPtr handle
, TreeView tree
) : this ( )
68 Nodes
.TreeView
= tree
;
72 public TreeNode( string text
) : this ( )
77 public TreeNode( string text
, TreeNode
[] children
) : this ( text
)
79 Nodes
.AddRange ( children
);
83 public TreeNode( string text
, int imageIndex
, int selectedImageIndex
) : this ( text
)
85 this.imageIndex
= imageIndex
;
86 this.selectedImageIndex
= selectedImageIndex
;
90 public TreeNode( string text
, int imageIndex
, int selectedImageIndex
, TreeNode
[] children
) : this ( text
, children
)
92 this.imageIndex
= imageIndex
;
93 this.selectedImageIndex
= selectedImageIndex
;
96 // --- Public Properties
99 public Color BackColor
{
102 throw new NotImplementedException ();
110 public Rectangle Bounds
{
112 IntPtr ptr
= IntPtr
.Zero
;
114 TreeView tree
= TreeView
;
116 throw new NullReferenceException ( );
118 ptr
= Marshal
.AllocHGlobal ( Marshal
.SizeOf ( typeof ( RECT
) ) );
119 Marshal
.WriteInt32 ( ptr
, Handle
.ToInt32 ( ) );
120 Win32
.SendMessage ( tree
.Handle
, (int) TreeViewMessages
.TVM_GETITEMRECT
, 1, ptr
.ToInt32 ( ) );
121 RECT rect
= (RECT
) Marshal
.PtrToStructure ( ptr
, typeof ( RECT
) );
122 return new Rectangle ( rect
.left
, rect
.top
, rect
.right
- rect
.left
, rect
.bottom
- rect
.top
);
125 Marshal
.FreeHGlobal ( ptr
);
130 public bool Checked
{
132 TreeView tree
= TreeView
;
133 if ( tree
!= null && tree
.IsHandleCreated
&& Created
)
134 checked_
= getCheckedState ( tree
, Handle
);
141 TreeView tree
= TreeView
;
142 if ( tree
!= null && tree
.IsHandleCreated
&& Created
)
143 setCheckedState ( tree
, Handle
, checked_
);
147 public TreeNode FirstNode
{
149 if ( !Created
&& Nodes
.Count
> 0 )
152 IntPtr hfirst
= (IntPtr
) Win32
.SendMessage ( TreeView
.Handle
,
153 (int) TreeViewMessages
.TVM_GETNEXTITEM
,
154 (int) TreeViewItemSelFlags
.TVGN_CHILD
,
155 Handle
.ToInt32 ( ) );
156 return TreeNode
.FromHandle ( TreeView
, hfirst
);
161 public Color ForeColor
{
164 throw new NotImplementedException ();
168 throw new NotImplementedException ();
172 public string FullPath
{
175 throw new NotImplementedException ();
179 public IntPtr Handle
{
187 public int ImageIndex
{
189 TreeView tree
= TreeView
;
190 if ( tree
!= null && tree
.ImageIndex
!= 0 && imageIndex
== 0 )
191 return tree
.ImageIndex
;
201 if ( Parent
== null )
204 if ( Parent
.handle
== TreeView
.RootHandle
)
205 return TreeView
.Nodes
.IndexOf ( this );
207 return Parent
.Nodes
.IndexOf ( this );
211 public bool IsEditing
{
214 throw new NotImplementedException ();
218 public bool IsExpanded
{
221 throw new NotImplementedException ();
225 public bool IsSelected
{
227 if ( TreeView
!= null ) {
234 public bool IsVisible
{
237 throw new NotImplementedException ();
241 public TreeNode LastNode
{
243 return Nodes
[ Nodes
.Count
- 1 ];
247 public TreeNode NextNode
{
250 throw new NotImplementedException ();
254 public TreeNode NextVisibleNode
{
257 throw new NotImplementedException ();
265 public Font NodeFont
{
268 throw new NotImplementedException ();
276 public TreeNodeCollection Nodes
{
278 if ( children
== null )
279 children
= new TreeNodeCollection ( this );
284 public TreeNode Parent
{
285 get { return parent; }
288 public TreeNode PrevNode
{
291 throw new NotImplementedException ();
295 public TreeNode PrevVisibleNode
{
298 throw new NotImplementedException ();
302 public int SelectedImageIndex
{
304 TreeView tree
= TreeView
;
305 if ( tree
!= null && tree
.SelectedImageIndex
!= 0 && selectedImageIndex
== 0 )
306 return tree
.SelectedImageIndex
;
308 return selectedImageIndex
;
311 selectedImageIndex
= value;
318 throw new NotImplementedException ();
334 public TreeView TreeView
{
336 if ( Parent
!= null ) {
337 TreeNode parent
= Parent
;
338 while ( parent
.Parent
!= null )
339 parent
= parent
.Parent
;
340 return parent
.Nodes
.TreeView
;
343 if ( handle
== TreeView
.RootHandle
)
344 return Nodes
.TreeView
;
350 // --- Public Methods
353 public void BeginEdit()
358 public virtual object Clone()
360 throw new NotImplementedException ();
363 public void Collapse()
365 collapseImpl ( TreeView
);
368 public void EndEdit(bool cancel
)
373 public void EnsureVisible()
380 expandImpl ( TreeView
);
383 public void ExpandAll()
385 expandAllImpl ( TreeView
);
388 public static TreeNode
FromHandle(TreeView tree
, IntPtr handle
)
390 return FromHandle ( tree
.Nodes
, handle
);
393 public int GetNodeCount(bool includeSubTrees
)
395 throw new NotImplementedException ();
401 TreeView tree
= TreeView
;
402 if ( tree
!= null && tree
.IsHandleCreated
) {
403 int res
= Win32
.SendMessage ( tree
.Handle
, (int) TreeViewMessages
.TVM_DELETEITEM
, 0, handle
.ToInt32 ( ) );
404 if ( res
!= 0 ) zeroHandle ( );
414 public override string ToString()
417 return base.ToString();
420 internal void setParent ( TreeNode parent
)
422 this.parent
= parent
;
425 internal void setHandle ( IntPtr hItem
)
430 internal void makeTree ( IntPtr parent
, TreeView treeView
)
432 if ( handle
== IntPtr
.Zero
)
433 insertNode ( parent
, treeView
);
435 foreach ( TreeNode node
in Nodes
)
436 node
.makeTree ( handle
, treeView
);
439 internal void createNode ( TreeView treeView
)
441 IntPtr parentHandle
= IntPtr
.Zero
;
443 if ( Parent
!= null )
444 parentHandle
= Parent
.Handle
;
446 parentHandle
= TreeView
.RootHandle
;
448 if ( parentHandle
!= IntPtr
.Zero
) {
449 insertNode ( parentHandle
, treeView
);
453 internal void insertNode ( IntPtr parent
, TreeView treeView
)
455 TreeView tree
= ( treeView
!= null ) ? treeView
: TreeView
;
459 TVINSERTSTRUCT insStruct
= tree
.insStruct
;
460 insStruct
.hParent
= parent
;
463 int intPtr
= tree
.Sorted
? (int)TreeViewItemInsertPosition
.TVI_SORT
: (int) TreeViewItemInsertPosition
.TVI_LAST
;
464 insStruct
.hInsertAfter
= (IntPtr
) intPtr
;
467 insStruct
.item
.mask
= (uint) ( TreeViewItemFlags
.TVIF_TEXT
| TreeViewItemFlags
.TVIF_STATE
);
468 insStruct
.item
.pszText
= Text
;
469 insStruct
.item
.cchTextMax
= Text
.Length
;
472 insStruct
.item
.state
|= (uint) TreeViewItemState
.TVIS_EXPANDED
;
473 insStruct
.item
.stateMask
|= (uint) TreeViewItemState
.TVIS_EXPANDED
;
476 handle
= (IntPtr
) Win32
.SendMessage ( TreeView
.Handle
, TreeViewMessages
.TVM_INSERTITEMA
, 0, ref insStruct
);
478 if ( tree
.CheckBoxes
)
479 setCheckedState ( tree
, handle
, checked_
);
482 private static TreeNode
FromHandle ( TreeNodeCollection nodes
, IntPtr handle
)
484 foreach ( TreeNode node
in nodes
) {
485 if ( node
.handle
== handle
)
488 TreeNode cnode
= FromHandle ( node
.Nodes
, handle
);
495 internal void sortNode ( )
498 int res
= Win32
.SendMessage ( TreeView
.Handle
, (int)TreeViewMessages
.TVM_SORTCHILDREN
, 0, Handle
.ToInt32 ( ) );
499 foreach ( TreeNode node
in Nodes
)
504 private void zeroHandle ( )
506 handle
= IntPtr
.Zero
;
507 foreach ( TreeNode node
in Nodes
)
511 private bool Created
{
512 get { return handle != IntPtr.Zero; }
515 private void expandImpl ( TreeView tree
)
517 if ( tree
!= null && tree
.IsHandleCreated
&& Created
)
518 Win32
.SendMessage ( tree
.Handle
, (int)TreeViewMessages
.TVM_EXPAND
, (int) TreeViewItemExpansion
.TVE_EXPAND
, Handle
.ToInt32() );
523 internal void expandAllImpl ( TreeView tree
)
526 foreach ( TreeNode node
in Nodes
)
527 node
.expandAllImpl ( tree
);
530 internal void collapseImpl ( TreeView tree
)
532 if ( tree
!= null && tree
.IsHandleCreated
&& Created
)
533 Win32
.SendMessage ( tree
.Handle
, (int)TreeViewMessages
.TVM_EXPAND
, (int) TreeViewItemExpansion
.TVE_COLLAPSE
, Handle
.ToInt32() );
536 internal void collapseAllImpl ( TreeView tree
)
538 collapseImpl ( tree
);
539 foreach ( TreeNode node
in Nodes
)
540 node
.collapseAllImpl ( tree
);
543 private void setCheckedState ( TreeView tree
, IntPtr hitem
, bool isChecked
)
545 TVINSERTSTRUCT insStruct
= tree
.insStruct
;
547 insStruct
.item
.mask
= (uint) ( TreeViewItemFlags
.TVIF_HANDLE
| TreeViewItemFlags
.TVIF_STATE
);
548 insStruct
.item
.hItem
= hitem
;
549 insStruct
.item
.stateMask
= (uint) TreeViewItemState
.TVIS_STATEIMAGEMASK
;
550 insStruct
.item
.state
= (uint) Win32
.INDEXTOSTATEIMAGEMASK ( isChecked
? 2 : 1 );
552 Win32
.SendMessage ( TreeView
.Handle
, TreeViewMessages
.TVM_SETITEMA
, 0, ref insStruct
.item
);
555 private bool getCheckedState ( TreeView tree
, IntPtr hitem
)
557 TVINSERTSTRUCT insStruct
= tree
.insStruct
;
559 insStruct
.item
.mask
= (uint) ( TreeViewItemFlags
.TVIF_HANDLE
| TreeViewItemFlags
.TVIF_STATE
);
560 insStruct
.item
.hItem
= hitem
;
561 insStruct
.item
.stateMask
= (uint) TreeViewItemState
.TVIS_STATEIMAGEMASK
;
563 Win32
.SendMessage ( TreeView
.Handle
, TreeViewMessages
.TVM_GETITEMA
, 0, ref insStruct
.item
);
564 return ((insStruct
.item
.state
>> 12 ) - 1) > 0;
567 private void updateText ( )
569 TreeView tree
= TreeView
;
570 if ( tree
!= null && tree
.IsHandleCreated
&& this.Created
) {
571 TVINSERTSTRUCT insStruct
= tree
.insStruct
;
572 insStruct
.item
.mask
= (uint) ( TreeViewItemFlags
.TVIF_HANDLE
| TreeViewItemFlags
.TVIF_TEXT
);
573 insStruct
.item
.hItem
= Handle
;
574 insStruct
.item
.pszText
= Text
;
575 insStruct
.item
.cchTextMax
= Text
.Length
;
576 Win32
.SendMessage ( TreeView
.Handle
, TreeViewMessages
.TVM_SETITEMA
, 0, ref insStruct
.item
);