Important: The information in this document is obsolete and should not be used for new development.
Performing a Drag Operation
This section describes how the objects in the MacApp application DemoDialogs work together with the Macintosh Drag Manager to perform a drag operation. The discussion refers to the steps of a drag operation--those steps are shown in Figure 9-2 and Figure 9-3.Setting the Drag Cursor
When a user moves the cursor over a view, MacApp calls the view'sDoSetDragCursor
method. TheDoSetDragCursor
method calls two other view methods to determine whether a mouse click would initiate a drag:DoMakeDragCursorRegion
andWillDrag
.The
TView::DoMakeDragCursorRegion
method creates a region to describe the specific portion of the view's content that is draggable. Draggable content is often the area within the view that the user perceives as selected, such as highlighted cells in a grid view or selected text in a text view. In theTView
class,DoMakeDragCursorRegion
callsDoMakeDropHiliteRegion
, which creates a region that matches the view's extent. Your application can overrideDoMakeDragCursorRegion
orDoMakeDropHiliteRegion
to supply a specialized drag cursor region. See MacApp'sTEditText
class for an example of overridingDoMakeDropHiliteRegion
.The
TView::WillDrag
method checks whether thefDraggable
field is set toTRUE
and the cursor is currently over the draggable region. TheWillDrag
method of subclasses ofTView
may check for additional conditions. For example,TTEView::WillDrag
returnsTRUE
only ifTView::WillDrag
returnsTRUE
and the Shift key is not pressed. Dragging while the Shift key is pressed is used to select additional text rather than to initiate a drag operation.If
WillDrag
returnsTRUE
, theDoSetDragCursor
method calls theGetWillDragCursorID
method. InTView
,GetWillDragCursorID
returns the resource ID for an open-hand image--an image appropriate for dragging data such as a picture. In both theTTEView
andTEditText
view classes,GetWillDragCursorID
returns thekNoResource
ID constant. This results in the display of an arrow cursor--an image appropriate for dragging text.You can override
GetWillDragCursorID
to supply a special cursor ID resource for your view. The cursor you display should alert the user to the possibility of initiating a drag operation. Step 2 in Figure 9-2 shows an arrow cursor image over draggable text.Performing a Drag
When a user clicks the mouse with the cursor image over a view, MacApp calls the view'sHandleMouseDown
method. To determine whether the click can initiate a drag, theHandleMouseDown
method calls the same view methodsDoSetDragCursor
did:DoMakeDragCursorRegion
andWillDrag
.Figure 9-2 Dragging and dropping text in the DemoDialogs application (steps 1-3)
Figure 9-3 Dragging and dropping text in the DemoDialogs application (steps 4-6)
If
WillDrag
returnsTRUE
,HandleMouseDown
calls theGetIsDraggingCursorID
method. InTView
,GetIsDraggingCursorID
returns the resource ID for a closed-hand image--an image appropriate for dragging data such as a picture. In both theTTEView
andTEditText
view classes,GetIsDraggingCursorID
returns thekNoResource
ID constant. This results in the display of an arrow cursor--an image appropriate for dragging text.You can override
GetIsDraggingCursorID
to supply a special cursor ID resource for your view. Step 3 in Figure 9-2 shows an arrow cursor image as a user starts an operation to drag text.After setting the cursor based on the supplied cursor ID,
HandleMouseDown
calls the view'sHandleDrag
method to prepare data and initiate a drag operation. TheTView::HandleDrag
method is not intended to be overridden and is consequently not declared as virtual.
The
- Note
- To provide flexibility,
HandleMouseDown
calls theDoGetDragProxy
method, then calls theHandleDrag
method of the returned object. For theTView
class,DoGetDragProxy
returns a reference to the view itself, but you can overrideDoGetDragProxy
and provide a different object to handle the drag operation.- For example, the
TDialogTEView
class overridesDoGetDragProxy
and returns a reference to its associatedTEditText
object, which then handles the drag.TView::HandleDrag
method first calls the global drag session object'sClearDrag
method to dispose of any previous drag operation. It then calls two view methods,DoAddDragContent
andDoMakeDragOutlineRegion
. These methods are described in the following sections.Supplying Drag Data
TheTView::DoAddDragContent
method creates drag items to represent the data being dragged and installs the drag items into the drag session. TheDoAddDragContent
method calls the drag session object'sAddDragItem
method once for each item it adds to the drag.AddDragItem
returns a pointer to a newTDragItem
object.DoAddDragContent
then callsTDragItem->AddFlavor
once for each flavor it can supply for that data type. For example, a view might callAddFlavor
for PICT, GIF, JPEG, QuickTime, and other flavors. Instead ofAddFlavor
, it may callTDragItem->PromiseFlavor
to promise data that will only be delivered if requested. For information on the advantages of promising data, see "Promising Data" on page 267.Supplying a Drag Outline Region
TheTView::DoMakeDragOutlineRegion
method creates and returns an outline region that represents the item or items to be dragged. TheTView
class implementation ofDoMakeDragOutlineRegion
calls the view'sDoMakeDragCursorRegion
method, which calls theDoMakeDropHiliteRegion
method. InTView
,DoMakeDropHiliteRegion
provides the view's visible extent as a default region. If this doesn't work for your view, you can override any of these methods (DoMakeDragOutlineRegion
,DoMakeDragCursorRegion
, orDoMakeDropHiliteRegion
).Step 4 in Figure 9-3 shows the outline region for a dragged text selection.
Starting the Drag Session
Once the view'sHandleDrag
method has supplied data and an outline for the drag session, it calls the global drag session object'sStartDrag
method to initiate the drag session. TheStartDrag
method calls the Drag Manager routineTrackDrag
to initiate the drag session and calls its ownClearDrag
method to dispose of the session once it is completed.