2005-01-05 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TreeNode.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004-2005 Novell, Inc.
21 //
22 // Authors:
23 //      Jackson Harper (jackson@ximian.com)
24 //      Kazuki Oikawa (kazuki@panicode.com)
25
26 using System;
27 using System.Text;
28 using System.Drawing;
29 using System.Runtime.Serialization;
30
31 namespace System.Windows.Forms {
32
33         [Serializable]
34         public class TreeNode : MarshalByRefObject, ICloneable /*, ISerializable */ {
35
36                 private TreeView tree_view;
37                 internal TreeNode parent;
38                 private int index;
39
40                 private string text;
41                 private int image_index = -1;
42                 private int selected_image_index = -1;
43                 internal TreeNodeCollection nodes;
44                 
45                 private bool is_expanded = false;
46                 private Rectangle bounds = Rectangle.Empty;
47                 private Rectangle plus_minus_bounds = Rectangle.Empty;
48                 private Rectangle checkbox_bounds = Rectangle.Empty;
49                 private bool check;
50                 internal OwnerDrawPropertyBag prop_bag;
51
52                 private object tag;
53                 private IntPtr handle;
54                 
55                 internal TreeNode (TreeView tree_view) : this ()
56                 {
57                         this.tree_view = tree_view;
58                 }
59
60                 public TreeNode ()
61                 {
62                         nodes = new TreeNodeCollection (this);
63                 }
64
65                 public TreeNode (string text) : this ()
66                 {
67                         Text = text;
68                 }
69
70                 public TreeNode (string text, TreeNode [] children) : this (text)
71                 {
72                         Nodes.AddRange (children);
73                 }
74
75                 public TreeNode (string text, int image_index, int selected_image_index) : this (text)
76                 {
77                         this.image_index = image_index;
78                         this.selected_image_index = selected_image_index;
79                 }
80
81                 public TreeNode (string text, int image_index, int selected_image_index,
82                                 TreeNode [] children) : this (text, image_index, selected_image_index)
83                 {
84                         Nodes.AddRange (children);
85                 }
86
87                 internal TreeView TreeView {
88                         get {
89                                 if (tree_view != null)
90                                         return tree_view;
91                                 TreeNode walk = parent;
92                                 while (walk != null) {
93                                         if (walk.TreeView != null)
94                                                 tree_view = walk.TreeView;
95                                         walk = walk.parent;
96                                 }
97                                 return tree_view;
98                         }
99                 }
100
101                 #region ICloneable Members
102
103                 public object Clone()
104                 {
105                         TreeNode tn = new TreeNode (text, image_index, selected_image_index);
106                         if (nodes != null) {
107                                 foreach (TreeNode child in nodes)
108                                         tn.Nodes.Add ((TreeNode)child.Clone ());
109                         }
110                         tn.Tag = tag;
111                         tn.Checked = Checked;
112                         if (prop_bag != null)
113                                 tn.prop_bag = OwnerDrawPropertyBag.Copy (prop_bag);
114                         return tn;
115                 }
116
117                 #endregion
118
119                 public TreeNode Parent {
120                         get {
121                                 if (tree_view != null && tree_view.root_node == parent)
122                                         return null;
123                                 return parent;
124                         }
125                 }
126
127                 public string Text {
128                         get {
129                                 if (text == null)
130                                         return String.Empty;
131                                 return text;
132                         }
133                         set {
134                                 if (text == value)
135                                         return;
136                                 text = value;
137                                 bounds.Width = 0;
138                         }
139                 }
140
141                 public Rectangle Bounds {
142                         get { return bounds; }
143                 }
144
145                 internal Rectangle PlusMinusBounds {
146                         get { return plus_minus_bounds; }
147                 }
148
149                 internal Rectangle CheckBoxBounds {
150                         get { return checkbox_bounds; }
151                 }
152
153                 public bool Checked {
154                         get { return check; }
155                         set {
156                                 if (check == value)
157                                         return;
158                                 check = value;
159
160                                 // We should just be invalidating the node
161                                 if (TreeView != null)
162                                         tree_view.Refresh ();
163                         }
164                 }
165
166                 public Color BackColor {
167                         get { 
168                                 if (prop_bag != null)
169                                         return prop_bag.BackColor;
170                                 if (TreeView != null)
171                                         return TreeView.BackColor;
172                                 return Color.Empty;
173                         }
174                         set { 
175                                 if (prop_bag == null)
176                                         prop_bag = new OwnerDrawPropertyBag ();
177                                 prop_bag.BackColor = value;
178                         }
179                 }
180
181                 public Color ForeColor {
182                         get {
183                                 if (prop_bag != null)
184                                         return prop_bag.ForeColor;
185                                 if (TreeView != null)
186                                         return TreeView.ForeColor;
187                                 return Color.Empty;
188                         }
189                         set {
190                                 if (prop_bag == null)
191                                         prop_bag = new OwnerDrawPropertyBag ();
192                                 prop_bag.ForeColor = value;
193                         }
194                 }
195
196                 public Font NodeFont {
197                         get {
198                                 if (prop_bag != null)
199                                         return prop_bag.Font;
200                                 if (TreeView != null)
201                                         return TreeView.Font;
202                                 return null;
203                         }
204                         set {
205                                 if (prop_bag == null)
206                                         prop_bag = new OwnerDrawPropertyBag (); 
207                                 prop_bag.Font = value;
208                         }
209                 }
210
211                 public TreeNodeCollection Nodes {
212                         get {
213                                 if (nodes == null)
214                                         nodes = new TreeNodeCollection (this);
215                                 return nodes;
216                         }
217                 }
218
219                 public TreeNode FirstNode {
220                         get {
221                                 if (nodes.Count > 0)
222                                         return nodes [0];
223                                 return null;
224                         }
225                 }
226
227                 public string FullPath {
228                         get {
229                                 if (tree_view == null)
230                                         throw new Exception ("No TreeView associated");
231
232                                 StringBuilder builder = new StringBuilder ();
233                                 BuildFullPath (builder);
234                                 return builder.ToString ();
235                         }
236                 }
237
238                 bool BuildFullPath (StringBuilder path)
239                 {
240                         if (parent == null)
241                                 return false;
242
243                         if (parent.BuildFullPath (path))
244                                 path.Append (tree_view.PathSeparator);
245
246                         path.Append (text);
247                         return true;
248                 }
249
250                 public bool IsExpanded {
251                         get { return is_expanded; }
252                 }
253
254                 public TreeNode NextNode {
255                         get {
256                                 if (parent == null)
257                                         return null;
258                                 if (parent.Nodes.Count > index + 1)
259                                         return parent.Nodes [index + 1];
260                                 return null;
261                         }
262                 }
263                 
264                 public TreeNode PrevNode {
265                         get {
266                                 if (parent == null)
267                                         return null;
268                                 if (index == 0 || index > parent.Nodes.Count)
269                                         return null;
270                                 return parent.Nodes [index - 1];
271                         }
272                 }
273
274                 public TreeNode NextVisibleNode {
275                         get {
276                                 OpenTreeNodeEnumerator o = new OpenTreeNodeEnumerator (this);
277                                 if (!o.MoveNext ())
278                                         return null;
279                                 TreeNode c = (TreeNode) o.Current;
280                                 if (!c.IsInClippingRect)
281                                         return null;
282                                 return c;
283                         }
284                 }
285
286                 public TreeNode PrevVisibleNode {
287                         get {
288                                 OpenTreeNodeEnumerator o = new OpenTreeNodeEnumerator (this);
289                                 if (!o.MovePrevious ())
290                                         return null;
291                                 TreeNode c = (TreeNode) o.Current;
292                                 if (!c.IsInClippingRect)
293                                         return null;
294                                 return c;
295                         }
296                 }
297
298                 public TreeNode LastNode {
299                         get {
300                                 return (nodes == null || nodes.Count == 0) ? null : nodes [nodes.Count - 1];
301                         }
302                 }
303
304                 public int Index {
305                         get { return index; }
306                 }
307
308                 public int ImageIndex {
309                         get { return image_index; }
310                         set { image_index = value; }
311                 }
312
313                 public int SelectedImageIndex {
314                         get { return selected_image_index; }
315                         set { selected_image_index = value; }
316                 }
317
318                 public object Tag {
319                         get { return tag; }
320                         set { tag = value; }
321                 }
322
323                 public void Expand ()
324                 {
325                         Expand(false);
326                 }
327                 private void Expand (bool byInternal)
328                 {
329                         if (is_expanded)
330                                 return;
331
332                         bool cancel = false;
333                         if (TreeView != null) {
334                                 TreeViewCancelEventArgs e = new TreeViewCancelEventArgs (this, false, TreeViewAction.Expand);
335                                 TreeView.OnBeforeCollapse (e);
336                                 cancel = e.Cancel;
337                         }
338
339                         if (!cancel) {
340                                 is_expanded = true;
341                                 if (TreeView != null)
342                                         TreeView.OnAfterCollapse (new TreeViewEventArgs (this));
343                                 if (IsNodeVisible () && TreeView != null)
344                                         TreeView.UpdateBelow (this);
345                         }
346                 }
347
348                 public void Collapse ()
349                 {
350                         Collapse(false);
351                 }
352
353                 private void Collapse (bool byInternal)
354                 {
355                         if (!is_expanded)
356                                 return;
357
358                         if (tree_view != null && tree_view.root_node == this)
359                                 return;
360
361                         bool cancel = false;
362                         if (TreeView != null) {
363                                 TreeViewCancelEventArgs e = new TreeViewCancelEventArgs (this, false, TreeViewAction.Collapse);
364                                 TreeView.OnBeforeCollapse (e);
365                                 cancel = e.Cancel;
366                         }
367
368                         if (!cancel) {
369                                 is_expanded = false;
370                                 if (TreeView != null)
371                                         TreeView.OnAfterCollapse (new TreeViewEventArgs (this));
372                                 if (IsNodeVisible () && TreeView != null)
373                                         TreeView.UpdateBelow (this);
374                                 if(!byInternal && TreeView != null && HasFocusInChildren ())
375                                         TreeView.SelectedNode = this;
376                         }
377                 }
378
379                 private bool HasFocusInChildren()
380                 {
381                         if(TreeView == null) return false;
382                         foreach(TreeNode node in nodes) {
383                                 if(node == TreeView.SelectedNode) return true;
384                                 if(node.HasFocusInChildren())
385                                         return true;
386                         }
387                         return false;
388                 }
389
390                 public void Remove ()
391                 {
392                         if (parent == null)
393                                 return;
394                         parent.Nodes.RemoveAt (Index);
395                 }
396
397                 public void ExpandAll ()
398                 {
399                         ExpandRecursive (this);
400                         if(TreeView != null)
401                                 TreeView.Refresh();
402                 }
403
404                 private void ExpandRecursive (TreeNode node)
405                 {
406                         node.Expand (true);
407                         foreach (TreeNode child in node.Nodes) {
408                                 ExpandRecursive (child);
409                         }
410                 }
411
412                 internal void CollapseAll ()
413                 {
414                         CollapseRecursive (this);
415                 }
416
417                 internal void CollapseAllUncheck ()
418                 {
419                         CollapseUncheckRecursive (this);
420                 }
421
422                 private void CollapseRecursive (TreeNode node)
423                 {
424                         node.Collapse ();
425                         foreach (TreeNode child in node.Nodes) {
426                                 CollapseRecursive (child);
427                         }
428                 }
429
430                 private void CollapseUncheckRecursive (TreeNode node)
431                 {
432                         node.Collapse ();
433                         node.Checked = false;
434                         foreach (TreeNode child in node.Nodes) {
435                                 CollapseUncheckRecursive (child);
436                         }
437                 }
438
439                 public int GetNodeCount (bool include_subtrees)
440                 {
441                         if (!include_subtrees)
442                                 return Nodes.Count;
443
444                         int count = 0;
445                         GetNodeCountRecursive (this, ref count);
446
447                         return count;
448                 }
449
450                 public void Toggle ()
451                 {
452                         if (is_expanded)
453                                 Collapse ();
454                         else
455                                 Expand ();
456
457                         if (TreeView != null)
458                                 TreeView.Refresh ();
459                 }
460
461                 internal void SetNodes (TreeNodeCollection nodes)
462                 {
463                         this.nodes = nodes;
464                 }
465
466                 private void GetNodeCountRecursive (TreeNode node, ref int count)
467                 {
468                         count += node.Nodes.Count;
469                         foreach (TreeNode child in node.Nodes) {
470                                 GetNodeCountRecursive (child, ref count);
471                         }
472                 }
473
474                 public override String ToString ()
475                 {
476                         return String.Concat ("TreeNode: ", Text);
477                 }
478
479                 internal void UpdateBounds (int x, int y, int width, int height)
480                 {
481                         bounds.X = x;
482                         bounds.Y = y;
483                         bounds.Width = width;
484                         bounds.Height = height;
485                 }
486
487                 internal void UpdatePlusMinusBounds (int x, int y, int width, int height)
488                 {
489                         plus_minus_bounds.X = x;
490                         plus_minus_bounds.Y = y;
491                         plus_minus_bounds.Width = width;
492                         plus_minus_bounds.Height = height;
493                 }
494
495                 internal void UpdateCheckBoxBounds (int x, int y, int width, int height)
496                 {
497                         checkbox_bounds.X = x;
498                         checkbox_bounds.Y = y;
499                         checkbox_bounds.Width = width;
500                         checkbox_bounds.Height = height;
501                 }
502
503                 internal void SetAddedData (TreeView tree_view, TreeNode parent, int index)
504                 {
505                         this.tree_view = tree_view;
506                         this.parent = parent;
507                         this.index = index;
508                 }
509
510                 private bool IsInClippingRect
511                 {
512                         get {
513                                 if (TreeView == null)
514                                         return false;
515                                 if (bounds.Y < 0 && bounds.Y > tree_view.ClientRectangle.Height)
516                                         return false;
517                                 return true;
518                         }
519                 }
520
521                 private bool IsNodeVisible ()
522                 {
523                         if (TreeView == null)
524                                 return false;
525
526                         if (bounds.Y < 0 && bounds.Y > TreeView.ClientRectangle.Height)
527                                 return false;
528
529                         TreeNode parent = Parent;
530                         while (parent != null) {
531                                 if (!parent.IsExpanded)
532                                         return false;
533                                 parent = parent.Parent;
534                         }
535                         return true;
536                 }
537         }
538 }
539