Professional Web Applications Themes

how to properly handle events in OS X - Mac Programming

I have figured out how to install the necessary event handlers and how to handle those events. However, Carbon seems to provide numerous ways of handling the same event. For example, I could handle mouse clicks in a window (for a drawing program say) in at least two ways shown below. The same example also suggests that menu events can be handled in two ways either directly as in method 1 below, or through the window, as in method 2 below. How am I supposed to know which way is the "right" way if both ways work? Thank. METHOD 1 ...

  1. #1

    Default how to properly handle events in OS X

    I have figured out how to install the necessary event handlers and how to
    handle those events. However, Carbon seems to provide numerous ways of
    handling the same event. For example, I could handle mouse clicks in a
    window (for a drawing program say) in at least two ways shown below. The
    same example also suggests that menu events can be handled in two ways
    either directly as in method 1 below, or through the window, as in method
    2 below. How am I supposed to know which way is the "right" way if both
    ways work?

    Thank.

    METHOD 1 ================================================== =================
    EventTypeSpec eventTypesMD = {kEventClassMouse, kEventMouseDown};
    InstallApplicationEventHandler (NewEventHandlerUPP(MouseDownHandler),
    1, &eventTypesMD, NULL, NULL);

    pascal OSStatus MouseDownHandler(EventHandlerCallRef nextHandler,
    EventRef theEvent, void* userData)
    {
    UInt32 modifierKeys;
    GetEventParameter (theEvent, kEventParamKeyModifiers, typeUInt32,
    NULL, sizeof(modifierKeys), NULL, &modifierKeys);
    Point where;
    GetEventParameter (theEvent, kEventParamMouseLocation, typeQDPoint,
    NULL, sizeof(Point), NULL, &where);
    HandleContentClick(where, (modifierKeys & controlKey));

    return noErr;
    }

    METHOD 2 ================================================== =================
    EventTypeSpec windEventList[] = {{kEventClassWindow, kEventWindowDrawContent },
    {kEventClassWindow, kEventWindowClickContentRgn },
    {kEventClassWindow, kEventWindowBoundsChanged},
    {kEventClassCommand, kEventCommandProcess}};

    InstallWindowEventHandler(gMainWindow,
    NewEventHandlerUPP(WindowHandler), 4, windEventList, NULL, NULL);

    pascal OSStatus WindowHandler(EventHandlerCallRef nextHandler,
    EventRef theEvent, void* userData)
    {
    WindowRef window;
    Rect bounds;
    UInt32 whatHappened;
    HICommand commandStruct;
    MenuRef theMenuRef;
    UInt16 theMenuItem;
    OSStatus result = eventNotHandledErr;

    GetEventParameter(theEvent, kEventParamDirectObject, typeWindowRef,
    NULL, sizeof(window), NULL, &window);

    whatHappened = GetEventKind(theEvent);

    switch (whatHappened)
    {
    case kEventWindowDrawContent:
    RedrawMainWindow();
    break;
    case kEventWindowBoundsChanged:
    break;
    case kEventWindowClickContentRgn:
    UInt32 modifierKeys;
    GetEventParameter (theEvent, kEventParamKeyModifiers,
    typeUInt32, NULL, sizeof(modifierKeys),
    NULL, &modifierKeys);
    Point where;
    GetEventParameter (theEvent, kEventParamMouseLocation,
    typeQDPoint, NULL, sizeof(Point),
    NULL, &where);
    HandleSplinePoint(where, (modifierKeys & controlKey));
    break;
    case kEventCommandProcess:
    break;
    default:
    result = CallNextEventHandler (nextHandler, theEvent);
    }

    return result;
    }

    __________________________________________________ ______________________
    Keith Wiley unm.edu
    http://www.unm.edu/~keithw http://www.mp3.com/KeithWiley

    "Yet mark his perfect self-contentment, and hence learn his lesson,
    that to be self-contented is to be vile and ignorant, and that to
    aspire is better than to be blindly and impotently happy."
    -- Edwin A. Abbott, Flatland
    __________________________________________________ ______________________
    Keith Guest

  2. #2

    Default Re: how to properly handle events in OS X

    Small typo in my original question. The functions HandleContentClick and
    HandleSplinePoint should be the same function call in the two examples I
    provided, I just didn't get the function names synchronized between the
    two examples. Sorry.

    __________________________________________________ ______________________
    Keith Wiley unm.edu
    http://www.unm.edu/~keithw http://www.mp3.com/KeithWiley

    "Yet mark his perfect self-contentment, and hence learn his lesson,
    that to be self-contented is to be vile and ignorant, and that to
    aspire is better than to be blindly and impotently happy."
    -- Edwin A. Abbott, Flatland
    __________________________________________________ ______________________
    Keith Guest

  3. #3

    Default Re: how to properly handle events in OS X

    In article <cs.unm.edu>,
    Keith Wiley <unm.edu> wrote:
     

    The main difference between the two things that you did is that one installs and
    application event handler for mousedowns, and the other one installs a window
    event handler for mousedowns. The latter is better because the window event
    handler has more information (i.e., which window received the click) and you are
    very likely to need that information.

    Another way to see that the app handler is wrong is that you wouldn't want to
    handle mousedowns the same way if you had a different kind of window (e.g.,
    preferences) and it received a click.

    hth

    meeroh

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Guest

  4. #4

    Default Re: how to properly handle events in OS X

    In article <mit.edu>,
    Miro Jurisic <org> wrote:
     
    >
    > The main difference between the two things that you did is that one
    > installs and application event handler for mousedowns, and the other
    > one installs a window event handler for mousedowns. The latter is
    > better because the window event handler has more information (i.e.,
    > which window received the click) and you are very likely to need that
    > information.
    >
    > Another way to see that the app handler is wrong is that you wouldn't
    > want to handle mousedowns the same way if you had a different kind of
    > window (e.g., preferences) and it received a click.[/ref]

    What Miro said. :)

    In general, most Carbon events can be handled at a variety of different
    levels (user focus, control, menu, window, application, and in Panther,
    event monitor). For standard UI components, you typically only need to
    think about the control, window, and application levels. You can handle
    a particular type of event at any of those levels. The "right" way is
    the one that makes the most sense for what you're trying to do.

    In other words, if you're just handling an event for a specific control,
    handle it at that level. If you want to handle it for an entire window,
    handle it there. And if you want to handle all occurrences of that
    event the same way across your entire application, handle it there.

    One of the nice things about this event model is that it lets you have
    event handlers for more specific actions and general event handlers for
    actions that aren't already handled. For example, you may want to
    handle a mouse down in one control in a certain way, but you want to
    handle any mouse down that wasn't handled by a control in another way.
    To do that, you'd install a mouse down handler on any control that needs
    specific handling, and another mouse down handler on your application.
    Events that are handled by the controls that have registered handlers
    won't show up in your application handler.

    -Eric

    --
    Eric Albert edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

  5. #5

    Default Re: how to properly handle events in OS X

    Keith Wiley <unm.edu> wrote:
     
    [..] 
    [..] 

    In addition to what others have written:

    You can also install handlers on individual controls. See
    GetControlEventTarget().

    The various different points of entry into the event system allow you to
    control the scope of your handlers. That is, if you handle the mouse
    event at the application level, you have to account for which window,
    which control, etc., is receiving the event. If you handle it at the
    control level, you can let Carbon worry about all that, and simply
    handle the event.

    And as an added bonus, you'll have the opportunity to write more
    portable code that will work with a similar control (or window or
    application) in any subsequent program you write, if you consider the
    scope of the event you're encapsulating.
    Paul Guest

  6. #6

    Default Re: how to properly handle events in OS X

    Thanks guys.

    __________________________________________________ ______________________
    Keith Wiley unm.edu
    http://www.unm.edu/~keithw http://www.mp3.com/KeithWiley

    "Yet mark his perfect self-contentment, and hence learn his lesson,
    that to be self-contented is to be vile and ignorant, and that to
    aspire is better than to be blindly and impotently happy."
    -- Edwin A. Abbott, Flatland
    __________________________________________________ ______________________
    Keith Guest

  7. #7

    Default Re: how to properly handle events in OS X

    Apple's sample code often uses the silly strategy of registering a long
    list of events to the same handler function, then having the handler
    function use a switch() statement to dispatch to code for that specific
    event. Stupid. The carbon event dispatcher has already done the work of
    the switch statement, there is no need for you to do it over again.

    You should register the relevant code with the relevant event.
    David Guest

  8. #8

    Default Re: how to properly handle events in OS X

    In article <sf.sbcglobal.net>,
    David Phillip Oster <org> wrote:
     

    On the other hand, if some event handlers could share a lot of code, it
    might make sense to register a single handler for them (example: keyDown
    and autoKey)

    Reinder
    Reinder Guest

  9. #9

    Default Re: how to properly handle events in OS X

    In article <wxs.nl>,
    Reinder Verlinde <invalid> wrote:
     
    >
    > On the other hand, if some event handlers could share a lot of code, it
    > might make sense to register a single handler for them (example: keyDown
    > and autoKey)[/ref]

    Even then, I'd usually factor out the common part into a helper fuction,
    and often pass in a subFunction for the parts that differ.
    David Guest

Similar Threads

  1. Inherited Control Does Not Handle All Events
    By Jeff in forum ASP.NET Building Controls
    Replies: 2
    Last Post: December 4th, 10:05 PM
  2. How to handle events of controls created at run time?
    By Lalit in forum ASP.NET Building Controls
    Replies: 1
    Last Post: December 23rd, 01:20 AM
  3. Replies: 1
    Last Post: January 1st, 11:29 AM
  4. Can we handle events for user controls
    By Vannela in forum ASP.NET Building Controls
    Replies: 3
    Last Post: November 11th, 04:06 AM
  5. How to handle personalized events of a class in a web form?
    By Roberto López in forum ASP.NET General
    Replies: 4
    Last Post: August 1st, 05:23 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139