1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dc.h"
18 #include "wx/mac/uma.h"
19 #include "wx/dcmemory.h"
20 #include "wx/region.h"
29 #include "wx/mac/private.h"
30 #include "ATSUnicode.h"
31 #include "TextCommon.h"
32 #include "TextEncodingConverter.h"
34 #if !USE_SHARED_LIBRARY
35 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxObject
)
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 #define mm2inches 0.0393700787402
43 #define inches2mm 25.4
44 #define mm2twips 56.6929133859
45 #define twips2mm 0.0176388888889
46 #define mm2pt 2.83464566929
47 #define pt2mm 0.352777777778
49 const double M_PI
= 3.14159265358979 ;
51 const double RAD2DEG
= 180.0 / M_PI
;
52 const short kEmulatedMode
= -1 ;
53 const short kUnsupportedMode
= -2 ;
55 #define wxMAC_EXPERIMENTAL_PATTERN 0
57 wxMacPortSetter::wxMacPortSetter( const wxDC
* dc
) :
58 m_ph( (GrafPtr
) dc
->m_macPort
)
60 wxASSERT( dc
->Ok() ) ;
62 dc
->MacSetupPort(&m_ph
) ;
65 wxMacPortSetter::~wxMacPortSetter()
67 m_dc
->MacCleanupPort(&m_ph
) ;
70 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
74 static inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
75 static inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
76 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
82 // this function emulates all wx colour manipulations, used to verify the implementation
83 // by setting the mode in the blitting functions to kEmulatedMode
85 void wxMacCalculateColour( int logical_func
, const RGBColor
&srcColor
, RGBColor
&dstColor
) ;
86 void wxMacCalculateColour( int logical_func
, const RGBColor
&srcColor
, RGBColor
&dstColor
)
88 switch ( logical_func
)
90 case wxAND
: // src AND dst
91 dstColor
.red
= dstColor
.red
& srcColor
.red
;
92 dstColor
.green
= dstColor
.green
& srcColor
.green
;
93 dstColor
.blue
= dstColor
.blue
& srcColor
.blue
;
95 case wxAND_INVERT
: // (NOT src) AND dst
96 dstColor
.red
= dstColor
.red
& ~srcColor
.red
;
97 dstColor
.green
= dstColor
.green
& ~srcColor
.green
;
98 dstColor
.blue
= dstColor
.blue
& ~srcColor
.blue
;
100 case wxAND_REVERSE
:// src AND (NOT dst)
101 dstColor
.red
= ~dstColor
.red
& srcColor
.red
;
102 dstColor
.green
= ~dstColor
.green
& srcColor
.green
;
103 dstColor
.blue
= ~dstColor
.blue
& srcColor
.blue
;
111 dstColor
.red
= srcColor
.red
;
112 dstColor
.green
= srcColor
.green
;
113 dstColor
.blue
= srcColor
.blue
;
115 case wxEQUIV
: // (NOT src) XOR dst
116 dstColor
.red
= dstColor
.red
^ ~srcColor
.red
;
117 dstColor
.green
= dstColor
.green
^ ~srcColor
.green
;
118 dstColor
.blue
= dstColor
.blue
^ ~srcColor
.blue
;
120 case wxINVERT
: // NOT dst
121 dstColor
.red
= ~dstColor
.red
;
122 dstColor
.green
= ~dstColor
.green
;
123 dstColor
.blue
= ~dstColor
.blue
;
125 case wxNAND
: // (NOT src) OR (NOT dst)
126 dstColor
.red
= ~dstColor
.red
| ~srcColor
.red
;
127 dstColor
.green
= ~dstColor
.green
| ~srcColor
.green
;
128 dstColor
.blue
= ~dstColor
.blue
| ~srcColor
.blue
;
130 case wxNOR
: // (NOT src) AND (NOT dst)
131 dstColor
.red
= ~dstColor
.red
& ~srcColor
.red
;
132 dstColor
.green
= ~dstColor
.green
& ~srcColor
.green
;
133 dstColor
.blue
= ~dstColor
.blue
& ~srcColor
.blue
;
137 case wxOR
: // src OR dst
138 dstColor
.red
= dstColor
.red
| srcColor
.red
;
139 dstColor
.green
= dstColor
.green
| srcColor
.green
;
140 dstColor
.blue
= dstColor
.blue
| srcColor
.blue
;
142 case wxOR_INVERT
: // (NOT src) OR dst
143 dstColor
.red
= dstColor
.red
| ~srcColor
.red
;
144 dstColor
.green
= dstColor
.green
| ~srcColor
.green
;
145 dstColor
.blue
= dstColor
.blue
| ~srcColor
.blue
;
147 case wxOR_REVERSE
: // src OR (NOT dst)
148 dstColor
.red
= ~dstColor
.red
| srcColor
.red
;
149 dstColor
.green
= ~dstColor
.green
| srcColor
.green
;
150 dstColor
.blue
= ~dstColor
.blue
| srcColor
.blue
;
153 dstColor
.red
= 0xFFFF ;
154 dstColor
.green
= 0xFFFF ;
155 dstColor
.blue
= 0xFFFF ;
157 case wxSRC_INVERT
: // (NOT src)
158 dstColor
.red
= ~srcColor
.red
;
159 dstColor
.green
= ~srcColor
.green
;
160 dstColor
.blue
= ~srcColor
.blue
;
162 case wxXOR
: // src XOR dst
163 dstColor
.red
= dstColor
.red
^ srcColor
.red
;
164 dstColor
.green
= dstColor
.green
^ srcColor
.green
;
165 dstColor
.blue
= dstColor
.blue
^ srcColor
.blue
;
176 m_mm_to_pix_x
= mm2pt
;
177 m_mm_to_pix_y
= mm2pt
;
179 m_internalDeviceOriginX
= 0;
180 m_internalDeviceOriginY
= 0;
181 m_externalDeviceOriginX
= 0;
182 m_externalDeviceOriginY
= 0;
184 m_logicalScaleX
= 1.0;
185 m_logicalScaleY
= 1.0;
191 m_needComputeScaleX
= FALSE
;
192 m_needComputeScaleY
= FALSE
;
198 m_macFontInstalled
= false ;
199 m_macBrushInstalled
= false ;
200 m_macPenInstalled
= false ;
202 m_macLocalOrigin
.x
= m_macLocalOrigin
.y
= 0 ;
203 m_macBoundaryClipRgn
= NewRgn() ;
204 m_macCurrentClipRgn
= NewRgn() ;
206 SetRectRgn( (RgnHandle
) m_macBoundaryClipRgn
, -32000 , -32000 , 32000 , 32000 ) ;
207 SetRectRgn( (RgnHandle
) m_macCurrentClipRgn
, -32000 , -32000 , 32000 , 32000 ) ;
209 m_pen
= *wxBLACK_PEN
;
210 m_font
= *wxNORMAL_FONT
;
211 m_brush
= *wxWHITE_BRUSH
;
212 m_macCurrentPortStateHelper
= NULL
;
213 m_macATSUIStyle
= NULL
;
214 m_macAliasWasEnabled
= false;
215 m_macForegroundPixMap
= NULL
;
216 m_macBackgroundPixMap
= NULL
;
221 DisposeRgn( (RgnHandle
) m_macBoundaryClipRgn
) ;
222 DisposeRgn( (RgnHandle
) m_macCurrentClipRgn
) ;
224 void wxDC::MacSetupPort(wxMacPortStateHelper
* help
) const
226 wxASSERT( m_macCurrentPortStateHelper
== NULL
) ;
227 m_macCurrentPortStateHelper
= help
;
228 SetClip( (RgnHandle
) m_macCurrentClipRgn
);
230 m_macFontInstalled
= false ;
231 m_macBrushInstalled
= false ;
232 m_macPenInstalled
= false ;
235 void wxDC::MacCleanupPort(wxMacPortStateHelper
* help
) const
237 wxASSERT( m_macCurrentPortStateHelper
== help
) ;
238 m_macCurrentPortStateHelper
= NULL
;
239 if( m_macATSUIStyle
)
241 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUIStyle
);
242 m_macATSUIStyle
= NULL
;
244 if ( m_macAliasWasEnabled
)
246 SetAntiAliasedTextEnabled(m_macFormerAliasState
, m_macFormerAliasSize
);
247 m_macAliasWasEnabled
= false ;
249 if ( m_macForegroundPixMap
)
252 ::PenPat(GetQDGlobalsBlack(&blackColor
));
253 DisposePixPat( (PixPatHandle
) m_macForegroundPixMap
) ;
254 m_macForegroundPixMap
= NULL
;
256 if ( m_macBackgroundPixMap
)
259 ::BackPat(GetQDGlobalsWhite(&whiteColor
));
260 DisposePixPat( (PixPatHandle
) m_macBackgroundPixMap
) ;
261 m_macBackgroundPixMap
= NULL
;
265 void wxDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
267 wxCHECK_RET( Ok(), wxT("invalid window dc") );
269 wxCHECK_RET( bmp
.Ok(), wxT("invalid bitmap") );
271 wxMacPortSetter
helper(this) ;
273 wxCoord xx
= XLOG2DEVMAC(x
);
274 wxCoord yy
= YLOG2DEVMAC(y
);
275 wxCoord w
= bmp
.GetWidth();
276 wxCoord h
= bmp
.GetHeight();
277 wxCoord ww
= XLOG2DEVREL(w
);
278 wxCoord hh
= YLOG2DEVREL(h
);
280 // Set up drawing mode
281 short mode
= (m_logicalFunction
== wxCOPY
? srcCopy
:
282 //m_logicalFunction == wxCLEAR ? WHITENESS :
283 //m_logicalFunction == wxSET ? BLACKNESS :
284 m_logicalFunction
== wxINVERT
? hilite
:
285 //m_logicalFunction == wxAND ? MERGECOPY :
286 m_logicalFunction
== wxOR
? srcOr
:
287 m_logicalFunction
== wxSRC_INVERT
? notSrcCopy
:
288 m_logicalFunction
== wxXOR
? srcXor
:
289 m_logicalFunction
== wxOR_REVERSE
? notSrcOr
:
290 //m_logicalFunction == wxAND_REVERSE ? SRCERASE :
291 //m_logicalFunction == wxSRC_OR ? srcOr :
292 //m_logicalFunction == wxSRC_AND ? SRCAND :
295 if ( bmp
.GetBitmapType() == kMacBitmapTypePict
) {
296 Rect bitmaprect
= { 0 , 0 , hh
, ww
};
297 ::OffsetRect( &bitmaprect
, xx
, yy
) ;
298 ::DrawPicture( (PicHandle
) bmp
.GetPict(), &bitmaprect
) ;
300 else if ( bmp
.GetBitmapType() == kMacBitmapTypeGrafWorld
)
302 GWorldPtr bmapworld
= MAC_WXHBITMAP( bmp
.GetHBITMAP() );
303 PixMapHandle bmappixels
;
305 // Set foreground and background colours (for bitmaps depth = 1)
306 if(bmp
.GetDepth() == 1)
308 RGBColor fore
= MAC_WXCOLORREF(m_textForegroundColour
.GetPixel());
309 RGBColor back
= MAC_WXCOLORREF(m_textBackgroundColour
.GetPixel());
315 RGBColor white
= { 0xFFFF, 0xFFFF,0xFFFF} ;
316 RGBColor black
= { 0,0,0} ;
317 RGBForeColor( &black
) ;
318 RGBBackColor( &white
) ;
321 bmappixels
= GetGWorldPixMap( bmapworld
) ;
323 wxCHECK_RET(LockPixels(bmappixels
),
324 wxT("DoDrawBitmap: Unable to lock pixels"));
326 Rect source
= { 0, 0, h
, w
};
327 Rect dest
= { yy
, xx
, yy
+ hh
, xx
+ ww
};
329 if ( useMask
&& bmp
.GetMask() )
331 if( LockPixels(GetGWorldPixMap(MAC_WXHBITMAP(bmp
.GetMask()->GetMaskBitmap()))))
335 GetPortBitMapForCopyBits(bmapworld
),
336 GetPortBitMapForCopyBits(MAC_WXHBITMAP(bmp
.GetMask()->GetMaskBitmap())),
337 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort
) ),
338 &source
, &source
, &dest
, mode
, NULL
340 UnlockPixels(GetGWorldPixMap(MAC_WXHBITMAP(bmp
.GetMask()->GetMaskBitmap())));
344 CopyBits( GetPortBitMapForCopyBits( bmapworld
),
345 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort
) ),
346 &source
, &dest
, mode
, NULL
) ;
348 UnlockPixels( bmappixels
) ;
350 else if ( bmp
.GetBitmapType() == kMacBitmapTypeIcon
)
352 Rect bitmaprect
= { 0 , 0 , bmp
.GetHeight(), bmp
.GetWidth() } ;
353 OffsetRect( &bitmaprect
, xx
, yy
) ;
354 PlotCIconHandle( &bitmaprect
, atNone
, ttNone
, MAC_WXHICON(bmp
.GetHICON()) ) ;
356 m_macPenInstalled
= false ;
357 m_macBrushInstalled
= false ;
358 m_macFontInstalled
= false ;
362 void wxDC::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
364 wxCHECK_RET(Ok(), wxT("Invalid dc wxDC::DoDrawIcon"));
366 wxCHECK_RET(icon
.Ok(), wxT("Invalid icon wxDC::DoDrawIcon"));
368 DoDrawBitmap( icon
, x
, y
, icon
.GetMask() != NULL
) ;
370 void wxDC::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
372 wxCHECK_RET(Ok(), wxT("wxDC::DoSetClippingRegion Invalid DC"));
373 wxCoord xx
, yy
, ww
, hh
;
377 ww
= XLOG2DEVREL(width
);
378 hh
= YLOG2DEVREL(height
);
380 SetRectRgn( (RgnHandle
) m_macCurrentClipRgn
, xx
, yy
, xx
+ ww
, yy
+ hh
) ;
381 SectRgn( (RgnHandle
) m_macCurrentClipRgn
, (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) m_macCurrentClipRgn
) ;
385 m_clipX1
= wxMax( m_clipX1
, xx
);
386 m_clipY1
= wxMax( m_clipY1
, yy
);
387 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
));
388 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
));
400 void wxDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
402 wxCHECK_RET( Ok(), wxT("invalid window dc") ) ;
404 wxMacPortSetter
helper(this) ;
407 DestroyClippingRegion();
412 region
.GetBox( x
, y
, w
, h
);
413 wxCoord xx
, yy
, ww
, hh
;
420 // if we have a scaling that we cannot map onto native regions
421 // we must use the box
423 if ( ww
!= w
|| hh
!= h
)
425 wxDC::DoSetClippingRegion( x
, y
, w
, h
);
429 CopyRgn( (RgnHandle
) region
.GetWXHRGN() , (RgnHandle
) m_macCurrentClipRgn
) ;
430 if ( xx
!= x
|| yy
!= y
)
432 OffsetRgn( (RgnHandle
) m_macCurrentClipRgn
, xx
- x
, yy
- y
) ;
434 SectRgn( (RgnHandle
) m_macCurrentClipRgn
, (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) m_macCurrentClipRgn
) ;
437 m_clipX1
= wxMax( m_clipX1
, xx
);
438 m_clipY1
= wxMax( m_clipY1
, yy
);
439 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
));
440 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
));
454 void wxDC::DestroyClippingRegion()
456 wxMacPortSetter
helper(this) ;
457 CopyRgn( (RgnHandle
) m_macBoundaryClipRgn
, (RgnHandle
) m_macCurrentClipRgn
) ;
460 void wxDC::DoGetSize( int* width
, int* height
) const
462 *width
= m_maxX
-m_minX
;
463 *height
= m_maxY
-m_minY
;
465 void wxDC::DoGetSizeMM( int* width
, int* height
) const
470 *width
= long( double(w
) / (m_scaleX
*m_mm_to_pix_x
) );
471 *height
= long( double(h
) / (m_scaleY
*m_mm_to_pix_y
) );
473 void wxDC::SetTextForeground( const wxColour
&col
)
475 wxCHECK_RET(Ok(), wxT("Invalid DC"));
476 m_textForegroundColour
= col
;
477 m_macFontInstalled
= false ;
479 void wxDC::SetTextBackground( const wxColour
&col
)
481 wxCHECK_RET(Ok(), wxT("Invalid DC"));
482 m_textBackgroundColour
= col
;
483 m_macFontInstalled
= false ;
485 void wxDC::SetMapMode( int mode
)
490 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
493 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
496 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
499 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
503 SetLogicalScale( 1.0, 1.0 );
506 if (mode
!= wxMM_TEXT
)
508 m_needComputeScaleX
= TRUE
;
509 m_needComputeScaleY
= TRUE
;
512 void wxDC::SetUserScale( double x
, double y
)
514 // allow negative ? -> no
517 ComputeScaleAndOrigin();
519 void wxDC::SetLogicalScale( double x
, double y
)
524 ComputeScaleAndOrigin();
526 void wxDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
528 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
529 m_logicalOriginY
= y
* m_signY
;
530 ComputeScaleAndOrigin();
532 void wxDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
534 m_externalDeviceOriginX
= x
;
535 m_externalDeviceOriginY
= y
;
536 ComputeScaleAndOrigin();
540 void wxDC::SetInternalDeviceOrigin( long x
, long y
)
542 m_internalDeviceOriginX
= x
;
543 m_internalDeviceOriginY
= y
;
544 ComputeScaleAndOrigin();
546 void wxDC::GetInternalDeviceOrigin( long *x
, long *y
)
548 if (x
) *x
= m_internalDeviceOriginX
;
549 if (y
) *y
= m_internalDeviceOriginY
;
552 void wxDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
554 m_signX
= (xLeftRight
? 1 : -1);
555 m_signY
= (yBottomUp
? -1 : 1);
556 ComputeScaleAndOrigin();
559 wxSize
wxDC::GetPPI() const
561 return wxSize(72, 72);
564 int wxDC::GetDepth() const
566 if ( IsPortColor( (CGrafPtr
) m_macPort
) )
568 return ( (**GetPortPixMap( (CGrafPtr
) m_macPort
)).pixelSize
) ;
573 void wxDC::ComputeScaleAndOrigin()
575 // CMB: copy scale to see if it changes
576 double origScaleX
= m_scaleX
;
577 double origScaleY
= m_scaleY
;
579 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
580 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
582 m_deviceOriginX
= m_internalDeviceOriginX
+ m_externalDeviceOriginX
;
583 m_deviceOriginY
= m_internalDeviceOriginY
+ m_externalDeviceOriginY
;
585 // CMB: if scale has changed call SetPen to recalulate the line width
586 if (m_scaleX
!= origScaleX
|| m_scaleY
!= origScaleY
)
588 // this is a bit artificial, but we need to force wxDC to think
589 // the pen has changed
590 wxPen
* pen
= & GetPen();
596 void wxDC::SetPalette( const wxPalette
& palette
)
600 void wxDC::SetBackgroundMode( int mode
)
602 m_backgroundMode
= mode
;
605 void wxDC::SetFont( const wxFont
&font
)
608 m_macFontInstalled
= false ;
611 void wxDC::SetPen( const wxPen
&pen
)
618 m_macPenInstalled
= false ;
621 void wxDC::SetBrush( const wxBrush
&brush
)
623 if (m_brush
== brush
)
627 m_macBrushInstalled
= false ;
630 void wxDC::SetBackground( const wxBrush
&brush
)
632 if (m_backgroundBrush
== brush
)
635 m_backgroundBrush
= brush
;
637 if (!m_backgroundBrush
.Ok())
639 m_macBrushInstalled
= false ;
642 void wxDC::SetLogicalFunction( int function
)
644 if (m_logicalFunction
== function
)
647 m_logicalFunction
= function
;
648 m_macFontInstalled
= false ;
649 m_macBrushInstalled
= false ;
650 m_macPenInstalled
= false ;
653 void wxDC::DoFloodFill( wxCoord x
, wxCoord y
, const wxColour
& col
,
658 bool wxDC::DoGetPixel( wxCoord x
, wxCoord y
, wxColour
*col
) const
660 wxCHECK_MSG( Ok(), false, wxT("wxDC::DoGetPixel Invalid DC") );
661 wxMacPortSetter
helper(this) ;
665 GetCPixel( XLOG2DEVMAC(x
), YLOG2DEVMAC(y
), &colour
);
667 // Convert from Mac colour to wx
668 col
->Set( colour
.red
>> 8,
675 void wxDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
677 wxCHECK_RET(Ok(), wxT("Invalid DC"));
679 wxMacPortSetter
helper(this) ;
681 if (m_pen
.GetStyle() != wxTRANSPARENT
)
684 wxCoord offset
= ( (m_pen
.GetWidth() == 0 ? 1 :
685 m_pen
.GetWidth() ) * (wxCoord
)m_scaleX
- 1) / 2;
687 wxCoord xx1
= XLOG2DEVMAC(x1
) - offset
;
688 wxCoord yy1
= YLOG2DEVMAC(y1
) - offset
;
689 wxCoord xx2
= XLOG2DEVMAC(x2
) - offset
;
690 wxCoord yy2
= YLOG2DEVMAC(y2
) - offset
;
692 if ((m_pen
.GetCap() == wxCAP_ROUND
) &&
693 (m_pen
.GetWidth() <= 1))
695 // Implement LAST_NOT for MAC at least for
696 // orthogonal lines. RR.
718 void wxDC::DoCrossHair( wxCoord x
, wxCoord y
)
720 wxCHECK_RET( Ok(), wxT("wxDC::DoCrossHair Invalid window dc") );
722 if (m_pen
.GetStyle() != wxTRANSPARENT
)
727 wxCoord xx
= XLOG2DEVMAC(x
);
728 wxCoord yy
= YLOG2DEVMAC(y
);
731 ::MoveTo( XLOG2DEVMAC(0), yy
);
732 ::LineTo( XLOG2DEVMAC(w
), yy
);
733 ::MoveTo( xx
, YLOG2DEVMAC(0) );
734 ::LineTo( xx
, YLOG2DEVMAC(h
) );
736 CalcBoundingBox(x
, y
);
737 CalcBoundingBox(x
+w
, y
+h
);
743 * To draw arcs properly the angles need to be converted from the WX style:
744 * Angles start on the +ve X axis and go anti-clockwise (As you would draw on
745 * a normal axis on paper).
748 * Angles start on the +ve y axis and go clockwise.
749 * To achive this I work out which quadrant the angle lies in then map this to
750 * the equivalent quadrant on the Mac. (Sin and Cos values reveal which
751 * quadrant you are in).
753 static double wxConvertWXangleToMACangle(double angle
)
757 sin_a
= sin(angle
/ RAD2DEG
);
758 cos_a
= cos(angle
/ RAD2DEG
);
760 if( (sin_a
>= 0.0) && (cos_a
>= 0.0) ) {
761 angle
= acos(sin_a
) * RAD2DEG
;
763 else if( (sin_a
>= 0.0) && (cos_a
<= 0.0) ) {
765 angle
= acos(sin_a
) * RAD2DEG
+ 180;
767 else if( (sin_a
<= 0.0) && (cos_a
>= 0.0) ) {
768 angle
= acos(sin_a
) * RAD2DEG
+ 180;
770 else if( (sin_a
< 0.0) && (cos_a
< 0.0) ) {
772 angle
= acos(sin_a
) * RAD2DEG
+ 180;
777 void wxDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
778 wxCoord x2
, wxCoord y2
,
779 wxCoord xc
, wxCoord yc
)
781 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawArc Invalid DC"));
783 wxCoord xx1
= XLOG2DEVMAC(x1
);
784 wxCoord yy1
= YLOG2DEVMAC(y1
);
785 wxCoord xx2
= XLOG2DEVMAC(x2
);
786 wxCoord yy2
= YLOG2DEVMAC(y2
);
787 wxCoord xxc
= XLOG2DEVMAC(xc
);
788 wxCoord yyc
= YLOG2DEVMAC(yc
);
789 double dx
= xx1
- xxc
;
790 double dy
= yy1
- yyc
;
791 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
792 wxCoord rad
= (wxCoord
)radius
;
793 double radius1
, radius2
;
795 if (xx1
== xx2
&& yy1
== yy2
)
800 else if (radius
== 0.0)
802 radius1
= radius2
= 0.0;
806 radius1
= (xx1
- xxc
== 0) ?
807 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
808 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
809 radius2
= (xx2
- xxc
== 0) ?
810 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
811 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
813 wxCoord alpha2
= wxCoord(radius2
- radius1
);
814 wxCoord alpha1
= wxCoord(wxConvertWXangleToMACangle(radius1
));
815 if( (xx1
> xx2
) || (yy1
> yy2
) ) {
819 Rect r
= { yyc
- rad
, xxc
- rad
, yyc
+ rad
, xxc
+ rad
};
821 if(m_brush
.GetStyle() != wxTRANSPARENT
) {
823 PaintArc(&r
, alpha1
, alpha2
);
825 if(m_pen
.GetStyle() != wxTRANSPARENT
) {
827 FrameArc(&r
, alpha1
, alpha2
);
831 void wxDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
832 double sa
, double ea
)
834 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawEllepticArc Invalid DC"));
837 double angle
= sa
- ea
; // Order important Mac in opposite direction to wx
839 wxCoord xx
= XLOG2DEVMAC(x
);
840 wxCoord yy
= YLOG2DEVMAC(y
);
841 wxCoord ww
= m_signX
* XLOG2DEVREL(w
);
842 wxCoord hh
= m_signY
* YLOG2DEVREL(h
);
844 // handle -ve width and/or height
845 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
846 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
848 sa
= wxConvertWXangleToMACangle(sa
);
855 if(m_brush
.GetStyle() != wxTRANSPARENT
) {
857 PaintArc(&r
, (short)sa
, (short)angle
);
859 if(m_pen
.GetStyle() != wxTRANSPARENT
) {
861 FrameArc(&r
, (short)sa
, (short)angle
);
865 void wxDC::DoDrawPoint( wxCoord x
, wxCoord y
)
867 wxCHECK_RET(Ok(), wxT("Invalid DC"));
869 wxMacPortSetter
helper(this) ;
871 if (m_pen
.GetStyle() != wxTRANSPARENT
)
874 wxCoord xx1
= XLOG2DEVMAC(x
);
875 wxCoord yy1
= YLOG2DEVMAC(y
);
878 ::LineTo(xx1
+1, yy1
+1);
882 void wxDC::DoDrawLines(int n
, wxPoint points
[],
883 wxCoord xoffset
, wxCoord yoffset
)
885 wxCHECK_RET(Ok(), wxT("Invalid DC"));
886 wxMacPortSetter
helper(this) ;
888 if (m_pen
.GetStyle() == wxTRANSPARENT
)
893 wxCoord offset
= ( (m_pen
.GetWidth() == 0 ? 1 :
894 m_pen
.GetWidth() ) * (wxCoord
)m_scaleX
- 1) / 2 ;
896 wxCoord x1
, x2
, y1
, y2
;
897 x1
= XLOG2DEVMAC(points
[0].x
+ xoffset
);
898 y1
= YLOG2DEVMAC(points
[0].y
+ yoffset
);
899 ::MoveTo(x1
- offset
, y1
- offset
);
901 for (int i
= 0; i
< n
-1; i
++)
903 x2
= XLOG2DEVMAC(points
[i
+1].x
+ xoffset
);
904 y2
= YLOG2DEVMAC(points
[i
+1].y
+ yoffset
);
905 ::LineTo( x2
- offset
, y2
- offset
);
909 void wxDC::DoDrawPolygon(int n
, wxPoint points
[],
910 wxCoord xoffset
, wxCoord yoffset
,
913 wxCHECK_RET(Ok(), wxT("Invalid DC"));
914 wxMacPortSetter
helper(this) ;
916 wxCoord x1
, x2
, y1
, y2
;
918 if ( m_brush
.GetStyle() == wxTRANSPARENT
&& m_pen
.GetStyle() == wxTRANSPARENT
)
921 PolyHandle polygon
= OpenPoly();
923 x1
= XLOG2DEVMAC(points
[0].x
+ xoffset
);
924 y1
= YLOG2DEVMAC(points
[0].y
+ yoffset
);
927 for (int i
= 1; i
< n
; i
++)
929 x2
= XLOG2DEVMAC(points
[i
].x
+ xoffset
);
930 y2
= YLOG2DEVMAC(points
[i
].y
+ yoffset
);
934 // close the polyline if necessary
935 if ( x1
!= x2
|| y1
!= y2
)
942 if (m_brush
.GetStyle() != wxTRANSPARENT
)
946 ::PaintPoly( polygon
);
950 if (m_pen
.GetStyle() != wxTRANSPARENT
)
954 ::FramePoly( polygon
) ;
960 void wxDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
962 wxCHECK_RET(Ok(), wxT("Invalid DC"));
963 wxMacPortSetter
helper(this) ;
965 wxCoord xx
= XLOG2DEVMAC(x
);
966 wxCoord yy
= YLOG2DEVMAC(y
);
967 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
968 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
970 // CMB: draw nothing if transformed w or h is 0
971 if (ww
== 0 || hh
== 0)
974 // CMB: handle -ve width and/or height
987 Rect rect
= { yy
, xx
, yy
+ hh
, xx
+ ww
} ;
989 if (m_brush
.GetStyle() != wxTRANSPARENT
)
992 ::PaintRect( &rect
) ;
995 if (m_pen
.GetStyle() != wxTRANSPARENT
)
998 ::FrameRect( &rect
) ;
1002 void wxDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
1003 wxCoord width
, wxCoord height
,
1006 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1007 wxMacPortSetter
helper(this) ;
1010 radius
= - radius
* ((width
< height
) ? width
: height
);
1012 wxCoord xx
= XLOG2DEVMAC(x
);
1013 wxCoord yy
= YLOG2DEVMAC(y
);
1014 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
1015 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
1017 // CMB: draw nothing if transformed w or h is 0
1018 if (ww
== 0 || hh
== 0)
1021 // CMB: handle -ve width and/or height
1034 Rect rect
= { yy
, xx
, yy
+ hh
, xx
+ ww
} ;
1036 if (m_brush
.GetStyle() != wxTRANSPARENT
)
1039 ::PaintRoundRect( &rect
, int(radius
* 2) , int(radius
* 2) ) ;
1042 if (m_pen
.GetStyle() != wxTRANSPARENT
)
1045 ::FrameRoundRect( &rect
, int(radius
* 2) , int(radius
* 2) ) ;
1049 void wxDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
1051 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1052 wxMacPortSetter
helper(this) ;
1054 wxCoord xx
= XLOG2DEVMAC(x
);
1055 wxCoord yy
= YLOG2DEVMAC(y
);
1056 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
1057 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
1059 // CMB: draw nothing if transformed w or h is 0
1060 if (ww
== 0 || hh
== 0)
1063 // CMB: handle -ve width and/or height
1076 Rect rect
= { yy
, xx
, yy
+ hh
, xx
+ ww
} ;
1078 if (m_brush
.GetStyle() != wxTRANSPARENT
)
1081 ::PaintOval( &rect
) ;
1084 if (m_pen
.GetStyle() != wxTRANSPARENT
)
1087 ::FrameOval( &rect
) ;
1093 bool wxDC::CanDrawBitmap(void) const
1099 bool wxDC::DoBlit(wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
1100 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
,
1101 wxCoord xsrcMask
, wxCoord ysrcMask
)
1103 wxCHECK_MSG(Ok(), false, wxT("wxDC::DoBlit Illegal dc"));
1104 wxCHECK_MSG(source
->Ok(), false, wxT("wxDC::DoBlit Illegal source DC"));
1106 if ( logical_func
== wxNO_OP
)
1109 if (xsrcMask
== -1 && ysrcMask
== -1)
1111 xsrcMask
= xsrc
; ysrcMask
= ysrc
;
1114 // correct the parameter in case this dc does not have a mask at all
1116 if ( useMask
&& !source
->m_macMask
)
1119 Rect srcrect
, dstrect
;
1120 srcrect
.top
= source
->YLOG2DEVMAC(ysrc
) ;
1121 srcrect
.left
= source
->XLOG2DEVMAC(xsrc
) ;
1122 srcrect
.right
= source
->XLOG2DEVMAC(xsrc
+ width
) ;
1123 srcrect
.bottom
= source
->YLOG2DEVMAC(ysrc
+ height
) ;
1124 dstrect
.top
= YLOG2DEVMAC(ydest
) ;
1125 dstrect
.left
= XLOG2DEVMAC(xdest
) ;
1126 dstrect
.bottom
= YLOG2DEVMAC(ydest
+ height
) ;
1127 dstrect
.right
= XLOG2DEVMAC(xdest
+ width
) ;
1129 short mode
= kUnsupportedMode
;
1130 bool invertDestinationFirst
= false ;
1131 switch ( logical_func
)
1133 case wxAND
: // src AND dst
1134 mode
= srcOr
; // ok
1136 case wxAND_INVERT
: // (NOT src) AND dst
1137 mode
= notSrcOr
; // ok
1139 case wxAND_REVERSE
:// src AND (NOT dst)
1140 invertDestinationFirst
= true ;
1144 mode
= kEmulatedMode
;
1147 mode
= srcCopy
; // ok
1149 case wxEQUIV
: // (NOT src) XOR dst
1150 mode
= srcXor
; // ok
1152 case wxINVERT
: // NOT dst
1153 mode
= kEmulatedMode
; //or hilite ;
1155 case wxNAND
: // (NOT src) OR (NOT dst)
1156 invertDestinationFirst
= true ;
1159 case wxNOR
: // (NOT src) AND (NOT dst)
1160 invertDestinationFirst
= true ;
1163 case wxNO_OP
: // dst
1164 mode
= kEmulatedMode
; // this has already been handled upon entry
1166 case wxOR
: // src OR dst
1169 case wxOR_INVERT
: // (NOT src) OR dst
1172 case wxOR_REVERSE
: // src OR (NOT dst)
1173 invertDestinationFirst
= true ;
1177 mode
= kEmulatedMode
;
1179 case wxSRC_INVERT
: // (NOT src)
1180 mode
= notSrcCopy
; // ok
1182 case wxXOR
: // src XOR dst
1183 mode
= notSrcXor
; // ok
1191 if ( mode
== kUnsupportedMode
)
1193 wxFAIL_MSG("unsupported blitting mode" )
1197 CGrafPtr sourcePort
= (CGrafPtr
) source
->m_macPort
;
1198 PixMapHandle bmappixels
= GetGWorldPixMap( sourcePort
) ;
1199 if ( LockPixels(bmappixels
) )
1201 wxMacPortSetter
helper(this) ;
1202 RGBColor tempColor
;
1204 if ( source
->GetDepth() == 1 )
1206 RGBForeColor( &MAC_WXCOLORREF(m_textForegroundColour
.GetPixel()) ) ;
1207 RGBBackColor( &MAC_WXCOLORREF(m_textBackgroundColour
.GetPixel()) ) ;
1211 // the modes need this, otherwise we'll end up having really nice colors...
1212 RGBColor white
= { 0xFFFF, 0xFFFF,0xFFFF} ;
1213 RGBColor black
= { 0,0,0} ;
1214 RGBForeColor( &black
) ;
1215 RGBBackColor( &white
) ;
1218 if ( useMask
&& source
->m_macMask
)
1220 if ( mode
== srcCopy
)
1222 if ( LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source
->m_macMask
) ) ) )
1224 CopyMask( GetPortBitMapForCopyBits( sourcePort
) ,
1225 GetPortBitMapForCopyBits( MAC_WXHBITMAP(source
->m_macMask
) ) ,
1226 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort
) ) ,
1227 &srcrect
, &srcrect
, &dstrect
) ;
1228 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source
->m_macMask
) ) ) ;
1233 RgnHandle clipRgn
= NewRgn() ;
1234 LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source
->m_macMask
) ) ) ;
1235 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( MAC_WXHBITMAP(source
->m_macMask
) ) ) ;
1236 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source
->m_macMask
) ) ) ;
1237 OffsetRgn( clipRgn
, -srcrect
.left
+ dstrect
.left
, -srcrect
.top
+ dstrect
.top
) ;
1238 if ( mode
== kEmulatedMode
)
1241 ::PenPat(GetQDGlobalsBlack(&pat
));
1242 if ( logical_func
== wxSET
)
1244 RGBColor col
= { 0xFFFF, 0xFFFF, 0xFFFF } ;
1245 ::RGBForeColor( &col
) ;
1246 ::PaintRgn( clipRgn
) ;
1248 else if ( logical_func
== wxCLEAR
)
1250 RGBColor col
= { 0x0000, 0x0000, 0x0000 } ;
1251 ::RGBForeColor( &col
) ;
1252 ::PaintRgn( clipRgn
) ;
1254 else if ( logical_func
== wxINVERT
)
1256 MacInvertRgn( clipRgn
) ;
1260 for ( int y
= 0 ; y
< srcrect
.right
- srcrect
.left
; ++y
)
1262 for ( int x
= 0 ; x
< srcrect
.bottom
- srcrect
.top
; ++x
)
1264 Point dstPoint
= { dstrect
.top
+ y
, dstrect
.left
+ x
} ;
1265 Point srcPoint
= { srcrect
.top
+ y
, srcrect
.left
+ x
} ;
1266 if ( PtInRgn( dstPoint
, clipRgn
) )
1271 SetPort( (GrafPtr
) sourcePort
) ;
1272 GetCPixel( srcPoint
.h
, srcPoint
.v
, &srcColor
) ;
1273 SetPort( (GrafPtr
) m_macPort
) ;
1274 GetCPixel( dstPoint
.h
, dstPoint
.v
, &dstColor
) ;
1276 wxMacCalculateColour( logical_func
, srcColor
, dstColor
) ;
1277 SetCPixel( dstPoint
.h
, dstPoint
.v
, &dstColor
) ;
1285 if ( invertDestinationFirst
)
1287 MacInvertRgn( clipRgn
) ;
1289 CopyBits( GetPortBitMapForCopyBits( sourcePort
) ,
1290 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort
) ) ,
1291 &srcrect
, &dstrect
, mode
, clipRgn
) ;
1293 DisposeRgn( clipRgn
) ;
1298 RgnHandle clipRgn
= NewRgn() ;
1299 SetRectRgn( clipRgn
, dstrect
.left
, dstrect
.top
, dstrect
.right
, dstrect
.bottom
) ;
1300 if ( mode
== kEmulatedMode
)
1303 ::PenPat(GetQDGlobalsBlack(&pat
));
1304 if ( logical_func
== wxSET
)
1306 RGBColor col
= { 0xFFFF, 0xFFFF, 0xFFFF } ;
1307 ::RGBForeColor( &col
) ;
1308 ::PaintRgn( clipRgn
) ;
1310 else if ( logical_func
== wxCLEAR
)
1312 RGBColor col
= { 0x0000, 0x0000, 0x0000 } ;
1313 ::RGBForeColor( &col
) ;
1314 ::PaintRgn( clipRgn
) ;
1316 else if ( logical_func
== wxINVERT
)
1318 MacInvertRgn( clipRgn
) ;
1322 for ( int y
= 0 ; y
< srcrect
.right
- srcrect
.left
; ++y
)
1324 for ( int x
= 0 ; x
< srcrect
.bottom
- srcrect
.top
; ++x
)
1326 Point dstPoint
= { dstrect
.top
+ y
, dstrect
.left
+ x
} ;
1327 Point srcPoint
= { srcrect
.top
+ y
, srcrect
.left
+ x
} ;
1333 SetPort( (GrafPtr
) sourcePort
) ;
1334 GetCPixel( srcPoint
.h
, srcPoint
.v
, &srcColor
) ;
1335 SetPort( (GrafPtr
) m_macPort
) ;
1336 GetCPixel( dstPoint
.h
, dstPoint
.v
, &dstColor
) ;
1338 wxMacCalculateColour( logical_func
, srcColor
, dstColor
) ;
1339 SetCPixel( dstPoint
.h
, dstPoint
.v
, &dstColor
) ;
1348 if ( invertDestinationFirst
)
1350 MacInvertRgn( clipRgn
) ;
1352 CopyBits( GetPortBitMapForCopyBits( sourcePort
) ,
1353 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort
) ) ,
1354 &srcrect
, &dstrect
, mode
, NULL
) ;
1356 DisposeRgn( clipRgn
) ;
1358 UnlockPixels( bmappixels
) ;
1361 m_macPenInstalled
= false ;
1362 m_macBrushInstalled
= false ;
1363 m_macFontInstalled
= false ;
1368 inline Fixed
IntToFixed( int inInt
)
1370 return (((SInt32
) inInt
) << 16);
1374 void wxDC::DoDrawRotatedText(const wxString
& str
, wxCoord x
, wxCoord y
,
1377 wxCHECK_RET( Ok(), wxT("wxDC::DoDrawRotatedText Invalid window dc") );
1381 DrawText(str
, x
, y
);
1385 wxMacPortSetter
helper(this) ;
1389 if ( wxApp::s_macDefaultEncodingIsPC
)
1391 text
= wxMacMakeMacStringFromPC( str
) ;
1398 wxFontRefData
* font
= (wxFontRefData
*) m_font
.GetRefData() ;
1401 m_macFormerAliasState
= IsAntiAliasedTextEnabled(&m_macFormerAliasSize
);
1402 SetAntiAliasedTextEnabled(true, m_scaleY
* font
->m_macFontSize
);
1403 m_macAliasWasEnabled
= true ;
1406 OSStatus status
= noErr
;
1409 status
= TECCreateConverter(&ec
, kTextEncodingMacRoman
, kTextEncodingUnicodeDefault
);
1410 wxASSERT_MSG( status
== noErr
, "couldn't start converter" ) ;
1412 ByteCount byteOutLen
;
1413 ByteCount byteInLen
= text
.Length() ;
1414 ByteCount byteBufferLen
= byteInLen
*2 ;
1415 char* buf
= new char[byteBufferLen
] ;
1417 status
= TECConvertText(ec
, (ConstTextPtr
)text
.c_str() , byteInLen
, &byteInLen
,
1418 (TextPtr
)buf
, byteBufferLen
, &byteOutLen
);
1420 wxASSERT_MSG( status
== noErr
, "couldn't convert text" ) ;
1421 status
= TECDisposeConverter(ec
);
1422 wxASSERT_MSG( status
== noErr
, "couldn't dispose converter" ) ;
1424 ATSUTextLayout atsuLayout
;
1425 UniCharCount chars
= byteOutLen
/ 2 ;
1426 status
= ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr
) buf
, 0 , byteOutLen
/ 2 , byteOutLen
/ 2 , 1 ,
1427 &chars
, (ATSUStyle
*) &m_macATSUIStyle
, &atsuLayout
) ;
1428 wxASSERT_MSG( status
== noErr
, "couldn't create the layout of the rotated text" );
1430 Fixed atsuAngle
= IntToFixed( angle
) ;
1431 ByteCount angleSize
= sizeof(Fixed
) ;
1432 ATSUAttributeTag rotationTag
= kATSULineRotationTag
;
1433 ATSUAttributeValuePtr angleValue
= &atsuAngle
;
1434 status
= ::ATSUSetLayoutControls(atsuLayout
, 1 , &rotationTag
, &angleSize
, &angleValue
) ;
1436 status
= ::ATSUDrawText( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1437 IntToFixed(XLOG2DEVMAC(x
) ) , IntToFixed(YLOG2DEVMAC(y
) ) );
1438 wxASSERT_MSG( status
== noErr
, "couldn't draw the rotated text" );
1440 status
= ::ATSUMeasureTextImage( atsuLayout
, kATSUFromTextBeginning
, kATSUToTextEnd
,
1441 IntToFixed(XLOG2DEVMAC(x
) ) , IntToFixed(YLOG2DEVMAC(y
) ) , &rect
);
1442 wxASSERT_MSG( status
== noErr
, "couldn't measure the rotated text" );
1444 OffsetRect( &rect
, -m_macLocalOrigin
.x
, -m_macLocalOrigin
.y
) ;
1445 CalcBoundingBox(XDEV2LOG(rect
.left
), YDEV2LOG(rect
.top
) );
1446 CalcBoundingBox(XDEV2LOG(rect
.right
), YDEV2LOG(rect
.bottom
) );
1447 ::ATSUDisposeTextLayout(atsuLayout
);
1451 void wxDC::DoDrawText(const wxString
& strtext
, wxCoord x
, wxCoord y
)
1453 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawText Invalid DC"));
1454 wxMacPortSetter
helper(this) ;
1456 long xx
= XLOG2DEVMAC(x
);
1457 long yy
= YLOG2DEVMAC(y
);
1462 m_macFormerAliasState
= IsAntiAliasedTextEnabled(&m_macFormerAliasSize
);
1463 SetAntiAliasedTextEnabled(true, 8);
1464 m_macAliasWasEnabled
= true ;
1468 ::GetFontInfo( &fi
) ;
1471 ::MoveTo( xx
, yy
);
1472 if ( m_backgroundMode
== wxTRANSPARENT
)
1474 ::TextMode( srcOr
) ;
1478 ::TextMode( srcCopy
) ;
1481 const char *text
= NULL
;
1485 if ( wxApp::s_macDefaultEncodingIsPC
)
1487 macText
= wxMacMakeMacStringFromPC( strtext
) ;
1489 length
= macText
.Length() ;
1494 length
= strtext
.Length() ;
1503 if( text
[i
] == 13 || text
[i
] == 10)
1505 ::DrawText( text
, laststop
, i
- laststop
) ;
1507 ::MoveTo( xx
, yy
+ line
*(fi
.descent
+ fi
.ascent
+ fi
.leading
) );
1513 ::DrawText( text
, laststop
, i
- laststop
) ;
1514 ::TextMode( srcOr
) ;
1517 bool wxDC::CanGetTextExtent() const
1519 wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
1524 void wxDC::DoGetTextExtent( const wxString
&string
, wxCoord
*width
, wxCoord
*height
,
1525 wxCoord
*descent
, wxCoord
*externalLeading
,
1526 wxFont
*theFont
) const
1528 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1529 wxMacPortSetter
helper(this) ;
1531 wxFont formerFont
= m_font
;
1535 wxFontRefData
* font
= (wxFontRefData
*) m_font
.GetRefData() ;
1539 ::TextFont( font
->m_macFontNum
) ;
1540 ::TextSize( YLOG2DEVREL( font
->m_macFontSize
) ) ;
1541 ::TextFace( font
->m_macFontStyle
) ;
1550 ::GetFontInfo( &fi
) ;
1553 *height
= YDEV2LOGREL( fi
.descent
+ fi
.ascent
) ;
1555 *descent
=YDEV2LOGREL( fi
.descent
);
1556 if ( externalLeading
)
1557 *externalLeading
= YDEV2LOGREL( fi
.leading
) ;
1559 const char *text
= NULL
;
1562 if ( wxApp::s_macDefaultEncodingIsPC
)
1564 macText
= wxMacMakeMacStringFromPC( string
) ;
1566 length
= macText
.Length() ;
1571 length
= string
.Length() ;
1583 if( text
[i
] == 13 || text
[i
] == 10)
1586 *height
+= YDEV2LOGREL( fi
.descent
+ fi
.ascent
+ fi
.leading
) ;
1587 curwidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
1588 if ( curwidth
> *width
)
1589 *width
= XDEV2LOGREL( curwidth
) ;
1595 curwidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
1596 if ( curwidth
> *width
)
1597 *width
= XDEV2LOGREL( curwidth
) ;
1602 m_macFontInstalled
= false ;
1606 wxCoord
wxDC::GetCharWidth(void) const
1608 wxCHECK_MSG(Ok(), 1, wxT("Invalid DC"));
1610 wxMacPortSetter
helper(this) ;
1614 int width
= ::TextWidth( "n" , 0 , 1 ) ;
1616 return YDEV2LOGREL(width
) ;
1619 wxCoord
wxDC::GetCharHeight(void) const
1621 wxCHECK_MSG(Ok(), 1, wxT("Invalid DC"));
1623 wxMacPortSetter
helper(this) ;
1628 ::GetFontInfo( &fi
) ;
1630 return YDEV2LOGREL( fi
.descent
+ fi
.ascent
);
1633 void wxDC::Clear(void)
1635 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1636 wxMacPortSetter
helper(this) ;
1637 Rect rect
= { -32000 , -32000 , 32000 , 32000 } ;
1639 if (m_backgroundBrush
.GetStyle() != wxTRANSPARENT
)
1642 //MacInstallBrush() ;
1643 MacSetupBackgroundForCurrentPort( m_backgroundBrush
) ;
1644 ::EraseRect( &rect
) ;
1648 void wxDC::MacInstallFont() const
1650 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1651 // if ( m_macFontInstalled )
1653 Pattern blackColor
;
1655 wxFontRefData
* font
= (wxFontRefData
*) m_font
.GetRefData() ;
1659 ::TextFont( font
->m_macFontNum
) ;
1660 ::TextSize( short(m_scaleY
* font
->m_macFontSize
) ) ;
1661 ::TextFace( font
->m_macFontStyle
) ;
1663 m_macFontInstalled
= true ;
1664 m_macBrushInstalled
= false ;
1665 m_macPenInstalled
= false ;
1667 RGBColor forecolor
= MAC_WXCOLORREF( m_textForegroundColour
.GetPixel());
1668 RGBColor backcolor
= MAC_WXCOLORREF( m_textBackgroundColour
.GetPixel());
1669 ::RGBForeColor( &forecolor
);
1670 ::RGBBackColor( &backcolor
);
1676 GetFNum( "\pGeneva" , &fontnum
) ;
1677 ::TextFont( fontnum
) ;
1678 ::TextSize( short(m_scaleY
* 10) ) ;
1681 // todo reset after spacing changes - or store the current spacing somewhere
1683 m_macFontInstalled
= true ;
1684 m_macBrushInstalled
= false ;
1685 m_macPenInstalled
= false ;
1687 RGBColor forecolor
= MAC_WXCOLORREF( m_textForegroundColour
.GetPixel());
1688 RGBColor backcolor
= MAC_WXCOLORREF( m_textBackgroundColour
.GetPixel());
1689 ::RGBForeColor( &forecolor
);
1690 ::RGBBackColor( &backcolor
);
1693 short mode
= patCopy
;
1697 switch( m_logicalFunction
)
1702 case wxINVERT
: // NOT dst
1703 ::PenPat(GetQDGlobalsBlack(&blackColor
));
1706 case wxXOR
: // src XOR dst
1709 case wxOR_REVERSE
: // src OR (NOT dst)
1712 case wxSRC_INVERT
: // (NOT src)
1719 case wxAND_REVERSE
:// src AND (NOT dst)
1720 case wxAND
: // src AND dst
1721 case wxAND_INVERT
: // (NOT src) AND dst
1722 case wxNO_OP
: // dst
1723 case wxNOR
: // (NOT src) AND (NOT dst)
1724 case wxEQUIV
: // (NOT src) XOR dst
1725 case wxOR_INVERT
: // (NOT src) OR dst
1726 case wxNAND
: // (NOT src) OR (NOT dst)
1727 case wxOR
: // src OR dst
1729 // case wxSRC_OR: // source _bitmap_ OR destination
1730 // case wxSRC_AND: // source _bitmap_ AND destination
1735 OSStatus status
= noErr
;
1737 Fixed atsuSize
= IntToFixed(m_scaleY
* font
->m_macFontSize
) ;
1739 Style qdStyle
= font
->m_macFontStyle
;
1740 ATSUFontID atsuFont
= font
->m_macATSUFontID
;
1742 status
= ::ATSUCreateStyle(&(ATSUStyle
)m_macATSUIStyle
) ;
1743 wxASSERT_MSG( status
== noErr
, "couldn't create ATSU style" ) ;
1745 ATSUAttributeTag atsuTags
[] =
1751 kATSUQDBoldfaceTag
,
1753 kATSUQDUnderlineTag
,
1754 kATSUQDCondensedTag
,
1755 kATSUQDExtendedTag
,
1759 ByteCount atsuSizes
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1761 sizeof( ATSUFontID
) ,
1763 sizeof( RGBColor
) ,
1771 Boolean kTrue
= true ;
1772 Boolean kFalse
= false ;
1774 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
)/sizeof(ATSUAttributeTag
)] =
1778 &MAC_WXCOLORREF( m_textForegroundColour
.GetPixel() ) ,
1780 (qdStyle
& bold
) ? &kTrue
: &kFalse
,
1781 (qdStyle
& italic
) ? &kTrue
: &kFalse
,
1782 (qdStyle
& underline
) ? &kTrue
: &kFalse
,
1783 (qdStyle
& condense
) ? &kTrue
: &kFalse
,
1784 (qdStyle
& extend
) ? &kTrue
: &kFalse
,
1787 status
= ::ATSUSetAttributes((ATSUStyle
)m_macATSUIStyle
, sizeof(atsuTags
)/sizeof(ATSUAttributeTag
),
1788 atsuTags
, atsuSizes
, atsuValues
);
1789 wxASSERT_MSG( status
== noErr
, "couldn't set create ATSU style" ) ;
1793 static void wxMacGetHatchPattern(int hatchStyle
, Pattern
*pattern
)
1795 // we have our own pattern list now
1796 int thePatListID
= 128;
1800 case wxBDIAGONAL_HATCH
:
1803 case wxFDIAGONAL_HATCH
:
1809 case wxHORIZONTAL_HATCH
:
1812 case wxVERTICAL_HATCH
:
1815 case wxCROSSDIAG_HATCH
:
1819 theIndex
= 1; // solid pattern
1822 GetIndPattern( pattern
, thePatListID
, theIndex
);
1825 void wxDC::MacInstallPen() const
1827 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1831 // if ( m_macPenInstalled )
1834 RGBColor forecolor
= MAC_WXCOLORREF( m_pen
.GetColour().GetPixel());
1835 RGBColor backcolor
= MAC_WXCOLORREF( m_backgroundBrush
.GetColour().GetPixel());
1836 ::RGBForeColor( &forecolor
);
1837 ::RGBBackColor( &backcolor
);
1840 int penWidth
= m_pen
.GetWidth() * (int) m_scaleX
;
1842 // null means only one pixel, at whatever resolution
1843 if ( penWidth
== 0 )
1845 ::PenSize(penWidth
, penWidth
);
1847 int penStyle
= m_pen
.GetStyle();
1849 if (penStyle
== wxSOLID
)
1851 ::PenPat(GetQDGlobalsBlack(&blackColor
));
1853 else if (IS_HATCH(penStyle
))
1856 wxMacGetHatchPattern(penStyle
, &pat
);
1861 Pattern pat
= *GetQDGlobalsBlack(&blackColor
) ;
1865 for ( int i
= 0 ; i
< 8 ; ++i
)
1871 for ( int i
= 0 ; i
< 8 ; ++i
)
1877 for ( int i
= 0 ; i
< 8 ; ++i
)
1883 for ( int i
= 0 ; i
< 8 ; ++i
)
1891 int number
= m_pen
.GetDashes(&dash
) ;
1892 // right now we don't allocate larger pixmaps
1893 for ( int i
= 0 ; i
< 8 ; ++i
)
1895 pat
.pat
[i
] = dash
[0] ;
1903 short mode
= patCopy
;
1907 switch( m_logicalFunction
)
1909 case wxCOPY
: // only foreground color, leave background (thus not patCopy)
1912 case wxINVERT
: // NOT dst
1913 // ::PenPat(GetQDGlobalsBlack(&blackColor));
1916 case wxXOR
: // src XOR dst
1919 case wxOR_REVERSE
: // src OR (NOT dst)
1922 case wxSRC_INVERT
: // (NOT src)
1929 case wxAND_REVERSE
:// src AND (NOT dst)
1930 case wxAND
: // src AND dst
1931 case wxAND_INVERT
: // (NOT src) AND dst
1932 case wxNO_OP
: // dst
1933 case wxNOR
: // (NOT src) AND (NOT dst)
1934 case wxEQUIV
: // (NOT src) XOR dst
1935 case wxOR_INVERT
: // (NOT src) OR dst
1936 case wxNAND
: // (NOT src) OR (NOT dst)
1937 case wxOR
: // src OR dst
1939 // case wxSRC_OR: // source _bitmap_ OR destination
1940 // case wxSRC_AND: // source _bitmap_ AND destination
1944 m_macPenInstalled
= true ;
1945 m_macBrushInstalled
= false ;
1946 m_macFontInstalled
= false ;
1949 void wxDC::MacSetupBackgroundForCurrentPort(const wxBrush
& background
)
1951 Pattern whiteColor
;
1952 switch( background
.MacGetBrushKind() )
1954 case kwxMacBrushTheme
:
1956 ::SetThemeBackground( background
.GetMacTheme() , wxDisplayDepth() , true ) ;
1959 case kwxMacBrushThemeBackground
:
1962 ThemeBackgroundKind bg
= background
.GetMacThemeBackground( &extent
) ;
1963 ::ApplyThemeBackground( bg
, &extent
,kThemeStateActive
, wxDisplayDepth() , true ) ;
1966 case kwxMacBrushColour
:
1968 ::RGBBackColor( &MAC_WXCOLORREF( background
.GetColour().GetPixel()) );
1969 int brushStyle
= background
.GetStyle();
1970 if (brushStyle
== wxSOLID
)
1971 ::BackPat(GetQDGlobalsWhite(&whiteColor
));
1972 else if (IS_HATCH(brushStyle
))
1975 wxMacGetHatchPattern(brushStyle
, &pat
);
1980 ::BackPat(GetQDGlobalsWhite(&whiteColor
));
1987 void wxDC::MacInstallBrush() const
1989 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1991 Pattern blackColor
;
1992 // if ( m_macBrushInstalled )
1997 bool backgroundTransparent
= (GetBackgroundMode() == wxTRANSPARENT
) ;
1999 ::RGBForeColor( &MAC_WXCOLORREF( m_brush
.GetColour().GetPixel()) );
2000 ::RGBBackColor( &MAC_WXCOLORREF( m_backgroundBrush
.GetColour().GetPixel()) );
2002 int brushStyle
= m_brush
.GetStyle();
2003 if (brushStyle
== wxSOLID
)
2005 ::PenPat(GetQDGlobalsBlack(&blackColor
));
2007 else if (IS_HATCH(brushStyle
))
2010 wxMacGetHatchPattern(brushStyle
, &pat
);
2013 else if ( m_brush
.GetStyle() == wxSTIPPLE
|| m_brush
.GetStyle() == wxSTIPPLE_MASK_OPAQUE
)
2015 // we force this in order to be compliant with wxMSW
2016 backgroundTransparent
= false ;
2017 // for these the text fore (and back for MASK_OPAQUE) colors are used
2018 wxBitmap
* bitmap
= m_brush
.GetStipple() ;
2019 int width
= bitmap
->GetWidth() ;
2020 int height
= bitmap
->GetHeight() ;
2021 GWorldPtr gw
= NULL
;
2023 if ( m_brush
.GetStyle() == wxSTIPPLE
)
2024 gw
= MAC_WXHBITMAP(bitmap
->GetHBITMAP()) ;
2026 gw
= MAC_WXHBITMAP(bitmap
->GetMask()->GetMaskBitmap()) ;
2028 PixMapHandle gwpixmaphandle
= GetGWorldPixMap( gw
) ;
2029 LockPixels( gwpixmaphandle
) ;
2031 bool isMonochrome
= !IsPortColor( gw
) ;
2033 if ( !isMonochrome
)
2035 if ( (**gwpixmaphandle
).pixelSize
== 1 )
2036 isMonochrome
= true ;
2039 if ( isMonochrome
&& width
== 8 && height
== 8 )
2041 ::RGBForeColor( &MAC_WXCOLORREF( m_textForegroundColour
.GetPixel()) );
2042 ::RGBForeColor( &MAC_WXCOLORREF( m_textBackgroundColour
.GetPixel()) );
2043 BitMap
* gwbitmap
= (BitMap
*) *gwpixmaphandle
; // since the color depth is 1 it is a BitMap
2044 UInt8
*gwbits
= (UInt8
*) gwbitmap
->baseAddr
;
2045 int alignment
= gwbitmap
->rowBytes
& 0x7FFF ;
2047 for ( int i
= 0 ; i
< 8 ; ++i
)
2049 pat
.pat
[i
] = gwbits
[i
*alignment
+0] ;
2051 UnlockPixels( GetGWorldPixMap( gw
) ) ;
2056 // this will be the code to handle power of 2 patterns, we will have to arrive at a nice
2057 // caching scheme before putting this into production
2060 PixPatHandle pixpat
= NewPixPat() ;
2062 CopyPixMap(gwpixmaphandle
, (**pixpat
).patMap
);
2063 imageSize
= GetPixRowBytes((**pixpat
).patMap
) *
2064 ((**(**pixpat
).patMap
).bounds
.bottom
-
2065 (**(**pixpat
).patMap
).bounds
.top
);
2067 PtrToHand( (**gwpixmaphandle
).baseAddr
, &image
, imageSize
);
2068 (**pixpat
).patData
= image
;
2071 CTabHandle ctable
= ((**((**pixpat
).patMap
)).pmTable
) ;
2072 ColorSpecPtr ctspec
= (ColorSpecPtr
) &(**ctable
).ctTable
;
2073 if ( ctspec
[0].rgb
.red
== 0x0000 )
2075 ctspec
[1].rgb
= MAC_WXCOLORREF( m_textBackgroundColour
.GetPixel()) ;
2076 ctspec
[0].rgb
= MAC_WXCOLORREF( m_textForegroundColour
.GetPixel()) ;
2080 ctspec
[0].rgb
= MAC_WXCOLORREF( m_textBackgroundColour
.GetPixel()) ;
2081 ctspec
[1].rgb
= MAC_WXCOLORREF( m_textForegroundColour
.GetPixel()) ;
2083 ::CTabChanged( ctable
) ;
2085 ::PenPixPat(pixpat
);
2086 m_macForegroundPixMap
= pixpat
;
2088 UnlockPixels( gwpixmaphandle
) ;
2092 ::PenPat(GetQDGlobalsBlack(&blackColor
));
2095 short mode
= patCopy
;
2096 switch( m_logicalFunction
)
2099 if ( backgroundTransparent
)
2104 case wxINVERT
: // NOT dst
2105 if ( !backgroundTransparent
)
2107 ::PenPat(GetQDGlobalsBlack(&blackColor
));
2111 case wxXOR
: // src XOR dst
2114 case wxOR_REVERSE
: // src OR (NOT dst)
2117 case wxSRC_INVERT
: // (NOT src)
2124 case wxAND_REVERSE
:// src AND (NOT dst)
2125 case wxAND
: // src AND dst
2126 case wxAND_INVERT
: // (NOT src) AND dst
2127 case wxNO_OP
: // dst
2128 case wxNOR
: // (NOT src) AND (NOT dst)
2129 case wxEQUIV
: // (NOT src) XOR dst
2130 case wxOR_INVERT
: // (NOT src) OR dst
2131 case wxNAND
: // (NOT src) OR (NOT dst)
2132 case wxOR
: // src OR dst
2134 // case wxSRC_OR: // source _bitmap_ OR destination
2135 // case wxSRC_AND: // source _bitmap_ AND destination
2139 m_macBrushInstalled
= true ;
2140 m_macPenInstalled
= false ;
2141 m_macFontInstalled
= false ;
2144 // ---------------------------------------------------------------------------
2145 // coordinates transformations
2146 // ---------------------------------------------------------------------------
2149 wxCoord
wxDCBase::DeviceToLogicalX(wxCoord x
) const
2151 return ((wxDC
*)this)->XDEV2LOG(x
);
2154 wxCoord
wxDCBase::DeviceToLogicalY(wxCoord y
) const
2156 return ((wxDC
*)this)->YDEV2LOG(y
);
2159 wxCoord
wxDCBase::DeviceToLogicalXRel(wxCoord x
) const
2161 return ((wxDC
*)this)->XDEV2LOGREL(x
);
2164 wxCoord
wxDCBase::DeviceToLogicalYRel(wxCoord y
) const
2166 return ((wxDC
*)this)->YDEV2LOGREL(y
);
2169 wxCoord
wxDCBase::LogicalToDeviceX(wxCoord x
) const
2171 return ((wxDC
*)this)->XLOG2DEV(x
);
2174 wxCoord
wxDCBase::LogicalToDeviceY(wxCoord y
) const
2176 return ((wxDC
*)this)->YLOG2DEV(y
);
2179 wxCoord
wxDCBase::LogicalToDeviceXRel(wxCoord x
) const
2181 return ((wxDC
*)this)->XLOG2DEVREL(x
);
2184 wxCoord
wxDCBase::LogicalToDeviceYRel(wxCoord y
) const
2186 return ((wxDC
*)this)->YLOG2DEVREL(y
);