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