From 928e7a7e1b5bdaa2a475c2bfbe0f4679baf653e1 Mon Sep 17 00:00:00 2001 From: Kevin Ollivier Date: Thu, 11 Sep 2008 05:40:35 +0000 Subject: [PATCH] DoGetAsBitmap implementation for Cocoa. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@55539 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/osx/carbon/dcclient.cpp | 6 ++---- src/osx/cocoa/utils.mm | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/osx/carbon/dcclient.cpp b/src/osx/carbon/dcclient.cpp index 2a7404bd44..a943963a68 100644 --- a/src/osx/carbon/dcclient.cpp +++ b/src/osx/carbon/dcclient.cpp @@ -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 diff --git a/src/osx/cocoa/utils.mm b/src/osx/cocoa/utils.mm index 248075e4e5..d2374b6b8c 100644 --- a/src/osx/cocoa/utils.mm +++ b/src/osx/cocoa/utils.mm @@ -31,6 +31,7 @@ #if wxUSE_GUI #if wxOSX_USE_COCOA_OR_CARBON #include + #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 -- 2.47.2