private void OnMouseLeftButtonDown(ActivatableTabItem item, MouseButtonEventArgs e)
        {
            e.MouseDevice.Capture(item);

            this.draggedItem            = item;
            this.sourceControl          = item.FindParent <ActivatableTabControl>();
            this.sourceSplitTabsControl = this.sourceControl.FindParent <SplitTabsControl>();

            this.offset    = e.GetPosition(item);
            this.preMoving = true;

            item.LostMouseCapture  += this.OnLostMouseCapture;
            item.MouseLeftButtonUp += this.OnMouseLeftButtonUp;
            item.MouseMove         += this.OnMouseMove;
        }
        private void OnMouseLeftButtonDown(ActivatableTabItem item, MouseButtonEventArgs e)
        {
            e.MouseDevice.Capture(item);

            this.draggedItem = item;
            this.sourceControl = item.FindParent<ActivatableTabControl>();
            this.sourceSplitTabsControl = this.sourceControl.FindParent<SplitTabsControl>();

            this.offset = e.GetPosition(item);
            this.preMoving = true;

            item.LostMouseCapture += this.OnLostMouseCapture;
            item.MouseLeftButtonUp += this.OnMouseLeftButtonUp;
            item.MouseMove += this.OnMouseMove;
        }
        public void RemoveItemFromControl(ActivatableTabItem item)
        {
            var control = item.FindParent <ActivatableTabControl>();

            if (control == null)
            {
                Debug.Fail("Can't find parent tab control!");
                return;
            }

            var node = control.TabNode;

            if (node == null)
            {
                Debug.Fail("Tab control does not have a tab node set!");
                return;
            }

            node.TabControl.Items.Remove(item);
            if (node.TabControl.Items.Count == 0)
            {
                bool wasActive = (node == this.activeTabNode);

                // No more tabs in this control.
                if (node.Parent != null)
                {
                    // This node is no longer needed.  Remove the tab control and fix the tree.
                    this.host.Children.Remove(node.TabControl);

                    node.Parent.Children.Remove(node);
                    node.Parent.Slot.Children.Remove(node.Slot);

                    TabNode lastChild = node.Parent.Children[0];

                    if (node.Parent.Children.Count == 1)
                    {
                        // The parent now only has a single child. That means the last node (lastChild) is no longer
                        // necessary; its contents should collapse up into the parent. Note that this could "bleed up"
                        // into the grandparent, as in this case:
                        //
                        // ------------------------
                        // |      |   |   |       |
                        // |      |   |   |       |
                        // |      |   |   |       |
                        // |      |-------|       |
                        // |      |       |       |
                        // |      |   X   |       |
                        // |      |       |       |
                        // ------------------------
                        //
                        // Closing the X node leaves a single node in its parent, so that node replaces the parent.
                        // That node contains two children, and has horizontal orientation like it's *new* parent
                        // (the grandparent).  In these cases, the parent node also becomes unnecessary, and should
                        // be replaced in the grandparent node with all of its children.
                        //
                        // Note that programmatically, it is possible to create a node tree such that node has children
                        // (with grandchildren) that have the same orientation as the parent.  However, using the drag
                        // manager, this should never happen; the logic in SplitNode detects such cases and inserts
                        // nodes appropriately such that orientation always flips as you go down the tree.  So, any time
                        // the remaining child (lastChild) has children, it should always result in this "bleed up"
                        // effect. But we check just in case.
                        if (lastChild.Children != null)
                        {
                            var grandparent = node.Parent.Parent;

                            if (grandparent != null && lastChild.Slot.Orientation == grandparent.Slot.Orientation)
                            {
                                // Here's the "bleed up" -- skip right past parent
                                int    index = grandparent.Children.IndexOf(node.Parent);
                                double totalSizeInLastChild   = lastChild.Slot.Children.Sum(s => s.Length.Value);
                                double totalSizeInGrandParent = grandparent.Slot.Children.Sum(s => s.Length.Value);

                                grandparent.Children.RemoveAt(index);
                                grandparent.Slot.Children.RemoveAt(index);
                                foreach (var child in lastChild.Children)
                                {
                                    grandparent.Children.Insert(index, child);
                                    child.Parent = grandparent;
                                    grandparent.Slot.Children.Insert(index, child.Slot);
                                    child.Slot.Length = new GridLength((child.Slot.Length.Value / totalSizeInLastChild) * totalSizeInGrandParent, GridUnitType.Star);
                                    index            += 1;
                                }
                            }
                            else
                            {
                                // Just put lastChild's children into the parent, replacing itself.  This case should only
                                // get hit if the parent is the root, unless someone does programmatic manipulation of the slots.
                                node.Parent.Children.Clear();
                                node.Parent.Slot.Children.Clear();
                                foreach (var child in lastChild.Children)
                                {
                                    node.Parent.Children.Add(child);
                                    child.Parent = node.Parent;
                                    node.Parent.Slot.Children.Add(child.Slot);
                                }
                                node.Parent.Slot.Orientation = lastChild.Slot.Orientation;
                            }
                        }
                        else
                        {
                            // Just put lastChild's tab control into the parent, removing itself
                            node.Parent.Children = null;
                            node.Parent.Slot.Children.Clear();
                            node.Parent.TabControl = lastChild.TabControl;
                            SlotPanel.SetSlotName(node.Parent.TabControl, node.Parent.Slot.Name);
                            node.Parent.TabControl.TabNode = node.Parent;
                        }
                    }

                    if (wasActive)
                    {
                        ActivateLastActiveTabNode();
                    }
                }
                else
                {
                    // We're now empty, completely.  If we're a floating host, go away.
                    if (this.masterControl != null)
                    {
                        var parent = this.FindParent <FloatingWindow>();

                        if (parent != null)
                        {
                            parent.Close();
                        }
                    }
                }
            }
        }
        public void RemoveItemFromControl(ActivatableTabItem item)
        {
            var control = item.FindParent<ActivatableTabControl>();

            if (control == null)
            {
                Debug.Fail("Can't find parent tab control!");
                return;
            }

            var node = control.TabNode;

            if (node == null)
            {
                Debug.Fail("Tab control does not have a tab node set!");
                return;
            }

            node.TabControl.Items.Remove(item);
            if (node.TabControl.Items.Count == 0)
            {
                bool wasActive = (node == this.activeTabNode);

                // No more tabs in this control.
                if (node.Parent != null)
                {
                    // This node is no longer needed.  Remove the tab control and fix the tree.
                    this.host.Children.Remove(node.TabControl);

                    node.Parent.Children.Remove(node);
                    node.Parent.Slot.Children.Remove(node.Slot);

                    TabNode lastChild = node.Parent.Children[0];

                    if (node.Parent.Children.Count == 1)
                    {
                        // The parent now only has a single child. That means the last node (lastChild) is no longer
                        // necessary; its contents should collapse up into the parent. Note that this could "bleed up"
                        // into the grandparent, as in this case:
                        //
                        // ------------------------
                        // |      |   |   |       |
                        // |      |   |   |       |
                        // |      |   |   |       |
                        // |      |-------|       |
                        // |      |       |       |
                        // |      |   X   |       |
                        // |      |       |       |
                        // ------------------------
                        // 
                        // Closing the X node leaves a single node in its parent, so that node replaces the parent.
                        // That node contains two children, and has horizontal orientation like it's *new* parent
                        // (the grandparent).  In these cases, the parent node also becomes unnecessary, and should 
                        // be replaced in the grandparent node with all of its children.
                        //
                        // Note that programmatically, it is possible to create a node tree such that node has children
                        // (with grandchildren) that have the same orientation as the parent.  However, using the drag
                        // manager, this should never happen; the logic in SplitNode detects such cases and inserts
                        // nodes appropriately such that orientation always flips as you go down the tree.  So, any time
                        // the remaining child (lastChild) has children, it should always result in this "bleed up"
                        // effect. But we check just in case.
                        if (lastChild.Children != null)
                        {
                            var grandparent = node.Parent.Parent;

                            if (grandparent != null && lastChild.Slot.Orientation == grandparent.Slot.Orientation)
                            {
                                // Here's the "bleed up" -- skip right past parent
                                int index = grandparent.Children.IndexOf(node.Parent);
                                double totalSizeInLastChild = lastChild.Slot.Children.Sum(s => s.Length.Value);
                                double totalSizeInGrandParent = grandparent.Slot.Children.Sum(s => s.Length.Value);

                                grandparent.Children.RemoveAt(index);
                                grandparent.Slot.Children.RemoveAt(index);
                                foreach (var child in lastChild.Children)
                                {
                                    grandparent.Children.Insert(index, child);
                                    child.Parent = grandparent;
                                    grandparent.Slot.Children.Insert(index, child.Slot);
                                    child.Slot.Length = new GridLength((child.Slot.Length.Value / totalSizeInLastChild) * totalSizeInGrandParent, GridUnitType.Star);
                                    index += 1;
                                }
                            }
                            else
                            {
                                // Just put lastChild's children into the parent, replacing itself.  This case should only
                                // get hit if the parent is the root, unless someone does programmatic manipulation of the slots.
                                node.Parent.Children.Clear();
                                node.Parent.Slot.Children.Clear();
                                foreach (var child in lastChild.Children)
                                {
                                    node.Parent.Children.Add(child);
                                    child.Parent = node.Parent;
                                    node.Parent.Slot.Children.Add(child.Slot);
                                }
                                node.Parent.Slot.Orientation = lastChild.Slot.Orientation;
                            }
                        }
                        else
                        {
                            // Just put lastChild's tab control into the parent, removing itself
                            node.Parent.Children = null;
                            node.Parent.Slot.Children.Clear();
                            node.Parent.TabControl = lastChild.TabControl;
                            SlotPanel.SetSlotName(node.Parent.TabControl, node.Parent.Slot.Name);
                            node.Parent.TabControl.TabNode = node.Parent;
                        }
                    }

                    if (wasActive)
                    {
                        ActivateLastActiveTabNode();
                    }
                }
                else
                {
                    // We're now empty, completely.  If we're a floating host, go away.
                    if (this.masterControl != null)
                    {
                        var parent = this.FindParent<FloatingWindow>();

                        if (parent != null)
                            parent.Close();
                    }
                }
            }
        }