]>
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" 
  42 #define wxUSE_IMAGE_IN_DRAGIMAGE 1 
  44 #if wxUSE_IMAGE_IN_DRAGIMAGE 
  48 #include "wx/generic/dragimgg.h" 
  50 // ---------------------------------------------------------------------------- 
  52 // ---------------------------------------------------------------------------- 
  54 IMPLEMENT_DYNAMIC_CLASS(wxGenericDragImage
, wxObject
) 
  56 // ============================================================================ 
  58 // ============================================================================ 
  60 // ---------------------------------------------------------------------------- 
  61 // wxGenericDragImage ctors/dtor 
  62 // ---------------------------------------------------------------------------- 
  64 wxGenericDragImage::~wxGenericDragImage() 
  72 void wxGenericDragImage::Init() 
  76     m_windowDC 
= (wxDC
*) NULL
; 
  77     m_window 
= (wxWindow
*) NULL
; 
  79     m_pBackingBitmap 
= (wxBitmap
*) NULL
; 
  83 //////////////////////////////////////////////////////////////////////////// 
  87 //////////////////////////////////////////////////////////////////////////// 
  89 // Create a drag image with a virtual image (need to override DoDrawImage, GetImageRect) 
  90 bool wxGenericDragImage::Create(const wxCursor
& cursor
) 
  97 // Create a drag image from a bitmap and optional cursor 
  98 bool wxGenericDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
) 
 100     // We don't have to combine the cursor explicitly since we simply show the cursor 
 101     // as we drag. This currently will only work within one window. 
 109 // Create a drag image from an icon and optional cursor 
 110 bool wxGenericDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
) 
 112     // We don't have to combine the cursor explicitly since we simply show the cursor 
 113     // as we drag. This currently will only work within one window. 
 121 // Create a drag image from a string and optional cursor 
 122 bool wxGenericDragImage::Create(const wxString
& str
, const wxCursor
& cursor
) 
 124     wxFont 
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
)); 
 129     dc
.GetTextExtent(str
, & w
, & h
); 
 130     dc
.SetFont(wxNullFont
); 
 134     // Sometimes GetTextExtent isn't accurate enough, so make it longer 
 135     wxBitmap 
bitmap((int) ((w
+2) * 1.5), (int) h
+2); 
 136     dc2
.SelectObject(bitmap
); 
 139     dc2
.SetBackground(* wxWHITE_BRUSH
); 
 141     dc2
.SetBackgroundMode(wxTRANSPARENT
); 
 142     dc2
.SetTextForeground(* wxLIGHT_GREY
); 
 143     dc2
.DrawText(str
, 0, 0); 
 144     dc2
.DrawText(str
, 1, 0); 
 145     dc2
.DrawText(str
, 2, 0); 
 146     dc2
.DrawText(str
, 1, 1); 
 147     dc2
.DrawText(str
, 2, 1); 
 148     dc2
.DrawText(str
, 1, 2); 
 149     dc2
.DrawText(str
, 2, 2); 
 151     dc2
.SetTextForeground(* wxBLACK
); 
 152     dc2
.DrawText(str
, 1, 1); 
 154     dc2
.SelectObject(wxNullBitmap
); 
 156 #if wxUSE_IMAGE_IN_DRAGIMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB) 
 157     // Make the bitmap masked 
 158     wxImage image 
= bitmap
.ConvertToImage(); 
 159     image
.SetMaskColour(255, 255, 255); 
 160     bitmap 
= wxBitmap(image
); 
 163     return Create(bitmap
, cursor
); 
 167 // Create a drag image for the given tree control item 
 168 bool wxGenericDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
) 
 170     wxString str 
= treeCtrl
.GetItemText(id
); 
 176 // Create a drag image for the given list control item 
 177 bool wxGenericDragImage::Create(const wxListCtrl
& listCtrl
, long id
) 
 179     wxString str 
= listCtrl
.GetItemText(id
); 
 185 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
, 
 190     wxASSERT_MSG( (window 
!= 0), wxT("Window must not be null in BeginDrag.")); 
 192     // The image should be offset by this amount 
 195     m_fullScreen 
= fullScreen
; 
 198         m_boundingRect 
= * rect
; 
 205         window
->CaptureMouse(); 
 209             m_oldCursor 
= window
->GetCursor(); 
 210             window
->SetCursor(m_cursor
); 
 214     // Make a copy of the window so we can repair damage done as the image is 
 221         clientSize 
= window
->GetClientSize(); 
 222         m_boundingRect
.x 
= 0; m_boundingRect
.y 
= 0; 
 223         m_boundingRect
.width 
= clientSize
.x
; m_boundingRect
.height 
= clientSize
.y
; 
 228         wxDisplaySize(& w
, & h
); 
 229         clientSize
.x 
= w
; clientSize
.y 
= h
; 
 232             pt
.x 
= m_boundingRect
.x
; pt
.y 
= m_boundingRect
.y
; 
 233             clientSize
.x 
= m_boundingRect
.width
; clientSize
.y 
= m_boundingRect
.height
; 
 237             m_boundingRect
.x 
= 0; m_boundingRect
.y 
= 0; 
 238             m_boundingRect
.width 
= w
; m_boundingRect
.height 
= h
; 
 242     wxBitmap
* backing 
= (m_pBackingBitmap 
? m_pBackingBitmap 
: (wxBitmap
*) & m_backingBitmap
); 
 244     if (!backing
->Ok() || (backing
->GetWidth() < clientSize
.x 
|| backing
->GetHeight() < clientSize
.y
)) 
 245         (*backing
) = wxBitmap(clientSize
.x
, clientSize
.y
); 
 249         m_windowDC 
= new wxClientDC(window
); 
 253         m_windowDC 
= new wxScreenDC
; 
 256         // Use m_boundingRect to limit the area considered. 
 257         ((wxScreenDC
*) m_windowDC
)->StartDrawingOnTop(rect
); 
 260         m_windowDC
->SetClippingRegion(m_boundingRect
.x
, m_boundingRect
.y
, 
 261             m_boundingRect
.width
, m_boundingRect
.height
); 
 267 // Begin drag. hotspot is the location of the drag position relative to the upper-left 
 268 // corner of the image. This is full screen only. fullScreenRect gives the 
 269 // position of the window on the screen, to restrict the drag to. 
 270 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
) 
 274     int x 
= fullScreenRect
->GetPosition().x
; 
 275     int y 
= fullScreenRect
->GetPosition().y
; 
 277     wxSize sz 
= fullScreenRect
->GetSize(); 
 279     if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
))) 
 280         fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
); 
 282     rect
.x 
= x
; rect
.y 
= y
; 
 283     rect
.width 
= sz
.x
; rect
.height 
= sz
.y
; 
 285     return BeginDrag(hotspot
, window
, true, & rect
); 
 289 bool wxGenericDragImage::EndDrag() 
 294         // Under Windows we can be pretty sure this test will give 
 295         // the correct results 
 296         if (wxWindow::GetCapture() == m_window
) 
 298             m_window
->ReleaseMouse(); 
 300         if (m_cursor
.Ok() && m_oldCursor
.Ok()) 
 302             m_window
->SetCursor(m_oldCursor
); 
 308         m_windowDC
->DestroyClippingRegion(); 
 310         m_windowDC 
= (wxDC
*) NULL
; 
 313     m_repairBitmap 
= wxNullBitmap
; 
 318 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window 
 319 // is non-NULL, or in screen coordinates if NULL. 
 320 bool wxGenericDragImage::Move(const wxPoint
& pt
) 
 322     wxASSERT_MSG( (m_windowDC 
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Move()") ); 
 326         pt2 
= m_window
->ClientToScreen(pt
); 
 328     // Erase at old position, then show at the current position 
 329     wxPoint oldPos 
= m_position
; 
 331     bool eraseOldImage 
= (m_isDirty 
&& m_isShown
); 
 334         RedrawImage(oldPos 
- m_offset
, pt2 
- m_offset
, eraseOldImage
, true); 
 344 bool wxGenericDragImage::Show() 
 346     wxASSERT_MSG( (m_windowDC 
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Show()") ); 
 348     // Show at the current position 
 352         // This is where we restore the backing bitmap, in case 
 353         // something has changed on the window. 
 355         wxBitmap
* backing 
= (m_pBackingBitmap 
? m_pBackingBitmap 
: (wxBitmap
*) & m_backingBitmap
); 
 357         memDC
.SelectObject(* backing
); 
 359         UpdateBackingFromWindow(* m_windowDC
, memDC
, m_boundingRect
, wxRect(0, 0, m_boundingRect
.width
, m_boundingRect
.height
)); 
 361         //memDC.Blit(0, 0, m_boundingRect.width, m_boundingRect.height, m_windowDC, m_boundingRect.x, m_boundingRect.y); 
 362         memDC
.SelectObject(wxNullBitmap
); 
 364         RedrawImage(m_position 
- m_offset
, m_position 
- m_offset
, false, true); 
 373 bool wxGenericDragImage::UpdateBackingFromWindow(wxDC
& windowDC
, wxMemoryDC
& destDC
, 
 374     const wxRect
& sourceRect
, const wxRect
& destRect
) const 
 376     return destDC
.Blit(destRect
.x
, destRect
.y
, destRect
.width
, destRect
.height
, & windowDC
, 
 377         sourceRect
.x
, sourceRect
.y
); 
 380 bool wxGenericDragImage::Hide() 
 382     wxASSERT_MSG( (m_windowDC 
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Hide()") ); 
 384     // Repair the old position 
 386     if (m_isShown 
&& m_isDirty
) 
 388         RedrawImage(m_position 
- m_offset
, m_position 
- m_offset
, true, false); 
 397 // More efficient: erase and redraw simultaneously if possible 
 398 bool wxGenericDragImage::RedrawImage(const wxPoint
& oldPos
, const wxPoint
& newPos
, 
 399                                      bool eraseOld
, bool drawNew
) 
 404     wxBitmap
* backing 
= (m_pBackingBitmap 
? m_pBackingBitmap 
: (wxBitmap
*) & m_backingBitmap
); 
 408     wxRect 
oldRect(GetImageRect(oldPos
)); 
 409     wxRect 
newRect(GetImageRect(newPos
)); 
 413     // Full rect: the combination of both rects 
 414     if (eraseOld 
&& drawNew
) 
 416         int oldRight 
= oldRect
.GetRight(); 
 417         int oldBottom 
= oldRect
.GetBottom(); 
 418         int newRight 
= newRect
.GetRight(); 
 419         int newBottom 
= newRect
.GetBottom(); 
 421         wxPoint topLeft 
= wxPoint(wxMin(oldPos
.x
, newPos
.x
), wxMin(oldPos
.y
, newPos
.y
)); 
 422         wxPoint bottomRight 
= wxPoint(wxMax(oldRight
, newRight
), wxMax(oldBottom
, newBottom
)); 
 424         fullRect
.x 
= topLeft
.x
; fullRect
.y 
= topLeft
.y
; 
 425         fullRect
.SetRight(bottomRight
.x
); 
 426         fullRect
.SetBottom(bottomRight
.y
); 
 433     // Make the bitmap bigger than it need be, so we don't 
 434     // keep reallocating all the time. 
 437     if (!m_repairBitmap
.Ok() || (m_repairBitmap
.GetWidth() < fullRect
.GetWidth() || m_repairBitmap
.GetHeight() < fullRect
.GetHeight())) 
 439         m_repairBitmap 
= wxBitmap(fullRect
.GetWidth() + excess
, fullRect
.GetHeight() + excess
); 
 443     memDC
.SelectObject(* backing
); 
 445     wxMemoryDC memDCTemp
; 
 446     memDCTemp
.SelectObject(m_repairBitmap
); 
 448     // Draw the backing bitmap onto the repair bitmap. 
 449     // If full-screen, we may have specified the rect on the 
 450     // screen that we're using for our backing bitmap. 
 451     // So subtract this when we're blitting from the backing bitmap 
 452     // (translate from screen to backing-bitmap coords). 
 454     memDCTemp
.Blit(0, 0, fullRect
.GetWidth(), fullRect
.GetHeight(), & memDC
, fullRect
.x 
- m_boundingRect
.x
, fullRect
.y 
- m_boundingRect
.y
); 
 456     // If drawing, draw the image onto the mem DC 
 459         wxPoint 
pos(newPos
.x 
- fullRect
.x
, newPos
.y 
- fullRect
.y
) ; 
 460         DoDrawImage(memDCTemp
, pos
); 
 463     // Now blit to the window 
 464     // Finally, blit the temp mem DC to the window. 
 465     m_windowDC
->Blit(fullRect
.x
, fullRect
.y
, fullRect
.width
, fullRect
.height
, & memDCTemp
, 0, 0); 
 467     memDCTemp
.SelectObject(wxNullBitmap
); 
 468     memDC
.SelectObject(wxNullBitmap
); 
 473 // Override this if you are using a virtual image (drawing your own image) 
 474 bool wxGenericDragImage::DoDrawImage(wxDC
& dc
, const wxPoint
& pos
) const 
 478         dc
.DrawBitmap(m_bitmap
, pos
.x
, pos
.y
, (m_bitmap
.GetMask() != 0)); 
 481     else if (m_icon
.Ok()) 
 483         dc
.DrawIcon(m_icon
, pos
.x
, pos
.y
); 
 490 // Override this if you are using a virtual image (drawing your own image) 
 491 wxRect 
wxGenericDragImage::GetImageRect(const wxPoint
& pos
) const 
 495         return wxRect(pos
.x
, pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight()); 
 497     else if (m_icon
.Ok()) 
 499         return wxRect(pos
.x
, pos
.y
, m_icon
.GetWidth(), m_icon
.GetHeight()); 
 503         return wxRect(pos
.x
, pos
.y
, 0, 0); 
 507 #endif // wxUSE_DRAGIMAGE