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