]> git.saurik.com Git - wxWidgets.git/blame - src/common/dcgraph.cpp
switch having an affine transform even when there is no native context around
[wxWidgets.git] / src / common / dcgraph.cpp
CommitLineData
1b89a5cd
SC
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
34static const double RAD2DEG = 180.0 / M_PI;
35
36//-----------------------------------------------------------------------------
37// Local functions
38//-----------------------------------------------------------------------------
39
40static 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__
50IMPLEMENT_DYNAMIC_CLASS(wxGCDC, wxDCBase)
51#else
52IMPLEMENT_DYNAMIC_CLASS(wxGCDC, wxDC)
53#endif
54
55wxGCDC::wxGCDC()
56{
57 Init();
58}
59
60void wxGCDC::SetGraphicsContext( wxGraphicsContext* ctx )
61{
62 delete m_graphicContext;
63 m_graphicContext = ctx;
64}
65
66wxGCDC::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
79void 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
94wxGCDC::~wxGCDC()
95{
96 delete m_graphicContext;
97}
98
99void 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
114void 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
129void 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
158void 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
204void 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
213void 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
224void 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
235void wxGCDC::SetTextBackground( const wxColour &col )
236{
237 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::SetTextBackground - invalid DC") );
238
239 m_textBackgroundColour = col;
240}
241
242void 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
271void wxGCDC::SetUserScale( double x, double y )
272{
273 // allow negative ? -> no
274
275 m_userScaleX = x;
276 m_userScaleY = y;
277 ComputeScaleAndOrigin();
278}
279
280void wxGCDC::SetLogicalScale( double x, double y )
281{
282 // allow negative ?
283 m_logicalScaleX = x;
284 m_logicalScaleY = y;
285 ComputeScaleAndOrigin();
286}
287
288void 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
295void wxGCDC::SetDeviceOrigin( wxCoord x, wxCoord y )
296{
297 m_deviceOriginX = x;
298 m_deviceOriginY = y;
299 ComputeScaleAndOrigin();
300}
301
302void wxGCDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
303{
304 m_signX = (xLeftRight ? 1 : -1);
305 m_signY = (yBottomUp ? -1 : 1);
306 ComputeScaleAndOrigin();
307}
308
309wxSize wxGCDC::GetPPI() const
310{
311 return wxSize(72, 72);
312}
313
314int wxGCDC::GetDepth() const
315{
316 return 32;
317}
318
319void 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
341void wxGCDC::SetPalette( const wxPalette& WXUNUSED(palette) )
342{
343
344}
345
346void wxGCDC::SetBackgroundMode( int mode )
347{
348 m_backgroundMode = mode;
349}
350
351void 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
363void 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
388void 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
414void 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
424void 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
443bool wxGCDC::DoFloodFill(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
444 const wxColour& WXUNUSED(col), int WXUNUSED(style))
445{
446 return false;
447}
448
449bool 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
455void 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
476void 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
501void 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
a4e73390 543 wxGraphicsPath path = m_graphicContext->CreatePath();
1b89a5cd 544 if ( fill && ((x1!=x2)||(y1!=y2)) )
a4e73390
SC
545 path.MoveToPoint( xxc, yyc );
546 path.AddArc( xxc, yyc , rad , DegToRad(sa) , DegToRad(ea), false );
1b89a5cd 547 if ( fill && ((x1!=x2)||(y1!=y2)) )
a4e73390 548 path.AddLineToPoint( xxc, yyc );
1b89a5cd 549 m_graphicContext->DrawPath(path);
1b89a5cd
SC
550}
551
552void wxGCDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
553 double sa, double ea )
554{
555 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipticArc - invalid DC") );
556
557 if ( m_logicalFunction != wxCOPY )
558 return;
559
560 wxCoord xx = LogicalToDeviceX(x);
561 wxCoord yy = LogicalToDeviceY(y);
562 wxCoord ww = m_signX * LogicalToDeviceXRel(w);
563 wxCoord hh = m_signY * LogicalToDeviceYRel(h);
564
565 // handle -ve width and/or height
566 if (ww < 0)
567 {
568 ww = -ww;
569 xx = xx - ww;
570 }
571 if (hh < 0)
572 {
573 hh = -hh;
574 yy = yy - hh;
575 }
576
577 bool fill = m_brush.GetStyle() != wxTRANSPARENT;
578
a4e73390 579 wxGraphicsPath path = m_graphicContext->CreatePath();
1b89a5cd
SC
580 m_graphicContext->PushState();
581 m_graphicContext->Translate(xx+ww/2,yy+hh/2);
582 wxDouble factor = ((wxDouble) ww) / hh;
583 m_graphicContext->Scale( factor , 1.0);
584 if ( fill && (sa!=ea) )
a4e73390 585 path.MoveToPoint(0,0);
1b89a5cd
SC
586 // since these angles (ea,sa) are measured counter-clockwise, we invert them to
587 // get clockwise angles
a4e73390 588 path.AddArc( 0, 0, hh/2 , DegToRad(-sa) , DegToRad(-ea), sa > ea );
1b89a5cd 589 if ( fill && (sa!=ea) )
a4e73390 590 path.AddLineToPoint(0,0);
1b89a5cd
SC
591 m_graphicContext->DrawPath( path );
592 m_graphicContext->PopState();
1b89a5cd
SC
593}
594
595void wxGCDC::DoDrawPoint( wxCoord x, wxCoord y )
596{
597 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPoint - invalid DC") );
598
599 DoDrawLine( x , y , x + 1 , y + 1 );
600}
601
602void wxGCDC::DoDrawLines(int n, wxPoint points[],
603 wxCoord xoffset, wxCoord yoffset)
604{
605 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawLines - invalid DC") );
606
607#if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
608
609 if ( m_logicalFunction != wxCOPY )
610 return;
611#endif
612
613 wxPoint2DDouble* pointsD = new wxPoint2DDouble[n];
614 for( int i = 0; i < n; ++i)
615 {
616 pointsD[i].m_x = LogicalToDeviceX(points[i].x + xoffset);
617 pointsD[i].m_y = LogicalToDeviceY(points[i].y + yoffset);
618 }
619
620 m_graphicContext->StrokeLines( n , pointsD);
621 delete[] pointsD;
622}
623
624#if wxUSE_SPLINES
625void wxGCDC::DoDrawSpline(wxList *points)
626{
627 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawSpline - invalid DC") );
628
629 if ( m_logicalFunction != wxCOPY )
630 return;
631
a4e73390 632 wxGraphicsPath path = m_graphicContext->CreatePath();
1b89a5cd
SC
633
634 wxList::compatibility_iterator node = points->GetFirst();
635 if (node == wxList::compatibility_iterator())
636 // empty list
637 return;
638
639 wxPoint *p = (wxPoint *)node->GetData();
640
641 wxCoord x1 = p->x;
642 wxCoord y1 = p->y;
643
644 node = node->GetNext();
645 p = (wxPoint *)node->GetData();
646
647 wxCoord x2 = p->x;
648 wxCoord y2 = p->y;
649 wxCoord cx1 = ( x1 + x2 ) / 2;
650 wxCoord cy1 = ( y1 + y2 ) / 2;
651
a4e73390
SC
652 path.MoveToPoint( LogicalToDeviceX( x1 ) , LogicalToDeviceY( y1 ) );
653 path.AddLineToPoint( LogicalToDeviceX( cx1 ) , LogicalToDeviceY( cy1 ) );
1b89a5cd
SC
654#if !wxUSE_STL
655
656 while ((node = node->GetNext()) != NULL)
657#else
658
659 while ((node = node->GetNext()))
660#endif // !wxUSE_STL
661
662 {
663 p = (wxPoint *)node->GetData();
664 x1 = x2;
665 y1 = y2;
666 x2 = p->x;
667 y2 = p->y;
668 wxCoord cx4 = (x1 + x2) / 2;
669 wxCoord cy4 = (y1 + y2) / 2;
670
a4e73390 671 path.AddQuadCurveToPoint(
1b89a5cd
SC
672 LogicalToDeviceX( x1 ) , LogicalToDeviceY( y1 ) ,
673 LogicalToDeviceX( cx4 ) , LogicalToDeviceY( cy4 ) );
674
675 cx1 = cx4;
676 cy1 = cy4;
677 }
678
a4e73390 679 path.AddLineToPoint( LogicalToDeviceX( x2 ) , LogicalToDeviceY( y2 ) );
1b89a5cd
SC
680
681 m_graphicContext->StrokePath( path );
1b89a5cd
SC
682}
683#endif // wxUSE_SPLINES
684
685void wxGCDC::DoDrawPolygon( int n, wxPoint points[],
686 wxCoord xoffset, wxCoord yoffset,
687 int fillStyle )
688{
689 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawPolygon - invalid DC") );
690
691 if ( n <= 0 || (m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT ) )
692 return;
693 if ( m_logicalFunction != wxCOPY )
694 return;
695
696 bool closeIt = false;
697 if (points[n-1] != points[0])
698 closeIt = true;
699
700 wxPoint2DDouble* pointsD = new wxPoint2DDouble[n+(closeIt?1:0)];
701 for( int i = 0; i < n; ++i)
702 {
703 pointsD[i].m_x = LogicalToDeviceX(points[i].x + xoffset);
704 pointsD[i].m_y = LogicalToDeviceY(points[i].y + yoffset);
705 }
706 if ( closeIt )
707 pointsD[n] = pointsD[0];
708
709 m_graphicContext->DrawLines( n+(closeIt?1:0) , pointsD, fillStyle);
710 delete[] pointsD;
711}
712
713void wxGCDC::DoDrawPolyPolygon(int n,
714 int count[],
715 wxPoint points[],
716 wxCoord xoffset,
717 wxCoord yoffset,
718 int fillStyle)
719{
720 wxASSERT(n > 1);
a4e73390 721 wxGraphicsPath path = m_graphicContext->CreatePath();
1b89a5cd
SC
722
723 int i = 0;
724 for ( int j = 0; j < n; ++j)
725 {
726 wxPoint start = points[i];
a4e73390 727 path.MoveToPoint(LogicalToDeviceX(start.x+ xoffset), LogicalToDeviceY(start.y+ yoffset));
1b89a5cd
SC
728 ++i;
729 int l = count[j];
730 for ( int k = 1; k < l; ++k)
731 {
a4e73390 732 path.AddLineToPoint( LogicalToDeviceX(points[i].x+ xoffset), LogicalToDeviceY(points[i].y+ yoffset));
1b89a5cd
SC
733 ++i;
734 }
735 // close the polygon
736 if ( start != points[i-1])
a4e73390 737 path.AddLineToPoint( LogicalToDeviceX(start.x+ xoffset), LogicalToDeviceY(start.y+ yoffset));
1b89a5cd
SC
738 }
739 m_graphicContext->DrawPath( path , fillStyle);
1b89a5cd
SC
740}
741
742void wxGCDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
743{
744 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRectangle - invalid DC") );
745
746 if ( m_logicalFunction != wxCOPY )
747 return;
748
749 wxCoord xx = LogicalToDeviceX(x);
750 wxCoord yy = LogicalToDeviceY(y);
751 wxCoord ww = m_signX * LogicalToDeviceXRel(width);
752 wxCoord hh = m_signY * LogicalToDeviceYRel(height);
753
754 // CMB: draw nothing if transformed w or h is 0
755 if (ww == 0 || hh == 0)
756 return;
757
758 // CMB: handle -ve width and/or height
759 if (ww < 0)
760 {
761 ww = -ww;
762 xx = xx - ww;
763 }
764 if (hh < 0)
765 {
766 hh = -hh;
767 yy = yy - hh;
768 }
5ebfdf41
SC
769 if ( m_graphicContext->ShouldOffset() )
770 {
771 // if we are offsetting the entire rectangle is moved 0.5, so the
772 // border line gets off by 1
773 ww -= 1;
774 hh -= 1;
775 }
1b89a5cd
SC
776 m_graphicContext->DrawRectangle( xx,yy,ww,hh);
777}
778
779void wxGCDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
780 wxCoord width, wxCoord height,
781 double radius)
782{
783 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
784
785 if ( m_logicalFunction != wxCOPY )
786 return;
787
788 if (radius < 0.0)
789 radius = - radius * ((width < height) ? width : height);
790 wxCoord xx = LogicalToDeviceX(x);
791 wxCoord yy = LogicalToDeviceY(y);
792 wxCoord ww = m_signX * LogicalToDeviceXRel(width);
793 wxCoord hh = m_signY * LogicalToDeviceYRel(height);
794
795 // CMB: draw nothing if transformed w or h is 0
796 if (ww == 0 || hh == 0)
797 return;
798
799 // CMB: handle -ve width and/or height
800 if (ww < 0)
801 {
802 ww = -ww;
803 xx = xx - ww;
804 }
805 if (hh < 0)
806 {
807 hh = -hh;
808 yy = yy - hh;
809 }
810
811 m_graphicContext->DrawRoundedRectangle( xx,yy,ww,hh,radius);
812}
813
814void wxGCDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
815{
816 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
817
818 if ( m_logicalFunction != wxCOPY )
819 return;
820
821 wxDouble xx = LogicalToDeviceX(x);
822 wxDouble yy = LogicalToDeviceY(y);
823 wxDouble ww = m_signX * LogicalToDeviceXRel(width);
824 wxDouble hh = m_signY * LogicalToDeviceYRel(height);
825
826 // CMB: draw nothing if transformed w or h is 0
827 if (ww == 0 || hh == 0)
828 return;
829
830 // CMB: handle -ve width and/or height
831 if (ww < 0)
832 {
833 ww = -ww;
834 xx = xx - ww;
835 }
836 if (hh < 0)
837 {
838 hh = -hh;
839 yy = yy - hh;
840 }
841
842 m_graphicContext->DrawEllipse(xx,yy,ww,hh);
843}
844
845bool wxGCDC::CanDrawBitmap() const
846{
847 return true;
848}
849
850bool wxGCDC::DoBlit(
851 wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
852 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool WXUNUSED(useMask),
853 wxCoord xsrcMask, wxCoord ysrcMask )
854{
855 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") );
856 wxCHECK_MSG( source->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") );
c64c9cd3 857
1b89a5cd
SC
858 if ( logical_func == wxNO_OP )
859 return true;
c64c9cd3
KO
860 else if ( logical_func != wxCOPY )
861 {
862 wxFAIL_MSG( wxT("Blitting is only supported with wxCOPY logical operation.") );
863 return false;
864 }
1b89a5cd
SC
865
866 if (xsrcMask == -1 && ysrcMask == -1)
867 {
868 xsrcMask = xsrc;
869 ysrcMask = ysrc;
870 }
871
872 wxCoord yysrc = source-> LogicalToDeviceY(ysrc);
873 wxCoord xxsrc = source-> LogicalToDeviceX(xsrc);
874 wxCoord wwsrc = source-> LogicalToDeviceXRel(width);
875 wxCoord hhsrc = source-> LogicalToDeviceYRel(height);
876
877 wxCoord yydest = LogicalToDeviceY(ydest);
878 wxCoord xxdest = LogicalToDeviceX(xdest);
879 wxCoord wwdest = LogicalToDeviceXRel(width);
880 wxCoord hhdest = LogicalToDeviceYRel(height);
881
c64c9cd3 882 wxBitmap blit;
1b89a5cd 883 wxMemoryDC* memdc = wxDynamicCast(source,wxMemoryDC);
c64c9cd3 884 if ( memdc )
1b89a5cd 885 {
c64c9cd3 886 blit = memdc->GetSelectedBitmap();
1b89a5cd
SC
887
888 wxASSERT_MSG( blit.Ok() , wxT("Invalid bitmap for blitting") );
889
890 wxCoord bmpwidth = blit.GetWidth();
891 wxCoord bmpheight = blit.GetHeight();
892
893 if ( xxsrc != 0 || yysrc != 0 || bmpwidth != wwsrc || bmpheight != hhsrc )
894 {
895 wwsrc = wxMin( wwsrc , bmpwidth - xxsrc );
896 hhsrc = wxMin( hhsrc , bmpheight - yysrc );
897 if ( wwsrc > 0 && hhsrc > 0 )
898 {
899 if ( xxsrc >= 0 && yysrc >= 0 )
900 {
901 wxRect subrect( xxsrc, yysrc, wwsrc , hhsrc );
902 // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
903 blit = blit.GetSubBitmap( subrect );
904 }
905 else
906 {
907 // in this case we'd probably have to adjust the different coordinates, but
908 // we have to find out proper contract first
909 blit = wxNullBitmap;
910 }
911 }
912 else
913 {
914 blit = wxNullBitmap;
915 }
916 }
c64c9cd3
KO
917 }
918 else
919 {
920 wxWindowDC* windc = wxDynamicCast(source,wxWindowDC);
921 if (windc)
922 {
923 wxBitmap bmp;
924 bmp = windc->GetAsBitmap();
925 if (bmp.IsOk())
926 blit = bmp.GetSubBitmap( wxRect(xsrc, ysrc, width, height ) );
1b89a5cd
SC
927 }
928 }
c64c9cd3
KO
929
930 if ( blit.Ok() )
931 {
932 m_graphicContext->DrawBitmap( blit, xxdest , yydest , wwdest , hhdest );
933 }
1b89a5cd
SC
934 else
935 {
c64c9cd3 936 wxFAIL_MSG( wxT("Cannot Blit. Unable to get contents of DC as bitmap.") );
1b89a5cd
SC
937 return false;
938 }
939
940 return true;
941}
942
943void wxGCDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y,
944 double angle)
945{
946 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
947
948 if ( str.length() == 0 )
949 return;
950 if ( m_logicalFunction != wxCOPY )
951 return;
952
953 int drawX = LogicalToDeviceX(x);
954 int drawY = LogicalToDeviceY(y);
955
956 m_graphicContext->DrawText( str, drawX ,drawY , DegToRad(angle ));
957}
958
959void wxGCDC::DoDrawText(const wxString& str, wxCoord x, wxCoord y)
960{
961 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
962
963 if ( str.length() == 0 )
964 return;
965 if ( m_logicalFunction != wxCOPY )
966 return;
967
968 int drawX = LogicalToDeviceX(x);
969 int drawY = LogicalToDeviceY(y);
970
971 m_graphicContext->DrawText( str, drawX ,drawY);
972}
973
974bool wxGCDC::CanGetTextExtent() const
975{
976 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
977
978 return true;
979}
980
981void wxGCDC::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *height,
982 wxCoord *descent, wxCoord *externalLeading ,
983 wxFont *theFont ) const
984{
985 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
986
987 if ( theFont )
988 {
989 m_graphicContext->SetFont( *theFont, m_textForegroundColour );
990 }
991
992 wxDouble h , d , e , w;
993
994 m_graphicContext->GetTextExtent( str, &w, &h, &d, &e );
995
996 if ( height )
997 *height = DeviceToLogicalYRel((wxCoord)h);
998 if ( descent )
999 *descent = DeviceToLogicalYRel((wxCoord)d);
1000 if ( externalLeading )
1001 *externalLeading = DeviceToLogicalYRel((wxCoord)e);
1002 if ( width )
1003 *width = DeviceToLogicalXRel((wxCoord)w);
1004
1005 if ( theFont )
1006 {
1007 m_graphicContext->SetFont( m_font, m_textForegroundColour );
1008 }
1009}
1010
1011bool wxGCDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
1012{
1013 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1014 widths.Clear();
1015 widths.Add(0,text.Length());
1016 if ( text.IsEmpty() )
1017 return true;
1018
1019 wxArrayDouble widthsD;
1020
1021 m_graphicContext->GetPartialTextExtents( text, widthsD );
1022 for ( size_t i = 0; i < widths.GetCount(); ++i )
1023 widths[i] = DeviceToLogicalXRel((wxCoord)(widthsD[i] + 0.5));
1024
1025 return true;
1026}
1027
1028wxCoord wxGCDC::GetCharWidth(void) const
1029{
1030 wxCoord width;
1031 DoGetTextExtent( wxT("g") , &width , NULL , NULL , NULL , NULL );
1032
1033 return width;
1034}
1035
1036wxCoord wxGCDC::GetCharHeight(void) const
1037{
1038 wxCoord height;
1039 DoGetTextExtent( wxT("g") , NULL , &height , NULL , NULL , NULL );
1040
1041 return height;
1042}
1043
1044void wxGCDC::Clear(void)
1045{
1046 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1047 // TODO better implementation / incorporate size info into wxGCDC or context
1048 m_graphicContext->SetBrush( m_backgroundBrush );
1049 wxPen p = *wxTRANSPARENT_PEN;
1050 m_graphicContext->SetPen( p );
1051 DoDrawRectangle( 0, 0, 32000 , 32000 );
1052 m_graphicContext->SetPen( m_pen );
1053 m_graphicContext->SetBrush( m_brush );
1054}
1055
1056void wxGCDC::DoGetSize(int *width, int *height) const
1057{
1058 *width = 1000;
1059 *height = 1000;
1060}
1061
1062void wxGCDC::DoGradientFillLinear(const wxRect& rect,
1063 const wxColour& initialColour,
1064 const wxColour& destColour,
1065 wxDirection nDirection )
1066{
1067 wxPoint start;
1068 wxPoint end;
1069 switch( nDirection)
1070 {
1071 case wxWEST :
1072 start = rect.GetRightBottom();
1073 start.x++;
1074 end = rect.GetLeftBottom();
1075 break;
1076 case wxEAST :
1077 start = rect.GetLeftBottom();
1078 end = rect.GetRightBottom();
1079 end.x++;
1080 break;
1081 case wxNORTH :
1082 start = rect.GetLeftBottom();
1083 start.y++;
1084 end = rect.GetLeftTop();
1085 break;
1086 case wxSOUTH :
1087 start = rect.GetLeftTop();
1088 end = rect.GetLeftBottom();
1089 end.y++;
1090 break;
1091 default :
1092 break;
1093 }
1094
1095 m_graphicContext->SetBrush( m_graphicContext->CreateLinearGradientBrush(
1096 LogicalToDeviceX(start.x),LogicalToDeviceY(start.y),
1097 LogicalToDeviceX(end.x),LogicalToDeviceY(end.y), initialColour, destColour));
1098
1099 wxDouble xx = LogicalToDeviceX(rect.x);
1100 wxDouble yy = LogicalToDeviceY(rect.y);
1101 wxDouble ww = m_signX * LogicalToDeviceXRel(rect.width);
1102 wxDouble hh = m_signY * LogicalToDeviceYRel(rect.height);
1103
1104 if (ww == 0 || hh == 0)
1105 return;
1106
1107 if (ww < 0)
1108 {
1109 ww = -ww;
1110 xx = xx - ww;
1111 }
1112 if (hh < 0)
1113 {
1114 hh = -hh;
1115 yy = yy - hh;
1116 }
1117
1118 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
1119 m_graphicContext->DrawRectangle(xx,yy,ww,hh);
1120 m_graphicContext->SetPen(m_pen);
1121}
1122
1123void wxGCDC::DoGradientFillConcentric(const wxRect& rect,
1124 const wxColour& initialColour,
1125 const wxColour& destColour,
1126 const wxPoint& circleCenter)
1127{
1128 //Radius
1129 wxInt32 cx = rect.GetWidth() / 2;
1130 wxInt32 cy = rect.GetHeight() / 2;
1131 wxInt32 nRadius;
1132 if (cx < cy)
1133 nRadius = cx;
1134 else
1135 nRadius = cy;
1136
1137 wxDouble xx = LogicalToDeviceX(rect.x);
1138 wxDouble yy = LogicalToDeviceY(rect.y);
1139 wxDouble ww = m_signX * LogicalToDeviceXRel(rect.width);
1140 wxDouble hh = m_signY * LogicalToDeviceYRel(rect.height);
1141
1142 if (ww == 0 || hh == 0)
1143 return;
1144
1145 if (ww < 0)
1146 {
1147 ww = -ww;
1148 xx = xx - ww;
1149 }
1150 if (hh < 0)
1151 {
1152 hh = -hh;
1153 yy = yy - hh;
1154 }
1155
1156 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
1157 m_graphicContext->SetBrush( wxBrush( destColour) );
1158 m_graphicContext->DrawRectangle( xx,yy,ww,hh);
1159
1160 m_graphicContext->SetBrush( m_graphicContext->CreateRadialGradientBrush(
1161 xx+LogicalToDeviceX(circleCenter.x),yy+LogicalToDeviceY(circleCenter.y),
1162 xx+LogicalToDeviceX(circleCenter.x),yy+LogicalToDeviceY(circleCenter.y),
1163 LogicalToDeviceXRel(nRadius),
1164 initialColour,destColour));
1165
1166 m_graphicContext->DrawRectangle( xx,yy,ww,hh);
1167 m_graphicContext->SetPen(m_pen);
1168}
1169
1170void wxGCDC::DoDrawCheckMark(wxCoord x, wxCoord y,
1171 wxCoord width, wxCoord height)
1172{
1173 wxDCBase::DoDrawCheckMark(x,y,width,height);
1174}
1175
1176#endif // wxUSE_GRAPHICS_CONTEXT