home *** CD-ROM | disk | FTP | other *** search
- /* main for Flip A BNDL
- * ⌐ 1992 - Michael S. Engber - All Rights Reserved
- */
-
- #include <AppleEvents.h>
-
- #include "flab.h"
- #include "ierror.h"
-
- #define ABOUT_ALERT_ID 129
- #define RESTART_ALRT_ID 128
- #define RESTART_INO 2
-
- Boolean done;
-
- /* core AppleEvent handlers - only open documents is interesting */
-
- static pascal OSErr AEH_oapp(AppleEvent* ae_p, AppleEvent* reply_p, long refCon){
- OSErr err;
- Boolean killFinder;
- StandardFileReply sfReply;
- SFTypeList typeList;
-
- done = true;
-
- if(AEInteractWithUser(kAEDefaultTimeout,NULL,NULL)){
- SysBeep(0L);
- return noErr;
- }
-
- Alert(ABOUT_ALERT_ID,NULL);
-
- StandardGetFile(NULL,-1,typeList,&sfReply);
- if(sfReply.sfGood){
- if(FlipBNDL(&sfReply.sfFile) == noErr){
- killFinder = Alert(RESTART_ALRT_ID,NULL) == RESTART_INO;
- if(killFinder && (err=RestartFinder()))
- return IError(err,"\pAEH_odoc","\pRestartFinder()","\pfailed to kill finder");
- }
- }
- return noErr;
- }
-
- static pascal OSErr AEH_pdoc(AppleEvent* ae_p, AppleEvent* reply_p, long refCon){
- done = true;
- return errAEEventNotHandled;
- }
-
- static pascal OSErr AEH_quit(AppleEvent* ae_p, AppleEvent* reply_p, long refCon){
- done = true;
- return noErr;
- }
-
- static pascal OSErr AEH_odoc(AppleEvent* ae_p, AppleEvent* reply_p, long refCon){
- long i;
- long docCount;
- short purgeCount;
- Size sz;
- OSErr err;
- DescType type;
- AEDescList docList;
-
- done = true;
-
- /* get the list of docs */
- if(err=AEGetParamDesc(ae_p,keyDirectObject,typeAEList,&docList))
- return IError(err,"\pAEH_odoc","\pAEGetParamDesc()","\pgetting doclist");
-
- /* make sure there are no other parameters */
- switch(err=AEGetAttributePtr(ae_p,keyMissedKeywordAttr,typeWildCard,&type,NULL,0,&sz)){
- case errAEDescNotFound:
- err = noErr;
- break;
- case noErr:
- IError(0,"\pAEH_odoc","\pextra AE attributes",NULL);
- return errAEEventNotHandled;
- default:
- IError(err,"\pAEH_odoc","\pAEGetAttributePtr()",NULL);
- return errAEEventNotHandled;
- }
-
- /* loop through & process the docs */
- if(err=AECountItems(&docList,&docCount)) return err;
- purgeCount = 0;
- for(i=1; i<=docCount; ++i){
- FSSpec spec;
- AEKeyword key;
-
- if( err=AEGetNthPtr(&docList,i,typeFSS,&key,&type,(Ptr)&spec,sizeof(spec),&sz)){
- IError(err,"\pAEH_odoc","\pAEGetNthPtr()",NULL);
- continue;
- }
-
- if(type!=typeFSS){
- IError(i,"\pAEH_odoc","\pNth doclist entry failed coercion to FSSpec",NULL);
- continue;
- }
-
- if(!FlipBNDL(&spec)) ++purgeCount;
- }
-
- if(purgeCount == 0)
- return IError(0,"\pAEH_odoc",
- "\pno BNDLs processed",
- "\pcheck that file has BNDL rsrc & BNDL bit set");
-
-
- /* Prompt user to restart the Finder */
- {
- Boolean killFinder;
-
- if(AEInteractWithUser(kAEDefaultTimeout,NULL,NULL)){
- Handle h = Get1Resource('fKil',0);
- killFinder = h ? (Boolean)(**(short**)h) : false;
- ReleaseResource(h);
- }else{
- killFinder = Alert(RESTART_ALRT_ID,NULL) == RESTART_INO;
- }
-
- if(killFinder && (err=RestartFinder()))
- return IError(err,"\pAEH_odoc","\pRestartFinder()","\pfailed to kill finder");
- }
-
- return noErr;
- }
-
-
- static Boolean AppInit(void){
- Ptr newLimit;
- OSErr err;
- Handle h;
- SysEnvRec env;
-
- InitGraf(&thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
-
- /* System 7.0 required */
- if((err=SysEnvirons(1,&env)) || env.systemVersion<0x0700){
- IError(0,"\pAppInit","\pSystem 7.0 or later required",NULL);
- return false;
- }
-
- /* install core AE handlers */
- if( (err=AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,&AEH_oapp,0L,false)) ||
- (err=AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments ,&AEH_odoc,0L,false)) ||
- (err=AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments ,&AEH_pdoc,0L,false)) ||
- (err=AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,&AEH_quit,0L,false))){
- IError(err,"\pAppInit","\pAEInstallEventHandler","\pinstalling AE handlers");
- return false;
- }
-
- /* set the stack size (to value in the StSz rsrc) */
- h = Get1Resource('StSz',0);
- newLimit = CurStackBase - (h ? **(long**)h : 24678L);
- ReleaseResource(h);
- SetApplLimit(newLimit);
- MaxApplZone();
-
- /* set up a bare bones menubar - so Balloon Help will be avail for Alerts */
- {
- Handle h = GetNewMBar(1);
- SetMenuBar(h);
- DisposeHandle(h);
- if(h = (Handle)GetMHandle(1)) AddResMenu(h,'DRVR');
- DrawMenuBar();
- }
-
- done = false;
- return true;
- }
-
-
- static void EventLoop(void){
- EventRecord curEvent;
- while(!done){
- InitCursor();
- WaitNextEvent(everyEvent,&curEvent,15L,0L);
- switch(curEvent.what){
- case kHighLevelEvent:
- AEProcessAppleEvent(&curEvent);
- break;
- }
- }
- }
-
- void main(void){
- if(AppInit()){
- EventLoop();
- }
-
- ExitToShell();
- }
-