Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Stingray Foundation Library User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

6.3 Event Routers

An event router is an object that generates events and routes them to event listeners. Event listeners can be added and removed from an event router, and it is the event router's responsibility to route the events to interested listeners. Event routers implement the IEventRouter interface, which contains three methods. Example 35 shows the IEventRouter interface.

Example 35: IEventRouter interface

6.3.1 Default Event Router Implementation

The IEventRouterImpl class provides a default implementation of the IEventRouter interface. Example 36 shows the IEventRouterImpl implementation of RouteEvent().

Example 36: IEventRouterImpl implementation of RouteEvent()

The IEventRouterImpl class gives each event listener an opportunity to handle the event. An alternative implementation might stop as soon as a listener is found to handle the event.

6.3.2 ATL Integration

The Events package integrates seamlessly with the ATL message map architecture by generating and routing events processed by ATL message maps. ATL defines the CMessageMap class, which defines the ProcessWindowMessage() function for handling messages. The ATL message map macros implement the ProcessWindowMessage() function. The CEventRouterMap class derives from CMessageMap and implements the ProcessWindowMessage() function by creating an event using an event factory and then passing it to RouteEvent(). The implementation of CEventRouterMap is straightforward, as shown in Example 37.

Example 37: CEventRouterMap implementation

The CEventRouterMap class acts as a bridge between ATL message maps and the event-listener architecture. The template parameter passed into CEventRouterMap is the derived class, which is assumed to be an event router. The CEventRouterMap class defines a virtual GetEventFactory() method to provide derived classes the opportunity to supply a different event factory, which is useful for filtering events.

To use CEventRouterMap, you need to insert one line of code in your ATL message map to chain to the CEventRouterMap object. Example 38 shows an ATL window class that implements event routing from an ATL message map.

Example 38: Implementing event routing from an ATL message map

To integrate event routing with the message map, you need to derive from the CEventRouterMap class and then add one entry to the message map to chain to it. Alternatively, you can declare the CEventRouterMap as a member variable of your class and then use ATL's CHAIN_MSG_MAP_MEMBER macro instead of CHAIN_MSG_MAP. Chaining to a CEventRouterMap does not interfere with the normal behavior of the ATL message map. In other words, the ATL message and event router logic live together happily.

6.3.3 MFC Integration

The Events package integrates seamlessly with the MFC message map architecture by hooking the OnWndMsg() and OnCmdMsg() functions and routing events for the Windows messages received by those two functions. The template class CMFCEventRouter wraps any CWnd derived class and overrides the OnWndMsg() and OnCmdMsg() functions to implement event creation and routing. The CMFCEventRouter class takes two template arguments. The first template argument is the derived class, which is assumed to be an event router. The CMFCEventRouter class does a static_cast to the derived class to invoke the RouteEvent() method. The second template parameter is the base class, which must be a CWnd derived class or any other class that defines OnWndMsg() and OnCmdMsg() with the same signature as CWnd. Example 39 shows the implementation of CMFCEventRouter.

Example 39: CMFCEventRouter implementation

The CMFCEventRouter class acts as a bridge between MFC message maps and the event-listener architecture. To use the CMFCEventRouter class, you need to mix it into your CWnd derived class. The following code is of an MFC window class that implements event routing.

Using the CMFCEventRouter class does not interfere with MFC's own message map processing. Both techniques can co-exist without any conflicts and you can use MFC message maps in conjunction with event listeners to handle events.



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and Stingray, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.