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