]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/graphics.cpp | |
3 | // Purpose: wxGCDC class | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 2006-09-30 | |
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 | #include "wx/dc.h" | |
19 | ||
20 | #if wxUSE_GRAPHICS_CONTEXT | |
21 | ||
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/msw/wrapcdlg.h" | |
24 | #include "wx/image.h" | |
25 | #include "wx/window.h" | |
26 | #include "wx/utils.h" | |
27 | #include "wx/dialog.h" | |
28 | #include "wx/app.h" | |
29 | #include "wx/bitmap.h" | |
30 | #include "wx/log.h" | |
31 | #include "wx/icon.h" | |
32 | #include "wx/module.h" | |
33 | // include all dc types that are used as a param | |
34 | #include "wx/dc.h" | |
35 | #include "wx/dcclient.h" | |
36 | #include "wx/dcmemory.h" | |
37 | #include "wx/dcprint.h" | |
38 | #endif | |
39 | ||
40 | #include "wx/stack.h" | |
41 | ||
42 | #include "wx/private/graphics.h" | |
43 | #include "wx/msw/wrapgdip.h" | |
44 | #include "wx/msw/dc.h" | |
45 | #if wxUSE_ENH_METAFILE | |
46 | #include "wx/msw/enhmeta.h" | |
47 | #endif | |
48 | #include "wx/dcgraph.h" | |
49 | ||
50 | #include "wx/msw/private.h" // needs to be before #include <commdlg.h> | |
51 | ||
52 | #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__) | |
53 | #include <commdlg.h> | |
54 | #endif | |
55 | ||
56 | namespace | |
57 | { | |
58 | ||
59 | //----------------------------------------------------------------------------- | |
60 | // constants | |
61 | //----------------------------------------------------------------------------- | |
62 | ||
63 | const double RAD2DEG = 180.0 / M_PI; | |
64 | ||
65 | //----------------------------------------------------------------------------- | |
66 | // Local functions | |
67 | //----------------------------------------------------------------------------- | |
68 | ||
69 | inline double dmin(double a, double b) { return a < b ? a : b; } | |
70 | inline double dmax(double a, double b) { return a > b ? a : b; } | |
71 | ||
72 | inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; } | |
73 | inline double RadToDeg(double deg) { return (deg * 180.0) / M_PI; } | |
74 | ||
75 | // translate a wxColour to a Color | |
76 | inline Color wxColourToColor(const wxColour& col) | |
77 | { | |
78 | return Color(col.Alpha(), col.Red(), col.Green(), col.Blue()); | |
79 | } | |
80 | ||
81 | } // anonymous namespace | |
82 | ||
83 | //----------------------------------------------------------------------------- | |
84 | // device context implementation | |
85 | // | |
86 | // more and more of the dc functionality should be implemented by calling | |
87 | // the appropricate wxGDIPlusContext, but we will have to do that step by step | |
88 | // also coordinate conversions should be moved to native matrix ops | |
89 | //----------------------------------------------------------------------------- | |
90 | ||
91 | // we always stock two context states, one at entry, to be able to preserve the | |
92 | // state we were called with, the other one after changing to HI Graphics orientation | |
93 | // (this one is used for getting back clippings etc) | |
94 | ||
95 | //----------------------------------------------------------------------------- | |
96 | // wxGraphicsPath implementation | |
97 | //----------------------------------------------------------------------------- | |
98 | ||
99 | class wxGDIPlusContext; | |
100 | ||
101 | class wxGDIPlusPathData : public wxGraphicsPathData | |
102 | { | |
103 | public : | |
104 | wxGDIPlusPathData(wxGraphicsRenderer* renderer, GraphicsPath* path = NULL); | |
105 | ~wxGDIPlusPathData(); | |
106 | ||
107 | virtual wxGraphicsObjectRefData *Clone() const; | |
108 | ||
109 | // | |
110 | // These are the path primitives from which everything else can be constructed | |
111 | // | |
112 | ||
113 | // begins a new subpath at (x,y) | |
114 | virtual void MoveToPoint( wxDouble x, wxDouble y ); | |
115 | ||
116 | // adds a straight line from the current point to (x,y) | |
117 | virtual void AddLineToPoint( wxDouble x, wxDouble y ); | |
118 | ||
119 | // adds a cubic Bezier curve from the current point, using two control points and an end point | |
120 | virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y ); | |
121 | ||
122 | ||
123 | // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle | |
124 | virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ; | |
125 | ||
126 | // gets the last point of the current path, (0,0) if not yet set | |
127 | virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const; | |
128 | ||
129 | // adds another path | |
130 | virtual void AddPath( const wxGraphicsPathData* path ); | |
131 | ||
132 | // closes the current sub-path | |
133 | virtual void CloseSubpath(); | |
134 | ||
135 | // | |
136 | // These are convenience functions which - if not available natively will be assembled | |
137 | // using the primitives from above | |
138 | // | |
139 | ||
140 | // appends a rectangle as a new closed subpath | |
141 | virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ; | |
142 | /* | |
143 | ||
144 | // appends an ellipsis as a new closed subpath fitting the passed rectangle | |
145 | virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ; | |
146 | ||
147 | // 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) | |
148 | virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ; | |
149 | */ | |
150 | ||
151 | // returns the native path | |
152 | virtual void * GetNativePath() const { return m_path; } | |
153 | ||
154 | // give the native path returned by GetNativePath() back (there might be some deallocations necessary) | |
155 | virtual void UnGetNativePath(void * WXUNUSED(path)) const {} | |
156 | ||
157 | // transforms each point of this path by the matrix | |
158 | virtual void Transform( const wxGraphicsMatrixData* matrix ) ; | |
159 | ||
160 | // gets the bounding box enclosing all points (possibly including control points) | |
161 | virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const; | |
162 | ||
163 | virtual bool Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle = wxODDEVEN_RULE) const; | |
164 | ||
165 | private : | |
166 | GraphicsPath* m_path; | |
167 | }; | |
168 | ||
169 | class wxGDIPlusMatrixData : public wxGraphicsMatrixData | |
170 | { | |
171 | public : | |
172 | wxGDIPlusMatrixData(wxGraphicsRenderer* renderer, Matrix* matrix = NULL) ; | |
173 | virtual ~wxGDIPlusMatrixData() ; | |
174 | ||
175 | virtual wxGraphicsObjectRefData* Clone() const ; | |
176 | ||
177 | // concatenates the matrix | |
178 | virtual void Concat( const wxGraphicsMatrixData *t ); | |
179 | ||
180 | // sets the matrix to the respective values | |
181 | virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0, | |
182 | wxDouble tx=0.0, wxDouble ty=0.0); | |
183 | ||
184 | // gets the component valuess of the matrix | |
185 | virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL, | |
186 | wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const; | |
187 | ||
188 | // makes this the inverse matrix | |
189 | virtual void Invert(); | |
190 | ||
191 | // returns true if the elements of the transformation matrix are equal ? | |
192 | virtual bool IsEqual( const wxGraphicsMatrixData* t) const ; | |
193 | ||
194 | // return true if this is the identity matrix | |
195 | virtual bool IsIdentity() const; | |
196 | ||
197 | // | |
198 | // transformation | |
199 | // | |
200 | ||
201 | // add the translation to this matrix | |
202 | virtual void Translate( wxDouble dx , wxDouble dy ); | |
203 | ||
204 | // add the scale to this matrix | |
205 | virtual void Scale( wxDouble xScale , wxDouble yScale ); | |
206 | ||
207 | // add the rotation to this matrix (radians) | |
208 | virtual void Rotate( wxDouble angle ); | |
209 | ||
210 | // | |
211 | // apply the transforms | |
212 | // | |
213 | ||
214 | // applies that matrix to the point | |
215 | virtual void TransformPoint( wxDouble *x, wxDouble *y ) const; | |
216 | ||
217 | // applies the matrix except for translations | |
218 | virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const; | |
219 | ||
220 | // returns the native representation | |
221 | virtual void * GetNativeMatrix() const; | |
222 | private: | |
223 | Matrix* m_matrix ; | |
224 | } ; | |
225 | ||
226 | class wxGDIPlusPenData : public wxGraphicsObjectRefData | |
227 | { | |
228 | public: | |
229 | wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &pen ); | |
230 | ~wxGDIPlusPenData(); | |
231 | ||
232 | void Init(); | |
233 | ||
234 | virtual wxDouble GetWidth() { return m_width; } | |
235 | virtual Pen* GetGDIPlusPen() { return m_pen; } | |
236 | ||
237 | protected : | |
238 | Pen* m_pen; | |
239 | Image* m_penImage; | |
240 | Brush* m_penBrush; | |
241 | ||
242 | wxDouble m_width; | |
243 | }; | |
244 | ||
245 | class wxGDIPlusBrushData : public wxGraphicsObjectRefData | |
246 | { | |
247 | public: | |
248 | wxGDIPlusBrushData( wxGraphicsRenderer* renderer ); | |
249 | wxGDIPlusBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush ); | |
250 | ~wxGDIPlusBrushData (); | |
251 | ||
252 | void CreateLinearGradientBrush(wxDouble x1, wxDouble y1, | |
253 | wxDouble x2, wxDouble y2, | |
254 | const wxGraphicsGradientStops& stops); | |
255 | void CreateRadialGradientBrush(wxDouble xo, wxDouble yo, | |
256 | wxDouble xc, wxDouble yc, | |
257 | wxDouble radius, | |
258 | const wxGraphicsGradientStops& stops); | |
259 | ||
260 | virtual Brush* GetGDIPlusBrush() { return m_brush; } | |
261 | ||
262 | protected: | |
263 | virtual void Init(); | |
264 | ||
265 | private: | |
266 | // common part of Create{Linear,Radial}GradientBrush() | |
267 | template <typename T> | |
268 | void SetGradientStops(T *brush, const wxGraphicsGradientStops& stops); | |
269 | ||
270 | Brush* m_brush; | |
271 | Image* m_brushImage; | |
272 | GraphicsPath* m_brushPath; | |
273 | }; | |
274 | ||
275 | class WXDLLIMPEXP_CORE wxGDIPlusBitmapData : public wxGraphicsObjectRefData | |
276 | { | |
277 | public: | |
278 | wxGDIPlusBitmapData( wxGraphicsRenderer* renderer, Bitmap* bitmap ); | |
279 | wxGDIPlusBitmapData( wxGraphicsRenderer* renderer, const wxBitmap &bmp ); | |
280 | ~wxGDIPlusBitmapData (); | |
281 | ||
282 | virtual Bitmap* GetGDIPlusBitmap() { return m_bitmap; } | |
283 | ||
284 | private : | |
285 | Bitmap* m_bitmap; | |
286 | Bitmap* m_helper; | |
287 | }; | |
288 | ||
289 | class wxGDIPlusFontData : public wxGraphicsObjectRefData | |
290 | { | |
291 | public: | |
292 | wxGDIPlusFontData( wxGraphicsRenderer* renderer, | |
293 | const wxGDIPlusContext* gc, | |
294 | const wxFont &font, | |
295 | const wxColour& col ); | |
296 | ~wxGDIPlusFontData(); | |
297 | ||
298 | virtual Brush* GetGDIPlusBrush() { return m_textBrush; } | |
299 | virtual Font* GetGDIPlusFont() { return m_font; } | |
300 | private : | |
301 | Brush* m_textBrush; | |
302 | Font* m_font; | |
303 | }; | |
304 | ||
305 | class wxGDIPlusContext : public wxGraphicsContext | |
306 | { | |
307 | public: | |
308 | wxGDIPlusContext( wxGraphicsRenderer* renderer, const wxDC& dc ); | |
309 | wxGDIPlusContext( wxGraphicsRenderer* renderer, HDC hdc, wxDouble width, wxDouble height ); | |
310 | wxGDIPlusContext( wxGraphicsRenderer* renderer, HWND hwnd ); | |
311 | wxGDIPlusContext( wxGraphicsRenderer* renderer, Graphics* gr); | |
312 | wxGDIPlusContext(); | |
313 | ||
314 | virtual ~wxGDIPlusContext(); | |
315 | ||
316 | virtual void Clip( const wxRegion ®ion ); | |
317 | // clips drawings to the rect | |
318 | virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
319 | ||
320 | // resets the clipping to original extent | |
321 | virtual void ResetClip(); | |
322 | ||
323 | virtual void * GetNativeContext(); | |
324 | ||
325 | virtual void StrokePath( const wxGraphicsPath& p ); | |
326 | virtual void FillPath( const wxGraphicsPath& p , wxPolygonFillMode fillStyle = wxODDEVEN_RULE ); | |
327 | ||
328 | virtual void DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
329 | ||
330 | // stroke lines connecting each of the points | |
331 | virtual void StrokeLines( size_t n, const wxPoint2DDouble *points); | |
332 | ||
333 | // draws a polygon | |
334 | virtual void DrawLines( size_t n, const wxPoint2DDouble *points, wxPolygonFillMode fillStyle = wxODDEVEN_RULE ); | |
335 | ||
336 | virtual bool SetAntialiasMode(wxAntialiasMode antialias); | |
337 | ||
338 | virtual bool SetCompositionMode(wxCompositionMode op); | |
339 | ||
340 | virtual void BeginLayer(wxDouble opacity); | |
341 | ||
342 | virtual void EndLayer(); | |
343 | ||
344 | virtual void Translate( wxDouble dx , wxDouble dy ); | |
345 | virtual void Scale( wxDouble xScale , wxDouble yScale ); | |
346 | virtual void Rotate( wxDouble angle ); | |
347 | ||
348 | // concatenates this transform with the current transform of this context | |
349 | virtual void ConcatTransform( const wxGraphicsMatrix& matrix ); | |
350 | ||
351 | // sets the transform of this context | |
352 | virtual void SetTransform( const wxGraphicsMatrix& matrix ); | |
353 | ||
354 | // gets the matrix of this context | |
355 | virtual wxGraphicsMatrix GetTransform() const; | |
356 | ||
357 | virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
358 | virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
359 | virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
360 | virtual void PushState(); | |
361 | virtual void PopState(); | |
362 | ||
363 | // sets the font of this context | |
364 | virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) const; | |
365 | ||
366 | virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height, | |
367 | wxDouble *descent, wxDouble *externalLeading ) const; | |
368 | virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const; | |
369 | virtual bool ShouldOffset() const; | |
370 | virtual void GetSize( wxDouble* width, wxDouble *height ); | |
371 | ||
372 | Graphics* GetGraphics() const { return m_context; } | |
373 | ||
374 | protected: | |
375 | ||
376 | wxDouble m_fontScaleRatio; | |
377 | ||
378 | private: | |
379 | void Init(); | |
380 | void SetDefaults(); | |
381 | ||
382 | virtual void DoDrawText(const wxString& str, wxDouble x, wxDouble y) | |
383 | { DoDrawFilledText(str, x, y, wxNullGraphicsBrush); } | |
384 | virtual void DoDrawFilledText(const wxString& str, wxDouble x, wxDouble y, | |
385 | const wxGraphicsBrush& backgroundBrush); | |
386 | ||
387 | Graphics* m_context; | |
388 | wxStack<GraphicsState> m_stateStack; | |
389 | GraphicsState m_state1; | |
390 | GraphicsState m_state2; | |
391 | ||
392 | DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext) | |
393 | }; | |
394 | ||
395 | class wxGDIPlusMeasuringContext : public wxGDIPlusContext | |
396 | { | |
397 | public: | |
398 | wxGDIPlusMeasuringContext( wxGraphicsRenderer* renderer ) : wxGDIPlusContext( renderer , m_hdc = GetDC(NULL), 1000, 1000 ) | |
399 | { | |
400 | } | |
401 | wxGDIPlusMeasuringContext() | |
402 | { | |
403 | } | |
404 | ||
405 | virtual ~wxGDIPlusMeasuringContext() | |
406 | { | |
407 | ReleaseDC( NULL, m_hdc ); | |
408 | } | |
409 | ||
410 | private: | |
411 | HDC m_hdc ; | |
412 | DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusMeasuringContext) | |
413 | } ; | |
414 | ||
415 | class wxGDIPlusPrintingContext : public wxGDIPlusContext | |
416 | { | |
417 | public: | |
418 | wxGDIPlusPrintingContext( wxGraphicsRenderer* renderer, const wxDC& dc ); | |
419 | virtual ~wxGDIPlusPrintingContext() { } | |
420 | protected: | |
421 | }; | |
422 | ||
423 | //----------------------------------------------------------------------------- | |
424 | // wxGDIPlusRenderer declaration | |
425 | //----------------------------------------------------------------------------- | |
426 | ||
427 | class wxGDIPlusRenderer : public wxGraphicsRenderer | |
428 | { | |
429 | public : | |
430 | wxGDIPlusRenderer() | |
431 | { | |
432 | m_loaded = -1; | |
433 | m_gditoken = 0; | |
434 | } | |
435 | ||
436 | virtual ~wxGDIPlusRenderer() | |
437 | { | |
438 | if ( m_loaded == 1 ) | |
439 | { | |
440 | Unload(); | |
441 | } | |
442 | } | |
443 | ||
444 | // Context | |
445 | ||
446 | virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc); | |
447 | ||
448 | virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc); | |
449 | ||
450 | #if wxUSE_PRINTING_ARCHITECTURE | |
451 | virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc); | |
452 | #endif | |
453 | ||
454 | #if wxUSE_ENH_METAFILE | |
455 | virtual wxGraphicsContext * CreateContext( const wxEnhMetaFileDC& dc); | |
456 | #endif | |
457 | ||
458 | virtual wxGraphicsContext * CreateContextFromNativeContext( void * context ); | |
459 | ||
460 | virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window ); | |
461 | ||
462 | virtual wxGraphicsContext * CreateContext( wxWindow* window ); | |
463 | ||
464 | virtual wxGraphicsContext * CreateMeasuringContext(); | |
465 | ||
466 | // Path | |
467 | ||
468 | virtual wxGraphicsPath CreatePath(); | |
469 | ||
470 | // Matrix | |
471 | ||
472 | virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0, | |
473 | wxDouble tx=0.0, wxDouble ty=0.0); | |
474 | ||
475 | ||
476 | virtual wxGraphicsPen CreatePen(const wxPen& pen) ; | |
477 | ||
478 | virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ; | |
479 | ||
480 | virtual wxGraphicsBrush | |
481 | CreateLinearGradientBrush(wxDouble x1, wxDouble y1, | |
482 | wxDouble x2, wxDouble y2, | |
483 | const wxGraphicsGradientStops& stops); | |
484 | ||
485 | virtual wxGraphicsBrush | |
486 | CreateRadialGradientBrush(wxDouble xo, wxDouble yo, | |
487 | wxDouble xc, wxDouble yc, | |
488 | wxDouble radius, | |
489 | const wxGraphicsGradientStops& stops); | |
490 | ||
491 | // create a native bitmap representation | |
492 | virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap ); | |
493 | ||
494 | // stub: should not be called directly | |
495 | virtual wxGraphicsFont CreateFont( const wxFont& WXUNUSED(font), | |
496 | const wxColour& WXUNUSED(col) ) | |
497 | { wxFAIL; return wxNullGraphicsFont; } | |
498 | ||
499 | // this is used to really create the font | |
500 | wxGraphicsFont CreateGDIPlusFont( const wxGDIPlusContext* gc, | |
501 | const wxFont &font, | |
502 | const wxColour &col ); | |
503 | ||
504 | // create a graphics bitmap from a native bitmap | |
505 | virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap ); | |
506 | ||
507 | // create a subimage from a native image representation | |
508 | virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
509 | ||
510 | protected : | |
511 | bool EnsureIsLoaded(); | |
512 | void Load(); | |
513 | void Unload(); | |
514 | friend class wxGDIPlusRendererModule; | |
515 | ||
516 | private : | |
517 | int m_loaded; | |
518 | ULONG_PTR m_gditoken; | |
519 | ||
520 | DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer) | |
521 | } ; | |
522 | ||
523 | //----------------------------------------------------------------------------- | |
524 | // wxGDIPlusPen implementation | |
525 | //----------------------------------------------------------------------------- | |
526 | ||
527 | wxGDIPlusPenData::~wxGDIPlusPenData() | |
528 | { | |
529 | delete m_pen; | |
530 | delete m_penImage; | |
531 | delete m_penBrush; | |
532 | } | |
533 | ||
534 | void wxGDIPlusPenData::Init() | |
535 | { | |
536 | m_pen = NULL ; | |
537 | m_penImage = NULL; | |
538 | m_penBrush = NULL; | |
539 | } | |
540 | ||
541 | wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &pen ) | |
542 | : wxGraphicsObjectRefData(renderer) | |
543 | { | |
544 | Init(); | |
545 | m_width = pen.GetWidth(); | |
546 | if (m_width <= 0.0) | |
547 | m_width = 0.1; | |
548 | ||
549 | m_pen = new Pen(wxColourToColor(pen.GetColour()), m_width ); | |
550 | ||
551 | LineCap cap; | |
552 | switch ( pen.GetCap() ) | |
553 | { | |
554 | case wxCAP_ROUND : | |
555 | cap = LineCapRound; | |
556 | break; | |
557 | ||
558 | case wxCAP_PROJECTING : | |
559 | cap = LineCapSquare; | |
560 | break; | |
561 | ||
562 | case wxCAP_BUTT : | |
563 | cap = LineCapFlat; // TODO verify | |
564 | break; | |
565 | ||
566 | default : | |
567 | cap = LineCapFlat; | |
568 | break; | |
569 | } | |
570 | m_pen->SetLineCap(cap,cap, DashCapFlat); | |
571 | ||
572 | LineJoin join; | |
573 | switch ( pen.GetJoin() ) | |
574 | { | |
575 | case wxJOIN_BEVEL : | |
576 | join = LineJoinBevel; | |
577 | break; | |
578 | ||
579 | case wxJOIN_MITER : | |
580 | join = LineJoinMiter; | |
581 | break; | |
582 | ||
583 | case wxJOIN_ROUND : | |
584 | join = LineJoinRound; | |
585 | break; | |
586 | ||
587 | default : | |
588 | join = LineJoinMiter; | |
589 | break; | |
590 | } | |
591 | ||
592 | m_pen->SetLineJoin(join); | |
593 | ||
594 | m_pen->SetDashStyle(DashStyleSolid); | |
595 | ||
596 | DashStyle dashStyle = DashStyleSolid; | |
597 | switch ( pen.GetStyle() ) | |
598 | { | |
599 | case wxPENSTYLE_SOLID : | |
600 | break; | |
601 | ||
602 | case wxPENSTYLE_DOT : | |
603 | dashStyle = DashStyleDot; | |
604 | break; | |
605 | ||
606 | case wxPENSTYLE_LONG_DASH : | |
607 | dashStyle = DashStyleDash; // TODO verify | |
608 | break; | |
609 | ||
610 | case wxPENSTYLE_SHORT_DASH : | |
611 | dashStyle = DashStyleDash; | |
612 | break; | |
613 | ||
614 | case wxPENSTYLE_DOT_DASH : | |
615 | dashStyle = DashStyleDashDot; | |
616 | break; | |
617 | case wxPENSTYLE_USER_DASH : | |
618 | { | |
619 | dashStyle = DashStyleCustom; | |
620 | wxDash *dashes; | |
621 | int count = pen.GetDashes( &dashes ); | |
622 | if ((dashes != NULL) && (count > 0)) | |
623 | { | |
624 | REAL *userLengths = new REAL[count]; | |
625 | for ( int i = 0; i < count; ++i ) | |
626 | { | |
627 | userLengths[i] = dashes[i]; | |
628 | } | |
629 | m_pen->SetDashPattern( userLengths, count); | |
630 | delete[] userLengths; | |
631 | } | |
632 | } | |
633 | break; | |
634 | case wxPENSTYLE_STIPPLE : | |
635 | { | |
636 | wxBitmap* bmp = pen.GetStipple(); | |
637 | if ( bmp && bmp->Ok() ) | |
638 | { | |
639 | m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(), | |
640 | #if wxUSE_PALETTE | |
641 | (HPALETTE)bmp->GetPalette()->GetHPALETTE() | |
642 | #else | |
643 | NULL | |
644 | #endif | |
645 | ); | |
646 | m_penBrush = new TextureBrush(m_penImage); | |
647 | m_pen->SetBrush( m_penBrush ); | |
648 | } | |
649 | ||
650 | } | |
651 | break; | |
652 | default : | |
653 | if ( pen.GetStyle() >= wxPENSTYLE_FIRST_HATCH && | |
654 | pen.GetStyle() <= wxPENSTYLE_LAST_HATCH ) | |
655 | { | |
656 | HatchStyle style; | |
657 | switch( pen.GetStyle() ) | |
658 | { | |
659 | case wxPENSTYLE_BDIAGONAL_HATCH : | |
660 | style = HatchStyleBackwardDiagonal; | |
661 | break ; | |
662 | case wxPENSTYLE_CROSSDIAG_HATCH : | |
663 | style = HatchStyleDiagonalCross; | |
664 | break ; | |
665 | case wxPENSTYLE_FDIAGONAL_HATCH : | |
666 | style = HatchStyleForwardDiagonal; | |
667 | break ; | |
668 | case wxPENSTYLE_CROSS_HATCH : | |
669 | style = HatchStyleCross; | |
670 | break ; | |
671 | case wxPENSTYLE_HORIZONTAL_HATCH : | |
672 | style = HatchStyleHorizontal; | |
673 | break ; | |
674 | case wxPENSTYLE_VERTICAL_HATCH : | |
675 | style = HatchStyleVertical; | |
676 | break ; | |
677 | default: | |
678 | style = HatchStyleHorizontal; | |
679 | } | |
680 | m_penBrush = new HatchBrush | |
681 | ( | |
682 | style, | |
683 | wxColourToColor(pen.GetColour()), | |
684 | Color::Transparent | |
685 | ); | |
686 | m_pen->SetBrush( m_penBrush ); | |
687 | } | |
688 | break; | |
689 | } | |
690 | if ( dashStyle != DashStyleSolid ) | |
691 | m_pen->SetDashStyle(dashStyle); | |
692 | } | |
693 | ||
694 | //----------------------------------------------------------------------------- | |
695 | // wxGDIPlusBrush implementation | |
696 | //----------------------------------------------------------------------------- | |
697 | ||
698 | wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer* renderer ) | |
699 | : wxGraphicsObjectRefData(renderer) | |
700 | { | |
701 | Init(); | |
702 | } | |
703 | ||
704 | wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer* renderer , const wxBrush &brush ) | |
705 | : wxGraphicsObjectRefData(renderer) | |
706 | { | |
707 | Init(); | |
708 | if ( brush.GetStyle() == wxSOLID) | |
709 | { | |
710 | m_brush = new SolidBrush(wxColourToColor( brush.GetColour())); | |
711 | } | |
712 | else if ( brush.IsHatch() ) | |
713 | { | |
714 | HatchStyle style; | |
715 | switch( brush.GetStyle() ) | |
716 | { | |
717 | case wxBRUSHSTYLE_BDIAGONAL_HATCH : | |
718 | style = HatchStyleBackwardDiagonal; | |
719 | break ; | |
720 | case wxBRUSHSTYLE_CROSSDIAG_HATCH : | |
721 | style = HatchStyleDiagonalCross; | |
722 | break ; | |
723 | case wxBRUSHSTYLE_FDIAGONAL_HATCH : | |
724 | style = HatchStyleForwardDiagonal; | |
725 | break ; | |
726 | case wxBRUSHSTYLE_CROSS_HATCH : | |
727 | style = HatchStyleCross; | |
728 | break ; | |
729 | case wxBRUSHSTYLE_HORIZONTAL_HATCH : | |
730 | style = HatchStyleHorizontal; | |
731 | break ; | |
732 | case wxBRUSHSTYLE_VERTICAL_HATCH : | |
733 | style = HatchStyleVertical; | |
734 | break ; | |
735 | default: | |
736 | style = HatchStyleHorizontal; | |
737 | } | |
738 | m_brush = new HatchBrush | |
739 | ( | |
740 | style, | |
741 | wxColourToColor(brush.GetColour()), | |
742 | Color::Transparent | |
743 | ); | |
744 | } | |
745 | else | |
746 | { | |
747 | wxBitmap* bmp = brush.GetStipple(); | |
748 | if ( bmp && bmp->Ok() ) | |
749 | { | |
750 | wxDELETE( m_brushImage ); | |
751 | m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(), | |
752 | #if wxUSE_PALETTE | |
753 | (HPALETTE)bmp->GetPalette()->GetHPALETTE() | |
754 | #else | |
755 | NULL | |
756 | #endif | |
757 | ); | |
758 | m_brush = new TextureBrush(m_brushImage); | |
759 | } | |
760 | } | |
761 | } | |
762 | ||
763 | wxGDIPlusBrushData::~wxGDIPlusBrushData() | |
764 | { | |
765 | delete m_brush; | |
766 | delete m_brushImage; | |
767 | delete m_brushPath; | |
768 | }; | |
769 | ||
770 | void wxGDIPlusBrushData::Init() | |
771 | { | |
772 | m_brush = NULL; | |
773 | m_brushImage= NULL; | |
774 | m_brushPath= NULL; | |
775 | } | |
776 | ||
777 | template <typename T> | |
778 | void | |
779 | wxGDIPlusBrushData::SetGradientStops(T *brush, | |
780 | const wxGraphicsGradientStops& stops) | |
781 | { | |
782 | const unsigned numStops = stops.GetCount(); | |
783 | if ( numStops <= 2 ) | |
784 | { | |
785 | // initial and final colours are set during the brush creation, nothing | |
786 | // more to do | |
787 | return; | |
788 | } | |
789 | ||
790 | wxVector<Color> colors(numStops); | |
791 | wxVector<REAL> positions(numStops); | |
792 | ||
793 | for ( unsigned i = 0; i < numStops; i++ ) | |
794 | { | |
795 | wxGraphicsGradientStop stop = stops.Item(i); | |
796 | ||
797 | colors[i] = wxColourToColor(stop.GetColour()); | |
798 | positions[i] = stop.GetPosition(); | |
799 | } | |
800 | ||
801 | brush->SetInterpolationColors(&colors[0], &positions[0], numStops); | |
802 | } | |
803 | ||
804 | void | |
805 | wxGDIPlusBrushData::CreateLinearGradientBrush(wxDouble x1, wxDouble y1, | |
806 | wxDouble x2, wxDouble y2, | |
807 | const wxGraphicsGradientStops& stops) | |
808 | { | |
809 | LinearGradientBrush * const | |
810 | brush = new LinearGradientBrush(PointF(x1, y1) , PointF(x2, y2), | |
811 | wxColourToColor(stops.GetStartColour()), | |
812 | wxColourToColor(stops.GetEndColour())); | |
813 | m_brush = brush; | |
814 | ||
815 | SetGradientStops(brush, stops); | |
816 | } | |
817 | ||
818 | void | |
819 | wxGDIPlusBrushData::CreateRadialGradientBrush(wxDouble xo, wxDouble yo, | |
820 | wxDouble xc, wxDouble yc, | |
821 | wxDouble radius, | |
822 | const wxGraphicsGradientStops& stops) | |
823 | { | |
824 | m_brushPath = new GraphicsPath(); | |
825 | m_brushPath->AddEllipse( (REAL)(xc-radius), (REAL)(yc-radius), | |
826 | (REAL)(2*radius), (REAL)(2*radius)); | |
827 | ||
828 | PathGradientBrush * const brush = new PathGradientBrush(m_brushPath); | |
829 | m_brush = brush; | |
830 | brush->SetCenterPoint(PointF(xo, yo)); | |
831 | brush->SetCenterColor(wxColourToColor(stops.GetStartColour())); | |
832 | ||
833 | const Color col(wxColourToColor(stops.GetEndColour())); | |
834 | int count = 1; | |
835 | brush->SetSurroundColors(&col, &count); | |
836 | ||
837 | SetGradientStops(brush, stops); | |
838 | } | |
839 | ||
840 | //----------------------------------------------------------------------------- | |
841 | // wxGDIPlusFont implementation | |
842 | //----------------------------------------------------------------------------- | |
843 | ||
844 | wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer* renderer, | |
845 | const wxGDIPlusContext* gc, | |
846 | const wxFont &font, | |
847 | const wxColour& col ) | |
848 | : wxGraphicsObjectRefData( renderer ) | |
849 | { | |
850 | wxWCharBuffer s = font.GetFaceName().wc_str( *wxConvUI ); | |
851 | int style = FontStyleRegular; | |
852 | if ( font.GetStyle() == wxFONTSTYLE_ITALIC ) | |
853 | style |= FontStyleItalic; | |
854 | if ( font.GetUnderlined() ) | |
855 | style |= FontStyleUnderline; | |
856 | if ( font.GetWeight() == wxFONTWEIGHT_BOLD ) | |
857 | style |= FontStyleBold; | |
858 | ||
859 | Graphics* context = gc->GetGraphics(); | |
860 | ||
861 | Unit fontUnit = context->GetPageUnit(); | |
862 | // if fontUnit is UnitDisplay, then specify UnitPixel, otherwise | |
863 | // you'll get a "InvalidParameter" from GDI+ | |
864 | if ( fontUnit == UnitDisplay ) | |
865 | fontUnit = UnitPixel; | |
866 | ||
867 | REAL points = font.GetPointSize(); | |
868 | ||
869 | // This scaling is needed when we use unit other than the | |
870 | // default UnitPoint. It works for both display and printing. | |
871 | REAL size = points * (100.0 / 72.0); | |
872 | ||
873 | // NB: font unit should match context's unit. We can use UnitPixel, | |
874 | // as that is what the print context should use. | |
875 | m_font = new Font( s, size, style, fontUnit ); | |
876 | ||
877 | m_textBrush = new SolidBrush(wxColourToColor(col)); | |
878 | } | |
879 | ||
880 | wxGDIPlusFontData::~wxGDIPlusFontData() | |
881 | { | |
882 | delete m_textBrush; | |
883 | delete m_font; | |
884 | } | |
885 | ||
886 | // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the | |
887 | // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied | |
888 | // bytes as parameter, since there is no real copying of the data going in, only references are stored | |
889 | // m_helper has to be kept alive as well | |
890 | ||
891 | //----------------------------------------------------------------------------- | |
892 | // wxGDIPlusBitmapData implementation | |
893 | //----------------------------------------------------------------------------- | |
894 | ||
895 | wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer* renderer, Bitmap* bitmap ) : | |
896 | wxGraphicsObjectRefData( renderer ), m_bitmap( bitmap ) | |
897 | { | |
898 | m_helper = NULL; | |
899 | } | |
900 | ||
901 | wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer* renderer, | |
902 | const wxBitmap &bmp) : wxGraphicsObjectRefData( renderer ) | |
903 | { | |
904 | m_bitmap = NULL; | |
905 | m_helper = NULL; | |
906 | ||
907 | Bitmap* image = NULL; | |
908 | if ( bmp.GetMask() ) | |
909 | { | |
910 | Bitmap interim((HBITMAP)bmp.GetHBITMAP(), | |
911 | #if wxUSE_PALETTE | |
912 | (HPALETTE)bmp.GetPalette()->GetHPALETTE() | |
913 | #else | |
914 | NULL | |
915 | #endif | |
916 | ); | |
917 | ||
918 | size_t width = interim.GetWidth(); | |
919 | size_t height = interim.GetHeight(); | |
920 | Rect bounds(0,0,width,height); | |
921 | ||
922 | image = new Bitmap(width,height,PixelFormat32bppPARGB) ; | |
923 | ||
924 | Bitmap interimMask((HBITMAP)bmp.GetMask()->GetMaskBitmap(),NULL); | |
925 | wxASSERT(interimMask.GetPixelFormat() == PixelFormat1bppIndexed); | |
926 | ||
927 | BitmapData dataMask ; | |
928 | interimMask.LockBits(&bounds,ImageLockModeRead, | |
929 | interimMask.GetPixelFormat(),&dataMask); | |
930 | ||
931 | ||
932 | BitmapData imageData ; | |
933 | image->LockBits(&bounds,ImageLockModeWrite, PixelFormat32bppPARGB, &imageData); | |
934 | ||
935 | BYTE maskPattern = 0 ; | |
936 | BYTE maskByte = 0; | |
937 | size_t maskIndex ; | |
938 | ||
939 | for ( size_t y = 0 ; y < height ; ++y) | |
940 | { | |
941 | maskIndex = 0 ; | |
942 | for( size_t x = 0 ; x < width; ++x) | |
943 | { | |
944 | if ( x % 8 == 0) | |
945 | { | |
946 | maskPattern = 0x80; | |
947 | maskByte = *((BYTE*)dataMask.Scan0 + dataMask.Stride*y + maskIndex); | |
948 | maskIndex++; | |
949 | } | |
950 | else | |
951 | maskPattern = maskPattern >> 1; | |
952 | ||
953 | ARGB *dest = (ARGB*)((BYTE*)imageData.Scan0 + imageData.Stride*y + x*4); | |
954 | if ( (maskByte & maskPattern) == 0 ) | |
955 | *dest = 0x00000000; | |
956 | else | |
957 | { | |
958 | Color c ; | |
959 | interim.GetPixel(x,y,&c) ; | |
960 | *dest = (c.GetValue() | Color::AlphaMask); | |
961 | } | |
962 | } | |
963 | } | |
964 | ||
965 | image->UnlockBits(&imageData); | |
966 | ||
967 | interimMask.UnlockBits(&dataMask); | |
968 | interim.UnlockBits(&dataMask); | |
969 | } | |
970 | else | |
971 | { | |
972 | image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(), | |
973 | #if wxUSE_PALETTE | |
974 | (HPALETTE)bmp.GetPalette()->GetHPALETTE() | |
975 | #else | |
976 | NULL | |
977 | #endif | |
978 | ); | |
979 | if ( bmp.HasAlpha() && GetPixelFormatSize(image->GetPixelFormat()) == 32 ) | |
980 | { | |
981 | size_t width = image->GetWidth(); | |
982 | size_t height = image->GetHeight(); | |
983 | Rect bounds(0,0,width,height); | |
984 | static BitmapData data ; | |
985 | ||
986 | m_helper = image ; | |
987 | image = NULL ; | |
988 | m_helper->LockBits(&bounds, ImageLockModeRead, | |
989 | m_helper->GetPixelFormat(),&data); | |
990 | ||
991 | image = new Bitmap(data.Width, data.Height, data.Stride, | |
992 | PixelFormat32bppPARGB , (BYTE*) data.Scan0); | |
993 | ||
994 | m_helper->UnlockBits(&data); | |
995 | } | |
996 | } | |
997 | if ( image ) | |
998 | m_bitmap = image; | |
999 | } | |
1000 | ||
1001 | wxGDIPlusBitmapData::~wxGDIPlusBitmapData() | |
1002 | { | |
1003 | delete m_bitmap; | |
1004 | delete m_helper; | |
1005 | } | |
1006 | ||
1007 | //----------------------------------------------------------------------------- | |
1008 | // wxGDIPlusPath implementation | |
1009 | //----------------------------------------------------------------------------- | |
1010 | ||
1011 | wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer* renderer, GraphicsPath* path ) : wxGraphicsPathData(renderer) | |
1012 | { | |
1013 | if ( path ) | |
1014 | m_path = path; | |
1015 | else | |
1016 | m_path = new GraphicsPath(); | |
1017 | } | |
1018 | ||
1019 | wxGDIPlusPathData::~wxGDIPlusPathData() | |
1020 | { | |
1021 | delete m_path; | |
1022 | } | |
1023 | ||
1024 | wxGraphicsObjectRefData* wxGDIPlusPathData::Clone() const | |
1025 | { | |
1026 | return new wxGDIPlusPathData( GetRenderer() , m_path->Clone()); | |
1027 | } | |
1028 | ||
1029 | // | |
1030 | // The Primitives | |
1031 | // | |
1032 | ||
1033 | void wxGDIPlusPathData::MoveToPoint( wxDouble x , wxDouble y ) | |
1034 | { | |
1035 | m_path->StartFigure(); | |
1036 | m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y); | |
1037 | } | |
1038 | ||
1039 | void wxGDIPlusPathData::AddLineToPoint( wxDouble x , wxDouble y ) | |
1040 | { | |
1041 | m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y); | |
1042 | } | |
1043 | ||
1044 | void wxGDIPlusPathData::CloseSubpath() | |
1045 | { | |
1046 | m_path->CloseFigure(); | |
1047 | } | |
1048 | ||
1049 | void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y ) | |
1050 | { | |
1051 | PointF c1(cx1,cy1); | |
1052 | PointF c2(cx2,cy2); | |
1053 | PointF end(x,y); | |
1054 | PointF start; | |
1055 | m_path->GetLastPoint(&start); | |
1056 | m_path->AddBezier(start,c1,c2,end); | |
1057 | } | |
1058 | ||
1059 | // gets the last point of the current path, (0,0) if not yet set | |
1060 | void wxGDIPlusPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const | |
1061 | { | |
1062 | PointF start; | |
1063 | m_path->GetLastPoint(&start); | |
1064 | *x = start.X ; | |
1065 | *y = start.Y ; | |
1066 | } | |
1067 | ||
1068 | void wxGDIPlusPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise ) | |
1069 | { | |
1070 | double sweepAngle = endAngle - startAngle ; | |
1071 | if( fabs(sweepAngle) >= 2*M_PI) | |
1072 | { | |
1073 | sweepAngle = 2 * M_PI; | |
1074 | } | |
1075 | else | |
1076 | { | |
1077 | if ( clockwise ) | |
1078 | { | |
1079 | if( sweepAngle < 0 ) | |
1080 | sweepAngle += 2 * M_PI; | |
1081 | } | |
1082 | else | |
1083 | { | |
1084 | if( sweepAngle > 0 ) | |
1085 | sweepAngle -= 2 * M_PI; | |
1086 | ||
1087 | } | |
1088 | } | |
1089 | m_path->AddArc((REAL) (x-r),(REAL) (y-r),(REAL) (2*r),(REAL) (2*r),RadToDeg(startAngle),RadToDeg(sweepAngle)); | |
1090 | } | |
1091 | ||
1092 | void wxGDIPlusPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
1093 | { | |
1094 | m_path->AddRectangle(RectF(x,y,w,h)); | |
1095 | } | |
1096 | ||
1097 | void wxGDIPlusPathData::AddPath( const wxGraphicsPathData* path ) | |
1098 | { | |
1099 | m_path->AddPath( (GraphicsPath*) path->GetNativePath(), FALSE); | |
1100 | } | |
1101 | ||
1102 | ||
1103 | // transforms each point of this path by the matrix | |
1104 | void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData* matrix ) | |
1105 | { | |
1106 | m_path->Transform( (Matrix*) matrix->GetNativeMatrix() ); | |
1107 | } | |
1108 | ||
1109 | // gets the bounding box enclosing all points (possibly including control points) | |
1110 | void wxGDIPlusPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const | |
1111 | { | |
1112 | RectF bounds; | |
1113 | m_path->GetBounds( &bounds, NULL, NULL) ; | |
1114 | *x = bounds.X; | |
1115 | *y = bounds.Y; | |
1116 | *w = bounds.Width; | |
1117 | *h = bounds.Height; | |
1118 | } | |
1119 | ||
1120 | bool wxGDIPlusPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle ) const | |
1121 | { | |
1122 | m_path->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding); | |
1123 | return m_path->IsVisible( (FLOAT) x,(FLOAT) y) == TRUE ; | |
1124 | } | |
1125 | ||
1126 | //----------------------------------------------------------------------------- | |
1127 | // wxGDIPlusMatrixData implementation | |
1128 | //----------------------------------------------------------------------------- | |
1129 | ||
1130 | wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer* renderer, Matrix* matrix ) | |
1131 | : wxGraphicsMatrixData(renderer) | |
1132 | { | |
1133 | if ( matrix ) | |
1134 | m_matrix = matrix ; | |
1135 | else | |
1136 | m_matrix = new Matrix(); | |
1137 | } | |
1138 | ||
1139 | wxGDIPlusMatrixData::~wxGDIPlusMatrixData() | |
1140 | { | |
1141 | delete m_matrix; | |
1142 | } | |
1143 | ||
1144 | wxGraphicsObjectRefData *wxGDIPlusMatrixData::Clone() const | |
1145 | { | |
1146 | return new wxGDIPlusMatrixData( GetRenderer(), m_matrix->Clone()); | |
1147 | } | |
1148 | ||
1149 | // concatenates the matrix | |
1150 | void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData *t ) | |
1151 | { | |
1152 | m_matrix->Multiply( (Matrix*) t->GetNativeMatrix()); | |
1153 | } | |
1154 | ||
1155 | // sets the matrix to the respective values | |
1156 | void wxGDIPlusMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d, | |
1157 | wxDouble tx, wxDouble ty) | |
1158 | { | |
1159 | m_matrix->SetElements(a,b,c,d,tx,ty); | |
1160 | } | |
1161 | ||
1162 | // gets the component valuess of the matrix | |
1163 | void wxGDIPlusMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c, | |
1164 | wxDouble* d, wxDouble* tx, wxDouble* ty) const | |
1165 | { | |
1166 | REAL elements[6]; | |
1167 | m_matrix->GetElements(elements); | |
1168 | if (a) *a = elements[0]; | |
1169 | if (b) *b = elements[1]; | |
1170 | if (c) *c = elements[2]; | |
1171 | if (d) *d = elements[3]; | |
1172 | if (tx) *tx= elements[4]; | |
1173 | if (ty) *ty= elements[5]; | |
1174 | } | |
1175 | ||
1176 | // makes this the inverse matrix | |
1177 | void wxGDIPlusMatrixData::Invert() | |
1178 | { | |
1179 | m_matrix->Invert(); | |
1180 | } | |
1181 | ||
1182 | // returns true if the elements of the transformation matrix are equal ? | |
1183 | bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData* t) const | |
1184 | { | |
1185 | return m_matrix->Equals((Matrix*) t->GetNativeMatrix())== TRUE ; | |
1186 | } | |
1187 | ||
1188 | // return true if this is the identity matrix | |
1189 | bool wxGDIPlusMatrixData::IsIdentity() const | |
1190 | { | |
1191 | return m_matrix->IsIdentity() == TRUE ; | |
1192 | } | |
1193 | ||
1194 | // | |
1195 | // transformation | |
1196 | // | |
1197 | ||
1198 | // add the translation to this matrix | |
1199 | void wxGDIPlusMatrixData::Translate( wxDouble dx , wxDouble dy ) | |
1200 | { | |
1201 | m_matrix->Translate(dx,dy); | |
1202 | } | |
1203 | ||
1204 | // add the scale to this matrix | |
1205 | void wxGDIPlusMatrixData::Scale( wxDouble xScale , wxDouble yScale ) | |
1206 | { | |
1207 | m_matrix->Scale(xScale,yScale); | |
1208 | } | |
1209 | ||
1210 | // add the rotation to this matrix (radians) | |
1211 | void wxGDIPlusMatrixData::Rotate( wxDouble angle ) | |
1212 | { | |
1213 | m_matrix->Rotate( RadToDeg(angle) ); | |
1214 | } | |
1215 | ||
1216 | // | |
1217 | // apply the transforms | |
1218 | // | |
1219 | ||
1220 | // applies that matrix to the point | |
1221 | void wxGDIPlusMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const | |
1222 | { | |
1223 | PointF pt(*x,*y); | |
1224 | m_matrix->TransformPoints(&pt); | |
1225 | *x = pt.X; | |
1226 | *y = pt.Y; | |
1227 | } | |
1228 | ||
1229 | // applies the matrix except for translations | |
1230 | void wxGDIPlusMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const | |
1231 | { | |
1232 | PointF pt(*dx,*dy); | |
1233 | m_matrix->TransformVectors(&pt); | |
1234 | *dx = pt.X; | |
1235 | *dy = pt.Y; | |
1236 | } | |
1237 | ||
1238 | // returns the native representation | |
1239 | void * wxGDIPlusMatrixData::GetNativeMatrix() const | |
1240 | { | |
1241 | return m_matrix; | |
1242 | } | |
1243 | ||
1244 | //----------------------------------------------------------------------------- | |
1245 | // wxGDIPlusContext implementation | |
1246 | //----------------------------------------------------------------------------- | |
1247 | ||
1248 | IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext,wxGraphicsContext) | |
1249 | IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMeasuringContext,wxGDIPlusContext) | |
1250 | ||
1251 | class wxGDIPlusOffsetHelper | |
1252 | { | |
1253 | public : | |
1254 | wxGDIPlusOffsetHelper( Graphics* gr , bool offset ) | |
1255 | { | |
1256 | m_gr = gr; | |
1257 | m_offset = offset; | |
1258 | if ( m_offset ) | |
1259 | m_gr->TranslateTransform( 0.5, 0.5 ); | |
1260 | } | |
1261 | ~wxGDIPlusOffsetHelper( ) | |
1262 | { | |
1263 | if ( m_offset ) | |
1264 | m_gr->TranslateTransform( -0.5, -0.5 ); | |
1265 | } | |
1266 | public : | |
1267 | Graphics* m_gr; | |
1268 | bool m_offset; | |
1269 | } ; | |
1270 | ||
1271 | wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, HDC hdc, wxDouble width, wxDouble height ) | |
1272 | : wxGraphicsContext(renderer) | |
1273 | { | |
1274 | Init(); | |
1275 | m_context = new Graphics( hdc); | |
1276 | m_width = width; | |
1277 | m_height = height; | |
1278 | SetDefaults(); | |
1279 | } | |
1280 | ||
1281 | wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, const wxDC& dc ) | |
1282 | : wxGraphicsContext(renderer) | |
1283 | { | |
1284 | Init(); | |
1285 | ||
1286 | wxMSWDCImpl *msw = wxDynamicCast( dc.GetImpl() , wxMSWDCImpl ); | |
1287 | HDC hdc = (HDC) msw->GetHDC(); | |
1288 | ||
1289 | m_context = new Graphics(hdc); | |
1290 | wxSize sz = dc.GetSize(); | |
1291 | m_width = sz.x; | |
1292 | m_height = sz.y; | |
1293 | ||
1294 | SetDefaults(); | |
1295 | } | |
1296 | ||
1297 | wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, HWND hwnd ) | |
1298 | : wxGraphicsContext(renderer) | |
1299 | { | |
1300 | Init(); | |
1301 | m_enableOffset = true; | |
1302 | m_context = new Graphics( hwnd); | |
1303 | RECT rect = wxGetWindowRect(hwnd); | |
1304 | m_width = rect.right - rect.left; | |
1305 | m_height = rect.bottom - rect.top; | |
1306 | SetDefaults(); | |
1307 | } | |
1308 | ||
1309 | wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, Graphics* gr ) | |
1310 | : wxGraphicsContext(renderer) | |
1311 | { | |
1312 | Init(); | |
1313 | m_context = gr; | |
1314 | SetDefaults(); | |
1315 | } | |
1316 | ||
1317 | wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL) | |
1318 | { | |
1319 | Init(); | |
1320 | } | |
1321 | ||
1322 | void wxGDIPlusContext::Init() | |
1323 | { | |
1324 | m_context = NULL; | |
1325 | m_state1 = 0; | |
1326 | m_state2= 0; | |
1327 | m_height = 0; | |
1328 | m_width = 0; | |
1329 | m_fontScaleRatio = 1.0; | |
1330 | } | |
1331 | ||
1332 | void wxGDIPlusContext::SetDefaults() | |
1333 | { | |
1334 | m_context->SetTextRenderingHint(TextRenderingHintSystemDefault); | |
1335 | m_context->SetPixelOffsetMode(PixelOffsetModeHalf); | |
1336 | m_context->SetSmoothingMode(SmoothingModeHighQuality); | |
1337 | m_state1 = m_context->Save(); | |
1338 | m_state2 = m_context->Save(); | |
1339 | } | |
1340 | ||
1341 | wxGDIPlusContext::~wxGDIPlusContext() | |
1342 | { | |
1343 | if ( m_context ) | |
1344 | { | |
1345 | m_context->Restore( m_state2 ); | |
1346 | m_context->Restore( m_state1 ); | |
1347 | delete m_context; | |
1348 | } | |
1349 | } | |
1350 | ||
1351 | ||
1352 | void wxGDIPlusContext::Clip( const wxRegion ®ion ) | |
1353 | { | |
1354 | Region rgn((HRGN)region.GetHRGN()); | |
1355 | m_context->SetClip(&rgn,CombineModeIntersect); | |
1356 | } | |
1357 | ||
1358 | void wxGDIPlusContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
1359 | { | |
1360 | m_context->SetClip(RectF(x,y,w,h),CombineModeIntersect); | |
1361 | } | |
1362 | ||
1363 | void wxGDIPlusContext::ResetClip() | |
1364 | { | |
1365 | m_context->ResetClip(); | |
1366 | } | |
1367 | ||
1368 | void wxGDIPlusContext::DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
1369 | { | |
1370 | if (m_composition == wxCOMPOSITION_DEST) | |
1371 | return; | |
1372 | ||
1373 | wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() ); | |
1374 | Brush *brush = m_brush.IsNull() ? NULL : ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush(); | |
1375 | Pen *pen = m_pen.IsNull() ? NULL : ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen(); | |
1376 | ||
1377 | if ( brush ) | |
1378 | { | |
1379 | // the offset is used to fill only the inside of the rectangle and not paint underneath | |
1380 | // its border which may influence a transparent Pen | |
1381 | REAL offset = 0; | |
1382 | if ( pen ) | |
1383 | offset = pen->GetWidth(); | |
1384 | m_context->FillRectangle( brush, (REAL)x + offset/2, (REAL)y + offset/2, (REAL)w - offset, (REAL)h - offset); | |
1385 | } | |
1386 | ||
1387 | if ( pen ) | |
1388 | { | |
1389 | m_context->DrawRectangle( pen, (REAL)x, (REAL)y, (REAL)w, (REAL)h ); | |
1390 | } | |
1391 | } | |
1392 | ||
1393 | void wxGDIPlusContext::StrokeLines( size_t n, const wxPoint2DDouble *points) | |
1394 | { | |
1395 | if (m_composition == wxCOMPOSITION_DEST) | |
1396 | return; | |
1397 | ||
1398 | if ( !m_pen.IsNull() ) | |
1399 | { | |
1400 | wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() ); | |
1401 | Point *cpoints = new Point[n]; | |
1402 | for (size_t i = 0; i < n; i++) | |
1403 | { | |
1404 | cpoints[i].X = (int)(points[i].m_x ); | |
1405 | cpoints[i].Y = (int)(points[i].m_y ); | |
1406 | ||
1407 | } // for (size_t i = 0; i < n; i++) | |
1408 | m_context->DrawLines( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , cpoints , n ) ; | |
1409 | delete[] cpoints; | |
1410 | } | |
1411 | } | |
1412 | ||
1413 | void wxGDIPlusContext::DrawLines( size_t n, const wxPoint2DDouble *points, wxPolygonFillMode WXUNUSED(fillStyle) ) | |
1414 | { | |
1415 | if (m_composition == wxCOMPOSITION_DEST) | |
1416 | return; | |
1417 | ||
1418 | wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() ); | |
1419 | Point *cpoints = new Point[n]; | |
1420 | for (size_t i = 0; i < n; i++) | |
1421 | { | |
1422 | cpoints[i].X = (int)(points[i].m_x ); | |
1423 | cpoints[i].Y = (int)(points[i].m_y ); | |
1424 | ||
1425 | } // for (int i = 0; i < n; i++) | |
1426 | if ( !m_brush.IsNull() ) | |
1427 | m_context->FillPolygon( ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush() , cpoints , n ) ; | |
1428 | if ( !m_pen.IsNull() ) | |
1429 | m_context->DrawLines( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , cpoints , n ) ; | |
1430 | delete[] cpoints; | |
1431 | } | |
1432 | ||
1433 | void wxGDIPlusContext::StrokePath( const wxGraphicsPath& path ) | |
1434 | { | |
1435 | if (m_composition == wxCOMPOSITION_DEST) | |
1436 | return; | |
1437 | ||
1438 | if ( !m_pen.IsNull() ) | |
1439 | { | |
1440 | wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() ); | |
1441 | m_context->DrawPath( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath*) path.GetNativePath() ); | |
1442 | } | |
1443 | } | |
1444 | ||
1445 | void wxGDIPlusContext::FillPath( const wxGraphicsPath& path , wxPolygonFillMode fillStyle ) | |
1446 | { | |
1447 | if (m_composition == wxCOMPOSITION_DEST) | |
1448 | return; | |
1449 | ||
1450 | if ( !m_brush.IsNull() ) | |
1451 | { | |
1452 | wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() ); | |
1453 | ((GraphicsPath*) path.GetNativePath())->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding); | |
1454 | m_context->FillPath( ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush() , | |
1455 | (GraphicsPath*) path.GetNativePath()); | |
1456 | } | |
1457 | } | |
1458 | ||
1459 | bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias) | |
1460 | { | |
1461 | if (m_antialias == antialias) | |
1462 | return true; | |
1463 | ||
1464 | m_antialias = antialias; | |
1465 | ||
1466 | SmoothingMode antialiasMode; | |
1467 | switch (antialias) | |
1468 | { | |
1469 | case wxANTIALIAS_DEFAULT: | |
1470 | antialiasMode = SmoothingModeHighQuality; | |
1471 | break; | |
1472 | case wxANTIALIAS_NONE: | |
1473 | antialiasMode = SmoothingModeNone; | |
1474 | break; | |
1475 | default: | |
1476 | return false; | |
1477 | } | |
1478 | m_context->SetSmoothingMode(antialiasMode); | |
1479 | return true; | |
1480 | } | |
1481 | ||
1482 | bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op) | |
1483 | { | |
1484 | if ( m_composition == op ) | |
1485 | return true; | |
1486 | ||
1487 | m_composition = op; | |
1488 | ||
1489 | if (m_composition == wxCOMPOSITION_DEST) | |
1490 | return true; | |
1491 | ||
1492 | CompositingMode cop; | |
1493 | switch (op) | |
1494 | { | |
1495 | case wxCOMPOSITION_SOURCE: | |
1496 | cop = CompositingModeSourceCopy; | |
1497 | break; | |
1498 | case wxCOMPOSITION_OVER: | |
1499 | cop = CompositingModeSourceOver; | |
1500 | break; | |
1501 | default: | |
1502 | return false; | |
1503 | } | |
1504 | ||
1505 | m_context->SetCompositingMode(cop); | |
1506 | return true; | |
1507 | } | |
1508 | ||
1509 | void wxGDIPlusContext::BeginLayer(wxDouble /* opacity */) | |
1510 | { | |
1511 | // TODO | |
1512 | } | |
1513 | ||
1514 | void wxGDIPlusContext::EndLayer() | |
1515 | { | |
1516 | // TODO | |
1517 | } | |
1518 | ||
1519 | void wxGDIPlusContext::Rotate( wxDouble angle ) | |
1520 | { | |
1521 | m_context->RotateTransform( RadToDeg(angle) ); | |
1522 | } | |
1523 | ||
1524 | void wxGDIPlusContext::Translate( wxDouble dx , wxDouble dy ) | |
1525 | { | |
1526 | m_context->TranslateTransform( dx , dy ); | |
1527 | } | |
1528 | ||
1529 | void wxGDIPlusContext::Scale( wxDouble xScale , wxDouble yScale ) | |
1530 | { | |
1531 | m_context->ScaleTransform(xScale,yScale); | |
1532 | } | |
1533 | ||
1534 | void wxGDIPlusContext::PushState() | |
1535 | { | |
1536 | GraphicsState state = m_context->Save(); | |
1537 | m_stateStack.push(state); | |
1538 | } | |
1539 | ||
1540 | void wxGDIPlusContext::PopState() | |
1541 | { | |
1542 | GraphicsState state = m_stateStack.top(); | |
1543 | m_stateStack.pop(); | |
1544 | m_context->Restore(state); | |
1545 | } | |
1546 | ||
1547 | void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
1548 | { | |
1549 | if (m_composition == wxCOMPOSITION_DEST) | |
1550 | return; | |
1551 | ||
1552 | Bitmap* image = static_cast<wxGDIPlusBitmapData*>(bmp.GetRefData())->GetGDIPlusBitmap(); | |
1553 | if ( image ) | |
1554 | { | |
1555 | if( image->GetWidth() != (UINT) w || image->GetHeight() != (UINT) h ) | |
1556 | { | |
1557 | Rect drawRect((REAL) x, (REAL)y, (REAL)w, (REAL)h); | |
1558 | m_context->SetPixelOffsetMode( PixelOffsetModeNone ); | |
1559 | m_context->DrawImage(image, drawRect, 0 , 0 , image->GetWidth(), image->GetHeight(), UnitPixel ) ; | |
1560 | m_context->SetPixelOffsetMode( PixelOffsetModeHalf ); | |
1561 | } | |
1562 | else | |
1563 | m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ; | |
1564 | } | |
1565 | } | |
1566 | ||
1567 | void wxGDIPlusContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
1568 | { | |
1569 | wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp); | |
1570 | DrawBitmap(bitmap, x, y, w, h); | |
1571 | } | |
1572 | ||
1573 | void wxGDIPlusContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
1574 | { | |
1575 | if (m_composition == wxCOMPOSITION_DEST) | |
1576 | return; | |
1577 | ||
1578 | // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only | |
1579 | // find out by looking at the bitmap data whether there really was alpha in it | |
1580 | HICON hIcon = (HICON)icon.GetHICON(); | |
1581 | ICONINFO iconInfo ; | |
1582 | // IconInfo creates the bitmaps for color and mask, we must dispose of them after use | |
1583 | if (!GetIconInfo(hIcon,&iconInfo)) | |
1584 | return; | |
1585 | ||
1586 | Bitmap interim(iconInfo.hbmColor,NULL); | |
1587 | ||
1588 | Bitmap* image = NULL ; | |
1589 | ||
1590 | // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't | |
1591 | // work correctly, asking IsAlphaPixelFormat at this point fails as well | |
1592 | if( GetPixelFormatSize(interim.GetPixelFormat())!= 32 ) | |
1593 | { | |
1594 | image = Bitmap::FromHICON(hIcon); | |
1595 | } | |
1596 | else | |
1597 | { | |
1598 | size_t width = interim.GetWidth(); | |
1599 | size_t height = interim.GetHeight(); | |
1600 | Rect bounds(0,0,width,height); | |
1601 | BitmapData data ; | |
1602 | ||
1603 | interim.LockBits(&bounds, ImageLockModeRead, | |
1604 | interim.GetPixelFormat(),&data); | |
1605 | ||
1606 | bool hasAlpha = false; | |
1607 | for ( size_t y = 0 ; y < height && !hasAlpha ; ++y) | |
1608 | { | |
1609 | for( size_t x = 0 ; x < width && !hasAlpha; ++x) | |
1610 | { | |
1611 | ARGB *dest = (ARGB*)((BYTE*)data.Scan0 + data.Stride*y + x*4); | |
1612 | if ( ( *dest & Color::AlphaMask ) != 0 ) | |
1613 | hasAlpha = true; | |
1614 | } | |
1615 | } | |
1616 | ||
1617 | if ( hasAlpha ) | |
1618 | { | |
1619 | image = new Bitmap(data.Width, data.Height, data.Stride, | |
1620 | PixelFormat32bppARGB , (BYTE*) data.Scan0); | |
1621 | } | |
1622 | else | |
1623 | { | |
1624 | image = Bitmap::FromHICON(hIcon); | |
1625 | } | |
1626 | ||
1627 | interim.UnlockBits(&data); | |
1628 | } | |
1629 | ||
1630 | m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ; | |
1631 | ||
1632 | delete image ; | |
1633 | DeleteObject(iconInfo.hbmColor); | |
1634 | DeleteObject(iconInfo.hbmMask); | |
1635 | } | |
1636 | ||
1637 | wxGraphicsFont wxGDIPlusContext::CreateFont( const wxFont &font, | |
1638 | const wxColour &col ) const | |
1639 | { | |
1640 | wxGDIPlusRenderer* renderer = | |
1641 | static_cast<wxGDIPlusRenderer*>(GetRenderer()); | |
1642 | return renderer->CreateGDIPlusFont(this, font, col); | |
1643 | } | |
1644 | ||
1645 | void wxGDIPlusContext::DoDrawFilledText(const wxString& str, | |
1646 | wxDouble x, wxDouble y, | |
1647 | const wxGraphicsBrush& brush) | |
1648 | { | |
1649 | if (m_composition == wxCOMPOSITION_DEST) | |
1650 | return; | |
1651 | ||
1652 | wxCHECK_RET( !m_font.IsNull(), | |
1653 | wxT("wxGDIPlusContext::DrawText - no valid font set") ); | |
1654 | ||
1655 | if ( str.IsEmpty()) | |
1656 | return ; | |
1657 | ||
1658 | wxGDIPlusFontData * const | |
1659 | fontData = (wxGDIPlusFontData *)m_font.GetRefData(); | |
1660 | wxGDIPlusBrushData * const | |
1661 | brushData = (wxGDIPlusBrushData *)brush.GetRefData(); | |
1662 | ||
1663 | m_context->DrawString | |
1664 | ( | |
1665 | str.wc_str(*wxConvUI), // string to draw, always Unicode | |
1666 | -1, // length: string is NUL-terminated | |
1667 | fontData->GetGDIPlusFont(), | |
1668 | PointF(x, y), | |
1669 | StringFormat::GenericTypographic(), | |
1670 | brushData ? brushData->GetGDIPlusBrush() | |
1671 | : fontData->GetGDIPlusBrush() | |
1672 | ); | |
1673 | } | |
1674 | ||
1675 | void wxGDIPlusContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height, | |
1676 | wxDouble *descent, wxDouble *externalLeading ) const | |
1677 | { | |
1678 | wxCHECK_RET( !m_font.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") ); | |
1679 | ||
1680 | wxWCharBuffer s = str.wc_str( *wxConvUI ); | |
1681 | FontFamily ffamily ; | |
1682 | Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont(); | |
1683 | ||
1684 | f->GetFamily(&ffamily) ; | |
1685 | ||
1686 | REAL factorY = m_fontScaleRatio; | |
1687 | ||
1688 | REAL rDescent = ffamily.GetCellDescent(FontStyleRegular) * | |
1689 | f->GetSize() / ffamily.GetEmHeight(FontStyleRegular); | |
1690 | REAL rAscent = ffamily.GetCellAscent(FontStyleRegular) * | |
1691 | f->GetSize() / ffamily.GetEmHeight(FontStyleRegular); | |
1692 | REAL rHeight = ffamily.GetLineSpacing(FontStyleRegular) * | |
1693 | f->GetSize() / ffamily.GetEmHeight(FontStyleRegular); | |
1694 | ||
1695 | if ( height ) | |
1696 | *height = rHeight * factorY; | |
1697 | if ( descent ) | |
1698 | *descent = rDescent * factorY; | |
1699 | if ( externalLeading ) | |
1700 | *externalLeading = (rHeight - rAscent - rDescent) * factorY; | |
1701 | // measuring empty strings is not guaranteed, so do it by hand | |
1702 | if ( str.IsEmpty()) | |
1703 | { | |
1704 | if ( width ) | |
1705 | *width = 0 ; | |
1706 | } | |
1707 | else | |
1708 | { | |
1709 | RectF layoutRect(0,0, 100000.0f, 100000.0f); | |
1710 | StringFormat strFormat( StringFormat::GenericTypographic() ); | |
1711 | strFormat.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces | strFormat.GetFormatFlags() ); | |
1712 | ||
1713 | RectF bounds ; | |
1714 | m_context->MeasureString((const wchar_t *) s , wcslen(s) , f, layoutRect, &strFormat, &bounds ) ; | |
1715 | if ( width ) | |
1716 | *width = bounds.Width; | |
1717 | if ( height ) | |
1718 | *height = bounds.Height; | |
1719 | } | |
1720 | } | |
1721 | ||
1722 | void wxGDIPlusContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const | |
1723 | { | |
1724 | widths.Empty(); | |
1725 | widths.Add(0, text.length()); | |
1726 | ||
1727 | wxCHECK_RET( !m_font.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") ); | |
1728 | ||
1729 | if (text.empty()) | |
1730 | return; | |
1731 | ||
1732 | Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont(); | |
1733 | wxWCharBuffer ws = text.wc_str( *wxConvUI ); | |
1734 | size_t len = wcslen( ws ) ; | |
1735 | wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations")); | |
1736 | ||
1737 | RectF layoutRect(0,0, 100000.0f, 100000.0f); | |
1738 | StringFormat strFormat( StringFormat::GenericTypographic() ); | |
1739 | ||
1740 | size_t startPosition = 0; | |
1741 | size_t remainder = len; | |
1742 | const size_t maxSpan = 32; | |
1743 | CharacterRange* ranges = new CharacterRange[maxSpan] ; | |
1744 | Region* regions = new Region[maxSpan]; | |
1745 | ||
1746 | while( remainder > 0 ) | |
1747 | { | |
1748 | size_t span = wxMin( maxSpan, remainder ); | |
1749 | ||
1750 | for( size_t i = 0 ; i < span ; ++i) | |
1751 | { | |
1752 | ranges[i].First = 0 ; | |
1753 | ranges[i].Length = startPosition+i+1 ; | |
1754 | } | |
1755 | strFormat.SetMeasurableCharacterRanges(span,ranges); | |
1756 | strFormat.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces | strFormat.GetFormatFlags() ); | |
1757 | m_context->MeasureCharacterRanges(ws, -1 , f,layoutRect, &strFormat,span,regions) ; | |
1758 | ||
1759 | RectF bbox ; | |
1760 | for ( size_t i = 0 ; i < span ; ++i) | |
1761 | { | |
1762 | regions[i].GetBounds(&bbox,m_context); | |
1763 | widths[startPosition+i] = bbox.Width; | |
1764 | } | |
1765 | remainder -= span; | |
1766 | startPosition += span; | |
1767 | } | |
1768 | ||
1769 | delete[] ranges; | |
1770 | delete[] regions; | |
1771 | } | |
1772 | ||
1773 | bool wxGDIPlusContext::ShouldOffset() const | |
1774 | { | |
1775 | if ( !m_enableOffset ) | |
1776 | return false; | |
1777 | ||
1778 | int penwidth = 0 ; | |
1779 | if ( !m_pen.IsNull() ) | |
1780 | { | |
1781 | penwidth = (int)((wxGDIPlusPenData*)m_pen.GetRefData())->GetWidth(); | |
1782 | if ( penwidth == 0 ) | |
1783 | penwidth = 1; | |
1784 | } | |
1785 | return ( penwidth % 2 ) == 1; | |
1786 | } | |
1787 | ||
1788 | void* wxGDIPlusContext::GetNativeContext() | |
1789 | { | |
1790 | return m_context; | |
1791 | } | |
1792 | ||
1793 | // concatenates this transform with the current transform of this context | |
1794 | void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix& matrix ) | |
1795 | { | |
1796 | m_context->MultiplyTransform((Matrix*) matrix.GetNativeMatrix()); | |
1797 | } | |
1798 | ||
1799 | // sets the transform of this context | |
1800 | void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix& matrix ) | |
1801 | { | |
1802 | m_context->SetTransform((Matrix*) matrix.GetNativeMatrix()); | |
1803 | } | |
1804 | ||
1805 | // gets the matrix of this context | |
1806 | wxGraphicsMatrix wxGDIPlusContext::GetTransform() const | |
1807 | { | |
1808 | wxGraphicsMatrix matrix = CreateMatrix(); | |
1809 | m_context->GetTransform((Matrix*) matrix.GetNativeMatrix()); | |
1810 | return matrix; | |
1811 | } | |
1812 | ||
1813 | void wxGDIPlusContext::GetSize( wxDouble* width, wxDouble *height ) | |
1814 | { | |
1815 | *width = m_width; | |
1816 | *height = m_height; | |
1817 | } | |
1818 | ||
1819 | //----------------------------------------------------------------------------- | |
1820 | // wxGDIPlusPrintingContext implementation | |
1821 | //----------------------------------------------------------------------------- | |
1822 | ||
1823 | wxGDIPlusPrintingContext::wxGDIPlusPrintingContext( wxGraphicsRenderer* renderer, | |
1824 | const wxDC& dc ) | |
1825 | : wxGDIPlusContext(renderer, dc) | |
1826 | { | |
1827 | Graphics* context = GetGraphics(); | |
1828 | ||
1829 | //m_context->SetPageUnit(UnitDocument); | |
1830 | ||
1831 | // Setup page scale, based on DPI ratio. | |
1832 | // Antecedent should be 100dpi when the default page unit | |
1833 | // (UnitDisplay) is used. Page unit UnitDocument would require 300dpi | |
1834 | // instead. Note that calling SetPageScale() does not have effect on | |
1835 | // non-printing DCs (that is, any other than wxPrinterDC or | |
1836 | // wxEnhMetaFileDC). | |
1837 | REAL dpiRatio = 100.0 / context->GetDpiY(); | |
1838 | context->SetPageScale(dpiRatio); | |
1839 | ||
1840 | // We use this modifier when measuring fonts. It is needed because the | |
1841 | // page scale is modified above. | |
1842 | m_fontScaleRatio = context->GetDpiY() / 72.0; | |
1843 | } | |
1844 | ||
1845 | //----------------------------------------------------------------------------- | |
1846 | // wxGDIPlusRenderer implementation | |
1847 | //----------------------------------------------------------------------------- | |
1848 | ||
1849 | IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer,wxGraphicsRenderer) | |
1850 | ||
1851 | static wxGDIPlusRenderer gs_GDIPlusRenderer; | |
1852 | ||
1853 | wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer() | |
1854 | { | |
1855 | return &gs_GDIPlusRenderer; | |
1856 | } | |
1857 | ||
1858 | bool wxGDIPlusRenderer::EnsureIsLoaded() | |
1859 | { | |
1860 | // load gdiplus.dll if not yet loaded, but don't bother doing it again | |
1861 | // if we already tried and failed (we don't want to spend lot of time | |
1862 | // returning NULL from wxGraphicsContext::Create(), which may be called | |
1863 | // relatively frequently): | |
1864 | if ( m_loaded == -1 ) | |
1865 | { | |
1866 | Load(); | |
1867 | } | |
1868 | ||
1869 | return m_loaded == 1; | |
1870 | } | |
1871 | ||
1872 | // call EnsureIsLoaded() and return returnOnFail value if it fails | |
1873 | #define ENSURE_LOADED_OR_RETURN(returnOnFail) \ | |
1874 | if ( !EnsureIsLoaded() ) \ | |
1875 | return (returnOnFail) | |
1876 | ||
1877 | ||
1878 | void wxGDIPlusRenderer::Load() | |
1879 | { | |
1880 | GdiplusStartupInput input; | |
1881 | GdiplusStartupOutput output; | |
1882 | if ( GdiplusStartup(&m_gditoken,&input,&output) == Gdiplus::Ok ) | |
1883 | { | |
1884 | wxLogTrace("gdiplus", "successfully initialized GDI+"); | |
1885 | m_loaded = 1; | |
1886 | } | |
1887 | else | |
1888 | { | |
1889 | wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?"); | |
1890 | m_loaded = 0; | |
1891 | } | |
1892 | } | |
1893 | ||
1894 | void wxGDIPlusRenderer::Unload() | |
1895 | { | |
1896 | if ( m_gditoken ) | |
1897 | { | |
1898 | GdiplusShutdown(m_gditoken); | |
1899 | m_gditoken = NULL; | |
1900 | } | |
1901 | m_loaded = -1; // next Load() will try again | |
1902 | } | |
1903 | ||
1904 | wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxWindowDC& dc) | |
1905 | { | |
1906 | ENSURE_LOADED_OR_RETURN(NULL); | |
1907 | wxGDIPlusContext* context = new wxGDIPlusContext(this, dc); | |
1908 | context->EnableOffset(true); | |
1909 | return context; | |
1910 | } | |
1911 | ||
1912 | #if wxUSE_PRINTING_ARCHITECTURE | |
1913 | wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxPrinterDC& dc) | |
1914 | { | |
1915 | ENSURE_LOADED_OR_RETURN(NULL); | |
1916 | wxGDIPlusContext* context = new wxGDIPlusPrintingContext(this, dc); | |
1917 | return context; | |
1918 | } | |
1919 | #endif | |
1920 | ||
1921 | #if wxUSE_ENH_METAFILE | |
1922 | wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxEnhMetaFileDC& dc) | |
1923 | { | |
1924 | ENSURE_LOADED_OR_RETURN(NULL); | |
1925 | wxGDIPlusContext* context = new wxGDIPlusPrintingContext(this, dc); | |
1926 | return context; | |
1927 | } | |
1928 | #endif | |
1929 | ||
1930 | wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxMemoryDC& dc) | |
1931 | { | |
1932 | ENSURE_LOADED_OR_RETURN(NULL); | |
1933 | wxGDIPlusContext* context = new wxGDIPlusContext(this, dc); | |
1934 | context->EnableOffset(true); | |
1935 | return context; | |
1936 | } | |
1937 | ||
1938 | wxGraphicsContext * wxGDIPlusRenderer::CreateMeasuringContext() | |
1939 | { | |
1940 | ENSURE_LOADED_OR_RETURN(NULL); | |
1941 | return new wxGDIPlusMeasuringContext(this); | |
1942 | } | |
1943 | ||
1944 | wxGraphicsContext * wxGDIPlusRenderer::CreateContextFromNativeContext( void * context ) | |
1945 | { | |
1946 | ENSURE_LOADED_OR_RETURN(NULL); | |
1947 | return new wxGDIPlusContext(this,(Graphics*) context); | |
1948 | } | |
1949 | ||
1950 | ||
1951 | wxGraphicsContext * wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window ) | |
1952 | { | |
1953 | ENSURE_LOADED_OR_RETURN(NULL); | |
1954 | return new wxGDIPlusContext(this,(HWND) window); | |
1955 | } | |
1956 | ||
1957 | wxGraphicsContext * wxGDIPlusRenderer::CreateContext( wxWindow* window ) | |
1958 | { | |
1959 | ENSURE_LOADED_OR_RETURN(NULL); | |
1960 | return new wxGDIPlusContext(this, (HWND) window->GetHWND() ); | |
1961 | } | |
1962 | ||
1963 | // Path | |
1964 | ||
1965 | wxGraphicsPath wxGDIPlusRenderer::CreatePath() | |
1966 | { | |
1967 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath); | |
1968 | wxGraphicsPath m; | |
1969 | m.SetRefData( new wxGDIPlusPathData(this)); | |
1970 | return m; | |
1971 | } | |
1972 | ||
1973 | ||
1974 | // Matrix | |
1975 | ||
1976 | wxGraphicsMatrix wxGDIPlusRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d, | |
1977 | wxDouble tx, wxDouble ty) | |
1978 | ||
1979 | { | |
1980 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix); | |
1981 | wxGraphicsMatrix m; | |
1982 | wxGDIPlusMatrixData* data = new wxGDIPlusMatrixData( this ); | |
1983 | data->Set( a,b,c,d,tx,ty ) ; | |
1984 | m.SetRefData(data); | |
1985 | return m; | |
1986 | } | |
1987 | ||
1988 | wxGraphicsPen wxGDIPlusRenderer::CreatePen(const wxPen& pen) | |
1989 | { | |
1990 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen); | |
1991 | if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT ) | |
1992 | return wxNullGraphicsPen; | |
1993 | else | |
1994 | { | |
1995 | wxGraphicsPen p; | |
1996 | p.SetRefData(new wxGDIPlusPenData( this, pen )); | |
1997 | return p; | |
1998 | } | |
1999 | } | |
2000 | ||
2001 | wxGraphicsBrush wxGDIPlusRenderer::CreateBrush(const wxBrush& brush ) | |
2002 | { | |
2003 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush); | |
2004 | if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT ) | |
2005 | return wxNullGraphicsBrush; | |
2006 | else | |
2007 | { | |
2008 | wxGraphicsBrush p; | |
2009 | p.SetRefData(new wxGDIPlusBrushData( this, brush )); | |
2010 | return p; | |
2011 | } | |
2012 | } | |
2013 | ||
2014 | wxGraphicsBrush | |
2015 | wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1, | |
2016 | wxDouble x2, wxDouble y2, | |
2017 | const wxGraphicsGradientStops& stops) | |
2018 | { | |
2019 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush); | |
2020 | wxGraphicsBrush p; | |
2021 | wxGDIPlusBrushData* d = new wxGDIPlusBrushData( this ); | |
2022 | d->CreateLinearGradientBrush(x1, y1, x2, y2, stops); | |
2023 | p.SetRefData(d); | |
2024 | return p; | |
2025 | } | |
2026 | ||
2027 | wxGraphicsBrush | |
2028 | wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo, | |
2029 | wxDouble xc, wxDouble yc, | |
2030 | wxDouble radius, | |
2031 | const wxGraphicsGradientStops& stops) | |
2032 | { | |
2033 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush); | |
2034 | wxGraphicsBrush p; | |
2035 | wxGDIPlusBrushData* d = new wxGDIPlusBrushData( this ); | |
2036 | d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,stops); | |
2037 | p.SetRefData(d); | |
2038 | return p; | |
2039 | } | |
2040 | ||
2041 | wxGraphicsFont | |
2042 | wxGDIPlusRenderer::CreateGDIPlusFont( const wxGDIPlusContext* gc, | |
2043 | const wxFont &font, | |
2044 | const wxColour &col ) | |
2045 | { | |
2046 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont); | |
2047 | if ( font.Ok() ) | |
2048 | { | |
2049 | wxGraphicsFont p; | |
2050 | p.SetRefData(new wxGDIPlusFontData( this, gc, font, col )); | |
2051 | return p; | |
2052 | } | |
2053 | else | |
2054 | return wxNullGraphicsFont; | |
2055 | } | |
2056 | ||
2057 | wxGraphicsBitmap wxGDIPlusRenderer::CreateBitmap( const wxBitmap &bitmap ) | |
2058 | { | |
2059 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap); | |
2060 | if ( bitmap.Ok() ) | |
2061 | { | |
2062 | wxGraphicsBitmap p; | |
2063 | p.SetRefData(new wxGDIPlusBitmapData( this , bitmap )); | |
2064 | return p; | |
2065 | } | |
2066 | else | |
2067 | return wxNullGraphicsBitmap; | |
2068 | } | |
2069 | ||
2070 | wxGraphicsBitmap wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap ) | |
2071 | { | |
2072 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap); | |
2073 | if ( bitmap != NULL ) | |
2074 | { | |
2075 | wxGraphicsBitmap p; | |
2076 | p.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap*) bitmap )); | |
2077 | return p; | |
2078 | } | |
2079 | else | |
2080 | return wxNullGraphicsBitmap; | |
2081 | } | |
2082 | ||
2083 | wxGraphicsBitmap wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
2084 | { | |
2085 | ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap); | |
2086 | Bitmap* image = static_cast<wxGDIPlusBitmapData*>(bitmap.GetRefData())->GetGDIPlusBitmap(); | |
2087 | if ( image ) | |
2088 | { | |
2089 | wxGraphicsBitmap p; | |
2090 | p.SetRefData(new wxGDIPlusBitmapData( this , image->Clone( (REAL) x , (REAL) y , (REAL) w , (REAL) h , PixelFormat32bppPARGB) )); | |
2091 | return p; | |
2092 | } | |
2093 | else | |
2094 | return wxNullGraphicsBitmap; | |
2095 | } | |
2096 | ||
2097 | // Shutdown GDI+ at app exit, before possible dll unload | |
2098 | class wxGDIPlusRendererModule : public wxModule | |
2099 | { | |
2100 | public: | |
2101 | virtual bool OnInit() { return true; } | |
2102 | virtual void OnExit() { gs_GDIPlusRenderer.Unload(); } | |
2103 | ||
2104 | private: | |
2105 | DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule) | |
2106 | }; | |
2107 | ||
2108 | IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule, wxModule) | |
2109 | ||
2110 | // ---------------------------------------------------------------------------- | |
2111 | // wxMSW-specific parts of wxGCDC | |
2112 | // ---------------------------------------------------------------------------- | |
2113 | ||
2114 | WXHDC wxGCDC::AcquireHDC() | |
2115 | { | |
2116 | wxGraphicsContext * const gc = GetGraphicsContext(); | |
2117 | if ( !gc ) | |
2118 | return NULL; | |
2119 | ||
2120 | Graphics * const g = static_cast<Graphics *>(gc->GetNativeContext()); | |
2121 | return g ? g->GetHDC() : NULL; | |
2122 | } | |
2123 | ||
2124 | void wxGCDC::ReleaseHDC(WXHDC hdc) | |
2125 | { | |
2126 | if ( !hdc ) | |
2127 | return; | |
2128 | ||
2129 | wxGraphicsContext * const gc = GetGraphicsContext(); | |
2130 | wxCHECK_RET( gc, "can't release HDC because there is no wxGraphicsContext" ); | |
2131 | ||
2132 | Graphics * const g = static_cast<Graphics *>(gc->GetNativeContext()); | |
2133 | wxCHECK_RET( g, "can't release HDC because there is no Graphics" ); | |
2134 | ||
2135 | g->ReleaseHDC((HDC)hdc); | |
2136 | } | |
2137 | ||
2138 | #endif // wxUSE_GRAPHICS_CONTEXT |