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