home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / flipsrc.sit / main.c < prev   
Encoding:
C/C++ Source or Header  |  1992-05-29  |  4.6 KB  |  197 lines

  1. /* main for Flip A BNDL
  2.  * ⌐ 1992 - Michael S. Engber - All Rights Reserved
  3.  */
  4.  
  5. #include <AppleEvents.h>
  6.  
  7. #include "flab.h"
  8. #include "ierror.h"
  9.  
  10. #define ABOUT_ALERT_ID  129
  11. #define    RESTART_ALRT_ID    128
  12. #define        RESTART_INO    2
  13.  
  14. Boolean done;
  15.  
  16. /* core AppleEvent handlers - only open documents is interesting */
  17.  
  18. static pascal OSErr   AEH_oapp(AppleEvent* ae_p, AppleEvent* reply_p, long refCon){
  19.     OSErr                err;
  20.     Boolean                killFinder;
  21.     StandardFileReply    sfReply;
  22.     SFTypeList            typeList;
  23.     
  24.     done = true;
  25.  
  26.     if(AEInteractWithUser(kAEDefaultTimeout,NULL,NULL)){
  27.         SysBeep(0L);
  28.         return noErr;
  29.     }
  30.  
  31.     Alert(ABOUT_ALERT_ID,NULL);
  32.     
  33.     StandardGetFile(NULL,-1,typeList,&sfReply);
  34.     if(sfReply.sfGood){
  35.         if(FlipBNDL(&sfReply.sfFile) == noErr){
  36.             killFinder = Alert(RESTART_ALRT_ID,NULL) == RESTART_INO;
  37.             if(killFinder && (err=RestartFinder()))
  38.                 return IError(err,"\pAEH_odoc","\pRestartFinder()","\pfailed to kill finder");
  39.         }
  40.     }
  41.     return noErr;
  42. }
  43.  
  44. static pascal OSErr   AEH_pdoc(AppleEvent* ae_p, AppleEvent* reply_p, long refCon){
  45.     done = true;
  46.     return errAEEventNotHandled;
  47. }
  48.  
  49. static pascal OSErr   AEH_quit(AppleEvent* ae_p, AppleEvent* reply_p, long refCon){
  50.     done = true;
  51.     return noErr;
  52. }
  53.  
  54. static pascal OSErr   AEH_odoc(AppleEvent* ae_p, AppleEvent* reply_p, long refCon){
  55.     long        i;
  56.     long        docCount;
  57.     short        purgeCount;
  58.     Size        sz;
  59.     OSErr        err;
  60.     DescType    type;
  61.     AEDescList    docList;
  62.  
  63.     done = true;
  64.     
  65.     /* get the list of docs */
  66.     if(err=AEGetParamDesc(ae_p,keyDirectObject,typeAEList,&docList))
  67.         return IError(err,"\pAEH_odoc","\pAEGetParamDesc()","\pgetting doclist");
  68.     
  69.     /* make sure there are no other parameters */
  70.     switch(err=AEGetAttributePtr(ae_p,keyMissedKeywordAttr,typeWildCard,&type,NULL,0,&sz)){
  71.         case errAEDescNotFound:
  72.             err = noErr;
  73.             break;
  74.         case noErr:
  75.             IError(0,"\pAEH_odoc","\pextra AE attributes",NULL);
  76.             return errAEEventNotHandled;
  77.         default:
  78.             IError(err,"\pAEH_odoc","\pAEGetAttributePtr()",NULL);
  79.             return errAEEventNotHandled;
  80.     }
  81.  
  82.     /* loop through & process the docs */
  83.     if(err=AECountItems(&docList,&docCount)) return err;
  84.     purgeCount = 0;
  85.     for(i=1; i<=docCount; ++i){
  86.         FSSpec    spec;
  87.         AEKeyword    key;
  88.  
  89.         if(    err=AEGetNthPtr(&docList,i,typeFSS,&key,&type,(Ptr)&spec,sizeof(spec),&sz)){
  90.             IError(err,"\pAEH_odoc","\pAEGetNthPtr()",NULL);
  91.             continue;
  92.         }
  93.         
  94.         if(type!=typeFSS){
  95.             IError(i,"\pAEH_odoc","\pNth doclist entry failed coercion to FSSpec",NULL);
  96.             continue;
  97.         }
  98.         
  99.         if(!FlipBNDL(&spec)) ++purgeCount;
  100.     }
  101.  
  102.     if(purgeCount == 0)
  103.         return IError(0,"\pAEH_odoc",
  104.                         "\pno BNDLs processed",
  105.                         "\pcheck that file has BNDL rsrc & BNDL bit set");
  106.  
  107.  
  108.     /* Prompt user to restart the Finder */
  109.     {
  110.         Boolean killFinder;
  111.         
  112.         if(AEInteractWithUser(kAEDefaultTimeout,NULL,NULL)){
  113.             Handle    h = Get1Resource('fKil',0);
  114.             killFinder = h ? (Boolean)(**(short**)h) : false;
  115.             ReleaseResource(h);
  116.         }else{
  117.             killFinder = Alert(RESTART_ALRT_ID,NULL) == RESTART_INO;
  118.         }
  119.  
  120.         if(killFinder && (err=RestartFinder()))
  121.             return IError(err,"\pAEH_odoc","\pRestartFinder()","\pfailed to kill finder");
  122.     }
  123.     
  124.     return noErr;
  125. }
  126.  
  127.  
  128. static Boolean AppInit(void){
  129.     Ptr            newLimit;
  130.     OSErr        err;
  131.     Handle        h;
  132.     SysEnvRec    env;
  133.     
  134.     InitGraf(&thePort);
  135.     InitFonts();
  136.     InitWindows();
  137.     InitMenus();
  138.     TEInit();
  139.     InitDialogs(0L);
  140.             
  141.     /* System 7.0 required */
  142.     if((err=SysEnvirons(1,&env)) || env.systemVersion<0x0700){
  143.         IError(0,"\pAppInit","\pSystem 7.0 or later required",NULL);
  144.         return false;
  145.     }
  146.  
  147.     /* install core AE handlers */
  148.     if(    (err=AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,&AEH_oapp,0L,false)) ||
  149.         (err=AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments  ,&AEH_odoc,0L,false)) ||
  150.         (err=AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments ,&AEH_pdoc,0L,false)) ||
  151.         (err=AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,&AEH_quit,0L,false))){
  152.             IError(err,"\pAppInit","\pAEInstallEventHandler","\pinstalling AE handlers");
  153.             return false;
  154.     }
  155.     
  156.     /* set the stack size (to value in the StSz rsrc) */
  157.     h = Get1Resource('StSz',0);
  158.     newLimit = CurStackBase - (h ? **(long**)h : 24678L);
  159.     ReleaseResource(h);
  160.     SetApplLimit(newLimit);
  161.     MaxApplZone();
  162.  
  163.     /* set up a bare bones menubar - so Balloon Help will be avail for Alerts */
  164.     {
  165.         Handle h = GetNewMBar(1);
  166.         SetMenuBar(h);
  167.         DisposeHandle(h);
  168.         if(h = (Handle)GetMHandle(1)) AddResMenu(h,'DRVR');            
  169.         DrawMenuBar();
  170.     }
  171.     
  172.     done = false;
  173.     return true;
  174. }
  175.  
  176.  
  177. static void EventLoop(void){
  178.     EventRecord curEvent;
  179.     while(!done){
  180.         InitCursor();
  181.         WaitNextEvent(everyEvent,&curEvent,15L,0L);
  182.         switch(curEvent.what){
  183.             case kHighLevelEvent:
  184.                 AEProcessAppleEvent(&curEvent);
  185.                 break;
  186.         }
  187.     }
  188. }
  189.  
  190. void main(void){
  191.     if(AppInit()){
  192.         EventLoop();
  193.     }
  194.     
  195.     ExitToShell();
  196. }
  197.