]>
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() 
  79 void wxGenericDragImage::Init() 
  83     m_windowDC 
= (wxDC
*) NULL
; 
  84     m_window 
= (wxWindow
*) NULL
; 
  86     m_pBackingBitmap 
= (wxBitmap
*) NULL
; 
  90 //////////////////////////////////////////////////////////////////////////// 
  94 //////////////////////////////////////////////////////////////////////////// 
  96 // Create a drag image with a virtual image (need to override DoDrawImage, GetImageRect) 
  97 bool wxGenericDragImage::Create(const wxCursor
& cursor
, const wxPoint
& hotspot
) 
 105 // Create a drag image from a bitmap and optional cursor 
 106 bool wxGenericDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& hotspot
) 
 108     // We don't have to combine the cursor explicitly since we simply show the cursor 
 109     // 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
, const wxPoint
& hotspot
) 
 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. 
 131 // Create a drag image from a string and optional cursor 
 132 bool wxGenericDragImage::Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& hotspot
) 
 134     wxFont 
font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
)); 
 139     dc
.GetTextExtent(str
, & w
, & h
); 
 140     dc
.SetFont(wxNullFont
); 
 145     // Sometimes GetTextExtent isn't accurate enough, so make it longer 
 146     wxBitmap 
bitmap((int) ((w
+2) * 1.5), (int) h
+2); 
 147     dc2
.SelectObject(bitmap
); 
 149     dc2
.SetBackground(* wxWHITE_BRUSH
); 
 151     dc2
.SetBackgroundMode(wxTRANSPARENT
); 
 152     dc2
.SetTextForeground(* wxLIGHT_GREY
); 
 153     dc2
.DrawText(str
, 0, 0); 
 154     dc2
.DrawText(str
, 1, 0); 
 155     dc2
.DrawText(str
, 2, 0); 
 156     dc2
.DrawText(str
, 1, 1); 
 157     dc2
.DrawText(str
, 2, 1); 
 158     dc2
.DrawText(str
, 1, 2); 
 159     dc2
.DrawText(str
, 2, 2); 
 161     dc2
.SetTextForeground(* wxBLACK
); 
 162     dc2
.DrawText(str
, 1, 1); 
 164     dc2
.SelectObject(wxNullBitmap
); 
 166 #if wxUSE_IMAGE_IN_DRAGIMAGE 
 167     // Make the bitmap masked 
 168     wxImage 
image(bitmap
); 
 169     image
.SetMaskColour(255, 255, 255); 
 170     bitmap 
= image
.ConvertToBitmap(); 
 173     return Create(bitmap
, cursor
, hotspot
); 
 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
); 
 183 // Create a drag image for the given list control item 
 184 bool wxGenericDragImage::Create(const wxListCtrl
& listCtrl
, long id
) 
 186     wxString str 
= listCtrl
.GetItemText(id
); 
 191 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
, 
 196     wxASSERT_MSG( (window 
!= 0), wxT("Window must not be null in BeginDrag.")); 
 198     // The image should be offset by this amount 
 201     m_fullScreen 
= fullScreen
; 
 204         m_boundingRect 
= * rect
; 
 211         window
->CaptureMouse(); 
 215             m_oldCursor 
= window
->GetCursor(); 
 216             window
->SetCursor(m_cursor
); 
 220     // Make a copy of the window so we can repair damage done as the image is 
 227         clientSize 
= window
->GetClientSize(); 
 228         m_boundingRect
.x 
= 0; m_boundingRect
.y 
= 0; 
 229         m_boundingRect
.width 
= clientSize
.x
; m_boundingRect
.height 
= clientSize
.y
; 
 234         wxDisplaySize(& w
, & h
); 
 235         clientSize
.x 
= w
; clientSize
.y 
= h
; 
 238             pt
.x 
= m_boundingRect
.x
; pt
.y 
= m_boundingRect
.y
; 
 239             clientSize
.x 
= m_boundingRect
.width
; clientSize
.y 
= m_boundingRect
.height
; 
 243             m_boundingRect
.x 
= 0; m_boundingRect
.y 
= 0; 
 244             m_boundingRect
.width 
= w
; m_boundingRect
.height 
= h
; 
 248     wxBitmap
* backing 
= (m_pBackingBitmap 
? m_pBackingBitmap 
: (wxBitmap
*) & m_backingBitmap
); 
 250     if (!backing
->Ok() || (backing
->GetWidth() < clientSize
.x 
|| backing
->GetHeight() < clientSize
.y
)) 
 251         (*backing
) = wxBitmap(clientSize
.x
, clientSize
.y
); 
 254         m_windowDC 
= new wxClientDC(window
); 
 257         m_windowDC 
= new wxScreenDC
; 
 260         // Use m_boundingRect to limit the area considered. 
 261         ((wxScreenDC
*) m_windowDC
)->StartDrawingOnTop(rect
); 
 264         m_windowDC
->SetClippingRegion(m_boundingRect
.x
, m_boundingRect
.y
, 
 265             m_boundingRect
.width
, m_boundingRect
.height
); 
 271 // Begin drag. hotspot is the location of the drag position relative to the upper-left 
 272 // corner of the image. This is full screen only. fullScreenRect gives the 
 273 // position of the window on the screen, to restrict the drag to. 
 274 bool wxGenericDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
) 
 278     int x 
= fullScreenRect
->GetPosition().x
; 
 279     int y 
= fullScreenRect
->GetPosition().y
; 
 281     wxSize sz 
= fullScreenRect
->GetSize(); 
 283     if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
))) 
 284         fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
); 
 286     rect
.x 
= x
; rect
.y 
= y
; 
 287     rect
.width 
= sz
.x
; rect
.height 
= sz
.y
; 
 289     return BeginDrag(hotspot
, window
, TRUE
, & rect
); 
 293 bool wxGenericDragImage::EndDrag() 
 297         m_window
->ReleaseMouse(); 
 298         if (m_cursor
.Ok() && m_oldCursor
.Ok()) 
 300             m_window
->SetCursor(m_oldCursor
); 
 306         m_windowDC
->DestroyClippingRegion(); 
 308         m_windowDC 
= (wxDC
*) NULL
; 
 311     m_repairBitmap 
= wxNullBitmap
; 
 316 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window 
 317 // is non-NULL, or in screen coordinates if NULL. 
 318 bool wxGenericDragImage::Move(const wxPoint
& pt
) 
 320     wxASSERT_MSG( (m_windowDC 
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Move()") ); 
 322     // Erase at old position, then show at the current position 
 323     wxPoint oldPos 
= m_position
; 
 325     bool eraseOldImage 
= (m_isDirty 
&& m_isShown
); 
 328         RedrawImage(oldPos 
- m_offset
, pt 
- m_offset
, eraseOldImage
, TRUE
); 
 338 bool wxGenericDragImage::Show() 
 340     wxASSERT_MSG( (m_windowDC 
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Show()") ); 
 342     // Show at the current position 
 346         // This is where we restore the backing bitmap, in case 
 347         // something has changed on the window. 
 349         wxBitmap
* backing 
= (m_pBackingBitmap 
? m_pBackingBitmap 
: (wxBitmap
*) & m_backingBitmap
); 
 351         memDC
.SelectObject(* backing
); 
 353         UpdateBackingFromWindow(* m_windowDC
, memDC
, m_boundingRect
, wxRect(0, 0, m_boundingRect
.width
, m_boundingRect
.height
)); 
 355         //memDC.Blit(0, 0, m_boundingRect.width, m_boundingRect.height, m_windowDC, m_boundingRect.x, m_boundingRect.y); 
 356         memDC
.SelectObject(wxNullBitmap
); 
 358         RedrawImage(m_position 
- m_offset
, m_position 
- m_offset
, FALSE
, TRUE
); 
 367 bool wxGenericDragImage::UpdateBackingFromWindow(wxDC
& windowDC
, wxMemoryDC
& destDC
, 
 368     const wxRect
& sourceRect
, const wxRect
& destRect
) const 
 370     return destDC
.Blit(destRect
.x
, destRect
.y
, destRect
.width
, destRect
.height
, & windowDC
, sourceRect
.x
, sourceRect
.y
); 
 373 bool wxGenericDragImage::Hide() 
 375     wxASSERT_MSG( (m_windowDC 
!= (wxDC
*) NULL
), wxT("No window DC in wxGenericDragImage::Hide()") ); 
 377     // Repair the old position 
 379     if (m_isShown 
&& m_isDirty
) 
 381         RedrawImage(m_position 
- m_offset
, m_position 
- m_offset
, TRUE
, FALSE
); 
 390 // More efficient: erase and redraw simultaneously if possible 
 391 bool wxGenericDragImage::RedrawImage(const wxPoint
& oldPos
, const wxPoint
& newPos
, 
 392                                      bool eraseOld
, bool drawNew
) 
 397     wxBitmap
* backing 
= (m_pBackingBitmap 
? m_pBackingBitmap 
: (wxBitmap
*) & m_backingBitmap
); 
 401     wxRect 
oldRect(GetImageRect(oldPos
)); 
 402     wxRect 
newRect(GetImageRect(newPos
)); 
 406     // Full rect: the combination of both rects 
 407     if (eraseOld 
&& drawNew
) 
 409         int oldRight 
= oldRect
.GetRight(); 
 410         int oldBottom 
= oldRect
.GetBottom(); 
 411         int newRight 
= newRect
.GetRight(); 
 412         int newBottom 
= newRect
.GetBottom(); 
 414         wxPoint topLeft 
= wxPoint(wxMin(oldPos
.x
, newPos
.x
), wxMin(oldPos
.y
, newPos
.y
)); 
 415         wxPoint bottomRight 
= wxPoint(wxMax(oldRight
, newRight
), wxMax(oldBottom
, newBottom
)); 
 417         fullRect
.x 
= topLeft
.x
; fullRect
.y 
= topLeft
.y
; 
 418         fullRect
.SetRight(bottomRight
.x
); 
 419         fullRect
.SetBottom(bottomRight
.y
); 
 426     // Make the bitmap bigger than it need be, so we don't 
 427     // keep reallocating all the time. 
 430     if (!m_repairBitmap
.Ok() || (m_repairBitmap
.GetWidth() < fullRect
.GetWidth() || m_repairBitmap
.GetHeight() < fullRect
.GetHeight())) 
 432         m_repairBitmap 
= wxBitmap(fullRect
.GetWidth() + excess
, fullRect
.GetHeight() + excess
); 
 436     memDC
.SelectObject(* backing
); 
 438     wxMemoryDC memDCTemp
; 
 439     memDCTemp
.SelectObject(m_repairBitmap
); 
 441     // Draw the backing bitmap onto the repair bitmap. 
 442     // If full-screen, we may have specified the rect on the 
 443     // screen that we're using for our backing bitmap. 
 444     // So subtract this when we're blitting from the backing bitmap 
 445     // (translate from screen to backing-bitmap coords). 
 447     memDCTemp
.Blit(0, 0, fullRect
.GetWidth(), fullRect
.GetHeight(), & memDC
, fullRect
.x 
- m_boundingRect
.x
, fullRect
.y 
- m_boundingRect
.y
); 
 449     // If drawing, draw the image onto the mem DC 
 452         wxPoint 
pos(newPos
.x 
- fullRect
.x
, newPos
.y 
- fullRect
.y
) ; 
 453         DoDrawImage(memDCTemp
, pos
); 
 456     // Now blit to the window 
 457     // Finally, blit the temp mem DC to the window. 
 458     m_windowDC
->Blit(fullRect
.x
, fullRect
.y
, fullRect
.width
, fullRect
.height
, & memDCTemp
, 0, 0); 
 460     memDCTemp
.SelectObject(wxNullBitmap
); 
 461     memDC
.SelectObject(wxNullBitmap
); 
 466 // Override this if you are using a virtual image (drawing your own image) 
 467 bool wxGenericDragImage::DoDrawImage(wxDC
& dc
, const wxPoint
& pos
) const 
 471         dc
.DrawBitmap(m_bitmap
, pos
.x
, pos
.y
, (m_bitmap
.GetMask() != 0)); 
 474     else if (m_icon
.Ok()) 
 476         dc
.DrawIcon(m_icon
, pos
.x
, pos
.y
); 
 483 // Override this if you are using a virtual image (drawing your own image) 
 484 wxRect 
wxGenericDragImage::GetImageRect(const wxPoint
& pos
) const 
 488         return wxRect(pos
.x
, pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight()); 
 490     else if (m_icon
.Ok()) 
 492         return wxRect(pos
.x
, pos
.y
, m_icon
.GetWidth(), m_icon
.GetHeight()); 
 496         return wxRect(pos
.x
, pos
.y
, 0, 0);