]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/dcgraph.cpp
Bug fix.
[wxWidgets.git] / src / common / dcgraph.cpp
... / ...
CommitLineData
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
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
553void 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
597void 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
604void 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
627void 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
688void 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
716void 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
746void 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 if ( m_graphicContext->ShouldOffset() )
774 {
775 // if we are offsetting the entire rectangle is moved 0.5, so the
776 // border line gets off by 1
777 ww -= 1;
778 hh -= 1;
779 }
780 m_graphicContext->DrawRectangle( xx,yy,ww,hh);
781}
782
783void wxGCDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
784 wxCoord width, wxCoord height,
785 double radius)
786{
787 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRoundedRectangle - invalid DC") );
788
789 if ( m_logicalFunction != wxCOPY )
790 return;
791
792 if (radius < 0.0)
793 radius = - radius * ((width < height) ? width : height);
794 wxCoord xx = LogicalToDeviceX(x);
795 wxCoord yy = LogicalToDeviceY(y);
796 wxCoord ww = m_signX * LogicalToDeviceXRel(width);
797 wxCoord hh = m_signY * LogicalToDeviceYRel(height);
798
799 // CMB: draw nothing if transformed w or h is 0
800 if (ww == 0 || hh == 0)
801 return;
802
803 // CMB: handle -ve width and/or height
804 if (ww < 0)
805 {
806 ww = -ww;
807 xx = xx - ww;
808 }
809 if (hh < 0)
810 {
811 hh = -hh;
812 yy = yy - hh;
813 }
814
815 m_graphicContext->DrawRoundedRectangle( xx,yy,ww,hh,radius);
816}
817
818void wxGCDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
819{
820 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawEllipse - invalid DC") );
821
822 if ( m_logicalFunction != wxCOPY )
823 return;
824
825 wxDouble xx = LogicalToDeviceX(x);
826 wxDouble yy = LogicalToDeviceY(y);
827 wxDouble ww = m_signX * LogicalToDeviceXRel(width);
828 wxDouble hh = m_signY * LogicalToDeviceYRel(height);
829
830 // CMB: draw nothing if transformed w or h is 0
831 if (ww == 0 || hh == 0)
832 return;
833
834 // CMB: handle -ve width and/or height
835 if (ww < 0)
836 {
837 ww = -ww;
838 xx = xx - ww;
839 }
840 if (hh < 0)
841 {
842 hh = -hh;
843 yy = yy - hh;
844 }
845
846 m_graphicContext->DrawEllipse(xx,yy,ww,hh);
847}
848
849bool wxGCDC::CanDrawBitmap() const
850{
851 return true;
852}
853
854bool wxGCDC::DoBlit(
855 wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
856 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool WXUNUSED(useMask),
857 wxCoord xsrcMask, wxCoord ysrcMask )
858{
859 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid DC") );
860 wxCHECK_MSG( source->Ok(), false, wxT("wxGCDC(cg)::DoBlit - invalid source DC") );
861
862 if ( logical_func == wxNO_OP )
863 return true;
864
865 if (xsrcMask == -1 && ysrcMask == -1)
866 {
867 xsrcMask = xsrc;
868 ysrcMask = ysrc;
869 }
870
871 wxCoord yysrc = source-> LogicalToDeviceY(ysrc);
872 wxCoord xxsrc = source-> LogicalToDeviceX(xsrc);
873 wxCoord wwsrc = source-> LogicalToDeviceXRel(width);
874 wxCoord hhsrc = source-> LogicalToDeviceYRel(height);
875
876 wxCoord yydest = LogicalToDeviceY(ydest);
877 wxCoord xxdest = LogicalToDeviceX(xdest);
878 wxCoord wwdest = LogicalToDeviceXRel(width);
879 wxCoord hhdest = LogicalToDeviceYRel(height);
880
881 wxMemoryDC* memdc = wxDynamicCast(source,wxMemoryDC);
882 if ( memdc && logical_func == wxCOPY )
883 {
884 wxBitmap blit = memdc->GetSelectedBitmap();
885
886 wxASSERT_MSG( blit.Ok() , wxT("Invalid bitmap for blitting") );
887
888 wxCoord bmpwidth = blit.GetWidth();
889 wxCoord bmpheight = blit.GetHeight();
890
891 if ( xxsrc != 0 || yysrc != 0 || bmpwidth != wwsrc || bmpheight != hhsrc )
892 {
893 wwsrc = wxMin( wwsrc , bmpwidth - xxsrc );
894 hhsrc = wxMin( hhsrc , bmpheight - yysrc );
895 if ( wwsrc > 0 && hhsrc > 0 )
896 {
897 if ( xxsrc >= 0 && yysrc >= 0 )
898 {
899 wxRect subrect( xxsrc, yysrc, wwsrc , hhsrc );
900 // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
901 blit = blit.GetSubBitmap( subrect );
902 }
903 else
904 {
905 // in this case we'd probably have to adjust the different coordinates, but
906 // we have to find out proper contract first
907 blit = wxNullBitmap;
908 }
909 }
910 else
911 {
912 blit = wxNullBitmap;
913 }
914 }
915
916 if ( blit.Ok() )
917 {
918 m_graphicContext->DrawBitmap( blit, xxdest , yydest , wwdest , hhdest );
919 }
920 }
921 else
922 {
923 wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts, and only with wxCOPY logical operation.") );
924 return false;
925 }
926
927 return true;
928}
929
930void wxGCDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y,
931 double angle)
932{
933 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
934
935 if ( str.length() == 0 )
936 return;
937 if ( m_logicalFunction != wxCOPY )
938 return;
939
940 int drawX = LogicalToDeviceX(x);
941 int drawY = LogicalToDeviceY(y);
942
943 m_graphicContext->DrawText( str, drawX ,drawY , DegToRad(angle ));
944}
945
946void wxGCDC::DoDrawText(const wxString& str, wxCoord x, wxCoord y)
947{
948 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoDrawRotatedText - invalid DC") );
949
950 if ( str.length() == 0 )
951 return;
952 if ( m_logicalFunction != wxCOPY )
953 return;
954
955 int drawX = LogicalToDeviceX(x);
956 int drawY = LogicalToDeviceY(y);
957
958 m_graphicContext->DrawText( str, drawX ,drawY);
959}
960
961bool wxGCDC::CanGetTextExtent() const
962{
963 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::CanGetTextExtent - invalid DC") );
964
965 return true;
966}
967
968void wxGCDC::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *height,
969 wxCoord *descent, wxCoord *externalLeading ,
970 wxFont *theFont ) const
971{
972 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::DoGetTextExtent - invalid DC") );
973
974 if ( theFont )
975 {
976 m_graphicContext->SetFont( *theFont, m_textForegroundColour );
977 }
978
979 wxDouble h , d , e , w;
980
981 m_graphicContext->GetTextExtent( str, &w, &h, &d, &e );
982
983 if ( height )
984 *height = DeviceToLogicalYRel((wxCoord)h);
985 if ( descent )
986 *descent = DeviceToLogicalYRel((wxCoord)d);
987 if ( externalLeading )
988 *externalLeading = DeviceToLogicalYRel((wxCoord)e);
989 if ( width )
990 *width = DeviceToLogicalXRel((wxCoord)w);
991
992 if ( theFont )
993 {
994 m_graphicContext->SetFont( m_font, m_textForegroundColour );
995 }
996}
997
998bool wxGCDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
999{
1000 wxCHECK_MSG( Ok(), false, wxT("wxGCDC(cg)::DoGetPartialTextExtents - invalid DC") );
1001 widths.Clear();
1002 widths.Add(0,text.Length());
1003 if ( text.IsEmpty() )
1004 return true;
1005
1006 wxArrayDouble widthsD;
1007
1008 m_graphicContext->GetPartialTextExtents( text, widthsD );
1009 for ( size_t i = 0; i < widths.GetCount(); ++i )
1010 widths[i] = DeviceToLogicalXRel((wxCoord)(widthsD[i] + 0.5));
1011
1012 return true;
1013}
1014
1015wxCoord wxGCDC::GetCharWidth(void) const
1016{
1017 wxCoord width;
1018 DoGetTextExtent( wxT("g") , &width , NULL , NULL , NULL , NULL );
1019
1020 return width;
1021}
1022
1023wxCoord wxGCDC::GetCharHeight(void) const
1024{
1025 wxCoord height;
1026 DoGetTextExtent( wxT("g") , NULL , &height , NULL , NULL , NULL );
1027
1028 return height;
1029}
1030
1031void wxGCDC::Clear(void)
1032{
1033 wxCHECK_RET( Ok(), wxT("wxGCDC(cg)::Clear - invalid DC") );
1034 // TODO better implementation / incorporate size info into wxGCDC or context
1035 m_graphicContext->SetBrush( m_backgroundBrush );
1036 wxPen p = *wxTRANSPARENT_PEN;
1037 m_graphicContext->SetPen( p );
1038 DoDrawRectangle( 0, 0, 32000 , 32000 );
1039 m_graphicContext->SetPen( m_pen );
1040 m_graphicContext->SetBrush( m_brush );
1041}
1042
1043void wxGCDC::DoGetSize(int *width, int *height) const
1044{
1045 *width = 1000;
1046 *height = 1000;
1047}
1048
1049void wxGCDC::DoGradientFillLinear(const wxRect& rect,
1050 const wxColour& initialColour,
1051 const wxColour& destColour,
1052 wxDirection nDirection )
1053{
1054 wxPoint start;
1055 wxPoint end;
1056 switch( nDirection)
1057 {
1058 case wxWEST :
1059 start = rect.GetRightBottom();
1060 start.x++;
1061 end = rect.GetLeftBottom();
1062 break;
1063 case wxEAST :
1064 start = rect.GetLeftBottom();
1065 end = rect.GetRightBottom();
1066 end.x++;
1067 break;
1068 case wxNORTH :
1069 start = rect.GetLeftBottom();
1070 start.y++;
1071 end = rect.GetLeftTop();
1072 break;
1073 case wxSOUTH :
1074 start = rect.GetLeftTop();
1075 end = rect.GetLeftBottom();
1076 end.y++;
1077 break;
1078 default :
1079 break;
1080 }
1081
1082 m_graphicContext->SetBrush( m_graphicContext->CreateLinearGradientBrush(
1083 LogicalToDeviceX(start.x),LogicalToDeviceY(start.y),
1084 LogicalToDeviceX(end.x),LogicalToDeviceY(end.y), initialColour, destColour));
1085
1086 wxDouble xx = LogicalToDeviceX(rect.x);
1087 wxDouble yy = LogicalToDeviceY(rect.y);
1088 wxDouble ww = m_signX * LogicalToDeviceXRel(rect.width);
1089 wxDouble hh = m_signY * LogicalToDeviceYRel(rect.height);
1090
1091 if (ww == 0 || hh == 0)
1092 return;
1093
1094 if (ww < 0)
1095 {
1096 ww = -ww;
1097 xx = xx - ww;
1098 }
1099 if (hh < 0)
1100 {
1101 hh = -hh;
1102 yy = yy - hh;
1103 }
1104
1105 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
1106 m_graphicContext->DrawRectangle(xx,yy,ww,hh);
1107 m_graphicContext->SetPen(m_pen);
1108}
1109
1110void wxGCDC::DoGradientFillConcentric(const wxRect& rect,
1111 const wxColour& initialColour,
1112 const wxColour& destColour,
1113 const wxPoint& circleCenter)
1114{
1115 //Radius
1116 wxInt32 cx = rect.GetWidth() / 2;
1117 wxInt32 cy = rect.GetHeight() / 2;
1118 wxInt32 nRadius;
1119 if (cx < cy)
1120 nRadius = cx;
1121 else
1122 nRadius = cy;
1123
1124 wxDouble xx = LogicalToDeviceX(rect.x);
1125 wxDouble yy = LogicalToDeviceY(rect.y);
1126 wxDouble ww = m_signX * LogicalToDeviceXRel(rect.width);
1127 wxDouble hh = m_signY * LogicalToDeviceYRel(rect.height);
1128
1129 if (ww == 0 || hh == 0)
1130 return;
1131
1132 if (ww < 0)
1133 {
1134 ww = -ww;
1135 xx = xx - ww;
1136 }
1137 if (hh < 0)
1138 {
1139 hh = -hh;
1140 yy = yy - hh;
1141 }
1142
1143 m_graphicContext->SetPen(*wxTRANSPARENT_PEN);
1144 m_graphicContext->SetBrush( wxBrush( destColour) );
1145 m_graphicContext->DrawRectangle( xx,yy,ww,hh);
1146
1147 m_graphicContext->SetBrush( m_graphicContext->CreateRadialGradientBrush(
1148 xx+LogicalToDeviceX(circleCenter.x),yy+LogicalToDeviceY(circleCenter.y),
1149 xx+LogicalToDeviceX(circleCenter.x),yy+LogicalToDeviceY(circleCenter.y),
1150 LogicalToDeviceXRel(nRadius),
1151 initialColour,destColour));
1152
1153 m_graphicContext->DrawRectangle( xx,yy,ww,hh);
1154 m_graphicContext->SetPen(m_pen);
1155}
1156
1157void wxGCDC::DoDrawCheckMark(wxCoord x, wxCoord y,
1158 wxCoord width, wxCoord height)
1159{
1160 wxDCBase::DoDrawCheckMark(x,y,width,height);
1161}
1162
1163#endif // wxUSE_GRAPHICS_CONTEXT