[comp.sys.mac.programmer] Getting resource template ID from Control Handle

jtn@potomac.ads.com (John T. Nelson) (06/25/91)

Okay, here's another little wierd problem I haven't been able to
figure out.  I have some buttons on a Dialog.  The user is clicking on
various items in the Dialog but since I use a mixture of controls and
text in the dialog I need to use FindControl to figure out who got
clicked on and how they should be serviced.

Well... FindControl returns a control handle and not a dialog item
number (the resource template number).  Now I can look at the part
code returned by FindControl to figure out whether or not the control
is a button or not (inButton) but since there is more than one button
in the Dialog I have no way of figuring out WHICH f___ing button was
clicked on because all Find Control will tell me is what the handle of
the control is.  There doesn't seem to be a way to convert from a
Handle to the item number.

Surely there is an easy way to do this.  It's such an obvious problem
that someone must have solved it by now.

Thanks in advance!


---
John T. Nelson			Internet: jtn@potomac.ads.com
Advanced Decision Systems	Uucp: kzin!speaker@mimsy.umd.edu
Arlington, VA (703) 243-1611

stevec@Apple.COM (Steve Christensen) (06/26/91)

jtn@potomac.ads.com (John T. Nelson) writes:
>[...that he has a dialog with a mixture of controls and text that the user
> can click on.  He's calling FindControl to figure out who got clicked on,
> but it returns a ControlHandle and not a dialog item number.  So, he
> wonders how he can get the dialog item number of which control was hit...]

Well, amazingly, the answer is in the Dialog Manager chapter of Inside Mac,
volume I.  If it's a modal dialog, do something like this:

	myDialog = GetNewDialog(...);
	do {
	  ModalDialog(nil, &itemHit);	// wait for user to click on an item
	  switch (itemHit) {		// do item-specific stuff
	    case xxx:
	    ...
	  }
	} while (itemHit != OKitem);	// wait until user clicks "OK"
	DisposDialog(myDialog);

ModalDialog tracks simple buttons and checkboxes correctly (however you may
have to handle scrollbars specially), and stuffs keypresses into the active
edit item.  You get control back every time a mouse clicks on an enabled
dialog item, or a key is hit and there is an active edit item.

If you have a non-modal dialog, you can do something similar to the above in
your main event loop:

	if (GetNextEvent(EveryEvent, &myEvent)) {
	...
	  if (IsDialogEvent(&myEvent))
	    if (DialogSelect(&myEvent, &whichDialog, &itemHit)) {
	      switch (itemHit) {
	        case xxx:
	        ...
	      }
	    }
	}
	...

In any case, pick up IM vol I and peruse the Dialog Manager chapter..

steve

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Steve Christensen			Never hit a man with glasses.
  stevec@apple.com			Hit him with a baseball bat.