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