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