Resources | developer.brewmp.com Resources | developer.brewmp.com

Developer

resources

Adding touch support to a widget-based application

This topic shows how to add touch support to a widget-based application. The example code shown is from c_viewporttouchapp.

To add touch support to a widget-based application:

  • Enable touch support on the root container by calling IWidget_EnableTouch().

    Optionally, for advanced functionality:

    • Retrieve the touch observer from the root container.
    • Add a listener to the root container using IObserver.

Widgets have default event handling. A widget's event handling can be customized by calling IWidget_SetHandler(). For more information, see the http://developer.brewmp.com/reference/api-all on the Brew MP website.

The following include files are required for the examples in this topic to implement touch and create supporting widgets:

#include "AEEIQI.h"     
#include "AEEIWidget.h"    
#include "AEEIPropContainer.h"    
#include "AEEPointerHelpers.h"    
#include "AEEImageStaticWidget.h"    
#include "AEEXYContainer.h"    
    
#include "AEEIRootContainer.h"    
#include "AEERootContainer.bid"    
#include "AEEDisplayCanvas.bid"    
    
#include "AEEImageWidget.bid"    
    
#include "AppUtil.h"    
    
#include "AEEWidgetElement.h"    
#include "AEEPointerObserver.h"

Retrieve and activate the Touch Observer

//Try to retrieve TouchObserver from root container    
IWidget_GetPropertyEx(pMe->piwRoot, PROPEX_TOUCHOBSERVER, (int) sizeof(pIObserver),    
(void*)&pIObserver);    
    
IObserver_SetMaxTapDelay(pIObserver, 900);    
IObserver_GetGestureModel(pIObserver, &piGestureModel);    
IModel_AddListenerEx(piGestureModel, &pMe->mlGesturesListener, (PFNLISTENER)     
ViewPortTouchApp_GesturesModelListener, (void*) pMe);    
IObserver_StartObservation(pIObserver, TOUCHOBSF_TAPANDHOLD);

Enable widgets to handle touch events

//allow it to receive touch events    
ERR_TRY( IWidget_SetTouchMode(pMe->m_pWorldMapWidget, AEEWIDGET_TOUCH_MODE_ALWAYS));     
//set a handler to process those touch events    
HANDLERDESC_INIT(&pMe->hdWorldMapImage, WorldMapImage_Handler, pMe, 0);    
IWidget_SetHandler(pMe->m_pWorldMapWidget, &pMe->hdWorldMapImage);    
    
//set the extent of the widget    
//you need this or you will not see the image    
{    
   WidgetExtent we;    
   IWidget_GetPreferredExtent(pMe->m_pWorldMapWidget, &we);    
   IWidget_SetExtent(pMe->m_pWorldMapWidget, &we);    
}    
    
//inset widget into the root container     
//leave enough space for a text label at the top    
iX = (pMe->m_cxWidth/2) - (tImageInfo.cx/2);    
iY = 48;    
ViewPortTouchApp_InsertWidget(pMe, pMe->m_pWorldMapWidget, iX, iY, TRUE);    
iY = tImageInfo.cy;

Implement a handler for the widgets touch messages

static boolean WorldMapImage_Handler(ViewPortTouchApp *pMe, 
            AEEEvent evt, uint16 wParam, uint32 dwParam)    
{    
   AEEResult nErr = 0;    
   if (EVT_POINTER_DOWN == evt)     
   {    
      //set to the second frame (down button position)    
      IWidget_SetImage(pMe->m_pWorldMapWidget, pMe->m_pWorldMapImage[1]);    
   }    
   else if (EVT_POINTER_UP == evt)    
   {    
      if (NULL != pMe->piwChild)     
      {    
         (void) IRootContainer_Remove(pMe->picRoot, pMe->piwChild);    
         RELEASEIF(pMe->piwChild);    
      }    
      //set to the first frame (up button position)    
      IWidget_SetImage(pMe->m_pWorldMapWidget, pMe->m_pWorldMapImage[0]);    
      pMe->m_iSelection = 0;    
      pMe->m_bRunningMenuChoice = TRUE;    
    
      //we use a timer here so that the 'up' position of the    
      //button has a chance to draw before launching the other screen    
      ISHELL_SetTimer(pMe->piShell, 33, (PFNNOTIFY)LaunchOption, pMe);    
   }    
    
   ERR_CATCH:    
   return HANDLERDESC_CALL(&pMe->hdWorldMapImage, evt, wParam, dwParam);    
}    

Error handling

The following Macros are used for error handling.

#define ERR_CATCH  __errcatch    
#define ERR_TRY(x) do { nErr = (x); if (AEE_SUCCESS != nErr) goto ERR_CATCH; } while(0)    
#define ERR_THROW(e) do { nErr = (e); goto ERR_CATCH; } while(0)    
#define ERR_CHECK_PTR(p) if (NULL == p) ERR_THROW(AEE_EFAILED);