home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ctcoll95.zip / PARALLEL / DSAPEV.P < prev    next >
Text File  |  1995-03-06  |  9KB  |  257 lines

  1. {******************************************************************************}
  2. {**}
  3. {**  Project Name:    DropShell}
  4. {**     File Name:    DSAppleEvents.p}
  5. {**}
  6. {**   Description:    Generic AppleEvent handling routines}
  7. {**                    }
  8. {**                    This is the set of routines for handling the required Apple events.}
  9. {**                    You should NEVER have to modify this file!!!}
  10. {**                    Simply add code in DSUserProcs to the routines called by these.}
  11. {**}
  12. {*******************************************************************************}
  13. {**                       A U T H O R   I D E N T I T Y}
  14. {*******************************************************************************}
  15. {**}
  16. {**    Initials    Name}
  17. {**    --------    -----------------------------------------------}
  18. {**    LDR            Leonard Rosenthol}
  19. {**}
  20. {*******************************************************************************}
  21. {**                      R E V I S I O N   H I S T O R Y}
  22. {*******************************************************************************}
  23. {**}
  24. {**      Date        Time    Author    Description}
  25. {**    --------    -----    ------    ---------------------------------------------}
  26. {**    11/24/91            LDR        Added a handler for 'pdoc' as per DTS recommendation}
  27. {**                                This caused some reorg & userProc routine changes}
  28. {**                                I also created a new common AEVT doc extractor}
  29. {**                                Pass the new userDataHandle to odoc/pdoc routines}
  30. {**                                FailErr now uses the ErrorAlert routine to report probs}
  31. {**    10/30/91            LDR        Modified USES clause for new include & ifc'ed ThP}
  32. {**    10/28/91            LDR        Officially renamed DropShell (from QuickShell)}
  33. {**                                Added a bunch of comments for clarification}
  34. {**    04/09/91    00:04    LDR        Original Version}
  35. {**}
  36. {******************************************************************************}
  37.  
  38. unit DSAppleEvents;
  39. interface
  40.  
  41.     uses
  42.         AppleTalk, Processes, PPCToolbox, EPPC, Notification, AppleEvents, DSGlobals, DSUtils, DSUserProcs;    {just the DropShell files}
  43.  
  44. {---------------------}
  45. {Interface Definitions}
  46. {---------------------}
  47.  
  48.     procedure InitAEVTStuff;
  49.     procedure DoHighLevelEvent (event: EventRecord);
  50.  
  51.     function HandleOAPP (theAppleEvent, reply: AppleEvent; handlerRefcon: LONGINT): OSErr;
  52.     function HandleODOC (theAppleEvent, reply: AppleEvent; handlerRefcon: LONGINT): OSErr;
  53.     function HandlePDOC (theAppleEvent, reply: AppleEvent; handlerRefcon: LONGINT): OSErr;
  54.     function HandleQuit (theAppleEvent, reply: AppleEvent; handlerRefcon: LONGINT): OSErr;
  55.  
  56. implementation
  57.  
  58.     {$S Initialize}
  59.     { }
  60. {        This routine does all initialization for AEM, including the}
  61. {        creation and then population of the dispatch table.}
  62. {    }
  63.     procedure InitAEVTStuff;
  64.         var
  65.             aevtErr: OSErr;
  66.  
  67.     begin
  68.         aevtErr := noErr;
  69.  
  70.         if (aevtErr = noErr) then
  71.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, @HandleOAPP, 0, false);
  72.         if (aevtErr = noErr) then
  73.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, @HandleODOC, 0, false);
  74.         if (aevtErr = noErr) then
  75.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, @HandlePDOC, 0, false);
  76.         if (aevtErr = noErr) then
  77.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, @HandleQUIT, 0, false);
  78.  
  79.         if (aevtErr = noErr) then
  80.             InstallOtherEvents;
  81.  
  82.         if (aevtErr <> noErr) then
  83.             ; {Report an error to the user, if you are so inclinded!}
  84.     end;
  85.  
  86.  
  87.     {$S Main}
  88.     { }
  89. {        This routine is a utility routine for checking that all required }
  90. {        parameters in the Apple event have been used.}
  91. {    }
  92.  
  93.     function GotRequiredParams (theAppleEvent: AppleEvent): OSErr;
  94.         var
  95.             typeCode: DescType;
  96.             actualSize: Size;
  97.             err: OSErr;
  98.  
  99.     begin
  100.         err := AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, typeCode, nil, 0, actualSize);
  101.         if err = errAEDescNotFound then { we got all the required params: all is ok }
  102.             GotRequiredParams := noErr
  103.         else if err = noErr then
  104.             GotRequiredParams := errAEEventNotHandled
  105.         else
  106.             GotRequiredParams := err;
  107.     end; { GotRequiredParams }
  108.  
  109.     {}
  110. {        This is another routine useful for showing debugging info.}
  111. {        }
  112. {        It calls the ErrorAlert routine from DSUtils to put up the }
  113. {        error message.}
  114. {    }
  115.     procedure FailErr (err: OSErr);
  116.     begin
  117.         if err <> noErr then
  118.             ErrorAlert(kErrStringID, kAEVTErr, err);
  119.     end;
  120.  
  121.  
  122.  
  123.     {}
  124. {        This routine is the handler for the oapp (Open Application) event.}
  125. {        }
  126. {        It first checks the number of parameters to make sure we got them all }
  127. {        (even though we don't want any) and then calls the OpenApp userProc in QSUserProcs.}
  128. {        Finally it checks to see if the caller wanted a reply & sends one, setting any error.}
  129. {    }
  130.  
  131.     function HandleOAPP (theAppleEvent, reply: AppleEvent; handlerRefcon: LONGINT): OSErr;
  132.         var
  133.             errStr: Str255;
  134.  
  135.     begin
  136.         FailErr(GotRequiredParams(theAppleEvent));
  137.  
  138.         { let's show the user the splash screen }
  139.         ShowWindow(gSplashScreen);
  140.  
  141.         OpenApp;     {pass it on to the app specific routine}
  142.  
  143.         if reply.dataHandle <> nil then { a reply is sought }
  144.             begin
  145.                 errStr := 'Opening';
  146.                 HandleOAPP := AEPutParamPtr(reply, 'errs', 'TEXT', Ptr(@errStr[1]), length(errStr));
  147.             end
  148.         else
  149.             HandleOAPP := noErr;
  150.     end;
  151.  
  152.     {}
  153. {        This routine is the handler for the quit (Quit Application) event.}
  154. {        }
  155. {        It first checks the number of parameters to make sure we got them all }
  156. {        (even though we don't want any) and then calls the QuitApp userProc in QSUserProcs.}
  157. {        Finally it checks to see if the caller wanted a reply & sends one, setting any error.}
  158. {    }
  159.  
  160.     function HandleQUIT (theAppleEvent, reply: AppleEvent; handlerRefcon: LONGINT): OSErr;
  161.         var
  162.             errStr: Str255;
  163.  
  164.     begin
  165.         FailErr(GotRequiredParams(theAppleEvent));
  166.  
  167.         QuitApp;    {pass it on to the app specific routine}
  168.  
  169.         if reply.dataHandle <> nil then { a reply is sought }
  170.             begin
  171.                 errStr := 'Quiting';
  172.                 HandleQUIT := AEPutParamPtr(reply, 'errs', 'TEXT', Ptr(@errStr[1]), length(errStr));
  173.             end
  174.         else
  175.             HandleQUIT := noErr;
  176.     end;
  177.  
  178.     {}
  179. {        This routine is the low level processing routine for both the }
  180. {        odoc (Open Document) and pdoc (Print Document) events.}
  181. {        }
  182. {        This routine is the key one, since this is how we get the list of}
  183. {        files/folders/disks to process.  The first thing to do is the get the}
  184. {        list of files, and then make sure that's all the parameters (should be!).}
  185. {        We then send call the PreflightDocs routine (from DSUserProcs), process}
  186. {        each file in the list by calling OpenDoc (again in DSUserProcs), and finally}
  187. {        call PostflightDocs (you know where) and setting a return value.}
  188. {    }
  189.  
  190.     function _HandleDocs (theAppleEvent, reply: AppleEvent; opening: Boolean): OSErr;
  191.         var
  192.             myFSS: FSSpec;
  193.             docList: AEDescList;
  194.             index, itemsInList: LONGINT;
  195.             actualSize: Size;
  196.             keywd: AEKeyword;
  197.             typeCode: descType;
  198.             userDataHandle: Handle;
  199.  
  200.     begin
  201.         FailErr(AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, docList));
  202.         FailErr(GotRequiredParams(theAppleEvent));
  203.  
  204.         if PreFlightDocs(opening, userDataHandle) then {let the app do any preflighting it might need}
  205.             begin
  206.                 FailErr(AECountItems(docList, itemsInList));    {count the number of items}
  207.                 for index := 1 to itemsInList do
  208.                     begin
  209.                         FailErr(AEGetNthPtr(docList, index, typeFSS, keywd, typeCode, @myFSS, sizeof(myFSS), actualSize));
  210.                         OpenDoc(@myFSS, opening, userDataHandle);    {call the open routine}
  211.                     end;
  212.  
  213.                 PostFlightDocs(opening, userDataHandle); {and any cleanup}
  214.  
  215.                 _HandleDocs := noErr;
  216.             end
  217.         else
  218.             _HandleDocs := errAEEventNotHandled;    {this tells AEM we didn't handle it}
  219.  
  220.         FailErr(AEDisposeDesc(docList));
  221.     end; { _HandleDocs }
  222.  
  223.  
  224.     {}
  225. {        This routine is the handler for the odoc (Open Document) event.}
  226. {        }
  227. {        The odoc event simply calls the common _HandleDocs routines, which will}
  228. {        do the dirty work of parsing the AEVT & calling the userProcs.}
  229. {    }
  230.  
  231.     function HandleODOC (theAppleEvent, reply: AppleEvent; handlerRefcon: LONGINT): OSErr;
  232.     begin
  233.         HandleODOC := _HandleDocs(theAppleEvent, reply, TRUE);    {call the low level routine}
  234.     end; { HandleODOC }
  235.  
  236.     {}
  237. {        This routine is the handler for the pdoc (Print Document) event.}
  238. {        }
  239. {        The pdoc event like the odoc simply calls the common _HandleDocs routines}
  240. {    }
  241.  
  242.     function HandlePDOC (theAppleEvent, reply: AppleEvent; handlerRefcon: LONGINT): OSErr;
  243.     begin
  244.         HandlePDOC := _HandleDocs(theAppleEvent, reply, FALSE);    {call the low level routine}
  245.     end; { HandlePDOC }
  246.  
  247.     {}
  248. {        This is the routine called by the main event loop, when a high level}
  249. {        event is found.  Since we only deal with Apple events, and not other}
  250. {        high level events, we just pass everything onto the AEM via AEProcessAppleEvent}
  251. {    }
  252.     procedure DoHighLevelEvent (event: EventRecord);
  253.     begin
  254.         FailErr(AEProcessAppleEvent(event));
  255.     end;
  256.  
  257. end.