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