]>
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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
32 #include "wx/window.h"
34 #include "wx/dcclient.h"
35 #include "wx/dcscreen.h"
36 #include "wx/dcmemory.h"
37 #include "wx/settings.h"
43 #define wxUSE_IMAGE_IN_DRAGIMAGE 1
45 #if wxUSE_IMAGE_IN_DRAGIMAGE
49 #include "wx/generic/dragimgg.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 IMPLEMENT_DYNAMIC_CLASS(wxGenericDragImage
, wxObject
)
57 // ============================================================================
59 // ============================================================================
61 // ----------------------------------------------------------------------------
62 // wxGenericDragImage ctors/dtor
63 // ----------------------------------------------------------------------------
65 wxGenericDragImage::~wxGenericDragImage()
73 void wxGenericDragImage::Init()
77 m_windowDC
= (wxDC
*) NULL
;
78 m_window
= (wxWindow
*) NULL
;
80 m_pBackingBitmap
= (wxBitmap
*) NULL
;
84 ////////////////////////////////////////////////////////////////////////////
88 ////////////////////////////////////////////////////////////////////////////
90 // Create a drag image with a virtual image (need to override DoDrawImage, GetImageRect)
91 bool wxGenericDragImage::Create(const wxCursor
& cursor
)
98 // Create a drag image from a bitmap and optional cursor
99 bool wxGenericDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
)
101 // We don't have to combine the cursor explicitly since we simply show the cursor
102 // as we drag. This currently will only work within one window.
110 // Create a drag image from an icon and optional cursor
111 bool wxGenericDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
)
113 // We don't have to combine the cursor explicitly since we simply show the cursor
114 // as we drag. This currently will only work within one window.
122 // Create a drag image from a string and optional cursor
123 bool wxGenericDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
125 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
130 dc
.GetTextExtent(str
, & w
, & h
);
131 dc
.SetFont(wxNullFont
);
135 // Sometimes GetTextExtent isn't accurate enough, so make it longer
136 wxBitmap
bitmap((int) ((w
+2) * 1.5), (int) h
+2);
137 dc2
.SelectObject(bitmap
);
140 dc2
.SetBackground(* wxWHITE_BRUSH
);
142 dc2
.SetBackgroundMode(wxTRANSPARENT
);
143 dc2
.SetTextForeground(* wxLIGHT_GREY
);
144 dc2
.DrawText(str
, 0, 0);
145 dc2
.DrawText(str
, 1, 0);
146 dc2
.DrawText(str
, 2, 0);
147 dc2
.DrawText(str
, 1, 1);
148 dc2
.DrawText(str
, 2, 1);
149 dc2
.DrawText(str
, 1, 2);
150 dc2
.DrawText(str
, 2, 2);
152 dc2
.SetTextForeground(* wxBLACK
);
153 dc2
.DrawText(str
, 1, 1);
155 dc2
.SelectObject(wxNullBitmap
);
157 #if wxUSE_IMAGE_IN_DRAGIMAGE
158 // Make the bitmap masked
159 wxImage image
= bitmap
.ConvertToImage();
160 image
.SetMaskColour(255, 255, 255);
161 bitmap
= wxBitmap(image
);
164 return Create(bitmap
, cursor
);
168 // Create a drag image for the given tree control item
169 bool wxGenericDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
171 wxString str
= treeCtrl
.GetItemText(id
);
177 // Create a drag image for the given list control item
178 bool wxGenericDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
180 wxString str
= listCtrl
.GetItemText(id
);
186 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
,
191 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
193 // The image should be offset by this amount
196 m_fullScreen
= fullScreen
;
199 m_boundingRect
= * rect
;
206 window
->CaptureMouse();
210 m_oldCursor
= window
->GetCursor();
211 window
->SetCursor(m_cursor
);
215 // Make a copy of the window so we can repair damage done as the image is
222 clientSize
= window
->GetClientSize();
223 m_boundingRect
.x
= 0; m_boundingRect
.y
= 0;
224 m_boundingRect
.width
= clientSize
.x
; m_boundingRect
.height
= clientSize
.y
;
229 wxDisplaySize(& w
, & h
);
230 clientSize
.x
= w
; clientSize
.y
= h
;
233 pt
.x
= m_boundingRect
.x
; pt
.y
= m_boundingRect
.y
;
234 clientSize
.x
= m_boundingRect
.width
; clientSize
.y
= m_boundingRect
.height
;
238 m_boundingRect
.x
= 0; m_boundingRect
.y
= 0;
239 m_boundingRect
.width
= w
; m_boundingRect
.height
= h
;
243 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
245 if (!backing
->Ok() || (backing
->GetWidth() < clientSize
.x
|| backing
->GetHeight() < clientSize
.y
))
246 (*backing
) = wxBitmap(clientSize
.x
, clientSize
.y
);
250 m_windowDC
= new wxClientDC(window
);
254 m_windowDC
= new wxScreenDC
;
257 // Use m_boundingRect to limit the area considered.
258 ((wxScreenDC
*) m_windowDC
)->StartDrawingOnTop(rect
);
261 m_windowDC
->SetClippingRegion(m_boundingRect
.x
, m_boundingRect
.y
,
262 m_boundingRect
.width
, m_boundingRect
.height
);
268 // Begin drag. hotspot is the location of the drag position relative to the upper-left
269 // corner of the image. This is full screen only. fullScreenRect gives the
270 // position of the window on the screen, to restrict the drag to.
271 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
275 int x
= fullScreenRect
->GetPosition().x
;
276 int y
= fullScreenRect
->GetPosition().y
;
278 wxSize sz
= fullScreenRect
->GetSize();
280 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
281 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
283 rect
.x
= x
; rect
.y
= y
;
284 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
286 return BeginDrag(hotspot
, window
, true, & rect
);
290 bool wxGenericDragImage::EndDrag()
295 // Under Windows we can be pretty sure this test will give
296 // the correct results
297 if (wxWindow::GetCapture() == m_window
)
299 m_window
->ReleaseMouse();
301 if (m_cursor
.Ok() && m_oldCursor
.Ok())
303 m_window
->SetCursor(m_oldCursor
);
309 m_windowDC
->DestroyClippingRegion();
311 m_windowDC
= (wxDC
*) NULL
;
314 m_repairBitmap
= wxNullBitmap
;
319 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
320 // is non-NULL, or in screen coordinates if NULL.
321 bool wxGenericDragImage::Move(const wxPoint
& pt
)
323 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Move()") );
327 pt2
= m_window
->ClientToScreen(pt
);
329 // Erase at old position, then show at the current position
330 wxPoint oldPos
= m_position
;
332 bool eraseOldImage
= (m_isDirty
&& m_isShown
);
335 RedrawImage(oldPos
- m_offset
, pt2
- m_offset
, eraseOldImage
, true);
345 bool wxGenericDragImage::Show()
347 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Show()") );
349 // Show at the current position
353 // This is where we restore the backing bitmap, in case
354 // something has changed on the window.
356 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
358 memDC
.SelectObject(* backing
);
360 UpdateBackingFromWindow(* m_windowDC
, memDC
, m_boundingRect
, wxRect(0, 0, m_boundingRect
.width
, m_boundingRect
.height
));
362 //memDC.Blit(0, 0, m_boundingRect.width, m_boundingRect.height, m_windowDC, m_boundingRect.x, m_boundingRect.y);
363 memDC
.SelectObject(wxNullBitmap
);
365 RedrawImage(m_position
- m_offset
, m_position
- m_offset
, false, true);
374 bool wxGenericDragImage::UpdateBackingFromWindow(wxDC
& windowDC
, wxMemoryDC
& destDC
,
375 const wxRect
& sourceRect
, const wxRect
& destRect
) const
377 return destDC
.Blit(destRect
.x
, destRect
.y
, destRect
.width
, destRect
.height
, & windowDC
,
378 sourceRect
.x
, sourceRect
.y
);
381 bool wxGenericDragImage::Hide()
383 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Hide()") );
385 // Repair the old position
387 if (m_isShown
&& m_isDirty
)
389 RedrawImage(m_position
- m_offset
, m_position
- m_offset
, true, false);
398 // More efficient: erase and redraw simultaneously if possible
399 bool wxGenericDragImage::RedrawImage(const wxPoint
& oldPos
, const wxPoint
& newPos
,
400 bool eraseOld
, bool drawNew
)
405 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
409 wxRect
oldRect(GetImageRect(oldPos
));
410 wxRect
newRect(GetImageRect(newPos
));
414 // Full rect: the combination of both rects
415 if (eraseOld
&& drawNew
)
417 int oldRight
= oldRect
.GetRight();
418 int oldBottom
= oldRect
.GetBottom();
419 int newRight
= newRect
.GetRight();
420 int newBottom
= newRect
.GetBottom();
422 wxPoint topLeft
= wxPoint(wxMin(oldPos
.x
, newPos
.x
), wxMin(oldPos
.y
, newPos
.y
));
423 wxPoint bottomRight
= wxPoint(wxMax(oldRight
, newRight
), wxMax(oldBottom
, newBottom
));
425 fullRect
.x
= topLeft
.x
; fullRect
.y
= topLeft
.y
;
426 fullRect
.SetRight(bottomRight
.x
);
427 fullRect
.SetBottom(bottomRight
.y
);
434 // Make the bitmap bigger than it need be, so we don't
435 // keep reallocating all the time.
438 if (!m_repairBitmap
.Ok() || (m_repairBitmap
.GetWidth() < fullRect
.GetWidth() || m_repairBitmap
.GetHeight() < fullRect
.GetHeight()))
440 m_repairBitmap
= wxBitmap(fullRect
.GetWidth() + excess
, fullRect
.GetHeight() + excess
);
444 memDC
.SelectObject(* backing
);
446 wxMemoryDC memDCTemp
;
447 memDCTemp
.SelectObject(m_repairBitmap
);
449 // Draw the backing bitmap onto the repair bitmap.
450 // If full-screen, we may have specified the rect on the
451 // screen that we're using for our backing bitmap.
452 // So subtract this when we're blitting from the backing bitmap
453 // (translate from screen to backing-bitmap coords).
455 memDCTemp
.Blit(0, 0, fullRect
.GetWidth(), fullRect
.GetHeight(), & memDC
, fullRect
.x
- m_boundingRect
.x
, fullRect
.y
- m_boundingRect
.y
);
457 // If drawing, draw the image onto the mem DC
460 wxPoint
pos(newPos
.x
- fullRect
.x
, newPos
.y
- fullRect
.y
) ;
461 DoDrawImage(memDCTemp
, pos
);
464 // Now blit to the window
465 // Finally, blit the temp mem DC to the window.
466 m_windowDC
->Blit(fullRect
.x
, fullRect
.y
, fullRect
.width
, fullRect
.height
, & memDCTemp
, 0, 0);
468 memDCTemp
.SelectObject(wxNullBitmap
);
469 memDC
.SelectObject(wxNullBitmap
);
474 // Override this if you are using a virtual image (drawing your own image)
475 bool wxGenericDragImage::DoDrawImage(wxDC
& dc
, const wxPoint
& pos
) const
479 dc
.DrawBitmap(m_bitmap
, pos
.x
, pos
.y
, (m_bitmap
.GetMask() != 0));
482 else if (m_icon
.Ok())
484 dc
.DrawIcon(m_icon
, pos
.x
, pos
.y
);
491 // Override this if you are using a virtual image (drawing your own image)
492 wxRect
wxGenericDragImage::GetImageRect(const wxPoint
& pos
) const
496 return wxRect(pos
.x
, pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight());
498 else if (m_icon
.Ok())
500 return wxRect(pos
.x
, pos
.y
, m_icon
.GetWidth(), m_icon
.GetHeight());
504 return wxRect(pos
.x
, pos
.y
, 0, 0);
508 #endif // wxUSE_DRAGIMAGE