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