Sunday, January 28, 2007

Creating a dynamic form in Palm OS

Code below shows how to create a dynamic form for Palm OS 3.0 or greater. See this link of the entire project created using Palm OS Development Suite.

/****************************************************************************** * Application Name : DynamicUI.prc * Purpose : Demonstrate how to create a dynamic UI * Programmed by : Techno Scavenger *****************************************************************************/ #include <PalmOS.h> /*********************************************************************** * * Internal Constants * ***********************************************************************/ #define appFileCreator 'ewa8' #define appVersionNum 0x01 #define appPrefID 0x00 #define appPrefVersionNum 0x01 /*********************************************************************** * * Application constants * ***********************************************************************/ #define MainForm 1000 /*********************************************************************** * FUNCTION: MainFormHandleEvent * DESCRIPTION: This routine is the event handler for the * "MainForm" of this application. * PARAMETERS: pEvent - a pointer to an EventType structure * RETURNED: true if the event has handle and should not be passed * to a higher level handler. * REVISION HISTORY: ***********************************************************************/ static Boolean MainFormHandleEvent(EventType* pEvent) { Boolean handled = false; FormType* pForm; switch (pEvent->eType) { case frmOpenEvent: pForm = FrmGetActiveForm(); FrmDrawForm(pForm); handled = true; break; default: break; } return handled; } /*********************************************************************** * FUNCTION: AppHandleEvent * DESCRIPTION: This routine loads form resources and set the event * handler for the form loaded. * PARAMETERS: event - a pointer to an EventType structure * RETURNED: true if the event has handle and should not be passed * to a higher level handler. * REVISION HISTORY: ***********************************************************************/ static Boolean AppHandleEvent(EventType* pEvent) { UInt16 formId; FormType* pForm; Boolean handled = false; if (pEvent->eType == frmLoadEvent) { formId = pEvent->data.frmLoad.formID; // pForm = FrmInitForm(formId); pForm = FrmGetFormPtr(MainForm); FrmSetActiveForm(pForm); // Set the event handler for the form. The handler of the currently // active form is called by FrmHandleEvent each time is receives an // event. switch (formId) { case MainForm: FrmSetEventHandler(pForm, MainFormHandleEvent); break; default: break; } handled = true; } return handled; } /*********************************************************************** * FUNCTION: AppEventLoop * DESCRIPTION: This routine is the event loop for the application. * PARAMETERS: nothing * RETURNED: nothing * REVISION HISTORY: ***********************************************************************/ static void AppEventLoop(void) { Err error; EventType event; do { EvtGetEvent(&event, evtWaitForever); if (SysHandleEvent(&event)) continue; if (MenuHandleEvent(0, &event, &error)) continue; if (AppHandleEvent(&event)) continue; FrmDispatchEvent(&event); } while (event.eType != appStopEvent); } /*********************************************************************** * FUNCTION: AppStart * DESCRIPTION: Get the current application's preferences. * PARAMETERS: nothing * RETURNED: Err value errNone if nothing went wrong * REVISION HISTORY: ***********************************************************************/ static Err AppStart(void) { // FrmGotoForm(MainForm); static Char strTitle[13] = "Dynamic Form"; FormType* pMainForm; pMainForm = FrmNewForm( MainForm, strTitle, 0, 0, 160, 160, false, 0, 0, 0); FrmGotoForm(MainForm); return errNone; } /*********************************************************************** * FUNCTION: AppStop * DESCRIPTION: Save the current state of the application. * PARAMETERS: nothing * RETURNED: nothing * REVISION HISTORY: ***********************************************************************/ static void AppStop(void) { // Close all the open forms. FrmCloseAllForms(); } /*********************************************************************** * FUNCTION: PilotMain * DESCRIPTION: This is the main entry point for the application. * PARAMETERS: cmd - word value specifying the launch code. * cmdPB - pointer to a structure that is associated with the launch code. * launchFlags - word value providing extra information about the launch. * RETURNED: Result of launch * REVISION HISTORY: ***********************************************************************/ UInt32 PilotMain(UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags) { Err error = errNone; switch (cmd) { case sysAppLaunchCmdNormalLaunch: if ((error = AppStart()) == 0) { AppEventLoop(); AppStop(); } break; default: break; } return error; }

No comments: