home *** CD-ROM | disk | FTP | other *** search
- /*-------------------------------------------------------------------------------------
- *
- * IPM MessageBoard AOCE Sample
- *
- * ©1992-1993 Apple Computer
- *
- -------------------------------------------------------------------------------------*/
- /*
- * utils.c -- utility routines
- *
- * change history:
- *
- * SJF 2/12/93 1.0b1 udpate to AOCE beta seed
- * SJF 11/6/91 1.0d1 initial coding
- *
- */
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- #ifndef __PACKAGES__
- #include <Packages.h>
- #endif
-
- #ifndef __GESTALTEQU__
- #include <GestaltEqu.h>
- #endif
-
- #ifndef __TRAPS__
- #include <Traps.h>
- #endif
-
- #ifndef __NOTIFICATION__
- #include <Notification.h>
- #endif
-
- #ifndef __PALETTES__
- #include <Palettes.h>
- #endif
-
- #include "const.h"
- #include "globals.h"
- #include "trapavailable.h"
- #include "main.h"
- #include "mymenus.h"
-
- #include "utils.h"
-
- Boolean HasColorQD(void)
- {
- SysEnvRec theEnv;
-
- if (SysEnvirons(1,&theEnv) != noErr)
- return false;
-
- return (theEnv.hasColorQD);
- }
-
-
- OSErr DoError(OSErr err)
- {
- Str255 errStr;
- Str255 errNumStr;
-
- if (err==noErr)
- return noErr;
-
- NumToString(err,errNumStr);
- pstrcpy(errStr,"\pAn error has occurred: ");
- pstrcat(errStr,errNumStr);
-
- Notify(errStr);
- // DebugStr(errStr);
-
- return err;
- }
-
-
- void Notify(StringPtr string)
- {
- NMRecPtr nm;
- StringPtr strPtr;
-
- nm = (NMRecPtr)NewPtr(sizeof(NMRec));
- if (MemError()!=noErr)
- return;
- strPtr = (StringPtr)NewPtr(string[0]);
- if (MemError()!=noErr)
- return;
- BlockMove(string,strPtr,string[0]+1);
-
- nm->qType = nmType;
- nm->nmMark = 0;
- nm->nmIcon = nil;
- nm->nmSound = nil;
- nm->nmStr = strPtr;
- nm->nmResp = nil;
- NMInstall(nm);
- }
-
-
- void pstrcpy(void *dest,void *src)
- {
- unsigned char srcLen = ((unsigned char *)src)[0];
-
- BlockMove(src,dest,srcLen+1);
- }
-
-
- void pstrcat(void *original,void *catStr)
- {
- short length;
- unsigned char originalLen = ((unsigned char *)original)[0];
- unsigned char catStrLen = ((unsigned char *)catStr)[0];
-
- length = (short) originalLen;
- length += (short) catStrLen;
-
- if (length > 255) {
- DebugStr("\pstring catenation overflow");
- ExitProc();
- }
-
- BlockMove((char *)catStr+1,(char *)original+originalLen+1,catStrLen);
- ((unsigned char *)original)[0] = (unsigned char) length;
- }
-
-
- void *NewPtrChk(Size ptrSize)
- {
- Ptr thePtr;
-
- thePtr = NewPtr(ptrSize);
- if (MemError()!=noErr) {
- thePtr = nil;
- DoError(MemError());
- }
-
- #if kDEBUG
- {
- long *longPtr = (long *)thePtr;
- *longPtr = kBetterBusErr;
- }
- #endif
- return thePtr;
- }
-
-
- void *NewHandleChk(Size hndlSize)
- {
- Handle theHndl;
-
- theHndl = NewHandle(hndlSize);
- if (MemError()!=noErr) {
- theHndl = nil;
- DoError(MemError());
- }
-
- #if kDEBUG
- {
- long **longHndl = (long **)theHndl;
- **longHndl = kBetterBusErr;
- }
- #endif
- return theHndl;
- }
-
-
- void DisposPtrChk(void *thePtr)
- {
- #if kDEBUG
- {
- long *longPtr = (long *)thePtr;
- *longPtr = kBetterBusErr;
- }
- #endif
-
- DisposPtr(thePtr);
- if (MemError()!=noErr)
- DoError(MemError());
- }
-
-
- void DisposHandleChk(void *theHndl)
- {
- #if kDEBUG
- {
- long **longHndl = (long **)theHndl;
- **longHndl = kBetterBusErr;
- }
- #endif
-
- DisposHandle(theHndl);
- if (MemError()!=noErr)
- DoError(MemError());
- }
-
-
- OSErr WaitPBDone(void *voidBlock)
- {
- IOParam *pBlock;
-
- pBlock = (IOParam *)voidBlock;
- while (pBlock->ioResult > 0)
- Yield();
- return pBlock->ioResult;
- }
-
-
- void Yield(void)
- {
- EventRecord ev;
-
- EventAvail(everyEvent,&ev);
- }
-
-
- void ExitProc(void)
- {
- ExitToShell();
- }
-
-
- #define kButtonFrameInset -4
- #define kButtonFrameSize 3
- #define kCntrActivate 0
- #define kColorPortMask 0xc000
- #define kActivateControl 0
-
- void MyDrawDefaultButtonOutline(DialogPtr theDialog,short theItem)
- {
- short itemType;
- Rect itemRect;
- ControlHandle itemHandle;
- PenState curPen;
- short buttonOval;
- RGBColor fgSaveColor,bgColor,newFGColor;
- Boolean newGray;
- WindowPtr oldPort;
- Boolean isColor;
- GDHandle targetDevice;
-
- // get the default button and draw a bold border around it
-
- GetDItem(theDialog,theItem,&itemType,(Handle *)&itemHandle,&itemRect);
- GetPort(&oldPort);
- SetPort((**itemHandle).contrlOwner);
- GetPenState(&curPen);
-
- PenNormal();
- InsetRect(&itemRect,kButtonFrameInset,kButtonFrameInset);
- FrameRoundRect(&itemRect,16,16);
-
- buttonOval = ((itemRect.bottom-itemRect.top)/2) + 2;
- if (((CGrafPtr)((**itemHandle).contrlOwner))->portVersion & kColorPortMask)
- isColor = true;
- else
- isColor = false;
-
- if ((**itemHandle).contrlHilite != kActivateControl) { // control is dimmed, so draw gray
- newGray = false;
- if (isColor) {
- GetBackColor(&bgColor);
- GetForeColor(&fgSaveColor);
- newFGColor = fgSaveColor;
- targetDevice = MyGetDeviceFromRect(&(**itemHandle).contrlRect);
- newGray = GetGray(targetDevice,&bgColor,&newFGColor);
- }
- if (newGray)
- RGBForeColor(&newFGColor);
- else
- PenPat(qd.gray);
- PenSize(kButtonFrameSize,kButtonFrameSize);
- FrameRoundRect(&itemRect,buttonOval,buttonOval);
- if (isColor)
- RGBForeColor(&fgSaveColor);
- }
- else {
- PenPat(qd.black);
- PenSize(kButtonFrameSize,kButtonFrameSize);
- FrameRoundRect(&itemRect,buttonOval,buttonOval);
- }
-
- SetPenState(&curPen);
- SetPort(oldPort);
- }
-
-
- GDHandle MyGetDeviceFromRect(Rect *localRect)
- {
- GDHandle device;
- Point wCenter;
-
- wCenter.v = localRect->bottom-localRect->top;
- wCenter.h = localRect->right-localRect->left;
- LocalToGlobal(&wCenter);
-
- device = GetDeviceList();
- while (device) {
- if (PtInRect(wCenter,&(**device).gdRect))
- return device;
- device = GetNextDevice(device);
- }
-
- return GetMainDevice();
- }
-
-
- void FlashControl(DialogPtr theDialog,short item)
- {
- short iType;
- ControlHandle iHndl;
- Rect iRect;
- long fTicks;
-
- GetDItem(theDialog,item,&iType,(Handle *)&iHndl,&iRect);
- HiliteControl(iHndl,inButton);
- Delay(8,&fTicks);
- HiliteControl(iHndl,0);
- }