Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: Programmer's Guide to MacApp / Part 2 - Working With MacApp
Chapter 13 - Working With Events and Commands


Overview

MacApp provides a sophisticated mechanism for receiving events and dispatching them to objects in your application. MacApp also supplies a mechanism for creating command objects to respond to events by performing an operation that can be done, undone, and redone. MacApp classes used in handling events and commands are shown in Figure 5-1 on page 99.

Although there are many kinds of events, the ones your application most often works with are mouse events, key events, menu events (which MacApp translates into command choices), and Apple events.

Which Object Should Handle an Event or Menu Command

MacApp dispatches events to event-handler objects--objects based on classes that descend from the TEventHandler class. MacApp applications use command-handler objects--objects based on the TCommandHandler class--to respond to events by posting a command. The TCommandHandler class is itself a descendant of TEventHandler. MacApp's TApplication, TDocument, TWindow, and TView classes all descend from TCommandHandler, so any class you define based on one of these classes can both handle events and post commands. In fact, applications rarely define a direct subclass of TEventHandler.

Classes that handle Apple events, either directly or in an attached script, mix in the MScriptableObject class. The TDocument, TFile, TDesignator, TWindow, and other MacApp classes already mix in MScriptable, and the TApplication class mixes in MDefaultScriptableObject. Any class you define that descends from one of these classes has access to the MScriptable methods and fields. You can also mix in MScriptable to any class you define that doesn't already use it.

In general, the objects in a MacApp application divide the responsibility for handling events as follows:

When you add a new function to your application, it is usually clear whether one of these classes should handle the function or whether it should be handled by some other class you have defined.

Working With Behaviors

MacApp supplies the TBehavior class and a number of subclasses to provide a mechanism for altering the behavior of event-handler objects in your application. MacApp's behavior mechanism is described in detail in "Behaviors," beginning on page 101.

Using a behavior object, you can modify an event-handling object without having to change its class definition or define a new subclass. Instead, you define a behavior class, override one or more event-handling methods, instantiate a behavior object, and attach it to the event-handler object whose behavior you wish to modify. In addition to offering great flexibility, the behavior classes you define don't carry around all the fields and methods of an event-handling class, so they tend to be simpler, smaller, and more efficient.

Behaviors can be specified through resources or they can be created and attached procedurally, allowing you to modify event-handler objects "on the fly." For code samples that demonstrate the use of behavior objects, see "Recipe--Using a Behavior Object to Implement Alphabetic Type-Ahead," beginning on page 344, and "Recipe--Using Dependencies and Behaviors to Synchronize Control Views," beginning on page 588.

The TBehavior class contains methods you can override to modify most event-handling behavior. These methods include DoCommandKeyEvent, DoEvent, DoKeyEvent, DoKeyUp, DoMenuCommand, DoMouseCommand, DoMouseUp, DoScriptCommand, DoSetCursor, DoSetupMenus, Draw, and DoToolboxEvent.

You can read more about these methods in the MacApp Class and Method Reference.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
25 JUL 1996