[comp.sys.mac.programmer] CDEF's

poleary@bonnie.ics.uci.edu (Petey O'Leary) (01/27/89)

	I am trying to code a simple CDEF that is exactly like the standard
push-button, only with square instead of rounded corners.  The CDEF that
I have functions except that no "tracking" seems to be done when I
click in the control.  By this I mean that when I click in the button,
it is not inverted and it seems that action takes place on the mouse-
down rather than the mouse-up, like a standard push-button.

	The code seems very simple but obviously I have missed some detail.
I'm working in LightspeedC and my only source of info so far has been the 
Inside Mac volumes.  IM hasn't been much help.

	Anyone have any hints/suggestions/source fragments to offer.  Mail
them and I will post when I figure this out.

Thanks in advance.


Peter O'Leary
poleary@bonnie.ics.edu

thecloud@dhw68k.cts.com (Ken McLeod) (01/30/89)

In article <5631@paris.ics.uci.edu> Petey O'Leary <poleary%bonnie.ics.uci.edu@orion.cf.uci.edu> writes:
>
>	I am trying to code a simple CDEF that is exactly like the standard
>push-button, only with square instead of rounded corners.  The CDEF that
>I have functions except that no "tracking" seems to be done when I
>click in the control.  By this I mean that when I click in the button,
>it is not inverted and it seems that action takes place on the mouse-
>down rather than the mouse-up, like a standard push-button.

 It sounds like a CDEF may be overkill for what you want to do. Here's
a simple function I use for tracking rectangular buttons (replace the
calls to InvertRect() with InvertOval() if you want a circular button,
and so on).
/*===================================================================*/
short
TrackButton ()
/* called when we determine that a mouseDown has occurred in the rect
   of our 'pseudo-button'; handles button tracking until the mouse is
   up, then returns 1 (true) if the mouse was released inside the button,
   or 0 (false) if released outside the button. */
{
	short	itemtype, struck;
	Handle	itemhand;
	Rect	itemrect;
	Point	mPt;

	/* set up 'itemrect' to be your button's rectangle here; you could
	   also pass it as a parameter to this function if you wanted */
	InsetRect(&itemrect, 1, 1);
	InvertRect(&itemrect);
	struck = 1;							/* used to track inverted state */
	while (Button())	{
		GetMouse(&mPt);
		if (PtInRect(mPt, &itemrect))	{
			if (!struck)	{
				InvertRect(&itemrect);
				struck = !struck;
			}
		}
		else if (!(PtInRect(mPt, &itemrect)))	{
			if (struck)	{
				InvertRect(&itemrect);
				struck = !struck;
			}
		}
	}
	if (struck)	{
		/* mouse was released in button box */
		InvertRect(&itemrect);
		return(1);
	}
	else return(0);
}

/*===================================================================*/

Hope that helps...
-ken



-- 
==========     .......     =============================================
Ken McLeod    :.     .:    felix!dhw68k!thecloud@ics.uci.edu
==========   :::.. ..:::   InterNet: thecloud@dhw68k.cts.com
                ////       =============================================

jmunkki@kampi.hut.fi (Juri Munkki) (02/01/89)

In article <19324@dhw68k.cts.com> thecloud@dhw68k.cts.com (Ken McLeod) writes:
> It sounds like a CDEF may be overkill for what you want to do. Here's
>a simple function I use for tracking rectangular buttons (replace the
>calls to InvertRect() with InvertOval() if you want a circular button,
>and so on).

>	while (Button())	{

I used to track buttons this way myself (try my Mandelbrot DA), but discovered
that QuicKeys does not work correctly with buttons that use "Button()" to
determine when the mouse came up. I think the correct way to do this tracking
is to use GetNextEvent to get a mouseUp event.

    {	GetNextEvent(mUpMask+mDownMask,&Event);
	... do your highlighting etc. here.	
    } 	while(Event.what!=mouseUp);
    Now unhilite the button if it was hilited.
    Use Event.where to find if mouse was within button on mouseup...

I don't know why I have the mDownMask there, but it doesn't seem to hurt and
I remember putting it there for some reason...

Your PtInRect/inverting routine can easily be optimized to about 1/2 size.

_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
|     Juri Munkki jmunkki@hut.fi  jmunkki@fingate.bitnet        I Want   Ne   |
|     Helsinki University of Technology Computing Centre        My Own   XT   |
~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~