]>
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 | ||
14 | #include "wx/dc.h" | |
15 | ||
6fea499c SC |
16 | #ifdef __BORLANDC__ |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #ifndef WX_PRECOMP | |
21 | #include "wx/msw/wrapcdlg.h" | |
22 | #include "wx/image.h" | |
23 | #include "wx/window.h" | |
24 | #include "wx/dc.h" | |
25 | #include "wx/utils.h" | |
26 | #include "wx/dialog.h" | |
27 | #include "wx/app.h" | |
28 | #include "wx/bitmap.h" | |
29 | #include "wx/dcmemory.h" | |
30 | #include "wx/log.h" | |
31 | #include "wx/icon.h" | |
32 | #include "wx/dcprint.h" | |
33 | #include "wx/module.h" | |
34 | #endif | |
35 | ||
36 | #include "wx/graphics.h" | |
37 | ||
7ba86d93 RD |
38 | #if wxUSE_GRAPHICS_CONTEXT |
39 | ||
6fea499c SC |
40 | #include <vector> |
41 | ||
42 | using namespace std; | |
43 | ||
44 | //----------------------------------------------------------------------------- | |
45 | // constants | |
46 | //----------------------------------------------------------------------------- | |
47 | ||
48 | const double RAD2DEG = 180.0 / M_PI; | |
49 | ||
50 | //----------------------------------------------------------------------------- | |
51 | // Local functions | |
52 | //----------------------------------------------------------------------------- | |
53 | ||
54 | static inline double dmin(double a, double b) { return a < b ? a : b; } | |
55 | static inline double dmax(double a, double b) { return a > b ? a : b; } | |
56 | ||
57 | static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; } | |
58 | static inline double RadToDeg(double deg) { return (deg * 180.0) / M_PI; } | |
59 | ||
60 | //----------------------------------------------------------------------------- | |
61 | // device context implementation | |
62 | // | |
63 | // more and more of the dc functionality should be implemented by calling | |
64 | // the appropricate wxGDIPlusContext, but we will have to do that step by step | |
65 | // also coordinate conversions should be moved to native matrix ops | |
66 | //----------------------------------------------------------------------------- | |
67 | ||
68 | // we always stock two context states, one at entry, to be able to preserve the | |
69 | // state we were called with, the other one after changing to HI Graphics orientation | |
70 | // (this one is used for getting back clippings etc) | |
71 | ||
72 | //----------------------------------------------------------------------------- | |
73 | // wxGraphicsPath implementation | |
74 | //----------------------------------------------------------------------------- | |
75 | ||
76 | #include "wx/msw/private.h" // needs to be before #include <commdlg.h> | |
77 | ||
78 | #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__) | |
79 | #include <commdlg.h> | |
80 | #endif | |
81 | ||
82 | // TODO remove this dependency (gdiplus needs the macros) | |
83 | ||
84 | #ifndef max | |
85 | #define max(a,b) (((a) > (b)) ? (a) : (b)) | |
86 | #endif | |
87 | ||
88 | #ifndef min | |
89 | #define min(a,b) (((a) < (b)) ? (a) : (b)) | |
90 | #endif | |
91 | ||
92 | #include "gdiplus.h" | |
93 | using namespace Gdiplus; | |
94 | ||
95 | class GDILoader | |
96 | { | |
97 | public : | |
98 | GDILoader() | |
99 | { | |
100 | m_loaded = false; | |
101 | m_gditoken = NULL; | |
102 | } | |
103 | ||
104 | ~GDILoader() | |
105 | { | |
106 | if (m_loaded) | |
107 | { | |
108 | Unload(); | |
109 | } | |
110 | } | |
111 | void EnsureIsLoaded() | |
112 | { | |
113 | if (!m_loaded) | |
114 | { | |
115 | Load(); | |
116 | } | |
117 | } | |
118 | void Load() | |
119 | { | |
120 | GdiplusStartupInput input; | |
121 | GdiplusStartupOutput output; | |
122 | GdiplusStartup(&m_gditoken,&input,&output); | |
123 | m_loaded = true; | |
124 | } | |
125 | void Unload() | |
126 | { | |
127 | if ( m_gditoken ) | |
128 | GdiplusShutdown(m_gditoken); | |
129 | } | |
130 | private : | |
131 | bool m_loaded; | |
132 | DWORD m_gditoken; | |
133 | ||
134 | }; | |
135 | ||
136 | static GDILoader gGDILoader; | |
137 | ||
138 | class WXDLLEXPORT wxGDIPlusPath : public wxGraphicsPath | |
139 | { | |
140 | DECLARE_NO_COPY_CLASS(wxGDIPlusPath) | |
141 | public : | |
142 | wxGDIPlusPath(); | |
143 | ~wxGDIPlusPath(); | |
144 | ||
145 | ||
146 | // | |
147 | // These are the path primitives from which everything else can be constructed | |
148 | // | |
149 | ||
150 | // begins a new subpath at (x,y) | |
151 | virtual void MoveToPoint( wxDouble x, wxDouble y ); | |
152 | ||
153 | // adds a straight line from the current point to (x,y) | |
154 | virtual void AddLineToPoint( wxDouble x, wxDouble y ); | |
155 | ||
156 | // adds a cubic Bezier curve from the current point, using two control points and an end point | |
157 | virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y ); | |
158 | ||
159 | ||
160 | // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle | |
161 | virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ; | |
162 | ||
163 | // gets the last point of the current path, (0,0) if not yet set | |
164 | virtual void GetCurrentPoint( wxDouble& x, wxDouble&y) ; | |
165 | ||
166 | // closes the current sub-path | |
167 | virtual void CloseSubpath(); | |
168 | ||
169 | // | |
170 | // These are convenience functions which - if not available natively will be assembled | |
171 | // using the primitives from above | |
172 | // | |
173 | ||
174 | // appends a rectangle as a new closed subpath | |
175 | virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ; | |
176 | /* | |
177 | ||
178 | // appends an ellipsis as a new closed subpath fitting the passed rectangle | |
179 | virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ; | |
180 | ||
181 | // 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) | |
182 | virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ; | |
183 | */ | |
184 | ||
185 | GraphicsPath* GetPath() const; | |
186 | private : | |
187 | GraphicsPath* m_path; | |
188 | }; | |
189 | ||
190 | class WXDLLEXPORT wxGDIPlusContext : public wxGraphicsContext | |
191 | { | |
192 | DECLARE_NO_COPY_CLASS(wxGDIPlusContext) | |
193 | ||
194 | public: | |
195 | wxGDIPlusContext( WXHDC hdc ); | |
196 | wxGDIPlusContext(); | |
197 | virtual ~wxGDIPlusContext(); | |
198 | ||
199 | virtual void Clip( const wxRegion ®ion ); | |
539e2795 SC |
200 | // clips drawings to the rect |
201 | virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
202 | ||
203 | // resets the clipping to original extent | |
204 | virtual void ResetClip(); | |
205 | ||
206 | virtual void * GetNativeContext(); | |
207 | ||
6fea499c SC |
208 | virtual void StrokePath( const wxGraphicsPath *p ); |
209 | virtual void FillPath( const wxGraphicsPath *p , int fillStyle = wxWINDING_RULE ); | |
210 | ||
211 | virtual wxGraphicsPath* CreatePath(); | |
212 | virtual void SetPen( const wxPen &pen ); | |
213 | virtual void SetBrush( const wxBrush &brush ); | |
214 | virtual void SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxColour&c1, const wxColour&c2) ; | |
215 | virtual void SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius, | |
216 | const wxColour &oColor, const wxColour &cColor); | |
217 | ||
218 | virtual void Translate( wxDouble dx , wxDouble dy ); | |
219 | virtual void Scale( wxDouble xScale , wxDouble yScale ); | |
220 | virtual void Rotate( wxDouble angle ); | |
221 | ||
222 | virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
223 | virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
224 | virtual void PushState(); | |
225 | virtual void PopState(); | |
226 | ||
227 | virtual void SetFont( const wxFont &font ); | |
228 | virtual void SetTextColor( const wxColour &col ); | |
229 | virtual void DrawText( const wxString &str, wxDouble x, wxDouble y); | |
230 | virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height, | |
231 | wxDouble *descent, wxDouble *externalLeading ) const; | |
232 | virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const; | |
233 | ||
234 | private: | |
235 | Graphics* m_context; | |
236 | vector<GraphicsState> m_stateStack; | |
237 | GraphicsState m_state1; | |
238 | GraphicsState m_state2; | |
239 | ||
240 | Pen* m_pen; | |
241 | bool m_penTransparent; | |
242 | Image* m_penImage; | |
243 | Brush* m_penBrush; | |
244 | ||
245 | Brush* m_brush; | |
246 | bool m_brushTransparent; | |
247 | Image* m_brushImage; | |
248 | GraphicsPath* m_brushPath; | |
249 | ||
250 | Brush* m_textBrush; | |
251 | Font* m_font; | |
252 | // wxPen m_pen; | |
253 | // wxBrush m_brush; | |
254 | }; | |
255 | ||
256 | wxGDIPlusPath::wxGDIPlusPath() | |
257 | { | |
258 | m_path = new GraphicsPath(); | |
259 | } | |
260 | ||
261 | wxGDIPlusPath::~wxGDIPlusPath() | |
262 | { | |
263 | delete m_path; | |
264 | } | |
265 | ||
266 | GraphicsPath* wxGDIPlusPath::GetPath() const | |
267 | { | |
268 | return m_path; | |
269 | } | |
270 | ||
271 | // | |
272 | // The Primitives | |
273 | // | |
274 | ||
275 | void wxGDIPlusPath::MoveToPoint( wxDouble x , wxDouble y ) | |
276 | { | |
277 | m_path->StartFigure(); | |
278 | m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y); | |
279 | } | |
280 | ||
281 | void wxGDIPlusPath::AddLineToPoint( wxDouble x , wxDouble y ) | |
282 | { | |
283 | m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y); | |
284 | } | |
285 | ||
286 | void wxGDIPlusPath::CloseSubpath() | |
287 | { | |
288 | m_path->CloseFigure(); | |
289 | } | |
290 | ||
291 | void wxGDIPlusPath::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y ) | |
292 | { | |
293 | PointF c1(cx1,cy1); | |
294 | PointF c2(cx2,cy2); | |
295 | PointF end(x,y); | |
296 | PointF start; | |
297 | m_path->GetLastPoint(&start); | |
298 | m_path->AddBezier(start,c1,c2,end); | |
299 | } | |
300 | ||
301 | // gets the last point of the current path, (0,0) if not yet set | |
302 | void wxGDIPlusPath::GetCurrentPoint( wxDouble& x, wxDouble&y) | |
303 | { | |
304 | PointF start; | |
305 | m_path->GetLastPoint(&start); | |
306 | x = start.X ; | |
307 | y = start.Y ; | |
308 | } | |
309 | ||
310 | void wxGDIPlusPath::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise ) | |
311 | { | |
312 | double sweepAngle = endAngle - startAngle ; | |
313 | if( abs(sweepAngle) >= 2*M_PI) | |
314 | { | |
315 | sweepAngle = 2 * M_PI; | |
316 | } | |
317 | else | |
318 | { | |
319 | if ( clockwise ) | |
320 | { | |
321 | if( sweepAngle < 0 ) | |
322 | sweepAngle += 2 * M_PI; | |
323 | } | |
324 | else | |
325 | { | |
326 | if( sweepAngle > 0 ) | |
327 | sweepAngle -= 2 * M_PI; | |
328 | ||
329 | } | |
330 | } | |
331 | m_path->AddArc((REAL) (x-r),(REAL) (y-r),(REAL) (2*r),(REAL) (2*r),RadToDeg(startAngle),RadToDeg(sweepAngle)); | |
332 | } | |
333 | ||
334 | void wxGDIPlusPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
335 | { | |
336 | m_path->AddRectangle(RectF(x,y,w,h)); | |
337 | } | |
338 | ||
339 | // | |
340 | // | |
341 | // | |
342 | /* | |
343 | // closes the current subpath | |
344 | void wxGDIPlusPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) | |
345 | { | |
346 | // CGPathAddArcToPoint( m_path, NULL , x1, y1, x2, y2, r); | |
347 | } | |
348 | ||
349 | */ | |
350 | ||
351 | //----------------------------------------------------------------------------- | |
352 | // wxGDIPlusContext implementation | |
353 | //----------------------------------------------------------------------------- | |
354 | ||
355 | wxGDIPlusContext::wxGDIPlusContext( WXHDC hdc ) | |
356 | { | |
357 | gGDILoader.EnsureIsLoaded(); | |
358 | m_context = new Graphics( (HDC) hdc); | |
f868f886 | 359 | m_context->SetSmoothingMode(SmoothingModeHighQuality); |
6fea499c SC |
360 | m_state1 = m_context->Save(); |
361 | m_state2 = m_context->Save(); | |
362 | ||
363 | // set defaults | |
364 | ||
365 | m_penTransparent = false; | |
366 | m_pen = new Pen((ARGB)Color::Black); | |
367 | m_penImage = NULL; | |
368 | m_penBrush = NULL; | |
369 | ||
370 | m_brushTransparent = false; | |
371 | m_brush = new SolidBrush((ARGB)Color::White); | |
372 | m_brushImage = NULL; | |
373 | m_brushPath = NULL; | |
374 | m_textBrush = new SolidBrush((ARGB)Color::Black); | |
375 | m_font = new Font( L"Arial" , 9 , FontStyleRegular ); | |
376 | } | |
377 | ||
378 | wxGDIPlusContext::~wxGDIPlusContext() | |
379 | { | |
380 | if ( m_context ) | |
381 | { | |
382 | m_context->Restore( m_state2 ); | |
383 | m_context->Restore( m_state1 ); | |
384 | delete m_context; | |
385 | delete m_pen; | |
386 | delete m_penImage; | |
387 | delete m_penBrush; | |
388 | delete m_brush; | |
389 | delete m_brushImage; | |
390 | delete m_brushPath; | |
391 | delete m_textBrush; | |
392 | delete m_font; | |
393 | } | |
394 | } | |
395 | ||
396 | ||
397 | void wxGDIPlusContext::Clip( const wxRegion & WXUNUSED(region) ) | |
398 | { | |
539e2795 SC |
399 | // TODO |
400 | } | |
401 | ||
402 | void wxGDIPlusContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
403 | { | |
404 | // TODO | |
405 | } | |
406 | ||
407 | void wxGDIPlusContext::ResetClip() | |
408 | { | |
409 | // TODO | |
6fea499c SC |
410 | } |
411 | ||
412 | void wxGDIPlusContext::StrokePath( const wxGraphicsPath *p ) | |
413 | { | |
414 | if ( m_penTransparent ) | |
415 | return; | |
416 | ||
417 | const wxGDIPlusPath* path = dynamic_cast< const wxGDIPlusPath*>( p ); | |
418 | m_context->DrawPath( m_pen , path->GetPath() ); | |
419 | } | |
420 | ||
421 | void wxGDIPlusContext::FillPath( const wxGraphicsPath *p , int fillStyle ) | |
422 | { | |
423 | if ( !m_brushTransparent ) | |
424 | { | |
425 | const wxGDIPlusPath* path = dynamic_cast< const wxGDIPlusPath*>( p ); | |
426 | path->GetPath()->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding); | |
427 | m_context->FillPath( m_brush , path->GetPath() ); | |
428 | } | |
429 | } | |
430 | ||
431 | wxGraphicsPath* wxGDIPlusContext::CreatePath() | |
432 | { | |
433 | return new wxGDIPlusPath(); | |
434 | } | |
435 | ||
436 | void wxGDIPlusContext::Rotate( wxDouble angle ) | |
437 | { | |
ebbe5c66 | 438 | m_context->RotateTransform( RadToDeg(angle) ); |
6fea499c SC |
439 | } |
440 | ||
441 | void wxGDIPlusContext::Translate( wxDouble dx , wxDouble dy ) | |
442 | { | |
443 | m_context->TranslateTransform( dx , dy ); | |
444 | } | |
445 | ||
446 | void wxGDIPlusContext::Scale( wxDouble xScale , wxDouble yScale ) | |
447 | { | |
6fea499c | 448 | m_context->ScaleTransform(xScale,yScale); |
6fea499c SC |
449 | } |
450 | ||
451 | void wxGDIPlusContext::PushState() | |
452 | { | |
453 | GraphicsState state = m_context->Save(); | |
454 | m_stateStack.push_back(state); | |
455 | } | |
456 | ||
457 | void wxGDIPlusContext::PopState() | |
458 | { | |
459 | GraphicsState state = m_stateStack.back(); | |
460 | m_stateStack.pop_back(); | |
461 | m_context->Restore(state); | |
462 | } | |
463 | ||
464 | void wxGDIPlusContext::SetTextColor( const wxColour &col ) | |
465 | { | |
466 | delete m_textBrush; | |
467 | m_textBrush = new SolidBrush( Color( col.Alpha() , col.Red() , | |
468 | col.Green() , col.Blue() )); | |
469 | } | |
470 | ||
471 | void wxGDIPlusContext::SetPen( const wxPen &pen ) | |
472 | { | |
473 | m_penTransparent = pen.GetStyle() == wxTRANSPARENT; | |
474 | if ( m_penTransparent ) | |
475 | return; | |
476 | ||
477 | m_pen->SetColor( Color( pen.GetColour().Alpha() , pen.GetColour().Red() , | |
478 | pen.GetColour().Green() , pen.GetColour().Blue() ) ); | |
479 | ||
480 | // TODO: * m_dc->m_scaleX | |
481 | double penWidth = pen.GetWidth(); | |
482 | if (penWidth <= 0.0) | |
483 | penWidth = 0.1; | |
484 | ||
485 | m_pen->SetWidth(penWidth); | |
486 | ||
487 | LineCap cap; | |
488 | switch ( pen.GetCap() ) | |
489 | { | |
490 | case wxCAP_ROUND : | |
491 | cap = LineCapRound; | |
492 | break; | |
493 | ||
494 | case wxCAP_PROJECTING : | |
495 | cap = LineCapSquare; | |
496 | break; | |
497 | ||
498 | case wxCAP_BUTT : | |
499 | cap = LineCapFlat; // TODO verify | |
500 | break; | |
501 | ||
502 | default : | |
503 | cap = LineCapFlat; | |
504 | break; | |
505 | } | |
506 | m_pen->SetLineCap(cap,cap, DashCapFlat); | |
507 | ||
508 | LineJoin join; | |
509 | switch ( pen.GetJoin() ) | |
510 | { | |
511 | case wxJOIN_BEVEL : | |
512 | join = LineJoinBevel; | |
513 | break; | |
514 | ||
515 | case wxJOIN_MITER : | |
516 | join = LineJoinMiter; | |
517 | break; | |
518 | ||
519 | case wxJOIN_ROUND : | |
520 | join = LineJoinRound; | |
521 | break; | |
522 | ||
523 | default : | |
524 | join = LineJoinMiter; | |
525 | break; | |
526 | } | |
527 | ||
528 | m_pen->SetLineJoin(join); | |
529 | ||
530 | m_pen->SetDashStyle(DashStyleSolid); | |
531 | ||
532 | DashStyle dashStyle = DashStyleSolid; | |
533 | switch ( pen.GetStyle() ) | |
534 | { | |
535 | case wxSOLID : | |
536 | break; | |
537 | ||
538 | case wxDOT : | |
539 | dashStyle = DashStyleDot; | |
540 | break; | |
541 | ||
542 | case wxLONG_DASH : | |
543 | dashStyle = DashStyleDash; // TODO verify | |
544 | break; | |
545 | ||
546 | case wxSHORT_DASH : | |
547 | dashStyle = DashStyleDash; | |
548 | break; | |
549 | ||
550 | case wxDOT_DASH : | |
551 | dashStyle = DashStyleDashDot; | |
552 | break; | |
553 | case wxUSER_DASH : | |
554 | { | |
555 | dashStyle = DashStyleCustom; | |
556 | wxDash *dashes; | |
557 | int count = pen.GetDashes( &dashes ); | |
558 | if ((dashes != NULL) && (count > 0)) | |
559 | { | |
560 | REAL *userLengths = new REAL[count]; | |
561 | for ( int i = 0; i < count; ++i ) | |
562 | { | |
563 | userLengths[i] = dashes[i]; | |
564 | } | |
565 | m_pen->SetDashPattern( userLengths, count); | |
566 | delete[] userLengths; | |
567 | } | |
568 | } | |
569 | break; | |
570 | case wxSTIPPLE : | |
571 | { | |
572 | wxBitmap* bmp = pen.GetStipple(); | |
573 | if ( bmp && bmp->Ok() ) | |
574 | { | |
575 | wxDELETE( m_penImage ); | |
576 | wxDELETE( m_penBrush ); | |
577 | m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE()); | |
578 | m_penBrush = new TextureBrush(m_penImage); | |
579 | m_pen->SetBrush( m_penBrush ); | |
580 | } | |
581 | ||
582 | } | |
583 | break; | |
584 | default : | |
585 | if ( pen.GetStyle() >= wxFIRST_HATCH && pen.GetStyle() <= wxLAST_HATCH ) | |
586 | { | |
587 | wxDELETE( m_penBrush ); | |
588 | HatchStyle style = HatchStyleHorizontal; | |
589 | switch( pen.GetStyle() ) | |
590 | { | |
591 | case wxBDIAGONAL_HATCH : | |
592 | style = HatchStyleBackwardDiagonal; | |
593 | break ; | |
594 | case wxCROSSDIAG_HATCH : | |
595 | style = HatchStyleDiagonalCross; | |
596 | break ; | |
597 | case wxFDIAGONAL_HATCH : | |
598 | style = HatchStyleForwardDiagonal; | |
599 | break ; | |
600 | case wxCROSS_HATCH : | |
601 | style = HatchStyleCross; | |
602 | break ; | |
603 | case wxHORIZONTAL_HATCH : | |
604 | style = HatchStyleHorizontal; | |
605 | break ; | |
606 | case wxVERTICAL_HATCH : | |
607 | style = HatchStyleVertical; | |
608 | break ; | |
609 | ||
610 | } | |
611 | m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() , | |
612 | pen.GetColour().Green() , pen.GetColour().Blue() ), Color::Transparent ); | |
613 | m_pen->SetBrush( m_penBrush ); | |
614 | } | |
615 | break; | |
616 | } | |
617 | if ( dashStyle != DashStyleSolid ) | |
618 | m_pen->SetDashStyle(dashStyle); | |
619 | } | |
620 | ||
621 | void wxGDIPlusContext::SetBrush( const wxBrush &brush ) | |
622 | { | |
623 | // m_brush = brush; | |
624 | if ( m_context == NULL ) | |
625 | return; | |
626 | ||
627 | m_brushTransparent = brush.GetStyle() == wxTRANSPARENT; | |
628 | ||
629 | if ( m_brushTransparent ) | |
630 | return; | |
631 | ||
632 | wxDELETE(m_brush); | |
633 | ||
634 | if ( brush.GetStyle() == wxSOLID) | |
635 | { | |
636 | m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() , | |
637 | brush.GetColour().Green() , brush.GetColour().Blue() ) ); | |
638 | } | |
639 | else if ( brush.IsHatch() ) | |
640 | { | |
641 | HatchStyle style = HatchStyleHorizontal; | |
642 | switch( brush.GetStyle() ) | |
643 | { | |
644 | case wxBDIAGONAL_HATCH : | |
645 | style = HatchStyleBackwardDiagonal; | |
646 | break ; | |
647 | case wxCROSSDIAG_HATCH : | |
648 | style = HatchStyleDiagonalCross; | |
649 | break ; | |
650 | case wxFDIAGONAL_HATCH : | |
651 | style = HatchStyleForwardDiagonal; | |
652 | break ; | |
653 | case wxCROSS_HATCH : | |
654 | style = HatchStyleCross; | |
655 | break ; | |
656 | case wxHORIZONTAL_HATCH : | |
657 | style = HatchStyleHorizontal; | |
658 | break ; | |
659 | case wxVERTICAL_HATCH : | |
660 | style = HatchStyleVertical; | |
661 | break ; | |
662 | ||
663 | } | |
664 | m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() , | |
665 | brush.GetColour().Green() , brush.GetColour().Blue() ), Color::Transparent ); | |
666 | } | |
667 | else | |
668 | { | |
669 | wxBitmap* bmp = brush.GetStipple(); | |
670 | if ( bmp && bmp->Ok() ) | |
671 | { | |
672 | wxDELETE( m_brushImage ); | |
673 | m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE()); | |
674 | m_brush = new TextureBrush(m_brushImage); | |
675 | } | |
676 | } | |
677 | } | |
678 | ||
679 | void wxGDIPlusContext::SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxColour&c1, const wxColour&c2) | |
680 | { | |
681 | m_brushTransparent = false ; | |
682 | ||
683 | wxDELETE(m_brush); | |
684 | ||
685 | m_brush = new LinearGradientBrush( PointF( x1,y1) , PointF( x2,y2), | |
686 | Color( c1.Alpha(), c1.Red(),c1.Green() , c1.Blue() ), | |
687 | Color( c2.Alpha(), c2.Red(),c2.Green() , c2.Blue() )); | |
688 | } | |
689 | ||
690 | void wxGDIPlusContext::SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius, | |
691 | const wxColour &oColor, const wxColour &cColor) | |
692 | { | |
693 | m_brushTransparent = false ; | |
694 | ||
695 | wxDELETE(m_brush); | |
696 | wxDELETE(m_brushPath); | |
697 | ||
698 | // Create a path that consists of a single circle. | |
699 | m_brushPath = new GraphicsPath(); | |
700 | m_brushPath->AddEllipse( (REAL)(xc-radius), (REAL)(yc-radius), (REAL)(2*radius), (REAL)(2*radius)); | |
701 | ||
702 | PathGradientBrush *b = new PathGradientBrush(m_brushPath); | |
703 | m_brush = b; | |
704 | b->SetCenterPoint( PointF(xo,yo)); | |
705 | b->SetCenterColor(Color( oColor.Alpha(), oColor.Red(),oColor.Green() , oColor.Blue() )); | |
706 | ||
707 | Color colors[] = {Color( cColor.Alpha(), cColor.Red(),cColor.Green() , cColor.Blue() )}; | |
708 | int count = 1; | |
709 | b->SetSurroundColors(colors, &count); | |
710 | } | |
711 | ||
fe31db81 SC |
712 | // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the |
713 | // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied | |
714 | // bytes as parameter | |
715 | ||
6fea499c SC |
716 | void wxGDIPlusContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) |
717 | { | |
fe31db81 SC |
718 | Bitmap* image = NULL; |
719 | Bitmap* helper = NULL; | |
720 | if ( bmp.GetMask() ) | |
721 | { | |
722 | Bitmap interim((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE()) ; | |
723 | ||
724 | size_t width = interim.GetWidth(); | |
725 | size_t height = interim.GetHeight(); | |
726 | Rect bounds(0,0,width,height); | |
727 | ||
728 | image = new Bitmap(width,height,PixelFormat32bppPARGB) ; | |
729 | ||
730 | Bitmap interimMask((HBITMAP)bmp.GetMask()->GetMaskBitmap(),NULL); | |
731 | wxASSERT(interimMask.GetPixelFormat() == PixelFormat1bppIndexed); | |
732 | ||
733 | BitmapData dataMask ; | |
734 | interimMask.LockBits(&bounds,ImageLockModeRead, | |
735 | interimMask.GetPixelFormat(),&dataMask); | |
736 | ||
737 | ||
738 | BitmapData imageData ; | |
739 | image->LockBits(&bounds,ImageLockModeWrite, PixelFormat32bppPARGB, &imageData); | |
740 | ||
741 | BYTE maskPattern = 0 ; | |
742 | BYTE maskByte = 0; | |
743 | size_t maskIndex ; | |
744 | ||
745 | for ( size_t y = 0 ; y < height ; ++y) | |
746 | { | |
747 | maskIndex = 0 ; | |
748 | for( size_t x = 0 ; x < width; ++x) | |
749 | { | |
750 | if ( x % 8 == 0) | |
751 | { | |
752 | maskPattern = 0x80; | |
753 | maskByte = *((BYTE*)dataMask.Scan0 + dataMask.Stride*y + maskIndex); | |
754 | maskIndex++; | |
755 | } | |
756 | else | |
757 | maskPattern = maskPattern >> 1; | |
758 | ||
759 | ARGB *dest = (ARGB*)((BYTE*)imageData.Scan0 + imageData.Stride*y + x*4); | |
760 | if ( (maskByte & maskPattern) == 0 ) | |
761 | *dest = 0x00000000; | |
762 | else | |
763 | { | |
764 | Color c ; | |
765 | interim.GetPixel(x,y,&c) ; | |
766 | *dest = (c.GetValue() | Color::AlphaMask); | |
767 | } | |
768 | } | |
769 | } | |
770 | ||
771 | image->UnlockBits(&imageData); | |
772 | ||
773 | interimMask.UnlockBits(&dataMask); | |
774 | interim.UnlockBits(&dataMask); | |
775 | } | |
776 | else | |
777 | { | |
778 | image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE()); | |
779 | if ( GetPixelFormatSize(image->GetPixelFormat()) == 32 ) | |
780 | { | |
781 | size_t width = image->GetWidth(); | |
782 | size_t height = image->GetHeight(); | |
783 | Rect bounds(0,0,width,height); | |
784 | BitmapData data ; | |
785 | ||
786 | helper = image ; | |
787 | image = NULL ; | |
788 | helper->LockBits(&bounds, ImageLockModeRead, | |
789 | helper->GetPixelFormat(),&data); | |
790 | ||
791 | image = new Bitmap(data.Width, data.Height, data.Stride, | |
792 | PixelFormat32bppARGB , (BYTE*) data.Scan0); | |
793 | ||
794 | helper->UnlockBits(&data); | |
795 | } | |
796 | } | |
797 | if ( image ) | |
798 | m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ; | |
6fea499c | 799 | delete image ; |
fe31db81 | 800 | delete helper ; |
6fea499c SC |
801 | } |
802 | ||
803 | void wxGDIPlusContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
804 | { | |
fe31db81 SC |
805 | HICON hIcon = (HICON)icon.GetHICON(); |
806 | ICONINFO iconInfo ; | |
807 | // IconInfo creates the bitmaps for color and mask, we must dispose of them after use | |
808 | if (!GetIconInfo(hIcon,&iconInfo)) | |
809 | return; | |
810 | ||
811 | BITMAP iconBmpData ; | |
812 | GetObject(iconInfo.hbmColor,sizeof(BITMAP),&iconBmpData); | |
813 | Bitmap interim(iconInfo.hbmColor,NULL); | |
814 | ||
815 | Bitmap* image = NULL ; | |
816 | ||
817 | if( GetPixelFormatSize(interim.GetPixelFormat())!= 32 ) | |
818 | { | |
819 | image = Bitmap::FromHICON(hIcon); | |
820 | } | |
821 | else | |
822 | { | |
823 | size_t width = interim.GetWidth(); | |
824 | size_t height = interim.GetHeight(); | |
825 | Rect bounds(0,0,width,height); | |
826 | BitmapData data ; | |
827 | ||
828 | interim.LockBits(&bounds, ImageLockModeRead, | |
829 | interim.GetPixelFormat(),&data); | |
830 | image = new Bitmap(data.Width, data.Height, data.Stride, | |
831 | PixelFormat32bppARGB , (BYTE*) data.Scan0); | |
832 | interim.UnlockBits(&data); | |
833 | } | |
834 | ||
6fea499c | 835 | m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ; |
fe31db81 | 836 | |
6fea499c | 837 | delete image ; |
fe31db81 SC |
838 | DeleteObject(iconInfo.hbmColor); |
839 | DeleteObject(iconInfo.hbmMask); | |
6fea499c SC |
840 | } |
841 | ||
842 | ||
843 | void wxGDIPlusContext::DrawText( const wxString &str, wxDouble x, wxDouble y ) | |
844 | { | |
845 | if ( str.IsEmpty()) | |
846 | return ; | |
847 | ||
848 | wxWCharBuffer s = str.wc_str( *wxConvUI ); | |
849 | m_context->DrawString( s , -1 , m_font , PointF( x , y ) , m_textBrush ); | |
850 | // TODO m_backgroundMode == wxSOLID | |
851 | } | |
852 | ||
853 | void wxGDIPlusContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height, | |
854 | wxDouble *descent, wxDouble *externalLeading ) const | |
855 | { | |
856 | wxWCharBuffer s = str.wc_str( *wxConvUI ); | |
857 | FontFamily ffamily ; | |
858 | ||
859 | m_font->GetFamily(&ffamily) ; | |
860 | ||
861 | REAL factorY = m_context->GetDpiY() / 72.0 ; | |
862 | ||
863 | REAL rDescent = ffamily.GetCellDescent(FontStyleRegular) * | |
864 | m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular); | |
865 | REAL rAscent = ffamily.GetCellAscent(FontStyleRegular) * | |
866 | m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular); | |
867 | REAL rHeight = ffamily.GetLineSpacing(FontStyleRegular) * | |
868 | m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular); | |
869 | ||
870 | if ( height ) | |
871 | *height = rHeight * factorY + 0.5 ; | |
872 | if ( descent ) | |
873 | *descent = rDescent * factorY + 0.5 ; | |
874 | if ( externalLeading ) | |
875 | *externalLeading = (rHeight - rAscent - rDescent) * factorY + 0.5 ; | |
876 | // measuring empty strings is not guaranteed, so do it by hand | |
877 | if ( str.IsEmpty()) | |
878 | { | |
879 | if ( width ) | |
880 | *width = 0 ; | |
881 | } | |
882 | else | |
883 | { | |
884 | // MeasureString does return a rectangle that is way too large, so it is | |
885 | // not usable here | |
886 | RectF layoutRect(0,0, 100000.0f, 100000.0f); | |
887 | StringFormat strFormat; | |
888 | CharacterRange strRange(0,wcslen(s)); | |
889 | strFormat.SetMeasurableCharacterRanges(1,&strRange); | |
890 | Region region ; | |
891 | m_context->MeasureCharacterRanges(s, -1 , m_font,layoutRect, &strFormat,1,®ion) ; | |
892 | RectF bbox ; | |
893 | region.GetBounds(&bbox,m_context); | |
894 | if ( width ) | |
895 | *width = bbox.GetRight()-bbox.GetLeft()+0.5; | |
896 | } | |
897 | } | |
898 | ||
899 | void wxGDIPlusContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const | |
900 | { | |
901 | widths.Empty(); | |
902 | widths.Add(0, text.length()); | |
903 | ||
904 | if (text.empty()) | |
905 | return; | |
906 | ||
907 | wxWCharBuffer ws = text.wc_str( *wxConvUI ); | |
908 | size_t len = wcslen( ws ) ; | |
909 | wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations")); | |
910 | ||
911 | RectF layoutRect(0,0, 100000.0f, 100000.0f); | |
912 | StringFormat strFormat; | |
913 | ||
914 | CharacterRange* ranges = new CharacterRange[len] ; | |
915 | Region* regions = new Region[len]; | |
916 | for( size_t i = 0 ; i < len ; ++i) | |
917 | { | |
918 | ranges[i].First = i ; | |
919 | ranges[i].Length = 1 ; | |
920 | } | |
921 | strFormat.SetMeasurableCharacterRanges(len,ranges); | |
922 | m_context->MeasureCharacterRanges(ws, -1 , m_font,layoutRect, &strFormat,1,regions) ; | |
923 | ||
924 | RectF bbox ; | |
925 | for ( size_t i = 0 ; i < len ; ++i) | |
926 | { | |
927 | regions[i].GetBounds(&bbox,m_context); | |
928 | widths[i] = bbox.GetRight()-bbox.GetLeft(); | |
929 | } | |
930 | } | |
931 | ||
932 | void wxGDIPlusContext::SetFont( const wxFont &font ) | |
933 | { | |
934 | wxASSERT( font.Ok()); | |
935 | delete m_font; | |
936 | wxWCharBuffer s = font.GetFaceName().wc_str( *wxConvUI ); | |
937 | int size = font.GetPointSize(); | |
938 | int style = FontStyleRegular; | |
939 | if ( font.GetStyle() == wxFONTSTYLE_ITALIC ) | |
940 | style |= FontStyleItalic; | |
941 | if ( font.GetUnderlined() ) | |
942 | style |= FontStyleUnderline; | |
943 | if ( font.GetWeight() == wxFONTWEIGHT_BOLD ) | |
944 | style |= FontStyleBold; | |
945 | m_font = new Font( s , size , style ); | |
946 | } | |
947 | ||
539e2795 SC |
948 | void * wxCairoContext::GetNativeContext() |
949 | { | |
950 | return m_context; | |
951 | } | |
952 | ||
6fea499c SC |
953 | wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC& dc) |
954 | { | |
955 | return new wxGDIPlusContext( (HDC) dc.GetHDC() ); | |
956 | } | |
7ba86d93 | 957 | |
539e2795 SC |
958 | wxGraphicsContext* wxGraphicsContext::Create( wxWindow * window ) |
959 | { | |
960 | return NULL; // TODO | |
961 | } | |
962 | ||
963 | wxGraphicsContext* wxGraphicsContext::CreateFromNative( void * context ) | |
964 | { | |
965 | return NULL; // TODO | |
966 | } | |
967 | ||
968 | ||
7ba86d93 RD |
969 | |
970 | #endif // wxUSE_GRAPHICS_CONTEXT |