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