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