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 wxDragImage(const wxIcon
& image
, const wxCursor
& cursor
= wxNullCursor
)
121 Create(image
, cursor
);
124 wxDragImage(const wxString
& str
, const wxCursor
& cursor
= wxNullCursor
)
132 wxDragImage(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
136 Create(treeCtrl
, id
);
141 wxDragImage(const wxListCtrl
& listCtrl
, long id
)
145 Create(listCtrl
, id
);
149 virtual ~wxDragImage();
152 ////////////////////////////////////////////////////////////////////////////
155 ////////////////////////////////////////////////////////////////////////////
157 // Create a drag image from a bitmap and optional cursor
158 bool Create(const wxBitmap
& image
, const wxCursor
& cursor
= wxNullCursor
);
160 // Create a drag image from an icon and optional cursor
161 bool Create(const wxIcon
& image
, const wxCursor
& cursor
= wxNullCursor
);
163 // Create a drag image from a string and optional cursor
164 bool Create(const wxString
& str
, const wxCursor
& cursor
= wxNullCursor
);
167 // Create a drag image for the given tree control item
168 bool Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
);
172 // Create a drag image for the given list control item
173 bool Create(const wxListCtrl
& listCtrl
, long id
);
176 // Begin drag. hotspot is the location of the drag position relative to the upper-left
177 // corner of the image.
178 bool BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
= false, wxRect
* rect
= NULL
);
180 // Begin drag. hotspot is the location of the drag position relative to the upper-left
181 // corner of the image. This is full screen only. fullScreenRect gives the
182 // position of the window on the screen, to restrict the drag to.
183 bool BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
);
188 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
189 // is non-NULL, or in screen coordinates if NULL.
190 bool Move(const wxPoint
& pt
);
199 ////////////////////////////////////////////////////////////////////////////
201 // Initialize variables
204 // Returns the native image list handle
205 WXHIMAGELIST
GetHIMAGELIST() const { return m_hImageList
; }
207 #if !wxUSE_SIMPLER_DRAGIMAGE
208 // Returns the native image list handle for the cursor
209 WXHIMAGELIST
GetCursorHIMAGELIST() const { return m_hCursorImageList
; }
212 // don't use in new code, use versions without hot spot parameter
213 #if WXWIN_COMPATIBILITY_2_8
214 wxDEPRECATED_CONSTRUCTOR( wxDragImage(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
215 wxDEPRECATED_CONSTRUCTOR( wxDragImage(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
216 wxDEPRECATED_CONSTRUCTOR( wxDragImage(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
217 wxDEPRECATED( bool Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
218 wxDEPRECATED( bool Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
219 wxDEPRECATED( bool Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
220 #endif // WXWIN_COMPATIBILITY_2_8
223 WXHIMAGELIST m_hImageList
;
225 #if wxUSE_SIMPLER_DRAGIMAGE
226 wxCursor m_oldCursor
;
228 WXHIMAGELIST m_hCursorImageList
;
232 // wxPoint m_cursorHotspot; // Obsolete
235 wxRect m_boundingRect
;
239 DECLARE_DYNAMIC_CLASS(wxDragImage
)
240 wxDECLARE_NO_COPY_CLASS(wxDragImage
);
243 #endif // wxUSE_DRAGIMAGE