]> git.saurik.com Git - wxWidgets.git/blame - src/generic/graphicc.cpp
use window's font instead of the default one in wxStatusBar
[wxWidgets.git] / src / generic / graphicc.cpp
CommitLineData
184fc6c8
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/generic/graphicc.cpp
3// Purpose: cairo device context class
4// Author: Stefan Csomor
5// Modified by:
e0876d73 6// Created: 2006-10-03
184fc6c8 7// RCS-ID: $Id$
e0876d73 8// Copyright: (c) 2006 Stefan Csomor
184fc6c8
SC
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#include "wx/dc.h"
15
184fc6c8
SC
16#ifdef __BORLANDC__
17#pragma hdrstop
18#endif
19
20#ifndef WX_PRECOMP
21#include "wx/image.h"
22#include "wx/window.h"
23#include "wx/dc.h"
24#include "wx/utils.h"
25#include "wx/dialog.h"
26#include "wx/app.h"
27#include "wx/bitmap.h"
28#include "wx/dcmemory.h"
29#include "wx/log.h"
30#include "wx/icon.h"
31#include "wx/dcprint.h"
32#include "wx/module.h"
33#endif
34
35#include "wx/graphics.h"
36
7ba86d93
RD
37#if wxUSE_GRAPHICS_CONTEXT
38
184fc6c8
SC
39#include <vector>
40
41using namespace std;
42
43//-----------------------------------------------------------------------------
44// constants
45//-----------------------------------------------------------------------------
46
47const double RAD2DEG = 180.0 / M_PI;
48
49//-----------------------------------------------------------------------------
50// Local functions
51//-----------------------------------------------------------------------------
52
53static inline double dmin(double a, double b)
54{
55 return a < b ? a : b;
56}
57static inline double dmax(double a, double b)
58{
59 return a > b ? a : b;
60}
61
62static inline double DegToRad(double deg)
63{
64 return (deg * M_PI) / 180.0;
65}
66static inline double RadToDeg(double deg)
67{
68 return (deg * 180.0) / M_PI;
69}
70
71//-----------------------------------------------------------------------------
72// device context implementation
73//
74// more and more of the dc functionality should be implemented by calling
75// the appropricate wxCairoContext, but we will have to do that step by step
76// also coordinate conversions should be moved to native matrix ops
77//-----------------------------------------------------------------------------
78
79// we always stock two context states, one at entry, to be able to preserve the
80// state we were called with, the other one after changing to HI Graphics orientation
81// (this one is used for getting back clippings etc)
82
83//-----------------------------------------------------------------------------
84// wxGraphicsPath implementation
85//-----------------------------------------------------------------------------
86
87// TODO remove this dependency (gdiplus needs the macros)
88
89#ifndef max
90#define max(a,b) (((a) > (b)) ? (a) : (b))
91#endif
92
93#ifndef min
94#define min(a,b) (((a) < (b)) ? (a) : (b))
95#endif
96
97#include <cairo.h>
98#include <gtk/gtk.h>
99
100class WXDLLEXPORT wxCairoPath : public wxGraphicsPath
101{
102 DECLARE_NO_COPY_CLASS(wxCairoPath)
103public :
104 wxCairoPath();
105 ~wxCairoPath();
106
107
108 //
109 // These are the path primitives from which everything else can be constructed
110 //
111
112 // begins a new subpath at (x,y)
113 virtual void MoveToPoint( wxDouble x, wxDouble y );
114
115 // adds a straight line from the current point to (x,y)
116 virtual void AddLineToPoint( wxDouble x, wxDouble y );
117
118 // adds a cubic Bezier curve from the current point, using two control points and an end point
119 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
120
121
122 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
123 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ;
124
125 // gets the last point of the current path, (0,0) if not yet set
126 virtual void GetCurrentPoint( wxDouble& x, wxDouble&y) ;
127
128 // closes the current sub-path
129 virtual void CloseSubpath();
130
131 //
132 // These are convenience functions which - if not available natively will be assembled
133 // using the primitives from above
134 //
135
136 /*
137
138 // appends a rectangle as a new closed subpath
139 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
140 // appends an ellipsis as a new closed subpath fitting the passed rectangle
141 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
142
143 // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1)
144 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
145 */
146
cd5adaa6
RD
147 // returns the native path
148 virtual void * GetNativePath() const ;
149
150 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
151 virtual void UnGetNativePath(void *p) ;
f540e5bd 152
184fc6c8
SC
153private :
154 cairo_t* m_pathContext;
155};
156
157wxCairoPath::wxCairoPath()
158{
159 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
160 m_pathContext = cairo_create(surface);
161 cairo_surface_destroy (surface);
162}
163
164wxCairoPath::~wxCairoPath()
165{
166 cairo_destroy(m_pathContext);
167}
168
cd5adaa6 169void* wxCairoPath::GetNativePath() const
184fc6c8
SC
170{
171 return cairo_copy_path(m_pathContext) ;
172}
173
f540e5bd
SC
174void wxCairoPath::UnGetNativePath(void *p)
175{
cd5adaa6 176 cairo_path_destroy((cairo_path_t*)p);
f540e5bd
SC
177}
178
184fc6c8
SC
179//
180// The Primitives
181//
182
183void wxCairoPath::MoveToPoint( wxDouble x , wxDouble y )
184{
185 cairo_move_to(m_pathContext,x,y);
186}
187
188void wxCairoPath::AddLineToPoint( wxDouble x , wxDouble y )
189{
190 cairo_line_to(m_pathContext,x,y);
191}
192
193void wxCairoPath::CloseSubpath()
194{
195 cairo_close_path(m_pathContext);
196}
197
198void wxCairoPath::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
199{
200 cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y);
201}
202
203// gets the last point of the current path, (0,0) if not yet set
204void wxCairoPath::GetCurrentPoint( wxDouble& x, wxDouble&y)
205{
206 double dx,dy;
207 cairo_get_current_point(m_pathContext,&dx,&dy);
208 x = dx;
209 y = dy;
210}
211
212void wxCairoPath::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
213{
214 // as clockwise means positive in our system (y pointing downwards)
215 // TODO make this interpretation dependent of the
216 // real device trans
217 if ( clockwise||(endAngle-startAngle)>=2*M_PI)
218 cairo_arc(m_pathContext,x,y,r,startAngle,endAngle);
219 else
220 cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle);
221}
222
223/*
224void wxCairoPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
225{
226 m_path->AddRectangle(RectF(x,y,w,h));
227}
228*/
229//
230//
231//
232/*
233// closes the current subpath
234void wxCairoPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
235{
236// CGPathAddArcToPoint( m_path, NULL , x1, y1, x2, y2, r);
237}
238
239*/
240
241class WXDLLEXPORT wxCairoContext : public wxGraphicsContext
242{
243 DECLARE_NO_COPY_CLASS(wxCairoContext)
244
245public:
246 wxCairoContext( const wxWindowDC& dc );
247 wxCairoContext();
248 virtual ~wxCairoContext();
249
250 virtual void Clip( const wxRegion &region );
539e2795
SC
251
252 // clips drawings to the rect
253 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
539e2795 254
cd5adaa6
RD
255 // resets the clipping to original extent
256 virtual void ResetClip();
257
258 virtual void * GetNativeContext();
259
184fc6c8
SC
260 virtual void StrokePath( const wxGraphicsPath *p );
261 virtual void FillPath( const wxGraphicsPath *p , int fillStyle = wxWINDING_RULE );
262
263 virtual wxGraphicsPath* CreatePath();
264 virtual void SetPen( const wxPen &pen );
265 virtual void SetBrush( const wxBrush &brush );
266 virtual void SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxColour&c1, const wxColour&c2) ;
267 virtual void SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
268 const wxColour &oColor, const wxColour &cColor);
269
270 virtual void Translate( wxDouble dx , wxDouble dy );
271 virtual void Scale( wxDouble xScale , wxDouble yScale );
272 virtual void Rotate( wxDouble angle );
273
274 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
275 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
276 virtual void PushState();
277 virtual void PopState();
278
279 virtual void SetFont( const wxFont &font );
280 virtual void SetTextColor( const wxColour &col );
281 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y);
282 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
283 wxDouble *descent, wxDouble *externalLeading ) const;
284 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
285
286private:
287 cairo_t* m_context;
288 bool m_penTransparent;
289 bool m_brushTransparent;
290
291 wxPen m_pen;
292 wxBrush m_brush;
293 cairo_pattern_t* m_brushPattern;
294 wxColour m_textColour;
295};
296
297
298
299
300//-----------------------------------------------------------------------------
301// wxCairoContext implementation
302//-----------------------------------------------------------------------------
303
304wxCairoContext::wxCairoContext( const wxWindowDC& dc )
305{
306 m_context = gdk_cairo_create( dc.m_window ) ;
307 PushState();
308 PushState();
309 m_penTransparent = true;
310 m_brushTransparent = true;
311 m_brushPattern = NULL ;
312}
313
314wxCairoContext::~wxCairoContext()
315{
316 if ( m_context )
317 {
318 PopState();
319 PopState();
320 cairo_destroy(m_context);
321 if ( m_brushPattern )
322 cairo_pattern_destroy(m_brushPattern);
323 }
324}
325
326
539e2795 327void wxCairoContext::Clip( const wxRegion & WXUNUSED(region) )
184fc6c8 328{
539e2795 329// TODO
184fc6c8
SC
330}
331
539e2795
SC
332void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
333{
334// TODO
335}
cd5adaa6 336
539e2795
SC
337void wxCairoContext::ResetClip()
338{
339// TODO
340}
341
342
f540e5bd 343void wxCairoContext::StrokePath( const wxGraphicsPath *path )
184fc6c8
SC
344{
345 if ( m_penTransparent )
346 return;
347
cd5adaa6 348 cairo_path_t* cp = (cairo_path_t*) path->GetNativePath() ;
184fc6c8
SC
349 cairo_append_path(m_context,cp);
350
351 // setup pen
352
353 // TODO: * m_dc->m_scaleX
354 double penWidth = m_pen.GetWidth();
355 if (penWidth <= 0.0)
356 penWidth = 0.1;
357
358 cairo_set_line_width(m_context,penWidth);
359 cairo_set_source_rgba(m_context,m_pen.GetColour().Red()/255.0,
360 m_pen.GetColour().Green()/255.0, m_pen.GetColour().Blue()/255.0,m_pen.GetColour().Alpha()/255.0);
361
362 cairo_line_cap_t cap;
363 switch ( m_pen.GetCap() )
364 {
365 case wxCAP_ROUND :
366 cap = CAIRO_LINE_CAP_ROUND;
367 break;
368
369 case wxCAP_PROJECTING :
370 cap = CAIRO_LINE_CAP_SQUARE;
371 break;
372
373 case wxCAP_BUTT :
374 cap = CAIRO_LINE_CAP_BUTT;
375 break;
376
377 default :
378 cap = CAIRO_LINE_CAP_BUTT;
379 break;
380 }
381 cairo_set_line_cap(m_context,cap);
382
383 cairo_line_join_t join;
384 switch ( m_pen.GetJoin() )
385 {
386 case wxJOIN_BEVEL :
387 join = CAIRO_LINE_JOIN_BEVEL;
388 break;
389
390 case wxJOIN_MITER :
391 join = CAIRO_LINE_JOIN_MITER;
392 break;
393
394 case wxJOIN_ROUND :
395 join = CAIRO_LINE_JOIN_ROUND;
396 break;
397
398 default :
399 join = CAIRO_LINE_JOIN_MITER;
400 break;
401 }
402 cairo_set_line_join(m_context,join);
403
404 int num_dashes = 0;
405 const double * dashes = NULL;
406 double offset = 0.0;
407
408 const double dashUnit = penWidth < 1.0 ? 1.0 : penWidth;
409
410 double *userLengths = NULL;
411 const double dotted[] =
412 {
413 dashUnit , dashUnit + 2.0
414 };
415 const double short_dashed[] =
416 {
417 9.0 , 6.0
418 };
419 const double dashed[] =
420 {
421 19.0 , 9.0
422 };
423 const double dotted_dashed[] =
424 {
425 9.0 , 6.0 , 3.0 , 3.0
426 };
427
428 switch ( m_pen.GetStyle() )
429 {
430 case wxSOLID :
431 break;
432
433 case wxDOT :
434 dashes = dotted ;
435 num_dashes = WXSIZEOF(dotted)
436 ;
437 break;
438
439 case wxLONG_DASH :
440 dashes = dotted ;
441 num_dashes = WXSIZEOF(dashed);
442 break;
443
444 case wxSHORT_DASH :
445 dashes = dotted ;
446 num_dashes = WXSIZEOF(short_dashed);
447 break;
448
449 case wxDOT_DASH :
450 dashes = dotted ;
451 num_dashes = WXSIZEOF(dotted_dashed);
452 break;
453
454 case wxUSER_DASH :
455 {
456 wxDash *wxdashes ;
457 num_dashes = m_pen.GetDashes( &wxdashes ) ;
458 if ((wxdashes != NULL) && (num_dashes > 0))
459 {
460 userLengths = new double[num_dashes] ;
461 for ( int i = 0 ; i < num_dashes ; ++i )
462 {
463 userLengths[i] = wxdashes[i] * dashUnit ;
464
465 if ( i % 2 == 1 && userLengths[i] < dashUnit + 2.0 )
466 userLengths[i] = dashUnit + 2.0 ;
467 else if ( i % 2 == 0 && userLengths[i] < dashUnit )
468 userLengths[i] = dashUnit ;
469 }
470 }
471 dashes = userLengths ;
472 }
473 break;
474 case wxSTIPPLE :
475 {
476 /*
477 wxBitmap* bmp = pen.GetStipple();
478 if ( bmp && bmp->Ok() )
479 {
480 wxDELETE( m_penImage );
481 wxDELETE( m_penBrush );
482 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
483 m_penBrush = new TextureBrush(m_penImage);
484 m_pen->SetBrush( m_penBrush );
485 }
486 */
487 }
488 break;
489 default :
490 if ( m_pen.GetStyle() >= wxFIRST_HATCH && m_pen.GetStyle() <= wxLAST_HATCH )
491 {
492 /*
493 wxDELETE( m_penBrush );
494 HatchStyle style = HatchStyleHorizontal;
495 switch( pen.GetStyle() )
496 {
497 case wxBDIAGONAL_HATCH :
498 style = HatchStyleBackwardDiagonal;
499 break ;
500 case wxCROSSDIAG_HATCH :
501 style = HatchStyleDiagonalCross;
502 break ;
503 case wxFDIAGONAL_HATCH :
504 style = HatchStyleForwardDiagonal;
505 break ;
506 case wxCROSS_HATCH :
507 style = HatchStyleCross;
508 break ;
509 case wxHORIZONTAL_HATCH :
510 style = HatchStyleHorizontal;
511 break ;
512 case wxVERTICAL_HATCH :
513 style = HatchStyleVertical;
514 break ;
515
516 }
517 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
518 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
519 m_pen->SetBrush( m_penBrush )
520 */
521 }
522 break;
523 }
524
525 cairo_set_dash(m_context,(double*)dashes,num_dashes,offset);
526 if ( userLengths )
527 delete[] userLengths;
528 cairo_stroke(m_context);
cd5adaa6 529 wxConstCast(path, wxGraphicsPath)->UnGetNativePath(cp);
184fc6c8
SC
530}
531
f540e5bd 532void wxCairoContext::FillPath( const wxGraphicsPath *path , int fillStyle )
184fc6c8
SC
533{
534 if ( !m_brushTransparent )
535 {
cd5adaa6 536 cairo_path_t* cp = (cairo_path_t*) path->GetNativePath() ;
184fc6c8
SC
537 cairo_append_path(m_context,cp);
538
539 if ( m_brushPattern )
540 {
541 cairo_set_source(m_context,m_brushPattern);
542 }
543 else
544 {
545 cairo_set_source_rgba(m_context,m_brush.GetColour().Red()/255.0,
546 m_brush.GetColour().Green()/255.0,
547 m_brush.GetColour().Blue()/255.0,
548 m_brush.GetColour().Alpha()/255.0);
549 }
550
551 cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
552 cairo_fill(m_context);
cd5adaa6 553 wxConstCast(path, wxGraphicsPath)->UnGetNativePath(cp);
184fc6c8
SC
554 }
555}
556
557wxGraphicsPath* wxCairoContext::CreatePath()
558{
559 return new wxCairoPath();
560}
561
562void wxCairoContext::Rotate( wxDouble angle )
563{
564 cairo_rotate(m_context,angle);
565}
566
567void wxCairoContext::Translate( wxDouble dx , wxDouble dy )
568{
569 cairo_translate(m_context,dx,dy);
570}
571
572void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale )
573{
574 cairo_scale(m_context,xScale,yScale);
575 /*
576 PointF penWidth( m_pen->GetWidth(), 0);
577 Matrix matrix ;
578 if ( !m_penTransparent )
579 {
580 m_context->GetTransform(&matrix);
581 matrix.TransformVectors(&penWidth);
582 }
583 m_context->ScaleTransform(xScale,yScale);
584 if ( !m_penTransparent )
585 {
586 m_context->GetTransform(&matrix);
587 matrix.Invert();
588 matrix.TransformVectors(&penWidth) ;
589 m_pen->SetWidth( sqrt( penWidth.X*penWidth.X + penWidth.Y*penWidth.Y));
590 }
591 */
592}
593
594void wxCairoContext::PushState()
595{
596 cairo_save(m_context);
597}
598
599void wxCairoContext::PopState()
600{
601 cairo_restore(m_context);
602}
603
604void wxCairoContext::SetTextColor( const wxColour &col )
605{
606 m_textColour = col;
607}
608
609void wxCairoContext::SetPen( const wxPen &pen )
610{
611 m_pen = pen ;
612 m_penTransparent = pen.GetStyle() == wxTRANSPARENT;
613 if ( m_penTransparent )
614 return;
615
616 /*
617
618 case wxSTIPPLE :
619 {
620 wxBitmap* bmp = pen.GetStipple();
621 if ( bmp && bmp->Ok() )
622 {
623 wxDELETE( m_penImage );
624 wxDELETE( m_penBrush );
625 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
626 m_penBrush = new TextureBrush(m_penImage);
627 m_pen->SetBrush( m_penBrush );
628 }
629
630 }
631 break;
632 default :
633 if ( pen.GetStyle() >= wxFIRST_HATCH && pen.GetStyle() <= wxLAST_HATCH )
634 {
635 wxDELETE( m_penBrush );
636 HatchStyle style = HatchStyleHorizontal;
637 switch( pen.GetStyle() )
638 {
639 case wxBDIAGONAL_HATCH :
640 style = HatchStyleBackwardDiagonal;
641 break ;
642 case wxCROSSDIAG_HATCH :
643 style = HatchStyleDiagonalCross;
644 break ;
645 case wxFDIAGONAL_HATCH :
646 style = HatchStyleForwardDiagonal;
647 break ;
648 case wxCROSS_HATCH :
649 style = HatchStyleCross;
650 break ;
651 case wxHORIZONTAL_HATCH :
652 style = HatchStyleHorizontal;
653 break ;
654 case wxVERTICAL_HATCH :
655 style = HatchStyleVertical;
656 break ;
657
658 }
659 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
660 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
661 m_pen->SetBrush( m_penBrush );
662 }
663 break;
664 }
665 if ( dashStyle != DashStyleSolid )
666 m_pen->SetDashStyle(dashStyle);
667 */
668}
669
670void wxCairoContext::SetBrush( const wxBrush &brush )
671{
672 m_brush = brush;
673 if (m_brushPattern)
674 {
675 cairo_pattern_destroy(m_brushPattern);
676 m_brushPattern = NULL;
677 }
678 m_brushTransparent = brush.GetStyle() == wxTRANSPARENT;
679
680 if ( m_brushTransparent )
681 return;
682 /*
683 wxDELETE(m_brush);
684
685 if ( brush.GetStyle() == wxSOLID)
686 {
687 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
688 brush.GetColour().Green() , brush.GetColour().Blue() ) );
689 }
690 else if ( brush.IsHatch() )
691 {
692 HatchStyle style = HatchStyleHorizontal;
693 switch( brush.GetStyle() )
694 {
695 case wxBDIAGONAL_HATCH :
696 style = HatchStyleBackwardDiagonal;
697 break ;
698 case wxCROSSDIAG_HATCH :
699 style = HatchStyleDiagonalCross;
700 break ;
701 case wxFDIAGONAL_HATCH :
702 style = HatchStyleForwardDiagonal;
703 break ;
704 case wxCROSS_HATCH :
705 style = HatchStyleCross;
706 break ;
707 case wxHORIZONTAL_HATCH :
708 style = HatchStyleHorizontal;
709 break ;
710 case wxVERTICAL_HATCH :
711 style = HatchStyleVertical;
712 break ;
713
714 }
715 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
716 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
717 }
718 else
719 {
720 wxBitmap* bmp = brush.GetStipple();
721 if ( bmp && bmp->Ok() )
722 {
723 wxDELETE( m_brushImage );
724 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
725 m_brush = new TextureBrush(m_brushImage);
726 }
727 }
728 */
729}
730
731void wxCairoContext::SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxColour&c1, const wxColour&c2)
732{
733 if ( m_brushPattern )
734 {
735 cairo_pattern_destroy(m_brushPattern);
736 m_brushPattern=NULL;
737 }
738
739 m_brushTransparent = false;
740 m_brushPattern = cairo_pattern_create_linear(x1,y1,x2,y2);
741 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,c1.Red()/255.0,
742 c1.Green()/255.0, c1.Blue()/255.0,c1.Alpha()/255.0);
743 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,c2.Red()/255.0,
744 c2.Green()/255.0, c2.Blue()/255.0,c2.Alpha()/255.0);
745 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
746}
747
748void wxCairoContext::SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
749 const wxColour &oColor, const wxColour &cColor)
750{
751 if ( m_brushPattern )
752 {
753 cairo_pattern_destroy(m_brushPattern);
754 m_brushPattern=NULL;
755 }
756
757 m_brushTransparent = false;
758 m_brushPattern = cairo_pattern_create_radial(xo,yo,0.0,xc,yc,radius);
759 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,oColor.Red()/255.0,
760 oColor.Green()/255.0, oColor.Blue()/255.0,oColor.Alpha()/255.0);
761 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,cColor.Red()/255.0,
762 cColor.Green()/255.0, cColor.Blue()/255.0,cColor.Alpha()/255.0);
763 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
764 }
765
766void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
767{
768 /*
769 Bitmap* image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
770 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
771 delete image ;
772 */
773}
774
775void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
776{
777 /*
778 Bitmap* image = Bitmap::FromHICON((HICON)icon.GetHICON());
779 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
780 delete image ;
781 */
782}
783
784
785void wxCairoContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
786{
787 if ( str.IsEmpty())
788 return ;
789 cairo_move_to(m_context,x,y);
790 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
791
792 cairo_set_source_rgba(m_context,m_textColour.Red()/255.0,
793 m_textColour.Green()/255.0, m_textColour.Blue()/255.0,m_textColour.Alpha()/255.0);
794 cairo_show_text(m_context,buf);
795
796 // TODO m_backgroundMode == wxSOLID
797}
798
799void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
800 wxDouble *descent, wxDouble *externalLeading ) const
801{
802 /*
803 wxWCharBuffer s = str.wc_str( *wxConvUI );
804 FontFamily ffamily ;
805
806 m_font->GetFamily(&ffamily) ;
807
808 REAL factorY = m_context->GetDpiY() / 72.0 ;
809
810 REAL rDescent = ffamily.GetCellDescent(FontStyleRegular) *
811 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
812 REAL rAscent = ffamily.GetCellAscent(FontStyleRegular) *
813 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
814 REAL rHeight = ffamily.GetLineSpacing(FontStyleRegular) *
815 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
816
817 if ( height )
818 *height = rHeight * factorY + 0.5 ;
819 if ( descent )
820 *descent = rDescent * factorY + 0.5 ;
821 if ( externalLeading )
822 *externalLeading = (rHeight - rAscent - rDescent) * factorY + 0.5 ;
823 // measuring empty strings is not guaranteed, so do it by hand
824 if ( str.IsEmpty())
825 {
826 if ( width )
827 *width = 0 ;
828 }
829 else
830 {
831 // MeasureString does return a rectangle that is way too large, so it is
832 // not usable here
833 RectF layoutRect(0,0, 100000.0f, 100000.0f);
834 StringFormat strFormat;
835 CharacterRange strRange(0,wcslen(s));
836 strFormat.SetMeasurableCharacterRanges(1,&strRange);
837 Region region ;
838 m_context->MeasureCharacterRanges(s, -1 , m_font,layoutRect, &strFormat,1,&region) ;
839 RectF bbox ;
840 region.GetBounds(&bbox,m_context);
841 if ( width )
842 *width = bbox.GetRight()-bbox.GetLeft()+0.5;
843 }
844 */
845}
846
847void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
848{
849 widths.Empty();
850 widths.Add(0, text.length());
851
852 if (text.empty())
853 return;
854 /*
855 wxWCharBuffer ws = text.wc_str( *wxConvUI );
856 size_t len = wcslen( ws ) ;
857 wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations"));
858
859 RectF layoutRect(0,0, 100000.0f, 100000.0f);
860 StringFormat strFormat;
861
862 CharacterRange* ranges = new CharacterRange[len] ;
863 Region* regions = new Region[len];
864 for( int i = 0 ; i < len ; ++i)
865 {
866 ranges[i].First = i ;
867 ranges[i].Length = 1 ;
868 }
869 strFormat.SetMeasurableCharacterRanges(len,ranges);
870 m_context->MeasureCharacterRanges(ws, -1 , m_font,layoutRect, &strFormat,1,regions) ;
871
872 RectF bbox ;
873 for ( int i = 0 ; i < len ; ++i)
874 {
875 regions[i].GetBounds(&bbox,m_context);
876 widths[i] = bbox.GetRight()-bbox.GetLeft();
877 }
878 */
879}
880
881void wxCairoContext::SetFont( const wxFont &font )
882{
883 cairo_select_font_face(m_context,font.GetFaceName().mb_str(wxConvUTF8),
884 font.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC:CAIRO_FONT_SLANT_NORMAL,
885 font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD:CAIRO_FONT_WEIGHT_NORMAL);
886
887 cairo_set_font_size(m_context,font.GetPointSize());
888 // TODO UNDERLINE
889 // TODO FIX SIZE
890}
891
539e2795
SC
892void * wxCairoContext::GetNativeContext()
893{
cd5adaa6 894 return m_context;
539e2795
SC
895}
896
184fc6c8
SC
897wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC& dc )
898{
899 return new wxCairoContext(dc);
7ba86d93
RD
900}
901
539e2795
SC
902wxGraphicsContext* wxGraphicsContext::Create( wxWindow * window )
903{
cd5adaa6 904 return NULL; // TODO
539e2795
SC
905}
906
907wxGraphicsContext* wxGraphicsContext::CreateFromNative( void * context )
908{
cd5adaa6 909 return NULL; // TODO
539e2795
SC
910}
911
7ba86d93 912#endif // wxUSE_GRAPHICS_CONTEXT