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