previous chapter contents page top page next chapter

Swallower

mixes in with Viewable
encodes empty field list;

Class Description

Use mixin Swallower in your viewable classes that can accept morsels. Swallowers implement a Swallow routine that you can override to define what your subclass can eat.

Programming information

Instantiate: never
Subclass: always
Call its methods: rarely

You're likely to mix class Swallower in with your viewable classes whenever those classes need to accept morsels dropped into them by the user. You will override the Swallow method often.

Methods defined by class Swallower

Class Swallower defines the following methods:

Method Description
Swallower Check if the viewable can accept the given morsel

Fields defined by class Swallower

Class Swallower defines no fields.

Method Descriptions

Swallow
overrides Swallow

Call: rarely
Override: often

The system calls Swallow when the user holds over or attempts to drop a morsel onto a viewable. The system calls Swallow when the user drags an object (the "morsel") over a viewable to determine if the viewable can accept the dragged morsel and perform some action with itthat is, not simply add it as a subview. (You might find it helpful to think of it as digestion.) The morsel parameter is the object that the user is dragging. The where parameter gives the location of the hovering object.

If realSwallow is false, the caller is only testing to see if the object can be swallowed; Swallow doesn't actually swallow the object. If realSwallow is true, the object is swallowed if appropriate. Swallow returns a Boolean value indicating if the object can be swallowed.

See Viewable_Swallow for more information on what class Viewable expects from this method and related methods.

Swallower_Swallow enforces the following rules about swallowing:

Override Swallow to add new morsel types to those already accepted by your viewable, or to reject other types. Call the inherited method if your class should behave like normal swallowers for all morsels except those of special interest to you. Your override of Swallow might look something like this:

Method Boolean
PurplePeopleEater_Swallow(ObjectID self, ObjectID morsel, const Dot *where,
                          Boolean realSwallow)
{
   /* decide if the morsel cannot be swallowed, and return false */
   [do work here, possibly calling the inherited method]

   /* We know we can eat this morsel. Should we really swallow, or just give
      feedback?  */
   if (!realSwallow) return true;

   /* We're really swallowing. Now do whatever processing we need to do
      to digest this morsel, which might be moving the morsel to a new
      superview, copying data from the morsel, or destroying the morsel. */
   [do work here]

   /* Remember to return true if we swallowed. */
   return true;
}