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