]>
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 m_pBackingBitmap
= (wxBitmap
*) NULL
;
78 #if WXWIN_COMPATIBILITY_2_6
79 wxGenericDragImage::wxGenericDragImage(const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
85 wxGenericDragImage::wxGenericDragImage(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
89 Create(image
, cursor
);
92 wxGenericDragImage::wxGenericDragImage(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
96 Create(image
, cursor
);
99 wxGenericDragImage::wxGenericDragImage(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
106 bool wxGenericDragImage::Create(const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
108 return Create(cursor
);
111 bool wxGenericDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
113 return Create(image
, cursor
);
116 bool wxGenericDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
118 return Create(image
, cursor
);
121 bool wxGenericDragImage::Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
123 return Create(str
, cursor
);
125 #endif // WXWIN_COMPATIBILITY_2_6
128 ////////////////////////////////////////////////////////////////////////////
132 ////////////////////////////////////////////////////////////////////////////
134 // Create a drag image with a virtual image (need to override DoDrawImage, GetImageRect)
135 bool wxGenericDragImage::Create(const wxCursor
& cursor
)
142 // Create a drag image from a bitmap and optional cursor
143 bool wxGenericDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
)
145 // We don't have to combine the cursor explicitly since we simply show the cursor
146 // as we drag. This currently will only work within one window.
154 // Create a drag image from an icon and optional cursor
155 bool wxGenericDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
)
157 // We don't have to combine the cursor explicitly since we simply show the cursor
158 // as we drag. This currently will only work within one window.
166 // Create a drag image from a string and optional cursor
167 bool wxGenericDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
169 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
174 dc
.GetTextExtent(str
, & w
, & h
);
175 dc
.SetFont(wxNullFont
);
179 // Sometimes GetTextExtent isn't accurate enough, so make it longer
180 wxBitmap
bitmap((int) ((w
+2) * 1.5), (int) h
+2);
181 dc2
.SelectObject(bitmap
);
184 dc2
.SetBackground(* wxWHITE_BRUSH
);
186 dc2
.SetBackgroundMode(wxTRANSPARENT
);
187 dc2
.SetTextForeground(* wxLIGHT_GREY
);
188 dc2
.DrawText(str
, 0, 0);
189 dc2
.DrawText(str
, 1, 0);
190 dc2
.DrawText(str
, 2, 0);
191 dc2
.DrawText(str
, 1, 1);
192 dc2
.DrawText(str
, 2, 1);
193 dc2
.DrawText(str
, 1, 2);
194 dc2
.DrawText(str
, 2, 2);
196 dc2
.SetTextForeground(* wxBLACK
);
197 dc2
.DrawText(str
, 1, 1);
199 dc2
.SelectObject(wxNullBitmap
);
201 #if wxUSE_IMAGE_IN_DRAGIMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
202 // Make the bitmap masked
203 wxImage image
= bitmap
.ConvertToImage();
204 image
.SetMaskColour(255, 255, 255);
205 bitmap
= wxBitmap(image
);
208 return Create(bitmap
, cursor
);
212 // Create a drag image for the given tree control item
213 bool wxGenericDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
215 wxString str
= treeCtrl
.GetItemText(id
);
221 // Create a drag image for the given list control item
222 bool wxGenericDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
224 wxString str
= listCtrl
.GetItemText(id
);
230 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
,
235 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
237 // The image should be offset by this amount
240 m_fullScreen
= fullScreen
;
243 m_boundingRect
= * rect
;
250 window
->CaptureMouse();
254 m_oldCursor
= window
->GetCursor();
255 window
->SetCursor(m_cursor
);
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 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
289 if (!backing
->Ok() || (backing
->GetWidth() < clientSize
.x
|| backing
->GetHeight() < clientSize
.y
))
290 (*backing
) = wxBitmap(clientSize
.x
, clientSize
.y
);
294 m_windowDC
= new wxClientDC(window
);
298 m_windowDC
= new wxScreenDC
;
301 // Use m_boundingRect to limit the area considered.
302 ((wxScreenDC
*) m_windowDC
)->StartDrawingOnTop(rect
);
305 m_windowDC
->SetClippingRegion(m_boundingRect
.x
, m_boundingRect
.y
,
306 m_boundingRect
.width
, m_boundingRect
.height
);
312 // Begin drag. hotspot is the location of the drag position relative to the upper-left
313 // corner of the image. This is full screen only. fullScreenRect gives the
314 // position of the window on the screen, to restrict the drag to.
315 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
319 int x
= fullScreenRect
->GetPosition().x
;
320 int y
= fullScreenRect
->GetPosition().y
;
322 wxSize sz
= fullScreenRect
->GetSize();
324 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
325 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
327 rect
.x
= x
; rect
.y
= y
;
328 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
330 return BeginDrag(hotspot
, window
, true, & rect
);
334 bool wxGenericDragImage::EndDrag()
339 // Under Windows we can be pretty sure this test will give
340 // the correct results
341 if (wxWindow::GetCapture() == m_window
)
343 m_window
->ReleaseMouse();
345 if (m_cursor
.Ok() && m_oldCursor
.Ok())
347 m_window
->SetCursor(m_oldCursor
);
353 m_windowDC
->DestroyClippingRegion();
355 m_windowDC
= (wxDC
*) NULL
;
358 m_repairBitmap
= wxNullBitmap
;
363 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
364 // is non-NULL, or in screen coordinates if NULL.
365 bool wxGenericDragImage::Move(const wxPoint
& pt
)
367 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Move()") );
371 pt2
= m_window
->ClientToScreen(pt
);
373 // Erase at old position, then show at the current position
374 wxPoint oldPos
= m_position
;
376 bool eraseOldImage
= (m_isDirty
&& m_isShown
);
379 RedrawImage(oldPos
- m_offset
, pt2
- m_offset
, eraseOldImage
, true);
389 bool wxGenericDragImage::Show()
391 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Show()") );
393 // Show at the current position
397 // This is where we restore the backing bitmap, in case
398 // something has changed on the window.
400 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
402 memDC
.SelectObject(* backing
);
404 UpdateBackingFromWindow(* m_windowDC
, memDC
, m_boundingRect
, wxRect(0, 0, m_boundingRect
.width
, m_boundingRect
.height
));
406 //memDC.Blit(0, 0, m_boundingRect.width, m_boundingRect.height, m_windowDC, m_boundingRect.x, m_boundingRect.y);
407 memDC
.SelectObject(wxNullBitmap
);
409 RedrawImage(m_position
- m_offset
, m_position
- m_offset
, false, true);
418 bool wxGenericDragImage::UpdateBackingFromWindow(wxDC
& windowDC
, wxMemoryDC
& destDC
,
419 const wxRect
& sourceRect
, const wxRect
& destRect
) const
421 return destDC
.Blit(destRect
.x
, destRect
.y
, destRect
.width
, destRect
.height
, & windowDC
,
422 sourceRect
.x
, sourceRect
.y
);
425 bool wxGenericDragImage::Hide()
427 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Hide()") );
429 // Repair the old position
431 if (m_isShown
&& m_isDirty
)
433 RedrawImage(m_position
- m_offset
, m_position
- m_offset
, true, false);
442 // More efficient: erase and redraw simultaneously if possible
443 bool wxGenericDragImage::RedrawImage(const wxPoint
& oldPos
, const wxPoint
& newPos
,
444 bool eraseOld
, bool drawNew
)
449 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
453 wxRect
oldRect(GetImageRect(oldPos
));
454 wxRect
newRect(GetImageRect(newPos
));
458 // Full rect: the combination of both rects
459 if (eraseOld
&& drawNew
)
461 int oldRight
= oldRect
.GetRight();
462 int oldBottom
= oldRect
.GetBottom();
463 int newRight
= newRect
.GetRight();
464 int newBottom
= newRect
.GetBottom();
466 wxPoint topLeft
= wxPoint(wxMin(oldPos
.x
, newPos
.x
), wxMin(oldPos
.y
, newPos
.y
));
467 wxPoint bottomRight
= wxPoint(wxMax(oldRight
, newRight
), wxMax(oldBottom
, newBottom
));
469 fullRect
.x
= topLeft
.x
; fullRect
.y
= topLeft
.y
;
470 fullRect
.SetRight(bottomRight
.x
);
471 fullRect
.SetBottom(bottomRight
.y
);
478 // Make the bitmap bigger than it need be, so we don't
479 // keep reallocating all the time.
482 if (!m_repairBitmap
.Ok() || (m_repairBitmap
.GetWidth() < fullRect
.GetWidth() || m_repairBitmap
.GetHeight() < fullRect
.GetHeight()))
484 m_repairBitmap
= wxBitmap(fullRect
.GetWidth() + excess
, fullRect
.GetHeight() + excess
);
488 memDC
.SelectObject(* backing
);
490 wxMemoryDC memDCTemp
;
491 memDCTemp
.SelectObject(m_repairBitmap
);
493 // Draw the backing bitmap onto the repair bitmap.
494 // If full-screen, we may have specified the rect on the
495 // screen that we're using for our backing bitmap.
496 // So subtract this when we're blitting from the backing bitmap
497 // (translate from screen to backing-bitmap coords).
499 memDCTemp
.Blit(0, 0, fullRect
.GetWidth(), fullRect
.GetHeight(), & memDC
, fullRect
.x
- m_boundingRect
.x
, fullRect
.y
- m_boundingRect
.y
);
501 // If drawing, draw the image onto the mem DC
504 wxPoint
pos(newPos
.x
- fullRect
.x
, newPos
.y
- fullRect
.y
) ;
505 DoDrawImage(memDCTemp
, pos
);
508 // Now blit to the window
509 // Finally, blit the temp mem DC to the window.
510 m_windowDC
->Blit(fullRect
.x
, fullRect
.y
, fullRect
.width
, fullRect
.height
, & memDCTemp
, 0, 0);
512 memDCTemp
.SelectObject(wxNullBitmap
);
513 memDC
.SelectObject(wxNullBitmap
);
518 // Override this if you are using a virtual image (drawing your own image)
519 bool wxGenericDragImage::DoDrawImage(wxDC
& dc
, const wxPoint
& pos
) const
523 dc
.DrawBitmap(m_bitmap
, pos
.x
, pos
.y
, (m_bitmap
.GetMask() != 0));
526 else if (m_icon
.Ok())
528 dc
.DrawIcon(m_icon
, pos
.x
, pos
.y
);
535 // Override this if you are using a virtual image (drawing your own image)
536 wxRect
wxGenericDragImage::GetImageRect(const wxPoint
& pos
) const
540 return wxRect(pos
.x
, pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight());
542 else if (m_icon
.Ok())
544 return wxRect(pos
.x
, pos
.y
, m_icon
.GetWidth(), m_icon
.GetHeight());
548 return wxRect(pos
.x
, pos
.y
, 0, 0);
552 #endif // wxUSE_DRAGIMAGE