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_
16 #include "wx/bitmap.h"
18 #include "wx/cursor.h"
19 #include "wx/treectrl.h"
20 #include "wx/listctrl.h"
22 // If 1, use a simple wxCursor instead of ImageList_SetDragCursorImage
23 #define wxUSE_SIMPLER_DRAGIMAGE 0
26 To use this class, create a wxDragImage when you start dragging, for example:
28 void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
31 ::UpdateWindow((HWND) GetHWND()); // We need to implement this in wxWidgets
36 m_dragImage = new wxDragImage(* this, itemId);
37 m_dragImage->BeginDrag(wxPoint(0, 0), this);
38 m_dragImage->Move(pt, this);
39 m_dragImage->Show(this);
43 In your OnMouseMove function, hide the image, do any display updating required,
44 then move and show the image again:
46 void MyTreeCtrl::OnMouseMove(wxMouseEvent& event)
48 if (m_dragMode == MY_TREE_DRAG_NONE)
54 // Prevent screen corruption by hiding the image
56 m_dragImage->Hide(this);
58 // Do some updating of the window, such as highlighting the drop target
63 ::UpdateWindow((HWND) GetHWND());
66 // Move and show the image again
67 m_dragImage->Move(event.GetPosition(), this);
68 m_dragImage->Show(this);
71 Eventually we end the drag and delete the drag image.
73 void MyTreeCtrl::OnLeftUp(wxMouseEvent& event)
77 // End the drag and delete the drag image
80 m_dragImage->EndDrag(this);
89 Notes for Unix version:
90 Can we simply use cursors instead, creating a cursor dynamically, setting it into the window
91 in BeginDrag, and restoring the old cursor in EndDrag?
92 For a really bog-standard implementation, we could simply use a normal dragging cursor
100 class WXDLLEXPORT wxDragImage
: public wxObject
105 ////////////////////////////////////////////////////////////////////////////
108 wxDragImage(const wxBitmap
& image
, const wxCursor
& cursor
= wxNullCursor
)
112 Create(image
, cursor
);
115 // Deprecated form of the above
116 wxDragImage(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
)
120 Create(image
, cursor
, cursorHotspot
);
123 wxDragImage(const wxIcon
& image
, const wxCursor
& cursor
= wxNullCursor
)
127 Create(image
, cursor
);
130 // Deprecated form of the above
131 wxDragImage(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
)
135 Create(image
, cursor
, cursorHotspot
);
138 wxDragImage(const wxString
& str
, const wxCursor
& cursor
= wxNullCursor
)
145 // Deprecated form of the above
146 wxDragImage(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
)
150 Create(str
, cursor
, cursorHotspot
);
154 wxDragImage(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
158 Create(treeCtrl
, id
);
163 wxDragImage(const wxListCtrl
& listCtrl
, long id
)
167 Create(listCtrl
, id
);
171 virtual ~wxDragImage();
174 ////////////////////////////////////////////////////////////////////////////
177 ////////////////////////////////////////////////////////////////////////////
179 // Create a drag image from a bitmap and optional cursor
180 bool Create(const wxBitmap
& image
, const wxCursor
& cursor
= wxNullCursor
);
181 bool Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
183 wxLogDebug(wxT("wxDragImage::Create: use of a cursor hotspot is now deprecated. Please omit this argument."));
184 return Create(image
, cursor
);
187 // Create a drag image from an icon and optional cursor
188 bool Create(const wxIcon
& image
, const wxCursor
& cursor
= wxNullCursor
);
189 bool Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
191 wxLogDebug(wxT("wxDragImage::Create: use of a cursor hotspot is now deprecated. Please omit this argument."));
192 return Create(image
, cursor
);
195 // Create a drag image from a string and optional cursor
196 bool Create(const wxString
& str
, const wxCursor
& cursor
= wxNullCursor
);
197 bool Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
199 wxLogDebug(wxT("wxDragImage::Create: use of a cursor hotspot is now deprecated. Please omit this argument."));
200 return Create(str
, cursor
);
204 // Create a drag image for the given tree control item
205 bool Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
);
209 // Create a drag image for the given list control item
210 bool Create(const wxListCtrl
& listCtrl
, long id
);
213 // Begin drag. hotspot is the location of the drag position relative to the upper-left
214 // corner of the image.
215 bool BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
= false, wxRect
* rect
= (wxRect
*) NULL
);
217 // Begin drag. hotspot is the location of the drag position relative to the upper-left
218 // corner of the image. This is full screen only. fullScreenRect gives the
219 // position of the window on the screen, to restrict the drag to.
220 bool BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
);
225 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
226 // is non-NULL, or in screen coordinates if NULL.
227 bool Move(const wxPoint
& pt
);
236 ////////////////////////////////////////////////////////////////////////////
238 // Initialize variables
241 // Returns the native image list handle
242 WXHIMAGELIST
GetHIMAGELIST() const { return m_hImageList
; }
244 #if !wxUSE_SIMPLER_DRAGIMAGE
245 // Returns the native image list handle for the cursor
246 WXHIMAGELIST
GetCursorHIMAGELIST() const { return m_hCursorImageList
; }
250 WXHIMAGELIST m_hImageList
;
252 #if wxUSE_SIMPLER_DRAGIMAGE
253 wxCursor m_oldCursor
;
255 WXHIMAGELIST m_hCursorImageList
;
259 // wxPoint m_cursorHotspot; // Obsolete
262 wxRect m_boundingRect
;
266 DECLARE_DYNAMIC_CLASS(wxDragImage
)
267 DECLARE_NO_COPY_CLASS(wxDragImage
)