]> git.saurik.com Git - wxWidgets.git/commitdiff
DoGetAsBitmap implementation for Cocoa.
authorKevin Ollivier <kevino@theolliviers.com>
Thu, 11 Sep 2008 05:40:35 +0000 (05:40 +0000)
committerKevin Ollivier <kevino@theolliviers.com>
Thu, 11 Sep 2008 05:40:35 +0000 (05:40 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@55539 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/osx/carbon/dcclient.cpp
src/osx/cocoa/utils.mm

index 2a7404bd4401fcf57b0eaf98162febde4e71b303..a943963a68deb78ec336ea467c7f6b7f0b41714f 100644 (file)
@@ -96,6 +96,7 @@ void wxWindowDCImpl::DoGetSize( int* width, int* height ) const
         *height = m_height;
 }
 
+#if wxOSX_USE_CARBON
 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
 {
     // wxScreenDC is derived from wxWindowDC, so a screen dc will
@@ -103,7 +104,6 @@ wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
     if (!m_window)
         return wxNullBitmap;
 
-#if wxOSX_USE_CARBON
     ControlRef handle = (ControlRef) m_window->GetHandle();
     if ( !handle )
         return wxNullBitmap;
@@ -136,10 +136,8 @@ wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
 
     CGContextRestoreGState(context);
     return bmp;
-#else
-    return wxNullBitmap;
-#endif
 }
+#endif
 
 /*
  * wxClientDCImpl
index 248075e4e5979d5294807eaa6d88882fe40a134e..d2374b6b8c12c1ead718ac5b9d0947f53d22b477 100644 (file)
@@ -31,6 +31,7 @@
 #if wxUSE_GUI
 #if wxOSX_USE_COCOA_OR_CARBON
     #include <CoreServices/CoreServices.h>
+    #include "wx/osx/dcclient.h"
     #include "wx/osx/private/timer.h"
 #endif
 #endif // wxUSE_GUI
@@ -140,6 +141,41 @@ void wxMacLocalToGlobal( WindowRef window , Point*pt )
 {
 }
 
+wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
+{
+    // wxScreenDC is derived from wxWindowDC, so a screen dc will
+    // call this method when a Blit is performed with it as a source.
+    if (!m_window)
+        return wxNullBitmap;
+        
+    wxSize sz = m_window->GetSize();
+    
+    int left = subrect != NULL ? subrect->x : 0 ;
+    int top = subrect != NULL ? subrect->y : 0 ;
+    int width = subrect != NULL ? subrect->width : sz.x;
+    int height = subrect !=  NULL ? subrect->height : sz.y ;
+    
+    NSRect rect = NSMakeRect(left, top, width, height );
+    NSView* view = (NSView*) m_window->GetHandle();
+    [view lockFocus];
+    // we use this method as other methods force a repaint, and this method can be 
+    // called from OnPaint, even with the window's paint dc as source (see wxHTMLWindow)
+    NSBitmapImageRep *rep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect: [view bounds]] retain];
+    [view unlockFocus];
+    
+    CGImageRef cgImageRef = [rep CGImage]; 
+
+    wxBitmap bitmap(CGImageGetWidth(cgImageRef)  , CGImageGetHeight(cgImageRef) );
+    CGRect r = CGRectMake( 0 , 0 , CGImageGetWidth(cgImageRef)  , CGImageGetHeight(cgImageRef) );
+    // since our context is upside down we dont use CGContextDrawImage
+    wxMacDrawCGImage( (CGContextRef) bitmap.GetHBITMAP() , &r, cgImageRef ) ;
+    CGImageRelease(cgImageRef);
+    cgImageRef = NULL;
+    [rep release];
+        
+    return bitmap;
+}
+
 #endif // wxUSE_GUI