]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dc.cpp
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"
27 #if !USE_SHARED_LIBRARY
28 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxObject
)
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 #define mm2inches 0.0393700787402
36 #define inches2mm 25.4
37 #define mm2twips 56.6929133859
38 #define twips2mm 0.0176388888889
39 #define mm2pt 2.83464566929
40 #define pt2mm 0.352777777778
42 const double M_PI
= 3.14159265358979 ;
44 const double RAD2DEG
= 180.0 / M_PI
;
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 static inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
51 static inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
52 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
63 m_mm_to_pix_x
= mm2pt
;
64 m_mm_to_pix_y
= mm2pt
;
66 m_internalDeviceOriginX
= 0;
67 m_internalDeviceOriginY
= 0;
68 m_externalDeviceOriginX
= 0;
69 m_externalDeviceOriginY
= 0;
71 m_logicalScaleX
= 1.0;
72 m_logicalScaleY
= 1.0;
78 m_needComputeScaleX
= FALSE
;
79 m_needComputeScaleY
= FALSE
;
81 m_maxX
= m_maxY
= -100000;
82 m_minY
= m_minY
= 100000;
88 m_macFontInstalled
= false ;
89 m_macBrushInstalled
= false ;
90 m_macPenInstalled
= false ;
92 m_macLocalOrigin
.h
= m_macLocalOrigin
.v
= 0 ;
93 m_macClipRect
.left
= -32000 ;
94 m_macClipRect
.top
= -32000 ;
95 m_macClipRect
.right
= 32000 ;
96 m_macClipRect
.bottom
= 32000 ;
99 m_font
= *wxNORMAL_FONT
;
100 m_brush
= *wxWHITE_BRUSH
;
102 wxMacPortSetter::wxMacPortSetter( const wxDC
* dc
) :
103 m_ph( dc
->m_macPort
)
105 wxASSERT( dc
->Ok() ) ;
107 dc
->MacSetupPort(&m_ph
) ;
110 wxMacPortSetter::~wxMacPortSetter()
117 void wxDC::MacSetupPort(AGAPortHelper
* help
) const
119 // help->Setup( m_macPort ) ;
120 ::SetOrigin(-m_macLocalOrigin
.h
, -m_macLocalOrigin
.v
);
124 Rect clip
= { m_clipY1
, m_clipX1
, m_clipY2
, m_clipX2
} ;
125 ::SectRect( &clip
, &m_macClipRect
, &clip
) ;
126 ::ClipRect( &clip
) ;
130 ::ClipRect(&m_macClipRect
);
134 m_macFontInstalled
= false ;
135 m_macBrushInstalled
= false ;
136 m_macPenInstalled
= false ;
139 void wxDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
141 wxCHECK_RET( Ok(), wxT("invalid window dc") );
143 wxCHECK_RET( bmp
.Ok(), wxT("invalid bitmap") );
145 wxMacPortSetter
helper(this) ;
147 wxCoord xx
= XLOG2DEV(x
);
148 wxCoord yy
= YLOG2DEV(y
);
149 wxCoord w
= bmp
.GetWidth();
150 wxCoord h
= bmp
.GetHeight();
151 wxCoord ww
= XLOG2DEVREL(w
);
152 wxCoord hh
= YLOG2DEVREL(h
);
154 // Set up drawing mode
155 short mode
= (m_logicalFunction
== wxCOPY
? srcCopy
:
156 //m_logicalFunction == wxCLEAR ? WHITENESS :
157 //m_logicalFunction == wxSET ? BLACKNESS :
158 m_logicalFunction
== wxINVERT
? hilite
:
159 //m_logicalFunction == wxAND ? MERGECOPY :
160 m_logicalFunction
== wxOR
? srcOr
:
161 m_logicalFunction
== wxSRC_INVERT
? notSrcCopy
:
162 m_logicalFunction
== wxXOR
? srcXor
:
163 m_logicalFunction
== wxOR_REVERSE
? notSrcOr
:
164 //m_logicalFunction == wxAND_REVERSE ? SRCERASE :
165 //m_logicalFunction == wxSRC_OR ? srcOr :
166 //m_logicalFunction == wxSRC_AND ? SRCAND :
169 if ( bmp
.GetBitmapType() == kMacBitmapTypePict
) {
170 Rect bitmaprect
= { 0 , 0 , hh
, ww
};
171 ::OffsetRect( &bitmaprect
, xx
, yy
) ;
172 ::DrawPicture( bmp
.GetPict(), &bitmaprect
) ;
174 else if ( bmp
.GetBitmapType() == kMacBitmapTypeGrafWorld
)
176 GWorldPtr bmapworld
= bmp
.GetHBITMAP();
177 PixMapHandle bmappixels
;
179 // Set foreground and background colours (for bitmaps depth = 1)
180 if(bmp
.GetDepth() == 1)
182 RGBColor fore
= m_textForegroundColour
.GetPixel();
183 RGBColor back
= m_textBackgroundColour
.GetPixel();
189 RGBColor white
= { 0xFFFF, 0xFFFF,0xFFFF} ;
190 RGBColor black
= { 0,0,0} ;
191 RGBForeColor( &black
) ;
192 RGBBackColor( &white
) ;
195 bmappixels
= GetGWorldPixMap( bmapworld
) ;
197 wxCHECK_RET(LockPixels(bmappixels
),
198 wxT("DoDrawBitmap: Unable to lock pixels"));
200 Rect source
= { 0, 0, h
, w
};
201 Rect dest
= { yy
, xx
, yy
+ hh
, xx
+ ww
};
203 if ( useMask
&& bmp
.GetMask() )
205 if( LockPixels(GetGWorldPixMap(bmp
.GetMask()->GetMaskBitmap())))
209 GetPortBitMapForCopyBits(bmapworld
),
210 GetPortBitMapForCopyBits(bmp
.GetMask()->GetMaskBitmap()),
211 GetPortBitMapForCopyBits( m_macPort
),
212 &source
, &source
, &dest
, mode
, NULL
214 UnlockPixels(GetGWorldPixMap(bmp
.GetMask()->GetMaskBitmap()));
218 CopyBits( GetPortBitMapForCopyBits( bmapworld
),
219 GetPortBitMapForCopyBits( m_macPort
),
220 &source
, &dest
, mode
, NULL
) ;
222 UnlockPixels( bmappixels
) ;
224 else if ( bmp
.GetBitmapType() == kMacBitmapTypeIcon
)
226 Rect bitmaprect
= { 0 , 0 , bmp
.GetHeight(), bmp
.GetWidth() } ;
227 OffsetRect( &bitmaprect
, xx
, yy
) ;
228 PlotCIconHandle( &bitmaprect
, atNone
, ttNone
, bmp
.GetHICON() ) ;
230 m_macPenInstalled
= false ;
231 m_macBrushInstalled
= false ;
232 m_macFontInstalled
= false ;
236 void wxDC::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
238 wxCHECK_RET(Ok(), wxT("Invalid dc wxDC::DoDrawIcon"));
240 wxCHECK_RET(icon
.Ok(), wxT("Invalid icon wxDC::DoDrawIcon"));
242 DoDrawBitmap( icon
, x
, y
, icon
.GetMask() != NULL
) ;
244 void wxDC::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
246 wxCHECK_RET(Ok(), wxT("wxDC::DoSetClippingRegion Invalid DC"));
247 wxCoord xx
, yy
, ww
, hh
;
251 ww
= XLOG2DEVREL(width
);
252 hh
= YLOG2DEVREL(height
);
256 m_clipX1
= wxMax( m_clipX1
, xx
);
257 m_clipY1
= wxMax( m_clipY1
, yy
);
258 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
));
259 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
));
271 void wxDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
273 wxCHECK_RET( Ok(), wxT("invalid window dc") ) ;
275 wxMacPortSetter
helper(this) ;
278 DestroyClippingRegion();
282 wxCoord xx
, yy
, ww
, hh
;
283 region
.GetBox( xx
, yy
, ww
, hh
);
284 wxDC::DoSetClippingRegion( xx
, yy
, ww
, hh
);
287 void wxDC::DestroyClippingRegion()
289 wxMacPortSetter
helper(this) ;
292 void wxDC::DoGetSize( int* width
, int* height
) const
294 *width
= m_maxX
-m_minX
;
295 *height
= m_maxY
-m_minY
;
297 void wxDC::DoGetSizeMM( int* width
, int* height
) const
302 *width
= long( double(w
) / (m_scaleX
*m_mm_to_pix_x
) );
303 *height
= long( double(h
) / (m_scaleY
*m_mm_to_pix_y
) );
305 void wxDC::SetTextForeground( const wxColour
&col
)
307 wxCHECK_RET(Ok(), wxT("Invalid DC"));
308 m_textForegroundColour
= col
;
309 m_macFontInstalled
= false ;
311 void wxDC::SetTextBackground( const wxColour
&col
)
313 wxCHECK_RET(Ok(), wxT("Invalid DC"));
314 m_textBackgroundColour
= col
;
315 m_macFontInstalled
= false ;
317 void wxDC::SetMapMode( int mode
)
322 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
325 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
328 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
331 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
335 SetLogicalScale( 1.0, 1.0 );
338 if (mode
!= wxMM_TEXT
)
340 m_needComputeScaleX
= TRUE
;
341 m_needComputeScaleY
= TRUE
;
344 void wxDC::SetUserScale( double x
, double y
)
346 // allow negative ? -> no
349 ComputeScaleAndOrigin();
351 void wxDC::SetLogicalScale( double x
, double y
)
356 ComputeScaleAndOrigin();
358 void wxDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
360 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
361 m_logicalOriginY
= y
* m_signY
;
362 ComputeScaleAndOrigin();
364 void wxDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
366 m_externalDeviceOriginX
= x
;
367 m_externalDeviceOriginY
= y
;
368 ComputeScaleAndOrigin();
372 void wxDC::SetInternalDeviceOrigin( long x
, long y
)
374 m_internalDeviceOriginX
= x
;
375 m_internalDeviceOriginY
= y
;
376 ComputeScaleAndOrigin();
378 void wxDC::GetInternalDeviceOrigin( long *x
, long *y
)
380 if (x
) *x
= m_internalDeviceOriginX
;
381 if (y
) *y
= m_internalDeviceOriginY
;
384 void wxDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
386 m_signX
= (xLeftRight
? 1 : -1);
387 m_signY
= (yBottomUp
? -1 : 1);
388 ComputeScaleAndOrigin();
391 void wxDC::CalcBoundingBox( long x, long y )
393 if (x < m_minX) m_minX = x;
394 if (y < m_minY) m_minY = y;
395 if (x > m_maxX) m_maxX = x;
396 if (y > m_maxY) m_maxY = y;
398 wxSize
wxDC::GetPPI() const
400 return wxSize(72, 72);
403 int wxDC::GetDepth() const
405 return wxDisplayDepth() ;
408 void wxDC::ComputeScaleAndOrigin()
410 // CMB: copy scale to see if it changes
411 double origScaleX
= m_scaleX
;
412 double origScaleY
= m_scaleY
;
414 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
415 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
417 m_deviceOriginX
= m_internalDeviceOriginX
+ m_externalDeviceOriginX
;
418 m_deviceOriginY
= m_internalDeviceOriginY
+ m_externalDeviceOriginY
;
420 // CMB: if scale has changed call SetPen to recalulate the line width
421 if (m_scaleX
!= origScaleX
|| m_scaleY
!= origScaleY
)
423 // this is a bit artificial, but we need to force wxDC to think
424 // the pen has changed
425 wxPen
* pen
= & GetPen();
431 void wxDC::SetPalette( const wxPalette
& palette
)
435 void wxDC::SetBackgroundMode( int mode
)
437 m_backgroundMode
= mode
;
440 void wxDC::SetFont( const wxFont
&font
)
442 wxCHECK_RET(Ok(), wxT("Invalid DC"));
445 m_macFontInstalled
= false ;
448 void wxDC::SetPen( const wxPen
&pen
)
450 wxCHECK_RET(Ok(), wxT("Invalid DC"));
457 m_macPenInstalled
= false ;
460 void wxDC::SetBrush( const wxBrush
&brush
)
462 wxCHECK_RET(Ok(), wxT("Invalid DC"));
464 if (m_brush
== brush
)
468 m_macBrushInstalled
= false ;
471 void wxDC::SetBackground( const wxBrush
&brush
)
473 wxCHECK_RET(Ok(), wxT("Invalid DC"));
475 if (m_backgroundBrush
== brush
)
478 m_backgroundBrush
= brush
;
480 if (!m_backgroundBrush
.Ok())
482 m_macBrushInstalled
= false ;
485 void wxDC::SetLogicalFunction( int function
)
487 if (m_logicalFunction
== function
)
490 m_logicalFunction
= function
;
491 m_macFontInstalled
= false ;
492 m_macBrushInstalled
= false ;
493 m_macPenInstalled
= false ;
496 void wxDC::DoFloodFill( wxCoord x
, wxCoord y
, const wxColour
& col
,
501 bool wxDC::DoGetPixel( wxCoord x
, wxCoord y
, wxColour
*col
) const
503 wxCHECK_MSG( Ok(), false, wxT("wxDC::DoGetPixel Invalid DC") );
504 wxMacPortSetter
helper(this) ;
508 GetCPixel( XLOG2DEV(x
), YLOG2DEV(y
), &colour
);
510 // Convert from Mac colour to wx
511 col
->Set( colour
.red
>> 8,
518 void wxDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
520 wxCHECK_RET(Ok(), wxT("Invalid DC"));
522 wxMacPortSetter
helper(this) ;
524 if (m_pen
.GetStyle() != wxTRANSPARENT
)
527 wxCoord offset
= ( (m_pen
.GetWidth() == 0 ? 1 :
528 m_pen
.GetWidth() ) * (wxCoord
)m_scaleX
- 1) / 2;
530 wxCoord xx1
= XLOG2DEV(x1
) - offset
;
531 wxCoord yy1
= YLOG2DEV(y1
) - offset
;
532 wxCoord xx2
= XLOG2DEV(x2
) - offset
;
533 wxCoord yy2
= YLOG2DEV(y2
) - offset
;
535 if ((m_pen
.GetCap() == wxCAP_ROUND
) &&
536 (m_pen
.GetWidth() <= 1))
538 // Implement LAST_NOT for MAC at least for
539 // orthogonal lines. RR.
561 void wxDC::DoCrossHair( wxCoord x
, wxCoord y
)
563 wxCHECK_RET( Ok(), wxT("wxDC::DoCrossHair Invalid window dc") );
565 if (m_pen
.GetStyle() != wxTRANSPARENT
)
570 wxCoord xx
= XLOG2DEV(x
);
571 wxCoord yy
= YLOG2DEV(y
);
575 ::LineTo( XLOG2DEVREL(w
), yy
);
577 ::LineTo( xx
, YLOG2DEVREL(h
) );
582 * To draw arcs properly the angles need to be converted from the WX style:
583 * Angles start on the +ve X axis and go anti-clockwise (As you would draw on
584 * a normal axis on paper).
587 * Angles start on the +ve y axis and go clockwise.
588 * To achive this I work out which quadrant the angle lies in then map this to
589 * the equivalent quadrant on the Mac. (Sin and Cos values reveal which
590 * quadrant you are in).
592 static double wxConvertWXangleToMACangle(double angle
)
596 sin_a
= sin(angle
/ RAD2DEG
);
597 cos_a
= cos(angle
/ RAD2DEG
);
599 if( (sin_a
>= 0.0) && (cos_a
>= 0.0) ) {
600 angle
= acos(sin_a
) * RAD2DEG
;
602 else if( (sin_a
>= 0.0) && (cos_a
<= 0.0) ) {
604 angle
= acos(sin_a
) * RAD2DEG
+ 180;
606 else if( (sin_a
<= 0.0) && (cos_a
>= 0.0) ) {
607 angle
= acos(sin_a
) * RAD2DEG
+ 180;
609 else if( (sin_a
< 0.0) && (cos_a
< 0.0) ) {
611 angle
= acos(sin_a
) * RAD2DEG
+ 180;
616 void wxDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
617 wxCoord x2
, wxCoord y2
,
618 wxCoord xc
, wxCoord yc
)
620 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawArc Invalid DC"));
622 wxCoord xx1
= XLOG2DEV(x1
);
623 wxCoord yy1
= YLOG2DEV(y1
);
624 wxCoord xx2
= XLOG2DEV(x2
);
625 wxCoord yy2
= YLOG2DEV(y2
);
626 wxCoord xxc
= XLOG2DEV(xc
);
627 wxCoord yyc
= YLOG2DEV(yc
);
628 double dx
= xx1
- xxc
;
629 double dy
= yy1
- yyc
;
630 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
631 wxCoord rad
= (wxCoord
)radius
;
632 double radius1
, radius2
;
634 if (xx1
== xx2
&& yy1
== yy2
)
639 else if (radius
== 0.0)
641 radius1
= radius2
= 0.0;
645 radius1
= (xx1
- xxc
== 0) ?
646 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
647 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
648 radius2
= (xx2
- xxc
== 0) ?
649 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
650 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
652 wxCoord alpha2
= wxCoord(radius2
- radius1
);
653 wxCoord alpha1
= wxCoord(wxConvertWXangleToMACangle(radius1
));
654 if( (xx1
> xx2
) || (yy1
> yy2
) ) {
658 Rect r
= { yyc
- rad
, xxc
- rad
, yyc
+ rad
, xxc
+ rad
};
660 if(m_brush
.GetStyle() != wxTRANSPARENT
) {
662 PaintArc(&r
, alpha1
, alpha2
);
664 if(m_pen
.GetStyle() != wxTRANSPARENT
) {
666 FrameArc(&r
, alpha1
, alpha2
);
670 void wxDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
671 double sa
, double ea
)
673 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawEllepticArc Invalid DC"));
676 double angle
= sa
- ea
; // Order important Mac in opposite direction to wx
678 wxCoord xx
= XLOG2DEV(x
);
679 wxCoord yy
= YLOG2DEV(y
);
680 wxCoord ww
= m_signX
* XLOG2DEVREL(w
);
681 wxCoord hh
= m_signY
* YLOG2DEVREL(h
);
683 // handle -ve width and/or height
684 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
685 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
687 sa
= wxConvertWXangleToMACangle(sa
);
694 if(m_brush
.GetStyle() != wxTRANSPARENT
) {
696 PaintArc(&r
, (short)sa
, (short)angle
);
698 if(m_pen
.GetStyle() != wxTRANSPARENT
) {
700 FrameArc(&r
, (short)sa
, (short)angle
);
704 void wxDC::DoDrawPoint( wxCoord x
, wxCoord y
)
706 wxCHECK_RET(Ok(), wxT("Invalid DC"));
708 wxMacPortSetter
helper(this) ;
710 if (m_pen
.GetStyle() != wxTRANSPARENT
)
713 wxCoord xx1
= XLOG2DEV(x
);
714 wxCoord yy1
= YLOG2DEV(y
);
717 ::LineTo(xx1
+1, yy1
+1);
721 void wxDC::DoDrawLines(int n
, wxPoint points
[],
722 wxCoord xoffset
, wxCoord yoffset
)
724 wxCHECK_RET(Ok(), wxT("Invalid DC"));
725 wxMacPortSetter
helper(this) ;
727 if (m_pen
.GetStyle() == wxTRANSPARENT
)
732 wxCoord offset
= ( (m_pen
.GetWidth() == 0 ? 1 :
733 m_pen
.GetWidth() ) * (wxCoord
)m_scaleX
- 1) / 2 ;
735 wxCoord x1
, x2
, y1
, y2
;
736 x1
= XLOG2DEV(points
[0].x
+ xoffset
);
737 y1
= YLOG2DEV(points
[0].y
+ yoffset
);
738 ::MoveTo(x1
- offset
, y1
- offset
);
740 for (int i
= 0; i
< n
-1; i
++)
742 x2
= XLOG2DEV(points
[i
+1].x
+ xoffset
);
743 y2
= YLOG2DEV(points
[i
+1].y
+ yoffset
);
744 ::LineTo( x2
- offset
, y2
- offset
);
748 void wxDC::DoDrawPolygon(int n
, wxPoint points
[],
749 wxCoord xoffset
, wxCoord yoffset
,
752 wxCHECK_RET(Ok(), wxT("Invalid DC"));
753 wxMacPortSetter
helper(this) ;
755 wxCoord x1
, x2
, y1
, y2
;
757 if (m_brush
.GetStyle() != wxTRANSPARENT
)
759 PolyHandle polygon
= OpenPoly();
761 x1
= XLOG2DEV(points
[0].x
+ xoffset
);
762 y1
= YLOG2DEV(points
[0].y
+ yoffset
);
765 for (int i
= 0; i
< n
-1; i
++)
767 x2
= XLOG2DEV(points
[i
+1].x
+ xoffset
);
768 y2
= YLOG2DEV(points
[i
+1].y
+ yoffset
);
775 ::PaintPoly( polygon
);
780 if (m_pen
.GetStyle() != wxTRANSPARENT
)
782 PolyHandle polygon
= OpenPoly();
784 x1
= XLOG2DEV(points
[0].x
+ xoffset
);
785 y1
= YLOG2DEV(points
[0].y
+ yoffset
);
788 for (int i
= 0; i
< n
-1; i
++)
790 x2
= XLOG2DEV(points
[i
+1].x
+ xoffset
);
791 y2
= YLOG2DEV(points
[i
+1].y
+ yoffset
);
795 // return to origin to close path
801 ::FramePoly( polygon
) ;
807 void wxDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
809 wxCHECK_RET(Ok(), wxT("Invalid DC"));
810 wxMacPortSetter
helper(this) ;
812 wxCoord xx
= XLOG2DEV(x
);
813 wxCoord yy
= YLOG2DEV(y
);
814 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
815 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
817 // CMB: draw nothing if transformed w or h is 0
818 if (ww
== 0 || hh
== 0)
821 // CMB: handle -ve width and/or height
834 Rect rect
= { yy
, xx
, yy
+ hh
, xx
+ ww
} ;
836 if (m_brush
.GetStyle() != wxTRANSPARENT
)
839 ::PaintRect( &rect
) ;
842 if (m_pen
.GetStyle() != wxTRANSPARENT
)
845 ::FrameRect( &rect
) ;
849 void wxDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
850 wxCoord width
, wxCoord height
,
853 wxCHECK_RET(Ok(), wxT("Invalid DC"));
854 wxMacPortSetter
helper(this) ;
857 radius
= - radius
* ((width
< height
) ? width
: height
);
859 wxCoord xx
= XLOG2DEV(x
);
860 wxCoord yy
= YLOG2DEV(y
);
861 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
862 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
864 // CMB: draw nothing if transformed w or h is 0
865 if (ww
== 0 || hh
== 0)
868 // CMB: handle -ve width and/or height
881 Rect rect
= { yy
, xx
, yy
+ hh
, xx
+ ww
} ;
883 if (m_brush
.GetStyle() != wxTRANSPARENT
)
886 ::PaintRoundRect( &rect
, int(radius
* 2) , int(radius
* 2) ) ;
889 if (m_pen
.GetStyle() != wxTRANSPARENT
)
892 ::FrameRoundRect( &rect
, int(radius
* 2) , int(radius
* 2) ) ;
896 void wxDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
898 wxCHECK_RET(Ok(), wxT("Invalid DC"));
899 wxMacPortSetter
helper(this) ;
901 wxCoord xx
= XLOG2DEV(x
);
902 wxCoord yy
= YLOG2DEV(y
);
903 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
904 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
906 // CMB: draw nothing if transformed w or h is 0
907 if (ww
== 0 || hh
== 0)
910 // CMB: handle -ve width and/or height
923 Rect rect
= { yy
, xx
, yy
+ hh
, xx
+ ww
} ;
925 if (m_brush
.GetStyle() != wxTRANSPARENT
)
928 ::PaintOval( &rect
) ;
931 if (m_pen
.GetStyle() != wxTRANSPARENT
)
934 ::FrameOval( &rect
) ;
940 bool wxDC::CanDrawBitmap(void) const
946 bool wxDC::DoBlit(wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
947 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
)
949 wxCHECK_MSG(Ok(), false, wxT("wxDC::DoBlit Illegal dc"));
950 wxCHECK_MSG(source
->Ok(), false, wxT("wxDC::DoBlit Illegal source DC"));
951 wxMacPortSetter
helper(this) ;
953 CGrafPtr sourcePort
= (CGrafPtr
) source
->m_macPort
;
954 PixMapHandle bmappixels
= GetGWorldPixMap( sourcePort
) ;
955 RGBColor white
= { 0xFFFF, 0xFFFF,0xFFFF} ;
956 RGBColor black
= { 0,0,0} ;
957 RGBColor forecolor
= m_textForegroundColour
.GetPixel();
958 RGBColor backcolor
= m_textBackgroundColour
.GetPixel();
959 RGBForeColor( &forecolor
) ;
960 RGBBackColor( &backcolor
) ;
962 if ( LockPixels(bmappixels
) )
964 Rect srcrect
, dstrect
;
965 srcrect
.top
= source
->YLOG2DEV(ysrc
) ;
966 srcrect
.left
= source
->XLOG2DEV(xsrc
) ;
967 srcrect
.right
= source
->XLOG2DEV(xsrc
+ width
) ;
968 srcrect
.bottom
= source
->YLOG2DEV(ysrc
+ height
) ;
969 dstrect
.top
= YLOG2DEV(ydest
) ;
970 dstrect
.left
= XLOG2DEV(xdest
) ;
971 dstrect
.bottom
= YLOG2DEV(ydest
+ height
) ;
972 dstrect
.right
= XLOG2DEV(xdest
+ width
) ;
974 short mode
= (logical_func
== wxCOPY
? srcCopy
:
975 // logical_func == wxCLEAR ? WHITENESS :
976 // logical_func == wxSET ? BLACKNESS :
977 logical_func
== wxINVERT
? hilite
:
978 // logical_func == wxAND ? MERGECOPY :
979 logical_func
== wxOR
? srcOr
:
980 logical_func
== wxSRC_INVERT
? notSrcCopy
:
981 logical_func
== wxXOR
? srcXor
:
982 // logical_func == wxOR_REVERSE ? MERGEPAINT :
983 // logical_func == wxAND_REVERSE ? SRCERASE :
984 // logical_func == wxSRC_OR ? srcOr :
985 // logical_func == wxSRC_AND ? SRCAND :
988 if ( useMask
&& source
->m_macMask
)
990 wxASSERT( mode
== srcCopy
) ;
991 if ( LockPixels( GetGWorldPixMap( source
->m_macMask
) ) )
993 CopyMask( GetPortBitMapForCopyBits( sourcePort
) ,
994 GetPortBitMapForCopyBits( source
->m_macMask
) ,
995 GetPortBitMapForCopyBits( m_macPort
) ,
996 &srcrect
, &srcrect
, &dstrect
) ;
997 UnlockPixels( GetGWorldPixMap( source
->m_macMask
) ) ;
1002 CopyBits( GetPortBitMapForCopyBits( sourcePort
) ,
1003 GetPortBitMapForCopyBits( m_macPort
) ,
1004 &srcrect
, &dstrect
, mode
, NULL
) ;
1006 UnlockPixels( bmappixels
) ;
1009 m_macPenInstalled
= false ;
1010 m_macBrushInstalled
= false ;
1011 m_macFontInstalled
= false ;
1016 void wxDC::DoDrawRotatedText(const wxString
& text
, wxCoord x
, wxCoord y
,
1019 wxCHECK_RET( Ok(), wxT("wxDC::DoDrawRotatedText Invalid window dc") );
1023 DrawText(text
, x
, y
);
1029 // the size of the text
1031 GetTextExtent(text
, &w
, &h
);
1033 // draw the string normally
1036 dc
.SelectObject(src
);
1037 dc
.SetFont(GetFont());
1038 dc
.SetBackground(*wxWHITE_BRUSH
);
1039 dc
.SetBrush(*wxBLACK_BRUSH
);
1041 dc
.DrawText(text
, 0, 0);
1042 dc
.SetFont(wxNullFont
);
1043 dc
.SelectObject(wxNullBitmap
);
1045 wxMacPortSetter
helper(this) ;
1047 // Calculate the size of the rotated bounding box.
1048 double rad
= DegToRad(angle
);
1049 double dx
= cos(rad
);
1050 double dy
= sin(rad
);
1052 // the rectngle vertices are counted clockwise with the first one being at
1053 // (0, 0) (or, rather, at (x, y))
1055 double y2
= -w
* dy
; // y axis points to the bottom, hence minus
1058 double x3
= x4
+ x2
;
1059 double y3
= y4
+ y2
;
1062 wxCoord maxX
= (wxCoord
)(dmax(x2
, dmax(x3
, x4
)) + 0.5);
1063 wxCoord maxY
= (wxCoord
)(dmax(y2
, dmax(y3
, y4
)) + 0.5);
1064 wxCoord minX
= (wxCoord
)(dmin(x2
, dmin(x3
, x4
)) - 0.5);
1065 wxCoord minY
= (wxCoord
)(dmin(y2
, dmin(y3
, y4
)) - 0.5);
1067 // prepare to blit-with-rotate the bitmap to the DC
1070 RGBColor colText
= m_textForegroundColour
.GetPixel();
1071 RGBColor colBack
= m_textBackgroundColour
.GetPixel();
1073 unsigned char *data
= image
.GetData();
1076 double r
, angleOrig
;
1079 // paint pixel by pixel
1080 for ( wxCoord srcX
= 0; srcX
< w
; srcX
++ )
1082 for ( wxCoord srcY
= 0; srcY
< h
; srcY
++ )
1084 // transform source coords to dest coords
1085 r
= sqrt( (double)(srcX
* srcX
+ srcY
* srcY
) );
1086 angleOrig
= atan2((double)srcY
, (double)srcX
) - rad
;
1087 dstX
= (wxCoord
)(r
* cos(angleOrig
) + 0.5);
1088 dstY
= (wxCoord
)(r
* sin(angleOrig
) + 0.5);
1091 textPixel
= data
[(srcY
*w
+ srcX
)*3] == 0;
1092 if ( textPixel
|| (m_backgroundMode
== wxSOLID
) )
1094 SetCPixel(XLOG2DEV(x
+ dstX
), YLOG2DEV(y
+ dstY
),
1095 textPixel
? &colText
: &colBack
);
1100 // it would be better to draw with non underlined font and draw the line
1101 // manually here (it would be more straight...)
1103 if ( m_font
.GetUnderlined() )
1105 ::MoveTo(XLOG2DEV(x
+ x4
), YLOG2DEV(y
+ y4
+ font
->descent
));
1106 ::LineTo(XLOG2DEV(x
+ x3
), YLOG2DEV(y
+ y3
+ font
->descent
));
1110 // update the bounding box
1111 CalcBoundingBox(x
+ minX
, y
+ minY
);
1112 CalcBoundingBox(x
+ maxX
, y
+ maxY
);
1114 void wxDC::DoDrawText(const wxString
& strtext
, wxCoord x
, wxCoord y
)
1116 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawText Invalid DC"));
1117 wxMacPortSetter
helper(this) ;
1119 long xx
= XLOG2DEV(x
);
1120 long yy
= YLOG2DEV(y
);
1122 // if (m_pen.GetStyle() != wxTRANSPARENT)
1126 Rect clip = { -32000 , -32000 , 32000 , 32000 } ;
1128 ::ClipRect( &clip ) ;
1132 ::GetFontInfo( &fi
) ;
1135 ::MoveTo( xx
, yy
);
1136 if ( m_backgroundMode
== wxTRANSPARENT
)
1138 ::TextMode( srcOr
) ;
1142 ::TextMode( srcCopy
) ;
1145 const char *text
= NULL
;
1149 if ( wxApp::s_macDefaultEncodingIsPC
)
1151 macText
= wxMacMakeMacStringFromPC( strtext
) ;
1153 length
= macText
.Length() ;
1158 length
= strtext
.Length() ;
1167 if( text
[i
] == 13 || text
[i
] == 10)
1169 ::DrawText( text
, laststop
, i
- laststop
) ;
1171 ::MoveTo( xx
, yy
+ line
*(fi
.descent
+ fi
.ascent
+ fi
.leading
) );
1177 ::DrawText( text
, laststop
, i
- laststop
) ;
1178 ::TextMode( srcOr
) ;
1182 bool wxDC::CanGetTextExtent() const
1184 wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
1189 void wxDC::DoGetTextExtent( const wxString
&string
, wxCoord
*width
, wxCoord
*height
,
1190 wxCoord
*descent
, wxCoord
*externalLeading
,
1191 wxFont
*theFont
) const
1193 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1194 wxMacPortSetter
helper(this) ;
1196 wxFont formerFont
= m_font
;
1200 wxFontRefData
* font
= (wxFontRefData
*) m_font
.GetRefData() ;
1204 ::TextFont( font
->m_macFontNum
) ;
1205 ::TextSize( YLOG2DEVREL( font
->m_macFontSize
) ) ;
1206 ::TextFace( font
->m_macFontStyle
) ;
1215 ::GetFontInfo( &fi
) ;
1218 *height
= YDEV2LOGREL( fi
.descent
+ fi
.ascent
) ;
1220 *descent
=YDEV2LOGREL( fi
.descent
);
1221 if ( externalLeading
)
1222 *externalLeading
= YDEV2LOGREL( fi
.leading
) ;
1224 const char *text
= NULL
;
1227 if ( wxApp::s_macDefaultEncodingIsPC
)
1229 macText
= wxMacMakeMacStringFromPC( string
) ;
1231 length
= macText
.Length() ;
1236 length
= string
.Length() ;
1248 if( text
[i
] == 13 || text
[i
] == 10)
1251 *height
+= YDEV2LOGREL( fi
.descent
+ fi
.ascent
+ fi
.leading
) ;
1252 curwidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
1253 if ( curwidth
> *width
)
1254 *width
= XDEV2LOGREL( curwidth
) ;
1260 curwidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
1261 if ( curwidth
> *width
)
1262 *width
= XDEV2LOGREL( curwidth
) ;
1267 m_macFontInstalled
= false ;
1271 wxCoord
wxDC::GetCharWidth(void) const
1273 wxCHECK_MSG(Ok(), 1, wxT("Invalid DC"));
1275 wxMacPortSetter
helper(this) ;
1279 int width
= ::TextWidth( "n" , 0 , 1 ) ;
1281 return YDEV2LOGREL(width
) ;
1284 wxCoord
wxDC::GetCharHeight(void) const
1286 wxCHECK_MSG(Ok(), 1, wxT("Invalid DC"));
1288 wxMacPortSetter
helper(this) ;
1293 ::GetFontInfo( &fi
) ;
1295 return YDEV2LOGREL( fi
.descent
+ fi
.ascent
);
1298 void wxDC::Clear(void)
1300 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1301 wxMacPortSetter
helper(this) ;
1302 Rect rect
= { -32767 , -32767 , 32767 , 32767 } ;
1304 if (m_backgroundBrush
.GetStyle() != wxTRANSPARENT
)
1307 ::EraseRect( &rect
) ;
1311 void wxDC::MacInstallFont() const
1313 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1314 // if ( m_macFontInstalled )
1316 Pattern blackColor
;
1318 wxFontRefData
* font
= (wxFontRefData
*) m_font
.GetRefData() ;
1322 ::TextFont( font
->m_macFontNum
) ;
1323 ::TextSize( short(m_scaleY
* font
->m_macFontSize
) ) ;
1324 ::TextFace( font
->m_macFontStyle
) ;
1326 m_macFontInstalled
= true ;
1327 m_macBrushInstalled
= false ;
1328 m_macPenInstalled
= false ;
1330 RGBColor forecolor
= m_textForegroundColour
.GetPixel();
1331 RGBColor backcolor
= m_textBackgroundColour
.GetPixel();
1332 ::RGBForeColor( &forecolor
);
1333 ::RGBBackColor( &backcolor
);
1339 GetFNum( "\pGeneva" , &fontnum
) ;
1340 ::TextFont( fontnum
) ;
1341 ::TextSize( short(m_scaleY
* 10) ) ;
1344 // todo reset after spacing changes - or store the current spacing somewhere
1346 m_macFontInstalled
= true ;
1347 m_macBrushInstalled
= false ;
1348 m_macPenInstalled
= false ;
1350 RGBColor forecolor
= m_textForegroundColour
.GetPixel();
1351 RGBColor backcolor
= m_textBackgroundColour
.GetPixel();
1352 ::RGBForeColor( &forecolor
);
1353 ::RGBBackColor( &backcolor
);
1356 short mode
= patCopy
;
1360 switch( m_logicalFunction
)
1365 case wxINVERT
: // NOT dst
1366 ::PenPat(GetQDGlobalsBlack(&blackColor
));
1369 case wxXOR
: // src XOR dst
1372 case wxOR_REVERSE
: // src OR (NOT dst)
1375 case wxSRC_INVERT
: // (NOT src)
1382 case wxAND_REVERSE
:// src AND (NOT dst)
1383 case wxAND
: // src AND dst
1384 case wxAND_INVERT
: // (NOT src) AND dst
1385 case wxNO_OP
: // dst
1386 case wxNOR
: // (NOT src) AND (NOT dst)
1387 case wxEQUIV
: // (NOT src) XOR dst
1388 case wxOR_INVERT
: // (NOT src) OR dst
1389 case wxNAND
: // (NOT src) OR (NOT dst)
1390 case wxOR
: // src OR dst
1392 // case wxSRC_OR: // source _bitmap_ OR destination
1393 // case wxSRC_AND: // source _bitmap_ AND destination
1399 static void wxMacGetHatchPattern(int hatchStyle
, Pattern
*pattern
)
1401 int thePatListID
= sysPatListID
;
1405 case wxBDIAGONAL_HATCH
:
1406 theIndex
= 34; // WCH: this is not good
1408 case wxFDIAGONAL_HATCH
:
1414 case wxHORIZONTAL_HATCH
:
1417 case wxVERTICAL_HATCH
:
1420 case wxCROSSDIAG_HATCH
:
1421 theIndex
= 4; // WCH: this is not good
1424 theIndex
= 1; // solid pattern
1427 GetIndPattern( pattern
, thePatListID
, theIndex
);
1430 void wxDC::MacInstallPen() const
1432 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1436 // if ( m_macPenInstalled )
1439 RGBColor forecolor
= m_pen
.GetColour().GetPixel();
1440 RGBColor backcolor
= m_backgroundBrush
.GetColour().GetPixel();
1441 ::RGBForeColor( &forecolor
);
1442 ::RGBBackColor( &backcolor
);
1445 int penWidth
= m_pen
.GetWidth() * (int) m_scaleX
;
1447 // null means only one pixel, at whatever resolution
1448 if ( penWidth
== 0 )
1450 ::PenSize(penWidth
, penWidth
);
1452 int penStyle
= m_pen
.GetStyle();
1454 if (penStyle
== wxSOLID
)
1456 ::PenPat(GetQDGlobalsBlack(&blackColor
));
1458 else if (IS_HATCH(penStyle
))
1461 wxMacGetHatchPattern(penStyle
, &pat
);
1466 ::PenPat(GetQDGlobalsBlack(&blackColor
));
1469 short mode
= patCopy
;
1473 switch( m_logicalFunction
)
1478 case wxINVERT
: // NOT dst
1479 ::PenPat(GetQDGlobalsBlack(&blackColor
));
1482 case wxXOR
: // src XOR dst
1485 case wxOR_REVERSE
: // src OR (NOT dst)
1488 case wxSRC_INVERT
: // (NOT src)
1495 case wxAND_REVERSE
:// src AND (NOT dst)
1496 case wxAND
: // src AND dst
1497 case wxAND_INVERT
: // (NOT src) AND dst
1498 case wxNO_OP
: // dst
1499 case wxNOR
: // (NOT src) AND (NOT dst)
1500 case wxEQUIV
: // (NOT src) XOR dst
1501 case wxOR_INVERT
: // (NOT src) OR dst
1502 case wxNAND
: // (NOT src) OR (NOT dst)
1503 case wxOR
: // src OR dst
1505 // case wxSRC_OR: // source _bitmap_ OR destination
1506 // case wxSRC_AND: // source _bitmap_ AND destination
1510 m_macPenInstalled
= true ;
1511 m_macBrushInstalled
= false ;
1512 m_macFontInstalled
= false ;
1515 void wxDC::MacInstallBrush() const
1517 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1519 Pattern blackColor
, whiteColor
;
1520 // if ( m_macBrushInstalled )
1525 RGBColor forecolor
= m_brush
.GetColour().GetPixel();
1526 RGBColor backcolor
= m_backgroundBrush
.GetColour().GetPixel();
1527 ::RGBForeColor( &forecolor
);
1528 ::RGBBackColor( &backcolor
);
1530 int brushStyle
= m_brush
.GetStyle();
1531 if (brushStyle
== wxSOLID
)
1532 ::PenPat(GetQDGlobalsBlack(&blackColor
));
1533 else if (IS_HATCH(brushStyle
))
1536 wxMacGetHatchPattern(brushStyle
, &pat
);
1541 ::PenPat(GetQDGlobalsBlack(&blackColor
));
1547 brushStyle
= m_backgroundBrush
.GetStyle();
1548 if (brushStyle
== wxSOLID
)
1549 ::BackPat(GetQDGlobalsWhite(&whiteColor
));
1550 else if (IS_HATCH(brushStyle
))
1553 wxMacGetHatchPattern(brushStyle
, &pat
);
1558 ::BackPat(GetQDGlobalsWhite(&whiteColor
));
1561 short mode
= patCopy
;
1565 switch( m_logicalFunction
)
1570 case wxINVERT
: // NOT dst
1571 ::PenPat(GetQDGlobalsBlack(&blackColor
));
1574 case wxXOR
: // src XOR dst
1577 case wxOR_REVERSE
: // src OR (NOT dst)
1580 case wxSRC_INVERT
: // (NOT src)
1587 case wxAND_REVERSE
:// src AND (NOT dst)
1588 case wxAND
: // src AND dst
1589 case wxAND_INVERT
: // (NOT src) AND dst
1590 case wxNO_OP
: // dst
1591 case wxNOR
: // (NOT src) AND (NOT dst)
1592 case wxEQUIV
: // (NOT src) XOR dst
1593 case wxOR_INVERT
: // (NOT src) OR dst
1594 case wxNAND
: // (NOT src) OR (NOT dst)
1595 case wxOR
: // src OR dst
1597 // case wxSRC_OR: // source _bitmap_ OR destination
1598 // case wxSRC_AND: // source _bitmap_ AND destination
1602 m_macBrushInstalled
= true ;
1603 m_macPenInstalled
= false ;
1604 m_macFontInstalled
= false ;
1607 // ---------------------------------------------------------------------------
1608 // coordinates transformations
1609 // ---------------------------------------------------------------------------
1612 wxCoord
wxDCBase::DeviceToLogicalX(wxCoord x
) const
1614 return ((wxDC
*)this)->XDEV2LOG(x
);
1617 wxCoord
wxDCBase::DeviceToLogicalY(wxCoord y
) const
1619 return ((wxDC
*)this)->YDEV2LOG(y
);
1622 wxCoord
wxDCBase::DeviceToLogicalXRel(wxCoord x
) const
1624 return ((wxDC
*)this)->XDEV2LOGREL(x
);
1627 wxCoord
wxDCBase::DeviceToLogicalYRel(wxCoord y
) const
1629 return ((wxDC
*)this)->YDEV2LOGREL(y
);
1632 wxCoord
wxDCBase::LogicalToDeviceX(wxCoord x
) const
1634 return ((wxDC
*)this)->XLOG2DEV(x
);
1637 wxCoord
wxDCBase::LogicalToDeviceY(wxCoord y
) const
1639 return ((wxDC
*)this)->YLOG2DEV(y
);
1642 wxCoord
wxDCBase::LogicalToDeviceXRel(wxCoord x
) const
1644 return ((wxDC
*)this)->XLOG2DEVREL(x
);
1647 wxCoord
wxDCBase::LogicalToDeviceYRel(wxCoord y
) const
1649 return ((wxDC
*)this)->YLOG2DEVREL(y
);