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