GetNavigationSourceRectangle() protected method

protected GetNavigationSourceRectangle ( ) : Rectangle
return System.Drawing.Rectangle
Exemplo n.º 1
0
        /// <summary>
        /// If a key down event is unhandled, this will attempt to handle it by navigating or scrolling.
        /// </summary>
        /// <param name="control"></param>
        /// <param name="e"></param>
        static void OnWindowlessUnhandledKeyDown(Control control, KeyEventArgs e)
        {
            if (e.Handled)
            {
                return;
            }

            WindowlessControlHost host = control as WindowlessControlHost;

            switch (e.KeyCode)
            {
            case Keys.F19:
            case Keys.F20:
            case Keys.Up:
            case Keys.Down:
            case Keys.Left:
            case Keys.Right:
            {
                // attempt to navigate first
                Point     p = WindowlessPointToForm(control, Point.Empty);
                Rectangle sourceRect;
                if (host != null)
                {
                    sourceRect = host.GetNavigationSourceRectangle();
                }
                else
                {
                    sourceRect = new Rectangle(0, 0, control.Width, control.Height);
                }
                Rectangle rect = sourceRect;
                rect.X += p.X;
                rect.Y += p.Y;

                Control bestControl     = null;
                Control topLevelControl = FormlessTopLevelControl(control);

                if (e.KeyCode == Keys.F19 || e.KeyCode == Keys.F20)
                {
                    Control previous = null;
                    Control next     = null;
                    bool    found    = false;
                    RecurseControls(topLevelControl, (current) =>
                        {
                            if (current == control)
                            {
                                found = true;
                                return(true);
                            }

                            if (!IsFocusable(current))
                            {
                                return(true);
                            }
                            if (!found)
                            {
                                previous = current;
                            }
                            next = current;
                            return(!found);
                        },
                                    null);

                    if (e.KeyCode == Keys.F19)
                    {
                        bestControl = previous;
                    }
                    else
                    {
                        bestControl = next;
                    }
                    //WindowlessNavigateEventArgs ne = new WindowlessNavigateEventArgs(control,
                }
                else
                {
                    ulong bestScore;
                    bestControl = GetBestNavigationHost(rect, e.KeyCode, topLevelControl, control, out bestScore);
                }
                WindowlessNavigateEventArgs ne = new WindowlessNavigateEventArgs(control, sourceRect, e.KeyCode, bestControl);

                Control parent = control;
                while (parent != null)
                {
                    WindowlessControlHost parentHost = parent as WindowlessControlHost;
                    if (parentHost != null)
                    {
                        parentHost.OnWindowlessNavigate(control, ne);
                    }
                    parent = parent.Parent;
                }

                if (ne.Destination != null && ne.Destination != control)
                {
                    WindowlessControlHost destHost = ne.Destination as WindowlessControlHost;
                    if (destHost != null)
                    {
                        WindowlessControlHost reportChain = destHost;
                        while (reportChain != null)
                        {
                            reportChain.OnWindowlessNavigatingTo(destHost, ne);
                            reportChain = reportChain.Parent as WindowlessControlHost;
                        }
                    }
                    if (ne.Destination != null)
                    {
                        e.Handled = true;
                        // focus causes problems if this is not actually on a form
                        if (topLevelControl is Form)
                        {
                            ne.Destination.Focus();
                        }
                    }
                }

                const int scrollSpeed = 8;

                // now attempt to scroll
                parent = control;
                while (!e.Handled && parent != null)
                {
                    ScrollableControl scrollControl = parent as ScrollableControl;
                    if (scrollControl != null)
                    {
                        // see if we want to scroll this window
                        if (scrollControl.AutoScroll && !e.Handled)
                        {
                            int left   = Int32.MaxValue;
                            int top    = Int32.MaxValue;
                            int right  = Int32.MinValue;
                            int bottom = Int32.MinValue;

                            foreach (Control childControl in scrollControl.Controls)
                            {
                                left   = Math.Min(left, childControl.Left);
                                top    = Math.Min(top, childControl.Top);
                                bottom = Math.Max(bottom, childControl.Bottom);
                                right  = Math.Max(right, childControl.Right);
                            }

                            Point oldPos = scrollControl.AutoScrollPosition;
                            if (e.KeyCode == Keys.Down && bottom > scrollControl.ClientSize.Height)
                            {
                                scrollControl.AutoScrollPosition = new Point(Math.Abs(scrollControl.AutoScrollPosition.X), Math.Min(Math.Abs(scrollControl.AutoScrollPosition.Y) + scrollSpeed, Math.Abs(scrollControl.AutoScrollPosition.Y) + (bottom - scrollControl.ClientSize.Height)));
                            }
                            if (e.KeyCode == Keys.Up && scrollControl.AutoScrollPosition.Y < 0)
                            {
                                scrollControl.AutoScrollPosition = new Point(Math.Abs(scrollControl.AutoScrollPosition.X), Math.Max(0, Math.Abs(scrollControl.AutoScrollPosition.Y) - scrollSpeed));
                            }
                            if (e.KeyCode == Keys.Right && right > scrollControl.ClientSize.Width)
                            {
                                scrollControl.AutoScrollPosition = new Point(Math.Min(Math.Abs(scrollControl.AutoScrollPosition.X) + scrollSpeed, Math.Abs(scrollControl.AutoScrollPosition.X) + (right - scrollControl.ClientSize.Width)), Math.Abs(scrollControl.AutoScrollPosition.Y));
                            }
                            if (e.KeyCode == Keys.Left && scrollControl.AutoScrollPosition.X < 0)
                            {
                                scrollControl.AutoScrollPosition = new Point(Math.Max(0, Math.Abs(scrollControl.AutoScrollPosition.X) - scrollSpeed), Math.Abs(scrollControl.AutoScrollPosition.Y));
                            }
                            e.Handled = scrollControl.AutoScrollPosition != oldPos;
                        }
                    }
                    parent = parent.Parent;
                }
            }
            break;
            }
        }