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