]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dc.cpp
776f63b21fcfd32a89ce320f1fe9cc739e79723a
[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_ok = false ;
285 m_macFontInstalled = false ;
286 m_macBrushInstalled = false ;
287 m_macPenInstalled = false ;
288 m_macLocalOrigin.x = m_macLocalOrigin.y = 0 ;
289 m_macBoundaryClipRgn = NewRgn() ;
290 m_macCurrentClipRgn = NewRgn() ;
291 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , -32000 , -32000 , 32000 , 32000 ) ;
292 SetRectRgn( (RgnHandle) m_macCurrentClipRgn , -32000 , -32000 , 32000 , 32000 ) ;
293 m_pen = *wxBLACK_PEN;
294 m_font = *wxNORMAL_FONT;
295 m_brush = *wxWHITE_BRUSH;
296
297 #ifdef __WXDEBUG__
298 // needed to debug possible errors with two active drawing methods at the same time on
299 // the same DC
300 m_macCurrentPortStateHelper = NULL ;
301 #endif
302
303 m_macATSUIStyle = NULL ;
304 m_macAliasWasEnabled = false;
305 m_macForegroundPixMap = NULL ;
306 m_macBackgroundPixMap = NULL ;
307 }
308
309 wxDC::~wxDC(void)
310 {
311 DisposeRgn( (RgnHandle) m_macBoundaryClipRgn ) ;
312 DisposeRgn( (RgnHandle) m_macCurrentClipRgn ) ;
313 }
314
315 void wxDC::MacSetupPort(wxMacPortStateHelper* help) const
316 {
317 #ifdef __WXDEBUG__
318 wxASSERT( m_macCurrentPortStateHelper == NULL ) ;
319 m_macCurrentPortStateHelper = help ;
320 #endif
321
322 SetClip( (RgnHandle) m_macCurrentClipRgn);
323
324 #if ! wxMAC_EXPERIMENTAL_DC
325 m_macFontInstalled = false ;
326 m_macBrushInstalled = false ;
327 m_macPenInstalled = false ;
328 #endif
329 }
330
331 void wxDC::MacCleanupPort(wxMacPortStateHelper* help) const
332 {
333 #ifdef __WXDEBUG__
334 wxASSERT( m_macCurrentPortStateHelper == help ) ;
335 m_macCurrentPortStateHelper = NULL ;
336 #endif
337
338 if ( m_macATSUIStyle )
339 {
340 ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
341 m_macATSUIStyle = NULL ;
342 }
343
344 if ( m_macAliasWasEnabled )
345 {
346 SetAntiAliasedTextEnabled(m_macFormerAliasState, m_macFormerAliasSize);
347 m_macAliasWasEnabled = false ;
348 }
349
350 if ( m_macForegroundPixMap )
351 {
352 Pattern blackColor ;
353 ::PenPat(GetQDGlobalsBlack(&blackColor));
354 DisposePixPat( (PixPatHandle) m_macForegroundPixMap ) ;
355 m_macForegroundPixMap = NULL ;
356 }
357
358 if ( m_macBackgroundPixMap )
359 {
360 Pattern whiteColor ;
361 ::BackPat(GetQDGlobalsWhite(&whiteColor));
362 DisposePixPat( (PixPatHandle) m_macBackgroundPixMap ) ;
363 m_macBackgroundPixMap = NULL ;
364 }
365 }
366
367 void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask )
368 {
369 wxCHECK_RET( Ok(), wxT("wxDC::DoDrawBitmap - invalid DC") );
370 wxCHECK_RET( bmp.Ok(), wxT("wxDC::DoDrawBitmap - invalid bitmap") );
371
372 wxMacFastPortSetter helper(this) ;
373 wxCoord xx = XLOG2DEVMAC(x);
374 wxCoord yy = YLOG2DEVMAC(y);
375 wxCoord w = bmp.GetWidth();
376 wxCoord h = bmp.GetHeight();
377 wxCoord ww = XLOG2DEVREL(w);
378 wxCoord hh = YLOG2DEVREL(h);
379
380 // Set up drawing mode
381 short mode = (m_logicalFunction == wxCOPY ? srcCopy :
382 //m_logicalFunction == wxCLEAR ? WHITENESS :
383 //m_logicalFunction == wxSET ? BLACKNESS :
384 m_logicalFunction == wxINVERT ? hilite :
385 //m_logicalFunction == wxAND ? MERGECOPY :
386 m_logicalFunction == wxOR ? srcOr :
387 m_logicalFunction == wxSRC_INVERT ? notSrcCopy :
388 m_logicalFunction == wxXOR ? srcXor :
389 m_logicalFunction == wxOR_REVERSE ? notSrcOr :
390 //m_logicalFunction == wxAND_REVERSE ? SRCERASE :
391 //m_logicalFunction == wxSRC_OR ? srcOr :
392 //m_logicalFunction == wxSRC_AND ? SRCAND :
393 srcCopy );
394
395 GWorldPtr maskworld = NULL ;
396 GWorldPtr bmapworld = MAC_WXHBITMAP( bmp.GetHBITMAP((WXHBITMAP*)&maskworld) );
397 PixMapHandle bmappixels ;
398
399 // Set foreground and background colours (for bitmaps depth = 1)
400 if (bmp.GetDepth() == 1)
401 {
402 RGBColor fore = MAC_WXCOLORREF(m_textForegroundColour.GetPixel());
403 RGBColor back = MAC_WXCOLORREF(m_textBackgroundColour.GetPixel());
404 RGBForeColor(&fore);
405 RGBBackColor(&back);
406 }
407 else
408 {
409 RGBColor white = { 0xFFFF, 0xFFFF, 0xFFFF} ;
410 RGBColor black = { 0, 0, 0} ;
411 RGBForeColor( &black ) ;
412 RGBBackColor( &white ) ;
413 }
414 bmappixels = GetGWorldPixMap( bmapworld ) ;
415
416 wxCHECK_RET(LockPixels(bmappixels),
417 wxT("wxDC::DoDrawBitmap - failed to lock pixels"));
418
419 Rect source = { 0, 0, h, w };
420 Rect dest = { yy, xx, yy + hh, xx + ww };
421 if ( useMask && maskworld )
422 {
423 if ( LockPixels(GetGWorldPixMap(MAC_WXHBITMAP(maskworld))))
424 {
425 CopyDeepMask
426 (
427 GetPortBitMapForCopyBits(bmapworld),
428 GetPortBitMapForCopyBits(MAC_WXHBITMAP(maskworld)),
429 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ),
430 &source, &source, &dest, mode, NULL
431 );
432 UnlockPixels(GetGWorldPixMap(MAC_WXHBITMAP(maskworld)));
433 }
434 }
435 else
436 {
437 CopyBits( GetPortBitMapForCopyBits( bmapworld ),
438 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ),
439 &source, &dest, mode, NULL ) ;
440 }
441 UnlockPixels( bmappixels ) ;
442
443 m_macPenInstalled = false ;
444 m_macBrushInstalled = false ;
445 m_macFontInstalled = false ;
446 }
447
448 void wxDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
449 {
450 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawIcon - invalid DC"));
451 wxCHECK_RET(icon.Ok(), wxT("wxDC::DoDrawIcon - invalid icon"));
452
453 wxMacFastPortSetter helper(this) ;
454
455 wxCoord xx = XLOG2DEVMAC(x);
456 wxCoord yy = YLOG2DEVMAC(y);
457 wxCoord w = icon.GetWidth();
458 wxCoord h = icon.GetHeight();
459 wxCoord ww = XLOG2DEVREL(w);
460 wxCoord hh = YLOG2DEVREL(h);
461
462 Rect r = { yy , xx, yy + hh , xx + ww } ;
463 PlotIconRef( &r , kAlignNone , kTransformNone , kPlotIconRefNormalFlags , MAC_WXHICON( icon.GetHICON() ) ) ;
464 }
465
466 void wxDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
467 {
468 wxCHECK_RET(Ok(), wxT("wxDC::DoSetClippingRegion - invalid DC"));
469
470 wxCoord xx, yy, ww, hh;
471 xx = XLOG2DEVMAC(x);
472 yy = YLOG2DEVMAC(y);
473 ww = XLOG2DEVREL(width);
474 hh = YLOG2DEVREL(height);
475
476 SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh ) ;
477 SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
478 if ( m_clipping )
479 {
480 m_clipX1 = wxMax( m_clipX1 , xx );
481 m_clipY1 = wxMax( m_clipY1 , yy );
482 m_clipX2 = wxMin( m_clipX2, (xx + ww));
483 m_clipY2 = wxMin( m_clipY2, (yy + hh));
484 }
485 else
486 {
487 m_clipping = true;
488 m_clipX1 = xx;
489 m_clipY1 = yy;
490 m_clipX2 = xx + ww;
491 m_clipY2 = yy + hh;
492 }
493 }
494
495 void wxDC::DoSetClippingRegionAsRegion( const wxRegion &region )
496 {
497 wxCHECK_RET(Ok(), wxT("wxDC::DoSetClippingRegionAsRegion - invalid DC"));
498
499 wxMacFastPortSetter helper(this) ;
500 wxCoord x, y, w, h;
501 region.GetBox( x, y, w, h );
502 wxCoord xx, yy, ww, hh;
503 xx = XLOG2DEVMAC(x);
504 yy = YLOG2DEVMAC(y);
505 ww = XLOG2DEVREL(w);
506 hh = YLOG2DEVREL(h);
507
508 // if we have a scaling that we cannot map onto native regions
509 // we must use the box
510 if ( ww != w || hh != h )
511 {
512 wxDC::DoSetClippingRegion( x, y, w, h );
513 }
514 else
515 {
516 CopyRgn( (RgnHandle) region.GetWXHRGN() , (RgnHandle) m_macCurrentClipRgn ) ;
517 if ( xx != x || yy != y )
518 OffsetRgn( (RgnHandle) m_macCurrentClipRgn , xx - x , yy - y ) ;
519
520 SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
521 if ( m_clipping )
522 {
523 m_clipX1 = wxMax( m_clipX1 , xx );
524 m_clipY1 = wxMax( m_clipY1 , yy );
525 m_clipX2 = wxMin( m_clipX2, (xx + ww));
526 m_clipY2 = wxMin( m_clipY2, (yy + hh));
527 }
528 else
529 {
530 m_clipping = true;
531 m_clipX1 = xx;
532 m_clipY1 = yy;
533 m_clipX2 = xx + ww;
534 m_clipY2 = yy + hh;
535 }
536 }
537 }
538
539 void wxDC::DestroyClippingRegion()
540 {
541 wxMacFastPortSetter helper(this) ;
542
543 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
544 ResetClipping();
545 }
546
547 void wxDC::DoGetSizeMM( int* width, int* height ) const
548 {
549 int w = 0, h = 0;
550
551 GetSize( &w, &h );
552 if (width)
553 *width = long( double(w) / (m_scaleX * m_mm_to_pix_x) );
554 if (height)
555 *height = long( double(h) / (m_scaleY * m_mm_to_pix_y) );
556 }
557
558 void wxDC::SetTextForeground( const wxColour &col )
559 {
560 wxCHECK_RET(Ok(), wxT("wxDC::SetTextForeground - invalid DC"));
561
562 m_textForegroundColour = col;
563 m_macFontInstalled = false ;
564 }
565
566 void wxDC::SetTextBackground( const wxColour &col )
567 {
568 wxCHECK_RET(Ok(), wxT("wxDC::SetTextBackground - invalid DC"));
569
570 m_textBackgroundColour = col;
571 m_macFontInstalled = false ;
572 }
573
574 void wxDC::SetMapMode( int mode )
575 {
576 switch (mode)
577 {
578 case wxMM_TWIPS:
579 SetLogicalScale( twips2mm * m_mm_to_pix_x, twips2mm * m_mm_to_pix_y );
580 break;
581
582 case wxMM_POINTS:
583 SetLogicalScale( pt2mm * m_mm_to_pix_x, pt2mm * m_mm_to_pix_y );
584 break;
585
586 case wxMM_METRIC:
587 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
588 break;
589
590 case wxMM_LOMETRIC:
591 SetLogicalScale( m_mm_to_pix_x / 10.0, m_mm_to_pix_y / 10.0 );
592 break;
593
594 default:
595 case wxMM_TEXT:
596 SetLogicalScale( 1.0, 1.0 );
597 break;
598 }
599
600 if (mode != wxMM_TEXT)
601 {
602 m_needComputeScaleX = true;
603 m_needComputeScaleY = true;
604 }
605 }
606
607 void wxDC::SetUserScale( double x, double y )
608 {
609 // allow negative ? -> no
610 m_userScaleX = x;
611 m_userScaleY = y;
612 ComputeScaleAndOrigin();
613 }
614
615 void wxDC::SetLogicalScale( double x, double y )
616 {
617 // allow negative ?
618 m_logicalScaleX = x;
619 m_logicalScaleY = y;
620 ComputeScaleAndOrigin();
621 }
622
623 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
624 {
625 // is this still correct ?
626 m_logicalOriginX = x * m_signX;
627 m_logicalOriginY = y * m_signY;
628 ComputeScaleAndOrigin();
629 }
630
631 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
632 {
633 m_externalDeviceOriginX = x;
634 m_externalDeviceOriginY = y;
635 ComputeScaleAndOrigin();
636 }
637
638 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
639 {
640 m_signX = (xLeftRight ? 1 : -1);
641 m_signY = (yBottomUp ? -1 : 1);
642 ComputeScaleAndOrigin();
643 }
644
645 wxSize wxDC::GetPPI() const
646 {
647 return wxSize(72, 72);
648 }
649
650 int wxDC::GetDepth() const
651 {
652 if ( IsPortColor( (CGrafPtr) m_macPort ) )
653 return ( (**GetPortPixMap( (CGrafPtr) m_macPort)).pixelSize ) ;
654
655 return 1 ;
656 }
657
658 void wxDC::ComputeScaleAndOrigin()
659 {
660 // CMB: copy scale to see if it changes
661 double origScaleX = m_scaleX;
662 double origScaleY = m_scaleY;
663 m_scaleX = m_logicalScaleX * m_userScaleX;
664 m_scaleY = m_logicalScaleY * m_userScaleY;
665 m_deviceOriginX = m_internalDeviceOriginX + m_externalDeviceOriginX;
666 m_deviceOriginY = m_internalDeviceOriginY + m_externalDeviceOriginY;
667
668 // CMB: if scale has changed call SetPen to recalulate the line width
669 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
670 {
671 // this is a bit artificial, but we need to force wxDC to think
672 // the pen has changed
673 wxPen pen(GetPen());
674 m_pen = wxNullPen;
675 SetPen(pen);
676 }
677 }
678
679 void wxDC::SetPalette( const wxPalette& palette )
680 {
681 }
682
683 void wxDC::SetBackgroundMode( int mode )
684 {
685 m_backgroundMode = mode ;
686 }
687
688 void wxDC::SetFont( const wxFont &font )
689 {
690 m_font = font;
691 m_macFontInstalled = false ;
692 }
693
694 void wxDC::SetPen( const wxPen &pen )
695 {
696 if ( m_pen == pen )
697 return ;
698
699 m_pen = pen;
700 m_macPenInstalled = false ;
701 }
702
703 void wxDC::SetBrush( const wxBrush &brush )
704 {
705 if (m_brush == brush)
706 return;
707
708 m_brush = brush;
709 m_macBrushInstalled = false ;
710 }
711
712 void wxDC::SetBackground( const wxBrush &brush )
713 {
714 if (m_backgroundBrush == brush)
715 return;
716
717 m_backgroundBrush = brush;
718 if (m_backgroundBrush.Ok())
719 m_macBrushInstalled = false ;
720 }
721
722 void wxDC::SetLogicalFunction( int function )
723 {
724 if (m_logicalFunction == function)
725 return;
726
727 m_logicalFunction = function ;
728 m_macFontInstalled = false ;
729 m_macBrushInstalled = false ;
730 m_macPenInstalled = false ;
731 }
732
733 extern bool wxDoFloodFill(wxDC *dc, wxCoord x, wxCoord y,
734 const wxColour & col, int style);
735
736 bool wxDC::DoFloodFill(wxCoord x, wxCoord y,
737 const wxColour& col, int style)
738 {
739 return wxDoFloodFill(this, x, y, col, style);
740 }
741
742 bool wxDC::DoGetPixel( wxCoord x, wxCoord y, wxColour *col ) const
743 {
744 wxCHECK_MSG( Ok(), false, wxT("wxDC::DoGetPixel - invalid DC") );
745
746 wxMacFastPortSetter helper(this) ;
747
748 // NOTE: Get/SetCPixel are slow!
749 RGBColor colour;
750 GetCPixel( XLOG2DEVMAC(x), YLOG2DEVMAC(y), &colour );
751
752 // convert from Mac colour to wx
753 col->Set( colour.red >> 8,
754 colour.green >> 8,
755 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 wxCoord x1, x2 , y1 , y2 ;
990 x1 = XLOG2DEVMAC(points[0].x + xoffset);
991 y1 = YLOG2DEVMAC(points[0].y + yoffset);
992
993 ::MoveTo(x1 - offset, y1 - offset );
994 for (int i = 0; i < n-1; i++)
995 {
996 x2 = XLOG2DEVMAC(points[i + 1].x + xoffset);
997 y2 = YLOG2DEVMAC(points[i + 1].y + yoffset);
998 ::LineTo( x2 - offset, y2 - offset );
999 }
1000 }
1001
1002 void wxDC::DoDrawPolygon(int n, wxPoint points[],
1003 wxCoord xoffset, wxCoord yoffset,
1004 int fillStyle )
1005 {
1006 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawPolygon - invalid DC"));
1007
1008 if ( m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT )
1009 return ;
1010
1011 wxMacFastPortSetter helper(this) ;
1012
1013 wxCoord x1, x2 , y1 , y2 ;
1014 PolyHandle polygon = OpenPoly();
1015 x2 = x1 = XLOG2DEVMAC(points[0].x + xoffset);
1016 y2 = y1 = YLOG2DEVMAC(points[0].y + yoffset);
1017
1018 ::MoveTo(x1, y1);
1019 for (int i = 1; i < n; i++)
1020 {
1021 x2 = XLOG2DEVMAC(points[i].x + xoffset);
1022 y2 = YLOG2DEVMAC(points[i].y + yoffset);
1023 ::LineTo(x2, y2);
1024 }
1025
1026 // close the polyline if necessary
1027 if ( x1 != x2 || y1 != y2 )
1028 ::LineTo(x1, y1 ) ;
1029 ClosePoly();
1030
1031 if (m_brush.GetStyle() != wxTRANSPARENT)
1032 {
1033 MacInstallBrush();
1034 ::PaintPoly( polygon );
1035 }
1036
1037 if (m_pen.GetStyle() != wxTRANSPARENT)
1038 {
1039 MacInstallPen() ;
1040 ::FramePoly( polygon ) ;
1041 }
1042
1043 KillPoly( polygon );
1044 }
1045
1046 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1047 {
1048 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawRectangle - invalid DC"));
1049
1050 wxMacFastPortSetter helper(this) ;
1051
1052 wxCoord xx = XLOG2DEVMAC(x);
1053 wxCoord yy = YLOG2DEVMAC(y);
1054 wxCoord ww = m_signX * XLOG2DEVREL(width);
1055 wxCoord hh = m_signY * YLOG2DEVREL(height);
1056
1057 // CMB: draw nothing if transformed w or h is 0
1058 if (ww == 0 || hh == 0)
1059 return;
1060
1061 // CMB: handle -ve width and/or height
1062 if (ww < 0)
1063 {
1064 ww = -ww;
1065 xx = xx - ww;
1066 }
1067
1068 if (hh < 0)
1069 {
1070 hh = -hh;
1071 yy = yy - hh;
1072 }
1073
1074 Rect rect = { yy , xx , yy + hh , xx + ww } ;
1075
1076 if (m_brush.GetStyle() != wxTRANSPARENT)
1077 {
1078 MacInstallBrush() ;
1079 ::PaintRect( &rect ) ;
1080 }
1081
1082 if (m_pen.GetStyle() != wxTRANSPARENT)
1083 {
1084 MacInstallPen() ;
1085 ::FrameRect( &rect ) ;
1086 }
1087 }
1088
1089 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
1090 wxCoord width, wxCoord height,
1091 double radius)
1092 {
1093 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawRoundedRectangle - invalid DC"));
1094
1095 wxMacFastPortSetter helper(this) ;
1096 if (radius < 0.0)
1097 radius = - radius * ((width < height) ? width : height);
1098 wxCoord xx = XLOG2DEVMAC(x);
1099 wxCoord yy = YLOG2DEVMAC(y);
1100 wxCoord ww = m_signX * XLOG2DEVREL(width);
1101 wxCoord hh = m_signY * YLOG2DEVREL(height);
1102
1103 // CMB: draw nothing if transformed w or h is 0
1104 if (ww == 0 || hh == 0)
1105 return;
1106
1107 // CMB: handle -ve width and/or height
1108 if (ww < 0)
1109 {
1110 ww = -ww;
1111 xx = xx - ww;
1112 }
1113
1114 if (hh < 0)
1115 {
1116 hh = -hh;
1117 yy = yy - hh;
1118 }
1119
1120 Rect rect = { yy , xx , yy + hh , xx + ww } ;
1121
1122 if (m_brush.GetStyle() != wxTRANSPARENT)
1123 {
1124 MacInstallBrush() ;
1125 ::PaintRoundRect( &rect , int(radius * 2) , int(radius * 2) ) ;
1126 }
1127
1128 if (m_pen.GetStyle() != wxTRANSPARENT)
1129 {
1130 MacInstallPen() ;
1131 ::FrameRoundRect( &rect , int(radius * 2) , int(radius * 2) ) ;
1132 }
1133 }
1134
1135 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1136 {
1137 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawEllipse - invalid DC"));
1138
1139 wxMacFastPortSetter helper(this) ;
1140
1141 wxCoord xx = XLOG2DEVMAC(x);
1142 wxCoord yy = YLOG2DEVMAC(y);
1143 wxCoord ww = m_signX * XLOG2DEVREL(width);
1144 wxCoord hh = m_signY * YLOG2DEVREL(height);
1145
1146 // CMB: draw nothing if transformed w or h is 0
1147 if (ww == 0 || hh == 0)
1148 return;
1149
1150 // CMB: handle -ve width and/or height
1151 if (ww < 0)
1152 {
1153 ww = -ww;
1154 xx = xx - ww;
1155 }
1156
1157 if (hh < 0)
1158 {
1159 hh = -hh;
1160 yy = yy - hh;
1161 }
1162
1163 Rect rect = { yy , xx , yy + hh , xx + ww } ;
1164
1165 if (m_brush.GetStyle() != wxTRANSPARENT)
1166 {
1167 MacInstallBrush() ;
1168 ::PaintOval( &rect ) ;
1169 }
1170
1171 if (m_pen.GetStyle() != wxTRANSPARENT)
1172 {
1173 MacInstallPen() ;
1174 ::FrameOval( &rect ) ;
1175 }
1176 }
1177
1178 bool wxDC::CanDrawBitmap(void) const
1179 {
1180 return true ;
1181 }
1182
1183 bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
1184 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool useMask,
1185 wxCoord xsrcMask, wxCoord ysrcMask )
1186 {
1187 wxCHECK_MSG(Ok(), false, wxT("wxDC::DoBlit - invalid DC"));
1188 wxCHECK_MSG(source->Ok(), false, wxT("wxDC::DoBlit - invalid source DC"));
1189
1190 if ( logical_func == wxNO_OP )
1191 return true ;
1192
1193 if (xsrcMask == -1 && ysrcMask == -1)
1194 {
1195 xsrcMask = xsrc;
1196 ysrcMask = ysrc;
1197 }
1198
1199 // correct the parameter in case this dc does not have a mask at all
1200 if ( useMask && !source->m_macMask )
1201 useMask = false ;
1202
1203 Rect srcrect , dstrect ;
1204 srcrect.top = source->YLOG2DEVMAC(ysrc) ;
1205 srcrect.left = source->XLOG2DEVMAC(xsrc) ;
1206 srcrect.right = source->XLOG2DEVMAC(xsrc + width ) ;
1207 srcrect.bottom = source->YLOG2DEVMAC(ysrc + height) ;
1208 dstrect.top = YLOG2DEVMAC(ydest) ;
1209 dstrect.left = XLOG2DEVMAC(xdest) ;
1210 dstrect.bottom = YLOG2DEVMAC(ydest + height ) ;
1211 dstrect.right = XLOG2DEVMAC(xdest + width ) ;
1212 short mode = kUnsupportedMode ;
1213 bool invertDestinationFirst = false ;
1214
1215 switch ( logical_func )
1216 {
1217 case wxAND: // src AND dst
1218 mode = adMin ; // ok
1219 break ;
1220
1221 case wxAND_INVERT: // (NOT src) AND dst
1222 mode = notSrcOr ; // ok
1223 break ;
1224
1225 case wxAND_REVERSE:// src AND (NOT dst)
1226 invertDestinationFirst = true ;
1227 mode = srcOr ;
1228 break ;
1229
1230 case wxCLEAR: // 0
1231 mode = kEmulatedMode ;
1232 break ;
1233
1234 case wxCOPY: // src
1235 mode = srcCopy ; // ok
1236 break ;
1237
1238 case wxEQUIV: // (NOT src) XOR dst
1239 mode = srcXor ; // ok
1240 break ;
1241
1242 case wxINVERT: // NOT dst
1243 mode = kEmulatedMode ; //or hilite ;
1244 break ;
1245
1246 case wxNAND: // (NOT src) OR (NOT dst)
1247 invertDestinationFirst = true ;
1248 mode = srcBic ;
1249 break ;
1250
1251 case wxNOR: // (NOT src) AND (NOT dst)
1252 invertDestinationFirst = true ;
1253 mode = notSrcOr ;
1254 break ;
1255
1256 case wxNO_OP: // dst
1257 mode = kEmulatedMode ; // this has already been handled upon entry
1258 break ;
1259
1260 case wxOR: // src OR dst
1261 mode = notSrcBic ;
1262 break ;
1263
1264 case wxOR_INVERT: // (NOT src) OR dst
1265 mode = srcBic ;
1266 break ;
1267
1268 case wxOR_REVERSE: // src OR (NOT dst)
1269 invertDestinationFirst = true ;
1270 mode = notSrcBic ;
1271 break ;
1272
1273 case wxSET: // 1
1274 mode = kEmulatedMode ;
1275 break ;
1276
1277 case wxSRC_INVERT: // (NOT src)
1278 mode = notSrcCopy ; // ok
1279 break ;
1280
1281 case wxXOR: // src XOR dst
1282 mode = notSrcXor ; // ok
1283 break ;
1284
1285 default :
1286 break ;
1287 }
1288
1289 if ( mode == kUnsupportedMode )
1290 {
1291 wxFAIL_MSG(wxT("unsupported blitting mode" ));
1292
1293 return false ;
1294 }
1295
1296 CGrafPtr sourcePort = (CGrafPtr) source->m_macPort ;
1297 PixMapHandle bmappixels = GetGWorldPixMap( sourcePort ) ;
1298 if ( LockPixels(bmappixels) )
1299 {
1300 wxMacFastPortSetter helper(this) ;
1301
1302 if ( source->GetDepth() == 1 )
1303 {
1304 RGBForeColor( &MAC_WXCOLORREF(m_textForegroundColour.GetPixel()) ) ;
1305 RGBBackColor( &MAC_WXCOLORREF(m_textBackgroundColour.GetPixel()) ) ;
1306 }
1307 else
1308 {
1309 // the modes need this, otherwise we'll end up having really nice colors...
1310 RGBColor white = { 0xFFFF, 0xFFFF, 0xFFFF} ;
1311 RGBColor black = { 0, 0, 0} ;
1312
1313 RGBForeColor( &black ) ;
1314 RGBBackColor( &white ) ;
1315 }
1316
1317 if ( useMask && source->m_macMask )
1318 {
1319 if ( mode == srcCopy )
1320 {
1321 if ( LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) )
1322 {
1323 CopyMask( GetPortBitMapForCopyBits( sourcePort ) ,
1324 GetPortBitMapForCopyBits( MAC_WXHBITMAP(source->m_macMask) ) ,
1325 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ) ,
1326 &srcrect, &srcrect , &dstrect ) ;
1327 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) ;
1328 }
1329 }
1330 else
1331 {
1332 RgnHandle clipRgn = NewRgn() ;
1333 LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) ;
1334 BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) ;
1335 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) ;
1336 OffsetRgn( clipRgn , -srcrect.left + dstrect.left, -srcrect.top + dstrect.top ) ;
1337
1338 if ( mode == kEmulatedMode )
1339 {
1340 Pattern pat ;
1341
1342 ::PenPat(GetQDGlobalsBlack(&pat));
1343 if ( logical_func == wxSET )
1344 {
1345 RGBColor col= { 0xFFFF, 0xFFFF, 0xFFFF } ;
1346 ::RGBForeColor( &col ) ;
1347 ::PaintRgn( clipRgn ) ;
1348 }
1349 else if ( logical_func == wxCLEAR )
1350 {
1351 RGBColor col= { 0x0000, 0x0000, 0x0000 } ;
1352 ::RGBForeColor( &col ) ;
1353 ::PaintRgn( clipRgn ) ;
1354 }
1355 else if ( logical_func == wxINVERT )
1356 {
1357 MacInvertRgn( clipRgn ) ;
1358 }
1359 else
1360 {
1361 for ( int y = 0 ; y < srcrect.right - srcrect.left ; ++y )
1362 {
1363 for ( int x = 0 ; x < srcrect.bottom - srcrect.top ; ++x )
1364 {
1365 Point dstPoint = { dstrect.top + y , dstrect.left + x } ;
1366 Point srcPoint = { srcrect.top + y , srcrect.left + x } ;
1367 if ( PtInRgn( dstPoint , clipRgn ) )
1368 {
1369 RGBColor srcColor, dstColor ;
1370
1371 // NOTE: Get/SetCPixel are slow!
1372 SetPort( (GrafPtr) sourcePort ) ;
1373 GetCPixel( srcPoint.h , srcPoint.v , &srcColor ) ;
1374 SetPort( (GrafPtr) m_macPort ) ;
1375 GetCPixel( dstPoint.h , dstPoint.v , &dstColor ) ;
1376 wxMacCalculateColour( logical_func , srcColor , dstColor ) ;
1377 SetCPixel( dstPoint.h , dstPoint.v , &dstColor ) ;
1378 }
1379 }
1380 }
1381 }
1382 }
1383 else
1384 {
1385 if ( invertDestinationFirst )
1386 MacInvertRgn( clipRgn ) ;
1387
1388 CopyBits( GetPortBitMapForCopyBits( sourcePort ) ,
1389 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ) ,
1390 &srcrect, &dstrect, mode, clipRgn ) ;
1391 }
1392
1393 DisposeRgn( clipRgn ) ;
1394 }
1395 }
1396 else
1397 {
1398 RgnHandle clipRgn = NewRgn() ;
1399 SetRectRgn( clipRgn , dstrect.left , dstrect.top , dstrect.right , dstrect.bottom ) ;
1400
1401 if ( mode == kEmulatedMode )
1402 {
1403 Pattern pat ;
1404
1405 ::PenPat(GetQDGlobalsBlack(&pat));
1406 if ( logical_func == wxSET )
1407 {
1408 RGBColor col= { 0xFFFF, 0xFFFF, 0xFFFF } ;
1409 ::RGBForeColor( &col ) ;
1410 ::PaintRgn( clipRgn ) ;
1411 }
1412 else if ( logical_func == wxCLEAR )
1413 {
1414 RGBColor col = { 0x0000, 0x0000, 0x0000 } ;
1415
1416 ::RGBForeColor( &col ) ;
1417 ::PaintRgn( clipRgn ) ;
1418 }
1419 else if ( logical_func == wxINVERT )
1420 {
1421 MacInvertRgn( clipRgn ) ;
1422 }
1423 else
1424 {
1425 for ( int y = 0 ; y < srcrect.right - srcrect.left ; ++y )
1426 {
1427 for ( int x = 0 ; x < srcrect.bottom - srcrect.top ; ++x )
1428 {
1429 Point dstPoint = { dstrect.top + y , dstrect.left + x } ;
1430 Point srcPoint = { srcrect.top + y , srcrect.left + x } ;
1431 {
1432 RGBColor srcColor, dstColor ;
1433
1434 // NOTE: Get/SetCPixel are slow!
1435 SetPort( (GrafPtr) sourcePort ) ;
1436 GetCPixel( srcPoint.h , srcPoint.v , &srcColor) ;
1437 SetPort( (GrafPtr) m_macPort ) ;
1438 GetCPixel( dstPoint.h , dstPoint.v , &dstColor ) ;
1439 wxMacCalculateColour( logical_func , srcColor , dstColor ) ;
1440 SetCPixel( dstPoint.h , dstPoint.v , &dstColor ) ;
1441 }
1442 }
1443 }
1444 }
1445 }
1446 else
1447 {
1448 if ( invertDestinationFirst )
1449 MacInvertRgn( clipRgn ) ;
1450
1451 CopyBits( GetPortBitMapForCopyBits( sourcePort ) ,
1452 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ) ,
1453 &srcrect, &dstrect, mode, NULL ) ;
1454 }
1455
1456 DisposeRgn( clipRgn ) ;
1457 }
1458
1459 UnlockPixels( bmappixels ) ;
1460 }
1461
1462 m_macPenInstalled = false ;
1463 m_macBrushInstalled = false ;
1464 m_macFontInstalled = false ;
1465
1466 return true;
1467 }
1468
1469 void wxDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y,
1470 double angle)
1471 {
1472 // TODO: support text background color (only possible by hand, ATSUI does not support it)
1473 wxCHECK_RET( Ok(), wxT("wxDC::DoDrawRotatedText - invalid DC") );
1474
1475 if ( str.Length() == 0 )
1476 return ;
1477
1478 wxMacFastPortSetter helper(this) ;
1479 MacInstallFont() ;
1480
1481 #if 0
1482 if ( 0 )
1483 {
1484 m_macFormerAliasState = IsAntiAliasedTextEnabled(&m_macFormerAliasSize);
1485 SetAntiAliasedTextEnabled(true, SInt16(m_scaleY * m_font.MacGetFontSize()));
1486 m_macAliasWasEnabled = true ;
1487 }
1488 #endif
1489
1490 OSStatus status = noErr ;
1491 ATSUTextLayout atsuLayout ;
1492 UniCharCount chars = str.Length() ;
1493 UniChar* ubuf = NULL ;
1494
1495 #if SIZEOF_WCHAR_T == 4
1496 wxMBConvUTF16 converter ;
1497 #if wxUSE_UNICODE
1498 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ;
1499 ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
1500 converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 ) ;
1501 #else
1502 const wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
1503 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ;
1504 ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
1505 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ) ;
1506 #endif
1507 chars = unicharlen / 2 ;
1508 #else
1509 #if wxUSE_UNICODE
1510 ubuf = (UniChar*) str.wc_str() ;
1511 #else
1512 wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
1513 chars = wxWcslen( wchar.data() ) ;
1514 ubuf = (UniChar*) wchar.data() ;
1515 #endif
1516 #endif
1517
1518 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1519 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ;
1520
1521 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") );
1522
1523 status = ::ATSUSetTransientFontMatching( atsuLayout , true ) ;
1524 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
1525
1526 int iAngle = int( angle );
1527 int drawX = XLOG2DEVMAC(x) ;
1528 int drawY = YLOG2DEVMAC(y) ;
1529
1530 ATSUTextMeasurement textBefore, textAfter ;
1531 ATSUTextMeasurement ascent, descent ;
1532
1533 if ( abs(iAngle) > 0 )
1534 {
1535 Fixed atsuAngle = IntToFixed( iAngle ) ;
1536
1537 ATSUAttributeTag atsuTags[] =
1538 {
1539 kATSULineRotationTag ,
1540 } ;
1541 ByteCount atsuSizes[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
1542 {
1543 sizeof( Fixed ) ,
1544 } ;
1545 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
1546 {
1547 &atsuAngle ,
1548 } ;
1549
1550 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags)/sizeof(ATSUAttributeTag),
1551 atsuTags, atsuSizes, atsuValues ) ;
1552 }
1553
1554 status = ::ATSUMeasureText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1555 &textBefore , &textAfter, &ascent , &descent );
1556
1557 drawX += (int)(sin(angle/RAD2DEG) * FixedToInt(ascent));
1558 drawY += (int)(cos(angle/RAD2DEG) * FixedToInt(ascent));
1559 status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1560 IntToFixed(drawX) , IntToFixed(drawY) );
1561
1562 wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") );
1563
1564 Rect rect ;
1565 status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1566 IntToFixed(drawX) , IntToFixed(drawY) , &rect );
1567 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
1568 OffsetRect( &rect , -m_macLocalOrigin.x , -m_macLocalOrigin.y ) ;
1569 CalcBoundingBox(XDEV2LOG(rect.left), YDEV2LOG(rect.top) );
1570 CalcBoundingBox(XDEV2LOG(rect.right), YDEV2LOG(rect.bottom) );
1571 ::ATSUDisposeTextLayout(atsuLayout);
1572
1573 #if SIZEOF_WCHAR_T == 4
1574 free( ubuf ) ;
1575 #endif
1576 }
1577
1578 void wxDC::DoDrawText(const wxString& strtext, wxCoord x, wxCoord y)
1579 {
1580 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawText - invalid DC"));
1581
1582 wxMacFastPortSetter helper(this) ;
1583 long xx = XLOG2DEVMAC(x);
1584 long yy = YLOG2DEVMAC(y);
1585
1586 #if TARGET_CARBON
1587 bool useDrawThemeText = ( DrawThemeTextBox != (void*) kUnresolvedCFragSymbolAddress ) ;
1588 if ( UMAGetSystemVersion() < 0x1000 || IsKindOf(CLASSINFO( wxPrinterDC ) ) || m_font.GetNoAntiAliasing() )
1589 useDrawThemeText = false ;
1590 #endif
1591
1592 MacInstallFont() ;
1593
1594 FontInfo fi ;
1595 ::GetFontInfo( &fi ) ;
1596
1597 #if TARGET_CARBON
1598 if ( !useDrawThemeText )
1599 yy += fi.ascent ;
1600 #else
1601 yy += fi.ascent ;
1602 #endif
1603
1604 ::TextMode( (m_backgroundMode == wxTRANSPARENT) ? srcOr : srcCopy ) ;
1605 ::MoveTo( xx , yy );
1606
1607 int line = 0 ;
1608 {
1609 wxString linetext = strtext ;
1610
1611 #if TARGET_CARBON
1612 if ( useDrawThemeText )
1613 {
1614 Rect frame = {
1615 yy + line*(fi.descent + fi.ascent + fi.leading), xx ,
1616 yy + (line+1)*(fi.descent + fi.ascent + fi.leading) , xx + 10000 } ;
1617 wxMacCFStringHolder mString( linetext , m_font.GetEncoding()) ;
1618
1619 if ( m_backgroundMode != wxTRANSPARENT )
1620 {
1621 Point bounds = {0, 0} ;
1622 Rect background = frame ;
1623 SInt16 baseline ;
1624 ::GetThemeTextDimensions( mString,
1625 m_font.MacGetThemeFontID() ,
1626 kThemeStateActive,
1627 false,
1628 &bounds,
1629 &baseline );
1630 background.right = background.left + bounds.h ;
1631 background.bottom = background.top + bounds.v ;
1632 ::EraseRect( &background ) ;
1633 }
1634
1635 ::DrawThemeTextBox( mString,
1636 m_font.MacGetThemeFontID() ,
1637 kThemeStateActive,
1638 false,
1639 &frame,
1640 teJustLeft,
1641 NULL );
1642 }
1643 else
1644 #endif
1645 {
1646 wxCharBuffer text = linetext.mb_str(wxConvLocal) ;
1647 ::DrawText( text , 0 , strlen(text) ) ;
1648 }
1649 }
1650
1651 ::TextMode( srcOr ) ;
1652 }
1653
1654 bool wxDC::CanGetTextExtent() const
1655 {
1656 wxCHECK_MSG(Ok(), false, wxT("wxDC::CanGetTextExtent - invalid DC"));
1657
1658 return true ;
1659 }
1660
1661 void wxDC::DoGetTextExtent( const wxString &strtext, wxCoord *width, wxCoord *height,
1662 wxCoord *descent, wxCoord *externalLeading ,
1663 wxFont *theFont ) const
1664 {
1665 wxCHECK_RET(Ok(), wxT("wxDC::DoGetTextExtent - invalid DC"));
1666
1667 wxMacFastPortSetter helper(this) ;
1668 wxFont formerFont = m_font ;
1669 if ( theFont )
1670 {
1671 // work around the constness
1672 *((wxFont*)(&m_font)) = *theFont ;
1673 }
1674
1675 MacInstallFont() ;
1676 FontInfo fi ;
1677 ::GetFontInfo( &fi ) ;
1678
1679 #if TARGET_CARBON
1680 bool useGetThemeText = ( GetThemeTextDimensions != (void*) kUnresolvedCFragSymbolAddress ) ;
1681 if ( UMAGetSystemVersion() < 0x1000 || IsKindOf(CLASSINFO( wxPrinterDC ) ) || ((wxFont*)&m_font)->GetNoAntiAliasing() )
1682 useGetThemeText = false ;
1683 #endif
1684
1685 if ( height )
1686 *height = YDEV2LOGREL( fi.descent + fi.ascent ) ;
1687 if ( descent )
1688 *descent =YDEV2LOGREL( fi.descent );
1689 if ( externalLeading )
1690 *externalLeading = YDEV2LOGREL( fi.leading ) ;
1691
1692 int curwidth = 0 ;
1693 if ( width )
1694 {
1695 *width = 0 ;
1696 wxString linetext = strtext ;
1697
1698 if ( useGetThemeText )
1699 {
1700 Point bounds = {0, 0} ;
1701 SInt16 baseline ;
1702 wxMacCFStringHolder mString( linetext , m_font.GetEncoding() ) ;
1703 ThemeFontID themeFont = m_font.MacGetThemeFontID() ;
1704 ::GetThemeTextDimensions( mString,
1705 themeFont ,
1706 kThemeStateActive,
1707 false,
1708 &bounds,
1709 &baseline );
1710 curwidth = bounds.h ;
1711 }
1712 else
1713 {
1714 wxCharBuffer text = linetext.mb_str(wxConvLocal) ;
1715 curwidth = ::TextWidth( text , 0 , strlen(text) ) ;
1716 }
1717
1718 if ( curwidth > *width )
1719 *width = XDEV2LOGREL( curwidth ) ;
1720 }
1721
1722 if ( theFont )
1723 {
1724 // work around the constness
1725 *((wxFont*)(&m_font)) = formerFont ;
1726 m_macFontInstalled = false ;
1727 }
1728 }
1729
1730 bool wxDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
1731 {
1732 wxCHECK_MSG(Ok(), false, wxT("wxDC::DoGetPartialTextExtents - invalid DC"));
1733
1734 widths.Empty();
1735 widths.Add(0, text.Length());
1736
1737 if (text.Length() == 0)
1738 return false;
1739
1740 wxMacFastPortSetter helper(this) ;
1741 MacInstallFont() ;
1742
1743 #if TARGET_CARBON
1744 bool useGetThemeText = ( GetThemeTextDimensions != (void*) kUnresolvedCFragSymbolAddress ) ;
1745 if ( UMAGetSystemVersion() < 0x1000 || IsKindOf(CLASSINFO( wxPrinterDC ) ) || ((wxFont*)&m_font)->GetNoAntiAliasing() )
1746 useGetThemeText = false ;
1747
1748 if ( useGetThemeText )
1749 {
1750 // If anybody knows how to do this more efficiently yet still handle
1751 // the fractional glyph widths that may be present when using AA
1752 // fonts, please change it. Currently it is measuring from the
1753 // begining of the string for each succeding substring, which is much
1754 // slower than this should be.
1755 for (size_t i=0; i<text.Length(); i++)
1756 {
1757 wxString str(text.Left(i + 1));
1758 Point bounds = {0, 0};
1759 SInt16 baseline ;
1760 wxMacCFStringHolder mString(str, m_font.GetEncoding());
1761
1762 ::GetThemeTextDimensions( mString,
1763 m_font.MacGetThemeFontID(),
1764 kThemeStateActive,
1765 false,
1766 &bounds,
1767 &baseline );
1768 widths[i] = XDEV2LOGREL(bounds.h);
1769 }
1770 }
1771 else
1772 #endif
1773 {
1774 wxCharBuffer buff = text.mb_str(wxConvLocal);
1775 size_t len = strlen(buff);
1776 short* measurements = new short[len+1];
1777 MeasureText(len, buff.data(), measurements);
1778
1779 // Copy to widths, starting at measurements[1]
1780 // NOTE: this doesn't take into account any multi-byte characters
1781 // in buff, it probabkly should...
1782 for (size_t i=0; i<text.Length(); i++)
1783 widths[i] = XDEV2LOGREL(measurements[i + 1]);
1784
1785 delete [] measurements;
1786 }
1787
1788 return true;
1789 }
1790
1791 wxCoord wxDC::GetCharWidth(void) const
1792 {
1793 wxCHECK_MSG(Ok(), 1, wxT("wxDC::GetCharWidth - invalid DC"));
1794
1795 wxMacFastPortSetter helper(this) ;
1796 int width = 0 ;
1797 const char text[] = "g" ;
1798
1799 MacInstallFont() ;
1800
1801 #if TARGET_CARBON
1802 bool useGetThemeText = ( GetThemeTextDimensions != (void*) kUnresolvedCFragSymbolAddress ) ;
1803 if ( UMAGetSystemVersion() < 0x1000 || ((wxFont*)&m_font)->GetNoAntiAliasing() )
1804 useGetThemeText = false ;
1805
1806 if ( useGetThemeText )
1807 {
1808 Point bounds = {0, 0} ;
1809 SInt16 baseline ;
1810 CFStringRef mString = CFStringCreateWithBytes( NULL , (UInt8*) text , 1 , CFStringGetSystemEncoding(), false ) ;
1811 ::GetThemeTextDimensions( mString,
1812 m_font.MacGetThemeFontID(),
1813 kThemeStateActive,
1814 false,
1815 &bounds,
1816 &baseline );
1817 CFRelease( mString ) ;
1818 width = bounds.h ;
1819 }
1820 else
1821 #endif
1822 {
1823 width = ::TextWidth( text , 0 , 1 ) ;
1824 }
1825
1826 return YDEV2LOGREL(width) ;
1827 }
1828
1829 wxCoord wxDC::GetCharHeight(void) const
1830 {
1831 wxCHECK_MSG(Ok(), 1, wxT("wxDC::GetCharHeight - invalid DC"));
1832
1833 wxMacFastPortSetter helper(this) ;
1834 MacInstallFont() ;
1835 FontInfo fi ;
1836 ::GetFontInfo( &fi ) ;
1837
1838 return YDEV2LOGREL( fi.descent + fi.ascent );
1839 }
1840
1841 void wxDC::Clear(void)
1842 {
1843 wxCHECK_RET(Ok(), wxT("wxDC::Clear - invalid DC"));
1844
1845 wxMacFastPortSetter helper(this) ;
1846 Rect rect = { -31000 , -31000 , 31000 , 31000 } ;
1847
1848 if ( m_backgroundBrush.Ok() && m_backgroundBrush.GetStyle() != wxTRANSPARENT)
1849 {
1850 ::PenNormal() ;
1851 MacSetupBackgroundForCurrentPort( m_backgroundBrush ) ;
1852 ::EraseRect( &rect ) ;
1853 }
1854 }
1855
1856 void wxDC::MacInstallFont() const
1857 {
1858 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1859
1860 // if ( m_macFontInstalled )
1861 // return ;
1862 Pattern blackColor ;
1863 MacSetupBackgroundForCurrentPort(m_backgroundBrush) ;
1864 if ( m_backgroundMode != wxTRANSPARENT )
1865 {
1866 Pattern whiteColor ;
1867 ::BackPat(GetQDGlobalsWhite(&whiteColor));
1868 }
1869
1870 wxASSERT( m_font.Ok() ) ;
1871
1872 ::TextFont( m_font.MacGetFontNum() ) ;
1873 ::TextSize( (short)(m_scaleY * m_font.MacGetFontSize()) ) ;
1874 ::TextFace( m_font.MacGetFontStyle() ) ;
1875 m_macFontInstalled = true ;
1876 m_macBrushInstalled = false ;
1877 m_macPenInstalled = false ;
1878 RGBColor forecolor = MAC_WXCOLORREF( m_textForegroundColour.GetPixel());
1879 RGBColor backcolor = MAC_WXCOLORREF( m_textBackgroundColour.GetPixel());
1880 ::RGBForeColor( &forecolor );
1881 ::RGBBackColor( &backcolor );
1882
1883 // TODO:
1884 short mode = patCopy ;
1885 switch ( m_logicalFunction )
1886 {
1887 case wxCOPY: // src
1888 mode = patCopy ;
1889 break ;
1890
1891 case wxINVERT: // NOT dst
1892 ::PenPat(GetQDGlobalsBlack(&blackColor));
1893 mode = patXor ;
1894 break ;
1895
1896 case wxXOR: // src XOR dst
1897 mode = patXor ;
1898 break ;
1899
1900 case wxOR_REVERSE: // src OR (NOT dst)
1901 mode = notPatOr ;
1902 break ;
1903
1904 case wxSRC_INVERT: // (NOT src)
1905 mode = notPatCopy ;
1906 break ;
1907
1908 case wxAND: // src AND dst
1909 mode = adMin ;
1910 break ;
1911
1912 // TODO: unsupported
1913 case wxCLEAR: // 0
1914 case wxAND_REVERSE:// src AND (NOT dst)
1915 case wxAND_INVERT: // (NOT src) AND dst
1916 case wxNO_OP: // dst
1917 case wxNOR: // (NOT src) AND (NOT dst)
1918 case wxEQUIV: // (NOT src) XOR dst
1919 case wxOR_INVERT: // (NOT src) OR dst
1920 case wxNAND: // (NOT src) OR (NOT dst)
1921 case wxOR: // src OR dst
1922 case wxSET: // 1
1923 // case wxSRC_OR: // source _bitmap_ OR destination
1924 // case wxSRC_AND: // source _bitmap_ AND destination
1925 break ;
1926
1927 default:
1928 break ;
1929 }
1930
1931 ::PenMode( mode ) ;
1932 OSStatus status = noErr ;
1933 status = ATSUCreateAndCopyStyle( (ATSUStyle) m_font.MacGetATSUStyle() , (ATSUStyle*) &m_macATSUIStyle ) ;
1934 wxASSERT_MSG( status == noErr , wxT("couldn't set create ATSU style") ) ;
1935
1936 Fixed atsuSize = IntToFixed( int(m_scaleY * m_font.MacGetFontSize()) ) ;
1937 ATSUAttributeTag atsuTags[] =
1938 {
1939 kATSUSizeTag ,
1940 } ;
1941 ByteCount atsuSizes[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
1942 {
1943 sizeof( Fixed ) ,
1944 } ;
1945 // Boolean kTrue = true ;
1946 // Boolean kFalse = false ;
1947
1948 // ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
1949 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
1950 {
1951 &atsuSize ,
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 RGBColor forecolor = MAC_WXCOLORREF( m_pen.GetColour().GetPixel());
2011 RGBColor backcolor = MAC_WXCOLORREF( m_backgroundBrush.GetColour().GetPixel());
2012 ::RGBForeColor( &forecolor );
2013 ::RGBBackColor( &backcolor );
2014 ::PenNormal() ;
2015 int penWidth = (int) (m_pen.GetWidth() * m_scaleX) ; ;
2016 // null means only one pixel, at whatever resolution
2017 if ( penWidth == 0 )
2018 penWidth = 1 ;
2019 ::PenSize(penWidth, penWidth);
2020
2021 int penStyle = m_pen.GetStyle();
2022 Pattern pat;
2023 if (penStyle == wxUSER_DASH)
2024 {
2025 // FIXME: there should be exactly 8 items in the dash
2026 wxDash* dash ;
2027 int number = m_pen.GetDashes(&dash) ;
2028 int index = 0;
2029 for ( int i = 0 ; i < 8 ; ++i )
2030 {
2031 pat.pat[i] = dash[index] ;
2032 if (index < number - 1)
2033 index++;
2034 }
2035 }
2036 else
2037 {
2038 wxMacGetPattern(penStyle, &pat);
2039 }
2040 ::PenPat(&pat);
2041
2042 // TODO:
2043 short mode = patCopy ;
2044 switch ( m_logicalFunction )
2045 {
2046 case wxCOPY: // only foreground color, leave background (thus not patCopy)
2047 mode = patOr ;
2048 break ;
2049
2050 case wxINVERT: // NOT dst
2051 // ::PenPat(GetQDGlobalsBlack(&blackColor));
2052 mode = patXor ;
2053 break ;
2054
2055 case wxXOR: // src XOR dst
2056 mode = patXor ;
2057 break ;
2058
2059 case wxOR_REVERSE: // src OR (NOT dst)
2060 mode = notPatOr ;
2061 break ;
2062
2063 case wxSRC_INVERT: // (NOT src)
2064 mode = notPatCopy ;
2065 break ;
2066
2067 case wxAND: // src AND dst
2068 mode = adMin ;
2069 break ;
2070
2071 // TODO: unsupported
2072 case wxCLEAR: // 0
2073 case wxAND_REVERSE:// src AND (NOT dst)
2074 case wxAND_INVERT: // (NOT src) AND dst
2075 case wxNO_OP: // dst
2076 case wxNOR: // (NOT src) AND (NOT dst)
2077 case wxEQUIV: // (NOT src) XOR dst
2078 case wxOR_INVERT: // (NOT src) OR dst
2079 case wxNAND: // (NOT src) OR (NOT dst)
2080 case wxOR: // src OR dst
2081 case wxSET: // 1
2082 // case wxSRC_OR: // source _bitmap_ OR destination
2083 // case wxSRC_AND: // source _bitmap_ AND destination
2084 break ;
2085
2086 default:
2087 break ;
2088 }
2089
2090 ::PenMode( mode ) ;
2091 m_macPenInstalled = true ;
2092 m_macBrushInstalled = false ;
2093 m_macFontInstalled = false ;
2094 }
2095
2096 void wxDC::MacSetupBackgroundForCurrentPort(const wxBrush& background )
2097 {
2098 Pattern whiteColor ;
2099 if ( background.Ok() )
2100 {
2101 switch ( background.MacGetBrushKind() )
2102 {
2103 case kwxMacBrushTheme :
2104 ::SetThemeBackground( background.MacGetTheme() , wxDisplayDepth() , true ) ;
2105 break ;
2106
2107 case kwxMacBrushThemeBackground :
2108 {
2109 Rect extent ;
2110 ThemeBackgroundKind bg = background.MacGetThemeBackground( &extent ) ;
2111 ::ApplyThemeBackground( bg , &extent ,kThemeStateActive , wxDisplayDepth() , true ) ;
2112 }
2113 break ;
2114
2115 case kwxMacBrushColour :
2116 {
2117 ::RGBBackColor( &MAC_WXCOLORREF( background.GetColour().GetPixel()) );
2118 int brushStyle = background.GetStyle();
2119 if (brushStyle == wxSOLID)
2120 ::BackPat(GetQDGlobalsWhite(&whiteColor));
2121 else if (background.IsHatch())
2122 {
2123 Pattern pat ;
2124 wxMacGetPattern(brushStyle, &pat);
2125 ::BackPat(&pat);
2126 }
2127 else
2128 {
2129 ::BackPat(GetQDGlobalsWhite(&whiteColor));
2130 }
2131 }
2132 break ;
2133
2134 default:
2135 break ;
2136 }
2137 }
2138 }
2139
2140 void wxDC::MacInstallBrush() const
2141 {
2142 wxCHECK_RET(Ok(), wxT("Invalid DC"));
2143 Pattern blackColor ;
2144 // if ( m_macBrushInstalled )
2145 // return ;
2146 // foreground
2147 bool backgroundTransparent = (GetBackgroundMode() == wxTRANSPARENT) ;
2148 ::RGBForeColor( &MAC_WXCOLORREF( m_brush.GetColour().GetPixel()) );
2149 ::RGBBackColor( &MAC_WXCOLORREF( m_backgroundBrush.GetColour().GetPixel()) );
2150
2151 int brushStyle = m_brush.GetStyle();
2152 if (brushStyle == wxSOLID)
2153 {
2154 ::PenPat(GetQDGlobalsBlack(&blackColor));
2155 }
2156 else if (m_brush.IsHatch())
2157 {
2158 Pattern pat ;
2159
2160 wxMacGetPattern(brushStyle, &pat);
2161 ::PenPat(&pat);
2162 }
2163 else if ( m_brush.GetStyle() == wxSTIPPLE || m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE )
2164 {
2165 // we force this in order to be compliant with wxMSW
2166 backgroundTransparent = false ;
2167
2168 // for these the text fore (and back for MASK_OPAQUE) colors are used
2169 wxBitmap* bitmap = m_brush.GetStipple() ;
2170 int width = bitmap->GetWidth() ;
2171 int height = bitmap->GetHeight() ;
2172 GWorldPtr gw = NULL ;
2173
2174 if ( m_brush.GetStyle() == wxSTIPPLE )
2175 gw = MAC_WXHBITMAP(bitmap->GetHBITMAP(NULL)) ;
2176 else
2177 gw = MAC_WXHBITMAP(bitmap->GetMask()->GetHBITMAP()) ;
2178
2179 PixMapHandle gwpixmaphandle = GetGWorldPixMap( gw ) ;
2180 LockPixels( gwpixmaphandle ) ;
2181 bool isMonochrome = !IsPortColor( gw ) ;
2182 if ( !isMonochrome )
2183 {
2184 if ( (**gwpixmaphandle).pixelSize == 1 )
2185 isMonochrome = true ;
2186 }
2187
2188 if ( isMonochrome && width == 8 && height == 8 )
2189 {
2190 ::RGBForeColor( &MAC_WXCOLORREF( m_textForegroundColour.GetPixel()) );
2191 ::RGBForeColor( &MAC_WXCOLORREF( m_textBackgroundColour.GetPixel()) );
2192 BitMap* gwbitmap = (BitMap*) *gwpixmaphandle ; // since the color depth is 1 it is a BitMap
2193 UInt8 *gwbits = (UInt8*) gwbitmap->baseAddr ;
2194 int alignment = gwbitmap->rowBytes & 0x7FFF ;
2195 Pattern pat ;
2196
2197 for ( int i = 0 ; i < 8 ; ++i )
2198 {
2199 pat.pat[i] = gwbits[i*alignment+0] ;
2200 }
2201
2202 UnlockPixels( GetGWorldPixMap( gw ) ) ;
2203 ::PenPat( &pat ) ;
2204 }
2205 else
2206 {
2207 // this will be the code to handle power of 2 patterns, we will have to arrive at a nice
2208 // caching scheme before putting this into production
2209 Handle image;
2210 long imageSize;
2211
2212 PixPatHandle pixpat = NewPixPat() ;
2213 CopyPixMap(gwpixmaphandle, (**pixpat).patMap);
2214 imageSize = GetPixRowBytes((**pixpat).patMap) *
2215 ((**(**pixpat).patMap).bounds.bottom -
2216 (**(**pixpat).patMap).bounds.top);
2217 PtrToHand( (**gwpixmaphandle).baseAddr, &image, imageSize );
2218 (**pixpat).patData = image;
2219
2220 if ( isMonochrome )
2221 {
2222 CTabHandle ctable = ((**((**pixpat).patMap)).pmTable) ;
2223 ColorSpecPtr ctspec = (ColorSpecPtr) &(**ctable).ctTable ;
2224 if ( ctspec[0].rgb.red == 0x0000 )
2225 {
2226 ctspec[1].rgb = MAC_WXCOLORREF( m_textBackgroundColour.GetPixel()) ;
2227 ctspec[0].rgb = MAC_WXCOLORREF( m_textForegroundColour.GetPixel()) ;
2228 }
2229 else
2230 {
2231 ctspec[0].rgb = MAC_WXCOLORREF( m_textBackgroundColour.GetPixel()) ;
2232 ctspec[1].rgb = MAC_WXCOLORREF( m_textForegroundColour.GetPixel()) ;
2233 }
2234 ::CTabChanged( ctable ) ;
2235 }
2236
2237 ::PenPixPat(pixpat);
2238 m_macForegroundPixMap = pixpat ;
2239 }
2240
2241 UnlockPixels( gwpixmaphandle ) ;
2242 }
2243 else
2244 {
2245 ::PenPat(GetQDGlobalsBlack(&blackColor));
2246 }
2247
2248 short mode = patCopy ;
2249 switch ( m_logicalFunction )
2250 {
2251 case wxCOPY: // src
2252 if ( backgroundTransparent )
2253 mode = patOr ;
2254 else
2255 mode = patCopy ;
2256 break ;
2257
2258 case wxINVERT: // NOT dst
2259 if ( !backgroundTransparent )
2260 ::PenPat(GetQDGlobalsBlack(&blackColor));
2261 mode = patXor ;
2262 break ;
2263
2264 case wxXOR: // src XOR dst
2265 mode = patXor ;
2266 break ;
2267
2268 case wxOR_REVERSE: // src OR (NOT dst)
2269 mode = notPatOr ;
2270 break ;
2271
2272 case wxSRC_INVERT: // (NOT src)
2273 mode = notPatCopy ;
2274 break ;
2275
2276 case wxAND: // src AND dst
2277 mode = adMin ;
2278 break ;
2279
2280 // TODO: unsupported
2281 case wxCLEAR: // 0
2282 case wxAND_REVERSE:// src AND (NOT dst)
2283 case wxAND_INVERT: // (NOT src) AND dst
2284 case wxNO_OP: // dst
2285 case wxNOR: // (NOT src) AND (NOT dst)
2286 case wxEQUIV: // (NOT src) XOR dst
2287 case wxOR_INVERT: // (NOT src) OR dst
2288 case wxNAND: // (NOT src) OR (NOT dst)
2289 case wxOR: // src OR dst
2290 case wxSET: // 1
2291 // case wxSRC_OR: // source _bitmap_ OR destination
2292 // case wxSRC_AND: // source _bitmap_ AND destination
2293 break ;
2294
2295 default:
2296 break ;
2297 }
2298
2299 ::PenMode( mode ) ;
2300 m_macBrushInstalled = true ;
2301 m_macPenInstalled = false ;
2302 m_macFontInstalled = false ;
2303 }
2304
2305 // ---------------------------------------------------------------------------
2306 // coordinates transformations
2307 // ---------------------------------------------------------------------------
2308
2309 wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
2310 {
2311 return ((wxDC *)this)->XDEV2LOG(x);
2312 }
2313
2314 wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
2315 {
2316 return ((wxDC *)this)->YDEV2LOG(y);
2317 }
2318
2319 wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
2320 {
2321 return ((wxDC *)this)->XDEV2LOGREL(x);
2322 }
2323
2324 wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
2325 {
2326 return ((wxDC *)this)->YDEV2LOGREL(y);
2327 }
2328
2329 wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
2330 {
2331 return ((wxDC *)this)->XLOG2DEV(x);
2332 }
2333
2334 wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
2335 {
2336 return ((wxDC *)this)->YLOG2DEV(y);
2337 }
2338
2339 wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
2340 {
2341 return ((wxDC *)this)->XLOG2DEVREL(x);
2342 }
2343
2344 wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
2345 {
2346 return ((wxDC *)this)->YLOG2DEVREL(y);
2347 }
2348
2349 #endif // !wxMAC_USE_CORE_GRAPHICS