Resources | Resources |



Overriding a widget event handler

This topic shows how to override a widget's event handler for customized event handling. The example code shown in this topic is from c_checkboxstaticwidget_app.

Overriding the event handler allows an application to subclass an object by installing a new event handler. An application could provide a customized event handler to a container object for the purpose of handling certain events in very specific ways -- for example, handling (or even ignoring) attempts to set various widget properties in a special manner.

To override a widget's event handler, an application needs to do the following:

  1. Initialize HandlerDesc structure.
  2. Set the event handler using IWidget_SetHandler().
  3. Implement custom event handler.
  4. Pass unhandled events to the widget's default handler.
  5. Free memory.

Example: initialize HandlerDesc structure

In order to override a widget's event handler, an application needs to provide a HandlerDesc structure, which should have information about the function overriding the widget's event handler. HandlerDesc structure has three members:.

  1. The custom event handler function called by Brew MP when handling events sent to this object.
  2. A pointer to private data allocated and managed by the caller that has registered the handler. This data pointer is passed into the event handler, as well as the free handler, providing each of these callbacks with access to this private storage.
  3. Pointer to the callback function called by Brew MP when freeing the private memory associated with the handler. This gives the object the opportunity to release any "deep storage" that may have been allocated by the object.

The HANDLERDESC_INIT() macro can be used to initialize this structure. Please note that the HandlerDesc structure should be in the heap.

   ERR_TRY( ISHELL_CreateInstance(piShell, AEECLSID_CRootContainer,
               (void**)&me->picRoot) );
   ERR_TRY( IRootContainer_QueryInterface(me->picRoot, AEEIID_IWidget,
               (void**)&me->piwRoot) );

   // override the default handler for the root container
   HANDLERDESC_INIT(&me->hdRoot, SampleTouchApp_RootHandler, me, 0);

Example: set the event handler using IWidget_SetHandler()

IWidget_SetHandler() can be used to set the handler.

IWidget_SetHandler(me->piwRoot, &me->hdRoot);

Example: implement custom event handler

This example shows the custom event handler. It handles specific cases for EVT_POINTER_DOWN (for WETYPE_CHILD and WETYPE_BACKGROUND). It calls the widget's default event handler for other properties and events.

static boolean SampleTouchApp_RootHandler(SampleTouchApp *me,
            AEEEvent evt, uint16 wParam, uint32 dwParam)
   if (EVT_POINTER_DOWN == evt) {

      WidgetElemInfo wei;
      int x, y;

      GetXY(dwParam, &x, &y);

      if (AEE_SUCCESS == IWidget_FindElement(me->piwRoot, x, y, &wei)) {

         switch (wei.nElemType) {

            case WETYPE_CHILD: {
               IWidget *piwClick = SampleTouchApp_GetWidgetByID( me->piwRoot, 

               if (piwClick != me->piwSoftkey && piwClick != me->piwMask) {
                  SampleTouchApp_SetWidgetZTopmost(me, piwClick);

               if (SampleTouchApp_IRootContainer_IsVisible(me->picRoot,
                           && me->piwLeftMenu != piwClick) {

                              me->piwLeftMenu, 0);

            case WETYPE_BACKGROUND:
               if (SampleTouchApp_IRootContainer_IsVisible(me->picRoot,
                           me->piwLeftMenu)) {
                              me->piwLeftMenu, 0);


   return HANDLERDESC_CALL(&me->hdRoot, evt, wParam, dwParam);