예제 #1
0
        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------
        // None

        //------------------------------------------------------
        //
        //  Internal Static Methods
        //
        //------------------------------------------------------

        #region Internal Static Methods
        /// <summary>
        ///     Called by UIElement(3D)s when a visual ancestor link has changed.
        /// </summary>
        /// <param name="uie">The UIElement whose ancestory may have changed.</param>
        /// <param name="e">  Event Args.</param>
        internal static void OnVisualAncestorChanged(DependencyObject uie, AncestorChangedEventArgs e)
        {
            Debug.Assert(InputElement.IsUIElement3D(uie) || InputElement.IsUIElement(uie));

            if (true == (bool)uie.GetValue(GetsSourceChangedEventProperty))
            {
                UpdateSourceOfElement(uie, e.Ancestor, e.OldParent);
            }
        }
예제 #2
0
        /// <summary>
        ///     Adds a handler for the SourceChanged event to the element.
        /// </summary>
        /// <param name="element">The element to add the handler too.</param>
        /// <param name="handler">The hander to add.</param>
        /// <remarks>
        ///     Even though this is a routed event handler, there are special
        ///     restrictions placed on this event.
        ///     1) You cannot use the UIElement or ContentElement AddHandler() method.
        ///     2) Class handlers are not allowed.
        ///     3) The handlers will receive the SourceChanged event even if it was handled.
        ///     Callers must have UIPermission(UIPermissionWindow.AllWindows) to call this API.
        /// </remarks>
        public static void AddSourceChangedHandler(IInputElement element, SourceChangedEventHandler handler)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            // Either UIElement or ContentElement
            if (!InputElement.IsValid(element))
            {
                throw new ArgumentException(SR.Get(SRID.Invalid_IInputElement), "element");
            }
            DependencyObject o = (DependencyObject)element;

            //             o.VerifyAccess();


            // I would rather throw an exception here, but the CLR doesn't
            // so we won't either.
            if (handler != null)
            {
                FrugalObjectList <RoutedEventHandlerInfo> info;

                if (InputElement.IsUIElement(o))
                {
                    UIElement uie = o as UIElement;
                    uie.AddHandler(SourceChangedEvent, handler);
                    info = uie.EventHandlersStore[SourceChangedEvent];
                    if (1 == info.Count)
                    {
                        uie.VisualAncestorChanged += new Visual.AncestorChangedEventHandler(uie.OnVisualAncestorChanged);
                        AddElementToWatchList(uie);
                    }
                }
                else if (InputElement.IsUIElement3D(o))
                {
                    UIElement3D uie3D = o as UIElement3D;
                    uie3D.AddHandler(SourceChangedEvent, handler);
                    info = uie3D.EventHandlersStore[SourceChangedEvent];
                    if (1 == info.Count)
                    {
                        uie3D.VisualAncestorChanged += new Visual.AncestorChangedEventHandler(uie3D.OnVisualAncestorChanged);
                        AddElementToWatchList(uie3D);
                    }
                }
                else
                {
                    ContentElement ce = o as ContentElement;
                    ce.AddHandler(SourceChangedEvent, handler);
                    info = ce.EventHandlersStore[SourceChangedEvent];
                    if (1 == info.Count)
                    {
                        AddElementToWatchList(ce);
                    }
                }
            }
        }
예제 #3
0
        private bool RaiseContextMenuOpeningEvent(IInputElement source, double x, double y, bool userInitiated)
        {
            ContextMenuEventArgs contextMenuEventArgs = new ContextMenuEventArgs(source, true, x, y);
            DependencyObject     dependencyObject     = source as DependencyObject;

            if (userInitiated && dependencyObject != null)
            {
                if (InputElement.IsUIElement(dependencyObject))
                {
                    ((UIElement)dependencyObject).RaiseEvent(contextMenuEventArgs, userInitiated);
                }
                else if (InputElement.IsContentElement(dependencyObject))
                {
                    ((ContentElement)dependencyObject).RaiseEvent(contextMenuEventArgs, userInitiated);
                }
                else if (InputElement.IsUIElement3D(dependencyObject))
                {
                    ((UIElement3D)dependencyObject).RaiseEvent(contextMenuEventArgs, userInitiated);
                }
                else
                {
                    source.RaiseEvent(contextMenuEventArgs);
                }
            }
            else
            {
                source.RaiseEvent(contextMenuEventArgs);
            }
            if (contextMenuEventArgs.Handled)
            {
                this.RaiseToolTipClosingEvent(true);
                return(true);
            }
            DependencyObject targetElement = contextMenuEventArgs.TargetElement;

            if (targetElement != null && ContextMenuService.ContextMenuIsEnabled(targetElement))
            {
                object      contextMenu  = ContextMenuService.GetContextMenu(targetElement);
                ContextMenu contextMenu2 = contextMenu as ContextMenu;
                contextMenu2.SetValue(PopupControlService.OwnerProperty, targetElement);
                contextMenu2.Closed += this.OnContextMenuClosed;
                if (x == -1.0 && y == -1.0)
                {
                    contextMenu2.Placement = PlacementMode.Center;
                }
                else
                {
                    contextMenu2.Placement = PlacementMode.MousePoint;
                }
                this.RaiseToolTipClosingEvent(true);
                contextMenu2.SetCurrentValueInternal(ContextMenu.IsOpenProperty, BooleanBoxes.TrueBox);
                return(true);
            }
            return(false);
        }
예제 #4
0
 internal static void RaiseAutomationEvents()
 {
     if (InputElement.IsUIElement(InputManager.ListeningElement))
     {
         UIElement e = (UIElement)InputManager.ListeningElement;
         //Raise InputDiscarded automation event
         SynchronizedInputHelper.RaiseAutomationEvent(e.GetAutomationPeer());
     }
     else if (InputElement.IsContentElement(InputManager.ListeningElement))
     {
         ContentElement ce = (ContentElement)InputManager.ListeningElement;
         //Raise InputDiscarded automation event
         SynchronizedInputHelper.RaiseAutomationEvent(ce.GetAutomationPeer());
     }
     else if (InputElement.IsUIElement3D(InputManager.ListeningElement))
     {
         UIElement3D e3D = (UIElement3D)InputManager.ListeningElement;
         //Raise InputDiscarded automation event
         SynchronizedInputHelper.RaiseAutomationEvent(e3D.GetAutomationPeer());
     }
 }
예제 #5
0
        /// <summary>
        ///     Removes a handler for the SourceChanged event to the element.
        /// </summary>
        /// <param name="e">The element to remove the handler from.</param>
        /// <param name="handler">The hander to remove.</param>
        /// <remarks>
        ///     Even though this is a routed event handler, there are special
        ///     restrictions placed on this event.
        ///     1) You cannot use the UIElement or ContentElement RemoveHandler() method.
        /// </remarks>
        public static void RemoveSourceChangedHandler(IInputElement e, SourceChangedEventHandler handler)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }

            if (!InputElement.IsValid(e))
            {
                throw new ArgumentException(SR.Get(SRID.Invalid_IInputElement), "e");
            }
            DependencyObject o = (DependencyObject)e;

            //             o.VerifyAccess();

            // I would rather throw an exception here, but the CLR doesn't
            // so we won't either.
            if (handler != null)
            {
                FrugalObjectList <RoutedEventHandlerInfo> info = null;
                EventHandlersStore store;

                // Either UIElement or ContentElement.
                if (InputElement.IsUIElement(o))
                {
                    UIElement uie = o as UIElement;
                    uie.RemoveHandler(SourceChangedEvent, handler);
                    store = uie.EventHandlersStore;
                    if (store != null)
                    {
                        info = store[SourceChangedEvent];
                    }
                    if (info == null || info.Count == 0)
                    {
                        uie.VisualAncestorChanged -= new Visual.AncestorChangedEventHandler(uie.OnVisualAncestorChanged);;
                        RemoveElementFromWatchList(uie);
                    }
                }
                else if (InputElement.IsUIElement3D(o))
                {
                    UIElement3D uie3D = o as UIElement3D;
                    uie3D.RemoveHandler(SourceChangedEvent, handler);
                    store = uie3D.EventHandlersStore;
                    if (store != null)
                    {
                        info = store[SourceChangedEvent];
                    }
                    if (info == null || info.Count == 0)
                    {
                        uie3D.VisualAncestorChanged -= new Visual.AncestorChangedEventHandler(uie3D.OnVisualAncestorChanged);;
                        RemoveElementFromWatchList(uie3D);
                    }
                }
                else
                {
                    ContentElement ce = o as ContentElement;
                    ce.RemoveHandler(SourceChangedEvent, handler);
                    store = ce.EventHandlersStore;
                    if (store != null)
                    {
                        info = store[SourceChangedEvent];
                    }
                    if (info == null || info.Count == 0)
                    {
                        RemoveElementFromWatchList(ce);
                    }
                }
            }
        }
예제 #6
0
        private bool RaiseContextMenuOpeningEvent(IInputElement source, double x, double y, bool userInitiated)
        {
            // Fire the event
            ContextMenuEventArgs args     = new ContextMenuEventArgs(source, true /* opening */, x, y);
            DependencyObject     sourceDO = source as DependencyObject;

            if (userInitiated && sourceDO != null)
            {
                if (InputElement.IsUIElement(sourceDO))
                {
                    ((UIElement)sourceDO).RaiseEvent(args, userInitiated);
                }
                else if (InputElement.IsContentElement(sourceDO))
                {
                    ((ContentElement)sourceDO).RaiseEvent(args, userInitiated);
                }
                else if (InputElement.IsUIElement3D(sourceDO))
                {
                    ((UIElement3D)sourceDO).RaiseEvent(args, userInitiated);
                }
                else
                {
                    source.RaiseEvent(args);
                }
            }
            else
            {
                source.RaiseEvent(args);
            }


            if (!args.Handled)
            {
                // No one handled the event, auto show any available ContextMenus

                // Saved from the bubble up the tree where we looked for a set ContextMenu property
                DependencyObject o = args.TargetElement;
                if ((o != null) && ContextMenuService.ContextMenuIsEnabled(o))
                {
                    // Retrieve the value
                    object      menu = ContextMenuService.GetContextMenu(o);
                    ContextMenu cm   = menu as ContextMenu;
                    cm.SetValue(OwnerProperty, o);
                    cm.Closed += new RoutedEventHandler(OnContextMenuClosed);

                    if ((x == -1.0) && (y == -1.0))
                    {
                        // We infer this to mean that the ContextMenu was opened with the keyboard
                        cm.Placement = PlacementMode.Center;
                    }
                    else
                    {
                        // If there is a CursorLeft and CursorTop, it was opened with the mouse.
                        cm.Placement = PlacementMode.MousePoint;
                    }

                    // Clear any open tooltips
                    RaiseToolTipClosingEvent(true /*reset */);

                    cm.SetCurrentValueInternal(ContextMenu.IsOpenProperty, BooleanBoxes.TrueBox);

                    return(true); // A menu was opened
                }

                return(false); // There was no menu to open
            }

            // Clear any open tooltips since someone else opened one
            RaiseToolTipClosingEvent(true /*reset */);

            return(true); // The event was handled by someone else
        }