]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/dcscreen.cpp
guard against invalid color ref
[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
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"
b2680ced 15#include "wx/osx/dcscreen.h"
489468fe 16
b2680ced 17#include "wx/osx/private.h"
489468fe 18#include "wx/graphics.h"
ac50e694 19#if wxOSX_USE_COCOA_OR_CARBON
4fcb208a 20#include "wx/osx/private/glgrab.h"
ac50e694 21#endif
489468fe
SC
22
23IMPLEMENT_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
30wxScreenDCImpl::wxScreenDCImpl( wxDC *owner ) :
31 wxWindowDCImpl( owner )
32{
9e55f38d 33#if wxOSX_USE_COCOA_OR_CARBON
71c5a4d1
SC
34 CGRect cgbounds ;
35 cgbounds = CGDisplayBounds(CGMainDisplayID());
36 m_width = (wxCoord)cgbounds.size.width;
37 m_height = (wxCoord)cgbounds.size.height;
9e55f38d 38#else
03647350 39 wxDisplaySize( &m_width, &m_height );
9e55f38d 40#endif
b2680ced 41#if wxOSX_USE_COCOA_OR_IPHONE
5a8ea512 42 SetGraphicsContext( wxGraphicsContext::Create() );
489468fe 43#else
489468fe
SC
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 ) );
489468fe 53#endif
71c5a4d1 54 m_ok = true ;
489468fe
SC
55}
56
57wxScreenDCImpl::~wxScreenDCImpl()
58{
5276b0a5 59 wxDELETE(m_graphicContext);
b2680ced 60#if wxOSX_USE_COCOA_OR_IPHONE
489468fe
SC
61#else
62 DisposeWindow((WindowRef) m_overlayWindow );
63#endif
64}
4fcb208a 65
5575dacc
SC
66#if wxOSX_USE_IPHONE
67// Apple has allowed usage of this API as of 15th Dec 2009w
68extern CGImageRef UIGetScreenImage();
69#endif
70
17db93e9
SC
71// TODO Switch to CGWindowListCreateImage for 10.5 and above
72
4fcb208a
SC
73wxBitmap wxScreenDCImpl::DoGetAsBitmap(const wxRect *subrect) const
74{
c22ace4d
VZ
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
4fcb208a 82 CGContextRef context = (CGContextRef)bmp.GetHBITMAP();
03647350 83
4fcb208a 84 CGContextSaveGState(context);
03647350 85
4fcb208a
SC
86 CGContextTranslateCTM( context, 0, m_height );
87 CGContextScaleCTM( context, 1, -1 );
03647350 88
4fcb208a
SC
89 if ( subrect )
90 srcRect = CGRectOffset( srcRect, -subrect->x, -subrect->y ) ;
03647350 91
4fcb208a 92 CGImageRef image = grabViaOpenGL(kCGNullDirectDisplay, srcRect);
03647350 93
4fcb208a 94 wxASSERT_MSG(image, wxT("wxScreenDC::GetAsBitmap - unable to get screenshot."));
03647350 95
4fcb208a 96 CGContextDrawImage(context, srcRect, image);
ce00f59b 97
5575dacc 98 CGImageRelease(image);
03647350 99
4fcb208a 100 CGContextRestoreGState(context);
5575dacc
SC
101#else
102 // TODO implement using UIGetScreenImage, CGImageCreateWithImageInRect, CGContextDrawImage
ac50e694 103#endif
4fcb208a
SC
104 return bmp;
105}