]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dc.cpp
added definition/initialisation of static class member
[wxWidgets.git] / src / mac / carbon / dc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dc.cpp
3 // Purpose: wxDC class
4 // Author: AUTHOR
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
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
20 #if __MSL__ >= 0x6000
21 #include "math.h"
22 #endif
23
24 #if !USE_SHARED_LIBRARY
25 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
26 #endif
27
28 //-----------------------------------------------------------------------------
29 // constants
30 //-----------------------------------------------------------------------------
31
32 #define mm2inches 0.0393700787402
33 #define inches2mm 25.4
34 #define mm2twips 56.6929133859
35 #define twips2mm 0.0176388888889
36 #define mm2pt 2.83464566929
37 #define pt2mm 0.352777777778
38
39 long wxDC::m_macCurrentPortId = 1 ;
40
41 //-----------------------------------------------------------------------------
42 // wxDC
43 //-----------------------------------------------------------------------------
44
45 wxDC::wxDC()
46 {
47 m_ok = FALSE;
48 m_colour = TRUE;
49
50 m_mm_to_pix_x = mm2pt;
51 m_mm_to_pix_y = mm2pt;
52
53 m_internalDeviceOriginX = 0;
54 m_internalDeviceOriginY = 0;
55 m_externalDeviceOriginX = 0;
56 m_externalDeviceOriginY = 0;
57
58 m_logicalScaleX = 1.0;
59 m_logicalScaleY = 1.0;
60 m_userScaleX = 1.0;
61 m_userScaleY = 1.0;
62 m_scaleX = 1.0;
63 m_scaleY = 1.0;
64
65 m_needComputeScaleX = FALSE;
66 m_needComputeScaleY = FALSE;
67
68 m_maxX = m_maxY = -100000;
69 m_minY = m_minY = 100000;
70
71 m_macPort = NULL ;
72 m_macMask = NULL ;
73 m_ok = FALSE ;
74
75 m_macFontInstalled = false ;
76 m_macBrushInstalled = false ;
77 m_macPenInstalled = false ;
78
79 m_macPortId = 0 ;
80 m_macLocalOrigin.h = m_macLocalOrigin.v = 0 ;
81 m_macClipRect.left = -32000 ;
82 m_macClipRect.top = -32000 ;
83 m_macClipRect.right = 32000 ;
84 m_macClipRect.bottom = 32000 ;
85 ::GetPort( &m_macOrigPort ) ;
86
87 m_pen = *wxBLACK_PEN;
88 m_font = *wxNORMAL_FONT;
89 m_brush = *wxWHITE_BRUSH;
90 };
91
92 wxDC::~wxDC(void)
93 {
94 if ( !m_macPortHelper.IsCleared() )
95 {
96 GrafPtr port ;
97 GetPort( &port ) ;
98 SetPort( m_macPortHelper.GetCurrentPort() ) ;
99 SetOrigin( 0 , 0 ) ;
100 SetPort( port ) ;
101 }
102 /*
103 if ( m_macPort )
104 {
105 ::SetPort( m_macPort ) ;
106 ::SetOrigin( 0 , 0 ) ;
107 ::ClipRect( &m_macPort->portRect ) ;
108 ::PenNormal() ;
109 ::SetPort( m_macOrigPort ) ;
110 }
111 */
112 ++m_macCurrentPortId ;
113 };
114
115 void wxDC::MacSetupPort() const
116 {
117 AGAPortHelper* help = (AGAPortHelper*) &m_macPortHelper ;
118 help->Setup( m_macPort ) ;
119 m_macPortId = ++m_macCurrentPortId ;
120 ::SetOrigin(-m_macLocalOrigin.h, -m_macLocalOrigin.v);
121 ::ClipRect(&m_macClipRect);
122
123 m_macFontInstalled = false ;
124 m_macBrushInstalled = false ;
125 m_macPenInstalled = false ;
126 }
127
128 void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask )
129 {
130 float scale = 1.0 ;
131
132 if (!Ok())
133 return;
134 MacVerifySetup() ;
135
136 long xx1 = XLOG2DEV(x);
137 long yy1 = YLOG2DEV(y);
138
139 {
140 wxBitmapRefData * bmap = (wxBitmapRefData*) ( bmp.GetRefData()) ;
141
142 if ( bmap )
143 {
144 if ( bmap->m_bitmapType == kMacBitmapTypePict )
145 {
146 Rect bitmaprect = { 0 , 0 , int(bmap->m_height * scale) , int(bmap->m_width * scale)} ;
147 ::OffsetRect( &bitmaprect , xx1 , yy1 ) ;
148 ::DrawPicture( bmap->m_hPict , &bitmaprect ) ;
149 }
150 else if ( bmap->m_bitmapType == kMacBitmapTypeGrafWorld )
151 {
152 if ( bmap->m_hBitmap )
153 {
154 GWorldPtr bmapworld = bmap->m_hBitmap ;
155 PixMapHandle bmappixels ;
156 RGBColor white = { 0xFFFF, 0xFFFF,0xFFFF} ;
157 RGBColor black = { 0,0,0} ;
158 RGBForeColor( &black ) ;
159 RGBBackColor( &white ) ;
160
161 bmappixels = GetGWorldPixMap( bmapworld ) ;
162 if ( LockPixels(bmappixels) )
163 {
164 Rect source , dest ;
165 source.top = 0 ;
166 source.left = 0 ;
167 source.right = bmap->m_width ;
168 source.bottom = bmap->m_height ;
169 dest.top = YLOG2DEV(y) ;
170 dest.left = XLOG2DEV(x) ;
171 dest.bottom = YLOG2DEV(y + bmap->m_height * scale) ;
172 dest.right = XLOG2DEV(x + bmap->m_width * scale ) ;
173
174 if ( useMask && bmp.GetMask() )
175 {
176 if ( LockPixels( GetGWorldPixMap( bmp.GetMask()->GetMaskBitmap( ) ) ) )
177 {
178 CopyMask( GetPortBitMapForCopyBits( bmapworld ) , GetPortBitMapForCopyBits( bmp.GetMask()->GetMaskBitmap( ) ) ,
179 GetPortBitMapForCopyBits( m_macPort ) ,
180 &source, &source , &dest ) ;
181 UnlockPixels( GetGWorldPixMap( bmp.GetMask()->GetMaskBitmap( ) ) ) ;
182 }
183 }
184 else
185 CopyBits( GetPortBitMapForCopyBits( bmapworld ) , GetPortBitMapForCopyBits( m_macPort ),
186 &source, &dest, srcCopy, NULL ) ;
187
188 UnlockPixels( bmappixels ) ;
189 }
190 m_macPenInstalled = false ;
191 m_macBrushInstalled = false ;
192 m_macFontInstalled = false ;
193 }
194 }
195 }
196 }
197 }
198
199 void wxDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
200 {
201 if (!Ok())
202 return;
203 MacVerifySetup() ;
204
205 long xx1 = XLOG2DEV(x);
206 long yy1 = YLOG2DEV(y);
207
208 {
209 wxIconRefData * iconref = (wxIconRefData*) ( icon.GetRefData()) ;
210
211 if ( iconref && iconref->m_ok && iconref->m_hIcon )
212 {
213 Rect bitmaprect = { 0 , 0 , iconref->m_height , iconref->m_width } ;
214 OffsetRect( &bitmaprect , xx1 , yy1 ) ;
215 PlotCIconHandle( &bitmaprect , atNone , ttNone , iconref->m_hIcon ) ;
216 }
217 }
218 };
219
220 void wxDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
221 {
222 MacVerifySetup() ;
223 if( m_clipping )
224 {
225 m_clipX1 = wxMax( m_clipX1 , x ) ;
226 m_clipY1 = wxMax( m_clipY1 ,y );
227 m_clipX2 = wxMin( m_clipX2, (x + width));
228 m_clipY2 = wxMin( m_clipY2,(y + height));
229
230 }
231 else
232 {
233 m_clipping = TRUE;
234 m_clipX1 = x;
235 m_clipY1 = y;
236 m_clipX2 = x + width;
237 m_clipY2 = y + height;
238 }
239
240 long x1 = XLOG2DEV(m_clipX1);
241 long y1 = YLOG2DEV(m_clipY1);
242 long x2 = XLOG2DEV(m_clipX2);
243 long y2 = YLOG2DEV(m_clipY2);
244
245 Rect clip = { y1 , x1 , y2 , x2 } ;
246
247 ::ClipRect( &clip ) ;
248
249 };
250
251 void wxDC::DoSetClippingRegionAsRegion( const wxRegion &region )
252 {
253 wxCHECK_RET( Ok(), wxT("invalid window dc") );
254
255 MacVerifySetup() ;
256 if (region.Empty())
257 {
258 DestroyClippingRegion();
259 return;
260 }
261
262 wxCoord xx, yy, ww, hh;
263 region.GetBox( xx, yy, ww, hh );
264 wxDC::DoSetClippingRegion( xx, yy, ww, hh );
265 }
266
267 void wxDC::DestroyClippingRegion(void)
268 {
269 MacVerifySetup() ;
270 m_clipping = FALSE;
271 // Rect clip = { -32000 , -32000 , 32000 , 32000 } ;
272 ::ClipRect(&m_macClipRect);
273 };
274
275 void wxDC::DoGetSize( int* width, int* height ) const
276 {
277 *width = m_maxX-m_minX;
278 *height = m_maxY-m_minY;
279 };
280
281 void wxDC::DoGetSizeMM( int* width, int* height ) const
282 {
283 int w = 0;
284 int h = 0;
285 GetSize( &w, &h );
286 *width = long( double(w) / (m_scaleX*m_mm_to_pix_x) );
287 *height = long( double(h) / (m_scaleY*m_mm_to_pix_y) );
288 };
289
290 void wxDC::SetTextForeground( const wxColour &col )
291 {
292 if (!Ok()) return;
293 m_textForegroundColour = col;
294 m_macFontInstalled = false ;
295 };
296
297 void wxDC::SetTextBackground( const wxColour &col )
298 {
299 if (!Ok()) return;
300 m_textBackgroundColour = col;
301 m_macFontInstalled = false ;
302 };
303
304 void wxDC::SetMapMode( int mode )
305 {
306 switch (mode)
307 {
308 case wxMM_TWIPS:
309 SetLogicalScale( twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y );
310 break;
311 case wxMM_POINTS:
312 SetLogicalScale( pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y );
313 break;
314 case wxMM_METRIC:
315 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
316 break;
317 case wxMM_LOMETRIC:
318 SetLogicalScale( m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0 );
319 break;
320 default:
321 case wxMM_TEXT:
322 SetLogicalScale( 1.0, 1.0 );
323 break;
324 };
325 if (mode != wxMM_TEXT)
326 {
327 m_needComputeScaleX = TRUE;
328 m_needComputeScaleY = TRUE;
329 };
330 };
331
332 void wxDC::SetUserScale( double x, double y )
333 {
334 // allow negative ? -> no
335 m_userScaleX = x;
336 m_userScaleY = y;
337 ComputeScaleAndOrigin();
338 };
339
340 void wxDC::SetLogicalScale( double x, double y )
341 {
342 // allow negative ?
343 m_logicalScaleX = x;
344 m_logicalScaleY = y;
345 ComputeScaleAndOrigin();
346 };
347
348 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
349 {
350 m_logicalOriginX = x * m_signX; // is this still correct ?
351 m_logicalOriginY = y * m_signY;
352 ComputeScaleAndOrigin();
353 };
354
355 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
356 {
357 m_externalDeviceOriginX = x;
358 m_externalDeviceOriginY = y;
359 ComputeScaleAndOrigin();
360 };
361 /*
362 void wxDC::SetInternalDeviceOrigin( long x, long y )
363 {
364 m_internalDeviceOriginX = x;
365 m_internalDeviceOriginY = y;
366 ComputeScaleAndOrigin();
367 };
368
369 void wxDC::GetInternalDeviceOrigin( long *x, long *y )
370 {
371 if (x) *x = m_internalDeviceOriginX;
372 if (y) *y = m_internalDeviceOriginY;
373 };
374 */
375 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
376 {
377 m_signX = (xLeftRight ? 1 : -1);
378 m_signY = (yBottomUp ? -1 : 1);
379 ComputeScaleAndOrigin();
380 };
381 /*
382
383 void wxDC::CalcBoundingBox( long x, long y )
384 {
385 if (x < m_minX) m_minX = x;
386 if (y < m_minY) m_minY = y;
387 if (x > m_maxX) m_maxX = x;
388 if (y > m_maxY) m_maxY = y;
389 };
390 */
391 wxSize wxDC::GetPPI() const
392 {
393 return wxSize(72, 72);
394 }
395
396 int wxDC::GetDepth() const
397 {
398 return wxDisplayDepth() ;
399 }
400
401 void wxDC::ComputeScaleAndOrigin(void)
402 {
403 // CMB: copy scale to see if it changes
404 double origScaleX = m_scaleX;
405 double origScaleY = m_scaleY;
406
407 m_scaleX = m_logicalScaleX * m_userScaleX;
408 m_scaleY = m_logicalScaleY * m_userScaleY;
409
410 m_deviceOriginX = m_internalDeviceOriginX + m_externalDeviceOriginX;
411 m_deviceOriginY = m_internalDeviceOriginY + m_externalDeviceOriginY;
412
413 // CMB: if scale has changed call SetPen to recalulate the line width
414 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
415 {
416 // this is a bit artificial, but we need to force wxDC to think
417 // the pen has changed
418 wxPen* pen = & GetPen();
419 wxPen tempPen;
420 m_pen = tempPen;
421 SetPen(* pen);
422 }
423 };
424
425 void wxDC::SetPalette( const wxPalette& palette )
426 {
427 }
428
429 void wxDC::SetBackgroundMode( int mode )
430 {
431 m_backgroundMode = mode ;
432 }
433
434 void wxDC::SetFont( const wxFont &font )
435 {
436 if (!Ok())
437 return;
438
439 MacVerifySetup() ;
440
441 m_font = font;
442 m_macFontInstalled = false ;
443 }
444
445 void wxDC::SetPen( const wxPen &pen )
446 {
447 if (!Ok() )
448 return;
449
450 MacVerifySetup() ;
451
452 if ( m_pen == pen )
453 return ;
454
455 m_pen = pen;
456 /*
457 if (!m_pen.Ok())
458 return;
459 */
460 m_macPenInstalled = false ;
461 }
462
463 void wxDC::SetBrush( const wxBrush &brush )
464 {
465 if (!Ok() )
466 return;
467 MacVerifySetup() ;
468
469 if (m_brush == brush)
470 return;
471
472 m_brush = brush;
473 m_macBrushInstalled = false ;
474 }
475
476 void wxDC::SetBackground( const wxBrush &brush )
477 {
478 if (!Ok())
479 return;
480 MacVerifySetup() ;
481
482 if (m_backgroundBrush == brush)
483 return;
484
485 m_backgroundBrush = brush;
486
487 if (!m_backgroundBrush.Ok())
488 return;
489 m_macBrushInstalled = false ;
490 }
491
492 void wxDC::SetLogicalFunction( int function )
493 {
494 if (m_logicalFunction == function)
495 return;
496
497 m_logicalFunction = function ;
498 m_macFontInstalled = false ;
499 m_macBrushInstalled = false ;
500 m_macPenInstalled = false ;
501 }
502
503 void wxDC::DoFloodFill( wxCoord x, wxCoord y, const wxColour& col,
504 int style )
505 {
506 }
507
508 bool wxDC::DoGetPixel( wxCoord x, wxCoord y, wxColour *col ) const
509 {
510 return true ;
511 }
512
513 void wxDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
514 {
515 if (!Ok())
516 return;
517
518 MacVerifySetup() ;
519
520 if (m_pen.GetStyle() != wxTRANSPARENT)
521 {
522 MacInstallPen() ;
523 int offset = ( (m_pen.GetWidth() == 0 ? 1 : m_pen.GetWidth() ) * m_scaleX - 1) / 2 ;
524 long xx1 = XLOG2DEV(x1);
525 long yy1 = YLOG2DEV(y1);
526 long xx2 = XLOG2DEV(x2);
527 long yy2 = YLOG2DEV(y2);
528
529 ::MoveTo(xx1 - offset ,yy1 - offset);
530 ::LineTo(xx2 - offset , yy2 - offset );
531 };
532 }
533
534 void wxDC::DoCrossHair( wxCoord x, wxCoord y )
535 {
536 }
537
538 void wxDC::DoDrawArc( wxCoord x1, wxCoord y1,
539 wxCoord x2, wxCoord y2,
540 wxCoord xc, wxCoord yc )
541 {
542 }
543
544 void wxDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
545 double sa, double ea )
546 {
547 }
548
549 void wxDC::DoDrawPoint( wxCoord x, wxCoord y )
550 {
551 if (!Ok())
552 return;
553
554 MacVerifySetup() ;
555
556 if (m_pen.GetStyle() != wxTRANSPARENT)
557 {
558 MacInstallPen() ;
559 long xx1 = XLOG2DEV(x);
560 long yy1 = YLOG2DEV(y);
561
562 ::MoveTo(xx1,yy1);
563 ::LineTo(xx1+1, yy1+1);
564 };
565 }
566
567 void wxDC::DoDrawLines(int n, wxPoint points[],
568 wxCoord xoffset, wxCoord yoffset)
569 {
570 if (!Ok())
571 return;
572 MacVerifySetup() ;
573
574 if (m_pen.GetStyle() == wxTRANSPARENT)
575 return;
576
577 MacInstallPen() ;
578
579 int offset = (m_pen.GetWidth() - 1 ) / 2 ;
580 long x1, x2 , y1 , y2 ;
581 x1 = XLOG2DEV(points[0].x + xoffset);
582 y1 = YLOG2DEV(points[0].y + yoffset);
583 ::MoveTo(x1 - offset ,y1 - offset );
584
585 for (int i = 0; i < n-1; i++)
586 {
587 x2 = XLOG2DEV(points[i+1].x + xoffset);
588 y2 = YLOG2DEV(points[i+1].y + yoffset);
589 ::LineTo(x2 - offset , y2 - offset );
590 }
591 }
592
593 void wxDC::DoDrawPolygon(int n, wxPoint points[],
594 wxCoord xoffset, wxCoord yoffset,
595 int fillStyle )
596 {
597 if (!Ok())
598 return;
599 MacVerifySetup() ;
600
601 PolyHandle polygon = OpenPoly() ;
602 long x1, x2 , y1 , y2 ;
603 x1 = XLOG2DEV(points[0].x + xoffset);
604 y1 = YLOG2DEV(points[0].y + yoffset);
605 ::MoveTo(x1,y1);
606
607 for (int i = 0; i < n-1; i++)
608 {
609 x2 = XLOG2DEV(points[i+1].x + xoffset);
610 y2 = YLOG2DEV(points[i+1].y + yoffset);
611 ::LineTo(x2, y2);
612 }
613
614 ClosePoly() ;
615 if (m_brush.GetStyle() != wxTRANSPARENT)
616 {
617 MacInstallBrush() ;
618 ::PaintPoly( polygon ) ;
619 };
620
621 if (m_pen.GetStyle() != wxTRANSPARENT)
622 {
623 MacInstallPen() ;
624 ::FramePoly( polygon ) ;
625 };
626 KillPoly( polygon ) ;
627 }
628
629 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
630 {
631 if (!Ok())
632 return;
633 MacVerifySetup() ;
634
635 long xx = XLOG2DEV(x);
636 long yy = YLOG2DEV(y);
637 long ww = m_signX * XLOG2DEVREL(width);
638 long hh = m_signY * YLOG2DEVREL(height);
639
640 // CMB: draw nothing if transformed w or h is 0
641 if (ww == 0 || hh == 0)
642 return;
643
644 // CMB: handle -ve width and/or height
645 if (ww < 0)
646 {
647 ww = -ww;
648 xx = xx - ww;
649 }
650
651 if (hh < 0)
652 {
653 hh = -hh;
654 yy = yy - hh;
655 }
656
657 Rect rect = { yy , xx , yy + hh , xx + ww } ;
658
659 if (m_brush.GetStyle() != wxTRANSPARENT)
660 {
661 MacInstallBrush() ;
662 ::PaintRect( &rect ) ;
663 };
664
665 if (m_pen.GetStyle() != wxTRANSPARENT)
666 {
667 MacInstallPen() ;
668 ::FrameRect( &rect ) ;
669 };
670 }
671
672 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
673 wxCoord width, wxCoord height,
674 double radius)
675 {
676 if (!Ok())
677 return;
678 MacVerifySetup() ;
679
680 if (radius < 0.0)
681 radius = - radius * ((width < height) ? width : height);
682
683 long xx = XLOG2DEV(x);
684 long yy = YLOG2DEV(y);
685 long ww = m_signX * XLOG2DEVREL(width);
686 long hh = m_signY * YLOG2DEVREL(height);
687
688 // CMB: draw nothing if transformed w or h is 0
689 if (ww == 0 || hh == 0)
690 return;
691
692 // CMB: handle -ve width and/or height
693 if (ww < 0)
694 {
695 ww = -ww;
696 xx = xx - ww;
697 }
698
699 if (hh < 0)
700 {
701 hh = -hh;
702 yy = yy - hh;
703 }
704
705 Rect rect = { yy , xx , yy + hh , xx + ww } ;
706
707 if (m_brush.GetStyle() != wxTRANSPARENT)
708 {
709 MacInstallBrush() ;
710 ::PaintRoundRect( &rect , int(radius * 2) , int(radius * 2) ) ;
711 };
712
713 if (m_pen.GetStyle() != wxTRANSPARENT)
714 {
715 MacInstallPen() ;
716 ::FrameRoundRect( &rect , int(radius * 2) , int(radius * 2) ) ;
717 };
718 }
719
720 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
721 {
722 if (!Ok())
723 return;
724 MacVerifySetup() ;
725
726 long xx = XLOG2DEV(x);
727 long yy = YLOG2DEV(y);
728 long ww = m_signX * XLOG2DEVREL(width);
729 long hh = m_signY * YLOG2DEVREL(height);
730
731 // CMB: draw nothing if transformed w or h is 0
732 if (ww == 0 || hh == 0)
733 return;
734
735 // CMB: handle -ve width and/or height
736 if (ww < 0)
737 {
738 ww = -ww;
739 xx = xx - ww;
740 }
741
742 if (hh < 0)
743 {
744 hh = -hh;
745 yy = yy - hh;
746 }
747
748 Rect rect = { yy , xx , yy + hh , xx + ww } ;
749
750 if (m_brush.GetStyle() != wxTRANSPARENT)
751 {
752 MacInstallBrush() ;
753 ::PaintOval( &rect ) ;
754 };
755
756 if (m_pen.GetStyle() != wxTRANSPARENT)
757 {
758 MacInstallPen() ;
759 ::FrameOval( &rect ) ;
760 };
761 }
762
763
764
765 bool wxDC::CanDrawBitmap(void) const
766 {
767 return true ;
768 }
769
770
771 bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
772 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool useMask )
773 {
774 if (!Ok()) return FALSE;
775 MacVerifySetup() ;
776
777 CGrafPtr sourcePort = (CGrafPtr) source->m_macPort ;
778 PixMapHandle bmappixels = GetGWorldPixMap( sourcePort ) ;
779 RGBColor white = { 0xFFFF, 0xFFFF,0xFFFF} ;
780 RGBColor black = { 0,0,0} ;
781 RGBColor forecolor = m_textForegroundColour.GetPixel();
782 RGBColor backcolor = m_textBackgroundColour.GetPixel();
783 RGBForeColor( &forecolor ) ;
784 RGBBackColor( &backcolor ) ;
785
786 if ( LockPixels(bmappixels) )
787 {
788 Rect srcrect , dstrect ;
789 srcrect.top = source->YLOG2DEV(ysrc) ;
790 srcrect.left = source->XLOG2DEV(xsrc) ;
791 srcrect.right = source->XLOG2DEV(xsrc + width ) ;
792 srcrect.bottom = source->YLOG2DEV(ysrc + height) ;
793 dstrect.top = YLOG2DEV(ydest) ;
794 dstrect.left = XLOG2DEV(xdest) ;
795 dstrect.bottom = YLOG2DEV(ydest + height ) ;
796 dstrect.right = XLOG2DEV(xdest + width ) ;
797
798 short mode = (logical_func == wxCOPY ? srcCopy :
799 // logical_func == wxCLEAR ? WHITENESS :
800 // logical_func == wxSET ? BLACKNESS :
801 logical_func == wxINVERT ? hilite :
802 // logical_func == wxAND ? MERGECOPY :
803 logical_func == wxOR ? srcOr :
804 logical_func == wxSRC_INVERT ? notSrcCopy :
805 logical_func == wxXOR ? srcXor :
806 // logical_func == wxOR_REVERSE ? MERGEPAINT :
807 // logical_func == wxAND_REVERSE ? SRCERASE :
808 // logical_func == wxSRC_OR ? srcOr :
809 // logical_func == wxSRC_AND ? SRCAND :
810 srcCopy );
811
812 if ( useMask && source->m_macMask )
813 {
814 wxASSERT( mode == srcCopy ) ;
815 if ( LockPixels( GetGWorldPixMap( source->m_macMask ) ) )
816 {
817 CopyMask( GetPortBitMapForCopyBits( sourcePort ) , GetPortBitMapForCopyBits( source->m_macMask ) ,
818 GetPortBitMapForCopyBits( m_macPort ) ,
819 &srcrect, &srcrect , &dstrect ) ;
820 UnlockPixels( GetGWorldPixMap( source->m_macMask ) ) ;
821 }
822 }
823 else
824 {
825 CopyBits( GetPortBitMapForCopyBits( sourcePort ) , GetPortBitMapForCopyBits( m_macPort ) ,
826 &srcrect, &dstrect, mode, NULL ) ;
827 }
828 UnlockPixels( bmappixels ) ;
829 }
830
831 m_macPenInstalled = false ;
832 m_macBrushInstalled = false ;
833 m_macFontInstalled = false ;
834
835 return TRUE;
836 }
837
838 void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y,
839 double angle)
840 {
841 }
842 void wxDC::DoDrawText(const wxString& strtext, wxCoord x, wxCoord y)
843 {
844 if (!Ok())
845 return;
846 MacVerifySetup() ;
847
848 long xx = XLOG2DEV(x);
849 long yy = YLOG2DEV(y);
850
851 // if (m_pen.GetStyle() != wxTRANSPARENT)
852 {
853 MacInstallFont() ;
854 /*
855 Rect clip = { -32000 , -32000 , 32000 , 32000 } ;
856
857 ::ClipRect( &clip ) ;
858 */
859
860 FontInfo fi ;
861 ::GetFontInfo( &fi ) ;
862
863 yy += fi.ascent ;
864 ::MoveTo( xx , yy );
865 if ( m_backgroundMode == wxTRANSPARENT )
866 {
867 ::TextMode( srcOr) ;
868 }
869 else
870 {
871 ::TextMode( srcCopy ) ;
872 }
873
874 const char *text = NULL ;
875 int length = 0 ;
876 wxString macText ;
877
878 if ( wxApp::s_macDefaultEncodingIsPC )
879 {
880 macText = wxMacMakeMacStringFromPC( strtext ) ;
881 text = macText ;
882 length = macText.Length() ;
883 }
884 else
885 {
886 text = strtext ;
887 length = strtext.Length() ;
888 }
889
890 int laststop = 0 ;
891 int i = 0 ;
892 int line = 0 ;
893
894 while( i < length )
895 {
896 if( text[i] == 13 || text[i] == 10)
897 {
898 ::DrawText( text , laststop , i - laststop ) ;
899 line++ ;
900 ::MoveTo( xx , yy + line*(fi.descent + fi.ascent + fi.leading) );
901 laststop = i+1 ;
902 }
903 i++ ;
904 }
905
906 ::DrawText( text , laststop , i - laststop ) ;
907 ::TextMode( srcOr ) ;
908 }
909 }
910
911 bool wxDC::CanGetTextExtent(void) const
912 {
913 if ( !Ok() )
914 return false ;
915
916 return true ;
917 }
918
919 void wxDC::DoGetTextExtent( const wxString &string, wxCoord *width, wxCoord *height,
920 wxCoord *descent, wxCoord *externalLeading ,
921 wxFont *theFont ) const
922 {
923 if (!Ok())
924 return;
925
926 MacVerifySetup() ;
927
928 wxFont formerFont = m_font ;
929
930 if ( theFont )
931 {
932 wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ;
933
934 if ( font )
935 {
936 ::TextFont( font->m_macFontNum ) ;
937 ::TextSize( YLOG2DEVREL( font->m_macFontSize) ) ;
938 ::TextFace( font->m_macFontStyle ) ;
939 }
940 }
941 else
942 {
943 MacInstallFont() ;
944 }
945
946 FontInfo fi ;
947 ::GetFontInfo( &fi ) ;
948
949 if ( height )
950 *height = YDEV2LOGREL( fi.descent + fi.ascent ) ;
951 if ( descent )
952 *descent =YDEV2LOGREL( fi.descent );
953 if ( externalLeading )
954 *externalLeading = YDEV2LOGREL( fi.leading ) ;
955
956 const char *text = NULL ;
957 int length = 0 ;
958 wxString macText ;
959 if ( wxApp::s_macDefaultEncodingIsPC )
960 {
961 macText = wxMacMakeMacStringFromPC( string ) ;
962 text = macText ;
963 length = macText.Length() ;
964 }
965 else
966 {
967 text = string ;
968 length = string.Length() ;
969 }
970
971 int laststop = 0 ;
972 int i = 0 ;
973 int curwidth = 0 ;
974 if ( width )
975 {
976 *width = 0 ;
977
978 while( i < length )
979 {
980 if( text[i] == 13 || text[i] == 10)
981 {
982 if ( height )
983 *height += YDEV2LOGREL( fi.descent + fi.ascent + fi.leading ) ;
984 curwidth = ::TextWidth( text , laststop , i - laststop ) ;
985 if ( curwidth > *width )
986 *width = XDEV2LOGREL( curwidth ) ;
987 laststop = i+1 ;
988 }
989 i++ ;
990 }
991
992 curwidth = ::TextWidth( text , laststop , i - laststop ) ;
993 if ( curwidth > *width )
994 *width = XDEV2LOGREL( curwidth ) ;
995 }
996
997 if ( theFont )
998 {
999 m_macFontInstalled = false ;
1000 }
1001 }
1002
1003 wxCoord wxDC::GetCharWidth(void) const
1004 {
1005 if (!Ok())
1006 return 1;
1007
1008 MacVerifySetup() ;
1009
1010 MacInstallFont() ;
1011
1012 FontInfo fi ;
1013 ::GetFontInfo( &fi ) ;
1014
1015 return YDEV2LOGREL((fi.descent + fi.ascent) / 2) ;
1016 }
1017
1018 wxCoord wxDC::GetCharHeight(void) const
1019 {
1020 if (!Ok())
1021 return 1;
1022
1023 MacVerifySetup() ;
1024
1025 MacInstallFont() ;
1026
1027 FontInfo fi ;
1028 ::GetFontInfo( &fi ) ;
1029
1030 return YDEV2LOGREL( fi.descent + fi.ascent );
1031 }
1032
1033 void wxDC::Clear(void)
1034 {
1035 if (!Ok())
1036 return;
1037 MacVerifySetup() ;
1038 Rect rect = { -32767 , -32767 , 32767 , 32767 } ;
1039
1040 if (m_backgroundBrush.GetStyle() != wxTRANSPARENT)
1041 {
1042 MacInstallBrush() ;
1043 ::EraseRect( &rect ) ;
1044 };
1045 }
1046
1047 void wxDC::MacInstallFont() const
1048 {
1049 if (!Ok())
1050 return;
1051 MacVerifySetup() ;
1052
1053 if ( m_macFontInstalled )
1054 return ;
1055 Pattern blackColor ;
1056
1057 wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ;
1058
1059 if ( font )
1060 {
1061 ::TextFont( font->m_macFontNum ) ;
1062 ::TextSize( short(m_scaleY * font->m_macFontSize) ) ;
1063 ::TextFace( font->m_macFontStyle ) ;
1064
1065 m_macFontInstalled = true ;
1066 m_macBrushInstalled = false ;
1067 m_macPenInstalled = false ;
1068
1069 RGBColor forecolor = m_textForegroundColour.GetPixel();
1070 RGBColor backcolor = m_textBackgroundColour.GetPixel();
1071 ::RGBForeColor( &forecolor );
1072 ::RGBBackColor( &backcolor );
1073 }
1074 else
1075 {
1076 short fontnum ;
1077
1078 GetFNum( "\pGeneva" , &fontnum ) ;
1079 ::TextFont( fontnum ) ;
1080 ::TextSize( short(m_scaleY * 10) ) ;
1081 ::TextFace( 0 ) ;
1082
1083 // todo reset after spacing changes - or store the current spacing somewhere
1084
1085 m_macFontInstalled = true ;
1086 m_macBrushInstalled = false ;
1087 m_macPenInstalled = false ;
1088
1089 RGBColor forecolor = m_textForegroundColour.GetPixel();
1090 RGBColor backcolor = m_textBackgroundColour.GetPixel();
1091 ::RGBForeColor( &forecolor );
1092 ::RGBBackColor( &backcolor );
1093 }
1094
1095 short mode = patCopy ;
1096
1097 // todo :
1098
1099 switch( m_logicalFunction )
1100 {
1101 case wxCOPY: // src
1102 mode = patCopy ;
1103 break ;
1104 case wxINVERT: // NOT dst
1105 ::PenPat(GetQDGlobalsBlack(&blackColor));
1106 mode = patXor ;
1107 break ;
1108 case wxXOR: // src XOR dst
1109 mode = patXor ;
1110 break ;
1111 case wxOR_REVERSE: // src OR (NOT dst)
1112 mode = notPatOr ;
1113 break ;
1114 case wxSRC_INVERT: // (NOT src)
1115 mode = notPatCopy ;
1116 break ;
1117
1118 // unsupported TODO
1119
1120 case wxCLEAR: // 0
1121 case wxAND_REVERSE:// src AND (NOT dst)
1122 case wxAND: // src AND dst
1123 case wxAND_INVERT: // (NOT src) AND dst
1124 case wxNO_OP: // dst
1125 case wxNOR: // (NOT src) AND (NOT dst)
1126 case wxEQUIV: // (NOT src) XOR dst
1127 case wxOR_INVERT: // (NOT src) OR dst
1128 case wxNAND: // (NOT src) OR (NOT dst)
1129 case wxOR: // src OR dst
1130 case wxSET: // 1
1131 // case wxSRC_OR: // source _bitmap_ OR destination
1132 // case wxSRC_AND: // source _bitmap_ AND destination
1133 break ;
1134 }
1135 ::PenMode( mode ) ;
1136 }
1137
1138 static void wxMacGetHatchPattern(int hatchStyle, Pattern *pattern)
1139 {
1140 int thePatListID = sysPatListID;
1141 int theIndex;
1142 switch(hatchStyle)
1143 {
1144 case wxBDIAGONAL_HATCH:
1145 theIndex = 34; // WCH: this is not good
1146 break;
1147 case wxFDIAGONAL_HATCH:
1148 theIndex = 26;
1149 break;
1150 case wxCROSS_HATCH:
1151 theIndex = 5;
1152 break;
1153 case wxHORIZONTAL_HATCH:
1154 theIndex = 25;
1155 break;
1156 case wxVERTICAL_HATCH:
1157 theIndex = 6;
1158 break;
1159 case wxCROSSDIAG_HATCH:
1160 theIndex = 4; // WCH: this is not good
1161 break;
1162 default:
1163 theIndex = 1; // solid pattern
1164 break;
1165 }
1166 GetIndPattern( pattern, thePatListID, theIndex);
1167 }
1168
1169 void wxDC::MacInstallPen() const
1170 {
1171 if (!Ok())
1172 return;
1173 MacVerifySetup() ;
1174
1175 Pattern blackColor;
1176
1177 if ( m_macPenInstalled )
1178 return ;
1179
1180 RGBColor forecolor = m_pen.GetColour().GetPixel();
1181 RGBColor backcolor = m_backgroundBrush.GetColour().GetPixel();
1182 ::RGBForeColor( &forecolor );
1183 ::RGBBackColor( &backcolor );
1184
1185 ::PenNormal() ;
1186 int penWidth = m_pen.GetWidth() * m_scaleX ;
1187
1188 // null means only one pixel, at whatever resolution
1189 if ( penWidth == 0 )
1190 penWidth = 1 ;
1191 ::PenSize(penWidth, penWidth);
1192
1193 int penStyle = m_pen.GetStyle();
1194
1195 if (penStyle == wxSOLID)
1196 {
1197 ::PenPat(GetQDGlobalsBlack(&blackColor));
1198 }
1199 else if (IS_HATCH(penStyle))
1200 {
1201 Pattern pat ;
1202 wxMacGetHatchPattern(penStyle, &pat);
1203 ::PenPat(&pat);
1204 }
1205 else
1206 {
1207 ::PenPat(GetQDGlobalsBlack(&blackColor));
1208 }
1209
1210 short mode = patCopy ;
1211
1212 // todo :
1213
1214 switch( m_logicalFunction )
1215 {
1216 case wxCOPY: // src
1217 mode = patCopy ;
1218 break ;
1219 case wxINVERT: // NOT dst
1220 ::PenPat(GetQDGlobalsBlack(&blackColor));
1221 mode = patXor ;
1222 break ;
1223 case wxXOR: // src XOR dst
1224 mode = patXor ;
1225 break ;
1226 case wxOR_REVERSE: // src OR (NOT dst)
1227 mode = notPatOr ;
1228 break ;
1229 case wxSRC_INVERT: // (NOT src)
1230 mode = notPatCopy ;
1231 break ;
1232
1233 // unsupported TODO
1234
1235 case wxCLEAR: // 0
1236 case wxAND_REVERSE:// src AND (NOT dst)
1237 case wxAND: // src AND dst
1238 case wxAND_INVERT: // (NOT src) AND dst
1239 case wxNO_OP: // dst
1240 case wxNOR: // (NOT src) AND (NOT dst)
1241 case wxEQUIV: // (NOT src) XOR dst
1242 case wxOR_INVERT: // (NOT src) OR dst
1243 case wxNAND: // (NOT src) OR (NOT dst)
1244 case wxOR: // src OR dst
1245 case wxSET: // 1
1246 // case wxSRC_OR: // source _bitmap_ OR destination
1247 // case wxSRC_AND: // source _bitmap_ AND destination
1248 break ;
1249 }
1250 ::PenMode( mode ) ;
1251 m_macPenInstalled = true ;
1252 m_macBrushInstalled = false ;
1253 m_macFontInstalled = false ;
1254 }
1255
1256 void wxDC::MacInstallBrush() const
1257 {
1258 if (!Ok())
1259 return;
1260 MacVerifySetup() ;
1261 Pattern blackColor, whiteColor ;
1262 if ( m_macBrushInstalled )
1263 return ;
1264
1265 // foreground
1266
1267 RGBColor forecolor = m_brush.GetColour().GetPixel();
1268 RGBColor backcolor = m_backgroundBrush.GetColour().GetPixel();
1269 ::RGBForeColor( &forecolor );
1270 ::RGBBackColor( &backcolor );
1271
1272 int brushStyle = m_brush.GetStyle();
1273 if (brushStyle == wxSOLID)
1274 ::PenPat(GetQDGlobalsBlack(&blackColor));
1275 else if (IS_HATCH(brushStyle))
1276 {
1277 Pattern pat ;
1278 wxMacGetHatchPattern(brushStyle, &pat);
1279 ::PenPat(&pat);
1280 }
1281 else
1282 {
1283 ::PenPat(GetQDGlobalsBlack(&blackColor));
1284 }
1285
1286
1287 // background
1288
1289 brushStyle = m_backgroundBrush.GetStyle();
1290 if (brushStyle == wxSOLID)
1291 ::BackPat(GetQDGlobalsWhite(&whiteColor));
1292 else if (IS_HATCH(brushStyle))
1293 {
1294 Pattern pat ;
1295 wxMacGetHatchPattern(brushStyle, &pat);
1296 ::BackPat(&pat);
1297 }
1298 else
1299 {
1300 ::BackPat(GetQDGlobalsWhite(&whiteColor));
1301 }
1302
1303 short mode = patCopy ;
1304
1305 // todo :
1306
1307 switch( m_logicalFunction )
1308 {
1309 case wxCOPY: // src
1310 mode = patCopy ;
1311 break ;
1312 case wxINVERT: // NOT dst
1313 ::PenPat(GetQDGlobalsBlack(&blackColor));
1314 mode = patXor ;
1315 break ;
1316 case wxXOR: // src XOR dst
1317 mode = patXor ;
1318 break ;
1319 case wxOR_REVERSE: // src OR (NOT dst)
1320 mode = notPatOr ;
1321 break ;
1322 case wxSRC_INVERT: // (NOT src)
1323 mode = notPatCopy ;
1324 break ;
1325
1326 // unsupported TODO
1327
1328 case wxCLEAR: // 0
1329 case wxAND_REVERSE:// src AND (NOT dst)
1330 case wxAND: // src AND dst
1331 case wxAND_INVERT: // (NOT src) AND dst
1332 case wxNO_OP: // dst
1333 case wxNOR: // (NOT src) AND (NOT dst)
1334 case wxEQUIV: // (NOT src) XOR dst
1335 case wxOR_INVERT: // (NOT src) OR dst
1336 case wxNAND: // (NOT src) OR (NOT dst)
1337 case wxOR: // src OR dst
1338 case wxSET: // 1
1339 // case wxSRC_OR: // source _bitmap_ OR destination
1340 // case wxSRC_AND: // source _bitmap_ AND destination
1341 break ;
1342 }
1343 ::PenMode( mode ) ;
1344 m_macBrushInstalled = true ;
1345 m_macPenInstalled = false ;
1346 m_macFontInstalled = false ;
1347 }
1348
1349 // ---------------------------------------------------------------------------
1350 // coordinates transformations
1351 // ---------------------------------------------------------------------------
1352
1353
1354 wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
1355 {
1356 return ((wxDC *)this)->XDEV2LOG(x);
1357 }
1358
1359 wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
1360 {
1361 return ((wxDC *)this)->YDEV2LOG(y);
1362 }
1363
1364 wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
1365 {
1366 return ((wxDC *)this)->XDEV2LOGREL(x);
1367 }
1368
1369 wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
1370 {
1371 return ((wxDC *)this)->YDEV2LOGREL(y);
1372 }
1373
1374 wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
1375 {
1376 return ((wxDC *)this)->XLOG2DEV(x);
1377 }
1378
1379 wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
1380 {
1381 return ((wxDC *)this)->YLOG2DEV(y);
1382 }
1383
1384 wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
1385 {
1386 return ((wxDC *)this)->XLOG2DEVREL(x);
1387 }
1388
1389 wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
1390 {
1391 return ((wxDC *)this)->YLOG2DEVREL(y);
1392 }