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