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