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