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