private void FloatingPane_EndDrag(object sender, EventArgs e)
        {
            System.Diagnostics.Trace.Assert(sender is FloatingPane, System.Reflection.MethodBase.GetCurrentMethod().Name + ": sender not a FloatingPane");
            Application.Current.Dispatcher.Invoke(delegate
            {
                FloatingPane floatingPane = sender as FloatingPane;

                if (
                    (floatingPane == null) ||
                    ((SelectedPane != null) && !(SelectedPane is FloatingPane) && !(SelectedPane.Parent is SplitterPane) && !(SelectedPane.Parent is DocumentPanel) && (SelectedPane.Parent != IFloatingPaneHost.RootGrid)) ||
                    (_insertionIndicatorManager == null) ||
                    (_insertionIndicatorManager.WindowLocation == WindowLocation.None)
                    )
                {
                    if (floatingPane != null)
                    {
                        floatingPane.Close();
                    }
                    CancelSelection();
                    return;
                }

                ISelectablePane selectedPane  = SelectedPane;
                WindowLocation windowLocation = _insertionIndicatorManager.WindowLocation;
                CancelSelection();

                if (selectedPane is FloatingPane)
                {
                    IFloatingPane selectedFloatingPane = selectedPane as IFloatingPane;
                    selectedFloatingPane.IViewContainer.ExtractDocuments(floatingPane.IViewContainer);
                    if (floatingPane is FloatingToolPaneGroup)
                    {
                        FloatingToolPaneGroups.Remove(floatingPane);
                    }
                    else
                    {
                        FloatingDocumentPaneGroups.Remove(floatingPane);
                    }
                    floatingPane.Close();
                }
                else
                {
                    IFloatingPaneHost.Unfloat(floatingPane, selectedPane as SelectablePane, windowLocation);
                }

                Application.Current.MainWindow.Activate();
            });
        }
        private void FloatingWindow_LocationChanged(object sender, EventArgs e)
        {
            System.Diagnostics.Trace.Assert(sender is FloatingPane);

            Window          floatingWindow         = sender as Window;
            Point           cursorPositionOnScreen = OpenControls.Wpf.Utilities.Windows.GetCursorPosition();
            ISelectablePane previousPane           = SelectedPane;
            bool            foundSelectedPane      = false;

            int           windowCount = 0;
            List <IntPtr> hWnds       = new List <IntPtr>();
            Dictionary <IntPtr, IFloatingPane> dict = new Dictionary <IntPtr, IFloatingPane>();

            List <IFloatingPane> floatingPanes = (sender is FloatingToolPaneGroup) ? FloatingToolPaneGroups : FloatingDocumentPaneGroups;

            foreach (IFloatingPane iFloatingPane in floatingPanes)
            {
                if (iFloatingPane != sender)
                {
                    Point         cursorPositionInFloatingPane = (iFloatingPane as FloatingPane).PointFromScreen(cursorPositionOnScreen);
                    ViewContainer viewContainer = (iFloatingPane.IViewContainer as ViewContainer);
                    if (
                        (cursorPositionInFloatingPane.X >= 0) &&
                        (cursorPositionInFloatingPane.X <= iFloatingPane.ActualWidth) &&
                        (cursorPositionInFloatingPane.Y >= 0) &&
                        (cursorPositionInFloatingPane.Y <= iFloatingPane.ActualHeight)
                        )
                    {
                        var wih = new System.Windows.Interop.WindowInteropHelper((iFloatingPane as FloatingPane));
                        hWnds.Add(wih.Handle);
                        dict.Add(wih.Handle, iFloatingPane);
                        windowCount++;
                    }
                }
            }

            if (windowCount > 0)
            {
                IntPtr intPtr = GetTopmostWindow(hWnds);
                if (dict.ContainsKey(intPtr))
                {
                    foundSelectedPane = true;
                    IFloatingPane iFloatingPane = dict[intPtr];

                    if (SelectedPane != null)
                    {
                        SelectedPane.IsHighlighted = false;
                    }

                    if (SelectedPane != iFloatingPane)
                    {
                        _windowLocationPane?.Close();
                        _windowLocationPane         = null;
                        SelectedPane                = iFloatingPane;
                        iFloatingPane.IsHighlighted = true;

                        _windowLocationPane = new WindowLocationPane();
                        _windowLocationPane.AllowsTransparency = true;
                        _windowLocationPane.ShowIcons(WindowLocation.Middle);
                        ViewContainer viewContainer    = (iFloatingPane.IViewContainer as ViewContainer);
                        Point         topLeftPanePoint = viewContainer.PointToScreen(new Point(0, 0));

                        PresentationSource source = PresentationSource.FromVisual(viewContainer);
                        double             dpix   = source.CompositionTarget.TransformToDevice.M11;
                        double             dpiy   = source.CompositionTarget.TransformToDevice.M22;

                        _windowLocationPane.Left   = topLeftPanePoint.X / dpix;
                        _windowLocationPane.Top    = topLeftPanePoint.Y / dpiy;
                        _windowLocationPane.Width  = viewContainer.ActualWidth;
                        _windowLocationPane.Height = viewContainer.ActualHeight;
                        _windowLocationPane.Show();
                    }
                    _sideLocationPane?.Close();
                    _sideLocationPane = null;
                }
            }

            if (!foundSelectedPane)
            {
                Point cursorPositionInMainWindow = IFloatingPaneHost.RootGrid.PointFromScreen(cursorPositionOnScreen);
                if (
                    (cursorPositionInMainWindow.X >= 0) &&
                    (cursorPositionInMainWindow.X <= IFloatingPaneHost.RootGrid.ActualWidth) &&
                    (cursorPositionInMainWindow.Y >= 0) &&
                    (cursorPositionInMainWindow.Y <= IFloatingPaneHost.RootGrid.ActualHeight)
                    )
                {
                    Type paneType = (sender is FloatingDocumentPaneGroup) ? typeof(DocumentPaneGroup) : typeof(ToolPaneGroup);
                    var  pane     = IFloatingPaneHost.FindSelectablePane(cursorPositionOnScreen);
                    foundSelectedPane = pane != null;
                    if ((pane != null) && (SelectedPane != pane))
                    {
                        if (SelectedPane != null)
                        {
                            SelectedPane.IsHighlighted = false;
                        }

                        pane.IsHighlighted = true;
                        SelectedPane       = pane;
                        _windowLocationPane?.Close();
                        _windowLocationPane = null;

                        if ((paneType == pane.GetType()) || ((pane is DocumentPanel) && (sender is FloatingDocumentPaneGroup)))
                        {
                            _windowLocationPane = new WindowLocationPane();
                            _windowLocationPane.AllowsTransparency = true;
                            if (SelectedPane is DocumentPanel)
                            {
                                _windowLocationPane.ShowIcons(WindowLocation.Middle);
                            }
                            Point topLeftPanePoint = pane.PointToScreen(new Point(0, 0));

                            PresentationSource source = PresentationSource.FromVisual((UIElement)pane);
                            double             dpix   = source.CompositionTarget.TransformToDevice.M11;
                            double             dpiy   = source.CompositionTarget.TransformToDevice.M22;

                            _windowLocationPane.Left   = topLeftPanePoint.X / dpix;
                            _windowLocationPane.Top    = topLeftPanePoint.Y / dpiy;
                            _windowLocationPane.Width  = SelectedPane.ActualWidth;
                            _windowLocationPane.Height = SelectedPane.ActualHeight;
                            _windowLocationPane.Show();
                        }
                    }

                    if (sender is FloatingToolPaneGroup)
                    {
                        if (_sideLocationPane == null)
                        {
                            _sideLocationPane = new SideLocationPane();
                            _sideLocationPane.AllowsTransparency = true;
                        }

                        Point topLeftRootPoint = IFloatingPaneHost.RootPane.PointToScreen(new Point(0, 0));

                        PresentationSource source = PresentationSource.FromVisual(IFloatingPaneHost.RootPane);
                        double             dpix   = source.CompositionTarget.TransformToDevice.M11;
                        double             dpiy   = source.CompositionTarget.TransformToDevice.M22;

                        _sideLocationPane.Left   = topLeftRootPoint.X / dpix;
                        _sideLocationPane.Top    = topLeftRootPoint.Y / dpiy;
                        _sideLocationPane.Width  = IFloatingPaneHost.RootPane.ActualWidth;
                        _sideLocationPane.Height = IFloatingPaneHost.RootPane.ActualHeight;
                        _sideLocationPane.Show();
                    }
                }
            }

            if (!foundSelectedPane)
            {
                SelectedPane = null;
                _windowLocationPane?.Close();
                _windowLocationPane = null;
            }

            if ((previousPane != null) && (SelectedPane != previousPane))
            {
                previousPane.IsHighlighted = false;
            }

            WindowLocation windowLocation = WindowLocation.None;

            if (sender is FloatingDocumentPaneGroup)
            {
                _sideLocationPane?.Close();
                _sideLocationPane = null;
            }

            if (_sideLocationPane != null)
            {
                windowLocation = _sideLocationPane.TrySelectIndicator(cursorPositionOnScreen);
                switch (windowLocation)
                {
                case WindowLocation.LeftSide:
                case WindowLocation.RightSide:
                case WindowLocation.TopSide:
                case WindowLocation.BottomSide:
                    if ((_insertionIndicatorManager != null) && (_insertionIndicatorManager.ParentGrid != IFloatingPaneHost))
                    {
                        _insertionIndicatorManager.HideInsertionIndicator();
                        _insertionIndicatorManager = null;
                    }
                    if (_insertionIndicatorManager == null)
                    {
                        _insertionIndicatorManager = new InsertionIndicatorManager(IFloatingPaneHost.RootGrid);
                    }
                    _insertionIndicatorManager.ShowInsertionIndicator(windowLocation);
                    return;
                }
            }

            if ((_windowLocationPane != null) && (SelectedPane != null))
            {
                windowLocation = _windowLocationPane.TrySelectIndicator(cursorPositionOnScreen);
                if ((_insertionIndicatorManager != null) && (_insertionIndicatorManager.ParentGrid != SelectedPane))
                {
                    _insertionIndicatorManager.HideInsertionIndicator();
                    _insertionIndicatorManager = null;
                }
                if (_insertionIndicatorManager == null)
                {
                    if (SelectedPane is IFloatingPane)
                    {
                        _insertionIndicatorManager = new InsertionIndicatorManager(((SelectedPane as IFloatingPane).IViewContainer as ViewContainer));
                    }
                    else
                    {
                        _insertionIndicatorManager = new InsertionIndicatorManager(SelectedPane as Grid);
                    }
                }

                _insertionIndicatorManager.ShowInsertionIndicator(windowLocation);
            }
            else
            {
                _insertionIndicatorManager?.HideInsertionIndicator();
                _insertionIndicatorManager = null;
            }
        }