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