hide random home http://www.be.com/documentation/be_book/app/Handler.html (Amiga Plus Extra No. 5/97, 05/1997)


The Application Kit: BHandler

Derived from: public BArchivable

Declared in: <app/Handler.h>


Overview

BHandlers are the objects that respond to messages received in message loops. The class declares a hook function--MessageReceived()--that derived classes must implement to handle expected messages. BLooper's DispatchMessage() function calls MessageReceived() to pass incoming messages from the BLooper to the BHandler.

All messages are entrusted to BHandler objects--even system messages, which are dispatched by calling a message-specific function, not MessageReceived(). These specific functions are declared in classes derived from BHandler--especially BWindow and BView in the Interface Kit and BLooper and BApplication in this kit. For example, the BApplication class declares a ReadyToRun() function to respond to B_READY_TO_RUN messages, and the BView class declares a KeyDown() function to respond to B_KEY_DOWN messages.

All messages that aren't matched to a specific hook function--messages defined by applications rather than the kits--are dispatched by calling MessageReceived().


Associations

To be eligible to get messages from a BLooper, a BHandler must be in the BLooper's circle of handlers. At any given time, a BHandler can belong to only one BLooper. BLooper's AddHandler() function is the agent for forming looper-handler associations; a BHandler's Looper() function identifies the BLooper it's currently associated with.

BHandlers that belong to the same BLooper can be chained together in a linked list. The default behavior for MessageReceived() is simply to pass the message to the next handler in the chain. However, system messages are not passed from handler to handler.

When a BHandler is assigned to a BLooper, the BLooper becomes its default next handler. That assignment can be changed by SetNextHandler().


Targets

You can designate a target BHandler for most messages. The designation is made when calling BLooper's PostMessage() function or when constructing the BMessenger object that will send the message. Messages that a user drags and drops are targeted to the object (a BView) that controls the part of the window where the message was dropped. The messaging mechanism eventually passes the target BHandler to DispatchMessage(), so that the message can be delivered to its designated destination.


Filtering

Messages can be filtered before they're dispatched--that is, you can define a function that will look at the message before the target BHandler's hook function is called. The filter function is associated with a BMessageFilter object, which records the criteria for calling the function.

Filters that should apply only to messages targeted to a particular BHandler are assigned to the BHandler by SetFilterList() or AddFilter(). Filters that might apply to any message a BLooper dispatches, regardless of its target, are assigned by the parallel BLooper functions, SetCommonFilterList() and AddCommonFilter(). See those functions and the BMessageFilter class for details.


Hook Functions

MessageReceived() Implemented to handle received messages.


Constructor and Destructor


BHandler()


      BHandler(const char *name = NULL) 
      BHandler(BMessage *archive) 

Initializes the BHandler by assigning it a name and registering it with the messaging system. Because BHandlers are archivable objects, they can also be reconstructed from a BMessage archive.


~BHandler()


      virtual ~BHandler(void)

Removes the BHandler's registration, frees the memory allocated for its name, and gets rid of any BMessageFilters assigned to the BHandler and the BList object that holds them.


Static Functions


Instantiate()


      static BHandler *Instantiate(BMessage *archive) 

Returns a new BHandler object, allocated by new and created with the version of the constructor that takes a BMessage archive. However, if the archive doesn't contain data for a BHandler of some kind, this function returns NULL.

See also: BArchivable::Instantiate(), instantiate_object(), Archive()


Member Functions


AddFilter() see SetFilterList()


Archive()


      virtual status_t Archive(BMessage *archive, bool deep = true) const

Archives the BHandler by writing its name, if any, to the BMessage archive.

See also: BArchivable::Archive(), Instantiate() static function


FilterList() see SetFilterList()


GetSupportedSuites()


      virtual status_t GetSupportedSuites(BMessage *message) 

< Documentation of the scripting system is not ready for the prerelease, but will be available soon. >


Looper()


      BLooper *Looper(void) const

Returns the BLooper object that the BHandler is associated with, or NULL if it's not associated with any BLooper. A BHandler must be associated with a BLooper before the BLooper can call upon it to handle messages it dispatches. (However, strictly speaking, this restriction is imposed when the message is posted or when the BMessenger that will send it is constructed, rather than when it's dispatched.)

BLooper objects are automatically associated with themselves; they can act as handlers only for messages that they receive in their own message loops. All other BHandlers must be explicitly tied to a particular BLooper by calling that BLooper's AddHandler() function. A BHandler can be associated with only one BLooper at a time.

In the Interface Kit, when a BView is added to a window's view hierarchy, it's also added as a BHandler to the BWindow object.

See also: BLooper::AddHandler(), BLooper::PostMessage(), the BMessenger constructor


MessageReceived()


      virtual void MessageReceived(BMessage *message)

Implemented by derived classes to respond to messages that are dispatched to the BHandler. The default (BHandler) implementation of this function responds only to scripting requests. It passes all other messages to the next handler by calling that object's version of MessageReceived().

You must implement MessageReceived() to handle the variety of messages that might be dispatched to the BHandler. It can distinguish between messages by the value recorded in the what data member of the BMessage object. For example:

   void MyHandler::MessageReceived(BMessage *message)
   {
       switch ( message->what ) {
       case COMMAND_ONE:
           . . .
           break;
       case COMMAND_TWO:
           . . .
           break;
       case COMMAND_THREE:
           . . .
           break;
       default:
           inherited::MessageReceived(message);
           break;
       . . .
       }
   }

When defining a version of MessageReceived(), you must incorporate the inherited version as well, as shown in the example above. This ensures that:

If the message comes to the end of the line--if it's not recognized and there is no next handler--the BHandler version of this function sends a B_MESSAGE_NOT_UNDERSTOOD reply to notify the message source.

See also: SetNextHandler(), BLooper::PostMessage(), BLooper::DispatchMessage()


NextHandler() see SetNextHandler()


Perform()


      virtual status_t Perform(uint32 op, void *data) 

< Calls the inherited version of Perform(), which, like this version, does nothing. This function may be implemented in a future release; it should not now be implemented or called in application code. >


ResolveSpecifier()


      virtual BHandler *ResolveSpecifier(BMessage *message, int32 index, BMessage *specifier, int32 command, const char *property)

< Documentation on the scripting sytem will be available soon. >


SetFilterList(), FilterList(), AddFilter(), RemoveFilter()


      virtual void SetFilterList(BList *list)

      BList *FilterList(void) const

      virtual void AddFilter(BMessageFilter *filter)

      virtual bool RemoveFilter(BMessageFilter *filter)

These functions manage a list of BMessageFilter objects associated with the BHandler.

SetFilterList() assigns the BHandler a new list of filters; the list must contain pointers to instances of the BMessageFilter class or to instances of classes that derive from BMessageFilter. The new list replaces any list of filters previously assigned. All objects in the previous list are deleted, as is the BList that contains them. If list is NULL, the current list is removed without a replacement. FilterList() returns the current list of filters.

AddFilter() adds a filter to the end of the BHandler's list of filters. It creates the BList object if it doesn't already exist. By default, BHandlers don't maintain a BList of filters until one is assigned or the first BMessageFilter is added. RemoveFilter() removes a filter from the list without deleting it. It returns true if successful, and false if it can't find the specified filter in the list (or the list doesn't exist). It leaves the BList in place even after removing the last filter.

For SetFilterList(), AddFilter(), and RemoveFilter() to work, the BHandler must be assigned to a BLooper object and the BLooper must be locked.

See also: BLooper::SetCommonFilterList(), BLooper::Lock(), the BMessageFilter class


SetName(), Name()


      void SetName(const char *string)

      const char *Name(void) const

These functions set and return the name that identifies the BHandler. The name is originally set by the constructor. SetName() assigns the BHandler a new name, and Name() returns the current name. The string returned by Name() belongs to the BHandler object; it shouldn't be altered or freed.

See also: the BHandler constructor, BView::FindView() in the Interface Kit


SetNextHandler(), NextHandler()


      void SetNextHandler(BHandler *handler)

      BHandler *NextHandler(void) const

These functions set and return the BHandler object that's linked to this BHandler. By default, the MessageReceived() function passes any messages that a BHandler can't understand to its next handler.

When a BHandler object is added to a BLooper (by BLooper's AddHandler() function), the BLooper becomes its next handler by default. BLoopers don't have a next handler.

However, when a BView object is added to a view hierarchy (by AddChild()), the Interface Kit assigns the BView's parent as its next handler--unless the parent is the window's top view, in which case the BWindow object becomes its next handler. The handler chain for BViews is therefore BView to BView, up the view hierarchy, to the BWindow object.

SetNextHandler() can alter any of these default assignments. For it to work, the BHandler must belong to a BLooper object, its prospective next handler must belong to the same BLooper, and the BLooper must be locked.

See also: MessageReceived(), BLooper::AddHandler()






The Be Book, in lovely HTML, for the BeOS Preview Release.

Copyright © 1997 Be, Inc. All rights reserved.

Be is a registered trademark; BeOS, BeBox, BeWare, GeekPort, the Be logo, and the BeOS logo are trademarks of Be, Inc.

Last modified June 30, 1997.