forward port from 2.8
[wxWidgets.git] / src / common / dcgraph.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/graphcmn.cpp
3 // Purpose: graphics context methods common to all platforms
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created:
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if defined(__BORLANDC__)
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_GRAPHICS_CONTEXT
20
21 #include "wx/graphics.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/icon.h"
25 #include "wx/bitmap.h"
26 #include "wx/dcmemory.h"
27 #include "wx/region.h"
28 #endif
29
30 #ifdef __WXMAC__
31 #include "wx/mac/private.h"
32 #endif
33 //-----------------------------------------------------------------------------
34 // constants
35 //-----------------------------------------------------------------------------
36
37 static const double RAD2DEG = 180.0 / M_PI;
38
39 //-----------------------------------------------------------------------------
40 // Local functions
41 //-----------------------------------------------------------------------------
42
43 static inline double DegToRad(double deg)
44 {
45 return (deg * M_PI) / 180.0;
46 }
47
48 //-----------------------------------------------------------------------------
49 // wxDC bridge class
50 //-----------------------------------------------------------------------------
51
52 #ifdef __WXMAC__
53 IMPLEMENT_DYNAMIC_CLASS(wxGCDC, wxDCBase)
54 #else
55 IMPLEMENT_DYNAMIC_CLASS(wxGCDC, wxDC)
56 #endif
57
58 wxGCDC::wxGCDC()
59 {
60 Init();
61 }
62
63 void wxGCDC::SetGraphicsContext( wxGraphicsContext* ctx )
64 {
65 delete m_graphicContext;
66 m_graphicContext = ctx;
67 if ( m_graphicContext )
68 {
69 m_matrixOriginal = m_graphicContext->GetTransform();
70 m_ok = true;
71 // apply the stored transformations to the passed in context
72 ComputeScaleAndOrigin();
73 m_graphicContext->SetFont( m_font , m_textForegroundColour );
74 m_graphicContext->SetPen( m_pen );
75 m_graphicContext->SetBrush( m_brush);
76 }
77 }
78
79 wxGCDC::wxGCDC(const wxWindowDC& dc)
80 {
81 Init();
82 SetGraphicsContext( wxGraphicsContext::Create(dc) );
83 }
84
85 #ifdef __WXMSW__
86 wxGCDC::wxGCDC(const wxMemoryDC& dc)
87 {
88 Init();
89 SetGraphicsContext( wxGraphicsContext::Create(dc) );
90 }
91 #endif
92
93 void wxGCDC::Init()
94 {
95 m_ok = false;
96 m_colour = true;
97 m_mm_to_pix_x = mm2pt;
98 m_mm_to_pix_y = mm2pt;
99
100 m_pen = *wxBLACK_PEN;
101 m_font = *wxNORMAL_FONT;
102 m_brush = *wxWHITE_BRUSH;
103
104 m_graphicContext = NULL;
105 m_logicalFunctionSupported = true;
106 }
107
108
109 wxGCDC::~wxGCDC()
110 {
111 delete m_graphicContext;
112 }
113
114 void wxGCDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool WXUNUSED(useMask) )
115 {
116 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
117 wxCHECK_RET( bmp.Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
118
119 if ( bmp.GetDepth() == 1 )
120 {
121 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
122 m_graphicContext->SetBrush( wxBrush( m_textBackgroundColour , wxSOLID ) );
123 m_graphicContext->DrawRectangle( x , y , bmp.GetWidth() , bmp.GetHeight() );
124 m_graphicContext->SetBrush( wxBrush( m_textForegroundColour , wxSOLID ) );
125 m_graphicContext->DrawBitmap( bmp, x , y , bmp.GetWidth() , bmp.GetHeight() );
126 m_graphicContext->SetBrush( m_graphicContext->CreateBrush(m_brush));
127 m_graphicContext->SetPen( m_graphicContext->CreatePen(m_pen));
128 }
129 else
130 m_graphicContext->DrawBitmap( bmp, x , y , bmp.GetWidth() , bmp.GetHeight() );
131 }
132
133 void wxGCDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
134 {
135 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
136 wxCHECK_RET( icon.Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
137
138 wxCoord w = icon.GetWidth();
139 wxCoord h = icon.GetHeight();
140
141 m_graphicContext->DrawIcon( icon , x, y, w, h );
142 }
143
144 bool wxGCDC::StartDoc( const wxString& WXUNUSED(message) )
145 {
146 return false;
147 }
148
149 void wxGCDC::EndDoc()
150 {
151 }
152
153 void wxGCDC::StartPage()
154 {
155 }
156
157 void wxGCDC::EndPage()
158 {
159 }
160
161 void wxGCDC::Flush()
162 {
163 #ifdef __WXMAC__
164 CGContextFlush( (CGContextRef) m_graphicContext->GetNativeContext() );
165 #endif
166 }
167
168 void wxGCDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord w, wxCoord h )
169 {
170 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
171
172 m_graphicContext->Clip( x, y, w, h );
173 if ( m_clipping )
174 {
175 m_clipX1 = wxMax( m_clipX1, x );
176 m_clipY1 = wxMax( m_clipY1, y );
177 m_clipX2 = wxMin( m_clipX2, (x + w) );
178 m_clipY2 = wxMin( m_clipY2, (y + h) );
179 }
180 else
181 {
182 m_clipping = true;
183
184 m_clipX1 = x;
185 m_clipY1 = y;
186 m_clipX2 = x + w;
187 m_clipY2 = y + h;
188 }
189 }
190
191 void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion &region )
192 {
193 // region is in device coordinates
194 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
195
196 if (region.Empty())
197 {
198 //DestroyClippingRegion();
199 return;
200 }
201
202 wxRegion logRegion( region );
203 wxCoord x, y, w, h;
204
205 logRegion.Offset( DeviceToLogicalX(0), DeviceToLogicalY(0) );
206 logRegion.GetBox( x, y, w, h );
207
208 m_graphicContext->Clip( logRegion );
209 if ( m_clipping )
210 {
211 m_clipX1 = wxMax( m_clipX1, x );
212 m_clipY1 = wxMax( m_clipY1, y );
213 m_clipX2 = wxMin( m_clipX2, (x + w) );
214 m_clipY2 = wxMin( m_clipY2, (y + h) );
215 }
216 else
217 {
218 m_clipping = true;
219
220 m_clipX1 = x;
221 m_clipY1 = y;
222 m_clipX2 = x + w;
223 m_clipY2 = y + h;
224 }
225 }
226
227 void wxGCDC::DestroyClippingRegion()
228 {
229 m_graphicContext->ResetClip();
230 // currently the clip eg of a window extends to the area between the scrollbars
231 // so we must explicitely make sure it only covers the area we want it to draw
232 int width, height ;
233 GetSize( &width , &height ) ;
234 m_graphicContext->Clip( DeviceToLogicalX(0) , DeviceToLogicalY(0) , DeviceToLogicalXRel(width), DeviceToLogicalYRel(height) );
235
236 m_graphicContext->SetPen( m_pen );
237 m_graphicContext->SetBrush( m_brush );
238
239 m_clipping = false;
240 }
241
242 void wxGCDC::DoGetSizeMM( int* width, int* height ) const
243 {
244 int w = 0, h = 0;
245
246 GetSize( &w, &h );
247 if (width)
248 *width = long( double(w) / (m_scaleX * m_mm_to_pix_x) );
249 if (height)
250 *height = long( double(h) / (m_scaleY * m_mm_to_pix_y) );
251 }
252
253 void wxGCDC::SetTextForeground( const wxColour &col )
254 {
255 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
256
257 if ( col != m_textForegroundColour )
258 {
259 m_textForegroundColour = col;
260 m_graphicContext->SetFont( m_font, m_textForegroundColour );
261 }
262 }
263
264 void wxGCDC::SetTextBackground( const wxColour &col )
265 {
266 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
267
268 m_textBackgroundColour = col;
269 }
270
271 void wxGCDC::SetMapMode( int mode )
272 {
273 switch (mode)
274 {
275 case wxMM_TWIPS:
276 SetLogicalScale( twips2mm * m_mm_to_pix_x, twips2mm * m_mm_to_pix_y );
277 break;
278
279 case wxMM_POINTS:
280 SetLogicalScale( pt2mm * m_mm_to_pix_x, pt2mm * m_mm_to_pix_y );
281 break;
282
283 case wxMM_METRIC:
284 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
285 break;
286
287 case wxMM_LOMETRIC:
288 SetLogicalScale( m_mm_to_pix_x / 10.0, m_mm_to_pix_y / 10.0 );
289 break;
290
291 case wxMM_TEXT:
292 default:
293 SetLogicalScale( 1.0, 1.0 );
294 break;
295 }
296
297 ComputeScaleAndOrigin();
298 }
299
300 void wxGCDC::SetUserScale( double x, double y )
301 {
302 // allow negative ? -> no
303
304 m_userScaleX = x;
305 m_userScaleY = y;
306 ComputeScaleAndOrigin();
307 }
308
309 void wxGCDC::SetLogicalScale( double x, double y )
310 {
311 // allow negative ?
312 m_logicalScaleX = x;
313 m_logicalScaleY = y;
314 ComputeScaleAndOrigin();
315 }
316
317 void wxGCDC::SetLogicalOrigin( wxCoord x, wxCoord y )
318 {
319 m_logicalOriginX = x * m_signX; // is this still correct ?
320 m_logicalOriginY = y * m_signY;
321 ComputeScaleAndOrigin();
322 }
323
324 void wxGCDC::SetDeviceOrigin( wxCoord x, wxCoord y )
325 {
326 m_deviceOriginX = x;
327 m_deviceOriginY = y;
328 ComputeScaleAndOrigin();
329 }
330
331 void wxGCDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
332 {
333 m_signX = (xLeftRight ? 1 : -1);
334 m_signY = (yBottomUp ? -1 : 1);
335 ComputeScaleAndOrigin();
336 }
337
338 wxSize wxGCDC::GetPPI() const
339 {
340 return wxSize(72, 72);
341 }
342
343 int wxGCDC::GetDepth() const
344 {
345 return 32;
346 }
347
348 void wxGCDC::ComputeScaleAndOrigin()
349 {
350 m_scaleX = m_logicalScaleX * m_userScaleX;
351 m_scaleY = m_logicalScaleY * m_userScaleY;
352
353 if ( m_graphicContext )
354 {
355 m_matrixCurrent = m_graphicContext->CreateMatrix();
356 m_matrixCurrent.Translate( m_deviceOriginX, m_deviceOriginY );
357 m_matrixCurrent.Scale( m_scaleX, m_scaleY );
358 // the logical origin sets the origin to have new coordinates
359 m_matrixCurrent.Translate( -m_logicalOriginX, -m_logicalOriginY );
360
361 m_graphicContext->SetTransform( m_matrixOriginal );
362 m_graphicContext->ConcatTransform( m_matrixCurrent );
363 }
364 }
365
366 void wxGCDC::SetPalette( const wxPalette& WXUNUSED(palette) )
367 {
368
369 }
370
371 void wxGCDC::SetBackgroundMode( int mode )
372 {
373 m_backgroundMode = mode;
374 }
375
376 void wxGCDC::SetFont( const wxFont &font )
377 {
378 m_font = font;
379 if ( m_graphicContext )
380 {
381 wxFont f = font;
382 if ( f.Ok() )
383 f.SetPointSize( /*LogicalToDeviceYRel*/(font.GetPointSize()));
384 m_graphicContext->SetFont( f, m_textForegroundColour );
385 }
386 }
387
388 void wxGCDC::SetPen( const wxPen &pen )
389 {
390 if ( m_pen == pen )
391 return;
392
393 m_pen = pen;
394 if ( m_graphicContext )
395 {
396 m_graphicContext->SetPen( m_pen );
397 }
398 }
399
400 void wxGCDC::SetBrush( const wxBrush &brush )
401 {
402 if (m_brush == brush)
403 return;
404
405 m_brush = brush;
406 if ( m_graphicContext )
407 {
408 m_graphicContext->SetBrush( m_brush );
409 }
410 }
411
412 void wxGCDC::SetBackground( const wxBrush &brush )
413 {
414 if (m_backgroundBrush == brush)
415 return;
416
417 m_backgroundBrush = brush;
418 if (!m_backgroundBrush.Ok())
419 return;
420 }
421
422 void wxGCDC::SetLogicalFunction( int function )
423 {
424 if (m_logicalFunction == function)
425 return;
426
427 m_logicalFunction = function;
428 if ( m_graphicContext->SetLogicalFunction( function ) )
429 m_logicalFunctionSupported=true;
430 else
431 m_logicalFunctionSupported=false;
432 }
433
434 bool wxGCDC::DoFloodFill(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
435 const wxColour& WXUNUSED(col), int WXUNUSED(style))
436 {
437 return false;
438 }
439
440 bool wxGCDC::DoGetPixel( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxColour *WXUNUSED(col) ) const
441 {
442 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
443 return false;
444 }
445
446 void wxGCDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
447 {
448 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
449
450 if ( !m_logicalFunctionSupported )
451 return;
452
453 m_graphicContext->StrokeLine(x1,y1,x2,y2);
454
455 CalcBoundingBox(x1, y1);
456 CalcBoundingBox(x2, y2);
457 }
458
459 void wxGCDC::DoCrossHair( wxCoord x, wxCoord y )
460 {
461 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
462
463 if ( !m_logicalFunctionSupported )
464 return;
465
466 int w = 0, h = 0;
467
468 GetSize( &w, &h );
469
470 m_graphicContext->StrokeLine(0,y,w,y);
471 m_graphicContext->StrokeLine(x,0,x,h);
472
473 CalcBoundingBox(0, 0);
474 CalcBoundingBox(0+w, 0+h);
475 }
476
477 void wxGCDC::DoDrawArc( wxCoord x1, wxCoord y1,
478 wxCoord x2, wxCoord y2,
479 wxCoord xc, wxCoord yc )
480 {
481 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
482
483 if ( !m_logicalFunctionSupported )
484 return;
485
486 double dx = x1 - xc;
487 double dy = y1 - yc;
488 double radius = sqrt((double)(dx * dx + dy * dy));
489 wxCoord rad = (wxCoord)radius;
490 double sa, ea;
491 if (x1 == x2 && y1 == y2)
492 {
493 sa = 0.0;
494 ea = 360.0;
495 }
496 else if (radius == 0.0)
497 {
498 sa = ea = 0.0;
499 }
500 else
501 {
502 sa = (x1 - xc == 0) ?
503 (y1 - yc < 0) ? 90.0 : -90.0 :
504 -atan2(double(y1 - yc), double(x1 - xc)) * RAD2DEG;
505 ea = (x2 - xc == 0) ?
506 (y2 - yc < 0) ? 90.0 : -90.0 :
507 -atan2(double(y2 - yc), double(x2 - xc)) * RAD2DEG;
508 }
509
510 bool fill = m_brush.GetStyle() != wxTRANSPARENT;
511
512 wxGraphicsPath path = m_graphicContext->CreatePath();
513 if ( fill && ((x1!=x2)||(y1!=y2)) )
514 path.MoveToPoint( xc, yc );
515 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
516 // get clockwise angles
517 path.AddArc( xc, yc , rad , DegToRad(-sa) , DegToRad(-ea), false );
518 if ( fill && ((x1!=x2)||(y1!=y2)) )
519 path.AddLineToPoint( xc, yc );
520 m_graphicContext->DrawPath(path);
521 }
522
523 void wxGCDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
524 double sa, double ea )
525 {
526 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
527
528 if ( !m_logicalFunctionSupported )
529 return;
530
531 m_graphicContext->PushState();
532 m_graphicContext->Translate(x+w/2.0,y+h/2.0);
533 wxDouble factor = ((wxDouble) w) / h;
534 m_graphicContext->Scale( factor , 1.0);
535
536 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
537 // get clockwise angles
538 if ( m_brush.GetStyle() != wxTRANSPARENT )
539 {
540 wxGraphicsPath path = m_graphicContext->CreatePath();
541 path.MoveToPoint( 0, 0 );
542 path.AddLineToPoint( h / 2.0 * cos(DegToRad(sa)) , h / 2.0 * sin(DegToRad(-sa)) );
543 path.AddLineToPoint( h / 2.0 * cos(DegToRad(ea)) , h / 2.0 * sin(DegToRad(-ea)) );
544 path.AddLineToPoint( 0, 0 );
545 m_graphicContext->FillPath( path );
546
547 path = m_graphicContext->CreatePath();
548 path.AddArc( 0, 0, h/2.0 , DegToRad(-sa) , DegToRad(-ea), sa > ea );
549 m_graphicContext->FillPath( path );
550 m_graphicContext->StrokePath( path );
551 }
552 else
553 {
554 wxGraphicsPath path = m_graphicContext->CreatePath();
555 path.AddArc( 0, 0, h/2.0 , DegToRad(-sa) , DegToRad(-ea), sa > ea );
556 m_graphicContext->DrawPath( path );
557 }
558
559 m_graphicContext->PopState();
560 }
561
562 void wxGCDC::DoDrawPoint( wxCoord x, wxCoord y )
563 {
564 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
565
566 DoDrawLine( x , y , x + 1 , y + 1 );
567 }
568
569 void wxGCDC::DoDrawLines(int n, wxPoint points[],
570 wxCoord xoffset, wxCoord yoffset)
571 {
572 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
573
574 if ( !m_logicalFunctionSupported )
575 return;
576
577 wxPoint2DDouble* pointsD = new wxPoint2DDouble[n];
578 for( int i = 0; i < n; ++i)
579 {
580 pointsD[i].m_x = points[i].x + xoffset;
581 pointsD[i].m_y = points[i].y + yoffset;
582 }
583
584 m_graphicContext->StrokeLines( n , pointsD);
585 delete[] pointsD;
586 }
587
588 #if wxUSE_SPLINES
589 void wxGCDC::DoDrawSpline(const wxPointList *points)
590 {
591 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
592
593 if ( !m_logicalFunctionSupported )
594 return;
595
596 wxGraphicsPath path = m_graphicContext->CreatePath();
597
598 wxPointList::compatibility_iterator node = points->GetFirst();
599 if (node == wxPointList::compatibility_iterator())
600 // empty list
601 return;
602
603 wxPoint *p = node->GetData();
604
605 wxCoord x1 = p->x;
606 wxCoord y1 = p->y;
607
608 node = node->GetNext();
609 p = node->GetData();
610
611 wxCoord x2 = p->x;
612 wxCoord y2 = p->y;
613 wxCoord cx1 = ( x1 + x2 ) / 2;
614 wxCoord cy1 = ( y1 + y2 ) / 2;
615
616 path.MoveToPoint( x1 , y1 );
617 path.AddLineToPoint( cx1 , cy1 );
618 #if !wxUSE_STL
619
620 while ((node = node->GetNext()) != NULL)
621 #else
622
623 while ((node = node->GetNext()))
624 #endif // !wxUSE_STL
625
626 {
627 p = node->GetData();
628 x1 = x2;
629 y1 = y2;
630 x2 = p->x;
631 y2 = p->y;
632 wxCoord cx4 = (x1 + x2) / 2;
633 wxCoord cy4 = (y1 + y2) / 2;
634
635 path.AddQuadCurveToPoint(x1 , y1 ,cx4 , cy4 );
636
637 cx1 = cx4;
638 cy1 = cy4;
639 }
640
641 path.AddLineToPoint( x2 , y2 );
642
643 m_graphicContext->StrokePath( path );
644 }
645 #endif // wxUSE_SPLINES
646
647 void wxGCDC::DoDrawPolygon( int n, wxPoint points[],
648 wxCoord xoffset, wxCoord yoffset,
649 int fillStyle )
650 {
651 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
652
653 if ( n <= 0 || (m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT ) )
654 return;
655 if ( !m_logicalFunctionSupported )
656 return;
657
658 bool closeIt = false;
659 if (points[n-1] != points[0])
660 closeIt = true;
661
662 wxPoint2DDouble* pointsD = new wxPoint2DDouble[n+(closeIt?1:0)];
663 for( int i = 0; i < n; ++i)
664 {
665 pointsD[i].m_x = points[i].x + xoffset;
666 pointsD[i].m_y = points[i].y + yoffset;
667 }
668 if ( closeIt )
669 pointsD[n] = pointsD[0];
670
671 m_graphicContext->DrawLines( n+(closeIt?1:0) , pointsD, fillStyle);
672 delete[] pointsD;
673 }
674
675 void wxGCDC::DoDrawPolyPolygon(int n,
676 int count[],
677 wxPoint points[],
678 wxCoord xoffset,
679 wxCoord yoffset,
680 int fillStyle)
681 {
682 wxASSERT(n > 1);
683 wxGraphicsPath path = m_graphicContext->CreatePath();
684
685 int i = 0;
686 for ( int j = 0; j < n; ++j)
687 {
688 wxPoint start = points[i];
689 path.MoveToPoint( start.x+ xoffset, start.y+ yoffset);
690 ++i;
691 int l = count[j];
692 for ( int k = 1; k < l; ++k)
693 {
694 path.AddLineToPoint( points[i].x+ xoffset, points[i].y+ yoffset);
695 ++i;
696 }
697 // close the polygon
698 if ( start != points[i-1])
699 path.AddLineToPoint( start.x+ xoffset, start.y+ yoffset);
700 }
701 m_graphicContext->DrawPath( path , fillStyle);
702 }
703
704 void wxGCDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
705 {
706 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
707
708 if ( !m_logicalFunctionSupported )
709 return;
710
711 // CMB: draw nothing if transformed w or h is 0
712 if (w == 0 || h == 0)
713 return;
714
715 if ( m_graphicContext->ShouldOffset() )
716 {
717 // if we are offsetting the entire rectangle is moved 0.5, so the
718 // border line gets off by 1
719 w -= 1;
720 h -= 1;
721 }
722 m_graphicContext->DrawRectangle(x,y,w,h);
723 }
724
725 void wxGCDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
726 wxCoord w, wxCoord h,
727 double radius)
728 {
729 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
730
731 if ( !m_logicalFunctionSupported )
732 return;
733
734 if (radius < 0.0)
735 radius = - radius * ((w < h) ? w : h);
736
737 // CMB: draw nothing if transformed w or h is 0
738 if (w == 0 || h == 0)
739 return;
740
741 if ( m_graphicContext->ShouldOffset() )
742 {
743 // if we are offsetting the entire rectangle is moved 0.5, so the
744 // border line gets off by 1
745 w -= 1;
746 h -= 1;
747 }
748 m_graphicContext->DrawRoundedRectangle( x,y,w,h,radius);
749 }
750
751 void wxGCDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
752 {
753 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
754
755 if ( !m_logicalFunctionSupported )
756 return;
757
758 if ( m_graphicContext->ShouldOffset() )
759 {
760 // if we are offsetting the entire rectangle is moved 0.5, so the
761 // border line gets off by 1
762 w -= 1;
763 h -= 1;
764 }
765 m_graphicContext->DrawEllipse(x,y,w,h);
766 }
767
768 bool wxGCDC::CanDrawBitmap() const
769 {
770 return true;
771 }
772
773 bool wxGCDC::DoBlit(
774 wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
775 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool useMask,
776 wxCoord xsrcMask, wxCoord ysrcMask )
777 {
778 return DoStretchBlit( xdest, ydest, width, height,
779 source, xsrc, ysrc, width, height, logical_func, useMask,
780 xsrcMask,ysrcMask );
781 }
782
783 bool wxGCDC::DoStretchBlit(
784 wxCoord xdest, wxCoord ydest, wxCoord dstWidth, wxCoord dstHeight,
785 wxDC *source, wxCoord xsrc, wxCoord ysrc, wxCoord srcWidth, wxCoord srcHeight,
786 int logical_func , bool WXUNUSED(useMask),
787 wxCoord xsrcMask, wxCoord ysrcMask )
788 {
789 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid DC") );
790 wxCHECK_MSG( source->Ok(), false, wxT("wxGCDC(cg)::DoStretchBlit - invalid source DC") );
791
792 if ( logical_func == wxNO_OP )
793 return true;
794 else if ( !m_graphicContext->SetLogicalFunction( logical_func ) )
795
796 {
797 wxFAIL_MSG( wxT("Blitting is only supported with wxCOPY logical operation.") );
798 return false;
799 }
800
801 if (xsrcMask == -1 && ysrcMask == -1)
802 {
803 xsrcMask = xsrc;
804 ysrcMask = ysrc;
805 }
806
807 wxRect subrect(source->LogicalToDeviceX(xsrc),
808 source->LogicalToDeviceY(ysrc),
809 source->LogicalToDeviceXRel(srcWidth),
810 source->LogicalToDeviceYRel(srcHeight));
811
812 // if needed clip the subrect down to the size of the source DC
813 wxCoord sw, sh;
814 source->GetSize(&sw, &sh);
815 sw = source->LogicalToDeviceXRel(sw);
816 sh = source->LogicalToDeviceYRel(sh);
817 if (subrect.x + subrect.width > sw)
818 subrect.width = sw - subrect.x;
819 if (subrect.y + subrect.height > sh)
820 subrect.height = sh - subrect.y;
821
822 wxBitmap blit = source->GetAsBitmap( &subrect );
823
824 if ( blit.Ok() )
825 {
826 m_graphicContext->DrawBitmap( blit, xdest, ydest,
827 dstWidth, dstHeight);
828 }
829 else
830 {
831 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
832 return false;
833 }
834
835 // reset logical function
836 m_graphicContext->SetLogicalFunction( m_logicalFunction );
837
838 return true;
839 }
840
841 void wxGCDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y,
842 double angle)
843 {
844 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
845
846 if ( str.length() == 0 )
847 return;
848 if ( !m_logicalFunctionSupported )
849 return;
850
851 if ( m_backgroundMode == wxTRANSPARENT )
852 m_graphicContext->DrawText( str, x ,y , DegToRad(angle ));
853 else
854 m_graphicContext->DrawText( str, x ,y , DegToRad(angle ), m_graphicContext->CreateBrush( wxBrush(m_textBackgroundColour,wxSOLID) ) );
855 }
856
857 void wxGCDC::DoDrawText(const wxString& str, wxCoord x, wxCoord y)
858 {
859 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
860
861 if ( str.length() == 0 )
862 return;
863
864 if ( !m_logicalFunctionSupported )
865 return;
866
867 if ( m_backgroundMode == wxTRANSPARENT )
868 m_graphicContext->DrawText( str, x ,y);
869 else
870 m_graphicContext->DrawText( str, x ,y , m_graphicContext->CreateBrush( wxBrush(m_textBackgroundColour,wxSOLID) ) );
871 }
872
873 bool wxGCDC::CanGetTextExtent() const
874 {
875 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
876
877 return true;
878 }
879
880 void wxGCDC::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *height,
881 wxCoord *descent, wxCoord *externalLeading ,
882 const wxFont *theFont ) const
883 {
884 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
885
886 if ( theFont )
887 {
888 m_graphicContext->SetFont( *theFont, m_textForegroundColour );
889 }
890
891 wxDouble h , d , e , w;
892
893 m_graphicContext->GetTextExtent( str, &w, &h, &d, &e );
894
895 if ( height )
896 *height = (wxCoord)(h+0.5);
897 if ( descent )
898 *descent = (wxCoord)(d+0.5);
899 if ( externalLeading )
900 *externalLeading = (wxCoord)(e+0.5);
901 if ( width )
902 *width = (wxCoord)(w+0.5);
903
904 if ( theFont )
905 {
906 m_graphicContext->SetFont( m_font, m_textForegroundColour );
907 }
908 }
909
910 bool wxGCDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
911 {
912 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
913 widths.Clear();
914 widths.Add(0,text.Length());
915 if ( text.IsEmpty() )
916 return true;
917
918 wxArrayDouble widthsD;
919
920 m_graphicContext->GetPartialTextExtents( text, widthsD );
921 for ( size_t i = 0; i < widths.GetCount(); ++i )
922 widths[i] = (wxCoord)(widthsD[i] + 0.5);
923
924 return true;
925 }
926
927 wxCoord wxGCDC::GetCharWidth(void) const
928 {
929 wxCoord width;
930 DoGetTextExtent( wxT("g") , &width , NULL , NULL , NULL , NULL );
931
932 return width;
933 }
934
935 wxCoord wxGCDC::GetCharHeight(void) const
936 {
937 wxCoord height;
938 DoGetTextExtent( wxT("g") , NULL , &height , NULL , NULL , NULL );
939
940 return height;
941 }
942
943 void wxGCDC::Clear(void)
944 {
945 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
946 // TODO better implementation / incorporate size info into wxGCDC or context
947 m_graphicContext->SetBrush( m_backgroundBrush );
948 wxPen p = *wxTRANSPARENT_PEN;
949 m_graphicContext->SetPen( p );
950 DoDrawRectangle( 0, 0, 32000 , 32000 );
951 m_graphicContext->SetPen( m_pen );
952 m_graphicContext->SetBrush( m_brush );
953 }
954
955 void wxGCDC::DoGetSize(int *width, int *height) const
956 {
957 *width = 10000;
958 *height = 10000;
959 }
960
961 void wxGCDC::DoGradientFillLinear(const wxRect& rect,
962 const wxColour& initialColour,
963 const wxColour& destColour,
964 wxDirection nDirection )
965 {
966 wxPoint start;
967 wxPoint end;
968 switch( nDirection)
969 {
970 case wxWEST :
971 start = rect.GetRightBottom();
972 start.x++;
973 end = rect.GetLeftBottom();
974 break;
975 case wxEAST :
976 start = rect.GetLeftBottom();
977 end = rect.GetRightBottom();
978 end.x++;
979 break;
980 case wxNORTH :
981 start = rect.GetLeftBottom();
982 start.y++;
983 end = rect.GetLeftTop();
984 break;
985 case wxSOUTH :
986 start = rect.GetLeftTop();
987 end = rect.GetLeftBottom();
988 end.y++;
989 break;
990 default :
991 break;
992 }
993
994 if (rect.width == 0 || rect.height == 0)
995 return;
996
997 m_graphicContext->SetBrush( m_graphicContext->CreateLinearGradientBrush(
998 start.x,start.y,end.x,end.y, initialColour, destColour));
999 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
1000 m_graphicContext->DrawRectangle(rect.x,rect.y,rect.width,rect.height);
1001 m_graphicContext->SetPen(m_pen);
1002 }
1003
1004 void wxGCDC::DoGradientFillConcentric(const wxRect& rect,
1005 const wxColour& initialColour,
1006 const wxColour& destColour,
1007 const wxPoint& circleCenter)
1008 {
1009 //Radius
1010 wxInt32 cx = rect.GetWidth() / 2;
1011 wxInt32 cy = rect.GetHeight() / 2;
1012 wxInt32 nRadius;
1013 if (cx < cy)
1014 nRadius = cx;
1015 else
1016 nRadius = cy;
1017
1018 // make sure the background is filled (todo move into specific platform implementation ?)
1019 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
1020 m_graphicContext->SetBrush( wxBrush( destColour) );
1021 m_graphicContext->DrawRectangle(rect.x,rect.y,rect.width,rect.height);
1022
1023 m_graphicContext->SetBrush( m_graphicContext->CreateRadialGradientBrush(
1024 rect.x+circleCenter.x,rect.y+circleCenter.y,
1025 rect.x+circleCenter.x,rect.y+circleCenter.y,
1026 nRadius,initialColour,destColour));
1027
1028 m_graphicContext->DrawRectangle(rect.x,rect.y,rect.width,rect.height);
1029 m_graphicContext->SetPen(m_pen);
1030 }
1031
1032 void wxGCDC::DoDrawCheckMark(wxCoord x, wxCoord y,
1033 wxCoord width, wxCoord height)
1034 {
1035 wxDCBase::DoDrawCheckMark(x,y,width,height);
1036 }
1037
1038 #endif // wxUSE_GRAPHICS_CONTEXT