]>
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 // ----------------------------------------------------------------------------
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"
48 #define wxUSE_IMAGE_IN_DRAGIMAGE 0
50 #define wxUSE_IMAGE_IN_DRAGIMAGE 1
53 #if wxUSE_IMAGE_IN_DRAGIMAGE
57 #include "wx/generic/dragimgg.h"
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 IMPLEMENT_DYNAMIC_CLASS(wxGenericDragImage
, wxObject
)
65 // ============================================================================
67 // ============================================================================
69 // ----------------------------------------------------------------------------
70 // wxGenericDragImage ctors/dtor
71 // ----------------------------------------------------------------------------
73 wxGenericDragImage::~wxGenericDragImage()
81 void wxGenericDragImage::Init()
85 m_windowDC
= (wxDC
*) NULL
;
86 m_window
= (wxWindow
*) NULL
;
88 m_pBackingBitmap
= (wxBitmap
*) NULL
;
92 ////////////////////////////////////////////////////////////////////////////
96 ////////////////////////////////////////////////////////////////////////////
98 // Create a drag image with a virtual image (need to override DoDrawImage, GetImageRect)
99 bool wxGenericDragImage::Create(const wxCursor
& cursor
)
106 // Create a drag image from a bitmap and optional cursor
107 bool wxGenericDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
)
109 // We don't have to combine the cursor explicitly since we simply show the cursor
110 // as we drag. This currently will only work within one window.
118 // Create a drag image from an icon and optional cursor
119 bool wxGenericDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
)
121 // We don't have to combine the cursor explicitly since we simply show the cursor
122 // as we drag. This currently will only work within one window.
130 // Create a drag image from a string and optional cursor
131 bool wxGenericDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
133 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
138 dc
.GetTextExtent(str
, & w
, & h
);
139 dc
.SetFont(wxNullFont
);
143 // Sometimes GetTextExtent isn't accurate enough, so make it longer
144 wxBitmap
bitmap((int) ((w
+2) * 1.5), (int) h
+2);
145 dc2
.SelectObject(bitmap
);
148 dc2
.SetBackground(* wxWHITE_BRUSH
);
150 dc2
.SetBackgroundMode(wxTRANSPARENT
);
151 dc2
.SetTextForeground(* wxLIGHT_GREY
);
152 dc2
.DrawText(str
, 0, 0);
153 dc2
.DrawText(str
, 1, 0);
154 dc2
.DrawText(str
, 2, 0);
155 dc2
.DrawText(str
, 1, 1);
156 dc2
.DrawText(str
, 2, 1);
157 dc2
.DrawText(str
, 1, 2);
158 dc2
.DrawText(str
, 2, 2);
160 dc2
.SetTextForeground(* wxBLACK
);
161 dc2
.DrawText(str
, 1, 1);
163 dc2
.SelectObject(wxNullBitmap
);
165 #if wxUSE_IMAGE_IN_DRAGIMAGE
166 // Make the bitmap masked
167 wxImage image
= bitmap
.ConvertToImage();
168 image
.SetMaskColour(255, 255, 255);
169 bitmap
= wxBitmap(image
);
172 return Create(bitmap
, cursor
);
176 // Create a drag image for the given tree control item
177 bool wxGenericDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
179 wxString str
= treeCtrl
.GetItemText(id
);
185 // Create a drag image for the given list control item
186 bool wxGenericDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
188 wxString str
= listCtrl
.GetItemText(id
);
194 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
,
199 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
201 // The image should be offset by this amount
204 m_fullScreen
= fullScreen
;
207 m_boundingRect
= * rect
;
214 window
->CaptureMouse();
218 m_oldCursor
= window
->GetCursor();
219 window
->SetCursor(m_cursor
);
223 // Make a copy of the window so we can repair damage done as the image is
230 clientSize
= window
->GetClientSize();
231 m_boundingRect
.x
= 0; m_boundingRect
.y
= 0;
232 m_boundingRect
.width
= clientSize
.x
; m_boundingRect
.height
= clientSize
.y
;
237 wxDisplaySize(& w
, & h
);
238 clientSize
.x
= w
; clientSize
.y
= h
;
241 pt
.x
= m_boundingRect
.x
; pt
.y
= m_boundingRect
.y
;
242 clientSize
.x
= m_boundingRect
.width
; clientSize
.y
= m_boundingRect
.height
;
246 m_boundingRect
.x
= 0; m_boundingRect
.y
= 0;
247 m_boundingRect
.width
= w
; m_boundingRect
.height
= h
;
251 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
253 if (!backing
->Ok() || (backing
->GetWidth() < clientSize
.x
|| backing
->GetHeight() < clientSize
.y
))
254 (*backing
) = wxBitmap(clientSize
.x
, clientSize
.y
);
258 m_windowDC
= new wxClientDC(window
);
262 m_windowDC
= new wxScreenDC
;
265 // Use m_boundingRect to limit the area considered.
266 ((wxScreenDC
*) m_windowDC
)->StartDrawingOnTop(rect
);
269 m_windowDC
->SetClippingRegion(m_boundingRect
.x
, m_boundingRect
.y
,
270 m_boundingRect
.width
, m_boundingRect
.height
);
276 // Begin drag. hotspot is the location of the drag position relative to the upper-left
277 // corner of the image. This is full screen only. fullScreenRect gives the
278 // position of the window on the screen, to restrict the drag to.
279 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
283 int x
= fullScreenRect
->GetPosition().x
;
284 int y
= fullScreenRect
->GetPosition().y
;
286 wxSize sz
= fullScreenRect
->GetSize();
288 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
289 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
291 rect
.x
= x
; rect
.y
= y
;
292 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
294 return BeginDrag(hotspot
, window
, TRUE
, & rect
);
298 bool wxGenericDragImage::EndDrag()
303 // Under Windows we can be pretty sure this test will give
304 // the correct results
305 if (wxWindow::GetCapture() == m_window
)
307 m_window
->ReleaseMouse();
309 if (m_cursor
.Ok() && m_oldCursor
.Ok())
311 m_window
->SetCursor(m_oldCursor
);
317 m_windowDC
->DestroyClippingRegion();
319 m_windowDC
= (wxDC
*) NULL
;
322 m_repairBitmap
= wxNullBitmap
;
327 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
328 // is non-NULL, or in screen coordinates if NULL.
329 bool wxGenericDragImage::Move(const wxPoint
& pt
)
331 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Move()") );
335 pt2
= m_window
->ClientToScreen(pt
);
337 // Erase at old position, then show at the current position
338 wxPoint oldPos
= m_position
;
340 bool eraseOldImage
= (m_isDirty
&& m_isShown
);
343 RedrawImage(oldPos
- m_offset
, pt2
- m_offset
, eraseOldImage
, TRUE
);
353 bool wxGenericDragImage::Show()
355 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Show()") );
357 // Show at the current position
361 // This is where we restore the backing bitmap, in case
362 // something has changed on the window.
364 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
366 memDC
.SelectObject(* backing
);
368 UpdateBackingFromWindow(* m_windowDC
, memDC
, m_boundingRect
, wxRect(0, 0, m_boundingRect
.width
, m_boundingRect
.height
));
370 //memDC.Blit(0, 0, m_boundingRect.width, m_boundingRect.height, m_windowDC, m_boundingRect.x, m_boundingRect.y);
371 memDC
.SelectObject(wxNullBitmap
);
373 RedrawImage(m_position
- m_offset
, m_position
- m_offset
, FALSE
, TRUE
);
382 bool wxGenericDragImage::UpdateBackingFromWindow(wxDC
& windowDC
, wxMemoryDC
& destDC
,
383 const wxRect
& sourceRect
, const wxRect
& destRect
) const
385 return destDC
.Blit(destRect
.x
, destRect
.y
, destRect
.width
, destRect
.height
, & windowDC
,
386 sourceRect
.x
, sourceRect
.y
);
389 bool wxGenericDragImage::Hide()
391 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Hide()") );
393 // Repair the old position
395 if (m_isShown
&& m_isDirty
)
397 RedrawImage(m_position
- m_offset
, m_position
- m_offset
, TRUE
, FALSE
);
406 // More efficient: erase and redraw simultaneously if possible
407 bool wxGenericDragImage::RedrawImage(const wxPoint
& oldPos
, const wxPoint
& newPos
,
408 bool eraseOld
, bool drawNew
)
413 wxBitmap
* backing
= (m_pBackingBitmap
? m_pBackingBitmap
: (wxBitmap
*) & m_backingBitmap
);
417 wxRect
oldRect(GetImageRect(oldPos
));
418 wxRect
newRect(GetImageRect(newPos
));
422 // Full rect: the combination of both rects
423 if (eraseOld
&& drawNew
)
425 int oldRight
= oldRect
.GetRight();
426 int oldBottom
= oldRect
.GetBottom();
427 int newRight
= newRect
.GetRight();
428 int newBottom
= newRect
.GetBottom();
430 wxPoint topLeft
= wxPoint(wxMin(oldPos
.x
, newPos
.x
), wxMin(oldPos
.y
, newPos
.y
));
431 wxPoint bottomRight
= wxPoint(wxMax(oldRight
, newRight
), wxMax(oldBottom
, newBottom
));
433 fullRect
.x
= topLeft
.x
; fullRect
.y
= topLeft
.y
;
434 fullRect
.SetRight(bottomRight
.x
);
435 fullRect
.SetBottom(bottomRight
.y
);
442 // Make the bitmap bigger than it need be, so we don't
443 // keep reallocating all the time.
446 if (!m_repairBitmap
.Ok() || (m_repairBitmap
.GetWidth() < fullRect
.GetWidth() || m_repairBitmap
.GetHeight() < fullRect
.GetHeight()))
448 m_repairBitmap
= wxBitmap(fullRect
.GetWidth() + excess
, fullRect
.GetHeight() + excess
);
452 memDC
.SelectObject(* backing
);
454 wxMemoryDC memDCTemp
;
455 memDCTemp
.SelectObject(m_repairBitmap
);
457 // Draw the backing bitmap onto the repair bitmap.
458 // If full-screen, we may have specified the rect on the
459 // screen that we're using for our backing bitmap.
460 // So subtract this when we're blitting from the backing bitmap
461 // (translate from screen to backing-bitmap coords).
463 memDCTemp
.Blit(0, 0, fullRect
.GetWidth(), fullRect
.GetHeight(), & memDC
, fullRect
.x
- m_boundingRect
.x
, fullRect
.y
- m_boundingRect
.y
);
465 // If drawing, draw the image onto the mem DC
468 wxPoint
pos(newPos
.x
- fullRect
.x
, newPos
.y
- fullRect
.y
) ;
469 DoDrawImage(memDCTemp
, pos
);
472 // Now blit to the window
473 // Finally, blit the temp mem DC to the window.
474 m_windowDC
->Blit(fullRect
.x
, fullRect
.y
, fullRect
.width
, fullRect
.height
, & memDCTemp
, 0, 0);
476 memDCTemp
.SelectObject(wxNullBitmap
);
477 memDC
.SelectObject(wxNullBitmap
);
482 // Override this if you are using a virtual image (drawing your own image)
483 bool wxGenericDragImage::DoDrawImage(wxDC
& dc
, const wxPoint
& pos
) const
487 dc
.DrawBitmap(m_bitmap
, pos
.x
, pos
.y
, (m_bitmap
.GetMask() != 0));
490 else if (m_icon
.Ok())
492 dc
.DrawIcon(m_icon
, pos
.x
, pos
.y
);
499 // Override this if you are using a virtual image (drawing your own image)
500 wxRect
wxGenericDragImage::GetImageRect(const wxPoint
& pos
) const
504 return wxRect(pos
.x
, pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight());
506 else if (m_icon
.Ok())
508 return wxRect(pos
.x
, pos
.y
, m_icon
.GetWidth(), m_icon
.GetHeight());
512 return wxRect(pos
.x
, pos
.y
, 0, 0);
516 #endif // wxUSE_DRAGIMAGE