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 #include "wx/osx/private/glgrab.h"
21 IMPLEMENT_ABSTRACT_CLASS(wxScreenDCImpl
, wxWindowDCImpl
)
23 // TODO : for the Screenshot use case, which doesn't work in Quartz
24 // we should do a GetAsBitmap using something like
25 // http://www.cocoabuilder.com/archive/message/cocoa/2005/8/13/144256
27 // Create a DC representing the whole screen
28 wxScreenDCImpl::wxScreenDCImpl( wxDC
*owner
) :
29 wxWindowDCImpl( owner
)
31 #if wxOSX_USE_COCOA_OR_IPHONE
32 m_graphicContext
= NULL
;
36 cgbounds
= CGDisplayBounds(CGMainDisplayID());
38 bounds
.top
= (short)cgbounds
.origin
.y
;
39 bounds
.left
= (short)cgbounds
.origin
.x
;
40 bounds
.bottom
= bounds
.top
+ (short)cgbounds
.size
.height
;
41 bounds
.right
= bounds
.left
+ (short)cgbounds
.size
.width
;
42 WindowAttributes overlayAttributes
= kWindowIgnoreClicksAttribute
;
43 CreateNewWindow( kOverlayWindowClass
, overlayAttributes
, &bounds
, (WindowRef
*) &m_overlayWindow
);
44 ShowWindow((WindowRef
)m_overlayWindow
);
45 SetGraphicsContext( wxGraphicsContext::CreateFromNativeWindow( m_overlayWindow
) );
46 m_width
= (wxCoord
)cgbounds
.size
.width
;
47 m_height
= (wxCoord
)cgbounds
.size
.height
;
52 wxScreenDCImpl::~wxScreenDCImpl()
54 delete m_graphicContext
;
55 m_graphicContext
= NULL
;
56 #if wxOSX_USE_COCOA_OR_IPHONE
58 DisposeWindow((WindowRef
) m_overlayWindow
);
62 wxBitmap
wxScreenDCImpl::DoGetAsBitmap(const wxRect
*subrect
) const
64 CGRect srcRect
= CGRectMake(0, 0, m_width
, m_height
);
67 srcRect
.origin
.x
= subrect
->GetX();
68 srcRect
.origin
.y
= subrect
->GetY();
69 srcRect
.size
.width
= subrect
->GetWidth();
70 srcRect
.size
.height
= subrect
->GetHeight();
73 wxBitmap bmp
= wxBitmap(srcRect
.size
.width
, srcRect
.size
.height
, 32);
75 CGContextRef context
= (CGContextRef
)bmp
.GetHBITMAP();
77 CGContextSaveGState(context
);
79 CGContextTranslateCTM( context
, 0, m_height
);
80 CGContextScaleCTM( context
, 1, -1 );
83 srcRect
= CGRectOffset( srcRect
, -subrect
->x
, -subrect
->y
) ;
85 CGImageRef image
= grabViaOpenGL(kCGNullDirectDisplay
, srcRect
);
87 wxASSERT_MSG(image
, wxT("wxScreenDC::GetAsBitmap - unable to get screenshot."));
89 CGContextDrawImage(context
, srcRect
, image
);
91 CGContextRestoreGState(context
);