예제 #1
0
        private void Focus(DependencyObject focus, bool askOld, bool askNew, bool forceToNullIfFailed)
        {
            // Make sure that the element is valid for receiving focus.
            bool isValid = true;

            if (focus != null)
            {
                isValid = Keyboard.IsFocusable(focus);

                if (!isValid && forceToNullIfFailed)
                {
                    focus   = null;
                    isValid = true;
                }
            }

            if (isValid)
            {
                // Get the keyboard input provider that provides input for the active source.
                IKeyboardInputProvider keyboardInputProvider = null;
                DependencyObject       containingVisual      = InputElement.GetContainingVisual(focus);
                if (containingVisual != null)
                {
                    PresentationSource source = PresentationSource.CriticalFromVisual(containingVisual);
                    if (source != null)
                    {
                        keyboardInputProvider = (IKeyboardInputProvider)source.GetInputProvider(typeof(KeyboardDevice));
                    }
                }

                // Start the focus-change operation.
                TryChangeFocus(focus, keyboardInputProvider, askOld, askNew, forceToNullIfFailed);
            }
        }
예제 #2
0
파일: Mouse.cs 프로젝트: yk2012985/wpf
 public static int GetIntermediatePoints(IInputElement relativeTo, Point[] points)
 {
     // Security Mitigation: do not give out input state if the device is not active.
     if (Mouse.PrimaryDevice.IsActive)
     {
         if (relativeTo != null)
         {
             PresentationSource inputSource = PresentationSource.FromDependencyObject(InputElement.GetContainingVisual(relativeTo as DependencyObject));
             if (inputSource != null)
             {
                 IMouseInputProvider mouseInputProvider = inputSource.GetInputProvider(typeof(MouseDevice)) as IMouseInputProvider;
                 if (null != mouseInputProvider)
                 {
                     return(mouseInputProvider.GetIntermediatePoints(relativeTo, points));
                 }
             }
         }
     }
     return(-1);
 }
예제 #3
0
        /// <summary>
        ///     Determines if we can remain focused on the element we think has focus
        /// </summary>
        /// <remarks>
        ///     Invoked asynchronously by ReevaluateFocusAsync.
        ///     Confirms that the element we think has focus is:
        ///         - still enabled
        ///         - still visible
        ///         - still in the tree
        /// </remarks>
        private object ReevaluateFocusCallback(object arg)
        {
            _reevaluateFocusOperation = null;

            if (_focus == null)
            {
                return(null);
            }

            //
            // Reevaluate the eligability of the focused element to actually
            // have focus.  If that element is no longer focusable, then search
            // for an ancestor that is.
            //
            DependencyObject element = _focus;

            while (element != null)
            {
                if (Keyboard.IsFocusable(element))
                {
                    break;
                }

                // Walk the current tree structure.
                element = DeferredElementTreeState.GetCoreParent(element, null);
            }

            // Get the PresentationSource that contains the element to be focused.
            PresentationSource presentationSource = null;
            DependencyObject   visualContainer    = InputElement.GetContainingVisual(element);

            if (visualContainer != null)
            {
                presentationSource = PresentationSource.CriticalFromVisual(visualContainer);
            }

            // The default action is to reset focus to the root element
            // of the active presentation source.
            bool             moveFocus   = true;
            DependencyObject moveFocusTo = null;

            if (presentationSource != null)
            {
                IKeyboardInputProvider keyboardProvider = presentationSource.GetInputProvider(typeof(KeyboardDevice)) as IKeyboardInputProvider;
                if (keyboardProvider != null)
                {
                    // Confirm with the keyboard provider for this
                    // presentation source that it has acquired focus.
                    if (keyboardProvider.AcquireFocus(true))
                    {
                        if (element == _focus)
                        {
                            // The focus element is still good.
                            moveFocus = false;
                        }
                        else
                        {
                            // The focus element is no longer focusable, but we found
                            // an ancestor that is, so move focus there.
                            moveFocus   = true;
                            moveFocusTo = element;
                        }
                    }
                }
            }

            if (moveFocus)
            {
                if (moveFocusTo == null && _activeSource != null)
                {
                    moveFocusTo = _activeSource.Value.RootVisual as DependencyObject;
                }


                Focus(moveFocusTo, /*askOld=*/ false, /*askNew=*/ true, /*forceToNullIfFailed=*/ true);
            }
            else
            {
                // Refresh FocusWithinProperty so that ReverseInherited Flags are updated.
                //
                // We only need to do this if there is any information about the old
                // tree structure.
                if (_focusTreeState != null && !_focusTreeState.IsEmpty)
                {
                    UIElement.FocusWithinProperty.OnOriginValueChanged(_focus, _focus, ref _focusTreeState);
                }
            }

            return(null);
        }