]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dc.cpp
fixed crash introduced in v1.41
[wxWidgets.git] / src / mac / carbon / dc.cpp
CommitLineData
2ac013b1 1/////////////////////////////////////////////////////////////////////////////
e9576ca5
SC
2// Name: dc.cpp
3// Purpose: wxDC 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
3dee36ae 9// Licence: wxWindows licence
e9576ca5 10/////////////////////////////////////////////////////////////////////////////
e40298d5 11
ba9a486d
SC
12#include "wx/wxprec.h"
13
e9576ca5 14#include "wx/dc.h"
20b69855
SC
15
16#if !wxMAC_USE_CORE_GRAPHICS
03e11df5 17#include "wx/app.h"
2f1ae414 18#include "wx/mac/uma.h"
3dec57ad 19#include "wx/dcmemory.h"
a7390348 20#include "wx/dcprint.h"
3dec57ad
SC
21#include "wx/region.h"
22#include "wx/image.h"
b1699cd3 23#include "wx/log.h"
e9576ca5 24
2f31c6ec
VZ
25#ifdef __MSL__
26 #if __MSL__ >= 0x6000
27 namespace std {}
28 using namespace std ;
29 #endif
5b781a67 30#endif
a7390348 31
b34039cf 32#include "wx/mac/private.h"
768c6e8b 33#ifndef __DARWIN__
7f0c3a63
SC
34#include <ATSUnicode.h>
35#include <TextCommon.h>
36#include <TextEncodingConverter.h>
768c6e8b 37#endif
fd04970a 38
f23d3255
DS
39
40// set to 0 if problems arise
41#define wxMAC_EXPERIMENTAL_DC 1
42
43
e9576ca5 44IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
e40298d5 45
e9576ca5
SC
46//-----------------------------------------------------------------------------
47// constants
48//-----------------------------------------------------------------------------
e40298d5 49
3dec57ad 50const double RAD2DEG = 180.0 / M_PI;
76a5e5d2
SC
51const short kEmulatedMode = -1 ;
52const short kUnsupportedMode = -2 ;
e40298d5 53
939fba6c
SC
54extern TECObjectRef s_TECNativeCToUnicode ;
55
8bebc229 56
66a09d47 57wxMacPortSetter::wxMacPortSetter( const wxDC* dc ) :
e40298d5 58 m_ph( (GrafPtr) dc->m_macPort )
66a09d47 59{
e40298d5
JS
60 wxASSERT( dc->Ok() ) ;
61 m_dc = dc ;
62 dc->MacSetupPort(&m_ph) ;
66a09d47 63}
9b2eab52 64
99030bdf 65wxMacPortSetter::~wxMacPortSetter()
66a09d47 66{
e40298d5 67 m_dc->MacCleanupPort(&m_ph) ;
66a09d47 68}
e40298d5 69
8bebc229
SC
70#if wxMAC_EXPERIMENTAL_DC
71class wxMacFastPortSetter
72{
73public :
ab9d0a8c 74 wxMacFastPortSetter( const wxDC *dc )
8bebc229 75 {
ab9d0a8c
WS
76 wxASSERT( dc->Ok() ) ;
77 m_swapped = QDSwapPort( (GrafPtr) dc->m_macPort , &m_oldPort ) ;
78 m_clipRgn = NewRgn() ;
79 GetClip( m_clipRgn ) ;
80 m_dc = dc ;
81 dc->MacSetupPort( NULL ) ;
8bebc229 82 }
9b2eab52 83
8bebc229
SC
84 ~wxMacFastPortSetter()
85 {
bb253bfd 86 // SetPort( (GrafPtr) m_dc->m_macPort ) ;
105b0fca 87 SetClip( m_clipRgn ) ;
bb253bfd 88 if ( m_swapped )
ab9d0a8c
WS
89 SetPort( m_oldPort ) ;
90 m_dc->MacCleanupPort( NULL ) ;
91 DisposeRgn( m_clipRgn ) ;
8bebc229 92 }
9b2eab52 93
8bebc229 94private :
bb253bfd 95 bool m_swapped ;
105b0fca 96 RgnHandle m_clipRgn ;
8bebc229
SC
97 GrafPtr m_oldPort ;
98 const wxDC* m_dc ;
99} ;
100
101#else
102typedef wxMacPortSetter wxMacFastPortSetter ;
103#endif
104
facd6764
SC
105wxMacWindowClipper::wxMacWindowClipper( const wxWindow* win ) :
106 wxMacPortSaver( (GrafPtr) GetWindowPort((WindowRef) win->MacGetTopLevelWindowRef()) )
15cae9eb 107{
01299d56 108 m_newPort =(GrafPtr) GetWindowPort((WindowRef) win->MacGetTopLevelWindowRef()) ;
15cae9eb
SC
109 m_formerClip = NewRgn() ;
110 m_newClip = NewRgn() ;
111 GetClip( m_formerClip ) ;
ab9d0a8c 112
15cae9eb
SC
113 if ( win )
114 {
b905d6cc 115 // guard against half constructed objects, this just leads to a empty clip
9b2eab52 116 if ( win->GetPeer() )
b905d6cc
SC
117 {
118 int x = 0 , y = 0;
f23d3255 119 win->MacWindowToRootWindow( &x, &y ) ;
9b2eab52 120
b905d6cc
SC
121 // get area including focus rect
122 CopyRgn( (RgnHandle) ((wxWindow*)win)->MacGetVisibleRegion(true).GetWXHRGN() , m_newClip ) ;
123 if ( !EmptyRgn( m_newClip ) )
124 OffsetRgn( m_newClip , x , y ) ;
125 }
9b2eab52 126
1dd85cc3 127 SetClip( m_newClip ) ;
e40298d5 128 }
15cae9eb 129}
e40298d5 130
ab9d0a8c 131wxMacWindowClipper::~wxMacWindowClipper()
15cae9eb 132{
01299d56 133 SetPort( m_newPort ) ;
15cae9eb 134 SetClip( m_formerClip ) ;
e40298d5
JS
135 DisposeRgn( m_newClip ) ;
136 DisposeRgn( m_formerClip ) ;
15cae9eb 137}
e40298d5 138
01299d56
SC
139wxMacWindowStateSaver::wxMacWindowStateSaver( const wxWindow* win ) :
140 wxMacWindowClipper( win )
141{
142 // the port is already set at this point
9b2eab52 143 m_newPort = (GrafPtr) GetWindowPort((WindowRef) win->MacGetTopLevelWindowRef()) ;
01299d56
SC
144 GetThemeDrawingState( &m_themeDrawingState ) ;
145}
146
ab9d0a8c 147wxMacWindowStateSaver::~wxMacWindowStateSaver()
01299d56
SC
148{
149 SetPort( m_newPort ) ;
150 SetThemeDrawingState( m_themeDrawingState , true ) ;
151}
152
3dec57ad
SC
153//-----------------------------------------------------------------------------
154// Local functions
155//-----------------------------------------------------------------------------
3dec57ad
SC
156static inline double dmin(double a, double b) { return a < b ? a : b; }
157static inline double dmax(double a, double b) { return a > b ? a : b; }
158static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
e40298d5 159
e9576ca5
SC
160//-----------------------------------------------------------------------------
161// wxDC
162//-----------------------------------------------------------------------------
76a5e5d2
SC
163// this function emulates all wx colour manipulations, used to verify the implementation
164// by setting the mode in the blitting functions to kEmulatedMode
76a5e5d2 165void wxMacCalculateColour( int logical_func , const RGBColor &srcColor , RGBColor &dstColor ) ;
e40298d5 166
76a5e5d2
SC
167void wxMacCalculateColour( int logical_func , const RGBColor &srcColor , RGBColor &dstColor )
168{
169 switch ( logical_func )
170 {
e40298d5
JS
171 case wxAND: // src AND dst
172 dstColor.red = dstColor.red & srcColor.red ;
173 dstColor.green = dstColor.green & srcColor.green ;
174 dstColor.blue = dstColor.blue & srcColor.blue ;
175 break ;
9b2eab52 176
e40298d5
JS
177 case wxAND_INVERT: // (NOT src) AND dst
178 dstColor.red = dstColor.red & ~srcColor.red ;
179 dstColor.green = dstColor.green & ~srcColor.green ;
180 dstColor.blue = dstColor.blue & ~srcColor.blue ;
181 break ;
9b2eab52 182
e40298d5
JS
183 case wxAND_REVERSE:// src AND (NOT dst)
184 dstColor.red = ~dstColor.red & srcColor.red ;
185 dstColor.green = ~dstColor.green & srcColor.green ;
186 dstColor.blue = ~dstColor.blue & srcColor.blue ;
187 break ;
9b2eab52 188
e40298d5
JS
189 case wxCLEAR: // 0
190 dstColor.red = 0 ;
191 dstColor.green = 0 ;
192 dstColor.blue = 0 ;
193 break ;
9b2eab52 194
76a5e5d2 195 case wxCOPY: // src
e40298d5
JS
196 dstColor.red = srcColor.red ;
197 dstColor.green = srcColor.green ;
198 dstColor.blue = srcColor.blue ;
199 break ;
9b2eab52 200
e40298d5
JS
201 case wxEQUIV: // (NOT src) XOR dst
202 dstColor.red = dstColor.red ^ ~srcColor.red ;
203 dstColor.green = dstColor.green ^ ~srcColor.green ;
204 dstColor.blue = dstColor.blue ^ ~srcColor.blue ;
205 break ;
9b2eab52 206
e40298d5
JS
207 case wxINVERT: // NOT dst
208 dstColor.red = ~dstColor.red ;
209 dstColor.green = ~dstColor.green ;
210 dstColor.blue = ~dstColor.blue ;
211 break ;
9b2eab52 212
e40298d5
JS
213 case wxNAND: // (NOT src) OR (NOT dst)
214 dstColor.red = ~dstColor.red | ~srcColor.red ;
215 dstColor.green = ~dstColor.green | ~srcColor.green ;
216 dstColor.blue = ~dstColor.blue | ~srcColor.blue ;
217 break ;
9b2eab52 218
e40298d5
JS
219 case wxNOR: // (NOT src) AND (NOT dst)
220 dstColor.red = ~dstColor.red & ~srcColor.red ;
221 dstColor.green = ~dstColor.green & ~srcColor.green ;
222 dstColor.blue = ~dstColor.blue & ~srcColor.blue ;
223 break ;
9b2eab52 224
e40298d5
JS
225 case wxOR: // src OR dst
226 dstColor.red = dstColor.red | srcColor.red ;
227 dstColor.green = dstColor.green | srcColor.green ;
228 dstColor.blue = dstColor.blue | srcColor.blue ;
229 break ;
9b2eab52 230
e40298d5
JS
231 case wxOR_INVERT: // (NOT src) OR dst
232 dstColor.red = dstColor.red | ~srcColor.red ;
233 dstColor.green = dstColor.green | ~srcColor.green ;
234 dstColor.blue = dstColor.blue | ~srcColor.blue ;
235 break ;
9b2eab52 236
e40298d5
JS
237 case wxOR_REVERSE: // src OR (NOT dst)
238 dstColor.red = ~dstColor.red | srcColor.red ;
239 dstColor.green = ~dstColor.green | srcColor.green ;
240 dstColor.blue = ~dstColor.blue | srcColor.blue ;
241 break ;
9b2eab52 242
e40298d5
JS
243 case wxSET: // 1
244 dstColor.red = 0xFFFF ;
245 dstColor.green = 0xFFFF ;
246 dstColor.blue = 0xFFFF ;
247 break ;
9b2eab52 248
e40298d5
JS
249 case wxSRC_INVERT: // (NOT src)
250 dstColor.red = ~srcColor.red ;
251 dstColor.green = ~srcColor.green ;
252 dstColor.blue = ~srcColor.blue ;
253 break ;
9b2eab52 254
e40298d5
JS
255 case wxXOR: // src XOR dst
256 dstColor.red = dstColor.red ^ srcColor.red ;
257 dstColor.green = dstColor.green ^ srcColor.green ;
258 dstColor.blue = dstColor.blue ^ srcColor.blue ;
259 break ;
9b2eab52
DS
260
261 case wxNO_OP: // dst
262 default:
263 break ;
76a5e5d2 264 }
76a5e5d2 265}
e40298d5 266
2f1ae414 267wxDC::wxDC()
e9576ca5 268{
ab9d0a8c
WS
269 m_ok = false;
270 m_colour = true;
e40298d5
JS
271 m_mm_to_pix_x = mm2pt;
272 m_mm_to_pix_y = mm2pt;
273 m_internalDeviceOriginX = 0;
274 m_internalDeviceOriginY = 0;
275 m_externalDeviceOriginX = 0;
276 m_externalDeviceOriginY = 0;
277 m_logicalScaleX = 1.0;
278 m_logicalScaleY = 1.0;
279 m_userScaleX = 1.0;
280 m_userScaleY = 1.0;
281 m_scaleX = 1.0;
282 m_scaleY = 1.0;
ab9d0a8c
WS
283 m_needComputeScaleX = false;
284 m_needComputeScaleY = false;
e40298d5
JS
285 m_macPort = NULL ;
286 m_macMask = NULL ;
e40298d5
JS
287 m_macFontInstalled = false ;
288 m_macBrushInstalled = false ;
289 m_macPenInstalled = false ;
290 m_macLocalOrigin.x = m_macLocalOrigin.y = 0 ;
291 m_macBoundaryClipRgn = NewRgn() ;
292 m_macCurrentClipRgn = NewRgn() ;
293 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , -32000 , -32000 , 32000 , 32000 ) ;
294 SetRectRgn( (RgnHandle) m_macCurrentClipRgn , -32000 , -32000 , 32000 , 32000 ) ;
9ff647cf
SC
295 m_pen = *wxBLACK_PEN;
296 m_font = *wxNORMAL_FONT;
297 m_brush = *wxWHITE_BRUSH;
9b2eab52 298
26b9b4e1 299#ifdef __WXDEBUG__
ab9d0a8c 300 // needed to debug possible errors with two active drawing methods at the same time on
26b9b4e1 301 // the same DC
66a09d47 302 m_macCurrentPortStateHelper = NULL ;
26b9b4e1 303#endif
9b2eab52 304
66a09d47
SC
305 m_macATSUIStyle = NULL ;
306 m_macAliasWasEnabled = false;
307 m_macForegroundPixMap = NULL ;
308 m_macBackgroundPixMap = NULL ;
0eaa1d68 309}
e40298d5 310
e9576ca5
SC
311wxDC::~wxDC(void)
312{
76a5e5d2
SC
313 DisposeRgn( (RgnHandle) m_macBoundaryClipRgn ) ;
314 DisposeRgn( (RgnHandle) m_macCurrentClipRgn ) ;
3dec57ad 315}
e40298d5 316
76a5e5d2 317void wxDC::MacSetupPort(wxMacPortStateHelper* help) const
519cb848 318{
26b9b4e1 319#ifdef __WXDEBUG__
66a09d47
SC
320 wxASSERT( m_macCurrentPortStateHelper == NULL ) ;
321 m_macCurrentPortStateHelper = help ;
26b9b4e1 322#endif
9b2eab52 323
e40298d5 324 SetClip( (RgnHandle) m_macCurrentClipRgn);
9b2eab52 325
8bebc229 326#if ! wxMAC_EXPERIMENTAL_DC
e40298d5
JS
327 m_macFontInstalled = false ;
328 m_macBrushInstalled = false ;
329 m_macPenInstalled = false ;
8bebc229 330#endif
519cb848 331}
9b2eab52 332
66a09d47
SC
333void wxDC::MacCleanupPort(wxMacPortStateHelper* help) const
334{
26b9b4e1 335#ifdef __WXDEBUG__
66a09d47
SC
336 wxASSERT( m_macCurrentPortStateHelper == help ) ;
337 m_macCurrentPortStateHelper = NULL ;
26b9b4e1 338#endif
9b2eab52
DS
339
340 if ( m_macATSUIStyle )
66a09d47
SC
341 {
342 ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
343 m_macATSUIStyle = NULL ;
344 }
9b2eab52 345
66a09d47
SC
346 if ( m_macAliasWasEnabled )
347 {
348 SetAntiAliasedTextEnabled(m_macFormerAliasState, m_macFormerAliasSize);
349 m_macAliasWasEnabled = false ;
350 }
9b2eab52 351
66a09d47
SC
352 if ( m_macForegroundPixMap )
353 {
354 Pattern blackColor ;
355 ::PenPat(GetQDGlobalsBlack(&blackColor));
c99d6f99 356 DisposePixPat( (PixPatHandle) m_macForegroundPixMap ) ;
66a09d47
SC
357 m_macForegroundPixMap = NULL ;
358 }
9b2eab52 359
66a09d47
SC
360 if ( m_macBackgroundPixMap )
361 {
362 Pattern whiteColor ;
363 ::BackPat(GetQDGlobalsWhite(&whiteColor));
c99d6f99 364 DisposePixPat( (PixPatHandle) m_macBackgroundPixMap ) ;
66a09d47
SC
365 m_macBackgroundPixMap = NULL ;
366 }
367}
e40298d5 368
2f1ae414 369void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask )
519cb848 370{
9b2eab52
DS
371 wxCHECK_RET( Ok(), wxT("wxDC::DoDrawBitmap - invalid DC") );
372 wxCHECK_RET( bmp.Ok(), wxT("wxDC::DoDrawBitmap - invalid bitmap") );
373
8bebc229 374 wxMacFastPortSetter helper(this) ;
7d9d1fd7
SC
375 wxCoord xx = XLOG2DEVMAC(x);
376 wxCoord yy = YLOG2DEVMAC(y);
3dec57ad
SC
377 wxCoord w = bmp.GetWidth();
378 wxCoord h = bmp.GetHeight();
379 wxCoord ww = XLOG2DEVREL(w);
380 wxCoord hh = YLOG2DEVREL(h);
9b2eab52 381
3dec57ad
SC
382 // Set up drawing mode
383 short mode = (m_logicalFunction == wxCOPY ? srcCopy :
384 //m_logicalFunction == wxCLEAR ? WHITENESS :
385 //m_logicalFunction == wxSET ? BLACKNESS :
386 m_logicalFunction == wxINVERT ? hilite :
387 //m_logicalFunction == wxAND ? MERGECOPY :
388 m_logicalFunction == wxOR ? srcOr :
389 m_logicalFunction == wxSRC_INVERT ? notSrcCopy :
390 m_logicalFunction == wxXOR ? srcXor :
391 m_logicalFunction == wxOR_REVERSE ? notSrcOr :
392 //m_logicalFunction == wxAND_REVERSE ? SRCERASE :
393 //m_logicalFunction == wxSRC_OR ? srcOr :
394 //m_logicalFunction == wxSRC_AND ? SRCAND :
395 srcCopy );
20b69855 396
9b2eab52
DS
397 GWorldPtr maskworld = NULL ;
398 GWorldPtr bmapworld = MAC_WXHBITMAP( bmp.GetHBITMAP((WXHBITMAP*)&maskworld) );
20b69855 399 PixMapHandle bmappixels ;
9b2eab52 400
20b69855 401 // Set foreground and background colours (for bitmaps depth = 1)
9b2eab52 402 if (bmp.GetDepth() == 1)
3dee36ae 403 {
20b69855
SC
404 RGBColor fore = MAC_WXCOLORREF(m_textForegroundColour.GetPixel());
405 RGBColor back = MAC_WXCOLORREF(m_textBackgroundColour.GetPixel());
406 RGBForeColor(&fore);
407 RGBBackColor(&back);
408 }
409 else
3dec57ad 410 {
f23d3255
DS
411 RGBColor white = { 0xFFFF, 0xFFFF, 0xFFFF } ;
412 RGBColor black = { 0, 0, 0 } ;
20b69855
SC
413 RGBForeColor( &black ) ;
414 RGBBackColor( &white ) ;
415 }
416 bmappixels = GetGWorldPixMap( bmapworld ) ;
9b2eab52 417
20b69855 418 wxCHECK_RET(LockPixels(bmappixels),
9b2eab52
DS
419 wxT("wxDC::DoDrawBitmap - failed to lock pixels"));
420
20b69855
SC
421 Rect source = { 0, 0, h, w };
422 Rect dest = { yy, xx, yy + hh, xx + ww };
423 if ( useMask && maskworld )
424 {
9b2eab52 425 if ( LockPixels(GetGWorldPixMap(MAC_WXHBITMAP(maskworld))))
3dec57ad 426 {
20b69855
SC
427 CopyDeepMask
428 (
429 GetPortBitMapForCopyBits(bmapworld),
430 GetPortBitMapForCopyBits(MAC_WXHBITMAP(maskworld)),
431 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ),
432 &source, &source, &dest, mode, NULL
433 );
434 UnlockPixels(GetGWorldPixMap(MAC_WXHBITMAP(maskworld)));
3dec57ad 435 }
3dec57ad 436 }
9b2eab52
DS
437 else
438 {
20b69855
SC
439 CopyBits( GetPortBitMapForCopyBits( bmapworld ),
440 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ),
441 &source, &dest, mode, NULL ) ;
3dec57ad 442 }
20b69855
SC
443 UnlockPixels( bmappixels ) ;
444
3dec57ad
SC
445 m_macPenInstalled = false ;
446 m_macBrushInstalled = false ;
447 m_macFontInstalled = false ;
519cb848 448}
e40298d5 449
2f1ae414 450void wxDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
e9576ca5 451{
9b2eab52
DS
452 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawIcon - invalid DC"));
453 wxCHECK_RET(icon.Ok(), wxT("wxDC::DoDrawIcon - invalid icon"));
454
20b69855
SC
455 wxMacFastPortSetter helper(this) ;
456
457 wxCoord xx = XLOG2DEVMAC(x);
458 wxCoord yy = YLOG2DEVMAC(y);
37a87c67
SC
459 wxCoord w = icon.GetWidth();
460 wxCoord h = icon.GetHeight();
461 wxCoord ww = XLOG2DEVREL(w);
462 wxCoord hh = YLOG2DEVREL(h);
20b69855 463
f23d3255 464 Rect r = { yy , xx, yy + hh, xx + ww } ;
20b69855 465 PlotIconRef( &r , kAlignNone , kTransformNone , kPlotIconRefNormalFlags , MAC_WXHICON( icon.GetHICON() ) ) ;
3dec57ad 466}
e40298d5 467
2f1ae414 468void wxDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
e9576ca5 469{
9b2eab52
DS
470 wxCHECK_RET(Ok(), wxT("wxDC::DoSetClippingRegion - invalid DC"));
471
3dec57ad 472 wxCoord xx, yy, ww, hh;
7d9d1fd7
SC
473 xx = XLOG2DEVMAC(x);
474 yy = YLOG2DEVMAC(y);
3dec57ad
SC
475 ww = XLOG2DEVREL(width);
476 hh = YLOG2DEVREL(height);
9b2eab52 477
76a5e5d2
SC
478 SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh ) ;
479 SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
9b2eab52 480 if ( m_clipping )
3dec57ad
SC
481 {
482 m_clipX1 = wxMax( m_clipX1 , xx );
483 m_clipY1 = wxMax( m_clipY1 , yy );
f23d3255
DS
484 m_clipX2 = wxMin( m_clipX2, (xx + ww) );
485 m_clipY2 = wxMin( m_clipY2, (yy + hh) );
3dec57ad
SC
486 }
487 else
488 {
ab9d0a8c 489 m_clipping = true;
3dec57ad
SC
490 m_clipX1 = xx;
491 m_clipY1 = yy;
492 m_clipX2 = xx + ww;
493 m_clipY2 = yy + hh;
494 }
3dec57ad 495}
e40298d5 496
f23d3255 497void wxDC::DoSetClippingRegionAsRegion( const wxRegion &region )
2f1ae414 498{
9b2eab52
DS
499 wxCHECK_RET(Ok(), wxT("wxDC::DoSetClippingRegionAsRegion - invalid DC"));
500
8bebc229 501 wxMacFastPortSetter helper(this) ;
ffba56ed
SC
502 wxCoord x, y, w, h;
503 region.GetBox( x, y, w, h );
2f1ae414 504 wxCoord xx, yy, ww, hh;
7d9d1fd7
SC
505 xx = XLOG2DEVMAC(x);
506 yy = YLOG2DEVMAC(y);
ffba56ed
SC
507 ww = XLOG2DEVREL(w);
508 hh = YLOG2DEVREL(h);
9b2eab52 509
ffba56ed
SC
510 // if we have a scaling that we cannot map onto native regions
511 // we must use the box
ffba56ed
SC
512 if ( ww != w || hh != h )
513 {
514 wxDC::DoSetClippingRegion( x, y, w, h );
515 }
516 else
517 {
76a5e5d2 518 CopyRgn( (RgnHandle) region.GetWXHRGN() , (RgnHandle) m_macCurrentClipRgn ) ;
ffba56ed 519 if ( xx != x || yy != y )
76a5e5d2 520 OffsetRgn( (RgnHandle) m_macCurrentClipRgn , xx - x , yy - y ) ;
9b2eab52 521
76a5e5d2 522 SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
9b2eab52 523 if ( m_clipping )
ffba56ed
SC
524 {
525 m_clipX1 = wxMax( m_clipX1 , xx );
526 m_clipY1 = wxMax( m_clipY1 , yy );
f23d3255
DS
527 m_clipX2 = wxMin( m_clipX2, (xx + ww) );
528 m_clipY2 = wxMin( m_clipY2, (yy + hh) );
ffba56ed
SC
529 }
530 else
531 {
ab9d0a8c 532 m_clipping = true;
ffba56ed
SC
533 m_clipX1 = xx;
534 m_clipY1 = yy;
535 m_clipX2 = xx + ww;
536 m_clipY2 = yy + hh;
537 }
538 }
ee41971c 539}
e40298d5 540
3dec57ad 541void wxDC::DestroyClippingRegion()
e9576ca5 542{
8bebc229 543 wxMacFastPortSetter helper(this) ;
9b2eab52 544
e40298d5 545 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
d16b634f 546 ResetClipping();
99030bdf 547}
e40298d5 548
2f1ae414 549void wxDC::DoGetSizeMM( int* width, int* height ) const
e9576ca5 550{
9b2eab52
DS
551 int w = 0, h = 0;
552
e40298d5 553 GetSize( &w, &h );
9b2eab52
DS
554 if (width)
555 *width = long( double(w) / (m_scaleX * m_mm_to_pix_x) );
556 if (height)
557 *height = long( double(h) / (m_scaleY * m_mm_to_pix_y) );
3dec57ad 558}
e40298d5 559
e9576ca5
SC
560void wxDC::SetTextForeground( const wxColour &col )
561{
9b2eab52
DS
562 wxCHECK_RET(Ok(), wxT("wxDC::SetTextForeground - invalid DC"));
563
3dec57ad
SC
564 m_textForegroundColour = col;
565 m_macFontInstalled = false ;
566}
e40298d5 567
e9576ca5
SC
568void wxDC::SetTextBackground( const wxColour &col )
569{
9b2eab52
DS
570 wxCHECK_RET(Ok(), wxT("wxDC::SetTextBackground - invalid DC"));
571
3dec57ad
SC
572 m_textBackgroundColour = col;
573 m_macFontInstalled = false ;
574}
e40298d5 575
e9576ca5
SC
576void wxDC::SetMapMode( int mode )
577{
e40298d5
JS
578 switch (mode)
579 {
e3065973 580 case wxMM_TWIPS:
9b2eab52 581 SetLogicalScale( twips2mm * m_mm_to_pix_x, twips2mm * m_mm_to_pix_y );
e40298d5 582 break;
9b2eab52 583
e3065973 584 case wxMM_POINTS:
9b2eab52 585 SetLogicalScale( pt2mm * m_mm_to_pix_x, pt2mm * m_mm_to_pix_y );
e40298d5 586 break;
9b2eab52 587
e3065973 588 case wxMM_METRIC:
e40298d5
JS
589 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
590 break;
9b2eab52 591
e3065973 592 case wxMM_LOMETRIC:
9b2eab52 593 SetLogicalScale( m_mm_to_pix_x / 10.0, m_mm_to_pix_y / 10.0 );
e40298d5 594 break;
9b2eab52 595
e9576ca5 596 default:
e3065973 597 case wxMM_TEXT:
e40298d5
JS
598 SetLogicalScale( 1.0, 1.0 );
599 break;
600 }
9b2eab52 601
e40298d5
JS
602 if (mode != wxMM_TEXT)
603 {
ab9d0a8c
WS
604 m_needComputeScaleX = true;
605 m_needComputeScaleY = true;
e40298d5 606 }
3dec57ad 607}
e40298d5 608
e9576ca5
SC
609void wxDC::SetUserScale( double x, double y )
610{
e40298d5
JS
611 // allow negative ? -> no
612 m_userScaleX = x;
613 m_userScaleY = y;
614 ComputeScaleAndOrigin();
3dec57ad 615}
e40298d5 616
e9576ca5
SC
617void wxDC::SetLogicalScale( double x, double y )
618{
e40298d5
JS
619 // allow negative ?
620 m_logicalScaleX = x;
621 m_logicalScaleY = y;
622 ComputeScaleAndOrigin();
3dec57ad 623}
e40298d5 624
2f1ae414 625void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
e9576ca5 626{
9b2eab52
DS
627 // is this still correct ?
628 m_logicalOriginX = x * m_signX;
e40298d5
JS
629 m_logicalOriginY = y * m_signY;
630 ComputeScaleAndOrigin();
3dec57ad 631}
e40298d5 632
2f1ae414 633void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
e9576ca5 634{
e40298d5
JS
635 m_externalDeviceOriginX = x;
636 m_externalDeviceOriginY = y;
637 ComputeScaleAndOrigin();
3dec57ad 638}
e40298d5 639
e9576ca5
SC
640void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
641{
e40298d5
JS
642 m_signX = (xLeftRight ? 1 : -1);
643 m_signY = (yBottomUp ? -1 : 1);
644 ComputeScaleAndOrigin();
76a5e5d2 645}
e40298d5 646
2f1ae414
SC
647wxSize wxDC::GetPPI() const
648{
649 return wxSize(72, 72);
650}
e40298d5 651
2f1ae414
SC
652int wxDC::GetDepth() const
653{
c99d6f99 654 if ( IsPortColor( (CGrafPtr) m_macPort ) )
c99d6f99 655 return ( (**GetPortPixMap( (CGrafPtr) m_macPort)).pixelSize ) ;
9b2eab52 656
c99d6f99 657 return 1 ;
2f1ae414 658}
e40298d5 659
3dec57ad 660void wxDC::ComputeScaleAndOrigin()
e9576ca5 661{
e40298d5
JS
662 // CMB: copy scale to see if it changes
663 double origScaleX = m_scaleX;
664 double origScaleY = m_scaleY;
665 m_scaleX = m_logicalScaleX * m_userScaleX;
666 m_scaleY = m_logicalScaleY * m_userScaleY;
667 m_deviceOriginX = m_internalDeviceOriginX + m_externalDeviceOriginX;
668 m_deviceOriginY = m_internalDeviceOriginY + m_externalDeviceOriginY;
9b2eab52 669
e40298d5
JS
670 // CMB: if scale has changed call SetPen to recalulate the line width
671 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
672 {
673 // this is a bit artificial, but we need to force wxDC to think
674 // the pen has changed
7d9aac18
SC
675 wxPen pen(GetPen());
676 m_pen = wxNullPen;
677 SetPen(pen);
e40298d5 678 }
3dec57ad 679}
e40298d5 680
9b2eab52 681void wxDC::SetPalette( const wxPalette& palette )
519cb848
SC
682{
683}
e40298d5 684
9b2eab52 685void wxDC::SetBackgroundMode( int mode )
519cb848 686{
e40298d5 687 m_backgroundMode = mode ;
519cb848 688}
e40298d5 689
9b2eab52 690void wxDC::SetFont( const wxFont &font )
519cb848 691{
3dec57ad
SC
692 m_font = font;
693 m_macFontInstalled = false ;
519cb848 694}
e40298d5 695
9b2eab52 696void wxDC::SetPen( const wxPen &pen )
519cb848 697{
e40298d5
JS
698 if ( m_pen == pen )
699 return ;
9b2eab52 700
e40298d5
JS
701 m_pen = pen;
702 m_macPenInstalled = false ;
519cb848 703}
e40298d5 704
9b2eab52 705void wxDC::SetBrush( const wxBrush &brush )
519cb848 706{
e40298d5
JS
707 if (m_brush == brush)
708 return;
9b2eab52 709
e40298d5
JS
710 m_brush = brush;
711 m_macBrushInstalled = false ;
519cb848 712}
e40298d5 713
9b2eab52 714void wxDC::SetBackground( const wxBrush &brush )
519cb848 715{
e40298d5
JS
716 if (m_backgroundBrush == brush)
717 return;
9b2eab52 718
e40298d5 719 m_backgroundBrush = brush;
9b2eab52
DS
720 if (m_backgroundBrush.Ok())
721 m_macBrushInstalled = false ;
519cb848 722}
e40298d5 723
9b2eab52 724void wxDC::SetLogicalFunction( int function )
519cb848 725{
e40298d5
JS
726 if (m_logicalFunction == function)
727 return;
9b2eab52 728
e40298d5
JS
729 m_logicalFunction = function ;
730 m_macFontInstalled = false ;
731 m_macBrushInstalled = false ;
732 m_macPenInstalled = false ;
519cb848 733}
e40298d5 734
99030bdf 735extern bool wxDoFloodFill(wxDC *dc, wxCoord x, wxCoord y,
3a0a5ada 736 const wxColour & col, int style);
e40298d5 737
387ebd3e 738bool wxDC::DoFloodFill(wxCoord x, wxCoord y,
3a0a5ada
VS
739 const wxColour& col, int style)
740{
387ebd3e 741 return wxDoFloodFill(this, x, y, col, style);
71110b40 742}
e40298d5 743
9b2eab52 744bool wxDC::DoGetPixel( wxCoord x, wxCoord y, wxColour *col ) const
519cb848 745{
9b2eab52
DS
746 wxCHECK_MSG( Ok(), false, wxT("wxDC::DoGetPixel - invalid DC") );
747
8bebc229 748 wxMacFastPortSetter helper(this) ;
9b2eab52
DS
749
750 // NOTE: Get/SetCPixel are slow!
3dec57ad 751 RGBColor colour;
7d9d1fd7 752 GetCPixel( XLOG2DEVMAC(x), YLOG2DEVMAC(y), &colour );
9b2eab52
DS
753
754 // convert from Mac colour to wx
f23d3255 755 col->Set( colour.red >> 8, colour.green >> 8, colour.blue >> 8);
9b2eab52 756
3dec57ad 757 return true ;
519cb848 758}
e40298d5 759
9b2eab52 760void wxDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
519cb848 761{
9b2eab52
DS
762 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawLine - invalid DC"));
763
8bebc229 764 wxMacFastPortSetter helper(this) ;
9b2eab52 765
e40298d5
JS
766 if (m_pen.GetStyle() != wxTRANSPARENT)
767 {
768 MacInstallPen() ;
769 wxCoord offset = ( (m_pen.GetWidth() == 0 ? 1 :
f23d3255 770 m_pen.GetWidth() ) * (wxCoord)m_scaleX - 1) / 2;
7d9d1fd7
SC
771 wxCoord xx1 = XLOG2DEVMAC(x1) - offset;
772 wxCoord yy1 = YLOG2DEVMAC(y1) - offset;
773 wxCoord xx2 = XLOG2DEVMAC(x2) - offset;
774 wxCoord yy2 = YLOG2DEVMAC(y2) - offset;
9b2eab52 775
2301e281
RR
776 if ((m_pen.GetCap() == wxCAP_ROUND) &&
777 (m_pen.GetWidth() <= 1))
e40298d5
JS
778 {
779 // Implement LAST_NOT for MAC at least for
780 // orthogonal lines. RR.
781 if (xx1 == xx2)
782 {
783 if (yy1 < yy2)
784 yy2--;
785 if (yy1 > yy2)
786 yy2++;
787 }
9b2eab52 788
e40298d5
JS
789 if (yy1 == yy2)
790 {
791 if (xx1 < xx2)
792 xx2--;
793 if (xx1 > xx2)
794 xx2++;
795 }
796 }
9b2eab52 797
e40298d5
JS
798 ::MoveTo(xx1, yy1);
799 ::LineTo(xx2, yy2);
800 }
519cb848 801}
e40298d5 802
9b2eab52 803void wxDC::DoCrossHair( wxCoord x, wxCoord y )
519cb848 804{
9b2eab52
DS
805 wxCHECK_RET(Ok(), wxT("wxDC::DoCrossHair - invalid DC"));
806
8bebc229 807 wxMacFastPortSetter helper(this) ;
9b2eab52 808
3dec57ad
SC
809 if (m_pen.GetStyle() != wxTRANSPARENT)
810 {
9b2eab52
DS
811 int w = 0, h = 0;
812
3dec57ad 813 GetSize( &w, &h );
7d9d1fd7
SC
814 wxCoord xx = XLOG2DEVMAC(x);
815 wxCoord yy = YLOG2DEVMAC(y);
9b2eab52 816
3dec57ad 817 MacInstallPen();
7d9d1fd7
SC
818 ::MoveTo( XLOG2DEVMAC(0), yy );
819 ::LineTo( XLOG2DEVMAC(w), yy );
820 ::MoveTo( xx, YLOG2DEVMAC(0) );
821 ::LineTo( xx, YLOG2DEVMAC(h) );
66a09d47 822 CalcBoundingBox(x, y);
9b2eab52 823 CalcBoundingBox(x + w, y + h);
3dec57ad
SC
824 }
825}
e40298d5 826
3dec57ad 827/*
e40298d5
JS
828* To draw arcs properly the angles need to be converted from the WX style:
829* Angles start on the +ve X axis and go anti-clockwise (As you would draw on
830* a normal axis on paper).
831* TO
832* the Mac style:
833* Angles start on the +ve y axis and go clockwise.
834*/
835
3dec57ad
SC
836static double wxConvertWXangleToMACangle(double angle)
837{
2beae4b5 838 double newAngle = 90 - angle ;
9b2eab52
DS
839
840 while ( newAngle > 360 )
841 newAngle -= 360 ;
842 while ( newAngle < 0 )
843 newAngle += 360 ;
844
2beae4b5 845 return newAngle ;
519cb848 846}
e40298d5 847
9b2eab52 848void wxDC::DoDrawArc( wxCoord x1, wxCoord y1,
e40298d5
JS
849 wxCoord x2, wxCoord y2,
850 wxCoord xc, wxCoord yc )
519cb848 851{
9b2eab52
DS
852 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawArc - invalid DC"));
853
8bebc229 854 wxMacFastPortSetter helper(this) ;
9b2eab52 855
7d9d1fd7
SC
856 wxCoord xx1 = XLOG2DEVMAC(x1);
857 wxCoord yy1 = YLOG2DEVMAC(y1);
858 wxCoord xx2 = XLOG2DEVMAC(x2);
859 wxCoord yy2 = YLOG2DEVMAC(y2);
860 wxCoord xxc = XLOG2DEVMAC(xc);
861 wxCoord yyc = YLOG2DEVMAC(yc);
9b2eab52 862
3dec57ad
SC
863 double dx = xx1 - xxc;
864 double dy = yy1 - yyc;
f23d3255 865 double radius = sqrt((double)(dx * dx + dy * dy));
9b2eab52 866 wxCoord rad = (wxCoord)radius;
3dec57ad 867 double radius1, radius2;
9b2eab52 868
3dec57ad
SC
869 if (xx1 == xx2 && yy1 == yy2)
870 {
871 radius1 = 0.0;
872 radius2 = 360.0;
873 }
874 else if (radius == 0.0)
875 {
876 radius1 = radius2 = 0.0;
877 }
878 else
879 {
880 radius1 = (xx1 - xxc == 0) ?
881 (yy1 - yyc < 0) ? 90.0 : -90.0 :
f23d3255 882 -atan2(double(yy1 - yyc), double(xx1 - xxc)) * RAD2DEG;
e40298d5 883 radius2 = (xx2 - xxc == 0) ?
3dec57ad 884 (yy2 - yyc < 0) ? 90.0 : -90.0 :
f23d3255 885 -atan2(double(yy2 - yyc), double(xx2 - xxc)) * RAD2DEG;
e40298d5 886 }
9b2eab52 887
e40298d5 888 wxCoord alpha2 = wxCoord(radius2 - radius1);
3dec57ad 889 wxCoord alpha1 = wxCoord(wxConvertWXangleToMACangle(radius1));
9b2eab52
DS
890 while ( alpha2 < 0 )
891 alpha2 += 360 ;
a99915c2 892 alpha2 = -alpha2 ;
3dec57ad 893 Rect r = { yyc - rad, xxc - rad, yyc + rad, xxc + rad };
9b2eab52
DS
894
895 if (m_brush.GetStyle() != wxTRANSPARENT)
896 {
3dec57ad
SC
897 MacInstallBrush();
898 PaintArc(&r, alpha1, alpha2);
899 }
9b2eab52
DS
900
901 if (m_pen.GetStyle() != wxTRANSPARENT)
902 {
3dec57ad
SC
903 MacInstallPen();
904 FrameArc(&r, alpha1, alpha2);
905 }
519cb848 906}
e40298d5 907
9b2eab52 908void wxDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
e40298d5 909 double sa, double ea )
519cb848 910{
9b2eab52 911 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawEllepticArc - invalid DC"));
8bebc229 912 wxMacFastPortSetter helper(this) ;
3dec57ad 913 Rect r;
9b2eab52
DS
914
915 // Order important Mac in opposite direction to wx
2beae4b5 916 // we have to make sure that the filling is always counter-clockwise
9b2eab52 917 double angle = sa - ea;
2beae4b5
SC
918 if ( angle > 0 )
919 angle -= 360 ;
9b2eab52 920
7d9d1fd7
SC
921 wxCoord xx = XLOG2DEVMAC(x);
922 wxCoord yy = YLOG2DEVMAC(y);
3dec57ad
SC
923 wxCoord ww = m_signX * XLOG2DEVREL(w);
924 wxCoord hh = m_signY * YLOG2DEVREL(h);
9b2eab52 925
3dec57ad 926 // handle -ve width and/or height
9b2eab52
DS
927 if (ww < 0)
928 {
929 ww = -ww;
930 xx = xx - ww;
931 }
932
933 if (hh < 0)
934 {
935 hh = -hh;
936 yy = yy - hh;
937 }
938
3dec57ad 939 sa = wxConvertWXangleToMACangle(sa);
3dec57ad
SC
940 r.top = yy;
941 r.left = xx;
942 r.bottom = yy + hh;
943 r.right = xx + ww;
9b2eab52
DS
944
945 if (m_brush.GetStyle() != wxTRANSPARENT)
946 {
3dec57ad
SC
947 MacInstallBrush();
948 PaintArc(&r, (short)sa, (short)angle);
949 }
9b2eab52
DS
950
951 if (m_pen.GetStyle() != wxTRANSPARENT)
952 {
3dec57ad
SC
953 MacInstallPen();
954 FrameArc(&r, (short)sa, (short)angle);
955 }
519cb848 956}
e40298d5 957
9b2eab52 958void wxDC::DoDrawPoint( wxCoord x, wxCoord y )
519cb848 959{
9b2eab52
DS
960 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawPoint - invalid DC"));
961
8bebc229 962 wxMacFastPortSetter helper(this) ;
9b2eab52 963
e40298d5
JS
964 if (m_pen.GetStyle() != wxTRANSPARENT)
965 {
99030bdf 966 wxCoord xx1 = XLOG2DEVMAC(x);
7d9d1fd7 967 wxCoord yy1 = YLOG2DEVMAC(y);
5e420341 968 RGBColor pencolor = MAC_WXCOLORREF( m_pen.GetColour().GetPixel());
9b2eab52
DS
969
970 // NOTE: Get/SetCPixel are slow!
971 ::SetCPixel( xx1, yy1, &pencolor) ;
5e420341 972 CalcBoundingBox(x, y);
e40298d5 973 }
519cb848 974}
e40298d5 975
9b2eab52 976void wxDC::DoDrawLines(int n, wxPoint points[],
e40298d5
JS
977 wxCoord xoffset, wxCoord yoffset)
978{
9b2eab52
DS
979 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawLines - invalid DC"));
980
e40298d5
JS
981 if (m_pen.GetStyle() == wxTRANSPARENT)
982 return;
9b2eab52
DS
983
984 wxMacFastPortSetter helper(this) ;
985
e40298d5
JS
986 MacInstallPen() ;
987 wxCoord offset = ( (m_pen.GetWidth() == 0 ? 1 :
988 m_pen.GetWidth() ) * (wxCoord)m_scaleX - 1) / 2 ;
f23d3255 989
e40298d5
JS
990 wxCoord x1, x2 , y1 , y2 ;
991 x1 = XLOG2DEVMAC(points[0].x + xoffset);
992 y1 = YLOG2DEVMAC(points[0].y + yoffset);
9b2eab52 993
f23d3255 994 ::MoveTo( x1 - offset, y1 - offset );
e40298d5
JS
995 for (int i = 0; i < n-1; i++)
996 {
9b2eab52
DS
997 x2 = XLOG2DEVMAC(points[i + 1].x + xoffset);
998 y2 = YLOG2DEVMAC(points[i + 1].y + yoffset);
e40298d5
JS
999 ::LineTo( x2 - offset, y2 - offset );
1000 }
519cb848 1001}
e40298d5 1002
9b2eab52 1003void wxDC::DoDrawPolygon(int n, wxPoint points[],
d84afea9
GD
1004 wxCoord xoffset, wxCoord yoffset,
1005 int fillStyle )
519cb848 1006{
9b2eab52
DS
1007 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawPolygon - invalid DC"));
1008
75f7bc3b
SC
1009 if ( m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT )
1010 return ;
9b2eab52
DS
1011
1012 wxMacFastPortSetter helper(this) ;
1013
1014 wxCoord x1, x2 , y1 , y2 ;
e40298d5
JS
1015 PolyHandle polygon = OpenPoly();
1016 x2 = x1 = XLOG2DEVMAC(points[0].x + xoffset);
1017 y2 = y1 = YLOG2DEVMAC(points[0].y + yoffset);
9b2eab52
DS
1018
1019 ::MoveTo(x1, y1);
e40298d5
JS
1020 for (int i = 1; i < n; i++)
1021 {
1022 x2 = XLOG2DEVMAC(points[i].x + xoffset);
1023 y2 = YLOG2DEVMAC(points[i].y + yoffset);
1024 ::LineTo(x2, y2);
1025 }
9b2eab52 1026
76a5e5d2
SC
1027 // close the polyline if necessary
1028 if ( x1 != x2 || y1 != y2 )
f23d3255 1029 ::LineTo( x1, y1 ) ;
e40298d5 1030 ClosePoly();
9b2eab52 1031
e40298d5
JS
1032 if (m_brush.GetStyle() != wxTRANSPARENT)
1033 {
1034 MacInstallBrush();
1035 ::PaintPoly( polygon );
1036 }
9b2eab52 1037
e40298d5
JS
1038 if (m_pen.GetStyle() != wxTRANSPARENT)
1039 {
1040 MacInstallPen() ;
1041 ::FramePoly( polygon ) ;
1042 }
9b2eab52 1043
e40298d5 1044 KillPoly( polygon );
519cb848 1045}
e40298d5 1046
3dec57ad 1047void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
519cb848 1048{
9b2eab52
DS
1049 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawRectangle - invalid DC"));
1050
8bebc229 1051 wxMacFastPortSetter helper(this) ;
9b2eab52 1052
e40298d5
JS
1053 wxCoord xx = XLOG2DEVMAC(x);
1054 wxCoord yy = YLOG2DEVMAC(y);
1055 wxCoord ww = m_signX * XLOG2DEVREL(width);
1056 wxCoord hh = m_signY * YLOG2DEVREL(height);
9b2eab52 1057
e40298d5
JS
1058 // CMB: draw nothing if transformed w or h is 0
1059 if (ww == 0 || hh == 0)
1060 return;
9b2eab52 1061
e40298d5
JS
1062 // CMB: handle -ve width and/or height
1063 if (ww < 0)
1064 {
1065 ww = -ww;
1066 xx = xx - ww;
1067 }
9b2eab52 1068
e40298d5
JS
1069 if (hh < 0)
1070 {
1071 hh = -hh;
1072 yy = yy - hh;
1073 }
9b2eab52 1074
e40298d5 1075 Rect rect = { yy , xx , yy + hh , xx + ww } ;
9b2eab52 1076
e40298d5
JS
1077 if (m_brush.GetStyle() != wxTRANSPARENT)
1078 {
1079 MacInstallBrush() ;
1080 ::PaintRect( &rect ) ;
1081 }
9b2eab52 1082
e40298d5
JS
1083 if (m_pen.GetStyle() != wxTRANSPARENT)
1084 {
1085 MacInstallPen() ;
1086 ::FrameRect( &rect ) ;
1087 }
519cb848 1088}
e40298d5 1089
9b2eab52 1090void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
e40298d5
JS
1091 wxCoord width, wxCoord height,
1092 double radius)
519cb848 1093{
9b2eab52
DS
1094 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawRoundedRectangle - invalid DC"));
1095
8bebc229 1096 wxMacFastPortSetter helper(this) ;
99030bdf 1097 if (radius < 0.0)
e40298d5
JS
1098 radius = - radius * ((width < height) ? width : height);
1099 wxCoord xx = XLOG2DEVMAC(x);
1100 wxCoord yy = YLOG2DEVMAC(y);
1101 wxCoord ww = m_signX * XLOG2DEVREL(width);
1102 wxCoord hh = m_signY * YLOG2DEVREL(height);
9b2eab52 1103
e40298d5
JS
1104 // CMB: draw nothing if transformed w or h is 0
1105 if (ww == 0 || hh == 0)
1106 return;
9b2eab52 1107
e40298d5
JS
1108 // CMB: handle -ve width and/or height
1109 if (ww < 0)
1110 {
1111 ww = -ww;
1112 xx = xx - ww;
1113 }
9b2eab52 1114
e40298d5
JS
1115 if (hh < 0)
1116 {
1117 hh = -hh;
1118 yy = yy - hh;
1119 }
9b2eab52 1120
e40298d5 1121 Rect rect = { yy , xx , yy + hh , xx + ww } ;
9b2eab52 1122
e40298d5
JS
1123 if (m_brush.GetStyle() != wxTRANSPARENT)
1124 {
1125 MacInstallBrush() ;
1126 ::PaintRoundRect( &rect , int(radius * 2) , int(radius * 2) ) ;
1127 }
9b2eab52 1128
e40298d5
JS
1129 if (m_pen.GetStyle() != wxTRANSPARENT)
1130 {
1131 MacInstallPen() ;
1132 ::FrameRoundRect( &rect , int(radius * 2) , int(radius * 2) ) ;
1133 }
519cb848 1134}
e40298d5 1135
9b2eab52 1136void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
519cb848 1137{
9b2eab52
DS
1138 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawEllipse - invalid DC"));
1139
8bebc229 1140 wxMacFastPortSetter helper(this) ;
9b2eab52 1141
e40298d5
JS
1142 wxCoord xx = XLOG2DEVMAC(x);
1143 wxCoord yy = YLOG2DEVMAC(y);
1144 wxCoord ww = m_signX * XLOG2DEVREL(width);
1145 wxCoord hh = m_signY * YLOG2DEVREL(height);
9b2eab52 1146
e40298d5
JS
1147 // CMB: draw nothing if transformed w or h is 0
1148 if (ww == 0 || hh == 0)
1149 return;
9b2eab52 1150
e40298d5
JS
1151 // CMB: handle -ve width and/or height
1152 if (ww < 0)
1153 {
1154 ww = -ww;
1155 xx = xx - ww;
1156 }
9b2eab52 1157
e40298d5
JS
1158 if (hh < 0)
1159 {
1160 hh = -hh;
1161 yy = yy - hh;
1162 }
9b2eab52 1163
e40298d5 1164 Rect rect = { yy , xx , yy + hh , xx + ww } ;
9b2eab52 1165
e40298d5
JS
1166 if (m_brush.GetStyle() != wxTRANSPARENT)
1167 {
1168 MacInstallBrush() ;
1169 ::PaintOval( &rect ) ;
1170 }
9b2eab52 1171
e40298d5
JS
1172 if (m_pen.GetStyle() != wxTRANSPARENT)
1173 {
1174 MacInstallPen() ;
1175 ::FrameOval( &rect ) ;
1176 }
519cb848
SC
1177}
1178
9b2eab52 1179bool wxDC::CanDrawBitmap(void) const
519cb848 1180{
e40298d5 1181 return true ;
519cb848
SC
1182}
1183
9b2eab52 1184bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
e40298d5 1185 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool useMask,
f23d3255 1186 wxCoord xsrcMask, wxCoord ysrcMask )
519cb848 1187{
9b2eab52
DS
1188 wxCHECK_MSG(Ok(), false, wxT("wxDC::DoBlit - invalid DC"));
1189 wxCHECK_MSG(source->Ok(), false, wxT("wxDC::DoBlit - invalid source DC"));
1190
76a5e5d2 1191 if ( logical_func == wxNO_OP )
ab9d0a8c 1192 return true ;
9b2eab52 1193
0cbff120
JS
1194 if (xsrcMask == -1 && ysrcMask == -1)
1195 {
9b2eab52
DS
1196 xsrcMask = xsrc;
1197 ysrcMask = ysrc;
0cbff120 1198 }
9b2eab52 1199
76a5e5d2 1200 // correct the parameter in case this dc does not have a mask at all
76a5e5d2
SC
1201 if ( useMask && !source->m_macMask )
1202 useMask = false ;
9b2eab52 1203
e40298d5
JS
1204 Rect srcrect , dstrect ;
1205 srcrect.top = source->YLOG2DEVMAC(ysrc) ;
1206 srcrect.left = source->XLOG2DEVMAC(xsrc) ;
1207 srcrect.right = source->XLOG2DEVMAC(xsrc + width ) ;
1208 srcrect.bottom = source->YLOG2DEVMAC(ysrc + height) ;
1209 dstrect.top = YLOG2DEVMAC(ydest) ;
1210 dstrect.left = XLOG2DEVMAC(xdest) ;
1211 dstrect.bottom = YLOG2DEVMAC(ydest + height ) ;
1212 dstrect.right = XLOG2DEVMAC(xdest + width ) ;
76a5e5d2
SC
1213 short mode = kUnsupportedMode ;
1214 bool invertDestinationFirst = false ;
9b2eab52 1215
76a5e5d2
SC
1216 switch ( logical_func )
1217 {
e40298d5 1218 case wxAND: // src AND dst
4a95c885 1219 mode = adMin ; // ok
e40298d5 1220 break ;
9b2eab52 1221
e40298d5
JS
1222 case wxAND_INVERT: // (NOT src) AND dst
1223 mode = notSrcOr ; // ok
1224 break ;
9b2eab52 1225
e40298d5
JS
1226 case wxAND_REVERSE:// src AND (NOT dst)
1227 invertDestinationFirst = true ;
1228 mode = srcOr ;
1229 break ;
9b2eab52 1230
e40298d5
JS
1231 case wxCLEAR: // 0
1232 mode = kEmulatedMode ;
1233 break ;
9b2eab52 1234
e40298d5
JS
1235 case wxCOPY: // src
1236 mode = srcCopy ; // ok
1237 break ;
9b2eab52 1238
e40298d5
JS
1239 case wxEQUIV: // (NOT src) XOR dst
1240 mode = srcXor ; // ok
1241 break ;
9b2eab52 1242
e40298d5
JS
1243 case wxINVERT: // NOT dst
1244 mode = kEmulatedMode ; //or hilite ;
1245 break ;
9b2eab52 1246
e40298d5
JS
1247 case wxNAND: // (NOT src) OR (NOT dst)
1248 invertDestinationFirst = true ;
1249 mode = srcBic ;
1250 break ;
9b2eab52 1251
e40298d5
JS
1252 case wxNOR: // (NOT src) AND (NOT dst)
1253 invertDestinationFirst = true ;
1254 mode = notSrcOr ;
1255 break ;
9b2eab52 1256
e40298d5
JS
1257 case wxNO_OP: // dst
1258 mode = kEmulatedMode ; // this has already been handled upon entry
1259 break ;
9b2eab52 1260
e40298d5
JS
1261 case wxOR: // src OR dst
1262 mode = notSrcBic ;
1263 break ;
9b2eab52 1264
e40298d5
JS
1265 case wxOR_INVERT: // (NOT src) OR dst
1266 mode = srcBic ;
1267 break ;
9b2eab52 1268
e40298d5
JS
1269 case wxOR_REVERSE: // src OR (NOT dst)
1270 invertDestinationFirst = true ;
1271 mode = notSrcBic ;
1272 break ;
9b2eab52 1273
e40298d5
JS
1274 case wxSET: // 1
1275 mode = kEmulatedMode ;
1276 break ;
9b2eab52 1277
e40298d5
JS
1278 case wxSRC_INVERT: // (NOT src)
1279 mode = notSrcCopy ; // ok
1280 break ;
9b2eab52 1281
e40298d5
JS
1282 case wxXOR: // src XOR dst
1283 mode = notSrcXor ; // ok
1284 break ;
9b2eab52 1285
e40298d5
JS
1286 default :
1287 break ;
76a5e5d2 1288 }
9b2eab52 1289
76a5e5d2
SC
1290 if ( mode == kUnsupportedMode )
1291 {
427ff662 1292 wxFAIL_MSG(wxT("unsupported blitting mode" ));
9b2eab52 1293
ab9d0a8c 1294 return false ;
76a5e5d2 1295 }
9b2eab52 1296
e40298d5
JS
1297 CGrafPtr sourcePort = (CGrafPtr) source->m_macPort ;
1298 PixMapHandle bmappixels = GetGWorldPixMap( sourcePort ) ;
1299 if ( LockPixels(bmappixels) )
1300 {
8bebc229 1301 wxMacFastPortSetter helper(this) ;
9b2eab52 1302
e40298d5
JS
1303 if ( source->GetDepth() == 1 )
1304 {
1305 RGBForeColor( &MAC_WXCOLORREF(m_textForegroundColour.GetPixel()) ) ;
1306 RGBBackColor( &MAC_WXCOLORREF(m_textBackgroundColour.GetPixel()) ) ;
76a5e5d2
SC
1307 }
1308 else
1309 {
1310 // the modes need this, otherwise we'll end up having really nice colors...
f23d3255
DS
1311 RGBColor white = { 0xFFFF, 0xFFFF, 0xFFFF } ;
1312 RGBColor black = { 0, 0, 0 } ;
9b2eab52 1313
e40298d5
JS
1314 RGBForeColor( &black ) ;
1315 RGBBackColor( &white ) ;
76a5e5d2 1316 }
9b2eab52 1317
e40298d5
JS
1318 if ( useMask && source->m_macMask )
1319 {
1320 if ( mode == srcCopy )
1321 {
1322 if ( LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) )
1323 {
1324 CopyMask( GetPortBitMapForCopyBits( sourcePort ) ,
1325 GetPortBitMapForCopyBits( MAC_WXHBITMAP(source->m_macMask) ) ,
1326 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ) ,
1327 &srcrect, &srcrect , &dstrect ) ;
1328 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) ;
1329 }
1330 }
1331 else
1332 {
1dcbbdcf 1333 RgnHandle clipRgn = NewRgn() ;
76a5e5d2
SC
1334 LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) ;
1335 BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) ;
1336 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) ;
1dcbbdcf 1337 OffsetRgn( clipRgn , -srcrect.left + dstrect.left, -srcrect.top + dstrect.top ) ;
9b2eab52 1338
76a5e5d2
SC
1339 if ( mode == kEmulatedMode )
1340 {
1341 Pattern pat ;
9b2eab52 1342
e40298d5 1343 ::PenPat(GetQDGlobalsBlack(&pat));
76a5e5d2
SC
1344 if ( logical_func == wxSET )
1345 {
1346 RGBColor col= { 0xFFFF, 0xFFFF, 0xFFFF } ;
e40298d5 1347 ::RGBForeColor( &col ) ;
76a5e5d2
SC
1348 ::PaintRgn( clipRgn ) ;
1349 }
1350 else if ( logical_func == wxCLEAR )
1351 {
1352 RGBColor col= { 0x0000, 0x0000, 0x0000 } ;
e40298d5 1353 ::RGBForeColor( &col ) ;
76a5e5d2
SC
1354 ::PaintRgn( clipRgn ) ;
1355 }
1356 else if ( logical_func == wxINVERT )
1357 {
1358 MacInvertRgn( clipRgn ) ;
1359 }
1360 else
1361 {
1362 for ( int y = 0 ; y < srcrect.right - srcrect.left ; ++y )
1363 {
1364 for ( int x = 0 ; x < srcrect.bottom - srcrect.top ; ++x )
1365 {
1366 Point dstPoint = { dstrect.top + y , dstrect.left + x } ;
1367 Point srcPoint = { srcrect.top + y , srcrect.left + x } ;
1368 if ( PtInRgn( dstPoint , clipRgn ) )
1369 {
9b2eab52
DS
1370 RGBColor srcColor, dstColor ;
1371
1372 // NOTE: Get/SetCPixel are slow!
76a5e5d2 1373 SetPort( (GrafPtr) sourcePort ) ;
9b2eab52 1374 GetCPixel( srcPoint.h , srcPoint.v , &srcColor ) ;
76a5e5d2
SC
1375 SetPort( (GrafPtr) m_macPort ) ;
1376 GetCPixel( dstPoint.h , dstPoint.v , &dstColor ) ;
f23d3255 1377 wxMacCalculateColour( logical_func , srcColor , dstColor ) ;
76a5e5d2
SC
1378 SetCPixel( dstPoint.h , dstPoint.v , &dstColor ) ;
1379 }
1380 }
1381 }
76a5e5d2
SC
1382 }
1383 }
1384 else
1385 {
1386 if ( invertDestinationFirst )
76a5e5d2 1387 MacInvertRgn( clipRgn ) ;
9b2eab52 1388
e40298d5
JS
1389 CopyBits( GetPortBitMapForCopyBits( sourcePort ) ,
1390 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ) ,
1391 &srcrect, &dstrect, mode, clipRgn ) ;
1392 }
9b2eab52 1393
e40298d5
JS
1394 DisposeRgn( clipRgn ) ;
1395 }
1396 }
1397 else
1398 {
0d54461f
SC
1399 RgnHandle clipRgn = NewRgn() ;
1400 SetRectRgn( clipRgn , dstrect.left , dstrect.top , dstrect.right , dstrect.bottom ) ;
9b2eab52 1401
e40298d5
JS
1402 if ( mode == kEmulatedMode )
1403 {
0d54461f 1404 Pattern pat ;
9b2eab52 1405
e40298d5 1406 ::PenPat(GetQDGlobalsBlack(&pat));
0d54461f
SC
1407 if ( logical_func == wxSET )
1408 {
1409 RGBColor col= { 0xFFFF, 0xFFFF, 0xFFFF } ;
e40298d5 1410 ::RGBForeColor( &col ) ;
0d54461f
SC
1411 ::PaintRgn( clipRgn ) ;
1412 }
1413 else if ( logical_func == wxCLEAR )
1414 {
9b2eab52
DS
1415 RGBColor col = { 0x0000, 0x0000, 0x0000 } ;
1416
e40298d5 1417 ::RGBForeColor( &col ) ;
0d54461f
SC
1418 ::PaintRgn( clipRgn ) ;
1419 }
1420 else if ( logical_func == wxINVERT )
1421 {
1422 MacInvertRgn( clipRgn ) ;
1423 }
1424 else
1425 {
1426 for ( int y = 0 ; y < srcrect.right - srcrect.left ; ++y )
1427 {
1428 for ( int x = 0 ; x < srcrect.bottom - srcrect.top ; ++x )
1429 {
1430 Point dstPoint = { dstrect.top + y , dstrect.left + x } ;
1431 Point srcPoint = { srcrect.top + y , srcrect.left + x } ;
0d54461f 1432 {
9b2eab52
DS
1433 RGBColor srcColor, dstColor ;
1434
1435 // NOTE: Get/SetCPixel are slow!
0d54461f 1436 SetPort( (GrafPtr) sourcePort ) ;
9b2eab52 1437 GetCPixel( srcPoint.h , srcPoint.v , &srcColor) ;
0d54461f
SC
1438 SetPort( (GrafPtr) m_macPort ) ;
1439 GetCPixel( dstPoint.h , dstPoint.v , &dstColor ) ;
f23d3255 1440 wxMacCalculateColour( logical_func , srcColor , dstColor ) ;
0d54461f
SC
1441 SetCPixel( dstPoint.h , dstPoint.v , &dstColor ) ;
1442 }
1443 }
1444 }
1445 }
e40298d5
JS
1446 }
1447 else
1448 {
0d54461f 1449 if ( invertDestinationFirst )
0d54461f 1450 MacInvertRgn( clipRgn ) ;
9b2eab52 1451
e40298d5
JS
1452 CopyBits( GetPortBitMapForCopyBits( sourcePort ) ,
1453 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ) ,
1454 &srcrect, &dstrect, mode, NULL ) ;
1455 }
9b2eab52 1456
e40298d5
JS
1457 DisposeRgn( clipRgn ) ;
1458 }
9b2eab52 1459
e40298d5
JS
1460 UnlockPixels( bmappixels ) ;
1461 }
9b2eab52 1462
e40298d5
JS
1463 m_macPenInstalled = false ;
1464 m_macBrushInstalled = false ;
1465 m_macFontInstalled = false ;
9b2eab52 1466
ab9d0a8c 1467 return true;
e40298d5
JS
1468}
1469
9b2eab52 1470void wxDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y,
3dec57ad 1471 double angle)
2f1ae414 1472{
9b2eab52
DS
1473 // TODO: support text background color (only possible by hand, ATSUI does not support it)
1474 wxCHECK_RET( Ok(), wxT("wxDC::DoDrawRotatedText - invalid DC") );
26b9b4e1 1475
32907dbd
SC
1476 if ( str.Length() == 0 )
1477 return ;
fc58c6be 1478
8bebc229 1479 wxMacFastPortSetter helper(this) ;
66a09d47 1480 MacInstallFont() ;
427ff662 1481
9b2eab52 1482#if 0
66a09d47 1483 if ( 0 )
3dec57ad 1484 {
66a09d47 1485 m_macFormerAliasState = IsAntiAliasedTextEnabled(&m_macFormerAliasSize);
facd6764 1486 SetAntiAliasedTextEnabled(true, SInt16(m_scaleY * m_font.MacGetFontSize()));
66a09d47 1487 m_macAliasWasEnabled = true ;
3dec57ad 1488 }
9b2eab52
DS
1489#endif
1490
66a09d47 1491 OSStatus status = noErr ;
427ff662
SC
1492 ATSUTextLayout atsuLayout ;
1493 UniCharCount chars = str.Length() ;
96cadce3 1494 UniChar* ubuf = NULL ;
9b2eab52 1495
96cadce3 1496#if SIZEOF_WCHAR_T == 4
d9d488cf 1497 wxMBConvUTF16 converter ;
427ff662 1498#if wxUSE_UNICODE
ab9d0a8c
WS
1499 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ;
1500 ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
1501 converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 ) ;
96cadce3
SC
1502#else
1503 const wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
f23d3255 1504 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ;
ab9d0a8c
WS
1505 ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
1506 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ) ;
96cadce3
SC
1507#endif
1508 chars = unicharlen / 2 ;
1509#else
1510#if wxUSE_UNICODE
1511 ubuf = (UniChar*) str.wc_str() ;
427ff662 1512#else
939fba6c 1513 wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
96cadce3
SC
1514 chars = wxWcslen( wchar.data() ) ;
1515 ubuf = (UniChar*) wchar.data() ;
427ff662 1516#endif
96cadce3
SC
1517#endif
1518
1519 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1520 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ;
1521
427ff662 1522 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") );
28dd2407
SC
1523
1524 status = ::ATSUSetTransientFontMatching( atsuLayout , true ) ;
1525 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
1526
3340066a 1527 int iAngle = int( angle );
2beae4b5
SC
1528 int drawX = XLOG2DEVMAC(x) ;
1529 int drawY = YLOG2DEVMAC(y) ;
ab9d0a8c 1530
9b2eab52
DS
1531 ATSUTextMeasurement textBefore, textAfter ;
1532 ATSUTextMeasurement ascent, descent ;
ab9d0a8c 1533
3340066a 1534 if ( abs(iAngle) > 0 )
32907dbd 1535 {
3340066a 1536 Fixed atsuAngle = IntToFixed( iAngle ) ;
9b2eab52 1537
e40298d5
JS
1538 ATSUAttributeTag atsuTags[] =
1539 {
1540 kATSULineRotationTag ,
1541 } ;
1542 ByteCount atsuSizes[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
1543 {
1544 sizeof( Fixed ) ,
1545 } ;
1546 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
1547 {
1548 &atsuAngle ,
1549 } ;
9b2eab52 1550
e40298d5
JS
1551 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags)/sizeof(ATSUAttributeTag),
1552 atsuTags, atsuSizes, atsuValues ) ;
1553 }
9b2eab52 1554
e40298d5
JS
1555 status = ::ATSUMeasureText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1556 &textBefore , &textAfter, &ascent , &descent );
ab9d0a8c 1557
f23d3255
DS
1558 drawX += (int)(sin(angle / RAD2DEG) * FixedToInt(ascent));
1559 drawY += (int)(cos(angle / RAD2DEG) * FixedToInt(ascent));
32907dbd 1560 status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
e40298d5 1561 IntToFixed(drawX) , IntToFixed(drawY) );
9b2eab52 1562
427ff662 1563 wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") );
9b2eab52 1564
66a09d47 1565 Rect rect ;
e40298d5
JS
1566 status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1567 IntToFixed(drawX) , IntToFixed(drawY) , &rect );
427ff662 1568 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
f23d3255 1569
66a09d47
SC
1570 OffsetRect( &rect , -m_macLocalOrigin.x , -m_macLocalOrigin.y ) ;
1571 CalcBoundingBox(XDEV2LOG(rect.left), YDEV2LOG(rect.top) );
1572 CalcBoundingBox(XDEV2LOG(rect.right), YDEV2LOG(rect.bottom) );
1573 ::ATSUDisposeTextLayout(atsuLayout);
9b2eab52 1574
96cadce3
SC
1575#if SIZEOF_WCHAR_T == 4
1576 free( ubuf ) ;
1577#endif
2f1ae414 1578}
e40298d5 1579
9b2eab52 1580void wxDC::DoDrawText(const wxString& strtext, wxCoord x, wxCoord y)
99030bdf 1581{
9b2eab52 1582 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawText - invalid DC"));
26b9b4e1 1583
8bebc229 1584 wxMacFastPortSetter helper(this) ;
e40298d5
JS
1585 long xx = XLOG2DEVMAC(x);
1586 long yy = YLOG2DEVMAC(y);
9b2eab52 1587
99030bdf 1588#if TARGET_CARBON
e40298d5 1589 bool useDrawThemeText = ( DrawThemeTextBox != (void*) kUnresolvedCFragSymbolAddress ) ;
b7047aa5 1590 if ( UMAGetSystemVersion() < 0x1000 || IsKindOf(CLASSINFO( wxPrinterDC ) ) || m_font.GetNoAntiAliasing() )
e40298d5 1591 useDrawThemeText = false ;
6606ecac 1592#endif
9b2eab52 1593
e40298d5 1594 MacInstallFont() ;
fc58c6be 1595
e40298d5
JS
1596 FontInfo fi ;
1597 ::GetFontInfo( &fi ) ;
9b2eab52 1598
99030bdf 1599#if TARGET_CARBON
e40298d5 1600 if ( !useDrawThemeText )
e40298d5 1601 yy += fi.ascent ;
9b2eab52
DS
1602#else
1603 yy += fi.ascent ;
1604#endif
1605
1606 ::TextMode( (m_backgroundMode == wxTRANSPARENT) ? srcOr : srcCopy ) ;
e40298d5 1607 ::MoveTo( xx , yy );
9b2eab52 1608
e40298d5
JS
1609 int line = 0 ;
1610 {
b93f3832 1611 wxString linetext = strtext ;
9b2eab52 1612
6606ecac 1613#if TARGET_CARBON
32907dbd 1614 if ( useDrawThemeText )
e40298d5 1615 {
9b2eab52
DS
1616 Rect frame = {
1617 yy + line*(fi.descent + fi.ascent + fi.leading), xx ,
1618 yy + (line+1)*(fi.descent + fi.ascent + fi.leading) , xx + 10000 } ;
a9412f8f 1619 wxMacCFStringHolder mString( linetext , m_font.GetEncoding()) ;
4a95c885 1620
5fa00d24
SC
1621 if ( m_backgroundMode != wxTRANSPARENT )
1622 {
9b2eab52 1623 Point bounds = {0, 0} ;
e40298d5
JS
1624 Rect background = frame ;
1625 SInt16 baseline ;
1626 ::GetThemeTextDimensions( mString,
facd6764 1627 m_font.MacGetThemeFontID() ,
e40298d5
JS
1628 kThemeStateActive,
1629 false,
1630 &bounds,
1631 &baseline );
1632 background.right = background.left + bounds.h ;
1633 background.bottom = background.top + bounds.v ;
1634 ::EraseRect( &background ) ;
5fa00d24 1635 }
9b2eab52 1636
e40298d5 1637 ::DrawThemeTextBox( mString,
facd6764 1638 m_font.MacGetThemeFontID() ,
e40298d5
JS
1639 kThemeStateActive,
1640 false,
1641 &frame,
1642 teJustLeft,
9b2eab52 1643 NULL );
32907dbd
SC
1644 }
1645 else
6606ecac 1646#endif
99030bdf 1647 {
ab9d0a8c 1648 wxCharBuffer text = linetext.mb_str(wxConvLocal) ;
427ff662
SC
1649 ::DrawText( text , 0 , strlen(text) ) ;
1650 }
e40298d5 1651 }
9b2eab52 1652
e40298d5 1653 ::TextMode( srcOr ) ;
519cb848 1654}
e40298d5 1655
9b2eab52 1656bool wxDC::CanGetTextExtent() const
519cb848 1657{
9b2eab52
DS
1658 wxCHECK_MSG(Ok(), false, wxT("wxDC::CanGetTextExtent - invalid DC"));
1659
e40298d5 1660 return true ;
519cb848 1661}
e40298d5 1662
9b2eab52 1663void wxDC::DoGetTextExtent( const wxString &strtext, wxCoord *width, wxCoord *height,
e40298d5
JS
1664 wxCoord *descent, wxCoord *externalLeading ,
1665 wxFont *theFont ) const
1666{
9b2eab52
DS
1667 wxCHECK_RET(Ok(), wxT("wxDC::DoGetTextExtent - invalid DC"));
1668
8bebc229 1669 wxMacFastPortSetter helper(this) ;
e40298d5
JS
1670 wxFont formerFont = m_font ;
1671 if ( theFont )
1672 {
1673 // work around the constness
1674 *((wxFont*)(&m_font)) = *theFont ;
1675 }
9b2eab52 1676
e40298d5
JS
1677 MacInstallFont() ;
1678 FontInfo fi ;
1679 ::GetFontInfo( &fi ) ;
9b2eab52 1680
ab9d0a8c 1681#if TARGET_CARBON
e40298d5 1682 bool useGetThemeText = ( GetThemeTextDimensions != (void*) kUnresolvedCFragSymbolAddress ) ;
b7047aa5 1683 if ( UMAGetSystemVersion() < 0x1000 || IsKindOf(CLASSINFO( wxPrinterDC ) ) || ((wxFont*)&m_font)->GetNoAntiAliasing() )
e40298d5 1684 useGetThemeText = false ;
f52640f3 1685#endif
9b2eab52 1686
e40298d5
JS
1687 if ( height )
1688 *height = YDEV2LOGREL( fi.descent + fi.ascent ) ;
1689 if ( descent )
1690 *descent =YDEV2LOGREL( fi.descent );
1691 if ( externalLeading )
1692 *externalLeading = YDEV2LOGREL( fi.leading ) ;
939fba6c 1693
e40298d5
JS
1694 int curwidth = 0 ;
1695 if ( width )
1696 {
1697 *width = 0 ;
b93f3832 1698 wxString linetext = strtext ;
facd6764 1699
f52640f3
SC
1700 if ( useGetThemeText )
1701 {
9b2eab52 1702 Point bounds = {0, 0} ;
aee9fe73 1703 SInt16 baseline ;
a9412f8f 1704 wxMacCFStringHolder mString( linetext , m_font.GetEncoding() ) ;
facd6764 1705 ThemeFontID themeFont = m_font.MacGetThemeFontID() ;
e40298d5 1706 ::GetThemeTextDimensions( mString,
facd6764 1707 themeFont ,
e40298d5
JS
1708 kThemeStateActive,
1709 false,
1710 &bounds,
1711 &baseline );
e40298d5 1712 curwidth = bounds.h ;
f52640f3
SC
1713 }
1714 else
f52640f3 1715 {
ab9d0a8c 1716 wxCharBuffer text = linetext.mb_str(wxConvLocal) ;
427ff662 1717 curwidth = ::TextWidth( text , 0 , strlen(text) ) ;
e40298d5 1718 }
9b2eab52 1719
e40298d5
JS
1720 if ( curwidth > *width )
1721 *width = XDEV2LOGREL( curwidth ) ;
1722 }
9b2eab52 1723
e40298d5
JS
1724 if ( theFont )
1725 {
1726 // work around the constness
1727 *((wxFont*)(&m_font)) = formerFont ;
1728 m_macFontInstalled = false ;
1729 }
519cb848 1730}
e40298d5 1731
0919e93e
RD
1732bool wxDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
1733{
9b2eab52 1734 wxCHECK_MSG(Ok(), false, wxT("wxDC::DoGetPartialTextExtents - invalid DC"));
0919e93e
RD
1735
1736 widths.Empty();
1737 widths.Add(0, text.Length());
1738
1739 if (text.Length() == 0)
1740 return false;
ab9d0a8c 1741
0919e93e
RD
1742 wxMacFastPortSetter helper(this) ;
1743 MacInstallFont() ;
9b2eab52 1744
ab9d0a8c 1745#if TARGET_CARBON
0919e93e
RD
1746 bool useGetThemeText = ( GetThemeTextDimensions != (void*) kUnresolvedCFragSymbolAddress ) ;
1747 if ( UMAGetSystemVersion() < 0x1000 || IsKindOf(CLASSINFO( wxPrinterDC ) ) || ((wxFont*)&m_font)->GetNoAntiAliasing() )
1748 useGetThemeText = false ;
1749
1750 if ( useGetThemeText )
1751 {
1752 // If anybody knows how to do this more efficiently yet still handle
1753 // the fractional glyph widths that may be present when using AA
1754 // fonts, please change it. Currently it is measuring from the
f23d3255 1755 // beginning of the string for each succeeding substring, which is much
0919e93e
RD
1756 // slower than this should be.
1757 for (size_t i=0; i<text.Length(); i++)
1758 {
9b2eab52
DS
1759 wxString str(text.Left(i + 1));
1760 Point bounds = {0, 0};
0919e93e
RD
1761 SInt16 baseline ;
1762 wxMacCFStringHolder mString(str, m_font.GetEncoding());
9b2eab52 1763
0919e93e 1764 ::GetThemeTextDimensions( mString,
facd6764 1765 m_font.MacGetThemeFontID(),
0919e93e
RD
1766 kThemeStateActive,
1767 false,
1768 &bounds,
1769 &baseline );
1770 widths[i] = XDEV2LOGREL(bounds.h);
1771 }
1772 }
ab9d0a8c 1773 else
0919e93e
RD
1774#endif
1775 {
1776 wxCharBuffer buff = text.mb_str(wxConvLocal);
1777 size_t len = strlen(buff);
1778 short* measurements = new short[len+1];
1779 MeasureText(len, buff.data(), measurements);
1780
1781 // Copy to widths, starting at measurements[1]
1782 // NOTE: this doesn't take into account any multi-byte characters
f23d3255 1783 // in buff, it probably should...
0919e93e 1784 for (size_t i=0; i<text.Length(); i++)
9b2eab52 1785 widths[i] = XDEV2LOGREL(measurements[i + 1]);
0919e93e 1786
ab9d0a8c 1787 delete [] measurements;
0919e93e
RD
1788 }
1789
1790 return true;
1791}
1792
9b2eab52 1793wxCoord wxDC::GetCharWidth(void) const
519cb848 1794{
9b2eab52
DS
1795 wxCHECK_MSG(Ok(), 1, wxT("wxDC::GetCharWidth - invalid DC"));
1796
8bebc229 1797 wxMacFastPortSetter helper(this) ;
2beae4b5 1798 int width = 0 ;
9b2eab52
DS
1799 const char text[] = "g" ;
1800
1801 MacInstallFont() ;
1802
2beae4b5 1803#if TARGET_CARBON
e40298d5 1804 bool useGetThemeText = ( GetThemeTextDimensions != (void*) kUnresolvedCFragSymbolAddress ) ;
b7047aa5 1805 if ( UMAGetSystemVersion() < 0x1000 || ((wxFont*)&m_font)->GetNoAntiAliasing() )
e40298d5 1806 useGetThemeText = false ;
9b2eab52 1807
2beae4b5
SC
1808 if ( useGetThemeText )
1809 {
9b2eab52 1810 Point bounds = {0, 0} ;
2beae4b5
SC
1811 SInt16 baseline ;
1812 CFStringRef mString = CFStringCreateWithBytes( NULL , (UInt8*) text , 1 , CFStringGetSystemEncoding(), false ) ;
e40298d5 1813 ::GetThemeTextDimensions( mString,
facd6764 1814 m_font.MacGetThemeFontID(),
e40298d5
JS
1815 kThemeStateActive,
1816 false,
1817 &bounds,
1818 &baseline );
1819 CFRelease( mString ) ;
1820 width = bounds.h ;
2beae4b5
SC
1821 }
1822 else
1823#endif
1824 {
e40298d5
JS
1825 width = ::TextWidth( text , 0 , 1 ) ;
1826 }
9b2eab52 1827
e40298d5 1828 return YDEV2LOGREL(width) ;
519cb848 1829}
e40298d5 1830
9b2eab52 1831wxCoord wxDC::GetCharHeight(void) const
519cb848 1832{
9b2eab52
DS
1833 wxCHECK_MSG(Ok(), 1, wxT("wxDC::GetCharHeight - invalid DC"));
1834
8bebc229 1835 wxMacFastPortSetter helper(this) ;
e40298d5
JS
1836 MacInstallFont() ;
1837 FontInfo fi ;
1838 ::GetFontInfo( &fi ) ;
9b2eab52 1839
e40298d5 1840 return YDEV2LOGREL( fi.descent + fi.ascent );
519cb848 1841}
e40298d5 1842
9b2eab52 1843void wxDC::Clear(void)
519cb848 1844{
9b2eab52
DS
1845 wxCHECK_RET(Ok(), wxT("wxDC::Clear - invalid DC"));
1846
8bebc229 1847 wxMacFastPortSetter helper(this) ;
e40298d5 1848 Rect rect = { -31000 , -31000 , 31000 , 31000 } ;
9b2eab52 1849
f6809556 1850 if ( m_backgroundBrush.Ok() && m_backgroundBrush.GetStyle() != wxTRANSPARENT)
e40298d5
JS
1851 {
1852 ::PenNormal() ;
76a5e5d2 1853 MacSetupBackgroundForCurrentPort( m_backgroundBrush ) ;
e40298d5
JS
1854 ::EraseRect( &rect ) ;
1855 }
519cb848 1856}
e40298d5 1857
519cb848
SC
1858void wxDC::MacInstallFont() const
1859{
3dec57ad 1860 wxCHECK_RET(Ok(), wxT("Invalid DC"));
9b2eab52 1861
e40298d5
JS
1862 // if ( m_macFontInstalled )
1863 // return ;
f23d3255 1864
e40298d5
JS
1865 Pattern blackColor ;
1866 MacSetupBackgroundForCurrentPort(m_backgroundBrush) ;
fc58c6be
SC
1867 if ( m_backgroundMode != wxTRANSPARENT )
1868 {
1869 Pattern whiteColor ;
1870 ::BackPat(GetQDGlobalsWhite(&whiteColor));
1871 }
3dee36ae 1872
ba9a486d 1873 wxASSERT( m_font.Ok() ) ;
3dee36ae 1874
ba9a486d
SC
1875 ::TextFont( m_font.MacGetFontNum() ) ;
1876 ::TextSize( (short)(m_scaleY * m_font.MacGetFontSize()) ) ;
1877 ::TextFace( m_font.MacGetFontStyle() ) ;
1878 m_macFontInstalled = true ;
1879 m_macBrushInstalled = false ;
1880 m_macPenInstalled = false ;
1881 RGBColor forecolor = MAC_WXCOLORREF( m_textForegroundColour.GetPixel());
1882 RGBColor backcolor = MAC_WXCOLORREF( m_textBackgroundColour.GetPixel());
1883 ::RGBForeColor( &forecolor );
1884 ::RGBBackColor( &backcolor );
1885
9b2eab52 1886 // TODO:
e40298d5 1887 short mode = patCopy ;
9b2eab52 1888 switch ( m_logicalFunction )
e40298d5
JS
1889 {
1890 case wxCOPY: // src
1891 mode = patCopy ;
1892 break ;
9b2eab52 1893
e40298d5
JS
1894 case wxINVERT: // NOT dst
1895 ::PenPat(GetQDGlobalsBlack(&blackColor));
1896 mode = patXor ;
1897 break ;
9b2eab52 1898
e40298d5
JS
1899 case wxXOR: // src XOR dst
1900 mode = patXor ;
1901 break ;
9b2eab52 1902
e40298d5
JS
1903 case wxOR_REVERSE: // src OR (NOT dst)
1904 mode = notPatOr ;
1905 break ;
9b2eab52 1906
e40298d5
JS
1907 case wxSRC_INVERT: // (NOT src)
1908 mode = notPatCopy ;
1909 break ;
9b2eab52 1910
4a95c885
SC
1911 case wxAND: // src AND dst
1912 mode = adMin ;
1913 break ;
9b2eab52
DS
1914
1915 // TODO: unsupported
e40298d5
JS
1916 case wxCLEAR: // 0
1917 case wxAND_REVERSE:// src AND (NOT dst)
e40298d5
JS
1918 case wxAND_INVERT: // (NOT src) AND dst
1919 case wxNO_OP: // dst
1920 case wxNOR: // (NOT src) AND (NOT dst)
1921 case wxEQUIV: // (NOT src) XOR dst
1922 case wxOR_INVERT: // (NOT src) OR dst
1923 case wxNAND: // (NOT src) OR (NOT dst)
1924 case wxOR: // src OR dst
1925 case wxSET: // 1
1926 // case wxSRC_OR: // source _bitmap_ OR destination
1927 // case wxSRC_AND: // source _bitmap_ AND destination
1928 break ;
9b2eab52
DS
1929
1930 default:
1931 break ;
e40298d5 1932 }
9b2eab52 1933
e40298d5 1934 ::PenMode( mode ) ;
66a09d47 1935 OSStatus status = noErr ;
ba9a486d
SC
1936 status = ATSUCreateAndCopyStyle( (ATSUStyle) m_font.MacGetATSUStyle() , (ATSUStyle*) &m_macATSUIStyle ) ;
1937 wxASSERT_MSG( status == noErr , wxT("couldn't set create ATSU style") ) ;
1938
facd6764 1939 Fixed atsuSize = IntToFixed( int(m_scaleY * m_font.MacGetFontSize()) ) ;
e40298d5
JS
1940 ATSUAttributeTag atsuTags[] =
1941 {
e40298d5 1942 kATSUSizeTag ,
e40298d5 1943 } ;
66a09d47
SC
1944 ByteCount atsuSizes[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
1945 {
e40298d5 1946 sizeof( Fixed ) ,
66a09d47 1947 } ;
e40298d5
JS
1948 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
1949 {
e40298d5 1950 &atsuSize ,
e40298d5 1951 } ;
f23d3255 1952
26b9b4e1 1953 status = ::ATSUSetAttributes((ATSUStyle)m_macATSUIStyle, sizeof(atsuTags)/sizeof(ATSUAttributeTag) ,
e40298d5 1954 atsuTags, atsuSizes, atsuValues);
ba9a486d 1955
f23d3255 1956 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") ) ;
519cb848 1957}
e40298d5 1958
411bdf74 1959Pattern gPatterns[] =
9b2eab52
DS
1960{
1961 // hatch patterns
a47ae718
VZ
1962 { { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF } } ,
1963 { { 0x01 , 0x02 , 0x04 , 0x08 , 0x10 , 0x20 , 0x40 , 0x80 } } ,
1964 { { 0x80 , 0x40 , 0x20 , 0x10 , 0x08 , 0x04 , 0x02 , 0x01 } } ,
1965 { { 0x10 , 0x10 , 0x10 , 0xFF , 0x10 , 0x10 , 0x10 , 0x10 } } ,
1966 { { 0x00 , 0x00 , 0x00 , 0xFF , 0x00 , 0x00 , 0x00 , 0x00 } } ,
1967 { { 0x10 , 0x10 , 0x10 , 0x10 , 0x10 , 0x10 , 0x10 , 0x10 } } ,
1968 { { 0x81 , 0x42 , 0x24 , 0x18 , 0x18 , 0x24 , 0x42 , 0x81 } } ,
9b2eab52 1969
411bdf74 1970 // dash patterns
a47ae718
VZ
1971 { { 0xCC , 0x99 , 0x33 , 0x66 , 0xCC , 0x99 , 0x33 , 0x66 } } , // DOT
1972 { { 0xFE , 0xFD , 0xFB , 0xF7 , 0xEF , 0xDF , 0xBF , 0x7F } } , // LONG_DASH
1973 { { 0xEE , 0xDD , 0xBB , 0x77 , 0xEE , 0xDD , 0xBB , 0x77 } } , // SHORT_DASH
1974 { { 0xDE , 0xBD , 0x7B , 0xF6 , 0xED , 0xDB , 0xB7 , 0x6F } } , // DOT_DASH
ffd67837 1975} ;
e40298d5 1976
411bdf74
RR
1977static void wxMacGetPattern(int penStyle, Pattern *pattern)
1978{
1979 int index = 0; // solid pattern by default
9b2eab52 1980 switch (penStyle)
ab9d0a8c 1981 {
411bdf74
RR
1982 // hatches
1983 case wxBDIAGONAL_HATCH: index = 1; break;
1984 case wxFDIAGONAL_HATCH: index = 2; break;
1985 case wxCROSS_HATCH: index = 3; break;
1986 case wxHORIZONTAL_HATCH: index = 4; break;
1987 case wxVERTICAL_HATCH: index = 5; break;
1988 case wxCROSSDIAG_HATCH: index = 6; break;
9b2eab52 1989
411bdf74
RR
1990 // dashes
1991 case wxDOT: index = 7; break;
1992 case wxLONG_DASH: index = 8; break;
1993 case wxSHORT_DASH: index = 9; break;
1994 case wxDOT_DASH: index = 10; break;
9b2eab52
DS
1995
1996 default:
1997 break;
411bdf74 1998 }
9b2eab52 1999
ab9d0a8c 2000 *pattern = gPatterns[index];
519cb848 2001}
e40298d5 2002
519cb848
SC
2003void wxDC::MacInstallPen() const
2004{
9b2eab52
DS
2005 wxCHECK_RET(Ok(), wxT("wxDC::MacInstallPen - invalid DC"));
2006
a47ae718 2007 //Pattern blackColor;
e40298d5
JS
2008 // if ( m_macPenInstalled )
2009 // return ;
f23d3255 2010
e40298d5
JS
2011 RGBColor forecolor = MAC_WXCOLORREF( m_pen.GetColour().GetPixel());
2012 RGBColor backcolor = MAC_WXCOLORREF( m_backgroundBrush.GetColour().GetPixel());
2013 ::RGBForeColor( &forecolor );
2014 ::RGBBackColor( &backcolor );
2015 ::PenNormal() ;
f23d3255 2016
e40298d5 2017 // null means only one pixel, at whatever resolution
f23d3255 2018 int penWidth = (int) (m_pen.GetWidth() * m_scaleX) ;
e40298d5
JS
2019 if ( penWidth == 0 )
2020 penWidth = 1 ;
2021 ::PenSize(penWidth, penWidth);
ab9d0a8c 2022
411bdf74 2023 Pattern pat;
f23d3255 2024 int penStyle = m_pen.GetStyle();
411bdf74 2025 if (penStyle == wxUSER_DASH)
ab9d0a8c 2026 {
411bdf74
RR
2027 // FIXME: there should be exactly 8 items in the dash
2028 wxDash* dash ;
2029 int number = m_pen.GetDashes(&dash) ;
2030 int index = 0;
2031 for ( int i = 0 ; i < 8 ; ++i )
2032 {
2033 pat.pat[i] = dash[index] ;
2034 if (index < number - 1)
2035 index++;
2036 }
e40298d5
JS
2037 }
2038 else
2039 {
411bdf74 2040 wxMacGetPattern(penStyle, &pat);
e40298d5 2041 }
411bdf74
RR
2042 ::PenPat(&pat);
2043
9b2eab52 2044 // TODO:
e40298d5 2045 short mode = patCopy ;
9b2eab52 2046 switch ( m_logicalFunction )
e40298d5
JS
2047 {
2048 case wxCOPY: // only foreground color, leave background (thus not patCopy)
2049 mode = patOr ;
2050 break ;
9b2eab52 2051
e40298d5
JS
2052 case wxINVERT: // NOT dst
2053 // ::PenPat(GetQDGlobalsBlack(&blackColor));
2054 mode = patXor ;
2055 break ;
9b2eab52 2056
e40298d5
JS
2057 case wxXOR: // src XOR dst
2058 mode = patXor ;
2059 break ;
9b2eab52 2060
e40298d5
JS
2061 case wxOR_REVERSE: // src OR (NOT dst)
2062 mode = notPatOr ;
2063 break ;
9b2eab52 2064
e40298d5
JS
2065 case wxSRC_INVERT: // (NOT src)
2066 mode = notPatCopy ;
2067 break ;
9b2eab52 2068
4a95c885
SC
2069 case wxAND: // src AND dst
2070 mode = adMin ;
2071 break ;
9b2eab52
DS
2072
2073 // TODO: unsupported
e40298d5
JS
2074 case wxCLEAR: // 0
2075 case wxAND_REVERSE:// src AND (NOT dst)
e40298d5
JS
2076 case wxAND_INVERT: // (NOT src) AND dst
2077 case wxNO_OP: // dst
2078 case wxNOR: // (NOT src) AND (NOT dst)
2079 case wxEQUIV: // (NOT src) XOR dst
2080 case wxOR_INVERT: // (NOT src) OR dst
2081 case wxNAND: // (NOT src) OR (NOT dst)
2082 case wxOR: // src OR dst
2083 case wxSET: // 1
2084 // case wxSRC_OR: // source _bitmap_ OR destination
2085 // case wxSRC_AND: // source _bitmap_ AND destination
2086 break ;
9b2eab52 2087
35853cf2 2088 default:
9b2eab52 2089 break ;
e40298d5 2090 }
9b2eab52 2091
e40298d5
JS
2092 ::PenMode( mode ) ;
2093 m_macPenInstalled = true ;
2094 m_macBrushInstalled = false ;
2095 m_macFontInstalled = false ;
519cb848 2096}
e40298d5 2097
99030bdf 2098void wxDC::MacSetupBackgroundForCurrentPort(const wxBrush& background )
1dcbbdcf
SC
2099{
2100 Pattern whiteColor ;
f23d3255 2101
f6809556 2102 if ( background.Ok() )
7d9d1fd7 2103 {
9b2eab52 2104 switch ( background.MacGetBrushKind() )
7d9d1fd7 2105 {
f6809556 2106 case kwxMacBrushTheme :
f6809556
SC
2107 ::SetThemeBackground( background.MacGetTheme() , wxDisplayDepth() , true ) ;
2108 break ;
9b2eab52 2109
f6809556 2110 case kwxMacBrushThemeBackground :
e40298d5 2111 {
f6809556
SC
2112 Rect extent ;
2113 ThemeBackgroundKind bg = background.MacGetThemeBackground( &extent ) ;
f23d3255 2114 ::ApplyThemeBackground( bg , &extent, kThemeStateActive , wxDisplayDepth() , true ) ;
f6809556 2115 }
9b2eab52
DS
2116 break ;
2117
f6809556
SC
2118 case kwxMacBrushColour :
2119 {
2120 ::RGBBackColor( &MAC_WXCOLORREF( background.GetColour().GetPixel()) );
2121 int brushStyle = background.GetStyle();
2122 if (brushStyle == wxSOLID)
2123 ::BackPat(GetQDGlobalsWhite(&whiteColor));
ab9d0a8c 2124 else if (background.IsHatch())
f6809556
SC
2125 {
2126 Pattern pat ;
2127 wxMacGetPattern(brushStyle, &pat);
2128 ::BackPat(&pat);
2129 }
2130 else
2131 {
2132 ::BackPat(GetQDGlobalsWhite(&whiteColor));
2133 }
e40298d5 2134 }
9b2eab52
DS
2135 break ;
2136
2137 default:
2138 break ;
7d9d1fd7
SC
2139 }
2140 }
1dcbbdcf 2141}
e40298d5 2142
519cb848
SC
2143void wxDC::MacInstallBrush() const
2144{
3dec57ad 2145 wxCHECK_RET(Ok(), wxT("Invalid DC"));
f23d3255 2146
e40298d5
JS
2147 // if ( m_macBrushInstalled )
2148 // return ;
f23d3255
DS
2149
2150 Pattern blackColor ;
e40298d5
JS
2151 bool backgroundTransparent = (GetBackgroundMode() == wxTRANSPARENT) ;
2152 ::RGBForeColor( &MAC_WXCOLORREF( m_brush.GetColour().GetPixel()) );
2153 ::RGBBackColor( &MAC_WXCOLORREF( m_backgroundBrush.GetColour().GetPixel()) );
9b2eab52 2154
e40298d5
JS
2155 int brushStyle = m_brush.GetStyle();
2156 if (brushStyle == wxSOLID)
2157 {
2158 ::PenPat(GetQDGlobalsBlack(&blackColor));
2159 }
ab9d0a8c 2160 else if (m_brush.IsHatch())
e40298d5
JS
2161 {
2162 Pattern pat ;
9b2eab52 2163
411bdf74 2164 wxMacGetPattern(brushStyle, &pat);
e40298d5
JS
2165 ::PenPat(&pat);
2166 }
2167 else if ( m_brush.GetStyle() == wxSTIPPLE || m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE )
2168 {
2169 // we force this in order to be compliant with wxMSW
2170 backgroundTransparent = false ;
9b2eab52 2171
e40298d5
JS
2172 // for these the text fore (and back for MASK_OPAQUE) colors are used
2173 wxBitmap* bitmap = m_brush.GetStipple() ;
2174 int width = bitmap->GetWidth() ;
2175 int height = bitmap->GetHeight() ;
76a5e5d2 2176 GWorldPtr gw = NULL ;
9b2eab52 2177
e40298d5 2178 if ( m_brush.GetStyle() == wxSTIPPLE )
20b69855 2179 gw = MAC_WXHBITMAP(bitmap->GetHBITMAP(NULL)) ;
e40298d5 2180 else
20b69855 2181 gw = MAC_WXHBITMAP(bitmap->GetMask()->GetHBITMAP()) ;
9b2eab52 2182
76a5e5d2
SC
2183 PixMapHandle gwpixmaphandle = GetGWorldPixMap( gw ) ;
2184 LockPixels( gwpixmaphandle ) ;
e40298d5 2185 bool isMonochrome = !IsPortColor( gw ) ;
76a5e5d2
SC
2186 if ( !isMonochrome )
2187 {
2188 if ( (**gwpixmaphandle).pixelSize == 1 )
2189 isMonochrome = true ;
2190 }
9b2eab52 2191
66a09d47 2192 if ( isMonochrome && width == 8 && height == 8 )
e40298d5
JS
2193 {
2194 ::RGBForeColor( &MAC_WXCOLORREF( m_textForegroundColour.GetPixel()) );
2195 ::RGBForeColor( &MAC_WXCOLORREF( m_textBackgroundColour.GetPixel()) );
76a5e5d2
SC
2196 BitMap* gwbitmap = (BitMap*) *gwpixmaphandle ; // since the color depth is 1 it is a BitMap
2197 UInt8 *gwbits = (UInt8*) gwbitmap->baseAddr ;
2198 int alignment = gwbitmap->rowBytes & 0x7FFF ;
66a09d47 2199 Pattern pat ;
9b2eab52 2200
66a09d47
SC
2201 for ( int i = 0 ; i < 8 ; ++i )
2202 {
f23d3255 2203 pat.pat[i] = gwbits[i * alignment + 0] ;
66a09d47 2204 }
9b2eab52 2205
66a09d47
SC
2206 UnlockPixels( GetGWorldPixMap( gw ) ) ;
2207 ::PenPat( &pat ) ;
e40298d5
JS
2208 }
2209 else
2210 {
2211 // this will be the code to handle power of 2 patterns, we will have to arrive at a nice
2212 // caching scheme before putting this into production
2213 Handle image;
2214 long imageSize;
9b2eab52 2215
e40298d5
JS
2216 PixPatHandle pixpat = NewPixPat() ;
2217 CopyPixMap(gwpixmaphandle, (**pixpat).patMap);
66a09d47 2218 imageSize = GetPixRowBytes((**pixpat).patMap) *
e40298d5
JS
2219 ((**(**pixpat).patMap).bounds.bottom -
2220 (**(**pixpat).patMap).bounds.top);
66a09d47
SC
2221 PtrToHand( (**gwpixmaphandle).baseAddr, &image, imageSize );
2222 (**pixpat).patData = image;
9b2eab52 2223
66a09d47
SC
2224 if ( isMonochrome )
2225 {
e40298d5
JS
2226 CTabHandle ctable = ((**((**pixpat).patMap)).pmTable) ;
2227 ColorSpecPtr ctspec = (ColorSpecPtr) &(**ctable).ctTable ;
2228 if ( ctspec[0].rgb.red == 0x0000 )
2229 {
2230 ctspec[1].rgb = MAC_WXCOLORREF( m_textBackgroundColour.GetPixel()) ;
2231 ctspec[0].rgb = MAC_WXCOLORREF( m_textForegroundColour.GetPixel()) ;
2232 }
2233 else
2234 {
2235 ctspec[0].rgb = MAC_WXCOLORREF( m_textBackgroundColour.GetPixel()) ;
2236 ctspec[1].rgb = MAC_WXCOLORREF( m_textForegroundColour.GetPixel()) ;
2237 }
2238 ::CTabChanged( ctable ) ;
66a09d47 2239 }
9b2eab52 2240
66a09d47
SC
2241 ::PenPixPat(pixpat);
2242 m_macForegroundPixMap = pixpat ;
e40298d5 2243 }
9b2eab52 2244
e40298d5
JS
2245 UnlockPixels( gwpixmaphandle ) ;
2246 }
1ff301c4
SC
2247 else
2248 {
e40298d5
JS
2249 ::PenPat(GetQDGlobalsBlack(&blackColor));
2250 }
9b2eab52 2251
e40298d5 2252 short mode = patCopy ;
9b2eab52 2253 switch ( m_logicalFunction )
e40298d5
JS
2254 {
2255 case wxCOPY: // src
2256 if ( backgroundTransparent )
2257 mode = patOr ;
2258 else
2259 mode = patCopy ;
2260 break ;
9b2eab52 2261
e40298d5
JS
2262 case wxINVERT: // NOT dst
2263 if ( !backgroundTransparent )
e40298d5 2264 ::PenPat(GetQDGlobalsBlack(&blackColor));
e40298d5
JS
2265 mode = patXor ;
2266 break ;
9b2eab52 2267
e40298d5
JS
2268 case wxXOR: // src XOR dst
2269 mode = patXor ;
2270 break ;
9b2eab52 2271
e40298d5
JS
2272 case wxOR_REVERSE: // src OR (NOT dst)
2273 mode = notPatOr ;
2274 break ;
9b2eab52 2275
e40298d5
JS
2276 case wxSRC_INVERT: // (NOT src)
2277 mode = notPatCopy ;
2278 break ;
9b2eab52 2279
4a95c885
SC
2280 case wxAND: // src AND dst
2281 mode = adMin ;
2282 break ;
9b2eab52
DS
2283
2284 // TODO: unsupported
e40298d5
JS
2285 case wxCLEAR: // 0
2286 case wxAND_REVERSE:// src AND (NOT dst)
e40298d5
JS
2287 case wxAND_INVERT: // (NOT src) AND dst
2288 case wxNO_OP: // dst
2289 case wxNOR: // (NOT src) AND (NOT dst)
2290 case wxEQUIV: // (NOT src) XOR dst
2291 case wxOR_INVERT: // (NOT src) OR dst
2292 case wxNAND: // (NOT src) OR (NOT dst)
2293 case wxOR: // src OR dst
2294 case wxSET: // 1
2295 // case wxSRC_OR: // source _bitmap_ OR destination
2296 // case wxSRC_AND: // source _bitmap_ AND destination
2297 break ;
9b2eab52
DS
2298
2299 default:
2300 break ;
e40298d5 2301 }
9b2eab52 2302
e40298d5
JS
2303 ::PenMode( mode ) ;
2304 m_macBrushInstalled = true ;
2305 m_macPenInstalled = false ;
2306 m_macFontInstalled = false ;
519cb848 2307}
e40298d5 2308
2f1ae414
SC
2309// ---------------------------------------------------------------------------
2310// coordinates transformations
2311// ---------------------------------------------------------------------------
519cb848 2312
2f1ae414
SC
2313wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
2314{
2315 return ((wxDC *)this)->XDEV2LOG(x);
2316}
e40298d5 2317
2f1ae414
SC
2318wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
2319{
2320 return ((wxDC *)this)->YDEV2LOG(y);
2321}
e40298d5 2322
2f1ae414
SC
2323wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
2324{
2325 return ((wxDC *)this)->XDEV2LOGREL(x);
2326}
e40298d5 2327
2f1ae414
SC
2328wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
2329{
2330 return ((wxDC *)this)->YDEV2LOGREL(y);
2331}
e40298d5 2332
2f1ae414
SC
2333wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
2334{
2335 return ((wxDC *)this)->XLOG2DEV(x);
2336}
e40298d5 2337
2f1ae414
SC
2338wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
2339{
2340 return ((wxDC *)this)->YLOG2DEV(y);
2341}
e40298d5 2342
2f1ae414
SC
2343wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
2344{
2345 return ((wxDC *)this)->XLOG2DEVREL(x);
2346}
e40298d5 2347
2f1ae414
SC
2348wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
2349{
2350 return ((wxDC *)this)->YLOG2DEVREL(y);
a7390348 2351}
20b69855
SC
2352
2353#endif // !wxMAC_USE_CORE_GRAPHICS