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