dragimgg.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/generic/dragimgg.h
  3. // Purpose: wxDragImage class: a kind of a cursor, that can cope
  4. // with more sophisticated images
  5. // Author: Julian Smart
  6. // Modified by:
  7. // Created: 29/2/2000
  8. // Copyright: (c) Julian Smart
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_DRAGIMGG_H_
  12. #define _WX_DRAGIMGG_H_
  13. #include "wx/bitmap.h"
  14. #include "wx/icon.h"
  15. #include "wx/cursor.h"
  16. #include "wx/treectrl.h"
  17. #include "wx/listctrl.h"
  18. #include "wx/log.h"
  19. #include "wx/overlay.h"
  20. /*
  21. To use this class, create a wxDragImage when you start dragging, for example:
  22. void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
  23. {
  24. #ifdef __WXMSW__
  25. ::UpdateWindow((HWND) GetHWND()); // We need to implement this in wxWidgets
  26. #endif
  27. CaptureMouse();
  28. m_dragImage = new wxDragImage(* this, itemId);
  29. m_dragImage->BeginDrag(wxPoint(0, 0), this);
  30. m_dragImage->Move(pt, this);
  31. m_dragImage->Show(this);
  32. ...
  33. }
  34. In your OnMouseMove function, hide the image, do any display updating required,
  35. then move and show the image again:
  36. void MyTreeCtrl::OnMouseMove(wxMouseEvent& event)
  37. {
  38. if (m_dragMode == MY_TREE_DRAG_NONE)
  39. {
  40. event.Skip();
  41. return;
  42. }
  43. // Prevent screen corruption by hiding the image
  44. if (m_dragImage)
  45. m_dragImage->Hide(this);
  46. // Do some updating of the window, such as highlighting the drop target
  47. ...
  48. #ifdef __WXMSW__
  49. if (updateWindow)
  50. ::UpdateWindow((HWND) GetHWND());
  51. #endif
  52. // Move and show the image again
  53. m_dragImage->Move(event.GetPosition(), this);
  54. m_dragImage->Show(this);
  55. }
  56. Eventually we end the drag and delete the drag image.
  57. void MyTreeCtrl::OnLeftUp(wxMouseEvent& event)
  58. {
  59. ...
  60. // End the drag and delete the drag image
  61. if (m_dragImage)
  62. {
  63. m_dragImage->EndDrag(this);
  64. delete m_dragImage;
  65. m_dragImage = NULL;
  66. }
  67. ReleaseMouse();
  68. }
  69. */
  70. /*
  71. * wxGenericDragImage
  72. */
  73. class WXDLLIMPEXP_CORE wxGenericDragImage: public wxObject
  74. {
  75. public:
  76. // Ctors & dtor
  77. ////////////////////////////////////////////////////////////////////////////
  78. wxGenericDragImage(const wxCursor& cursor = wxNullCursor)
  79. {
  80. Init();
  81. Create(cursor);
  82. }
  83. wxGenericDragImage(const wxBitmap& image, const wxCursor& cursor = wxNullCursor)
  84. {
  85. Init();
  86. Create(image, cursor);
  87. }
  88. wxGenericDragImage(const wxIcon& image, const wxCursor& cursor = wxNullCursor)
  89. {
  90. Init();
  91. Create(image, cursor);
  92. }
  93. wxGenericDragImage(const wxString& str, const wxCursor& cursor = wxNullCursor)
  94. {
  95. Init();
  96. Create(str, cursor);
  97. }
  98. #if WXWIN_COMPATIBILITY_2_6
  99. // don't use in new code, use versions without hot spot parameter
  100. wxDEPRECATED( wxGenericDragImage(const wxCursor& cursor, const wxPoint& cursorHotspot) );
  101. wxDEPRECATED( wxGenericDragImage(const wxBitmap& image, const wxCursor& cursor, const wxPoint& cursorHotspot) );
  102. wxDEPRECATED( wxGenericDragImage(const wxIcon& image, const wxCursor& cursor, const wxPoint& cursorHotspot) );
  103. wxDEPRECATED( wxGenericDragImage(const wxString& str, const wxCursor& cursor, const wxPoint& cursorHotspot) );
  104. wxDEPRECATED( bool Create(const wxCursor& cursor, const wxPoint& cursorHotspot) );
  105. wxDEPRECATED( bool Create(const wxBitmap& image, const wxCursor& cursor, const wxPoint& cursorHotspot) );
  106. wxDEPRECATED( bool Create(const wxIcon& image, const wxCursor& cursor, const wxPoint& cursorHotspot) );
  107. wxDEPRECATED( bool Create(const wxString& str, const wxCursor& cursor, const wxPoint& cursorHotspot) );
  108. #endif // WXWIN_COMPATIBILITY_2_6
  109. #if wxUSE_TREECTRL
  110. wxGenericDragImage(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
  111. {
  112. Init();
  113. Create(treeCtrl, id);
  114. }
  115. #endif
  116. #if wxUSE_LISTCTRL
  117. wxGenericDragImage(const wxListCtrl& listCtrl, long id)
  118. {
  119. Init();
  120. Create(listCtrl, id);
  121. }
  122. #endif
  123. virtual ~wxGenericDragImage();
  124. // Attributes
  125. ////////////////////////////////////////////////////////////////////////////
  126. #ifdef wxHAS_NATIVE_OVERLAY
  127. // backing store is not used when native overlays are
  128. void SetBackingBitmap(wxBitmap* WXUNUSED(bitmap)) { }
  129. #else
  130. // For efficiency, tell wxGenericDragImage to use a bitmap that's already
  131. // created (e.g. from last drag)
  132. void SetBackingBitmap(wxBitmap* bitmap) { m_pBackingBitmap = bitmap; }
  133. #endif // wxHAS_NATIVE_OVERLAY/!wxHAS_NATIVE_OVERLAY
  134. // Operations
  135. ////////////////////////////////////////////////////////////////////////////
  136. // Create a drag image with a virtual image (need to override DoDrawImage, GetImageRect)
  137. bool Create(const wxCursor& cursor = wxNullCursor);
  138. // Create a drag image from a bitmap and optional cursor
  139. bool Create(const wxBitmap& image, const wxCursor& cursor = wxNullCursor);
  140. // Create a drag image from an icon and optional cursor
  141. bool Create(const wxIcon& image, const wxCursor& cursor = wxNullCursor);
  142. // Create a drag image from a string and optional cursor
  143. bool Create(const wxString& str, const wxCursor& cursor = wxNullCursor);
  144. #if wxUSE_TREECTRL
  145. // Create a drag image for the given tree control item
  146. bool Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id);
  147. #endif
  148. #if wxUSE_LISTCTRL
  149. // Create a drag image for the given list control item
  150. bool Create(const wxListCtrl& listCtrl, long id);
  151. #endif
  152. // Begin drag. hotspot is the location of the drag position relative to the upper-left
  153. // corner of the image.
  154. bool BeginDrag(const wxPoint& hotspot, wxWindow* window, bool fullScreen = false, wxRect* rect = NULL);
  155. // Begin drag. hotspot is the location of the drag position relative to the upper-left
  156. // corner of the image. This is full screen only. fullScreenRect gives the
  157. // position of the window on the screen, to restrict the drag to.
  158. bool BeginDrag(const wxPoint& hotspot, wxWindow* window, wxWindow* fullScreenRect);
  159. // End drag
  160. bool EndDrag();
  161. // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
  162. // is non-NULL, or in screen coordinates if NULL.
  163. bool Move(const wxPoint& pt);
  164. // Show the image
  165. bool Show();
  166. // Hide the image
  167. bool Hide();
  168. // Implementation
  169. ////////////////////////////////////////////////////////////////////////////
  170. void Init();
  171. // Override this if you are using a virtual image (drawing your own image)
  172. virtual wxRect GetImageRect(const wxPoint& pos) const;
  173. // Override this if you are using a virtual image (drawing your own image)
  174. virtual bool DoDrawImage(wxDC& dc, const wxPoint& pos) const;
  175. // Override this if you wish to draw the window contents to the backing bitmap
  176. // yourself. This can be desirable if you wish to avoid flicker by not having to
  177. // redraw the window itself before dragging in order to be graphic-minus-dragged-objects.
  178. // Instead, paint the drag image's backing bitmap to be correct, and leave the window
  179. // to be updated only when dragging the objects away (thus giving a smoother appearance).
  180. virtual bool UpdateBackingFromWindow(wxDC& windowDC, wxMemoryDC& destDC,
  181. const wxRect& sourceRect, const wxRect& destRect) const;
  182. // Erase and redraw simultaneously if possible
  183. virtual bool RedrawImage(const wxPoint& oldPos, const wxPoint& newPos, bool eraseOld, bool drawNew);
  184. protected:
  185. wxBitmap m_bitmap;
  186. wxIcon m_icon;
  187. wxCursor m_cursor;
  188. wxCursor m_oldCursor;
  189. // wxPoint m_hotspot;
  190. wxPoint m_offset; // The hostpot value passed to BeginDrag
  191. wxPoint m_position;
  192. bool m_isDirty;
  193. bool m_isShown;
  194. wxWindow* m_window;
  195. wxDC* m_windowDC;
  196. #ifdef wxHAS_NATIVE_OVERLAY
  197. wxOverlay m_overlay;
  198. wxDCOverlay* m_dcOverlay;
  199. #else
  200. // Stores the window contents while we're dragging the image around
  201. wxBitmap m_backingBitmap;
  202. wxBitmap* m_pBackingBitmap; // Pointer to existing backing bitmap
  203. // (pass to wxGenericDragImage as an efficiency measure)
  204. // A temporary bitmap for repairing/redrawing
  205. wxBitmap m_repairBitmap;
  206. #endif // !wxHAS_NATIVE_OVERLAY
  207. wxRect m_boundingRect;
  208. bool m_fullScreen;
  209. private:
  210. DECLARE_DYNAMIC_CLASS(wxGenericDragImage)
  211. wxDECLARE_NO_COPY_CLASS(wxGenericDragImage);
  212. };
  213. #endif
  214. // _WX_DRAGIMGG_H_