2004-04-08 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / TreeView.cs
1 //
2 // System.Windows.Forms.TreeView
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
10 //
11 using System.Drawing;
12 using System.ComponentModel;
13 using System.Runtime.InteropServices;
14
15
16 namespace System.Windows.Forms {
17
18         // <summary>
19
20         //
21         // </summary>
22
23     public class TreeView : Control {
24
25                 private int imageIndex;
26                 private int selectedImageIndex;
27                 private TreeNodeCollection nodes;
28                 private int indent;
29                 private BorderStyle borderStyle;
30                 private bool checkBoxes;
31                 private bool fullRowSelect;
32                 private bool hideSelection;
33                 private bool hotTracking;
34                 private bool showLines;
35                 private bool showPlusMinus;
36                 private bool showRootLines;
37                 private ImageList imageList;
38                 private bool sorted;
39                 private TreeNode selectedNode;
40                 private TreeNode dummyNode;
41                 private int itemHeight;
42                 private bool labelEdit;
43                 private bool scrollable;
44                 private string pathSeparator;
45                 private bool updateLocked;
46
47                 const int DefaultIndent = 19;
48                 const int DefaultItemHeight = 16;
49                 
50                 internal TVINSERTSTRUCT insStruct;
51                 //
52                 //  --- Public Constructors
53                 //
54                 [MonoTODO]
55                 public TreeView()
56                 {
57                         imageIndex = 0;
58                         selectedImageIndex = 0;
59                         SubClassWndProc_ = true;
60                         borderStyle = BorderStyle.Fixed3D;
61                         checkBoxes = false;
62                         fullRowSelect = false;
63                         hideSelection = true;
64                         hotTracking = false;
65                         showLines = true;
66                         showPlusMinus = true;
67                         showRootLines = true;
68                         sorted = false;
69                         imageIndex = 0;
70                         imageList = null;
71                         indent = DefaultIndent;
72                         selectedNode = null;
73                         itemHeight = -1;
74                         labelEdit = false;
75                         scrollable = true;
76                         pathSeparator = @"\";
77                         updateLocked = false;
78
79                         SubClassWndProc_ = true;
80
81                         dummyNode = new TreeNode( RootHandle, this );
82
83                         insStruct =  new TVINSERTSTRUCT ( );
84                 }
85                 
86                 // --- Public Properties
87                 
88                 [MonoTODO]
89                 public override Color BackColor {
90                         get { return base.BackColor; }
91                         set {
92                                 base.BackColor = value;
93
94                                 if ( IsHandleCreated )
95                                         setBackColor ( );
96                         }
97                 }
98
99                 [EditorBrowsable (EditorBrowsableState.Never)]
100                 public override Image BackgroundImage {
101                         get { return base.BackgroundImage;  }
102                         set { base.BackgroundImage = value; }
103                 }
104
105                 public BorderStyle BorderStyle {
106                         get {   return borderStyle; }
107                         set {
108                                 if ( !Enum.IsDefined ( typeof(BorderStyle), value ) )
109                                         throw new InvalidEnumArgumentException( "BorderStyle",
110                                                 (int)value,
111                                                 typeof(BorderStyle));
112                                 
113                                 if ( borderStyle != value ) {
114                                         int oldStyle = Win32.getBorderStyle ( borderStyle );
115                                         int oldExStyle = Win32.getBorderExStyle ( borderStyle );
116                                         borderStyle = value;
117
118                                         if ( IsHandleCreated ) {
119                                                 Win32.UpdateWindowStyle ( Handle, oldStyle, Win32.getBorderStyle ( borderStyle ) );
120                                                 Win32.UpdateWindowExStyle ( Handle, oldExStyle, Win32.getBorderExStyle ( borderStyle ) );
121                                         }
122                                 }
123                         }
124                 }
125                 [MonoTODO]
126                 public bool CheckBoxes {
127                         get { return checkBoxes; }
128                         set {
129                                 checkBoxes = value;
130                                 RecreateHandle ( );
131                         }
132                 }
133
134                 public override Color ForeColor {
135                         get { return base.ForeColor; }
136                         set {
137                                 base.ForeColor = value;
138                         
139                                 if ( IsHandleCreated )
140                                         setForeColor ( );
141                         }
142                 }
143
144                 public bool FullRowSelect {
145                         get { return fullRowSelect; }
146                         set {
147                                 if ( fullRowSelect != value ) {
148                                         int oldStyle = fullRowSelect ? (int)TreeViewStyles.TVS_FULLROWSELECT : 0;
149                                         fullRowSelect = value;
150                                                 
151                                         if ( IsHandleCreated ) {
152                                                 int newStyle = fullRowSelect ? (int)TreeViewStyles.TVS_FULLROWSELECT : 0;
153                                                 Win32.UpdateWindowStyle ( Handle, oldStyle, newStyle );
154                                         }
155                                 }
156                         }
157                 }
158
159                 public bool HideSelection {
160                         get { return hideSelection; }
161                         set {
162                                 if ( hideSelection != value ) {
163                                         int oldStyle = hideSelection ? 0 : (int)TreeViewStyles.TVS_SHOWSELALWAYS;
164                                         hideSelection = value;
165                                                 
166                                         if ( IsHandleCreated ) {
167                                                 int newStyle = hideSelection ? 0 : (int)TreeViewStyles.TVS_SHOWSELALWAYS;
168                                                 Win32.UpdateWindowStyle ( Handle, oldStyle, newStyle );
169                                         }
170                                 }
171                         }
172                 }
173
174                 public bool HotTracking {
175                         get { return hotTracking; }
176                         set {
177                                 if ( hotTracking != value ) {
178                                         int oldStyle = hotTracking ? (int)TreeViewStyles.TVS_TRACKSELECT : 0;
179                                         hotTracking = value;
180                                                 
181                                         if ( IsHandleCreated ) {
182                                                 int newStyle = hotTracking ? (int)TreeViewStyles.TVS_TRACKSELECT : 0;
183                                                 Win32.UpdateWindowStyle ( Handle, oldStyle, newStyle );
184                                         }
185                                 }
186                         }
187                 }
188                 [MonoTODO]
189                 public int ImageIndex {
190                         get { return imageIndex; }
191                         set {
192                                 if ( imageIndex != value ) {
193                                         imageIndex = value;
194                                         RecreateHandle ( );
195                                 }
196                         }
197                 }
198                 [MonoTODO]
199                 public ImageList ImageList {
200                         get { return imageList; }
201                         set {
202                                 if ( imageList != value ) {
203                                         imageList = value;
204                                         if ( IsHandleCreated )
205                                                 setImageList ( );
206                                 }
207                         }
208                 }
209
210                 public int Indent {
211                         get { return indent; }
212                         set {
213                                 if ( value < 0 )
214                                         throw new ArgumentException ( 
215                                                  string.Format ("'{0}' is not a valid value for 'Indent'.  'Indent' must be greater than or equal to 0.", value), "value");
216
217                                 if ( value > 32000 )
218                                         throw new ArgumentException ( 
219                                                 string.Format ("'{0}' is not a valid value for 'Indent'. 'Indent' must be less than or equal to 32000.", value), "value" );
220
221                                 if ( indent != value ) {
222                                         indent = value;
223
224                                         if ( IsHandleCreated )
225                                                 setIndent ( );
226                                 }
227                         }
228                 }
229
230                 public int ItemHeight {
231                         get {
232                                 if ( !IsHandleCreated )
233                                         if ( itemHeight == -1 ) return DefaultItemHeight;
234                                 else
235                                         itemHeight = Win32.SendMessage ( Handle, (int)TreeViewMessages.TVM_GETITEMHEIGHT, 0, 0 );
236                                 return itemHeight;
237                         }
238                         set {
239                                 if ( value < 1 )
240                                         throw new ArgumentException ( 
241                                                  string.Format ("'{0}' is not a valid value for 'ItemHeight'.  'ItemHeight' must be greater than or equal to 1.", value), "value");
242
243                                 if ( value > Int16.MaxValue )
244                                         throw new ArgumentException ( 
245                                                 string.Format ("'{0}' is not a valid value for 'ItemHeight'. 'ItemHeight' must be less than or equal to {1}.", value, Int16.MaxValue), "value" );
246
247                                 if ( itemHeight != value ) {
248                                         itemHeight = value;
249                                         if ( IsHandleCreated )
250                                                 Win32.SendMessage ( Handle, (int)TreeViewMessages.TVM_SETITEMHEIGHT, itemHeight, 0 );
251                                 }                               
252                         }
253                 }
254
255                 public bool LabelEdit {
256                         get { return labelEdit; }
257                         set {
258                                 if ( labelEdit != value ) {
259                                         int oldStyle = labelEdit ? (int)TreeViewStyles.TVS_EDITLABELS : 0;
260                                         labelEdit = value;
261                                                 
262                                         if ( IsHandleCreated ) {
263                                                 int newStyle = labelEdit ? (int)TreeViewStyles.TVS_EDITLABELS : 0;
264                                                 Win32.UpdateWindowStyle ( Handle, oldStyle, newStyle );
265                                         }
266                                 }
267                         }
268                 }
269
270                 public TreeNodeCollection Nodes {
271                         get {
272                                 if ( nodes == null )
273                                         nodes = new TreeNodeCollection ( dummyNode );
274                                 return nodes;
275                         }
276                 }
277
278                 public string PathSeparator {
279                         get { return pathSeparator; }
280                         set {
281                                 pathSeparator = value;
282                         }
283                 }
284                 [MonoTODO]
285                 public bool Scrollable {
286                         get { return scrollable; }
287                         set {
288                                 if ( scrollable != value ) {
289                                         scrollable = value;
290                                         RecreateHandle ( );
291                                 }
292                         }
293                 }
294                 [MonoTODO]
295                 public int SelectedImageIndex {
296                         get { return selectedImageIndex; }
297                         set {
298                                 if ( selectedImageIndex != value ) {
299                                         selectedImageIndex = value;
300                                         RecreateHandle ( );
301                                 }
302                         }
303                 }
304                 [MonoTODO]
305                 public TreeNode SelectedNode {
306                         get {
307                                 if ( IsHandleCreated ) {
308                                         int hitem = Win32.SendMessage ( Handle, (int) TreeViewMessages.TVM_GETNEXTITEM, (int)TreeViewItemSelFlags.TVGN_CARET, 0 );
309                                         selectedNode = TreeNode.FromHandle ( this, (IntPtr) hitem );
310                                 }
311                                 return selectedNode;
312                         }
313                         set {
314                                 selectedNode = value;
315                                 if ( IsHandleCreated )
316                                         selectItem ( selectedNode != null ? selectedNode.Handle : IntPtr.Zero );
317                         }
318                 }
319
320                 public bool ShowLines {
321                         get { return showLines; }
322                         set {
323                                 if ( showLines != value ) {
324                                         int oldStyle = showLines ? (int)TreeViewStyles.TVS_HASLINES : 0;
325                                         showLines = value;
326                                                 
327                                         if ( IsHandleCreated ) {
328                                                 int newStyle = showLines ? (int)TreeViewStyles.TVS_HASLINES : 0;
329                                                 Win32.UpdateWindowStyle ( Handle, oldStyle, newStyle );
330                                         }
331                                 }
332                         }
333                 }
334
335                 public bool ShowPlusMinus {
336                         get { return showPlusMinus; }
337                         set {
338                                 if ( showPlusMinus != value ) {
339                                         int oldStyle = showPlusMinus ? (int)TreeViewStyles.TVS_HASBUTTONS : 0;
340                                         showPlusMinus = value;
341                                                 
342                                         if ( IsHandleCreated ) {
343                                                 int newStyle = showPlusMinus ? (int)TreeViewStyles.TVS_HASBUTTONS : 0;
344                                                 Win32.UpdateWindowStyle ( Handle, oldStyle, newStyle );
345                                         }
346                                 }
347                         }
348                 }
349
350                 public bool ShowRootLines {
351                         get { return showRootLines; }
352                         set {
353                                 if ( showRootLines != value ) {
354                                         int oldStyle = showRootLines ? (int)TreeViewStyles.TVS_LINESATROOT : 0;
355                                         showRootLines = value;
356                                                 
357                                         if ( IsHandleCreated ) {
358                                                 int newStyle = showRootLines ? (int)TreeViewStyles.TVS_LINESATROOT : 0;
359                                                 Win32.UpdateWindowStyle ( Handle, oldStyle, newStyle );
360                                         }
361                                 }
362                         }
363                 }
364                 [MonoTODO]
365                 public bool Sorted {
366                         get { return sorted; }
367                         set {
368                                 sorted = value;
369                                 if ( IsHandleCreated && sorted )
370                                         sortTree ( );
371                         }
372                 }
373
374                 [EditorBrowsable (EditorBrowsableState.Never)]
375                 public override string Text {
376                         get { return base.Text;  }
377                         set { base.Text = value; }
378                 }
379
380                 public TreeNode TopNode {
381                         get {
382                                 if ( IsHandleCreated ) {
383                                         int hitem = Win32.SendMessage ( Handle, (int) TreeViewMessages.TVM_GETNEXTITEM, (int)TreeViewItemSelFlags.TVGN_FIRSTVISIBLE, 0 );
384                                         return TreeNode.FromHandle ( this, (IntPtr) hitem );
385                                 }
386                                 return null;
387                         }
388                 }
389
390                 public int VisibleCount {
391                         get {
392                                 if ( IsHandleCreated )
393                                         return Win32.SendMessage ( Handle, (int) TreeViewMessages.TVM_GETVISIBLECOUNT, 0, 0 );
394                                 return 0;
395                         }
396                 }
397                 
398                 // --- Public Methods
399                 
400                 [MonoTODO]
401                 public void BeginUpdate() 
402                 {
403                         updateLocked = true;
404                 }
405
406                 public void CollapseAll()
407                 {
408                         foreach ( TreeNode node in Nodes )
409                                 node.collapseAllImpl ( this );
410                 }
411                 [MonoTODO]
412                 public void EndUpdate()
413                 {
414                         updateLocked = false;
415                 }
416
417                 public void ExpandAll()
418                 {
419                         foreach ( TreeNode node in Nodes )
420                                 node.expandAllImpl ( this );
421                 }
422
423                 public TreeNode GetNodeAt(Point pt)
424                 {
425                         return GetNodeAt ( pt.X, pt.Y );
426                 }
427                 [MonoTODO]
428                 public TreeNode GetNodeAt(int x, int y)
429                 {
430                         throw new NotImplementedException ();
431                 }
432                 [MonoTODO]
433                 public int GetNodeCount(bool includeSubTrees)
434                 {
435                         throw new NotImplementedException ();
436                 }
437                 [MonoTODO]
438                 public override string ToString()
439                 {
440                         //FIXME:
441                         return base.ToString();
442                 }
443                 
444                 // --- Public Events
445                 
446                 public event TreeViewEventHandler AfterCheck;
447                 public event TreeViewEventHandler AfterCollapse;
448                 public event TreeViewEventHandler AfterExpand;
449                 public event NodeLabelEditEventHandler AfterLabelEdit;
450                 public event TreeViewEventHandler AfterSelect;
451                 public event TreeViewCancelEventHandler BeforeCheck;
452                 public event TreeViewCancelEventHandler BeforeCollapse;
453                 public event TreeViewCancelEventHandler BeforeExpand;
454                 public event NodeLabelEditEventHandler BeforeLabelEdit;
455                 public event TreeViewCancelEventHandler BeforeSelect;
456                 public event ItemDragEventHandler ItemDrag;
457                 //public new event PaintEventHandler Paint;
458         
459         // --- Protected Properties
460         
461                 [MonoTODO]
462                 protected override CreateParams CreateParams {
463                         get {
464                                 CreateParams createParams = base.CreateParams;
465
466                                 createParams.ClassName = Win32.TREEVIEW_CLASS;
467                                 createParams.Style |= (int) WindowStyles.WS_CHILD ;
468
469                                 createParams.Style   |= Win32.getBorderStyle   ( BorderStyle );
470                                 createParams.ExStyle |= Win32.getBorderExStyle ( BorderStyle );
471
472                                 if ( CheckBoxes )
473                                         createParams.Style |= (int) TreeViewStyles.TVS_CHECKBOXES;
474
475                                 if ( FullRowSelect )
476                                         createParams.Style |= (int) TreeViewStyles.TVS_FULLROWSELECT;
477
478                                 if ( ShowLines )
479                                         createParams.Style |= (int) TreeViewStyles.TVS_HASLINES;
480
481                                 if ( !HideSelection )
482                                         createParams.Style |= (int) TreeViewStyles.TVS_SHOWSELALWAYS;
483
484                                 if ( HotTracking )
485                                         createParams.Style |= (int) TreeViewStyles.TVS_TRACKSELECT;
486
487                                 if ( ShowPlusMinus )
488                                         createParams.Style |= (int) TreeViewStyles.TVS_HASBUTTONS;
489
490                                 if ( ShowRootLines )
491                                         createParams.Style |= (int) TreeViewStyles.TVS_LINESATROOT;
492
493                                 if ( LabelEdit )
494                                         createParams.Style |= (int) TreeViewStyles.TVS_EDITLABELS;
495
496                                 if ( !Scrollable )
497                                         createParams.Style |= (int) TreeViewStyles.TVS_NOSCROLL;
498
499                                 return createParams;
500                         }               
501                 }
502
503                 protected override Size DefaultSize {
504                         get { return new Size(121,97); }
505                 }
506                 
507                 // --- Protected Methods
508                 
509                 [MonoTODO]
510                 protected override void CreateHandle()
511                 {
512                         initCommonControlsLibrary ( );
513                         base.CreateHandle();
514                 }
515
516                 [MonoTODO]
517                 protected override void Dispose(bool disposing) {
518                         // FIXME:
519                         base.Dispose(disposing);
520                 }
521
522                 [MonoTODO]
523                 protected override bool IsInputKey(Keys keyData)
524                 {
525                         //FIXME:
526                         return base.IsInputKey(keyData);
527                 }
528                 [MonoTODO]
529                 protected virtual void OnAfterCheck(TreeViewEventArgs e)
530                 {
531                         //FIXME:
532                 }
533                 [MonoTODO]
534                 protected virtual void OnAfterCollapse(TreeViewEventArgs e)
535                 {
536                         //FIXME:
537                 }
538                 [MonoTODO]
539                 protected virtual void OnAfterExpand( TreeViewEventArgs e )
540                 {
541                         if ( AfterExpand != null )
542                                 AfterExpand ( this, e );
543                 }
544                 [MonoTODO]
545                 protected virtual void OnAfterLabelEdit(NodeLabelEditEventArgs e)
546                 {
547                         //FIXME:
548                 }
549                 [MonoTODO]
550                 protected virtual void OnAfterSelect(TreeViewEventArgs e)
551                 {
552                         //FIXME:
553                 }
554                 [MonoTODO]
555                 protected virtual void OnBeforeCheck(TreeViewCancelEventArgs e)
556                 {
557                         //FIXME:
558                 }
559                 [MonoTODO]
560                 protected virtual void OnBeforeCollapse(TreeViewCancelEventArgs e)
561                 {
562                         //FIXME:
563                 }
564                 [MonoTODO]
565                 protected virtual void OnBeforeExpand( TreeViewCancelEventArgs e )
566                 {
567                         if ( BeforeExpand != null )
568                                 BeforeExpand ( this, e );
569                 }
570                 [MonoTODO]
571                 protected virtual void OnBeforeLabelEdit(NodeLabelEditEventArgs e)
572                 {
573                         //FIXME:
574                 }
575                 [MonoTODO]
576                 protected virtual void OnBeforeSelect(TreeViewCancelEventArgs e)
577                 {
578                         //FIXME:
579                 }
580                 [MonoTODO]
581                 protected override void OnHandleCreated(EventArgs e)
582                 {
583                         //FIXME:
584                         base.OnHandleCreated(e);
585
586                         makeTree ( );
587                         
588                         setImageList ( );
589                         if ( itemHeight != -1 )
590                                 Win32.SendMessage ( Handle, (int)TreeViewMessages.TVM_SETITEMHEIGHT, itemHeight, 0 );
591                         if ( BackColor != Control.DefaultBackColor )
592                                 setBackColor ( );
593                         if ( ForeColor != Control.DefaultForeColor )
594                                 setForeColor ( );
595                         if ( Indent != DefaultIndent )
596                                 setIndent ( );
597                         if ( selectedNode != null )
598                                 selectItem ( selectedNode.Handle );
599                 }
600                 [MonoTODO]
601                 protected override void OnHandleDestroyed(EventArgs e)
602                 {
603                         //FIXME:
604                         base.OnHandleDestroyed(e);
605                 }
606                 [MonoTODO]
607                 protected virtual void OnItemDrag(ItemDragEventArgs e)
608                 {
609                         //FIXME:
610                 }
611                 [MonoTODO]
612                 protected override void OnKeyDown(KeyEventArgs e)
613                 {
614                         //FIXME:
615                         base.OnKeyDown(e);
616                 }
617                 [MonoTODO]
618                 protected override void OnKeyPress(KeyPressEventArgs e)
619                 {
620                         //FIXME:
621                         base.OnKeyPress(e);
622                 }
623                 [MonoTODO]
624                 protected override void OnKeyUp(KeyEventArgs e)
625                 {
626                         //FIXME:
627                         base.OnKeyUp(e);
628                 }
629                 [MonoTODO]
630                 protected override void WndProc(ref Message m)
631                 {/*
632                         switch ( m.Msg ) {
633                         case Msg.WM_NOTIFY:
634                                 NMTREEVIEW nmhdr = (NMTREEVIEW)Marshal.PtrToStructure ( m.LParam, typeof ( NMTREEVIEW ) );
635
636                                 switch ( nmhdr.hdr.code ) {
637                                 case (int) TreeViewNotifications.TVN_ITEMEXPANDINGA: {
638                                         TreeNode node = TreeNode.FromHandle ( this, nmhdr.itemNew.hItem );
639                                         if ( node != null ) {
640                                                 TreeViewCancelEventArgs args = new TreeViewCancelEventArgs ( node, false, Win32.uint2TreeViewAction ( nmhdr.action ) );
641                                                 OnBeforeExpand ( args );
642                                                 if ( args.Cancel )
643                                                         m.Result = (IntPtr) 1;
644                                         }
645                                 }
646                                 break;
647                                 case (int) TreeViewNotifications.TVN_ITEMEXPANDEDA: {
648                                         TreeNode node = TreeNode.FromHandle ( this, nmhdr.itemNew.hItem );
649                                         if ( node != null ) {
650                                                 TreeViewEventArgs args = new TreeViewEventArgs ( node, Win32.uint2TreeViewAction ( nmhdr.action ) );
651                                                 OnAfterExpand ( args );
652                                         }
653                                 }
654                                 break;
655                                 default:
656                                         CallControlWndProc( ref m );
657                                 break;
658                                 }
659                                 
660                         break;
661                         default:
662                                 CallControlWndProc( ref m );
663                         break;
664                         }*/
665                         CallControlWndProc ( ref m );
666                 }
667
668                 private void initCommonControlsLibrary ( ) {
669                         if ( !RecreatingHandle ) {
670                                 INITCOMMONCONTROLSEX initEx = new INITCOMMONCONTROLSEX();
671                                 initEx.dwICC = CommonControlInitFlags.ICC_TREEVIEW_CLASSES;
672                                 Win32.InitCommonControlsEx(initEx);
673                         }
674                 }
675
676                 internal void makeTree ( )
677                 {
678                         foreach ( TreeNode node in Nodes )
679                                 node.makeTree ( RootHandle, this );
680                 }
681
682                 private void setBackColor ( )
683                 {
684                         Win32.SendMessage ( Handle , (int)TreeViewMessages.TVM_SETBKCOLOR, 0, Win32.RGB( BackColor ) ) ;
685                 }
686
687                 private void setForeColor ( )
688                 {
689                         Win32.SendMessage ( Handle , (int)TreeViewMessages.TVM_SETTEXTCOLOR, 0, Win32.RGB( ForeColor ) ) ;
690                 }
691
692                 private void setImageList ( )
693                 {
694                         int handle = ( ImageList != null ) ? ImageList.Handle.ToInt32 ( ) : 0 ;
695                         Win32.SendMessage ( Handle , (int)TreeViewMessages.TVM_SETIMAGELIST, (int)TreeViewImageListFlags.TVSIL_NORMAL, handle ) ;
696                 }
697
698                 private void setIndent ( )
699                 {
700                         Win32.SendMessage ( Handle , (int)TreeViewMessages.TVM_SETINDENT, Indent, 0 ) ;
701                 }
702
703                 private void selectItem ( IntPtr handle )
704                 {
705                         Win32.SendMessage ( Handle , (int)TreeViewMessages.TVM_SELECTITEM, (int)TreeViewItemSelFlags.TVGN_CARET, handle.ToInt32 ( ) ) ;
706                 }
707
708                 internal static IntPtr RootHandle {
709                         get {
710                                 int rootHandle = 0;
711                                 unchecked {
712                                         rootHandle = (int) TreeViewItemInsertPosition.TVI_ROOT;
713                                 }
714                                 return ( IntPtr ) rootHandle;
715                         }
716                 }
717
718                 private void sortTree ( )
719                 {
720                         int res = Win32.SendMessage ( Handle, (int)TreeViewMessages.TVM_SORTCHILDREN, 0, RootHandle.ToInt32 ( ) );
721                         foreach ( TreeNode node in Nodes )
722                                 node.sortNode ( );
723                 }               
724         }
725 }