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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004-2005 Novell, Inc.
23 // Jackson Harper (jackson@ximian.com)
24 // Kazuki Oikawa (kazuki@panicode.com)
29 using System.Runtime.Serialization;
31 namespace System.Windows.Forms {
34 public class TreeNode : MarshalByRefObject, ICloneable /*, ISerializable */ {
36 private TreeView tree_view;
37 internal TreeNode parent;
41 private int image_index = -1;
42 private int selected_image_index = -1;
43 internal TreeNodeCollection nodes;
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;
50 internal OwnerDrawPropertyBag prop_bag;
53 private IntPtr handle;
55 internal TreeNode (TreeView tree_view) : this ()
57 this.tree_view = tree_view;
62 nodes = new TreeNodeCollection (this);
65 public TreeNode (string text) : this ()
70 public TreeNode (string text, TreeNode [] children) : this (text)
72 Nodes.AddRange (children);
75 public TreeNode (string text, int image_index, int selected_image_index) : this (text)
77 this.image_index = image_index;
78 this.selected_image_index = selected_image_index;
81 public TreeNode (string text, int image_index, int selected_image_index,
82 TreeNode [] children) : this (text, image_index, selected_image_index)
84 Nodes.AddRange (children);
87 internal TreeView TreeView {
89 if (tree_view != null)
91 TreeNode walk = parent;
92 while (walk != null) {
93 if (walk.TreeView != null)
94 tree_view = walk.TreeView;
101 #region ICloneable Members
103 public object Clone()
105 TreeNode tn = new TreeNode (text, image_index, selected_image_index);
107 foreach (TreeNode child in nodes)
108 tn.Nodes.Add ((TreeNode)child.Clone ());
111 tn.Checked = Checked;
112 if (prop_bag != null)
113 tn.prop_bag = OwnerDrawPropertyBag.Copy (prop_bag);
119 public TreeNode Parent {
121 if (tree_view != null && tree_view.root_node == parent)
141 public Rectangle Bounds {
142 get { return bounds; }
145 internal Rectangle PlusMinusBounds {
146 get { return plus_minus_bounds; }
149 internal Rectangle CheckBoxBounds {
150 get { return checkbox_bounds; }
153 public bool Checked {
154 get { return check; }
160 // We should just be invalidating the node
161 if (TreeView != null)
162 tree_view.Refresh ();
166 public Color BackColor {
168 if (prop_bag != null)
169 return prop_bag.BackColor;
170 if (TreeView != null)
171 return TreeView.BackColor;
175 if (prop_bag == null)
176 prop_bag = new OwnerDrawPropertyBag ();
177 prop_bag.BackColor = value;
181 public Color ForeColor {
183 if (prop_bag != null)
184 return prop_bag.ForeColor;
185 if (TreeView != null)
186 return TreeView.ForeColor;
190 if (prop_bag == null)
191 prop_bag = new OwnerDrawPropertyBag ();
192 prop_bag.ForeColor = value;
196 public Font NodeFont {
198 if (prop_bag != null)
199 return prop_bag.Font;
200 if (TreeView != null)
201 return TreeView.Font;
205 if (prop_bag == null)
206 prop_bag = new OwnerDrawPropertyBag ();
207 prop_bag.Font = value;
211 public TreeNodeCollection Nodes {
214 nodes = new TreeNodeCollection (this);
219 public TreeNode FirstNode {
227 public string FullPath {
229 if (tree_view == null)
230 throw new Exception ("No TreeView associated");
232 StringBuilder builder = new StringBuilder ();
233 BuildFullPath (builder);
234 return builder.ToString ();
238 bool BuildFullPath (StringBuilder path)
243 if (parent.BuildFullPath (path))
244 path.Append (tree_view.PathSeparator);
250 public bool IsExpanded {
251 get { return is_expanded; }
254 public TreeNode NextNode {
258 if (parent.Nodes.Count > index + 1)
259 return parent.Nodes [index + 1];
264 public TreeNode PrevNode {
268 if (index == 0 || index > parent.Nodes.Count)
270 return parent.Nodes [index - 1];
274 public TreeNode NextVisibleNode {
276 OpenTreeNodeEnumerator o = new OpenTreeNodeEnumerator (this);
279 TreeNode c = (TreeNode) o.Current;
280 if (!c.IsInClippingRect)
286 public TreeNode PrevVisibleNode {
288 OpenTreeNodeEnumerator o = new OpenTreeNodeEnumerator (this);
289 if (!o.MovePrevious ())
291 TreeNode c = (TreeNode) o.Current;
292 if (!c.IsInClippingRect)
298 public TreeNode LastNode {
300 return (nodes == null || nodes.Count == 0) ? null : nodes [nodes.Count - 1];
305 get { return index; }
308 public int ImageIndex {
309 get { return image_index; }
310 set { image_index = value; }
313 public int SelectedImageIndex {
314 get { return selected_image_index; }
315 set { selected_image_index = value; }
323 public void Expand ()
327 private void Expand (bool byInternal)
333 if (TreeView != null) {
334 TreeViewCancelEventArgs e = new TreeViewCancelEventArgs (this, false, TreeViewAction.Expand);
335 TreeView.OnBeforeCollapse (e);
341 if (TreeView != null)
342 TreeView.OnAfterCollapse (new TreeViewEventArgs (this));
343 if (IsNodeVisible () && TreeView != null)
344 TreeView.UpdateBelow (this);
348 public void Collapse ()
353 private void Collapse (bool byInternal)
358 if (tree_view != null && tree_view.root_node == this)
362 if (TreeView != null) {
363 TreeViewCancelEventArgs e = new TreeViewCancelEventArgs (this, false, TreeViewAction.Collapse);
364 TreeView.OnBeforeCollapse (e);
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;
379 private bool HasFocusInChildren()
381 if(TreeView == null) return false;
382 foreach(TreeNode node in nodes) {
383 if(node == TreeView.SelectedNode) return true;
384 if(node.HasFocusInChildren())
390 public void Remove ()
394 parent.Nodes.RemoveAt (Index);
397 public void ExpandAll ()
399 ExpandRecursive (this);
404 private void ExpandRecursive (TreeNode node)
407 foreach (TreeNode child in node.Nodes) {
408 ExpandRecursive (child);
412 internal void CollapseAll ()
414 CollapseRecursive (this);
417 internal void CollapseAllUncheck ()
419 CollapseUncheckRecursive (this);
422 private void CollapseRecursive (TreeNode node)
425 foreach (TreeNode child in node.Nodes) {
426 CollapseRecursive (child);
430 private void CollapseUncheckRecursive (TreeNode node)
433 node.Checked = false;
434 foreach (TreeNode child in node.Nodes) {
435 CollapseUncheckRecursive (child);
439 public int GetNodeCount (bool include_subtrees)
441 if (!include_subtrees)
445 GetNodeCountRecursive (this, ref count);
450 public void Toggle ()
457 if (TreeView != null)
461 internal void SetNodes (TreeNodeCollection nodes)
466 private void GetNodeCountRecursive (TreeNode node, ref int count)
468 count += node.Nodes.Count;
469 foreach (TreeNode child in node.Nodes) {
470 GetNodeCountRecursive (child, ref count);
474 public override String ToString ()
476 return String.Concat ("TreeNode: ", Text);
479 internal void UpdateBounds (int x, int y, int width, int height)
483 bounds.Width = width;
484 bounds.Height = height;
487 internal void UpdatePlusMinusBounds (int x, int y, int width, int height)
489 plus_minus_bounds.X = x;
490 plus_minus_bounds.Y = y;
491 plus_minus_bounds.Width = width;
492 plus_minus_bounds.Height = height;
495 internal void UpdateCheckBoxBounds (int x, int y, int width, int height)
497 checkbox_bounds.X = x;
498 checkbox_bounds.Y = y;
499 checkbox_bounds.Width = width;
500 checkbox_bounds.Height = height;
503 internal void SetAddedData (TreeView tree_view, TreeNode parent, int index)
505 this.tree_view = tree_view;
506 this.parent = parent;
510 private bool IsInClippingRect
513 if (TreeView == null)
515 if (bounds.Y < 0 && bounds.Y > tree_view.ClientRectangle.Height)
521 private bool IsNodeVisible ()
523 if (TreeView == null)
526 if (bounds.Y < 0 && bounds.Y > TreeView.ClientRectangle.Height)
529 TreeNode parent = Parent;
530 while (parent != null) {
531 if (!parent.IsExpanded)
533 parent = parent.Parent;