BasicDragAndDrop

BasicDragAndDrop is a sample that illustrates the use of drag and drop between two separate applications. It shows how to derive classes from TScrapItem and TDragAndDropItem, and how to use them and TMouseDragAndDropInteractor to communicate with classes that mix in MDropAcceptor.

See the Input samples and documentation for more information on using interactors and on handling mouse events.

The supported enablers directory contains a templatized version of TScrapItem, TScrapItemOn<>, which facilitates the creation of new scrap items; and a derived class of TDragAndDropItem, TSimpleDragAndDropItem, which illustrates how to support multiple scrap item types with one drag-and-drop item.

Running the sample

Execute the binaries BasicDragAndDropSourceSApp and BasicDragAndDropAcceptorSApp. Click the mouse in one of the source views, drag the mouse to the acceptor view, and release. While you drag, a dot displaying the source color tracks the mouse. When you release the mouse over the acceptor view, that view responds by changing to the dragged color.

Files and classes

TColorScrapItem is defined in ColorScrapItem.h and ColorScrapItem.C. It inherits from TScrapItem, and overrides InternalCopyAvailableTypes to provide a type description for its type. It also defines GetColor (so that acceptors of the scrap item can extract the color) and kScrapItemType (to provide a means for acceptors to indicate that they accept this type).

TSingleDragAndDropItem is defined in SingleDragAndDropItem.h and SingleDragAndDropItem.C. It inherits from TDragAndDropItem and overrides CopyAvailableTypes and FindItemMatchingDescription. It holds a single scrap item and implements these member functions to access and query the type information of that scrap item.

TColorAcceptorView is defined in ColorAcceptorView.h and ColorAcceptorView.C It inherits from MDropAcceptor and overrides ChoosePreferredType and AcceptDrop to indicate which scrap item types it accepts and to take specific action when a drop occurs. It only accepts the type exported by TColorScrapItem. When it accepts a drop of this type, it sets the color to the color contained by that item.

TColorSourceView is defined in ColorSourceView.h and ColorSourceView.C. It inherits from MMouseEventHandler and overrides MouseButtonDown to start the drag interaction. (For more information on writing views that handle mouse input, see the Input samples and documentation). TColorSourceView instantiates a TColorScrapItem with its color, and instantiates a TSingleDragAndDropItem, handing it the item it created. This item and a graphic to be displayed while dragging are handed to an instance of TMouseDragAndDropInteractor, which is then passed to the input system via TEvent::StartInteractor.


Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.