[comp.sys.mac] Help with modal dialog filterProc needed

wade@sdacs.ucsd.EDU (Wade S. Blomgren) (08/12/87)

I need some help troubleshooting a segment of code. System used: Mac+,
Dataframe20, LightspeedC version 2.01.  The object is to present a
dialog box with some static text (disabled) and a single OK button
(enabled), and to use my own filterProc to accept keystrokes and save
them away, and simulate the OK button being hit if a return or an 
enter is hit. (See Inside Mac Volume 1, page 415-416). The OK button
is Edit Item #1.

Consider the following snippets of the code. If I call ModalDialog with
a NULL ProcPtr, it behaves as you would expect, ie the OK button is
"hit" if you type return or enter. For this reason I figure the dialog
box's resources are all set up properly.  When I use my filterProc as 
suggested in IM, and I try to force an exit from the modal dialog when 
a return or enter is hit, it just sits there. But it captures my characters 
just fine, and when I actually hit the OK button with the mouse, it exits and 
the characters are all there. Any returns or enters are just "gone", ie
it gets to the right place in the switch, (confirmed by debugging statements),
but the setting of the itemHit and the return(TRUE) don't abort the modal
dialog. So what is wrong with the filterProc?  Any ideas?

Thanks,

Wade Blomgren, UCSD ACS (wade@sdacs.ucsd.edu or ...sdcsvax!sdacs!wade)

-------------------------------------

char	data[16];	/* global storage for the characters typed */
int	index;		/* index into 'data' */

main()        
{
/* Inits, etc, then call my testdialog function from the event loop */
}

int testdialog ()
{
int			itemhit;
DialogPtr	pdialog;

index = 0;
pdialog = GetNewDialog(5,0L,(Ptr) -1);
ModalDialog((ProcPtr) &MyFilter,&itemhit);
DisposDialog(pdialog);
/* look at the results in the data string here, then return */
}

/* the filterProc */
Boolean MyFilter(theDialog,theEvent,theItemHit)
DialogPtr theDialog;
EventRecord *theEvent;
int	*theItemHit;

{
char	c;
switch (theEvent->what) {
	case keyDown:
		c = (char) theEvent->message & 0x7f;
		switch (c){
			case 0x03:   /* enter  */
			case 0x0d:   /* return */
			*theItemHit=1;
			return(TRUE);   /* dialog doesn't terminate when this is done - why?*/
			break;
		default:
			data[index++] = c; /* accept all other characters - this works fine */
			return(FALSE); 
			break;
		}     /* end of the keydown switch */
		default:
			return(FALSE);
		}	/* end of the event switch */
}	/* end of the filterProc */

wade@sdacs.ucsd.EDU (Wade S. Blomgren) (08/13/87)

In article <427@sdacs.ucsd.EDU>, wade@sdacs.ucsd.EDU (That's Me) writes:
  [description of my problem getting a ModalDialog filterProc to return the 
  appropriate values to simulate the return key being hit deleted]

I received 2 prompt replies and the problem is solved. 

------
From: chi@tybalt.caltech.edu (Curt O. Hagenlocher)
....I was experiencing the exact same problem for the longest time, until 
I put the word "pascal" in front of Boolean in the function header.  

------
From: Gregory.Stein@K.GP.CS.CMU.EDU
	I think your problem is that the filter function was not
declared as a Pascal function.  Here would be your new declaration:

pascal Boolean MyFilter(theDialog,theEvent,theItemHit)
------

Thanks for the help.

Wade Blomgren, UCSD ACS (wade@sdacs.ucsd.edu or ...sdcsvax!sdacs!wade)