Introduction
- Drag and drop support within wxPython is a conversation between a "drop source" and a "drop target". The conversation is accomplished by sharing a "data object" which includes certain pieces of metadata (primarily the data types available from the drop source) as well as the actual shared data.
What Objects are Involved
- Source window -- Interprets an event as the beginning of a new drag-and-drop request. Instantiates a new drop source
and triggers the drag-and-drop sequence by calling DoDragDrop on the drop source object. Responds to the result code of the drag-and-drop sequence (move/copy/cancel/fail).
wxDropSource -- Provides common functionality for drag-and-drop source windows (UI interactions, data object storage)
- Destination window -- Advertises acceptable data types by
calling SetDropTarget with a configured drop target object.
wxDropTarget -- Responds to various "pseudo-events" during the drag-and-drop process, most importantly the OnData "event" which indicates that an object has been dropped on the associated window.
wxDataObject -- Provides storage and metadata relating to the data being transferred during a drag-and-drop operation. May support one of the built-in data types (text, bitmap, file), a single declared data type, or multiple data types. Includes: wxTextDataObject, wxBitmapDataObject, wxFileDataObject, wxDataObjectSimple, wxDataObjectComposite, wxDataObject
Process Overview
Create or choose a wxDataObject subclass appropriate to the data being shared
Provide OnData method to handle incoming data and update your application with the results of the drag-and-drop operation.
- If you are not using a built-in data type, choose a type specifier (a unique string used to identify the datatype). Only targets whose type specifiers include one of the current data source specifiers will be eligible for drops.
Create a wxPyDropTarget subclass (and instance)
Provide an OnData method to handle a drop "event"
Call self.GetData() to transfer data to the target's wxDataObject instance
Call wxDataObject.GetData() to retrieve the actual shared data
- Update your application to reflect drag-and-drop operation (in many cases you will need a pointer to the target window within the drop target instance to accomplish this).
Instantiate an instance of your wxDataObject class
Bind a wxDataObject to the drop target by calling SetDataObject. You will also want to keep a pointer to the wxDataObject in the drop target instance, so that you can call wxDataObject.GetData() within the OnData method.
Bind the drop target to the target window using wxWindow.SetDropTarget
- Handle an event on the source window which signals the start of the drag-and-drop cycle
Create a wxDropSource
Set the data object of the drop source to another instance of your wxDataObject class
Call the drop source DoDragDrop() method to begin the drag-and-drop operation
Respond to the result code of the DoDragDrop method as appropriate to your application
wxDataObjects
- Discuss the various concerns relating to the data object. Python pickling options Multiple formats -- currently only works for source in Python (as far as I can see) Format specifiers, do these need to be discussed? Special Data Types
Code Samples
- Simple drag-and-drop
- File/bitmap/text
Comments...
I will add a example for dragging items in a wxTreeCtrl and what is more interresting how to drag a branch in a wxTreeCtrl. if added it will be found under "ListAndTreeControls".
--René Freund