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