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