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