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
9 // Copyright: (c) Julian Smart
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_DRAGIMAG_H_
14 #define _WX_DRAGIMAG_H_
18 #include "wx/bitmap.h"
20 #include "wx/cursor.h"
21 #include "wx/treectrl.h"
22 #include "wx/listctrl.h"
24 // If 1, use a simple wxCursor instead of ImageList_SetDragCursorImage
25 #define wxUSE_SIMPLER_DRAGIMAGE 0
28 To use this class, create a wxDragImage when you start dragging, for example:
30 void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
33 ::UpdateWindow((HWND) GetHWND()); // We need to implement this in wxWidgets
38 m_dragImage = new wxDragImage(* this, itemId);
39 m_dragImage->BeginDrag(wxPoint(0, 0), this);
40 m_dragImage->Move(pt, this);
41 m_dragImage->Show(this);
45 In your OnMouseMove function, hide the image, do any display updating required,
46 then move and show the image again:
48 void MyTreeCtrl::OnMouseMove(wxMouseEvent& event)
50 if (m_dragMode == MY_TREE_DRAG_NONE)
56 // Prevent screen corruption by hiding the image
58 m_dragImage->Hide(this);
60 // Do some updating of the window, such as highlighting the drop target
65 ::UpdateWindow((HWND) GetHWND());
68 // Move and show the image again
69 m_dragImage->Move(event.GetPosition(), this);
70 m_dragImage->Show(this);
73 Eventually we end the drag and delete the drag image.
75 void MyTreeCtrl::OnLeftUp(wxMouseEvent& event)
79 // End the drag and delete the drag image
82 m_dragImage->EndDrag(this);
91 Notes for Unix version:
92 Can we simply use cursors instead, creating a cursor dynamically, setting it into the window
93 in BeginDrag, and restoring the old cursor in EndDrag?
94 For a really bog-standard implementation, we could simply use a normal dragging cursor
102 class WXDLLIMPEXP_CORE wxDragImage
: public wxObject
107 ////////////////////////////////////////////////////////////////////////////
110 wxDragImage(const wxBitmap
& image
, const wxCursor
& cursor
= wxNullCursor
)
114 Create(image
, cursor
);
117 // Deprecated form of the above
118 wxDragImage(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
)
122 Create(image
, cursor
, cursorHotspot
);
125 wxDragImage(const wxIcon
& image
, const wxCursor
& cursor
= wxNullCursor
)
129 Create(image
, cursor
);
132 // Deprecated form of the above
133 wxDragImage(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
)
137 Create(image
, cursor
, cursorHotspot
);
140 wxDragImage(const wxString
& str
, const wxCursor
& cursor
= wxNullCursor
)
147 // Deprecated form of the above
148 wxDragImage(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
)
152 Create(str
, cursor
, cursorHotspot
);
156 wxDragImage(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
160 Create(treeCtrl
, id
);
165 wxDragImage(const wxListCtrl
& listCtrl
, long id
)
169 Create(listCtrl
, id
);
173 virtual ~wxDragImage();
176 ////////////////////////////////////////////////////////////////////////////
179 ////////////////////////////////////////////////////////////////////////////
181 // Create a drag image from a bitmap and optional cursor
182 bool Create(const wxBitmap
& image
, const wxCursor
& cursor
= wxNullCursor
);
183 bool Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
185 wxLogDebug(wxT("wxDragImage::Create: use of a cursor hotspot is now deprecated. Please omit this argument."));
186 return Create(image
, cursor
);
189 // Create a drag image from an icon and optional cursor
190 bool Create(const wxIcon
& image
, const wxCursor
& cursor
= wxNullCursor
);
191 bool Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
193 wxLogDebug(wxT("wxDragImage::Create: use of a cursor hotspot is now deprecated. Please omit this argument."));
194 return Create(image
, cursor
);
197 // Create a drag image from a string and optional cursor
198 bool Create(const wxString
& str
, const wxCursor
& cursor
= wxNullCursor
);
199 bool Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
201 wxLogDebug(wxT("wxDragImage::Create: use of a cursor hotspot is now deprecated. Please omit this argument."));
202 return Create(str
, cursor
);
206 // Create a drag image for the given tree control item
207 bool Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
);
211 // Create a drag image for the given list control item
212 bool Create(const wxListCtrl
& listCtrl
, long id
);
215 // Begin drag. hotspot is the location of the drag position relative to the upper-left
216 // corner of the image.
217 bool BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
= false, wxRect
* rect
= NULL
);
219 // Begin drag. hotspot is the location of the drag position relative to the upper-left
220 // corner of the image. This is full screen only. fullScreenRect gives the
221 // position of the window on the screen, to restrict the drag to.
222 bool BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
);
227 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
228 // is non-NULL, or in screen coordinates if NULL.
229 bool Move(const wxPoint
& pt
);
238 ////////////////////////////////////////////////////////////////////////////
240 // Initialize variables
243 // Returns the native image list handle
244 WXHIMAGELIST
GetHIMAGELIST() const { return m_hImageList
; }
246 #if !wxUSE_SIMPLER_DRAGIMAGE
247 // Returns the native image list handle for the cursor
248 WXHIMAGELIST
GetCursorHIMAGELIST() const { return m_hCursorImageList
; }
252 WXHIMAGELIST m_hImageList
;
254 #if wxUSE_SIMPLER_DRAGIMAGE
255 wxCursor m_oldCursor
;
257 WXHIMAGELIST m_hCursorImageList
;
261 // wxPoint m_cursorHotspot; // Obsolete
264 wxRect m_boundingRect
;
268 DECLARE_DYNAMIC_CLASS(wxDragImage
)
269 wxDECLARE_NO_COPY_CLASS(wxDragImage
);
272 #endif // wxUSE_DRAGIMAGE