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 #include "wx/bitmap.h"
18 #include "wx/cursor.h"
19 #include "wx/treectrl.h"
20 #include "wx/listctrl.h"
22 #include "wx/overlay.h"
25 To use this class, create a wxDragImage when you start dragging, for example:
27 void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
30 ::UpdateWindow((HWND) GetHWND()); // We need to implement this in wxWidgets
35 m_dragImage = new wxDragImage(* this, itemId);
36 m_dragImage->BeginDrag(wxPoint(0, 0), this);
37 m_dragImage->Move(pt, this);
38 m_dragImage->Show(this);
42 In your OnMouseMove function, hide the image, do any display updating required,
43 then move and show the image again:
45 void MyTreeCtrl::OnMouseMove(wxMouseEvent& event)
47 if (m_dragMode == MY_TREE_DRAG_NONE)
53 // Prevent screen corruption by hiding the image
55 m_dragImage->Hide(this);
57 // Do some updating of the window, such as highlighting the drop target
62 ::UpdateWindow((HWND) GetHWND());
65 // Move and show the image again
66 m_dragImage->Move(event.GetPosition(), this);
67 m_dragImage->Show(this);
70 Eventually we end the drag and delete the drag image.
72 void MyTreeCtrl::OnLeftUp(wxMouseEvent& event)
76 // End the drag and delete the drag image
79 m_dragImage->EndDrag(this);
91 class WXDLLEXPORT wxGenericDragImage
: public wxObject
96 ////////////////////////////////////////////////////////////////////////////
98 wxGenericDragImage(const wxCursor
& cursor
= wxNullCursor
)
104 wxGenericDragImage(const wxBitmap
& image
, const wxCursor
& cursor
= wxNullCursor
)
108 Create(image
, cursor
);
111 wxGenericDragImage(const wxIcon
& image
, const wxCursor
& cursor
= wxNullCursor
)
115 Create(image
, cursor
);
118 wxGenericDragImage(const wxString
& str
, const wxCursor
& cursor
= wxNullCursor
)
125 #if WXWIN_COMPATIBILITY_2_6
126 // don't use in new code, use versions without hot spot parameter
127 wxDEPRECATED( wxGenericDragImage(const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
128 wxDEPRECATED( wxGenericDragImage(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
129 wxDEPRECATED( wxGenericDragImage(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
130 wxDEPRECATED( wxGenericDragImage(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
131 wxDEPRECATED( bool Create(const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
132 wxDEPRECATED( bool Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
133 wxDEPRECATED( bool Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
134 wxDEPRECATED( bool Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& cursorHotspot
) );
135 #endif // WXWIN_COMPATIBILITY_2_6
138 wxGenericDragImage(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
142 Create(treeCtrl
, id
);
147 wxGenericDragImage(const wxListCtrl
& listCtrl
, long id
)
151 Create(listCtrl
, id
);
155 virtual ~wxGenericDragImage();
158 ////////////////////////////////////////////////////////////////////////////
160 // For efficiency, tell wxGenericDragImage to use a bitmap that's already
161 // created (e.g. from last drag)
162 void SetBackingBitmap(wxBitmap
* bitmap
) {
163 #ifndef wxHAS_NATIVE_OVERLAY
164 m_pBackingBitmap
= bitmap
;
169 ////////////////////////////////////////////////////////////////////////////
171 // Create a drag image with a virtual image (need to override DoDrawImage, GetImageRect)
172 bool Create(const wxCursor
& cursor
= wxNullCursor
);
174 // Create a drag image from a bitmap and optional cursor
175 bool Create(const wxBitmap
& image
, const wxCursor
& cursor
= wxNullCursor
);
177 // Create a drag image from an icon and optional cursor
178 bool Create(const wxIcon
& image
, const wxCursor
& cursor
= wxNullCursor
);
180 // Create a drag image from a string and optional cursor
181 bool Create(const wxString
& str
, const wxCursor
& cursor
= wxNullCursor
);
184 // Create a drag image for the given tree control item
185 bool Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
);
189 // Create a drag image for the given list control item
190 bool Create(const wxListCtrl
& listCtrl
, long id
);
193 // Begin drag. hotspot is the location of the drag position relative to the upper-left
194 // corner of the image.
195 bool BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
= false, wxRect
* rect
= (wxRect
*) NULL
);
197 // Begin drag. hotspot is the location of the drag position relative to the upper-left
198 // corner of the image. This is full screen only. fullScreenRect gives the
199 // position of the window on the screen, to restrict the drag to.
200 bool BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
);
205 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
206 // is non-NULL, or in screen coordinates if NULL.
207 bool Move(const wxPoint
& pt
);
216 ////////////////////////////////////////////////////////////////////////////
220 // Override this if you are using a virtual image (drawing your own image)
221 virtual wxRect
GetImageRect(const wxPoint
& pos
) const;
223 // Override this if you are using a virtual image (drawing your own image)
224 virtual bool DoDrawImage(wxDC
& dc
, const wxPoint
& pos
) const;
226 // Override this if you wish to draw the window contents to the backing bitmap
227 // yourself. This can be desirable if you wish to avoid flicker by not having to
228 // redraw the window itself before dragging in order to be graphic-minus-dragged-objects.
229 // Instead, paint the drag image's backing bitmap to be correct, and leave the window
230 // to be updated only when dragging the objects away (thus giving a smoother appearance).
231 virtual bool UpdateBackingFromWindow(wxDC
& windowDC
, wxMemoryDC
& destDC
,
232 const wxRect
& sourceRect
, const wxRect
& destRect
) const;
234 // Erase and redraw simultaneously if possible
235 virtual bool RedrawImage(const wxPoint
& oldPos
, const wxPoint
& newPos
, bool eraseOld
, bool drawNew
);
241 wxCursor m_oldCursor
;
242 // wxPoint m_hotspot;
243 wxPoint m_offset
; // The hostpot value passed to BeginDrag
250 #ifdef wxHAS_NATIVE_OVERLAY
252 wxDCOverlay
* m_dcOverlay
;
254 // Stores the window contents while we're dragging the image around
255 wxBitmap m_backingBitmap
;
256 wxBitmap
* m_pBackingBitmap
; // Pointer to existing backing bitmap
257 // (pass to wxGenericDragImage as an efficiency measure)
258 // A temporary bitmap for repairing/redrawing
259 wxBitmap m_repairBitmap
;
260 #endif // !wxHAS_NATIVE_OVERLAY
262 wxRect m_boundingRect
;
266 DECLARE_DYNAMIC_CLASS(wxGenericDragImage
)
267 DECLARE_NO_COPY_CLASS(wxGenericDragImage
)