]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/dcscreen.cpp
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / src / osx / carbon / dcscreen.cpp
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
524c47aa 2// Name: src/osx/carbon/dcscreen.cpp
489468fe
SC
3// Purpose: wxScreenDC class
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
489468fe
SC
7// Copyright: (c) Stefan Csomor
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#include "wx/wxprec.h"
12
13#include "wx/dcscreen.h"
b2680ced 14#include "wx/osx/dcscreen.h"
489468fe 15
b2680ced 16#include "wx/osx/private.h"
489468fe 17#include "wx/graphics.h"
ac50e694 18#if wxOSX_USE_COCOA_OR_CARBON
4fcb208a 19#include "wx/osx/private/glgrab.h"
ac50e694 20#endif
489468fe
SC
21
22IMPLEMENT_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
29wxScreenDCImpl::wxScreenDCImpl( wxDC *owner ) :
30 wxWindowDCImpl( owner )
31{
9e55f38d 32#if wxOSX_USE_COCOA_OR_CARBON
71c5a4d1
SC
33 CGRect cgbounds ;
34 cgbounds = CGDisplayBounds(CGMainDisplayID());
35 m_width = (wxCoord)cgbounds.size.width;
36 m_height = (wxCoord)cgbounds.size.height;
9e55f38d 37#else
03647350 38 wxDisplaySize( &m_width, &m_height );
9e55f38d 39#endif
b2680ced 40#if wxOSX_USE_COCOA_OR_IPHONE
5a8ea512 41 SetGraphicsContext( wxGraphicsContext::Create() );
489468fe 42#else
489468fe
SC
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 ) );
489468fe 52#endif
71c5a4d1 53 m_ok = true ;
489468fe
SC
54}
55
56wxScreenDCImpl::~wxScreenDCImpl()
57{
5276b0a5 58 wxDELETE(m_graphicContext);
b2680ced 59#if wxOSX_USE_COCOA_OR_IPHONE
489468fe
SC
60#else
61 DisposeWindow((WindowRef) m_overlayWindow );
62#endif
63}
4fcb208a 64
5575dacc
SC
65#if wxOSX_USE_IPHONE
66// Apple has allowed usage of this API as of 15th Dec 2009w
67extern CGImageRef UIGetScreenImage();
68#endif
69
17db93e9
SC
70// TODO Switch to CGWindowListCreateImage for 10.5 and above
71
4fcb208a
SC
72wxBitmap wxScreenDCImpl::DoGetAsBitmap(const wxRect *subrect) const
73{
c22ace4d
VZ
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
4fcb208a 81 CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
03647350 82
4fcb208a 83 CGContextSaveGState(context);
03647350 84
4fcb208a
SC
85 CGContextTranslateCTM( context, 0, m_height );
86 CGContextScaleCTM( context, 1, -1 );
03647350 87
4fcb208a
SC
88 if ( subrect )
89 srcRect = CGRectOffset( srcRect, -subrect->x, -subrect->y ) ;
03647350 90
0476fe3d
SC
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 }
03647350 103
4fcb208a 104 wxASSERT_MSG(image, wxT("wxScreenDC::GetAsBitmap - unable to get screenshot."));
03647350 105
4fcb208a 106 CGContextDrawImage(context, srcRect, image);
ce00f59b 107
5575dacc 108 CGImageRelease(image);
03647350 109
4fcb208a 110 CGContextRestoreGState(context);
5575dacc
SC
111#else
112 // TODO implement using UIGetScreenImage, CGImageCreateWithImageInRect, CGContextDrawImage
ac50e694 113#endif
4fcb208a
SC
114 return bmp;
115}