home *** CD-ROM | disk | FTP | other *** search
/ BMUG PD-ROM A / PD-ROM A.iso / Utility / Virus Checkers / Disinfectant 2.5.1 / Source Code for 2.4 / Source Code / utl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-22  |  57.6 KB  |  1,948 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________________________
  2.  
  3.     utl.c - Utilities.
  4.     
  5.     Copyright © 1988, 1989, 1990 Northwestern University.  Permission is granted
  6.     to use this code in your own projects, provided you give credit to both
  7.     John Norstad and Northwestern University in your about box or document.
  8.     
  9.     This module exports miscellaneous reusable utility routines.
  10. _____________________________________________________________________*/
  11.  
  12.  
  13. #pragma load "precompile"
  14. #include "utl.h"
  15.  
  16. #pragma segment utl
  17.  
  18.  
  19. /*______________________________________________________________________
  20.  
  21.     Global Variables.
  22. _____________________________________________________________________*/
  23.  
  24.  
  25. static SysEnvRec        TheWorld;        /* system environment record */
  26. static Boolean            GotSysEnviron = false;    
  27.                                                 /* true if sys environ has been
  28.                                                     gotten */
  29.     
  30.             
  31. static CursHandle     *CursArray;        /* ptr to array of cursor handles */
  32. static short             NumCurs;            /* number of cursors to rotate */
  33. static short             TickInterval;    /* number of ticks between rotations */
  34. static short             CurCurs;            /* index of current cursor */
  35. static short             LastTick;        /* tick count at loast rotation */
  36.  
  37. static ModalFilterProcPtr    Filter;    /* dialog filter proc */
  38. static short            CancelItem;        /* item number of cancel button */
  39.  
  40. /*______________________________________________________________________
  41.  
  42.     GetSysEnvirons - Get System Environment.
  43.     
  44.     Exit:        global variable TheWorld = system environment record.
  45.                 global variable GotSysEnviron = true.
  46. _____________________________________________________________________*/
  47.  
  48.  
  49. static void GetSysEnvirons (void)
  50.  
  51. {
  52.     if (!GotSysEnviron) {
  53.         (void) SysEnvirons(curSysEnvVers, &TheWorld);
  54.         GotSysEnviron = true;
  55.     };
  56. }
  57.  
  58. /*______________________________________________________________________
  59.  
  60.     utl_AppendDITL - Append DITL to End of Dialog.
  61.     
  62.     Entry:    theDialog = pointer to dialog.
  63.                 theDITLID = rsrc id of DITL.
  64.                     
  65.     Exit:        function result = item number of first appended item.
  66.     
  67.     The dialog window is expanded to accomodate the new items, and the
  68.     new items are offset to appear at the bottom of the dialog.
  69.     
  70.     This routine is particularly useful for appending items to the
  71.     standard Page Setup and Print Job dialogs.  (See TN 95).  It was
  72.     written by Lew Rollins of Apple's Human-Systems Interface Group,
  73.     in MPW Pascal.  I translated it to MPW C.
  74.     
  75.     The only significant difference between this routine and Rollin's
  76.     version is that this version does not release the DITL resource.
  77. _____________________________________________________________________*/
  78.  
  79.  
  80. short utl_AppendDITL (DialogPtr theDialog, short theDITLID)
  81.  
  82. {
  83.     typedef struct DITLItem {
  84.         Handle            itmHndl;                /* handle or proc ptr */
  85.         Rect                itmRect;                /* display rect */
  86.         char                itmType;                /* item type */
  87.         unsigned char    itmData;                /* item data length byte */
  88.     } DITLItem;
  89.     
  90.     typedef struct itemList {
  91.         short                dlgMaxIndex;        /* num items - 1 */
  92.         DITLItem            DITLItems[1];        /* array of DITL items */
  93.     } itemList;
  94.     
  95.     short                offset;            /* item offset */
  96.     Rect                maxRect;            /* max dialog rect size so far */
  97.     Handle            hDITL;            /* handle to DITL */
  98.     DITLItem            *pItem;            /* pointer to item being appended */
  99.     itemList            **hItems;        /* handle to DLOG's item list */
  100.     short                sizeDITL;        /* size of DLOG's item list */
  101.     short                firstItem;        /* item num of first appended item */
  102.     short                newItems;        /* number of new items */
  103.     short                dataSize;        /* size of data for current item */
  104.     short                i;                    /* loop index */
  105.     
  106.     /* Initialize. */
  107.     
  108.     maxRect = theDialog->portRect;
  109.     offset = maxRect.bottom;
  110.     maxRect.bottom -= 5;
  111.     maxRect.right -= 5;
  112.     hItems = (itemList**)(((DialogPeek)theDialog)->items);
  113.     sizeDITL = GetHandleSize((Handle)hItems);
  114.     firstItem = (**hItems).dlgMaxIndex + 2;
  115.     hDITL = GetResource('DITL', theDITLID);
  116.     HLock(hDITL);
  117.     newItems = **(short**)hDITL + 1;
  118.     PtrAndHand(*hDITL+2, (Handle)hItems,
  119.         GetHandleSize(hDITL)-2);
  120.     (**hItems).dlgMaxIndex += newItems;
  121.     HUnlock(hDITL);
  122.     HLock((Handle)hItems);
  123.     (char*)pItem = (char*)(*hItems) + sizeDITL;
  124.     
  125.     /* Main loop.  Add each item to dialog item list. */
  126.     
  127.     for (i = 1; i <= newItems; i++) {
  128.         OffsetRect(&pItem->itmRect, 0, offset);
  129.         UnionRect(&pItem->itmRect, &maxRect, &maxRect);
  130.         switch (pItem->itmType & 0x7f) {
  131.             case ctrlItem+btnCtrl:
  132.             case ctrlItem+chkCtrl:
  133.             case ctrlItem+radCtrl:
  134.                 pItem->itmHndl = (Handle)NewControl(theDialog, 
  135.                     &pItem->itmRect, &pItem->itmData, true, 0, 0, 1, 
  136.                     pItem->itmType & 0x03, 0);
  137.                 break;
  138.             case ctrlItem+resCtrl:
  139.                 pItem->itmHndl = (Handle)GetNewControl(
  140.                     *(short*)(&pItem->itmData+1),
  141.                     theDialog);
  142.                 (**((ControlHandle)(pItem->itmHndl))).contrlRect = 
  143.                     pItem->itmRect;
  144.                 break;
  145.             case statText:
  146.             case editText:
  147.                 PtrToHand(&pItem->itmData+1, &(Handle)(pItem->itmHndl), 
  148.                     pItem->itmData);
  149.                 break;
  150.             case iconItem:
  151.                 pItem->itmHndl = GetIcon(*(short*)(&pItem->itmData+1));
  152.                 break;
  153.             default:
  154.                 pItem->itmHndl = nil;
  155.         };
  156.         dataSize = (pItem->itmData + 1) & 0xfffe;
  157.         (char*)pItem += dataSize + sizeof(DITLItem);
  158.     };
  159.     
  160.     /* Finish up. */
  161.     
  162.     HUnlock((Handle)hItems);
  163.     maxRect.bottom += 5;
  164.     maxRect.right += 5;
  165.     SizeWindow(theDialog, maxRect.right, maxRect.bottom, true);
  166.     return firstItem;
  167. }
  168.  
  169. /*______________________________________________________________________
  170.  
  171.     utl_CenterDlogRect - Center a dialog rectangle.
  172.     
  173.     Entry:    rect = rectangle.
  174.                 centerMain = true to center on main (menu bar) screen.
  175.                 centerMain = false to center on the screen containing
  176.                     the maximum intersection with the frontmost window.
  177.     
  178.     Exit:        rect = rectangle offset so that it is centered on
  179.                     the specified screen, with twice as much space below
  180.                     the rect as above.
  181.                     
  182.     See HIN 6.
  183. _____________________________________________________________________*/
  184.  
  185.  
  186. void utl_CenterDlogRect (Rect *rect, Boolean centerMain)
  187.  
  188. {
  189.     Rect        screenRect;            /* screen rectangle */
  190.     short        mBHeight;            /* menu bar height */
  191.     GDHandle    gd;                    /* gdevice */
  192.     Rect        windRect;            /* window rectangle */
  193.     Boolean    hasMB;                /* true if screen contains menu bar */
  194.  
  195.     mBHeight = utl_GetMBarHeight();
  196.     if (centerMain) {
  197.         screenRect = qd.screenBits.bounds;
  198.     } else {
  199.         utl_GetWindGD(FrontWindow(), &gd, &screenRect, &windRect, &hasMB);
  200.         if (!hasMB) mBHeight = 0;
  201.     };
  202.     OffsetRect(rect,
  203.         (screenRect.right + screenRect.left - rect->right - rect->left) >> 1,
  204.         (screenRect.bottom + ((screenRect.top + mBHeight - rect->top)<<1) - 
  205.             rect->bottom + 7) / 3);
  206. }
  207.  
  208. /*______________________________________________________________________
  209.  
  210.     utl_CenterRect - Center a rectangle on the main screen.
  211.     
  212.     Entry:    rect = rectangle.
  213.     
  214.     Exit:        rect = rectangle offset so that it is centered on
  215.                     the main screen.
  216. _____________________________________________________________________*/
  217.  
  218.  
  219. void utl_CenterRect (Rect *rect)
  220.  
  221. {
  222.     Rect        screenRect;            /* main screen rectangle */
  223.     short        mBHeight;            /* menu bar height */
  224.  
  225.     mBHeight = utl_GetMBarHeight();
  226.     screenRect = qd.screenBits.bounds;
  227.     OffsetRect(rect,
  228.         (screenRect.right + screenRect.left - rect->right - rect->left) >> 1,
  229.         (screenRect.bottom - screenRect.top + mBHeight - 
  230.             rect->bottom - rect->top) >> 1);
  231. }
  232.  
  233. /*______________________________________________________________________
  234.  
  235.     utl_CheckPack - Check to see if a package exists.
  236.     
  237.     Entry:    packNum = package number.
  238.                 preload = true to preload package.
  239.                     
  240.     Exit:        function result = true if package exists.
  241. _____________________________________________________________________*/
  242.  
  243.  
  244. Boolean utl_CheckPack (short packNum, Boolean preload)
  245.  
  246. {
  247.     short            trapNum;            /* trap number */
  248.     Handle        h;                    /* handle to PACK resource */
  249.     
  250.     /* Check to make sure the trap exists, by comparing its trap address to
  251.         the trap address of the unimplemented trap. */
  252.     
  253.     trapNum = packNum + 0x1e7;
  254.     if (NGetTrapAddress(trapNum & 0x3ff, ToolTrap) == 
  255.         NGetTrapAddress(_Unimplemented & 0x3ff, ToolTrap)) return false;
  256.         
  257.     /* Check to make sure the package exists on the System file or in 
  258.         ROM.  If it's not in ROM make it nonpurgeable, if requested. */
  259.     
  260.     if (preload) {
  261.         if (utl_Rom64()) {
  262.             h = GetResource('PACK', packNum);
  263.             if (!h) return false;
  264.             HNoPurge(h);
  265.         } else {
  266.             *(unsigned short*)RomMapInsert = 0xFF00;
  267.             h = GetResource('PACK', packNum);
  268.             if (!h) return false;
  269.             if ((*(unsigned long*)h & 0x00FFFFFF) < 
  270.                 *(unsigned long*)ROMBase) {
  271.                 h = GetResource('PACK', packNum);
  272.                 HNoPurge(h);
  273.             };
  274.         };
  275.         return true;
  276.     } else {
  277.         SetResLoad(false);
  278.         if (!utl_Rom64()) *(unsigned short*)RomMapInsert = 0xFF00;
  279.         h = GetResource('PACK', packNum);
  280.         SetResLoad(true);
  281.         if (h) return true; else return false;
  282.     };
  283. }
  284.  
  285. /*______________________________________________________________________
  286.  
  287.     utl_CopyPString - Copy Pascal String.
  288.     
  289.     Entry:    dest = destination string.
  290.                 source = source string.
  291. _____________________________________________________________________*/
  292.  
  293.  
  294. void utl_CopyPString (Str255 dest, Str255 source)
  295.  
  296. {
  297.     memcpy(dest, source, *source+1);
  298. }
  299.  
  300. /*______________________________________________________________________
  301.  
  302.     utl_CouldDrag - Determine if a window could be dragged to a location.
  303.     
  304.     Entry:    windRect = window rectangle, in global coords.
  305.                 offset = pixel offset used in DragRect calls.
  306.     
  307.     Exit:        function result = true if the window could have been
  308.                     dragged to the specified position.
  309.                     
  310.     This routine is used when restoring windows to saved positions.  According
  311.     to HIN 6, we must check to see if the window "could have been dragged to
  312.     the saved position."
  313.     
  314.     The "offset" parameter is usually 4.  When initializing the boundary rectangle
  315.     for DragWindow calls, normally the boundary rectangle of the desktop gray
  316.     region is inset by 4 pixels.  If some value other than 4 is used, it should
  317.     be passed to CouldDrag as the "offset" parameter.
  318.     
  319.     The algorithm used is the following:  The routine computes the four squares
  320.     at the corners of the title bar.  "true" is returned if and only if at least one 
  321.     of these four squares is completely contained within the desktop gray region.
  322.     
  323.     Three pixels are added to the offset to err on the side of requiring a larger
  324.     portion of the drag bar to be visible.  
  325. _____________________________________________________________________*/
  326.  
  327.  
  328. Boolean utl_CouldDrag (Rect *windRect, short offset)
  329.  
  330. {
  331.     RgnHandle            rgn;            /* scratch region handle */
  332.     Boolean                could;        /* function result */
  333.     short                    corner;        /* which corner */
  334.     Rect                    r;                /* corner rectangle */
  335.  
  336.     rgn = NewRgn();
  337.     could = false;
  338.     offset += 3;
  339.     for (corner = 1; corner <= 4; corner++) {
  340.         switch (corner) {
  341.             case 1:
  342.                 r.top = windRect->top - titleBarHeight;
  343.                 r.left = windRect->left;
  344.                 break;
  345.             case 2:
  346.                 r.top = windRect->top - offset;
  347.                 r.left = windRect->left;
  348.                 break;
  349.             case 3:
  350.                 r.top = windRect->top - titleBarHeight;
  351.                 r.left = windRect->right - offset;
  352.                 break;
  353.             case 4:
  354.                 r.top = windRect->top - offset;
  355.                 r.left = windRect->right - offset;
  356.                 break;
  357.         };
  358.         r.bottom = r.top + offset;
  359.         r.right = r.left + offset;
  360.         RectRgn(rgn, &r);
  361.         DiffRgn(rgn, *(RgnHandle*)GrayRgn, rgn);
  362.         if (EmptyRgn(rgn)) {
  363.             could = true;
  364.             break;
  365.         };
  366.     };
  367.     DisposeRgn(rgn);
  368.     return could;
  369. }
  370.  
  371. /*______________________________________________________________________
  372.  
  373.     utl_DILoad - Load Disk Initialization Package.
  374.     
  375.     Exit:        Disk initialization package loaded.
  376.                 
  377.     This routine is identical to the DILoad routine (see IM II-396),
  378.     except that it closes any resource files opened by the routine.  This is
  379.     necessary to undo a bug in the DaynaFile software.  DaynaFile patches
  380.     DILoad.  The patch opens a resource file without closing it.  The
  381.     effect on Disinfectant if we don't do anything about this is that 
  382.     the DaynaFile icon is displayed in Disinfectant's main window instead of
  383.     Disinfectant's icon.  
  384. _____________________________________________________________________*/
  385.  
  386.  
  387. void utl_DILoad (void)
  388.  
  389. {
  390.     short            curResFile;            /* ref num of current resource file
  391.                                                 before calling DILoad */
  392.     short            topResFile;            /* ref num of top resource file
  393.                                                 after calling DILoad */
  394.     
  395.     curResFile = CurResFile();
  396.     DILoad();
  397.     while ((topResFile = CurResFile()) != curResFile) CloseResFile(topResFile);
  398. }
  399.  
  400. /*______________________________________________________________________
  401.  
  402.     utl_DoDiskInsert - Handle a disk inserted event.
  403.     
  404.     Entry:    message = message field from disk insertion event record
  405.                     = 16/MountVol return code, 16/drive number.
  406.     
  407.     Exit:        vRefNum = vol ref num of inserted volume.
  408.                 function result = error code.
  409.     
  410.     If MountVol returned an error code, the disk initialization package
  411.     is called to initialize the disk.
  412. _____________________________________________________________________*/
  413.  
  414.  
  415. OSErr utl_DoDiskInsert (long message, short *vRefNum)
  416.  
  417. {
  418.     OSErr                rCode;                /* result code */
  419.     short                driveNum;            /* drive number */
  420.     HParamBlockRec    pBlock;                /* vol info param block */
  421.     Handle            dlgHandle;            /* handle to disk init dialog */
  422.     Rect                dlgRect;                /* disk init dialog rectangle */
  423.     Point                where;                /* location of disk init dialog */
  424.     short                curVol;                /* index in VCB queue */
  425.     
  426.     /* Get result code and drive number from event message. */
  427.     
  428.     rCode = (message >> 16) & 0xffff;
  429.     driveNum = message & 0xffff;
  430.     
  431.     /* If the result code indicates an error, call DIBadMount to initialize
  432.         the disk. */
  433.         
  434.     if (rCode) {
  435.         
  436.         /* Center the disk initialization package dialog. */
  437.         
  438.         DILoad();
  439.         dlgHandle = GetResource('DLOG', -6047);
  440.         dlgRect = **(Rect**)dlgHandle;
  441.         utl_CenterDlogRect(&dlgRect, false);
  442.         SetPt(&where, dlgRect.left, dlgRect.top);
  443.         
  444.         /* Call DIBadMount. */
  445.         
  446.         if (rCode = DIBadMount(where, message)) return rCode;
  447.         
  448.     };
  449.     
  450.     /* Search mounted volumes to find inserted one. */
  451.     
  452.     pBlock.volumeParam.ioNamePtr = nil;
  453.     curVol = 0;
  454.     while (true) {
  455.         pBlock.volumeParam.ioVolIndex = ++curVol;
  456.         pBlock.volumeParam.ioVRefNum = 0;
  457.         if (rCode = PBHGetVInfo(&pBlock, false)) return rCode;
  458.         if (pBlock.volumeParam.ioVDrvInfo == driveNum) break;
  459.     };
  460.     *vRefNum = pBlock.volumeParam.ioVRefNum;
  461.     return noErr;
  462. }    
  463.  
  464. /*______________________________________________________________________
  465.  
  466.     utl_DrawGrowIcon - Draw Grow Icon.
  467.     
  468.     Entry:            theWindow = pointer to window.
  469.     
  470.     This routine is identical to the Window Manager routine 
  471.     DrawGrowIcon, except that it does not draw the lines enclosing the
  472.     scroll bars.
  473. _____________________________________________________________________*/
  474.  
  475.  
  476. void utl_DrawGrowIcon (WindowPtr theWindow) 
  477.  
  478. {
  479.     RgnHandle            clipRgn;            /* saved clip region */
  480.     Rect                    clipRect;        /* clip rectangle */
  481.     
  482.     clipRgn = NewRgn();
  483.     GetClip(clipRgn);
  484.     clipRect = theWindow->portRect;
  485.     clipRect.left = clipRect.right - 15;
  486.     clipRect.top = clipRect.bottom - 15;
  487.     ClipRect(&clipRect);
  488.     DrawGrowIcon(theWindow);
  489.     SetClip(clipRgn);
  490.     DisposeRgn(clipRgn);
  491. }
  492.  
  493. /*______________________________________________________________________
  494.  
  495.     utl_Ejectable - Test for ejectable volume.
  496.     
  497.     Entry:    vRefNum = volume reference number.
  498.     
  499.     Exit:        function result = true if volume is on an ejectable drive.
  500. _____________________________________________________________________*/
  501.  
  502.  
  503. Boolean utl_Ejectable (short vRefNum)
  504.     
  505. {
  506.     HParamBlockRec    pBlock;                /* vol info param block */
  507.     short                driveNum;            /* driver number of cur vol */
  508.     DrvQEl            *curDrive;            /* ptr to current drive queue element */
  509.     OSErr                rCode;                /* result code */
  510.     unsigned char    flagByte;            /* drive queue element flag byte */
  511.     
  512.     /* Get driveNum = drive number of drive containing volume. */
  513.     
  514.     pBlock.volumeParam.ioNamePtr = nil;
  515.     pBlock.volumeParam.ioVolIndex = 0;
  516.     pBlock.volumeParam.ioVRefNum = vRefNum;;
  517.     if (rCode = PBHGetVInfo(&pBlock, false)) return false;
  518.     driveNum = pBlock.volumeParam.ioVDrvInfo;
  519.     
  520.     /*    Walk the drive queue until we find driveNum.  The second byte in
  521.         the four flag bytes preceding the drive queue element is 8 or $48 
  522.         if the drive is nonejectable. */
  523.     
  524.     curDrive = (DrvQEl*)(GetDrvQHdr())->qHead;
  525.     while (true) {
  526.         if (curDrive->dQDrive == driveNum) {
  527.             flagByte = *((Ptr)curDrive - 3);
  528.             return (flagByte != 8 && flagByte != 0x48);
  529.         };
  530.         curDrive = (DrvQEl*)curDrive->qLink;
  531.         if (!curDrive) return false;
  532.     };
  533. }
  534.  
  535. /*______________________________________________________________________
  536.  
  537.     utl_FixStdFile - Fix Standard File Pacakge.
  538.     
  539.     This routine should be called before calling the Standard File 
  540.     package if there's any chance that SFSaveDisk might specify
  541.     a volume that has been umounted.  Standard File gets confused if this
  542.     happens and presents an alert telling the user that a "system error"
  543.     has occurred.
  544.     
  545.     This routine checks to make sure that SFSaveDisk specifies a volume 
  546.     that is still mounted.  If not, it sets it to the first mounted 
  547.     volume, and it sets CurDirStore to the root directory on that volume.
  548. _____________________________________________________________________*/
  549.                     
  550.                     
  551. void utl_FixStdFile (void)
  552.  
  553. {
  554.     ParamBlockRec    vBlock;            /* vol info param block */
  555.                                                 
  556.     vBlock.volumeParam.ioNamePtr = nil;
  557.     vBlock.volumeParam.ioVRefNum = -*(short*)SFSaveDisk;
  558.     vBlock.volumeParam.ioVolIndex = 0;
  559.     if (PBGetVInfo(&vBlock, false)) {
  560.         vBlock.volumeParam.ioVolIndex = 1;
  561.         (void) PBGetVInfo(&vBlock, false);
  562.         *(short*)SFSaveDisk = -vBlock.volumeParam.ioVRefNum;
  563.         if (*(short*)FSFCBLen > 0) *(long*)CurDirStore = fsRtDirID;
  564.     };
  565. }
  566.  
  567. /*______________________________________________________________________
  568.  
  569.     utl_FlashButton - Flash Dialog Button.
  570.     
  571.     Entry:    theDialog = pointer to dialog.
  572.                 itemNo = item number of button to flash.
  573.                 
  574.     The push button is inverted for 8 ticks.  See HIN #10.
  575. _____________________________________________________________________*/
  576.  
  577.  
  578. void utl_FlashButton (DialogPtr theDialog, short itemNo)
  579.  
  580. {
  581.     short            itemType;            /* item type */
  582.     Handle        item;                    /* item handle */
  583.     Rect            box;                    /* item rectangle */
  584.     short            roundFactor;        /* rounded corner factor for InvertRoundRect */
  585.     long            tickEnd;                /* tick count to end flash */
  586.     
  587.     GetDItem(theDialog, itemNo, &itemType, &item, &box);
  588.     SetPort(theDialog);
  589.     roundFactor = (box.bottom - box.top)>>1;
  590.     InvertRoundRect(&box, roundFactor, roundFactor);
  591.     tickEnd = TickCount() + 8;
  592.     while (TickCount() < tickEnd);
  593.     InvertRoundRect(&box, roundFactor, roundFactor);
  594. }
  595.  
  596. /*______________________________________________________________________
  597.  
  598.     utl_FrameItem
  599.     
  600.     Entry:    theWindow = pointer to dialog window.
  601.                 itemNo = dialog item number.
  602.     
  603.     Exit:        dialog item rectangle framed.
  604.                 
  605.     This function is for use as a Dialog Manager user item procedure.
  606.     It is particularly useful for drawing rules (straight lines).  Simply
  607.     define the user item rectangle with bottom=top+1 or right=left+1.
  608. _____________________________________________________________________*/
  609.  
  610.  
  611. pascal void utl_FrameItem (WindowPtr theWindow, short itemNo)
  612.  
  613. {
  614.     short            itemType;            /* item type */
  615.     Handle        item;                    /* item handle */
  616.     Rect            box;                    /* item rectangle */
  617.  
  618.     GetDItem(theWindow, itemNo, &itemType, &item, &box);
  619.     FrameRect(&box);
  620. }
  621.  
  622. /*______________________________________________________________________
  623.  
  624.     utl_GetApplVol - Get the volume reference number of the application volume.
  625.     
  626.     Exit:        function result = volume reference number of the volume 
  627.                     containing the current application.
  628. _____________________________________________________________________*/
  629.  
  630.  
  631. short utl_GetApplVol (void)
  632.  
  633. {
  634.     short            vRefNum;                /* vol ref num */
  635.  
  636.     GetVRefNum(*(short*)(CurMap), &vRefNum);
  637.     return vRefNum;
  638. }
  639.  
  640. /*______________________________________________________________________
  641.  
  642.     utl_GetBlessedWDRefNum - Get the working directory reference number of
  643.         the Blessed Folder.
  644.     
  645.     
  646.     Exit:        function result = wdRefNum of blessed folder
  647. _____________________________________________________________________*/
  648.  
  649.  
  650. short utl_GetBlessedWDRefNum (void)
  651.  
  652. {
  653.     if (!GotSysEnviron) GetSysEnvirons();
  654.     return TheWorld.sysVRefNum;
  655. }
  656.  
  657. /*______________________________________________________________________
  658.  
  659.     utl_GetFontNumber - Get Font Number.
  660.     
  661.     Entry:    fontName = font name.
  662.     
  663.     Exit:        function result = true if font exists.
  664.                 fontNum = font number.
  665.                 
  666.     Copied from TN 191.
  667. _____________________________________________________________________*/
  668.  
  669.  
  670. Boolean utl_GetFontNumber (Str255 fontName, short *fontNum)
  671.  
  672. {
  673.     Str255        systemFontName;
  674.     
  675.     GetFNum(fontName, fontNum);
  676.     if (*fontNum) {
  677.         return true;
  678.     } else {
  679.         GetFontName(0, systemFontName);
  680.         return EqualString(fontName, systemFontName, false, false);
  681.     };
  682. }
  683.  
  684. /*______________________________________________________________________
  685.  
  686.     utl_GetLongSleep - Get Long Sleep Time.
  687.     
  688.     Exit:            function result = long sleep time.
  689.     
  690.     Returns the largest positive long integer (0x7fffffff) if the 
  691.     system version is > 0x04ff, else returns 50.  See TN 177.
  692. _____________________________________________________________________*/
  693.  
  694.  
  695. long utl_GetLongSleep (void)
  696.  
  697. {
  698.     if (!GotSysEnviron) GetSysEnvirons();
  699.     return (TheWorld.systemVersion > 0x04ff) ? 0x7fffffff : 50;
  700. }
  701.  
  702. /*______________________________________________________________________
  703.  
  704.     utl_GetMBarHeight - Get Menu Bar Height
  705.     
  706.     Exit:        function result = menu bar height.
  707.     
  708.     See TN 117.
  709. _____________________________________________________________________*/
  710.  
  711.  
  712. short utl_GetMBarHeight (void)
  713.  
  714. {
  715.     static short        mBHeight = 0;
  716.     
  717.     if (!mBHeight) {
  718.         mBHeight = utl_Rom64() ? 20 : *(short*)MBarHeight;
  719.     };
  720.     return mBHeight;
  721. }
  722.  
  723. /*______________________________________________________________________
  724.  
  725.     utl_GetNewControl - Get New Control.
  726.     
  727.     Entry:    controlID = resource id of CNTL resource.
  728.                 theWindow = pointer to window record.
  729.                 
  730.     Exit:        function result = handle to new control record.
  731.                 
  732.     This routine is identical to the Control Manager routine GetNewControl,
  733.     except it does not release or make purgeable the CNTL resource.
  734. _____________________________________________________________________*/
  735.  
  736.  
  737. ControlHandle utl_GetNewControl (short controlID, 
  738.     WindowPtr theWindow)
  739.     
  740. {
  741.     Rect            boundsRect;            /* boundary rectangle */
  742.     short            value;                /* initial control value */
  743.     Boolean        visible;                /* true if visible */
  744.     short            max;                    /* max control value */
  745.     short            min;                    /* min control value */
  746.     short            procID;                /* window proc id */
  747.     long            refCon;                /* refCon field for window record */
  748.     Str255        title;                /* window title */
  749.     Handle        theRez;                /* handle to CNTL resource */
  750.     
  751.     theRez = GetResource('CNTL', controlID);
  752.     boundsRect = *(Rect*)(*theRez);
  753.     value = *(short*)(*theRez+8);
  754.     visible = *(Boolean*)(*theRez+10);
  755.     max = *(short*)(*theRez+12);
  756.     min = *(short*)(*theRez+14);
  757.     procID = *(short*)(*theRez+16);
  758.     refCon = *(long*)(*theRez+18);
  759.     utl_CopyPString(title, *theRez+22);
  760.     return NewControl(theWindow, &boundsRect, title, visible, value,
  761.         min, max, procID, refCon);
  762. }
  763.  
  764. /*______________________________________________________________________
  765.  
  766.     utl_GetNewDialog - Get New Dialog.
  767.     
  768.     Entry:    dialogID = resource id of DLOG resource.
  769.                 dStorage = pointer to dialog record.
  770.                 behind = window to insert in back of.
  771.                 
  772.     Exit:        function result = pointer to new dialog record.
  773.                 
  774.     This routine is identical to the Dialog Manager routine GetNewDialog,
  775.     except it does not release or make purgeable the DLOG resource.
  776. _____________________________________________________________________*/
  777.  
  778.  
  779. DialogPtr utl_GetNewDialog (short dialogID, Ptr dStorage, 
  780.     WindowPtr behind)
  781.     
  782. {
  783.     Rect            boundsRect;            /* boundary rectangle */
  784.     short            procID;                /* window proc id */
  785.     Boolean        visible;                /* true if visible */
  786.     Boolean        goAwayFlag;            /* true if window has go away box */
  787.     long            refCon;                /* refCon field for window record */
  788.     Str255        title;                /* window title */
  789.     Handle        theRez;                /* handle to DLOG resource */
  790.     short            itemID;                /* rsrc id of item list */
  791.     Handle        items;                /* handle to item list */
  792.     
  793.     theRez = GetResource('DLOG', dialogID);
  794.     boundsRect = *(Rect*)(*theRez);
  795.     procID = *(short*)(*theRez+8);
  796.     visible = *(Boolean*)(*theRez+10);
  797.     goAwayFlag = *(Boolean*)(*theRez+12);
  798.     refCon = *(long*)(*theRez+14);
  799.     itemID = *(short*)(*theRez+18);
  800.     utl_CopyPString(title, *theRez+20);
  801.     items = GetResource('DITL', itemID);
  802.     return NewDialog(dStorage, &boundsRect, title, visible, procID, behind,
  803.         goAwayFlag, refCon, items);
  804. }
  805.  
  806. /*______________________________________________________________________
  807.  
  808.     utl_GetNewWindow - Get New Window.
  809.     
  810.     Entry:    windowID = resource id of WIND resource.
  811.                 wStorage = pointer to window record.
  812.                 behind = window to insert in back of.
  813.                 
  814.     Exit:        function result = pointer to new window record.
  815.                 
  816.     This routine is identical to the Window Manager routine GetNewWindow,
  817.     except it does not release or make purgeable the WIND resource.
  818. _____________________________________________________________________*/
  819.  
  820.  
  821. WindowPtr utl_GetNewWindow (short windowID, Ptr wStorage, 
  822.     WindowPtr behind)
  823.     
  824. {
  825.     Rect            boundsRect;            /* boundary rectangle */
  826.     short            procID;                /* window proc id */
  827.     Boolean        visible;                /* true if visible */
  828.     Boolean        goAwayFlag;            /* true if window has go away box */
  829.     long            refCon;                /* refCon field for window record */
  830.     Str255        title;                /* window title */
  831.     Handle        theRez;                /* handle to WIND resource */
  832.     
  833.     theRez = GetResource('WIND', windowID);
  834.     boundsRect = *(Rect*)(*theRez);
  835.     procID = *(short*)(*theRez+8);
  836.     visible = *(Boolean*)(*theRez+10);
  837.     goAwayFlag = *(Boolean*)(*theRez+12);
  838.     refCon = *(long*)(*theRez+14);
  839.     utl_CopyPString(title, *theRez+18);
  840.     return NewWindow(wStorage, &boundsRect, title, visible, procID, behind,
  841.         goAwayFlag, refCon);
  842. }
  843.  
  844. /*______________________________________________________________________
  845.  
  846.     utl_GetSysVol - Get the volume reference number of the system volume.
  847.     
  848.     Exit:        function result = volume reference number of the volume 
  849.                     containing the currently active system file.
  850. _____________________________________________________________________*/
  851.  
  852.  
  853. short utl_GetSysVol (void)
  854.  
  855. {
  856.     
  857.     short            vRefNum;                /* vol ref num */
  858.  
  859.     GetVRefNum(*(short*)(SysMap), &vRefNum);
  860.     return vRefNum;
  861. }
  862.  
  863. /*______________________________________________________________________
  864.  
  865.     utl_GetSysWD - Get WDRefNum of Blessed Folder.
  866.     
  867.     Exit:        function result = WDRefNum of Blessed Folder.
  868. _____________________________________________________________________*/
  869.  
  870.  
  871. short utl_GetSysWD (void)
  872.  
  873. {
  874.     if (!GotSysEnviron) GetSysEnvirons();
  875.     return TheWorld.sysVRefNum;
  876. }
  877.  
  878. /*______________________________________________________________________
  879.  
  880.     utl_GetWindGD - Get the GDevice containing a window.
  881.     
  882.     Entry:    theWindow = pointer to window.
  883.     
  884.     Exit:        gd = handle to GDevice, or nil if no color QD.
  885.                 screenRect = bounding rectangle of GDevice, or 
  886.                     qd.screenBits.bounds if no color QD.
  887.                 windRect = content rectangle of window, in global
  888.                     coords.
  889.                 hasMB = true if this screen contains the menu bar.
  890.                 
  891.     The routine determines the GDevice (screen) containing the maximum
  892.     intersection with a window.  See TN 79.
  893. _____________________________________________________________________*/
  894.  
  895.  
  896. void utl_GetWindGD (WindowPtr theWindow, GDHandle *gd, 
  897.     Rect *screenRect, Rect *windRect, Boolean *hasMB)
  898.     
  899. {
  900.     GrafPtr            savePort;            /* saved grafport */
  901.     Rect                sectRect;            /* intersection rect */
  902.     GDHandle            curDevice;            /* current GDevice */
  903.     GDHandle            dominantDevice;    /* dominant GDevice */
  904.     long                sectArea;            /* intersection area */
  905.     long                maxArea;                /* max intersection area */
  906.     
  907.     *windRect = theWindow->portRect;
  908.     GetPort(&savePort);
  909.     SetPort(theWindow);
  910.     LocalToGlobal((Point*)&windRect->top);
  911.     LocalToGlobal((Point*)&windRect->bottom);
  912.     if (utl_HaveColor()) {
  913.         windRect->top -= titleBarHeight;
  914.         curDevice = GetDeviceList();
  915.         maxArea = 0;
  916.         dominantDevice = nil;
  917.         while (curDevice) {
  918.             if (TestDeviceAttribute(curDevice, screenDevice) &&
  919.                 TestDeviceAttribute(curDevice, screenActive)) {
  920.                 SectRect(windRect, &(**curDevice).gdRect, §Rect);
  921.                 sectArea = (long)(sectRect.right - sectRect.left) * 
  922.                     (long)(sectRect.bottom - sectRect.top);
  923.                 if (sectArea > maxArea) {
  924.                     maxArea = sectArea;
  925.                     dominantDevice = curDevice;
  926.                 };
  927.             };
  928.             curDevice = GetNextDevice(curDevice);
  929.         };
  930.         windRect->top += titleBarHeight;
  931.         if (dominantDevice) {
  932.             *gd = dominantDevice;
  933.             *screenRect = (**dominantDevice).gdRect;
  934.             *hasMB = dominantDevice == GetMainDevice();
  935.         } else {
  936.             *gd = nil;
  937.             *screenRect = qd.screenBits.bounds;
  938.             *hasMB = true;
  939.         };    
  940.     } else {
  941.         *gd = nil;
  942.         *screenRect = qd.screenBits.bounds;
  943.         *hasMB = true;
  944.     };
  945.     SetPort(savePort);
  946. }
  947.  
  948. /*______________________________________________________________________
  949.  
  950.     utl_GetVolFilCnt - Get the number of files on a volume.
  951.     
  952.     Entry:    volRefNum = volume reference number of volume.
  953.     
  954.     Exit:        function result = number of files on volume.
  955.     
  956.     For serdver volumes this function always returns 0.
  957. _____________________________________________________________________*/
  958.  
  959.  
  960. long utl_GetVolFilCnt (short volRefNum)
  961.  
  962. {
  963.     HParamBlockRec        pBlock;        /* param block for PHBGetVInfo */
  964.     
  965.     pBlock.volumeParam.ioNamePtr = nil;
  966.     pBlock.volumeParam.ioVRefNum = volRefNum;
  967.     pBlock.volumeParam.ioVolIndex = 0;
  968.     (void) PBHGetVInfo(&pBlock, false);
  969.     return pBlock.volumeParam.ioVFilCnt;
  970. }
  971.  
  972. /*______________________________________________________________________
  973.  
  974.     utl_HaveColor - Determine if system has color QuickDraw.
  975.  
  976.     Exit:        function result = true if we have color QD.
  977. _____________________________________________________________________*/
  978.  
  979.  
  980. Boolean utl_HaveColor (void)
  981.  
  982. {
  983.     if (!GotSysEnviron) GetSysEnvirons();
  984.     return TheWorld.hasColorQD;
  985. }
  986.  
  987. /*______________________________________________________________________
  988.  
  989.     utl_HaveSound - Determine if system has the Sound Manager.
  990.  
  991.     Exit:        function result = true if we have sound.
  992. _____________________________________________________________________*/
  993.  
  994.  
  995. Boolean utl_HaveSound (void)
  996.  
  997. {
  998.     if (!GotSysEnviron) GetSysEnvirons();
  999.     return (TheWorld.systemVersion >= 0x0602);
  1000. }
  1001.  
  1002. /*______________________________________________________________________
  1003.  
  1004.     utl_InitSpinCursor - Initialize animated cursor.
  1005.     
  1006.     Entry:    cursArray = array of handles to cursors.
  1007.                 numCurs = number of cursors to rotate.
  1008.                 tickInterval = interval between cursor rotations.
  1009. _____________________________________________________________________*/
  1010.  
  1011.  
  1012. void utl_InitSpinCursor (CursHandle *cursArray, short numCurs, 
  1013.     short tickInterval)
  1014.  
  1015. {
  1016.     CursHandle        h;
  1017.  
  1018.     CursArray = cursArray;
  1019.     CurCurs = 0;
  1020.     NumCurs = numCurs;
  1021.     TickInterval = tickInterval;
  1022.     LastTick = TickCount();
  1023.     h = *cursArray;
  1024.     SetCursor(*h);
  1025. }
  1026.  
  1027. /*______________________________________________________________________
  1028.  
  1029.     utl_InvalGrow - Invalidate Grow Icon.
  1030.     
  1031.     Entry:            theWindow = pointer to window.
  1032.     
  1033.     This routine should be called before and after calling SizeWindow
  1034.     for windows with grow icons.
  1035. _____________________________________________________________________*/
  1036.  
  1037.  
  1038. void utl_InvalGrow (WindowPtr theWindow)
  1039.  
  1040. {
  1041.     Rect            r;            /* rect to be invalidated */
  1042.     
  1043.     r = theWindow->portRect;
  1044.     r.top = r.bottom - 15;
  1045.     r.left = r.right - 15;
  1046.     InvalRect(&r);
  1047. };
  1048.  
  1049. /*______________________________________________________________________
  1050.  
  1051.     utl_IsDAWindow - Check to see if a window is a DA.
  1052.     
  1053.     Entry:    theWindow = pointer to dialog window.
  1054.                 
  1055.     
  1056.     Exit:        function result = true if DA window.
  1057. _____________________________________________________________________*/
  1058.  
  1059.  
  1060. Boolean utl_IsDAWindow (WindowPtr theWindow)
  1061.  
  1062. {
  1063.     return ((WindowPeek)theWindow)->windowKind < 0;
  1064. }
  1065.  
  1066. /*______________________________________________________________________
  1067.  
  1068.     utl_IsLaser - Check Printer for LaserWriter.
  1069.     
  1070.     Entry:    hPrint = handle to print record.
  1071.     
  1072.     Exit:        function result = true if LaserWriter.
  1073.     
  1074. _____________________________________________________________________*/
  1075.  
  1076.  
  1077. Boolean utl_IsLaser (THPrint hPrint)
  1078.  
  1079. {
  1080.     unsigned char    wDev;                /* printer device */
  1081.  
  1082.     wDev = (**hPrint).prStl.wDev >> 8;
  1083.     return wDev==3 || wDev==4;
  1084. }
  1085.  
  1086. /*______________________________________________________________________
  1087.  
  1088.     utl_LockControls - Lock Window Controls
  1089.     
  1090.     Entry:    theWindow = pointer to window.
  1091.     
  1092.     This routine moves all the control records in a window high and
  1093.     locks them.  It should be called immediately after creating a new
  1094.     window, and before drawing it.  It works around errors in the Control
  1095.     Manager which showed up when I was testing with TMON's heap scramble and
  1096.     purge option.
  1097. _____________________________________________________________________*/
  1098.  
  1099.  
  1100. void utl_LockControls (WindowPtr theWindow)
  1101.  
  1102. {
  1103.     ControlHandle        theControl;        /* handle to control */
  1104.  
  1105.     theControl = ((WindowPeek)theWindow)->controlList;
  1106.     while (theControl) {
  1107.         MoveHHi((Handle)theControl);
  1108.         HLock((Handle)theControl);
  1109.         theControl = (**theControl).nextControl;
  1110.     };
  1111. }
  1112.  
  1113. /*______________________________________________________________________
  1114.  
  1115.     utl_PlotSmallIcon - Draw a small icon.
  1116.     
  1117.     Entry:    theRect = rectangle in which to draw the small icon.
  1118.                 theHandle = handle to small icon.
  1119.                     
  1120.     For best results, the rectangle should be exactly 16 pixels square.
  1121. _____________________________________________________________________*/
  1122.  
  1123.  
  1124. void utl_PlotSmallIcon (Rect *theRect, Handle theHandle)
  1125.  
  1126. {
  1127.     BitMap        srcBits;            /* Source bitmap for CopyBits */
  1128.     
  1129.     MoveHHi(theHandle);
  1130.     HLock(theHandle);
  1131.     srcBits.baseAddr = *theHandle;
  1132.     srcBits.rowBytes = 2;
  1133.     SetRect(&srcBits.bounds, 0, 0, 16, 16);
  1134.     CopyBits(&srcBits, &qd.thePort->portBits, &srcBits.bounds, theRect,
  1135.         srcCopy, nil);
  1136.     HUnlock(theHandle);
  1137. }
  1138.  
  1139. /*______________________________________________________________________
  1140.  
  1141.     utl_PlugParams - Plug parameters into message.
  1142.     
  1143.     Entry:    line1 = input line.
  1144.                 p0, p1, p2, p3 = parameters.
  1145.                     
  1146.     Exit:        line2 = output line.
  1147.                     
  1148.     This routine works just like the toolbox routine ParamText.
  1149.     The input line may contain place-holders ^0, ^1, ^2, and ^3, 
  1150.     which are replaced by the parameters p0-p3.  The input line
  1151.     must not contain any other ^ characters.  The input and output lines
  1152.     may not be the same string.  Pass nil for parameters which don't
  1153.     occur in line1.  If the output line exceeds 255 characters it's
  1154.     truncated.
  1155. _____________________________________________________________________*/
  1156.  
  1157.  
  1158. void utl_PlugParams (Str255 line1, Str255 line2, Str255 p0, 
  1159.     Str255 p1, Str255 p2, Str255 p3)
  1160.  
  1161. {
  1162.     char                *in;            /* pointer to cur pos in input line */
  1163.     char                *out;            /* pointer to cur pos in output line */
  1164.     char                *inEnd;        /* pointer to end of input line */
  1165.     char                *outEnd;        /* pointer to end of output line */
  1166.     char                *param;        /* pointer to param to be plugged */
  1167.     short                len;            /* length of param */
  1168.     
  1169.     in = line1+1;
  1170.     out = line2+1;
  1171.     inEnd = line1 + 1 + *line1;
  1172.     outEnd = line2 + 256;
  1173.     while (in < inEnd ) {
  1174.         if (*in == '^') {
  1175.             in++;
  1176.             if (in >= inEnd) break;
  1177.             switch (*in++) {
  1178.                 case '0':
  1179.                     param = p0;
  1180.                     break;
  1181.                 case '1':
  1182.                     param = p1;
  1183.                     break;
  1184.                 case '2':
  1185.                     param = p2;
  1186.                     break;
  1187.                 case '3':
  1188.                     param = p3;
  1189.                     break;
  1190.                 default:
  1191.                     continue;
  1192.             };
  1193.             if (!param) continue;
  1194.             len = *param;
  1195.             if (out + len > outEnd) len = outEnd - out;
  1196.             memcpy(out, param+1, len);
  1197.             out += len;
  1198.         } else {
  1199.             if (out >= outEnd) break;
  1200.             *out++ = *in++;
  1201.         };
  1202.     };
  1203.     *line2 = out - (line2+1);
  1204. };
  1205.  
  1206. /*______________________________________________________________________
  1207.  
  1208.     utl_RestoreWindowPos - Restore Window Position.
  1209.     
  1210.     Entry:    theWindow = pointer to window.
  1211.                 userState = saved user state rectangle for the window.
  1212.                 zoomed = true if window in zoomed state when saved.
  1213.                 offset = pixel offset used in DragRect calls.
  1214.                 computeStdState = pointer to function to compute standard state.
  1215.                 computeDefState = pointer to function to compute default state.
  1216.     
  1217.     Exit:        window position, size, and zoom state restored.
  1218.                 userState = new user state.
  1219.                 
  1220.     See HIN 6: "When reopening a movable window, check its saved 
  1221.     position.  If the window is in a position to which the user could
  1222.     have dragged it, then leave it there.  If the window can be zoomed
  1223.     and was in the zoomed state when it was last closed, put it in the
  1224.     zoomed state again.  (Note that the current and previous zoomed states
  1225.     are not necessarily the same, since the window may be reopened on a
  1226.     different monitor.)  If the window is not in a position to which the
  1227.     user could have dragged it, then it must be relocated, so use the
  1228.     default location.  However, do not automatically use the default size
  1229.     when using the default location; if the entire window would be visible
  1230.     using the default location and stored size, then use the stored size."
  1231.     
  1232.     The "offset" parameter is usually 4.  When initializing the boundary rectangle
  1233.     for DragWindow calls, normally the boundary rectangle of the desktop gray
  1234.     region is inset by 4 pixels.  If some value other than 4 is used, it should
  1235.     be passed to RestoreWindowPos as the "offset" parameter.
  1236.     
  1237.     The computeStdState function is passed a pointer to the window.  Given
  1238.     the userState in the window zoom info, it must compute the standard
  1239.     (zoom) state in the window zoom info.  This is an application-dependent
  1240.     function.
  1241.     
  1242.     The computeDefState function must determine the default position and
  1243.     size of a new window, and return the result as a rectangle.  This may
  1244.     involve invocation of a staggering algorithm or some other algorithm.
  1245.     This is an application-dependent function.
  1246. _____________________________________________________________________*/
  1247.  
  1248.  
  1249. void utl_RestoreWindowPos (WindowPtr theWindow, Rect *userState, 
  1250.     Boolean zoomed, short offset,
  1251.     utl_ComputeStdStatePtr computeStdState,
  1252.     utl_ComputeDefStatePtr computeDefState)
  1253.  
  1254. {
  1255.     WindowPeek        w;                    /* window pointer */
  1256.     short                userHeight;        /* height of userState */
  1257.     short                userWidth;        /* width of userState */
  1258.     Rect                r;                    /* scratch rectangle */
  1259.     RgnHandle        rgn;                /* scratch region */
  1260.     Rect                stdState;        /* standard state */
  1261.     short                windHeight;        /* window height */
  1262.     short                windWidth;        /* window width */
  1263.  
  1264.     w = (WindowPeek)theWindow;
  1265.     if (!utl_CouldDrag(userState, offset)) {
  1266.         userHeight = userState->bottom - userState->top;
  1267.         userWidth = userState->right - userState->left;
  1268.         (*computeDefState)(theWindow, userState);
  1269.         if (!zoomed) {
  1270.             r = *userState;
  1271.             r.bottom = r.top + userHeight;
  1272.             r.right = r.left + userWidth;
  1273.             r.top -= titleBarHeight;
  1274.             InsetRect(&r, -1, -1);
  1275.             rgn = NewRgn();
  1276.             RectRgn(rgn, &r);
  1277.             DiffRgn(rgn, *(RgnHandle*)GrayRgn, rgn);
  1278.             if (EmptyRgn(rgn)) {
  1279.                 userState->bottom = userState->top + userHeight;
  1280.                 userState->right = userState->left + userWidth;
  1281.             };
  1282.             DisposeRgn(rgn);
  1283.         };
  1284.     };
  1285.     MoveWindow(theWindow, userState->left, userState->top, false);
  1286.     windHeight = userState->bottom - userState->top;
  1287.     windWidth = userState->right - userState->left;
  1288.     SizeWindow(theWindow, windWidth, windHeight, true);
  1289.     if (w->dataHandle && w->spareFlag) {
  1290.         (**((WStateData**)w->dataHandle)).userState = *userState;
  1291.         (*computeStdState)(theWindow);
  1292.         if (zoomed) {
  1293.             stdState = (**((WStateData**)w->dataHandle)).stdState;
  1294.             MoveWindow(theWindow, stdState.left, stdState.top, false);
  1295.             windHeight = stdState.bottom - stdState.top;
  1296.             windWidth = stdState.right - stdState.left;
  1297.             SizeWindow(theWindow, windWidth, windHeight, true);
  1298.         };
  1299.     };
  1300. }
  1301.  
  1302. /*______________________________________________________________________
  1303.  
  1304.     utl_Rom64 - Check to see if we have the old 64K ROM.
  1305.     
  1306.     Exit:        function result = true if 64K ROM.
  1307. _____________________________________________________________________*/
  1308.  
  1309.  
  1310. Boolean utl_Rom64 (void)
  1311.  
  1312. {
  1313.     return *(short*)ROM85 < 0;
  1314. }
  1315.  
  1316. /*______________________________________________________________________
  1317.  
  1318.     utl_RFSanity - Check a Resource File's Sanity.
  1319.     
  1320.     Entry:    *fName = file name.
  1321.     
  1322.     Exit:        *sane = true if resource fork is sane.
  1323.                 function result = error code.
  1324.                 
  1325.     This routine checks the sanity of a resource file.  The Resource Manager
  1326.     does not do error checking, and can bomb or hang if you use it to open
  1327.     a damaged resource file.  This routine can be called first to precheck
  1328.     the file.
  1329.     
  1330.     The routine checks the entire resource map.
  1331.     
  1332.     It is the caller's responsibility to set the proper default volume and
  1333.     directory.
  1334. _____________________________________________________________________*/
  1335.  
  1336.  
  1337. OSErr utl_RFSanity (Str255 fName, Boolean *sane)
  1338.  
  1339. {
  1340.     ParamBlockRec        fBlock;            /* file param block */
  1341.     short                    refNum;            /* file refnum */
  1342.     long                    count;            /* number of bytes to read */
  1343.     long                    logEOF;            /* logical EOF */
  1344.     Ptr                    map;                /* pointer to resource map */
  1345.     unsigned long        dataLWA;            /* offset in file of data end */
  1346.     unsigned long        mapLWA;            /* offset in file of map end */
  1347.     unsigned short        typeFWA;            /* offset from map begin to type list */
  1348.     unsigned short        nameFWA;            /* offset from map begin to name list */
  1349.     unsigned char        *pType;            /* pointer into type list */
  1350.     unsigned char        *pName;            /* pointer to start of name list */
  1351.     unsigned char        *pMapEnd;        /* pointer to end of map */
  1352.     short                    nType;            /* number of resource types in map */
  1353.     unsigned char        *pTypeEnd;        /* pointer to end of type list */
  1354.     short                    nRes;                /* number of resources of given type */
  1355.     unsigned short        refFWA;            /* offset from type list to ref list */
  1356.     unsigned char        *pRef;            /* pointer into reference list */
  1357.     unsigned char        *pRefEnd;        /* pointer to end of reference list */
  1358.     unsigned short        resNameFWA;        /* offset from name list to resource name */
  1359.     unsigned char        *pResName;        /* pointer to resource name */
  1360.     unsigned long        resDataFWA;        /* offset from data begin to resource data */
  1361.     Boolean                mapOK;            /* true if map is sane */
  1362.     OSErr                    rCode;            /* error code */
  1363.     
  1364.     struct {
  1365.         unsigned long        dataFWA;        /* offset in file of data */
  1366.         unsigned long        mapFWA;        /* offset in file of map */
  1367.         unsigned long        dataLen;        /* data area length */
  1368.         unsigned long        mapLen;        /* map area length */
  1369.     } header;
  1370.     
  1371.     /* Open the resource file. */
  1372.     
  1373.     fBlock.ioParam.ioNamePtr = fName;
  1374.     fBlock.ioParam.ioVRefNum = 0;
  1375.     fBlock.ioParam.ioVersNum = 0;
  1376.     fBlock.ioParam.ioPermssn = fsRdPerm;
  1377.     fBlock.ioParam.ioMisc = nil;
  1378.     if (rCode = PBOpenRF(&fBlock, false)) {
  1379.         if (rCode == fnfErr) {
  1380.             *sane = true;
  1381.             return noErr;
  1382.         } else {
  1383.             return rCode;
  1384.         };
  1385.     };
  1386.     
  1387.     /* Get the logical eof of the file. */
  1388.     
  1389.     refNum = fBlock.ioParam.ioRefNum;
  1390.     if (rCode = GetEOF(refNum, &logEOF)) return rCode;
  1391.     if (!logEOF) {
  1392.         *sane = true;
  1393.         if (rCode = FSClose(refNum)) return rCode;
  1394.         return noErr;
  1395.     };
  1396.     
  1397.     /* Read and validate the resource header. */
  1398.     
  1399.     count = 16;
  1400.     if (rCode = FSRead(refNum, &count, (Ptr)&header)) {
  1401.         FSClose(refNum);
  1402.         return rCode;
  1403.     };
  1404.     dataLWA = header.dataFWA + header.dataLen;
  1405.     mapLWA = header.mapFWA + header.mapLen;
  1406.     mapOK = count == 16 && header.mapLen > 28 &&
  1407.         header.dataFWA < 0x01000000 && header.mapFWA < 0x01000000 &&
  1408.         dataLWA <= logEOF && mapLWA <= logEOF &&
  1409.         (dataLWA <= header.mapFWA || mapLWA <= header.dataFWA);
  1410.         
  1411.     /* Read the resource map. */
  1412.     
  1413.     map = nil;
  1414.     if (mapOK) {
  1415.         map = NewPtr(header.mapLen);
  1416.         if (!(rCode = SetFPos(refNum, fsFromStart, header.mapFWA))) {
  1417.             count = header.mapLen;
  1418.             rCode = FSRead(refNum, &count, map);
  1419.         };
  1420.     };
  1421.     
  1422.     /* Verify the type list and name list offsets. */
  1423.     
  1424.     if (!rCode) {
  1425.         typeFWA = *(unsigned short*)(map+24);
  1426.         nameFWA = *(unsigned short*)(map+26);
  1427.         mapOK = typeFWA == 28 && nameFWA >= typeFWA && nameFWA <= header.mapLen &&
  1428.             !(typeFWA & 1) && !(nameFWA & 1);
  1429.     };
  1430.     
  1431.     /* Verify the type list, reference lists, and name list. */
  1432.     
  1433.     if (mapOK) {
  1434.         pType = map + typeFWA;
  1435.         pName = map + nameFWA;
  1436.         pMapEnd = map + header.mapLen;
  1437.         nType = *(short*)pType + 1;
  1438.         pType += 2;
  1439.         pTypeEnd = pType + (nType<<3);
  1440.         if (mapOK = pTypeEnd <= pMapEnd) {
  1441.             while (pType < pTypeEnd) {
  1442.                 nRes = *(short*)(pType+4) + 1;
  1443.                 refFWA = *(unsigned short*)(pType+6);
  1444.                 pRef = map + typeFWA + refFWA;
  1445.                 pRefEnd = pRef + 12*nRes;
  1446.                 if (!(mapOK = pRef >= pTypeEnd && pRef < pName && 
  1447.                     !(refFWA & 1))) break;
  1448.                 while (pRef < pRefEnd) {
  1449.                     resNameFWA = *(unsigned short*)(pRef+2);
  1450.                     if (resNameFWA != 0xFFFF) {
  1451.                         pResName = pName + resNameFWA;
  1452.                         if (!(mapOK = pResName + *pResName < pMapEnd)) break;
  1453.                     };
  1454.                     resDataFWA = *(unsigned long*)(pRef+4) & 0x00FFFFFF;
  1455.                     if (!(mapOK = header.dataFWA + resDataFWA < dataLWA)) break;
  1456.                     pRef += 12;
  1457.                 };
  1458.                 if (!mapOK) break;
  1459.                 pType += 8;
  1460.             };
  1461.         };
  1462.     };
  1463.     
  1464.     /* Dispose of the resource map, close the file and return. */
  1465.     
  1466.     if (map) DisposPtr(map);
  1467.     if (!rCode) {
  1468.         rCode = FSClose(refNum);
  1469.     } else {
  1470.         (void) FSClose(refNum);
  1471.     };
  1472.     *sane = mapOK;
  1473.     return rCode;
  1474. }
  1475.  
  1476. /*______________________________________________________________________
  1477.  
  1478.     utl_SaveWindowPos - Save Window Position.
  1479.     
  1480.     Entry:    theWindow = window pointer.
  1481.     
  1482.     Exit:        userState = user state rectangle of the window.
  1483.                 zoomed = true if window zoomed.
  1484.                 
  1485.     See HIN 6: "Before closing a movable window, check to see if its
  1486.     location or size have changed.  If so, save the new location and
  1487.     size.  If the window can be zoomed, save the user state and also 
  1488.     save whether or not the window is in the zoomed (standard) sate."
  1489.     
  1490.     We assume in this routine that the caller has already kept track
  1491.     of the fact that this window's location or size has changed, and that
  1492.     we do indeed need to save the location and size.
  1493.     
  1494.     This routine only works if the window's origin has not been offset.
  1495. _____________________________________________________________________*/
  1496.  
  1497.  
  1498. void utl_SaveWindowPos (WindowPtr theWindow, Rect *userState, Boolean *zoomed)
  1499.  
  1500. {
  1501.     GrafPtr        savedPort;        /* saved grafport */
  1502.     WindowPeek    w;                    /* window pointer */
  1503.     Rect            curState;        /* current window rect, global coords */
  1504.     Rect            stdState;        /* standard (zoom) rect, global coords */
  1505.     Point            p;                    /* scratch point */
  1506.     Rect            r;                    /* scratch rect */
  1507.     
  1508.     GetPort(&savedPort);
  1509.     SetPort(theWindow);
  1510.     SetPt(&p, 0, 0);
  1511.     LocalToGlobal(&p);
  1512.     curState = theWindow->portRect;
  1513.     OffsetRect(&curState, p.h, p.v);
  1514.     w = (WindowPeek)theWindow;
  1515.     if (w->dataHandle && w->spareFlag) {
  1516.         /* This window supports zooming */
  1517.         /* Determine if window is zoomed.  The criteria is that both the
  1518.             top left and bottom right corners of the current window rectangle
  1519.             and the standard (zoom) rectangle must be within 7 pixels of each
  1520.             other.  This is the same algorithm as the one used by the standard
  1521.             system window definition function. */
  1522.         *zoomed = false;
  1523.         stdState = (**((WStateData**)w->dataHandle)).stdState;
  1524.         SetPt(&p, curState.left, curState.top);
  1525.         SetRect(&r, stdState.left, stdState.top, stdState.left, stdState.top);
  1526.         InsetRect(&r, -7, -7);
  1527.         if (PtInRect(p, &r)) {
  1528.             SetPt(&p, curState.right, curState.bottom);
  1529.             SetRect(&r, stdState.right, stdState.bottom, stdState.right,
  1530.                 stdState.bottom);
  1531.             InsetRect(&r, -7, -7);
  1532.             *zoomed = PtInRect(p, &r);
  1533.         };
  1534.         if (*zoomed) {
  1535.             *userState = (**((WStateData**)w->dataHandle)).userState;
  1536.         } else {
  1537.             *userState = curState;
  1538.         };
  1539.     } else {
  1540.         /* This window does not support zooming. */
  1541.         *zoomed = false;
  1542.         *userState = curState;
  1543.     };
  1544.     SetPort(savedPort);
  1545. }
  1546.  
  1547. /*______________________________________________________________________
  1548.  
  1549.     utl_ScaleFontSize - Scale Font Size
  1550.     
  1551.     Entry:    fontNum = font number.
  1552.                 fontSize = nominal font size.
  1553.                 percent = percent change in size.
  1554.                 laser = true if laserwriter.
  1555.     
  1556.     Exit:        function result = scaled font size.
  1557.                 
  1558.     The nominal font size is multiplied by the percentage,
  1559.     then truncated.  For non-laserwriters it is then rounded down
  1560.     to the nearest font size which is available in that true size,
  1561.     without font manager scaling, or which can be generated by doubling
  1562.     an existing font size.
  1563. _____________________________________________________________________*/
  1564.  
  1565.  
  1566. short utl_ScaleFontSize (short fontNum, short fontSize, short percent,
  1567.     Boolean laser)
  1568.     
  1569. {
  1570.     short            nSize;                /* new size */
  1571.     short            x;                        /* new test size */
  1572.     
  1573.     nSize = fontSize * percent / 100;
  1574.     if (!laser) {
  1575.         x = nSize;
  1576.         while (x > 0) {
  1577.             if (RealFont(fontNum, x)) break;
  1578.             if (!(x&1) && RealFont(fontNum, x>>1)) break;
  1579.             x--;
  1580.         };
  1581.         if (x) nSize = x;
  1582.     };
  1583.     return nSize;
  1584. }
  1585.  
  1586. /*______________________________________________________________________
  1587.  
  1588.     utl_SpinCursor - Animate cursor.
  1589.     
  1590.     After calling InitSpinCursor to initialize an animated cursor, call
  1591.     SpinCursor periodically to make the cursor animate.
  1592. _____________________________________________________________________*/
  1593.  
  1594.  
  1595. void utl_SpinCursor (void)
  1596.  
  1597. {
  1598.     CursHandle        h;                /* handle to cursor */
  1599.     long                ticksNow;    /* current tick count */
  1600.     
  1601.     ticksNow = TickCount();
  1602.     if (ticksNow < LastTick + TickInterval) return;
  1603.     LastTick = ticksNow;
  1604.     CurCurs++;
  1605.     if (CurCurs >= NumCurs) CurCurs = 0;
  1606.     h = CursArray[CurCurs];
  1607.     SetCursor(*h);
  1608. }
  1609.  
  1610. /*______________________________________________________________________
  1611.  
  1612.     utl_StaggerWindow - Stagger a New Window
  1613.     
  1614.     Entry:    windRect = window portrect.
  1615.                 initialOffset = intitial pixel offset of window from corner.
  1616.                 offset = offset for subsequent staggered windows.
  1617.     
  1618.     Exit:        pos = window position.
  1619.     
  1620.     According to HIN 6, a new window should be positioned as follows:
  1621.     "The first document window should be positioned in the upper-left corner
  1622.     of the gray area of the main screen (the screen with the menu bar).
  1623.     Each additional independent window should be staggered from the upper-left
  1624.     corner of the screen that contains the largest portion of the
  1625.     frontmost window."  Also, "When a window is used or closed, its original
  1626.     position becomes available again.  The next window opened should use this
  1627.     position.  Similarly, if a window is moved onto a previously available
  1628.     position, that position becomes unavailable again."
  1629.     
  1630.     This routine implements these rules.  A position is considered to be
  1631.     "unavailable" if some other window is within (offset+1)/2 pixels of the
  1632.     position in both the vertical and horizontal directions.
  1633.     
  1634.     If all slots are occupied, the routine attempts to locate the first one
  1635.     which is occupied by only one other window.  If this attempt fails, it
  1636.     tries to locate one which is occupied by only two other windows, etc.
  1637.     Thus, if the screen is filled with staggered windows, subsequent windows
  1638.     will be staggered on top of the existing ones, forming a second "layer."
  1639.     If this layer fills up, a third layer is started, etc.
  1640. _____________________________________________________________________*/
  1641.  
  1642.  
  1643. void utl_StaggerWindow (Rect *windRect, short initialOffset, short offset, 
  1644.     Point *pos)
  1645.  
  1646. {
  1647.     GrafPtr            savedPort;        /* saved grafport */
  1648.     short                offsetDiv2;        /* offset/2 */
  1649.     short                windHeight;        /* window height */
  1650.     short                windWidth;        /* window width */
  1651.     WindowPtr        frontWind;        /* pointer to front window */
  1652.     GDHandle            gd;                /* GDevice */
  1653.     Rect                screenRect;        /* screen rectangle */
  1654.     Rect                junkRect;        /* window rectangle */
  1655.     Boolean            hasMB;            /* true if screen has menu bar */
  1656.     Point                initPos;            /* initial staggered window position */
  1657.     Point                curPos;            /* current staggered window position */
  1658.     WindowPtr        curWind;            /* pointer to current window */
  1659.     Point                windPos;            /* current window position */
  1660.     short                deltaH;            /* horizontal distance */
  1661.     short                deltaV;            /* vertical distance */
  1662.     short                layer;            /* layer number */
  1663.     short                nOccupied;        /* number windows occupying cur pos */
  1664.     Boolean            noPos;            /* true if no position is on screen */
  1665.     
  1666.     GetPort(&savedPort);
  1667.     offsetDiv2 = (offset+1)>>1;
  1668.     windHeight = windRect->bottom - windRect->top;
  1669.     windWidth = windRect->right - windRect->left;
  1670.     frontWind = FrontWindow();
  1671.     if (frontWind) {
  1672.         utl_GetWindGD(frontWind, &gd, &screenRect, &junkRect, &hasMB);
  1673.     } else {
  1674.         screenRect = qd.screenBits.bounds;
  1675.         hasMB = true;
  1676.     };
  1677.     if (hasMB) screenRect.top += utl_GetMBarHeight();
  1678.     SetPt(&initPos, screenRect.left + initialOffset + 1, 
  1679.         screenRect.top + initialOffset + titleBarHeight);
  1680.     layer = 1;
  1681.     while (true) {
  1682.         /* Test each layer number "layer", starting with 1 and incrementing by 1.
  1683.             Break out of the loop when we find a position curPos which
  1684.             is "occupied" by fewer than "layer" other windows. */
  1685.         curPos = initPos;
  1686.         noPos = true;
  1687.         while (true) {
  1688.             /* Test each possible position curPos.  Break out of the loop
  1689.                 when we have exhaused the possible positions, or when we
  1690.                 have located one which has < layer "occupants". */
  1691.             curWind = frontWind;
  1692.             if (curPos.v + windHeight >= screenRect.bottom ||
  1693.                 curPos.h + windWidth >= screenRect.right) {
  1694.                 break;
  1695.             };
  1696.             noPos = false;
  1697.             nOccupied = 0;
  1698.             while (curWind) {
  1699.                 /* Scan the window list and count up how many of them "occupy"
  1700.                     the current location curPos.  Break out of the loop when
  1701.                     we reach the end of the list, or when the count is >=
  1702.                     layer. */
  1703.                 SetPt(&windPos, 0, 0);
  1704.                 SetPort(curWind);
  1705.                 LocalToGlobal(&windPos);
  1706.                 deltaH = curPos.h - windPos.h;
  1707.                 deltaV = curPos.v - windPos.v;
  1708.                 if (deltaH < 0) deltaH = -deltaH;
  1709.                 if (deltaV < 0) deltaV = -deltaV;
  1710.                 if (deltaH <= offsetDiv2 && deltaV <= offsetDiv2) {
  1711.                     nOccupied++;
  1712.                     if (nOccupied >= layer) break;
  1713.                 };
  1714.                 curWind = (WindowPtr)(((WindowPeek)curWind)->nextWindow);
  1715.             };
  1716.             if (!curWind) break;
  1717.             curPos.h += offset;
  1718.             curPos.v += offset;
  1719.         };
  1720.         if (!curWind || noPos) break;
  1721.         layer++;
  1722.     };
  1723.     SetPort(savedPort);
  1724.     *pos = curPos;
  1725. }
  1726.  
  1727. /*______________________________________________________________________
  1728.  
  1729.     CancelFilter - Command Period Dialog Filter Proc.
  1730.     
  1731.     Entry:    theDialog = pointer to dialog record.
  1732.                 theEvent = pointer to event record.
  1733.     
  1734.     Exit:        if command-period or escape typed:
  1735.     
  1736.                 function result = true.
  1737.                 itemHit = item number of cancel button.
  1738.                 
  1739.                 else if user specified a filterProc on the call to
  1740.                 utl_StopAlert:
  1741.                 
  1742.                 user's filterProc called, function result and itemHit
  1743.                 returned by user's filterProc.
  1744.                 
  1745.                 else:
  1746.                 
  1747.                 function result = false.
  1748.                 itemHit = undefined.
  1749. _____________________________________________________________________*/
  1750.  
  1751.  
  1752. static pascal Boolean CancelFilter (DialogPtr theDialog,
  1753.     EventRecord *theEvent, short *itemHit)
  1754.     
  1755. {
  1756.     short                key;            /* ascii code of key pressed */
  1757.     
  1758.     if (theEvent->what != keyDown && theEvent->what != autoKey) {
  1759.         if (Filter) {
  1760.             return (*Filter)(theDialog, theEvent, itemHit);
  1761.         } else {
  1762.             return false;
  1763.         };
  1764.     } else {
  1765.         key = theEvent->message & charCodeMask;
  1766.         if (key == escapeKey) {
  1767.             *itemHit = CancelItem;
  1768.             utl_FlashButton(theDialog, CancelItem);
  1769.             return true;
  1770.         } else if ((theEvent->modifiers & cmdKey) &&
  1771.             (key == '.')) {
  1772.             *itemHit = CancelItem;
  1773.             utl_FlashButton(theDialog, CancelItem);
  1774.             return true;
  1775.         } else if (Filter) {
  1776.             return (*Filter)(theDialog, theEvent, itemHit);
  1777.         } else if (key == returnKey || key==enterKey) {
  1778.             *itemHit = 1;
  1779.             utl_FlashButton(theDialog, 1);
  1780.             return true;
  1781.         } else {
  1782.             return false;
  1783.         };
  1784.     };
  1785. }
  1786.  
  1787. /*______________________________________________________________________
  1788.  
  1789.     utl_StopAlert - Present Stop Alert
  1790.     
  1791.     Entry:    alertID = resource id of alert.
  1792.                 filterProc = pointer to filter proc.
  1793.                 cancelItem = item number of cancel button, or 0 if
  1794.                     none.
  1795.     
  1796.     Exit:        function result = item number
  1797.                 
  1798.     This routine is identical to the Dialog Manager routine StopAlert,
  1799.     except that it centers the alert on the main window, and if requested,
  1800.     command/period or the Escape key is treated the same as a click on the 
  1801.     Cancel button.
  1802. _____________________________________________________________________*/
  1803.  
  1804.  
  1805. short utl_StopAlert (short alertID, ModalFilterProcPtr filterProc,
  1806.     short cancelItem)
  1807.  
  1808. {
  1809.     Handle            h;                /* handle to alert resource */
  1810.     short                result;        /* function result */
  1811.     
  1812.     h = GetResource('ALRT', alertID);
  1813.     HLock(h);
  1814.     utl_CenterDlogRect(*(Rect**)h, false);
  1815.     if (cancelItem) {
  1816.         Filter = filterProc;
  1817.         CancelItem = cancelItem;
  1818.         result = StopAlert(alertID, CancelFilter);
  1819.     } else {
  1820.         result = StopAlert(alertID, filterProc);
  1821.     };
  1822.     HUnlock(h);
  1823.     return result;
  1824. }
  1825.  
  1826. /*______________________________________________________________________
  1827.  
  1828.     utl_SysHasNotMgr - Check to See if System has Notification Manager.
  1829.     
  1830.     Exit:            function result = true if system has Notification
  1831.                         Manager..
  1832.     
  1833.     The system version must be >= 6.0.
  1834. _____________________________________________________________________*/
  1835.  
  1836.  
  1837. Boolean utl_SysHasNotMgr (void)
  1838.  
  1839. {
  1840.     if (!GotSysEnviron) GetSysEnvirons();
  1841.     return TheWorld.systemVersion >= 0x0600;
  1842. }
  1843.  
  1844. /*______________________________________________________________________
  1845.  
  1846.     utl_SysHasPopUp - Check to See if System has Popup Menus.
  1847.     
  1848.     Exit:            function result = true if system has popup menus.
  1849.     
  1850.     The system version must be >= 4.1, the machine must have the 128 ROM or 
  1851.     later, and the PopUpMenuSelect trap must exist.
  1852. _____________________________________________________________________*/
  1853.  
  1854.  
  1855. Boolean utl_SysHasPopUp (void)
  1856.  
  1857. {
  1858.     if (!GotSysEnviron) GetSysEnvirons();
  1859.     if ((TheWorld.systemVersion < 0x0410) || (TheWorld.machineType == envMac)) {
  1860.         return false;
  1861.     } else {
  1862.         return NGetTrapAddress(_PopUpMenuSelect & 0x3ff, ToolTrap) !=
  1863.             NGetTrapAddress(_Unimplemented & 0x3ff, ToolTrap);
  1864.     };
  1865. }
  1866.  
  1867. /*______________________________________________________________________
  1868.  
  1869.     utl_VolIsMFS - Test for MFS volume.
  1870.     
  1871.     Entry:    vRefNum = volume reference number.
  1872.     
  1873.     Exit:        function result = true if volume is MFS.
  1874. _____________________________________________________________________*/
  1875.  
  1876.  
  1877. Boolean utl_VolIsMFS (short vRefNum)
  1878.  
  1879. {
  1880.     HParamBlockRec        vBlock;            /* vol info param block */
  1881.     
  1882.     vBlock.volumeParam.ioNamePtr = nil;
  1883.     vBlock.volumeParam.ioVolIndex = 0;
  1884.     vBlock.volumeParam.ioVRefNum = vRefNum;
  1885.     vBlock.volumeParam.ioVSigWord = 0xd2d7;    /* in case we don't have HFS */
  1886.     (void) PBHGetVInfo(&vBlock, false);
  1887.     return vBlock.volumeParam.ioVSigWord == 0xd2d7;
  1888. }
  1889.  
  1890. /*______________________________________________________________________
  1891.  
  1892.     utl_WaitNextEvent - Get Next Event.
  1893.     
  1894.     Entry:    eventMask = event mask.
  1895.                 sleep = sleep interval.
  1896.                 mouseRgn = mouse region.
  1897.     
  1898.     Exit:        theEvent = the next event.
  1899.                 function result = true if event to be processed.
  1900.                 
  1901.     This routine calls WaitNextEvent if the trap exists, otherwise it
  1902.     calls GetNextEvent.  
  1903.     
  1904.     If GetNextEvent is called, the sleep and mouseRgn parameters are 
  1905.     ignored.  SystemTask is also called.
  1906.                 
  1907.     This routine also saves and restores the current grafport.  This is
  1908.     necessary to protect against some GetNextEvent trap patches which change
  1909.     the grafport without restoring it (e.g., the Flex screen saver).
  1910. _____________________________________________________________________*/
  1911.  
  1912.  
  1913. Boolean utl_WaitNextEvent (short eventMask, EventRecord *theEvent,
  1914.     long sleep, RgnHandle mouseRgn)
  1915.  
  1916. {
  1917.     GrafPtr            curPort;        /* pointer to current grafport */
  1918.     Boolean            result;        /* function result */
  1919.     static short    wne = 2;        /* 0 if WaitNextEvent does not exist
  1920.                                             1 if WaitNextEvent exists
  1921.                                             2 if we don't yet know (first call) */
  1922.     
  1923.     /* Find out whether the WaitNextEvent trap is implemented if this is 
  1924.         the first call. */
  1925.     
  1926.     if (wne == 2) {
  1927.         if (!GotSysEnviron) GetSysEnvirons();
  1928.         if (TheWorld.machineType < 0) {
  1929.             wne = false;
  1930.         } else {
  1931.             wne = (NGetTrapAddress(_WaitNextEvent & 0x3ff, ToolTrap) == 
  1932.                 NGetTrapAddress(_Unimplemented & 0x3ff, ToolTrap)) ? 0 : 1;
  1933.         };
  1934.     };
  1935.     
  1936.     /* Save the port, call the trap, and restore the port. */
  1937.     
  1938.     GetPort(&curPort);
  1939.     if (wne) {
  1940.         result = WaitNextEvent(eventMask, theEvent, sleep, mouseRgn);
  1941.     } else {
  1942.         SystemTask();
  1943.         result = GetNextEvent(eventMask, theEvent);
  1944.     };
  1945.     SetPort(curPort);
  1946.     return result;
  1947. }
  1948.