Why am I not receiving kEventControlHit events for some of the parts of my custom HIView?

Q: Why am I not receiving kEventControlHit events for some of the parts of my custom HIView?

A: The Control Manager of the early days of the Macintosh was using 1 byte part codes. The values from 1 to 127 were available to any non-moving part, while the values from 128 to 255 were reserved for indicators. 0 meant no part.

Since the HIView architecture is an extension of the Control Manager, part codes can now use the range 1 to 32767, but the special meaning of the earlier ranges has been preserved. Currently the range 256 to 32767 is meaningful only in HIMenuViews.

If

  • you create a custom HIView, _and_

  • you handle the kEventControlHitTest event, _and_

  • you wish to receive kEventControlHit events, _and/or_

  • you wish to receive kEventCommandProcess events if your HIView has a command ID,

then you must return a part code in the range 1 to 127 in your kEventControlHitTest handler.

If you do not wish to reuse the part code range currently used by Apple (1 to 27 in Mac OS X v10.4), we suggest that you use custom part codes starting at 127 and going down if you have multiple parts.

Listing 1: Typical kEventControlHitTest handler.

case kEventControlHitTest:
{
  // the point parameter is in view-local coords.
  HIPoint  pt;
  GetEventParameter(inEvent, kEventParamMouseLocation, typeHIPoint, NULL, sizeof(pt), NULL, &pt);

  HIRect  bounds;
  HIViewGetBounds(myData->view, &bounds);

  if (CGRectContainsPoint(bounds, pt))
  {
    ControlPartCode part = 127;

    SetEventParameter(inEvent, kEventParamControlPart, typeControlPartCode, sizeof(part), &part);
    result = noErr;
  }
  else
    result = eventNotHandledErr;

  break;
}

Note: If you create a custom HIView which is not a HIMenuView, part codes outside of the range 1 to 127 will not trigger the kEventControlHit or kEventCommandProcess events, even if you use the kHIViewDoesNotUseSpecialParts feature.

If you wish to handle kEventControlTrack events yourself then you are free to use any part code value between 1 and 32767. You are then responsible for posting or sending the other kinds of events according to your custom HIView design.

Document Revision History

DateNotes
2005-07-14Explains why part codes greater than 127 should not be used

Posted: 2005-07-14


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.