]>
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"
41 #define wxUSE_IMAGE_IN_DRAGIMAGE 1
43 #if wxUSE_IMAGE_IN_DRAGIMAGE
47 #include "wx/generic/dragimgg.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxGenericDragImage
, wxObject
)
55 // ============================================================================
57 // ============================================================================
59 // ----------------------------------------------------------------------------
60 // wxGenericDragImage ctors/dtor
61 // ----------------------------------------------------------------------------
63 wxGenericDragImage::~wxGenericDragImage()
71 void wxGenericDragImage::Init()
75 m_windowDC
= (wxDC
*) NULL
;
76 m_window
= (wxWindow
*) NULL
;
78 m_pBackingBitmap
= (wxBitmap
*) 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
));
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(wxTRANSPARENT
);
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 wxASSERT_MSG( (window
!= 0), 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 window
->CaptureMouse();
257 m_oldCursor
= window
->GetCursor();
258 window
->SetCursor(m_cursor
);
262 // Make a copy of the window so we can repair damage done as the image is
269 clientSize
= window
->GetClientSize();
270 m_boundingRect
.x
= 0; m_boundingRect
.y
= 0;
271 m_boundingRect
.width
= clientSize
.x
; m_boundingRect
.height
= clientSize
.y
;
276 wxDisplaySize(& w
, & h
);
277 clientSize
.x
= w
; clientSize
.y
= h
;
280 pt
.x
= m_boundingRect
.x
; pt
.y
= m_boundingRect
.y
;
281 clientSize
.x
= m_boundingRect
.width
; clientSize
.y
= m_boundingRect
.height
;
285 m_boundingRect
.x
= 0; m_boundingRect
.y
= 0;
286 m_boundingRect
.width
= w
; m_boundingRect
.height
= h
;
290 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
292 if (!backing
->Ok() || (backing
->GetWidth() < clientSize
.x
|| backing
->GetHeight() < clientSize
.y
))
293 (*backing
) = wxBitmap(clientSize
.x
, clientSize
.y
);
297 m_windowDC
= new wxClientDC(window
);
301 m_windowDC
= new wxScreenDC
;
304 // Use m_boundingRect to limit the area considered.
305 ((wxScreenDC
*) m_windowDC
)->StartDrawingOnTop(rect
);
308 m_windowDC
->SetClippingRegion(m_boundingRect
.x
, m_boundingRect
.y
,
309 m_boundingRect
.width
, m_boundingRect
.height
);
315 // Begin drag. hotspot is the location of the drag position relative to the upper-left
316 // corner of the image. This is full screen only. fullScreenRect gives the
317 // position of the window on the screen, to restrict the drag to.
318 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
322 int x
= fullScreenRect
->GetPosition().x
;
323 int y
= fullScreenRect
->GetPosition().y
;
325 wxSize sz
= fullScreenRect
->GetSize();
327 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
328 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
330 rect
.x
= x
; rect
.y
= y
;
331 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
333 return BeginDrag(hotspot
, window
, true, & rect
);
337 bool wxGenericDragImage::EndDrag()
342 // Under Windows we can be pretty sure this test will give
343 // the correct results
344 if (wxWindow::GetCapture() == m_window
)
346 m_window
->ReleaseMouse();
348 if (m_cursor
.Ok() && m_oldCursor
.Ok())
350 m_window
->SetCursor(m_oldCursor
);
356 m_windowDC
->DestroyClippingRegion();
358 m_windowDC
= (wxDC
*) NULL
;
361 m_repairBitmap
= wxNullBitmap
;
366 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
367 // is non-NULL, or in screen coordinates if NULL.
368 bool wxGenericDragImage::Move(const wxPoint
& pt
)
370 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Move()") );
374 pt2
= m_window
->ClientToScreen(pt
);
376 // Erase at old position, then show at the current position
377 wxPoint oldPos
= m_position
;
379 bool eraseOldImage
= (m_isDirty
&& m_isShown
);
382 RedrawImage(oldPos
- m_offset
, pt2
- m_offset
, eraseOldImage
, true);
392 bool wxGenericDragImage::Show()
394 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Show()") );
396 // Show at the current position
400 // This is where we restore the backing bitmap, in case
401 // something has changed on the window.
403 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
405 memDC
.SelectObject(* backing
);
407 UpdateBackingFromWindow(* m_windowDC
, memDC
, m_boundingRect
, wxRect(0, 0, m_boundingRect
.width
, m_boundingRect
.height
));
409 //memDC.Blit(0, 0, m_boundingRect.width, m_boundingRect.height, m_windowDC, m_boundingRect.x, m_boundingRect.y);
410 memDC
.SelectObject(wxNullBitmap
);
412 RedrawImage(m_position
- m_offset
, m_position
- m_offset
, false, true);
421 bool wxGenericDragImage::UpdateBackingFromWindow(wxDC
& windowDC
, wxMemoryDC
& destDC
,
422 const wxRect
& sourceRect
, const wxRect
& destRect
) const
424 return destDC
.Blit(destRect
.x
, destRect
.y
, destRect
.width
, destRect
.height
, & windowDC
,
425 sourceRect
.x
, sourceRect
.y
);
428 bool wxGenericDragImage::Hide()
430 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Hide()") );
432 // Repair the old position
434 if (m_isShown
&& m_isDirty
)
436 RedrawImage(m_position
- m_offset
, m_position
- m_offset
, true, false);
445 // More efficient: erase and redraw simultaneously if possible
446 bool wxGenericDragImage::RedrawImage(const wxPoint
& oldPos
, const wxPoint
& newPos
,
447 bool eraseOld
, bool drawNew
)
452 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
456 wxRect
oldRect(GetImageRect(oldPos
));
457 wxRect
newRect(GetImageRect(newPos
));
461 // Full rect: the combination of both rects
462 if (eraseOld
&& drawNew
)
464 int oldRight
= oldRect
.GetRight();
465 int oldBottom
= oldRect
.GetBottom();
466 int newRight
= newRect
.GetRight();
467 int newBottom
= newRect
.GetBottom();
469 wxPoint topLeft
= wxPoint(wxMin(oldPos
.x
, newPos
.x
), wxMin(oldPos
.y
, newPos
.y
));
470 wxPoint bottomRight
= wxPoint(wxMax(oldRight
, newRight
), wxMax(oldBottom
, newBottom
));
472 fullRect
.x
= topLeft
.x
; fullRect
.y
= topLeft
.y
;
473 fullRect
.SetRight(bottomRight
.x
);
474 fullRect
.SetBottom(bottomRight
.y
);
481 // Make the bitmap bigger than it need be, so we don't
482 // keep reallocating all the time.
485 if (!m_repairBitmap
.Ok() || (m_repairBitmap
.GetWidth() < fullRect
.GetWidth() || m_repairBitmap
.GetHeight() < fullRect
.GetHeight()))
487 m_repairBitmap
= wxBitmap(fullRect
.GetWidth() + excess
, fullRect
.GetHeight() + excess
);
491 memDC
.SelectObject(* backing
);
493 wxMemoryDC memDCTemp
;
494 memDCTemp
.SelectObject(m_repairBitmap
);
496 // Draw the backing bitmap onto the repair bitmap.
497 // If full-screen, we may have specified the rect on the
498 // screen that we're using for our backing bitmap.
499 // So subtract this when we're blitting from the backing bitmap
500 // (translate from screen to backing-bitmap coords).
502 memDCTemp
.Blit(0, 0, fullRect
.GetWidth(), fullRect
.GetHeight(), & memDC
, fullRect
.x
- m_boundingRect
.x
, fullRect
.y
- m_boundingRect
.y
);
504 // If drawing, draw the image onto the mem DC
507 wxPoint
pos(newPos
.x
- fullRect
.x
, newPos
.y
- fullRect
.y
) ;
508 DoDrawImage(memDCTemp
, pos
);
511 // Now blit to the window
512 // Finally, blit the temp mem DC to the window.
513 m_windowDC
->Blit(fullRect
.x
, fullRect
.y
, fullRect
.width
, fullRect
.height
, & memDCTemp
, 0, 0);
515 memDCTemp
.SelectObject(wxNullBitmap
);
516 memDC
.SelectObject(wxNullBitmap
);
521 // Override this if you are using a virtual image (drawing your own image)
522 bool wxGenericDragImage::DoDrawImage(wxDC
& dc
, const wxPoint
& pos
) const
526 dc
.DrawBitmap(m_bitmap
, pos
.x
, pos
.y
, (m_bitmap
.GetMask() != 0));
529 else if (m_icon
.Ok())
531 dc
.DrawIcon(m_icon
, pos
.x
, pos
.y
);
538 // Override this if you are using a virtual image (drawing your own image)
539 wxRect
wxGenericDragImage::GetImageRect(const wxPoint
& pos
) const
543 return wxRect(pos
.x
, pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight());
545 else if (m_icon
.Ok())
547 return wxRect(pos
.x
, pos
.y
, m_icon
.GetWidth(), m_icon
.GetHeight());
551 return wxRect(pos
.x
, pos
.y
, 0, 0);
555 #endif // wxUSE_DRAGIMAGE