home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / AOCE / Development Tools / Sample Code / Interprogram Messaging Manager / IPM MessageBoard / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-23  |  5.6 KB  |  321 lines  |  [TEXT/MPS ]

  1. /*-------------------------------------------------------------------------------------
  2.  *
  3.  * IPM MessageBoard AOCE Sample
  4.  *
  5.  * ©1992-1993 Apple Computer
  6.  *
  7.  -------------------------------------------------------------------------------------*/
  8. /*
  9.  * utils.c -- utility routines
  10.  *
  11.  * change history:
  12.  *
  13.  * SJF        2/12/93        1.0b1        udpate to AOCE beta seed
  14.  * SJF        11/6/91        1.0d1        initial coding
  15.  *
  16.  */
  17.  
  18. #ifndef __TYPES__
  19. #include <Types.h>
  20. #endif
  21.  
  22. #ifndef __PACKAGES__
  23. #include <Packages.h>
  24. #endif
  25.  
  26. #ifndef __GESTALTEQU__
  27. #include <GestaltEqu.h>
  28. #endif
  29.  
  30. #ifndef __TRAPS__
  31. #include <Traps.h>
  32. #endif
  33.  
  34. #ifndef __NOTIFICATION__
  35. #include <Notification.h>
  36. #endif
  37.  
  38. #ifndef __PALETTES__
  39. #include <Palettes.h>
  40. #endif
  41.  
  42. #include "const.h"
  43. #include "globals.h"
  44. #include "trapavailable.h"
  45. #include "main.h"
  46. #include "mymenus.h"
  47.  
  48. #include "utils.h"
  49.  
  50. Boolean HasColorQD(void)
  51. {
  52.     SysEnvRec theEnv;
  53.     
  54.     if (SysEnvirons(1,&theEnv) != noErr)
  55.         return false;
  56.  
  57.     return (theEnv.hasColorQD);
  58. }
  59.  
  60.  
  61. OSErr DoError(OSErr err)
  62. {
  63.     Str255 errStr;
  64.     Str255 errNumStr;
  65.     
  66.     if (err==noErr)
  67.         return noErr;
  68.     
  69.     NumToString(err,errNumStr);
  70.     pstrcpy(errStr,"\pAn error has occurred: ");
  71.     pstrcat(errStr,errNumStr);
  72.     
  73.     Notify(errStr);
  74. //    DebugStr(errStr);
  75.  
  76.     return err;
  77. }
  78.  
  79.  
  80. void Notify(StringPtr string)
  81. {
  82.     NMRecPtr nm;
  83.     StringPtr strPtr;
  84.     
  85.     nm = (NMRecPtr)NewPtr(sizeof(NMRec));
  86.     if (MemError()!=noErr)
  87.         return;
  88.     strPtr = (StringPtr)NewPtr(string[0]);
  89.     if (MemError()!=noErr)
  90.         return;
  91.     BlockMove(string,strPtr,string[0]+1);
  92.     
  93.     nm->qType = nmType;
  94.     nm->nmMark = 0;
  95.     nm->nmIcon = nil;
  96.     nm->nmSound = nil;
  97.     nm->nmStr = strPtr;
  98.     nm->nmResp = nil;
  99.     NMInstall(nm);
  100. }
  101.  
  102.  
  103. void pstrcpy(void *dest,void *src)
  104. {
  105.     unsigned char srcLen = ((unsigned char *)src)[0];
  106.     
  107.     BlockMove(src,dest,srcLen+1);
  108. }
  109.  
  110.  
  111. void pstrcat(void *original,void *catStr)
  112. {
  113.     short length;
  114.     unsigned char originalLen = ((unsigned char *)original)[0];
  115.     unsigned char catStrLen = ((unsigned char *)catStr)[0];
  116.     
  117.     length = (short) originalLen;
  118.     length += (short) catStrLen;
  119.     
  120.     if (length > 255) {
  121.         DebugStr("\pstring catenation overflow");
  122.         ExitProc();
  123.     }
  124.     
  125.     BlockMove((char *)catStr+1,(char *)original+originalLen+1,catStrLen);
  126.     ((unsigned char *)original)[0] = (unsigned char) length;
  127. }
  128.  
  129.  
  130. void *NewPtrChk(Size ptrSize)
  131. {
  132.     Ptr thePtr;
  133.  
  134.     thePtr = NewPtr(ptrSize);
  135.     if (MemError()!=noErr) {
  136.         thePtr = nil;
  137.         DoError(MemError());
  138.     }
  139.     
  140. #if kDEBUG
  141.     {
  142.         long *longPtr = (long *)thePtr;
  143.         *longPtr = kBetterBusErr;
  144.     }
  145. #endif
  146.     return thePtr;
  147. }
  148.  
  149.  
  150. void *NewHandleChk(Size hndlSize)
  151. {
  152.     Handle theHndl;
  153.     
  154.     theHndl = NewHandle(hndlSize);
  155.     if (MemError()!=noErr) {
  156.         theHndl = nil;
  157.         DoError(MemError());
  158.     }
  159.     
  160. #if kDEBUG
  161.     {
  162.         long **longHndl = (long **)theHndl;
  163.         **longHndl = kBetterBusErr;
  164.     }
  165. #endif
  166.     return theHndl;
  167. }
  168.  
  169.  
  170. void DisposPtrChk(void *thePtr)
  171. {
  172. #if kDEBUG
  173.     {
  174.         long *longPtr = (long *)thePtr;
  175.         *longPtr = kBetterBusErr;
  176.     }
  177. #endif
  178.  
  179.     DisposPtr(thePtr);
  180.     if (MemError()!=noErr)
  181.         DoError(MemError());
  182. }
  183.  
  184.  
  185. void DisposHandleChk(void *theHndl)
  186. {
  187. #if kDEBUG
  188.     {
  189.         long **longHndl = (long **)theHndl;
  190.         **longHndl = kBetterBusErr;
  191.     }
  192. #endif
  193.  
  194.     DisposHandle(theHndl);
  195.     if (MemError()!=noErr)
  196.         DoError(MemError());
  197. }
  198.  
  199.  
  200. OSErr WaitPBDone(void *voidBlock)
  201. {
  202.     IOParam *pBlock;
  203.     
  204.     pBlock = (IOParam *)voidBlock;
  205.     while (pBlock->ioResult > 0)
  206.         Yield();
  207.     return pBlock->ioResult;
  208. }
  209.  
  210.  
  211. void Yield(void)
  212. {
  213.     EventRecord ev;
  214.     
  215.     EventAvail(everyEvent,&ev);
  216. }
  217.  
  218.  
  219. void ExitProc(void)
  220. {
  221.     ExitToShell();
  222. }
  223.  
  224.  
  225. #define    kButtonFrameInset    -4
  226. #define    kButtonFrameSize    3
  227. #define    kCntrActivate        0
  228. #define    kColorPortMask        0xc000
  229. #define    kActivateControl    0
  230.  
  231. void MyDrawDefaultButtonOutline(DialogPtr theDialog,short theItem)
  232. {
  233.     short itemType;
  234.     Rect itemRect;
  235.     ControlHandle itemHandle;
  236.     PenState curPen;
  237.     short buttonOval;
  238.     RGBColor fgSaveColor,bgColor,newFGColor;
  239.     Boolean newGray;
  240.     WindowPtr oldPort;
  241.     Boolean isColor;
  242.     GDHandle targetDevice;
  243.     
  244.     // get the default button and draw a bold border around it
  245.     
  246.     GetDItem(theDialog,theItem,&itemType,(Handle *)&itemHandle,&itemRect);
  247.     GetPort(&oldPort);
  248.     SetPort((**itemHandle).contrlOwner);
  249.     GetPenState(&curPen);
  250.  
  251.     PenNormal();
  252.     InsetRect(&itemRect,kButtonFrameInset,kButtonFrameInset);
  253.     FrameRoundRect(&itemRect,16,16);
  254.  
  255.     buttonOval = ((itemRect.bottom-itemRect.top)/2) + 2;
  256.     if (((CGrafPtr)((**itemHandle).contrlOwner))->portVersion & kColorPortMask)
  257.         isColor = true;
  258.     else
  259.         isColor = false;
  260.     
  261.     if ((**itemHandle).contrlHilite != kActivateControl) {    // control is dimmed, so draw gray
  262.         newGray = false;
  263.         if (isColor) {
  264.             GetBackColor(&bgColor);
  265.             GetForeColor(&fgSaveColor);
  266.             newFGColor = fgSaveColor;
  267.             targetDevice = MyGetDeviceFromRect(&(**itemHandle).contrlRect);
  268.             newGray = GetGray(targetDevice,&bgColor,&newFGColor);
  269.         }
  270.         if (newGray)
  271.             RGBForeColor(&newFGColor);
  272.         else
  273.             PenPat(qd.gray);
  274.         PenSize(kButtonFrameSize,kButtonFrameSize);
  275.         FrameRoundRect(&itemRect,buttonOval,buttonOval);
  276.         if (isColor)
  277.             RGBForeColor(&fgSaveColor);
  278.     }
  279.     else {
  280.         PenPat(qd.black);
  281.         PenSize(kButtonFrameSize,kButtonFrameSize);
  282.         FrameRoundRect(&itemRect,buttonOval,buttonOval);
  283.     }
  284.     
  285.     SetPenState(&curPen);
  286.     SetPort(oldPort);
  287. }
  288.  
  289.  
  290. GDHandle MyGetDeviceFromRect(Rect *localRect)
  291. {
  292.     GDHandle device;
  293.     Point wCenter;
  294.     
  295.     wCenter.v = localRect->bottom-localRect->top;
  296.     wCenter.h = localRect->right-localRect->left;
  297.     LocalToGlobal(&wCenter);
  298.     
  299.     device = GetDeviceList();
  300.     while (device) {
  301.         if (PtInRect(wCenter,&(**device).gdRect))
  302.             return device;
  303.         device = GetNextDevice(device);
  304.     }
  305.     
  306.     return GetMainDevice();
  307. }
  308.  
  309.  
  310. void FlashControl(DialogPtr theDialog,short item)
  311. {
  312.     short iType;
  313.     ControlHandle iHndl;
  314.     Rect iRect;
  315.     long fTicks;
  316.         
  317.     GetDItem(theDialog,item,&iType,(Handle *)&iHndl,&iRect);
  318.     HiliteControl(iHndl,inButton);
  319.     Delay(8,&fTicks);
  320.     HiliteControl(iHndl,0);
  321. }