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_IPHONE
34 SetGraphicsContext( wxGraphicsContext::Create() );
38 cgbounds
= CGDisplayBounds(CGMainDisplayID());
40 bounds
.top
= (short)cgbounds
.origin
.y
;
41 bounds
.left
= (short)cgbounds
.origin
.x
;
42 bounds
.bottom
= bounds
.top
+ (short)cgbounds
.size
.height
;
43 bounds
.right
= bounds
.left
+ (short)cgbounds
.size
.width
;
44 WindowAttributes overlayAttributes
= kWindowIgnoreClicksAttribute
;
45 CreateNewWindow( kOverlayWindowClass
, overlayAttributes
, &bounds
, (WindowRef
*) &m_overlayWindow
);
46 ShowWindow((WindowRef
)m_overlayWindow
);
47 SetGraphicsContext( wxGraphicsContext::CreateFromNativeWindow( m_overlayWindow
) );
48 m_width
= (wxCoord
)cgbounds
.size
.width
;
49 m_height
= (wxCoord
)cgbounds
.size
.height
;
54 wxScreenDCImpl::~wxScreenDCImpl()
56 delete m_graphicContext
;
57 m_graphicContext
= NULL
;
58 #if wxOSX_USE_COCOA_OR_IPHONE
60 DisposeWindow((WindowRef
) m_overlayWindow
);
64 // TODO Switch to CGWindowListCreateImage for 10.5 and above
66 wxBitmap
wxScreenDCImpl::DoGetAsBitmap(const wxRect
*subrect
) const
68 CGRect srcRect
= CGRectMake(0, 0, m_width
, m_height
);
71 srcRect
.origin
.x
= subrect
->GetX();
72 srcRect
.origin
.y
= subrect
->GetY();
73 srcRect
.size
.width
= subrect
->GetWidth();
74 srcRect
.size
.height
= subrect
->GetHeight();
76 wxBitmap bmp
= wxBitmap(srcRect
.size
.width
, srcRect
.size
.height
, 32);
79 CGContextRef context
= (CGContextRef
)bmp
.GetHBITMAP();
81 CGContextSaveGState(context
);
83 CGContextTranslateCTM( context
, 0, m_height
);
84 CGContextScaleCTM( context
, 1, -1 );
87 srcRect
= CGRectOffset( srcRect
, -subrect
->x
, -subrect
->y
) ;
89 CGImageRef image
= grabViaOpenGL(kCGNullDirectDisplay
, srcRect
);
91 wxASSERT_MSG(image
, wxT("wxScreenDC::GetAsBitmap - unable to get screenshot."));
93 CGContextDrawImage(context
, srcRect
, image
);
95 CGContextRestoreGState(context
);