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