新手问个BREW MP中GridWidget的问题
最近开始学做BREW MP,基础太差,许多东西还是一头雾水。昨天开始写个Grid Widget的例子,想简单的只显示个Static就好,可水平太差不知道怎么写。在Wizard生成的代码的基础上参考了 http://topic.csdn.net/u/20091123/18/3c67c251-79c0-45cf-b799-08ca40a839a5.html 一帖中两位仁兄的代码,现写出如下代码,请各位大哥看看小弟哪里写的有问题,谢谢了。
/*=============================================================================
FILE: GridWidgetSample.c
=============================================================================*/
/*-----------------------------------------------------------------------------
Includes and Variable Definitions
-----------------------------------------------------------------------------*/
#include "AEEModGen.h"
#include "AEEAppGen.h"
#include "AEEShell.h"
#include "GridWidgetSample.bid"
#include "GridWidgetSample_res.h"
/* some property definition, like AEERect, ect. */
#include "AEEWProperties.h"
#include "AEERootContainer.h"
#include "AEECLSID_ROOTCONTAINER.bid"
#include "AEEDisplayCanvas.h"
#include "AEECLSID_DISPLAYCANVAS.bid"
#include "AEEListWidget.h"
#include "AEECLSID_GRIDWIDGET.bid"
#include "AEEStaticWidget.h"
#include "AEECLSID_STATICWIDGET.bid"
#include "AEEIDecorator.h"
#include "AEEIVectorModel.h"
#include "AEECLSID_VECTORMODEL.bid"
#ifndef RELEASEIF
#define RELEASEIF(p) ReleaseIf((IBase**)(void*)&p)
//#define ERR_TRY(x) do { nErr = (x); if (nErr) goto ERR_CATCH; } while(0)
static void ReleaseIf(IBase **ppif) {
if (*ppif) {
IBASE_Release(*ppif);
*ppif = 0;
}
}
#endif
/*-----------------------------------------------------------------------------
Applet Structure
-----------------------------------------------------------------------------*/
typedef struct _GridWidgetSample {
AEEApplet applet;
IDisplay * piDisplay;
IShell * piShell;
AEEDeviceInfo deviceInfo;
// Add your own variables here...
IRootContainer *picRootContainer;
IWidget *piwRootWidget;
} GridWidgetSample;
/*-----------------------------------------------------------------------------
Function Prototypes
-----------------------------------------------------------------------------*/
static boolean GridWidgetSample_HandleEvent(GridWidgetSample* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam);
boolean GridWidgetSample_InitAppData(GridWidgetSample* pMe);
void GridWidgetSample_FreeAppData(GridWidgetSample* pMe);
static void GridWidgetSample_DrawScreen(GridWidgetSample * pMe);
/*-----------------------------------------------------------------------------
Function Definitions
-----------------------------------------------------------------------------*/
/*=============================================================================
FUNCTION: AEEClsCreateInstance
=============================================================================*/
int AEEClsCreateInstance(AEECLSID ClsId, IShell * piShell, IModule * piModule,
void ** ppObj)
{
*ppObj = NULL;
if( AEECLSID_GRIDWIDGETSAMPLE == ClsId ) {
if( TRUE == AEEApplet_New(sizeof(GridWidgetSample),
ClsId,
piShell,
piModule,
(IApplet**)ppObj,
(AEEHANDLER)GridWidgetSample_HandleEvent,
(PFNFREEAPPDATA)GridWidgetSample_FreeAppData) ) {
if(TRUE == GridWidgetSample_InitAppData((GridWidgetSample*)*ppObj)) {
return AEE_SUCCESS; // Data initialized successfully.
}
else {
IApplet_Release((IApplet*)*ppObj);
return AEE_EFAILED;
}
} // End AEEApplet_New
}
return AEE_EFAILED;
}
/*=============================================================================
FUNCTION: GridWidgetSample_InitAppData
=============================================================================*/
boolean GridWidgetSample_InitAppData(GridWidgetSample * pMe)
{
int nErr;
// Save local copy for easy access:
pMe->piDisplay = pMe->applet.m_pIDisplay;
pMe->piShell = pMe->applet.m_pIShell;
// Get the device information for this handset.
pMe->deviceInfo.wStructSize = sizeof(pMe->deviceInfo);
ISHELL_GetDeviceInfo(pMe->applet.m_pIShell,&pMe->deviceInfo);
// Insert your code here for initializing or allocating resources...
nErr = ISHELL_CreateInstance(pMe->piShell, AEECLSID_ROOTCONTAINER, (void *)(&pMe->picRootContainer));
if (AEE_SUCCESS != nErr) {
return FALSE;
}
nErr = IROOTCONTAINER_QueryInterface(pMe->picRootContainer, AEEIID_WIDGET, (void *)(&pMe->piwRootWidget));
if (AEE_SUCCESS != nErr) {
return FALSE;
}
return TRUE;// No failures up to this point, so return success.
}
/*=============================================================================
FUNCTION: GridWidgetSample_FreeAppData
=============================================================================*/
void GridWidgetSample_FreeAppData(GridWidgetSample * pMe)
{
// Insert your code here for freeing any resources you have allocated...
// Example to use for releasing each interface:
// if ( NULL != pMe->piMenuCtl ) { // check for NULL first
// IMenuCtl_Release(pMe->piMenuCtl)// release the interface
// pMe->piMenuCtl = NULL; // set to NULL so no problems later
// }
//
}
/*=============================================================================
FUNCTION: GridWidgetSample_HandleEvent
=============================================================================*/
static boolean GridWidgetSample_HandleEvent(GridWidgetSample* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
switch (eCode) {
case EVT_APP_START:
GridWidgetSample_DrawScreen(pMe); // Draw text on display screen.
return TRUE;
case EVT_APP_STOP:
return TRUE;
case EVT_APP_SUSPEND:
return TRUE;
case EVT_APP_RESUME:
GridWidgetSample_DrawScreen(pMe);
return TRUE;
case EVT_APP_MESSAGE:
return TRUE;
case EVT_KEY:
return FALSE;
case EVT_FLIP:
return TRUE;
case EVT_KEYGUARD:
return TRUE;
default:
break;
}
return FALSE; // Event wasn't handled.
}
/*=============================================================================
FUNCTION: GridWidgetSample_DrawScreen
=============================================================================*/
static void GridWidgetSample_DrawScreen(GridWidgetSample * pMe)
{
AEERect rcRoot;
ICanvas *piCanvas;
IWidget *piw, *pisw;
IVectorModel *pivm;
WidgetPos wpos;
WidgetExtent weStatic;
AECHAR pwszText[64] = {0};
int nStrLen = 0;
int nErr;
/* Set Canvas for RootContainer */
SETAEERECT(&(rcRoot), 0, 0, pMe->deviceInfo.cxScreen, pMe->deviceInfo.cyScreen);
nErr = ISHELL_CreateInstance(pMe->piShell, AEECLSID_DISPLAYCANVAS, (void**)&(piCanvas));
if (AEE_SUCCESS != nErr)
{
return;
}
nErr = IDISPLAYCANVAS_SetDisplay((IDisplayCanvas*)(void *)piCanvas, pMe->piDisplay);
if (AEE_SUCCESS != nErr)
{
return;
}
IROOTCONTAINER_SetCanvas(pMe->picRootContainer, piCanvas, &(rcRoot));
RELEASEIF(piCanvas);
nErr = ISHELL_CreateInstance(pMe->piShell, AEECLSID_GRIDWIDGET, (void**)&(piw));
if (AEE_SUCCESS != nErr)
{
return;
}
/* Create the item widget that will be wrapped by the list widget. */
nErr = ISHELL_CreateInstance(pMe->piShell, AEECLSID_STATICWIDGET, (void**)&(pisw));
if (AEE_SUCCESS != nErr)
{
return;
}
/* Identify the value model that is to be associated with this widget. */
nErr = ISHELL_CreateInstance(pMe->piShell, AEECLSID_VECTORMODEL, (void**)&(pivm));
if (AEE_SUCCESS != nErr)
{
return;
}
/* Specify the text to be displayed within the static widget */
nStrLen = ISHELL_LoadResString(pMe->piShell,
GRIDWIDGETSAMPLE_RES_FILE,
IDS_STRING_1001, pwszText, sizeof(pwszText));
if (pwszText)
{
IValueModel *piValueModel = 0;
if (AEE_SUCCESS == IWidget_GetModel(pisw, AEEIID_VALUEMODEL, (IModel **)(void**) &(piValueModel)))
{
IVALUEMODEL_SetText(piValueModel, pwszText, -1);
IValueModel_Release(piValueModel);
}
}
/* Attach the appropriate model to the list. */
nErr = IWidget_SetModel(piw, IVectorModel_to_IModel(pivm));
if (AEE_SUCCESS != nErr)
{
return;
}
/* Wrap the list widget around the item widget, use staticWidget as 'item' widget. */
IDecorator_SetWidget((IDecorator *)(void*) piw, pisw);
IWidget_SetRows(piw, 2);
IWidget_SetCols(piw, 3);
IWidget_GetPreferredExtent(pisw, &weStatic);
/* Set the width and height of the list widget. */
IWidget_SetItemWidth(piw, weStatic.width);
IWidget_SetItemHeight(piw, weStatic.height);
weStatic.width = 200;
weStatic.height = 240;
IWidget_SetExtent(piw, &weStatic);
IWidget_SetBGColor(pisw, MAKE_RGB(0,0,255));
wpos.x = 0;
wpos.y = 0;
wpos.bVisible = TRUE;
IROOTCONTAINER_Insert( pMe->picRootContainer, piw, WIDGET_ZNORMAL, &wpos );
RELEASEIF(piw);
RELEASEIF(pisw);
RELEASEIF(pivm);
}