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