move the graphics context bridge dc into separate file
[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 //-----------------------------------------------------------------------------
31 // constants
32 //-----------------------------------------------------------------------------
33
34 static const double RAD2DEG = 180.0 / M_PI;
35
36 //-----------------------------------------------------------------------------
37 // Local functions
38 //-----------------------------------------------------------------------------
39
40 static inline double DegToRad(double deg)
41 {
42 return (deg * M_PI) / 180.0;
43 }
44
45 //-----------------------------------------------------------------------------
46 // wxDC bridge class
47 //-----------------------------------------------------------------------------
48
49 #ifdef __WXMAC__
50 IMPLEMENT_DYNAMIC_CLASS(wxGCDC, wxDCBase)
51 #else
52 IMPLEMENT_DYNAMIC_CLASS(wxGCDC, wxDC)
53 #endif
54
55 wxGCDC::wxGCDC()
56 {
57 Init();
58 }
59
60 void wxGCDC::SetGraphicsContext( wxGraphicsContext* ctx )
61 {
62 delete m_graphicContext;
63 m_graphicContext = ctx;
64 }
65
66 wxGCDC::wxGCDC(const wxWindowDC& dc)
67 {
68 Init();
69 m_graphicContext = wxGraphicsContext::Create(dc);
70 m_ok = true;
71 if ( dc.GetFont().Ok())
72 m_graphicContext->SetFont( m_graphicContext->CreateFont(dc.GetFont(),dc.GetTextForeground()));
73 if ( dc.GetPen().Ok())
74 m_graphicContext->SetPen( m_graphicContext->CreatePen(dc.GetPen()));
75 if ( dc.GetBrush().Ok())
76 m_graphicContext->SetBrush( m_graphicContext->CreateBrush(dc.GetBrush()));
77 }
78
79 void wxGCDC::Init()
80 {
81 m_ok = false;
82 m_colour = true;
83 m_mm_to_pix_x = mm2pt;
84 m_mm_to_pix_y = mm2pt;
85
86 m_pen = *wxBLACK_PEN;
87 m_font = *wxNORMAL_FONT;
88 m_brush = *wxWHITE_BRUSH;
89
90 m_graphicContext = NULL;
91 }
92
93
94 wxGCDC::~wxGCDC()
95 {
96 delete m_graphicContext;
97 }
98
99 void wxGCDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool WXUNUSED(useMask) )
100 {
101 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid DC") );
102 wxCHECK_RET( bmp.Ok(), wxT("wxGCDC(cg)::DoDrawBitmap - invalid bitmap") );
103
104 wxCoord xx = LogicalToDeviceX(x);
105 wxCoord yy = LogicalToDeviceY(y);
106 wxCoord w = bmp.GetWidth();
107 wxCoord h = bmp.GetHeight();
108 wxCoord ww = LogicalToDeviceXRel(w);
109 wxCoord hh = LogicalToDeviceYRel(h);
110
111 m_graphicContext->DrawBitmap( bmp, xx , yy , ww , hh );
112 }
113
114 void wxGCDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
115 {
116 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid DC") );
117 wxCHECK_RET( icon.Ok(), wxT("wxGCDC(cg)::DoDrawIcon - invalid icon") );
118
119 wxCoord xx = LogicalToDeviceX(x);
120 wxCoord yy = LogicalToDeviceY(y);
121 wxCoord w = icon.GetWidth();
122 wxCoord h = icon.GetHeight();
123 wxCoord ww = LogicalToDeviceXRel(w);
124 wxCoord hh = LogicalToDeviceYRel(h);
125
126 m_graphicContext->DrawIcon( icon , xx, yy, ww, hh );
127 }
128
129 void wxGCDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
130 {
131 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegion - invalid DC") );
132
133 wxCoord xx, yy, ww, hh;
134 xx = LogicalToDeviceX(x);
135 yy = LogicalToDeviceY(y);
136 ww = LogicalToDeviceXRel(width);
137 hh = LogicalToDeviceYRel(height);
138
139 m_graphicContext->Clip( xx, yy, ww, hh );
140 if ( m_clipping )
141 {
142 m_clipX1 = wxMax( m_clipX1, xx );
143 m_clipY1 = wxMax( m_clipY1, yy );
144 m_clipX2 = wxMin( m_clipX2, (xx + ww) );
145 m_clipY2 = wxMin( m_clipY2, (yy + hh) );
146 }
147 else
148 {
149 m_clipping = true;
150
151 m_clipX1 = xx;
152 m_clipY1 = yy;
153 m_clipX2 = xx + ww;
154 m_clipY2 = yy + hh;
155 }
156 }
157
158 void wxGCDC::DoSetClippingRegionAsRegion( const wxRegion &region )
159 {
160 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
161
162 if (region.Empty())
163 {
164 DestroyClippingRegion();
165 return;
166 }
167
168 wxCoord x, y, w, h;
169 region.GetBox( x, y, w, h );
170 wxCoord xx, yy, ww, hh;
171 xx = LogicalToDeviceX(x);
172 yy = LogicalToDeviceY(y);
173 ww = LogicalToDeviceXRel(w);
174 hh = LogicalToDeviceYRel(h);
175
176 // if we have a scaling that we cannot map onto native regions
177 // we must use the box
178 if ( ww != w || hh != h )
179 {
180 wxGCDC::DoSetClippingRegion( x, y, w, h );
181 }
182 else
183 {
184 m_graphicContext->Clip( region );
185 if ( m_clipping )
186 {
187 m_clipX1 = wxMax( m_clipX1, xx );
188 m_clipY1 = wxMax( m_clipY1, yy );
189 m_clipX2 = wxMin( m_clipX2, (xx + ww) );
190 m_clipY2 = wxMin( m_clipY2, (yy + hh) );
191 }
192 else
193 {
194 m_clipping = true;
195
196 m_clipX1 = xx;
197 m_clipY1 = yy;
198 m_clipX2 = xx + ww;
199 m_clipY2 = yy + hh;
200 }
201 }
202 }
203
204 void wxGCDC::DestroyClippingRegion()
205 {
206 m_graphicContext->ResetClip();
207 m_graphicContext->SetPen( m_pen );
208 m_graphicContext->SetBrush( m_brush );
209
210 m_clipping = false;
211 }
212
213 void wxGCDC::DoGetSizeMM( int* width, int* height ) const
214 {
215 int w = 0, h = 0;
216
217 GetSize( &w, &h );
218 if (width)
219 *width = long( double(w) / (m_scaleX * m_mm_to_pix_x) );
220 if (height)
221 *height = long( double(h) / (m_scaleY * m_mm_to_pix_y) );
222 }
223
224 void wxGCDC::SetTextForeground( const wxColour &col )
225 {
226 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextForeground - invalid DC") );
227
228 if ( col != m_textForegroundColour )
229 {
230 m_textForegroundColour = col;
231 m_graphicContext->SetFont( m_font, m_textForegroundColour );
232 }
233 }
234
235 void wxGCDC::SetTextBackground( const wxColour &col )
236 {
237 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
238
239 m_textBackgroundColour = col;
240 }
241
242 void wxGCDC::SetMapMode( int mode )
243 {
244 switch (mode)
245 {
246 case wxMM_TWIPS:
247 SetLogicalScale( twips2mm * m_mm_to_pix_x, twips2mm * m_mm_to_pix_y );
248 break;
249
250 case wxMM_POINTS:
251 SetLogicalScale( pt2mm * m_mm_to_pix_x, pt2mm * m_mm_to_pix_y );
252 break;
253
254 case wxMM_METRIC:
255 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
256 break;
257
258 case wxMM_LOMETRIC:
259 SetLogicalScale( m_mm_to_pix_x / 10.0, m_mm_to_pix_y / 10.0 );
260 break;
261
262 case wxMM_TEXT:
263 default:
264 SetLogicalScale( 1.0, 1.0 );
265 break;
266 }
267
268 ComputeScaleAndOrigin();
269 }
270
271 void wxGCDC::SetUserScale( double x, double y )
272 {
273 // allow negative ? -> no
274
275 m_userScaleX = x;
276 m_userScaleY = y;
277 ComputeScaleAndOrigin();
278 }
279
280 void wxGCDC::SetLogicalScale( double x, double y )
281 {
282 // allow negative ?
283 m_logicalScaleX = x;
284 m_logicalScaleY = y;
285 ComputeScaleAndOrigin();
286 }
287
288 void wxGCDC::SetLogicalOrigin( wxCoord x, wxCoord y )
289 {
290 m_logicalOriginX = x * m_signX; // is this still correct ?
291 m_logicalOriginY = y * m_signY;
292 ComputeScaleAndOrigin();
293 }
294
295 void wxGCDC::SetDeviceOrigin( wxCoord x, wxCoord y )
296 {
297 m_deviceOriginX = x;
298 m_deviceOriginY = y;
299 ComputeScaleAndOrigin();
300 }
301
302 void wxGCDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
303 {
304 m_signX = (xLeftRight ? 1 : -1);
305 m_signY = (yBottomUp ? -1 : 1);
306 ComputeScaleAndOrigin();
307 }
308
309 wxSize wxGCDC::GetPPI() const
310 {
311 return wxSize(72, 72);
312 }
313
314 int wxGCDC::GetDepth() const
315 {
316 return 32;
317 }
318
319 void wxGCDC::ComputeScaleAndOrigin()
320 {
321 // CMB: copy scale to see if it changes
322 double origScaleX = m_scaleX;
323 double origScaleY = m_scaleY;
324 m_scaleX = m_logicalScaleX * m_userScaleX;
325 m_scaleY = m_logicalScaleY * m_userScaleY;
326 m_deviceOriginX = m_deviceOriginX + m_logicalOriginX;
327 m_deviceOriginY = m_deviceOriginY + m_logicalOriginY;
328
329 // CMB: if scale has changed call SetPen to recalulate the line width
330 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
331 {
332 // this is a bit artificial, but we need to force wxDC to think
333 // the pen has changed
334 wxPen pen(GetPen());
335 m_pen = wxNullPen;
336 SetPen(pen);
337 SetFont(m_font);
338 }
339 }
340
341 void wxGCDC::SetPalette( const wxPalette& WXUNUSED(palette) )
342 {
343
344 }
345
346 void wxGCDC::SetBackgroundMode( int mode )
347 {
348 m_backgroundMode = mode;
349 }
350
351 void wxGCDC::SetFont( const wxFont &font )
352 {
353 m_font = font;
354 if ( m_graphicContext )
355 {
356 wxFont f = font;
357 if ( f.Ok() )
358 f.SetPointSize( LogicalToDeviceYRel(font.GetPointSize()));
359 m_graphicContext->SetFont( f, m_textForegroundColour );
360 }
361 }
362
363 void wxGCDC::SetPen( const wxPen &pen )
364 {
365 if ( m_pen == pen )
366 return;
367
368 m_pen = pen;
369 if ( m_graphicContext )
370 {
371 if ( m_pen.GetStyle() == wxSOLID || m_pen.GetStyle() == wxTRANSPARENT )
372 {
373 m_graphicContext->SetPen( m_pen );
374 }
375 else
376 {
377 // we have to compensate for moved device origins etc. otherwise patterned pens are standing still
378 // eg when using a wxScrollWindow and scrolling around
379 int origX = LogicalToDeviceX( 0 );
380 int origY = LogicalToDeviceY( 0 );
381 m_graphicContext->Translate( origX , origY );
382 m_graphicContext->SetPen( m_pen );
383 m_graphicContext->Translate( -origX , -origY );
384 }
385 }
386 }
387
388 void wxGCDC::SetBrush( const wxBrush &brush )
389 {
390 if (m_brush == brush)
391 return;
392
393 m_brush = brush;
394 if ( m_graphicContext )
395 {
396 if ( brush.GetStyle() == wxSOLID || brush.GetStyle() == wxTRANSPARENT )
397 {
398 m_graphicContext->SetBrush( m_brush );
399 }
400 else
401 {
402 // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still
403 // eg when using a wxScrollWindow and scrolling around
404 // TODO on MSW / GDIPlus this still occurs with hatched brushes
405 int origX = LogicalToDeviceX(0);
406 int origY = LogicalToDeviceY(0);
407 m_graphicContext->Translate( origX , origY );
408 m_graphicContext->SetBrush( m_brush );
409 m_graphicContext->Translate( -origX , -origY );
410 }
411 }
412 }
413
414 void wxGCDC::SetBackground( const wxBrush &brush )
415 {
416 if (m_backgroundBrush == brush)
417 return;
418
419 m_backgroundBrush = brush;
420 if (!m_backgroundBrush.Ok())
421 return;
422 }
423
424 void wxGCDC::SetLogicalFunction( int function )
425 {
426 if (m_logicalFunction == function)
427 return;
428
429 m_logicalFunction = function;
430 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
431
432 CGContextRef cgContext = ((wxCairoContext*)(m_graphicContext))->GetNativeContext();
433 if ( m_logicalFunction == wxCOPY )
434 CGContextSetBlendMode( cgContext, kCGBlendModeNormal );
435 else if ( m_logicalFunction == wxINVERT )
436 CGContextSetBlendMode( cgContext, kCGBlendModeExclusion );
437 else
438 CGContextSetBlendMode( cgContext, kCGBlendModeNormal );
439 #endif
440
441 }
442
443 bool wxGCDC::DoFloodFill(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
444 const wxColour& WXUNUSED(col), int WXUNUSED(style))
445 {
446 return false;
447 }
448
449 bool wxGCDC::DoGetPixel( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxColour *WXUNUSED(col) ) const
450 {
451 // wxCHECK_MSG( 0 , false, wxT("wxGCDC(cg)::DoGetPixel - not implemented") );
452 return false;
453 }
454
455 void wxGCDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
456 {
457 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLine - invalid DC") );
458
459 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
460
461 if ( m_logicalFunction != wxCOPY )
462 return;
463 #endif
464
465 wxCoord xx1 = LogicalToDeviceX(x1);
466 wxCoord yy1 = LogicalToDeviceY(y1);
467 wxCoord xx2 = LogicalToDeviceX(x2);
468 wxCoord yy2 = LogicalToDeviceY(y2);
469
470 m_graphicContext->StrokeLine(xx1,yy1,xx2,yy2);
471
472 CalcBoundingBox(x1, y1);
473 CalcBoundingBox(x2, y2);
474 }
475
476 void wxGCDC::DoCrossHair( wxCoord x, wxCoord y )
477 {
478 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoCrossHair - invalid DC") );
479
480 if ( m_logicalFunction != wxCOPY )
481 return;
482
483 int w = 0, h = 0;
484
485 GetSize( &w, &h );
486
487 wxCoord xx = LogicalToDeviceX(x);
488 wxCoord yy = LogicalToDeviceY(y);
489 wxCoord xw = LogicalToDeviceX(w);
490 wxCoord x0 = LogicalToDeviceX(0);
491 wxCoord y0 = LogicalToDeviceY(0);
492 wxCoord yh = LogicalToDeviceY(h);
493
494 m_graphicContext->StrokeLine(x0,yy,xw,yy);
495 m_graphicContext->StrokeLine(xx,y0,xx,yh);
496
497 CalcBoundingBox(x0, y0);
498 CalcBoundingBox(x0+xw, y0+yh);
499 }
500
501 void wxGCDC::DoDrawArc( wxCoord x1, wxCoord y1,
502 wxCoord x2, wxCoord y2,
503 wxCoord xc, wxCoord yc )
504 {
505 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawArc - invalid DC") );
506
507 if ( m_logicalFunction != wxCOPY )
508 return;
509
510 wxCoord xx1 = LogicalToDeviceX(x1);
511 wxCoord yy1 = LogicalToDeviceY(y1);
512 wxCoord xx2 = LogicalToDeviceX(x2);
513 wxCoord yy2 = LogicalToDeviceY(y2);
514 wxCoord xxc = LogicalToDeviceX(xc);
515 wxCoord yyc = LogicalToDeviceY(yc);
516
517 double dx = xx1 - xxc;
518 double dy = yy1 - yyc;
519 double radius = sqrt((double)(dx * dx + dy * dy));
520 wxCoord rad = (wxCoord)radius;
521 double sa, ea;
522 if (xx1 == xx2 && yy1 == yy2)
523 {
524 sa = 0.0;
525 ea = 360.0;
526 }
527 else if (radius == 0.0)
528 {
529 sa = ea = 0.0;
530 }
531 else
532 {
533 sa = (xx1 - xxc == 0) ?
534 (yy1 - yyc < 0) ? 90.0 : -90.0 :
535 -atan2(double(yy1 - yyc), double(xx1 - xxc)) * RAD2DEG;
536 ea = (xx2 - xxc == 0) ?
537 (yy2 - yyc < 0) ? 90.0 : -90.0 :
538 -atan2(double(yy2 - yyc), double(xx2 - xxc)) * RAD2DEG;
539 }
540
541 bool fill = m_brush.GetStyle() != wxTRANSPARENT;
542
543 wxGraphicsPath* path = m_graphicContext->CreatePath();
544 if ( fill && ((x1!=x2)||(y1!=y2)) )
545 path->MoveToPoint( xxc, yyc );
546 path->AddArc( xxc, yyc , rad , DegToRad(sa) , DegToRad(ea), false );
547 if ( fill && ((x1!=x2)||(y1!=y2)) )
548 path->AddLineToPoint( xxc, yyc );
549 m_graphicContext->DrawPath(path);
550 delete path;
551 }
552
553 void wxGCDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
554 double sa, double ea )
555 {
556 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
557
558 if ( m_logicalFunction != wxCOPY )
559 return;
560
561 wxCoord xx = LogicalToDeviceX(x);
562 wxCoord yy = LogicalToDeviceY(y);
563 wxCoord ww = m_signX * LogicalToDeviceXRel(w);
564 wxCoord hh = m_signY * LogicalToDeviceYRel(h);
565
566 // handle -ve width and/or height
567 if (ww < 0)
568 {
569 ww = -ww;
570 xx = xx - ww;
571 }
572 if (hh < 0)
573 {
574 hh = -hh;
575 yy = yy - hh;
576 }
577
578 bool fill = m_brush.GetStyle() != wxTRANSPARENT;
579
580 wxGraphicsPath* path = m_graphicContext->CreatePath();
581 m_graphicContext->PushState();
582 m_graphicContext->Translate(xx+ww/2,yy+hh/2);
583 wxDouble factor = ((wxDouble) ww) / hh;
584 m_graphicContext->Scale( factor , 1.0);
585 if ( fill && (sa!=ea) )
586 path->MoveToPoint(0,0);
587 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
588 // get clockwise angles
589 path->AddArc( 0, 0, hh/2 , DegToRad(-sa) , DegToRad(-ea), sa > ea );
590 if ( fill && (sa!=ea) )
591 path->AddLineToPoint(0,0);
592 m_graphicContext->DrawPath( path );
593 m_graphicContext->PopState();
594 delete path;
595 }
596
597 void wxGCDC::DoDrawPoint( wxCoord x, wxCoord y )
598 {
599 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
600
601 DoDrawLine( x , y , x + 1 , y + 1 );
602 }
603
604 void wxGCDC::DoDrawLines(int n, wxPoint points[],
605 wxCoord xoffset, wxCoord yoffset)
606 {
607 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
608
609 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
610
611 if ( m_logicalFunction != wxCOPY )
612 return;
613 #endif
614
615 wxPoint2DDouble* pointsD = new wxPoint2DDouble[n];
616 for( int i = 0; i < n; ++i)
617 {
618 pointsD[i].m_x = LogicalToDeviceX(points[i].x + xoffset);
619 pointsD[i].m_y = LogicalToDeviceY(points[i].y + yoffset);
620 }
621
622 m_graphicContext->StrokeLines( n , pointsD);
623 delete[] pointsD;
624 }
625
626 #if wxUSE_SPLINES
627 void wxGCDC::DoDrawSpline(wxList *points)
628 {
629 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
630
631 if ( m_logicalFunction != wxCOPY )
632 return;
633
634 wxGraphicsPath* path = m_graphicContext->CreatePath();
635
636 wxList::compatibility_iterator node = points->GetFirst();
637 if (node == wxList::compatibility_iterator())
638 // empty list
639 return;
640
641 wxPoint *p = (wxPoint *)node->GetData();
642
643 wxCoord x1 = p->x;
644 wxCoord y1 = p->y;
645
646 node = node->GetNext();
647 p = (wxPoint *)node->GetData();
648
649 wxCoord x2 = p->x;
650 wxCoord y2 = p->y;
651 wxCoord cx1 = ( x1 + x2 ) / 2;
652 wxCoord cy1 = ( y1 + y2 ) / 2;
653
654 path->MoveToPoint( LogicalToDeviceX( x1 ) , LogicalToDeviceY( y1 ) );
655 path->AddLineToPoint( LogicalToDeviceX( cx1 ) , LogicalToDeviceY( cy1 ) );
656 #if !wxUSE_STL
657
658 while ((node = node->GetNext()) != NULL)
659 #else
660
661 while ((node = node->GetNext()))
662 #endif // !wxUSE_STL
663
664 {
665 p = (wxPoint *)node->GetData();
666 x1 = x2;
667 y1 = y2;
668 x2 = p->x;
669 y2 = p->y;
670 wxCoord cx4 = (x1 + x2) / 2;
671 wxCoord cy4 = (y1 + y2) / 2;
672
673 path->AddQuadCurveToPoint(
674 LogicalToDeviceX( x1 ) , LogicalToDeviceY( y1 ) ,
675 LogicalToDeviceX( cx4 ) , LogicalToDeviceY( cy4 ) );
676
677 cx1 = cx4;
678 cy1 = cy4;
679 }
680
681 path->AddLineToPoint( LogicalToDeviceX( x2 ) , LogicalToDeviceY( y2 ) );
682
683 m_graphicContext->StrokePath( path );
684 delete path;
685 }
686 #endif // wxUSE_SPLINES
687
688 void wxGCDC::DoDrawPolygon( int n, wxPoint points[],
689 wxCoord xoffset, wxCoord yoffset,
690 int fillStyle )
691 {
692 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
693
694 if ( n <= 0 || (m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT ) )
695 return;
696 if ( m_logicalFunction != wxCOPY )
697 return;
698
699 bool closeIt = false;
700 if (points[n-1] != points[0])
701 closeIt = true;
702
703 wxPoint2DDouble* pointsD = new wxPoint2DDouble[n+(closeIt?1:0)];
704 for( int i = 0; i < n; ++i)
705 {
706 pointsD[i].m_x = LogicalToDeviceX(points[i].x + xoffset);
707 pointsD[i].m_y = LogicalToDeviceY(points[i].y + yoffset);
708 }
709 if ( closeIt )
710 pointsD[n] = pointsD[0];
711
712 m_graphicContext->DrawLines( n+(closeIt?1:0) , pointsD, fillStyle);
713 delete[] pointsD;
714 }
715
716 void wxGCDC::DoDrawPolyPolygon(int n,
717 int count[],
718 wxPoint points[],
719 wxCoord xoffset,
720 wxCoord yoffset,
721 int fillStyle)
722 {
723 wxASSERT(n > 1);
724 wxGraphicsPath* path = m_graphicContext->CreatePath();
725
726 int i = 0;
727 for ( int j = 0; j < n; ++j)
728 {
729 wxPoint start = points[i];
730 path->MoveToPoint(LogicalToDeviceX(start.x+ xoffset), LogicalToDeviceY(start.y+ yoffset));
731 ++i;
732 int l = count[j];
733 for ( int k = 1; k < l; ++k)
734 {
735 path->AddLineToPoint( LogicalToDeviceX(points[i].x+ xoffset), LogicalToDeviceY(points[i].y+ yoffset));
736 ++i;
737 }
738 // close the polygon
739 if ( start != points[i-1])
740 path->AddLineToPoint( LogicalToDeviceX(start.x+ xoffset), LogicalToDeviceY(start.y+ yoffset));
741 }
742 m_graphicContext->DrawPath( path , fillStyle);
743 delete path;
744 }
745
746 void wxGCDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
747 {
748 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
749
750 if ( m_logicalFunction != wxCOPY )
751 return;
752
753 wxCoord xx = LogicalToDeviceX(x);
754 wxCoord yy = LogicalToDeviceY(y);
755 wxCoord ww = m_signX * LogicalToDeviceXRel(width);
756 wxCoord hh = m_signY * LogicalToDeviceYRel(height);
757
758 // CMB: draw nothing if transformed w or h is 0
759 if (ww == 0 || hh == 0)
760 return;
761
762 // CMB: handle -ve width and/or height
763 if (ww < 0)
764 {
765 ww = -ww;
766 xx = xx - ww;
767 }
768 if (hh < 0)
769 {
770 hh = -hh;
771 yy = yy - hh;
772 }
773 m_graphicContext->DrawRectangle( xx,yy,ww,hh);
774 }
775
776 void wxGCDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
777 wxCoord width, wxCoord height,
778 double radius)
779 {
780 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
781
782 if ( m_logicalFunction != wxCOPY )
783 return;
784
785 if (radius < 0.0)
786 radius = - radius * ((width < height) ? width : height);
787 wxCoord xx = LogicalToDeviceX(x);
788 wxCoord yy = LogicalToDeviceY(y);
789 wxCoord ww = m_signX * LogicalToDeviceXRel(width);
790 wxCoord hh = m_signY * LogicalToDeviceYRel(height);
791
792 // CMB: draw nothing if transformed w or h is 0
793 if (ww == 0 || hh == 0)
794 return;
795
796 // CMB: handle -ve width and/or height
797 if (ww < 0)
798 {
799 ww = -ww;
800 xx = xx - ww;
801 }
802 if (hh < 0)
803 {
804 hh = -hh;
805 yy = yy - hh;
806 }
807
808 m_graphicContext->DrawRoundedRectangle( xx,yy,ww,hh,radius);
809 }
810
811 void wxGCDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
812 {
813 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
814
815 if ( m_logicalFunction != wxCOPY )
816 return;
817
818 wxDouble xx = LogicalToDeviceX(x);
819 wxDouble yy = LogicalToDeviceY(y);
820 wxDouble ww = m_signX * LogicalToDeviceXRel(width);
821 wxDouble hh = m_signY * LogicalToDeviceYRel(height);
822
823 // CMB: draw nothing if transformed w or h is 0
824 if (ww == 0 || hh == 0)
825 return;
826
827 // CMB: handle -ve width and/or height
828 if (ww < 0)
829 {
830 ww = -ww;
831 xx = xx - ww;
832 }
833 if (hh < 0)
834 {
835 hh = -hh;
836 yy = yy - hh;
837 }
838
839 m_graphicContext->DrawEllipse(xx,yy,ww,hh);
840 }
841
842 bool wxGCDC::CanDrawBitmap() const
843 {
844 return true;
845 }
846
847 bool wxGCDC::DoBlit(
848 wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
849 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool WXUNUSED(useMask),
850 wxCoord xsrcMask, wxCoord ysrcMask )
851 {
852 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") );
853 wxCHECK_MSG( source->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") );
854
855 if ( logical_func == wxNO_OP )
856 return true;
857
858 if (xsrcMask == -1 && ysrcMask == -1)
859 {
860 xsrcMask = xsrc;
861 ysrcMask = ysrc;
862 }
863
864 wxCoord yysrc = source-> LogicalToDeviceY(ysrc);
865 wxCoord xxsrc = source-> LogicalToDeviceX(xsrc);
866 wxCoord wwsrc = source-> LogicalToDeviceXRel(width);
867 wxCoord hhsrc = source-> LogicalToDeviceYRel(height);
868
869 wxCoord yydest = LogicalToDeviceY(ydest);
870 wxCoord xxdest = LogicalToDeviceX(xdest);
871 wxCoord wwdest = LogicalToDeviceXRel(width);
872 wxCoord hhdest = LogicalToDeviceYRel(height);
873
874 wxMemoryDC* memdc = wxDynamicCast(source,wxMemoryDC);
875 if ( memdc && logical_func == wxCOPY )
876 {
877 wxBitmap blit = memdc->GetSelectedBitmap();
878
879 wxASSERT_MSG( blit.Ok() , wxT("Invalid bitmap for blitting") );
880
881 wxCoord bmpwidth = blit.GetWidth();
882 wxCoord bmpheight = blit.GetHeight();
883
884 if ( xxsrc != 0 || yysrc != 0 || bmpwidth != wwsrc || bmpheight != hhsrc )
885 {
886 wwsrc = wxMin( wwsrc , bmpwidth - xxsrc );
887 hhsrc = wxMin( hhsrc , bmpheight - yysrc );
888 if ( wwsrc > 0 && hhsrc > 0 )
889 {
890 if ( xxsrc >= 0 && yysrc >= 0 )
891 {
892 wxRect subrect( xxsrc, yysrc, wwsrc , hhsrc );
893 // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
894 blit = blit.GetSubBitmap( subrect );
895 }
896 else
897 {
898 // in this case we'd probably have to adjust the different coordinates, but
899 // we have to find out proper contract first
900 blit = wxNullBitmap;
901 }
902 }
903 else
904 {
905 blit = wxNullBitmap;
906 }
907 }
908
909 if ( blit.Ok() )
910 {
911 m_graphicContext->DrawBitmap( blit, xxdest , yydest , wwdest , hhdest );
912 }
913 }
914 else
915 {
916 wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts, and only with wxCOPY logical operation.") );
917 return false;
918 }
919
920 return true;
921 }
922
923 void wxGCDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y,
924 double angle)
925 {
926 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
927
928 if ( str.length() == 0 )
929 return;
930 if ( m_logicalFunction != wxCOPY )
931 return;
932
933 int drawX = LogicalToDeviceX(x);
934 int drawY = LogicalToDeviceY(y);
935
936 m_graphicContext->DrawText( str, drawX ,drawY , DegToRad(angle ));
937 }
938
939 void wxGCDC::DoDrawText(const wxString& str, wxCoord x, wxCoord y)
940 {
941 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
942
943 if ( str.length() == 0 )
944 return;
945 if ( m_logicalFunction != wxCOPY )
946 return;
947
948 int drawX = LogicalToDeviceX(x);
949 int drawY = LogicalToDeviceY(y);
950
951 m_graphicContext->DrawText( str, drawX ,drawY);
952 }
953
954 bool wxGCDC::CanGetTextExtent() const
955 {
956 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
957
958 return true;
959 }
960
961 void wxGCDC::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *height,
962 wxCoord *descent, wxCoord *externalLeading ,
963 wxFont *theFont ) const
964 {
965 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
966
967 if ( theFont )
968 {
969 m_graphicContext->SetFont( *theFont, m_textForegroundColour );
970 }
971
972 wxDouble h , d , e , w;
973
974 m_graphicContext->GetTextExtent( str, &w, &h, &d, &e );
975
976 if ( height )
977 *height = DeviceToLogicalYRel((wxCoord)h);
978 if ( descent )
979 *descent = DeviceToLogicalYRel((wxCoord)d);
980 if ( externalLeading )
981 *externalLeading = DeviceToLogicalYRel((wxCoord)e);
982 if ( width )
983 *width = DeviceToLogicalXRel((wxCoord)w);
984
985 if ( theFont )
986 {
987 m_graphicContext->SetFont( m_font, m_textForegroundColour );
988 }
989 }
990
991 bool wxGCDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
992 {
993 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
994 widths.Clear();
995 widths.Add(0,text.Length());
996 if ( text.IsEmpty() )
997 return true;
998
999 wxArrayDouble widthsD;
1000
1001 m_graphicContext->GetPartialTextExtents( text, widthsD );
1002 for ( size_t i = 0; i < widths.GetCount(); ++i )
1003 widths[i] = DeviceToLogicalXRel((wxCoord)(widthsD[i] + 0.5));
1004
1005 return true;
1006 }
1007
1008 wxCoord wxGCDC::GetCharWidth(void) const
1009 {
1010 wxCoord width;
1011 DoGetTextExtent( wxT("g") , &width , NULL , NULL , NULL , NULL );
1012
1013 return width;
1014 }
1015
1016 wxCoord wxGCDC::GetCharHeight(void) const
1017 {
1018 wxCoord height;
1019 DoGetTextExtent( wxT("g") , NULL , &height , NULL , NULL , NULL );
1020
1021 return height;
1022 }
1023
1024 void wxGCDC::Clear(void)
1025 {
1026 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1027 // TODO better implementation / incorporate size info into wxGCDC or context
1028 m_graphicContext->SetBrush( m_backgroundBrush );
1029 wxPen p = *wxTRANSPARENT_PEN;
1030 m_graphicContext->SetPen( p );
1031 DoDrawRectangle( 0, 0, 32000 , 32000 );
1032 m_graphicContext->SetPen( m_pen );
1033 m_graphicContext->SetBrush( m_brush );
1034 }
1035
1036 void wxGCDC::DoGetSize(int *width, int *height) const
1037 {
1038 *width = 1000;
1039 *height = 1000;
1040 }
1041
1042 void wxGCDC::DoGradientFillLinear(const wxRect& rect,
1043 const wxColour& initialColour,
1044 const wxColour& destColour,
1045 wxDirection nDirection )
1046 {
1047 wxPoint start;
1048 wxPoint end;
1049 switch( nDirection)
1050 {
1051 case wxWEST :
1052 start = rect.GetRightBottom();
1053 start.x++;
1054 end = rect.GetLeftBottom();
1055 break;
1056 case wxEAST :
1057 start = rect.GetLeftBottom();
1058 end = rect.GetRightBottom();
1059 end.x++;
1060 break;
1061 case wxNORTH :
1062 start = rect.GetLeftBottom();
1063 start.y++;
1064 end = rect.GetLeftTop();
1065 break;
1066 case wxSOUTH :
1067 start = rect.GetLeftTop();
1068 end = rect.GetLeftBottom();
1069 end.y++;
1070 break;
1071 default :
1072 break;
1073 }
1074
1075 m_graphicContext->SetBrush( m_graphicContext->CreateLinearGradientBrush(
1076 LogicalToDeviceX(start.x),LogicalToDeviceY(start.y),
1077 LogicalToDeviceX(end.x),LogicalToDeviceY(end.y), initialColour, destColour));
1078
1079 wxDouble xx = LogicalToDeviceX(rect.x);
1080 wxDouble yy = LogicalToDeviceY(rect.y);
1081 wxDouble ww = m_signX * LogicalToDeviceXRel(rect.width);
1082 wxDouble hh = m_signY * LogicalToDeviceYRel(rect.height);
1083
1084 if (ww == 0 || hh == 0)
1085 return;
1086
1087 if (ww < 0)
1088 {
1089 ww = -ww;
1090 xx = xx - ww;
1091 }
1092 if (hh < 0)
1093 {
1094 hh = -hh;
1095 yy = yy - hh;
1096 }
1097
1098 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
1099 m_graphicContext->DrawRectangle(xx,yy,ww,hh);
1100 m_graphicContext->SetPen(m_pen);
1101 }
1102
1103 void wxGCDC::DoGradientFillConcentric(const wxRect& rect,
1104 const wxColour& initialColour,
1105 const wxColour& destColour,
1106 const wxPoint& circleCenter)
1107 {
1108 //Radius
1109 wxInt32 cx = rect.GetWidth() / 2;
1110 wxInt32 cy = rect.GetHeight() / 2;
1111 wxInt32 nRadius;
1112 if (cx < cy)
1113 nRadius = cx;
1114 else
1115 nRadius = cy;
1116
1117 wxDouble xx = LogicalToDeviceX(rect.x);
1118 wxDouble yy = LogicalToDeviceY(rect.y);
1119 wxDouble ww = m_signX * LogicalToDeviceXRel(rect.width);
1120 wxDouble hh = m_signY * LogicalToDeviceYRel(rect.height);
1121
1122 if (ww == 0 || hh == 0)
1123 return;
1124
1125 if (ww < 0)
1126 {
1127 ww = -ww;
1128 xx = xx - ww;
1129 }
1130 if (hh < 0)
1131 {
1132 hh = -hh;
1133 yy = yy - hh;
1134 }
1135
1136 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
1137 m_graphicContext->SetBrush( wxBrush( destColour) );
1138 m_graphicContext->DrawRectangle( xx,yy,ww,hh);
1139
1140 m_graphicContext->SetBrush( m_graphicContext->CreateRadialGradientBrush(
1141 xx+LogicalToDeviceX(circleCenter.x),yy+LogicalToDeviceY(circleCenter.y),
1142 xx+LogicalToDeviceX(circleCenter.x),yy+LogicalToDeviceY(circleCenter.y),
1143 LogicalToDeviceXRel(nRadius),
1144 initialColour,destColour));
1145
1146 m_graphicContext->DrawRectangle( xx,yy,ww,hh);
1147 m_graphicContext->SetPen(m_pen);
1148 }
1149
1150 void wxGCDC::DoDrawCheckMark(wxCoord x, wxCoord y,
1151 wxCoord width, wxCoord height)
1152 {
1153 wxDCBase::DoDrawCheckMark(x,y,width,height);
1154 }
1155
1156 #endif // wxUSE_GRAPHICS_CONTEXT