]>
Commit | Line | Data |
---|---|---|
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 | ||
184fc6c8 | 14 | #ifdef __BORLANDC__ |
7f0d0111 | 15 | #pragma hdrstop |
184fc6c8 SC |
16 | #endif |
17 | ||
9a67941f | 18 | #if wxUSE_GRAPHICS_CONTEXT |
c0e69d72 | 19 | |
6c99dbd5 | 20 | #include "wx/graphics.h" |
9a67941f VZ |
21 | |
22 | #if wxUSE_CAIRO | |
23 | ||
932d0768 RD |
24 | // keep cairo.h from defining dllimport as we're defining the symbols inside |
25 | // the wx dll in order to load them dynamically. | |
26 | #define cairo_public | |
27 | ||
6c99dbd5 VZ |
28 | #include "wx/cairo.h" |
29 | ||
184fc6c8 | 30 | #ifndef WX_PRECOMP |
7f0d0111 VZ |
31 | #include "wx/bitmap.h" |
32 | #include "wx/icon.h" | |
7f0d0111 VZ |
33 | #include "wx/dcclient.h" |
34 | #include "wx/dcmemory.h" | |
35 | #include "wx/dcprint.h" | |
081d8d96 PC |
36 | #ifdef __WXGTK__ |
37 | #include "wx/window.h" | |
38 | #endif | |
184fc6c8 SC |
39 | #endif |
40 | ||
2f94ab40 | 41 | #include "wx/private/graphics.h" |
d9485f89 | 42 | #include "wx/rawbmp.h" |
50de831a | 43 | #include "wx/vector.h" |
184fc6c8 | 44 | |
184fc6c8 SC |
45 | using namespace std; |
46 | ||
184fc6c8 SC |
47 | //----------------------------------------------------------------------------- |
48 | // device context implementation | |
49 | // | |
50 | // more and more of the dc functionality should be implemented by calling | |
51 | // the appropricate wxCairoContext, but we will have to do that step by step | |
52 | // also coordinate conversions should be moved to native matrix ops | |
53 | //----------------------------------------------------------------------------- | |
54 | ||
55 | // we always stock two context states, one at entry, to be able to preserve the | |
56 | // state we were called with, the other one after changing to HI Graphics orientation | |
57 | // (this one is used for getting back clippings etc) | |
58 | ||
59 | //----------------------------------------------------------------------------- | |
60 | // wxGraphicsPath implementation | |
61 | //----------------------------------------------------------------------------- | |
62 | ||
63 | // TODO remove this dependency (gdiplus needs the macros) | |
64 | ||
65 | #ifndef max | |
66 | #define max(a,b) (((a) > (b)) ? (a) : (b)) | |
67 | #endif | |
68 | ||
69 | #ifndef min | |
70 | #define min(a,b) (((a) < (b)) ? (a) : (b)) | |
71 | #endif | |
72 | ||
73 | #include <cairo.h> | |
c0e69d72 KO |
74 | #ifdef __WXMSW__ |
75 | #include <cairo-win32.h> | |
76 | #endif | |
77 | ||
00bd8e72 | 78 | #ifdef __WXGTK__ |
184fc6c8 | 79 | #include <gtk/gtk.h> |
84f67b11 | 80 | #include "wx/fontutil.h" |
888dde65 | 81 | #include "wx/gtk/dc.h" |
00bd8e72 | 82 | #endif |
184fc6c8 | 83 | |
e7f9f819 SC |
84 | #ifdef __WXMSW__ |
85 | #include <cairo-win32.h> | |
86 | #endif | |
87 | ||
88 | #ifdef __WXMAC__ | |
33ddeaba | 89 | #include "wx/osx/private.h" |
e7f9f819 SC |
90 | #include <cairo-quartz.h> |
91 | #include <cairo-atsui.h> | |
92 | #endif | |
93 | ||
0db8a70e | 94 | class WXDLLIMPEXP_CORE wxCairoPathData : public wxGraphicsPathData |
184fc6c8 | 95 | { |
184fc6c8 | 96 | public : |
0db8a70e RD |
97 | wxCairoPathData(wxGraphicsRenderer* renderer, cairo_t* path = NULL); |
98 | ~wxCairoPathData(); | |
184fc6c8 | 99 | |
0db8a70e | 100 | virtual wxGraphicsObjectRefData *Clone() const; |
184fc6c8 SC |
101 | |
102 | // | |
103 | // These are the path primitives from which everything else can be constructed | |
104 | // | |
105 | ||
106 | // begins a new subpath at (x,y) | |
107 | virtual void MoveToPoint( wxDouble x, wxDouble y ); | |
108 | ||
109 | // adds a straight line from the current point to (x,y) | |
110 | virtual void AddLineToPoint( wxDouble x, wxDouble y ); | |
111 | ||
112 | // adds a cubic Bezier curve from the current point, using two control points and an end point | |
113 | virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y ); | |
114 | ||
115 | ||
116 | // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle | |
117 | virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ; | |
118 | ||
119 | // gets the last point of the current path, (0,0) if not yet set | |
0db8a70e | 120 | virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const; |
184fc6c8 | 121 | |
00bd8e72 | 122 | // adds another path |
0db8a70e | 123 | virtual void AddPath( const wxGraphicsPathData* path ); |
00bd8e72 | 124 | |
184fc6c8 SC |
125 | // closes the current sub-path |
126 | virtual void CloseSubpath(); | |
127 | ||
128 | // | |
129 | // These are convenience functions which - if not available natively will be assembled | |
130 | // using the primitives from above | |
131 | // | |
132 | ||
133 | /* | |
134 | ||
49ad157e | 135 | // appends a rectangle as a new closed subpath |
184fc6c8 SC |
136 | virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ; |
137 | // appends an ellipsis as a new closed subpath fitting the passed rectangle | |
138 | virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ; | |
139 | ||
140 | // 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) | |
141 | virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ; | |
142 | */ | |
143 | ||
cd5adaa6 RD |
144 | // returns the native path |
145 | virtual void * GetNativePath() const ; | |
146 | ||
147 | // give the native path returned by GetNativePath() back (there might be some deallocations necessary) | |
0db8a70e | 148 | virtual void UnGetNativePath(void *p) const; |
f540e5bd | 149 | |
00bd8e72 | 150 | // transforms each point of this path by the matrix |
0db8a70e | 151 | virtual void Transform( const wxGraphicsMatrixData* matrix ) ; |
00bd8e72 SC |
152 | |
153 | // gets the bounding box enclosing all points (possibly including control points) | |
0db8a70e | 154 | virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const; |
00bd8e72 | 155 | |
94a007ec | 156 | virtual bool Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle = wxWINDING_RULE) const; |
00bd8e72 | 157 | |
184fc6c8 SC |
158 | private : |
159 | cairo_t* m_pathContext; | |
160 | }; | |
161 | ||
0db8a70e | 162 | class WXDLLIMPEXP_CORE wxCairoMatrixData : public wxGraphicsMatrixData |
184fc6c8 | 163 | { |
00bd8e72 | 164 | public : |
0db8a70e RD |
165 | wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix = NULL ) ; |
166 | virtual ~wxCairoMatrixData() ; | |
184fc6c8 | 167 | |
0db8a70e | 168 | virtual wxGraphicsObjectRefData *Clone() const ; |
184fc6c8 | 169 | |
00bd8e72 | 170 | // concatenates the matrix |
0db8a70e | 171 | virtual void Concat( const wxGraphicsMatrixData *t ); |
184fc6c8 | 172 | |
00bd8e72 | 173 | // sets the matrix to the respective values |
49ad157e | 174 | virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0, |
00bd8e72 | 175 | wxDouble tx=0.0, wxDouble ty=0.0); |
184fc6c8 | 176 | |
248802d0 RD |
177 | // gets the component valuess of the matrix |
178 | virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL, | |
179 | wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const; | |
49ad157e | 180 | |
00bd8e72 SC |
181 | // makes this the inverse matrix |
182 | virtual void Invert(); | |
184fc6c8 | 183 | |
00bd8e72 | 184 | // returns true if the elements of the transformation matrix are equal ? |
0db8a70e | 185 | virtual bool IsEqual( const wxGraphicsMatrixData* t) const ; |
184fc6c8 | 186 | |
00bd8e72 | 187 | // return true if this is the identity matrix |
0db8a70e | 188 | virtual bool IsIdentity() const; |
184fc6c8 | 189 | |
00bd8e72 SC |
190 | // |
191 | // transformation | |
192 | // | |
184fc6c8 | 193 | |
00bd8e72 SC |
194 | // add the translation to this matrix |
195 | virtual void Translate( wxDouble dx , wxDouble dy ); | |
196 | ||
197 | // add the scale to this matrix | |
198 | virtual void Scale( wxDouble xScale , wxDouble yScale ); | |
199 | ||
200 | // add the rotation to this matrix (radians) | |
49ad157e | 201 | virtual void Rotate( wxDouble angle ); |
00bd8e72 SC |
202 | |
203 | // | |
204 | // apply the transforms | |
205 | // | |
206 | ||
207 | // applies that matrix to the point | |
0db8a70e | 208 | virtual void TransformPoint( wxDouble *x, wxDouble *y ) const; |
00bd8e72 SC |
209 | |
210 | // applies the matrix except for translations | |
0db8a70e | 211 | virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const; |
00bd8e72 SC |
212 | |
213 | // returns the native representation | |
214 | virtual void * GetNativeMatrix() const; | |
215 | private: | |
216 | cairo_matrix_t m_matrix ; | |
00bd8e72 SC |
217 | } ; |
218 | ||
87752530 | 219 | class WXDLLIMPEXP_CORE wxCairoPenData : public wxGraphicsObjectRefData |
184fc6c8 | 220 | { |
00bd8e72 | 221 | public: |
87752530 SC |
222 | wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen ); |
223 | ~wxCairoPenData(); | |
00bd8e72 SC |
224 | |
225 | void Init(); | |
226 | ||
227 | virtual void Apply( wxGraphicsContext* context ); | |
228 | virtual wxDouble GetWidth() { return m_width; } | |
229 | ||
230 | private : | |
231 | double m_width; | |
49ad157e | 232 | |
00bd8e72 SC |
233 | double m_red; |
234 | double m_green; | |
235 | double m_blue; | |
236 | double m_alpha; | |
49ad157e | 237 | |
00bd8e72 SC |
238 | cairo_line_cap_t m_cap; |
239 | cairo_line_join_t m_join; | |
240 | ||
241 | int m_count; | |
242 | const double *m_lengths; | |
243 | double *m_userLengths; | |
184fc6c8 | 244 | |
00bd8e72 | 245 | wxPen m_pen; |
50de831a PC |
246 | |
247 | wxDECLARE_NO_COPY_CLASS(wxCairoPenData); | |
00bd8e72 SC |
248 | }; |
249 | ||
87752530 | 250 | class WXDLLIMPEXP_CORE wxCairoBrushData : public wxGraphicsObjectRefData |
184fc6c8 | 251 | { |
00bd8e72 | 252 | public: |
87752530 SC |
253 | wxCairoBrushData( wxGraphicsRenderer* renderer ); |
254 | wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush ); | |
255 | ~wxCairoBrushData (); | |
00bd8e72 SC |
256 | |
257 | virtual void Apply( wxGraphicsContext* context ); | |
4ee4c7b9 VZ |
258 | |
259 | void CreateLinearGradientBrush(wxDouble x1, wxDouble y1, | |
260 | wxDouble x2, wxDouble y2, | |
261 | const wxGraphicsGradientStops& stops); | |
262 | void CreateRadialGradientBrush(wxDouble xo, wxDouble yo, | |
263 | wxDouble xc, wxDouble yc, wxDouble radius, | |
264 | const wxGraphicsGradientStops& stops); | |
00bd8e72 SC |
265 | |
266 | protected: | |
267 | virtual void Init(); | |
268 | ||
4ee4c7b9 VZ |
269 | // common part of Create{Linear,Radial}GradientBrush() |
270 | void AddGradientStops(const wxGraphicsGradientStops& stops); | |
271 | ||
00bd8e72 SC |
272 | private : |
273 | double m_red; | |
274 | double m_green; | |
275 | double m_blue; | |
276 | double m_alpha; | |
277 | ||
278 | cairo_pattern_t* m_brushPattern; | |
00bd8e72 SC |
279 | }; |
280 | ||
87752530 | 281 | class wxCairoFontData : public wxGraphicsObjectRefData |
184fc6c8 | 282 | { |
00bd8e72 | 283 | public: |
87752530 SC |
284 | wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col ); |
285 | ~wxCairoFontData(); | |
00bd8e72 SC |
286 | |
287 | virtual void Apply( wxGraphicsContext* context ); | |
84f67b11 SC |
288 | #ifdef __WXGTK__ |
289 | const PangoFontDescription* GetFont() const { return m_font; } | |
0d19936d | 290 | bool GetUnderlined() const { return m_underlined; } |
84f67b11 | 291 | #endif |
00bd8e72 | 292 | private : |
00bd8e72 | 293 | double m_size; |
0d19936d | 294 | bool m_underlined; |
00bd8e72 SC |
295 | double m_red; |
296 | double m_green; | |
297 | double m_blue; | |
298 | double m_alpha; | |
84f67b11 | 299 | #ifdef __WXMAC__ |
e7f9f819 | 300 | cairo_font_face_t *m_font; |
84f67b11 SC |
301 | #elif defined(__WXGTK__) |
302 | PangoFontDescription* m_font; | |
303 | #else | |
304 | wxCharBuffer m_fontName; | |
305 | cairo_font_slant_t m_slant; | |
306 | cairo_font_weight_t m_weight; | |
307 | #endif | |
00bd8e72 | 308 | }; |
184fc6c8 | 309 | |
2fc9c1ea KO |
310 | class wxCairoBitmapData : public wxGraphicsObjectRefData |
311 | { | |
312 | public: | |
313 | wxCairoBitmapData( wxGraphicsRenderer* renderer, const wxBitmap& bmp ); | |
2986eb86 | 314 | wxCairoBitmapData( wxGraphicsRenderer* renderer, cairo_surface_t* bitmap ); |
2fc9c1ea KO |
315 | ~wxCairoBitmapData(); |
316 | ||
317 | virtual cairo_surface_t* GetCairoSurface() { return m_surface; } | |
318 | virtual cairo_pattern_t* GetCairoPattern() { return m_pattern; } | |
319 | virtual wxSize GetSize() { return wxSize(m_width, m_height); } | |
320 | private : | |
321 | cairo_surface_t* m_surface; | |
322 | cairo_pattern_t* m_pattern; | |
323 | int m_width; | |
324 | int m_height; | |
325 | unsigned char* m_buffer; | |
326 | }; | |
327 | ||
00bd8e72 | 328 | class WXDLLIMPEXP_CORE wxCairoContext : public wxGraphicsContext |
184fc6c8 | 329 | { |
184fc6c8 | 330 | public: |
00bd8e72 | 331 | wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc ); |
888dde65 | 332 | wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& dc ); |
c9008abe | 333 | wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc ); |
00bd8e72 SC |
334 | #ifdef __WXGTK__ |
335 | wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable ); | |
9f2b6b31 RD |
336 | #endif |
337 | #ifdef __WXMSW__ | |
338 | wxCairoContext( wxGraphicsRenderer* renderer, HDC context ); | |
00bd8e72 SC |
339 | #endif |
340 | wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context ); | |
341 | wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window); | |
184fc6c8 SC |
342 | wxCairoContext(); |
343 | virtual ~wxCairoContext(); | |
344 | ||
e7f9f819 | 345 | virtual bool ShouldOffset() const |
49ad157e | 346 | { |
136a1de9 SC |
347 | if ( !m_enableOffset ) |
348 | return false; | |
349 | ||
e7f9f819 SC |
350 | int penwidth = 0 ; |
351 | if ( !m_pen.IsNull() ) | |
352 | { | |
353 | penwidth = (int)((wxCairoPenData*)m_pen.GetRefData())->GetWidth(); | |
354 | if ( penwidth == 0 ) | |
355 | penwidth = 1; | |
356 | } | |
357 | return ( penwidth % 2 ) == 1; | |
358 | } | |
359 | ||
184fc6c8 | 360 | virtual void Clip( const wxRegion ®ion ); |
c0e69d72 KO |
361 | #ifdef __WXMSW__ |
362 | cairo_surface_t* m_mswSurface; | |
363 | #endif | |
539e2795 SC |
364 | |
365 | // clips drawings to the rect | |
366 | virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
539e2795 | 367 | |
cd5adaa6 RD |
368 | // resets the clipping to original extent |
369 | virtual void ResetClip(); | |
370 | ||
371 | virtual void * GetNativeContext(); | |
372 | ||
bf02a7f9 | 373 | virtual bool SetAntialiasMode(wxAntialiasMode antialias); |
49ad157e | 374 | |
133cb28b SC |
375 | virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation); |
376 | ||
bf02a7f9 SC |
377 | virtual bool SetCompositionMode(wxCompositionMode op); |
378 | ||
379 | virtual void BeginLayer(wxDouble opacity); | |
380 | ||
381 | virtual void EndLayer(); | |
03647350 | 382 | |
0db8a70e | 383 | virtual void StrokePath( const wxGraphicsPath& p ); |
94a007ec | 384 | virtual void FillPath( const wxGraphicsPath& p , wxPolygonFillMode fillStyle = wxWINDING_RULE ); |
184fc6c8 | 385 | |
184fc6c8 SC |
386 | virtual void Translate( wxDouble dx , wxDouble dy ); |
387 | virtual void Scale( wxDouble xScale , wxDouble yScale ); | |
388 | virtual void Rotate( wxDouble angle ); | |
389 | ||
00bd8e72 | 390 | // concatenates this transform with the current transform of this context |
0db8a70e | 391 | virtual void ConcatTransform( const wxGraphicsMatrix& matrix ); |
00bd8e72 SC |
392 | |
393 | // sets the transform of this context | |
0db8a70e | 394 | virtual void SetTransform( const wxGraphicsMatrix& matrix ); |
00bd8e72 SC |
395 | |
396 | // gets the matrix of this context | |
0db8a70e | 397 | virtual wxGraphicsMatrix GetTransform() const; |
00bd8e72 | 398 | |
e77cba1a | 399 | virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); |
184fc6c8 SC |
400 | virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); |
401 | virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
402 | virtual void PushState(); | |
403 | virtual void PopState(); | |
404 | ||
184fc6c8 SC |
405 | virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height, |
406 | wxDouble *descent, wxDouble *externalLeading ) const; | |
407 | virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const; | |
408 | ||
50de831a PC |
409 | protected: |
410 | virtual void DoDrawText( const wxString &str, wxDouble x, wxDouble y ); | |
411 | ||
184fc6c8 | 412 | private: |
e7f9f819 | 413 | void Init(cairo_t *context); |
49ad157e | 414 | |
184fc6c8 | 415 | cairo_t* m_context; |
03647350 | 416 | |
bf02a7f9 | 417 | wxVector<float> m_layerOpacities; |
0b7dce54 | 418 | |
c0c133e1 | 419 | wxDECLARE_NO_COPY_CLASS(wxCairoContext); |
184fc6c8 SC |
420 | }; |
421 | ||
184fc6c8 | 422 | //----------------------------------------------------------------------------- |
87752530 | 423 | // wxCairoPenData implementation |
184fc6c8 SC |
424 | //----------------------------------------------------------------------------- |
425 | ||
87752530 | 426 | wxCairoPenData::~wxCairoPenData() |
539e2795 | 427 | { |
00bd8e72 | 428 | delete[] m_userLengths; |
539e2795 | 429 | } |
cd5adaa6 | 430 | |
87752530 | 431 | void wxCairoPenData::Init() |
539e2795 | 432 | { |
00bd8e72 SC |
433 | m_lengths = NULL; |
434 | m_userLengths = NULL; | |
435 | m_width = 0; | |
436 | m_count = 0; | |
539e2795 SC |
437 | } |
438 | ||
87752530 SC |
439 | wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) |
440 | : wxGraphicsObjectRefData(renderer) | |
49ad157e | 441 | { |
00bd8e72 | 442 | Init(); |
7f8bd9fc RD |
443 | m_pen = pen; |
444 | m_width = m_pen.GetWidth(); | |
00bd8e72 SC |
445 | if (m_width <= 0.0) |
446 | m_width = 0.1; | |
539e2795 | 447 | |
00bd8e72 | 448 | m_red = m_pen.GetColour().Red()/255.0; |
49ad157e | 449 | m_green = m_pen.GetColour().Green()/255.0; |
00bd8e72 SC |
450 | m_blue = m_pen.GetColour().Blue()/255.0; |
451 | m_alpha = m_pen.GetColour().Alpha()/255.0; | |
184fc6c8 | 452 | |
184fc6c8 SC |
453 | switch ( m_pen.GetCap() ) |
454 | { | |
455 | case wxCAP_ROUND : | |
00bd8e72 | 456 | m_cap = CAIRO_LINE_CAP_ROUND; |
184fc6c8 SC |
457 | break; |
458 | ||
459 | case wxCAP_PROJECTING : | |
00bd8e72 | 460 | m_cap = CAIRO_LINE_CAP_SQUARE; |
184fc6c8 SC |
461 | break; |
462 | ||
463 | case wxCAP_BUTT : | |
00bd8e72 | 464 | m_cap = CAIRO_LINE_CAP_BUTT; |
184fc6c8 SC |
465 | break; |
466 | ||
467 | default : | |
00bd8e72 | 468 | m_cap = CAIRO_LINE_CAP_BUTT; |
184fc6c8 SC |
469 | break; |
470 | } | |
184fc6c8 | 471 | |
184fc6c8 SC |
472 | switch ( m_pen.GetJoin() ) |
473 | { | |
474 | case wxJOIN_BEVEL : | |
00bd8e72 | 475 | m_join = CAIRO_LINE_JOIN_BEVEL; |
184fc6c8 SC |
476 | break; |
477 | ||
478 | case wxJOIN_MITER : | |
00bd8e72 | 479 | m_join = CAIRO_LINE_JOIN_MITER; |
184fc6c8 SC |
480 | break; |
481 | ||
482 | case wxJOIN_ROUND : | |
00bd8e72 | 483 | m_join = CAIRO_LINE_JOIN_ROUND; |
184fc6c8 SC |
484 | break; |
485 | ||
486 | default : | |
00bd8e72 | 487 | m_join = CAIRO_LINE_JOIN_MITER; |
184fc6c8 SC |
488 | break; |
489 | } | |
184fc6c8 | 490 | |
00bd8e72 | 491 | const double dashUnit = m_width < 1.0 ? 1.0 : m_width; |
184fc6c8 | 492 | const double dotted[] = |
00bd8e72 SC |
493 | { |
494 | dashUnit , dashUnit + 2.0 | |
495 | }; | |
496 | static const double short_dashed[] = | |
497 | { | |
498 | 9.0 , 6.0 | |
499 | }; | |
500 | static const double dashed[] = | |
501 | { | |
502 | 19.0 , 9.0 | |
503 | }; | |
504 | static const double dotted_dashed[] = | |
505 | { | |
506 | 9.0 , 6.0 , 3.0 , 3.0 | |
507 | }; | |
184fc6c8 SC |
508 | |
509 | switch ( m_pen.GetStyle() ) | |
510 | { | |
fb040094 | 511 | case wxPENSTYLE_SOLID : |
184fc6c8 SC |
512 | break; |
513 | ||
fb040094 | 514 | case wxPENSTYLE_DOT : |
00bd8e72 SC |
515 | m_count = WXSIZEOF(dotted); |
516 | m_userLengths = new double[ m_count ] ; | |
517 | memcpy( m_userLengths, dotted, sizeof(dotted) ); | |
518 | m_lengths = m_userLengths; | |
184fc6c8 SC |
519 | break; |
520 | ||
fb040094 | 521 | case wxPENSTYLE_LONG_DASH : |
84f67b11 | 522 | m_lengths = dashed ; |
00bd8e72 | 523 | m_count = WXSIZEOF(dashed); |
184fc6c8 SC |
524 | break; |
525 | ||
fb040094 | 526 | case wxPENSTYLE_SHORT_DASH : |
84f67b11 | 527 | m_lengths = short_dashed ; |
00bd8e72 | 528 | m_count = WXSIZEOF(short_dashed); |
184fc6c8 SC |
529 | break; |
530 | ||
fb040094 | 531 | case wxPENSTYLE_DOT_DASH : |
84f67b11 | 532 | m_lengths = dotted_dashed ; |
00bd8e72 | 533 | m_count = WXSIZEOF(dotted_dashed); |
184fc6c8 SC |
534 | break; |
535 | ||
fb040094 | 536 | case wxPENSTYLE_USER_DASH : |
184fc6c8 SC |
537 | { |
538 | wxDash *wxdashes ; | |
00bd8e72 SC |
539 | m_count = m_pen.GetDashes( &wxdashes ) ; |
540 | if ((wxdashes != NULL) && (m_count > 0)) | |
184fc6c8 | 541 | { |
00bd8e72 SC |
542 | m_userLengths = new double[m_count] ; |
543 | for ( int i = 0 ; i < m_count ; ++i ) | |
184fc6c8 | 544 | { |
00bd8e72 | 545 | m_userLengths[i] = wxdashes[i] * dashUnit ; |
184fc6c8 | 546 | |
00bd8e72 SC |
547 | if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 ) |
548 | m_userLengths[i] = dashUnit + 2.0 ; | |
549 | else if ( i % 2 == 0 && m_userLengths[i] < dashUnit ) | |
550 | m_userLengths[i] = dashUnit ; | |
184fc6c8 SC |
551 | } |
552 | } | |
00bd8e72 | 553 | m_lengths = m_userLengths ; |
184fc6c8 SC |
554 | } |
555 | break; | |
fb040094 | 556 | case wxPENSTYLE_STIPPLE : |
184fc6c8 SC |
557 | { |
558 | /* | |
00bd8e72 | 559 | wxBitmap* bmp = pen.GetStipple(); |
a1b806b9 | 560 | if ( bmp && bmp->IsOk() ) |
00bd8e72 SC |
561 | { |
562 | wxDELETE( m_penImage ); | |
563 | wxDELETE( m_penBrush ); | |
564 | m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE()); | |
565 | m_penBrush = new TextureBrush(m_penImage); | |
566 | m_pen->SetBrush( m_penBrush ); | |
567 | } | |
184fc6c8 SC |
568 | */ |
569 | } | |
570 | break; | |
571 | default : | |
89efaf2b | 572 | if ( m_pen.GetStyle() >= wxPENSTYLE_FIRST_HATCH |
fb040094 | 573 | && m_pen.GetStyle() <= wxPENSTYLE_LAST_HATCH ) |
184fc6c8 SC |
574 | { |
575 | /* | |
00bd8e72 SC |
576 | wxDELETE( m_penBrush ); |
577 | HatchStyle style = HatchStyleHorizontal; | |
578 | switch( pen.GetStyle() ) | |
579 | { | |
fb040094 | 580 | case wxPENSTYLE_BDIAGONAL_HATCH : |
00bd8e72 SC |
581 | style = HatchStyleBackwardDiagonal; |
582 | break ; | |
fb040094 | 583 | case wxPENSTYLE_CROSSDIAG_HATCH : |
00bd8e72 SC |
584 | style = HatchStyleDiagonalCross; |
585 | break ; | |
fb040094 | 586 | case wxPENSTYLE_FDIAGONAL_HATCH : |
00bd8e72 SC |
587 | style = HatchStyleForwardDiagonal; |
588 | break ; | |
fb040094 | 589 | case wxPENSTYLE_CROSS_HATCH : |
00bd8e72 SC |
590 | style = HatchStyleCross; |
591 | break ; | |
fb040094 | 592 | case wxPENSTYLE_HORIZONTAL_HATCH : |
00bd8e72 SC |
593 | style = HatchStyleHorizontal; |
594 | break ; | |
fb040094 | 595 | case wxPENSTYLE_VERTICAL_HATCH : |
00bd8e72 SC |
596 | style = HatchStyleVertical; |
597 | break ; | |
598 | ||
599 | } | |
600 | m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() , | |
601 | pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent ); | |
602 | m_pen->SetBrush( m_penBrush ) | |
184fc6c8 SC |
603 | */ |
604 | } | |
605 | break; | |
606 | } | |
00bd8e72 | 607 | } |
184fc6c8 | 608 | |
87752530 | 609 | void wxCairoPenData::Apply( wxGraphicsContext* context ) |
00bd8e72 SC |
610 | { |
611 | cairo_t * ctext = (cairo_t*) context->GetNativeContext(); | |
612 | cairo_set_line_width(ctext,m_width); | |
613 | cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha); | |
614 | cairo_set_line_cap(ctext,m_cap); | |
615 | cairo_set_line_join(ctext,m_join); | |
616 | cairo_set_dash(ctext,(double*)m_lengths,m_count,0.0); | |
184fc6c8 SC |
617 | } |
618 | ||
00bd8e72 | 619 | //----------------------------------------------------------------------------- |
87752530 | 620 | // wxCairoBrushData implementation |
00bd8e72 SC |
621 | //----------------------------------------------------------------------------- |
622 | ||
7f8bd9fc RD |
623 | wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer ) |
624 | : wxGraphicsObjectRefData( renderer ) | |
00bd8e72 SC |
625 | { |
626 | Init(); | |
627 | } | |
184fc6c8 | 628 | |
87752530 | 629 | wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush ) |
7f8bd9fc | 630 | : wxGraphicsObjectRefData(renderer) |
00bd8e72 | 631 | { |
7f8bd9fc | 632 | Init(); |
49ad157e | 633 | |
00bd8e72 | 634 | m_red = brush.GetColour().Red()/255.0; |
49ad157e | 635 | m_green = brush.GetColour().Green()/255.0; |
00bd8e72 SC |
636 | m_blue = brush.GetColour().Blue()/255.0; |
637 | m_alpha = brush.GetColour().Alpha()/255.0; | |
638 | /* | |
fb040094 | 639 | if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID) |
00bd8e72 SC |
640 | { |
641 | m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() , | |
642 | brush.GetColour().Green() , brush.GetColour().Blue() ) ); | |
643 | } | |
644 | else if ( brush.IsHatch() ) | |
645 | { | |
646 | HatchStyle style = HatchStyleHorizontal; | |
647 | switch( brush.GetStyle() ) | |
648 | { | |
fb040094 | 649 | case wxBRUSHSTYLE_BDIAGONAL_HATCH : |
00bd8e72 SC |
650 | style = HatchStyleBackwardDiagonal; |
651 | break ; | |
fb040094 | 652 | case wxBRUSHSTYLE_CROSSDIAG_HATCH : |
00bd8e72 SC |
653 | style = HatchStyleDiagonalCross; |
654 | break ; | |
fb040094 | 655 | case wxBRUSHSTYLE_FDIAGONAL_HATCH : |
00bd8e72 SC |
656 | style = HatchStyleForwardDiagonal; |
657 | break ; | |
fb040094 | 658 | case wxBRUSHSTYLE_CROSS_HATCH : |
00bd8e72 SC |
659 | style = HatchStyleCross; |
660 | break ; | |
fb040094 | 661 | case wxBRUSHSTYLE_HORIZONTAL_HATCH : |
00bd8e72 SC |
662 | style = HatchStyleHorizontal; |
663 | break ; | |
fb040094 | 664 | case wxBRUSHSTYLE_VERTICAL_HATCH : |
00bd8e72 SC |
665 | style = HatchStyleVertical; |
666 | break ; | |
667 | ||
668 | } | |
669 | m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() , | |
670 | brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent ); | |
671 | } | |
49ad157e | 672 | else |
00bd8e72 SC |
673 | { |
674 | wxBitmap* bmp = brush.GetStipple(); | |
a1b806b9 | 675 | if ( bmp && bmp->IsOk() ) |
00bd8e72 SC |
676 | { |
677 | wxDELETE( m_brushImage ); | |
678 | m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE()); | |
679 | m_brush = new TextureBrush(m_brushImage); | |
680 | } | |
184fc6c8 | 681 | } |
00bd8e72 | 682 | */ |
184fc6c8 SC |
683 | } |
684 | ||
87752530 | 685 | wxCairoBrushData::~wxCairoBrushData () |
184fc6c8 | 686 | { |
00bd8e72 SC |
687 | if (m_brushPattern) |
688 | cairo_pattern_destroy(m_brushPattern); | |
184fc6c8 SC |
689 | } |
690 | ||
87752530 | 691 | void wxCairoBrushData::Apply( wxGraphicsContext* context ) |
184fc6c8 | 692 | { |
00bd8e72 SC |
693 | cairo_t * ctext = (cairo_t*) context->GetNativeContext(); |
694 | if ( m_brushPattern ) | |
695 | { | |
696 | cairo_set_source(ctext,m_brushPattern); | |
697 | } | |
698 | else | |
699 | { | |
700 | cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha); | |
701 | } | |
184fc6c8 SC |
702 | } |
703 | ||
4ee4c7b9 VZ |
704 | void wxCairoBrushData::AddGradientStops(const wxGraphicsGradientStops& stops) |
705 | { | |
706 | // loop over all the stops, they include the beginning and ending ones | |
707 | const unsigned numStops = stops.GetCount(); | |
708 | for ( unsigned n = 0; n < numStops; n++ ) | |
709 | { | |
710 | const wxGraphicsGradientStop stop = stops.Item(n); | |
711 | ||
712 | const wxColour col = stop.GetColour(); | |
713 | ||
714 | cairo_pattern_add_color_stop_rgba | |
715 | ( | |
716 | m_brushPattern, | |
717 | stop.GetPosition(), | |
718 | col.Red()/255.0, | |
719 | col.Green()/255.0, | |
720 | col.Blue()/255.0, | |
721 | col.Alpha()/255.0 | |
722 | ); | |
723 | } | |
724 | ||
725 | wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, | |
726 | wxT("Couldn't create cairo pattern")); | |
727 | } | |
728 | ||
729 | void | |
730 | wxCairoBrushData::CreateLinearGradientBrush(wxDouble x1, wxDouble y1, | |
731 | wxDouble x2, wxDouble y2, | |
732 | const wxGraphicsGradientStops& stops) | |
184fc6c8 | 733 | { |
00bd8e72 | 734 | m_brushPattern = cairo_pattern_create_linear(x1,y1,x2,y2); |
4ee4c7b9 VZ |
735 | |
736 | AddGradientStops(stops); | |
184fc6c8 SC |
737 | } |
738 | ||
4ee4c7b9 VZ |
739 | void |
740 | wxCairoBrushData::CreateRadialGradientBrush(wxDouble xo, wxDouble yo, | |
741 | wxDouble xc, wxDouble yc, | |
742 | wxDouble radius, | |
743 | const wxGraphicsGradientStops& stops) | |
184fc6c8 | 744 | { |
00bd8e72 | 745 | m_brushPattern = cairo_pattern_create_radial(xo,yo,0.0,xc,yc,radius); |
4ee4c7b9 VZ |
746 | |
747 | AddGradientStops(stops); | |
184fc6c8 SC |
748 | } |
749 | ||
87752530 | 750 | void wxCairoBrushData::Init() |
184fc6c8 | 751 | { |
00bd8e72 | 752 | m_brushPattern = NULL; |
184fc6c8 SC |
753 | } |
754 | ||
00bd8e72 | 755 | //----------------------------------------------------------------------------- |
87752530 | 756 | // wxCairoFontData implementation |
00bd8e72 SC |
757 | //----------------------------------------------------------------------------- |
758 | ||
49ad157e | 759 | wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font, |
87752530 | 760 | const wxColour& col ) : wxGraphicsObjectRefData(renderer) |
184fc6c8 | 761 | { |
00bd8e72 | 762 | m_red = col.Red()/255.0; |
49ad157e | 763 | m_green = col.Green()/255.0; |
00bd8e72 SC |
764 | m_blue = col.Blue()/255.0; |
765 | m_alpha = col.Alpha()/255.0; | |
00bd8e72 | 766 | m_size = font.GetPointSize(); |
0d19936d | 767 | m_underlined = font.GetUnderlined(); |
84f67b11 SC |
768 | |
769 | #ifdef __WXMAC__ | |
aa6208d9 | 770 | m_font = cairo_quartz_font_face_create_for_cgfont( font.OSXGetCGFont() ); |
84f67b11 SC |
771 | #elif defined(__WXGTK__) |
772 | m_font = pango_font_description_copy( font.GetNativeFontInfo()->description ); | |
773 | #else | |
00bd8e72 SC |
774 | m_fontName = font.GetFaceName().mb_str(wxConvUTF8); |
775 | m_slant = font.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC:CAIRO_FONT_SLANT_NORMAL; | |
776 | m_weight = font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD:CAIRO_FONT_WEIGHT_NORMAL; | |
e7f9f819 | 777 | #endif |
184fc6c8 SC |
778 | } |
779 | ||
87752530 | 780 | wxCairoFontData::~wxCairoFontData() |
184fc6c8 | 781 | { |
84f67b11 | 782 | #ifdef __WXMAC__ |
e7f9f819 | 783 | cairo_font_face_destroy( m_font ); |
84f67b11 SC |
784 | #elif defined(__WXGTK__) |
785 | pango_font_description_free( m_font ); | |
786 | #else | |
787 | #endif | |
00bd8e72 | 788 | } |
184fc6c8 | 789 | |
87752530 | 790 | void wxCairoFontData::Apply( wxGraphicsContext* context ) |
00bd8e72 SC |
791 | { |
792 | cairo_t * ctext = (cairo_t*) context->GetNativeContext(); | |
793 | cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha); | |
84f67b11 SC |
794 | #ifdef __WXGTK__ |
795 | // the rest is done using Pango layouts | |
796 | #elif defined(__WXMAC__) | |
797 | cairo_set_font_face(ctext, m_font); | |
3e700936 | 798 | cairo_set_font_size(ctext, m_size ); |
84f67b11 | 799 | #else |
711a4812 | 800 | cairo_select_font_face(ctext, m_fontName, m_slant, m_weight ); |
84f67b11 | 801 | cairo_set_font_size(ctext, m_size ); |
e7f9f819 | 802 | #endif |
00bd8e72 SC |
803 | } |
804 | ||
805 | //----------------------------------------------------------------------------- | |
0db8a70e | 806 | // wxCairoPathData implementation |
00bd8e72 SC |
807 | //----------------------------------------------------------------------------- |
808 | ||
0db8a70e RD |
809 | wxCairoPathData::wxCairoPathData( wxGraphicsRenderer* renderer, cairo_t* pathcontext) |
810 | : wxGraphicsPathData(renderer) | |
184fc6c8 | 811 | { |
00bd8e72 | 812 | if (pathcontext) |
184fc6c8 | 813 | { |
00bd8e72 SC |
814 | m_pathContext = pathcontext; |
815 | } | |
816 | else | |
817 | { | |
818 | cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1); | |
819 | m_pathContext = cairo_create(surface); | |
820 | cairo_surface_destroy (surface); | |
184fc6c8 | 821 | } |
00bd8e72 | 822 | } |
184fc6c8 | 823 | |
0db8a70e | 824 | wxCairoPathData::~wxCairoPathData() |
00bd8e72 SC |
825 | { |
826 | cairo_destroy(m_pathContext); | |
184fc6c8 SC |
827 | } |
828 | ||
0db8a70e | 829 | wxGraphicsObjectRefData *wxCairoPathData::Clone() const |
184fc6c8 | 830 | { |
00bd8e72 SC |
831 | cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1); |
832 | cairo_t* pathcontext = cairo_create(surface); | |
833 | cairo_surface_destroy (surface); | |
834 | ||
835 | cairo_path_t* path = cairo_copy_path(m_pathContext); | |
836 | cairo_append_path(pathcontext, path); | |
837 | cairo_path_destroy(path); | |
0db8a70e | 838 | return new wxCairoPathData( GetRenderer() ,pathcontext); |
00bd8e72 SC |
839 | } |
840 | ||
841 | ||
0db8a70e | 842 | void* wxCairoPathData::GetNativePath() const |
00bd8e72 SC |
843 | { |
844 | return cairo_copy_path(m_pathContext) ; | |
845 | } | |
846 | ||
0db8a70e | 847 | void wxCairoPathData::UnGetNativePath(void *p) const |
00bd8e72 SC |
848 | { |
849 | cairo_path_destroy((cairo_path_t*)p); | |
850 | } | |
851 | ||
852 | // | |
853 | // The Primitives | |
854 | // | |
855 | ||
0db8a70e | 856 | void wxCairoPathData::MoveToPoint( wxDouble x , wxDouble y ) |
00bd8e72 SC |
857 | { |
858 | cairo_move_to(m_pathContext,x,y); | |
859 | } | |
860 | ||
0db8a70e | 861 | void wxCairoPathData::AddLineToPoint( wxDouble x , wxDouble y ) |
00bd8e72 SC |
862 | { |
863 | cairo_line_to(m_pathContext,x,y); | |
864 | } | |
865 | ||
0db8a70e | 866 | void wxCairoPathData::AddPath( const wxGraphicsPathData* path ) |
6b06903d | 867 | { |
d9485f89 RD |
868 | cairo_path_t* p = (cairo_path_t*)path->GetNativePath(); |
869 | cairo_append_path(m_pathContext, p); | |
870 | UnGetNativePath(p); | |
6b06903d RD |
871 | } |
872 | ||
0db8a70e | 873 | void wxCairoPathData::CloseSubpath() |
00bd8e72 SC |
874 | { |
875 | cairo_close_path(m_pathContext); | |
876 | } | |
877 | ||
0db8a70e | 878 | void wxCairoPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y ) |
00bd8e72 SC |
879 | { |
880 | cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y); | |
881 | } | |
882 | ||
883 | // gets the last point of the current path, (0,0) if not yet set | |
0db8a70e | 884 | void wxCairoPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const |
00bd8e72 SC |
885 | { |
886 | double dx,dy; | |
887 | cairo_get_current_point(m_pathContext,&dx,&dy); | |
0db8a70e RD |
888 | if (x) |
889 | *x = dx; | |
890 | if (y) | |
891 | *y = dy; | |
00bd8e72 SC |
892 | } |
893 | ||
0db8a70e | 894 | void wxCairoPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise ) |
00bd8e72 | 895 | { |
49ad157e | 896 | // as clockwise means positive in our system (y pointing downwards) |
00bd8e72 SC |
897 | // TODO make this interpretation dependent of the |
898 | // real device trans | |
899 | if ( clockwise||(endAngle-startAngle)>=2*M_PI) | |
900 | cairo_arc(m_pathContext,x,y,r,startAngle,endAngle); | |
901 | else | |
902 | cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle); | |
903 | } | |
904 | ||
905 | // transforms each point of this path by the matrix | |
49ad157e | 906 | void wxCairoPathData::Transform( const wxGraphicsMatrixData* matrix ) |
00bd8e72 SC |
907 | { |
908 | // as we don't have a true path object, we have to apply the inverse | |
909 | // matrix to the context | |
910 | cairo_matrix_t m = *((cairo_matrix_t*) matrix->GetNativeMatrix()); | |
911 | cairo_matrix_invert( &m ); | |
912 | cairo_transform(m_pathContext,&m); | |
49ad157e | 913 | } |
00bd8e72 SC |
914 | |
915 | // gets the bounding box enclosing all points (possibly including control points) | |
0db8a70e | 916 | void wxCairoPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const |
00bd8e72 SC |
917 | { |
918 | double x1,y1,x2,y2; | |
919 | ||
920 | cairo_stroke_extents( m_pathContext, &x1, &y1, &x2, &y2 ); | |
921 | if ( x2 < x1 ) | |
184fc6c8 | 922 | { |
00bd8e72 SC |
923 | *x = x2; |
924 | *w = x1-x2; | |
925 | } | |
926 | else | |
927 | { | |
928 | *x = x1; | |
929 | *w = x2-x1; | |
184fc6c8 | 930 | } |
49ad157e | 931 | |
00bd8e72 SC |
932 | if( y2 < y1 ) |
933 | { | |
934 | *y = y2; | |
935 | *h = y1-y2; | |
936 | } | |
937 | else | |
938 | { | |
939 | *y = y1; | |
940 | *h = y2-y1; | |
941 | } | |
942 | } | |
943 | ||
f2b905d7 | 944 | bool wxCairoPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle ) const |
00bd8e72 | 945 | { |
f2b905d7 SC |
946 | cairo_set_fill_rule(m_pathContext,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING); |
947 | return cairo_in_fill( m_pathContext, x, y) != 0; | |
00bd8e72 SC |
948 | } |
949 | ||
950 | //----------------------------------------------------------------------------- | |
0db8a70e | 951 | // wxCairoMatrixData implementation |
00bd8e72 SC |
952 | //----------------------------------------------------------------------------- |
953 | ||
0db8a70e RD |
954 | wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix ) |
955 | : wxGraphicsMatrixData(renderer) | |
00bd8e72 SC |
956 | { |
957 | if ( matrix ) | |
958 | m_matrix = *matrix; | |
959 | } | |
960 | ||
49ad157e | 961 | wxCairoMatrixData::~wxCairoMatrixData() |
00bd8e72 SC |
962 | { |
963 | // nothing to do | |
964 | } | |
965 | ||
49ad157e | 966 | wxGraphicsObjectRefData *wxCairoMatrixData::Clone() const |
00bd8e72 | 967 | { |
0db8a70e | 968 | return new wxCairoMatrixData(GetRenderer(),&m_matrix); |
00bd8e72 SC |
969 | } |
970 | ||
971 | // concatenates the matrix | |
49ad157e | 972 | void wxCairoMatrixData::Concat( const wxGraphicsMatrixData *t ) |
00bd8e72 | 973 | { |
49ad157e | 974 | cairo_matrix_multiply( &m_matrix, &m_matrix, (cairo_matrix_t*) t->GetNativeMatrix()); |
00bd8e72 SC |
975 | } |
976 | ||
00bd8e72 | 977 | // sets the matrix to the respective values |
49ad157e VZ |
978 | void wxCairoMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d, |
979 | wxDouble tx, wxDouble ty) | |
00bd8e72 SC |
980 | { |
981 | cairo_matrix_init( &m_matrix, a, b, c, d, tx, ty); | |
982 | } | |
983 | ||
248802d0 RD |
984 | // gets the component valuess of the matrix |
985 | void wxCairoMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c, | |
986 | wxDouble* d, wxDouble* tx, wxDouble* ty) const | |
987 | { | |
988 | if (a) *a = m_matrix.xx; | |
989 | if (b) *b = m_matrix.yx; | |
990 | if (c) *c = m_matrix.xy; | |
991 | if (d) *d = m_matrix.yy; | |
992 | if (tx) *tx= m_matrix.x0; | |
993 | if (ty) *ty= m_matrix.y0; | |
994 | } | |
995 | ||
00bd8e72 | 996 | // makes this the inverse matrix |
49ad157e | 997 | void wxCairoMatrixData::Invert() |
00bd8e72 SC |
998 | { |
999 | cairo_matrix_invert( &m_matrix ); | |
1000 | } | |
1001 | ||
1002 | // returns true if the elements of the transformation matrix are equal ? | |
49ad157e | 1003 | bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData* t) const |
00bd8e72 SC |
1004 | { |
1005 | const cairo_matrix_t* tm = (cairo_matrix_t*) t->GetNativeMatrix(); | |
49ad157e VZ |
1006 | return ( |
1007 | m_matrix.xx == tm->xx && | |
1008 | m_matrix.yx == tm->yx && | |
1009 | m_matrix.xy == tm->xy && | |
1010 | m_matrix.yy == tm->yy && | |
1011 | m_matrix.x0 == tm->x0 && | |
00bd8e72 SC |
1012 | m_matrix.y0 == tm->y0 ) ; |
1013 | } | |
1014 | ||
1015 | // return true if this is the identity matrix | |
0db8a70e | 1016 | bool wxCairoMatrixData::IsIdentity() const |
00bd8e72 SC |
1017 | { |
1018 | return ( m_matrix.xx == 1 && m_matrix.yy == 1 && | |
1019 | m_matrix.yx == 0 && m_matrix.xy == 0 && m_matrix.x0 == 0 && m_matrix.y0 == 0); | |
1020 | } | |
1021 | ||
1022 | // | |
1023 | // transformation | |
1024 | // | |
1025 | ||
1026 | // add the translation to this matrix | |
0db8a70e | 1027 | void wxCairoMatrixData::Translate( wxDouble dx , wxDouble dy ) |
00bd8e72 SC |
1028 | { |
1029 | cairo_matrix_translate( &m_matrix, dx, dy) ; | |
1030 | } | |
1031 | ||
1032 | // add the scale to this matrix | |
0db8a70e | 1033 | void wxCairoMatrixData::Scale( wxDouble xScale , wxDouble yScale ) |
00bd8e72 SC |
1034 | { |
1035 | cairo_matrix_scale( &m_matrix, xScale, yScale) ; | |
1036 | } | |
1037 | ||
1038 | // add the rotation to this matrix (radians) | |
49ad157e | 1039 | void wxCairoMatrixData::Rotate( wxDouble angle ) |
00bd8e72 SC |
1040 | { |
1041 | cairo_matrix_rotate( &m_matrix, angle) ; | |
49ad157e | 1042 | } |
00bd8e72 SC |
1043 | |
1044 | // | |
1045 | // apply the transforms | |
1046 | // | |
1047 | ||
1048 | // applies that matrix to the point | |
0db8a70e | 1049 | void wxCairoMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const |
00bd8e72 SC |
1050 | { |
1051 | double lx = *x, ly = *y ; | |
1052 | cairo_matrix_transform_point( &m_matrix, &lx, &ly); | |
1053 | *x = lx; | |
1054 | *y = ly; | |
1055 | } | |
1056 | ||
1057 | // applies the matrix except for translations | |
0db8a70e | 1058 | void wxCairoMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const |
00bd8e72 SC |
1059 | { |
1060 | double lx = *dx, ly = *dy ; | |
1061 | cairo_matrix_transform_distance( &m_matrix, &lx, &ly); | |
1062 | *dx = lx; | |
1063 | *dy = ly; | |
1064 | } | |
1065 | ||
1066 | // returns the native representation | |
0db8a70e | 1067 | void * wxCairoMatrixData::GetNativeMatrix() const |
00bd8e72 SC |
1068 | { |
1069 | return (void*) &m_matrix; | |
1070 | } | |
1071 | ||
2fc9c1ea KO |
1072 | // wxCairoBitmap implementation |
1073 | //----------------------------------------------------------------------------- | |
1074 | ||
2986eb86 SC |
1075 | wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer* renderer, cairo_surface_t* bitmap ) : |
1076 | wxGraphicsObjectRefData( renderer ) | |
1077 | { | |
1078 | m_surface = bitmap; | |
1079 | m_pattern = cairo_pattern_create_for_surface(m_surface); | |
1080 | } | |
1081 | ||
2fc9c1ea KO |
1082 | wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer* renderer, const wxBitmap& bmp ) : wxGraphicsObjectRefData( renderer ) |
1083 | { | |
1084 | wxCHECK_RET( bmp.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap")); | |
1085 | ||
711a4812 | 1086 | #ifdef wxHAS_RAW_BITMAP |
125e7c11 KO |
1087 | int bw = m_width = bmp.GetWidth(); |
1088 | int bh = m_height = bmp.GetHeight(); | |
2fc9c1ea KO |
1089 | wxBitmap bmpSource = bmp; // we need a non-const instance |
1090 | m_buffer = new unsigned char[bw*bh*4]; | |
3c10f6db | 1091 | wxUint32* data = (wxUint32*)m_buffer; |
2fc9c1ea KO |
1092 | |
1093 | // Create a surface object and copy the bitmap pixel data to it. if the | |
1094 | // image has alpha (or a mask represented as alpha) then we'll use a | |
1095 | // different format and iterator than if it doesn't... | |
932d0768 RD |
1096 | if (bmpSource.HasAlpha() || (bmpSource.GetMask() |
1097 | #ifdef __WXMSW__ | |
1098 | // this check is needed under wxMSW, but adding this condition to wxGTK | |
1099 | // causes an assert when getting alpha pixel data, not sure about Mac. | |
1100 | && bmpSource.GetDepth() == 32 | |
1101 | #endif | |
1102 | )) | |
2fc9c1ea KO |
1103 | { |
1104 | m_surface = cairo_image_surface_create_for_data( | |
1105 | m_buffer, CAIRO_FORMAT_ARGB32, bw, bh, bw*4); | |
1106 | wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh)); | |
1107 | wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data.")); | |
1108 | ||
1109 | wxAlphaPixelData::Iterator p(pixData); | |
1110 | for (int y=0; y<bh; y++) | |
1111 | { | |
1112 | wxAlphaPixelData::Iterator rowStart = p; | |
1113 | for (int x=0; x<bw; x++) | |
1114 | { | |
1115 | // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity, | |
1116 | // with alpha in the upper 8 bits, then red, then green, then | |
1117 | // blue. The 32-bit quantities are stored native-endian. | |
1118 | // Pre-multiplied alpha is used. | |
1119 | unsigned char alpha = p.Alpha(); | |
1120 | if (alpha == 0) | |
1121 | *data = 0; | |
1122 | else | |
1123 | *data = ( alpha << 24 | |
1124 | | (p.Red() * alpha/255) << 16 | |
1125 | | (p.Green() * alpha/255) << 8 | |
1126 | | (p.Blue() * alpha/255) ); | |
1127 | ++data; | |
1128 | ++p; | |
1129 | } | |
1130 | p = rowStart; | |
1131 | p.OffsetY(pixData, 1); | |
1132 | } | |
1133 | } | |
1134 | else // no alpha | |
1135 | { | |
1136 | m_surface = cairo_image_surface_create_for_data( | |
1137 | m_buffer, CAIRO_FORMAT_RGB24, bw, bh, bw*4); | |
1138 | wxNativePixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh)); | |
1139 | wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data.")); | |
1140 | ||
1141 | wxNativePixelData::Iterator p(pixData); | |
1142 | for (int y=0; y<bh; y++) | |
1143 | { | |
1144 | wxNativePixelData::Iterator rowStart = p; | |
1145 | for (int x=0; x<bw; x++) | |
1146 | { | |
1147 | // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with | |
1148 | // the upper 8 bits unused. Red, Green, and Blue are stored in | |
1149 | // the remaining 24 bits in that order. The 32-bit quantities | |
1150 | // are stored native-endian. | |
1151 | *data = ( p.Red() << 16 | p.Green() << 8 | p.Blue() ); | |
1152 | ++data; | |
1153 | ++p; | |
1154 | } | |
1155 | p = rowStart; | |
1156 | p.OffsetY(pixData, 1); | |
1157 | } | |
1158 | } | |
66f917a7 | 1159 | m_pattern = cairo_pattern_create_for_surface(m_surface); |
711a4812 | 1160 | #endif // wxHAS_RAW_BITMAP |
2fc9c1ea KO |
1161 | } |
1162 | ||
1163 | wxCairoBitmapData::~wxCairoBitmapData() | |
1164 | { | |
66f917a7 KO |
1165 | if (m_pattern) |
1166 | cairo_pattern_destroy(m_pattern); | |
4ee4c7b9 | 1167 | |
66f917a7 KO |
1168 | if (m_surface) |
1169 | cairo_surface_destroy(m_surface); | |
4ee4c7b9 | 1170 | |
2fc9c1ea KO |
1171 | delete [] m_buffer; |
1172 | } | |
1173 | ||
1174 | ||
1175 | ||
00bd8e72 SC |
1176 | //----------------------------------------------------------------------------- |
1177 | // wxCairoContext implementation | |
1178 | //----------------------------------------------------------------------------- | |
1179 | ||
e7f9f819 SC |
1180 | class wxCairoOffsetHelper |
1181 | { | |
1182 | public : | |
1183 | wxCairoOffsetHelper( cairo_t* ctx , bool offset ) | |
1184 | { | |
1185 | m_ctx = ctx; | |
1186 | m_offset = offset; | |
1187 | if ( m_offset ) | |
1188 | cairo_translate( m_ctx, 0.5, 0.5 ); | |
1189 | } | |
1190 | ~wxCairoOffsetHelper( ) | |
1191 | { | |
1192 | if ( m_offset ) | |
1193 | cairo_translate( m_ctx, -0.5, -0.5 ); | |
1194 | } | |
1195 | public : | |
1196 | cairo_t* m_ctx; | |
1197 | bool m_offset; | |
1198 | } ; | |
1199 | ||
c9008abe RR |
1200 | wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc ) |
1201 | : wxGraphicsContext(renderer) | |
1202 | { | |
932d0768 RD |
1203 | #ifdef __WXMSW__ |
1204 | // wxMSW contexts always use MM_ANISOTROPIC, which messes up | |
1205 | // text rendering when printing using Cairo. Switch it to MM_TEXT | |
1206 | // map mode to avoid this problem. | |
1207 | HDC hdc = (HDC)dc.GetHDC(); | |
1208 | ::SetMapMode(hdc, MM_TEXT); | |
1209 | m_mswSurface = cairo_win32_printing_surface_create(hdc); | |
1210 | Init( cairo_create(m_mswSurface) ); | |
1211 | #endif | |
1212 | ||
c9008abe RR |
1213 | #ifdef __WXGTK20__ |
1214 | const wxDCImpl *impl = dc.GetImpl(); | |
1215 | Init( (cairo_t*) impl->GetCairoContext() ); | |
932d0768 | 1216 | #endif |
b129eaa3 VZ |
1217 | wxSize sz = dc.GetSize(); |
1218 | m_width = sz.x; | |
1219 | m_height = sz.y; | |
1220 | ||
c9008abe | 1221 | wxPoint org = dc.GetDeviceOrigin(); |
eaeb9985 | 1222 | cairo_translate( m_context, org.x, org.y ); |
49ad157e | 1223 | |
c9008abe RR |
1224 | double sx,sy; |
1225 | dc.GetUserScale( &sx, &sy ); | |
932d0768 RD |
1226 | |
1227 | // TODO: Determine if these fixes are needed on other platforms too. | |
1228 | // On MSW, without this the printer context will not respect wxDC SetMapMode calls. | |
1229 | // For example, using dc.SetMapMode(wxMM_POINTS) can let us share printer and screen | |
1230 | // drawing code | |
1231 | #ifdef __WXMSW__ | |
1232 | double lsx,lsy; | |
1233 | dc.GetLogicalScale( &lsx, &lsy ); | |
1234 | sx *= lsx; | |
1235 | sy *= lsy; | |
1236 | #endif | |
eaeb9985 | 1237 | cairo_scale( m_context, sx, sy ); |
c9008abe | 1238 | |
eaeb9985 RR |
1239 | org = dc.GetLogicalOrigin(); |
1240 | cairo_translate( m_context, -org.x, -org.y ); | |
c9008abe RR |
1241 | } |
1242 | ||
00bd8e72 SC |
1243 | wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc ) |
1244 | : wxGraphicsContext(renderer) | |
1245 | { | |
b129eaa3 VZ |
1246 | int width, height; |
1247 | dc.GetSize( &width, &height ); | |
1248 | m_width = width; | |
1249 | m_height = height; | |
1250 | ||
136a1de9 SC |
1251 | m_enableOffset = true; |
1252 | ||
932d0768 RD |
1253 | #ifdef __WXMSW__ |
1254 | m_mswSurface = cairo_win32_surface_create((HDC)dc.GetHDC()); | |
1255 | Init( cairo_create(m_mswSurface) ); | |
1256 | #endif | |
1257 | ||
c9008abe | 1258 | #ifdef __WXGTK20__ |
888dde65 | 1259 | wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl(); |
64a226f7 | 1260 | Init( gdk_cairo_create( impldc->GetGDKWindow() ) ); |
49ad157e VZ |
1261 | |
1262 | #if 0 | |
c9008abe | 1263 | wxGraphicsMatrix matrix = CreateMatrix(); |
49ad157e | 1264 | |
c9008abe RR |
1265 | wxPoint org = dc.GetDeviceOrigin(); |
1266 | matrix.Translate( org.x, org.y ); | |
49ad157e | 1267 | |
c9008abe RR |
1268 | org = dc.GetLogicalOrigin(); |
1269 | matrix.Translate( -org.x, -org.y ); | |
1270 | ||
1271 | double sx,sy; | |
1272 | dc.GetUserScale( &sx, &sy ); | |
1273 | matrix.Scale( sx, sy ); | |
1274 | ||
1275 | ConcatTransform( matrix ); | |
1276 | #endif | |
e7f9f819 | 1277 | #endif |
c9008abe | 1278 | |
888dde65 | 1279 | #ifdef __WXMAC__ |
888dde65 RR |
1280 | CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef(); |
1281 | cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height); | |
1282 | Init( cairo_create( surface ) ); | |
1283 | cairo_surface_destroy( surface ); | |
1284 | #endif | |
1285 | } | |
1286 | ||
1287 | wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& dc ) | |
1288 | : wxGraphicsContext(renderer) | |
1289 | { | |
b129eaa3 VZ |
1290 | int width, height; |
1291 | dc.GetSize( &width, &height ); | |
1292 | m_width = width; | |
1293 | m_height = height; | |
1294 | ||
136a1de9 | 1295 | m_enableOffset = true; |
932d0768 RD |
1296 | |
1297 | #ifdef __WXMSW__ | |
1298 | m_mswSurface = cairo_win32_surface_create((HDC)dc.GetHDC()); | |
1299 | Init( cairo_create(m_mswSurface) ); | |
1300 | #endif | |
136a1de9 | 1301 | |
c9008abe | 1302 | #ifdef __WXGTK20__ |
888dde65 RR |
1303 | wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl(); |
1304 | Init( gdk_cairo_create( impldc->GetGDKWindow() ) ); | |
49ad157e VZ |
1305 | |
1306 | #if 0 | |
c9008abe | 1307 | wxGraphicsMatrix matrix = CreateMatrix(); |
49ad157e | 1308 | |
c9008abe RR |
1309 | wxPoint org = dc.GetDeviceOrigin(); |
1310 | matrix.Translate( org.x, org.y ); | |
49ad157e | 1311 | |
c9008abe RR |
1312 | org = dc.GetLogicalOrigin(); |
1313 | matrix.Translate( -org.x, -org.y ); | |
1314 | ||
1315 | double sx,sy; | |
1316 | dc.GetUserScale( &sx, &sy ); | |
1317 | matrix.Scale( sx, sy ); | |
1318 | ||
1319 | ConcatTransform( matrix ); | |
64a226f7 | 1320 | #endif |
c9008abe RR |
1321 | #endif |
1322 | ||
e7f9f819 | 1323 | #ifdef __WXMAC__ |
e7f9f819 SC |
1324 | CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef(); |
1325 | cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height); | |
1326 | Init( cairo_create( surface ) ); | |
1327 | cairo_surface_destroy( surface ); | |
00bd8e72 | 1328 | #endif |
00bd8e72 SC |
1329 | } |
1330 | ||
c9008abe | 1331 | #ifdef __WXGTK20__ |
00bd8e72 SC |
1332 | wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable ) |
1333 | : wxGraphicsContext(renderer) | |
1334 | { | |
e7f9f819 | 1335 | Init( gdk_cairo_create( drawable ) ); |
b129eaa3 VZ |
1336 | |
1337 | int width, height; | |
1338 | gdk_drawable_get_size( drawable, &width, &height ); | |
1339 | m_width = width; | |
1340 | m_height = height; | |
00bd8e72 SC |
1341 | } |
1342 | #endif | |
1343 | ||
c0e69d72 KO |
1344 | #ifdef __WXMSW__ |
1345 | wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, HDC handle ) | |
1346 | : wxGraphicsContext(renderer) | |
1347 | { | |
1348 | m_mswSurface = cairo_win32_surface_create(handle); | |
b129eaa3 VZ |
1349 | Init( cairo_create(m_mswSurface) ); |
1350 | m_width = | |
1351 | m_height = 0; | |
c0e69d72 KO |
1352 | } |
1353 | #endif | |
1354 | ||
1355 | ||
00bd8e72 SC |
1356 | wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context ) |
1357 | : wxGraphicsContext(renderer) | |
1358 | { | |
e7f9f819 | 1359 | Init( context ); |
b129eaa3 VZ |
1360 | m_width = |
1361 | m_height = 0; | |
184fc6c8 SC |
1362 | } |
1363 | ||
00bd8e72 SC |
1364 | wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window) |
1365 | : wxGraphicsContext(renderer) | |
184fc6c8 | 1366 | { |
136a1de9 | 1367 | m_enableOffset = true; |
00bd8e72 SC |
1368 | #ifdef __WXGTK__ |
1369 | // something along these lines (copied from dcclient) | |
1370 | ||
00bd8e72 SC |
1371 | // Some controls don't have m_wxwindow - like wxStaticBox, but the user |
1372 | // code should still be able to create wxClientDCs for them, so we will | |
1373 | // use the parent window here then. | |
82a234fb | 1374 | if (window->m_wxwindow == NULL) |
184fc6c8 | 1375 | { |
00bd8e72 | 1376 | window = window->GetParent(); |
184fc6c8 | 1377 | } |
00bd8e72 | 1378 | |
82a234fb | 1379 | wxASSERT_MSG( window->m_wxwindow, wxT("wxCairoContext needs a widget") ); |
00bd8e72 | 1380 | |
82a234fb | 1381 | Init(gdk_cairo_create(window->GTKGetDrawingWindow())); |
b129eaa3 VZ |
1382 | |
1383 | wxSize sz = window->GetSize(); | |
1384 | m_width = sz.x; | |
1385 | m_height = sz.y; | |
00bd8e72 | 1386 | #endif |
9f2b6b31 RD |
1387 | |
1388 | #ifdef __WXMSW__ | |
1389 | m_mswSurface = cairo_win32_surface_create((HDC)window->GetHandle()); | |
1390 | Init(cairo_create(m_mswSurface)); | |
1391 | #endif | |
1392 | ||
00bd8e72 SC |
1393 | } |
1394 | ||
1395 | wxCairoContext::~wxCairoContext() | |
1396 | { | |
00bd8e72 SC |
1397 | if ( m_context ) |
1398 | { | |
1399 | PopState(); | |
1400 | PopState(); | |
1401 | cairo_destroy(m_context); | |
1402 | } | |
c0e69d72 KO |
1403 | #ifdef __WXMSW__ |
1404 | if ( m_mswSurface ) | |
1405 | cairo_surface_destroy(m_mswSurface); | |
1406 | #endif | |
00bd8e72 SC |
1407 | } |
1408 | ||
e7f9f819 SC |
1409 | void wxCairoContext::Init(cairo_t *context) |
1410 | { | |
1411 | m_context = context ; | |
1412 | PushState(); | |
1413 | PushState(); | |
1414 | } | |
1415 | ||
00bd8e72 | 1416 | |
d9485f89 | 1417 | void wxCairoContext::Clip( const wxRegion& region ) |
00bd8e72 | 1418 | { |
d9485f89 RD |
1419 | // Create a path with all the rectangles in the region |
1420 | wxGraphicsPath path = GetRenderer()->CreatePath(); | |
1421 | wxRegionIterator ri(region); | |
1422 | while (ri) | |
1423 | { | |
1424 | path.AddRectangle(ri.GetX(), ri.GetY(), ri.GetW(), ri.GetH()); | |
82a234fb | 1425 | ++ri; |
d9485f89 | 1426 | } |
49ad157e | 1427 | |
d9485f89 RD |
1428 | // Put it in the context |
1429 | cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ; | |
1430 | cairo_append_path(m_context, cp); | |
1431 | ||
1432 | // clip to that path | |
1433 | cairo_clip(m_context); | |
49ad157e | 1434 | path.UnGetNativePath(cp); |
00bd8e72 SC |
1435 | } |
1436 | ||
1437 | void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
1438 | { | |
d9485f89 RD |
1439 | // Create a path with this rectangle |
1440 | wxGraphicsPath path = GetRenderer()->CreatePath(); | |
1441 | path.AddRectangle(x,y,w,h); | |
1442 | ||
1443 | // Put it in the context | |
1444 | cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ; | |
1445 | cairo_append_path(m_context, cp); | |
1446 | ||
1447 | // clip to that path | |
71696da0 | 1448 | cairo_clip(m_context); |
49ad157e | 1449 | path.UnGetNativePath(cp); |
00bd8e72 SC |
1450 | } |
1451 | ||
1452 | void wxCairoContext::ResetClip() | |
1453 | { | |
d9485f89 | 1454 | cairo_reset_clip(m_context); |
00bd8e72 SC |
1455 | } |
1456 | ||
1457 | ||
0db8a70e | 1458 | void wxCairoContext::StrokePath( const wxGraphicsPath& path ) |
00bd8e72 | 1459 | { |
87752530 | 1460 | if ( !m_pen.IsNull() ) |
49ad157e VZ |
1461 | { |
1462 | wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ; | |
0db8a70e | 1463 | cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ; |
00bd8e72 | 1464 | cairo_append_path(m_context,cp); |
87752530 | 1465 | ((wxCairoPenData*)m_pen.GetRefData())->Apply(this); |
00bd8e72 | 1466 | cairo_stroke(m_context); |
0db8a70e | 1467 | path.UnGetNativePath(cp); |
00bd8e72 SC |
1468 | } |
1469 | } | |
1470 | ||
94a007ec | 1471 | void wxCairoContext::FillPath( const wxGraphicsPath& path , wxPolygonFillMode fillStyle ) |
00bd8e72 | 1472 | { |
87752530 | 1473 | if ( !m_brush.IsNull() ) |
00bd8e72 | 1474 | { |
e7f9f819 | 1475 | wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ; |
0db8a70e | 1476 | cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ; |
00bd8e72 | 1477 | cairo_append_path(m_context,cp); |
87752530 | 1478 | ((wxCairoBrushData*)m_brush.GetRefData())->Apply(this); |
00bd8e72 SC |
1479 | cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING); |
1480 | cairo_fill(m_context); | |
0db8a70e | 1481 | path.UnGetNativePath(cp); |
00bd8e72 SC |
1482 | } |
1483 | } | |
1484 | ||
1485 | void wxCairoContext::Rotate( wxDouble angle ) | |
1486 | { | |
1487 | cairo_rotate(m_context,angle); | |
1488 | } | |
1489 | ||
1490 | void wxCairoContext::Translate( wxDouble dx , wxDouble dy ) | |
1491 | { | |
1492 | cairo_translate(m_context,dx,dy); | |
1493 | } | |
1494 | ||
1495 | void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale ) | |
1496 | { | |
1497 | cairo_scale(m_context,xScale,yScale); | |
1498 | } | |
1499 | ||
1500 | // concatenates this transform with the current transform of this context | |
0db8a70e | 1501 | void wxCairoContext::ConcatTransform( const wxGraphicsMatrix& matrix ) |
00bd8e72 | 1502 | { |
0db8a70e | 1503 | cairo_transform(m_context,(const cairo_matrix_t *) matrix.GetNativeMatrix()); |
00bd8e72 SC |
1504 | } |
1505 | ||
1506 | // sets the transform of this context | |
0db8a70e | 1507 | void wxCairoContext::SetTransform( const wxGraphicsMatrix& matrix ) |
00bd8e72 | 1508 | { |
0db8a70e | 1509 | cairo_set_matrix(m_context,(const cairo_matrix_t*) matrix.GetNativeMatrix()); |
00bd8e72 SC |
1510 | } |
1511 | ||
1512 | // gets the matrix of this context | |
0db8a70e | 1513 | wxGraphicsMatrix wxCairoContext::GetTransform() const |
00bd8e72 | 1514 | { |
0db8a70e RD |
1515 | wxGraphicsMatrix matrix = CreateMatrix(); |
1516 | cairo_get_matrix(m_context,(cairo_matrix_t*) matrix.GetNativeMatrix()); | |
1517 | return matrix; | |
00bd8e72 SC |
1518 | } |
1519 | ||
1520 | ||
1521 | ||
1522 | void wxCairoContext::PushState() | |
1523 | { | |
1524 | cairo_save(m_context); | |
1525 | } | |
1526 | ||
1527 | void wxCairoContext::PopState() | |
1528 | { | |
1529 | cairo_restore(m_context); | |
1530 | } | |
184fc6c8 SC |
1531 | |
1532 | void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
1533 | { | |
2fc9c1ea | 1534 | wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp); |
3c10f6db | 1535 | DrawBitmap(bitmap, x, y, w, h); |
d9485f89 | 1536 | |
2fc9c1ea | 1537 | } |
49ad157e | 1538 | |
2fc9c1ea KO |
1539 | void wxCairoContext::DrawBitmap(const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) |
1540 | { | |
d9485f89 | 1541 | PushState(); |
49ad157e | 1542 | |
d9485f89 RD |
1543 | // In case we're scaling the image by using a width and height different |
1544 | // than the bitmap's size create a pattern transformation on the surface and | |
1545 | // draw the transformed pattern. | |
2fc9c1ea KO |
1546 | wxCairoBitmapData* data = static_cast<wxCairoBitmapData*>(bmp.GetRefData()); |
1547 | cairo_pattern_t* pattern = data->GetCairoPattern(); | |
1548 | wxSize size = data->GetSize(); | |
1549 | ||
1550 | wxDouble scaleX = w / size.GetWidth(); | |
1551 | wxDouble scaleY = h / size.GetHeight(); | |
d9485f89 RD |
1552 | |
1553 | // prepare to draw the image | |
1554 | cairo_translate(m_context, x, y); | |
ce6b1014 | 1555 | cairo_scale(m_context, scaleX, scaleY); |
d9485f89 RD |
1556 | cairo_set_source(m_context, pattern); |
1557 | // use the original size here since the context is scaled already... | |
2fc9c1ea | 1558 | cairo_rectangle(m_context, 0, 0, size.GetWidth(), size.GetHeight()); |
d9485f89 RD |
1559 | // fill the rectangle using the pattern |
1560 | cairo_fill(m_context); | |
1561 | ||
d9485f89 | 1562 | PopState(); |
184fc6c8 SC |
1563 | } |
1564 | ||
1565 | void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
1566 | { | |
d9485f89 RD |
1567 | // An icon is a bitmap on wxGTK, so do this the easy way. When we want to |
1568 | // start using the Cairo backend on other platforms then we may need to | |
1569 | // fiddle with this... | |
1570 | DrawBitmap(icon, x, y, w, h); | |
184fc6c8 SC |
1571 | } |
1572 | ||
1573 | ||
0b7dce54 | 1574 | void wxCairoContext::DoDrawText(const wxString& str, wxDouble x, wxDouble y) |
184fc6c8 | 1575 | { |
0b7dce54 VZ |
1576 | wxCHECK_RET( !m_font.IsNull(), |
1577 | wxT("wxCairoContext::DrawText - no valid font set") ); | |
1011fbeb SC |
1578 | |
1579 | if ( str.empty()) | |
d9485f89 | 1580 | return; |
84f67b11 | 1581 | |
10c508ef | 1582 | const wxCharBuffer data = str.utf8_str(); |
e7f9f819 SC |
1583 | if ( !data ) |
1584 | return; | |
0b7dce54 | 1585 | |
84f67b11 SC |
1586 | ((wxCairoFontData*)m_font.GetRefData())->Apply(this); |
1587 | ||
0b7dce54 VZ |
1588 | #ifdef __WXGTK__ |
1589 | size_t datalen = strlen(data); | |
1590 | ||
e7f9f819 | 1591 | PangoLayout *layout = pango_cairo_create_layout (m_context); |
0d19936d FM |
1592 | wxCairoFontData* font_data = (wxCairoFontData*) m_font.GetRefData(); |
1593 | pango_layout_set_font_description( layout, font_data->GetFont()); | |
84f67b11 | 1594 | pango_layout_set_text(layout, data, datalen); |
03647350 VZ |
1595 | |
1596 | if (font_data->GetUnderlined()) | |
0d19936d FM |
1597 | { |
1598 | PangoAttrList *attrs = pango_attr_list_new(); | |
1599 | PangoAttribute *attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE); | |
1600 | pango_attr_list_insert(attrs, attr); | |
1601 | pango_layout_set_attributes(layout, attrs); | |
1602 | pango_attr_list_unref(attrs); | |
1603 | } | |
1604 | ||
e7f9f819 SC |
1605 | cairo_move_to(m_context, x, y); |
1606 | pango_cairo_show_layout (m_context, layout); | |
1607 | ||
1608 | g_object_unref (layout); | |
1609 | #else | |
d9485f89 RD |
1610 | // Cairo's x,y for drawing text is at the baseline, so we need to adjust |
1611 | // the position we move to by the ascent. | |
1612 | cairo_font_extents_t fe; | |
1613 | cairo_font_extents(m_context, &fe); | |
1614 | cairo_move_to(m_context, x, y+fe.ascent); | |
49ad157e | 1615 | |
0b7dce54 | 1616 | cairo_show_text(m_context, data); |
e7f9f819 | 1617 | #endif |
184fc6c8 SC |
1618 | } |
1619 | ||
1620 | void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height, | |
1621 | wxDouble *descent, wxDouble *externalLeading ) const | |
1622 | { | |
1011fbeb SC |
1623 | wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") ); |
1624 | ||
e7f9f819 SC |
1625 | if ( width ) |
1626 | *width = 0; | |
1627 | if ( height ) | |
1628 | *height = 0; | |
1629 | if ( descent ) | |
1630 | *descent = 0; | |
1631 | if ( externalLeading ) | |
1632 | *externalLeading = 0; | |
1633 | ||
1011fbeb | 1634 | if ( str.empty()) |
d9485f89 RD |
1635 | return; |
1636 | ||
e7f9f819 SC |
1637 | #ifdef __WXGTK__ |
1638 | int w, h; | |
49ad157e | 1639 | |
e7f9f819 | 1640 | PangoLayout *layout = pango_cairo_create_layout (m_context); |
84f67b11 | 1641 | pango_layout_set_font_description( layout, ((wxCairoFontData*)m_font.GetRefData())->GetFont()); |
10c508ef | 1642 | const wxCharBuffer data = str.utf8_str(); |
84f67b11 | 1643 | if ( !data ) |
e7f9f819 SC |
1644 | { |
1645 | return; | |
1646 | } | |
84f67b11 | 1647 | pango_layout_set_text( layout, data, strlen(data) ); |
e7f9f819 SC |
1648 | pango_layout_get_pixel_size (layout, &w, &h); |
1649 | if ( width ) | |
1650 | *width = w; | |
1651 | if ( height ) | |
1652 | *height = h; | |
1653 | if (descent) | |
1654 | { | |
1655 | PangoLayoutIter *iter = pango_layout_get_iter(layout); | |
1656 | int baseline = pango_layout_iter_get_baseline(iter); | |
1657 | pango_layout_iter_free(iter); | |
1658 | *descent = h - PANGO_PIXELS(baseline); | |
1659 | } | |
1660 | g_object_unref (layout); | |
1661 | #else | |
d9485f89 RD |
1662 | ((wxCairoFontData*)m_font.GetRefData())->Apply((wxCairoContext*)this); |
1663 | ||
1664 | if (width) | |
1665 | { | |
1666 | const wxWX2MBbuf buf(str.mb_str(wxConvUTF8)); | |
1667 | cairo_text_extents_t te; | |
1668 | cairo_text_extents(m_context, buf, &te); | |
1669 | *width = te.width; | |
1670 | } | |
1671 | ||
1672 | if (height || descent || externalLeading) | |
1673 | { | |
1674 | cairo_font_extents_t fe; | |
1675 | cairo_font_extents(m_context, &fe); | |
49ad157e | 1676 | |
3e700936 | 1677 | // some backends have negative descents |
49ad157e | 1678 | |
3e700936 SC |
1679 | if ( fe.descent < 0 ) |
1680 | fe.descent = -fe.descent; | |
49ad157e | 1681 | |
3e700936 SC |
1682 | if ( fe.height < (fe.ascent + fe.descent ) ) |
1683 | { | |
1684 | // some backends are broken re height ... (eg currently ATSUI) | |
1685 | fe.height = fe.ascent + fe.descent; | |
1686 | } | |
49ad157e | 1687 | |
d9485f89 RD |
1688 | if (height) |
1689 | *height = fe.height; | |
1690 | if ( descent ) | |
1691 | *descent = fe.descent; | |
1692 | if ( externalLeading ) | |
1693 | *externalLeading = wxMax(0, fe.height - (fe.ascent + fe.descent)); | |
1694 | } | |
e7f9f819 | 1695 | #endif |
184fc6c8 SC |
1696 | } |
1697 | ||
1698 | void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const | |
1699 | { | |
1700 | widths.Empty(); | |
1701 | widths.Add(0, text.length()); | |
1702 | ||
1011fbeb SC |
1703 | wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") ); |
1704 | ||
184fc6c8 SC |
1705 | if (text.empty()) |
1706 | return; | |
00bd8e72 SC |
1707 | |
1708 | // TODO | |
184fc6c8 SC |
1709 | } |
1710 | ||
49ad157e | 1711 | void * wxCairoContext::GetNativeContext() |
184fc6c8 | 1712 | { |
00bd8e72 SC |
1713 | return m_context; |
1714 | } | |
184fc6c8 | 1715 | |
bf02a7f9 | 1716 | bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias) |
49ad157e | 1717 | { |
bf02a7f9 | 1718 | if (m_antialias == antialias) |
49ad157e VZ |
1719 | return true; |
1720 | ||
bf02a7f9 | 1721 | m_antialias = antialias; |
03647350 | 1722 | |
bf02a7f9 SC |
1723 | cairo_antialias_t antialiasMode; |
1724 | switch (antialias) | |
1725 | { | |
1726 | case wxANTIALIAS_DEFAULT: | |
1727 | antialiasMode = CAIRO_ANTIALIAS_DEFAULT; | |
1728 | break; | |
1729 | case wxANTIALIAS_NONE: | |
1730 | antialiasMode = CAIRO_ANTIALIAS_NONE; | |
1731 | break; | |
1732 | default: | |
1733 | return false; | |
1734 | } | |
1735 | cairo_set_antialias(m_context, antialiasMode); | |
1736 | return true; | |
1737 | } | |
49ad157e | 1738 | |
133cb28b SC |
1739 | bool wxCairoContext::SetInterpolationQuality(wxInterpolationQuality WXUNUSED(interpolation)) |
1740 | { | |
1741 | // placeholder | |
1742 | return false; | |
1743 | } | |
1744 | ||
bf02a7f9 SC |
1745 | bool wxCairoContext::SetCompositionMode(wxCompositionMode op) |
1746 | { | |
1747 | if ( m_composition == op ) | |
1748 | return true; | |
03647350 | 1749 | |
bf02a7f9 SC |
1750 | m_composition = op; |
1751 | cairo_operator_t cop; | |
1752 | switch (op) | |
49ad157e | 1753 | { |
50de831a | 1754 | case wxCOMPOSITION_CLEAR: |
bf02a7f9 | 1755 | cop = CAIRO_OPERATOR_CLEAR; |
49ad157e | 1756 | break; |
bf02a7f9 SC |
1757 | case wxCOMPOSITION_SOURCE: |
1758 | cop = CAIRO_OPERATOR_SOURCE; | |
49ad157e | 1759 | break; |
bf02a7f9 SC |
1760 | case wxCOMPOSITION_OVER: |
1761 | cop = CAIRO_OPERATOR_OVER; | |
49ad157e | 1762 | break; |
bf02a7f9 SC |
1763 | case wxCOMPOSITION_IN: |
1764 | cop = CAIRO_OPERATOR_IN; | |
1765 | break; | |
1766 | case wxCOMPOSITION_OUT: | |
1767 | cop = CAIRO_OPERATOR_OUT; | |
1768 | break; | |
1769 | case wxCOMPOSITION_ATOP: | |
1770 | cop = CAIRO_OPERATOR_ATOP; | |
1771 | break; | |
1772 | case wxCOMPOSITION_DEST: | |
1773 | cop = CAIRO_OPERATOR_DEST; | |
1774 | break; | |
1775 | case wxCOMPOSITION_DEST_OVER: | |
1776 | cop = CAIRO_OPERATOR_DEST_OVER; | |
1777 | break; | |
1778 | case wxCOMPOSITION_DEST_IN: | |
1779 | cop = CAIRO_OPERATOR_DEST_IN; | |
1780 | break; | |
1781 | case wxCOMPOSITION_DEST_OUT: | |
1782 | cop = CAIRO_OPERATOR_DEST_OUT; | |
1783 | break; | |
1784 | case wxCOMPOSITION_DEST_ATOP: | |
1785 | cop = CAIRO_OPERATOR_DEST_ATOP; | |
1786 | break; | |
1787 | case wxCOMPOSITION_XOR: | |
1788 | cop = CAIRO_OPERATOR_XOR; | |
1789 | break; | |
1790 | case wxCOMPOSITION_ADD: | |
1791 | cop = CAIRO_OPERATOR_ADD; | |
49ad157e | 1792 | break; |
49ad157e VZ |
1793 | default: |
1794 | return false; | |
1795 | } | |
bf02a7f9 | 1796 | cairo_set_operator(m_context, cop); |
49ad157e VZ |
1797 | return true; |
1798 | } | |
1799 | ||
bf02a7f9 SC |
1800 | void wxCairoContext::BeginLayer(wxDouble opacity) |
1801 | { | |
1802 | m_layerOpacities.push_back(opacity); | |
1803 | cairo_push_group(m_context); | |
1804 | } | |
49ad157e | 1805 | |
bf02a7f9 SC |
1806 | void wxCairoContext::EndLayer() |
1807 | { | |
1808 | float opacity = m_layerOpacities.back(); | |
1809 | m_layerOpacities.pop_back(); | |
1810 | cairo_pop_group_to_source(m_context); | |
1811 | cairo_paint_with_alpha(m_context,opacity); | |
1812 | } | |
03647350 | 1813 | |
00bd8e72 SC |
1814 | //----------------------------------------------------------------------------- |
1815 | // wxCairoRenderer declaration | |
1816 | //----------------------------------------------------------------------------- | |
1817 | ||
1818 | class WXDLLIMPEXP_CORE wxCairoRenderer : public wxGraphicsRenderer | |
1819 | { | |
1820 | public : | |
1821 | wxCairoRenderer() {} | |
1822 | ||
1823 | virtual ~wxCairoRenderer() {} | |
1824 | ||
1825 | // Context | |
1826 | ||
1827 | virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc); | |
773ccc31 | 1828 | virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc); |
0b822969 | 1829 | virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc); |
773ccc31 | 1830 | |
00bd8e72 SC |
1831 | virtual wxGraphicsContext * CreateContextFromNativeContext( void * context ); |
1832 | ||
1833 | virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window ); | |
1834 | ||
1835 | virtual wxGraphicsContext * CreateContext( wxWindow* window ); | |
1836 | ||
091ef146 | 1837 | virtual wxGraphicsContext * CreateMeasuringContext(); |
9f2b6b31 RD |
1838 | #ifdef __WXMSW__ |
1839 | #if wxUSE_ENH_METAFILE | |
1840 | virtual wxGraphicsContext * CreateContext( const wxEnhMetaFileDC& dc); | |
1841 | #endif | |
1842 | #endif | |
00bd8e72 SC |
1843 | // Path |
1844 | ||
0db8a70e | 1845 | virtual wxGraphicsPath CreatePath(); |
00bd8e72 SC |
1846 | |
1847 | // Matrix | |
1848 | ||
49ad157e | 1849 | virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0, |
00bd8e72 SC |
1850 | wxDouble tx=0.0, wxDouble ty=0.0); |
1851 | ||
1852 | ||
87752530 | 1853 | virtual wxGraphicsPen CreatePen(const wxPen& pen) ; |
00bd8e72 | 1854 | |
87752530 | 1855 | virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ; |
00bd8e72 | 1856 | |
4ee4c7b9 VZ |
1857 | virtual wxGraphicsBrush |
1858 | CreateLinearGradientBrush(wxDouble x1, wxDouble y1, | |
1859 | wxDouble x2, wxDouble y2, | |
1860 | const wxGraphicsGradientStops& stops); | |
00bd8e72 | 1861 | |
4ee4c7b9 VZ |
1862 | virtual wxGraphicsBrush |
1863 | CreateRadialGradientBrush(wxDouble xo, wxDouble yo, | |
1864 | wxDouble xc, wxDouble yc, | |
1865 | wxDouble radius, | |
1866 | const wxGraphicsGradientStops& stops); | |
00bd8e72 SC |
1867 | |
1868 | // sets the font | |
87752530 | 1869 | virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ; |
00bd8e72 | 1870 | |
d974a494 | 1871 | // create a native bitmap representation |
2fc9c1ea | 1872 | virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap ); |
49ad157e | 1873 | |
2986eb86 SC |
1874 | // create a graphics bitmap from a native bitmap |
1875 | virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap ); | |
1876 | ||
d974a494 | 1877 | // create a subimage from a native image representation |
2fc9c1ea | 1878 | virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); |
d974a494 | 1879 | |
932d0768 RD |
1880 | protected : |
1881 | bool EnsureIsLoaded(); | |
1882 | void Load(); | |
1883 | void Unload(); | |
1884 | friend class wxCairoModule; | |
00bd8e72 | 1885 | private : |
932d0768 | 1886 | int m_loaded; |
00bd8e72 SC |
1887 | DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer) |
1888 | } ; | |
1889 | ||
1890 | //----------------------------------------------------------------------------- | |
1891 | // wxCairoRenderer implementation | |
1892 | //----------------------------------------------------------------------------- | |
1893 | ||
1894 | IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer,wxGraphicsRenderer) | |
1895 | ||
1896 | static wxCairoRenderer gs_cairoGraphicsRenderer; | |
3e700936 SC |
1897 | // temporary hack to allow creating a cairo context on any platform |
1898 | extern wxGraphicsRenderer* gCairoRenderer; | |
1899 | wxGraphicsRenderer* gCairoRenderer = &gs_cairoGraphicsRenderer; | |
00bd8e72 | 1900 | |
932d0768 RD |
1901 | bool wxCairoRenderer::EnsureIsLoaded() |
1902 | { | |
1903 | #ifndef __WXGTK__ | |
1904 | Load(); | |
1905 | return wxCairoInit(); | |
1906 | #else | |
1907 | return true; | |
1908 | #endif | |
1909 | } | |
1910 | ||
1911 | void wxCairoRenderer::Load() | |
1912 | { | |
1913 | wxCairoInit(); | |
1914 | } | |
1915 | ||
1916 | void wxCairoRenderer::Unload() | |
1917 | { | |
1918 | wxCairoCleanUp(); | |
1919 | } | |
1920 | ||
1921 | // call EnsureIsLoaded() and return returnOnFail value if it fails | |
1922 | #define ENSURE_LOADED_OR_RETURN(returnOnFail) \ | |
1923 | if ( !EnsureIsLoaded() ) \ | |
1924 | return (returnOnFail) | |
1925 | ||
00bd8e72 | 1926 | wxGraphicsContext * wxCairoRenderer::CreateContext( const wxWindowDC& dc) |
539e2795 | 1927 | { |
932d0768 | 1928 | ENSURE_LOADED_OR_RETURN(NULL); |
00bd8e72 SC |
1929 | return new wxCairoContext(this,dc); |
1930 | } | |
1931 | ||
773ccc31 SC |
1932 | wxGraphicsContext * wxCairoRenderer::CreateContext( const wxMemoryDC& dc) |
1933 | { | |
932d0768 | 1934 | ENSURE_LOADED_OR_RETURN(NULL); |
888dde65 | 1935 | return new wxCairoContext(this,dc); |
773ccc31 | 1936 | } |
773ccc31 | 1937 | |
0b822969 RR |
1938 | wxGraphicsContext * wxCairoRenderer::CreateContext( const wxPrinterDC& dc) |
1939 | { | |
932d0768 | 1940 | ENSURE_LOADED_OR_RETURN(NULL); |
c9008abe | 1941 | #ifdef __WXGTK20__ |
0b822969 RR |
1942 | const wxDCImpl *impl = dc.GetImpl(); |
1943 | cairo_t* context = (cairo_t*) impl->GetCairoContext(); | |
1944 | if (context) | |
c9008abe | 1945 | return new wxCairoContext(this,dc); |
0b822969 | 1946 | else |
c9008abe | 1947 | #endif |
932d0768 | 1948 | return new wxCairoContext(this,dc); |
0b822969 RR |
1949 | } |
1950 | ||
9f2b6b31 RD |
1951 | #ifdef __WXMSW__ |
1952 | #if wxUSE_ENH_METAFILE | |
932d0768 | 1953 | wxGraphicsContext * wxCairoRenderer::CreateContext( const wxEnhMetaFileDC& WXUNUSED(dc) ) |
9f2b6b31 | 1954 | { |
932d0768 | 1955 | ENSURE_LOADED_OR_RETURN(NULL); |
9f2b6b31 RD |
1956 | return NULL; |
1957 | } | |
1958 | #endif | |
1959 | #endif | |
1960 | ||
00bd8e72 SC |
1961 | wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context ) |
1962 | { | |
932d0768 | 1963 | ENSURE_LOADED_OR_RETURN(NULL); |
a8f9fe13 | 1964 | #ifdef __WXMSW__ |
c0e69d72 | 1965 | return new wxCairoContext(this,(HDC)context); |
711a4812 | 1966 | #else |
00bd8e72 | 1967 | return new wxCairoContext(this,(cairo_t*)context); |
c0e69d72 | 1968 | #endif |
00bd8e72 SC |
1969 | } |
1970 | ||
1971 | ||
1972 | wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window ) | |
1973 | { | |
932d0768 | 1974 | ENSURE_LOADED_OR_RETURN(NULL); |
00bd8e72 | 1975 | #ifdef __WXGTK__ |
ed1b38a8 | 1976 | return new wxCairoContext(this,(GdkDrawable*)window); |
00bd8e72 | 1977 | #else |
932d0768 | 1978 | wxUnusedVar(window); |
00bd8e72 SC |
1979 | return NULL; |
1980 | #endif | |
1981 | } | |
1982 | ||
091ef146 SC |
1983 | wxGraphicsContext * wxCairoRenderer::CreateMeasuringContext() |
1984 | { | |
932d0768 | 1985 | ENSURE_LOADED_OR_RETURN(NULL); |
5e8d27fe KO |
1986 | #ifdef __WXGTK__ |
1987 | return CreateContextFromNativeWindow(gdk_get_default_root_window()); | |
1988 | #endif | |
091ef146 SC |
1989 | return NULL; |
1990 | // TODO | |
1991 | } | |
1992 | ||
00bd8e72 SC |
1993 | wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window ) |
1994 | { | |
932d0768 | 1995 | ENSURE_LOADED_OR_RETURN(NULL); |
00bd8e72 SC |
1996 | return new wxCairoContext(this, window ); |
1997 | } | |
1998 | ||
1999 | // Path | |
2000 | ||
0db8a70e | 2001 | wxGraphicsPath wxCairoRenderer::CreatePath() |
00bd8e72 | 2002 | { |
932d0768 | 2003 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath); |
0db8a70e RD |
2004 | wxGraphicsPath path; |
2005 | path.SetRefData( new wxCairoPathData(this) ); | |
2006 | return path; | |
539e2795 SC |
2007 | } |
2008 | ||
00bd8e72 SC |
2009 | |
2010 | // Matrix | |
2011 | ||
49ad157e | 2012 | wxGraphicsMatrix wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d, |
0db8a70e | 2013 | wxDouble tx, wxDouble ty) |
00bd8e72 | 2014 | |
184fc6c8 | 2015 | { |
932d0768 | 2016 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix); |
0db8a70e RD |
2017 | wxGraphicsMatrix m; |
2018 | wxCairoMatrixData* data = new wxCairoMatrixData( this ); | |
2019 | data->Set( a,b,c,d,tx,ty ) ; | |
2020 | m.SetRefData(data); | |
00bd8e72 | 2021 | return m; |
7ba86d93 RD |
2022 | } |
2023 | ||
49ad157e | 2024 | wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen) |
539e2795 | 2025 | { |
932d0768 | 2026 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen); |
a1b806b9 | 2027 | if ( !pen.IsOk() || pen.GetStyle() == wxPENSTYLE_TRANSPARENT ) |
87752530 | 2028 | return wxNullGraphicsPen; |
00bd8e72 | 2029 | else |
87752530 SC |
2030 | { |
2031 | wxGraphicsPen p; | |
2032 | p.SetRefData(new wxCairoPenData( this, pen )); | |
2033 | return p; | |
2034 | } | |
539e2795 SC |
2035 | } |
2036 | ||
49ad157e | 2037 | wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush ) |
539e2795 | 2038 | { |
932d0768 | 2039 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush); |
a1b806b9 | 2040 | if ( !brush.IsOk() || brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT ) |
87752530 | 2041 | return wxNullGraphicsBrush; |
00bd8e72 | 2042 | else |
87752530 SC |
2043 | { |
2044 | wxGraphicsBrush p; | |
2045 | p.SetRefData(new wxCairoBrushData( this, brush )); | |
2046 | return p; | |
2047 | } | |
00bd8e72 SC |
2048 | } |
2049 | ||
4ee4c7b9 VZ |
2050 | wxGraphicsBrush |
2051 | wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1, | |
2052 | wxDouble x2, wxDouble y2, | |
2053 | const wxGraphicsGradientStops& stops) | |
00bd8e72 | 2054 | { |
932d0768 | 2055 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush); |
87752530 SC |
2056 | wxGraphicsBrush p; |
2057 | wxCairoBrushData* d = new wxCairoBrushData( this ); | |
4ee4c7b9 | 2058 | d->CreateLinearGradientBrush(x1, y1, x2, y2, stops); |
87752530 SC |
2059 | p.SetRefData(d); |
2060 | return p; | |
00bd8e72 SC |
2061 | } |
2062 | ||
4ee4c7b9 VZ |
2063 | wxGraphicsBrush |
2064 | wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo, | |
2065 | wxDouble xc, wxDouble yc, wxDouble r, | |
2066 | const wxGraphicsGradientStops& stops) | |
00bd8e72 | 2067 | { |
932d0768 | 2068 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush); |
87752530 SC |
2069 | wxGraphicsBrush p; |
2070 | wxCairoBrushData* d = new wxCairoBrushData( this ); | |
4ee4c7b9 | 2071 | d->CreateRadialGradientBrush(xo, yo, xc, yc, r, stops); |
87752530 SC |
2072 | p.SetRefData(d); |
2073 | return p; | |
00bd8e72 SC |
2074 | } |
2075 | ||
2076 | // sets the font | |
49ad157e | 2077 | wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col ) |
00bd8e72 | 2078 | { |
932d0768 | 2079 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont); |
a1b806b9 | 2080 | if ( font.IsOk() ) |
49ad157e | 2081 | { |
87752530 SC |
2082 | wxGraphicsFont p; |
2083 | p.SetRefData(new wxCairoFontData( this , font, col )); | |
2084 | return p; | |
2085 | } | |
00bd8e72 | 2086 | else |
87752530 | 2087 | return wxNullGraphicsFont; |
539e2795 SC |
2088 | } |
2089 | ||
f5008769 | 2090 | wxGraphicsBitmap wxCairoRenderer::CreateBitmap( const wxBitmap& bmp ) |
2fc9c1ea | 2091 | { |
932d0768 | 2092 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap); |
a1b806b9 | 2093 | if ( bmp.IsOk() ) |
2fc9c1ea KO |
2094 | { |
2095 | wxGraphicsBitmap p; | |
2096 | p.SetRefData(new wxCairoBitmapData( this , bmp )); | |
2097 | return p; | |
2098 | } | |
2099 | else | |
2100 | return wxNullGraphicsBitmap; | |
2101 | } | |
2102 | ||
2986eb86 SC |
2103 | wxGraphicsBitmap wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap ) |
2104 | { | |
932d0768 | 2105 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap); |
2986eb86 SC |
2106 | if ( bitmap != NULL ) |
2107 | { | |
2108 | wxGraphicsBitmap p; | |
2109 | p.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t*) bitmap )); | |
2110 | return p; | |
2111 | } | |
2112 | else | |
2113 | return wxNullGraphicsBitmap; | |
2114 | } | |
2115 | ||
84e0e526 VZ |
2116 | wxGraphicsBitmap |
2117 | wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap& WXUNUSED(bitmap), | |
2118 | wxDouble WXUNUSED(x), | |
2119 | wxDouble WXUNUSED(y), | |
2120 | wxDouble WXUNUSED(w), | |
2121 | wxDouble WXUNUSED(h)) | |
2fc9c1ea | 2122 | { |
932d0768 | 2123 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap); |
2fc9c1ea KO |
2124 | wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented."); |
2125 | return wxNullGraphicsBitmap; | |
2126 | } | |
2127 | ||
c0e69d72 KO |
2128 | wxGraphicsRenderer* wxGraphicsRenderer::GetCairoRenderer() |
2129 | { | |
c0e69d72 | 2130 | return &gs_cairoGraphicsRenderer; |
ef40c54c VZ |
2131 | } |
2132 | ||
2133 | #else // !wxUSE_CAIRO | |
2134 | ||
2135 | wxGraphicsRenderer* wxGraphicsRenderer::GetCairoRenderer() | |
2136 | { | |
c0e69d72 | 2137 | return NULL; |
c0e69d72 | 2138 | } |
9a67941f | 2139 | |
ef40c54c VZ |
2140 | #endif // wxUSE_CAIRO/!wxUSE_CAIRO |
2141 | ||
2142 | // MSW and OS X have their own native default renderers, but the other ports | |
2143 | // use Cairo by default | |
2144 | #if !(defined(__WXMSW__) || defined(__WXOSX__)) | |
2145 | wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer() | |
2146 | { | |
2147 | return GetCairoRenderer(); | |
2148 | } | |
2149 | #endif // !(__WXMSW__ || __WXOSX__) | |
2150 | ||
9a67941f | 2151 | #endif // wxUSE_GRAPHICS_CONTEXT |