ListForm & IMenuModel example of use | developer.brewmp.com ListForm & IMenuModel example of use | developer.brewmp.com

Developer

ListForm & IMenuModel example of use

Here is an example a ready code for a list menu. Probably, it will help beginners to understand use of BUIWF. Sorry for my bad english and possible mistakes in the code (it's my first experience in BUIWF).

#define RES_FILE_NAME "resource.bar"

// safe release
#ifndef SAFERELEASE
#define SAFERELEASE(p) _SAFERELEASE((IBase**)&p)
static __inline void _SAFERELEASE(IBase **p) {
if (*p) {
IBASE_Release(*p);
*p = 0;
}

#endif /* SAFERELEASE */

/*
* MainMenu ListForm event handler
*/
static boolean List_MainMenu_eventHandler(void *po, AEEEvent evt, uint16 wParam, uint32 dwParam)
{
AppForm* pMe = (AppForm*) po;
IWidget * pListWidget; // ListWidget

if (evt == EVT_KEY){
if (wParam == AVK_SOFT2) {
// Exit app
ISHELL_CloseApplet(pMe->piShell, FALSE);
return TRUE;
} else if (wParam == AVK_CLR) {
// release MainMenu ListForm
SAFERELEASE(pMe->mainMenu);
}
}

if (evt == EVT_WDG_GETPROPERTY && wParam == FID_PREFRECT) {
//catch FID_PREFRECT and set preferred extent of menu

AEERect* prc= (AEERect*) dwParam;

prc->x = 14;
prc->y = 40;
prc->dx = 150;
prc->dy = 120;

// get ListWidget and set height of ListWidget items
IFORM_GetWidget(pMe->mainMenu, WID_LIST, &pListWidget);
IWIDGET_SetProperty(pListWidget, PROP_ITEMHEIGHT, 24);

// release Widget
SAFERELEASE(pListWidget);

return TRUE;
}

//the default form handler is swapped with the AppForm handler
// calling this allows the default form handler to handle the event
return HANDLERDESC_Call(&pMe->List_MainMenu_HandlerDesc, evt, wParam, dwParam);

/*
* Process menu selection
*/
static void List_MainMenu_selectionHandler(AppForm* pMe, int idOption) {

// pop top Form from RootForm stack
IROOTFORM_PopForm(pMe->rootForm);
// release MainMenu ListForm
SAFERELEASE(pMe->mainMenu);

switch (idOption) {
case 0:
DBGPRINTF("** Option: %d", idOption);
break;
case 1:
DBGPRINTF("** Option: %d", idOption);
break;
default:
DBGPRINTF("** Unknown Option: %d", idOption);
}

/*
* Load string resources
*/
void List_MainMenu_loadMenuItems(AppForm* pMe, IMenuModel* pMenuModel)
{
AECHAR* menuString1 = (AECHAR*) MALLOC(24); // menu item string

// load string from resource file
ISHELL_LoadResString(pMe->piShell, RES_FILE_NAME, SKEY_SELECT, menuString1, 24);

// add menu items
IMENUMODEL_Add(pMenuModel, menuString1, 0, MMF_ENABLED);
IMENUMODEL_Add(pMenuModel, menuString1, 1, MMF_ENABLED);
IMENUMODEL_Add(pMenuModel, menuString1, 2, MMF_ENABLED);
IMENUMODEL_Add(pMenuModel, menuString1, 3, MMF_ENABLED);
IMENUMODEL_Add(pMenuModel, menuString1, 4, MMF_ENABLED);
IMENUMODEL_Add(pMenuModel, menuString1, 5, MMF_ENABLED);
IMENUMODEL_Add(pMenuModel, menuString1, 6, MMF_ENABLED);
IMENUMODEL_Add(pMenuModel, menuString1, 7, MMF_ENABLED);
IMENUMODEL_Add(pMenuModel, menuString1, 8, MMF_ENABLED);
IMENUMODEL_Add(pMenuModel, menuString1, 9, MMF_ENABLED);

/*
* Create Main Menu (ListForm)
*/
int List_MainMenu_create(AppForm* pMe) {

IWidget* pListWidget = NULL; // ListWidget
IWidget* pScrollWidget = NULL; // ScrollWidget
IMenuModel* pMenuModel = NULL; // ListModel (MenuModel)
IWidget* pItemWidget = NULL; // ItemWidget (StaticWidget)

int nErr = 0; // error code
int wProp = 0; // widget properies

// ------------------ Create ListForm ----------------------

// create ListForm instance
nErr = ISHELL_CreateInstance(pMe->piShell, AEECLSID_LISTFORM_SCROLL, (void**) &pMe->mainMenu);

// set Form Title & SoftKeys
nErr = IFORM_SetSoftkeys(pMe->mainMenu, RES_FILE_NAME, SKEY_SELECT, SKEY_EXIT);
nErr = IFORM_SetResText(pMe->mainMenu, FID_TITLE, RES_FILE_NAME, TITLE_MAINMENU);

// ------------------ Set widgets properties ----------------------

// retrieve ListWidget from Form
nErr = IFORM_GetWidget(pMe->mainMenu, WID_LIST, &pListWidget);
// get default ListWidget properties
wProp = 0;
nErr = IWIDGET_GetFlags(pListWidget, &wProp);

// clear flags
wProp &= ~LWF_NOSELECT;
wProp &= ~LWF_WRAP;

// set new ListWidget properties
nErr = IWIDGET_SetFlags(pListWidget, wProp);

IWIDGET_SetProperty(pListWidget, PROP_ITEMHEIGHT, 5);

// retrieve ScrollWidget from Form
nErr = IFORM_GetWidget(pMe->mainMenu, WID_SCROLLBAR, &pScrollWidget);
// get default ScrollWidget properties
wProp = 0;
IWIDGET_GetFlags(pScrollWidget, &wProp);

// set and clear flags
wProp |= SBWF_ARROWS;
wProp &= ~SBWF_NOAUTOHIDE;

// set new ScrollWidget properties
nErr = IWIDGET_SetFlags(pScrollWidget, wProp);

// ------------------ Set Model ----------------------

//create ListModel (IMenuModel)
nErr = ISHELL_CreateInstance(pMe->piShell, AEECLSID_MENUMODEL, (void**)&pMenuModel);
// set callback function for memory release
IMENUMODEL_SetPfnFree(pMenuModel, WidgetAEEFree);
// set model to ListWidget
nErr = IWIDGET_SetModel(pListWidget, (IModel*)pMenuModel);

// ------------------ Load resources to model ----------------------

List_MainMenu_loadMenuItems(pMe, pMenuModel);

// ------------------ Create ItemWidget ----------------------

nErr = ISHELL_CreateInstance(pMe->piShell, AEECLSID_STATICWIDGET, (void**)&pItemWidget);

// ------------------ Set ItemWidget properties ----------------------
// ---------------------- AEEWProperties.h ---------------------------

nErr = IWIDGET_SetBorderWidth(pItemWidget, 1);
nErr += IWIDGET_SetSelectedBorderColor(pItemWidget, MAKE_RGB(0x66, 0xff, 0xff));
nErr += IWIDGET_SetSelectedFGColor(pItemWidget, MAKE_RGBA(0x66, 0x00, 0xCC, 0x80));
nErr += IWIDGET_SetSelectedBGColor(pItemWidget, MAKE_RGBA(0x99, 0xCC, 0xCC, 0x80));
nErr += IWIDGET_SetBorderRadius(pItemWidget, 7);
nErr += IWIDGET_SetBorderStyle(pItemWidget, BORDERSTYLE_ROUNDED);
nErr += IWIDGET_SetFlags(pItemWidget, SWF_NOSHORTENTEXT | IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);

// ------------------ Set Decorator ----------------------

// Wrap ItemWidget into ListWidget
IDECORATOR_SetWidget((IDecorator*)pListWidget, pItemWidget);

// ------------------ Set Handlers ----------------------

// attach selection handler to ListForm
IFORM_SetSelectHandler((IForm*)pMe->mainMenu, (PFNSELECT)List_MainMenu_selectionHandler, pMe);
// init handler structure
HANDLERDESC_Init(&pMe->List_MainMenu_HandlerDesc, List_MainMenu_eventHandler, pMe, 0);
// insert special handler to ListForm
IFORM_SetHandler((IForm*)pMe->mainMenu, &pMe->List_MainMenu_HandlerDesc);

// ------------------ Release all ----------------------

SAFERELEASE(pListWidget);
SAFERELEASE(pScrollWidget);
SAFERELEASE(pMenuModel);
SAFERELEASE(pItemWidget);

return nErr;

Also, I've two questions:

- Correct this example please, if it have the errors.
- Explain me please why we should invoke IROOTFORM_PopForm(pMe->rootForm) in List_MainMenu_selectionHandler and should not do it in List_MainMenu_eventHandler (after CLR key pressed)?

Thanks!

1 Correct this example please, if it have the errors.
Setting the preferred extent - programming error
do it this way
AEERect *prc=(AEERect*) dwParam ;
prc->x= 14;
prc->y = 40;
prc->dx = 150;
prc->dy = 120;
bcoz
*(AEERect*) dwParam = rc;, wont copy the values in members of structure varaible of rc into member variables of structure pointer in dwparam.
else u have to use
MEMCPY((AEERect*) dwParam,&rc,sizeof(AEERect ));
2. Explain me please why we should invoke IROOTFORM_PopForm(pMe->rootForm) in List_MainMenu_selectionHandler and should not do it in List_MainMenu_eventHandler (after CLR key pressed)?
You can use IROOTFORM_PopForm() in both the places (CLR key as wells as
List_MainMenu_selectionHandler). List_MainMenu_selectionHandler is used to handle AVK_SELECT which is send to the list form. The advantage of select handler is that, u get the index which is focussed in the list and there is no need to handle AVK_SELECT in main form event handler. In both cases, u can pop the form using IROOTFORM_PopForm.

1 Correct this example please, if it have the errors.
Setting the preferred extent - programming error
do it this way
AEERect *prc=(AEERect*) dwParam ;
prc->x= 14;
prc->y = 40;
prc->dx = 150;
prc->dy = 120;
bcoz
*(AEERect*) dwParam = rc;, wont copy the values in members of structure varaible of rc into member variables of structure pointer in dwparam.
else u have to use
MEMCPY((AEERect*) dwParam,&rc,sizeof(AEERect ));
2. Explain me please why we should invoke IROOTFORM_PopForm(pMe->rootForm) in List_MainMenu_selectionHandler and should not do it in List_MainMenu_eventHandler (after CLR key pressed)?
You can use IROOTFORM_PopForm() in both the places (CLR key as wells as
List_MainMenu_selectionHandler). List_MainMenu_selectionHandler is used to handle AVK_SELECT which is send to the list form. The advantage of select handler is that, u get the index which is focussed in the list and there is no need to handle AVK_SELECT in main form event handler. In both cases, u can pop the form using IROOTFORM_PopForm.

Thanks! I've corrected the example in a first message.
The "wrong" code I've taken from the Calculator example. :confused:
AEERect rc;
rc.x = 14;
rc.y = 40;
rc.dx = 150;
rc.dy = 120;
*(AEERect*) dwParam = rc;
May be somebody will continue my initiative and will offer the other cognitive examples?

Thanks! I've corrected the example in a first message.
The "wrong" code I've taken from the Calculator example. :confused:
AEERect rc;
rc.x = 14;
rc.y = 40;
rc.dx = 150;
rc.dy = 120;
*(AEERect*) dwParam = rc;
May be somebody will continue my initiative and will offer the other cognitive examples?

Hi,
I am new BUIWF and trying as sample program. I am using this program, able to get the screen with MenuList Items. I am not able to handle the scroll with same program.
Can any one tell me how to sroll up and down in menu list.
regards
:)

Hi,
I am new BUIWF and trying as sample program. I am using this program, able to get the screen with MenuList Items. I am not able to handle the scroll with same program.
Can any one tell me how to sroll up and down in menu list.
regards
:)