Ejemplo n.º 1
0
        // AddListener - Adds a listener for this client and notifies the UIA server.  All event
        // handler additions call through to this method.
        internal static void AddListener(AutomationElement rawEl, Delegate eventCallback, EventListener l)
        {
            lock (_classLock)
            {
                // If we are adding a listener then a proxy could be created as a result of an event so make sure they are loaded
                ProxyManager.LoadDefaultProxies();

                if (_listeners == null)
                {
                    // enough space for 16 AddXxxListeners (100 bytes)
                    _listeners = new ArrayList(16);
                }

                // Start the callback queue that gets us off the server's
                // UI thread when events arrive cross-proc
                CheckStartCallbackQueueing();

                //
                // The framework handles some events on behalf of providers; do those here
                //

                // If listening for BoundingRectangleProperty then may need to start listening on the
                // client-side for LocationChange WinEvent (only use *one* BoundingRectTracker instance).
                if (_winEventTrackers[(int)Tracker.BoundingRect] == null && HasProperty(AutomationElement.BoundingRectangleProperty, l.Properties))
                {
                    // There may be special cases to not map BoundingRect to WinEvent. One may be
                    // where rawEl is a non-top-level native implementation and TreeScope == Element.
                    // Another may be if rawEl is a native impl and TreeScope includes descendents then
                    // may not need to worry about BoundingRect prop change on hosted Win32 UI? I think the
                    // answer is 'yes' because BoundingRectangleProperty should fire events for the top-most
                    // element that moved in the hierarchy.  So if hosted Win32 content Rect changes w/o
                    // the host changing then it would fire this event.
                    // Note: Part of cleaning up WinEvent listeners is to move away from WinEvent handlers calling UIA client handlers
                    AddWinEventListener(Tracker.BoundingRect, new BoundingRectTracker());
                }

                // Start listening for menu event in order to raise MenuOpened/Closed events.
                if (_winEventTrackers [(int)Tracker.MenuOpenedOrClosed] == null && (l.EventId == AutomationElement.MenuOpenedEvent || l.EventId == AutomationElement.MenuClosedEvent))
                {
                    AddWinEventListener(Tracker.MenuOpenedOrClosed, new MenuTracker(new MenuHandler(OnMenuEvent)));
                }

                // Begin watching for hwnd open/close/show/hide so can advise of what events are being listened for.
                // Only advise UI contexts of events being added if the event might be raised by a provider.
                // TopLevelWindow event is raised by UI Automation framework so no need to track new UI.
                // Are there other events like this where Advise can be skipped?
                if (_winEventTrackers[(int)Tracker.WindowShowOrOpen] == null)
                {
                    AddWinEventListener(Tracker.WindowShowOrOpen, new WindowShowOrOpenTracker(new WindowShowOrOpenHandler(OnWindowShowOrOpen)));
                    AddWinEventListener(Tracker.WindowHideOrClose, new WindowHideOrCloseTracker(new WindowHideOrCloseHandler(OnWindowHideOrClose)));
                }

                // If listening for WindowInteractionStateProperty then may need to start listening on the
                // client-side for ObjectStateChange WinEvent.
                if (_winEventTrackers[(int)Tracker.WindowInteractionState] == null && HasProperty(WindowPattern.WindowInteractionStateProperty, l.Properties))
                {
                    AddWinEventListener(Tracker.WindowInteractionState, new WindowInteractionStateTracker());
                }

                // If listening for WindowVisualStateProperty then may need to start listening on the
                // client-side for ObjectLocationChange WinEvent.
                if (_winEventTrackers[(int)Tracker.WindowVisualState] == null && HasProperty(WindowPattern.WindowVisualStateProperty, l.Properties))
                {
                    AddWinEventListener(Tracker.WindowVisualState, new WindowVisualStateTracker());
                }

                // Wrap and store this record on the client...
                EventListenerClientSide ec = new EventListenerClientSide(rawEl, eventCallback, l);
                _listeners.Add(ec);

                // Only advise UI contexts of events being added if the event might be raised by
                // a provider.  TopLevelWindow event is raised by UI Automation framework.
                if (ShouldAdviseProviders(l.EventId))
                {
                    // .. then let the server know about this listener
                    ec.EventHandle = UiaCoreApi.UiaAddEvent(rawEl.RawNode, l.EventId.Id, ec.CallbackDelegate, l.TreeScope, PropertyArrayToIntArray(l.Properties), l.CacheRequest);
                }
            }
        }
        // AddListener - Adds a listener for this client and notifies the UIA server.  All event
        // handler additions call through to this method.
        internal static void AddListener(AutomationElement rawEl, Delegate eventCallback, EventListener l)
        {
            lock (_classLock)
            {
                // If we are adding a listener then a proxy could be created as a result of an event so make sure they are loaded
                ProxyManager.LoadDefaultProxies();

                if (_listeners == null)
                {
                    // enough space for 16 AddXxxListeners (100 bytes)
                    _listeners = new ArrayList(16);
                }

                // Start the callback queue that gets us off the server's
                // UI thread when events arrive cross-proc
                CheckStartCallbackQueueing();

                //
                // The framework handles some events on behalf of providers; do those here
                //

                // If listening for BoundingRectangleProperty then may need to start listening on the
                // client-side for LocationChange WinEvent (only use *one* BoundingRectTracker instance).
                if (_winEventTrackers[(int)Tracker.BoundingRect] == null && HasProperty(AutomationElement.BoundingRectangleProperty, l.Properties))
                {
                    //



                    AddWinEventListener(Tracker.BoundingRect, new BoundingRectTracker());
                }

                // Start listening for menu event in order to raise MenuOpened/Closed events.
                if (_winEventTrackers [(int)Tracker.MenuOpenedOrClosed] == null && (l.EventId == AutomationElement.MenuOpenedEvent || l.EventId == AutomationElement.MenuClosedEvent))
                {
                    AddWinEventListener(Tracker.MenuOpenedOrClosed, new MenuTracker(new MenuHandler(OnMenuEvent)));
                }

                // Begin watching for hwnd open/close/show/hide so can advise of what events are being listened for.
                // Only advise UI contexts of events being added if the event might be raised by a provider.
                // TopLevelWindow event is raised by UI Automation framework so no need to track new UI.
                //
                if (_winEventTrackers[(int)Tracker.WindowShowOrOpen] == null)
                {
                    AddWinEventListener(Tracker.WindowShowOrOpen, new WindowShowOrOpenTracker(new WindowShowOrOpenHandler(OnWindowShowOrOpen)));
                    AddWinEventListener(Tracker.WindowHideOrClose, new WindowHideOrCloseTracker(new WindowHideOrCloseHandler(OnWindowHideOrClose)));
                }

                // If listening for WindowInteractionStateProperty then may need to start listening on the
                // client-side for ObjectStateChange WinEvent.
                if (_winEventTrackers[(int)Tracker.WindowInteractionState] == null && HasProperty(WindowPattern.WindowInteractionStateProperty, l.Properties))
                {
                    AddWinEventListener(Tracker.WindowInteractionState, new WindowInteractionStateTracker());
                }

                // If listening for WindowVisualStateProperty then may need to start listening on the
                // client-side for ObjectLocationChange WinEvent.
                if (_winEventTrackers[(int)Tracker.WindowVisualState] == null && HasProperty(WindowPattern.WindowVisualStateProperty, l.Properties))
                {
                    AddWinEventListener(Tracker.WindowVisualState, new WindowVisualStateTracker());
                }

                // Wrap and store this record on the client...
                EventListenerClientSide ec = new EventListenerClientSide(rawEl, eventCallback, l);
                _listeners.Add(ec);

                // Only advise UI contexts of events being added if the event might be raised by
                // a provider.  TopLevelWindow event is raised by UI Automation framework.
                if (ShouldAdviseProviders(l.EventId))
                {
                    // .. then let the server know about this listener
                    ec.EventHandle = UiaCoreApi.UiaAddEvent(rawEl.RawNode, l.EventId.Id, ec.CallbackDelegate, l.TreeScope, PropertyArrayToIntArray(l.Properties), l.CacheRequest);
                }
            }
        }