]>
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
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
30 #include "wx/window.h"
32 #include "wx/dcclient.h"
33 #include "wx/dcscreen.h"
34 #include "wx/dcmemory.h"
35 #include "wx/settings.h"
41 #define wxUSE_IMAGE_IN_DRAGIMAGE 1
43 #include "wx/generic/dragimgg.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 IMPLEMENT_DYNAMIC_CLASS(wxGenericDragImage
, wxObject
)
51 // ============================================================================
53 // ============================================================================
55 // ----------------------------------------------------------------------------
56 // wxGenericDragImage ctors/dtor
57 // ----------------------------------------------------------------------------
59 wxGenericDragImage::~wxGenericDragImage()
67 void wxGenericDragImage::Init()
74 #ifdef wxHAS_NATIVE_OVERLAY
77 m_pBackingBitmap
= NULL
;
81 #if WXWIN_COMPATIBILITY_2_6
82 wxGenericDragImage::wxGenericDragImage(const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
88 wxGenericDragImage::wxGenericDragImage(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
92 Create(image
, cursor
);
95 wxGenericDragImage::wxGenericDragImage(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
99 Create(image
, cursor
);
102 wxGenericDragImage::wxGenericDragImage(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
109 bool wxGenericDragImage::Create(const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
111 return Create(cursor
);
114 bool wxGenericDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
116 return Create(image
, cursor
);
119 bool wxGenericDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
121 return Create(image
, cursor
);
124 bool wxGenericDragImage::Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
126 return Create(str
, cursor
);
128 #endif // WXWIN_COMPATIBILITY_2_6
131 ////////////////////////////////////////////////////////////////////////////
135 ////////////////////////////////////////////////////////////////////////////
137 // Create a drag image with a virtual image (need to override DoDrawImage, GetImageRect)
138 bool wxGenericDragImage::Create(const wxCursor
& cursor
)
145 // Create a drag image from a bitmap and optional cursor
146 bool wxGenericDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
)
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.
157 // Create a drag image from an icon and optional cursor
158 bool wxGenericDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
)
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.
169 // Create a drag image from a string and optional cursor
170 bool wxGenericDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
172 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
174 wxCoord w
= 0, h
= 0;
177 dc
.GetTextExtent(str
, & w
, & h
);
178 dc
.SetFont(wxNullFont
);
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
);
187 dc2
.SetBackground(* wxWHITE_BRUSH
);
189 dc2
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
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);
199 dc2
.SetTextForeground(* wxBLACK
);
200 dc2
.DrawText(str
, 1, 1);
202 dc2
.SelectObject(wxNullBitmap
);
204 #if wxUSE_IMAGE_IN_DRAGIMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
205 // Make the bitmap masked
206 wxImage image
= bitmap
.ConvertToImage();
207 image
.SetMaskColour(255, 255, 255);
208 bitmap
= wxBitmap(image
);
211 return Create(bitmap
, cursor
);
215 // Create a drag image for the given tree control item
216 bool wxGenericDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
218 wxString str
= treeCtrl
.GetItemText(id
);
224 // Create a drag image for the given list control item
225 bool wxGenericDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
227 wxString str
= listCtrl
.GetItemText(id
);
233 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
,
238 wxCHECK_MSG( window
, false, wxT("Window must not be null in BeginDrag."));
240 // The image should be offset by this amount
243 m_fullScreen
= fullScreen
;
246 m_boundingRect
= * rect
;
253 m_oldCursor
= window
->GetCursor();
254 window
->SetCursor(m_cursor
);
257 window
->CaptureMouse();
259 // Make a copy of the window so we can repair damage done as the image is
266 clientSize
= window
->GetClientSize();
267 m_boundingRect
.x
= 0; m_boundingRect
.y
= 0;
268 m_boundingRect
.width
= clientSize
.x
; m_boundingRect
.height
= clientSize
.y
;
273 wxDisplaySize(& w
, & h
);
274 clientSize
.x
= w
; clientSize
.y
= h
;
277 pt
.x
= m_boundingRect
.x
; pt
.y
= m_boundingRect
.y
;
278 clientSize
.x
= m_boundingRect
.width
; clientSize
.y
= m_boundingRect
.height
;
282 m_boundingRect
.x
= 0; m_boundingRect
.y
= 0;
283 m_boundingRect
.width
= w
; m_boundingRect
.height
= h
;
287 #ifndef wxHAS_NATIVE_OVERLAY
288 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
290 if (!backing
->IsOk() || (backing
->GetWidth() < clientSize
.x
|| backing
->GetHeight() < clientSize
.y
))
291 (*backing
) = wxBitmap(clientSize
.x
, clientSize
.y
);
292 #endif // !wxHAS_NATIVE_OVERLAY
296 m_windowDC
= new wxClientDC(window
);
300 m_windowDC
= new wxScreenDC
;
303 // Use m_boundingRect to limit the area considered.
304 ((wxScreenDC
*) m_windowDC
)->StartDrawingOnTop(rect
);
307 m_windowDC
->SetClippingRegion(m_boundingRect
.x
, m_boundingRect
.y
,
308 m_boundingRect
.width
, m_boundingRect
.height
);
314 // Begin drag. hotspot is the location of the drag position relative to the upper-left
315 // corner of the image. This is full screen only. fullScreenRect gives the
316 // position of the window on the screen, to restrict the drag to.
317 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
321 int x
= fullScreenRect
->GetPosition().x
;
322 int y
= fullScreenRect
->GetPosition().y
;
324 wxSize sz
= fullScreenRect
->GetSize();
326 if (fullScreenRect
->GetParent() && !wxDynamicCast(fullScreenRect
, wxFrame
))
327 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
329 rect
.x
= x
; rect
.y
= y
;
330 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
332 return BeginDrag(hotspot
, window
, true, & rect
);
336 bool wxGenericDragImage::EndDrag()
341 // Under Windows we can be pretty sure this test will give
342 // the correct results
343 if (wxWindow::GetCapture() == m_window
)
345 m_window
->ReleaseMouse();
347 if (m_cursor
.IsOk() && m_oldCursor
.IsOk())
349 m_window
->SetCursor(m_oldCursor
);
355 #ifdef wxHAS_NATIVE_OVERLAY
358 m_windowDC
->DestroyClippingRegion();
360 wxDELETE(m_windowDC
);
363 #ifndef wxHAS_NATIVE_OVERLAY
364 m_repairBitmap
= wxNullBitmap
;
370 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
371 // is non-NULL, or in screen coordinates if NULL.
372 bool wxGenericDragImage::Move(const wxPoint
& pt
)
374 wxASSERT_MSG( (m_windowDC
!= NULL
), wxT("No window DC in wxGenericDragImage::Move()") );
378 pt2
= m_window
->ClientToScreen(pt
);
380 // Erase at old position, then show at the current position
381 wxPoint oldPos
= m_position
;
383 bool eraseOldImage
= (m_isDirty
&& m_isShown
);
386 RedrawImage(oldPos
- m_offset
, pt2
- m_offset
, eraseOldImage
, true);
396 bool wxGenericDragImage::Show()
398 wxASSERT_MSG( (m_windowDC
!= NULL
), wxT("No window DC in wxGenericDragImage::Show()") );
400 // Show at the current position
404 // This is where we restore the backing bitmap, in case
405 // something has changed on the window.
407 #ifndef wxHAS_NATIVE_OVERLAY
408 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
410 memDC
.SelectObject(* backing
);
412 UpdateBackingFromWindow(* m_windowDC
, memDC
, m_boundingRect
, wxRect(0, 0, m_boundingRect
.width
, m_boundingRect
.height
));
414 //memDC.Blit(0, 0, m_boundingRect.width, m_boundingRect.height, m_windowDC, m_boundingRect.x, m_boundingRect.y);
415 memDC
.SelectObject(wxNullBitmap
);
416 #endif // !wxHAS_NATIVE_OVERLAY
418 RedrawImage(m_position
- m_offset
, m_position
- m_offset
, false, true);
427 bool wxGenericDragImage::UpdateBackingFromWindow(wxDC
& windowDC
, wxMemoryDC
& destDC
,
428 const wxRect
& sourceRect
, const wxRect
& destRect
) const
430 return destDC
.Blit(destRect
.x
, destRect
.y
, destRect
.width
, destRect
.height
, & windowDC
,
431 sourceRect
.x
, sourceRect
.y
);
434 bool wxGenericDragImage::Hide()
436 wxASSERT_MSG( (m_windowDC
!= NULL
), wxT("No window DC in wxGenericDragImage::Hide()") );
438 // Repair the old position
440 if (m_isShown
&& m_isDirty
)
442 RedrawImage(m_position
- m_offset
, m_position
- m_offset
, true, false);
451 // More efficient: erase and redraw simultaneously if possible
452 bool wxGenericDragImage::RedrawImage(const wxPoint
& oldPos
,
453 const wxPoint
& newPos
,
454 bool eraseOld
, bool drawNew
)
459 #ifdef wxHAS_NATIVE_OVERLAY
462 wxDCOverlay
dcoverlay( m_overlay
, (wxWindowDC
*) m_windowDC
) ;
466 DoDrawImage(*m_windowDC
, newPos
);
467 #else // !wxHAS_NATIVE_OVERLAY
468 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
469 if (!backing
->IsOk())
472 wxRect
oldRect(GetImageRect(oldPos
));
473 wxRect
newRect(GetImageRect(newPos
));
477 // Full rect: the combination of both rects
478 if (eraseOld
&& drawNew
)
480 int oldRight
= oldRect
.GetRight();
481 int oldBottom
= oldRect
.GetBottom();
482 int newRight
= newRect
.GetRight();
483 int newBottom
= newRect
.GetBottom();
485 wxPoint topLeft
= wxPoint(wxMin(oldPos
.x
, newPos
.x
), wxMin(oldPos
.y
, newPos
.y
));
486 wxPoint bottomRight
= wxPoint(wxMax(oldRight
, newRight
), wxMax(oldBottom
, newBottom
));
488 fullRect
.x
= topLeft
.x
; fullRect
.y
= topLeft
.y
;
489 fullRect
.SetRight(bottomRight
.x
);
490 fullRect
.SetBottom(bottomRight
.y
);
497 // Make the bitmap bigger than it need be, so we don't
498 // keep reallocating all the time.
501 if (!m_repairBitmap
.IsOk() || (m_repairBitmap
.GetWidth() < fullRect
.GetWidth() || m_repairBitmap
.GetHeight() < fullRect
.GetHeight()))
503 m_repairBitmap
= wxBitmap(fullRect
.GetWidth() + excess
, fullRect
.GetHeight() + excess
);
507 memDC
.SelectObject(* backing
);
509 wxMemoryDC memDCTemp
;
510 memDCTemp
.SelectObject(m_repairBitmap
);
512 // Draw the backing bitmap onto the repair bitmap.
513 // If full-screen, we may have specified the rect on the
514 // screen that we're using for our backing bitmap.
515 // So subtract this when we're blitting from the backing bitmap
516 // (translate from screen to backing-bitmap coords).
518 memDCTemp
.Blit(0, 0, fullRect
.GetWidth(), fullRect
.GetHeight(), & memDC
, fullRect
.x
- m_boundingRect
.x
, fullRect
.y
- m_boundingRect
.y
);
520 // If drawing, draw the image onto the mem DC
523 wxPoint
pos(newPos
.x
- fullRect
.x
, newPos
.y
- fullRect
.y
) ;
524 DoDrawImage(memDCTemp
, pos
);
527 // Now blit to the window
528 // Finally, blit the temp mem DC to the window.
529 m_windowDC
->Blit(fullRect
.x
, fullRect
.y
, fullRect
.width
, fullRect
.height
, & memDCTemp
, 0, 0);
531 memDCTemp
.SelectObject(wxNullBitmap
);
532 memDC
.SelectObject(wxNullBitmap
);
533 #endif // wxHAS_NATIVE_OVERLAY/!wxHAS_NATIVE_OVERLAY
538 // Override this if you are using a virtual image (drawing your own image)
539 bool wxGenericDragImage::DoDrawImage(wxDC
& dc
, const wxPoint
& pos
) const
543 dc
.DrawBitmap(m_bitmap
, pos
.x
, pos
.y
, (m_bitmap
.GetMask() != 0));
546 else if (m_icon
.IsOk())
548 dc
.DrawIcon(m_icon
, pos
.x
, pos
.y
);
555 // Override this if you are using a virtual image (drawing your own image)
556 wxRect
wxGenericDragImage::GetImageRect(const wxPoint
& pos
) const
560 return wxRect(pos
.x
, pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight());
562 else if (m_icon
.IsOk())
564 return wxRect(pos
.x
, pos
.y
, m_icon
.GetWidth(), m_icon
.GetHeight());
568 return wxRect(pos
.x
, pos
.y
, 0, 0);
572 #endif // wxUSE_DRAGIMAGE