(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / TreeNode.cs
blob59c9137dd1b2b706251c575c4c14d547e526dca3
1 //
2 // System.Windows.Forms.TreeNode
3 //
4 // Author:
5 // stubbed out by Jackson Harper (jackson@latitudegeo.com)
6 // Dennis Hayes (dennish@Raytek.com)
7 // Aleksey Ryabchuk (ryabchuk@yahoo.com)
8 //
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:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
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.
32 using System.Drawing;
33 using System.Runtime.InteropServices;
35 namespace System.Windows.Forms {
37 // <summary>
39 // </summary>
41 public class TreeNode : MarshalByRefObject, ICloneable {
43 TreeNodeCollection children;
44 TreeNode parent;
45 string text;
46 IntPtr handle;
47 int imageIndex;
48 int selectedImageIndex;
49 bool checked_;
50 bool expanded;
52 [MonoTODO]
53 public TreeNode()
55 children = null;
56 parent = null;
57 text = String.Empty;
58 handle = IntPtr.Zero;
59 imageIndex = 0;
60 selectedImageIndex = 0;
61 checked_ = false;
62 expanded = false;
65 internal TreeNode ( IntPtr handle, TreeView tree ) : this ( )
67 this.handle = handle;
68 Nodes.TreeView = tree;
71 [MonoTODO]
72 public TreeNode( string text ) : this ( )
74 this.text = text;
76 [MonoTODO]
77 public TreeNode( string text, TreeNode[] children ) : this ( text )
79 Nodes.AddRange ( children );
82 [MonoTODO]
83 public TreeNode( string text, int imageIndex, int selectedImageIndex ) : this ( text )
85 this.imageIndex = imageIndex;
86 this.selectedImageIndex = selectedImageIndex;
89 [MonoTODO]
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
98 [MonoTODO]
99 public Color BackColor {
102 throw new NotImplementedException ();
106 //FIXME:
110 public Rectangle Bounds {
111 get {
112 IntPtr ptr = IntPtr.Zero;
113 try {
114 TreeView tree = TreeView;
115 if ( tree == null )
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 );
124 finally {
125 Marshal.FreeHGlobal ( ptr );
129 [MonoTODO]
130 public bool Checked {
131 get {
132 TreeView tree = TreeView;
133 if ( tree != null && tree.IsHandleCreated && Created )
134 checked_ = getCheckedState ( tree, Handle );
136 return checked_;
138 set {
139 checked_ = value;
141 TreeView tree = TreeView;
142 if ( tree != null && tree.IsHandleCreated && Created )
143 setCheckedState ( tree, Handle, checked_ );
147 public TreeNode FirstNode {
148 get {
149 if ( !Created && Nodes.Count > 0 )
150 return Nodes[0];
151 else {
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 );
160 [MonoTODO]
161 public Color ForeColor {
164 throw new NotImplementedException ();
168 throw new NotImplementedException ();
171 [MonoTODO]
172 public string FullPath {
175 throw new NotImplementedException ();
178 [MonoTODO]
179 public IntPtr Handle {
180 get {
181 if ( !Created )
182 createNode ( null );
183 return handle;
186 [MonoTODO]
187 public int ImageIndex {
188 get {
189 TreeView tree = TreeView;
190 if ( tree != null && tree.ImageIndex != 0 && imageIndex == 0 )
191 return tree.ImageIndex;
192 return imageIndex;
194 set {
195 imageIndex = value;
199 public int Index {
200 get {
201 if ( Parent == null )
202 return 0;
204 if ( Parent.handle == TreeView.RootHandle )
205 return TreeView.Nodes.IndexOf ( this );
207 return Parent.Nodes.IndexOf ( this );
210 [MonoTODO]
211 public bool IsEditing {
214 throw new NotImplementedException ();
217 [MonoTODO]
218 public bool IsExpanded {
221 throw new NotImplementedException ();
224 [MonoTODO]
225 public bool IsSelected {
226 get {
227 if ( TreeView != null ) {
228 return false;
230 return false;
233 [MonoTODO]
234 public bool IsVisible {
237 throw new NotImplementedException ();
240 [MonoTODO]
241 public TreeNode LastNode {
242 get {
243 return Nodes [ Nodes.Count - 1 ];
246 [MonoTODO]
247 public TreeNode NextNode {
250 throw new NotImplementedException ();
253 [MonoTODO]
254 public TreeNode NextVisibleNode {
257 throw new NotImplementedException ();
261 //FIXME:
264 [MonoTODO]
265 public Font NodeFont {
268 throw new NotImplementedException ();
272 //FIXME:
275 [MonoTODO]
276 public TreeNodeCollection Nodes {
277 get {
278 if ( children == null )
279 children = new TreeNodeCollection ( this );
280 return children;
284 public TreeNode Parent {
285 get { return parent; }
287 [MonoTODO]
288 public TreeNode PrevNode {
291 throw new NotImplementedException ();
294 [MonoTODO]
295 public TreeNode PrevVisibleNode {
298 throw new NotImplementedException ();
301 [MonoTODO]
302 public int SelectedImageIndex {
303 get {
304 TreeView tree = TreeView;
305 if ( tree != null && tree.SelectedImageIndex != 0 && selectedImageIndex == 0 )
306 return tree.SelectedImageIndex;
308 return selectedImageIndex;
310 set {
311 selectedImageIndex = value;
314 [MonoTODO]
315 public object Tag {
318 throw new NotImplementedException ();
322 //FIXME:
325 [MonoTODO]
326 public string Text {
327 get { return text; }
328 set {
329 text = value;
330 updateText ( );
334 public TreeView TreeView {
335 get {
336 if ( Parent != null ) {
337 TreeNode parent = Parent;
338 while ( parent.Parent != null )
339 parent = parent.Parent;
340 return parent.Nodes.TreeView;
342 else {
343 if ( handle == TreeView.RootHandle )
344 return Nodes.TreeView;
346 return null;
350 // --- Public Methods
352 [MonoTODO]
353 public void BeginEdit()
355 //FIXME:
357 [MonoTODO]
358 public virtual object Clone()
360 throw new NotImplementedException ();
362 [MonoTODO]
363 public void Collapse()
365 collapseImpl ( TreeView );
367 [MonoTODO]
368 public void EndEdit(bool cancel)
370 //FIXME:
372 [MonoTODO]
373 public void EnsureVisible()
375 //FIXME:
378 public void Expand()
380 expandImpl ( TreeView );
383 public void ExpandAll()
385 expandAllImpl ( TreeView );
387 [MonoTODO]
388 public static TreeNode FromHandle(TreeView tree, IntPtr handle)
390 return FromHandle ( tree.Nodes , handle );
392 [MonoTODO]
393 public int GetNodeCount(bool includeSubTrees)
395 throw new NotImplementedException ();
397 [MonoTODO]
398 public void Remove()
400 if ( Created ) {
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 ( );
408 [MonoTODO]
409 public void Toggle()
411 //FIXME:
413 [MonoTODO]
414 public override string ToString()
416 //FIXME:
417 return base.ToString();
420 internal void setParent ( TreeNode parent )
422 this.parent = parent;
425 internal void setHandle ( IntPtr hItem )
427 this.handle = 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;
445 else
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;
456 if ( tree == null )
457 return;
459 TVINSERTSTRUCT insStruct = tree.insStruct;
460 insStruct.hParent = parent;
462 unchecked {
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;
471 if ( expanded ) {
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 )
486 return node;
488 TreeNode cnode = FromHandle ( node.Nodes, handle );
489 if ( cnode != null )
490 return cnode;
492 return null;
495 internal void sortNode ( )
497 if ( Created ) {
498 int res = Win32.SendMessage ( TreeView.Handle, (int)TreeViewMessages.TVM_SORTCHILDREN, 0, Handle.ToInt32 ( ) );
499 foreach ( TreeNode node in Nodes )
500 node.sortNode ( );
504 private void zeroHandle ( )
506 handle = IntPtr.Zero;
507 foreach ( TreeNode node in Nodes )
508 node.zeroHandle ( );
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() );
519 else
520 expanded = true;
523 internal void expandAllImpl ( TreeView tree )
525 expandImpl ( 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 );