wxdataobject overviewclasses: wxdataobject, wxclipboard, wxdataformat, wxdropsource, wxdroptarget see also: drag and drop overview and dnd sample this overview discusses data transfer through clipboard or drag and drop. in wxwidgets, these two ways to transfer data (either between different applications or inside one and the same) are very similar which allows to implement both of them using almost the same code - or, in other words, if you implement drag and drop support for your application, you get clipboard support for free and vice versa. at the heart of both clipboard and drag and drop operations lies the wxdataobject class. the objects of this class (or, to be precise, classes derived from it) represent the data which is being carried by the mouse during drag and drop operation or copied to or pasted from the clipboard. wxdataobject is a "smart" piece of data because it knows which formats it supports (see getformatcount and getallformats) and knows how to render itself in any of them (see getdatahere). it can also receive its value from the outside in a format it supports if it implements the setdata method. please see the documentation of this class for more details. both clipboard and drag and drop operations have two sides: the source and target, the data provider and the data receiver. these which may be in the same application and even the same window when, for example, you drag some text from one position to another in a word processor. let us describe what each of them should do.
the data provider (source) duties
the data provider (source) dutiesthe data provider is responsible for creating a wxdataobject containing the data to be transferred. then it should either pass it to the clipboard using setdata function or to wxdropsource and call dodragdrop function. the only (but important) difference is that the object for the clipboard transfer must always be created on the heap (i.e. using new) and it will be freed by the clipboard when it is no longer needed (indeed, it is not known in advance when, if ever, the data will be pasted from the clipboard). on the other hand, the object for drag and drop operation must only exist while dodragdrop executes and may be safely deleted afterwards and so can be created either on heap or on stack (i.e. as a local variable). another small difference is that in the case of clipboard operation, the application usually knows in advance whether it copies or cuts (i.e. copies and deletes) data - in fact, this usually depends on which menu item the user chose. but for drag and drop it can only know it after dodragdrop returns (from its return value).
the data receiver (target) dutiesto receive (paste in usual terminology) data from the clipboard, you should create a wxdataobject derived class which supports the data formats you need and pass it as argument to wxclipboard::getdata. if it returns false, no data in (any of) the supported format(s) is available. if it returns true, the data has been successfully transferred to wxdataobject. for drag and drop case, the wxdroptarget::ondata virtual function will be called when a data object is dropped, from which the data itself may be requested by calling wxdroptarget::getdata method which fills the data object.
|