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