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