Ejemplo n.º 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);
            }
        }
Ejemplo n.º 2
0
        private void OnReevaluateCapturedWithinAsync()
        {
            if (_captured == null)
            {
                return;
            }

            bool killCapture = false;

            //
            // First, check things like IsEnabled, IsVisible, etc. on a
            // UIElement vs. ContentElement basis.
            //
            UIElement      uiElement;
            ContentElement contentElement;
            UIElement3D    uiElement3D;

            CastInputElement(_captured, out uiElement, out contentElement, out uiElement3D);
            if (uiElement != null)
            {
                killCapture = !uiElement.IsEnabled || !uiElement.IsVisible || !uiElement.IsHitTestVisible;
            }
            else if (contentElement != null)
            {
                killCapture = !contentElement.IsEnabled;
            }
            else if (uiElement3D != null)
            {
                killCapture = !uiElement3D.IsEnabled || !uiElement3D.IsVisible || !uiElement3D.IsHitTestVisible;
            }
            else
            {
                killCapture = true;
            }

            //
            // Second, if we still haven't thought of a reason to kill capture, validate
            // it on a Visual basis for things like still being in the right tree.
            //
            if (killCapture == false)
            {
                DependencyObject containingVisual = InputElement.GetContainingVisual(_captured as DependencyObject);
                killCapture = !ValidateVisualForCapture(containingVisual);
            }

            //
            // Lastly, if we found any reason above, kill capture.
            //
            if (killCapture)
            {
                Capture(null);
            }

            // Refresh AreAnyTouchCapturesWithinProperty so that ReverseInherited flags are updated.
            if ((_capturedWithinTreeState != null) && !_capturedWithinTreeState.IsEmpty)
            {
                UpdateReverseInheritedProperty(/* capture = */ true, _captured, _captured);
            }
        }
Ejemplo n.º 3
0
        private void GetRootTransforms(IInputElement relativeTo, out GeneralTransform elementToRoot, out GeneralTransform rootToElement)
        {
            elementToRoot = rootToElement = null;

            DependencyObject containingVisual = InputElement.GetContainingVisual(relativeTo as DependencyObject);

            if (containingVisual != null)
            {
                PresentationSource relativePresentationSource = PresentationSource.CriticalFromVisual(containingVisual);
                Visual             rootVisual         = (relativePresentationSource != null) ? relativePresentationSource.RootVisual : null;
                Visual             containingVisual2D = VisualTreeHelper.GetContainingVisual2D(containingVisual);
                if ((rootVisual != null) && (containingVisual2D != null))
                {
                    elementToRoot = containingVisual2D.TransformToAncestor(rootVisual);
                    rootToElement = rootVisual.TransformToDescendant(containingVisual2D);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets the transform relative to a particular element in the visual tree.
        /// </summary>
        internal static GeneralTransform GetElementTransform(IInputElement relativeTo)
        {
            GeneralTransform elementTransform = Transform.Identity;
            DependencyObject doRelativeTo     = relativeTo as DependencyObject;

            if (doRelativeTo != null)
            {
                Visual visualFirstAncestor = VisualTreeHelper.GetContainingVisual2D(InputElement.GetContainingVisual(doRelativeTo));
                Visual visualRoot          = VisualTreeHelper.GetContainingVisual2D(InputElement.GetRootVisual(doRelativeTo));

                GeneralTransform g = visualRoot.TransformToDescendant(visualFirstAncestor);
                if (g != null)
                {
                    elementTransform = g;
                }
            }

            return(elementTransform);
        }
Ejemplo n.º 5
0
        private PresentationSource GetSourceForElement(IInputElement element)
        {
            PresentationSource source    = null;
            DependencyObject   elementDO = element as DependencyObject;

            // Use internal helpers to try to find the source of the element.
            // Because IInputElements can move around without notification we need to
            // look up the source every time.
            if (elementDO != null)
            {
                DependencyObject containingVisual = InputElement.GetContainingVisual(elementDO);

                if (containingVisual != null)
                {
                    source = PresentationSource.CriticalFromVisual(containingVisual);
                }
            }

            // NOTE: source can be null but IsTargetable(element) == true if the
            // element is in an orphaned tree but the tree has not yet been garbage collected.
            return(source);
        }
Ejemplo n.º 6
0
 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);
 }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        internal static Point TranslatePoint(Point pt, DependencyObject from, DependencyObject to, out bool translated)
        {
            translated = false;

            Point ptTranslated = pt;

            // Get the containing and root visuals we are coming from.
            DependencyObject vFromAsDO = InputElement.GetContainingVisual(from);
            Visual           rootFrom  = InputElement.GetRootVisual(from) as Visual;

            Visual vFrom = vFromAsDO as Visual;

            if (vFromAsDO != null && vFrom == null)
            {
                // must be a Visual3D - get it's 2D visual parent
                vFrom = VisualTreeHelper.GetContainingVisual2D(vFromAsDO);
            }

            if (vFrom != null && rootFrom != null)
            {
                GeneralTransform gUp;
                Matrix           mUp;

                bool isUpSimple = false;
                isUpSimple = vFrom.TrySimpleTransformToAncestor(rootFrom,
                                                                false, /* do not apply inverse */
                                                                out gUp,
                                                                out mUp);
                if (isUpSimple)
                {
                    ptTranslated = mUp.Transform(ptTranslated);
                }
                else if (gUp.TryTransform(ptTranslated, out ptTranslated) == false)
                {
                    // Error.  Out parameter has been set false.
                    return(new Point());
                }

                // If no element was specified to translate to, we leave the coordinates
                // translated to the root.
                if (to != null)
                {
                    // Get the containing and root visuals we are going to.
                    DependencyObject vTo    = InputElement.GetContainingVisual(to);
                    Visual           rootTo = InputElement.GetRootVisual(to) as Visual;

                    if (vTo != null && rootTo != null)
                    {
                        // If both are under the same root visual, we can easily translate the point
                        // between them by translating up to the root, and then back down.
                        //
                        // However, if both are under different roots, we can only translate
                        // between them if we know how to relate the two root visuals.  Currently
                        // we only know how to do that if both roots are sourced in HwndSources.
                        if (rootFrom != rootTo)
                        {
                            HwndSource sourceFrom = PresentationSource.CriticalFromVisual(rootFrom) as HwndSource;
                            HwndSource sourceTo   = PresentationSource.CriticalFromVisual(rootTo) as HwndSource;


                            if (sourceFrom != null && sourceFrom.CriticalHandle != IntPtr.Zero && sourceFrom.CompositionTarget != null &&
                                sourceTo != null && sourceTo.CriticalHandle != IntPtr.Zero && sourceTo.CompositionTarget != null)
                            {
                                // Translate the point into client coordinates.
                                ptTranslated = PointUtil.RootToClient(ptTranslated, sourceFrom);

                                // Translate the point into screen coordinates.
                                Point ptScreen = PointUtil.ClientToScreen(ptTranslated, sourceFrom);

                                // Translate the point back the the client coordinates of the To window.
                                ptTranslated = PointUtil.ScreenToClient(ptScreen, sourceTo);

                                // Translate the point back to the root element.
                                ptTranslated = PointUtil.ClientToRoot(ptTranslated, sourceTo);
                            }
                            else
                            {
                                // Error.  Out parameter has been set false.
                                return(new Point());
                            }
                        }

                        // Translate the point from the root to the visual.
                        GeneralTransform gDown;
                        Matrix           mDown;

                        Visual vToAsVisual = vTo as Visual;
                        if (vToAsVisual == null)
                        {
                            // must be a Visual3D
                            vToAsVisual = VisualTreeHelper.GetContainingVisual2D(vTo);
                        }

                        bool isDownSimple = vToAsVisual.TrySimpleTransformToAncestor(rootTo,
                                                                                     true, /* apply inverse */
                                                                                     out gDown,
                                                                                     out mDown);

                        if (isDownSimple)
                        {
                            ptTranslated = mDown.Transform(ptTranslated);
                        }
                        else if (gDown != null)
                        {
                            if (gDown.TryTransform(ptTranslated, out ptTranslated) == false)
                            {
                                // Error.  Out parameter has been set false.
                                return(new Point());
                            }
                        }
                        else
                        {
                            // Error.  Out parameter has been set false.
                            return(new Point());
                        }
                    }
                    else
                    {
                        // Error.  Out parameter has been set false.
                        return(new Point());
                    }
                }
            }
            else
            {
                // Error.  Out parameter has been set false.
                return(new Point());
            }

            translated = true;
            return(ptTranslated);
        }