d_and_d.txt 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. Drag-and-Drop Support in wxWindows
  2. ==================================
  3. 1. Overview
  4. --------
  5. a) What is it?
  6. We're calling drag-and-drop (or d&d for short) the OLE mechanism of data
  7. transfer. Please note that it's not the same thing as the file oriented d&d
  8. of Windows 3.1 "File Manager" which is designed for and limited to the file
  9. names only.
  10. OLE d&d allows application to transfer data of any type to the same or
  11. another process.
  12. b) How is it done? (user's point of view)
  13. To start a d&d operation the user presses the mouse button 1 (left) and
  14. drags the selected object to another window (which must be at least partially
  15. visible on the screen) or to an icon on the taskbar in which case the
  16. corresponding window will be automatically restored. To finish the operation,
  17. the user releases the button. Default d&d operation is "move", but several key
  18. act as modifiers: keeping down the <Ctrl> key at the moment of drop does
  19. "copy", while <Shift> or <Alt> force the "move" (makes sense if default isn't
  20. "move").
  21. c) How is it done? (programmer's point of view)
  22. There are several objects participating in a d&d operation. First of all,
  23. there is the data object itself. Second, there is the drop source which is
  24. responsible for creating the data object (if it doesn't exist yet) and starting
  25. the d&d operation. Finally, the drop target receives the notification when
  26. the data is dropped onto the associated window (see below) and is responsible
  27. for pasting the data and returning the result code (copy, move or failure).
  28. There is one class for each one of these roles in wxWindows d&d implementation,
  29. plese see their descriptions below for details.
  30. 2. Drop Target
  31. -----------
  32. a) Being a drop target
  33. ... is as easy as deriving your window class from wxDropTarget and
  34. associating it with a wxWindow object (or perhaps some wxWindow-derived class,
  35. such as wxFrame). The pure virtual function wxDropTarget::OnDrop() must be
  36. implemented in your application and will be called whenever the mouse button
  37. is released over the window in question. Other virtual functions that will be
  38. called in the process of the d&d operation are OnEnter and OnLeave.
  39. @@ should OnDragOver() be user overridable also?
  40. You should associate wxDropTarget and wxWindow calling SetDropTarget:
  41. wxWindow *pWindow = GetTopWindow();
  42. pWindow->SetDropTarget(new MyDropTarget);
  43. The object created passed to SetDropTarget becomes the propriety of wxWindow
  44. and will be deleted with the window (or when you call SetDropTarget next
  45. time). You can always break the association by calling SetDropTarget(NULL).
  46. When some data is dragged over a window, the program must decide if it's
  47. going to accept this data or not. The virtual function IsAcceptedData() is
  48. called to do it. The default implementation takes care of OLE interface
  49. pointer manipulations and only requires you to override GetCountFormats()
  50. and GetFormat(n) functions to let it know what data formats you support.
  51. If it's not flexible enough for your application (i.e. the set of supported
  52. formats changes over time...), you should override IsAcceptedData(). In 99%
  53. of cases the default implementation is ok and you only have to return count
  54. of supported formats (CF_xxx constants or one of your custom formats which
  55. must have been registered) and their values.
  56. b) OnDrop(long x, long y, const void *pData)
  57. (x, y) are drop point (client) coordinates, pData is the pointer to data
  58. (whatever it is).
  59. If 'true' is returned from OnDrop, the operation is considered to be
  60. successful and the corresponding code (MOVE or COPY depending on the
  61. keyboard control keys) is returned. Otherwise, the operation is cancelled.
  62. Please remember that returning 'true' here may mean 'move' and so the
  63. drop source will delete the corresponding data - which would lead to
  64. data loss if you didn't paste it properly.
  65. c) OnEnter()
  66. called when the mouse enters the window: you might use this function to
  67. give some additional visual feedback.
  68. d) OnLeave()
  69. called when the mouse leaves the window; might be a good place to clean
  70. up things allocated in OnEnter.
  71. e) Simple wxDropTarget specializations
  72. Two (very simple) wxDropTarget-derived classes are provided for two most
  73. common situations: d&d of text and file d&d. To use them you only need to
  74. override one virtual function OnDropText in wxTextDropTarget's case and
  75. OnDropFiles for wxFileDropTarget.
  76. The (x, y) are the same as for OnDrop() function. OnDropText's last
  77. parameter points to a (always ANSI, not Unicode) text string, while
  78. OnDropFiles() parameter is the array of file names just dropped (and the
  79. count of them is passed in the 3rd parameter).
  80. 3. Data Object
  81. -----------
  82. a) Drag and drop and clipboard
  83. The effect of a d&d operation is the same as using the clipboard to
  84. cut/copy and paste data and it would be nice to use the same code to implement
  85. these two data transfer mechanisms. The wxDataObject allows you to do exactly
  86. this. It encapsulates the data which can be passed either through the clipboard
  87. or d&d.
  88. b) Data format
  89. There are several standard clipboard formats, such as text, bitmap or
  90. metafile picture. All of them are defined in wxDataObject::StdFormats
  91. enumeration. Of course, it's not always enough and you'll often need your
  92. own format for data transfer. The simple helper class wxDataFormat may help
  93. you: when you create an object of this class, it registers a new clipboard
  94. data format identified by the string passed to its ctor.
  95. After your new format is registered, you may use it as any other one.
  96. 4. Drop Source
  97. -----------
  98. a) Starting the d&d operation
  99. In order to start the d&d operation you should call the DoDragDrop function
  100. (typically in reply to a "mouse button press" message). NB: DoDragDrop() is a
  101. blocking function which enters into its own message loop and may return after
  102. an arbitrarily long time interval. During it, the QueryContinueDrag() is called
  103. whenever the mouse or keyboard state changes. The default behaviour is quite
  104. reasonable for 99% of cases: the drag operation is cancelled if the <Esc> key
  105. is preessed and the drop is initiated if the mouse button is released.
  106. b) After the end of d&d
  107. The drop source behaviour depends on DoDragDrop() return code. If it
  108. returns wxDropSource::None or wxDropSource::Copy there is normally nothing to
  109. do, but you shouldn't forget to delete your data if it returns the
  110. wxDropSource::Move code.
  111. c) DoDragDrop
  112. d) QueryContinueDrag
  113. 5. Remarks
  114. -------
  115. @@@@ TODO: support tymed != TYMED_HGLOBAL;
  116. better support of CF_BMP, CF_METAFILE
  117. scrolling support!! (how?)
  118. sample demonstrating use of user-defined formats
  119. sample which really does something useful