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