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