]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/dragimgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/dragimgg.cpp
3 // Purpose: Generic wxDragImage implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
31 #include "wx/window.h"
33 #include "wx/dcclient.h"
34 #include "wx/dcscreen.h"
35 #include "wx/dcmemory.h"
36 #include "wx/settings.h"
42 #define wxUSE_IMAGE_IN_DRAGIMAGE 1
44 #include "wx/generic/dragimgg.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS(wxGenericDragImage
, wxObject
)
52 // ============================================================================
54 // ============================================================================
56 // ----------------------------------------------------------------------------
57 // wxGenericDragImage ctors/dtor
58 // ----------------------------------------------------------------------------
60 wxGenericDragImage::~wxGenericDragImage()
68 void wxGenericDragImage::Init()
72 m_windowDC
= (wxDC
*) NULL
;
73 m_window
= (wxWindow
*) NULL
;
75 #if defined(wxMAC_USE_CORE_GRAPHICS) && wxMAC_USE_CORE_GRAPHICS
78 m_pBackingBitmap
= (wxBitmap
*) NULL
;
82 #if WXWIN_COMPATIBILITY_2_6
83 wxGenericDragImage::wxGenericDragImage(const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
89 wxGenericDragImage::wxGenericDragImage(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
93 Create(image
, cursor
);
96 wxGenericDragImage::wxGenericDragImage(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
100 Create(image
, cursor
);
103 wxGenericDragImage::wxGenericDragImage(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
110 bool wxGenericDragImage::Create(const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
112 return Create(cursor
);
115 bool wxGenericDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
117 return Create(image
, cursor
);
120 bool wxGenericDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
122 return Create(image
, cursor
);
125 bool wxGenericDragImage::Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
127 return Create(str
, cursor
);
129 #endif // WXWIN_COMPATIBILITY_2_6
132 ////////////////////////////////////////////////////////////////////////////
136 ////////////////////////////////////////////////////////////////////////////
138 // Create a drag image with a virtual image (need to override DoDrawImage, GetImageRect)
139 bool wxGenericDragImage::Create(const wxCursor
& cursor
)
146 // Create a drag image from a bitmap and optional cursor
147 bool wxGenericDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
)
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.
158 // Create a drag image from an icon and optional cursor
159 bool wxGenericDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
)
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.
170 // Create a drag image from a string and optional cursor
171 bool wxGenericDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
173 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
178 dc
.GetTextExtent(str
, & w
, & h
);
179 dc
.SetFont(wxNullFont
);
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
);
188 dc2
.SetBackground(* wxWHITE_BRUSH
);
190 dc2
.SetBackgroundMode(wxTRANSPARENT
);
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);
200 dc2
.SetTextForeground(* wxBLACK
);
201 dc2
.DrawText(str
, 1, 1);
203 dc2
.SelectObject(wxNullBitmap
);
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
);
212 return Create(bitmap
, cursor
);
216 // Create a drag image for the given tree control item
217 bool wxGenericDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
219 wxString str
= treeCtrl
.GetItemText(id
);
225 // Create a drag image for the given list control item
226 bool wxGenericDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
228 wxString str
= listCtrl
.GetItemText(id
);
234 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
,
239 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
241 // The image should be offset by this amount
244 m_fullScreen
= fullScreen
;
247 m_boundingRect
= * rect
;
254 window
->CaptureMouse();
258 m_oldCursor
= window
->GetCursor();
259 window
->SetCursor(m_cursor
);
263 // Make a copy of the window so we can repair damage done as the image is
270 clientSize
= window
->GetClientSize();
271 m_boundingRect
.x
= 0; m_boundingRect
.y
= 0;
272 m_boundingRect
.width
= clientSize
.x
; m_boundingRect
.height
= clientSize
.y
;
277 wxDisplaySize(& w
, & h
);
278 clientSize
.x
= w
; clientSize
.y
= h
;
281 pt
.x
= m_boundingRect
.x
; pt
.y
= m_boundingRect
.y
;
282 clientSize
.x
= m_boundingRect
.width
; clientSize
.y
= m_boundingRect
.height
;
286 m_boundingRect
.x
= 0; m_boundingRect
.y
= 0;
287 m_boundingRect
.width
= w
; m_boundingRect
.height
= h
;
291 #if defined(wxMAC_USE_CORE_GRAPHICS) && wxMAC_USE_CORE_GRAPHICS
292 // nothing to setup here
294 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
296 if (!backing
->Ok() || (backing
->GetWidth() < clientSize
.x
|| backing
->GetHeight() < clientSize
.y
))
297 (*backing
) = wxBitmap(clientSize
.x
, clientSize
.y
);
302 m_windowDC
= new wxClientDC(window
);
306 m_windowDC
= new wxScreenDC
;
309 // Use m_boundingRect to limit the area considered.
310 ((wxScreenDC
*) m_windowDC
)->StartDrawingOnTop(rect
);
313 m_windowDC
->SetClippingRegion(m_boundingRect
.x
, m_boundingRect
.y
,
314 m_boundingRect
.width
, m_boundingRect
.height
);
320 // Begin drag. hotspot is the location of the drag position relative to the upper-left
321 // corner of the image. This is full screen only. fullScreenRect gives the
322 // position of the window on the screen, to restrict the drag to.
323 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
327 int x
= fullScreenRect
->GetPosition().x
;
328 int y
= fullScreenRect
->GetPosition().y
;
330 wxSize sz
= fullScreenRect
->GetSize();
332 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
333 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
335 rect
.x
= x
; rect
.y
= y
;
336 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
338 return BeginDrag(hotspot
, window
, true, & rect
);
342 bool wxGenericDragImage::EndDrag()
347 // Under Windows we can be pretty sure this test will give
348 // the correct results
349 if (wxWindow::GetCapture() == m_window
)
351 m_window
->ReleaseMouse();
353 if (m_cursor
.Ok() && m_oldCursor
.Ok())
355 m_window
->SetCursor(m_oldCursor
);
361 #if defined(wxMAC_USE_CORE_GRAPHICS) && wxMAC_USE_CORE_GRAPHICS
364 m_windowDC
->DestroyClippingRegion();
367 m_windowDC
= (wxDC
*) NULL
;
370 #if defined(wxMAC_USE_CORE_GRAPHICS) && wxMAC_USE_CORE_GRAPHICS
371 // nothing to do for overlays
373 m_repairBitmap
= wxNullBitmap
;
379 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
380 // is non-NULL, or in screen coordinates if NULL.
381 bool wxGenericDragImage::Move(const wxPoint
& pt
)
383 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Move()") );
387 pt2
= m_window
->ClientToScreen(pt
);
389 // Erase at old position, then show at the current position
390 wxPoint oldPos
= m_position
;
392 bool eraseOldImage
= (m_isDirty
&& m_isShown
);
395 RedrawImage(oldPos
- m_offset
, pt2
- m_offset
, eraseOldImage
, true);
405 bool wxGenericDragImage::Show()
407 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Show()") );
409 // Show at the current position
413 // This is where we restore the backing bitmap, in case
414 // something has changed on the window.
416 #if defined(wxMAC_USE_CORE_GRAPHICS) && wxMAC_USE_CORE_GRAPHICS
417 // overlay will be set up in the drawing routine
419 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
421 memDC
.SelectObject(* backing
);
423 UpdateBackingFromWindow(* m_windowDC
, memDC
, m_boundingRect
, wxRect(0, 0, m_boundingRect
.width
, m_boundingRect
.height
));
425 //memDC.Blit(0, 0, m_boundingRect.width, m_boundingRect.height, m_windowDC, m_boundingRect.x, m_boundingRect.y);
426 memDC
.SelectObject(wxNullBitmap
);
429 RedrawImage(m_position
- m_offset
, m_position
- m_offset
, false, true);
438 bool wxGenericDragImage::UpdateBackingFromWindow(wxDC
& windowDC
, wxMemoryDC
& destDC
,
439 const wxRect
& sourceRect
, const wxRect
& destRect
) const
441 return destDC
.Blit(destRect
.x
, destRect
.y
, destRect
.width
, destRect
.height
, & windowDC
,
442 sourceRect
.x
, sourceRect
.y
);
445 bool wxGenericDragImage::Hide()
447 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Hide()") );
449 // Repair the old position
451 if (m_isShown
&& m_isDirty
)
453 RedrawImage(m_position
- m_offset
, m_position
- m_offset
, true, false);
462 // More efficient: erase and redraw simultaneously if possible
463 bool wxGenericDragImage::RedrawImage(const wxPoint
& oldPos
, const wxPoint
& newPos
,
464 bool eraseOld
, bool drawNew
)
469 #if defined(wxMAC_USE_CORE_GRAPHICS) && wxMAC_USE_CORE_GRAPHICS
470 wxDCOverlay
dcoverlay( m_overlay
, (wxWindowDC
*) m_windowDC
) ;
474 DoDrawImage(*m_windowDC
, newPos
);
476 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
480 wxRect
oldRect(GetImageRect(oldPos
));
481 wxRect
newRect(GetImageRect(newPos
));
485 // Full rect: the combination of both rects
486 if (eraseOld
&& drawNew
)
488 int oldRight
= oldRect
.GetRight();
489 int oldBottom
= oldRect
.GetBottom();
490 int newRight
= newRect
.GetRight();
491 int newBottom
= newRect
.GetBottom();
493 wxPoint topLeft
= wxPoint(wxMin(oldPos
.x
, newPos
.x
), wxMin(oldPos
.y
, newPos
.y
));
494 wxPoint bottomRight
= wxPoint(wxMax(oldRight
, newRight
), wxMax(oldBottom
, newBottom
));
496 fullRect
.x
= topLeft
.x
; fullRect
.y
= topLeft
.y
;
497 fullRect
.SetRight(bottomRight
.x
);
498 fullRect
.SetBottom(bottomRight
.y
);
505 // Make the bitmap bigger than it need be, so we don't
506 // keep reallocating all the time.
509 if (!m_repairBitmap
.Ok() || (m_repairBitmap
.GetWidth() < fullRect
.GetWidth() || m_repairBitmap
.GetHeight() < fullRect
.GetHeight()))
511 m_repairBitmap
= wxBitmap(fullRect
.GetWidth() + excess
, fullRect
.GetHeight() + excess
);
515 memDC
.SelectObject(* backing
);
517 wxMemoryDC memDCTemp
;
518 memDCTemp
.SelectObject(m_repairBitmap
);
520 // Draw the backing bitmap onto the repair bitmap.
521 // If full-screen, we may have specified the rect on the
522 // screen that we're using for our backing bitmap.
523 // So subtract this when we're blitting from the backing bitmap
524 // (translate from screen to backing-bitmap coords).
526 memDCTemp
.Blit(0, 0, fullRect
.GetWidth(), fullRect
.GetHeight(), & memDC
, fullRect
.x
- m_boundingRect
.x
, fullRect
.y
- m_boundingRect
.y
);
528 // If drawing, draw the image onto the mem DC
531 wxPoint
pos(newPos
.x
- fullRect
.x
, newPos
.y
- fullRect
.y
) ;
532 DoDrawImage(memDCTemp
, pos
);
535 // Now blit to the window
536 // Finally, blit the temp mem DC to the window.
537 m_windowDC
->Blit(fullRect
.x
, fullRect
.y
, fullRect
.width
, fullRect
.height
, & memDCTemp
, 0, 0);
539 memDCTemp
.SelectObject(wxNullBitmap
);
540 memDC
.SelectObject(wxNullBitmap
);
545 // Override this if you are using a virtual image (drawing your own image)
546 bool wxGenericDragImage::DoDrawImage(wxDC
& dc
, const wxPoint
& pos
) const
550 dc
.DrawBitmap(m_bitmap
, pos
.x
, pos
.y
, (m_bitmap
.GetMask() != 0));
553 else if (m_icon
.Ok())
555 dc
.DrawIcon(m_icon
, pos
.x
, pos
.y
);
562 // Override this if you are using a virtual image (drawing your own image)
563 wxRect
wxGenericDragImage::GetImageRect(const wxPoint
& pos
) const
567 return wxRect(pos
.x
, pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight());
569 else if (m_icon
.Ok())
571 return wxRect(pos
.x
, pos
.y
, m_icon
.GetWidth(), m_icon
.GetHeight());
575 return wxRect(pos
.x
, pos
.y
, 0, 0);
579 #endif // wxUSE_DRAGIMAGE