]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dcmemory.cpp
Explicitly forward non-command events along to the native control first.
[wxWidgets.git] / src / mac / carbon / dcmemory.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
f38924e8 2// Name: src/mac/carbon/dcmemory.cpp
e9576ca5 3// Purpose: wxMemoryDC class
a31a5f85 4// Author: Stefan Csomor
e9576ca5
SC
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
f38924e8 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
3d1a4878
SC
12#include "wx/wxprec.h"
13
e9576ca5 14#include "wx/dcmemory.h"
8acd14d1 15#include "wx/graphics.h"
f38924e8 16
76a5e5d2 17#include "wx/mac/private.h"
e9576ca5
SC
18
19//-----------------------------------------------------------------------------
20// wxMemoryDC
21//-----------------------------------------------------------------------------
22
23IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC,wxPaintDC)
24
fea35690 25void wxMemoryDC::Init()
e9576ca5 26{
cefe5886 27 m_ok = true;
0a67a93b
SC
28 SetBackground(*wxWHITE_BRUSH);
29 SetBrush(*wxWHITE_BRUSH);
30 SetPen(*wxBLACK_PEN);
cefe5886
DS
31 SetFont(*wxNORMAL_FONT);
32 m_ok = false;
33}
e9576ca5
SC
34
35wxMemoryDC::wxMemoryDC( wxDC *WXUNUSED(dc) )
e40298d5 36: m_selected()
e9576ca5 37{
fea35690 38 Init();
cefe5886 39}
e9576ca5 40
3dec57ad 41wxMemoryDC::~wxMemoryDC()
e9576ca5 42{
e40298d5
JS
43 if ( m_selected.Ok() )
44 {
20b69855
SC
45#if wxMAC_USE_CORE_GRAPHICS
46 m_selected.EndRawAccess() ;
f7862d3e 47 CGContextRef bmCtx = (CGContextRef) m_graphicContext->GetNativeContext() ;
20b69855
SC
48 delete m_graphicContext ;
49 m_graphicContext = NULL ;
50 CGContextRelease( bmCtx ) ;
51#else
cefe5886 52// TODO: UnlockPixels( GetGWorldPixMap(MAC_WXHBITMAP(m_selected.GetHBITMAP())) );
20b69855 53#endif
e40298d5 54 }
cefe5886 55}
e9576ca5 56
fea35690 57void wxMemoryDC::DoSelect( const wxBitmap& bitmap )
e9576ca5 58{
e40298d5
JS
59 if ( m_selected.Ok() )
60 {
20b69855
SC
61#if wxMAC_USE_CORE_GRAPHICS
62 m_selected.EndRawAccess() ;
f7862d3e 63 CGContextRef bmCtx = (CGContextRef) m_graphicContext->GetNativeContext() ;
20b69855
SC
64 delete m_graphicContext ;
65 m_graphicContext = NULL ;
66 CGContextRelease( bmCtx ) ;
67#else
cefe5886 68// TODO: UnlockPixels( GetGWorldPixMap(MAC_WXHBITMAP(m_selected.GetHBITMAP())) );
20b69855 69#endif
e40298d5 70 }
cefe5886 71
3dec57ad
SC
72 m_selected = bitmap;
73 if (m_selected.Ok())
74 {
20b69855 75#if wxMAC_USE_CORE_GRAPHICS
514fd350
SC
76 if ( m_selected.GetDepth() != 1 )
77 m_selected.UseAlpha() ;
20b69855
SC
78 void * data = m_selected.BeginRawAccess() ;
79
80 int bitsPerComp = 8 ;
81 int bytesPerPixel = 4 ;
82 int w = bitmap.GetWidth() ;
83 int h = bitmap.GetHeight() ;
f7862d3e
SC
84 m_width = w;
85 m_height = h;
cefe5886
DS
86
87 // TODO: should this be kCGImageAlphaPremultiplied[First,Last] ?
f38924e8 88 CGImageAlphaInfo a = kCGImageAlphaNoneSkipFirst ;
cefe5886 89
20b69855 90 CGColorSpaceRef genericColorSpace = wxMacGetGenericRGBColorSpace();
cefe5886 91 CGContextRef bmCtx = CGBitmapContextCreate( data , w, h, bitsPerComp , bytesPerPixel * w , genericColorSpace, a );
20b69855
SC
92 wxASSERT_MSG( bmCtx , wxT("Unable to create bitmap context") ) ;
93
cefe5886 94 if ( bmCtx )
20b69855 95 {
f38924e8
WS
96 CGContextSetFillColorSpace( bmCtx, genericColorSpace );
97 CGContextSetStrokeColorSpace( bmCtx, genericColorSpace );
cefe5886 98
20b69855
SC
99 CGContextTranslateCTM( bmCtx , 0 , m_selected.GetHeight() ) ;
100 CGContextScaleCTM( bmCtx , 1 , -1 ) ;
cefe5886 101
f7862d3e 102 SetGraphicsContext( wxGraphicsContext::CreateFromNative( bmCtx ) );
20b69855 103 m_graphicContext->SetPen( m_pen ) ;
f38924e8 104 m_graphicContext->SetBrush( m_brush ) ;
a60a5499 105 m_graphicContext->SetFont( m_font , m_textForegroundColour) ;
20b69855 106 }
cefe5886
DS
107 m_ok = (m_graphicContext != NULL) ;
108
20b69855 109#else
cefe5886
DS
110 m_macPort = m_selected.GetHBITMAP( &m_macMask ) ;
111 m_ok = (m_macPort != NULL) ;
112 if (m_ok)
e40298d5 113 {
cefe5886 114 LockPixels( GetGWorldPixMap( (CGrafPtr) m_macPort ) ) ;
e40298d5 115 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , 0 , 0 , m_selected.GetWidth() , m_selected.GetHeight() ) ;
cefe5886 116 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
e40298d5 117 }
20b69855 118#endif
e40298d5
JS
119 }
120 else
121 {
cefe5886 122 m_ok = false;
e40298d5 123 }
3dec57ad 124}
e9576ca5 125
0a67a93b 126void wxMemoryDC::DoGetSize( int *width, int *height ) const
e9576ca5 127{
e40298d5
JS
128 if (m_selected.Ok())
129 {
cefe5886
DS
130 if (width)
131 (*width) = m_selected.GetWidth();
132 if (height)
133 (*height) = m_selected.GetHeight();
e40298d5
JS
134 }
135 else
136 {
cefe5886
DS
137 if (width)
138 (*width) = 0;
139 if (height)
140 (*height) = 0;
e40298d5 141 }
3dec57ad 142}