home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Utilities / Programming / EnterAct 3.5 / code to call Drag_ons / Call_Resource.c
Encoding:
C/C++ Source or Header  |  1995-01-13  |  34.9 KB  |  1,171 lines  |  [TEXT/KEEN]

  1. /* Call_Resource.c: file to be added to any applications wishing to
  2. call the code resources that come with EnterAct.
  3. THIS IS A COPY of the other Call_Resource.c file on your source disk. */
  4.  
  5. /* Copyright © 1991 the Free Software Foundation, Inc.
  6.  *         This file is free software; you can redistribute or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 1, or any later version.
  9.  *         This file is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12.  * GNU General Public License for more details.
  13.  *         You should have received a copy of the GNU General Public License
  14.  * along with GAWK; see the file "COPYING hAWK". If not, write to
  15.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  * Written for THINK C 4 on the Macintosh by Ken Earle (Dynabyte) Aug 1991.
  17.  */
  18.  
  19. /*
  20. To call EnterAct-style code resources from your application:
  21. 1    Add a copy of this file to your app.
  22. 2    Call "InitCallResources()" in your startup code, just before
  23.     entering your event loop. The arguments are:
  24.     vRefNumForApp : preferably your application working directory, or 0
  25.     codeFolderName : preferably (Ptr)"\pDrag_on Modules"
  26.     menuID : the integer menu id of the menu you wish code resources
  27.         to be appended to. Preferably not File or Edit.
  28. 3    Call "CallResource()" to handle the code resources picked from
  29.     the menu you have added them to. Typically looks something like
  30.         #define    MenuInQuestion    261
  31.         void DoMenuInQuestionCommand(short item)
  32.             {
  33.             switch (item)
  34.                 {
  35.             case 1:
  36.                 DoItemOne();
  37.             break;
  38.             case 2:
  39.                 DoItemTwo();
  40.             break;
  41.             ...etc...
  42.             default:
  43.                 if (item > 0)
  44.                     CallResource(MenuInQuestion, item);
  45.                     --or if you set VERSION2 == TRUE,
  46.                     CallResource(MenuInQuestion, item, true/false)--
  47.             break;
  48.                 }
  49.             }
  50. 4    Go through the extension ("callback") functions defined at the
  51.     bottom of this file, and decide which ones you wish to support.
  52.     You will probably not be able to support "InDictionary()", and
  53.     "GetNextMultiFile()" requires that your application keep a list
  54.     of files for some purpose or other (typically searching), but if
  55.     your application supports text files you should be able to provide
  56.     support for the others. Note "GetScreenHeight" and "GetScreenWidth"
  57.     are done here, no modification needed. The code resources supplied
  58.     with EnterAct can still do useful things even if no extensions are
  59.     supplied.
  60.     
  61.     If you decide not to support a particular extension, you need only
  62.     set the corresponding pointer to NULL in the function SetUpCodeCommunication().
  63.     For example, if you don't support InDictionary_Ext, then put
  64.         gacc.InDictionary_Ext = NULL;
  65.     in SetUpCodeCommunication().
  66.     
  67.     For more about the "DoEventLoopOnce()" callback, see 6 below.
  68.     
  69. 5    "ShowResult" and "SelectResult" defined below are called by
  70.     "CallResource" in response to a request by a code resource to display
  71.     the text results of the code resource run. If your application supports
  72.     TEXT documents, you should modify these functions to provide your
  73.     equivalents for these abilities. Otherwise, leave them as-is.
  74.  
  75. 6    Version 2 of hAWK (the one you received with this file) supports
  76. concurrent execution. This means you fire up a hAWK program, go back
  77. to working in your application (or even switch to some other app)
  78. and hAWK notifies you when the program is done. There is a
  79. penalty in speed of the hAWK program, usually far outweighed by
  80. the benefit of being able to go back to work instantly rather than
  81. having to sit there watching the watch cursor. Your calling application
  82. should not be noticeably affected, except when the hAWK program
  83. is doing something massive such as a huge "sort" or a large "copy".
  84.  
  85. The "VERSION2" defined constant just below should be set to TRUE if
  86. you wish to support concurrent execution of hAWK programs, FALSE
  87. if you do not. That's all that VERSION2 affects. Read Resource cannot
  88. run concurrently, and is not affected by the value of VERSION2.
  89.  
  90. To support concurrent exec of hAWK programs, follow these steps:
  91.     1    define VERSION2 just below to be TRUE
  92.     2    In the menu where you call Drag_on Modules, use
  93.         ...CallResource(MenuInQuestion, item, bool);
  94.         rather than just CallResource(MenuInQuestion, item);
  95.         -the "bool" variable is a Boolean, to be set to TRUE if
  96.         you want concurrent execution. Recommended interface is
  97.         to set this variable FALSE if the user has held down
  98.         either the <Shift> or <Option> key or both, and set it
  99.         TRUE otherwise.
  100.     3    One additional callback function needs to be provided,
  101.         in "DoEventLoopOnce()" below. Your callback should
  102.         have the form
  103.             extern void HandleOneEvent(void);
  104.         and can be a copy of your main event loop function,
  105.         except that it should handle just one event and then
  106.         return, and startup initializations if any can
  107.         be skipped (creating a region to track the mouse,
  108.         for example). If you use a global event record, you
  109.         can use the same global in your "HandleOneEvent"
  110.         function, but keep in mind that when "CallResource()"
  111.         returns, your global event record will reflect the
  112.         last event processed during your Drag_on Module run,
  113.         rather than the original menu call -- check what your
  114.         main event loop does with the event after the orginal
  115.         call, and verify that nothing important will be confused
  116.         by having the nature of the event changed during the
  117.         Drag_on Modules run.
  118.         (Note your event handler does not need to do any special
  119.         checking for the <Command><period> that stops hAWK.)
  120.     4    If your calling app is in the background when hAWK
  121.         finishes a run, a notice will be installed by the
  122.         DoNotify() routine below, flashing an icon in the
  123.         menu bar etc until you return to the calling app.
  124.         In your MAIN event handler (not the handle-one-event)
  125.         you should place the following bit of code to properly
  126.         handle the notify when your app resumes:
  127.             --declaration at the top of its file;
  128.                 -- to handle concurrent Drag_on Modules --
  129.                 Boolean gNotifying;
  130.                 Boolean gHawkIsRunning;
  131.                 NMRec    gNotifyRec;
  132.             
  133.             -- and in your main event handler, for the case of
  134.             "kSuspendResumeMessage";
  135.                 if (gNotifying)
  136.                     {
  137.                     NMRemove(&gNotifyRec);
  138.                     if (gNotifyRec.nmIcon)
  139.                         ReleaseResource(gNotifyRec.nmIcon);
  140.                     gNotifying = FALSE;
  141.                     ShowResultsAfterNotify();
  142.                     }
  143.         You will also need the standard global that records
  144.         whether or not your application is in the background,
  145.             Boolean gInBackground;
  146.         This variable should be set in your handle-one-event
  147.         function, and if your spelling of this variable name
  148.         differs from "gInBackground", change it here as well.
  149.         This file requires access to "gInBackground" in order
  150.         to determine if a notice should be posted at the end
  151.         of a hAWK program run.
  152.     5    In both your main event handler and the one-event
  153.         handler, calculate the "sleep" parameter for your
  154.         WaitNextEvent() call roughly as
  155.             gHawkIsRunning ? 2UL : GetCaretTime()
  156.         --adjust to suit your taste.
  157.     6    If you want the small icon "hAWK!" to flash for
  158.         notifications, move it to your application from "hAWK"
  159.         itself (it's just stored there).
  160.     7    One little wrinkle: what happens if you Quit your
  161.         application while hAWK is running? The recommended
  162.         solution here is to ask your user to issue a
  163.         <Command><period> to halt hAWK before proceeding
  164.         with the Quit. If you just pull the rug out from
  165.         under hAWK, files will probably be left open.
  166.         Sketchily:
  167.             Boolean DoQuit (...
  168.             extern Boolean gHawkIsRunning;...
  169.             if (gHawkIsRunning)
  170.                 {
  171.                 FlexAlert(justOK, stopIcon, "Please terminate your hAWK program \
  172.         with <Command><period> before quitting.");
  173.                 return(FALSE);
  174.                 }...
  175.     
  176. */
  177.  
  178. #include <string.h>
  179.  
  180. #ifndef NULL
  181. #define NULL        ((void *) 0)
  182. #endif
  183.  
  184. /* Change this to TRUE if you support concurrent hAWK - see above, point 6. */
  185. #define VERSION2 FALSE
  186.  
  187. #if VERSION2 == TRUE
  188. extern Boolean gInBackground;
  189. extern Boolean gNotifying;
  190. extern Boolean gHawkIsRunning;
  191. extern NMRec    gNotifyRec;
  192. #endif
  193.  
  194. /* Canned options, if you want to get going in a hurry:
  195. SUPPORT_LEVEL should be set to one of the following four options:
  196. MINIMAL - no extensions, no showing of results after a code resource run.
  197.     Note this doesn't mean the code resource won't be creating a text file,
  198.     it just means your application won't be showing it.
  199. RESULTSONLY - no extensions, but ability to show results supplied by you.
  200.     This means you should provide the hooks in ShowResult() and
  201.     SelectResult() - nothing else to do.
  202. BASICTEXT - in addition to providing the hooks in ShowResult() and
  203.     SelectResult(), fill in the blanks in GetFrontText() to allow the
  204.     code resource access to your front text window during a run.
  205. CUSTOMSUPPORT - ie avoid the above canned options.
  206.  
  207. There is one special case: if you support the "GetAppClip" extension,
  208. you’ll need to set SUPPORT_LEVEL to CUSTOMSUPPORT, and also
  209. define VERSION2 TRUE (getclip was not supported by version 1).
  210. */
  211.  
  212.  
  213. #define MINIMAL            1
  214. #define RESULTSONLY        2
  215. #define BASICTEXT        3
  216. #define CUSTOMSUPPORT    4
  217.  
  218. /* Pick yer pleasure - from the above 4 options only, please. */
  219. #define SUPPORT_LEVEL MINIMAL
  220.  
  221. /* Struct passed to all code resources. Everything should be optional.
  222. "stdInFileName" is primarily for code resources that insist on having input
  223. from an existing file - hAWK, for example. "stdOutFileName", however, is
  224. handy for use by any code resource that creates text, as this file can be
  225. shown by the calling application */
  226. typedef struct AppCodeComm
  227.     {
  228.     /* c strings except as noted */
  229.     char        *stdInFileName,    /* text from application to code resource */
  230.                 *stdInFileNameP, /* pascal version of above */
  231.                 *stdOutFileName, /* text from code resource to application */
  232.                 *stdErrFileName, /* error messages from code resource */
  233.                 *callerName,     /* the name of your application */
  234.                 *thisCodeName;     /* name of code resource being called */
  235.     short            result;
  236.     short            version; /* currently 1 */
  237. /* Extension ("callback") functions - ALL OPTIONAL. These pointers-to-functions
  238. are all set in  SetUpCodeCommunication() just before calling a code resource.*/
  239.     short            (*InDictionary_Ext)(char *tokenName);
  240.     Handle        (*GetFrontText_Ext)(Boolean getItAll);
  241.     void        (*GetNextMultiFile_Ext)(short *panePtr, short *indexPtr, 
  242.                 short *vRefNumPtr, char *fileName, Boolean clearFlag);
  243.     short         (*OKStopAlert_Ext)(Ptr cstringPtr);
  244.     void        (*MemoryAlert_Ext)(void);
  245.     short            (*GetScreenHeight_Ext)(void);
  246.     short            (*GetScreenWidth_Ext)(void);
  247.     void        (*ShowWatchCursor_Ext)(void);
  248. /* Concurrent exec, added for version 2 */
  249.     void        (*DoEventLoopOnce_Ext)(void);
  250. /* Access to scrap/clip of calling app */
  251.     Handle        (*GetAppClip_Ext)(void);
  252. /* Added for version 3 */
  253.     long        extendID; // Caller should set to 'VER3'
  254.     short        (*PutAppClip_Ext)(char *newClipStr);
  255.     } AppCodeComm, *ACCPtr;
  256.  
  257. static AppCodeComm    gacc;
  258.  
  259. /* Your application name goes here. */
  260. static char callerName[] = "MyApp";
  261.  
  262. typedef void (*CallCode)(ACCPtr ac);
  263.  
  264. static char *stdPathP;
  265.  
  266. typedef struct SpecificFolder
  267.     {
  268.     char     volName[32];
  269.     long    theDirID;
  270.     short        vRefNum;
  271.     } SpecificFolder;
  272. static SpecificFolder codeFolder;
  273.  
  274. /* Functions defined in this file: */
  275. /* Call this one towards the end of your startup, just before event loop */
  276. void InitCallResources(short vRefNumForApp, char *codeFolderName, short menuID);
  277. /* Called by InitCallResource: */
  278. void SetUpStandardFileNames(short vRefNum);
  279. void ShowResourcesInMenu(short vRefNumForApp, 
  280.     char *codeFolderName, short menuID);
  281. /* Two support routines for above */
  282. long FindCodeResFolder(short vRefNumForApp, char *codeFolderName);
  283. void AddCodesToMenu(long theDirID, short menuID);
  284. void OpenWDForCODE(void);
  285. /* Call this one in the "default" part of the switch for the
  286. menu containing the code resource names */
  287. void CallResource(short menuID, short item
  288. #if VERSION2 == TRUE
  289.     , Boolean concurrent
  290. #endif
  291.     );
  292.  
  293. #if VERSION2 == TRUE
  294. /* Enable/disable Drag_ons if running concurrently */
  295. void XableDrag_ons(short menuID, Boolean enable);
  296. /* Post a notify - NOTE this must be cleared by calling app */
  297. void DoNotify(void);
  298. /* For delayed showing of results */
  299. void ShowResultsAfterNotify(void);
  300. /* Beep and flush events */
  301. void AnnounceCompletion(void);
  302. #endif
  303.  
  304. /* Called by CallResource(), decide which extension functions to pass along */
  305. Boolean SetUpCodeCommunication(char *progName);
  306. /* Called by CallResource() to display results of resource run */
  307. Boolean ShowResult(char *name); /* either stdout or stderr */
  308. void SelectResult(void);
  309. /* A few support functions: */
  310. /* Pascal strings */
  311. void CopyPStr(Byte *srcStr, Byte *dstStr);
  312. void AppendPStr(Byte *s1, Byte *s2);
  313. Boolean PasEqualStrs(char *aStr, char *bStr);
  314. /* Files, names and locations */
  315. Byte *FullPathNameFromDirectory(long DirID, short vRefNum, Byte *s);
  316. Byte *FullPathNameFromVRefNum(short vRefNum, Byte *s);
  317. short    OpenWorkingDirectoryFromFullName(char *name, short len);
  318.  
  319. /* The extension functions - wrappers for your own calls */
  320. short InDictionary(char *tokenName);
  321. Handle GetFrontText(Boolean getItAll);
  322. void GetNextMultiFile(short *panePtr, short *indexPtr, 
  323.             short *vRefNumPtr, char *fileName, Boolean clearFlag);
  324. short OKStopAlert(Ptr cstringPtr);
  325. void MemoryAlert(void);
  326. short GetScreenHeight(void);
  327. short GetScreenWidth(void);
  328. void ShowWatchCursor(void);
  329. void DoEventLoopOnce(void);
  330. Handle GetAppClip(void);
  331. short PutAppClip(char *newClipStr);
  332.  
  333.  
  334.  
  335.  
  336. /* Call just before entering your event loop.
  337. Sets up full path names for standard in/out/err files,
  338. adds any code resources present to the menu of your choice.
  339. */
  340. void InitCallResources(short vRefNumForApp, char *codeFolderName, short menuID)
  341.     {
  342.     SetUpStandardFileNames(vRefNumForApp);
  343.     ShowResourcesInMenu(vRefNumForApp, codeFolderName, menuID);
  344.     }
  345.  
  346. /* Called once at beginning of application. Sets up full path names
  347. for std in/out/err files, and as a bonus sets gacc.callerName.
  348. The vRefNum passed in should be the application's vRefNum, but
  349. can be any old thing you want.
  350. -you can determine your application vrefnum at startup with
  351. short        appVRefNum;
  352. if (GetVol(NULL, &appVRefNum))
  353.     appVRefNum = 0;
  354.  */
  355. void SetUpStandardFileNames(short vRefNum)
  356.     {
  357.     if (!vRefNum)
  358.         GetVol(NULL, &vRefNum);
  359.     stdPathP = NewPtr(256);
  360.     if (MemError() != noErr)
  361.         goto PathTrouble;
  362.     gacc.stdInFileName = NewPtr(256);
  363.     if (MemError() != noErr)
  364.         goto PathTrouble;
  365.     gacc.stdInFileNameP = NewPtr(256);
  366.     if (MemError() != noErr)
  367.         goto PathTrouble;
  368.     gacc.stdOutFileName = NewPtr(256);
  369.     if (MemError() != noErr)
  370.         goto PathTrouble;
  371.     gacc.stdErrFileName = NewPtr(256);
  372.     if (MemError() != noErr)
  373.         goto PathTrouble;
  374.     if (vRefNum)
  375.         (void)(FullPathNameFromVRefNum(vRefNum, (Byte *)stdPathP));
  376.     else
  377.         stdPathP[0] = 0;
  378.     PtoCstr(stdPathP);
  379.     strcpy((Ptr)(gacc.stdInFileName), (Ptr)stdPathP);
  380.     strcpy((Ptr)(gacc.stdInFileNameP), (Ptr)stdPathP);
  381.     strcpy((Ptr)(gacc.stdOutFileName), (Ptr)stdPathP);
  382.     strcpy((Ptr)(gacc.stdErrFileName), (Ptr)stdPathP);
  383.     strcat((Ptr)(gacc.stdInFileName), "$tempStdIn");
  384.     strcat((Ptr)(gacc.stdInFileNameP), "$tempStdIn");
  385.     strcat((Ptr)(gacc.stdOutFileName), "$tempStdOut");
  386.     strcat((Ptr)gacc.stdErrFileName, "$tempStdErr");
  387.     CtoPstr((Ptr)(gacc.stdInFileNameP));
  388.     gacc.callerName = NewPtr(strlen(callerName)+1);
  389.     if (MemError() != noErr)
  390.         goto PathTrouble;
  391.     strcpy(gacc.callerName, callerName);
  392.     
  393.     return;
  394. PathTrouble:
  395.     OKStopAlert("Out of memory right at the start! \
  396. Blew the tubes trying to allocate standard file names.");
  397.     ExitToShell();
  398.     }
  399.  
  400. /* Index through folders in application's folder, looking for
  401. "\pDrag_on Modules". If found, index through files in folder and add
  402. any resource files found. */
  403. void ShowResourcesInMenu(short vRefNumForApp, 
  404.     char *codeFolderName, short menuID)
  405.     {
  406.     long    theDirID;
  407.     
  408.     if (theDirID = FindCodeResFolder(vRefNumForApp, codeFolderName))
  409.         AddCodesToMenu(theDirID, menuID);
  410.     }
  411.  
  412. long FindCodeResFolder(short vRefNumForApp, char *codeFolderName)
  413.     {
  414.     HFileInfo    myCPB;
  415.     WDPBRec        theParms;
  416.     char        fName[32];
  417.     long        theDirID;
  418.     short            index = 1, len;
  419.     OSErr        err;
  420.     
  421.     /* First extract "\pVolName:", volRef, and dirID for application folder */
  422.     theParms.ioNamePtr = (StringPtr)(codeFolder.volName);
  423.     theParms.ioVRefNum = vRefNumForApp;
  424.     theParms.ioWDIndex = 0;
  425.     theParms.ioWDProcID = 0;
  426.     theParms.ioWDVRefNum = 0;
  427.     if (PBGetWDInfo(&theParms,false))
  428.         return(0L);
  429.     len = codeFolder.volName[0];
  430.     codeFolder.volName[len + 1] = ':';
  431.     codeFolder.volName[0] = len + 1;
  432.     codeFolder.vRefNum = theParms.ioWDVRefNum;
  433.     theDirID = theParms.ioWDDirID;
  434.     
  435.     myCPB.ioNamePtr = (StringPtr)fName;
  436.     myCPB.ioVRefNum = vRefNumForApp;
  437.     do
  438.         {
  439.         myCPB.ioFDirIndex = index;
  440.         myCPB.ioDirID = theDirID;
  441.         if ((err = PBGetCatInfo(&myCPB, FALSE)) == noErr)
  442.             {
  443.             if (((myCPB.ioFlAttrib>>4) & 0x01) == 1) /* a folder */
  444.                 {
  445.                 if (PasEqualStrs(fName, codeFolderName))
  446.                     return(myCPB.ioDirID);
  447.                 }
  448.             }
  449.         ++index;
  450.         } while (err == noErr);
  451.     return(0L);
  452.     }
  453.  
  454. /* Search thru folder for code resources and add all codes to the menu. */
  455. void AddCodesToMenu(long theDirID, short menuID)
  456.     {
  457.     HFileParam     fp;
  458.     MenuHandle     theMenu;
  459.     char        fName[32];
  460.     short            index = 1;
  461.     OSErr        err;
  462.     Boolean        firstAdd = TRUE;
  463.     
  464.     theMenu = GetMHandle(menuID);
  465.     codeFolder.theDirID = theDirID;
  466.     fp.ioNamePtr = (StringPtr)fName;;
  467.     fp.ioVRefNum = codeFolder.vRefNum;
  468.     fp.ioFVersNum = 0;
  469.     do
  470.         {
  471.         fp.ioFDirIndex = index;
  472.         fp.ioDirID = codeFolder.theDirID;
  473.         if ((err = PBHGetFInfoSync((HParmBlkPtr)&fp)) == noErr)
  474.             {
  475.             if (fp.ioFlFndrInfo.fdType == 'rsrc'
  476.                 && fp.ioFlFndrInfo.fdCreator == 'RSED')
  477.                 {
  478.                 if (firstAdd)
  479.                     {
  480.                     AppendMenu(theMenu, "\P-");
  481.                     firstAdd = FALSE;
  482.                     }
  483.                 AppendMenu(theMenu, (StringPtr)fName);
  484.                 }
  485.             }
  486.         ++index;
  487.         } while (err == noErr);
  488.     /****** Open a working directory for the CODE folder: this is currently required
  489.     for hAWK, but will be eliminated for hAWK's next version. */
  490.     if (!firstAdd)
  491.         OpenWDForCODE();
  492.     }
  493.  
  494. /****** Open a working directory for the CODE folder: this is currently required
  495. for hAWK, but will be eliminated for hAWK's next version. */
  496. void OpenWDForCODE()
  497.     {
  498.     WDPBRec        theParms;
  499.     
  500.     theParms.ioCompletion = NULL;
  501.     theParms.ioVRefNum = codeFolder.vRefNum;
  502.     theParms.ioNamePtr = NULL;
  503.     theParms.ioWDDirID = codeFolder.theDirID;
  504.     theParms.ioWDProcID = 'ERIK';
  505.     if (!PBOpenWD(&theParms, FALSE)) /* IM IV pg 158 */
  506.         codeFolder.vRefNum = theParms.ioVRefNum;
  507.     }
  508.  
  509. /* The main event. Given a pick from your menu, load and call
  510. the resource. Display results if asked to by the resource.
  511. */
  512. void CallResource(short menuID, short item
  513. #if VERSION2 == TRUE
  514.     , Boolean concurrent
  515. #endif
  516.     )
  517.     {
  518.     MenuHandle     theMenu;
  519.     Handle        rHdle;
  520.     CallCode    codeCaller;
  521.     char        codeNameString[64];
  522.     long        eof;
  523.     short            saveVol, refNum;
  524.     
  525.     theMenu = GetMHandle(menuID);
  526.     GetItem(theMenu, item, (StringPtr)codeNameString);
  527.     if (31 < (unsigned char)(codeNameString[0]))
  528.         {
  529.         /* An odd error: if the code resource was added to the menu during
  530.         startup, how can it have a name that is longer than a file name?
  531.         The assumption here is that we are calling something that is not a
  532.         code resource - either the wrong menu or an inappropriate item.
  533.         Uncomment the following alert if you want to double-check this.
  534.         
  535.         OKStopAlert("Code resource name is too long.");
  536.         */
  537.         return;
  538.         }
  539.     gacc.thisCodeName = NULL;
  540.     if (GetVol(NULL, &saveVol))
  541.         saveVol = 0;
  542.     SetVol(NULL, codeFolder.vRefNum);
  543.     /* See if there's enough memory to load the code resource
  544.     - the brute force approach, if anything more strict than necessary. */
  545.     if (HOpenRF(codeFolder.vRefNum, codeFolder.theDirID,
  546.                 (StringPtr)codeNameString, fsRdPerm, &refNum))
  547.         {
  548.         if (saveVol)
  549.             SetVol(NULL, saveVol);
  550.         OKStopAlert("Couldn't open the code resource.");
  551.         return;
  552.         }
  553.     if (GetEOF(refNum, &eof))
  554.         {
  555.         FSClose(refNum);
  556.         if (saveVol)
  557.             SetVol(NULL, saveVol);
  558.         OKStopAlert("Code resource seems damaged or empty - giving up.");
  559.         return;
  560.         }
  561.     FSClose(refNum);
  562.     rHdle = NewHandle(eof + eof/10); /* a guess */
  563.     if (MemError() != noErr)
  564.         {
  565.         if (saveVol)
  566.             SetVol(NULL, saveVol);
  567.         OKStopAlert("Not enough memory to proceed, sorry.");
  568.         return;
  569.         }
  570.     DisposHandle(rHdle);
  571.     rHdle = NULL;
  572.     if ((refNum = HOpenResFile(codeFolder.vRefNum, codeFolder.theDirID,
  573.                 (StringPtr)codeNameString, fsRdPerm)) != -1 && refNum
  574.                 && ResError() == noErr)
  575.         {
  576.         /* load CODE 0, set up extensions etc, lock down and call
  577.         -after, show results */
  578.         rHdle = Get1Resource ('CODE', 0);
  579.         if (!rHdle)
  580.             {
  581.             /* In error thinking it was a code resource - this really isn't fair */
  582.             CloseResFile(refNum);
  583.             if (saveVol)
  584.                 SetVol(NULL, saveVol);
  585.             OKStopAlert("CODE 0 from that resource file seems to be missing.");
  586.             return;
  587.             }
  588. #if VERSION2 == TRUE
  589.         gHawkIsRunning = TRUE;
  590.         XableDrag_ons(menuID, FALSE);
  591.         HiliteMenu(0);
  592. #endif
  593.         HLock(rHdle);
  594.         codeCaller = (CallCode)*rHdle;
  595.         SetUpCodeCommunication(codeNameString);
  596. #if VERSION2 == TRUE
  597.         if (!concurrent)
  598.             gacc.DoEventLoopOnce_Ext = NULL;
  599. #endif
  600.         codeCaller(&gacc);
  601.             
  602.         HUnlock(rHdle);
  603.         ReleaseResource(rHdle);
  604.         CloseResFile (refNum);
  605. #if VERSION2 == TRUE
  606.         XableDrag_ons(menuID, TRUE);
  607. #endif
  608.         }
  609.     else
  610.         OKStopAlert("Could not open the resource fork for that code resource.");
  611.     if (saveVol)
  612.         SetVol(NULL, saveVol);
  613.     /* Show any file requested */
  614.     /* results:
  615.     <= -3 : no action at present
  616.     -2 : show stderr
  617.     -1 : user cancelled or error during dialog - no run
  618.     0  : run OK, do nothing special after
  619.     1  : show stdout
  620.     2  : show and select stdout
  621.     > 2 : no action at present (counts as equivalent to 0)
  622.     */
  623.  
  624. #if VERSION2 == TRUE
  625.     if (!(gHawkIsRunning && gInBackground))
  626.         {
  627.         AnnounceCompletion();
  628.         }
  629.     if (gHawkIsRunning && gInBackground)
  630.         DoNotify();
  631.     else
  632. #endif
  633.  
  634.     if (gacc.result == -2)
  635.         ShowResult(gacc.stdErrFileName);
  636.     else if (gacc.result == 1 || gacc.result == 2)
  637.         {
  638.         if (ShowResult(gacc.stdOutFileName) && gacc.result == 2)
  639.             SelectResult();
  640.         }
  641.  
  642. #if VERSION2 == TRUE
  643.     gHawkIsRunning = FALSE;
  644. #endif
  645.  
  646.     /* A small cleanup after */
  647.     if (gacc.thisCodeName)
  648.         DisposPtr(gacc.thisCodeName);
  649.     }
  650.  
  651. #if VERSION2 == TRUE
  652. void XableDrag_ons(short menuID, Boolean enable)
  653.     {
  654.     MenuHandle menu = GetMHandle(menuID);
  655.     char    mText[64];
  656.     short        i;
  657.     
  658.     if (!menu) return;
  659.     i = CountMItems(menu);
  660.     GetItem(menu, i, mText);
  661.     while (i >= 1 && mText[1] != '-')
  662.         {
  663.         if (enable)
  664.             EnableItem(menu, i);
  665.         else
  666.             DisableItem(menu, i);
  667.         --i;
  668.         GetItem(menu, i, mText);
  669.         }
  670.     }
  671.  
  672. /* Post a notify. Beep, small icon, diamond beside calling app's name.
  673. NOTE when the calling app resumes, it should remove this notify and
  674. show results, with
  675.         if (gNotifying)
  676.             {
  677.             NMRemove(&gNotifyRec);
  678.             if (gNotifyRec.nmIcon)
  679.                 ReleaseResource(gNotifyRec.nmIcon);
  680.             gNotifying = FALSE;
  681.             ShowResultsAfterNotify();
  682.             }
  683. */
  684. void DoNotify()
  685.     {
  686.     OSErr nmError;
  687.     
  688.     if (!gInBackground) return;
  689.     gNotifyRec.qType = nmType;
  690.     gNotifyRec.nmMark = 1;
  691.     gNotifyRec.nmIcon = GetResource('SICN', 128); /* or NULL */
  692.     HNoPurge(gNotifyRec.nmIcon);
  693.     gNotifyRec.nmSound = (Handle)-1L;
  694.     gNotifyRec.nmStr = 0L;
  695.     gNotifyRec.nmResp = (NMProcPtr)0L;
  696.     gNotifyRec.nmRefCon = 0L;
  697.     
  698.     nmError = NMInstall(&gNotifyRec);
  699.     gNotifying = TRUE;
  700.     }
  701.  
  702. void ShowResultsAfterNotify()
  703.     {
  704.     if (gacc.result == -2)
  705.         ShowResult(gacc.stdErrFileName);
  706.     else if (gacc.result == 1 || gacc.result == 2)
  707.         {
  708.         if (ShowResult(gacc.stdOutFileName) && gacc.result == 2)
  709.             SelectResult();
  710.         }
  711.     FlushEvents(activMask+mDownMask+mUpMask, 0);
  712.     }
  713.  
  714. void AnnounceCompletion()
  715.     {
  716.     long    startTime, endTime;
  717.     
  718.     
  719.     SysBeep(2);
  720.     Delay(0L, &startTime);
  721.     endTime = startTime;
  722.     while (endTime - startTime < 50L)
  723.         Delay(0L, &endTime);
  724.     FlushEvents(62, 0);
  725.     }
  726.  
  727. #endif
  728.  
  729.  
  730. /* If you don't support some extensions, set the pointer for it to NULL here. */
  731. Boolean SetUpCodeCommunication(char *progName)
  732.     {
  733.     short    len;
  734. #if SUPPORT_LEVEL >= BASICTEXT
  735.     /* MyTextInFront(), which should return TRUE if one of your text
  736.     windows is in front or second from front (normally at the time of the call
  737.     the hAWK setup dialog will be the front window).
  738.     */
  739.     extern Boolean MyTextInFront(void);
  740. #if SUPPORT_LEVEL == CUSTOMSUPPORT
  741.     /* CanDoMultiFiles() should return TRUE if the user has selected
  742.     any files for multi-file operations, however you define that.
  743.     */
  744.     extern Boolean CanDoMultiFiles(void);
  745. #endif
  746. #endif
  747.     gacc.result = 0; /* clear before run - 0 means don't do anything after */
  748.     
  749. #if VERSION2 == TRUE
  750.     gacc.version = 2;
  751. #else
  752.     gacc.version = 1;
  753. #endif
  754.  
  755.     /* Extensions, set up every run */
  756.     gacc.GetScreenHeight_Ext = GetScreenHeight;
  757.     gacc.GetScreenWidth_Ext = GetScreenWidth;
  758. #if SUPPORT_LEVEL >= MINIMAL && SUPPORT_LEVEL <= BASICTEXT
  759. #if SUPPORT_LEVEL == BASICTEXT
  760.     if (MyTextInFront())
  761.         gacc.GetFrontText_Ext = GetFrontText; /* MODIFY GetFrontText()! */
  762.     else
  763.         gacc.GetFrontText_Ext = NULL;
  764. #else
  765.         gacc.GetFrontText_Ext = NULL;
  766. #endif
  767.     gacc.InDictionary_Ext = NULL;
  768.     gacc.GetNextMultiFile_Ext = NULL;
  769.     gacc.OKStopAlert_Ext = NULL;
  770.     gacc.MemoryAlert_Ext = NULL;
  771.     gacc.ShowWatchCursor_Ext = NULL;
  772. #if VERSION2 == TRUE
  773.     gacc.DoEventLoopOnce_Ext = DoEventLoopOnce;
  774.     gacc.GetAppClip_Ext = NULL;
  775. #else
  776.     gacc.DoEventLoopOnce_Ext = NULL;
  777.     gacc.GetAppClip_Ext = NULL;
  778. #endif
  779. #else /* CUSTOMSUPPORT - check all extension functions below. */
  780.     gacc.InDictionary_Ext = InDictionary;
  781.     if (MyTextInFront())
  782.         gacc.GetFrontText_Ext = GetFrontText;
  783.     else
  784.         gacc.GetFrontText_Ext = NULL;
  785.     if (CanDoMultiFiles())
  786.         gacc.GetNextMultiFile_Ext = GetNextMultiFile;
  787.     else
  788.         gacc.GetNextMultiFile_Ext = NULL;
  789.     gacc.OKStopAlert_Ext = OKStopAlert;
  790.     gacc.MemoryAlert_Ext = MemoryAlert;
  791.     
  792.     gacc.ShowWatchCursor_Ext = ShowWatchCursor;
  793. #if VERSION2 == TRUE
  794.     gacc.DoEventLoopOnce_Ext = DoEventLoopOnce;
  795.     gacc.GetAppClip_Ext = GetAppClip;
  796. #else
  797.     gacc.DoEventLoopOnce_Ext = NULL;
  798.     gacc.GetAppClip_Ext = NULL;
  799. #endif
  800. #endif
  801.     
  802.     if (gacc.extendID == 'VER3')
  803.         gacc.PutAppClip_Ext = PutAppClip;
  804.     else
  805.         gacc.PutAppClip_Ext = NULL;
  806.     
  807.  
  808.     /* Set up code resource name for each call. */
  809.     len = progName[0];
  810.     gacc.thisCodeName = NewPtr(len+1);
  811.     if (MemError() != noErr)
  812.         return(FALSE); /* No big deal - but suggests code resource will
  813.                         run into trouble real quick if can't get 32 bytes
  814.                         now...*/
  815.     BlockMove(progName, gacc.thisCodeName, len);
  816.     gacc.thisCodeName[len] = '\0';
  817.     return(TRUE);
  818.     }
  819.  
  820. /* This function is called by CallResource() to display the TEXT result
  821. of a code resource run. If your application supports TEXT files, you will
  822. have a function that takes a file name and vRefNum as arguments, and displays
  823. the TEXT file in a window - that's the one to use.
  824.  
  825. A small but important complication: if the file is already being displayed
  826. in a window, your application should close it without asking to save
  827. changes, and then reread it from disk. Changes not saved, because these
  828. are temporary files needed for communication between the application and
  829. the code resource. */
  830. Boolean ShowResult(char *name)
  831.     {
  832. #if SUPPORT_LEVEL >= RESULTSONLY
  833.     short vrefnum;
  834.     char filename[32];
  835.     short len = strlen(name);
  836.     Ptr    endPtr = name + len, startPtr = endPtr;
  837.  
  838.     extern Boolean MyAppShowResult(short vRefNum, char *fileName);
  839.  
  840.  
  841.     while (startPtr >= name)
  842.         {
  843.         if (*startPtr == ':')
  844.             break;
  845.         --startPtr;
  846.         }
  847.     ++startPtr;
  848.     if (startPtr >= endPtr) return(FALSE);
  849.     filename[0] = endPtr - startPtr;
  850.     BlockMove(startPtr, filename+1, filename[0]);
  851.     
  852.     vrefnum = OpenWorkingDirectoryFromFullName(name, (short)(startPtr - name));
  853.     if (!vrefnum) return(FALSE);
  854.     return(MyAppShowResult(vrefnum, filename));
  855. #else
  856.     return(FALSE);
  857. #endif
  858.  
  859. /* For reference, the entire "MyAppShowResult" function for EnterAct is
  860.     if (wdPtr = IsFileOpen(fileName, vRefNum))
  861.         DoForcedCloseWindow(wdPtr);
  862.     return(DoOpenFile(text, FALSE, vRefNum, fileName));
  863. */
  864.  
  865.     }
  866.  
  867. /* This function should select all of the text in the front window, after
  868. checking that the front window is indeed a text window. */
  869. void SelectResult()
  870.     {
  871. #if SUPPORT_LEVEL >= RESULTSONLY
  872.     extern void MyAppSelectResult(void);
  873.     
  874.     MyAppSelectResult();
  875. #endif
  876.     }
  877.  
  878. /* Pascal strings */
  879.  
  880. /* Copy one pascal string to another */
  881. void CopyPStr(Byte *srcStr, Byte *dstStr)
  882.     {
  883.     long   srcLen = srcStr[0];
  884.  
  885.     BlockMove(srcStr, dstStr, srcLen + 1);
  886.     }
  887.  
  888. /* Append pascal s2 to pascal s1, avoiding overflow. */
  889. void AppendPStr(Byte *s1, Byte *s2)
  890.     {
  891.     short    s1Len = s1[0];
  892.     short    s2Len = s2[0];
  893.  
  894.     if (s1Len + s2Len > 255)
  895.         s2Len = 255 - s1Len;
  896.  
  897.     if (s2Len)
  898.         {
  899.         BlockMove (s2 + 1, s1 + s1Len + 1, s2Len);
  900.         s1Len += s2Len;
  901.         s1[0] = s1Len;
  902.         }
  903.     }
  904.  
  905. Boolean PasEqualStrs(char *aStr, char *bStr)
  906.     {
  907.     short i, lena = aStr[0], lenb = bStr[0];
  908.     
  909.     if (!lena || !lenb || lena != lenb)return(FALSE);
  910.     for (i = 1; i <= lena; ++i)
  911.         {
  912.         if (aStr[i] != bStr[i])
  913.             return(FALSE);
  914.         }
  915.     return(TRUE);
  916.     }
  917.  
  918. /* Files, names and locations */
  919.  
  920. /* NOTE the following two functions are based on examples supplied
  921. by Apple on one of their DTS disks - error checking has been added,
  922. and these versions are independent of the signed vs unsigned char
  923. controversy surrounding str255. Byte is defined in MacTypes.h
  924. for THINK C v4. */
  925.  
  926. /* Warning, these calls can fail! And why not? Everything else can... */
  927. /* Bug, these two are not for use by unix imitations such as A/UX. */
  928.  
  929. /* Construct "\PDisk:folder1:folder2:...folderN:" where folderN
  930. contains the file of interest. */
  931. Byte *FullPathNameFromDirectory(long DirID, short vRefNum, Byte *s)
  932.     {
  933.     CInfoPBRec    pb;
  934.     Byte        directoryName[256];
  935.  
  936.     s[0] = 0;
  937.     pb.dirInfo.ioNamePtr = (StringPtr)directoryName;
  938.     pb.dirInfo.ioDrParID = DirID;
  939.  
  940.     do 
  941.         {
  942.         pb.dirInfo.ioVRefNum = vRefNum;
  943.         pb.dirInfo.ioFDirIndex = -1;
  944.         pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
  945.         if (PBGetCatInfo(&pb, FALSE))
  946.             {
  947.             break;
  948.             }
  949.         /* Append a colon  */
  950.         AppendPStr(directoryName, (Byte *)"\p:");
  951.         AppendPStr(directoryName, s);
  952.         CopyPStr(directoryName, s);
  953.         } while (pb.dirInfo.ioDrDirID != 2);
  954.     return(s);
  955.     }
  956.  
  957.  
  958. Byte *FullPathNameFromVRefNum(short vRefNum, Byte *s)
  959.     {
  960.  
  961.     WDPBRec    pb;
  962.  
  963.     pb.ioNamePtr = NULL;
  964.     pb.ioVRefNum = vRefNum;
  965.     pb.ioWDIndex = 0;
  966.     pb.ioWDProcID = 0;
  967.  
  968.     if (PBGetWDInfo(&pb,false))
  969.         {
  970.         s[0] = 0;
  971.         return(s);
  972.         }
  973.     return(FullPathNameFromDirectory(pb.ioWDDirID,pb.ioWDVRefNum,s));
  974.     }
  975.  
  976. /* Determine working directory for file based on full path name. */
  977. short    OpenWorkingDirectoryFromFullName(char *name, short len)
  978.     {
  979.     WDPBRec        theParms;
  980.     OSErr     IOResult;
  981.     char volname[256];
  982.     
  983.     volname[0] = len;
  984.     BlockMove(name, volname+1, len);
  985.     
  986.     theParms.ioCompletion = NULL;
  987.     theParms.ioVRefNum = 0;
  988.     theParms.ioNamePtr = (StringPtr)volname;
  989.     theParms.ioWDDirID = 0;
  990.     theParms.ioWDProcID = 'ERIK';
  991.     if (IOResult = PBOpenWD(&theParms, FALSE)) /* IM IV pg 158 */
  992.         {
  993.         OKStopAlert("Disk may not be on-line, \
  994. or file may have been moved, deleted, or renamed.");
  995.         theParms.ioVRefNum = 0;
  996.         }
  997.     return(theParms.ioVRefNum);
  998.     }
  999.  
  1000.  
  1001. /* The extension functions.*/
  1002.  
  1003. /* InDictionary() returns the general C type
  1004. of a word, according to the following table:
  1005. value    C type
  1006. 0        none - keyword, comment word, local variable, operator etc
  1007. 1        #define or macro name    eg    #define TAB '\t'
  1008. 2        variable name with more than function scope
  1009. 4        function or method name
  1010. 8        enum constant
  1011. 16        typedef name
  1012. 32        struct tag
  1013. 64        union tag
  1014. 128        enum tag
  1015.  
  1016. See hAWK cross-referencing programs for an example of usage. It's unlikely
  1017. that you will be able to provide an equivalent for this function, and
  1018. there’s no great loss if you don’t.
  1019.  */
  1020. short InDictionary(char *tokenName)
  1021.     {
  1022. #if SUPPORT_LEVEL == CUSTOMSUPPORT
  1023.     extern short InMyAppDictionary(char *s);
  1024.     
  1025.     return(InMyAppDictionary(tokenName));
  1026. #else
  1027.     return(0);
  1028. #endif
  1029.     }
  1030.  
  1031. /* This extension function should copy all or the selected part of the
  1032. text in the frontmost window to a Handle. If the front window is not a
  1033. text window, you should try the second-front window - if it isn't text
  1034. either, return NULL. Front OR second front, because a dialog window may be
  1035. the front window at the time - this is the case with hAWK, for example.
  1036. */
  1037. Handle GetFrontText(Boolean getItAll)
  1038.     {
  1039. #if SUPPORT_LEVEL >= BASICTEXT
  1040.     extern Handle MyAppGetFrontText(Boolean getItAll);
  1041.     
  1042.     return(MyAppGetFrontText(getItAll));
  1043. #else
  1044.     return(NULL);
  1045. #endif
  1046.     }
  1047.  
  1048. /* This function should retrieve file names and vRefNums from a one
  1049. or two-dimensional list of files. If *panePtr == -1, you are being asked
  1050. for the first file, otherwise the next file. When there are no more files,
  1051. set *indexPtr = -1. If you have a two-dimensional list, think of panePtr as
  1052. the column index and indexPtr as the row index. For a one-dimensional list,
  1053. use indexPtr to keep track of where you are - just remember to set panePtr to
  1054. something != -1 during the first call. Other than setting panePtr to something
  1055. besides -1 during the first call, and setting indexPtr to -1 when there are
  1056. no more files, you can use them for tracking which file comes next in any way
  1057. you want.
  1058. If you perhaps use full path names, see OpenWorkingDirectoryFromFullName()
  1059. above for hints on how to convert to filename/vRefNum.
  1060. clearFlag TRUE means clear the file from your list; FALSE means leave it in the
  1061. list. Normally FALSE is best - someone might want to reuse the list quite soon,
  1062. as in running two hAWK programs on the same list of files.
  1063. */
  1064. void GetNextMultiFile(short *panePtr, short *indexPtr, 
  1065.             short *vRefNumPtr, char *fileName, Boolean clearFlag)
  1066.     {
  1067. #if SUPPORT_LEVEL == CUSTOMSUPPORT
  1068.     extern void GetNextFileToSearch(short *panePtr, short *indexPtr, 
  1069.         short *vRefNumPtr, char *fileName, Boolean clearFlag);
  1070.     
  1071.     GetNextFileToSearch(panePtr, indexPtr, 
  1072.                 vRefNumPtr, fileName, clearFlag);
  1073. #else
  1074.     *indexPtr = -1;
  1075. #endif
  1076.     }
  1077.  
  1078. /* If you have an alert mechanism with just an OK button that accepts C
  1079. strings, insert it here. Return of 1 means alert was shown and user
  1080. clicked OK, return of 0 means the alert was not shown. If this happens,
  1081. it would be quite OK for your alert function to try to get more memory or
  1082. show an out-of memory alert before returning the 0 or 1. */
  1083. short OKStopAlert(Ptr cstringPtr)
  1084.     {
  1085. #if SUPPORT_LEVEL == CUSTOMSUPPORT
  1086. /* Left in from EnterAct as an example - EnterAct uses FlexAlert(),
  1087. essentially as published in MacTutor Jan '91. As a small refinement, it
  1088. will display an out-of-memory alert if it runs into trouble. FlexAlert
  1089. sizes the alert box to fit the text, and also formats the text nicely. */
  1090.     extern short FlexAlert(short buttonMode, short whichIcon, Ptr csPtr);
  1091. #define JUSTOK    0
  1092. #define STOPICON    0
  1093.     return(FlexAlert(JUSTOK, STOPICON, cstringPtr));
  1094. #else
  1095.     return(0);
  1096. #endif
  1097.     }
  1098.  
  1099. /* Advise the user that memory has run out during code resource execution.
  1100. You must have something for this kicking around in your application - a
  1101. text message is much more likeable than a beep if things fo worng. */
  1102. void MemoryAlert()
  1103.     {
  1104. #if SUPPORT_LEVEL == CUSTOMSUPPORT
  1105.     extern void DoMemoryAlert(short msgNum, long memLimit);
  1106.  
  1107. #define NONELEFT        4
  1108.  
  1109.     DoMemoryAlert(NONELEFT, 0L);
  1110. #else
  1111.     SysBeep(2);
  1112. #endif
  1113.     }
  1114.  
  1115. short GetScreenHeight(void)
  1116.     {
  1117.     return(screenBits.bounds.bottom - screenBits.bounds.top);
  1118.     }
  1119.  
  1120. short GetScreenWidth()
  1121.     {
  1122.     return(screenBits.bounds.right - screenBits.bounds.left);
  1123.     }
  1124.  
  1125. void ShowWatchCursor()
  1126.     {
  1127. #if SUPPORT_LEVEL == CUSTOMSUPPORT
  1128.     extern void MySetWatchCursor(void);
  1129.     
  1130.     MySetWatchCursor();
  1131. #endif
  1132.  
  1133.     /* this goes something like:
  1134.     CursHandle        Watch; -- ToolboxUtil.h  enum constant --
  1135.     Watch = GetCursor(watchCursor);
  1136.     if (Watch)
  1137.         SetCursor (*Watch);
  1138.     else
  1139.         InitCursor();
  1140.     */
  1141.     }
  1142.  
  1143. void DoEventLoopOnce()
  1144.     {
  1145. #if VERSION2 == TRUE
  1146.     extern void HandleOneEvent(void);
  1147.     
  1148.     HandleOneEvent();
  1149. #endif
  1150.     }
  1151.  
  1152. Handle GetAppClip()
  1153.     {
  1154. #if SUPPORT_LEVEL == CUSTOMSUPPORT
  1155. #if VERSION2 == TRUE
  1156.     extern pascal Handle    PEGetScrap(void);
  1157.     return(PEGetScrap());
  1158. #endif
  1159. #endif
  1160.     return(NULL);
  1161.     }
  1162.  
  1163. short PutAppClip(char *newClipStr)
  1164.     {
  1165.     // Put the C string newClipStr on your application's private clip,
  1166.     // as for example make a handle here, put the string in it, and
  1167.     // set TextEdit's scrap to the new handle.
  1168.     // return 0 if failure, 1 if success.
  1169.     return 0;
  1170.     }
  1171.