]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/dragimgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Generic wxDragImage implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "dragimgg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
36 #include "wx/window.h"
38 #include "wx/dcclient.h"
39 #include "wx/dcscreen.h"
40 #include "wx/dcmemory.h"
41 #include "wx/settings.h"
47 #define wxUSE_IMAGE_IN_DRAGIMAGE 1
49 #if wxUSE_IMAGE_IN_DRAGIMAGE
53 #include "wx/generic/dragimgg.h"
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 IMPLEMENT_DYNAMIC_CLASS(wxGenericDragImage
, wxObject
)
61 // ============================================================================
63 // ============================================================================
65 // ----------------------------------------------------------------------------
66 // wxGenericDragImage ctors/dtor
67 // ----------------------------------------------------------------------------
69 wxGenericDragImage::~wxGenericDragImage()
77 void wxGenericDragImage::Init()
81 m_windowDC
= (wxDC
*) NULL
;
82 m_window
= (wxWindow
*) NULL
;
84 m_pBackingBitmap
= (wxBitmap
*) NULL
;
88 ////////////////////////////////////////////////////////////////////////////
92 ////////////////////////////////////////////////////////////////////////////
94 // Create a drag image with a virtual image (need to override DoDrawImage, GetImageRect)
95 bool wxGenericDragImage::Create(const wxCursor
& cursor
)
102 // Create a drag image from a bitmap and optional cursor
103 bool wxGenericDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
)
105 // We don't have to combine the cursor explicitly since we simply show the cursor
106 // as we drag. This currently will only work within one window.
114 // Create a drag image from an icon and optional cursor
115 bool wxGenericDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
)
117 // We don't have to combine the cursor explicitly since we simply show the cursor
118 // as we drag. This currently will only work within one window.
126 // Create a drag image from a string and optional cursor
127 bool wxGenericDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
129 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
134 dc
.GetTextExtent(str
, & w
, & h
);
135 dc
.SetFont(wxNullFont
);
139 // Sometimes GetTextExtent isn't accurate enough, so make it longer
140 wxBitmap
bitmap((int) ((w
+2) * 1.5), (int) h
+2);
141 dc2
.SelectObject(bitmap
);
144 dc2
.SetBackground(* wxWHITE_BRUSH
);
146 dc2
.SetBackgroundMode(wxTRANSPARENT
);
147 dc2
.SetTextForeground(* wxLIGHT_GREY
);
148 dc2
.DrawText(str
, 0, 0);
149 dc2
.DrawText(str
, 1, 0);
150 dc2
.DrawText(str
, 2, 0);
151 dc2
.DrawText(str
, 1, 1);
152 dc2
.DrawText(str
, 2, 1);
153 dc2
.DrawText(str
, 1, 2);
154 dc2
.DrawText(str
, 2, 2);
156 dc2
.SetTextForeground(* wxBLACK
);
157 dc2
.DrawText(str
, 1, 1);
159 dc2
.SelectObject(wxNullBitmap
);
161 #if wxUSE_IMAGE_IN_DRAGIMAGE
162 // Make the bitmap masked
163 wxImage image
= bitmap
.ConvertToImage();
164 image
.SetMaskColour(255, 255, 255);
165 bitmap
= wxBitmap(image
);
168 return Create(bitmap
, cursor
);
172 // Create a drag image for the given tree control item
173 bool wxGenericDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
175 wxString str
= treeCtrl
.GetItemText(id
);
181 // Create a drag image for the given list control item
182 bool wxGenericDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
184 wxString str
= listCtrl
.GetItemText(id
);
190 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
,
195 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
197 // The image should be offset by this amount
200 m_fullScreen
= fullScreen
;
203 m_boundingRect
= * rect
;
210 window
->CaptureMouse();
214 m_oldCursor
= window
->GetCursor();
215 window
->SetCursor(m_cursor
);
219 // Make a copy of the window so we can repair damage done as the image is
226 clientSize
= window
->GetClientSize();
227 m_boundingRect
.x
= 0; m_boundingRect
.y
= 0;
228 m_boundingRect
.width
= clientSize
.x
; m_boundingRect
.height
= clientSize
.y
;
233 wxDisplaySize(& w
, & h
);
234 clientSize
.x
= w
; clientSize
.y
= h
;
237 pt
.x
= m_boundingRect
.x
; pt
.y
= m_boundingRect
.y
;
238 clientSize
.x
= m_boundingRect
.width
; clientSize
.y
= m_boundingRect
.height
;
242 m_boundingRect
.x
= 0; m_boundingRect
.y
= 0;
243 m_boundingRect
.width
= w
; m_boundingRect
.height
= h
;
247 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
249 if (!backing
->Ok() || (backing
->GetWidth() < clientSize
.x
|| backing
->GetHeight() < clientSize
.y
))
250 (*backing
) = wxBitmap(clientSize
.x
, clientSize
.y
);
254 m_windowDC
= new wxClientDC(window
);
258 m_windowDC
= new wxScreenDC
;
261 // Use m_boundingRect to limit the area considered.
262 ((wxScreenDC
*) m_windowDC
)->StartDrawingOnTop(rect
);
265 m_windowDC
->SetClippingRegion(m_boundingRect
.x
, m_boundingRect
.y
,
266 m_boundingRect
.width
, m_boundingRect
.height
);
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.
275 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
279 int x
= fullScreenRect
->GetPosition().x
;
280 int y
= fullScreenRect
->GetPosition().y
;
282 wxSize sz
= fullScreenRect
->GetSize();
284 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
285 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
287 rect
.x
= x
; rect
.y
= y
;
288 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
290 return BeginDrag(hotspot
, window
, TRUE
, & rect
);
294 bool wxGenericDragImage::EndDrag()
299 // Under Windows we can be pretty sure this test will give
300 // the correct results
301 if (wxWindow::GetCapture() == m_window
)
303 m_window
->ReleaseMouse();
305 if (m_cursor
.Ok() && m_oldCursor
.Ok())
307 m_window
->SetCursor(m_oldCursor
);
313 m_windowDC
->DestroyClippingRegion();
315 m_windowDC
= (wxDC
*) NULL
;
318 m_repairBitmap
= wxNullBitmap
;
323 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
324 // is non-NULL, or in screen coordinates if NULL.
325 bool wxGenericDragImage::Move(const wxPoint
& pt
)
327 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Move()") );
331 pt2
= m_window
->ClientToScreen(pt
);
333 // Erase at old position, then show at the current position
334 wxPoint oldPos
= m_position
;
336 bool eraseOldImage
= (m_isDirty
&& m_isShown
);
339 RedrawImage(oldPos
- m_offset
, pt2
- m_offset
, eraseOldImage
, TRUE
);
349 bool wxGenericDragImage::Show()
351 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Show()") );
353 // Show at the current position
357 // This is where we restore the backing bitmap, in case
358 // something has changed on the window.
360 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
362 memDC
.SelectObject(* backing
);
364 UpdateBackingFromWindow(* m_windowDC
, memDC
, m_boundingRect
, wxRect(0, 0, m_boundingRect
.width
, m_boundingRect
.height
));
366 //memDC.Blit(0, 0, m_boundingRect.width, m_boundingRect.height, m_windowDC, m_boundingRect.x, m_boundingRect.y);
367 memDC
.SelectObject(wxNullBitmap
);
369 RedrawImage(m_position
- m_offset
, m_position
- m_offset
, FALSE
, TRUE
);
378 bool wxGenericDragImage::UpdateBackingFromWindow(wxDC
& windowDC
, wxMemoryDC
& destDC
,
379 const wxRect
& sourceRect
, const wxRect
& destRect
) const
381 return destDC
.Blit(destRect
.x
, destRect
.y
, destRect
.width
, destRect
.height
, & windowDC
,
382 sourceRect
.x
, sourceRect
.y
);
385 bool wxGenericDragImage::Hide()
387 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Hide()") );
389 // Repair the old position
391 if (m_isShown
&& m_isDirty
)
393 RedrawImage(m_position
- m_offset
, m_position
- m_offset
, TRUE
, FALSE
);
402 // More efficient: erase and redraw simultaneously if possible
403 bool wxGenericDragImage::RedrawImage(const wxPoint
& oldPos
, const wxPoint
& newPos
,
404 bool eraseOld
, bool drawNew
)
409 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
413 wxRect
oldRect(GetImageRect(oldPos
));
414 wxRect
newRect(GetImageRect(newPos
));
418 // Full rect: the combination of both rects
419 if (eraseOld
&& drawNew
)
421 int oldRight
= oldRect
.GetRight();
422 int oldBottom
= oldRect
.GetBottom();
423 int newRight
= newRect
.GetRight();
424 int newBottom
= newRect
.GetBottom();
426 wxPoint topLeft
= wxPoint(wxMin(oldPos
.x
, newPos
.x
), wxMin(oldPos
.y
, newPos
.y
));
427 wxPoint bottomRight
= wxPoint(wxMax(oldRight
, newRight
), wxMax(oldBottom
, newBottom
));
429 fullRect
.x
= topLeft
.x
; fullRect
.y
= topLeft
.y
;
430 fullRect
.SetRight(bottomRight
.x
);
431 fullRect
.SetBottom(bottomRight
.y
);
438 // Make the bitmap bigger than it need be, so we don't
439 // keep reallocating all the time.
442 if (!m_repairBitmap
.Ok() || (m_repairBitmap
.GetWidth() < fullRect
.GetWidth() || m_repairBitmap
.GetHeight() < fullRect
.GetHeight()))
444 m_repairBitmap
= wxBitmap(fullRect
.GetWidth() + excess
, fullRect
.GetHeight() + excess
);
448 memDC
.SelectObject(* backing
);
450 wxMemoryDC memDCTemp
;
451 memDCTemp
.SelectObject(m_repairBitmap
);
453 // Draw the backing bitmap onto the repair bitmap.
454 // If full-screen, we may have specified the rect on the
455 // screen that we're using for our backing bitmap.
456 // So subtract this when we're blitting from the backing bitmap
457 // (translate from screen to backing-bitmap coords).
459 memDCTemp
.Blit(0, 0, fullRect
.GetWidth(), fullRect
.GetHeight(), & memDC
, fullRect
.x
- m_boundingRect
.x
, fullRect
.y
- m_boundingRect
.y
);
461 // If drawing, draw the image onto the mem DC
464 wxPoint
pos(newPos
.x
- fullRect
.x
, newPos
.y
- fullRect
.y
) ;
465 DoDrawImage(memDCTemp
, pos
);
468 // Now blit to the window
469 // Finally, blit the temp mem DC to the window.
470 m_windowDC
->Blit(fullRect
.x
, fullRect
.y
, fullRect
.width
, fullRect
.height
, & memDCTemp
, 0, 0);
472 memDCTemp
.SelectObject(wxNullBitmap
);
473 memDC
.SelectObject(wxNullBitmap
);
478 // Override this if you are using a virtual image (drawing your own image)
479 bool wxGenericDragImage::DoDrawImage(wxDC
& dc
, const wxPoint
& pos
) const
483 dc
.DrawBitmap(m_bitmap
, pos
.x
, pos
.y
, (m_bitmap
.GetMask() != 0));
486 else if (m_icon
.Ok())
488 dc
.DrawIcon(m_icon
, pos
.x
, pos
.y
);
495 // Override this if you are using a virtual image (drawing your own image)
496 wxRect
wxGenericDragImage::GetImageRect(const wxPoint
& pos
) const
500 return wxRect(pos
.x
, pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight());
502 else if (m_icon
.Ok())
504 return wxRect(pos
.x
, pos
.y
, m_icon
.GetWidth(), m_icon
.GetHeight());
508 return wxRect(pos
.x
, pos
.y
, 0, 0);
512 #endif // wxUSE_DRAGIMAGE