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