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