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