]>
Commit | Line | Data |
---|---|---|
68be9f09 | 1 | ///////////////////////////////////////////////////////////////////////////// |
9b5f1895 | 2 | // Name: src/generic/dragimgg.cpp |
68be9f09 JS |
3 | // Purpose: Generic wxDragImage implementation |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 29/2/2000 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
68be9f09 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
68be9f09 JS |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
1e6feb95 VZ |
27 | #if wxUSE_DRAGIMAGE |
28 | ||
68be9f09 JS |
29 | #ifndef WX_PRECOMP |
30 | #include <stdio.h> | |
68be9f09 JS |
31 | #include "wx/window.h" |
32 | #include "wx/frame.h" | |
33 | #include "wx/dcclient.h" | |
34 | #include "wx/dcscreen.h" | |
35 | #include "wx/dcmemory.h" | |
36 | #include "wx/settings.h" | |
37 | #endif | |
38 | ||
39 | #include "wx/log.h" | |
40 | #include "wx/intl.h" | |
41 | ||
68be9f09 | 42 | #define wxUSE_IMAGE_IN_DRAGIMAGE 1 |
68be9f09 JS |
43 | |
44 | #if wxUSE_IMAGE_IN_DRAGIMAGE | |
45 | #include "wx/image.h" | |
46 | #endif | |
47 | ||
48 | #include "wx/generic/dragimgg.h" | |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // macros | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | IMPLEMENT_DYNAMIC_CLASS(wxGenericDragImage, wxObject) | |
55 | ||
56 | // ============================================================================ | |
57 | // implementation | |
58 | // ============================================================================ | |
59 | ||
60 | // ---------------------------------------------------------------------------- | |
61 | // wxGenericDragImage ctors/dtor | |
62 | // ---------------------------------------------------------------------------- | |
63 | ||
68be9f09 JS |
64 | wxGenericDragImage::~wxGenericDragImage() |
65 | { | |
66 | if (m_windowDC) | |
67 | { | |
68 | delete m_windowDC; | |
69 | } | |
70 | } | |
71 | ||
72 | void wxGenericDragImage::Init() | |
73 | { | |
ca65c044 WS |
74 | m_isDirty = false; |
75 | m_isShown = false; | |
68be9f09 JS |
76 | m_windowDC = (wxDC*) NULL; |
77 | m_window = (wxWindow*) NULL; | |
ca65c044 | 78 | m_fullScreen = false; |
f6bcfd97 | 79 | m_pBackingBitmap = (wxBitmap*) NULL; |
68be9f09 JS |
80 | } |
81 | ||
82 | // Attributes | |
83 | //////////////////////////////////////////////////////////////////////////// | |
84 | ||
85 | ||
86 | // Operations | |
87 | //////////////////////////////////////////////////////////////////////////// | |
88 | ||
f6bcfd97 | 89 | // Create a drag image with a virtual image (need to override DoDrawImage, GetImageRect) |
aa2d25a5 | 90 | bool wxGenericDragImage::Create(const wxCursor& cursor) |
f6bcfd97 BP |
91 | { |
92 | m_cursor = cursor; | |
f6bcfd97 | 93 | |
ca65c044 | 94 | return true; |
f6bcfd97 BP |
95 | } |
96 | ||
68be9f09 | 97 | // Create a drag image from a bitmap and optional cursor |
aa2d25a5 | 98 | bool wxGenericDragImage::Create(const wxBitmap& image, const wxCursor& cursor) |
68be9f09 JS |
99 | { |
100 | // We don't have to combine the cursor explicitly since we simply show the cursor | |
101 | // as we drag. This currently will only work within one window. | |
102 | ||
103 | m_cursor = cursor; | |
68be9f09 JS |
104 | m_bitmap = image; |
105 | ||
ca65c044 | 106 | return true ; |
68be9f09 JS |
107 | } |
108 | ||
109 | // Create a drag image from an icon and optional cursor | |
aa2d25a5 | 110 | bool wxGenericDragImage::Create(const wxIcon& image, const wxCursor& cursor) |
68be9f09 JS |
111 | { |
112 | // We don't have to combine the cursor explicitly since we simply show the cursor | |
113 | // as we drag. This currently will only work within one window. | |
114 | ||
115 | m_cursor = cursor; | |
68be9f09 JS |
116 | m_icon = image; |
117 | ||
ca65c044 | 118 | return true ; |
68be9f09 JS |
119 | } |
120 | ||
121 | // Create a drag image from a string and optional cursor | |
aa2d25a5 | 122 | bool wxGenericDragImage::Create(const wxString& str, const wxCursor& cursor) |
68be9f09 | 123 | { |
a756f210 | 124 | wxFont font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); |
68be9f09 JS |
125 | |
126 | long w, h; | |
127 | wxScreenDC dc; | |
128 | dc.SetFont(font); | |
129 | dc.GetTextExtent(str, & w, & h); | |
130 | dc.SetFont(wxNullFont); | |
131 | ||
132 | wxMemoryDC dc2; | |
68be9f09 JS |
133 | |
134 | // Sometimes GetTextExtent isn't accurate enough, so make it longer | |
135 | wxBitmap bitmap((int) ((w+2) * 1.5), (int) h+2); | |
136 | dc2.SelectObject(bitmap); | |
137 | ||
2b5f62a0 | 138 | dc2.SetFont(font); |
68be9f09 JS |
139 | dc2.SetBackground(* wxWHITE_BRUSH); |
140 | dc2.Clear(); | |
141 | dc2.SetBackgroundMode(wxTRANSPARENT); | |
142 | dc2.SetTextForeground(* wxLIGHT_GREY); | |
143 | dc2.DrawText(str, 0, 0); | |
144 | dc2.DrawText(str, 1, 0); | |
145 | dc2.DrawText(str, 2, 0); | |
146 | dc2.DrawText(str, 1, 1); | |
147 | dc2.DrawText(str, 2, 1); | |
148 | dc2.DrawText(str, 1, 2); | |
149 | dc2.DrawText(str, 2, 2); | |
150 | ||
151 | dc2.SetTextForeground(* wxBLACK); | |
152 | dc2.DrawText(str, 1, 1); | |
153 | ||
154 | dc2.SelectObject(wxNullBitmap); | |
155 | ||
64c288fa | 156 | #if wxUSE_IMAGE_IN_DRAGIMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB) |
68be9f09 | 157 | // Make the bitmap masked |
368d59f0 | 158 | wxImage image = bitmap.ConvertToImage(); |
68be9f09 | 159 | image.SetMaskColour(255, 255, 255); |
368d59f0 | 160 | bitmap = wxBitmap(image); |
68be9f09 JS |
161 | #endif |
162 | ||
aa2d25a5 | 163 | return Create(bitmap, cursor); |
68be9f09 JS |
164 | } |
165 | ||
3080bf59 | 166 | #if wxUSE_TREECTRL |
68be9f09 JS |
167 | // Create a drag image for the given tree control item |
168 | bool wxGenericDragImage::Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id) | |
169 | { | |
170 | wxString str = treeCtrl.GetItemText(id); | |
171 | return Create(str); | |
172 | } | |
3080bf59 | 173 | #endif |
68be9f09 | 174 | |
3080bf59 | 175 | #if wxUSE_LISTCTRL |
68be9f09 JS |
176 | // Create a drag image for the given list control item |
177 | bool wxGenericDragImage::Create(const wxListCtrl& listCtrl, long id) | |
178 | { | |
179 | wxString str = listCtrl.GetItemText(id); | |
180 | return Create(str); | |
181 | } | |
3080bf59 | 182 | #endif |
68be9f09 JS |
183 | |
184 | // Begin drag | |
f6bcfd97 | 185 | bool wxGenericDragImage::BeginDrag(const wxPoint& hotspot, |
bc1dcfc1 VZ |
186 | wxWindow* window, |
187 | bool fullScreen, | |
188 | wxRect* rect) | |
68be9f09 JS |
189 | { |
190 | wxASSERT_MSG( (window != 0), wxT("Window must not be null in BeginDrag.")); | |
191 | ||
f6bcfd97 BP |
192 | // The image should be offset by this amount |
193 | m_offset = hotspot; | |
68be9f09 JS |
194 | m_window = window; |
195 | m_fullScreen = fullScreen; | |
196 | ||
197 | if (rect) | |
198 | m_boundingRect = * rect; | |
199 | ||
ca65c044 WS |
200 | m_isDirty = false; |
201 | m_isDirty = false; | |
68be9f09 JS |
202 | |
203 | if (window) | |
204 | { | |
205 | window->CaptureMouse(); | |
206 | ||
207 | if (m_cursor.Ok()) | |
208 | { | |
209 | m_oldCursor = window->GetCursor(); | |
210 | window->SetCursor(m_cursor); | |
211 | } | |
212 | } | |
213 | ||
214 | // Make a copy of the window so we can repair damage done as the image is | |
215 | // dragged. | |
216 | ||
217 | wxSize clientSize; | |
2997ca30 | 218 | wxPoint pt; |
68be9f09 JS |
219 | if (!m_fullScreen) |
220 | { | |
221 | clientSize = window->GetClientSize(); | |
222 | m_boundingRect.x = 0; m_boundingRect.y = 0; | |
223 | m_boundingRect.width = clientSize.x; m_boundingRect.height = clientSize.y; | |
224 | } | |
225 | else | |
226 | { | |
227 | int w, h; | |
228 | wxDisplaySize(& w, & h); | |
229 | clientSize.x = w; clientSize.y = h; | |
230 | if (rect) | |
231 | { | |
232 | pt.x = m_boundingRect.x; pt.y = m_boundingRect.y; | |
233 | clientSize.x = m_boundingRect.width; clientSize.y = m_boundingRect.height; | |
234 | } | |
235 | else | |
236 | { | |
237 | m_boundingRect.x = 0; m_boundingRect.y = 0; | |
238 | m_boundingRect.width = w; m_boundingRect.height = h; | |
239 | } | |
240 | } | |
241 | ||
f6bcfd97 BP |
242 | wxBitmap* backing = (m_pBackingBitmap ? m_pBackingBitmap : (wxBitmap*) & m_backingBitmap); |
243 | ||
244 | if (!backing->Ok() || (backing->GetWidth() < clientSize.x || backing->GetHeight() < clientSize.y)) | |
245 | (*backing) = wxBitmap(clientSize.x, clientSize.y); | |
68be9f09 JS |
246 | |
247 | if (!m_fullScreen) | |
69477ac4 | 248 | { |
68be9f09 | 249 | m_windowDC = new wxClientDC(window); |
69477ac4 | 250 | } |
68be9f09 JS |
251 | else |
252 | { | |
253 | m_windowDC = new wxScreenDC; | |
254 | ||
255 | #if 0 | |
256 | // Use m_boundingRect to limit the area considered. | |
257 | ((wxScreenDC*) m_windowDC)->StartDrawingOnTop(rect); | |
258 | #endif | |
259 | ||
260 | m_windowDC->SetClippingRegion(m_boundingRect.x, m_boundingRect.y, | |
261 | m_boundingRect.width, m_boundingRect.height); | |
262 | } | |
263 | ||
ca65c044 | 264 | return true; |
68be9f09 JS |
265 | } |
266 | ||
267 | // Begin drag. hotspot is the location of the drag position relative to the upper-left | |
268 | // corner of the image. This is full screen only. fullScreenRect gives the | |
269 | // position of the window on the screen, to restrict the drag to. | |
270 | bool wxGenericDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, wxWindow* fullScreenRect) | |
271 | { | |
272 | wxRect rect; | |
273 | ||
274 | int x = fullScreenRect->GetPosition().x; | |
275 | int y = fullScreenRect->GetPosition().y; | |
ca65c044 | 276 | |
68be9f09 JS |
277 | wxSize sz = fullScreenRect->GetSize(); |
278 | ||
279 | if (fullScreenRect->GetParent() && !fullScreenRect->IsKindOf(CLASSINFO(wxFrame))) | |
280 | fullScreenRect->GetParent()->ClientToScreen(& x, & y); | |
281 | ||
282 | rect.x = x; rect.y = y; | |
283 | rect.width = sz.x; rect.height = sz.y; | |
284 | ||
ca65c044 | 285 | return BeginDrag(hotspot, window, true, & rect); |
68be9f09 JS |
286 | } |
287 | ||
288 | // End drag | |
289 | bool wxGenericDragImage::EndDrag() | |
290 | { | |
291 | if (m_window) | |
292 | { | |
a5e84126 JS |
293 | #ifdef __WXMSW__ |
294 | // Under Windows we can be pretty sure this test will give | |
295 | // the correct results | |
296 | if (wxWindow::GetCapture() == m_window) | |
297 | #endif | |
298 | m_window->ReleaseMouse(); | |
299 | ||
68be9f09 JS |
300 | if (m_cursor.Ok() && m_oldCursor.Ok()) |
301 | { | |
302 | m_window->SetCursor(m_oldCursor); | |
303 | } | |
304 | } | |
305 | ||
306 | if (m_windowDC) | |
307 | { | |
308 | m_windowDC->DestroyClippingRegion(); | |
309 | delete m_windowDC; | |
310 | m_windowDC = (wxDC*) NULL; | |
311 | } | |
312 | ||
313 | m_repairBitmap = wxNullBitmap; | |
314 | ||
ca65c044 | 315 | return true; |
68be9f09 JS |
316 | } |
317 | ||
318 | // Move the image: call from OnMouseMove. Pt is in window client coordinates if window | |
319 | // is non-NULL, or in screen coordinates if NULL. | |
320 | bool wxGenericDragImage::Move(const wxPoint& pt) | |
321 | { | |
f6bcfd97 | 322 | wxASSERT_MSG( (m_windowDC != (wxDC*) NULL), wxT("No window DC in wxGenericDragImage::Move()") ); |
68be9f09 | 323 | |
aa2d25a5 JS |
324 | wxPoint pt2(pt); |
325 | if (m_fullScreen) | |
326 | pt2 = m_window->ClientToScreen(pt); | |
327 | ||
68be9f09 JS |
328 | // Erase at old position, then show at the current position |
329 | wxPoint oldPos = m_position; | |
330 | ||
331 | bool eraseOldImage = (m_isDirty && m_isShown); | |
ca65c044 | 332 | |
68be9f09 | 333 | if (m_isShown) |
ca65c044 | 334 | RedrawImage(oldPos - m_offset, pt2 - m_offset, eraseOldImage, true); |
68be9f09 | 335 | |
aa2d25a5 | 336 | m_position = pt2; |
68be9f09 JS |
337 | |
338 | if (m_isShown) | |
ca65c044 | 339 | m_isDirty = true; |
68be9f09 | 340 | |
ca65c044 | 341 | return true; |
68be9f09 JS |
342 | } |
343 | ||
344 | bool wxGenericDragImage::Show() | |
345 | { | |
f6bcfd97 | 346 | wxASSERT_MSG( (m_windowDC != (wxDC*) NULL), wxT("No window DC in wxGenericDragImage::Show()") ); |
ca65c044 | 347 | |
68be9f09 JS |
348 | // Show at the current position |
349 | ||
350 | if (!m_isShown) | |
351 | { | |
352 | // This is where we restore the backing bitmap, in case | |
353 | // something has changed on the window. | |
354 | ||
f6bcfd97 | 355 | wxBitmap* backing = (m_pBackingBitmap ? m_pBackingBitmap : (wxBitmap*) & m_backingBitmap); |
68be9f09 | 356 | wxMemoryDC memDC; |
f6bcfd97 BP |
357 | memDC.SelectObject(* backing); |
358 | ||
359 | UpdateBackingFromWindow(* m_windowDC, memDC, m_boundingRect, wxRect(0, 0, m_boundingRect.width, m_boundingRect.height)); | |
360 | ||
361 | //memDC.Blit(0, 0, m_boundingRect.width, m_boundingRect.height, m_windowDC, m_boundingRect.x, m_boundingRect.y); | |
68be9f09 JS |
362 | memDC.SelectObject(wxNullBitmap); |
363 | ||
ca65c044 | 364 | RedrawImage(m_position - m_offset, m_position - m_offset, false, true); |
68be9f09 JS |
365 | } |
366 | ||
ca65c044 WS |
367 | m_isShown = true; |
368 | m_isDirty = true; | |
68be9f09 | 369 | |
ca65c044 | 370 | return true; |
68be9f09 JS |
371 | } |
372 | ||
f6bcfd97 BP |
373 | bool wxGenericDragImage::UpdateBackingFromWindow(wxDC& windowDC, wxMemoryDC& destDC, |
374 | const wxRect& sourceRect, const wxRect& destRect) const | |
375 | { | |
69477ac4 JS |
376 | return destDC.Blit(destRect.x, destRect.y, destRect.width, destRect.height, & windowDC, |
377 | sourceRect.x, sourceRect.y); | |
f6bcfd97 BP |
378 | } |
379 | ||
68be9f09 JS |
380 | bool wxGenericDragImage::Hide() |
381 | { | |
f6bcfd97 | 382 | wxASSERT_MSG( (m_windowDC != (wxDC*) NULL), wxT("No window DC in wxGenericDragImage::Hide()") ); |
68be9f09 JS |
383 | |
384 | // Repair the old position | |
385 | ||
386 | if (m_isShown && m_isDirty) | |
387 | { | |
ca65c044 | 388 | RedrawImage(m_position - m_offset, m_position - m_offset, true, false); |
68be9f09 JS |
389 | } |
390 | ||
ca65c044 WS |
391 | m_isShown = false; |
392 | m_isDirty = false; | |
68be9f09 | 393 | |
ca65c044 | 394 | return true; |
68be9f09 JS |
395 | } |
396 | ||
397 | // More efficient: erase and redraw simultaneously if possible | |
398 | bool wxGenericDragImage::RedrawImage(const wxPoint& oldPos, const wxPoint& newPos, | |
399 | bool eraseOld, bool drawNew) | |
400 | { | |
401 | if (!m_windowDC) | |
ca65c044 | 402 | return false; |
68be9f09 | 403 | |
f6bcfd97 BP |
404 | wxBitmap* backing = (m_pBackingBitmap ? m_pBackingBitmap : (wxBitmap*) & m_backingBitmap); |
405 | if (!backing->Ok()) | |
ca65c044 | 406 | return false; |
68be9f09 JS |
407 | |
408 | wxRect oldRect(GetImageRect(oldPos)); | |
409 | wxRect newRect(GetImageRect(newPos)); | |
410 | ||
411 | wxRect fullRect; | |
412 | ||
413 | // Full rect: the combination of both rects | |
414 | if (eraseOld && drawNew) | |
415 | { | |
416 | int oldRight = oldRect.GetRight(); | |
417 | int oldBottom = oldRect.GetBottom(); | |
418 | int newRight = newRect.GetRight(); | |
419 | int newBottom = newRect.GetBottom(); | |
420 | ||
421 | wxPoint topLeft = wxPoint(wxMin(oldPos.x, newPos.x), wxMin(oldPos.y, newPos.y)); | |
422 | wxPoint bottomRight = wxPoint(wxMax(oldRight, newRight), wxMax(oldBottom, newBottom)); | |
423 | ||
424 | fullRect.x = topLeft.x; fullRect.y = topLeft.y; | |
425 | fullRect.SetRight(bottomRight.x); | |
426 | fullRect.SetBottom(bottomRight.y); | |
427 | } | |
428 | else if (eraseOld) | |
429 | fullRect = oldRect; | |
430 | else if (drawNew) | |
431 | fullRect = newRect; | |
432 | ||
433 | // Make the bitmap bigger than it need be, so we don't | |
434 | // keep reallocating all the time. | |
435 | int excess = 50; | |
436 | ||
437 | if (!m_repairBitmap.Ok() || (m_repairBitmap.GetWidth() < fullRect.GetWidth() || m_repairBitmap.GetHeight() < fullRect.GetHeight())) | |
438 | { | |
439 | m_repairBitmap = wxBitmap(fullRect.GetWidth() + excess, fullRect.GetHeight() + excess); | |
440 | } | |
441 | ||
442 | wxMemoryDC memDC; | |
f6bcfd97 | 443 | memDC.SelectObject(* backing); |
68be9f09 JS |
444 | |
445 | wxMemoryDC memDCTemp; | |
446 | memDCTemp.SelectObject(m_repairBitmap); | |
447 | ||
448 | // Draw the backing bitmap onto the repair bitmap. | |
449 | // If full-screen, we may have specified the rect on the | |
450 | // screen that we're using for our backing bitmap. | |
451 | // So subtract this when we're blitting from the backing bitmap | |
452 | // (translate from screen to backing-bitmap coords). | |
453 | ||
454 | memDCTemp.Blit(0, 0, fullRect.GetWidth(), fullRect.GetHeight(), & memDC, fullRect.x - m_boundingRect.x, fullRect.y - m_boundingRect.y); | |
455 | ||
456 | // If drawing, draw the image onto the mem DC | |
457 | if (drawNew) | |
458 | { | |
f6bcfd97 BP |
459 | wxPoint pos(newPos.x - fullRect.x, newPos.y - fullRect.y) ; |
460 | DoDrawImage(memDCTemp, pos); | |
68be9f09 JS |
461 | } |
462 | ||
463 | // Now blit to the window | |
464 | // Finally, blit the temp mem DC to the window. | |
465 | m_windowDC->Blit(fullRect.x, fullRect.y, fullRect.width, fullRect.height, & memDCTemp, 0, 0); | |
466 | ||
467 | memDCTemp.SelectObject(wxNullBitmap); | |
468 | memDC.SelectObject(wxNullBitmap); | |
469 | ||
ca65c044 | 470 | return true; |
68be9f09 JS |
471 | } |
472 | ||
f6bcfd97 BP |
473 | // Override this if you are using a virtual image (drawing your own image) |
474 | bool wxGenericDragImage::DoDrawImage(wxDC& dc, const wxPoint& pos) const | |
475 | { | |
476 | if (m_bitmap.Ok()) | |
477 | { | |
478 | dc.DrawBitmap(m_bitmap, pos.x, pos.y, (m_bitmap.GetMask() != 0)); | |
ca65c044 | 479 | return true; |
f6bcfd97 BP |
480 | } |
481 | else if (m_icon.Ok()) | |
482 | { | |
483 | dc.DrawIcon(m_icon, pos.x, pos.y); | |
ca65c044 | 484 | return true; |
f6bcfd97 BP |
485 | } |
486 | else | |
ca65c044 | 487 | return false; |
f6bcfd97 BP |
488 | } |
489 | ||
490 | // Override this if you are using a virtual image (drawing your own image) | |
68be9f09 JS |
491 | wxRect wxGenericDragImage::GetImageRect(const wxPoint& pos) const |
492 | { | |
493 | if (m_bitmap.Ok()) | |
494 | { | |
495 | return wxRect(pos.x, pos.y, m_bitmap.GetWidth(), m_bitmap.GetHeight()); | |
496 | } | |
497 | else if (m_icon.Ok()) | |
498 | { | |
499 | return wxRect(pos.x, pos.y, m_icon.GetWidth(), m_icon.GetHeight()); | |
500 | } | |
501 | else | |
502 | { | |
503 | return wxRect(pos.x, pos.y, 0, 0); | |
504 | } | |
505 | } | |
506 | ||
1e6feb95 | 507 | #endif // wxUSE_DRAGIMAGE |