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