dragimag.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/msw/dragimag.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: 08/04/99
  8. // Copyright: (c) Julian Smart
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_DRAGIMAG_H_
  12. #define _WX_DRAGIMAG_H_
  13. #if wxUSE_DRAGIMAGE
  14. #include "wx/bitmap.h"
  15. #include "wx/icon.h"
  16. #include "wx/cursor.h"
  17. #include "wx/treectrl.h"
  18. #include "wx/listctrl.h"
  19. // If 1, use a simple wxCursor instead of ImageList_SetDragCursorImage
  20. #define wxUSE_SIMPLER_DRAGIMAGE 0
  21. /*
  22. To use this class, create a wxDragImage when you start dragging, for example:
  23. void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
  24. {
  25. #ifdef __WXMSW__
  26. ::UpdateWindow((HWND) GetHWND()); // We need to implement this in wxWidgets
  27. #endif
  28. CaptureMouse();
  29. m_dragImage = new wxDragImage(* this, itemId);
  30. m_dragImage->BeginDrag(wxPoint(0, 0), this);
  31. m_dragImage->Move(pt, this);
  32. m_dragImage->Show(this);
  33. ...
  34. }
  35. In your OnMouseMove function, hide the image, do any display updating required,
  36. then move and show the image again:
  37. void MyTreeCtrl::OnMouseMove(wxMouseEvent& event)
  38. {
  39. if (m_dragMode == MY_TREE_DRAG_NONE)
  40. {
  41. event.Skip();
  42. return;
  43. }
  44. // Prevent screen corruption by hiding the image
  45. if (m_dragImage)
  46. m_dragImage->Hide(this);
  47. // Do some updating of the window, such as highlighting the drop target
  48. ...
  49. #ifdef __WXMSW__
  50. if (updateWindow)
  51. ::UpdateWindow((HWND) GetHWND());
  52. #endif
  53. // Move and show the image again
  54. m_dragImage->Move(event.GetPosition(), this);
  55. m_dragImage->Show(this);
  56. }
  57. Eventually we end the drag and delete the drag image.
  58. void MyTreeCtrl::OnLeftUp(wxMouseEvent& event)
  59. {
  60. ...
  61. // End the drag and delete the drag image
  62. if (m_dragImage)
  63. {
  64. m_dragImage->EndDrag(this);
  65. delete m_dragImage;
  66. m_dragImage = NULL;
  67. }
  68. ReleaseMouse();
  69. }
  70. */
  71. /*
  72. Notes for Unix version:
  73. Can we simply use cursors instead, creating a cursor dynamically, setting it into the window
  74. in BeginDrag, and restoring the old cursor in EndDrag?
  75. For a really bog-standard implementation, we could simply use a normal dragging cursor
  76. and ignore the image.
  77. */
  78. /*
  79. * wxDragImage
  80. */
  81. class WXDLLIMPEXP_CORE wxDragImage: public wxObject
  82. {
  83. public:
  84. // Ctors & dtor
  85. ////////////////////////////////////////////////////////////////////////////
  86. wxDragImage();
  87. wxDragImage(const wxBitmap& image, const wxCursor& cursor = wxNullCursor)
  88. {
  89. Init();
  90. Create(image, cursor);
  91. }
  92. wxDragImage(const wxIcon& image, const wxCursor& cursor = wxNullCursor)
  93. {
  94. Init();
  95. Create(image, cursor);
  96. }
  97. wxDragImage(const wxString& str, const wxCursor& cursor = wxNullCursor)
  98. {
  99. Init();
  100. Create(str, cursor);
  101. }
  102. #if wxUSE_TREECTRL
  103. wxDragImage(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
  104. {
  105. Init();
  106. Create(treeCtrl, id);
  107. }
  108. #endif
  109. #if wxUSE_LISTCTRL
  110. wxDragImage(const wxListCtrl& listCtrl, long id)
  111. {
  112. Init();
  113. Create(listCtrl, id);
  114. }
  115. #endif
  116. virtual ~wxDragImage();
  117. // Attributes
  118. ////////////////////////////////////////////////////////////////////////////
  119. // Operations
  120. ////////////////////////////////////////////////////////////////////////////
  121. // Create a drag image from a bitmap and optional cursor
  122. bool Create(const wxBitmap& image, const wxCursor& cursor = wxNullCursor);
  123. // Create a drag image from an icon and optional cursor
  124. bool Create(const wxIcon& image, const wxCursor& cursor = wxNullCursor);
  125. // Create a drag image from a string and optional cursor
  126. bool Create(const wxString& str, const wxCursor& cursor = wxNullCursor);
  127. #if wxUSE_TREECTRL
  128. // Create a drag image for the given tree control item
  129. bool Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id);
  130. #endif
  131. #if wxUSE_LISTCTRL
  132. // Create a drag image for the given list control item
  133. bool Create(const wxListCtrl& listCtrl, long id);
  134. #endif
  135. // Begin drag. hotspot is the location of the drag position relative to the upper-left
  136. // corner of the image.
  137. bool BeginDrag(const wxPoint& hotspot, wxWindow* window, bool fullScreen = false, wxRect* rect = NULL);
  138. // Begin drag. hotspot is the location of the drag position relative to the upper-left
  139. // corner of the image. This is full screen only. fullScreenRect gives the
  140. // position of the window on the screen, to restrict the drag to.
  141. bool BeginDrag(const wxPoint& hotspot, wxWindow* window, wxWindow* fullScreenRect);
  142. // End drag
  143. bool EndDrag();
  144. // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
  145. // is non-NULL, or in screen coordinates if NULL.
  146. bool Move(const wxPoint& pt);
  147. // Show the image
  148. bool Show();
  149. // Hide the image
  150. bool Hide();
  151. // Implementation
  152. ////////////////////////////////////////////////////////////////////////////
  153. // Initialize variables
  154. void Init();
  155. // Returns the native image list handle
  156. WXHIMAGELIST GetHIMAGELIST() const { return m_hImageList; }
  157. #if !wxUSE_SIMPLER_DRAGIMAGE
  158. // Returns the native image list handle for the cursor
  159. WXHIMAGELIST GetCursorHIMAGELIST() const { return m_hCursorImageList; }
  160. #endif
  161. // don't use in new code, use versions without hot spot parameter
  162. #if WXWIN_COMPATIBILITY_2_8
  163. wxDEPRECATED_CONSTRUCTOR( wxDragImage(const wxBitmap& image, const wxCursor& cursor, const wxPoint& cursorHotspot) );
  164. wxDEPRECATED_CONSTRUCTOR( wxDragImage(const wxString& str, const wxCursor& cursor, const wxPoint& cursorHotspot) );
  165. wxDEPRECATED_CONSTRUCTOR( wxDragImage(const wxIcon& image, const wxCursor& cursor, const wxPoint& cursorHotspot) );
  166. wxDEPRECATED( bool Create(const wxBitmap& image, const wxCursor& cursor, const wxPoint& cursorHotspot) );
  167. wxDEPRECATED( bool Create(const wxIcon& image, const wxCursor& cursor, const wxPoint& cursorHotspot) );
  168. wxDEPRECATED( bool Create(const wxString& str, const wxCursor& cursor, const wxPoint& cursorHotspot) );
  169. #endif // WXWIN_COMPATIBILITY_2_8
  170. protected:
  171. WXHIMAGELIST m_hImageList;
  172. #if wxUSE_SIMPLER_DRAGIMAGE
  173. wxCursor m_oldCursor;
  174. #else
  175. WXHIMAGELIST m_hCursorImageList;
  176. #endif
  177. wxCursor m_cursor;
  178. // wxPoint m_cursorHotspot; // Obsolete
  179. wxPoint m_position;
  180. wxWindow* m_window;
  181. wxRect m_boundingRect;
  182. bool m_fullScreen;
  183. private:
  184. DECLARE_DYNAMIC_CLASS(wxDragImage)
  185. wxDECLARE_NO_COPY_CLASS(wxDragImage);
  186. };
  187. #endif // wxUSE_DRAGIMAGE
  188. #endif
  189. // _WX_DRAGIMAG_H_