]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/dragimgg.h
added template wxScopedArray<> too
[wxWidgets.git] / include / wx / generic / dragimgg.h
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
6 // Modified by:
7 // Created: 29/2/2000
8 // RCS-ID: $Id$
9 // Copyright: (c) Julian Smart
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_DRAGIMGG_H_
14 #define _WX_DRAGIMGG_H_
15
16 #include "wx/bitmap.h"
17 #include "wx/icon.h"
18 #include "wx/cursor.h"
19 #include "wx/treectrl.h"
20 #include "wx/listctrl.h"
21 #include "wx/log.h"
22 #include "wx/overlay.h"
23
24 /*
25 To use this class, create a wxDragImage when you start dragging, for example:
26
27 void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
28 {
29 #ifdef __WXMSW__
30 ::UpdateWindow((HWND) GetHWND()); // We need to implement this in wxWidgets
31 #endif
32
33 CaptureMouse();
34
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);
39 ...
40 }
41
42 In your OnMouseMove function, hide the image, do any display updating required,
43 then move and show the image again:
44
45 void MyTreeCtrl::OnMouseMove(wxMouseEvent& event)
46 {
47 if (m_dragMode == MY_TREE_DRAG_NONE)
48 {
49 event.Skip();
50 return;
51 }
52
53 // Prevent screen corruption by hiding the image
54 if (m_dragImage)
55 m_dragImage->Hide(this);
56
57 // Do some updating of the window, such as highlighting the drop target
58 ...
59
60 #ifdef __WXMSW__
61 if (updateWindow)
62 ::UpdateWindow((HWND) GetHWND());
63 #endif
64
65 // Move and show the image again
66 m_dragImage->Move(event.GetPosition(), this);
67 m_dragImage->Show(this);
68 }
69
70 Eventually we end the drag and delete the drag image.
71
72 void MyTreeCtrl::OnLeftUp(wxMouseEvent& event)
73 {
74 ...
75
76 // End the drag and delete the drag image
77 if (m_dragImage)
78 {
79 m_dragImage->EndDrag(this);
80 delete m_dragImage;
81 m_dragImage = NULL;
82 }
83 ReleaseMouse();
84 }
85 */
86
87 /*
88 * wxGenericDragImage
89 */
90
91 class WXDLLIMPEXP_CORE wxGenericDragImage: public wxObject
92 {
93 public:
94
95 // Ctors & dtor
96 ////////////////////////////////////////////////////////////////////////////
97
98 wxGenericDragImage(const wxCursor& cursor = wxNullCursor)
99 {
100 Init();
101 Create(cursor);
102 }
103
104 wxGenericDragImage(const wxBitmap& image, const wxCursor& cursor = wxNullCursor)
105 {
106 Init();
107
108 Create(image, cursor);
109 }
110
111 wxGenericDragImage(const wxIcon& image, const wxCursor& cursor = wxNullCursor)
112 {
113 Init();
114
115 Create(image, cursor);
116 }
117
118 wxGenericDragImage(const wxString& str, const wxCursor& cursor = wxNullCursor)
119 {
120 Init();
121
122 Create(str, cursor);
123 }
124
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
136
137 #if wxUSE_TREECTRL
138 wxGenericDragImage(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
139 {
140 Init();
141
142 Create(treeCtrl, id);
143 }
144 #endif
145
146 #if wxUSE_LISTCTRL
147 wxGenericDragImage(const wxListCtrl& listCtrl, long id)
148 {
149 Init();
150
151 Create(listCtrl, id);
152 }
153 #endif
154
155 virtual ~wxGenericDragImage();
156
157 // Attributes
158 ////////////////////////////////////////////////////////////////////////////
159
160 #ifdef wxHAS_NATIVE_OVERLAY
161 // backing store is not used when native overlays are
162 void SetBackingBitmap(wxBitmap* WXUNUSED(bitmap)) { }
163 #else
164 // For efficiency, tell wxGenericDragImage to use a bitmap that's already
165 // created (e.g. from last drag)
166 void SetBackingBitmap(wxBitmap* bitmap) { m_pBackingBitmap = bitmap; }
167 #endif // wxHAS_NATIVE_OVERLAY/!wxHAS_NATIVE_OVERLAY
168
169 // Operations
170 ////////////////////////////////////////////////////////////////////////////
171
172 // Create a drag image with a virtual image (need to override DoDrawImage, GetImageRect)
173 bool Create(const wxCursor& cursor = wxNullCursor);
174
175 // Create a drag image from a bitmap and optional cursor
176 bool Create(const wxBitmap& image, const wxCursor& cursor = wxNullCursor);
177
178 // Create a drag image from an icon and optional cursor
179 bool Create(const wxIcon& image, const wxCursor& cursor = wxNullCursor);
180
181 // Create a drag image from a string and optional cursor
182 bool Create(const wxString& str, const wxCursor& cursor = wxNullCursor);
183
184 #if wxUSE_TREECTRL
185 // Create a drag image for the given tree control item
186 bool Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id);
187 #endif
188
189 #if wxUSE_LISTCTRL
190 // Create a drag image for the given list control item
191 bool Create(const wxListCtrl& listCtrl, long id);
192 #endif
193
194 // Begin drag. hotspot is the location of the drag position relative to the upper-left
195 // corner of the image.
196 bool BeginDrag(const wxPoint& hotspot, wxWindow* window, bool fullScreen = false, wxRect* rect = NULL);
197
198 // Begin drag. hotspot is the location of the drag position relative to the upper-left
199 // corner of the image. This is full screen only. fullScreenRect gives the
200 // position of the window on the screen, to restrict the drag to.
201 bool BeginDrag(const wxPoint& hotspot, wxWindow* window, wxWindow* fullScreenRect);
202
203 // End drag
204 bool EndDrag();
205
206 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
207 // is non-NULL, or in screen coordinates if NULL.
208 bool Move(const wxPoint& pt);
209
210 // Show the image
211 bool Show();
212
213 // Hide the image
214 bool Hide();
215
216 // Implementation
217 ////////////////////////////////////////////////////////////////////////////
218
219 void Init();
220
221 // Override this if you are using a virtual image (drawing your own image)
222 virtual wxRect GetImageRect(const wxPoint& pos) const;
223
224 // Override this if you are using a virtual image (drawing your own image)
225 virtual bool DoDrawImage(wxDC& dc, const wxPoint& pos) const;
226
227 // Override this if you wish to draw the window contents to the backing bitmap
228 // yourself. This can be desirable if you wish to avoid flicker by not having to
229 // redraw the window itself before dragging in order to be graphic-minus-dragged-objects.
230 // Instead, paint the drag image's backing bitmap to be correct, and leave the window
231 // to be updated only when dragging the objects away (thus giving a smoother appearance).
232 virtual bool UpdateBackingFromWindow(wxDC& windowDC, wxMemoryDC& destDC,
233 const wxRect& sourceRect, const wxRect& destRect) const;
234
235 // Erase and redraw simultaneously if possible
236 virtual bool RedrawImage(const wxPoint& oldPos, const wxPoint& newPos, bool eraseOld, bool drawNew);
237
238 protected:
239 wxBitmap m_bitmap;
240 wxIcon m_icon;
241 wxCursor m_cursor;
242 wxCursor m_oldCursor;
243 // wxPoint m_hotspot;
244 wxPoint m_offset; // The hostpot value passed to BeginDrag
245 wxPoint m_position;
246 bool m_isDirty;
247 bool m_isShown;
248 wxWindow* m_window;
249 wxDC* m_windowDC;
250
251 #ifdef wxHAS_NATIVE_OVERLAY
252 wxOverlay m_overlay;
253 wxDCOverlay* m_dcOverlay;
254 #else
255 // Stores the window contents while we're dragging the image around
256 wxBitmap m_backingBitmap;
257 wxBitmap* m_pBackingBitmap; // Pointer to existing backing bitmap
258 // (pass to wxGenericDragImage as an efficiency measure)
259 // A temporary bitmap for repairing/redrawing
260 wxBitmap m_repairBitmap;
261 #endif // !wxHAS_NATIVE_OVERLAY
262
263 wxRect m_boundingRect;
264 bool m_fullScreen;
265
266 private:
267 DECLARE_DYNAMIC_CLASS(wxGenericDragImage)
268 DECLARE_NO_COPY_CLASS(wxGenericDragImage)
269 };
270
271 #endif
272 // _WX_DRAGIMGG_H_