Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 5 Compiling with resources Rob Pooley

Similar presentations


Presentation on theme: "Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 5 Compiling with resources Rob Pooley"— Presentation transcript:

1 Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 5 Compiling with resources Rob Pooley rjp@macs.hw.ac.uk

2 Programming Handheld and Mobile devices 2 Standard Make In the standard make project wizards, Developer Suite provides a generic set of makefiles that you can modify and tailor for your specific application build. The standard make makefiles are user-defined and user-controlled. You have to manually update when you make changes to your project (adding files, removing files, changing the output, etc.). Developer Suite essentially invokes make to build the standard make projects. You also have the option of not using the Developer Suite supplied makefiles, but the you must modify your makefiles to meet certain Developer Suite requirements. Developer Suite requires that the following variables be defined in the makefile:

3 Programming Handheld and Mobile devices 3 AppStart() and AppStop() static Err AppStart(void) { return errNone; } static void AppStop(void) { FrmCloseAllForms(); }

4 Programming Handheld and Mobile devices 4 The event loop static void AppEventLoop(void) { Err error; EventType event; do { EvtGetEvent(&event, evtWaitForever); if (! SysHandleEvent(&event)) if (! MenuHandleEvent(0, &event, &error)) if (! AppHandleEvent(&event)) FrmDispatchEvent(&event); } while (event.eType != appStopEvent); }

5 Programming Handheld and Mobile devices 5 Event handler static Boolean AppHandleEvent(EventPtr event) { UInt16 formId; FormPtr form; if (event->eType == frmLoadEvent) { // Load the form resource. formId = event->data.frmLoad.formID; form = FrmInitForm(formId); ErrFatalDisplayIf(!form, "Can't initialize form"); FrmSetActiveForm(form); // 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(form, MainFormHandleEvent); break; case SecondForm: FrmSetEventHandler(form, SecondFormHandleEvent); break; default: ErrFatalDisplay("Invalid Form Load Event"); break; } return true; } else return false; }

6 Programming Handheld and Mobile devices 6 Header /* Copyright (c) 2000-2001, Neil Rhodes and Julie McKeehan neil@pobox.com All rights reserved. From the book "Palm Programming, the Developer's Guide, 2nd edition" by O'Reilly. Permission granted to use this file however you see fit. */ #define DO_NOT_ALLOW_ACCESS_TO_INTERNALS_OF_STRU CTS #include #ifdef DEBUG_BUILD #define ERROR_CHECK_LEVEL ERROR_CHECK_FULL #endif #include #include "ResourceDefines.h" #include "MainForm.h" #include "SecondForm.h" #include "Utils.h" #include "Constants.h" ResourceDefines.h #define RomIncompatibleAlert 1001 #define MainForm 2000 #define MainBeepButton 2001 #define MainGotoSecondFormButton 2002 #define SecondForm 3000 #define SecondGotoMainFormButton 3001

7 Programming Handheld and Mobile devices 7 Main form static void MainFormDeinit(FormPtr form) { #pragma unused(form) } Boolean MainFormHandleEvent(EventPtr event) { Boolean handled = false; FormPtr form; switch (event->eType) { case frmOpenEvent: form = FrmGetActiveForm(); MainFormInit(form); FrmDrawForm(form); // here's where you'd add a call to FrmSetFocus handled = true; break; case ctlSelectEvent: switch (event- >data.ctlSelect.controlID) { case MainBeepButton: SndPlaySystemSound(sndWarning); handled = true; break; case MainGotoSecondFormButton: FrmGotoForm(SecondForm); handled = true; break; } break; case frmCloseEvent: MainFormDeinit(FrmGetActiveForm()); handled = false; break; default: break; } return handled; }

8 Programming Handheld and Mobile devices 8 Second form static void SecondFormInit(FormPtr form) { #pragma unused(form) // warning-- don't do any drawing in this routine. // Also, don't call FrmSetFocus from here (it must be called *after* // FrmDrawForm) } static void SecondFormDeinit(FormPtr form) { #pragma unused(form) } Boolean SecondFormHandleEvent(EventPtr event) { Boolean handled = false; FormPtr form; switch (event->eType) { case frmOpenEvent: form = FrmGetActiveForm(); SecondFormInit(form); FrmDrawForm(form); // here's where you'd add a call to FrmSetFocus handled = true; break; case ctlSelectEvent: switch (event- >data.ctlSelect.controlID) { case SecondGotoMainFormButton: FrmGotoForm(MainForm); handled = true; break; } break; case frmCloseEvent: SecondFormDeinit(FrmGetActiveForm()); handled = false; break; default: break; } return handled; }

9 Programming Handheld and Mobile devices 9 Headers MainForm.h #ifndef MAINFORM_H #define MAINFORM_H Boolean MainFormHandleEvent(EventPtr eventP); #endif SecondForm.h #ifndef SECONDFORM_H #define SECONDFORM_H Boolean SecondFormHandleEvent(EventPtr eventP); #endif

10 Programming Handheld and Mobile devices 10 Defining the resources used Resources.rcp #include "ResourceDefines.h" APPLICATIONICONNAME 1000 "Starter" ALERT ID RomIncompatibleAlert CONFIRMATION BEGIN TITLE "System Incompatible" MESSAGE "System Version 3.0 or greater " \ "is required to run this application." BUTTONS "OK" END FORM ID MainForm AT (0 0 160 160) USABLE BEGIN TITLE "OReilly Starter Main" BUTTON "Beep" ID MainBeepButton AT (40 100 AUTO AUTO) BUTTON "Goto Second Form" ID MainGotoSecondFormButton AT (PrevLeft PrevBottom + 5 AUTO AUTO) END FORM ID SecondForm AT (0 0 160 160) USABLE BEGIN TITLE "OReilly Starter Second" BUTTON "Goto Main Form" ID SecondGotoMainFormButton AT (40 130 AUTO AUTO) END

11 Programming Handheld and Mobile devices 11 Palm’s simple example - PilotMain uint32_t PilotMain(uint16_t cmd, MemPtr cmdPBP, uint16_t launchFlags) { // OS6NEW: New error type status_t error = errNone; // OS6NEW: Get app database ref - needed for Form Mgr calls, resources. if ((error = SysGetModuleDatabase(SysGetRefNum(), NULL, &gAppDB)) < errNone) return error; // Handle launch code switch (cmd) { case sysAppLaunchCmdNormalLaunch: // Perform app initialization. error = AppStart(); if (error) return error; // OS6NEW: FrmGotoForm() now requires app db ref argument FrmGotoForm(gAppDB, MainForm); // Handle events until user switches to another app. AppEventLoop(); // Clean up before exit. AppStop(); break; default: break; } return errNone; }

12 Programming Handheld and Mobile devices 12 AppStart() static status_t AppStart(void) { #if 0 AppPrefsType prefs; // OS6NEW: PrefGetAppPreferences() size is now 32-bit uint32_tprefsSize; // Read the saved preferences / saved-state information. prefsSize = sizeof(AppPrefsType); if (PrefGetAppPreferences(kAppCreatorID, kAppPrefsID, &prefs, &prefsSize, kSyncedPrefsDB) != noPreferenceFound) { // Init globals, etc. } #endif return errNone; }

13 Programming Handheld and Mobile devices 13 AppEventLoop() static void AppEventLoop(void) { status_terror; EventTypeevent; do { EvtGetEvent(&event, evtWaitForever); if (SysHandleEvent(&event)) continue; //OS6NEW: error codes are now status_t (32 bit), not Err (16 bit) if (MenuHandleEvent(0, &event, &error)) continue; if (AppHandleEvent(&event)) continue; FrmDispatchEvent(&event); } while (event.eType != appStopEvent); }

14 Programming Handheld and Mobile devices 14 AppStop() static void AppStop(void) { #if 0 AppPrefsType prefs; // Write the saved preferences/saved-state information. This // data will be saved during a HotSync backup. PrefSetAppPreferences(kAppCreatorID,kAppPrefsID, kAppPrefsVersion, &prefs, sizeof(prefs), kSyncedPrefsDB); #endif // Close all the open forms. FrmCloseAllForms(); }

15 Programming Handheld and Mobile devices 15 AppHandleEvent() static Boolean AppHandleEvent(EventType* pEvent) { uint16_t formId; FormType* pForm; if (pEvent->eType == frmLoadEvent) { // Load the form resource. formId = pEvent->data.frmLoad.formID; // OS6NEW: FrmInitForm() needs app db ptr as first argument pForm = FrmInitForm(gAppDB, formId); 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); FrmInitLayout(pForm, gMainFormLayout); break; case Form2Form: FrmSetEventHandler(pForm, Form2FormHandleEvent); FrmInitLayout(pForm, gForm2FormLayout); break; default: ErrFatalDisplay("Invalid Form Load Event"); break; } return true; } return false; }

16 Programming Handheld and Mobile devices 16 Some references The O’Reilly example is available online at: –www.oreillynet.comwww.oreillynet.com


Download ppt "Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 5 Compiling with resources Rob Pooley"

Similar presentations


Ads by Google