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