Application frame

«*»= #include <Pilot.h> #include "callback.h" #include <Common.h> #include <System/SysAll.h> #include <UI/UIAll.h> #include <UI/Font.h>

#include "mapperRsc.h" #include "mapper.h"

Boolean MainFormHandleEvent (EventPtr); Boolean EditLocFormHandleEvent (EventPtr); Boolean EditTextFormHandleEvent (EventPtr); Boolean SelectMapHandleEvent (EventPtr); Boolean AboutFormHandleEvent (EventPtr); Boolean ConnectionParamsHandleEvent (EventPtr e); Boolean LocParamsHandleEvent (EventPtr e); Boolean TextParamsHandleEvent (EventPtr e); Boolean SelectDumpHandleEvent (EventPtr e);

extern Map *currentMap; extern Section *clipboard; extern int section; extern Section *currentSection; extern Boolean modified; extern NameListMem MapList;

Prefs prefs= true, false, true, false, false, 2 ;

static Boolean ApplicationHandleEvent(EventPtr e) FormPtr frm; Word formId; Boolean handled = false;

if(e->eType == frmLoadEvent) formId = e->data.frmLoad.formID; frm = FrmInitForm(formId); FrmSetActiveForm(frm);

switch(formId) case MainForm: FrmSetEventHandler(frm, MainFormHandleEvent); break; case EditLocForm: FrmSetEventHandler(frm, EditLocFormHandleEvent); break; case MapSelect: FrmSetEventHandler(frm,SelectMapHandleEvent); break; case AboutForm: FrmSetEventHandler(frm,AboutFormHandleEvent); break; case EditTextForm: FrmSetEventHandler(frm, EditTextFormHandleEvent); break; case ConnectionParams: FrmSetEventHandler(frm, ConnectionParamsHandleEvent); break; case LocParams: FrmSetEventHandler(frm, LocParamsHandleEvent); break; case TextParams: FrmSetEventHandler(frm, TextParamsHandleEvent); break; case ImportSelect: FrmSetEventHandler(frm, SelectDumpHandleEvent); break; handled = true;

return handled;

#define IFMAPPERSTAT "IFStatus" extern Byte mode;

void SaveStatus() LocalID id; DmOpenRef statDB; UInt loc=0; char vbuf[32]; VoidHand RecHandle; Ptr RecPointer;

if(currentMap != NULL) StrPrintF(vbuf,"else vbuf[0]='';

if((id=DmFindDatabase(0,IFMAPPERSTAT)) != 0) DmDeleteDatabase(0,id);

if(DmCreateDatabase(0,IFMAPPERSTAT,APPID,'Data', false)) return;

id=DmFindDatabase(0,IFMAPPERSTAT); statDB=DmOpenDatabase(0,id,dmModeReadWrite);

RecHandle=DmNewRecord(statDB,&loc,sizeof(Byte)); RecPointer=MemHandleLock(RecHandle); DmWrite(RecPointer,0, &mode, sizeof(Byte));

MemHandleUnlock(RecHandle); DmReleaseRecord(statDB,loc++,true);

RecHandle=DmNewRecord(statDB,&loc,StrLen(vbuf)+1); RecPointer=MemHandleLock(RecHandle);

DmWrite(RecPointer,0, vbuf, StrLen(vbuf)+1);

MemHandleUnlock(RecHandle); DmReleaseRecord(statDB,loc++,true);

RecHandle=DmNewRecord(statDB,&loc,sizeof(int)); RecPointer=MemHandleLock(RecHandle); DmWrite(RecPointer,0, &section, sizeof(int));

MemHandleUnlock(RecHandle); DmReleaseRecord(statDB,loc++,true);

RecHandle=DmNewRecord(statDB,&loc,sizeof(Prefs)); RecPointer=MemHandleLock(RecHandle); DmWrite(RecPointer,0, &prefs, sizeof(Prefs)); MemHandleUnlock(RecHandle); DmReleaseRecord(statDB,loc++,true);

saveSection(clipboard,statDB,loc);

DmCloseDatabase(statDB);

void ReadStatus() LocalID id; DmOpenRef statDB; char *name; VoidHand RecHandle; Ptr RecPointer; UInt loc=0;

currentMap=NULL; currentSection=NULL;

if((id=DmFindDatabase(0,IFMAPPERSTAT)) == 0) mode=EDIT_ITEM; return;

statDB=DmOpenDatabase(0,id,dmModeReadWrite);

RecHandle=DmGetRecord(statDB,loc); RecPointer=MemHandleLock(RecHandle); mode=*RecPointer; MemHandleUnlock(RecHandle); DmReleaseRecord(statDB,loc++,true);

RecHandle=DmGetRecord(statDB,loc); RecPointer=MemHandleLock(RecHandle); name=RecPointer;

if(StrLen(name)>0) currentMap=LoadMap(name);

MemHandleUnlock(RecHandle); DmReleaseRecord(statDB,loc++,true);

RecHandle=DmGetRecord(statDB,loc); RecPointer=MemHandleLock(RecHandle); section=*(int *)RecPointer; if(currentMap != NULL) currentSection=&currentMap->sections[section]; MemHandleUnlock(RecHandle); DmReleaseRecord(statDB,loc++,true);

RecHandle=DmGetRecord(statDB,loc); RecPointer=MemHandleLock(RecHandle); MemMove(&prefs,RecPointer,sizeof(Prefs)); MemHandleUnlock(RecHandle); DmReleaseRecord(statDB,loc++,true);

LoadSection(statDB,clipboard,loc); DmCloseDatabase(statDB);

#define FONTID 129

VoidHand fhnd;

/* Get preferences, open (or create) app database */ static Word StartApplication(void) FontPtr font;

if((fhnd = DmGetResource('NFNT', 129)) == NULL) ErrFatalDisplay("can't get font resource"); if((font = MemHandleLock(fhnd)) == NULL) ErrFatalDisplay("can't lock font memory"); if(FntDefineFont(FONTID, font)) ErrFatalDisplay("can't define font");

clipboard=MemPtrNew(sizeof(Section)); clipboard->itemNum=0; clipboard->items=NULL; clipboard->Name=NULL; ReadStatus();

if(clipboard->Name == NULL) clipboard->Name=MemPtrNew(5); StrCopy(clipboard->Name,"Clip");

#ifdef DEBUG if(DmFindDatabase(0,"IFLog") == 0) DmCreateDatabase(0,"IFLog",APPID,'IFlg', false); #endif

if(currentMap) FrmGotoForm(MainForm); else FrmGotoForm(MapSelect); return 0;

/* Save preferences, close forms, close app database */ static void StopApplication(void) SaveStatus();

if(currentMap != NULL) if(modified) saveMap(currentMap); FreeMap(currentMap); currentMap=NULL;

FreeSection(clipboard);

MemHandleUnlock(fhnd); FreeNameListMem(&MapList); FrmSaveAllForms(); FrmCloseAllForms();

static void normalHandleEvent(EventType e) Word err;

if (! SysHandleEvent (&e)) if (! MenuHandleEvent (NULL, &e, &err)) if (! ApplicationHandleEvent (&e)) FrmDispatchEvent (&e);

/* The main event loop */ static void EventLoop(void) EventType e;

do EvtGetEvent(&e, 50); if(currentMap != NULL && e.eType==keyDownEvent) if(!FrmDispatchEvent (&e)) normalHandleEvent(e); else normalHandleEvent(e); while (e.eType != appStopEvent);

/* Main entry point; it is unlikely you will need to change this except to handle other launch command codes */ DWord PilotMain(Word cmd, Ptr cmdPBP, Word launchFlags) Word err;

if (cmd == sysAppLaunchCmdNormalLaunch) err = StartApplication(); if (err) return err;

EventLoop(); StopApplication(); else return sysErrParamErr;

return 0; @