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
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_DRAGIMAG_H_
13 #define _WX_DRAGIMAG_H_
17 #include "wx/bitmap.h"
19 #include "wx/cursor.h"
20 #include "wx/treectrl.h"
21 #include "wx/listctrl.h"
23 // If 1, use a simple wxCursor instead of ImageList_SetDragCursorImage
24 #define wxUSE_SIMPLER_DRAGIMAGE 0
27 To use this class, create a wxDragImage when you start dragging, for example:
29 void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
32 ::UpdateWindow((HWND) GetHWND()); // We need to implement this in wxWidgets
37 m_dragImage = new wxDragImage(* this, itemId);
38 m_dragImage->BeginDrag(wxPoint(0, 0), this);
39 m_dragImage->Move(pt, this);
40 m_dragImage->Show(this);
44 In your OnMouseMove function, hide the image, do any display updating required,
45 then move and show the image again:
47 void MyTreeCtrl::OnMouseMove(wxMouseEvent& event)
49 if (m_dragMode == MY_TREE_DRAG_NONE)
55 // Prevent screen corruption by hiding the image
57 m_dragImage->Hide(this);
59 // Do some updating of the window, such as highlighting the drop target
64 ::UpdateWindow((HWND) GetHWND());
67 // Move and show the image again
68 m_dragImage->Move(event.GetPosition(), this);
69 m_dragImage->Show(this);
72 Eventually we end the drag and delete the drag image.
74 void MyTreeCtrl::OnLeftUp(wxMouseEvent& event)
78 // End the drag and delete the drag image
81 m_dragImage->EndDrag(this);
90 Notes for Unix version:
91 Can we simply use cursors instead, creating a cursor dynamically, setting it into the window
92 in BeginDrag, and restoring the old cursor in EndDrag?
93 For a really bog-standard implementation, we could simply use a normal dragging cursor
101 class WXDLLIMPEXP_CORE wxDragImage
: public wxObject
106 ////////////////////////////////////////////////////////////////////////////
109 wxDragImage(const wxBitmap
& image
, const wxCursor
& cursor
= wxNullCursor
)
113 Create(image
, cursor
);
116 wxDragImage(const wxIcon
& image
, const wxCursor
& cursor
= wxNullCursor
)
120 Create(image
, cursor
);
123 wxDragImage(const wxString
& str
, const wxCursor
& cursor
= wxNullCursor
)
131 wxDragImage(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
135 Create(treeCtrl
, id
);
140 wxDragImage(const wxListCtrl
& listCtrl
, long id
)
144 Create(listCtrl
, id
);
148 virtual ~wxDragImage();
151 ////////////////////////////////////////////////////////////////////////////
154 ////////////////////////////////////////////////////////////////////////////
156 // Create a drag image from a bitmap and optional cursor
157 bool Create(const wxBitmap
& image
, const wxCursor
& cursor
= wxNullCursor
);
159 // Create a drag image from an icon and optional cursor
160 bool Create(const wxIcon
& image
, const wxCursor
& cursor
= wxNullCursor
);
162 // Create a drag image from a string and optional cursor
163 bool Create(const wxString
& str
, const wxCursor
& cursor
= wxNullCursor
);
166 // Create a drag image for the given tree control item
167 bool Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
);
171 // Create a drag image for the given list control item
172 bool Create(const wxListCtrl
& listCtrl
, long id
);
175 // Begin drag. hotspot is the location of the drag position relative to the upper-left
176 // corner of the image.
177 bool BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
= false, wxRect
* rect
= NULL
);
179 // Begin drag. hotspot is the location of the drag position relative to the upper-left
180 // corner of the image. This is full screen only. fullScreenRect gives the
181 // position of the window on the screen, to restrict the drag to.
182 bool BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
);
187 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
188 // is non-NULL, or in screen coordinates if NULL.
189 bool Move(const wxPoint
& pt
);
198 ////////////////////////////////////////////////////////////////////////////
200 // Initialize variables
203 // Returns the native image list handle
204 WXHIMAGELIST
GetHIMAGELIST() const { return m_hImageList
; }
206 #if !wxUSE_SIMPLER_DRAGIMAGE
207 // Returns the native image list handle for the cursor
208 WXHIMAGELIST
GetCursorHIMAGELIST() const { return m_hCursorImageList
; }
211 // don't use in new code, use versions without hot spot parameter
212 #if WXWIN_COMPATIBILITY_2_8
213 wxDEPRECATED_CONSTRUCTOR( wxDragImage(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
214 wxDEPRECATED_CONSTRUCTOR( wxDragImage(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
215 wxDEPRECATED_CONSTRUCTOR( wxDragImage(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
216 wxDEPRECATED( bool Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
217 wxDEPRECATED( bool Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
218 wxDEPRECATED( bool Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
219 #endif // WXWIN_COMPATIBILITY_2_8
222 WXHIMAGELIST m_hImageList
;
224 #if wxUSE_SIMPLER_DRAGIMAGE
225 wxCursor m_oldCursor
;
227 WXHIMAGELIST m_hCursorImageList
;
231 // wxPoint m_cursorHotspot; // Obsolete
234 wxRect m_boundingRect
;
238 DECLARE_DYNAMIC_CLASS(wxDragImage
)
239 wxDECLARE_NO_COPY_CLASS(wxDragImage
);
242 #endif // wxUSE_DRAGIMAGE