[comp.sys.mac.programmer] Hilight "OK" buttons -- standard filter

minow@thundr.dec.com (Fortran for Precedent) (09/12/88)

/*
 * This is taken from a demo program that Mike Kahl (who wrote Think C)
 * passed out at a Boston Computer Society meeting.  It is intended to
 * be used as the filterProc argument to ModalDialog, and handles highlighting
 * the OK button.  I believe it can also be used for this function
 * in the SF...file procedures.  Usage:
 *	dialog = GetNewDialog(...);
 *	do {
 *		ModalDialog(standard_filter, &item);
 *	} while (item != OK && item != Cancel);
 *
 * Martin Minow
 * minow%thundr.dec@decwrl.dec.com
 */
void	ring_button(DialogPeek);
int	flash_button(DialogPeek, int);

/*
 * Dialog support routines
 *
 * Replacement for the default ModalDialog procedure.  It does everything
 * the default filter does, plus it draws a ring around the OK button.
 */
pascal Boolean
standard_filter(dp, event, item)
DialogPeek	dp;
EventRecord	*event;
int		*item;
{
	GrafPtr		save_port;
	Boolean		result = TRUE;
	int		c;
		

	GetPort(&save_port);
	switch (event->what) {		/* Handle standard events	*/
	case updateEvt:
	    if ((DialogPeek) event->message == dp)
		ring_button(dp);
	    break;
		
	case keyDown:
	    c = (char) event->message;
	    if (c == '\r' || c == '\003') {
		if ((*item = flash_button(dp, dp->aDefItem)) != FALSE)
		    goto done;
		event->what = nullEvent;
	    }
	    break;
	}
	result = FALSE;
done:	SetPort(save_port);
	return (result);
}		

/*
 * Draw a ring around the OK (default) item.
 * (Should it do a GetPort, too?)
 */
static void
ring_button(dp)
register DialogPeek	dp;
{
	int		type;
	ControlHandle	button;
	Rect		box;
		
	if (dp->aDefItem) {
	    GetDItem(dp, dp->aDefItem, &type, &button, &box);
	    if (type == ctrlItem + btnCtrl) {
		SetPort(dp);
		PenNormal();
		PenSize(3, 3);
		InsetRect(&box, -4, -4);
		FrameRoundRect(&box, 16, 16);
		PenNormal();
	    }
	}
}

/*
 * Pretend the button was clicked.  Returns the item number,
 * or zero if it was disabled.
 */
static int
flash_button(dp, item)
DialogPeek	dp;
int		item;
{
	int		type;
	ControlHandle	button;
	Rect		box;
	long		dummy;
		
	if (item != 0) {
	    GetDItem(dp, item, &type, &button, &box);
	    if (type == ctrlItem + btnCtrl) {
		if ((**button).contrlHilite)
		    item = 0;
		else {
		    HiliteControl(button, inButton);
		    Delay(8, &dummy);
		    HiliteControl(button, 0);
		}
	    }
	}
	return (item);
}