home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 March / macformat-035.iso / Shareware City / Developers / ICAppSourceKit1.2 / RequiredEventSupport.p < prev    next >
Encoding:
Text File  |  1995-11-04  |  4.0 KB  |  128 lines  |  [TEXT/CWIE]

  1. unit RequiredEventSupport;
  2.  
  3. interface
  4.  
  5.     uses
  6.         AppleEvents;
  7.  
  8.     function InitAppleEvents (DoOApp, DoODoc, DoPrint, DoQuit: Ptr): OSErr;
  9.     function GotRequiredParams (theAppleEvent: AppleEvent): OSErr;
  10.  
  11. implementation
  12.  
  13.     function DoOApp (p: ptr): OSErr;
  14.     inline
  15.         $205F, (* movea.l    (a7)+,a0        ; pop proc/func address    *)
  16.         $4E90; (* jsr            (a0)                ; call proc/func                *)
  17.  
  18.     function DoDocs (fs: FSSpec; p: ptr): OSErr;
  19.     inline
  20.         $205F, (* movea.l    (a7)+,a0        ; pop proc/func address    *)
  21.         $4E90; (* jsr            (a0)                ; call proc/func                *)
  22.  
  23.     function DoQuit (p: ptr): OSErr;
  24.     inline
  25.         $205F, (* movea.l    (a7)+,a0        ; pop proc/func address    *)
  26.         $4E90; (* jsr            (a0)                ; call proc/func                *)
  27.  
  28.     function GotRequiredParams (theAppleEvent: AppleEvent): OSErr;
  29.         var
  30.             typeCode: DescType;
  31.             actualSize: Size;
  32.             err: OSErr;
  33.     begin
  34.         err := AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, typeCode, nil, 0, actualSize);
  35. (* nil ok: need only function result *)
  36.  
  37.         if err = errAEDescNotFound then        (* we got all the required params: all is ok *)
  38.             GotRequiredParams := noErr
  39.         else if err = noErr then
  40.             GotRequiredParams := errAEEventNotHandled
  41.         else
  42.             GotRequiredParams := err;
  43.     end; (* GotRequiredParams *)
  44.  
  45.     function HandleOAPP (var theAppleEvent, reply: AppleEvent; openappp: ptr): OSErr;
  46. (* <aevt> }
  47. {    kCoreEventClass kAEOpenApplication}
  48. {*)
  49.         var
  50.             oe: OSErr;
  51.     begin
  52.         reply := reply; { Unused }
  53.     (* We don't expect any params at all, but check in case the client requires any *)
  54.         oe := GotRequiredParams(theAppleEvent);
  55.         oe := DoOApp(openappp);
  56.         HandleOAPP := oe;
  57.     end;
  58.  
  59.     function HandleDocs (var theAppleEvent, reply: AppleEvent; dodocp: ptr): OSErr;
  60. (* <aevt>}
  61. {    kCoreEventClass kAEOpenDocuments}
  62. {    kCoreEventClass kAEPrintDocuments}
  63. {*)
  64.         var
  65.             myFSS: FSSpec;
  66.             docList: AEDescList;
  67.             index, itemsInList: longint;
  68.             actualSize: Size;
  69.             keywd: AEKeyword;
  70.             typeCode: descType;
  71.             oe, ooe: OSErr;
  72.     begin
  73.         reply := reply; { Unused }
  74.         oe := AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, docList);
  75.         if oe = noErr then begin
  76.             ooe := GotRequiredParams(theAppleEvent);
  77. (* now get each alias from the list (as an FSSSpec) and open the associated file. *)
  78.             oe := AECountItems(docList, itemsInList);
  79.             for index := 1 to itemsInList do begin
  80.                 ooe := AEGetNthPtr(docList, index, typeFSS, keywd, typeCode, @myFSS, sizeof(myFSS), actualSize);
  81. (* coercion does alias->fsspec *)
  82.                 if ooe = noErr then
  83.                     ooe := DoDocs(myFSS, dodocp);
  84.             end;
  85.             ooe := AEDisposeDesc(docList);
  86.         end;
  87.         HandleDocs := oe;
  88.     end; (* HandleDocs *)
  89.  
  90.     function HandleQUIT (var theAppleEvent, reply: AppleEvent; quitp: ptr): OSErr;
  91. (* <aevt> }
  92. {    kCoreEventClass kAEQuitApplication}
  93. {*)
  94.         var
  95.             oe: OSErr;
  96.             errStr: Str255;
  97.     begin
  98. (* We don't expect any params at all, but check in case the client requires any *)
  99.         oe := GotRequiredParams(theAppleEvent);
  100.         oe := DoQuit(quitp);                (* set global boolean: app will exit at end of event loop *)
  101.         if reply.dataHandle <> nil then        (* a reply is sought *)
  102.             begin
  103.             if oe = noErr then
  104.                 errStr := 'OK'
  105.             else
  106.                 errStr := 'user cancelled quit';
  107.             oe := AEPutParamPtr(reply, keyErrorString, typeChar, Ptr(@errStr[1]), length(errStr));
  108.         end;
  109.         HandleQUIT := oe;
  110.     end;
  111.  
  112.     function InitAppleEvents (DoOApp, DoODoc, DoPrint, DoQuit: Ptr): OSErr;
  113.         var
  114.             aevtErr: OSErr;
  115.     begin
  116.         aevtErr := noErr;
  117.         if (aevtErr = noErr) and (DoOApp <> nil) then
  118.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, @HandleOAPP, longint(DoOApp), false);
  119.         if (aevtErr = noErr) and (DoODoc <> nil) then
  120.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, @HandleDocs, longint(DoODoc), false);
  121.         if (aevtErr = noErr) and (DoPrint <> nil) then
  122.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, @HandleDocs, longint(DoPrint), false);
  123.         if (aevtErr = noErr) and (DoQuit <> nil) then
  124.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, @HandleQUIT, longint(DoQuit), false);
  125.         InitAppleEvents := aevtErr;
  126.     end;
  127.  
  128. end. (* RequiredEventSupport *)