public void RaiseEvent(RoutedEventArgs e) { EventRoute eventRoute = new EventRoute(e.RoutedEvent, GetEventRouteItems(e.RoutedEvent, this, this)); e.Source = this; eventRoute.InvokeHandlers(e); }
/// <summary> /// Recycle the given instance of EventRoute /// </summary> internal static void RecycleObject(EventRoute eventRoute) { // Cleanup all refernces held eventRoute.Clear(); // Push instance on to the stack Push(eventRoute); }
// If this element is currently listening to synchronized input, add a pre-opportunity handler to keep track of event routed through this element. internal void AddSynchronizedInputPreOpportunityHandler(EventRoute route, RoutedEventArgs args) { if (InputManager.IsSynchronizedInput) { if (SynchronizedInputHelper.IsListening(this, args)) { RoutedEventHandler eventHandler = new RoutedEventHandler(this.SynchronizedInputPreOpportunityHandler); SynchronizedInputHelper.AddHandlerToRoute(this, route, eventHandler, false); } } }
/// <summary> /// Pop off the last instance of EventRoute in the stack /// </summary> private static EventRoute Pop() { lock (_synchronized) { if (_stackTop > 0) { EventRoute eventRoute = _eventRouteStack[--_stackTop]; _eventRouteStack[_stackTop] = null; return(eventRoute); } } return(null); }
/// <summary> /// Fetch a recycled object if available /// else create a new instance /// </summary> internal static EventRoute FetchObject(RoutedEvent routedEvent) { EventRoute eventRoute = Pop(); if (eventRoute == null) { eventRoute = new EventRoute(routedEvent); } else { eventRoute.RoutedEvent = routedEvent; } return eventRoute; }
/// <summary> /// Fetch a recycled object if available /// else create a new instance /// </summary> internal static EventRoute FetchObject(RoutedEvent routedEvent) { EventRoute eventRoute = Pop(); if (eventRoute == null) { eventRoute = new EventRoute(routedEvent); } else { eventRoute.RoutedEvent = routedEvent; } return(eventRoute); }
// If this element is currently listening to synchronized input, add a handler to post process the synchronized input otherwise // add a synchronized input pre-opportunity handler from parent if parent is listening. internal void AddSynchronizedInputPostOpportunityHandler(EventRoute route, RoutedEventArgs args) { if (InputManager.IsSynchronizedInput) { if (SynchronizedInputHelper.IsListening(this, args)) { RoutedEventHandler eventHandler = new RoutedEventHandler(this.SynchronizedInputPostOpportunityHandler); SynchronizedInputHelper.AddHandlerToRoute(this, route, eventHandler, true); } else { // Add a preview handler from the parent. SynchronizedInputHelper.AddParentPreOpportunityHandler(this, route, args); } } }
/// <summary> /// Push the given instance of EventRoute on to the stack /// </summary> private static void Push(EventRoute eventRoute) { lock (_synchronized) { // In a normal scenario it is extremely rare to // require more than 2 EventRoutes at the same time if (_eventRouteStack == null) { _eventRouteStack = new EventRoute[2]; _stackTop = 0; } if (_stackTop < 2) { _eventRouteStack[_stackTop++] = eventRoute; } } }
// Add a preopportunity handler for the logical parent incase of templated element. internal static void AddParentPreOpportunityHandler(DependencyObject o, EventRoute route, RoutedEventArgs args) { // If the logical parent is different from visual parent then add handler on behalf of the // parent into the route. This is to cover the templated elements, where event could be // handled by one of the child visual element but we should consider it as if event is handled by // parent element ( logical parent). DependencyObject visualParent = null; if(o is Visual || o is Visual3D) { visualParent = UIElementHelper.GetUIParent(o); } DependencyObject logicalParent = SynchronizedInputHelper.GetUIParentCore(o); if (logicalParent != null && logicalParent != visualParent) { UIElement e = logicalParent as UIElement; if (e != null) { e.AddSynchronizedInputPreOpportunityHandler(route, args); } else { ContentElement ce = logicalParent as ContentElement; if (ce != null) { ce.AddSynchronizedInputPreOpportunityHandler(route, args); } else { UIElement3D e3D = logicalParent as UIElement3D; if (e3D != null) { e3D.AddSynchronizedInputPreOpportunityHandler(route, args); } } } } }
/// <summary> /// Allows ContentElement to augment the /// <see cref="EventRoute"/> /// </summary> /// <remarks> /// Sub-classes of ContentElement can override /// this method to custom augment the route /// </remarks> /// <param name="route"> /// The <see cref="EventRoute"/> to be /// augmented /// </param> /// <param name="args"> /// <see cref="RoutedEventArgs"/> for the /// RoutedEvent to be raised post building /// the route /// </param> /// <returns> /// Whether or not the route should continue past the visual tree. /// If this is true, and there are no more visual parents, the route /// building code will call the GetUIParentCore method to find the /// next non-visual parent. /// </returns> internal virtual bool BuildRouteCore(EventRoute route, RoutedEventArgs args) { return false; }
/// <summary> /// Allows FrameworkElement to augment the /// <see cref="EventRoute"/> /// </summary> /// <remarks> /// NOTE: If this instance does not have a /// visualParent but has a model parent /// then route is built through the model /// parent /// </remarks> /// <param name="route"> /// The <see cref="EventRoute"/> to be /// augmented /// </param> /// <param name="args"> /// <see cref="RoutedEventArgs"/> for the /// RoutedEvent to be raised post building /// the route /// </param> /// <returns> /// Whether or not the route should continue past the visual tree. /// If this is true, and there are no more visual parents, the route /// building code will call the GetUIParentCore method to find the /// next non-visual parent. /// </returns> internal override sealed bool BuildRouteCore(EventRoute route, RoutedEventArgs args) { bool continuePastCoreTree = false; // Verify Context Access // VerifyAccess(); DependencyObject visualParent = (DependencyObject) ContentOperations.GetParent(this); DependencyObject modelParent = this._parent; // FrameworkElement extends the basic event routing strategy by // introducing the concept of a logical tree. When an event // passes through an element in a logical tree, the source of // the event needs to change to the leaf-most node in the same // logical tree that is in the route. // Check the route to see if we are returning into a logical tree // that we left before. If so, restore the source of the event to // be the source that it was when we left the logical tree. DependencyObject branchNode = route.PeekBranchNode() as DependencyObject; if (branchNode != null && IsLogicalDescendent(branchNode)) { // We keep the most recent source in the event args. Note that // this is only for our consumption. Once the event is raised, // it will use the source information in the route. args.Source = (route.PeekBranchSource()); AdjustBranchSource(args); route.AddSource(args.Source); // By popping the branch node we will also be setting the source // in the event route. route.PopBranchNode(); // Add intermediate ContentElements to the route FrameworkElement.AddIntermediateElementsToRoute(this, route, args, LogicalTreeHelper.GetParent(branchNode)); } // Check if the next element in the route is in a different // logical tree. if (!IgnoreModelParentBuildRoute(args)) { // If there is no visual parent, route via the model tree. if (visualParent == null) { continuePastCoreTree = modelParent != null; } else if(modelParent != null) { // If there is a model parent, record the branch node. route.PushBranchNode(this, args.Source); // The source is going to be the visual parent, which // could live in a different logical tree. args.Source = (visualParent); } } return continuePastCoreTree; }
/// <summary> /// Add Style TargetType and FEF EventHandlers to the EventRoute /// </summary> internal override void AddToEventRouteCore(EventRoute route, RoutedEventArgs args) { FrameworkElement.AddStyleHandlersToEventRoute(null, this, route, args); }
public void AddToEventRoute(EventRoute route, RoutedEventArgs e) { }
private static void AddStyleHandlersToEventRoute(EventRoute route, DependencyObject source, RoutedEventHandlerInfo[] handlers) { if (handlers == null) return; for (int index = 0; index < handlers.Length; ++index) route.Add((object) source, handlers[index].Handler, handlers[index].InvokeHandledEventsToo); }
// This is a helper that will facilitate adding a given array of handlers to the route private static void AddStyleHandlersToEventRoute( EventRoute route, DependencyObject source, RoutedEventHandlerInfo[] handlers) { if (handlers != null) { for (int i=0; i<handlers.Length; i++) { route.Add(source, handlers[i].Handler, handlers[i].InvokeHandledEventsToo); } } }
public void RaiseEvent(RoutedEventArgs e) { EventRoute eventRoute = new EventRoute(e.RoutedEvent, GetEventRouteItems(e.RoutedEvent, this)); e.Source = this; eventRoute.InvokeHandlers(e); }
/// <summary> /// Add Style TargetType and FEF EventHandlers to the EventRoute /// </summary> internal override void AddToEventRouteCore(EventRoute route, RoutedEventArgs args) { AddStyleHandlersToEventRoute(this, null, route, args); }
/// <summary> /// Allows FrameworkElement to augment the /// <see cref="EventRoute"/> /// </summary> /// <remarks> /// NOTE: If this instance does not have a /// visualParent but has a model parent /// then route is built through the model /// parent /// </remarks> /// <param name="route"> /// The <see cref="EventRoute"/> to be /// augmented /// </param> /// <param name="args"> /// <see cref="RoutedEventArgs"/> for the /// RoutedEvent to be raised post building /// the route /// </param> /// <returns> /// Whether or not the route should continue past the visual tree. /// If this is true, and there are no more visual parents, the route /// building code will call the GetUIParentCore method to find the /// next non-visual parent. /// </returns> internal override bool BuildRouteCore(EventRoute route, RoutedEventArgs args) { return BuildRouteCoreHelper(route, args, true); }
public void AddToEventRoute(EventRoute route, RoutedEventArgs e);
/// <summary> /// This virtual method is to be overridden in Framework /// to be able to add handlers for styles /// </summary> internal virtual void AddToEventRouteCore(EventRoute route, RoutedEventArgs args) { }
public void AddToEventRoute (EventRoute route, RoutedEventArgs e) { throw new NotImplementedException (); }
internal static void AddIntermediateElementsToRoute( DependencyObject mergePoint, EventRoute route, RoutedEventArgs args, DependencyObject modelTreeNode) { while (modelTreeNode != null && modelTreeNode != mergePoint) { UIElement uiElement = modelTreeNode as UIElement; ContentElement contentElement = modelTreeNode as ContentElement; UIElement3D uiElement3D = modelTreeNode as UIElement3D; if(uiElement != null) { uiElement.AddToEventRoute(route, args); FrameworkElement fe = uiElement as FrameworkElement; if (fe != null) { AddStyleHandlersToEventRoute(fe, null, route, args); } } else if (contentElement != null) { contentElement.AddToEventRoute(route, args); FrameworkContentElement fce = contentElement as FrameworkContentElement; if (fce != null) { AddStyleHandlersToEventRoute(null, fce, route, args); } } else if (uiElement3D != null) { uiElement3D.AddToEventRoute(route, args); } // Get model parent modelTreeNode = LogicalTreeHelper.GetParent(modelTreeNode); } }
// Add synchronized input handler for templated parent. internal override void AddSynchronizedInputPreOpportunityHandlerCore(EventRoute route, RoutedEventArgs args) { UIElement uiElement = this._templatedParent as UIElement; if (uiElement != null) { uiElement.AddSynchronizedInputPreOpportunityHandler(route, args); } }
/// <summary> /// Builds the <see cref="EventRoute"/> /// </summary> /// <param name="route"> /// The <see cref="EventRoute"/> being /// built /// </param> /// <param name="args"> /// <see cref="RoutedEventArgs"/> for the /// RoutedEvent to be raised post building /// the route /// </param> internal void BuildRoute(EventRoute route, RoutedEventArgs args) { UIElement.BuildRouteHelper(this, route, args); }
internal bool BuildRouteCoreHelper(EventRoute route, RoutedEventArgs args, bool shouldAddIntermediateElementsToRoute) { bool flag = false; DependencyObject parent = VisualTreeHelper.GetParent((DependencyObject) this); DependencyObject uiParentCore = this.GetUIParentCore(); DependencyObject dependencyObject = route.PeekBranchNode() as DependencyObject; if (dependencyObject != null && this.IsLogicalDescendent(dependencyObject)) { args.Source = route.PeekBranchSource(); this.AdjustBranchSource(args); route.AddSource(args.Source); route.PopBranchNode(); if (shouldAddIntermediateElementsToRoute) FrameworkElement.AddIntermediateElementsToRoute((DependencyObject) this, route, args, LogicalTreeHelper.GetParent(dependencyObject)); } if (!this.IgnoreModelParentBuildRoute(args)) { if (parent == null) flag = uiParentCore != null; else if (uiParentCore != null) { Visual visual = parent as Visual; if (visual != null) { if (visual.CheckFlagsAnd(VisualFlags.IsLayoutIslandRoot)) flag = true; } else if (((Visual3D) parent).CheckFlagsAnd(VisualFlags.IsLayoutIslandRoot)) flag = true; route.PushBranchNode((object) this, args.Source); args.Source = (object) parent; } } return flag; }
/// <summary> /// Add the event handlers for this element to the route. /// </summary> public void AddToEventRoute(EventRoute route, RoutedEventArgs e) { if (route == null) { throw new ArgumentNullException("route"); } if (e == null) { throw new ArgumentNullException("e"); } // Get class listeners for this ContentElement RoutedEventHandlerInfoList classListeners = GlobalEventManager.GetDTypedClassListeners(this.DependencyObjectType, e.RoutedEvent); // Add all class listeners for this ContentElement while (classListeners != null) { for(int i = 0; i < classListeners.Handlers.Length; i++) { route.Add(this, classListeners.Handlers[i].Handler, classListeners.Handlers[i].InvokeHandledEventsToo); } classListeners = classListeners.Next; } // Get instance listeners for this ContentElement FrugalObjectList<RoutedEventHandlerInfo> instanceListeners = null; EventHandlersStore store = EventHandlersStore; if (store != null) { instanceListeners = store[e.RoutedEvent]; // Add all instance listeners for this ContentElement if (instanceListeners != null) { for (int i = 0; i < instanceListeners.Count; i++) { route.Add(this, instanceListeners[i].Handler, instanceListeners[i].InvokeHandledEventsToo); } } } // Allow Framework to add event handlers in styles AddToEventRouteCore(route, e); }
// Add Style TargetType and FEF EventHandlers to the EventRoute internal static void AddStyleHandlersToEventRoute( FrameworkElement fe, FrameworkContentElement fce, EventRoute route, RoutedEventArgs args) { Debug.Assert(fe != null || fce != null); DependencyObject source = (fe != null) ? (DependencyObject)fe : (DependencyObject)fce; Style selfStyle = null; FrameworkTemplate selfFrameworkTemplate = null; DependencyObject templatedParent = null; int templateChildIndex = -1; // Fetch selfStyle, TemplatedParent and TemplateChildIndex if (fe != null) { selfStyle = fe.Style; selfFrameworkTemplate = fe.TemplateInternal; templatedParent = fe.TemplatedParent; templateChildIndex = fe.TemplateChildIndex; } else { selfStyle = fce.Style; templatedParent = fce.TemplatedParent; templateChildIndex = fce.TemplateChildIndex; } // Add TargetType EventHandlers to the route. Notice that ThemeStyle // cannot have EventHandlers and hence are ignored here. RoutedEventHandlerInfo[] handlers = null; if (selfStyle != null && selfStyle.EventHandlersStore != null) { handlers = selfStyle.EventHandlersStore.GetRoutedEventHandlers(args.RoutedEvent); AddStyleHandlersToEventRoute(route, source, handlers); } if (selfFrameworkTemplate != null && selfFrameworkTemplate.EventHandlersStore != null) { handlers = selfFrameworkTemplate.EventHandlersStore.GetRoutedEventHandlers(args.RoutedEvent); AddStyleHandlersToEventRoute(route, source, handlers); } if (templatedParent != null) { FrameworkTemplate templatedParentTemplate = null; FrameworkElement feTemplatedParent = templatedParent as FrameworkElement; Debug.Assert( feTemplatedParent != null ); templatedParentTemplate = feTemplatedParent.TemplateInternal; // Fetch handlers from either the parent style or template handlers = null; if (templatedParentTemplate != null && templatedParentTemplate.HasEventDependents) { handlers = StyleHelper.GetChildRoutedEventHandlers(templateChildIndex, args.RoutedEvent, ref templatedParentTemplate.EventDependents); } // Add FEF EventHandlers to the route AddStyleHandlersToEventRoute(route, source, handlers); } }
// Helper to add pre-opportunity handler for templated parent of this element in case parent is listening // for synchronized input. internal virtual void AddSynchronizedInputPreOpportunityHandlerCore(EventRoute route, RoutedEventArgs args) { }
public void AddToEventRoute(EventRoute eventRoute, RoutedEventArgs args) { throw new NotImplementedException(); }
internal static void BuildRouteHelper(DependencyObject e, EventRoute route, RoutedEventArgs args) { if (route == null) { throw new ArgumentNullException("route"); } if (args == null) { throw new ArgumentNullException("args"); } if (args.Source == null) { throw new ArgumentException(SR.Get(SRID.SourceNotSet)); } if (args.RoutedEvent != route.RoutedEvent) { throw new ArgumentException(SR.Get(SRID.Mismatched_RoutedEvent)); } // Route via visual tree if (args.RoutedEvent.RoutingStrategy == RoutingStrategy.Direct) { UIElement uiElement = e as UIElement; ContentElement contentElement = null; UIElement3D uiElement3D = null; if (uiElement == null) { contentElement = e as ContentElement; if (contentElement == null) { uiElement3D = e as UIElement3D; } } // Add this element to route if (uiElement != null) { uiElement.AddToEventRoute(route, args); } else if (contentElement != null) { contentElement.AddToEventRoute(route, args); } else if (uiElement3D != null) { uiElement3D.AddToEventRoute(route, args); } } else { int cElements = 0; while (e != null) { UIElement uiElement = e as UIElement; ContentElement contentElement = null; UIElement3D uiElement3D = null; if (uiElement == null) { contentElement = e as ContentElement; if (contentElement == null) { uiElement3D = e as UIElement3D; } } // Protect against infinite loops by limiting the number of elements // that we will process. if (cElements++ > MAX_ELEMENTS_IN_ROUTE) { throw new InvalidOperationException(SR.Get(SRID.TreeLoop)); } // Allow the element to adjust source object newSource = null; if (uiElement != null) { newSource = uiElement.AdjustEventSource(args); } else if (contentElement != null) { newSource = contentElement.AdjustEventSource(args); } else if (uiElement3D != null) { newSource = uiElement3D.AdjustEventSource(args); } // Add changed source information to the route if (newSource != null) { route.AddSource(newSource); } // Invoke BuildRouteCore bool continuePastVisualTree = false; if (uiElement != null) { //Add a Synchronized input pre-opportunity handler just before the class and instance handlers uiElement.AddSynchronizedInputPreOpportunityHandler(route, args); continuePastVisualTree = uiElement.BuildRouteCore(route, args); // Add this element to route uiElement.AddToEventRoute(route, args); //Add a Synchronized input post-opportunity handler just after class and instance handlers uiElement.AddSynchronizedInputPostOpportunityHandler(route, args); // Get element's visual parent e = uiElement.GetUIParent(continuePastVisualTree); } else if (contentElement != null) { //Add a Synchronized input pre-opportunity handler just before the class and instance handlers contentElement.AddSynchronizedInputPreOpportunityHandler(route, args); continuePastVisualTree = contentElement.BuildRouteCore(route, args); // Add this element to route contentElement.AddToEventRoute(route, args); //Add a Synchronized input post-opportunity handler just after the class and instance handlers contentElement.AddSynchronizedInputPostOpportunityHandler(route, args); // Get element's visual parent e = (DependencyObject)contentElement.GetUIParent(continuePastVisualTree); } else if (uiElement3D != null) { //Add a Synchronized input pre-opportunity handler just before the class and instance handlers uiElement3D.AddSynchronizedInputPreOpportunityHandler(route, args); continuePastVisualTree = uiElement3D.BuildRouteCore(route, args); // Add this element to route uiElement3D.AddToEventRoute(route, args); //Add a Synchronized input post-opportunity handler just after the class and instance handlers uiElement3D.AddSynchronizedInputPostOpportunityHandler(route, args); // Get element's visual parent e = uiElement3D.GetUIParent(continuePastVisualTree); } // If the BuildRouteCore implementation changed the // args.Source to the route parent, respect it in // the actual route. if (e == args.Source) { route.AddSource(e); } } } }
internal static void AddStyleHandlersToEventRoute(FrameworkElement fe, FrameworkContentElement fce, EventRoute route, RoutedEventArgs args) { DependencyObject source = fe != null ? (DependencyObject) fe : (DependencyObject) fce; FrameworkTemplate frameworkTemplate = (FrameworkTemplate) null; Style style; DependencyObject templatedParent; int templateChildIndex; if (fe != null) { style = fe.Style; frameworkTemplate = fe.TemplateInternal; templatedParent = fe.TemplatedParent; templateChildIndex = fe.TemplateChildIndex; } else { style = fce.Style; templatedParent = fce.TemplatedParent; templateChildIndex = fce.TemplateChildIndex; } if (style != null && style.EventHandlersStore != null) { RoutedEventHandlerInfo[] routedEventHandlers = style.EventHandlersStore.GetRoutedEventHandlers(args.RoutedEvent); FrameworkElement.AddStyleHandlersToEventRoute(route, source, routedEventHandlers); } if (frameworkTemplate != null && frameworkTemplate.EventHandlersStore != null) { RoutedEventHandlerInfo[] routedEventHandlers = frameworkTemplate.EventHandlersStore.GetRoutedEventHandlers(args.RoutedEvent); FrameworkElement.AddStyleHandlersToEventRoute(route, source, routedEventHandlers); } if (templatedParent == null) return; FrameworkTemplate templateInternal = (templatedParent as FrameworkElement).TemplateInternal; RoutedEventHandlerInfo[] handlers = (RoutedEventHandlerInfo[]) null; if (templateInternal != null && templateInternal.HasEventDependents) handlers = StyleHelper.GetChildRoutedEventHandlers(templateChildIndex, args.RoutedEvent, ref templateInternal.EventDependents); FrameworkElement.AddStyleHandlersToEventRoute(route, source, handlers); }
// If the routed event type matches one the element listening on then add handler to the event route. internal static void AddHandlerToRoute(DependencyObject o, EventRoute route, RoutedEventHandler eventHandler, bool handledToo) { // Add a synchronized input handler to the route. route.Add(o, eventHandler, handledToo); }
public void AddToEventRoute(System.Windows.EventRoute route, System.Windows.RoutedEventArgs e) { inter.AddToEventRoute(route, e); }