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