]> git.saurik.com Git - wxWidgets.git/blob - src/mac/dc.cpp
OSX fixes for printing
[wxWidgets.git] / src / mac / dc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dc.cpp
3 // Purpose: wxDC class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "dc.h"
14 #endif
15
16 #include "wx/dc.h"
17 #include "wx/app.h"
18 #include "wx/mac/uma.h"
19 #include "wx/dcmemory.h"
20 #include "wx/region.h"
21 #include "wx/image.h"
22 #include "wx/log.h"
23
24 #if __MSL__ >= 0x6000
25 #include "math.h"
26 using namespace std ;
27 #endif
28 #include "wx/mac/private.h"
29 #include "ATSUnicode.h"
30 #include "TextCommon.h"
31 #include "TextEncodingConverter.h"
32 #if !USE_SHARED_LIBRARY
33 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
34 #endif
35
36 //-----------------------------------------------------------------------------
37 // constants
38 //-----------------------------------------------------------------------------
39
40 #define mm2inches 0.0393700787402
41 #define inches2mm 25.4
42 #define mm2twips 56.6929133859
43 #define twips2mm 0.0176388888889
44 #define mm2pt 2.83464566929
45 #define pt2mm 0.352777777778
46 #ifndef __DARWIN__
47 const double M_PI = 3.14159265358979 ;
48 #endif
49 const double RAD2DEG = 180.0 / M_PI;
50 const short kEmulatedMode = -1 ;
51 const short kUnsupportedMode = -2 ;
52
53 wxMacPortSetter::wxMacPortSetter( const wxDC* dc ) :
54 m_ph( (GrafPtr) dc->m_macPort )
55 {
56 wxASSERT( dc->Ok() ) ;
57 m_dc = dc ;
58 dc->MacSetupPort(&m_ph) ;
59 }
60
61 wxMacPortSetter::~wxMacPortSetter()
62 {
63 m_dc->MacCleanupPort(&m_ph) ;
64 }
65
66 wxMacWindowClipper::wxMacWindowClipper( const wxWindow* win )
67 {
68 m_formerClip = NewRgn() ;
69 m_newClip = NewRgn() ;
70 GetClip( m_formerClip ) ;
71
72 if ( win )
73 {
74 #if 0
75 // this clipping area was set to the parent window's drawing area, lead to problems
76 // with MacOSX controls drawing outside their wx' rectangle
77 RgnHandle insidergn = NewRgn() ;
78 int x = 0 , y = 0;
79 wxWindow *parent = win->GetParent() ;
80 parent->MacWindowToRootWindow( &x,&y ) ;
81 wxSize size = parent->GetSize() ;
82 SetRectRgn( insidergn , parent->MacGetLeftBorderSize() , parent->MacGetTopBorderSize() ,
83 size.x - parent->MacGetRightBorderSize(),
84 size.y - parent->MacGetBottomBorderSize()) ;
85 CopyRgn( (RgnHandle) parent->MacGetVisibleRegion(false).GetWXHRGN() , m_newClip ) ;
86 SectRgn( m_newClip , insidergn , m_newClip ) ;
87 OffsetRgn( m_newClip , x , y ) ;
88 SetClip( m_newClip ) ;
89 DisposeRgn( insidergn ) ;
90 #endif
91 RgnHandle insidergn = NewRgn() ;
92 int x = 0 , y = 0;
93 win->MacWindowToRootWindow( &x,&y ) ;
94 CopyRgn( (RgnHandle) ((wxWindow*)win)->MacGetVisibleRegion().GetWXHRGN() , m_newClip ) ;
95 OffsetRgn( m_newClip , x , y ) ;
96 SetClip( m_newClip ) ;
97 }
98 }
99
100 wxMacWindowClipper::~wxMacWindowClipper()
101 {
102 SetClip( m_formerClip ) ;
103 DisposeRgn( m_newClip ) ;
104 DisposeRgn( m_formerClip ) ;
105 }
106
107 //-----------------------------------------------------------------------------
108 // Local functions
109 //-----------------------------------------------------------------------------
110 static inline double dmin(double a, double b) { return a < b ? a : b; }
111 static inline double dmax(double a, double b) { return a > b ? a : b; }
112 static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
113
114 //-----------------------------------------------------------------------------
115 // wxDC
116 //-----------------------------------------------------------------------------
117 // this function emulates all wx colour manipulations, used to verify the implementation
118 // by setting the mode in the blitting functions to kEmulatedMode
119 void wxMacCalculateColour( int logical_func , const RGBColor &srcColor , RGBColor &dstColor ) ;
120
121 void wxMacCalculateColour( int logical_func , const RGBColor &srcColor , RGBColor &dstColor )
122 {
123 switch ( logical_func )
124 {
125 case wxAND: // src AND dst
126 dstColor.red = dstColor.red & srcColor.red ;
127 dstColor.green = dstColor.green & srcColor.green ;
128 dstColor.blue = dstColor.blue & srcColor.blue ;
129 break ;
130 case wxAND_INVERT: // (NOT src) AND dst
131 dstColor.red = dstColor.red & ~srcColor.red ;
132 dstColor.green = dstColor.green & ~srcColor.green ;
133 dstColor.blue = dstColor.blue & ~srcColor.blue ;
134 break ;
135 case wxAND_REVERSE:// src AND (NOT dst)
136 dstColor.red = ~dstColor.red & srcColor.red ;
137 dstColor.green = ~dstColor.green & srcColor.green ;
138 dstColor.blue = ~dstColor.blue & srcColor.blue ;
139 break ;
140 case wxCLEAR: // 0
141 dstColor.red = 0 ;
142 dstColor.green = 0 ;
143 dstColor.blue = 0 ;
144 break ;
145 case wxCOPY: // src
146 dstColor.red = srcColor.red ;
147 dstColor.green = srcColor.green ;
148 dstColor.blue = srcColor.blue ;
149 break ;
150 case wxEQUIV: // (NOT src) XOR dst
151 dstColor.red = dstColor.red ^ ~srcColor.red ;
152 dstColor.green = dstColor.green ^ ~srcColor.green ;
153 dstColor.blue = dstColor.blue ^ ~srcColor.blue ;
154 break ;
155 case wxINVERT: // NOT dst
156 dstColor.red = ~dstColor.red ;
157 dstColor.green = ~dstColor.green ;
158 dstColor.blue = ~dstColor.blue ;
159 break ;
160 case wxNAND: // (NOT src) OR (NOT dst)
161 dstColor.red = ~dstColor.red | ~srcColor.red ;
162 dstColor.green = ~dstColor.green | ~srcColor.green ;
163 dstColor.blue = ~dstColor.blue | ~srcColor.blue ;
164 break ;
165 case wxNOR: // (NOT src) AND (NOT dst)
166 dstColor.red = ~dstColor.red & ~srcColor.red ;
167 dstColor.green = ~dstColor.green & ~srcColor.green ;
168 dstColor.blue = ~dstColor.blue & ~srcColor.blue ;
169 break ;
170 case wxNO_OP: // dst
171 break ;
172 case wxOR: // src OR dst
173 dstColor.red = dstColor.red | srcColor.red ;
174 dstColor.green = dstColor.green | srcColor.green ;
175 dstColor.blue = dstColor.blue | srcColor.blue ;
176 break ;
177 case wxOR_INVERT: // (NOT src) OR dst
178 dstColor.red = dstColor.red | ~srcColor.red ;
179 dstColor.green = dstColor.green | ~srcColor.green ;
180 dstColor.blue = dstColor.blue | ~srcColor.blue ;
181 break ;
182 case wxOR_REVERSE: // src OR (NOT dst)
183 dstColor.red = ~dstColor.red | srcColor.red ;
184 dstColor.green = ~dstColor.green | srcColor.green ;
185 dstColor.blue = ~dstColor.blue | srcColor.blue ;
186 break ;
187 case wxSET: // 1
188 dstColor.red = 0xFFFF ;
189 dstColor.green = 0xFFFF ;
190 dstColor.blue = 0xFFFF ;
191 break ;
192 case wxSRC_INVERT: // (NOT src)
193 dstColor.red = ~srcColor.red ;
194 dstColor.green = ~srcColor.green ;
195 dstColor.blue = ~srcColor.blue ;
196 break ;
197 case wxXOR: // src XOR dst
198 dstColor.red = dstColor.red ^ srcColor.red ;
199 dstColor.green = dstColor.green ^ srcColor.green ;
200 dstColor.blue = dstColor.blue ^ srcColor.blue ;
201 break ;
202 }
203 }
204
205 wxDC::wxDC()
206 {
207 m_ok = FALSE;
208 m_colour = TRUE;
209 m_mm_to_pix_x = mm2pt;
210 m_mm_to_pix_y = mm2pt;
211 m_internalDeviceOriginX = 0;
212 m_internalDeviceOriginY = 0;
213 m_externalDeviceOriginX = 0;
214 m_externalDeviceOriginY = 0;
215 m_logicalScaleX = 1.0;
216 m_logicalScaleY = 1.0;
217 m_userScaleX = 1.0;
218 m_userScaleY = 1.0;
219 m_scaleX = 1.0;
220 m_scaleY = 1.0;
221 m_needComputeScaleX = FALSE;
222 m_needComputeScaleY = FALSE;
223 m_macPort = NULL ;
224 m_macMask = NULL ;
225 m_ok = FALSE ;
226 m_macFontInstalled = false ;
227 m_macBrushInstalled = false ;
228 m_macPenInstalled = false ;
229 m_macLocalOrigin.x = m_macLocalOrigin.y = 0 ;
230 m_macBoundaryClipRgn = NewRgn() ;
231 m_macCurrentClipRgn = NewRgn() ;
232 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , -32000 , -32000 , 32000 , 32000 ) ;
233 SetRectRgn( (RgnHandle) m_macCurrentClipRgn , -32000 , -32000 , 32000 , 32000 ) ;
234 m_pen = *wxBLACK_PEN;
235 m_font = *wxNORMAL_FONT;
236 m_brush = *wxWHITE_BRUSH;
237 m_macCurrentPortStateHelper = NULL ;
238 m_macATSUIStyle = NULL ;
239 m_macAliasWasEnabled = false;
240 m_macForegroundPixMap = NULL ;
241 m_macBackgroundPixMap = NULL ;
242 }
243
244 wxDC::~wxDC(void)
245 {
246 DisposeRgn( (RgnHandle) m_macBoundaryClipRgn ) ;
247 DisposeRgn( (RgnHandle) m_macCurrentClipRgn ) ;
248 }
249
250 void wxDC::MacSetupPort(wxMacPortStateHelper* help) const
251 {
252 wxASSERT( m_macCurrentPortStateHelper == NULL ) ;
253 m_macCurrentPortStateHelper = help ;
254 SetClip( (RgnHandle) m_macCurrentClipRgn);
255 m_macFontInstalled = false ;
256 m_macBrushInstalled = false ;
257 m_macPenInstalled = false ;
258 }
259
260 void wxDC::MacCleanupPort(wxMacPortStateHelper* help) const
261 {
262 wxASSERT( m_macCurrentPortStateHelper == help ) ;
263 m_macCurrentPortStateHelper = NULL ;
264 if( m_macATSUIStyle )
265 {
266 ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
267 m_macATSUIStyle = NULL ;
268 }
269 if ( m_macAliasWasEnabled )
270 {
271 SetAntiAliasedTextEnabled(m_macFormerAliasState, m_macFormerAliasSize);
272 m_macAliasWasEnabled = false ;
273 }
274 if ( m_macForegroundPixMap )
275 {
276 Pattern blackColor ;
277 ::PenPat(GetQDGlobalsBlack(&blackColor));
278 DisposePixPat( (PixPatHandle) m_macForegroundPixMap ) ;
279 m_macForegroundPixMap = NULL ;
280 }
281 if ( m_macBackgroundPixMap )
282 {
283 Pattern whiteColor ;
284 ::BackPat(GetQDGlobalsWhite(&whiteColor));
285 DisposePixPat( (PixPatHandle) m_macBackgroundPixMap ) ;
286 m_macBackgroundPixMap = NULL ;
287 }
288 }
289
290 void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask )
291 {
292 wxCHECK_RET( Ok(), wxT("invalid window dc") );
293 wxCHECK_RET( bmp.Ok(), wxT("invalid bitmap") );
294 wxMacPortSetter helper(this) ;
295 wxCoord xx = XLOG2DEVMAC(x);
296 wxCoord yy = YLOG2DEVMAC(y);
297 wxCoord w = bmp.GetWidth();
298 wxCoord h = bmp.GetHeight();
299 wxCoord ww = XLOG2DEVREL(w);
300 wxCoord hh = YLOG2DEVREL(h);
301 // Set up drawing mode
302 short mode = (m_logicalFunction == wxCOPY ? srcCopy :
303 //m_logicalFunction == wxCLEAR ? WHITENESS :
304 //m_logicalFunction == wxSET ? BLACKNESS :
305 m_logicalFunction == wxINVERT ? hilite :
306 //m_logicalFunction == wxAND ? MERGECOPY :
307 m_logicalFunction == wxOR ? srcOr :
308 m_logicalFunction == wxSRC_INVERT ? notSrcCopy :
309 m_logicalFunction == wxXOR ? srcXor :
310 m_logicalFunction == wxOR_REVERSE ? notSrcOr :
311 //m_logicalFunction == wxAND_REVERSE ? SRCERASE :
312 //m_logicalFunction == wxSRC_OR ? srcOr :
313 //m_logicalFunction == wxSRC_AND ? SRCAND :
314 srcCopy );
315 if ( bmp.GetBitmapType() == kMacBitmapTypePict ) {
316 Rect bitmaprect = { 0 , 0 , hh, ww };
317 ::OffsetRect( &bitmaprect, xx, yy ) ;
318 ::DrawPicture( (PicHandle) bmp.GetPict(), &bitmaprect ) ;
319 }
320 else if ( bmp.GetBitmapType() == kMacBitmapTypeGrafWorld )
321 {
322 GWorldPtr bmapworld = MAC_WXHBITMAP( bmp.GetHBITMAP() );
323 PixMapHandle bmappixels ;
324 // Set foreground and background colours (for bitmaps depth = 1)
325 if(bmp.GetDepth() == 1)
326 {
327 RGBColor fore = MAC_WXCOLORREF(m_textForegroundColour.GetPixel());
328 RGBColor back = MAC_WXCOLORREF(m_textBackgroundColour.GetPixel());
329 RGBForeColor(&fore);
330 RGBBackColor(&back);
331 }
332 else
333 {
334 RGBColor white = { 0xFFFF, 0xFFFF,0xFFFF} ;
335 RGBColor black = { 0,0,0} ;
336 RGBForeColor( &black ) ;
337 RGBBackColor( &white ) ;
338 }
339 bmappixels = GetGWorldPixMap( bmapworld ) ;
340 wxCHECK_RET(LockPixels(bmappixels),
341 wxT("DoDrawBitmap: Unable to lock pixels"));
342 Rect source = { 0, 0, h, w };
343 Rect dest = { yy, xx, yy + hh, xx + ww };
344 if ( useMask && bmp.GetMask() )
345 {
346 if( LockPixels(GetGWorldPixMap(MAC_WXHBITMAP(bmp.GetMask()->GetMaskBitmap()))))
347 {
348 CopyDeepMask
349 (
350 GetPortBitMapForCopyBits(bmapworld),
351 GetPortBitMapForCopyBits(MAC_WXHBITMAP(bmp.GetMask()->GetMaskBitmap())),
352 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ),
353 &source, &source, &dest, mode, NULL
354 );
355 UnlockPixels(GetGWorldPixMap(MAC_WXHBITMAP(bmp.GetMask()->GetMaskBitmap())));
356 }
357 }
358 else {
359 CopyBits( GetPortBitMapForCopyBits( bmapworld ),
360 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ),
361 &source, &dest, mode, NULL ) ;
362 }
363 UnlockPixels( bmappixels ) ;
364 }
365 else if ( bmp.GetBitmapType() == kMacBitmapTypeIcon )
366 {
367 Rect bitmaprect = { 0 , 0 , bmp.GetHeight(), bmp.GetWidth() } ;
368 OffsetRect( &bitmaprect, xx, yy ) ;
369 PlotCIconHandle( &bitmaprect , atNone , ttNone , MAC_WXHICON(bmp.GetHICON()) ) ;
370 }
371 m_macPenInstalled = false ;
372 m_macBrushInstalled = false ;
373 m_macFontInstalled = false ;
374 }
375
376 void wxDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
377 {
378 wxCHECK_RET(Ok(), wxT("Invalid dc wxDC::DoDrawIcon"));
379 wxCHECK_RET(icon.Ok(), wxT("Invalid icon wxDC::DoDrawIcon"));
380 DoDrawBitmap( icon , x , y , icon.GetMask() != NULL ) ;
381 }
382
383 void wxDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
384 {
385 wxCHECK_RET(Ok(), wxT("wxDC::DoSetClippingRegion Invalid DC"));
386 wxCoord xx, yy, ww, hh;
387 xx = XLOG2DEVMAC(x);
388 yy = YLOG2DEVMAC(y);
389 ww = XLOG2DEVREL(width);
390 hh = YLOG2DEVREL(height);
391 SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh ) ;
392 SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
393 if( m_clipping )
394 {
395 m_clipX1 = wxMax( m_clipX1 , xx );
396 m_clipY1 = wxMax( m_clipY1 , yy );
397 m_clipX2 = wxMin( m_clipX2, (xx + ww));
398 m_clipY2 = wxMin( m_clipY2, (yy + hh));
399 }
400 else
401 {
402 m_clipping = TRUE;
403 m_clipX1 = xx;
404 m_clipY1 = yy;
405 m_clipX2 = xx + ww;
406 m_clipY2 = yy + hh;
407 }
408 }
409
410 void wxDC::DoSetClippingRegionAsRegion( const wxRegion &region )
411 {
412 wxCHECK_RET( Ok(), wxT("invalid window dc") ) ;
413 wxMacPortSetter helper(this) ;
414 if (region.Empty())
415 {
416 DestroyClippingRegion();
417 return;
418 }
419 wxCoord x, y, w, h;
420 region.GetBox( x, y, w, h );
421 wxCoord xx, yy, ww, hh;
422 xx = XLOG2DEVMAC(x);
423 yy = YLOG2DEVMAC(y);
424 ww = XLOG2DEVREL(w);
425 hh = YLOG2DEVREL(h);
426 // if we have a scaling that we cannot map onto native regions
427 // we must use the box
428 if ( ww != w || hh != h )
429 {
430 wxDC::DoSetClippingRegion( x, y, w, h );
431 }
432 else
433 {
434 CopyRgn( (RgnHandle) region.GetWXHRGN() , (RgnHandle) m_macCurrentClipRgn ) ;
435 if ( xx != x || yy != y )
436 {
437 OffsetRgn( (RgnHandle) m_macCurrentClipRgn , xx - x , yy - y ) ;
438 }
439 SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
440 if( m_clipping )
441 {
442 m_clipX1 = wxMax( m_clipX1 , xx );
443 m_clipY1 = wxMax( m_clipY1 , yy );
444 m_clipX2 = wxMin( m_clipX2, (xx + ww));
445 m_clipY2 = wxMin( m_clipY2, (yy + hh));
446 }
447 else
448 {
449 m_clipping = TRUE;
450 m_clipX1 = xx;
451 m_clipY1 = yy;
452 m_clipX2 = xx + ww;
453 m_clipY2 = yy + hh;
454 }
455 }
456 }
457
458 void wxDC::DestroyClippingRegion()
459 {
460 wxMacPortSetter helper(this) ;
461 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
462 m_clipping = FALSE;
463 }
464
465 void wxDC::DoGetSize( int* width, int* height ) const
466 {
467 *width = m_maxX-m_minX;
468 *height = m_maxY-m_minY;
469 }
470
471 void wxDC::DoGetSizeMM( int* width, int* height ) const
472 {
473 int w = 0;
474 int h = 0;
475 GetSize( &w, &h );
476 *width = long( double(w) / (m_scaleX*m_mm_to_pix_x) );
477 *height = long( double(h) / (m_scaleY*m_mm_to_pix_y) );
478 }
479
480 void wxDC::SetTextForeground( const wxColour &col )
481 {
482 wxCHECK_RET(Ok(), wxT("Invalid DC"));
483 m_textForegroundColour = col;
484 m_macFontInstalled = false ;
485 }
486
487 void wxDC::SetTextBackground( const wxColour &col )
488 {
489 wxCHECK_RET(Ok(), wxT("Invalid DC"));
490 m_textBackgroundColour = col;
491 m_macFontInstalled = false ;
492 }
493
494 void wxDC::SetMapMode( int mode )
495 {
496 switch (mode)
497 {
498 case wxMM_TWIPS:
499 SetLogicalScale( twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y );
500 break;
501 case wxMM_POINTS:
502 SetLogicalScale( pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y );
503 break;
504 case wxMM_METRIC:
505 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
506 break;
507 case wxMM_LOMETRIC:
508 SetLogicalScale( m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0 );
509 break;
510 default:
511 case wxMM_TEXT:
512 SetLogicalScale( 1.0, 1.0 );
513 break;
514 }
515 if (mode != wxMM_TEXT)
516 {
517 m_needComputeScaleX = TRUE;
518 m_needComputeScaleY = TRUE;
519 }
520 }
521
522 void wxDC::SetUserScale( double x, double y )
523 {
524 // allow negative ? -> no
525 m_userScaleX = x;
526 m_userScaleY = y;
527 ComputeScaleAndOrigin();
528 }
529
530 void wxDC::SetLogicalScale( double x, double y )
531 {
532 // allow negative ?
533 m_logicalScaleX = x;
534 m_logicalScaleY = y;
535 ComputeScaleAndOrigin();
536 }
537
538 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
539 {
540 m_logicalOriginX = x * m_signX; // is this still correct ?
541 m_logicalOriginY = y * m_signY;
542 ComputeScaleAndOrigin();
543 }
544
545 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
546 {
547 m_externalDeviceOriginX = x;
548 m_externalDeviceOriginY = y;
549 ComputeScaleAndOrigin();
550 }
551
552 #if 0
553 void wxDC::SetInternalDeviceOrigin( long x, long y )
554 {
555 m_internalDeviceOriginX = x;
556 m_internalDeviceOriginY = y;
557 ComputeScaleAndOrigin();
558 }
559 void wxDC::GetInternalDeviceOrigin( long *x, long *y )
560 {
561 if (x) *x = m_internalDeviceOriginX;
562 if (y) *y = m_internalDeviceOriginY;
563 }
564 #endif
565
566 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
567 {
568 m_signX = (xLeftRight ? 1 : -1);
569 m_signY = (yBottomUp ? -1 : 1);
570 ComputeScaleAndOrigin();
571 }
572
573 wxSize wxDC::GetPPI() const
574 {
575 return wxSize(72, 72);
576 }
577
578 int wxDC::GetDepth() const
579 {
580 if ( IsPortColor( (CGrafPtr) m_macPort ) )
581 {
582 return ( (**GetPortPixMap( (CGrafPtr) m_macPort)).pixelSize ) ;
583 }
584 return 1 ;
585 }
586
587 void wxDC::ComputeScaleAndOrigin()
588 {
589 // CMB: copy scale to see if it changes
590 double origScaleX = m_scaleX;
591 double origScaleY = m_scaleY;
592 m_scaleX = m_logicalScaleX * m_userScaleX;
593 m_scaleY = m_logicalScaleY * m_userScaleY;
594 m_deviceOriginX = m_internalDeviceOriginX + m_externalDeviceOriginX;
595 m_deviceOriginY = m_internalDeviceOriginY + m_externalDeviceOriginY;
596 // CMB: if scale has changed call SetPen to recalulate the line width
597 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
598 {
599 // this is a bit artificial, but we need to force wxDC to think
600 // the pen has changed
601 wxPen* pen = & GetPen();
602 wxPen tempPen;
603 m_pen = tempPen;
604 SetPen(* pen);
605 }
606 }
607
608 void wxDC::SetPalette( const wxPalette& palette )
609 {
610 }
611
612 void wxDC::SetBackgroundMode( int mode )
613 {
614 m_backgroundMode = mode ;
615 }
616
617 void wxDC::SetFont( const wxFont &font )
618 {
619 m_font = font;
620 m_macFontInstalled = false ;
621 }
622
623 void wxDC::SetPen( const wxPen &pen )
624 {
625 if ( m_pen == pen )
626 return ;
627 m_pen = pen;
628 m_macPenInstalled = false ;
629 }
630
631 void wxDC::SetBrush( const wxBrush &brush )
632 {
633 if (m_brush == brush)
634 return;
635 m_brush = brush;
636 m_macBrushInstalled = false ;
637 }
638
639 void wxDC::SetBackground( const wxBrush &brush )
640 {
641 if (m_backgroundBrush == brush)
642 return;
643 m_backgroundBrush = brush;
644 if (!m_backgroundBrush.Ok())
645 return;
646 m_macBrushInstalled = false ;
647 }
648
649 void wxDC::SetLogicalFunction( int function )
650 {
651 if (m_logicalFunction == function)
652 return;
653 m_logicalFunction = function ;
654 m_macFontInstalled = false ;
655 m_macBrushInstalled = false ;
656 m_macPenInstalled = false ;
657 }
658
659 extern bool wxDoFloodFill(wxDC *dc, wxCoord x, wxCoord y,
660 const wxColour & col, int style);
661
662 bool wxDC::DoFloodFill(wxCoord x, wxCoord y,
663 const wxColour& col, int style)
664 {
665 return wxDoFloodFill(this, x, y, col, style);
666 }
667
668 bool wxDC::DoGetPixel( wxCoord x, wxCoord y, wxColour *col ) const
669 {
670 wxCHECK_MSG( Ok(), false, wxT("wxDC::DoGetPixel Invalid DC") );
671 wxMacPortSetter helper(this) ;
672 RGBColor colour;
673 GetCPixel( XLOG2DEVMAC(x), YLOG2DEVMAC(y), &colour );
674 // Convert from Mac colour to wx
675 col->Set( colour.red >> 8,
676 colour.green >> 8,
677 colour.blue >> 8);
678 return true ;
679 }
680
681 void wxDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
682 {
683 wxCHECK_RET(Ok(), wxT("Invalid DC"));
684 wxMacPortSetter helper(this) ;
685 if (m_pen.GetStyle() != wxTRANSPARENT)
686 {
687 MacInstallPen() ;
688 wxCoord offset = ( (m_pen.GetWidth() == 0 ? 1 :
689 m_pen.GetWidth() ) * (wxCoord)m_scaleX - 1) / 2;
690 wxCoord xx1 = XLOG2DEVMAC(x1) - offset;
691 wxCoord yy1 = YLOG2DEVMAC(y1) - offset;
692 wxCoord xx2 = XLOG2DEVMAC(x2) - offset;
693 wxCoord yy2 = YLOG2DEVMAC(y2) - offset;
694 if ((m_pen.GetCap() == wxCAP_ROUND) &&
695 (m_pen.GetWidth() <= 1))
696 {
697 // Implement LAST_NOT for MAC at least for
698 // orthogonal lines. RR.
699 if (xx1 == xx2)
700 {
701 if (yy1 < yy2)
702 yy2--;
703 if (yy1 > yy2)
704 yy2++;
705 }
706 if (yy1 == yy2)
707 {
708 if (xx1 < xx2)
709 xx2--;
710 if (xx1 > xx2)
711 xx2++;
712 }
713 }
714 ::MoveTo(xx1, yy1);
715 ::LineTo(xx2, yy2);
716 }
717 }
718
719 void wxDC::DoCrossHair( wxCoord x, wxCoord y )
720 {
721 wxCHECK_RET( Ok(), wxT("wxDC::DoCrossHair Invalid window dc") );
722 wxMacPortSetter helper(this) ;
723 if (m_pen.GetStyle() != wxTRANSPARENT)
724 {
725 int w = 0;
726 int h = 0;
727 GetSize( &w, &h );
728 wxCoord xx = XLOG2DEVMAC(x);
729 wxCoord yy = YLOG2DEVMAC(y);
730 MacInstallPen();
731 ::MoveTo( XLOG2DEVMAC(0), yy );
732 ::LineTo( XLOG2DEVMAC(w), yy );
733 ::MoveTo( xx, YLOG2DEVMAC(0) );
734 ::LineTo( xx, YLOG2DEVMAC(h) );
735 CalcBoundingBox(x, y);
736 CalcBoundingBox(x+w, y+h);
737 }
738 }
739
740 /*
741 * To draw arcs properly the angles need to be converted from the WX style:
742 * Angles start on the +ve X axis and go anti-clockwise (As you would draw on
743 * a normal axis on paper).
744 * TO
745 * the Mac style:
746 * Angles start on the +ve y axis and go clockwise.
747 */
748
749 static double wxConvertWXangleToMACangle(double angle)
750 {
751 double newAngle = 90 - angle ;
752 if ( newAngle < 0 )
753 newAngle += 360 ;
754 return newAngle ;
755 }
756
757 void wxDC::DoDrawArc( wxCoord x1, wxCoord y1,
758 wxCoord x2, wxCoord y2,
759 wxCoord xc, wxCoord yc )
760 {
761 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawArc Invalid DC"));
762 wxMacPortSetter helper(this) ;
763 wxCoord xx1 = XLOG2DEVMAC(x1);
764 wxCoord yy1 = YLOG2DEVMAC(y1);
765 wxCoord xx2 = XLOG2DEVMAC(x2);
766 wxCoord yy2 = YLOG2DEVMAC(y2);
767 wxCoord xxc = XLOG2DEVMAC(xc);
768 wxCoord yyc = YLOG2DEVMAC(yc);
769 double dx = xx1 - xxc;
770 double dy = yy1 - yyc;
771 double radius = sqrt((double)(dx*dx+dy*dy));
772 wxCoord rad = (wxCoord)radius;
773 double radius1, radius2;
774 if (xx1 == xx2 && yy1 == yy2)
775 {
776 radius1 = 0.0;
777 radius2 = 360.0;
778 }
779 else if (radius == 0.0)
780 {
781 radius1 = radius2 = 0.0;
782 }
783 else
784 {
785 radius1 = (xx1 - xxc == 0) ?
786 (yy1 - yyc < 0) ? 90.0 : -90.0 :
787 -atan2(double(yy1-yyc), double(xx1-xxc)) * RAD2DEG;
788 radius2 = (xx2 - xxc == 0) ?
789 (yy2 - yyc < 0) ? 90.0 : -90.0 :
790 -atan2(double(yy2-yyc), double(xx2-xxc)) * RAD2DEG;
791 }
792 wxCoord alpha2 = wxCoord(radius2 - radius1);
793 wxCoord alpha1 = wxCoord(wxConvertWXangleToMACangle(radius1));
794 if( (xx1 > xx2) || (yy1 > yy2) ) {
795 alpha2 *= -1;
796 }
797 Rect r = { yyc - rad, xxc - rad, yyc + rad, xxc + rad };
798 if(m_brush.GetStyle() != wxTRANSPARENT) {
799 MacInstallBrush();
800 PaintArc(&r, alpha1, alpha2);
801 }
802 if(m_pen.GetStyle() != wxTRANSPARENT) {
803 MacInstallPen();
804 FrameArc(&r, alpha1, alpha2);
805 }
806 }
807
808 void wxDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
809 double sa, double ea )
810 {
811 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawEllepticArc Invalid DC"));
812 wxMacPortSetter helper(this) ;
813 Rect r;
814 double angle = sa - ea; // Order important Mac in opposite direction to wx
815 // we have to make sure that the filling is always counter-clockwise
816 if ( angle > 0 )
817 angle -= 360 ;
818 wxCoord xx = XLOG2DEVMAC(x);
819 wxCoord yy = YLOG2DEVMAC(y);
820 wxCoord ww = m_signX * XLOG2DEVREL(w);
821 wxCoord hh = m_signY * YLOG2DEVREL(h);
822 // handle -ve width and/or height
823 if (ww < 0) { ww = -ww; xx = xx - ww; }
824 if (hh < 0) { hh = -hh; yy = yy - hh; }
825 sa = wxConvertWXangleToMACangle(sa);
826 r.top = yy;
827 r.left = xx;
828 r.bottom = yy + hh;
829 r.right = xx + ww;
830 if(m_brush.GetStyle() != wxTRANSPARENT) {
831 MacInstallBrush();
832 PaintArc(&r, (short)sa, (short)angle);
833 }
834 if(m_pen.GetStyle() != wxTRANSPARENT) {
835 MacInstallPen();
836 FrameArc(&r, (short)sa, (short)angle);
837 }
838 }
839
840 void wxDC::DoDrawPoint( wxCoord x, wxCoord y )
841 {
842 wxCHECK_RET(Ok(), wxT("Invalid DC"));
843 wxMacPortSetter helper(this) ;
844 if (m_pen.GetStyle() != wxTRANSPARENT)
845 {
846 wxCoord xx1 = XLOG2DEVMAC(x);
847 wxCoord yy1 = YLOG2DEVMAC(y);
848 RGBColor pencolor = MAC_WXCOLORREF( m_pen.GetColour().GetPixel());
849 ::SetCPixel( xx1,yy1,&pencolor) ;
850 CalcBoundingBox(x, y);
851 }
852 }
853
854 void wxDC::DoDrawLines(int n, wxPoint points[],
855 wxCoord xoffset, wxCoord yoffset)
856 {
857 wxCHECK_RET(Ok(), wxT("Invalid DC"));
858 wxMacPortSetter helper(this) ;
859 if (m_pen.GetStyle() == wxTRANSPARENT)
860 return;
861 MacInstallPen() ;
862 wxCoord offset = ( (m_pen.GetWidth() == 0 ? 1 :
863 m_pen.GetWidth() ) * (wxCoord)m_scaleX - 1) / 2 ;
864 wxCoord x1, x2 , y1 , y2 ;
865 x1 = XLOG2DEVMAC(points[0].x + xoffset);
866 y1 = YLOG2DEVMAC(points[0].y + yoffset);
867 ::MoveTo(x1 - offset, y1 - offset );
868 for (int i = 0; i < n-1; i++)
869 {
870 x2 = XLOG2DEVMAC(points[i+1].x + xoffset);
871 y2 = YLOG2DEVMAC(points[i+1].y + yoffset);
872 ::LineTo( x2 - offset, y2 - offset );
873 }
874 }
875
876 void wxDC::DoDrawPolygon(int n, wxPoint points[],
877 wxCoord xoffset, wxCoord yoffset,
878 int fillStyle )
879 {
880 wxCHECK_RET(Ok(), wxT("Invalid DC"));
881 wxMacPortSetter helper(this) ;
882 wxCoord x1, x2 , y1 , y2 ;
883 if ( m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT )
884 return ;
885 PolyHandle polygon = OpenPoly();
886 x2 = x1 = XLOG2DEVMAC(points[0].x + xoffset);
887 y2 = y1 = YLOG2DEVMAC(points[0].y + yoffset);
888 ::MoveTo(x1,y1);
889 for (int i = 1; i < n; i++)
890 {
891 x2 = XLOG2DEVMAC(points[i].x + xoffset);
892 y2 = YLOG2DEVMAC(points[i].y + yoffset);
893 ::LineTo(x2, y2);
894 }
895 // close the polyline if necessary
896 if ( x1 != x2 || y1 != y2 )
897 {
898 ::LineTo(x1,y1 ) ;
899 }
900 ClosePoly();
901 if (m_brush.GetStyle() != wxTRANSPARENT)
902 {
903 MacInstallBrush();
904 ::PaintPoly( polygon );
905 }
906 if (m_pen.GetStyle() != wxTRANSPARENT)
907 {
908 MacInstallPen() ;
909 ::FramePoly( polygon ) ;
910 }
911 KillPoly( polygon );
912 }
913
914 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
915 {
916 wxCHECK_RET(Ok(), wxT("Invalid DC"));
917 wxMacPortSetter helper(this) ;
918 wxCoord xx = XLOG2DEVMAC(x);
919 wxCoord yy = YLOG2DEVMAC(y);
920 wxCoord ww = m_signX * XLOG2DEVREL(width);
921 wxCoord hh = m_signY * YLOG2DEVREL(height);
922 // CMB: draw nothing if transformed w or h is 0
923 if (ww == 0 || hh == 0)
924 return;
925 // CMB: handle -ve width and/or height
926 if (ww < 0)
927 {
928 ww = -ww;
929 xx = xx - ww;
930 }
931 if (hh < 0)
932 {
933 hh = -hh;
934 yy = yy - hh;
935 }
936 Rect rect = { yy , xx , yy + hh , xx + ww } ;
937 if (m_brush.GetStyle() != wxTRANSPARENT)
938 {
939 MacInstallBrush() ;
940 ::PaintRect( &rect ) ;
941 }
942 if (m_pen.GetStyle() != wxTRANSPARENT)
943 {
944 MacInstallPen() ;
945 ::FrameRect( &rect ) ;
946 }
947 }
948
949 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
950 wxCoord width, wxCoord height,
951 double radius)
952 {
953 wxCHECK_RET(Ok(), wxT("Invalid DC"));
954 wxMacPortSetter helper(this) ;
955 if (radius < 0.0)
956 radius = - radius * ((width < height) ? width : height);
957 wxCoord xx = XLOG2DEVMAC(x);
958 wxCoord yy = YLOG2DEVMAC(y);
959 wxCoord ww = m_signX * XLOG2DEVREL(width);
960 wxCoord hh = m_signY * YLOG2DEVREL(height);
961 // CMB: draw nothing if transformed w or h is 0
962 if (ww == 0 || hh == 0)
963 return;
964 // CMB: handle -ve width and/or height
965 if (ww < 0)
966 {
967 ww = -ww;
968 xx = xx - ww;
969 }
970 if (hh < 0)
971 {
972 hh = -hh;
973 yy = yy - hh;
974 }
975 Rect rect = { yy , xx , yy + hh , xx + ww } ;
976 if (m_brush.GetStyle() != wxTRANSPARENT)
977 {
978 MacInstallBrush() ;
979 ::PaintRoundRect( &rect , int(radius * 2) , int(radius * 2) ) ;
980 }
981 if (m_pen.GetStyle() != wxTRANSPARENT)
982 {
983 MacInstallPen() ;
984 ::FrameRoundRect( &rect , int(radius * 2) , int(radius * 2) ) ;
985 }
986 }
987
988 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
989 {
990 wxCHECK_RET(Ok(), wxT("Invalid DC"));
991 wxMacPortSetter helper(this) ;
992 wxCoord xx = XLOG2DEVMAC(x);
993 wxCoord yy = YLOG2DEVMAC(y);
994 wxCoord ww = m_signX * XLOG2DEVREL(width);
995 wxCoord hh = m_signY * YLOG2DEVREL(height);
996 // CMB: draw nothing if transformed w or h is 0
997 if (ww == 0 || hh == 0)
998 return;
999 // CMB: handle -ve width and/or height
1000 if (ww < 0)
1001 {
1002 ww = -ww;
1003 xx = xx - ww;
1004 }
1005 if (hh < 0)
1006 {
1007 hh = -hh;
1008 yy = yy - hh;
1009 }
1010 Rect rect = { yy , xx , yy + hh , xx + ww } ;
1011 if (m_brush.GetStyle() != wxTRANSPARENT)
1012 {
1013 MacInstallBrush() ;
1014 ::PaintOval( &rect ) ;
1015 }
1016 if (m_pen.GetStyle() != wxTRANSPARENT)
1017 {
1018 MacInstallPen() ;
1019 ::FrameOval( &rect ) ;
1020 }
1021 }
1022
1023 bool wxDC::CanDrawBitmap(void) const
1024 {
1025 return true ;
1026 }
1027
1028 bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
1029 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool useMask,
1030 wxCoord xsrcMask, wxCoord ysrcMask )
1031 {
1032 wxCHECK_MSG(Ok(), false, wxT("wxDC::DoBlit Illegal dc"));
1033 wxCHECK_MSG(source->Ok(), false, wxT("wxDC::DoBlit Illegal source DC"));
1034 if ( logical_func == wxNO_OP )
1035 return TRUE ;
1036 if (xsrcMask == -1 && ysrcMask == -1)
1037 {
1038 xsrcMask = xsrc; ysrcMask = ysrc;
1039 }
1040 // correct the parameter in case this dc does not have a mask at all
1041 if ( useMask && !source->m_macMask )
1042 useMask = false ;
1043 Rect srcrect , dstrect ;
1044 srcrect.top = source->YLOG2DEVMAC(ysrc) ;
1045 srcrect.left = source->XLOG2DEVMAC(xsrc) ;
1046 srcrect.right = source->XLOG2DEVMAC(xsrc + width ) ;
1047 srcrect.bottom = source->YLOG2DEVMAC(ysrc + height) ;
1048 dstrect.top = YLOG2DEVMAC(ydest) ;
1049 dstrect.left = XLOG2DEVMAC(xdest) ;
1050 dstrect.bottom = YLOG2DEVMAC(ydest + height ) ;
1051 dstrect.right = XLOG2DEVMAC(xdest + width ) ;
1052 short mode = kUnsupportedMode ;
1053 bool invertDestinationFirst = false ;
1054 switch ( logical_func )
1055 {
1056 case wxAND: // src AND dst
1057 mode = adMin ; // ok
1058 break ;
1059 case wxAND_INVERT: // (NOT src) AND dst
1060 mode = notSrcOr ; // ok
1061 break ;
1062 case wxAND_REVERSE:// src AND (NOT dst)
1063 invertDestinationFirst = true ;
1064 mode = srcOr ;
1065 break ;
1066 case wxCLEAR: // 0
1067 mode = kEmulatedMode ;
1068 break ;
1069 case wxCOPY: // src
1070 mode = srcCopy ; // ok
1071 break ;
1072 case wxEQUIV: // (NOT src) XOR dst
1073 mode = srcXor ; // ok
1074 break ;
1075 case wxINVERT: // NOT dst
1076 mode = kEmulatedMode ; //or hilite ;
1077 break ;
1078 case wxNAND: // (NOT src) OR (NOT dst)
1079 invertDestinationFirst = true ;
1080 mode = srcBic ;
1081 break ;
1082 case wxNOR: // (NOT src) AND (NOT dst)
1083 invertDestinationFirst = true ;
1084 mode = notSrcOr ;
1085 break ;
1086 case wxNO_OP: // dst
1087 mode = kEmulatedMode ; // this has already been handled upon entry
1088 break ;
1089 case wxOR: // src OR dst
1090 mode = notSrcBic ;
1091 break ;
1092 case wxOR_INVERT: // (NOT src) OR dst
1093 mode = srcBic ;
1094 break ;
1095 case wxOR_REVERSE: // src OR (NOT dst)
1096 invertDestinationFirst = true ;
1097 mode = notSrcBic ;
1098 break ;
1099 case wxSET: // 1
1100 mode = kEmulatedMode ;
1101 break ;
1102 case wxSRC_INVERT: // (NOT src)
1103 mode = notSrcCopy ; // ok
1104 break ;
1105 case wxXOR: // src XOR dst
1106 mode = notSrcXor ; // ok
1107 break ;
1108 default :
1109 break ;
1110 }
1111 if ( mode == kUnsupportedMode )
1112 {
1113 wxFAIL_MSG("unsupported blitting mode" );
1114 return FALSE ;
1115 }
1116 CGrafPtr sourcePort = (CGrafPtr) source->m_macPort ;
1117 PixMapHandle bmappixels = GetGWorldPixMap( sourcePort ) ;
1118 if ( LockPixels(bmappixels) )
1119 {
1120 wxMacPortSetter helper(this) ;
1121 if ( source->GetDepth() == 1 )
1122 {
1123 RGBForeColor( &MAC_WXCOLORREF(m_textForegroundColour.GetPixel()) ) ;
1124 RGBBackColor( &MAC_WXCOLORREF(m_textBackgroundColour.GetPixel()) ) ;
1125 }
1126 else
1127 {
1128 // the modes need this, otherwise we'll end up having really nice colors...
1129 RGBColor white = { 0xFFFF, 0xFFFF,0xFFFF} ;
1130 RGBColor black = { 0,0,0} ;
1131 RGBForeColor( &black ) ;
1132 RGBBackColor( &white ) ;
1133 }
1134 if ( useMask && source->m_macMask )
1135 {
1136 if ( mode == srcCopy )
1137 {
1138 if ( LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) )
1139 {
1140 CopyMask( GetPortBitMapForCopyBits( sourcePort ) ,
1141 GetPortBitMapForCopyBits( MAC_WXHBITMAP(source->m_macMask) ) ,
1142 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ) ,
1143 &srcrect, &srcrect , &dstrect ) ;
1144 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) ;
1145 }
1146 }
1147 else
1148 {
1149 RgnHandle clipRgn = NewRgn() ;
1150 LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) ;
1151 BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) ;
1152 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) ;
1153 OffsetRgn( clipRgn , -srcrect.left + dstrect.left, -srcrect.top + dstrect.top ) ;
1154 if ( mode == kEmulatedMode )
1155 {
1156 Pattern pat ;
1157 ::PenPat(GetQDGlobalsBlack(&pat));
1158 if ( logical_func == wxSET )
1159 {
1160 RGBColor col= { 0xFFFF, 0xFFFF, 0xFFFF } ;
1161 ::RGBForeColor( &col ) ;
1162 ::PaintRgn( clipRgn ) ;
1163 }
1164 else if ( logical_func == wxCLEAR )
1165 {
1166 RGBColor col= { 0x0000, 0x0000, 0x0000 } ;
1167 ::RGBForeColor( &col ) ;
1168 ::PaintRgn( clipRgn ) ;
1169 }
1170 else if ( logical_func == wxINVERT )
1171 {
1172 MacInvertRgn( clipRgn ) ;
1173 }
1174 else
1175 {
1176 for ( int y = 0 ; y < srcrect.right - srcrect.left ; ++y )
1177 {
1178 for ( int x = 0 ; x < srcrect.bottom - srcrect.top ; ++x )
1179 {
1180 Point dstPoint = { dstrect.top + y , dstrect.left + x } ;
1181 Point srcPoint = { srcrect.top + y , srcrect.left + x } ;
1182 if ( PtInRgn( dstPoint , clipRgn ) )
1183 {
1184 RGBColor srcColor ;
1185 RGBColor dstColor ;
1186 SetPort( (GrafPtr) sourcePort ) ;
1187 GetCPixel( srcPoint.h , srcPoint.v , &srcColor) ;
1188 SetPort( (GrafPtr) m_macPort ) ;
1189 GetCPixel( dstPoint.h , dstPoint.v , &dstColor ) ;
1190 wxMacCalculateColour( logical_func , srcColor , dstColor ) ;
1191 SetCPixel( dstPoint.h , dstPoint.v , &dstColor ) ;
1192 }
1193 }
1194 }
1195 }
1196 }
1197 else
1198 {
1199 if ( invertDestinationFirst )
1200 {
1201 MacInvertRgn( clipRgn ) ;
1202 }
1203 CopyBits( GetPortBitMapForCopyBits( sourcePort ) ,
1204 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ) ,
1205 &srcrect, &dstrect, mode, clipRgn ) ;
1206 }
1207 DisposeRgn( clipRgn ) ;
1208 }
1209 }
1210 else
1211 {
1212 RgnHandle clipRgn = NewRgn() ;
1213 SetRectRgn( clipRgn , dstrect.left , dstrect.top , dstrect.right , dstrect.bottom ) ;
1214 if ( mode == kEmulatedMode )
1215 {
1216 Pattern pat ;
1217 ::PenPat(GetQDGlobalsBlack(&pat));
1218 if ( logical_func == wxSET )
1219 {
1220 RGBColor col= { 0xFFFF, 0xFFFF, 0xFFFF } ;
1221 ::RGBForeColor( &col ) ;
1222 ::PaintRgn( clipRgn ) ;
1223 }
1224 else if ( logical_func == wxCLEAR )
1225 {
1226 RGBColor col= { 0x0000, 0x0000, 0x0000 } ;
1227 ::RGBForeColor( &col ) ;
1228 ::PaintRgn( clipRgn ) ;
1229 }
1230 else if ( logical_func == wxINVERT )
1231 {
1232 MacInvertRgn( clipRgn ) ;
1233 }
1234 else
1235 {
1236 for ( int y = 0 ; y < srcrect.right - srcrect.left ; ++y )
1237 {
1238 for ( int x = 0 ; x < srcrect.bottom - srcrect.top ; ++x )
1239 {
1240 Point dstPoint = { dstrect.top + y , dstrect.left + x } ;
1241 Point srcPoint = { srcrect.top + y , srcrect.left + x } ;
1242 {
1243 RGBColor srcColor ;
1244 RGBColor dstColor ;
1245 SetPort( (GrafPtr) sourcePort ) ;
1246 GetCPixel( srcPoint.h , srcPoint.v , &srcColor) ;
1247 SetPort( (GrafPtr) m_macPort ) ;
1248 GetCPixel( dstPoint.h , dstPoint.v , &dstColor ) ;
1249 wxMacCalculateColour( logical_func , srcColor , dstColor ) ;
1250 SetCPixel( dstPoint.h , dstPoint.v , &dstColor ) ;
1251 }
1252 }
1253 }
1254 }
1255 }
1256 else
1257 {
1258 if ( invertDestinationFirst )
1259 {
1260 MacInvertRgn( clipRgn ) ;
1261 }
1262 CopyBits( GetPortBitMapForCopyBits( sourcePort ) ,
1263 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ) ,
1264 &srcrect, &dstrect, mode, NULL ) ;
1265 }
1266 DisposeRgn( clipRgn ) ;
1267 }
1268 UnlockPixels( bmappixels ) ;
1269 }
1270 m_macPenInstalled = false ;
1271 m_macBrushInstalled = false ;
1272 m_macFontInstalled = false ;
1273 return TRUE;
1274 }
1275
1276 inline Fixed IntToFixed( int inInt )
1277 {
1278 return (((SInt32) inInt) << 16);
1279 }
1280
1281 inline int FixedToInt( Fixed inFixed )
1282 {
1283 return (((SInt32) inFixed) >> 16);
1284 }
1285
1286 void wxDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y,
1287 double angle)
1288 {
1289 wxCHECK_RET( Ok(), wxT("wxDC::DoDrawRotatedText Invalid window dc") );
1290 if (angle == 0.0 )
1291 {
1292 DrawText(str, x, y);
1293 return;
1294 }
1295 if ( str.Length() == 0 )
1296 return ;
1297 wxMacPortSetter helper(this) ;
1298 MacInstallFont() ;
1299 wxString text ;
1300 if ( wxApp::s_macDefaultEncodingIsPC )
1301 {
1302 text = wxMacMakeMacStringFromPC( str ) ;
1303 }
1304 else
1305 {
1306 text = str ;
1307 }
1308 wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ;
1309 if ( 0 )
1310 {
1311 m_macFormerAliasState = IsAntiAliasedTextEnabled(&m_macFormerAliasSize);
1312 SetAntiAliasedTextEnabled(true, SInt16(m_scaleY * font->m_macFontSize));
1313 m_macAliasWasEnabled = true ;
1314 }
1315 OSStatus status = noErr ;
1316 TECObjectRef ec;
1317 status = TECCreateConverter(&ec, kTextEncodingMacRoman, kTextEncodingUnicodeDefault);
1318 wxASSERT_MSG( status == noErr , "couldn't start converter" ) ;
1319 ByteCount byteOutLen ;
1320 ByteCount byteInLen = text.Length() ;
1321 ByteCount byteBufferLen = byteInLen *2 ;
1322 char* buf = new char[byteBufferLen] ;
1323 status = TECConvertText(ec, (ConstTextPtr)text.c_str() , byteInLen, &byteInLen,
1324 (TextPtr)buf, byteBufferLen, &byteOutLen);
1325 wxASSERT_MSG( status == noErr , "couldn't convert text" ) ;
1326 status = TECDisposeConverter(ec);
1327 wxASSERT_MSG( status == noErr , "couldn't dispose converter" ) ;
1328 ATSUTextLayout atsuLayout ;
1329 UniCharCount chars = byteOutLen / 2 ;
1330 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) buf , 0 , byteOutLen / 2 , byteOutLen / 2 , 1 ,
1331 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ;
1332 wxASSERT_MSG( status == noErr , "couldn't create the layout of the rotated text" );
1333 int iAngle = int( angle );
1334 int drawX = XLOG2DEVMAC(x) ;
1335 int drawY = YLOG2DEVMAC(y) ;
1336
1337 ATSUTextMeasurement textBefore ;
1338 ATSUTextMeasurement textAfter ;
1339 ATSUTextMeasurement ascent ;
1340 ATSUTextMeasurement descent ;
1341
1342
1343 if ( abs(iAngle) > 0 )
1344 {
1345 Fixed atsuAngle = IntToFixed( iAngle ) ;
1346 ATSUAttributeTag atsuTags[] =
1347 {
1348 kATSULineRotationTag ,
1349 } ;
1350 ByteCount atsuSizes[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
1351 {
1352 sizeof( Fixed ) ,
1353 } ;
1354 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
1355 {
1356 &atsuAngle ,
1357 } ;
1358 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags)/sizeof(ATSUAttributeTag),
1359 atsuTags, atsuSizes, atsuValues ) ;
1360 }
1361 status = ::ATSUMeasureText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1362 &textBefore , &textAfter, &ascent , &descent );
1363
1364 drawX += sin(angle/RAD2DEG) * FixedToInt(ascent) ;
1365 drawY += cos(angle/RAD2DEG) * FixedToInt(ascent) ;
1366 status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1367 IntToFixed(drawX) , IntToFixed(drawY) );
1368 wxASSERT_MSG( status == noErr , "couldn't draw the rotated text" );
1369 Rect rect ;
1370 status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1371 IntToFixed(drawX) , IntToFixed(drawY) , &rect );
1372 wxASSERT_MSG( status == noErr , "couldn't measure the rotated text" );
1373 OffsetRect( &rect , -m_macLocalOrigin.x , -m_macLocalOrigin.y ) ;
1374 CalcBoundingBox(XDEV2LOG(rect.left), YDEV2LOG(rect.top) );
1375 CalcBoundingBox(XDEV2LOG(rect.right), YDEV2LOG(rect.bottom) );
1376 ::ATSUDisposeTextLayout(atsuLayout);
1377 delete[] buf ;
1378 }
1379
1380 void wxDC::DoDrawText(const wxString& strtext, wxCoord x, wxCoord y)
1381 {
1382 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawText Invalid DC"));
1383 wxMacPortSetter helper(this) ;
1384 long xx = XLOG2DEVMAC(x);
1385 long yy = YLOG2DEVMAC(y);
1386 #if TARGET_CARBON
1387 bool useDrawThemeText = ( DrawThemeTextBox != (void*) kUnresolvedCFragSymbolAddress ) ;
1388 if ( IsKindOf(CLASSINFO( wxPrinterDC ) ) || m_font.GetNoAntiAliasing() )
1389 useDrawThemeText = false ;
1390 #endif
1391 MacInstallFont() ;
1392 if ( 0 )
1393 {
1394 m_macFormerAliasState = IsAntiAliasedTextEnabled(&m_macFormerAliasSize);
1395 SetAntiAliasedTextEnabled(true, 8);
1396 m_macAliasWasEnabled = true ;
1397 }
1398 FontInfo fi ;
1399 ::GetFontInfo( &fi ) ;
1400 #if TARGET_CARBON
1401 if ( !useDrawThemeText )
1402 #endif
1403 yy += fi.ascent ;
1404 ::MoveTo( xx , yy );
1405 if ( m_backgroundMode == wxTRANSPARENT )
1406 {
1407 ::TextMode( srcOr) ;
1408 }
1409 else
1410 {
1411 ::TextMode( srcCopy ) ;
1412 }
1413 const char *text = NULL ;
1414 int length = 0 ;
1415 wxString macText ;
1416 if ( wxApp::s_macDefaultEncodingIsPC )
1417 {
1418 macText = wxMacMakeMacStringFromPC( strtext ) ;
1419 text = macText ;
1420 length = macText.Length() ;
1421 }
1422 else
1423 {
1424 text = strtext ;
1425 length = strtext.Length() ;
1426 }
1427 int laststop = 0 ;
1428 int i = 0 ;
1429 int line = 0 ;
1430 {
1431 while( i < length )
1432 {
1433 if( text[i] == 13 || text[i] == 10)
1434 {
1435 #if TARGET_CARBON
1436 if ( useDrawThemeText )
1437 {
1438 Rect frame = { yy + line*(fi.descent + fi.ascent + fi.leading) ,xx , yy + (line+1)*(fi.descent + fi.ascent + fi.leading) , xx + 10000 } ;
1439 CFStringRef mString = CFStringCreateWithBytes( NULL , (UInt8*) text + laststop , i - laststop , CFStringGetSystemEncoding(), false ) ;
1440 if ( m_backgroundMode != wxTRANSPARENT )
1441 {
1442 Point bounds={0,0} ;
1443 Rect background = frame ;
1444 SInt16 baseline ;
1445 ::GetThemeTextDimensions( mString,
1446 kThemeCurrentPortFont,
1447 kThemeStateActive,
1448 false,
1449 &bounds,
1450 &baseline );
1451 background.right = background.left + bounds.h ;
1452 background.bottom = background.top + bounds.v ;
1453 ::EraseRect( &background ) ;
1454 }
1455 ::DrawThemeTextBox( mString,
1456 kThemeCurrentPortFont,
1457 kThemeStateActive,
1458 false,
1459 &frame,
1460 teJustLeft,
1461 nil );
1462 CFRelease( mString ) ;
1463 line++ ;
1464 }
1465 else
1466 #endif
1467 {
1468 ::DrawText( text , laststop , i - laststop ) ;
1469 line++ ;
1470 ::MoveTo( xx , yy + line*(fi.descent + fi.ascent + fi.leading) );
1471 }
1472 laststop = i+1 ;
1473 }
1474 i++ ;
1475 }
1476 #if TARGET_CARBON
1477 if ( useDrawThemeText )
1478 {
1479 Rect frame = { yy + line*(fi.descent + fi.ascent + fi.leading) ,xx , yy + (line+1)*(fi.descent + fi.ascent + fi.leading) , xx + 10000 } ;
1480 CFStringRef mString = CFStringCreateWithCString( NULL , text + laststop , kCFStringEncodingMacRoman ) ;
1481
1482 if ( m_backgroundMode != wxTRANSPARENT )
1483 {
1484 Point bounds={0,0} ;
1485 Rect background = frame ;
1486 SInt16 baseline ;
1487 ::GetThemeTextDimensions( mString,
1488 kThemeCurrentPortFont,
1489 kThemeStateActive,
1490 false,
1491 &bounds,
1492 &baseline );
1493 background.right = background.left + bounds.h ;
1494 background.bottom = background.top + bounds.v ;
1495 ::EraseRect( &background ) ;
1496 }
1497 ::DrawThemeTextBox( mString,
1498 kThemeCurrentPortFont,
1499 kThemeStateActive,
1500 false,
1501 &frame,
1502 teJustLeft,
1503 nil );
1504 CFRelease( mString ) ;
1505 }
1506 else
1507 #endif
1508 {
1509 ::DrawText( text , laststop , i - laststop ) ;
1510 }
1511 }
1512 ::TextMode( srcOr ) ;
1513 }
1514
1515 bool wxDC::CanGetTextExtent() const
1516 {
1517 wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
1518 return true ;
1519 }
1520
1521 void wxDC::DoGetTextExtent( const wxString &string, wxCoord *width, wxCoord *height,
1522 wxCoord *descent, wxCoord *externalLeading ,
1523 wxFont *theFont ) const
1524 {
1525 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1526 wxMacPortSetter helper(this) ;
1527 wxFont formerFont = m_font ;
1528 if ( theFont )
1529 {
1530 // work around the constness
1531 *((wxFont*)(&m_font)) = *theFont ;
1532 }
1533 MacInstallFont() ;
1534 FontInfo fi ;
1535 ::GetFontInfo( &fi ) ;
1536 #if TARGET_CARBON
1537 bool useGetThemeText = ( GetThemeTextDimensions != (void*) kUnresolvedCFragSymbolAddress ) ;
1538 if ( IsKindOf(CLASSINFO( wxPrinterDC ) ) || ((wxFont*)&m_font)->GetNoAntiAliasing() )
1539 useGetThemeText = false ;
1540 #endif
1541 if ( height )
1542 *height = YDEV2LOGREL( fi.descent + fi.ascent ) ;
1543 if ( descent )
1544 *descent =YDEV2LOGREL( fi.descent );
1545 if ( externalLeading )
1546 *externalLeading = YDEV2LOGREL( fi.leading ) ;
1547 const char *text = NULL ;
1548 int length = 0 ;
1549 wxString macText ;
1550 if ( wxApp::s_macDefaultEncodingIsPC )
1551 {
1552 macText = wxMacMakeMacStringFromPC( string ) ;
1553 text = macText ;
1554 length = macText.Length() ;
1555 }
1556 else
1557 {
1558 text = string ;
1559 length = string.Length() ;
1560 }
1561 int laststop = 0 ;
1562 int i = 0 ;
1563 int curwidth = 0 ;
1564 if ( width )
1565 {
1566 *width = 0 ;
1567 while( i < length )
1568 {
1569 if( text[i] == 13 || text[i] == 10)
1570 {
1571 if ( height )
1572 *height += YDEV2LOGREL( fi.descent + fi.ascent + fi.leading ) ;
1573 #if TARGET_CARBON
1574 if ( useGetThemeText )
1575 {
1576 Point bounds={0,0} ;
1577 SInt16 baseline ;
1578 CFStringRef mString = CFStringCreateWithBytes( NULL , (UInt8*) text + laststop , i - laststop , CFStringGetSystemEncoding(), false ) ;
1579 ::GetThemeTextDimensions( mString,
1580 kThemeCurrentPortFont,
1581 kThemeStateActive,
1582 false,
1583 &bounds,
1584 &baseline );
1585 CFRelease( mString ) ;
1586 curwidth = bounds.h ;
1587 }
1588 else
1589 #endif
1590 {
1591 curwidth = ::TextWidth( text , laststop , i - laststop ) ;
1592 }
1593 if ( curwidth > *width )
1594 *width = XDEV2LOGREL( curwidth ) ;
1595 laststop = i+1 ;
1596 }
1597 i++ ;
1598 }
1599
1600 #if TARGET_CARBON
1601 if ( useGetThemeText )
1602 {
1603 Point bounds={0,0} ;
1604 SInt16 baseline ;
1605 CFStringRef mString = CFStringCreateWithBytes( NULL , (UInt8*) text + laststop , i - laststop , CFStringGetSystemEncoding(), false ) ;
1606 ::GetThemeTextDimensions( mString,
1607 kThemeCurrentPortFont,
1608 kThemeStateActive,
1609 false,
1610 &bounds,
1611 &baseline );
1612 CFRelease( mString ) ;
1613 curwidth = bounds.h ;
1614 }
1615 else
1616 #endif
1617 {
1618 curwidth = ::TextWidth( text , laststop , i - laststop ) ;
1619 }
1620 if ( curwidth > *width )
1621 *width = XDEV2LOGREL( curwidth ) ;
1622 }
1623 if ( theFont )
1624 {
1625 // work around the constness
1626 *((wxFont*)(&m_font)) = formerFont ;
1627 m_macFontInstalled = false ;
1628 }
1629 }
1630
1631 wxCoord wxDC::GetCharWidth(void) const
1632 {
1633 wxCHECK_MSG(Ok(), 1, wxT("Invalid DC"));
1634 wxMacPortSetter helper(this) ;
1635 MacInstallFont() ;
1636 int width = 0 ;
1637 #if TARGET_CARBON
1638 bool useGetThemeText = ( GetThemeTextDimensions != (void*) kUnresolvedCFragSymbolAddress ) ;
1639 if ( ((wxFont*)&m_font)->GetNoAntiAliasing() )
1640 useGetThemeText = false ;
1641 #endif
1642 char text[] = "H" ;
1643 #if TARGET_CARBON
1644 if ( useGetThemeText )
1645 {
1646 Point bounds={0,0} ;
1647 SInt16 baseline ;
1648 CFStringRef mString = CFStringCreateWithBytes( NULL , (UInt8*) text , 1 , CFStringGetSystemEncoding(), false ) ;
1649 ::GetThemeTextDimensions( mString,
1650 kThemeCurrentPortFont,
1651 kThemeStateActive,
1652 false,
1653 &bounds,
1654 &baseline );
1655 CFRelease( mString ) ;
1656 width = bounds.h ;
1657 }
1658 else
1659 #endif
1660 {
1661 width = ::TextWidth( text , 0 , 1 ) ;
1662 }
1663 return YDEV2LOGREL(width) ;
1664 }
1665
1666 wxCoord wxDC::GetCharHeight(void) const
1667 {
1668 wxCHECK_MSG(Ok(), 1, wxT("Invalid DC"));
1669 wxMacPortSetter helper(this) ;
1670 MacInstallFont() ;
1671 FontInfo fi ;
1672 ::GetFontInfo( &fi ) ;
1673 return YDEV2LOGREL( fi.descent + fi.ascent );
1674 }
1675
1676 void wxDC::Clear(void)
1677 {
1678 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1679 wxMacPortSetter helper(this) ;
1680 Rect rect = { -31000 , -31000 , 31000 , 31000 } ;
1681 if (m_backgroundBrush.GetStyle() != wxTRANSPARENT)
1682 {
1683 ::PenNormal() ;
1684 //MacInstallBrush() ;
1685 MacSetupBackgroundForCurrentPort( m_backgroundBrush ) ;
1686 ::EraseRect( &rect ) ;
1687 }
1688 }
1689
1690 void wxDC::MacInstallFont() const
1691 {
1692 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1693 // if ( m_macFontInstalled )
1694 // return ;
1695 Pattern blackColor ;
1696 MacSetupBackgroundForCurrentPort(m_backgroundBrush) ;
1697 wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ;
1698 if ( font )
1699 {
1700 ::TextFont( font->m_macFontNum ) ;
1701 ::TextSize( short(m_scaleY * font->m_macFontSize) ) ;
1702 ::TextFace( font->m_macFontStyle ) ;
1703 m_macFontInstalled = true ;
1704 m_macBrushInstalled = false ;
1705 m_macPenInstalled = false ;
1706 RGBColor forecolor = MAC_WXCOLORREF( m_textForegroundColour.GetPixel());
1707 RGBColor backcolor = MAC_WXCOLORREF( m_textBackgroundColour.GetPixel());
1708 ::RGBForeColor( &forecolor );
1709 ::RGBBackColor( &backcolor );
1710 }
1711 else
1712 {
1713 FontFamilyID fontId ;
1714 Str255 fontName ;
1715 SInt16 fontSize ;
1716 Style fontStyle ;
1717 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
1718 GetFNum( fontName, &fontId );
1719 ::TextFont( fontId ) ;
1720 ::TextSize( short(m_scaleY * fontSize) ) ;
1721 ::TextFace( fontStyle ) ;
1722 // todo reset after spacing changes - or store the current spacing somewhere
1723 m_macFontInstalled = true ;
1724 m_macBrushInstalled = false ;
1725 m_macPenInstalled = false ;
1726 RGBColor forecolor = MAC_WXCOLORREF( m_textForegroundColour.GetPixel());
1727 RGBColor backcolor = MAC_WXCOLORREF( m_textBackgroundColour.GetPixel());
1728 ::RGBForeColor( &forecolor );
1729 ::RGBBackColor( &backcolor );
1730 }
1731 short mode = patCopy ;
1732 // todo :
1733 switch( m_logicalFunction )
1734 {
1735 case wxCOPY: // src
1736 mode = patCopy ;
1737 break ;
1738 case wxINVERT: // NOT dst
1739 ::PenPat(GetQDGlobalsBlack(&blackColor));
1740 mode = patXor ;
1741 break ;
1742 case wxXOR: // src XOR dst
1743 mode = patXor ;
1744 break ;
1745 case wxOR_REVERSE: // src OR (NOT dst)
1746 mode = notPatOr ;
1747 break ;
1748 case wxSRC_INVERT: // (NOT src)
1749 mode = notPatCopy ;
1750 break ;
1751 case wxAND: // src AND dst
1752 mode = adMin ;
1753 break ;
1754 // unsupported TODO
1755 case wxCLEAR: // 0
1756 case wxAND_REVERSE:// src AND (NOT dst)
1757 case wxAND_INVERT: // (NOT src) AND dst
1758 case wxNO_OP: // dst
1759 case wxNOR: // (NOT src) AND (NOT dst)
1760 case wxEQUIV: // (NOT src) XOR dst
1761 case wxOR_INVERT: // (NOT src) OR dst
1762 case wxNAND: // (NOT src) OR (NOT dst)
1763 case wxOR: // src OR dst
1764 case wxSET: // 1
1765 // case wxSRC_OR: // source _bitmap_ OR destination
1766 // case wxSRC_AND: // source _bitmap_ AND destination
1767 break ;
1768 }
1769 ::PenMode( mode ) ;
1770 OSStatus status = noErr ;
1771 Fixed atsuSize = IntToFixed( int(m_scaleY * font->m_macFontSize) ) ;
1772 Style qdStyle = font->m_macFontStyle ;
1773 ATSUFontID atsuFont = font->m_macATSUFontID ;
1774 status = ::ATSUCreateStyle(&(ATSUStyle)m_macATSUIStyle) ;
1775 wxASSERT_MSG( status == noErr , "couldn't create ATSU style" ) ;
1776 ATSUAttributeTag atsuTags[] =
1777 {
1778 kATSUFontTag ,
1779 kATSUSizeTag ,
1780 // kATSUColorTag ,
1781 // kATSUBaselineClassTag ,
1782 kATSUVerticalCharacterTag,
1783 kATSUQDBoldfaceTag ,
1784 kATSUQDItalicTag ,
1785 kATSUQDUnderlineTag ,
1786 kATSUQDCondensedTag ,
1787 kATSUQDExtendedTag ,
1788 } ;
1789 ByteCount atsuSizes[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
1790 {
1791 sizeof( ATSUFontID ) ,
1792 sizeof( Fixed ) ,
1793 // sizeof( RGBColor ) ,
1794 // sizeof( BslnBaselineClass ) ,
1795 sizeof( ATSUVerticalCharacterType),
1796 sizeof( Boolean ) ,
1797 sizeof( Boolean ) ,
1798 sizeof( Boolean ) ,
1799 sizeof( Boolean ) ,
1800 sizeof( Boolean ) ,
1801 } ;
1802 Boolean kTrue = true ;
1803 Boolean kFalse = false ;
1804 BslnBaselineClass kBaselineDefault = kBSLNHangingBaseline ;
1805 ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
1806 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
1807 {
1808 &atsuFont ,
1809 &atsuSize ,
1810 // &MAC_WXCOLORREF( m_textForegroundColour.GetPixel() ) ,
1811 // &kBaselineDefault ,
1812 &kHorizontal,
1813 (qdStyle & bold) ? &kTrue : &kFalse ,
1814 (qdStyle & italic) ? &kTrue : &kFalse ,
1815 (qdStyle & underline) ? &kTrue : &kFalse ,
1816 (qdStyle & condense) ? &kTrue : &kFalse ,
1817 (qdStyle & extend) ? &kTrue : &kFalse ,
1818 } ;
1819 status = ::ATSUSetAttributes((ATSUStyle)m_macATSUIStyle, sizeof(atsuTags)/sizeof(ATSUAttributeTag),
1820 atsuTags, atsuSizes, atsuValues);
1821 wxASSERT_MSG( status == noErr , "couldn't set create ATSU style" ) ;
1822 }
1823
1824 Pattern gHatchPatterns[] =
1825 {
1826 { { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF } },
1827 { { 0x01 , 0x02 , 0x04 , 0x08 , 0x10 , 0x20 , 0x40 , 0x80 } },
1828 { { 0x80 , 0x40 , 0x20 , 0x10 , 0x08 , 0x04 , 0x02 , 0x01 } },
1829 { { 0x10 , 0x10 , 0x10 , 0xFF , 0x10 , 0x10 , 0x10 , 0x10 } },
1830 { { 0x00 , 0x00 , 0x00 , 0xFF , 0x00 , 0x00 , 0x00 , 0x00 } },
1831 { { 0x10 , 0x10 , 0x10 , 0x10 , 0x10 , 0x10 , 0x10 , 0x10 } },
1832 { { 0x81 , 0x42 , 0x24 , 0x18 , 0x18 , 0x24 , 0x42 , 0x81 } }
1833 } ;
1834
1835 static void wxMacGetHatchPattern(int hatchStyle, Pattern *pattern)
1836 {
1837 int theIndex = 1 ;
1838 switch(hatchStyle)
1839 {
1840 case wxBDIAGONAL_HATCH:
1841 theIndex = 2;
1842 break;
1843 case wxFDIAGONAL_HATCH:
1844 theIndex = 3;
1845 break;
1846 case wxCROSS_HATCH:
1847 theIndex = 4;
1848 break;
1849 case wxHORIZONTAL_HATCH:
1850 theIndex = 5;
1851 break;
1852 case wxVERTICAL_HATCH:
1853 theIndex = 6;
1854 break;
1855 case wxCROSSDIAG_HATCH:
1856 theIndex = 7;
1857 break;
1858 default:
1859 theIndex = 1; // solid pattern
1860 break;
1861 }
1862 *pattern = gHatchPatterns[theIndex-1] ;
1863 }
1864
1865 void wxDC::MacInstallPen() const
1866 {
1867 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1868 Pattern blackColor;
1869 // if ( m_macPenInstalled )
1870 // return ;
1871 RGBColor forecolor = MAC_WXCOLORREF( m_pen.GetColour().GetPixel());
1872 RGBColor backcolor = MAC_WXCOLORREF( m_backgroundBrush.GetColour().GetPixel());
1873 ::RGBForeColor( &forecolor );
1874 ::RGBBackColor( &backcolor );
1875 ::PenNormal() ;
1876 int penWidth = m_pen.GetWidth() * (int) m_scaleX ;
1877 // null means only one pixel, at whatever resolution
1878 if ( penWidth == 0 )
1879 penWidth = 1 ;
1880 ::PenSize(penWidth, penWidth);
1881 int penStyle = m_pen.GetStyle();
1882 if (penStyle == wxSOLID)
1883 {
1884 ::PenPat(GetQDGlobalsBlack(&blackColor));
1885 }
1886 else if (IS_HATCH(penStyle))
1887 {
1888 Pattern pat ;
1889 wxMacGetHatchPattern(penStyle, &pat);
1890 ::PenPat(&pat);
1891 }
1892 else
1893 {
1894 Pattern pat = *GetQDGlobalsBlack(&blackColor) ;
1895 switch( penStyle )
1896 {
1897 case wxDOT :
1898 for ( int i = 0 ; i < 8 ; ++i )
1899 {
1900 pat.pat[i] = 0xCC ;
1901 }
1902 break ;
1903 case wxLONG_DASH :
1904 for ( int i = 0 ; i < 8 ; ++i )
1905 {
1906 pat.pat[i] = 0xFE ;
1907 }
1908 break ;
1909 case wxSHORT_DASH :
1910 for ( int i = 0 ; i < 8 ; ++i )
1911 {
1912 pat.pat[i] = 0xEE ;
1913 }
1914 break ;
1915 case wxDOT_DASH :
1916 for ( int i = 0 ; i < 8 ; ++i )
1917 {
1918 pat.pat[i] = 0x6F ;
1919 }
1920 break ;
1921 case wxUSER_DASH :
1922 {
1923 wxDash* dash ;
1924 m_pen.GetDashes(&dash) ;
1925 // right now we don't allocate larger pixmaps
1926 // int number =
1927 m_pen.GetDashes(&dash) ;
1928 for ( int i = 0 ; i < 8 ; ++i )
1929 {
1930 pat.pat[i] = dash[0] ;
1931 }
1932 }
1933 break ;
1934 }
1935 ::PenPat(&pat);
1936 }
1937 short mode = patCopy ;
1938 // todo :
1939 switch( m_logicalFunction )
1940 {
1941 case wxCOPY: // only foreground color, leave background (thus not patCopy)
1942 mode = patOr ;
1943 break ;
1944 case wxINVERT: // NOT dst
1945 // ::PenPat(GetQDGlobalsBlack(&blackColor));
1946 mode = patXor ;
1947 break ;
1948 case wxXOR: // src XOR dst
1949 mode = patXor ;
1950 break ;
1951 case wxOR_REVERSE: // src OR (NOT dst)
1952 mode = notPatOr ;
1953 break ;
1954 case wxSRC_INVERT: // (NOT src)
1955 mode = notPatCopy ;
1956 break ;
1957 case wxAND: // src AND dst
1958 mode = adMin ;
1959 break ;
1960 // unsupported TODO
1961 case wxCLEAR: // 0
1962 case wxAND_REVERSE:// src AND (NOT dst)
1963 case wxAND_INVERT: // (NOT src) AND dst
1964 case wxNO_OP: // dst
1965 case wxNOR: // (NOT src) AND (NOT dst)
1966 case wxEQUIV: // (NOT src) XOR dst
1967 case wxOR_INVERT: // (NOT src) OR dst
1968 case wxNAND: // (NOT src) OR (NOT dst)
1969 case wxOR: // src OR dst
1970 case wxSET: // 1
1971 // case wxSRC_OR: // source _bitmap_ OR destination
1972 // case wxSRC_AND: // source _bitmap_ AND destination
1973 break ;
1974 }
1975 ::PenMode( mode ) ;
1976 m_macPenInstalled = true ;
1977 m_macBrushInstalled = false ;
1978 m_macFontInstalled = false ;
1979 }
1980
1981 void wxDC::MacSetupBackgroundForCurrentPort(const wxBrush& background )
1982 {
1983 Pattern whiteColor ;
1984 switch( background.MacGetBrushKind() )
1985 {
1986 case kwxMacBrushTheme :
1987 {
1988 ::SetThemeBackground( background.GetMacTheme() , wxDisplayDepth() , true ) ;
1989 break ;
1990 }
1991 case kwxMacBrushThemeBackground :
1992 {
1993 Rect extent ;
1994 ThemeBackgroundKind bg = background.GetMacThemeBackground( &extent ) ;
1995 ::ApplyThemeBackground( bg , &extent ,kThemeStateActive , wxDisplayDepth() , true ) ;
1996 break ;
1997 }
1998 case kwxMacBrushColour :
1999 {
2000 ::RGBBackColor( &MAC_WXCOLORREF( background.GetColour().GetPixel()) );
2001 int brushStyle = background.GetStyle();
2002 if (brushStyle == wxSOLID)
2003 ::BackPat(GetQDGlobalsWhite(&whiteColor));
2004 else if (IS_HATCH(brushStyle))
2005 {
2006 Pattern pat ;
2007 wxMacGetHatchPattern(brushStyle, &pat);
2008 ::BackPat(&pat);
2009 }
2010 else
2011 {
2012 ::BackPat(GetQDGlobalsWhite(&whiteColor));
2013 }
2014 break ;
2015 }
2016 }
2017 }
2018
2019 void wxDC::MacInstallBrush() const
2020 {
2021 wxCHECK_RET(Ok(), wxT("Invalid DC"));
2022 Pattern blackColor ;
2023 // if ( m_macBrushInstalled )
2024 // return ;
2025 // foreground
2026 bool backgroundTransparent = (GetBackgroundMode() == wxTRANSPARENT) ;
2027 ::RGBForeColor( &MAC_WXCOLORREF( m_brush.GetColour().GetPixel()) );
2028 ::RGBBackColor( &MAC_WXCOLORREF( m_backgroundBrush.GetColour().GetPixel()) );
2029 int brushStyle = m_brush.GetStyle();
2030 if (brushStyle == wxSOLID)
2031 {
2032 ::PenPat(GetQDGlobalsBlack(&blackColor));
2033 }
2034 else if (IS_HATCH(brushStyle))
2035 {
2036 Pattern pat ;
2037 wxMacGetHatchPattern(brushStyle, &pat);
2038 ::PenPat(&pat);
2039 }
2040 else if ( m_brush.GetStyle() == wxSTIPPLE || m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE )
2041 {
2042 // we force this in order to be compliant with wxMSW
2043 backgroundTransparent = false ;
2044 // for these the text fore (and back for MASK_OPAQUE) colors are used
2045 wxBitmap* bitmap = m_brush.GetStipple() ;
2046 int width = bitmap->GetWidth() ;
2047 int height = bitmap->GetHeight() ;
2048 GWorldPtr gw = NULL ;
2049 if ( m_brush.GetStyle() == wxSTIPPLE )
2050 gw = MAC_WXHBITMAP(bitmap->GetHBITMAP()) ;
2051 else
2052 gw = MAC_WXHBITMAP(bitmap->GetMask()->GetMaskBitmap()) ;
2053 PixMapHandle gwpixmaphandle = GetGWorldPixMap( gw ) ;
2054 LockPixels( gwpixmaphandle ) ;
2055 bool isMonochrome = !IsPortColor( gw ) ;
2056 if ( !isMonochrome )
2057 {
2058 if ( (**gwpixmaphandle).pixelSize == 1 )
2059 isMonochrome = true ;
2060 }
2061 if ( isMonochrome && width == 8 && height == 8 )
2062 {
2063 ::RGBForeColor( &MAC_WXCOLORREF( m_textForegroundColour.GetPixel()) );
2064 ::RGBForeColor( &MAC_WXCOLORREF( m_textBackgroundColour.GetPixel()) );
2065 BitMap* gwbitmap = (BitMap*) *gwpixmaphandle ; // since the color depth is 1 it is a BitMap
2066 UInt8 *gwbits = (UInt8*) gwbitmap->baseAddr ;
2067 int alignment = gwbitmap->rowBytes & 0x7FFF ;
2068 Pattern pat ;
2069 for ( int i = 0 ; i < 8 ; ++i )
2070 {
2071 pat.pat[i] = gwbits[i*alignment+0] ;
2072 }
2073 UnlockPixels( GetGWorldPixMap( gw ) ) ;
2074 ::PenPat( &pat ) ;
2075 }
2076 else
2077 {
2078 // this will be the code to handle power of 2 patterns, we will have to arrive at a nice
2079 // caching scheme before putting this into production
2080 Handle image;
2081 long imageSize;
2082 PixPatHandle pixpat = NewPixPat() ;
2083 CopyPixMap(gwpixmaphandle, (**pixpat).patMap);
2084 imageSize = GetPixRowBytes((**pixpat).patMap) *
2085 ((**(**pixpat).patMap).bounds.bottom -
2086 (**(**pixpat).patMap).bounds.top);
2087 PtrToHand( (**gwpixmaphandle).baseAddr, &image, imageSize );
2088 (**pixpat).patData = image;
2089 if ( isMonochrome )
2090 {
2091 CTabHandle ctable = ((**((**pixpat).patMap)).pmTable) ;
2092 ColorSpecPtr ctspec = (ColorSpecPtr) &(**ctable).ctTable ;
2093 if ( ctspec[0].rgb.red == 0x0000 )
2094 {
2095 ctspec[1].rgb = MAC_WXCOLORREF( m_textBackgroundColour.GetPixel()) ;
2096 ctspec[0].rgb = MAC_WXCOLORREF( m_textForegroundColour.GetPixel()) ;
2097 }
2098 else
2099 {
2100 ctspec[0].rgb = MAC_WXCOLORREF( m_textBackgroundColour.GetPixel()) ;
2101 ctspec[1].rgb = MAC_WXCOLORREF( m_textForegroundColour.GetPixel()) ;
2102 }
2103 ::CTabChanged( ctable ) ;
2104 }
2105 ::PenPixPat(pixpat);
2106 m_macForegroundPixMap = pixpat ;
2107 }
2108 UnlockPixels( gwpixmaphandle ) ;
2109 }
2110 else
2111 {
2112 ::PenPat(GetQDGlobalsBlack(&blackColor));
2113 }
2114 short mode = patCopy ;
2115 switch( m_logicalFunction )
2116 {
2117 case wxCOPY: // src
2118 if ( backgroundTransparent )
2119 mode = patOr ;
2120 else
2121 mode = patCopy ;
2122 break ;
2123 case wxINVERT: // NOT dst
2124 if ( !backgroundTransparent )
2125 {
2126 ::PenPat(GetQDGlobalsBlack(&blackColor));
2127 }
2128 mode = patXor ;
2129 break ;
2130 case wxXOR: // src XOR dst
2131 mode = patXor ;
2132 break ;
2133 case wxOR_REVERSE: // src OR (NOT dst)
2134 mode = notPatOr ;
2135 break ;
2136 case wxSRC_INVERT: // (NOT src)
2137 mode = notPatCopy ;
2138 break ;
2139 case wxAND: // src AND dst
2140 mode = adMin ;
2141 break ;
2142 // unsupported TODO
2143 case wxCLEAR: // 0
2144 case wxAND_REVERSE:// src AND (NOT dst)
2145 case wxAND_INVERT: // (NOT src) AND dst
2146 case wxNO_OP: // dst
2147 case wxNOR: // (NOT src) AND (NOT dst)
2148 case wxEQUIV: // (NOT src) XOR dst
2149 case wxOR_INVERT: // (NOT src) OR dst
2150 case wxNAND: // (NOT src) OR (NOT dst)
2151 case wxOR: // src OR dst
2152 case wxSET: // 1
2153 // case wxSRC_OR: // source _bitmap_ OR destination
2154 // case wxSRC_AND: // source _bitmap_ AND destination
2155 break ;
2156 }
2157 ::PenMode( mode ) ;
2158 m_macBrushInstalled = true ;
2159 m_macPenInstalled = false ;
2160 m_macFontInstalled = false ;
2161 }
2162
2163 // ---------------------------------------------------------------------------
2164 // coordinates transformations
2165 // ---------------------------------------------------------------------------
2166
2167 wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
2168 {
2169 return ((wxDC *)this)->XDEV2LOG(x);
2170 }
2171
2172 wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
2173 {
2174 return ((wxDC *)this)->YDEV2LOG(y);
2175 }
2176
2177 wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
2178 {
2179 return ((wxDC *)this)->XDEV2LOGREL(x);
2180 }
2181
2182 wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
2183 {
2184 return ((wxDC *)this)->YDEV2LOGREL(y);
2185 }
2186
2187 wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
2188 {
2189 return ((wxDC *)this)->XLOG2DEV(x);
2190 }
2191
2192 wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
2193 {
2194 return ((wxDC *)this)->YLOG2DEV(y);
2195 }
2196
2197 wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
2198 {
2199 return ((wxDC *)this)->XLOG2DEVREL(x);
2200 }
2201
2202 wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
2203 {
2204 return ((wxDC *)this)->YLOG2DEVREL(y);
2205 }