]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/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
41 const double M_PI
= 3.14159265358979 ;
42 const double RAD2DEG
= 180.0 / M_PI
;
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 static inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
49 static inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
50 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
61 m_mm_to_pix_x
= mm2pt
;
62 m_mm_to_pix_y
= mm2pt
;
64 m_internalDeviceOriginX
= 0;
65 m_internalDeviceOriginY
= 0;
66 m_externalDeviceOriginX
= 0;
67 m_externalDeviceOriginY
= 0;
69 m_logicalScaleX
= 1.0;
70 m_logicalScaleY
= 1.0;
76 m_needComputeScaleX
= FALSE
;
77 m_needComputeScaleY
= FALSE
;
79 m_maxX
= m_maxY
= -100000;
80 m_minY
= m_minY
= 100000;
86 m_macFontInstalled
= false ;
87 m_macBrushInstalled
= false ;
88 m_macPenInstalled
= false ;
90 m_macLocalOrigin
.h
= m_macLocalOrigin
.v
= 0 ;
91 m_macClipRect
.left
= -32000 ;
92 m_macClipRect
.top
= -32000 ;
93 m_macClipRect
.right
= 32000 ;
94 m_macClipRect
.bottom
= 32000 ;
97 m_font
= *wxNORMAL_FONT
;
98 m_brush
= *wxWHITE_BRUSH
;
100 wxMacPortSetter::wxMacPortSetter( const wxDC
* dc
) :
101 m_ph( dc
->m_macPort
)
103 wxASSERT( dc
->Ok() ) ;
105 dc
->MacSetupPort(&m_ph
) ;
108 wxMacPortSetter::~wxMacPortSetter()
115 void wxDC::MacSetupPort(AGAPortHelper
* help
) const
117 // help->Setup( m_macPort ) ;
118 ::SetOrigin(-m_macLocalOrigin
.h
, -m_macLocalOrigin
.v
);
122 Rect clip
= { m_clipY1
, m_clipX1
, m_clipY2
, m_clipX2
} ;
123 ::SectRect( &clip
, &m_macClipRect
, &clip
) ;
124 ::ClipRect( &clip
) ;
128 ::ClipRect(&m_macClipRect
);
132 m_macFontInstalled
= false ;
133 m_macBrushInstalled
= false ;
134 m_macPenInstalled
= false ;
137 void wxDC::DoDrawBitmap( const wxBitmap
&bmp
, wxCoord x
, wxCoord y
, bool useMask
)
139 wxCHECK_RET( Ok(), wxT("invalid window dc") );
141 wxCHECK_RET( bmp
.Ok(), wxT("invalid bitmap") );
143 wxMacPortSetter
helper(this) ;
145 wxCoord xx
= XLOG2DEV(x
);
146 wxCoord yy
= YLOG2DEV(y
);
147 wxCoord w
= bmp
.GetWidth();
148 wxCoord h
= bmp
.GetHeight();
149 wxCoord ww
= XLOG2DEVREL(w
);
150 wxCoord hh
= YLOG2DEVREL(h
);
152 // Set up drawing mode
153 short mode
= (m_logicalFunction
== wxCOPY
? srcCopy
:
154 //m_logicalFunction == wxCLEAR ? WHITENESS :
155 //m_logicalFunction == wxSET ? BLACKNESS :
156 m_logicalFunction
== wxINVERT
? hilite
:
157 //m_logicalFunction == wxAND ? MERGECOPY :
158 m_logicalFunction
== wxOR
? srcOr
:
159 m_logicalFunction
== wxSRC_INVERT
? notSrcCopy
:
160 m_logicalFunction
== wxXOR
? srcXor
:
161 m_logicalFunction
== wxOR_REVERSE
? notSrcOr
:
162 //m_logicalFunction == wxAND_REVERSE ? SRCERASE :
163 //m_logicalFunction == wxSRC_OR ? srcOr :
164 //m_logicalFunction == wxSRC_AND ? SRCAND :
167 if ( bmp
.GetBitmapType() == kMacBitmapTypePict
) {
168 Rect bitmaprect
= { 0 , 0 , hh
, ww
};
169 ::OffsetRect( &bitmaprect
, xx
, yy
) ;
170 ::DrawPicture( bmp
.GetPict(), &bitmaprect
) ;
172 else if ( bmp
.GetBitmapType() == kMacBitmapTypeGrafWorld
)
174 GWorldPtr bmapworld
= bmp
.GetHBITMAP();
175 PixMapHandle bmappixels
;
177 // Set foreground and background colours (for bitmaps depth = 1)
178 if(bmp
.GetDepth() == 1)
180 RGBColor fore
= m_textForegroundColour
.GetPixel();
181 RGBColor back
= m_textBackgroundColour
.GetPixel();
187 RGBColor white
= { 0xFFFF, 0xFFFF,0xFFFF} ;
188 RGBColor black
= { 0,0,0} ;
189 RGBForeColor( &black
) ;
190 RGBBackColor( &white
) ;
193 bmappixels
= GetGWorldPixMap( bmapworld
) ;
195 wxCHECK_RET(LockPixels(bmappixels
),
196 wxT("DoDrawBitmap: Unable to lock pixels"));
198 Rect source
= { 0, 0, h
, w
};
199 Rect dest
= { yy
, xx
, yy
+ hh
, xx
+ ww
};
201 if ( useMask
&& bmp
.GetMask() )
203 if( LockPixels(GetGWorldPixMap(bmp
.GetMask()->GetMaskBitmap())))
207 GetPortBitMapForCopyBits(bmapworld
),
208 GetPortBitMapForCopyBits(bmp
.GetMask()->GetMaskBitmap()),
209 GetPortBitMapForCopyBits( m_macPort
),
210 &source
, &source
, &dest
, mode
, NULL
212 UnlockPixels(GetGWorldPixMap(bmp
.GetMask()->GetMaskBitmap()));
216 CopyBits( GetPortBitMapForCopyBits( bmapworld
),
217 GetPortBitMapForCopyBits( m_macPort
),
218 &source
, &dest
, mode
, NULL
) ;
220 UnlockPixels( bmappixels
) ;
222 else if ( bmp
.GetBitmapType() == kMacBitmapTypeIcon
)
224 Rect bitmaprect
= { 0 , 0 , bmp
.GetHeight(), bmp
.GetWidth() } ;
225 OffsetRect( &bitmaprect
, xx
, yy
) ;
226 PlotCIconHandle( &bitmaprect
, atNone
, ttNone
, bmp
.GetHICON() ) ;
228 m_macPenInstalled
= false ;
229 m_macBrushInstalled
= false ;
230 m_macFontInstalled
= false ;
234 void wxDC::DoDrawIcon( const wxIcon
&icon
, wxCoord x
, wxCoord y
)
236 wxCHECK_RET(Ok(), wxT("Invalid dc wxDC::DoDrawIcon"));
238 wxCHECK_RET(icon
.Ok(), wxT("Invalid icon wxDC::DoDrawIcon"));
240 DoDrawBitmap( icon
, x
, y
) ;
242 void wxDC::DoSetClippingRegion( wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
244 wxCHECK_RET(Ok(), wxT("wxDC::DoSetClippingRegion Invalid DC"));
245 wxCoord xx
, yy
, ww
, hh
;
249 ww
= XLOG2DEVREL(width
);
250 hh
= YLOG2DEVREL(height
);
254 m_clipX1
= wxMax( m_clipX1
, xx
);
255 m_clipY1
= wxMax( m_clipY1
, yy
);
256 m_clipX2
= wxMin( m_clipX2
, (xx
+ ww
));
257 m_clipY2
= wxMin( m_clipY2
, (yy
+ hh
));
269 void wxDC::DoSetClippingRegionAsRegion( const wxRegion
®ion
)
271 wxCHECK_RET( Ok(), wxT("invalid window dc") ) ;
273 wxMacPortSetter
helper(this) ;
276 DestroyClippingRegion();
280 wxCoord xx
, yy
, ww
, hh
;
281 region
.GetBox( xx
, yy
, ww
, hh
);
282 wxDC::DoSetClippingRegion( xx
, yy
, ww
, hh
);
285 void wxDC::DestroyClippingRegion()
287 wxMacPortSetter
helper(this) ;
290 void wxDC::DoGetSize( int* width
, int* height
) const
292 *width
= m_maxX
-m_minX
;
293 *height
= m_maxY
-m_minY
;
295 void wxDC::DoGetSizeMM( int* width
, int* height
) const
300 *width
= long( double(w
) / (m_scaleX
*m_mm_to_pix_x
) );
301 *height
= long( double(h
) / (m_scaleY
*m_mm_to_pix_y
) );
303 void wxDC::SetTextForeground( const wxColour
&col
)
305 wxCHECK_RET(Ok(), wxT("Invalid DC"));
306 m_textForegroundColour
= col
;
307 m_macFontInstalled
= false ;
309 void wxDC::SetTextBackground( const wxColour
&col
)
311 wxCHECK_RET(Ok(), wxT("Invalid DC"));
312 m_textBackgroundColour
= col
;
313 m_macFontInstalled
= false ;
315 void wxDC::SetMapMode( int mode
)
320 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
323 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
326 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
329 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
333 SetLogicalScale( 1.0, 1.0 );
336 if (mode
!= wxMM_TEXT
)
338 m_needComputeScaleX
= TRUE
;
339 m_needComputeScaleY
= TRUE
;
342 void wxDC::SetUserScale( double x
, double y
)
344 // allow negative ? -> no
347 ComputeScaleAndOrigin();
349 void wxDC::SetLogicalScale( double x
, double y
)
354 ComputeScaleAndOrigin();
356 void wxDC::SetLogicalOrigin( wxCoord x
, wxCoord y
)
358 m_logicalOriginX
= x
* m_signX
; // is this still correct ?
359 m_logicalOriginY
= y
* m_signY
;
360 ComputeScaleAndOrigin();
362 void wxDC::SetDeviceOrigin( wxCoord x
, wxCoord y
)
364 m_externalDeviceOriginX
= x
;
365 m_externalDeviceOriginY
= y
;
366 ComputeScaleAndOrigin();
370 void wxDC::SetInternalDeviceOrigin( long x
, long y
)
372 m_internalDeviceOriginX
= x
;
373 m_internalDeviceOriginY
= y
;
374 ComputeScaleAndOrigin();
376 void wxDC::GetInternalDeviceOrigin( long *x
, long *y
)
378 if (x
) *x
= m_internalDeviceOriginX
;
379 if (y
) *y
= m_internalDeviceOriginY
;
382 void wxDC::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
384 m_signX
= (xLeftRight
? 1 : -1);
385 m_signY
= (yBottomUp
? -1 : 1);
386 ComputeScaleAndOrigin();
389 void wxDC::CalcBoundingBox( long x, long y )
391 if (x < m_minX) m_minX = x;
392 if (y < m_minY) m_minY = y;
393 if (x > m_maxX) m_maxX = x;
394 if (y > m_maxY) m_maxY = y;
396 wxSize
wxDC::GetPPI() const
398 return wxSize(72, 72);
401 int wxDC::GetDepth() const
403 return wxDisplayDepth() ;
406 void wxDC::ComputeScaleAndOrigin()
408 // CMB: copy scale to see if it changes
409 double origScaleX
= m_scaleX
;
410 double origScaleY
= m_scaleY
;
412 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
413 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
415 m_deviceOriginX
= m_internalDeviceOriginX
+ m_externalDeviceOriginX
;
416 m_deviceOriginY
= m_internalDeviceOriginY
+ m_externalDeviceOriginY
;
418 // CMB: if scale has changed call SetPen to recalulate the line width
419 if (m_scaleX
!= origScaleX
|| m_scaleY
!= origScaleY
)
421 // this is a bit artificial, but we need to force wxDC to think
422 // the pen has changed
423 wxPen
* pen
= & GetPen();
429 void wxDC::SetPalette( const wxPalette
& palette
)
433 void wxDC::SetBackgroundMode( int mode
)
435 m_backgroundMode
= mode
;
438 void wxDC::SetFont( const wxFont
&font
)
440 wxCHECK_RET(Ok(), wxT("Invalid DC"));
443 m_macFontInstalled
= false ;
446 void wxDC::SetPen( const wxPen
&pen
)
448 wxCHECK_RET(Ok(), wxT("Invalid DC"));
455 m_macPenInstalled
= false ;
458 void wxDC::SetBrush( const wxBrush
&brush
)
460 wxCHECK_RET(Ok(), wxT("Invalid DC"));
462 if (m_brush
== brush
)
466 m_macBrushInstalled
= false ;
469 void wxDC::SetBackground( const wxBrush
&brush
)
471 wxCHECK_RET(Ok(), wxT("Invalid DC"));
473 if (m_backgroundBrush
== brush
)
476 m_backgroundBrush
= brush
;
478 if (!m_backgroundBrush
.Ok())
480 m_macBrushInstalled
= false ;
483 void wxDC::SetLogicalFunction( int function
)
485 if (m_logicalFunction
== function
)
488 m_logicalFunction
= function
;
489 m_macFontInstalled
= false ;
490 m_macBrushInstalled
= false ;
491 m_macPenInstalled
= false ;
494 void wxDC::DoFloodFill( wxCoord x
, wxCoord y
, const wxColour
& col
,
499 bool wxDC::DoGetPixel( wxCoord x
, wxCoord y
, wxColour
*col
) const
501 wxCHECK_MSG( Ok(), false, wxT("wxDC::DoGetPixel Invalid DC") );
502 wxMacPortSetter
helper(this) ;
506 GetCPixel( XLOG2DEV(x
), YLOG2DEV(y
), &colour
);
508 // Convert from Mac colour to wx
509 col
->Set( colour
.red
>> 8,
516 void wxDC::DoDrawLine( wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
)
518 wxCHECK_RET(Ok(), wxT("Invalid DC"));
520 wxMacPortSetter
helper(this) ;
522 if (m_pen
.GetStyle() != wxTRANSPARENT
)
525 wxCoord offset
= ( (m_pen
.GetWidth() == 0 ? 1 :
526 m_pen
.GetWidth() ) * (wxCoord
)m_scaleX
- 1) / 2;
528 wxCoord xx1
= XLOG2DEV(x1
);
529 wxCoord yy1
= YLOG2DEV(y1
);
530 wxCoord xx2
= XLOG2DEV(x2
);
531 wxCoord yy2
= YLOG2DEV(y2
);
533 ::MoveTo(xx1
- offset
, yy1
- offset
);
534 ::LineTo(xx2
- offset
, yy2
- offset
);
538 void wxDC::DoCrossHair( wxCoord x
, wxCoord y
)
540 wxCHECK_RET( Ok(), wxT("wxDC::DoCrossHair Invalid window dc") );
542 if (m_pen
.GetStyle() != wxTRANSPARENT
)
547 wxCoord xx
= XLOG2DEV(x
);
548 wxCoord yy
= YLOG2DEV(y
);
552 ::LineTo( XLOG2DEVREL(w
), yy
);
554 ::LineTo( xx
, YLOG2DEVREL(h
) );
559 * To draw arcs properly the angles need to be converted from the WX style:
560 * Angles start on the +ve X axis and go anti-clockwise (As you would draw on
561 * a normal axis on paper).
564 * Angles start on the +ve y axis and go clockwise.
565 * To achive this I work out which quadrant the angle lies in then map this to
566 * the equivalent quadrant on the Mac. (Sin and Cos values reveal which
567 * quadrant you are in).
569 static double wxConvertWXangleToMACangle(double angle
)
573 sin_a
= sin(angle
/ RAD2DEG
);
574 cos_a
= cos(angle
/ RAD2DEG
);
576 if( (sin_a
>= 0.0) && (cos_a
>= 0.0) ) {
577 angle
= acos(sin_a
) * RAD2DEG
;
579 else if( (sin_a
>= 0.0) && (cos_a
<= 0.0) ) {
581 angle
= acos(sin_a
) * RAD2DEG
+ 180;
583 else if( (sin_a
<= 0.0) && (cos_a
>= 0.0) ) {
584 angle
= acos(sin_a
) * RAD2DEG
+ 180;
586 else if( (sin_a
< 0.0) && (cos_a
< 0.0) ) {
588 angle
= acos(sin_a
) * RAD2DEG
+ 180;
593 void wxDC::DoDrawArc( wxCoord x1
, wxCoord y1
,
594 wxCoord x2
, wxCoord y2
,
595 wxCoord xc
, wxCoord yc
)
597 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawArc Invalid DC"));
599 wxCoord xx1
= XLOG2DEV(x1
);
600 wxCoord yy1
= YLOG2DEV(y1
);
601 wxCoord xx2
= XLOG2DEV(x2
);
602 wxCoord yy2
= YLOG2DEV(y2
);
603 wxCoord xxc
= XLOG2DEV(xc
);
604 wxCoord yyc
= YLOG2DEV(yc
);
605 double dx
= xx1
- xxc
;
606 double dy
= yy1
- yyc
;
607 double radius
= sqrt((double)(dx
*dx
+dy
*dy
));
608 wxCoord rad
= (wxCoord
)radius
;
609 double radius1
, radius2
;
611 if (xx1
== xx2
&& yy1
== yy2
)
616 else if (radius
== 0.0)
618 radius1
= radius2
= 0.0;
622 radius1
= (xx1
- xxc
== 0) ?
623 (yy1
- yyc
< 0) ? 90.0 : -90.0 :
624 -atan2(double(yy1
-yyc
), double(xx1
-xxc
)) * RAD2DEG
;
625 radius2
= (xx2
- xxc
== 0) ?
626 (yy2
- yyc
< 0) ? 90.0 : -90.0 :
627 -atan2(double(yy2
-yyc
), double(xx2
-xxc
)) * RAD2DEG
;
629 wxCoord alpha2
= wxCoord(radius2
- radius1
);
630 wxCoord alpha1
= wxCoord(wxConvertWXangleToMACangle(radius1
));
631 if( (xx1
> xx2
) || (yy1
> yy2
) ) {
635 Rect r
= { yyc
- rad
, xxc
- rad
, yyc
+ rad
, xxc
+ rad
};
637 if(m_brush
.GetStyle() != wxTRANSPARENT
) {
639 PaintArc(&r
, alpha1
, alpha2
);
641 if(m_pen
.GetStyle() != wxTRANSPARENT
) {
643 FrameArc(&r
, alpha1
, alpha2
);
647 void wxDC::DoDrawEllipticArc( wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
,
648 double sa
, double ea
)
650 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawEllepticArc Invalid DC"));
653 double angle
= sa
- ea
; // Order important Mac in opposite direction to wx
655 wxCoord xx
= XLOG2DEV(x
);
656 wxCoord yy
= YLOG2DEV(y
);
657 wxCoord ww
= m_signX
* XLOG2DEVREL(w
);
658 wxCoord hh
= m_signY
* YLOG2DEVREL(h
);
660 // handle -ve width and/or height
661 if (ww
< 0) { ww
= -ww
; xx
= xx
- ww
; }
662 if (hh
< 0) { hh
= -hh
; yy
= yy
- hh
; }
664 sa
= wxConvertWXangleToMACangle(sa
);
671 if(m_brush
.GetStyle() != wxTRANSPARENT
) {
673 PaintArc(&r
, (short)sa
, (short)angle
);
675 if(m_pen
.GetStyle() != wxTRANSPARENT
) {
677 FrameArc(&r
, (short)sa
, (short)angle
);
681 void wxDC::DoDrawPoint( wxCoord x
, wxCoord y
)
683 wxCHECK_RET(Ok(), wxT("Invalid DC"));
685 wxMacPortSetter
helper(this) ;
687 if (m_pen
.GetStyle() != wxTRANSPARENT
)
690 wxCoord xx1
= XLOG2DEV(x
);
691 wxCoord yy1
= YLOG2DEV(y
);
694 ::LineTo(xx1
+1, yy1
+1);
698 void wxDC::DoDrawLines(int n
, wxPoint points
[],
699 wxCoord xoffset
, wxCoord yoffset
)
701 wxCHECK_RET(Ok(), wxT("Invalid DC"));
702 wxMacPortSetter
helper(this) ;
704 if (m_pen
.GetStyle() == wxTRANSPARENT
)
709 wxCoord offset
= ( (m_pen
.GetWidth() == 0 ? 1 :
710 m_pen
.GetWidth() ) * (wxCoord
)m_scaleX
- 1) / 2 ;
712 wxCoord x1
, x2
, y1
, y2
;
713 x1
= XLOG2DEV(points
[0].x
+ xoffset
);
714 y1
= YLOG2DEV(points
[0].y
+ yoffset
);
715 ::MoveTo(x1
- offset
, y1
- offset
);
717 for (int i
= 0; i
< n
-1; i
++)
719 x2
= XLOG2DEV(points
[i
+1].x
+ xoffset
);
720 y2
= YLOG2DEV(points
[i
+1].y
+ yoffset
);
721 ::LineTo( x2
- offset
, y2
- offset
);
725 void wxDC::DoDrawPolygon(int n
, wxPoint points
[],
726 wxCoord xoffset
, wxCoord yoffset
,
729 wxCHECK_RET(Ok(), wxT("Invalid DC"));
730 wxMacPortSetter
helper(this) ;
732 PolyHandle polygon
= OpenPoly() ;
733 wxCoord x1
, x2
, y1
, y2
;
734 x1
= XLOG2DEV(points
[0].x
+ xoffset
);
735 y1
= YLOG2DEV(points
[0].y
+ yoffset
);
738 for (int i
= 0; i
< n
-1; i
++)
740 x2
= XLOG2DEV(points
[i
+1].x
+ xoffset
);
741 y2
= YLOG2DEV(points
[i
+1].y
+ yoffset
);
746 if (m_brush
.GetStyle() != wxTRANSPARENT
)
749 ::PaintPoly( polygon
) ;
752 if (m_pen
.GetStyle() != wxTRANSPARENT
)
755 ::FramePoly( polygon
) ;
757 KillPoly( polygon
) ;
760 void wxDC::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
762 wxCHECK_RET(Ok(), wxT("Invalid DC"));
763 wxMacPortSetter
helper(this) ;
765 wxCoord xx
= XLOG2DEV(x
);
766 wxCoord yy
= YLOG2DEV(y
);
767 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
768 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
770 // CMB: draw nothing if transformed w or h is 0
771 if (ww
== 0 || hh
== 0)
774 // CMB: handle -ve width and/or height
787 Rect rect
= { yy
, xx
, yy
+ hh
, xx
+ ww
} ;
789 if (m_brush
.GetStyle() != wxTRANSPARENT
)
792 ::PaintRect( &rect
) ;
795 if (m_pen
.GetStyle() != wxTRANSPARENT
)
798 ::FrameRect( &rect
) ;
802 void wxDC::DoDrawRoundedRectangle(wxCoord x
, wxCoord y
,
803 wxCoord width
, wxCoord height
,
806 wxCHECK_RET(Ok(), wxT("Invalid DC"));
807 wxMacPortSetter
helper(this) ;
810 radius
= - radius
* ((width
< height
) ? width
: height
);
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 ::PaintRoundRect( &rect
, int(radius
* 2) , int(radius
* 2) ) ;
842 if (m_pen
.GetStyle() != wxTRANSPARENT
)
845 ::FrameRoundRect( &rect
, int(radius
* 2) , int(radius
* 2) ) ;
849 void wxDC::DoDrawEllipse(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
851 wxCHECK_RET(Ok(), wxT("Invalid DC"));
852 wxMacPortSetter
helper(this) ;
854 wxCoord xx
= XLOG2DEV(x
);
855 wxCoord yy
= YLOG2DEV(y
);
856 wxCoord ww
= m_signX
* XLOG2DEVREL(width
);
857 wxCoord hh
= m_signY
* YLOG2DEVREL(height
);
859 // CMB: draw nothing if transformed w or h is 0
860 if (ww
== 0 || hh
== 0)
863 // CMB: handle -ve width and/or height
876 Rect rect
= { yy
, xx
, yy
+ hh
, xx
+ ww
} ;
878 if (m_brush
.GetStyle() != wxTRANSPARENT
)
881 ::PaintOval( &rect
) ;
884 if (m_pen
.GetStyle() != wxTRANSPARENT
)
887 ::FrameOval( &rect
) ;
893 bool wxDC::CanDrawBitmap(void) const
899 bool wxDC::DoBlit(wxCoord xdest
, wxCoord ydest
, wxCoord width
, wxCoord height
,
900 wxDC
*source
, wxCoord xsrc
, wxCoord ysrc
, int logical_func
, bool useMask
)
902 wxCHECK_MSG(Ok(), false, wxT("wxDC::DoBlit Illegal dc"));
903 wxCHECK_MSG(source
->Ok(), false, wxT("wxDC::DoBlit Illegal source DC"));
904 wxMacPortSetter
helper(this) ;
906 CGrafPtr sourcePort
= (CGrafPtr
) source
->m_macPort
;
907 PixMapHandle bmappixels
= GetGWorldPixMap( sourcePort
) ;
908 RGBColor white
= { 0xFFFF, 0xFFFF,0xFFFF} ;
909 RGBColor black
= { 0,0,0} ;
910 RGBColor forecolor
= m_textForegroundColour
.GetPixel();
911 RGBColor backcolor
= m_textBackgroundColour
.GetPixel();
912 RGBForeColor( &forecolor
) ;
913 RGBBackColor( &backcolor
) ;
915 if ( LockPixels(bmappixels
) )
917 Rect srcrect
, dstrect
;
918 srcrect
.top
= source
->YLOG2DEV(ysrc
) ;
919 srcrect
.left
= source
->XLOG2DEV(xsrc
) ;
920 srcrect
.right
= source
->XLOG2DEV(xsrc
+ width
) ;
921 srcrect
.bottom
= source
->YLOG2DEV(ysrc
+ height
) ;
922 dstrect
.top
= YLOG2DEV(ydest
) ;
923 dstrect
.left
= XLOG2DEV(xdest
) ;
924 dstrect
.bottom
= YLOG2DEV(ydest
+ height
) ;
925 dstrect
.right
= XLOG2DEV(xdest
+ width
) ;
927 short mode
= (logical_func
== wxCOPY
? srcCopy
:
928 // logical_func == wxCLEAR ? WHITENESS :
929 // logical_func == wxSET ? BLACKNESS :
930 logical_func
== wxINVERT
? hilite
:
931 // logical_func == wxAND ? MERGECOPY :
932 logical_func
== wxOR
? srcOr
:
933 logical_func
== wxSRC_INVERT
? notSrcCopy
:
934 logical_func
== wxXOR
? srcXor
:
935 // logical_func == wxOR_REVERSE ? MERGEPAINT :
936 // logical_func == wxAND_REVERSE ? SRCERASE :
937 // logical_func == wxSRC_OR ? srcOr :
938 // logical_func == wxSRC_AND ? SRCAND :
941 if ( useMask
&& source
->m_macMask
)
943 wxASSERT( mode
== srcCopy
) ;
944 if ( LockPixels( GetGWorldPixMap( source
->m_macMask
) ) )
946 CopyMask( GetPortBitMapForCopyBits( sourcePort
) ,
947 GetPortBitMapForCopyBits( source
->m_macMask
) ,
948 GetPortBitMapForCopyBits( m_macPort
) ,
949 &srcrect
, &srcrect
, &dstrect
) ;
950 UnlockPixels( GetGWorldPixMap( source
->m_macMask
) ) ;
955 CopyBits( GetPortBitMapForCopyBits( sourcePort
) ,
956 GetPortBitMapForCopyBits( m_macPort
) ,
957 &srcrect
, &dstrect
, mode
, NULL
) ;
959 UnlockPixels( bmappixels
) ;
962 m_macPenInstalled
= false ;
963 m_macBrushInstalled
= false ;
964 m_macFontInstalled
= false ;
969 void wxDC::DoDrawRotatedText(const wxString
& text
, wxCoord x
, wxCoord y
,
972 wxCHECK_RET( Ok(), wxT("wxDC::DoDrawRotatedText Invalid window dc") );
976 DrawText(text
, x
, y
);
982 // the size of the text
984 GetTextExtent(text
, &w
, &h
);
986 // draw the string normally
989 dc
.SelectObject(src
);
990 dc
.SetFont(GetFont());
991 dc
.SetBackground(*wxWHITE_BRUSH
);
992 dc
.SetBrush(*wxBLACK_BRUSH
);
994 dc
.DrawText(text
, 0, 0);
995 dc
.SetFont(wxNullFont
);
996 dc
.SelectObject(wxNullBitmap
);
998 wxMacPortSetter
helper(this) ;
1000 // Calculate the size of the rotated bounding box.
1001 double rad
= DegToRad(angle
);
1002 double dx
= cos(rad
);
1003 double dy
= sin(rad
);
1005 // the rectngle vertices are counted clockwise with the first one being at
1006 // (0, 0) (or, rather, at (x, y))
1008 double y2
= -w
* dy
; // y axis points to the bottom, hence minus
1011 double x3
= x4
+ x2
;
1012 double y3
= y4
+ y2
;
1015 wxCoord maxX
= (wxCoord
)(dmax(x2
, dmax(x3
, x4
)) + 0.5);
1016 wxCoord maxY
= (wxCoord
)(dmax(y2
, dmax(y3
, y4
)) + 0.5);
1017 wxCoord minX
= (wxCoord
)(dmin(x2
, dmin(x3
, x4
)) - 0.5);
1018 wxCoord minY
= (wxCoord
)(dmin(y2
, dmin(y3
, y4
)) - 0.5);
1020 // prepare to blit-with-rotate the bitmap to the DC
1023 RGBColor colText
= m_textForegroundColour
.GetPixel();
1024 RGBColor colBack
= m_textBackgroundColour
.GetPixel();
1026 unsigned char *data
= image
.GetData();
1029 double r
, angleOrig
;
1032 // paint pixel by pixel
1033 for ( wxCoord srcX
= 0; srcX
< w
; srcX
++ )
1035 for ( wxCoord srcY
= 0; srcY
< h
; srcY
++ )
1037 // transform source coords to dest coords
1038 r
= sqrt( (double)(srcX
* srcX
+ srcY
* srcY
) );
1039 angleOrig
= atan2((double)srcY
, (double)srcX
) - rad
;
1040 dstX
= (wxCoord
)(r
* cos(angleOrig
) + 0.5);
1041 dstY
= (wxCoord
)(r
* sin(angleOrig
) + 0.5);
1044 textPixel
= data
[(srcY
*w
+ srcX
)*3] == 0;
1045 if ( textPixel
|| (m_backgroundMode
== wxSOLID
) )
1047 SetCPixel(XLOG2DEV(x
+ dstX
), YLOG2DEV(y
+ dstY
),
1048 textPixel
? &colText
: &colBack
);
1053 // it would be better to draw with non underlined font and draw the line
1054 // manually here (it would be more straight...)
1056 if ( m_font
.GetUnderlined() )
1058 ::MoveTo(XLOG2DEV(x
+ x4
), YLOG2DEV(y
+ y4
+ font
->descent
));
1059 ::LineTo(XLOG2DEV(x
+ x3
), YLOG2DEV(y
+ y3
+ font
->descent
));
1063 // update the bounding box
1064 CalcBoundingBox(x
+ minX
, y
+ minY
);
1065 CalcBoundingBox(x
+ maxX
, y
+ maxY
);
1067 void wxDC::DoDrawText(const wxString
& strtext
, wxCoord x
, wxCoord y
)
1069 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawText Invalid DC"));
1070 wxMacPortSetter
helper(this) ;
1072 long xx
= XLOG2DEV(x
);
1073 long yy
= YLOG2DEV(y
);
1075 // if (m_pen.GetStyle() != wxTRANSPARENT)
1079 Rect clip = { -32000 , -32000 , 32000 , 32000 } ;
1081 ::ClipRect( &clip ) ;
1085 ::GetFontInfo( &fi
) ;
1088 ::MoveTo( xx
, yy
);
1089 if ( m_backgroundMode
== wxTRANSPARENT
)
1091 ::TextMode( srcOr
) ;
1095 ::TextMode( srcCopy
) ;
1098 const char *text
= NULL
;
1102 if ( wxApp::s_macDefaultEncodingIsPC
)
1104 macText
= wxMacMakeMacStringFromPC( strtext
) ;
1106 length
= macText
.Length() ;
1111 length
= strtext
.Length() ;
1120 if( text
[i
] == 13 || text
[i
] == 10)
1122 ::DrawText( text
, laststop
, i
- laststop
) ;
1124 ::MoveTo( xx
, yy
+ line
*(fi
.descent
+ fi
.ascent
+ fi
.leading
) );
1130 ::DrawText( text
, laststop
, i
- laststop
) ;
1131 ::TextMode( srcOr
) ;
1135 bool wxDC::CanGetTextExtent() const
1137 wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
1142 void wxDC::DoGetTextExtent( const wxString
&string
, wxCoord
*width
, wxCoord
*height
,
1143 wxCoord
*descent
, wxCoord
*externalLeading
,
1144 wxFont
*theFont
) const
1146 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1147 wxMacPortSetter
helper(this) ;
1149 wxFont formerFont
= m_font
;
1153 wxFontRefData
* font
= (wxFontRefData
*) m_font
.GetRefData() ;
1157 ::TextFont( font
->m_macFontNum
) ;
1158 ::TextSize( YLOG2DEVREL( font
->m_macFontSize
) ) ;
1159 ::TextFace( font
->m_macFontStyle
) ;
1168 ::GetFontInfo( &fi
) ;
1171 *height
= YDEV2LOGREL( fi
.descent
+ fi
.ascent
) ;
1173 *descent
=YDEV2LOGREL( fi
.descent
);
1174 if ( externalLeading
)
1175 *externalLeading
= YDEV2LOGREL( fi
.leading
) ;
1177 const char *text
= NULL
;
1180 if ( wxApp::s_macDefaultEncodingIsPC
)
1182 macText
= wxMacMakeMacStringFromPC( string
) ;
1184 length
= macText
.Length() ;
1189 length
= string
.Length() ;
1201 if( text
[i
] == 13 || text
[i
] == 10)
1204 *height
+= YDEV2LOGREL( fi
.descent
+ fi
.ascent
+ fi
.leading
) ;
1205 curwidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
1206 if ( curwidth
> *width
)
1207 *width
= XDEV2LOGREL( curwidth
) ;
1213 curwidth
= ::TextWidth( text
, laststop
, i
- laststop
) ;
1214 if ( curwidth
> *width
)
1215 *width
= XDEV2LOGREL( curwidth
) ;
1220 m_macFontInstalled
= false ;
1224 wxCoord
wxDC::GetCharWidth(void) const
1226 wxCHECK_MSG(Ok(), 1, wxT("Invalid DC"));
1228 wxMacPortSetter
helper(this) ;
1233 ::GetFontInfo( &fi
) ;
1235 return YDEV2LOGREL((fi
.descent
+ fi
.ascent
) / 2) ;
1238 wxCoord
wxDC::GetCharHeight(void) const
1240 wxCHECK_MSG(Ok(), 1, wxT("Invalid DC"));
1242 wxMacPortSetter
helper(this) ;
1247 ::GetFontInfo( &fi
) ;
1249 return YDEV2LOGREL( fi
.descent
+ fi
.ascent
);
1252 void wxDC::Clear(void)
1254 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1255 wxMacPortSetter
helper(this) ;
1256 Rect rect
= { -32767 , -32767 , 32767 , 32767 } ;
1258 if (m_backgroundBrush
.GetStyle() != wxTRANSPARENT
)
1261 ::EraseRect( &rect
) ;
1265 void wxDC::MacInstallFont() const
1267 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1268 // if ( m_macFontInstalled )
1270 Pattern blackColor
;
1272 wxFontRefData
* font
= (wxFontRefData
*) m_font
.GetRefData() ;
1276 ::TextFont( font
->m_macFontNum
) ;
1277 ::TextSize( short(m_scaleY
* font
->m_macFontSize
) ) ;
1278 ::TextFace( font
->m_macFontStyle
) ;
1280 m_macFontInstalled
= true ;
1281 m_macBrushInstalled
= false ;
1282 m_macPenInstalled
= false ;
1284 RGBColor forecolor
= m_textForegroundColour
.GetPixel();
1285 RGBColor backcolor
= m_textBackgroundColour
.GetPixel();
1286 ::RGBForeColor( &forecolor
);
1287 ::RGBBackColor( &backcolor
);
1293 GetFNum( "\pGeneva" , &fontnum
) ;
1294 ::TextFont( fontnum
) ;
1295 ::TextSize( short(m_scaleY
* 10) ) ;
1298 // todo reset after spacing changes - or store the current spacing somewhere
1300 m_macFontInstalled
= true ;
1301 m_macBrushInstalled
= false ;
1302 m_macPenInstalled
= false ;
1304 RGBColor forecolor
= m_textForegroundColour
.GetPixel();
1305 RGBColor backcolor
= m_textBackgroundColour
.GetPixel();
1306 ::RGBForeColor( &forecolor
);
1307 ::RGBBackColor( &backcolor
);
1310 short mode
= patCopy
;
1314 switch( m_logicalFunction
)
1319 case wxINVERT
: // NOT dst
1320 ::PenPat(GetQDGlobalsBlack(&blackColor
));
1323 case wxXOR
: // src XOR dst
1326 case wxOR_REVERSE
: // src OR (NOT dst)
1329 case wxSRC_INVERT
: // (NOT src)
1336 case wxAND_REVERSE
:// src AND (NOT dst)
1337 case wxAND
: // src AND dst
1338 case wxAND_INVERT
: // (NOT src) AND dst
1339 case wxNO_OP
: // dst
1340 case wxNOR
: // (NOT src) AND (NOT dst)
1341 case wxEQUIV
: // (NOT src) XOR dst
1342 case wxOR_INVERT
: // (NOT src) OR dst
1343 case wxNAND
: // (NOT src) OR (NOT dst)
1344 case wxOR
: // src OR dst
1346 // case wxSRC_OR: // source _bitmap_ OR destination
1347 // case wxSRC_AND: // source _bitmap_ AND destination
1353 static void wxMacGetHatchPattern(int hatchStyle
, Pattern
*pattern
)
1355 int thePatListID
= sysPatListID
;
1359 case wxBDIAGONAL_HATCH
:
1360 theIndex
= 34; // WCH: this is not good
1362 case wxFDIAGONAL_HATCH
:
1368 case wxHORIZONTAL_HATCH
:
1371 case wxVERTICAL_HATCH
:
1374 case wxCROSSDIAG_HATCH
:
1375 theIndex
= 4; // WCH: this is not good
1378 theIndex
= 1; // solid pattern
1381 GetIndPattern( pattern
, thePatListID
, theIndex
);
1384 void wxDC::MacInstallPen() const
1386 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1390 // if ( m_macPenInstalled )
1393 RGBColor forecolor
= m_pen
.GetColour().GetPixel();
1394 RGBColor backcolor
= m_backgroundBrush
.GetColour().GetPixel();
1395 ::RGBForeColor( &forecolor
);
1396 ::RGBBackColor( &backcolor
);
1399 int penWidth
= m_pen
.GetWidth() * (int) m_scaleX
;
1401 // null means only one pixel, at whatever resolution
1402 if ( penWidth
== 0 )
1404 ::PenSize(penWidth
, penWidth
);
1406 int penStyle
= m_pen
.GetStyle();
1408 if (penStyle
== wxSOLID
)
1410 ::PenPat(GetQDGlobalsBlack(&blackColor
));
1412 else if (IS_HATCH(penStyle
))
1415 wxMacGetHatchPattern(penStyle
, &pat
);
1420 ::PenPat(GetQDGlobalsBlack(&blackColor
));
1423 short mode
= patCopy
;
1427 switch( m_logicalFunction
)
1432 case wxINVERT
: // NOT dst
1433 ::PenPat(GetQDGlobalsBlack(&blackColor
));
1436 case wxXOR
: // src XOR dst
1439 case wxOR_REVERSE
: // src OR (NOT dst)
1442 case wxSRC_INVERT
: // (NOT src)
1449 case wxAND_REVERSE
:// src AND (NOT dst)
1450 case wxAND
: // src AND dst
1451 case wxAND_INVERT
: // (NOT src) AND dst
1452 case wxNO_OP
: // dst
1453 case wxNOR
: // (NOT src) AND (NOT dst)
1454 case wxEQUIV
: // (NOT src) XOR dst
1455 case wxOR_INVERT
: // (NOT src) OR dst
1456 case wxNAND
: // (NOT src) OR (NOT dst)
1457 case wxOR
: // src OR dst
1459 // case wxSRC_OR: // source _bitmap_ OR destination
1460 // case wxSRC_AND: // source _bitmap_ AND destination
1464 m_macPenInstalled
= true ;
1465 m_macBrushInstalled
= false ;
1466 m_macFontInstalled
= false ;
1469 void wxDC::MacInstallBrush() const
1471 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1473 Pattern blackColor
, whiteColor
;
1474 // if ( m_macBrushInstalled )
1479 RGBColor forecolor
= m_brush
.GetColour().GetPixel();
1480 RGBColor backcolor
= m_backgroundBrush
.GetColour().GetPixel();
1481 ::RGBForeColor( &forecolor
);
1482 ::RGBBackColor( &backcolor
);
1484 int brushStyle
= m_brush
.GetStyle();
1485 if (brushStyle
== wxSOLID
)
1486 ::PenPat(GetQDGlobalsBlack(&blackColor
));
1487 else if (IS_HATCH(brushStyle
))
1490 wxMacGetHatchPattern(brushStyle
, &pat
);
1495 ::PenPat(GetQDGlobalsBlack(&blackColor
));
1501 brushStyle
= m_backgroundBrush
.GetStyle();
1502 if (brushStyle
== wxSOLID
)
1503 ::BackPat(GetQDGlobalsWhite(&whiteColor
));
1504 else if (IS_HATCH(brushStyle
))
1507 wxMacGetHatchPattern(brushStyle
, &pat
);
1512 ::BackPat(GetQDGlobalsWhite(&whiteColor
));
1515 short mode
= patCopy
;
1519 switch( m_logicalFunction
)
1524 case wxINVERT
: // NOT dst
1525 ::PenPat(GetQDGlobalsBlack(&blackColor
));
1528 case wxXOR
: // src XOR dst
1531 case wxOR_REVERSE
: // src OR (NOT dst)
1534 case wxSRC_INVERT
: // (NOT src)
1541 case wxAND_REVERSE
:// src AND (NOT dst)
1542 case wxAND
: // src AND dst
1543 case wxAND_INVERT
: // (NOT src) AND dst
1544 case wxNO_OP
: // dst
1545 case wxNOR
: // (NOT src) AND (NOT dst)
1546 case wxEQUIV
: // (NOT src) XOR dst
1547 case wxOR_INVERT
: // (NOT src) OR dst
1548 case wxNAND
: // (NOT src) OR (NOT dst)
1549 case wxOR
: // src OR dst
1551 // case wxSRC_OR: // source _bitmap_ OR destination
1552 // case wxSRC_AND: // source _bitmap_ AND destination
1556 m_macBrushInstalled
= true ;
1557 m_macPenInstalled
= false ;
1558 m_macFontInstalled
= false ;
1561 // ---------------------------------------------------------------------------
1562 // coordinates transformations
1563 // ---------------------------------------------------------------------------
1566 wxCoord
wxDCBase::DeviceToLogicalX(wxCoord x
) const
1568 return ((wxDC
*)this)->XDEV2LOG(x
);
1571 wxCoord
wxDCBase::DeviceToLogicalY(wxCoord y
) const
1573 return ((wxDC
*)this)->YDEV2LOG(y
);
1576 wxCoord
wxDCBase::DeviceToLogicalXRel(wxCoord x
) const
1578 return ((wxDC
*)this)->XDEV2LOGREL(x
);
1581 wxCoord
wxDCBase::DeviceToLogicalYRel(wxCoord y
) const
1583 return ((wxDC
*)this)->YDEV2LOGREL(y
);
1586 wxCoord
wxDCBase::LogicalToDeviceX(wxCoord x
) const
1588 return ((wxDC
*)this)->XLOG2DEV(x
);
1591 wxCoord
wxDCBase::LogicalToDeviceY(wxCoord y
) const
1593 return ((wxDC
*)this)->YLOG2DEV(y
);
1596 wxCoord
wxDCBase::LogicalToDeviceXRel(wxCoord x
) const
1598 return ((wxDC
*)this)->XLOG2DEVREL(x
);
1601 wxCoord
wxDCBase::LogicalToDeviceYRel(wxCoord y
) const
1603 return ((wxDC
*)this)->YLOG2DEVREL(y
);