]>
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"
34 #include "wx/window.h"
36 #include "wx/dcclient.h"
37 #include "wx/dcscreen.h"
38 #include "wx/dcmemory.h"
39 #include "wx/settings.h"
46 #define wxUSE_IMAGE_IN_DRAGIMAGE 0
48 #define wxUSE_IMAGE_IN_DRAGIMAGE 1
51 #if wxUSE_IMAGE_IN_DRAGIMAGE
55 #include "wx/generic/dragimgg.h"
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 IMPLEMENT_DYNAMIC_CLASS(wxGenericDragImage
, wxObject
)
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
68 // wxGenericDragImage ctors/dtor
69 // ----------------------------------------------------------------------------
71 wxGenericDragImage::wxGenericDragImage()
76 wxGenericDragImage::~wxGenericDragImage()
84 void wxGenericDragImage::Init()
88 m_windowDC
= (wxDC
*) NULL
;
89 m_window
= (wxWindow
*) NULL
;
94 ////////////////////////////////////////////////////////////////////////////
98 ////////////////////////////////////////////////////////////////////////////
100 // Create a drag image from a bitmap and optional cursor
101 bool wxGenericDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
103 // We don't have to combine the cursor explicitly since we simply show the cursor
104 // as we drag. This currently will only work within one window.
113 // Create a drag image from an icon and optional cursor
114 bool wxGenericDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
116 // We don't have to combine the cursor explicitly since we simply show the cursor
117 // 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
, const wxPoint
& hotspot
)
129 wxFont
font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
134 dc
.GetTextExtent(str
, & w
, & h
);
135 dc
.SetFont(wxNullFont
);
140 // Sometimes GetTextExtent isn't accurate enough, so make it longer
141 wxBitmap
bitmap((int) ((w
+2) * 1.5), (int) h
+2);
142 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
);
164 image
.SetMaskColour(255, 255, 255);
165 bitmap
= image
.ConvertToBitmap();
168 return Create(bitmap
, cursor
, hotspot
);
171 // Create a drag image for the given tree control item
172 bool wxGenericDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
174 wxString str
= treeCtrl
.GetItemText(id
);
178 // Create a drag image for the given list control item
179 bool wxGenericDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
181 wxString str
= listCtrl
.GetItemText(id
);
186 bool wxGenericDragImage::BeginDrag(const wxPoint
& WXUNUSED(hotspot
),
191 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
194 m_fullScreen
= fullScreen
;
197 m_boundingRect
= * rect
;
204 window
->CaptureMouse();
208 m_oldCursor
= window
->GetCursor();
209 window
->SetCursor(m_cursor
);
213 // Make a copy of the window so we can repair damage done as the image is
220 clientSize
= window
->GetClientSize();
221 m_boundingRect
.x
= 0; m_boundingRect
.y
= 0;
222 m_boundingRect
.width
= clientSize
.x
; m_boundingRect
.height
= clientSize
.y
;
227 wxDisplaySize(& w
, & h
);
228 clientSize
.x
= w
; clientSize
.y
= h
;
231 pt
.x
= m_boundingRect
.x
; pt
.y
= m_boundingRect
.y
;
232 clientSize
.x
= m_boundingRect
.width
; clientSize
.y
= m_boundingRect
.height
;
236 m_boundingRect
.x
= 0; m_boundingRect
.y
= 0;
237 m_boundingRect
.width
= w
; m_boundingRect
.height
= h
;
241 if (!m_backingBitmap
.Ok() || (m_backingBitmap
.GetWidth() < clientSize
.x
|| m_backingBitmap
.GetHeight() < clientSize
.y
))
242 m_backingBitmap
= wxBitmap(clientSize
.x
, clientSize
.y
);
245 m_windowDC
= new wxClientDC(window
);
248 m_windowDC
= new wxScreenDC
;
251 // Use m_boundingRect to limit the area considered.
252 ((wxScreenDC
*) m_windowDC
)->StartDrawingOnTop(rect
);
255 m_windowDC
->SetClippingRegion(m_boundingRect
.x
, m_boundingRect
.y
,
256 m_boundingRect
.width
, m_boundingRect
.height
);
262 // Begin drag. hotspot is the location of the drag position relative to the upper-left
263 // corner of the image. This is full screen only. fullScreenRect gives the
264 // position of the window on the screen, to restrict the drag to.
265 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
269 int x
= fullScreenRect
->GetPosition().x
;
270 int y
= fullScreenRect
->GetPosition().y
;
272 wxSize sz
= fullScreenRect
->GetSize();
274 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
275 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
277 rect
.x
= x
; rect
.y
= y
;
278 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
280 return BeginDrag(hotspot
, window
, TRUE
, & rect
);
284 bool wxGenericDragImage::EndDrag()
288 m_window
->ReleaseMouse();
289 if (m_cursor
.Ok() && m_oldCursor
.Ok())
291 m_window
->SetCursor(m_oldCursor
);
297 m_windowDC
->DestroyClippingRegion();
299 m_windowDC
= (wxDC
*) NULL
;
302 m_repairBitmap
= wxNullBitmap
;
307 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
308 // is non-NULL, or in screen coordinates if NULL.
309 bool wxGenericDragImage::Move(const wxPoint
& pt
)
311 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), "No window DC in wxGenericDragImage::Move()" );
313 // Erase at old position, then show at the current position
314 wxPoint oldPos
= m_position
;
316 bool eraseOldImage
= (m_isDirty
&& m_isShown
);
319 RedrawImage(oldPos
, pt
, eraseOldImage
, TRUE
);
329 bool wxGenericDragImage::Show()
331 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), "No window DC in wxGenericDragImage::Show()" );
333 // Show at the current position
337 // This is where we restore the backing bitmap, in case
338 // something has changed on the window.
341 memDC
.SelectObject(m_backingBitmap
);
342 memDC
.Blit(0, 0, m_boundingRect
.width
, m_boundingRect
.height
, m_windowDC
, m_boundingRect
.x
, m_boundingRect
.y
);
343 memDC
.SelectObject(wxNullBitmap
);
345 RedrawImage(m_position
, m_position
, FALSE
, TRUE
);
354 bool wxGenericDragImage::Hide()
356 wxASSERT_MSG( (m_windowDC
!= (wxDC
*) NULL
), "No window DC in wxGenericDragImage::Hide()" );
358 // Repair the old position
360 if (m_isShown
&& m_isDirty
)
362 RedrawImage(m_position
, m_position
, TRUE
, FALSE
);
371 // More efficient: erase and redraw simultaneously if possible
372 bool wxGenericDragImage::RedrawImage(const wxPoint
& oldPos
, const wxPoint
& newPos
,
373 bool eraseOld
, bool drawNew
)
378 if (!m_backingBitmap
.Ok())
381 wxRect
oldRect(GetImageRect(oldPos
));
382 wxRect
newRect(GetImageRect(newPos
));
386 // Full rect: the combination of both rects
387 if (eraseOld
&& drawNew
)
389 int oldRight
= oldRect
.GetRight();
390 int oldBottom
= oldRect
.GetBottom();
391 int newRight
= newRect
.GetRight();
392 int newBottom
= newRect
.GetBottom();
394 wxPoint topLeft
= wxPoint(wxMin(oldPos
.x
, newPos
.x
), wxMin(oldPos
.y
, newPos
.y
));
395 wxPoint bottomRight
= wxPoint(wxMax(oldRight
, newRight
), wxMax(oldBottom
, newBottom
));
397 fullRect
.x
= topLeft
.x
; fullRect
.y
= topLeft
.y
;
398 fullRect
.SetRight(bottomRight
.x
);
399 fullRect
.SetBottom(bottomRight
.y
);
406 // Make the bitmap bigger than it need be, so we don't
407 // keep reallocating all the time.
410 if (!m_repairBitmap
.Ok() || (m_repairBitmap
.GetWidth() < fullRect
.GetWidth() || m_repairBitmap
.GetHeight() < fullRect
.GetHeight()))
412 m_repairBitmap
= wxBitmap(fullRect
.GetWidth() + excess
, fullRect
.GetHeight() + excess
);
416 memDC
.SelectObject(m_backingBitmap
);
418 wxMemoryDC memDCTemp
;
419 memDCTemp
.SelectObject(m_repairBitmap
);
421 // Draw the backing bitmap onto the repair bitmap.
422 // If full-screen, we may have specified the rect on the
423 // screen that we're using for our backing bitmap.
424 // So subtract this when we're blitting from the backing bitmap
425 // (translate from screen to backing-bitmap coords).
427 memDCTemp
.Blit(0, 0, fullRect
.GetWidth(), fullRect
.GetHeight(), & memDC
, fullRect
.x
- m_boundingRect
.x
, fullRect
.y
- m_boundingRect
.y
);
429 // If drawing, draw the image onto the mem DC
432 int x
= newPos
.x
- fullRect
.x
;
433 int y
= newPos
.y
- fullRect
.y
;
435 memDCTemp
.DrawBitmap(m_bitmap
, x
, y
, (m_bitmap
.GetMask() != 0));
436 else if (m_icon
.Ok())
437 memDCTemp
.DrawIcon(m_icon
, x
, y
);
440 // Now blit to the window
441 // Finally, blit the temp mem DC to the window.
442 m_windowDC
->Blit(fullRect
.x
, fullRect
.y
, fullRect
.width
, fullRect
.height
, & memDCTemp
, 0, 0);
444 memDCTemp
.SelectObject(wxNullBitmap
);
445 memDC
.SelectObject(wxNullBitmap
);
450 wxRect
wxGenericDragImage::GetImageRect(const wxPoint
& pos
) const
454 return wxRect(pos
.x
, pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight());
456 else if (m_icon
.Ok())
458 return wxRect(pos
.x
, pos
.y
, m_icon
.GetWidth(), m_icon
.GetHeight());
462 return wxRect(pos
.x
, pos
.y
, 0, 0);