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