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