1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/dcscreen.cpp
3 // Purpose: wxScreenDC class
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
13 #include "wx/dcscreen.h"
14 #include "wx/osx/dcscreen.h"
16 #include "wx/osx/private.h"
17 #include "wx/graphics.h"
18 #if wxOSX_USE_COCOA_OR_CARBON
19 #include "wx/osx/private/glgrab.h"
22 IMPLEMENT_ABSTRACT_CLASS(wxScreenDCImpl
, wxWindowDCImpl
)
24 // TODO : for the Screenshot use case, which doesn't work in Quartz
25 // we should do a GetAsBitmap using something like
26 // http://www.cocoabuilder.com/archive/message/cocoa/2005/8/13/144256
28 // Create a DC representing the whole screen
29 wxScreenDCImpl::wxScreenDCImpl( wxDC
*owner
) :
30 wxWindowDCImpl( owner
)
32 #if wxOSX_USE_COCOA_OR_CARBON
34 cgbounds
= CGDisplayBounds(CGMainDisplayID());
35 m_width
= (wxCoord
)cgbounds
.size
.width
;
36 m_height
= (wxCoord
)cgbounds
.size
.height
;
38 wxDisplaySize( &m_width
, &m_height
);
40 #if wxOSX_USE_COCOA_OR_IPHONE
41 SetGraphicsContext( wxGraphicsContext::Create() );
44 bounds
.top
= (short)cgbounds
.origin
.y
;
45 bounds
.left
= (short)cgbounds
.origin
.x
;
46 bounds
.bottom
= bounds
.top
+ (short)cgbounds
.size
.height
;
47 bounds
.right
= bounds
.left
+ (short)cgbounds
.size
.width
;
48 WindowAttributes overlayAttributes
= kWindowIgnoreClicksAttribute
;
49 CreateNewWindow( kOverlayWindowClass
, overlayAttributes
, &bounds
, (WindowRef
*) &m_overlayWindow
);
50 ShowWindow((WindowRef
)m_overlayWindow
);
51 SetGraphicsContext( wxGraphicsContext::CreateFromNativeWindow( m_overlayWindow
) );
56 wxScreenDCImpl::~wxScreenDCImpl()
58 wxDELETE(m_graphicContext
);
59 #if wxOSX_USE_COCOA_OR_IPHONE
61 DisposeWindow((WindowRef
) m_overlayWindow
);
66 // Apple has allowed usage of this API as of 15th Dec 2009w
67 extern CGImageRef
UIGetScreenImage();
70 // TODO Switch to CGWindowListCreateImage for 10.5 and above
72 wxBitmap
wxScreenDCImpl::DoGetAsBitmap(const wxRect
*subrect
) const
74 wxRect rect
= subrect
? *subrect
: wxRect(0, 0, m_width
, m_height
);
76 wxBitmap
bmp(rect
.GetSize(), 32);
79 CGRect srcRect
= CGRectMake(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
81 CGContextRef context
= (CGContextRef
)bmp
.GetHBITMAP();
83 CGContextSaveGState(context
);
85 CGContextTranslateCTM( context
, 0, m_height
);
86 CGContextScaleCTM( context
, 1, -1 );
89 srcRect
= CGRectOffset( srcRect
, -subrect
->x
, -subrect
->y
) ;
91 CGImageRef image
= NULL
;
93 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
94 if ( UMAGetSystemVersion() >= 10.6)
96 image
= CGDisplayCreateImage(kCGDirectMainDisplay
);
101 image
= grabViaOpenGL(kCGNullDirectDisplay
, srcRect
);
104 wxASSERT_MSG(image
, wxT("wxScreenDC::GetAsBitmap - unable to get screenshot."));
106 CGContextDrawImage(context
, srcRect
, image
);
108 CGImageRelease(image
);
110 CGContextRestoreGState(context
);
112 // TODO implement using UIGetScreenImage, CGImageCreateWithImageInRect, CGContextDrawImage