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