dataobject.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: dataobject.h
  3. // Purpose: topic overview
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @page overview_dataobject wxDataObject Overview
  9. @tableofcontents
  10. This overview discusses data transfer through clipboard or drag and drop. In
  11. wxWidgets, these two ways to transfer data (either between different
  12. applications or inside one and the same) are very similar which allows to
  13. implement both of them using almost the same code - or, in other words, if you
  14. implement drag and drop support for your application, you get clipboard support
  15. for free and vice versa.
  16. At the heart of both clipboard and drag and drop operations lies the
  17. wxDataObject class. The objects of this class (or, to be precise, classes
  18. derived from it) represent the data which is being carried by the mouse during
  19. drag and drop operation or copied to or pasted from the clipboard. wxDataObject
  20. is a "smart" piece of data because it knows which formats it supports (see
  21. GetFormatCount and GetAllFormats) and knows how to render itself in any of them
  22. (see GetDataHere). It can also receive its value from the outside in a format
  23. it supports if it implements the SetData method. Please see the documentation
  24. of this class for more details.
  25. Both clipboard and drag and drop operations have two sides: the source and
  26. target, the data provider and the data receiver. These which may be in the same
  27. application and even the same window when, for example, you drag some text from
  28. one position to another in a word processor. Let us describe what each of them
  29. should do.
  30. @see @ref overview_dnd, @ref group_class_dnd, @ref page_samples_dnd
  31. @section overview_dataobject_source The Data Provider (Source)
  32. The data provider is responsible for creating a wxDataObject containing the
  33. data to be transferred. Then it should either pass it to the clipboard using
  34. wxClipboard::SetData function or to wxDropSource and call
  35. wxDropSource::DoDragDrop function.
  36. The only (but important) difference is that the object for the clipboard
  37. transfer must always be created on the heap (i.e. using @c new) and it will be
  38. freed by the clipboard when it is no longer needed (indeed, it is not known in
  39. advance when, if ever, the data will be pasted from the clipboard). On the
  40. other hand, the object for drag and drop operation must only exist while
  41. wxDropSource::DoDragDrop executes and may be safely deleted afterwards and so
  42. can be created either on heap or on stack (i.e. as a local variable).
  43. Another small difference is that in the case of clipboard operation, the
  44. application usually knows in advance whether it copies or cuts (i.e. copies and
  45. deletes) data - in fact, this usually depends on which menu item the user
  46. chose. But for drag and drop it can only know it after wxDropSource::DoDragDrop
  47. returns (from its return value).
  48. @section overview_dataobject_target The Data Receiver (Target)
  49. To receive (paste in usual terminology) data from the clipboard, you should
  50. create a wxDataObject derived class which supports the data formats you need
  51. and pass it as argument to wxClipboard::GetData. If it returns @false,
  52. no data in (any of) the supported format(s) is available. If it returns @true,
  53. the data has been successfully transferred to wxDataObject.
  54. For drag and drop case, the wxDropTarget::OnData virtual function will be
  55. called when a data object is dropped, from which the data itself may be
  56. requested by calling wxDropTarget::GetData method which fills the data object.
  57. */