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

  1. {******************************************************************************}
  2. {**}
  3. {**  Project Name:    DropShell}
  4. {**     File Name:    DSUtils.p}
  5. {**}
  6. {**   Description:    Utility routines that may be useful to DropBoxes}
  7. {**}
  8. {*******************************************************************************}
  9. {**                       A U T H O R   I D E N T I T Y}
  10. {*******************************************************************************}
  11. {**}
  12. {**    Initials    Name}
  13. {**    --------    -----------------------------------------------}
  14. {**    LDR            Leonard Rosenthol}
  15. {**}
  16. {*******************************************************************************}
  17. {**                      R E V I S I O N   H I S T O R Y}
  18. {*******************************************************************************}
  19. {**}
  20. {**      Date        Time    Author    Description}
  21. {**    --------    -----    ------    ---------------------------------------------}
  22. {**    12/09/91            LDR        Added the Apple event routines}
  23. {**    11/24/91            LDR        Original Version}
  24. {**}
  25. {******************************************************************************}
  26. unit DSUtils;
  27. interface
  28.  
  29.     uses
  30.         AppleTalk, Processes, PPCToolbox, EPPC, Notification, AppleEvents, Aliases, Script, DSGlobals;
  31.  
  32. {---------------------}
  33. {Interface Definitions}
  34. {---------------------}
  35.  
  36.     procedure CenterAlert (theID: integer);
  37.     procedure ErrorAlert (stringListID, stringIndexID, errorID: integer);
  38.  
  39.     procedure GetAppName (var appName: Str255);
  40.     procedure GetAppFSSpec (var appSpec: FSSpec);
  41.  
  42.     function GetTargetFromSelf (var targetDesc: AEAddressDesc): OSErr;
  43.     function GetTargetFromSignature (processSig: OSType; var targetDesc: AEAddressDesc): OSErr;
  44.     function GetTargetFromBrowser (promptStr: Str255; var targetDesc: AEAddressDesc): OSErr;
  45.  
  46.     procedure SendODOCToSelf (theFileSpec: FSSpec);
  47.     procedure SendQuitToSelf;
  48.  
  49. implementation
  50. {$S Main}
  51.  
  52.     {}
  53. {        This routine is used to properly center an Alert before showing.}
  54. {        }
  55. {        It is per Human Interface specs by putting it in the top 1/3 of screen.}
  56. {        NOTE: This same technique can be used with DLOG resources as well.}
  57. {    }
  58.     procedure CenterAlert (theID: integer);
  59.         var
  60.             theX, theY: INTEGER;
  61.             theAlertHandle: AlertTHndl;
  62.     begin
  63.         theAlertHandle := AlertTHndl(GetResource('ALRT', theID));
  64.         if theAlertHandle <> nil then
  65.             begin
  66.                 HLock(Handle(theAlertHandle));
  67.                 with theAlertHandle^^ do
  68.                     begin
  69.                         with screenBits do
  70.                             begin
  71.                                 theX := ((bounds.right - bounds.left) - (boundsRect.right - boundsRect.left)) div 2;
  72.                                 theY := ((bounds.bottom - bounds.top) + GetMBarHeight - (boundsRect.bottom - boundsRect.top)) div 2;
  73.                                 theY := theY - ((bounds.bottom - bounds.top) div 4);    {this moves it up for better viewing!}
  74.                             end;
  75.                         OffsetRect(boundsRect, theX - boundsRect.left, theY - boundsRect.top);
  76.                     end;
  77.             end;
  78.         SetCursor(arrow);    {if you use this routine in a code resource, change this!}
  79.     end;
  80.  
  81.     {}
  82. {        This routine is just a quick & dirty error reporter}
  83. {    }
  84.     procedure ErrorAlert (stringListID, stringIndexID, errorID: integer);
  85.         const
  86.             alertID = 200;
  87.         var
  88.             item: integer;
  89.             param, errorStr: Str255;
  90.     begin
  91.         if ErrorID <> 0 then
  92.             begin
  93.                 NumToString(errorID, errorStr);
  94.                 GetIndString(param, stringListID, stringIndexID);
  95.                 ParamText(param, errorStr, '', '');
  96.                 CenterAlert(alertID);
  97.                 item := Alert(alertID, nil);
  98.             end;
  99.     end;
  100.  
  101. {*** These routines use the Process Manager to give you information about yourself ***}
  102.  
  103.     procedure GetAppName (var appName: Str255);
  104.         var
  105.             err: OSErr;
  106.             info: ProcessInfoRec;
  107.             curPSN: ProcessSerialNumber;
  108.     begin
  109.         err := GetCurrentProcess(curPSN);
  110.  
  111.         with info do
  112.             begin
  113.                 processInfoLength := sizeof(ProcessInfoRec);    {ALWAYS USE sizeof!}
  114.                 processName := @appName;                        {so it returned somewhere}
  115.                 processAppSpec := nil;                            {I don't care!}
  116.             end;
  117.         err := GetProcessInformation(curPSN, info);
  118.     end;
  119.  
  120.     procedure GetAppFSSpec (var appSpec: FSSpec);
  121.         var
  122.             err: OSErr;
  123.             info: ProcessInfoRec;
  124.             appName: Str255;
  125.             curPSN: ProcessSerialNumber;
  126.     begin
  127.         err := GetCurrentProcess(curPSN);
  128.  
  129.         with info do
  130.             begin
  131.                 processInfoLength := sizeof(ProcessInfoRec);    {ALWAYS USE sizeof!}
  132.                 processName := @appName;                        {so it returned somewhere}
  133.                 processAppSpec := @appSpec;                        {and here's where the spec goes}
  134.             end;
  135.         err := GetProcessInformation(curPSN, info);
  136.     end;
  137.  
  138.  
  139. { ÑÑÑ Apple event routines begin here ÑÑÑ }
  140.  
  141. {}
  142. {    This routine will create a targetDesc for sending to self.}
  143. {}
  144. {    We take IM VI's advice and use the typePSN form with }
  145. {    kCurrentProcess as the targetPSN.}
  146. {}
  147.     function GetTargetFromSelf (var targetDesc: AEAddressDesc): OSErr;
  148.         var
  149.             err: OSErr;
  150.             psn: ProcessSerialNumber;
  151.     begin
  152.         with psn do
  153.             begin
  154.                 highLongOfPSN := 0;
  155.                 lowLongOfPSN := kCurrentProcess;
  156.             end;
  157.         err := AECreateDesc(typeProcessSerialNumber, @psn, sizeof(ProcessSerialNumber), targetDesc);
  158.         GetTargetFromSelf := err;
  159.     end;
  160.  
  161. {This routine will create a targetDesc using the apps signature}
  162.     function GetTargetFromSignature (processSig: OSType; var targetDesc: AEAddressDesc): OSErr;
  163.         var
  164.             err: OSErr;
  165.  
  166.     begin
  167.         err := AECreateDesc(typeApplSignature, @processSIG, sizeof(processSig), targetDesc);
  168.         GetTargetFromSignature := err;
  169.     end;
  170.  
  171. {This routine will create a targetDesc by bringing up the PPCBrowser}
  172.     function GetTargetFromBrowser (promptStr: Str255; var targetDesc: AEAddressDesc): OSErr;
  173.         var
  174.             err: OSErr;
  175.             theTarget: TargetID;
  176.             portInfo: PortInfoRec;
  177.  
  178.     begin
  179.         err := PPCBrowser(promptStr, '', FALSE, theTarget.location, portInfo, nil, '');
  180.         if (err = noErr) then
  181.             begin
  182.                 theTarget.name := portInfo.name;
  183.                 err := AECreateDesc(typeTargetID, @theTarget, sizeof(TargetID), targetDesc);
  184.             end;
  185.         GetTargetFromBrowser := err;
  186.     end;
  187.  
  188.  
  189. {}
  190. {    This routine is the low level routine used by the SendODOCToSelf}
  191. {    routine.  It gets passed the list of files (in an AEDescList)}
  192. {    to be sent as the data for the 'odoc', builds up the event}
  193. {    and sends off the event.  }
  194. {}
  195. {    It is broken out from SendODOCToSelf so that a SendODOCListToSelf could}
  196. {    easily be written and it could then call this routine - but that is left}
  197. {    as an exercise to the reader.}
  198. {    }
  199. {    Read the comments in the code for the order and details}
  200. {}
  201.     procedure _SendDocsToSelf (aliasList: AEDescList);
  202.         var
  203.             err: OSErr;
  204.             theTarget: AEAddressDesc;
  205.             openDocAE: AppleEvent;
  206.             replyAE: AppleEvent;
  207.  
  208.     begin
  209.     {}
  210. {        First we create the target for the event.   We call another}
  211. {        utility routine for creating the target.}
  212. {    }
  213.         err := GetTargetFromSelf(theTarget);
  214.         if (err = noErr) then
  215.             begin
  216.         {Next we create the Apple event that will later get sent.}
  217.                 err := AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, theTarget, kAutoGenerateReturnID, kAnyTransactionID, openDocAE);
  218.  
  219.                 if err = noErr then
  220.                     begin
  221.             {Now add the aliasDescList to the openDocAE}
  222.                         err := AEPutParamDesc(openDocAE, keyDirectObject, aliasList);
  223.  
  224.                         if err = noErr then
  225.                             begin
  226.                 {}
  227. {                    and finally send the event}
  228. {                    Since we are sending to ourselves, no need for reply.}
  229. {                }
  230.                                 err := AESend(openDocAE, replyAE, kAENoReply + kAECanInteract, kAENormalPriority, 3600, nil, nil);
  231.  
  232.                 {}
  233. {                    NOTE: Since we are not requesting a reply, we do not need to}
  234. {                    need to dispose of the replyAE.  It is there simply as a }
  235. {                    placeholder.}
  236. {                }
  237.                             end;
  238.  
  239.             {    }
  240. {                Dispose of the aliasList descriptor}
  241. {                We do this instead of the caller since it needs to be done}
  242. {                before disposing the AEVT}
  243. {            }
  244.                         err := AEDisposeDesc(aliasList);
  245.                     end;
  246.  
  247.         {and of course dispose of the openDoc AEVT itself}
  248.                 err := AEDisposeDesc(openDocAE);
  249.             end;
  250.     end;
  251.  
  252. {}
  253. {    This is the routine called by SelectFile to send a single odoc to ourselves.}
  254. {    }
  255. {    It calls the above low level routine to do the dirty work of sending the AEVT -}
  256. {    all we do here is build a AEDescList of the file to be opened.}
  257. {}
  258.     procedure SendODOCToSelf (theFileSpec: FSSpec);
  259.         var
  260.             err: OSErr;
  261.             aliasList: AEDescList;
  262.             aliasDesc: AEDesc;
  263.             aliasH: AliasHandle;
  264.     begin
  265.     {Create the descList to hold the list of files}
  266.         err := AECreateList(nil, 0, FALSE, aliasList);
  267.  
  268.         if err = noErr then
  269.             begin
  270.         {First we setup the type of descriptor}
  271.                 aliasDesc.descriptorType := typeAlias;
  272.  
  273.         {}
  274. {            Now we add the file to descList by creating an alias and then}
  275. {            adding it into the descList using AEPutDesc}
  276. {        }
  277.                 err := NewAlias(nil, theFileSpec, aliasH);
  278.                 aliasDesc.dataHandle := Handle(aliasH);
  279.                 err := AEPutDesc(aliasList, 0, aliasDesc);
  280.                 DisposeHandle(Handle(aliasH));
  281.  
  282.         {Now call the real gut level routine to do the dirty work}
  283.                 _SendDocsToSelf(aliasList);
  284.  
  285.         {_SendDocsToSelf will dispose of aliasList for me}
  286.             end;
  287.     end;
  288.  
  289.  
  290.     procedure SendQuitToSelf;
  291.         var
  292.             err: OSErr;
  293.             theTarget: AEAddressDesc;
  294.             QuitAE: AppleEvent;
  295.             replyAE: AppleEvent;
  296.  
  297.     begin
  298.     {}
  299. {        First we create the target for the event.   We call another}
  300. {        utility routine for creating the target.}
  301. {    }
  302.         err := GetTargetFromSelf(theTarget);
  303.         if (err = noErr) then
  304.             begin
  305.         {Next we create the Apple event that will later get sent.}
  306.                 err := AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, theTarget, kAutoGenerateReturnID, kAnyTransactionID, QuitAE);
  307.  
  308.                 if err = noErr then
  309.                     begin
  310.             {}
  311. {                and send the event}
  312. {                Since we are sending to ourselves, no need for reply.}
  313. {            }
  314.                         err := AESend(QuitAE, replyAE, kAENoReply + kAECanInteract, kAENormalPriority, 3600, nil, nil);
  315.  
  316.             {}
  317. {                NOTE: Since we are not requesting a reply, we do not need to}
  318. {                need to dispose of the replyAE.  It is there simply as a }
  319. {                placeholder.}
  320. {            }
  321.                     end;
  322.  
  323.         {Dispose of the quit AEVT itself}
  324.                 err := AEDisposeDesc(QuitAE);
  325.             end;
  326.     end;
  327.  
  328. end.