2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TreeNode.cs
index 5f8353fe311b92330fcf5367e809f29861b1e1f4..1c8294730d11439b067595cb54cd2593b092886f 100644 (file)
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-// Copyright (c) 2004 Novell, Inc.
+// Copyright (c) 2004-2005 Novell, Inc.
 //
 // Authors:
 //     Jackson Harper (jackson@ximian.com)
+//     Kazuki Oikawa (kazuki@panicode.com)
 
 using System;
 using System.Text;
@@ -41,10 +42,12 @@ namespace System.Windows.Forms {
                private int selected_image_index = -1;
                internal TreeNodeCollection nodes;
                
-               private bool is_expanded = true;
+               private bool is_expanded = false;
                private Rectangle bounds = Rectangle.Empty;
-               internal Rectangle plus_minus_bounds = Rectangle.Empty;
+               private Rectangle plus_minus_bounds = Rectangle.Empty;
+               private Rectangle checkbox_bounds = Rectangle.Empty;
                private bool check;
+               private bool is_editing;
                internal OwnerDrawPropertyBag prop_bag;
 
                private object tag;
@@ -140,14 +143,33 @@ namespace System.Windows.Forms {
                        get { return bounds; }
                }
 
+               internal Rectangle PlusMinusBounds {
+                       get { return plus_minus_bounds; }
+               }
+
+               internal Rectangle CheckBoxBounds {
+                       get { return checkbox_bounds; }
+               }
+
                public bool Checked {
                        get { return check; }
-                       set { check = value; }
+                       set {
+                               if (check == value)
+                                       return;
+                               check = value;
+
+                               if (TreeView != null)
+                                       tree_view.UpdateNode (this);
+                       }
                }
 
                public Color BackColor {
                        get { 
-                               return prop_bag == null ? Color.Empty : prop_bag.BackColor;
+                               if (prop_bag != null)
+                                       return prop_bag.BackColor;
+                               if (TreeView != null)
+                                       return TreeView.BackColor;
+                               return Color.Empty;
                        }
                        set { 
                                if (prop_bag == null)
@@ -157,8 +179,12 @@ namespace System.Windows.Forms {
                }
 
                public Color ForeColor {
-                       get { 
-                               return prop_bag == null ? Color.Empty : prop_bag.ForeColor;
+                       get {
+                               if (prop_bag != null)
+                                       return prop_bag.ForeColor;
+                               if (TreeView != null)
+                                       return TreeView.ForeColor;
+                               return Color.Empty;
                        }
                        set {
                                if (prop_bag == null)
@@ -169,7 +195,11 @@ namespace System.Windows.Forms {
 
                public Font NodeFont {
                        get {
-                               return prop_bag == null ? null : prop_bag.Font;
+                               if (prop_bag != null)
+                                       return prop_bag.Font;
+                               if (TreeView != null)
+                                       return TreeView.Font;
+                               return null;
                        }
                        set {
                                if (prop_bag == null)
@@ -290,7 +320,53 @@ namespace System.Windows.Forms {
                        set { tag = value; }
                }
 
+               public bool IsSelected {
+                       get {
+                               if (TreeView == null)
+                                       return false;
+                               return TreeView.SelectedNode == this;
+                       }
+               }
+
+               public bool IsEditing {
+                       get { return is_editing; }
+               }
+
+               public bool IsVisible {
+                       get {
+                               if (TreeView == null)
+                                       return false;
+
+                               if (bounds.Y < 0 && bounds.Y > TreeView.ClientRectangle.Height)
+                                       return false;
+
+                               TreeNode parent = Parent;
+                               while (parent != null) {
+                                       if (!parent.IsExpanded)
+                                               return false;
+                                       parent = parent.Parent;
+                               }
+                               return true;
+                       }
+               }
+
+               public void BeginEdit ()
+               {
+                       is_editing = true;
+               }
+
+               public void EndEdit (bool cancel)
+               {
+                       is_editing = false;
+                       if (!cancel && TreeView != null)
+                               text = TreeView.LabelEditText;
+               }
+
                public void Expand ()
+               {
+                       Expand(false);
+               }
+               private void Expand (bool byInternal)
                {
                        if (is_expanded)
                                return;
@@ -306,16 +382,24 @@ namespace System.Windows.Forms {
                                is_expanded = true;
                                if (TreeView != null)
                                        TreeView.OnAfterCollapse (new TreeViewEventArgs (this));
-                               if (IsNodeVisible () && TreeView != null)
+                               if (IsVisible && TreeView != null)
                                        TreeView.UpdateBelow (this);
                        }
                }
 
                public void Collapse ()
+               {
+                       Collapse(false);
+               }
+
+               private void Collapse (bool byInternal)
                {
                        if (!is_expanded)
                                return;
 
+                       if (tree_view != null && tree_view.root_node == this)
+                               return;
+
                        bool cancel = false;
                        if (TreeView != null) {
                                TreeViewCancelEventArgs e = new TreeViewCancelEventArgs (this, false, TreeViewAction.Collapse);
@@ -327,9 +411,22 @@ namespace System.Windows.Forms {
                                is_expanded = false;
                                if (TreeView != null)
                                        TreeView.OnAfterCollapse (new TreeViewEventArgs (this));
-                               if (IsNodeVisible () && TreeView != null)
+                               if (IsVisible && TreeView != null)
                                        TreeView.UpdateBelow (this);
+                               if(!byInternal && TreeView != null && HasFocusInChildren ())
+                                       TreeView.SelectedNode = this;
+                       }
+               }
+
+               private bool HasFocusInChildren()
+               {
+                       if(TreeView == null) return false;
+                       foreach(TreeNode node in nodes) {
+                               if(node == TreeView.SelectedNode) return true;
+                               if(node.HasFocusInChildren())
+                                       return true;
                        }
+                       return false;
                }
 
                public void Remove ()
@@ -342,11 +439,13 @@ namespace System.Windows.Forms {
                public void ExpandAll ()
                {
                        ExpandRecursive (this);
+                       if(TreeView != null)
+                               TreeView.Refresh();
                }
 
                private void ExpandRecursive (TreeNode node)
                {
-                       node.Expand ();
+                       node.Expand (true);
                        foreach (TreeNode child in node.Nodes) {
                                ExpandRecursive (child);
                        }
@@ -357,6 +456,11 @@ namespace System.Windows.Forms {
                        CollapseRecursive (this);
                }
 
+               internal void CollapseAllUncheck ()
+               {
+                       CollapseUncheckRecursive (this);
+               }
+
                private void CollapseRecursive (TreeNode node)
                {
                        node.Collapse ();
@@ -365,6 +469,15 @@ namespace System.Windows.Forms {
                        }
                }
 
+               private void CollapseUncheckRecursive (TreeNode node)
+               {
+                       node.Collapse ();
+                       node.Checked = false;
+                       foreach (TreeNode child in node.Nodes) {
+                               CollapseUncheckRecursive (child);
+                       }
+               }
+
                public int GetNodeCount (bool include_subtrees)
                {
                        if (!include_subtrees)
@@ -382,9 +495,6 @@ namespace System.Windows.Forms {
                                Collapse ();
                        else
                                Expand ();
-
-                       if (TreeView != null)
-                               TreeView.Refresh ();
                }
 
                internal void SetNodes (TreeNodeCollection nodes)
@@ -421,6 +531,14 @@ namespace System.Windows.Forms {
                        plus_minus_bounds.Height = height;
                }
 
+               internal void UpdateCheckBoxBounds (int x, int y, int width, int height)
+               {
+                       checkbox_bounds.X = x;
+                       checkbox_bounds.Y = y;
+                       checkbox_bounds.Width = width;
+                       checkbox_bounds.Height = height;
+               }
+
                internal void SetAddedData (TreeView tree_view, TreeNode parent, int index)
                {
                        this.tree_view = tree_view;
@@ -428,6 +546,11 @@ namespace System.Windows.Forms {
                        this.index = index;
                }
 
+               internal void SetIndex (int index)
+               {
+                       this.index = index;
+               }
+
                private bool IsInClippingRect
                {
                        get {
@@ -438,23 +561,6 @@ namespace System.Windows.Forms {
                                return true;
                        }
                }
-
-               private bool IsNodeVisible ()
-               {
-                       if (TreeView == null)
-                               return false;
-
-                       if (bounds.Y < 0 && bounds.Y > TreeView.ClientRectangle.Height)
-                               return false;
-
-                       TreeNode parent = Parent;
-                       while (parent != null) {
-                               if (!parent.IsExpanded)
-                                       return false;
-                               parent = parent.Parent;
-                       }
-                       return true;
-               }
        }
 }