]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dc.cpp
Menu files for OS/2 builds
[wxWidgets.git] / src / mac / carbon / 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
a2f94f40 242 DoDrawBitmap( icon , x , y , icon.GetMask() != NULL ) ;
3dec57ad 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
2301e281
RR
530 wxCoord xx1 = XLOG2DEV(x1) - offset;
531 wxCoord yy1 = YLOG2DEV(y1) - offset;
532 wxCoord xx2 = XLOG2DEV(x2) - offset;
533 wxCoord yy2 = YLOG2DEV(y2) - offset;
534
535 if ((m_pen.GetCap() == wxCAP_ROUND) &&
536 (m_pen.GetWidth() <= 1))
537 {
538 // Implement LAST_NOT for MAC at least for
539 // orthogonal lines. RR.
540 if (xx1 == xx2)
541 {
542 if (yy1 < yy2)
543 yy2--;
544 if (yy1 > yy2)
545 yy2++;
546 }
547 if (yy1 == yy2)
548 {
549 if (xx1 < xx2)
550 xx2--;
551 if (xx1 > xx2)
552 xx2++;
553 }
554 }
555
556 ::MoveTo(xx1, yy1);
557 ::LineTo(xx2, yy2);
3dec57ad 558 }
519cb848
SC
559}
560
2f1ae414 561void wxDC::DoCrossHair( wxCoord x, wxCoord y )
519cb848 562{
3dec57ad
SC
563 wxCHECK_RET( Ok(), wxT("wxDC::DoCrossHair Invalid window dc") );
564
565 if (m_pen.GetStyle() != wxTRANSPARENT)
566 {
567 int w = 0;
568 int h = 0;
569 GetSize( &w, &h );
570 wxCoord xx = XLOG2DEV(x);
571 wxCoord yy = YLOG2DEV(y);
572
573 MacInstallPen();
574 ::MoveTo( 0, yy );
575 ::LineTo( XLOG2DEVREL(w), yy );
576 ::MoveTo( xx, 0 );
577 ::LineTo( xx, YLOG2DEVREL(h) );
578 }
579}
580
581/*
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).
585 * TO
586 * the Mac style:
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).
591 */
592static double wxConvertWXangleToMACangle(double angle)
593{
594 double sin_a, cos_a;
595
596 sin_a = sin(angle / RAD2DEG);
597 cos_a = cos(angle / RAD2DEG);
598
599 if( (sin_a >= 0.0) && (cos_a >= 0.0) ) {
600 angle = acos(sin_a) * RAD2DEG;
601 }
602 else if( (sin_a >= 0.0) && (cos_a <= 0.0) ) {
603 sin_a *= -1;
604 angle = acos(sin_a) * RAD2DEG + 180;
605 }
606 else if( (sin_a <= 0.0) && (cos_a >= 0.0) ) {
607 angle = acos(sin_a) * RAD2DEG + 180;
608 }
609 else if( (sin_a < 0.0) && (cos_a < 0.0) ) {
610 sin_a *= -1;
611 angle = acos(sin_a) * RAD2DEG + 180;
612 }
613 return angle;
519cb848
SC
614}
615
2f1ae414
SC
616void wxDC::DoDrawArc( wxCoord x1, wxCoord y1,
617 wxCoord x2, wxCoord y2,
618 wxCoord xc, wxCoord yc )
519cb848 619{
3dec57ad
SC
620 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawArc Invalid DC"));
621
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;
633
634 if (xx1 == xx2 && yy1 == yy2)
635 {
636 radius1 = 0.0;
637 radius2 = 360.0;
638 }
639 else if (radius == 0.0)
640 {
641 radius1 = radius2 = 0.0;
642 }
643 else
644 {
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;
651 }
652 wxCoord alpha2 = wxCoord(radius2 - radius1);
653 wxCoord alpha1 = wxCoord(wxConvertWXangleToMACangle(radius1));
654 if( (xx1 > xx2) || (yy1 > yy2) ) {
655 alpha2 *= -1;
656 }
657
658 Rect r = { yyc - rad, xxc - rad, yyc + rad, xxc + rad };
659
660 if(m_brush.GetStyle() != wxTRANSPARENT) {
661 MacInstallBrush();
662 PaintArc(&r, alpha1, alpha2);
663 }
664 if(m_pen.GetStyle() != wxTRANSPARENT) {
665 MacInstallPen();
666 FrameArc(&r, alpha1, alpha2);
667 }
519cb848
SC
668}
669
2f1ae414
SC
670void wxDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
671 double sa, double ea )
519cb848 672{
3dec57ad
SC
673 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawEllepticArc Invalid DC"));
674
675 Rect r;
676 double angle = sa - ea; // Order important Mac in opposite direction to wx
677
678 wxCoord xx = XLOG2DEV(x);
679 wxCoord yy = YLOG2DEV(y);
680 wxCoord ww = m_signX * XLOG2DEVREL(w);
681 wxCoord hh = m_signY * YLOG2DEVREL(h);
682
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; }
686
687 sa = wxConvertWXangleToMACangle(sa);
688
689 r.top = yy;
690 r.left = xx;
691 r.bottom = yy + hh;
692 r.right = xx + ww;
693
694 if(m_brush.GetStyle() != wxTRANSPARENT) {
695 MacInstallBrush();
696 PaintArc(&r, (short)sa, (short)angle);
697 }
698 if(m_pen.GetStyle() != wxTRANSPARENT) {
699 MacInstallPen();
700 FrameArc(&r, (short)sa, (short)angle);
701 }
519cb848
SC
702}
703
2f1ae414 704void wxDC::DoDrawPoint( wxCoord x, wxCoord y )
519cb848 705{
3dec57ad 706 wxCHECK_RET(Ok(), wxT("Invalid DC"));
519cb848 707
0eaa1d68 708 wxMacPortSetter helper(this) ;
519cb848
SC
709
710 if (m_pen.GetStyle() != wxTRANSPARENT)
711 {
712 MacInstallPen() ;
3dec57ad
SC
713 wxCoord xx1 = XLOG2DEV(x);
714 wxCoord yy1 = YLOG2DEV(y);
519cb848
SC
715
716 ::MoveTo(xx1,yy1);
717 ::LineTo(xx1+1, yy1+1);
3dec57ad 718 }
519cb848
SC
719}
720
2f1ae414
SC
721void wxDC::DoDrawLines(int n, wxPoint points[],
722 wxCoord xoffset, wxCoord yoffset)
519cb848 723{
3dec57ad 724 wxCHECK_RET(Ok(), wxT("Invalid DC"));
0eaa1d68 725 wxMacPortSetter helper(this) ;
519cb848
SC
726
727 if (m_pen.GetStyle() == wxTRANSPARENT)
728 return;
729
03e11df5 730 MacInstallPen() ;
519cb848 731
3dec57ad
SC
732 wxCoord offset = ( (m_pen.GetWidth() == 0 ? 1 :
733 m_pen.GetWidth() ) * (wxCoord)m_scaleX - 1) / 2 ;
734
735 wxCoord x1, x2 , y1 , y2 ;
519cb848 736 x1 = XLOG2DEV(points[0].x + xoffset);
03e11df5 737 y1 = YLOG2DEV(points[0].y + yoffset);
3dec57ad 738 ::MoveTo(x1 - offset, y1 - offset );
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);
3dec57ad 744 ::LineTo( x2 - offset, y2 - offset );
519cb848
SC
745 }
746}
747
2f1ae414
SC
748void wxDC::DoDrawPolygon(int n, wxPoint points[],
749 wxCoord xoffset, wxCoord yoffset,
750 int fillStyle )
519cb848 751{
32ea1f98
RR
752 wxCHECK_RET(Ok(), wxT("Invalid DC"));
753 wxMacPortSetter helper(this) ;
754
755 wxCoord x1, x2 , y1 , y2 ;
519cb848 756
32ea1f98
RR
757 if (m_brush.GetStyle() != wxTRANSPARENT)
758 {
759 PolyHandle polygon = OpenPoly();
760
761 x1 = XLOG2DEV(points[0].x + xoffset);
762 y1 = YLOG2DEV(points[0].y + yoffset);
763 ::MoveTo(x1,y1);
519cb848 764
32ea1f98
RR
765 for (int i = 0; i < n-1; i++)
766 {
767 x2 = XLOG2DEV(points[i+1].x + xoffset);
768 y2 = YLOG2DEV(points[i+1].y + yoffset);
769 ::LineTo(x2, y2);
770 }
519cb848 771
32ea1f98
RR
772 ClosePoly();
773
774 MacInstallBrush();
775 ::PaintPoly( polygon );
776
777 KillPoly( polygon );
3dec57ad 778 }
519cb848
SC
779
780 if (m_pen.GetStyle() != wxTRANSPARENT)
781 {
32ea1f98
RR
782 PolyHandle polygon = OpenPoly();
783
784 x1 = XLOG2DEV(points[0].x + xoffset);
785 y1 = YLOG2DEV(points[0].y + yoffset);
786 ::MoveTo(x1,y1);
787
788 for (int i = 0; i < n-1; i++)
789 {
790 x2 = XLOG2DEV(points[i+1].x + xoffset);
791 y2 = YLOG2DEV(points[i+1].y + yoffset);
792 ::LineTo(x2, y2);
793 }
794
795 // return to origin to close path
796 ::LineTo(x1,y1);
797
798 ClosePoly();
799
519cb848
SC
800 MacInstallPen() ;
801 ::FramePoly( polygon ) ;
32ea1f98
RR
802
803 KillPoly( polygon );
3dec57ad 804 }
519cb848
SC
805}
806
3dec57ad 807void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
519cb848 808{
3dec57ad
SC
809 wxCHECK_RET(Ok(), wxT("Invalid DC"));
810 wxMacPortSetter helper(this) ;
811
812 wxCoord xx = XLOG2DEV(x);
813 wxCoord yy = YLOG2DEV(y);
814 wxCoord ww = m_signX * XLOG2DEVREL(width);
815 wxCoord hh = m_signY * YLOG2DEVREL(height);
519cb848
SC
816
817 // CMB: draw nothing if transformed w or h is 0
818 if (ww == 0 || hh == 0)
819 return;
820
821 // CMB: handle -ve width and/or height
822 if (ww < 0)
823 {
824 ww = -ww;
825 xx = xx - ww;
826 }
827
828 if (hh < 0)
829 {
830 hh = -hh;
831 yy = yy - hh;
832 }
833
834 Rect rect = { yy , xx , yy + hh , xx + ww } ;
835
836 if (m_brush.GetStyle() != wxTRANSPARENT)
837 {
838 MacInstallBrush() ;
839 ::PaintRect( &rect ) ;
3dec57ad 840 }
519cb848
SC
841
842 if (m_pen.GetStyle() != wxTRANSPARENT)
843 {
844 MacInstallPen() ;
845 ::FrameRect( &rect ) ;
3dec57ad 846 }
519cb848
SC
847}
848
2f1ae414
SC
849void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
850 wxCoord width, wxCoord height,
851 double radius)
519cb848 852{
3dec57ad
SC
853 wxCHECK_RET(Ok(), wxT("Invalid DC"));
854 wxMacPortSetter helper(this) ;
519cb848 855
3dec57ad
SC
856 if (radius < 0.0)
857 radius = - radius * ((width < height) ? width : height);
519cb848 858
3dec57ad
SC
859 wxCoord xx = XLOG2DEV(x);
860 wxCoord yy = YLOG2DEV(y);
861 wxCoord ww = m_signX * XLOG2DEVREL(width);
862 wxCoord hh = m_signY * YLOG2DEVREL(height);
519cb848
SC
863
864 // CMB: draw nothing if transformed w or h is 0
865 if (ww == 0 || hh == 0)
866 return;
867
868 // CMB: handle -ve width and/or height
869 if (ww < 0)
870 {
871 ww = -ww;
872 xx = xx - ww;
873 }
874
875 if (hh < 0)
876 {
877 hh = -hh;
878 yy = yy - hh;
879 }
880
881 Rect rect = { yy , xx , yy + hh , xx + ww } ;
882
883 if (m_brush.GetStyle() != wxTRANSPARENT)
884 {
885 MacInstallBrush() ;
03e11df5 886 ::PaintRoundRect( &rect , int(radius * 2) , int(radius * 2) ) ;
3dec57ad 887 }
519cb848
SC
888
889 if (m_pen.GetStyle() != wxTRANSPARENT)
890 {
891 MacInstallPen() ;
03e11df5 892 ::FrameRoundRect( &rect , int(radius * 2) , int(radius * 2) ) ;
3dec57ad 893 }
519cb848
SC
894}
895
2f1ae414 896void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
519cb848 897{
3dec57ad
SC
898 wxCHECK_RET(Ok(), wxT("Invalid DC"));
899 wxMacPortSetter helper(this) ;
519cb848 900
3dec57ad
SC
901 wxCoord xx = XLOG2DEV(x);
902 wxCoord yy = YLOG2DEV(y);
903 wxCoord ww = m_signX * XLOG2DEVREL(width);
904 wxCoord hh = m_signY * YLOG2DEVREL(height);
519cb848
SC
905
906 // CMB: draw nothing if transformed w or h is 0
907 if (ww == 0 || hh == 0)
908 return;
909
910 // CMB: handle -ve width and/or height
911 if (ww < 0)
912 {
913 ww = -ww;
914 xx = xx - ww;
915 }
916
917 if (hh < 0)
918 {
919 hh = -hh;
920 yy = yy - hh;
921 }
922
923 Rect rect = { yy , xx , yy + hh , xx + ww } ;
924
925 if (m_brush.GetStyle() != wxTRANSPARENT)
926 {
927 MacInstallBrush() ;
928 ::PaintOval( &rect ) ;
3dec57ad 929 }
519cb848
SC
930
931 if (m_pen.GetStyle() != wxTRANSPARENT)
932 {
933 MacInstallPen() ;
934 ::FrameOval( &rect ) ;
3dec57ad 935 }
519cb848
SC
936}
937
519cb848
SC
938
939
940bool wxDC::CanDrawBitmap(void) const
941{
942 return true ;
943}
944
945
2f1ae414
SC
946bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
947 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool useMask )
519cb848 948{
3dec57ad
SC
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) ;
519cb848
SC
952
953 CGrafPtr sourcePort = (CGrafPtr) source->m_macPort ;
954 PixMapHandle bmappixels = GetGWorldPixMap( sourcePort ) ;
03e11df5
GD
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 ) ;
519cb848
SC
961
962 if ( LockPixels(bmappixels) )
963 {
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 ) ;
8208e181
SC
973
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 :
2f1ae414 984 // logical_func == wxSRC_OR ? srcOr :
8208e181
SC
985 // logical_func == wxSRC_AND ? SRCAND :
986 srcCopy );
987
988 if ( useMask && source->m_macMask )
989 {
990 wxASSERT( mode == srcCopy ) ;
991 if ( LockPixels( GetGWorldPixMap( source->m_macMask ) ) )
992 {
3dec57ad
SC
993 CopyMask( GetPortBitMapForCopyBits( sourcePort ) ,
994 GetPortBitMapForCopyBits( source->m_macMask ) ,
995 GetPortBitMapForCopyBits( m_macPort ) ,
996 &srcrect, &srcrect , &dstrect ) ;
8208e181
SC
997 UnlockPixels( GetGWorldPixMap( source->m_macMask ) ) ;
998 }
999 }
1000 else
1001 {
3dec57ad
SC
1002 CopyBits( GetPortBitMapForCopyBits( sourcePort ) ,
1003 GetPortBitMapForCopyBits( m_macPort ) ,
8208e181
SC
1004 &srcrect, &dstrect, mode, NULL ) ;
1005 }
519cb848
SC
1006 UnlockPixels( bmappixels ) ;
1007 }
1008
1009 m_macPenInstalled = false ;
1010 m_macBrushInstalled = false ;
1011 m_macFontInstalled = false ;
1012
1013 return TRUE;
1014}
1015
2f1ae414 1016void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y,
3dec57ad 1017 double angle)
2f1ae414 1018{
3dec57ad
SC
1019 wxCHECK_RET( Ok(), wxT("wxDC::DoDrawRotatedText Invalid window dc") );
1020
1021 if (angle == 0.0)
1022 {
1023 DrawText(text, x, y);
1024 return;
1025 }
1026
1027 MacInstallFont();
1028
1029 // the size of the text
1030 wxCoord w, h;
1031 GetTextExtent(text, &w, &h);
1032
1033 // draw the string normally
1034 wxBitmap src(w, h);
1035 wxMemoryDC dc;
1036 dc.SelectObject(src);
1037 dc.SetFont(GetFont());
1038 dc.SetBackground(*wxWHITE_BRUSH);
1039 dc.SetBrush(*wxBLACK_BRUSH);
1040 dc.Clear();
1041 dc.DrawText(text, 0, 0);
1042 dc.SetFont(wxNullFont);
1043 dc.SelectObject(wxNullBitmap);
1044
1045 wxMacPortSetter helper(this) ;
1046
1047 // Calculate the size of the rotated bounding box.
1048 double rad = DegToRad(angle);
1049 double dx = cos(rad);
1050 double dy = sin(rad);
1051
1052 // the rectngle vertices are counted clockwise with the first one being at
1053 // (0, 0) (or, rather, at (x, y))
1054 double x2 = w * dx;
1055 double y2 = -w * dy; // y axis points to the bottom, hence minus
1056 double x4 = h * dy;
1057 double y4 = h * dx;
1058 double x3 = x4 + x2;
1059 double y3 = y4 + y2;
1060
1061 // calc max and min
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);
1066
1067 // prepare to blit-with-rotate the bitmap to the DC
1068 wxImage image(src);
1069
1070 RGBColor colText = m_textForegroundColour.GetPixel();
1071 RGBColor colBack = m_textBackgroundColour.GetPixel();
1072
1073 unsigned char *data = image.GetData();
1074
1075 wxCoord dstX, dstY;
1076 double r, angleOrig;
1077 bool textPixel;
1078
1079 // paint pixel by pixel
1080 for ( wxCoord srcX = 0; srcX < w; srcX++ )
1081 {
1082 for ( wxCoord srcY = 0; srcY < h; srcY++ )
1083 {
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);
1089
1090 // black pixel?
1091 textPixel = data[(srcY*w + srcX)*3] == 0;
1092 if ( textPixel || (m_backgroundMode == wxSOLID) )
1093 {
1094 SetCPixel(XLOG2DEV(x + dstX), YLOG2DEV(y + dstY),
1095 textPixel ? &colText : &colBack);
1096 }
1097 }
1098 }
1099
1100 // it would be better to draw with non underlined font and draw the line
1101 // manually here (it would be more straight...)
1102#if 0
1103 if ( m_font.GetUnderlined() )
1104 {
1105 ::MoveTo(XLOG2DEV(x + x4), YLOG2DEV(y + y4 + font->descent));
1106 ::LineTo(XLOG2DEV(x + x3), YLOG2DEV(y + y3 + font->descent));
1107 }
1108#endif // 0
1109
1110 // update the bounding box
1111 CalcBoundingBox(x + minX, y + minY);
1112 CalcBoundingBox(x + maxX, y + maxY);
2f1ae414
SC
1113}
1114void wxDC::DoDrawText(const wxString& strtext, wxCoord x, wxCoord y)
519cb848 1115{
3dec57ad
SC
1116 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawText Invalid DC"));
1117 wxMacPortSetter helper(this) ;
519cb848
SC
1118
1119 long xx = XLOG2DEV(x);
1120 long yy = YLOG2DEV(y);
1121
1122// if (m_pen.GetStyle() != wxTRANSPARENT)
1123 {
1124 MacInstallFont() ;
1125 /*
1126 Rect clip = { -32000 , -32000 , 32000 , 32000 } ;
1127
1128 ::ClipRect( &clip ) ;
1129 */
1130
1131 FontInfo fi ;
1132 ::GetFontInfo( &fi ) ;
1133
1134 yy += fi.ascent ;
1135 ::MoveTo( xx , yy );
1136 if ( m_backgroundMode == wxTRANSPARENT )
1137 {
1138 ::TextMode( srcOr) ;
1139 }
1140 else
1141 {
1142 ::TextMode( srcCopy ) ;
1143 }
1144
1145 const char *text = NULL ;
1146 int length = 0 ;
1147 wxString macText ;
1148
1149 if ( wxApp::s_macDefaultEncodingIsPC )
1150 {
2f1ae414 1151 macText = wxMacMakeMacStringFromPC( strtext ) ;
519cb848
SC
1152 text = macText ;
1153 length = macText.Length() ;
1154 }
1155 else
1156 {
2f1ae414
SC
1157 text = strtext ;
1158 length = strtext.Length() ;
519cb848
SC
1159 }
1160
1161 int laststop = 0 ;
1162 int i = 0 ;
1163 int line = 0 ;
1164
1165 while( i < length )
1166 {
1167 if( text[i] == 13 || text[i] == 10)
1168 {
1169 ::DrawText( text , laststop , i - laststop ) ;
1170 line++ ;
1171 ::MoveTo( xx , yy + line*(fi.descent + fi.ascent + fi.leading) );
1172 laststop = i+1 ;
1173 }
1174 i++ ;
1175 }
1176
1177 ::DrawText( text , laststop , i - laststop ) ;
1178 ::TextMode( srcOr ) ;
1179 }
1180}
1181
3dec57ad 1182bool wxDC::CanGetTextExtent() const
519cb848 1183{
3dec57ad 1184 wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
519cb848
SC
1185
1186 return true ;
1187}
1188
2f1ae414
SC
1189void wxDC::DoGetTextExtent( const wxString &string, wxCoord *width, wxCoord *height,
1190 wxCoord *descent, wxCoord *externalLeading ,
1191 wxFont *theFont ) const
519cb848 1192{
3dec57ad 1193 wxCHECK_RET(Ok(), wxT("Invalid DC"));
0eaa1d68 1194 wxMacPortSetter helper(this) ;
519cb848
SC
1195
1196 wxFont formerFont = m_font ;
1197
1198 if ( theFont )
1199 {
1200 wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ;
1201
1202 if ( font )
1203 {
519cb848 1204 ::TextFont( font->m_macFontNum ) ;
2f1ae414 1205 ::TextSize( YLOG2DEVREL( font->m_macFontSize) ) ;
519cb848
SC
1206 ::TextFace( font->m_macFontStyle ) ;
1207 }
1208 }
1209 else
1210 {
1211 MacInstallFont() ;
1212 }
1213
1214 FontInfo fi ;
1215 ::GetFontInfo( &fi ) ;
1216
2f1ae414
SC
1217 if ( height )
1218 *height = YDEV2LOGREL( fi.descent + fi.ascent ) ;
1219 if ( descent )
1220 *descent =YDEV2LOGREL( fi.descent );
1221 if ( externalLeading )
1222 *externalLeading = YDEV2LOGREL( fi.leading ) ;
519cb848
SC
1223
1224 const char *text = NULL ;
1225 int length = 0 ;
1226 wxString macText ;
1227 if ( wxApp::s_macDefaultEncodingIsPC )
1228 {
1229 macText = wxMacMakeMacStringFromPC( string ) ;
1230 text = macText ;
1231 length = macText.Length() ;
1232 }
1233 else
1234 {
1235 text = string ;
1236 length = string.Length() ;
1237 }
1238
1239 int laststop = 0 ;
1240 int i = 0 ;
1241 int curwidth = 0 ;
2f1ae414 1242 if ( width )
519cb848 1243 {
2f1ae414
SC
1244 *width = 0 ;
1245
1246 while( i < length )
519cb848 1247 {
2f1ae414
SC
1248 if( text[i] == 13 || text[i] == 10)
1249 {
1250 if ( height )
1251 *height += YDEV2LOGREL( fi.descent + fi.ascent + fi.leading ) ;
1252 curwidth = ::TextWidth( text , laststop , i - laststop ) ;
1253 if ( curwidth > *width )
1254 *width = XDEV2LOGREL( curwidth ) ;
1255 laststop = i+1 ;
1256 }
1257 i++ ;
519cb848 1258 }
2f1ae414
SC
1259
1260 curwidth = ::TextWidth( text , laststop , i - laststop ) ;
1261 if ( curwidth > *width )
1262 *width = XDEV2LOGREL( curwidth ) ;
519cb848 1263 }
519cb848
SC
1264
1265 if ( theFont )
1266 {
1267 m_macFontInstalled = false ;
1268 }
1269}
1270
ee41971c 1271wxCoord wxDC::GetCharWidth(void) const
519cb848 1272{
3dec57ad 1273 wxCHECK_MSG(Ok(), 1, wxT("Invalid DC"));
519cb848 1274
0eaa1d68 1275 wxMacPortSetter helper(this) ;
519cb848
SC
1276
1277 MacInstallFont() ;
1278
1279 FontInfo fi ;
1280 ::GetFontInfo( &fi ) ;
1281
2f1ae414 1282 return YDEV2LOGREL((fi.descent + fi.ascent) / 2) ;
519cb848
SC
1283}
1284
ee41971c 1285wxCoord wxDC::GetCharHeight(void) const
519cb848 1286{
3dec57ad 1287 wxCHECK_MSG(Ok(), 1, wxT("Invalid DC"));
519cb848 1288
3dec57ad 1289 wxMacPortSetter helper(this) ;
519cb848
SC
1290
1291 MacInstallFont() ;
1292
1293 FontInfo fi ;
1294 ::GetFontInfo( &fi ) ;
1295
2f1ae414 1296 return YDEV2LOGREL( fi.descent + fi.ascent );
519cb848
SC
1297}
1298
1299void wxDC::Clear(void)
1300{
3dec57ad
SC
1301 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1302 wxMacPortSetter helper(this) ;
519cb848
SC
1303 Rect rect = { -32767 , -32767 , 32767 , 32767 } ;
1304
1305 if (m_backgroundBrush.GetStyle() != wxTRANSPARENT)
1306 {
1307 MacInstallBrush() ;
1308 ::EraseRect( &rect ) ;
3dec57ad 1309 }
519cb848
SC
1310}
1311
1312void wxDC::MacInstallFont() const
1313{
3dec57ad 1314 wxCHECK_RET(Ok(), wxT("Invalid DC"));
0eaa1d68
SC
1315// if ( m_macFontInstalled )
1316// return ;
2f1ae414
SC
1317 Pattern blackColor ;
1318
519cb848
SC
1319 wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ;
1320
1321 if ( font )
1322 {
1323 ::TextFont( font->m_macFontNum ) ;
03e11df5 1324 ::TextSize( short(m_scaleY * font->m_macFontSize) ) ;
519cb848
SC
1325 ::TextFace( font->m_macFontStyle ) ;
1326
1327 m_macFontInstalled = true ;
1328 m_macBrushInstalled = false ;
1329 m_macPenInstalled = false ;
03e11df5
GD
1330
1331 RGBColor forecolor = m_textForegroundColour.GetPixel();
1332 RGBColor backcolor = m_textBackgroundColour.GetPixel();
1333 ::RGBForeColor( &forecolor );
1334 ::RGBBackColor( &backcolor );
519cb848
SC
1335 }
1336 else
1337 {
1338 short fontnum ;
1339
1340 GetFNum( "\pGeneva" , &fontnum ) ;
1341 ::TextFont( fontnum ) ;
03e11df5 1342 ::TextSize( short(m_scaleY * 10) ) ;
519cb848
SC
1343 ::TextFace( 0 ) ;
1344
1345 // todo reset after spacing changes - or store the current spacing somewhere
1346
1347 m_macFontInstalled = true ;
1348 m_macBrushInstalled = false ;
1349 m_macPenInstalled = false ;
519cb848 1350
03e11df5
GD
1351 RGBColor forecolor = m_textForegroundColour.GetPixel();
1352 RGBColor backcolor = m_textBackgroundColour.GetPixel();
1353 ::RGBForeColor( &forecolor );
1354 ::RGBBackColor( &backcolor );
1355 }
519cb848
SC
1356
1357 short mode = patCopy ;
1358
1359 // todo :
1360
1361 switch( m_logicalFunction )
1362 {
1363 case wxCOPY: // src
1364 mode = patCopy ;
1365 break ;
1366 case wxINVERT: // NOT dst
2f1ae414 1367 ::PenPat(GetQDGlobalsBlack(&blackColor));
519cb848
SC
1368 mode = patXor ;
1369 break ;
1370 case wxXOR: // src XOR dst
1371 mode = patXor ;
1372 break ;
1373 case wxOR_REVERSE: // src OR (NOT dst)
1374 mode = notPatOr ;
1375 break ;
1376 case wxSRC_INVERT: // (NOT src)
1377 mode = notPatCopy ;
1378 break ;
1379
1380 // unsupported TODO
1381
1382 case wxCLEAR: // 0
1383 case wxAND_REVERSE:// src AND (NOT dst)
1384 case wxAND: // src AND dst
1385 case wxAND_INVERT: // (NOT src) AND dst
1386 case wxNO_OP: // dst
1387 case wxNOR: // (NOT src) AND (NOT dst)
1388 case wxEQUIV: // (NOT src) XOR dst
1389 case wxOR_INVERT: // (NOT src) OR dst
1390 case wxNAND: // (NOT src) OR (NOT dst)
1391 case wxOR: // src OR dst
1392 case wxSET: // 1
2f1ae414
SC
1393// case wxSRC_OR: // source _bitmap_ OR destination
1394// case wxSRC_AND: // source _bitmap_ AND destination
519cb848
SC
1395 break ;
1396 }
1397 ::PenMode( mode ) ;
1398}
1399
1400static void wxMacGetHatchPattern(int hatchStyle, Pattern *pattern)
1401{
1402 int thePatListID = sysPatListID;
1403 int theIndex;
1404 switch(hatchStyle)
1405 {
1406 case wxBDIAGONAL_HATCH:
1407 theIndex = 34; // WCH: this is not good
1408 break;
1409 case wxFDIAGONAL_HATCH:
1410 theIndex = 26;
1411 break;
1412 case wxCROSS_HATCH:
1413 theIndex = 5;
1414 break;
1415 case wxHORIZONTAL_HATCH:
1416 theIndex = 25;
1417 break;
1418 case wxVERTICAL_HATCH:
1419 theIndex = 6;
1420 break;
1421 case wxCROSSDIAG_HATCH:
1422 theIndex = 4; // WCH: this is not good
1423 break;
1424 default:
1425 theIndex = 1; // solid pattern
1426 break;
1427 }
1428 GetIndPattern( pattern, thePatListID, theIndex);
1429}
1430
1431void wxDC::MacInstallPen() const
1432{
3dec57ad 1433 wxCHECK_RET(Ok(), wxT("Invalid DC"));
519cb848 1434
2f1ae414
SC
1435 Pattern blackColor;
1436
0eaa1d68
SC
1437// if ( m_macPenInstalled )
1438// return ;
519cb848 1439
03e11df5
GD
1440 RGBColor forecolor = m_pen.GetColour().GetPixel();
1441 RGBColor backcolor = m_backgroundBrush.GetColour().GetPixel();
1442 ::RGBForeColor( &forecolor );
1443 ::RGBBackColor( &backcolor );
519cb848
SC
1444
1445 ::PenNormal() ;
3dec57ad 1446 int penWidth = m_pen.GetWidth() * (int) m_scaleX ;
0b014ec7
SC
1447
1448 // null means only one pixel, at whatever resolution
1449 if ( penWidth == 0 )
1450 penWidth = 1 ;
519cb848
SC
1451 ::PenSize(penWidth, penWidth);
1452
1453 int penStyle = m_pen.GetStyle();
1454
1455 if (penStyle == wxSOLID)
03e11df5 1456 {
2f1ae414 1457 ::PenPat(GetQDGlobalsBlack(&blackColor));
03e11df5 1458 }
519cb848
SC
1459 else if (IS_HATCH(penStyle))
1460 {
1461 Pattern pat ;
1462 wxMacGetHatchPattern(penStyle, &pat);
1463 ::PenPat(&pat);
1464 }
1465 else
1466 {
2f1ae414 1467 ::PenPat(GetQDGlobalsBlack(&blackColor));
519cb848
SC
1468 }
1469
1470 short mode = patCopy ;
1471
1472 // todo :
1473
1474 switch( m_logicalFunction )
1475 {
1476 case wxCOPY: // src
1477 mode = patCopy ;
1478 break ;
1479 case wxINVERT: // NOT dst
2f1ae414 1480 ::PenPat(GetQDGlobalsBlack(&blackColor));
519cb848
SC
1481 mode = patXor ;
1482 break ;
1483 case wxXOR: // src XOR dst
1484 mode = patXor ;
1485 break ;
1486 case wxOR_REVERSE: // src OR (NOT dst)
1487 mode = notPatOr ;
1488 break ;
1489 case wxSRC_INVERT: // (NOT src)
1490 mode = notPatCopy ;
1491 break ;
1492
1493 // unsupported TODO
1494
1495 case wxCLEAR: // 0
1496 case wxAND_REVERSE:// src AND (NOT dst)
1497 case wxAND: // src AND dst
1498 case wxAND_INVERT: // (NOT src) AND dst
1499 case wxNO_OP: // dst
1500 case wxNOR: // (NOT src) AND (NOT dst)
1501 case wxEQUIV: // (NOT src) XOR dst
1502 case wxOR_INVERT: // (NOT src) OR dst
1503 case wxNAND: // (NOT src) OR (NOT dst)
1504 case wxOR: // src OR dst
1505 case wxSET: // 1
2f1ae414
SC
1506// case wxSRC_OR: // source _bitmap_ OR destination
1507// case wxSRC_AND: // source _bitmap_ AND destination
519cb848
SC
1508 break ;
1509 }
1510 ::PenMode( mode ) ;
1511 m_macPenInstalled = true ;
1512 m_macBrushInstalled = false ;
1513 m_macFontInstalled = false ;
1514}
1515
1516void wxDC::MacInstallBrush() const
1517{
3dec57ad 1518 wxCHECK_RET(Ok(), wxT("Invalid DC"));
0eaa1d68 1519
2f1ae414 1520 Pattern blackColor, whiteColor ;
0eaa1d68
SC
1521// if ( m_macBrushInstalled )
1522// return ;
519cb848
SC
1523
1524 // foreground
1525
03e11df5
GD
1526 RGBColor forecolor = m_brush.GetColour().GetPixel();
1527 RGBColor backcolor = m_backgroundBrush.GetColour().GetPixel();
1528 ::RGBForeColor( &forecolor );
1529 ::RGBBackColor( &backcolor );
519cb848
SC
1530
1531 int brushStyle = m_brush.GetStyle();
1532 if (brushStyle == wxSOLID)
2f1ae414 1533 ::PenPat(GetQDGlobalsBlack(&blackColor));
519cb848
SC
1534 else if (IS_HATCH(brushStyle))
1535 {
1536 Pattern pat ;
1537 wxMacGetHatchPattern(brushStyle, &pat);
1538 ::PenPat(&pat);
1539 }
1540 else
1541 {
2f1ae414 1542 ::PenPat(GetQDGlobalsBlack(&blackColor));
519cb848
SC
1543 }
1544
1545
1546 // background
1547
1548 brushStyle = m_backgroundBrush.GetStyle();
1549 if (brushStyle == wxSOLID)
2f1ae414 1550 ::BackPat(GetQDGlobalsWhite(&whiteColor));
519cb848
SC
1551 else if (IS_HATCH(brushStyle))
1552 {
1553 Pattern pat ;
1554 wxMacGetHatchPattern(brushStyle, &pat);
1555 ::BackPat(&pat);
1556 }
1557 else
1558 {
2f1ae414 1559 ::BackPat(GetQDGlobalsWhite(&whiteColor));
519cb848
SC
1560 }
1561
1562 short mode = patCopy ;
1563
1564 // todo :
1565
1566 switch( m_logicalFunction )
1567 {
1568 case wxCOPY: // src
1569 mode = patCopy ;
1570 break ;
1571 case wxINVERT: // NOT dst
2f1ae414 1572 ::PenPat(GetQDGlobalsBlack(&blackColor));
519cb848
SC
1573 mode = patXor ;
1574 break ;
1575 case wxXOR: // src XOR dst
1576 mode = patXor ;
1577 break ;
1578 case wxOR_REVERSE: // src OR (NOT dst)
1579 mode = notPatOr ;
1580 break ;
1581 case wxSRC_INVERT: // (NOT src)
1582 mode = notPatCopy ;
1583 break ;
1584
1585 // unsupported TODO
1586
1587 case wxCLEAR: // 0
1588 case wxAND_REVERSE:// src AND (NOT dst)
1589 case wxAND: // src AND dst
1590 case wxAND_INVERT: // (NOT src) AND dst
1591 case wxNO_OP: // dst
1592 case wxNOR: // (NOT src) AND (NOT dst)
1593 case wxEQUIV: // (NOT src) XOR dst
1594 case wxOR_INVERT: // (NOT src) OR dst
1595 case wxNAND: // (NOT src) OR (NOT dst)
1596 case wxOR: // src OR dst
1597 case wxSET: // 1
2f1ae414
SC
1598// case wxSRC_OR: // source _bitmap_ OR destination
1599// case wxSRC_AND: // source _bitmap_ AND destination
519cb848
SC
1600 break ;
1601 }
1602 ::PenMode( mode ) ;
1603 m_macBrushInstalled = true ;
1604 m_macPenInstalled = false ;
1605 m_macFontInstalled = false ;
1606}
1607
2f1ae414
SC
1608// ---------------------------------------------------------------------------
1609// coordinates transformations
1610// ---------------------------------------------------------------------------
519cb848 1611
2f1ae414
SC
1612
1613wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
1614{
1615 return ((wxDC *)this)->XDEV2LOG(x);
1616}
1617
1618wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
1619{
1620 return ((wxDC *)this)->YDEV2LOG(y);
1621}
1622
1623wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
1624{
1625 return ((wxDC *)this)->XDEV2LOGREL(x);
1626}
1627
1628wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
1629{
1630 return ((wxDC *)this)->YDEV2LOGREL(y);
1631}
1632
1633wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
1634{
1635 return ((wxDC *)this)->XLOG2DEV(x);
1636}
1637
1638wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
1639{
1640 return ((wxDC *)this)->YLOG2DEV(y);
1641}
1642
1643wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
1644{
1645 return ((wxDC *)this)->XLOG2DEVREL(x);
1646}
1647
1648wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
1649{
1650 return ((wxDC *)this)->YLOG2DEVREL(y);
1651}