]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/dcscreen.cpp
adding new API needed for screenshots as old method stopped working in 10.7
[wxWidgets.git] / src / osx / carbon / dcscreen.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/dcscreen.cpp
3 // Purpose: wxScreenDC class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/dcscreen.h"
15 #include "wx/osx/dcscreen.h"
16
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"
21 #endif
22
23 IMPLEMENT_ABSTRACT_CLASS(wxScreenDCImpl, wxWindowDCImpl)
24
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
28
29 // Create a DC representing the whole screen
30 wxScreenDCImpl::wxScreenDCImpl( wxDC *owner ) :
31 wxWindowDCImpl( owner )
32 {
33 #if wxOSX_USE_COCOA_OR_CARBON
34 CGRect cgbounds ;
35 cgbounds = CGDisplayBounds(CGMainDisplayID());
36 m_width = (wxCoord)cgbounds.size.width;
37 m_height = (wxCoord)cgbounds.size.height;
38 #else
39 wxDisplaySize( &m_width, &m_height );
40 #endif
41 #if wxOSX_USE_COCOA_OR_IPHONE
42 SetGraphicsContext( wxGraphicsContext::Create() );
43 #else
44 Rect bounds;
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 ) );
53 #endif
54 m_ok = true ;
55 }
56
57 wxScreenDCImpl::~wxScreenDCImpl()
58 {
59 wxDELETE(m_graphicContext);
60 #if wxOSX_USE_COCOA_OR_IPHONE
61 #else
62 DisposeWindow((WindowRef) m_overlayWindow );
63 #endif
64 }
65
66 #if wxOSX_USE_IPHONE
67 // Apple has allowed usage of this API as of 15th Dec 2009w
68 extern CGImageRef UIGetScreenImage();
69 #endif
70
71 // TODO Switch to CGWindowListCreateImage for 10.5 and above
72
73 wxBitmap wxScreenDCImpl::DoGetAsBitmap(const wxRect *subrect) const
74 {
75 wxRect rect = subrect ? *subrect : wxRect(0, 0, m_width, m_height);
76
77 wxBitmap bmp(rect.GetSize(), 32);
78
79 #if !wxOSX_USE_IPHONE
80 CGRect srcRect = CGRectMake(rect.x, rect.y, rect.width, rect.height);
81
82 CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
83
84 CGContextSaveGState(context);
85
86 CGContextTranslateCTM( context, 0, m_height );
87 CGContextScaleCTM( context, 1, -1 );
88
89 if ( subrect )
90 srcRect = CGRectOffset( srcRect, -subrect->x, -subrect->y ) ;
91
92 CGImageRef image = NULL;
93
94 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
95 if ( UMAGetSystemVersion() >= 10.6)
96 {
97 image = CGDisplayCreateImage(kCGDirectMainDisplay);
98 }
99 else
100 #endif
101 {
102 image = grabViaOpenGL(kCGNullDirectDisplay, srcRect);
103 }
104
105 wxASSERT_MSG(image, wxT("wxScreenDC::GetAsBitmap - unable to get screenshot."));
106
107 CGContextDrawImage(context, srcRect, image);
108
109 CGImageRelease(image);
110
111 CGContextRestoreGState(context);
112 #else
113 // TODO implement using UIGetScreenImage, CGImageCreateWithImageInRect, CGContextDrawImage
114 #endif
115 return bmp;
116 }