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