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