]> git.saurik.com Git - wxWidgets.git/blame - src/mac/dc.cpp
Removed TABs that had gotten in to the source
[wxWidgets.git] / src / mac / dc.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: dc.cpp
3// Purpose: wxDC class
4// Author: AUTHOR
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "dc.h"
14#endif
15
16#include "wx/dc.h"
03e11df5 17#include "wx/app.h"
2f1ae414 18#include "wx/mac/uma.h"
3dec57ad
SC
19#include "wx/dcmemory.h"
20#include "wx/region.h"
21#include "wx/image.h"
e9576ca5 22
5b781a67
SC
23#if __MSL__ >= 0x6000
24#include "math.h"
25#endif
26
2f1ae414 27#if !USE_SHARED_LIBRARY
e9576ca5 28IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
2f1ae414 29#endif
e9576ca5
SC
30
31//-----------------------------------------------------------------------------
32// constants
33//-----------------------------------------------------------------------------
34
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
2461b2e7 41#ifndef __UNIX__
3dec57ad 42const double M_PI = 3.14159265358979 ;
2461b2e7 43#endif
3dec57ad
SC
44const double RAD2DEG = 180.0 / M_PI;
45
46//-----------------------------------------------------------------------------
47// Local functions
48//-----------------------------------------------------------------------------
49
50static inline double dmin(double a, double b) { return a < b ? a : b; }
51static inline double dmax(double a, double b) { return a > b ? a : b; }
52static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
e9576ca5
SC
53
54//-----------------------------------------------------------------------------
55// wxDC
56//-----------------------------------------------------------------------------
57
2f1ae414 58wxDC::wxDC()
e9576ca5
SC
59{
60 m_ok = FALSE;
e9576ca5 61 m_colour = TRUE;
e9576ca5 62
2f1ae414
SC
63 m_mm_to_pix_x = mm2pt;
64 m_mm_to_pix_y = mm2pt;
e9576ca5 65
e9576ca5
SC
66 m_internalDeviceOriginX = 0;
67 m_internalDeviceOriginY = 0;
68 m_externalDeviceOriginX = 0;
69 m_externalDeviceOriginY = 0;
70
71 m_logicalScaleX = 1.0;
72 m_logicalScaleY = 1.0;
73 m_userScaleX = 1.0;
74 m_userScaleY = 1.0;
75 m_scaleX = 1.0;
76 m_scaleY = 1.0;
77
e9576ca5
SC
78 m_needComputeScaleX = FALSE;
79 m_needComputeScaleY = FALSE;
80
e9576ca5
SC
81 m_maxX = m_maxY = -100000;
82 m_minY = m_minY = 100000;
83
519cb848 84 m_macPort = NULL ;
8208e181 85 m_macMask = NULL ;
519cb848
SC
86 m_ok = FALSE ;
87
88 m_macFontInstalled = false ;
89 m_macBrushInstalled = false ;
90 m_macPenInstalled = false ;
91
519cb848
SC
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 ;
9ff647cf
SC
97
98 m_pen = *wxBLACK_PEN;
99 m_font = *wxNORMAL_FONT;
100 m_brush = *wxWHITE_BRUSH;
3dec57ad 101}
f87a708b 102wxMacPortSetter::wxMacPortSetter( const wxDC* dc ) :
0eaa1d68
SC
103 m_ph( dc->m_macPort )
104{
105 wxASSERT( dc->Ok() ) ;
106
107 dc->MacSetupPort(&m_ph) ;
108}
109
110wxMacPortSetter::~wxMacPortSetter()
111{
112}
113
e9576ca5
SC
114wxDC::~wxDC(void)
115{
3dec57ad 116}
0eaa1d68 117void wxDC::MacSetupPort(AGAPortHelper* help) const
519cb848 118{
b668a735 119// help->Setup( m_macPort ) ;
519cb848 120 ::SetOrigin(-m_macLocalOrigin.h, -m_macLocalOrigin.v);
a8cd4a3a 121
65608e3b
SC
122 if ( m_clipping )
123 {
3dec57ad 124 Rect clip = { m_clipY1 , m_clipX1 , m_clipY2 , m_clipX2 } ;
65608e3b
SC
125 ::SectRect( &clip , &m_macClipRect , &clip ) ;
126 ::ClipRect( &clip ) ;
127 }
128 else
129 {
130 ::ClipRect(&m_macClipRect);
131 }
132
519cb848
SC
133
134 m_macFontInstalled = false ;
135 m_macBrushInstalled = false ;
136 m_macPenInstalled = false ;
137}
138
2f1ae414 139void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask )
519cb848 140{
3dec57ad
SC
141 wxCHECK_RET( Ok(), wxT("invalid window dc") );
142
143 wxCHECK_RET( bmp.Ok(), wxT("invalid bitmap") );
144
145 wxMacPortSetter helper(this) ;
146
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);
153
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 :
167 srcCopy );
168
169 if ( bmp.GetBitmapType() == kMacBitmapTypePict ) {
170 Rect bitmaprect = { 0 , 0 , hh, ww };
171 ::OffsetRect( &bitmaprect, xx, yy ) ;
172 ::DrawPicture( bmp.GetPict(), &bitmaprect ) ;
519cb848 173 }
3dec57ad
SC
174 else if ( bmp.GetBitmapType() == kMacBitmapTypeGrafWorld )
175 {
176 GWorldPtr bmapworld = bmp.GetHBITMAP();
177 PixMapHandle bmappixels ;
178
179 // Set foreground and background colours (for bitmaps depth = 1)
180 if(bmp.GetDepth() == 1)
181 {
182 RGBColor fore = m_textForegroundColour.GetPixel();
183 RGBColor back = m_textBackgroundColour.GetPixel();
184 RGBForeColor(&fore);
185 RGBBackColor(&back);
186 }
187 else
188 {
189 RGBColor white = { 0xFFFF, 0xFFFF,0xFFFF} ;
190 RGBColor black = { 0,0,0} ;
191 RGBForeColor( &black ) ;
192 RGBBackColor( &white ) ;
193 }
194
195 bmappixels = GetGWorldPixMap( bmapworld ) ;
196
197 wxCHECK_RET(LockPixels(bmappixels),
198 wxT("DoDrawBitmap: Unable to lock pixels"));
199
200 Rect source = { 0, 0, h, w };
201 Rect dest = { yy, xx, yy + hh, xx + ww };
202
203 if ( useMask && bmp.GetMask() )
204 {
205 if( LockPixels(GetGWorldPixMap(bmp.GetMask()->GetMaskBitmap())))
206 {
207 CopyDeepMask
208 (
209 GetPortBitMapForCopyBits(bmapworld),
210 GetPortBitMapForCopyBits(bmp.GetMask()->GetMaskBitmap()),
211 GetPortBitMapForCopyBits( m_macPort ),
212 &source, &source, &dest, mode, NULL
213 );
214 UnlockPixels(GetGWorldPixMap(bmp.GetMask()->GetMaskBitmap()));
215 }
216 }
217 else {
218 CopyBits( GetPortBitMapForCopyBits( bmapworld ),
219 GetPortBitMapForCopyBits( m_macPort ),
220 &source, &dest, mode, NULL ) ;
221 }
222 UnlockPixels( bmappixels ) ;
223 }
224 else if ( bmp.GetBitmapType() == kMacBitmapTypeIcon )
225 {
226 Rect bitmaprect = { 0 , 0 , bmp.GetHeight(), bmp.GetWidth() } ;
227 OffsetRect( &bitmaprect, xx, yy ) ;
228 PlotCIconHandle( &bitmaprect , atNone , ttNone , bmp.GetHICON() ) ;
229 }
230 m_macPenInstalled = false ;
231 m_macBrushInstalled = false ;
232 m_macFontInstalled = false ;
233
519cb848
SC
234}
235
2f1ae414 236void wxDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
e9576ca5 237{
3dec57ad
SC
238 wxCHECK_RET(Ok(), wxT("Invalid dc wxDC::DoDrawIcon"));
239
240 wxCHECK_RET(icon.Ok(), wxT("Invalid icon wxDC::DoDrawIcon"));
241
242 DoDrawBitmap( icon , x , y ) ;
243}
2f1ae414 244void wxDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
e9576ca5 245{
3dec57ad
SC
246 wxCHECK_RET(Ok(), wxT("wxDC::DoSetClippingRegion Invalid DC"));
247 wxCoord xx, yy, ww, hh;
248
249 xx = XLOG2DEV(x);
250 yy = YLOG2DEV(y);
251 ww = XLOG2DEVREL(width);
252 hh = YLOG2DEVREL(height);
e9576ca5 253
3dec57ad
SC
254 if( m_clipping )
255 {
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));
260 }
261 else
262 {
263 m_clipping = TRUE;
264 m_clipX1 = xx;
265 m_clipY1 = yy;
266 m_clipX2 = xx + ww;
267 m_clipY2 = yy + hh;
268 }
269
270}
2f1ae414
SC
271void wxDC::DoSetClippingRegionAsRegion( const wxRegion &region )
272{
3dec57ad 273 wxCHECK_RET( Ok(), wxT("invalid window dc") ) ;
2f1ae414 274
0eaa1d68 275 wxMacPortSetter helper(this) ;
2f1ae414
SC
276 if (region.Empty())
277 {
278 DestroyClippingRegion();
279 return;
280 }
281
282 wxCoord xx, yy, ww, hh;
283 region.GetBox( xx, yy, ww, hh );
284 wxDC::DoSetClippingRegion( xx, yy, ww, hh );
ee41971c
UJ
285}
286
3dec57ad 287void wxDC::DestroyClippingRegion()
e9576ca5 288{
0eaa1d68 289 wxMacPortSetter helper(this) ;
e9576ca5 290 m_clipping = FALSE;
3dec57ad 291}
2f1ae414 292void wxDC::DoGetSize( int* width, int* height ) const
e9576ca5
SC
293{
294 *width = m_maxX-m_minX;
295 *height = m_maxY-m_minY;
3dec57ad 296}
2f1ae414 297void wxDC::DoGetSizeMM( int* width, int* height ) const
e9576ca5
SC
298{
299 int w = 0;
300 int h = 0;
301 GetSize( &w, &h );
302 *width = long( double(w) / (m_scaleX*m_mm_to_pix_x) );
303 *height = long( double(h) / (m_scaleY*m_mm_to_pix_y) );
3dec57ad 304}
e9576ca5
SC
305void wxDC::SetTextForeground( const wxColour &col )
306{
3dec57ad
SC
307 wxCHECK_RET(Ok(), wxT("Invalid DC"));
308 m_textForegroundColour = col;
309 m_macFontInstalled = false ;
310}
e9576ca5
SC
311void wxDC::SetTextBackground( const wxColour &col )
312{
3dec57ad
SC
313 wxCHECK_RET(Ok(), wxT("Invalid DC"));
314 m_textBackgroundColour = col;
315 m_macFontInstalled = false ;
316}
e9576ca5
SC
317void wxDC::SetMapMode( int mode )
318{
319 switch (mode)
320 {
e3065973 321 case wxMM_TWIPS:
e9576ca5
SC
322 SetLogicalScale( twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y );
323 break;
e3065973 324 case wxMM_POINTS:
e9576ca5
SC
325 SetLogicalScale( pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y );
326 break;
e3065973 327 case wxMM_METRIC:
e9576ca5
SC
328 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
329 break;
e3065973 330 case wxMM_LOMETRIC:
e9576ca5
SC
331 SetLogicalScale( m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0 );
332 break;
333 default:
e3065973 334 case wxMM_TEXT:
e9576ca5
SC
335 SetLogicalScale( 1.0, 1.0 );
336 break;
3dec57ad 337 }
e3065973 338 if (mode != wxMM_TEXT)
e9576ca5
SC
339 {
340 m_needComputeScaleX = TRUE;
341 m_needComputeScaleY = TRUE;
3dec57ad
SC
342 }
343}
e9576ca5
SC
344void wxDC::SetUserScale( double x, double y )
345{
346 // allow negative ? -> no
347 m_userScaleX = x;
348 m_userScaleY = y;
349 ComputeScaleAndOrigin();
3dec57ad 350}
e9576ca5
SC
351void wxDC::SetLogicalScale( double x, double y )
352{
353 // allow negative ?
354 m_logicalScaleX = x;
355 m_logicalScaleY = y;
356 ComputeScaleAndOrigin();
3dec57ad 357}
2f1ae414 358void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
e9576ca5
SC
359{
360 m_logicalOriginX = x * m_signX; // is this still correct ?
361 m_logicalOriginY = y * m_signY;
362 ComputeScaleAndOrigin();
3dec57ad 363}
2f1ae414 364void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
e9576ca5
SC
365{
366 m_externalDeviceOriginX = x;
367 m_externalDeviceOriginY = y;
368 ComputeScaleAndOrigin();
3dec57ad
SC
369}
370
371#if 0
e9576ca5
SC
372void wxDC::SetInternalDeviceOrigin( long x, long y )
373{
374 m_internalDeviceOriginX = x;
375 m_internalDeviceOriginY = y;
376 ComputeScaleAndOrigin();
3dec57ad 377}
e9576ca5
SC
378void wxDC::GetInternalDeviceOrigin( long *x, long *y )
379{
380 if (x) *x = m_internalDeviceOriginX;
381 if (y) *y = m_internalDeviceOriginY;
3dec57ad
SC
382}
383#endif
e9576ca5
SC
384void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
385{
386 m_signX = (xLeftRight ? 1 : -1);
387 m_signY = (yBottomUp ? -1 : 1);
388 ComputeScaleAndOrigin();
3dec57ad 389}/*
e9576ca5
SC
390
391void wxDC::CalcBoundingBox( long x, long y )
392{
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;
3dec57ad 397}*/
2f1ae414
SC
398wxSize wxDC::GetPPI() const
399{
400 return wxSize(72, 72);
401}
402
403int wxDC::GetDepth() const
404{
405 return wxDisplayDepth() ;
406}
e9576ca5 407
3dec57ad 408void wxDC::ComputeScaleAndOrigin()
e9576ca5
SC
409{
410 // CMB: copy scale to see if it changes
411 double origScaleX = m_scaleX;
412 double origScaleY = m_scaleY;
413
414 m_scaleX = m_logicalScaleX * m_userScaleX;
415 m_scaleY = m_logicalScaleY * m_userScaleY;
416
417 m_deviceOriginX = m_internalDeviceOriginX + m_externalDeviceOriginX;
418 m_deviceOriginY = m_internalDeviceOriginY + m_externalDeviceOriginY;
419
420 // CMB: if scale has changed call SetPen to recalulate the line width
421 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
422 {
5b781a67
SC
423 // this is a bit artificial, but we need to force wxDC to think
424 // the pen has changed
425 wxPen* pen = & GetPen();
426 wxPen tempPen;
427 m_pen = tempPen;
428 SetPen(* pen);
e9576ca5 429 }
3dec57ad 430}
519cb848
SC
431void wxDC::SetPalette( const wxPalette& palette )
432{
433}
434
435void wxDC::SetBackgroundMode( int mode )
436{
437 m_backgroundMode = mode ;
438}
439
440void wxDC::SetFont( const wxFont &font )
441{
3dec57ad
SC
442 wxCHECK_RET(Ok(), wxT("Invalid DC"));
443
444 m_font = font;
445 m_macFontInstalled = false ;
519cb848
SC
446}
447
448void wxDC::SetPen( const wxPen &pen )
449{
3dec57ad 450 wxCHECK_RET(Ok(), wxT("Invalid DC"));
519cb848
SC
451
452 if ( m_pen == pen )
453 return ;
454
455 m_pen = pen;
3dec57ad 456
519cb848
SC
457 m_macPenInstalled = false ;
458}
459
460void wxDC::SetBrush( const wxBrush &brush )
461{
3dec57ad 462 wxCHECK_RET(Ok(), wxT("Invalid DC"));
519cb848
SC
463
464 if (m_brush == brush)
465 return;
466
467 m_brush = brush;
468 m_macBrushInstalled = false ;
469}
470
471void wxDC::SetBackground( const wxBrush &brush )
472{
3dec57ad 473 wxCHECK_RET(Ok(), wxT("Invalid DC"));
519cb848
SC
474
475 if (m_backgroundBrush == brush)
476 return;
477
478 m_backgroundBrush = brush;
479
480 if (!m_backgroundBrush.Ok())
481 return;
482 m_macBrushInstalled = false ;
483}
484
485void wxDC::SetLogicalFunction( int function )
486{
487 if (m_logicalFunction == function)
488 return;
489
490 m_logicalFunction = function ;
491 m_macFontInstalled = false ;
492 m_macBrushInstalled = false ;
493 m_macPenInstalled = false ;
494}
495
2f1ae414
SC
496void wxDC::DoFloodFill( wxCoord x, wxCoord y, const wxColour& col,
497 int style )
519cb848
SC
498{
499}
500
2f1ae414 501bool wxDC::DoGetPixel( wxCoord x, wxCoord y, wxColour *col ) const
519cb848 502{
3dec57ad
SC
503 wxCHECK_MSG( Ok(), false, wxT("wxDC::DoGetPixel Invalid DC") );
504 wxMacPortSetter helper(this) ;
505
506 RGBColor colour;
507
508 GetCPixel( XLOG2DEV(x), YLOG2DEV(y), &colour );
509
510 // Convert from Mac colour to wx
511 col->Set( colour.red >> 8,
512 colour.green >> 8,
513 colour.blue >> 8);
514
515 return true ;
519cb848
SC
516}
517
2f1ae414 518void wxDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
519cb848 519{
3dec57ad 520 wxCHECK_RET(Ok(), wxT("Invalid DC"));
519cb848 521
0eaa1d68 522 wxMacPortSetter helper(this) ;
519cb848
SC
523
524 if (m_pen.GetStyle() != wxTRANSPARENT)
525 {
526 MacInstallPen() ;
3dec57ad
SC
527 wxCoord offset = ( (m_pen.GetWidth() == 0 ? 1 :
528 m_pen.GetWidth() ) * (wxCoord)m_scaleX - 1) / 2;
529
530 wxCoord xx1 = XLOG2DEV(x1);
531 wxCoord yy1 = YLOG2DEV(y1);
532 wxCoord xx2 = XLOG2DEV(x2);
533 wxCoord yy2 = YLOG2DEV(y2);
534
535 ::MoveTo(xx1 - offset, yy1 - offset);
536 ::LineTo(xx2 - offset, yy2 - offset);
537 }
519cb848
SC
538}
539
2f1ae414 540void wxDC::DoCrossHair( wxCoord x, wxCoord y )
519cb848 541{
3dec57ad
SC
542 wxCHECK_RET( Ok(), wxT("wxDC::DoCrossHair Invalid window dc") );
543
544 if (m_pen.GetStyle() != wxTRANSPARENT)
545 {
546 int w = 0;
547 int h = 0;
548 GetSize( &w, &h );
549 wxCoord xx = XLOG2DEV(x);
550 wxCoord yy = YLOG2DEV(y);
551
552 MacInstallPen();
553 ::MoveTo( 0, yy );
554 ::LineTo( XLOG2DEVREL(w), yy );
555 ::MoveTo( xx, 0 );
556 ::LineTo( xx, YLOG2DEVREL(h) );
557 }
558}
559
560/*
561 * To draw arcs properly the angles need to be converted from the WX style:
562 * Angles start on the +ve X axis and go anti-clockwise (As you would draw on
563 * a normal axis on paper).
564 * TO
565 * the Mac style:
566 * Angles start on the +ve y axis and go clockwise.
567 * To achive this I work out which quadrant the angle lies in then map this to
568 * the equivalent quadrant on the Mac. (Sin and Cos values reveal which
569 * quadrant you are in).
570 */
571static double wxConvertWXangleToMACangle(double angle)
572{
573 double sin_a, cos_a;
574
575 sin_a = sin(angle / RAD2DEG);
576 cos_a = cos(angle / RAD2DEG);
577
578 if( (sin_a >= 0.0) && (cos_a >= 0.0) ) {
579 angle = acos(sin_a) * RAD2DEG;
580 }
581 else if( (sin_a >= 0.0) && (cos_a <= 0.0) ) {
582 sin_a *= -1;
583 angle = acos(sin_a) * RAD2DEG + 180;
584 }
585 else if( (sin_a <= 0.0) && (cos_a >= 0.0) ) {
586 angle = acos(sin_a) * RAD2DEG + 180;
587 }
588 else if( (sin_a < 0.0) && (cos_a < 0.0) ) {
589 sin_a *= -1;
590 angle = acos(sin_a) * RAD2DEG + 180;
591 }
592 return angle;
519cb848
SC
593}
594
2f1ae414
SC
595void wxDC::DoDrawArc( wxCoord x1, wxCoord y1,
596 wxCoord x2, wxCoord y2,
597 wxCoord xc, wxCoord yc )
519cb848 598{
3dec57ad
SC
599 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawArc Invalid DC"));
600
601 wxCoord xx1 = XLOG2DEV(x1);
602 wxCoord yy1 = YLOG2DEV(y1);
603 wxCoord xx2 = XLOG2DEV(x2);
604 wxCoord yy2 = YLOG2DEV(y2);
605 wxCoord xxc = XLOG2DEV(xc);
606 wxCoord yyc = YLOG2DEV(yc);
607 double dx = xx1 - xxc;
608 double dy = yy1 - yyc;
609 double radius = sqrt((double)(dx*dx+dy*dy));
610 wxCoord rad = (wxCoord)radius;
611 double radius1, radius2;
612
613 if (xx1 == xx2 && yy1 == yy2)
614 {
615 radius1 = 0.0;
616 radius2 = 360.0;
617 }
618 else if (radius == 0.0)
619 {
620 radius1 = radius2 = 0.0;
621 }
622 else
623 {
624 radius1 = (xx1 - xxc == 0) ?
625 (yy1 - yyc < 0) ? 90.0 : -90.0 :
626 -atan2(double(yy1-yyc), double(xx1-xxc)) * RAD2DEG;
627 radius2 = (xx2 - xxc == 0) ?
628 (yy2 - yyc < 0) ? 90.0 : -90.0 :
629 -atan2(double(yy2-yyc), double(xx2-xxc)) * RAD2DEG;
630 }
631 wxCoord alpha2 = wxCoord(radius2 - radius1);
632 wxCoord alpha1 = wxCoord(wxConvertWXangleToMACangle(radius1));
633 if( (xx1 > xx2) || (yy1 > yy2) ) {
634 alpha2 *= -1;
635 }
636
637 Rect r = { yyc - rad, xxc - rad, yyc + rad, xxc + rad };
638
639 if(m_brush.GetStyle() != wxTRANSPARENT) {
640 MacInstallBrush();
641 PaintArc(&r, alpha1, alpha2);
642 }
643 if(m_pen.GetStyle() != wxTRANSPARENT) {
644 MacInstallPen();
645 FrameArc(&r, alpha1, alpha2);
646 }
519cb848
SC
647}
648
2f1ae414
SC
649void wxDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
650 double sa, double ea )
519cb848 651{
3dec57ad
SC
652 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawEllepticArc Invalid DC"));
653
654 Rect r;
655 double angle = sa - ea; // Order important Mac in opposite direction to wx
656
657 wxCoord xx = XLOG2DEV(x);
658 wxCoord yy = YLOG2DEV(y);
659 wxCoord ww = m_signX * XLOG2DEVREL(w);
660 wxCoord hh = m_signY * YLOG2DEVREL(h);
661
662 // handle -ve width and/or height
663 if (ww < 0) { ww = -ww; xx = xx - ww; }
664 if (hh < 0) { hh = -hh; yy = yy - hh; }
665
666 sa = wxConvertWXangleToMACangle(sa);
667
668 r.top = yy;
669 r.left = xx;
670 r.bottom = yy + hh;
671 r.right = xx + ww;
672
673 if(m_brush.GetStyle() != wxTRANSPARENT) {
674 MacInstallBrush();
675 PaintArc(&r, (short)sa, (short)angle);
676 }
677 if(m_pen.GetStyle() != wxTRANSPARENT) {
678 MacInstallPen();
679 FrameArc(&r, (short)sa, (short)angle);
680 }
519cb848
SC
681}
682
2f1ae414 683void wxDC::DoDrawPoint( wxCoord x, wxCoord y )
519cb848 684{
3dec57ad 685 wxCHECK_RET(Ok(), wxT("Invalid DC"));
519cb848 686
0eaa1d68 687 wxMacPortSetter helper(this) ;
519cb848
SC
688
689 if (m_pen.GetStyle() != wxTRANSPARENT)
690 {
691 MacInstallPen() ;
3dec57ad
SC
692 wxCoord xx1 = XLOG2DEV(x);
693 wxCoord yy1 = YLOG2DEV(y);
519cb848
SC
694
695 ::MoveTo(xx1,yy1);
696 ::LineTo(xx1+1, yy1+1);
3dec57ad 697 }
519cb848
SC
698}
699
2f1ae414
SC
700void wxDC::DoDrawLines(int n, wxPoint points[],
701 wxCoord xoffset, wxCoord yoffset)
519cb848 702{
3dec57ad 703 wxCHECK_RET(Ok(), wxT("Invalid DC"));
0eaa1d68 704 wxMacPortSetter helper(this) ;
519cb848
SC
705
706 if (m_pen.GetStyle() == wxTRANSPARENT)
707 return;
708
03e11df5 709 MacInstallPen() ;
519cb848 710
3dec57ad
SC
711 wxCoord offset = ( (m_pen.GetWidth() == 0 ? 1 :
712 m_pen.GetWidth() ) * (wxCoord)m_scaleX - 1) / 2 ;
713
714 wxCoord x1, x2 , y1 , y2 ;
519cb848 715 x1 = XLOG2DEV(points[0].x + xoffset);
03e11df5 716 y1 = YLOG2DEV(points[0].y + yoffset);
3dec57ad 717 ::MoveTo(x1 - offset, y1 - offset );
519cb848
SC
718
719 for (int i = 0; i < n-1; i++)
720 {
03e11df5
GD
721 x2 = XLOG2DEV(points[i+1].x + xoffset);
722 y2 = YLOG2DEV(points[i+1].y + yoffset);
3dec57ad 723 ::LineTo( x2 - offset, y2 - offset );
519cb848
SC
724 }
725}
726
2f1ae414
SC
727void wxDC::DoDrawPolygon(int n, wxPoint points[],
728 wxCoord xoffset, wxCoord yoffset,
729 int fillStyle )
519cb848 730{
3dec57ad 731 wxCHECK_RET(Ok(), wxT("Invalid DC"));
0eaa1d68 732 wxMacPortSetter helper(this) ;
519cb848
SC
733
734 PolyHandle polygon = OpenPoly() ;
3dec57ad 735 wxCoord x1, x2 , y1 , y2 ;
519cb848 736 x1 = XLOG2DEV(points[0].x + xoffset);
03e11df5
GD
737 y1 = YLOG2DEV(points[0].y + yoffset);
738 ::MoveTo(x1,y1);
519cb848
SC
739
740 for (int i = 0; i < n-1; i++)
741 {
03e11df5
GD
742 x2 = XLOG2DEV(points[i+1].x + xoffset);
743 y2 = YLOG2DEV(points[i+1].y + yoffset);
744 ::LineTo(x2, y2);
519cb848
SC
745 }
746
747 ClosePoly() ;
748 if (m_brush.GetStyle() != wxTRANSPARENT)
749 {
750 MacInstallBrush() ;
751 ::PaintPoly( polygon ) ;
3dec57ad 752 }
519cb848
SC
753
754 if (m_pen.GetStyle() != wxTRANSPARENT)
755 {
756 MacInstallPen() ;
757 ::FramePoly( polygon ) ;
3dec57ad 758 }
519cb848
SC
759 KillPoly( polygon ) ;
760}
761
3dec57ad 762void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
519cb848 763{
3dec57ad
SC
764 wxCHECK_RET(Ok(), wxT("Invalid DC"));
765 wxMacPortSetter helper(this) ;
766
767 wxCoord xx = XLOG2DEV(x);
768 wxCoord yy = YLOG2DEV(y);
769 wxCoord ww = m_signX * XLOG2DEVREL(width);
770 wxCoord hh = m_signY * YLOG2DEVREL(height);
519cb848
SC
771
772 // CMB: draw nothing if transformed w or h is 0
773 if (ww == 0 || hh == 0)
774 return;
775
776 // CMB: handle -ve width and/or height
777 if (ww < 0)
778 {
779 ww = -ww;
780 xx = xx - ww;
781 }
782
783 if (hh < 0)
784 {
785 hh = -hh;
786 yy = yy - hh;
787 }
788
789 Rect rect = { yy , xx , yy + hh , xx + ww } ;
790
791 if (m_brush.GetStyle() != wxTRANSPARENT)
792 {
793 MacInstallBrush() ;
794 ::PaintRect( &rect ) ;
3dec57ad 795 }
519cb848
SC
796
797 if (m_pen.GetStyle() != wxTRANSPARENT)
798 {
799 MacInstallPen() ;
800 ::FrameRect( &rect ) ;
3dec57ad 801 }
519cb848
SC
802}
803
2f1ae414
SC
804void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
805 wxCoord width, wxCoord height,
806 double radius)
519cb848 807{
3dec57ad
SC
808 wxCHECK_RET(Ok(), wxT("Invalid DC"));
809 wxMacPortSetter helper(this) ;
519cb848 810
3dec57ad
SC
811 if (radius < 0.0)
812 radius = - radius * ((width < height) ? width : height);
519cb848 813
3dec57ad
SC
814 wxCoord xx = XLOG2DEV(x);
815 wxCoord yy = YLOG2DEV(y);
816 wxCoord ww = m_signX * XLOG2DEVREL(width);
817 wxCoord hh = m_signY * YLOG2DEVREL(height);
519cb848
SC
818
819 // CMB: draw nothing if transformed w or h is 0
820 if (ww == 0 || hh == 0)
821 return;
822
823 // CMB: handle -ve width and/or height
824 if (ww < 0)
825 {
826 ww = -ww;
827 xx = xx - ww;
828 }
829
830 if (hh < 0)
831 {
832 hh = -hh;
833 yy = yy - hh;
834 }
835
836 Rect rect = { yy , xx , yy + hh , xx + ww } ;
837
838 if (m_brush.GetStyle() != wxTRANSPARENT)
839 {
840 MacInstallBrush() ;
03e11df5 841 ::PaintRoundRect( &rect , int(radius * 2) , int(radius * 2) ) ;
3dec57ad 842 }
519cb848
SC
843
844 if (m_pen.GetStyle() != wxTRANSPARENT)
845 {
846 MacInstallPen() ;
03e11df5 847 ::FrameRoundRect( &rect , int(radius * 2) , int(radius * 2) ) ;
3dec57ad 848 }
519cb848
SC
849}
850
2f1ae414 851void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
519cb848 852{
3dec57ad
SC
853 wxCHECK_RET(Ok(), wxT("Invalid DC"));
854 wxMacPortSetter helper(this) ;
519cb848 855
3dec57ad
SC
856 wxCoord xx = XLOG2DEV(x);
857 wxCoord yy = YLOG2DEV(y);
858 wxCoord ww = m_signX * XLOG2DEVREL(width);
859 wxCoord hh = m_signY * YLOG2DEVREL(height);
519cb848
SC
860
861 // CMB: draw nothing if transformed w or h is 0
862 if (ww == 0 || hh == 0)
863 return;
864
865 // CMB: handle -ve width and/or height
866 if (ww < 0)
867 {
868 ww = -ww;
869 xx = xx - ww;
870 }
871
872 if (hh < 0)
873 {
874 hh = -hh;
875 yy = yy - hh;
876 }
877
878 Rect rect = { yy , xx , yy + hh , xx + ww } ;
879
880 if (m_brush.GetStyle() != wxTRANSPARENT)
881 {
882 MacInstallBrush() ;
883 ::PaintOval( &rect ) ;
3dec57ad 884 }
519cb848
SC
885
886 if (m_pen.GetStyle() != wxTRANSPARENT)
887 {
888 MacInstallPen() ;
889 ::FrameOval( &rect ) ;
3dec57ad 890 }
519cb848
SC
891}
892
519cb848
SC
893
894
895bool wxDC::CanDrawBitmap(void) const
896{
897 return true ;
898}
899
900
2f1ae414
SC
901bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
902 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool useMask )
519cb848 903{
3dec57ad
SC
904 wxCHECK_MSG(Ok(), false, wxT("wxDC::DoBlit Illegal dc"));
905 wxCHECK_MSG(source->Ok(), false, wxT("wxDC::DoBlit Illegal source DC"));
906 wxMacPortSetter helper(this) ;
519cb848
SC
907
908 CGrafPtr sourcePort = (CGrafPtr) source->m_macPort ;
909 PixMapHandle bmappixels = GetGWorldPixMap( sourcePort ) ;
03e11df5
GD
910 RGBColor white = { 0xFFFF, 0xFFFF,0xFFFF} ;
911 RGBColor black = { 0,0,0} ;
912 RGBColor forecolor = m_textForegroundColour.GetPixel();
913 RGBColor backcolor = m_textBackgroundColour.GetPixel();
914 RGBForeColor( &forecolor ) ;
915 RGBBackColor( &backcolor ) ;
519cb848
SC
916
917 if ( LockPixels(bmappixels) )
918 {
919 Rect srcrect , dstrect ;
920 srcrect.top = source->YLOG2DEV(ysrc) ;
921 srcrect.left = source->XLOG2DEV(xsrc) ;
922 srcrect.right = source->XLOG2DEV(xsrc + width ) ;
923 srcrect.bottom = source->YLOG2DEV(ysrc + height) ;
924 dstrect.top = YLOG2DEV(ydest) ;
925 dstrect.left = XLOG2DEV(xdest) ;
926 dstrect.bottom = YLOG2DEV(ydest + height ) ;
927 dstrect.right = XLOG2DEV(xdest + width ) ;
8208e181
SC
928
929 short mode = (logical_func == wxCOPY ? srcCopy :
930 // logical_func == wxCLEAR ? WHITENESS :
931 // logical_func == wxSET ? BLACKNESS :
932 logical_func == wxINVERT ? hilite :
933 // logical_func == wxAND ? MERGECOPY :
934 logical_func == wxOR ? srcOr :
935 logical_func == wxSRC_INVERT ? notSrcCopy :
936 logical_func == wxXOR ? srcXor :
937 // logical_func == wxOR_REVERSE ? MERGEPAINT :
938 // logical_func == wxAND_REVERSE ? SRCERASE :
2f1ae414 939 // logical_func == wxSRC_OR ? srcOr :
8208e181
SC
940 // logical_func == wxSRC_AND ? SRCAND :
941 srcCopy );
942
943 if ( useMask && source->m_macMask )
944 {
945 wxASSERT( mode == srcCopy ) ;
946 if ( LockPixels( GetGWorldPixMap( source->m_macMask ) ) )
947 {
3dec57ad
SC
948 CopyMask( GetPortBitMapForCopyBits( sourcePort ) ,
949 GetPortBitMapForCopyBits( source->m_macMask ) ,
950 GetPortBitMapForCopyBits( m_macPort ) ,
951 &srcrect, &srcrect , &dstrect ) ;
8208e181
SC
952 UnlockPixels( GetGWorldPixMap( source->m_macMask ) ) ;
953 }
954 }
955 else
956 {
3dec57ad
SC
957 CopyBits( GetPortBitMapForCopyBits( sourcePort ) ,
958 GetPortBitMapForCopyBits( m_macPort ) ,
8208e181
SC
959 &srcrect, &dstrect, mode, NULL ) ;
960 }
519cb848
SC
961 UnlockPixels( bmappixels ) ;
962 }
963
964 m_macPenInstalled = false ;
965 m_macBrushInstalled = false ;
966 m_macFontInstalled = false ;
967
968 return TRUE;
969}
970
2f1ae414 971void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y,
3dec57ad 972 double angle)
2f1ae414 973{
3dec57ad
SC
974 wxCHECK_RET( Ok(), wxT("wxDC::DoDrawRotatedText Invalid window dc") );
975
976 if (angle == 0.0)
977 {
978 DrawText(text, x, y);
979 return;
980 }
981
982 MacInstallFont();
983
984 // the size of the text
985 wxCoord w, h;
986 GetTextExtent(text, &w, &h);
987
988 // draw the string normally
989 wxBitmap src(w, h);
990 wxMemoryDC dc;
991 dc.SelectObject(src);
992 dc.SetFont(GetFont());
993 dc.SetBackground(*wxWHITE_BRUSH);
994 dc.SetBrush(*wxBLACK_BRUSH);
995 dc.Clear();
996 dc.DrawText(text, 0, 0);
997 dc.SetFont(wxNullFont);
998 dc.SelectObject(wxNullBitmap);
999
1000 wxMacPortSetter helper(this) ;
1001
1002 // Calculate the size of the rotated bounding box.
1003 double rad = DegToRad(angle);
1004 double dx = cos(rad);
1005 double dy = sin(rad);
1006
1007 // the rectngle vertices are counted clockwise with the first one being at
1008 // (0, 0) (or, rather, at (x, y))
1009 double x2 = w * dx;
1010 double y2 = -w * dy; // y axis points to the bottom, hence minus
1011 double x4 = h * dy;
1012 double y4 = h * dx;
1013 double x3 = x4 + x2;
1014 double y3 = y4 + y2;
1015
1016 // calc max and min
1017 wxCoord maxX = (wxCoord)(dmax(x2, dmax(x3, x4)) + 0.5);
1018 wxCoord maxY = (wxCoord)(dmax(y2, dmax(y3, y4)) + 0.5);
1019 wxCoord minX = (wxCoord)(dmin(x2, dmin(x3, x4)) - 0.5);
1020 wxCoord minY = (wxCoord)(dmin(y2, dmin(y3, y4)) - 0.5);
1021
1022 // prepare to blit-with-rotate the bitmap to the DC
1023 wxImage image(src);
1024
1025 RGBColor colText = m_textForegroundColour.GetPixel();
1026 RGBColor colBack = m_textBackgroundColour.GetPixel();
1027
1028 unsigned char *data = image.GetData();
1029
1030 wxCoord dstX, dstY;
1031 double r, angleOrig;
1032 bool textPixel;
1033
1034 // paint pixel by pixel
1035 for ( wxCoord srcX = 0; srcX < w; srcX++ )
1036 {
1037 for ( wxCoord srcY = 0; srcY < h; srcY++ )
1038 {
1039 // transform source coords to dest coords
1040 r = sqrt( (double)(srcX * srcX + srcY * srcY) );
1041 angleOrig = atan2((double)srcY, (double)srcX) - rad;
1042 dstX = (wxCoord)(r * cos(angleOrig) + 0.5);
1043 dstY = (wxCoord)(r * sin(angleOrig) + 0.5);
1044
1045 // black pixel?
1046 textPixel = data[(srcY*w + srcX)*3] == 0;
1047 if ( textPixel || (m_backgroundMode == wxSOLID) )
1048 {
1049 SetCPixel(XLOG2DEV(x + dstX), YLOG2DEV(y + dstY),
1050 textPixel ? &colText : &colBack);
1051 }
1052 }
1053 }
1054
1055 // it would be better to draw with non underlined font and draw the line
1056 // manually here (it would be more straight...)
1057#if 0
1058 if ( m_font.GetUnderlined() )
1059 {
1060 ::MoveTo(XLOG2DEV(x + x4), YLOG2DEV(y + y4 + font->descent));
1061 ::LineTo(XLOG2DEV(x + x3), YLOG2DEV(y + y3 + font->descent));
1062 }
1063#endif // 0
1064
1065 // update the bounding box
1066 CalcBoundingBox(x + minX, y + minY);
1067 CalcBoundingBox(x + maxX, y + maxY);
2f1ae414
SC
1068}
1069void wxDC::DoDrawText(const wxString& strtext, wxCoord x, wxCoord y)
519cb848 1070{
3dec57ad
SC
1071 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawText Invalid DC"));
1072 wxMacPortSetter helper(this) ;
519cb848
SC
1073
1074 long xx = XLOG2DEV(x);
1075 long yy = YLOG2DEV(y);
1076
1077// if (m_pen.GetStyle() != wxTRANSPARENT)
1078 {
1079 MacInstallFont() ;
1080 /*
1081 Rect clip = { -32000 , -32000 , 32000 , 32000 } ;
1082
1083 ::ClipRect( &clip ) ;
1084 */
1085
1086 FontInfo fi ;
1087 ::GetFontInfo( &fi ) ;
1088
1089 yy += fi.ascent ;
1090 ::MoveTo( xx , yy );
1091 if ( m_backgroundMode == wxTRANSPARENT )
1092 {
1093 ::TextMode( srcOr) ;
1094 }
1095 else
1096 {
1097 ::TextMode( srcCopy ) ;
1098 }
1099
1100 const char *text = NULL ;
1101 int length = 0 ;
1102 wxString macText ;
1103
1104 if ( wxApp::s_macDefaultEncodingIsPC )
1105 {
2f1ae414 1106 macText = wxMacMakeMacStringFromPC( strtext ) ;
519cb848
SC
1107 text = macText ;
1108 length = macText.Length() ;
1109 }
1110 else
1111 {
2f1ae414
SC
1112 text = strtext ;
1113 length = strtext.Length() ;
519cb848
SC
1114 }
1115
1116 int laststop = 0 ;
1117 int i = 0 ;
1118 int line = 0 ;
1119
1120 while( i < length )
1121 {
1122 if( text[i] == 13 || text[i] == 10)
1123 {
1124 ::DrawText( text , laststop , i - laststop ) ;
1125 line++ ;
1126 ::MoveTo( xx , yy + line*(fi.descent + fi.ascent + fi.leading) );
1127 laststop = i+1 ;
1128 }
1129 i++ ;
1130 }
1131
1132 ::DrawText( text , laststop , i - laststop ) ;
1133 ::TextMode( srcOr ) ;
1134 }
1135}
1136
3dec57ad 1137bool wxDC::CanGetTextExtent() const
519cb848 1138{
3dec57ad 1139 wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
519cb848
SC
1140
1141 return true ;
1142}
1143
2f1ae414
SC
1144void wxDC::DoGetTextExtent( const wxString &string, wxCoord *width, wxCoord *height,
1145 wxCoord *descent, wxCoord *externalLeading ,
1146 wxFont *theFont ) const
519cb848 1147{
3dec57ad 1148 wxCHECK_RET(Ok(), wxT("Invalid DC"));
0eaa1d68 1149 wxMacPortSetter helper(this) ;
519cb848
SC
1150
1151 wxFont formerFont = m_font ;
1152
1153 if ( theFont )
1154 {
1155 wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ;
1156
1157 if ( font )
1158 {
519cb848 1159 ::TextFont( font->m_macFontNum ) ;
2f1ae414 1160 ::TextSize( YLOG2DEVREL( font->m_macFontSize) ) ;
519cb848
SC
1161 ::TextFace( font->m_macFontStyle ) ;
1162 }
1163 }
1164 else
1165 {
1166 MacInstallFont() ;
1167 }
1168
1169 FontInfo fi ;
1170 ::GetFontInfo( &fi ) ;
1171
2f1ae414
SC
1172 if ( height )
1173 *height = YDEV2LOGREL( fi.descent + fi.ascent ) ;
1174 if ( descent )
1175 *descent =YDEV2LOGREL( fi.descent );
1176 if ( externalLeading )
1177 *externalLeading = YDEV2LOGREL( fi.leading ) ;
519cb848
SC
1178
1179 const char *text = NULL ;
1180 int length = 0 ;
1181 wxString macText ;
1182 if ( wxApp::s_macDefaultEncodingIsPC )
1183 {
1184 macText = wxMacMakeMacStringFromPC( string ) ;
1185 text = macText ;
1186 length = macText.Length() ;
1187 }
1188 else
1189 {
1190 text = string ;
1191 length = string.Length() ;
1192 }
1193
1194 int laststop = 0 ;
1195 int i = 0 ;
1196 int curwidth = 0 ;
2f1ae414 1197 if ( width )
519cb848 1198 {
2f1ae414
SC
1199 *width = 0 ;
1200
1201 while( i < length )
519cb848 1202 {
2f1ae414
SC
1203 if( text[i] == 13 || text[i] == 10)
1204 {
1205 if ( height )
1206 *height += YDEV2LOGREL( fi.descent + fi.ascent + fi.leading ) ;
1207 curwidth = ::TextWidth( text , laststop , i - laststop ) ;
1208 if ( curwidth > *width )
1209 *width = XDEV2LOGREL( curwidth ) ;
1210 laststop = i+1 ;
1211 }
1212 i++ ;
519cb848 1213 }
2f1ae414
SC
1214
1215 curwidth = ::TextWidth( text , laststop , i - laststop ) ;
1216 if ( curwidth > *width )
1217 *width = XDEV2LOGREL( curwidth ) ;
519cb848 1218 }
519cb848
SC
1219
1220 if ( theFont )
1221 {
1222 m_macFontInstalled = false ;
1223 }
1224}
1225
ee41971c 1226wxCoord wxDC::GetCharWidth(void) const
519cb848 1227{
3dec57ad 1228 wxCHECK_MSG(Ok(), 1, wxT("Invalid DC"));
519cb848 1229
0eaa1d68 1230 wxMacPortSetter helper(this) ;
519cb848
SC
1231
1232 MacInstallFont() ;
1233
1234 FontInfo fi ;
1235 ::GetFontInfo( &fi ) ;
1236
2f1ae414 1237 return YDEV2LOGREL((fi.descent + fi.ascent) / 2) ;
519cb848
SC
1238}
1239
ee41971c 1240wxCoord wxDC::GetCharHeight(void) const
519cb848 1241{
3dec57ad 1242 wxCHECK_MSG(Ok(), 1, wxT("Invalid DC"));
519cb848 1243
3dec57ad 1244 wxMacPortSetter helper(this) ;
519cb848
SC
1245
1246 MacInstallFont() ;
1247
1248 FontInfo fi ;
1249 ::GetFontInfo( &fi ) ;
1250
2f1ae414 1251 return YDEV2LOGREL( fi.descent + fi.ascent );
519cb848
SC
1252}
1253
1254void wxDC::Clear(void)
1255{
3dec57ad
SC
1256 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1257 wxMacPortSetter helper(this) ;
519cb848
SC
1258 Rect rect = { -32767 , -32767 , 32767 , 32767 } ;
1259
1260 if (m_backgroundBrush.GetStyle() != wxTRANSPARENT)
1261 {
1262 MacInstallBrush() ;
1263 ::EraseRect( &rect ) ;
3dec57ad 1264 }
519cb848
SC
1265}
1266
1267void wxDC::MacInstallFont() const
1268{
3dec57ad 1269 wxCHECK_RET(Ok(), wxT("Invalid DC"));
0eaa1d68
SC
1270// if ( m_macFontInstalled )
1271// return ;
2f1ae414
SC
1272 Pattern blackColor ;
1273
519cb848
SC
1274 wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ;
1275
1276 if ( font )
1277 {
1278 ::TextFont( font->m_macFontNum ) ;
03e11df5 1279 ::TextSize( short(m_scaleY * font->m_macFontSize) ) ;
519cb848
SC
1280 ::TextFace( font->m_macFontStyle ) ;
1281
1282 m_macFontInstalled = true ;
1283 m_macBrushInstalled = false ;
1284 m_macPenInstalled = false ;
03e11df5
GD
1285
1286 RGBColor forecolor = m_textForegroundColour.GetPixel();
1287 RGBColor backcolor = m_textBackgroundColour.GetPixel();
1288 ::RGBForeColor( &forecolor );
1289 ::RGBBackColor( &backcolor );
519cb848
SC
1290 }
1291 else
1292 {
1293 short fontnum ;
1294
1295 GetFNum( "\pGeneva" , &fontnum ) ;
1296 ::TextFont( fontnum ) ;
03e11df5 1297 ::TextSize( short(m_scaleY * 10) ) ;
519cb848
SC
1298 ::TextFace( 0 ) ;
1299
1300 // todo reset after spacing changes - or store the current spacing somewhere
1301
1302 m_macFontInstalled = true ;
1303 m_macBrushInstalled = false ;
1304 m_macPenInstalled = false ;
519cb848 1305
03e11df5
GD
1306 RGBColor forecolor = m_textForegroundColour.GetPixel();
1307 RGBColor backcolor = m_textBackgroundColour.GetPixel();
1308 ::RGBForeColor( &forecolor );
1309 ::RGBBackColor( &backcolor );
1310 }
519cb848
SC
1311
1312 short mode = patCopy ;
1313
1314 // todo :
1315
1316 switch( m_logicalFunction )
1317 {
1318 case wxCOPY: // src
1319 mode = patCopy ;
1320 break ;
1321 case wxINVERT: // NOT dst
2f1ae414 1322 ::PenPat(GetQDGlobalsBlack(&blackColor));
519cb848
SC
1323 mode = patXor ;
1324 break ;
1325 case wxXOR: // src XOR dst
1326 mode = patXor ;
1327 break ;
1328 case wxOR_REVERSE: // src OR (NOT dst)
1329 mode = notPatOr ;
1330 break ;
1331 case wxSRC_INVERT: // (NOT src)
1332 mode = notPatCopy ;
1333 break ;
1334
1335 // unsupported TODO
1336
1337 case wxCLEAR: // 0
1338 case wxAND_REVERSE:// src AND (NOT dst)
1339 case wxAND: // src AND dst
1340 case wxAND_INVERT: // (NOT src) AND dst
1341 case wxNO_OP: // dst
1342 case wxNOR: // (NOT src) AND (NOT dst)
1343 case wxEQUIV: // (NOT src) XOR dst
1344 case wxOR_INVERT: // (NOT src) OR dst
1345 case wxNAND: // (NOT src) OR (NOT dst)
1346 case wxOR: // src OR dst
1347 case wxSET: // 1
2f1ae414
SC
1348// case wxSRC_OR: // source _bitmap_ OR destination
1349// case wxSRC_AND: // source _bitmap_ AND destination
519cb848
SC
1350 break ;
1351 }
1352 ::PenMode( mode ) ;
1353}
1354
1355static void wxMacGetHatchPattern(int hatchStyle, Pattern *pattern)
1356{
1357 int thePatListID = sysPatListID;
1358 int theIndex;
1359 switch(hatchStyle)
1360 {
1361 case wxBDIAGONAL_HATCH:
1362 theIndex = 34; // WCH: this is not good
1363 break;
1364 case wxFDIAGONAL_HATCH:
1365 theIndex = 26;
1366 break;
1367 case wxCROSS_HATCH:
1368 theIndex = 5;
1369 break;
1370 case wxHORIZONTAL_HATCH:
1371 theIndex = 25;
1372 break;
1373 case wxVERTICAL_HATCH:
1374 theIndex = 6;
1375 break;
1376 case wxCROSSDIAG_HATCH:
1377 theIndex = 4; // WCH: this is not good
1378 break;
1379 default:
1380 theIndex = 1; // solid pattern
1381 break;
1382 }
1383 GetIndPattern( pattern, thePatListID, theIndex);
1384}
1385
1386void wxDC::MacInstallPen() const
1387{
3dec57ad 1388 wxCHECK_RET(Ok(), wxT("Invalid DC"));
519cb848 1389
2f1ae414
SC
1390 Pattern blackColor;
1391
0eaa1d68
SC
1392// if ( m_macPenInstalled )
1393// return ;
519cb848 1394
03e11df5
GD
1395 RGBColor forecolor = m_pen.GetColour().GetPixel();
1396 RGBColor backcolor = m_backgroundBrush.GetColour().GetPixel();
1397 ::RGBForeColor( &forecolor );
1398 ::RGBBackColor( &backcolor );
519cb848
SC
1399
1400 ::PenNormal() ;
3dec57ad 1401 int penWidth = m_pen.GetWidth() * (int) m_scaleX ;
0b014ec7
SC
1402
1403 // null means only one pixel, at whatever resolution
1404 if ( penWidth == 0 )
1405 penWidth = 1 ;
519cb848
SC
1406 ::PenSize(penWidth, penWidth);
1407
1408 int penStyle = m_pen.GetStyle();
1409
1410 if (penStyle == wxSOLID)
03e11df5 1411 {
2f1ae414 1412 ::PenPat(GetQDGlobalsBlack(&blackColor));
03e11df5 1413 }
519cb848
SC
1414 else if (IS_HATCH(penStyle))
1415 {
1416 Pattern pat ;
1417 wxMacGetHatchPattern(penStyle, &pat);
1418 ::PenPat(&pat);
1419 }
1420 else
1421 {
2f1ae414 1422 ::PenPat(GetQDGlobalsBlack(&blackColor));
519cb848
SC
1423 }
1424
1425 short mode = patCopy ;
1426
1427 // todo :
1428
1429 switch( m_logicalFunction )
1430 {
1431 case wxCOPY: // src
1432 mode = patCopy ;
1433 break ;
1434 case wxINVERT: // NOT dst
2f1ae414 1435 ::PenPat(GetQDGlobalsBlack(&blackColor));
519cb848
SC
1436 mode = patXor ;
1437 break ;
1438 case wxXOR: // src XOR dst
1439 mode = patXor ;
1440 break ;
1441 case wxOR_REVERSE: // src OR (NOT dst)
1442 mode = notPatOr ;
1443 break ;
1444 case wxSRC_INVERT: // (NOT src)
1445 mode = notPatCopy ;
1446 break ;
1447
1448 // unsupported TODO
1449
1450 case wxCLEAR: // 0
1451 case wxAND_REVERSE:// src AND (NOT dst)
1452 case wxAND: // src AND dst
1453 case wxAND_INVERT: // (NOT src) AND dst
1454 case wxNO_OP: // dst
1455 case wxNOR: // (NOT src) AND (NOT dst)
1456 case wxEQUIV: // (NOT src) XOR dst
1457 case wxOR_INVERT: // (NOT src) OR dst
1458 case wxNAND: // (NOT src) OR (NOT dst)
1459 case wxOR: // src OR dst
1460 case wxSET: // 1
2f1ae414
SC
1461// case wxSRC_OR: // source _bitmap_ OR destination
1462// case wxSRC_AND: // source _bitmap_ AND destination
519cb848
SC
1463 break ;
1464 }
1465 ::PenMode( mode ) ;
1466 m_macPenInstalled = true ;
1467 m_macBrushInstalled = false ;
1468 m_macFontInstalled = false ;
1469}
1470
1471void wxDC::MacInstallBrush() const
1472{
3dec57ad 1473 wxCHECK_RET(Ok(), wxT("Invalid DC"));
0eaa1d68 1474
2f1ae414 1475 Pattern blackColor, whiteColor ;
0eaa1d68
SC
1476// if ( m_macBrushInstalled )
1477// return ;
519cb848
SC
1478
1479 // foreground
1480
03e11df5
GD
1481 RGBColor forecolor = m_brush.GetColour().GetPixel();
1482 RGBColor backcolor = m_backgroundBrush.GetColour().GetPixel();
1483 ::RGBForeColor( &forecolor );
1484 ::RGBBackColor( &backcolor );
519cb848
SC
1485
1486 int brushStyle = m_brush.GetStyle();
1487 if (brushStyle == wxSOLID)
2f1ae414 1488 ::PenPat(GetQDGlobalsBlack(&blackColor));
519cb848
SC
1489 else if (IS_HATCH(brushStyle))
1490 {
1491 Pattern pat ;
1492 wxMacGetHatchPattern(brushStyle, &pat);
1493 ::PenPat(&pat);
1494 }
1495 else
1496 {
2f1ae414 1497 ::PenPat(GetQDGlobalsBlack(&blackColor));
519cb848
SC
1498 }
1499
1500
1501 // background
1502
1503 brushStyle = m_backgroundBrush.GetStyle();
1504 if (brushStyle == wxSOLID)
2f1ae414 1505 ::BackPat(GetQDGlobalsWhite(&whiteColor));
519cb848
SC
1506 else if (IS_HATCH(brushStyle))
1507 {
1508 Pattern pat ;
1509 wxMacGetHatchPattern(brushStyle, &pat);
1510 ::BackPat(&pat);
1511 }
1512 else
1513 {
2f1ae414 1514 ::BackPat(GetQDGlobalsWhite(&whiteColor));
519cb848
SC
1515 }
1516
1517 short mode = patCopy ;
1518
1519 // todo :
1520
1521 switch( m_logicalFunction )
1522 {
1523 case wxCOPY: // src
1524 mode = patCopy ;
1525 break ;
1526 case wxINVERT: // NOT dst
2f1ae414 1527 ::PenPat(GetQDGlobalsBlack(&blackColor));
519cb848
SC
1528 mode = patXor ;
1529 break ;
1530 case wxXOR: // src XOR dst
1531 mode = patXor ;
1532 break ;
1533 case wxOR_REVERSE: // src OR (NOT dst)
1534 mode = notPatOr ;
1535 break ;
1536 case wxSRC_INVERT: // (NOT src)
1537 mode = notPatCopy ;
1538 break ;
1539
1540 // unsupported TODO
1541
1542 case wxCLEAR: // 0
1543 case wxAND_REVERSE:// src AND (NOT dst)
1544 case wxAND: // src AND dst
1545 case wxAND_INVERT: // (NOT src) AND dst
1546 case wxNO_OP: // dst
1547 case wxNOR: // (NOT src) AND (NOT dst)
1548 case wxEQUIV: // (NOT src) XOR dst
1549 case wxOR_INVERT: // (NOT src) OR dst
1550 case wxNAND: // (NOT src) OR (NOT dst)
1551 case wxOR: // src OR dst
1552 case wxSET: // 1
2f1ae414
SC
1553// case wxSRC_OR: // source _bitmap_ OR destination
1554// case wxSRC_AND: // source _bitmap_ AND destination
519cb848
SC
1555 break ;
1556 }
1557 ::PenMode( mode ) ;
1558 m_macBrushInstalled = true ;
1559 m_macPenInstalled = false ;
1560 m_macFontInstalled = false ;
1561}
1562
2f1ae414
SC
1563// ---------------------------------------------------------------------------
1564// coordinates transformations
1565// ---------------------------------------------------------------------------
519cb848 1566
2f1ae414
SC
1567
1568wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
1569{
1570 return ((wxDC *)this)->XDEV2LOG(x);
1571}
1572
1573wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
1574{
1575 return ((wxDC *)this)->YDEV2LOG(y);
1576}
1577
1578wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
1579{
1580 return ((wxDC *)this)->XDEV2LOGREL(x);
1581}
1582
1583wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
1584{
1585 return ((wxDC *)this)->YDEV2LOGREL(y);
1586}
1587
1588wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
1589{
1590 return ((wxDC *)this)->XLOG2DEV(x);
1591}
1592
1593wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
1594{
1595 return ((wxDC *)this)->YLOG2DEV(y);
1596}
1597
1598wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
1599{
1600 return ((wxDC *)this)->XLOG2DEVREL(x);
1601}
1602
1603wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
1604{
1605 return ((wxDC *)this)->YLOG2DEVREL(y);
1606}