public Tree(Tree parent, object owner)
 {
     _parent = parent;
     if (parent != null)
     {
         TreeRoot    r   = parent.Root;
         ObjectIDmap map = r.Map;
         IClassRef   cr  = map.GetClassRefByObject(owner);
         if (cr != null)
         {
             owner = cr;
         }
     }
     _owner = owner;
 }
        /// <summary>
        /// to be called from root
        /// </summary>
        /// <param name="c"></param>
        public Tree AddChild(object c)
        {
            ObjectIDmap map = Root.Map;
            IClassRef   cr  = map.GetClassRefByObject(c);

            if (cr != null)
            {
                c = cr;
            }
            Tree t0 = SearchChildByOwner(c);

            if (t0 != null)
            {
                return(t0);
            }
            ToolStripItem mi = c as ToolStripItem;

            if (mi != null)
            {
                ToolStrip ts = GetTopMenuOwner(mi);
                if (ts == this.Owner)
                {
                    if (mi.OwnerItem == null)
                    {
                        this.Add(new Tree(this, mi));
                        return(this);
                    }
                    else
                    {
                        Tree t = this.SearchChildByOwner(mi.OwnerItem);
                        if (t != null)
                        {
                            t.Add(new Tree(t, mi));
                            return(t);
                        }
                        else
                        {
                            this.Add(new Tree(this, mi));
                            return(this);
                        }
                    }
                }
                else
                {
                    foreach (Tree t in this)
                    {
                        if (ts == t.Owner)
                        {
                            return(t.AddChild(mi));
                        }
                    }
                    this.Add(new Tree(this, mi));
                    return(this);
                }
            }
            else
            {
                DataGridViewColumn dc = c as DataGridViewColumn;
                if (dc != null)
                {
                    Tree tp = this.SearchChildByOwner(dc.DataGridView);
                    if (tp == null)
                    {
                        tp = this;
                    }
                    tp.Add(new Tree(tp, c));
                    return(tp);
                }
                SplitterPanel stp = c as SplitterPanel;
                if (stp != null)
                {
                    Tree tp = this.SearchChildByOwner(stp.Parent);
                    if (tp == null)
                    {
                        tp = this;
                    }
                    tp.Add(new Tree(tp, c));
                    return(tp);
                }
                else
                {
                    Control ctl = c as Control;
                    if (ctl != null)
                    {
                        TreeRoot tr = this as TreeRoot;
                        if (tr != null)
                        {
                            ctl.ParentChanged -= tr.ParentChangehandler;
                            ctl.ParentChanged += tr.ParentChangehandler;
                        }
                    }
                    Tree tParent = this;

                    Control r = this.Owner as Control;

                    if (ctl != null)
                    {
                        if (ctl.Parent != null)
                        {
                            if (ctl.Parent != r)
                            {
                                Tree t = this.SearchChildByOwner(ctl.Parent);
                                if (t != null)
                                {
                                    tParent = t;
                                }
                            }
                        }
                    }
                    Tree tx = new Tree(tParent, c);
                    tParent.Add(tx);
                    SplitContainer sc = c as SplitContainer;
                    if (sc != null)
                    {
                        tx.Add(new Tree(tx, sc.Panel1));
                        tx.Add(new Tree(tx, sc.Panel2));
                    }
                    return(tParent);
                }
            }
        }