override protected Point GetClickablePointCore()
        {
            Point pt = new Point(double.NaN, double.NaN);

            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(_owner);

            // If there's no source, the element is not visible, return (double.NaN, double.NaN) point
            if (presentationSource == null)
            {
                return(pt);
            }

            HwndSource hwndSource = presentationSource as HwndSource;

            // If the source isn't an HwnSource, there's not much we can do, return (double.NaN, double.NaN) point
            if (hwndSource == null)
            {
                return(pt);
            }

            Rect rectElement = new Rect(new Point(0, 0), _owner.RenderSize);
            Rect rectRoot    = PointUtil.ElementToRoot(rectElement, _owner, presentationSource);
            Rect rectClient  = PointUtil.RootToClient(rectRoot, presentationSource);
            Rect rectScreen  = PointUtil.ClientToScreen(rectClient, hwndSource);

            pt = new Point(rectScreen.Left + rectScreen.Width * 0.5, rectScreen.Top + rectScreen.Height * 0.5);

            return(pt);
        }
Esempio n. 2
0
        private bool OnEnterMenuMode(object sender, EventArgs e)
        {
            // Don't enter menu mode if someone has capture
            if (Mouse.Captured != null)
            {
                return(false);
            }

            // Need to check that ALT/F10 happened in our source.
            PresentationSource source   = sender as PresentationSource;
            PresentationSource mySource = null;

            mySource = PresentationSource.CriticalFromVisual(this);
            if (source == mySource)
            {
                // Give focus to the first possible element in the ItemsControl
                for (int i = 0; i < Items.Count; i++)
                {
                    MenuItem menuItem = ItemContainerGenerator.ContainerFromIndex(i) as MenuItem;

                    if (menuItem != null && !(Items[i] is Separator))
                    {
                        if (menuItem.Focus())
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Esempio n. 3
0
        private static void GetClippedPositionOffsets(TextEditor This, ITextPointer position, LogicalDirection direction, out double horizontalOffset, out double verticalOffset)
        {
            Rect characterRect = position.GetCharacterRect(direction);

            horizontalOffset = characterRect.X;
            verticalOffset   = characterRect.Y + characterRect.Height;
            FrameworkElement frameworkElement = This.TextView.RenderScope as FrameworkElement;

            if (frameworkElement != null)
            {
                GeneralTransform generalTransform = frameworkElement.TransformToAncestor(This.UiScope);
                if (generalTransform != null)
                {
                    TextEditorContextMenu.ClipToElement(frameworkElement, generalTransform, ref horizontalOffset, ref verticalOffset);
                }
            }
            for (Visual visual = This.UiScope; visual != null; visual = (VisualTreeHelper.GetParent(visual) as Visual))
            {
                frameworkElement = (visual as FrameworkElement);
                if (frameworkElement != null)
                {
                    GeneralTransform generalTransform2 = visual.TransformToDescendant(This.UiScope);
                    if (generalTransform2 != null)
                    {
                        TextEditorContextMenu.ClipToElement(frameworkElement, generalTransform2, ref horizontalOffset, ref verticalOffset);
                    }
                }
            }
            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(This.UiScope);
            IWin32Window       win32Window        = presentationSource as IWin32Window;

            if (win32Window != null)
            {
                IntPtr handle = IntPtr.Zero;
                new UIPermission(UIPermissionWindow.AllWindows).Assert();
                try
                {
                    handle = win32Window.Handle;
                }
                finally
                {
                    CodeAccessPermission.RevertAssert();
                }
                NativeMethods.RECT rect = new NativeMethods.RECT(0, 0, 0, 0);
                SafeNativeMethods.GetClientRect(new HandleRef(null, handle), ref rect);
                Point             point             = new Point((double)rect.left, (double)rect.top);
                Point             point2            = new Point((double)rect.right, (double)rect.bottom);
                CompositionTarget compositionTarget = presentationSource.CompositionTarget;
                point  = compositionTarget.TransformFromDevice.Transform(point);
                point2 = compositionTarget.TransformFromDevice.Transform(point2);
                GeneralTransform generalTransform3 = compositionTarget.RootVisual.TransformToDescendant(This.UiScope);
                if (generalTransform3 != null)
                {
                    generalTransform3.TryTransform(point, out point);
                    generalTransform3.TryTransform(point2, out point2);
                    horizontalOffset = TextEditorContextMenu.ClipToBounds(point.X, horizontalOffset, point2.X);
                    verticalOffset   = TextEditorContextMenu.ClipToBounds(point.Y, verticalOffset, point2.Y);
                }
            }
        }
Esempio n. 4
0
        private void SetContainer(IInputElement newContainer)
        {
            // unsubscribe from LayoutUpdated
            UnsubscribeFromLayoutUpdated();

            // clear cached values
            _containerPivotPoint = new Point();
            _containerSize       = new Size();
            _root = null;

            // remember the new container
            _currentContainer = newContainer;

            if (newContainer != null)
            {
                // get the new root
                PresentationSource presentationSource = PresentationSource.CriticalFromVisual((Visual)newContainer);
                if (presentationSource != null)
                {
                    _root = presentationSource.RootVisual as UIElement;
                }

                // subscribe to LayoutUpdated
                if (_containerLayoutUpdated != null)
                {
                    SubscribeToLayoutUpdated();
                }
            }
        }
        override protected Rect GetBoundingRectangleCore()
        {
            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(_owner);

            // If there's no source, the element is not visible, return empty rect
            if (presentationSource == null)
            {
                return(Rect.Empty);
            }

            HwndSource hwndSource = presentationSource as HwndSource;

            // If the source isn't an HwnSource, there's not much we can do, return empty rect
            if (hwndSource == null)
            {
                return(Rect.Empty);
            }

            Rect rectElement = new Rect(new Point(0, 0), _owner.RenderSize);
            Rect rectRoot    = PointUtil.ElementToRoot(rectElement, _owner, presentationSource);
            Rect rectClient  = PointUtil.RootToClient(rectRoot, presentationSource);
            Rect rectScreen  = PointUtil.ClientToScreen(rectClient, hwndSource);

            return(rectScreen);
        }
        internal override Rect GetVisibleBoundingRectCore()
        {
            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(_owner);

            // If there's no source, the element is not visible, return empty rect
            if (presentationSource == null)
            {
                return(Rect.Empty);
            }

            HwndSource hwndSource = presentationSource as HwndSource;

            // If the source isn't an HwnSource, there's not much we can do, return empty rect
            if (hwndSource == null)
            {
                return(Rect.Empty);
            }

            Rect rectElement = CalculateVisibleBoundingRect(_owner);
            Rect rectRoot    = PointUtil.ElementToRoot(rectElement, _owner, presentationSource);
            Rect rectClient  = PointUtil.RootToClient(rectRoot, presentationSource);
            Rect rectScreen  = PointUtil.ClientToScreen(rectClient, hwndSource);

            return(rectScreen);
        }
Esempio n. 7
0
 protected unsafe virtual IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
 {
     this.DemandIfUntrusted();
     if (msg != 61)
     {
         if (msg != 70)
         {
             if (msg == 130)
             {
                 this._hwnd = new HandleRef(null, IntPtr.Zero);
             }
         }
         else
         {
             PresentationSource presentationSource = PresentationSource.CriticalFromVisual(this, false);
             if (presentationSource != null)
             {
                 NativeMethods.RECT       rect = this.CalculateAssignedRC(presentationSource);
                 NativeMethods.WINDOWPOS *ptr  = (NativeMethods.WINDOWPOS *)((void *)lParam);
                 ptr->cx     = rect.right - rect.left;
                 ptr->cy     = rect.bottom - rect.top;
                 ptr->flags &= -2;
                 ptr->x      = rect.left;
                 ptr->y      = rect.top;
                 ptr->flags &= -3;
                 ptr->flags |= 256;
             }
         }
         return(IntPtr.Zero);
     }
     handled = true;
     return(this.OnWmGetObject(wParam, lParam));
 }
Esempio n. 8
0
        /// <summary>
        /// Return the property value of FocusedElement property. The return value is validated
        /// to be in the subtree of element. If FocusedElement element is not a descendant of element this method return null
        /// </summary>
        /// <param name="element"></param>
        /// <param name="validate"></param>
        /// <returns></returns>
        internal static IInputElement GetFocusedElement(DependencyObject element, bool validate)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            DependencyObject focusedElement = (DependencyObject)element.GetValue(FocusedElementProperty);

            // Validate FocusedElement wrt to its FocusScope. If the two do not belong to the same PresentationSource
            // then sever this link between them. The classic scenario for this is when an element with logical focus is
            // dragged out into a floating widow. We want to prevent the MainWindow (focus scope) to point to the
            // element in the floating window as its logical FocusedElement.

            if (validate && focusedElement != null)
            {
                DependencyObject focusScope = element;

                if (PresentationSource.CriticalFromVisual(focusScope) != PresentationSource.CriticalFromVisual(focusedElement))
                {
                    SetFocusedElement(focusScope, null);
                    focusedElement = null;
                }
            }

            return((IInputElement)focusedElement);
        }
Esempio n. 9
0
        public void UpdateWindowPos()
        {
            if (this._isDisposed)
            {
                return;
            }
            PresentationSource presentationSource = null;
            CompositionTarget  compositionTarget  = null;

            if (this.CriticalHandle != IntPtr.Zero && base.IsVisible)
            {
                presentationSource = PresentationSource.CriticalFromVisual(this, false);
                if (presentationSource != null)
                {
                    compositionTarget = presentationSource.CompositionTarget;
                }
            }
            if (compositionTarget != null && compositionTarget.RootVisual != null)
            {
                NativeMethods.RECT rc = this.CalculateAssignedRC(presentationSource);
                Rect rcBoundingBox    = PointUtil.ToRect(rc);
                this.OnWindowPositionChanged(rcBoundingBox);
                UnsafeNativeMethods.ShowWindowAsync(this._hwnd, 5);
                return;
            }
            UnsafeNativeMethods.ShowWindowAsync(this._hwnd, 0);
        }
Esempio n. 10
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);
            }
        }
Esempio n. 11
0
        /// <summary>
        ///     This is the method that responds to the TextInput event.
        /// </summary>
        /// <param name="e">Event arguments</param>
        protected override void OnTextInput(TextCompositionEventArgs e)
        {
            base.OnTextInput(e);
            if (e.Handled)
            {
                return;
            }

            // We don't use win32 menu's, so we need to emulate the win32
            // behavior for hitting Space while in menu mode.  Alt+Space
            // will be handled as a SysKey by the DefaultWindowProc, but
            // Alt, then Space needs to be special cased here because we prevent win32.
            // from entering menu mode.  In WPF the equiv. of win32 menu mode is having
            // a main menu with focus and no menu items opened.
            if (e.UserInitiated &&
                e.Text == " " &&
                IsMainMenu &&
                (CurrentSelection == null || !CurrentSelection.IsSubmenuOpen))
            {
                // We need to exit menu mode because it holds capture and prevents
                // the system menu from showing.
                IsMenuMode = false;
                System.Windows.Interop.HwndSource source = PresentationSource.CriticalFromVisual(this) as System.Windows.Interop.HwndSource;
                if (source != null)
                {
                    source.ShowSystemMenu();
                    e.Handled = true;
                }
            }
        }
Esempio n. 12
0
        protected override Rect GetBoundingRectangleCore()
        {
            TextElement textElement = (TextElement)base.Owner;
            ITextView   textView    = textElement.TextContainer.TextView;

            if (textView == null || !textView.IsValid)
            {
                return(Rect.Empty);
            }
            Geometry tightBoundingGeometryFromTextPositions = textView.GetTightBoundingGeometryFromTextPositions(textElement.ContentStart, textElement.ContentEnd);

            if (tightBoundingGeometryFromTextPositions == null)
            {
                return(Rect.Empty);
            }
            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(textView.RenderScope);

            if (presentationSource == null)
            {
                return(Rect.Empty);
            }
            HwndSource hwndSource = presentationSource as HwndSource;

            if (hwndSource == null)
            {
                return(Rect.Empty);
            }
            Rect bounds     = tightBoundingGeometryFromTextPositions.Bounds;
            Rect rectRoot   = PointUtil.ElementToRoot(bounds, textView.RenderScope, presentationSource);
            Rect rectClient = PointUtil.RootToClient(rectRoot, presentationSource);

            return(PointUtil.ClientToScreen(rectClient, hwndSource));
        }
Esempio n. 13
0
        private bool ComputeBoundingRectangle(out Rect rect)
        {
            rect = Rect.Empty;

            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(_owner);

            // If there's no source, the element is not visible, return empty rect
            if (presentationSource == null)
            {
                return(false);
            }

            HwndSource hwndSource = presentationSource as HwndSource;

            // If the source isn't an HwndSource, there's not much we can do, return empty rect
            if (hwndSource == null)
            {
                return(false);
            }

            Rect rectElement = _owner.Visual2DContentBounds;
            // we use VisualTreeHelper.GetContainingVisual2D to transform from the containing Viewport3DVisual
            Rect rectRoot   = PointUtil.ElementToRoot(rectElement, VisualTreeHelper.GetContainingVisual2D(_owner), presentationSource);
            Rect rectClient = PointUtil.RootToClient(rectRoot, presentationSource);

            rect = PointUtil.ClientToScreen(rectClient, hwndSource);

            return(true);
        }
        /// <summary>
        /// is this visual connected to presentation source ?
        /// i.e. is it "renderable" ?
        ///</summary>
        internal static bool IsConnectedToPresentationSource(Visual visual)
        {
            bool isConnected = false;

            isConnected = PresentationSource.CriticalFromVisual(visual) != null;

            return(isConnected);
        }
Esempio n. 15
0
 private void PushMenuMode(bool isAcquireFocusMenuMode)
 {
     Debug.Assert(_pushedMenuMode == null);
     _pushedMenuMode = PresentationSource.CriticalFromVisual(this);
     Debug.Assert(_pushedMenuMode != null);
     IsAcquireFocusMenuMode = isAcquireFocusMenuMode;
     InputManager.UnsecureCurrent.PushMenuMode(_pushedMenuMode);
 }
Esempio n. 16
0
        internal static bool IsFocusable(DependencyObject element)
        {
            // CODE



            if (element == null)
            {
                return(false);
            }

            UIElement uie = element as UIElement;

            if (uie != null)
            {
                if (uie.IsVisible == false)
                {
                    return(false);
                }
            }

            if ((bool)element.GetValue(UIElement.IsEnabledProperty) == false)
            {
                return(false);
            }

            // CODE



            bool hasModifiers = false;
            BaseValueSourceInternal valueSource = element.GetValueSource(UIElement.FocusableProperty, null, out hasModifiers);
            bool focusable = (bool)element.GetValue(UIElement.FocusableProperty);

            if (!focusable && valueSource == BaseValueSourceInternal.Default && !hasModifiers)
            {
                // The Focusable property was not explicitly set to anything.
                // The default value is generally false, but true in a few cases.

                if (FocusManager.GetIsFocusScope(element))
                {
                    // Focus scopes are considered focusable, even if
                    // the Focusable property is false.
                    return(true);
                }
                else if (uie != null && uie.InternalVisualParent == null)
                {
                    PresentationSource presentationSource = PresentationSource.CriticalFromVisual(uie);
                    if (presentationSource != null)
                    {
                        // A UIElements that is the root of a PresentationSource is considered focusable.
                        return(true);
                    }
                }
            }

            return(focusable);
        }
Esempio n. 17
0
        private void LoadedHandler(object sender, RoutedEventArgs args)
        {
            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(this);

            if (presentationSource != null && presentationSource.RootVisual is PopupRoot)
            {
                throw new InvalidOperationException(SR.Get("CannotBeInsidePopup"));
            }
        }
Esempio n. 18
0
        internal static bool IsFocusable(DependencyObject element)
        {
            // This should really be its own property, but it is hard to do efficiently.
            if (element == null)
            {
                return(false);
            }

            UIElement uie = element as UIElement;

            if (uie != null)
            {
                if (uie.IsVisible == false)
                {
                    return(false);
                }
            }

            if ((bool)element.GetValue(UIElement.IsEnabledProperty) == false)
            {
                return(false);
            }

            // There are too many conflicting desires for whether or not
            // an element is focusable.  We need to differentiate between
            // a false default value, and the user specifying false
            // explicitly.
            //
            bool hasModifiers = false;
            BaseValueSourceInternal valueSource = element.GetValueSource(UIElement.FocusableProperty, null, out hasModifiers);
            bool focusable = (bool)element.GetValue(UIElement.FocusableProperty);

            if (!focusable && valueSource == BaseValueSourceInternal.Default && !hasModifiers)
            {
                // The Focusable property was not explicitly set to anything.
                // The default value is generally false, but true in a few cases.

                if (FocusManager.GetIsFocusScope(element))
                {
                    // Focus scopes are considered focusable, even if
                    // the Focusable property is false.
                    return(true);
                }
                else if (uie != null && uie.InternalVisualParent == null)
                {
                    PresentationSource presentationSource = PresentationSource.CriticalFromVisual(uie);
                    if (presentationSource != null)
                    {
                        // A UIElements that is the root of a PresentationSource is considered focusable.
                        return(true);
                    }
                }
            }

            return(focusable);
        }
Esempio n. 19
0
        private ManipulationDevice(UIElement element) : base()
        {
            _target       = element;
            _activeSource = PresentationSource.CriticalFromVisual(element);

            _inputManager = InputManager.UnsecureCurrent;
            _inputManager.PostProcessInput += new ProcessInputEventHandler(PostProcessInput);

            _manipulationLogic = new ManipulationLogic(this);
        }
Esempio n. 20
0
        private bool ValidateVisualForCapture(DependencyObject visual)
        {
            if (visual == null)
            {
                return(false);
            }

            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(visual);

            return((presentationSource != null) && (presentationSource == _activeSource));
        }
        internal override void UpdateState(UIElement element)
        {
            bool unhookPenContexts = true;

            // Disable processing of the queue during blocking operations to prevent unrelated reentrancy
            // which a call to Lock() can cause.
            using (element.Dispatcher.DisableProcessing())
            {
                // See if we should be enabled
                if (element.IsVisible && element.IsEnabled && element.IsHitTestVisible)
                {
                    PresentationSource presentationSource = PresentationSource.CriticalFromVisual(element as Visual);

                    if (presentationSource != null)
                    {
                        unhookPenContexts = false;

                        // Are we currently hooked up?  If not then hook up.
                        if (_penContexts == null)
                        {
                            InputManager inputManager = (InputManager)element.Dispatcher.InputManager;
                            PenContexts  penContexts  = StylusLogic.GetCurrentStylusLogicAs <WispLogic>().GetPenContextsFromHwnd(presentationSource);

                            // _penContexts must be non null or don't do anything.
                            if (penContexts != null)
                            {
                                _penContexts = penContexts;

                                lock (penContexts.SyncRoot)
                                {
                                    penContexts.AddStylusPlugInCollection(Wrapper);

                                    foreach (StylusPlugIn spi in Wrapper)
                                    {
                                        spi.InvalidateIsActiveForInput(); // Uses _penContexts being set to determine active state.
                                    }
                                    // NTRAID:WINDOWSOS#1677277-2006/06/05-WAYNEZEN,
                                    // Normally the Rect will be updated when we receive the LayoutUpdate.
                                    // However there could be a race condition which the LayoutUpdate gets received
                                    // before the properties like IsVisible being set.
                                    // So we should always force to call OnLayoutUpdated whenever the input is active.
                                    Wrapper.OnLayoutUpdated(this.Wrapper, EventArgs.Empty);
                                }
                            }
                        }
                    }
                }

                if (unhookPenContexts)
                {
                    Unhook();
                }
            }
        }
        internal static void ShowFindUnsuccessfulMessage(FindToolBar findToolBar)
        {
            string text = findToolBar.SearchUp ? SR.Get("DocumentViewerSearchUpCompleteLabel") : SR.Get("DocumentViewerSearchDownCompleteLabel");

            text = string.Format(CultureInfo.CurrentCulture, text, new object[]
            {
                findToolBar.SearchText
            });
            HwndSource hwndSource = PresentationSource.CriticalFromVisual(findToolBar) as HwndSource;
            IntPtr     parentHwnd = (hwndSource != null) ? hwndSource.CriticalHandle : IntPtr.Zero;

            SecurityHelper.ShowMessageBoxHelper(parentHwnd, text, SR.Get("DocumentViewerSearchCompleteTitle"), MessageBoxButton.OK, MessageBoxImage.Asterisk);
        }
Esempio n. 23
0
        private Point ClientToScreen(Point point, Visual visual)
        {
            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(visual);

            if (presentationSource != null)
            {
                GeneralTransform transform = visual.TransformToAncestor(presentationSource.RootVisual);
                if (transform != null)
                {
                    point = transform.Transform(point);
                }
            }
            return(PointUtil.ClientToScreen(point, presentationSource));
        }
Esempio n. 24
0
        //----------------------------------------------
        //
        // Private Methods
        //
        //----------------------------------------------

        #region Private Methods

        private void LoadedHandler(object sender, RoutedEventArgs args)
        {
            PresentationSource pSource = PresentationSource.CriticalFromVisual(this);

            // Note that we cannot assert this condition here. The reason is that this element might have
            // been disconnected from the tree through one of its parents even while it waited for the
            // pending Loaded event to fire. More details for this scenario can be found in the
            // Windows OS Bug#1981485.
            // Invariant.Assert(pSource != null, "Loaded has fired. PresentationSource shouldn't be null");

            if (pSource != null && pSource.RootVisual is PopupRoot)
            {
                throw new InvalidOperationException(SR.Get(SRID.CannotBeInsidePopup));
            }
        }
Esempio n. 25
0
            /// <summary>
            /// Call Win32 SetForegroundWindow to set the drop target as the foreground window.
            /// </summary>
            private void Win32SetForegroundWindow()
            {
                PresentationSource source = null;
                IntPtr             hwnd   = IntPtr.Zero;

                source = PresentationSource.CriticalFromVisual(_textEditor.UiScope);
                if (source != null)
                {
                    hwnd = (source as IWin32Window).Handle;
                }

                if (hwnd != IntPtr.Zero)
                {
                    UnsafeNativeMethods.SetForegroundWindow(new HandleRef(null, hwnd));
                }
            }
        internal static Point ClientToScreen(UIElement relativeTo, Point point)
        {
            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(relativeTo);

            if (presentationSource == null)
            {
                return(new Point(double.NaN, double.NaN));
            }
            GeneralTransform generalTransform = relativeTo.TransformToAncestor(presentationSource.RootVisual);
            Point            point2;

            generalTransform.TryTransform(point, out point2);
            Point pointClient = PointUtil.RootToClient(point2, presentationSource);

            return(PointUtil.ClientToScreen(pointClient, presentationSource));
        }
        protected override Rect GetBoundingRectangleCore()
        {
            UIElement uielement;
            Rect      rect = this.CalculateBoundingRect(false, out uielement);

            if (rect != Rect.Empty && uielement != null)
            {
                HwndSource hwndSource = PresentationSource.CriticalFromVisual(uielement) as HwndSource;
                if (hwndSource != null)
                {
                    rect = PointUtil.ElementToRoot(rect, uielement, hwndSource);
                    rect = PointUtil.RootToClient(rect, hwndSource);
                    rect = PointUtil.ClientToScreen(rect, hwndSource);
                }
            }
            return(rect);
        }
Esempio n. 28
0
        private void OnSourceChanged(object sender, SourceChangedEventArgs e)
        {
            IKeyboardInputSite keyboardInputSite = ((IKeyboardInputSink)this).KeyboardInputSite;

            if (keyboardInputSite != null)
            {
                if (this._fTrusted.Value)
                {
                    new UIPermission(PermissionState.Unrestricted).Assert();
                }
                try
                {
                    ((IKeyboardInputSink)this).KeyboardInputSite = null;
                    keyboardInputSite.Unregister();
                }
                finally
                {
                    if (this._fTrusted.Value)
                    {
                        CodeAccessPermission.RevertAssert();
                    }
                }
            }
            IKeyboardInputSink keyboardInputSink = PresentationSource.CriticalFromVisual(this, false) as IKeyboardInputSink;

            if (keyboardInputSink != null)
            {
                if (this._fTrusted.Value)
                {
                    new UIPermission(PermissionState.Unrestricted).Assert();
                }
                try
                {
                    ((IKeyboardInputSink)this).KeyboardInputSite = keyboardInputSink.RegisterKeyboardInputSink(this);
                }
                finally
                {
                    if (this._fTrusted.Value)
                    {
                        CodeAccessPermission.RevertAssert();
                    }
                }
            }
            this.BuildOrReparentWindow();
        }
Esempio n. 29
0
        internal override Point GetPosition(IInputElement relativeTo)
        {
            VerifyAccess();

            // Validate that relativeTo is either a UIElement or a ContentElement
            if (relativeTo != null && !InputElement.IsValid(relativeTo))
            {
                throw new InvalidOperationException();
            }

            PresentationSource relativePresentationSource = null;

            if (relativeTo != null)
            {
                DependencyObject dependencyObject = relativeTo as DependencyObject;
                DependencyObject containingVisual = InputElement.GetContainingVisual(dependencyObject);
                if (containingVisual != null)
                {
                    relativePresentationSource = PresentationSource.CriticalFromVisual(containingVisual);
                }
            }
            else
            {
                if (_inputSource != null)
                {
                    relativePresentationSource = _inputSource.Value;
                }
            }

            // Verify that we have a valid PresentationSource with a valid RootVisual
            // - if we don't we won't be able to invoke ClientToRoot or TranslatePoint and
            //   we will just return 0,0
            if (relativePresentationSource == null || relativePresentationSource.RootVisual == null)
            {
                return(new Point(0, 0));
            }

            Point curPoint = new Point(_pointerData.Info.ptPixelLocationRaw.X, _pointerData.Info.ptPixelLocationRaw.Y);

            Point ptClient   = PointUtil.ScreenToClient(curPoint, relativePresentationSource);
            Point ptRoot     = PointUtil.ClientToRoot(ptClient, relativePresentationSource);
            Point ptRelative = InputElement.TranslatePoint(ptRoot, relativePresentationSource.RootVisual, (DependencyObject)relativeTo);

            return(ptRelative);
        }
Esempio n. 30
0
        internal static Point ClientToScreen(UIElement relativeTo, Point point)
        {
            GeneralTransform   transform;
            PresentationSource source = PresentationSource.CriticalFromVisual(relativeTo);

            if (source == null)
            {
                return(new Point(double.NaN, double.NaN));
            }
            transform = relativeTo.TransformToAncestor(source.RootVisual);
            Point ptRoot;

            transform.TryTransform(point, out ptRoot);
            Point ptClient = PointUtil.RootToClient(ptRoot, source);
            Point ptScreen = PointUtil.ClientToScreen(ptClient, source);

            return(ptScreen);
        }