1 //////////////////////////////////////////////////////////////////////////////
2 // Name: wx/generic/dragimgg.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_DRAGIMGG_H_
14 #define _WX_DRAGIMGG_H_
16 #if defined(__GNUG__) && !defined(__APPLE__)
17 #pragma interface "dragimgg.h"
20 #include "wx/bitmap.h"
22 #include "wx/cursor.h"
23 #include "wx/treectrl.h"
24 #include "wx/listctrl.h"
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 wxWindows
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);
94 class WXDLLEXPORT wxGenericDragImage
: public wxObject
99 ////////////////////////////////////////////////////////////////////////////
101 wxGenericDragImage(const wxCursor
& cursor
= wxNullCursor
)
107 // Deprecated version of the above
108 wxGenericDragImage(const wxCursor
& cursor
, const wxPoint
& cursorHotspot
)
111 Create(cursor
, cursorHotspot
);
114 wxGenericDragImage(const wxBitmap
& image
, const wxCursor
& cursor
= wxNullCursor
)
118 Create(image
, cursor
);
121 // Deprecated version of the above
122 wxGenericDragImage(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
)
126 Create(image
, cursor
, cursorHotspot
);
129 wxGenericDragImage(const wxIcon
& image
, const wxCursor
& cursor
= wxNullCursor
)
133 Create(image
, cursor
);
136 // Deprecated version of the above
137 wxGenericDragImage(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
)
141 Create(image
, cursor
, cursorHotspot
);
144 wxGenericDragImage(const wxString
& str
, const wxCursor
& cursor
= wxNullCursor
)
151 // Deprecated version of the above
152 wxGenericDragImage(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
)
156 Create(str
, cursor
, cursorHotspot
);
160 wxGenericDragImage(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
164 Create(treeCtrl
, id
);
169 wxGenericDragImage(const wxListCtrl
& listCtrl
, long id
)
173 Create(listCtrl
, id
);
177 ~wxGenericDragImage();
180 ////////////////////////////////////////////////////////////////////////////
182 // For efficiency, tell wxGenericDragImage to use a bitmap that's already
183 // created (e.g. from last drag)
184 void SetBackingBitmap(wxBitmap
* bitmap
) { m_pBackingBitmap
= bitmap
; }
187 ////////////////////////////////////////////////////////////////////////////
189 // Create a drag image with a virtual image (need to override DoDrawImage, GetImageRect)
190 bool Create(const wxCursor
& cursor
= wxNullCursor
);
191 bool Create(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(cursor
);
197 // Create a drag image from a bitmap and optional cursor
198 bool Create(const wxBitmap
& image
, const wxCursor
& cursor
= wxNullCursor
);
199 bool Create(const wxBitmap
& image
, 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(image
, cursor
);
205 // Create a drag image from an icon and optional cursor
206 bool Create(const wxIcon
& image
, const wxCursor
& cursor
= wxNullCursor
);
207 bool Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
209 wxLogDebug(wxT("wxDragImage::Create: use of a cursor hotspot is now deprecated. Please omit this argument."));
210 return Create(image
, cursor
);
213 // Create a drag image from a string and optional cursor
214 bool Create(const wxString
& str
, const wxCursor
& cursor
= wxNullCursor
);
215 bool Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
217 wxLogDebug(wxT("wxDragImage::Create: use of a cursor hotspot is now deprecated. Please omit this argument."));
218 return Create(str
, cursor
);
222 // Create a drag image for the given tree control item
223 bool Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
);
227 // Create a drag image for the given list control item
228 bool Create(const wxListCtrl
& listCtrl
, long id
);
231 // Begin drag. hotspot is the location of the drag position relative to the upper-left
232 // corner of the image.
233 bool BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
= FALSE
, wxRect
* rect
= (wxRect
*) NULL
);
235 // Begin drag. hotspot is the location of the drag position relative to the upper-left
236 // corner of the image. This is full screen only. fullScreenRect gives the
237 // position of the window on the screen, to restrict the drag to.
238 bool BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
);
243 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
244 // is non-NULL, or in screen coordinates if NULL.
245 bool Move(const wxPoint
& pt
);
254 ////////////////////////////////////////////////////////////////////////////
258 // Override this if you are using a virtual image (drawing your own image)
259 virtual wxRect
GetImageRect(const wxPoint
& pos
) const;
261 // Override this if you are using a virtual image (drawing your own image)
262 virtual bool DoDrawImage(wxDC
& dc
, const wxPoint
& pos
) const;
264 // Override this if you wish to draw the window contents to the backing bitmap
265 // yourself. This can be desirable if you wish to avoid flicker by not having to
266 // redraw the window itself before dragging in order to be graphic-minus-dragged-objects.
267 // Instead, paint the drag image's backing bitmap to be correct, and leave the window
268 // to be updated only when dragging the objects away (thus giving a smoother appearance).
269 virtual bool UpdateBackingFromWindow(wxDC
& windowDC
, wxMemoryDC
& destDC
,
270 const wxRect
& sourceRect
, const wxRect
& destRect
) const;
272 // Erase and redraw simultaneously if possible
273 virtual bool RedrawImage(const wxPoint
& oldPos
, const wxPoint
& newPos
, bool eraseOld
, bool drawNew
);
279 wxCursor m_oldCursor
;
280 // wxPoint m_hotspot;
281 wxPoint m_offset
; // The hostpot value passed to BeginDrag
288 // Stores the window contents while we're dragging the image around
289 wxBitmap m_backingBitmap
;
290 wxBitmap
* m_pBackingBitmap
; // Pointer to existing backing bitmap
291 // (pass to wxGenericDragImage as an efficiency measure)
292 // A temporary bitmap for repairing/redrawing
293 wxBitmap m_repairBitmap
;
295 wxRect m_boundingRect
;
299 DECLARE_DYNAMIC_CLASS(wxGenericDragImage
)
300 DECLARE_NO_COPY_CLASS(wxGenericDragImage
)