1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/dcscreen.cpp
3 // Purpose: wxScreenDC class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/dcscreen.h"
15 #include "wx/osx/dcscreen.h"
17 #include "wx/osx/private.h"
18 #include "wx/graphics.h"
19 #if wxOSX_USE_COCOA_OR_CARBON
20 #include "wx/osx/private/glgrab.h"
23 IMPLEMENT_ABSTRACT_CLASS(wxScreenDCImpl
, wxWindowDCImpl
)
25 // TODO : for the Screenshot use case, which doesn't work in Quartz
26 // we should do a GetAsBitmap using something like
27 // http://www.cocoabuilder.com/archive/message/cocoa/2005/8/13/144256
29 // Create a DC representing the whole screen
30 wxScreenDCImpl::wxScreenDCImpl( wxDC
*owner
) :
31 wxWindowDCImpl( owner
)
33 #if wxOSX_USE_COCOA_OR_CARBON
35 cgbounds
= CGDisplayBounds(CGMainDisplayID());
36 m_width
= (wxCoord
)cgbounds
.size
.width
;
37 m_height
= (wxCoord
)cgbounds
.size
.height
;
39 wxDisplaySize( &m_width
, &m_height
);
41 #if wxOSX_USE_COCOA_OR_IPHONE
42 SetGraphicsContext( wxGraphicsContext::Create() );
45 bounds
.top
= (short)cgbounds
.origin
.y
;
46 bounds
.left
= (short)cgbounds
.origin
.x
;
47 bounds
.bottom
= bounds
.top
+ (short)cgbounds
.size
.height
;
48 bounds
.right
= bounds
.left
+ (short)cgbounds
.size
.width
;
49 WindowAttributes overlayAttributes
= kWindowIgnoreClicksAttribute
;
50 CreateNewWindow( kOverlayWindowClass
, overlayAttributes
, &bounds
, (WindowRef
*) &m_overlayWindow
);
51 ShowWindow((WindowRef
)m_overlayWindow
);
52 SetGraphicsContext( wxGraphicsContext::CreateFromNativeWindow( m_overlayWindow
) );
57 wxScreenDCImpl::~wxScreenDCImpl()
59 delete m_graphicContext
;
60 m_graphicContext
= NULL
;
61 #if wxOSX_USE_COCOA_OR_IPHONE
63 DisposeWindow((WindowRef
) m_overlayWindow
);
68 // Apple has allowed usage of this API as of 15th Dec 2009w
69 extern CGImageRef
UIGetScreenImage();
72 // TODO Switch to CGWindowListCreateImage for 10.5 and above
74 wxBitmap
wxScreenDCImpl::DoGetAsBitmap(const wxRect
*subrect
) const
76 wxRect rect
= subrect
? *subrect
: wxRect(0, 0, m_width
, m_height
);
78 wxBitmap
bmp(rect
.GetSize(), 32);
81 CGRect srcRect
= CGRectMake(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
83 CGContextRef context
= (CGContextRef
)bmp
.GetHBITMAP();
85 CGContextSaveGState(context
);
87 CGContextTranslateCTM( context
, 0, m_height
);
88 CGContextScaleCTM( context
, 1, -1 );
91 srcRect
= CGRectOffset( srcRect
, -subrect
->x
, -subrect
->y
) ;
93 CGImageRef image
= grabViaOpenGL(kCGNullDirectDisplay
, srcRect
);
95 wxASSERT_MSG(image
, wxT("wxScreenDC::GetAsBitmap - unable to get screenshot."));
97 CGContextDrawImage(context
, srcRect
, image
);
99 CGImageRelease(image
);
101 CGContextRestoreGState(context
);
103 // TODO implement using UIGetScreenImage, CGImageCreateWithImageInRect, CGContextDrawImage