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