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