[comp.sys.mac.programmer] CDEF problem

mikeoro@hubcap.clemson.edu (Michael K O'Rourke) (05/12/89)

HELP!!!

I am new to writing DAs and CODE resources on the Mac.  I have been trying to
write a PICT button CDEF for a game i am creating and for general use.
However, I have been running into major snags.  I finally got everything
working, but i have one little problem left.  When I am finished with the
button and go to do a KillControls or DisposControl, i get an out of memory
error and the machine locks up.  Below is the LSC CDEF code.  Can you tell me
if there is something outright and stupid that i'm doing/not doing?

Thanx,
Michael O'Rourke




#include <Quickdraw.h>
#include <ControlMgr.h>
#include <OsUtil.h>
#include <SetUpA4.h>
#include <WindowMgr.h>
#include <EventMgr.h>
#include <ToolboxUtil.h>

pascal long main(varCode, theControl, message, param)
int varCode;
ControlHandle theControl;
int message;
long param;
{
   long myTestCtrl();
   void myInitCtrl(),myCalcCtrl();

   switch (message) {
   case drawCntl:
   if ((**theControl).contrlVis)
   myDrawCtrl(varCode, theControl, param);
   break;
   case testCntl:
   return(myTestCtrl(theControl, param));
   break;
   case calcCRgns: /* defines region */
   myCalcCtrl(theControl, param);
   break;
   case initCntl:
   myInitCtrl(theControl,varCode);
   break;
   case dispCntl:  /* no disposal */
   break;
   case posCntl:   /* no position */
   break;
   case thumbCntl: /* no thumb */
   break;
   case dragCntl:  /* no drag */
   break;
   case autoTrack: /* no auto tracking */
   break;
   }
   return((long) 0);
}

void myCalcCtrl(theControl,param)
ControlHandle theControl;
long param;
{
register ControlPtr cp;

   cp = *theControl;

   param = 0L;

   SetEmptyRgn((RgnHandle) param);
   RectRgn(param,&cp->contrlRect);
}

myDrawCtrl(varCode, theControl, param)
int varCode;
ControlHandle theControl;
long param;
{
   PenState pstate;
   register ControlPtr cp;
   Handle pict;
   Rect R;
   Point MLoc;
   int i;
   RgnHandle saveClip;
   unsigned int high;
   FontInfo finfo;

   HLock(theControl);  /* lock our data, just in case */
   cp = *theControl;
   varCode = LoWord(varCode);
   saveClip = NewRgn();
   GetClip(saveClip);
   ClipRect(&cp->contrlRect);
   GetFontInfo(&finfo);
   finfo.ascent += finfo.ascent + finfo.leading;

   GetPenState(&pstate);   /* save pen state to be nice */
   PenNormal();/* make no assumptions about the state of things */
   if (cp->contrlHilite == 255) PenPat(gray);

   if (param == 22) {
   if (cp->contrlRfCon != 0) {
   if (cp->contrlHilite != 255)
   InvertRect(&cp->contrlRect);
   }
   }
   else {
   if ((varCode == 0) || (varCode == 1) || (varCode == 5)) {
   pict = GetResource('PICT',cp->contrlValue);
   HLock(pict);
   LoadResource(pict);
   EraseRect(&cp->contrlRect);
   DrawPicture((PicHandle) pict,&cp->contrlRect);
   HUnlock(pict);
   ReleaseResource(pict);
   }
   if ((varCode == 0) || (varCode == 2) || (varCode == 5))
   FrameRect(&cp->contrlRect);
   if (varCode == 5){
   R = cp->contrlRect;
   MoveTo(R.right,R.top+2);
   LineTo(R.right,R.bottom+1);
   MoveTo(R.left+2,R.bottom);
   LineTo(R.right+1,R.bottom);
   }
   R = cp->contrlRect;
   MoveTo((R.left + R.right - StringWidth(cp->contrlTitle))/2,
   R.top + (R.bottom - R.top + finfo.ascent)/2);
   DrawString(cp->contrlTitle);
   }

   SetClip(saveClip);
   SetPenState(&pstate);
   HUnlock(theControl);
   DisposeRgn(saveClip);
}

void
myInitCtrl(theControl,varCode)
ControlHandle theControl;
int varCode;
{
   register ControlPtr cp;
   int width,height;
   PicHandle pict;
   Rect size;

   cp = *theControl;
   pict = (PicHandle) GetResource('PICT',cp->contrlMin);
   HLock((Handle) pict);
   LoadResource((Handle) pict);
   size = (**pict).picFrame;
   width = size.right - size.left;
   height = size.bottom - size.top;
   (*cp).contrlRect.right = (*cp).contrlRect.left + width;
   (*cp).contrlRect.bottom = (*cp).contrlRect.top + height;
   HUnlock((Handle) pict);
   ReleaseResource((Handle) pict);
}

long
myTestCtrl(theControl, param)
ControlHandle theControl;
long param;
{
   register ControlPtr cp;
   Point MLoc;

   MLoc.v = HiWord(param);
   MLoc.h = LoWord(param);

   cp = *theControl;

   if(PtInRect(MLoc, &(**theControl).contrlRect)) return((long) 22);
   return((long)0);
}