home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Applications / ZX Loader 1.0.4 / ZX Loader source / Eventloop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-26  |  9.9 KB  |  309 lines  |  [TEXT/MMCC]

  1.  
  2. /* ----- PROTOTYPE APPLICATION --- KIO ! --- 20.11.93 ----
  3.     Copyright (C) 1993-1994 G.Woigk
  4.     
  5.     This file is part of the ZX LOADER program and it is free software
  6.     See Application.c for details
  7.             
  8.     This program is distributed in the hope that it will be useful,
  9.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  11.  
  12.     22.06.94:    removed bug in handling required AppleEvent 'quit'
  13. */
  14.  
  15. #include     <AppleEvents.h>
  16. #include     <GestaltEqu.h> 
  17. #include     <Menus.h>
  18. #include     <Dialogs.h>
  19. #include    <Events.h>
  20. #include    "kio.h"
  21. #include    "mem.h"
  22. #include    "application.h"
  23. #include    "eventloop.h"
  24. #include    "console.h"
  25.  
  26.  
  27. // ----- Standarddialogs -----------------------------------------------------
  28.  
  29. short aboutID    = 660;    // 'ALRT' resourceID: DoAbout
  30. short helpID    = 661;    // 'ALRT' resourceID: DoHelp
  31. short alertID     = 666;    // 'ALRT' resourceID: DoAbort,DoAlert
  32. short warningID    = 667;    // 'ALRT' resourceID: DoWarning
  33. short messageID    = 668;    // 'ALRT' resourceID: DoMessage
  34. short confirmID = 669;    // 'ALRT' resourceID: DoConfirm
  35. short requestID = 670;    // 'ALRT' resourceID: DoRequest
  36.  
  37. // ----- Event Tracking -------------------------------------------------------
  38.  
  39. extern    EventRecord    event;            // event returned by WaitNextEvent
  40. extern    Boolean        done;            // break flag for wait next event loop
  41. extern    long        eventDelay;    
  42. extern    RgnHandle    eventRegion;
  43. Boolean    oApp = FALSE;                // HandleINIT() already called?
  44. short    eventMask = everyEvent;        // 
  45.  
  46.  
  47. // ----- prototypes ----------------------------------------------------------
  48. short DoBox ( short id, Str255 s, Str255 t, Str255 u );
  49. OSErr MyGotRequiredParams ( AppleEvent *theAppleEvent );
  50. pascal OSErr HandleOpenAPP(AppleEvent *eventIn, AppleEvent *eventOut, long refCon);
  51. pascal OSErr HandleQuitAPP(AppleEvent *eventIn, AppleEvent *eventOut, long refCon);
  52. OSErr OpenAppl (AppleEvent *eventIn, long how);
  53. pascal OSErr HandleOpenDOC(AppleEvent *eventIn, AppleEvent *eventOut, long refCon);
  54. pascal OSErr HandlePrintDOC(AppleEvent *eventIn, AppleEvent *eventOut, long refCon);
  55. InitToolBox();
  56. InitHighLevelEvents();
  57. HandleFileMenu(long itemNum);
  58. HandleFileMenu(long itemNum);
  59. HandleAppleMenu(long itemNum);
  60. HandleMenuSelection(long menuResult);
  61. HandleMouseDown();
  62. HandleEditMenu(long itemNum);
  63.  
  64.  
  65. /* ***** STANDARD DIALOGS ***************************************** */
  66.  
  67. short DoBox ( short id, Str255 s, Str255 t, Str255 u )
  68. {    long n;
  69.     ParamText(s,t,u,0);
  70.     n = Alert ( id, nil );
  71.     if (n>0) return(n);
  72.     if (id!=alertID) DoAbort("\pEine 'ALRT' Resource fehlt!","\p","\p");
  73.     SysBeep(20);SysBeep(20);SysBeep(20);SysBeep(20);
  74.     ExitToShell();
  75. }
  76.  
  77. DoAlert   ( Str255 s, Str255 t, Str255 u ) { DoBox (alertID, s,t,u); }
  78. DoWarning ( Str255 s, Str255 t, Str255 u ) { DoBox (warningID, s,t,u); }
  79. DoMessage ( Str255 s, Str255 t, Str255 u ) { DoBox (messageID, s,t,u); }
  80. short   DoConfirm ( Str255 s, Str255 t, Str255 u ) { return DoBox ( confirmID, s,t,u ); }
  81. short   DoRequest ( Str255 s, Str255 t, Str255 u ) { return DoBox ( requestID, s,t,u ); }
  82. DoAbort   ( Str255 s, Str255 t, Str255 u ) { DoAlert (s,t,u); ExitToShell(); }
  83.  
  84.  
  85. // ***** High Level Apple Event **************************************** 
  86.  
  87. OSErr MyGotRequiredParams ( AppleEvent *theAppleEvent )
  88. {    DescType    returnedType;
  89.     Size        actualSize;
  90.     OSErr        err;
  91.  
  92.     err = AEGetAttributePtr ( theAppleEvent, keyMissedKeywordAttr, typeWildCard, 
  93.                               &returnedType, nil, 0, &actualSize);
  94.     if (err==errAEDescNotFound)    return noErr;    // you got all the required parameters
  95.     if (err==noErr) return errAEParamMissed;    // you missed a required parameter    
  96.     return err;                                    // the call to AEGetAttributePtr failed
  97. }
  98.  
  99. pascal OSErr HandleOpenAPP(AppleEvent *eventIn, AppleEvent *eventOut, long refCon)
  100. {    OSErr    err;
  101.     err = MyGotRequiredParams ( eventIn ); 
  102.     if (!err && !oApp) HandleINIT('OAPP'), oApp=TRUE;
  103.     return err; 
  104. }
  105.  
  106. pascal OSErr HandleQuitAPP(AppleEvent *eventIn, AppleEvent *eventOut, long refCon)
  107. {    OSErr    err;
  108.     err = MyGotRequiredParams ( eventIn ); 
  109.     if (!err) HandleQUIT(); 
  110.     return err;    
  111. }
  112.  
  113. OSErr OpenAppl (AppleEvent *eventIn, long how)
  114. {    FSSpec        file;
  115.     AEDescList    docList;
  116.     OSErr        err;
  117.     long        index, itemsInList;
  118.     Size        actualSize;
  119.     AEKeyword    keywd;
  120.     DescType    returnedType;
  121.     err = AEGetParamDesc ( eventIn, keyDirectObject, typeAEList, &docList );
  122.     if (err) return err; 
  123.     if (!oApp) HandleINIT('ODOC'), oApp=TRUE;
  124.     err = MyGotRequiredParams ( eventIn );        
  125.     if (!err) err = AECountItems ( &docList, &itemsInList );
  126.     for (index=1; index<=itemsInList && !err; index++) 
  127.     {    err = AEGetNthPtr ( &docList, index, typeFSS, &keywd, &returnedType, 
  128.                             (Ptr)&file, sizeof(file), &actualSize );
  129.         if (!err) HandleOPEN ( file );
  130.         if (!err && how=='PDOC') 
  131.         {    HandlePRINT ();
  132.             HandleCLOSE ();
  133.         }
  134.     }
  135.     AEDisposeDesc(&docList);
  136.     return err;
  137. }
  138.  
  139. pascal OSErr HandleOpenDOC(AppleEvent *eventIn, AppleEvent *eventOut, long refCon)
  140. {    return OpenAppl ( eventIn, 'ODOC' );  }
  141.  
  142. pascal OSErr HandlePrintDOC(AppleEvent *eventIn, AppleEvent *eventOut, long refCon)
  143. {    return OpenAppl ( eventIn, 'PDOC' );  }
  144.  
  145.  
  146. /* ***** INITIALIZATIONS **************************************** */
  147.  
  148. InitToolBox()
  149. {    
  150.     InitGraf(&qd.thePort);    InitFonts();        InitWindows();
  151.     InitMenus();            TEInit();            InitDialogs(nil);
  152.     InitCursor();
  153. }
  154.  
  155.  
  156. #if GENERATINGPOWERPC
  157. #define    AEH(X)    NewAEEventHandlerProc(X)
  158. #else
  159. #define    AEH(X)    X
  160. #endif
  161.  
  162.  
  163. InitHighLevelEvents()
  164. {    long feature;
  165.     if ( Gestalt(gestaltAppleEventsAttr, &feature) != noErr ) 
  166.         DoAbort("\pThis program requires System 7","\p(Gestalt)","\p");
  167.     if ( !( feature & (1L<<gestaltAppleEventsPresent) ) )        
  168.         DoAbort("\pThis program requires System 7","\p(AppleEvents)","\p");
  169.     if ( AEInstallEventHandler ( kCoreEventClass,'oapp',AEH(HandleOpenAPP),0L,false ) == noErr )
  170.     if ( AEInstallEventHandler ( kCoreEventClass,'odoc',AEH(HandleOpenDOC),0L,false ) == noErr )
  171.     if ( AEInstallEventHandler ( kCoreEventClass,'pdoc',AEH(HandlePrintDOC),0L,false ) == noErr )
  172.     if ( AEInstallEventHandler ( kCoreEventClass,'quit',AEH(HandleQuitAPP),0L,false ) == noErr )
  173.         return;
  174.     DoAbort("\pFailed to install an AppleEvent handler","\p","\p");
  175. }
  176.  
  177.  
  178. // ***** THE MAIN EVENT LOOP *******************************************
  179.  
  180. main()
  181. {    long n, selection;
  182.     InitToolBox();                //    all times needed inits
  183.     InitHighLevelEvents();        //    required apple events
  184.     InitMenuBar();                //    draw menu bar
  185.  
  186.     done=false;
  187.     while (!done)
  188.     {
  189.         if ( WaitNextEvent(eventMask,&event,eventDelay,eventRegion) )
  190.         {                
  191.             switch (event.what)
  192.             {
  193.             case kHighLevelEvent:    AEProcessAppleEvent(&event);break;    
  194.             case mouseDown:            HandleMouseDown();             break;
  195.             case mouseUp:            HandleMOUSEUP();             break;
  196.             case keyUp:                HandleKEYUP(event.message&0x0ff);     break;
  197.             case autoKey:            HandleAUTOKEY(event.message&0x0ff); break;
  198.             case diskEvt:            HandleDISK();             break;
  199.             case driverEvt:            HandleDRIVER();            break;    
  200.             case app3Evt:            HandleAPP3();            break;
  201.             case keyDown:
  202.                 if (event.modifiers&cmdKey)
  203.                     {    selection = MenuKey(event.message&0x0ff);
  204.                         if (HiWord(selection)) 
  205.                         {     HandleMenuSelection( selection );
  206.                             break;
  207.                         }
  208.                     }
  209.                 HandleKEYDOWN (event.message&0x0ff); 
  210.                 break;
  211.             case activateEvt:    
  212.                 SetPort ( (WindowPtr)event.message );
  213.                 if (event.modifiers&activeFlag)    
  214.                         HandleACTIVATE   ();     // window activated  
  215.                 else    HandleDEACTIVATE ();    // window deactivated
  216.                 break; 
  217.             case updateEvt:    
  218.                 BeginUpdate ( (WindowPtr)event.message );
  219.                 HandleUPDATE( (WindowPtr)event.message );
  220.                 EndUpdate   ( (WindowPtr)event.message ); 
  221.                 break;
  222.             case osEvt:                
  223.                 if (event.message & 0x01000000 ) 
  224.                     if ( event.message & 0x00000001 ) 
  225.                             SetCursor(&qd.arrow),HandleRESUME(); // application re-activated 
  226.                     else    HandleSUSPEND();                      // application de-activated
  227.                 if ((event.message & 0xFF000000)==0xFA000000)
  228.                     HandleREGION();            // mouse leaves eventRegion
  229.                 break;
  230.             }
  231.         }
  232.         else HandleNULL();
  233.     }
  234. }
  235.  
  236.  
  237. HandleFileMenu(long itemNum)
  238. {    
  239.     if (itemNum==iNew)         { HandleNEW(); return;         }
  240.     if (itemNum==iOpen)     
  241.     {     StandardFileReply     reply;        
  242.         StandardGetFile ( nil, oDocNumTypes, oDocTypeList, &reply ); 
  243.         if (reply.sfGood) HandleOPEN ( reply.sfFile );
  244.         return;     
  245.     }
  246.     if (itemNum==iSave)     { HandleSAVE(); return;     }
  247.     if (itemNum==iClose)     { HandleCLOSE(); return;     }
  248.     if (itemNum==iQuit)     { HandleQUIT(); return;     }
  249.     if (itemNum==iPrint)    { HandlePRINT(); return;    }
  250.     HandleMENU ( mFile, itemNum );
  251. }
  252.  
  253. HandleEditMenu(long itemNum)
  254. {    
  255.     if (itemNum==iUndo)     { HandleUNDO(); return;     }
  256.     if (itemNum==iCut)         { HandleCUT(); return;         }
  257.     if (itemNum==iCopy)     { HandleCOPY(); return;     }
  258.     if (itemNum==iPaste)     { HandlePASTE(); return;     }
  259.     HandleMENU ( mFile, itemNum );
  260. }
  261.  
  262.  
  263. HandleAppleMenu(long itemNum)
  264. {    GrafPtr    savePort;
  265.     MenuHandle theMenu;
  266.     Str255 daName;
  267.     if (itemNum==iAbout)     { Alert(aboutID,nil); return;     }
  268.     if (itemNum==iHelp)     { Alert(helpID,nil); return;     }
  269.     theMenu = GetMenu ( mApple ); 
  270.     if (theMenu==nil) DoAbort("\pcould not get handle to apple menu","\p","\p");
  271.     GetItem ( theMenu, itemNum, daName );
  272.     GetPort( &savePort );
  273.     OpenDeskAcc ( daName );
  274.     SetPort( savePort );
  275. }
  276.  
  277.  
  278. HandleMenuSelection(long menuResult)
  279. {    long menuNum, itemNum;
  280.     menuNum = HiWord(menuResult); if (menuNum==0) return;
  281.     itemNum = LoWord(menuResult);
  282.     if (menuNum==mApple) HandleAppleMenu(itemNum);    else
  283.     if (menuNum==mFile)  HandleFileMenu(itemNum);     else
  284.     if (menuNum==mEdit)  HandleEditMenu(itemNum);    else  
  285.                          HandleMENU(menuNum,itemNum);
  286.     HiliteMenu(0);
  287. }
  288.  
  289.  
  290. HandleMouseDown()
  291. {    WindowPtr    window;
  292.     long        where;
  293.     where = FindWindow ( event.where, &window );
  294.     switch (where)
  295.     {
  296.     case inMenuBar:        HandleMenuSelection ( MenuSelect( event.where ) );         return;
  297.     case inDrag:        DragWindow ( window, event.where, &qd.screenBits.bounds ); return;
  298.     case inSysWindow:    SystemClick ( &event, window );                         return;
  299.     case inGoAway:        HandleCLOSE();      return;
  300.     case inGrow:        HandleGROW();       return;
  301.     case inZoomIn:        HandleZOOMIN();      return;
  302.     case inZoomOut:        HandleZOOMOUT();     return;
  303.     case inContent:        SelectWindow ( window ); HandleINCONTENT();             return;
  304.     case inDesk:        HandleINDESK();        return;
  305.     }
  306.     DoWarning ( "\pmouse down at unknown location","\p","\p" ); 
  307. }
  308.  
  309.