home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / PNL Libraries / MyRequiredEventSupport.p < prev    next >
Encoding:
Text File  |  1995-10-23  |  4.5 KB  |  143 lines  |  [TEXT/CWIE]

  1. unit MyRequiredEventSupport;
  2.  
  3. interface
  4.  
  5.     uses
  6.         AppleEvents;
  7.  
  8.     const
  9.         keyOdocOption = 'auto'; { optional parameter to open }
  10.         keyOdocControl = 'autC'; { optional parameter to open }
  11.  
  12.     var
  13.         odoc_with_option: boolean;
  14.         odoc_with_control: boolean;
  15.  
  16.     type
  17.         DocProc = function (var fs:FSSpec): OSErr;
  18.         NoDocProc = function: OSErr;
  19.     
  20.     procedure InitAppleEvents (DoOpenApplication:NoDocProc; DoOpenDocuments:DocProc; DoPrintDocuments: DocProc; DoQuitApplication: NoDocProc);
  21.  
  22. implementation
  23.  
  24.     uses
  25.         MyAEUtils;
  26.         
  27.     var
  28.         gDoOpenApplication: NoDocProc;
  29.         gDoOpenDocuments: DocProc;
  30.         gDoPrintDocuments: DocProc;
  31.         gDoQuitApplication: NoDocProc;
  32.  
  33.     function HandleNoDocs (var event, reply: AppleEvent; Doit:NoDocProc): OSErr;
  34.         var
  35.             err: OSErr;
  36.     begin
  37.         reply := reply; { UNUSED! }
  38.         err := GotRequiredParams(event);
  39.         if err = noErr then begin
  40.             err := Doit;
  41.         end;
  42.         HandleNoDocs := err;
  43.     end;
  44.  
  45.     function HandleDocs (var event, reply: AppleEvent; Doit:DocProc): OSErr;
  46.         var
  47.             myFSS: FSSpec;
  48.             docList: AEDescList;
  49.             index, itemsInList: longint;
  50.             actualSize: Size;
  51.             keywd: AEKeyword;
  52.             typeCode: descType;
  53.             err, oerr, junk: OSErr;
  54.             pi: ProcessInfoRec;
  55.             psn: ProcessSerialNumber;
  56.             dummy: Boolean;
  57.             er: EventRecord;
  58.     begin
  59.         reply := reply; { UNUSED! }
  60.  
  61.     { check if the event comes from the finder and honour the option/control keys }
  62.         odoc_with_control := false;
  63.         odoc_with_option := false;
  64.         err := AEGetAttributePtr(event, keyAddressAttr, keyProcessSerialNumber, keywd, @psn, SizeOf(psn), actualSize);
  65.         if err = noErr then begin
  66.             pi.processInfoLength := sizeof(ProcessInfoRec);
  67.             pi.processName := nil;
  68.             pi.processAppSpec := nil;
  69.             err := GetProcessInformation(psn, pi);
  70.         end;
  71.         if (err = noErr) & (pi.processSignature = 'MACS') then begin
  72.             dummy := OSEventAvail(everyEvent, er);
  73.             odoc_with_option := BAND(er.modifiers, optionKey) <> 0;
  74.             odoc_with_control := BAND(er.modifiers, controlKey) <> 0;
  75.         end;
  76.         junk := GetBooleanFromAERecord(event, keyOdocOption, odoc_with_option);
  77.         junk := GetBooleanFromAERecord(event, keyOdocControl, odoc_with_control);
  78.  
  79.         err := AEGetParamDesc(event, keyDirectObject, typeAEList, docList);
  80.         if err = noErr then begin
  81.             junk := GotRequiredParams(event);
  82. (* now get each alias from the list (as an FSSSpec) and open the associated file. *)
  83.             err := AECountItems(docList, itemsInList);
  84.             for index := 1 to itemsInList do begin
  85.                 oerr := AEGetNthPtr(docList, index, typeFSS, keywd, typeCode, @myFSS, sizeof(myFSS), actualSize);
  86. (* coercion does alias->fsspec *)
  87.                 if oerr = noErr then begin
  88.                     oerr := Doit(myFSS);
  89.                 end;
  90.             end;
  91.             junk := AEDisposeDesc(docList);
  92.         end;
  93.         HandleDocs := err;
  94.     end;
  95.  
  96.     function HandleOpenApplication(var event, reply: AppleEvent; ignored: longint): OSErr;
  97.     begin
  98.         ignored := ignored; { UNUSED! }
  99.         HandleOpenApplication := HandleNoDocs(event, reply, gDoOpenApplication);
  100.     end;
  101.  
  102.     function HandleOpenDocuments (var event, reply: AppleEvent; ignored: longint): OSErr;
  103.     begin
  104.         ignored := ignored; { UNUSED! }
  105.         HandleOpenDocuments := HandleDocs(event, reply, gDoOpenDocuments);
  106.     end;
  107.     
  108.     function HandlePrintDocuments(var event, reply: AppleEvent; ignored: longint): OSErr;
  109.     begin
  110.         ignored := ignored; { UNUSED! }
  111.         HandlePrintDocuments := HandleDocs(event, reply, gDoPrintDocuments);
  112.     end;
  113.     
  114.     function HandleQuitApplication(var event, reply: AppleEvent; ignored: longint): OSErr;
  115.     begin
  116.         ignored := ignored; { UNUSED! }
  117.         HandleQuitApplication := HandleNoDocs(event, reply, gDoQuitApplication);
  118.     end;
  119.  
  120.     procedure InitAppleEvents (DoOpenApplication:NoDocProc; DoOpenDocuments:DocProc; DoPrintDocuments: DocProc; DoQuitApplication: NoDocProc);
  121.         var
  122.             junk: OSErr;
  123.     begin
  124.         gDoOpenApplication := DoOpenApplication;
  125.         gDoOpenDocuments := DoOpenDocuments;
  126.         gDoPrintDocuments := DoPrintDocuments;
  127.         gDoQuitApplication := DoQuitApplication;
  128.         if (@gDoOpenApplication <> nil) then begin
  129.             junk := AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(@HandleOpenApplication), 0, false);
  130.         end;
  131.         if (@gDoOpenDocuments <> nil) then begin
  132.             junk := AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(@HandleOpenDocuments), 0, false);
  133.         end;
  134.         if (@gDoPrintDocuments <> nil) then begin
  135.             junk := AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc(@HandlePrintDocuments), 0, false);
  136.         end;
  137.         if (@gDoQuitApplication <> nil) then begin
  138.             junk := AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(@HandleQuitApplication), 0, false);
  139.         end;
  140.     end;
  141.  
  142. end.
  143.