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