]> git.saurik.com Git - wxWidgets.git/blob - src/msw/graphics.cpp
define ChangeSelection() (fixes wxMotif build)
[wxWidgets.git] / src / msw / graphics.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/graphics.cpp
3 // Purpose: wxGCDC class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 2006-09-30
7 // RCS-ID: $Id$
8 // Copyright: (c) 2006 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/dc.h"
15
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
38 #if wxUSE_GRAPHICS_CONTEXT
39
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 &region );
200 virtual void StrokePath( const wxGraphicsPath *p );
201 virtual void FillPath( const wxGraphicsPath *p , int fillStyle = wxWINDING_RULE );
202
203 virtual wxGraphicsPath* CreatePath();
204 virtual void SetPen( const wxPen &pen );
205 virtual void SetBrush( const wxBrush &brush );
206 virtual void SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxColour&c1, const wxColour&c2) ;
207 virtual void SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
208 const wxColour &oColor, const wxColour &cColor);
209
210 virtual void Translate( wxDouble dx , wxDouble dy );
211 virtual void Scale( wxDouble xScale , wxDouble yScale );
212 virtual void Rotate( wxDouble angle );
213
214 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
215 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
216 virtual void PushState();
217 virtual void PopState();
218
219 virtual void SetFont( const wxFont &font );
220 virtual void SetTextColor( const wxColour &col );
221 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y);
222 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
223 wxDouble *descent, wxDouble *externalLeading ) const;
224 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
225
226 private:
227 Graphics* m_context;
228 vector<GraphicsState> m_stateStack;
229 GraphicsState m_state1;
230 GraphicsState m_state2;
231
232 Pen* m_pen;
233 bool m_penTransparent;
234 Image* m_penImage;
235 Brush* m_penBrush;
236
237 Brush* m_brush;
238 bool m_brushTransparent;
239 Image* m_brushImage;
240 GraphicsPath* m_brushPath;
241
242 Brush* m_textBrush;
243 Font* m_font;
244 // wxPen m_pen;
245 // wxBrush m_brush;
246 };
247
248 wxGDIPlusPath::wxGDIPlusPath()
249 {
250 m_path = new GraphicsPath();
251 }
252
253 wxGDIPlusPath::~wxGDIPlusPath()
254 {
255 delete m_path;
256 }
257
258 GraphicsPath* wxGDIPlusPath::GetPath() const
259 {
260 return m_path;
261 }
262
263 //
264 // The Primitives
265 //
266
267 void wxGDIPlusPath::MoveToPoint( wxDouble x , wxDouble y )
268 {
269 m_path->StartFigure();
270 m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y);
271 }
272
273 void wxGDIPlusPath::AddLineToPoint( wxDouble x , wxDouble y )
274 {
275 m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y);
276 }
277
278 void wxGDIPlusPath::CloseSubpath()
279 {
280 m_path->CloseFigure();
281 }
282
283 void wxGDIPlusPath::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
284 {
285 PointF c1(cx1,cy1);
286 PointF c2(cx2,cy2);
287 PointF end(x,y);
288 PointF start;
289 m_path->GetLastPoint(&start);
290 m_path->AddBezier(start,c1,c2,end);
291 }
292
293 // gets the last point of the current path, (0,0) if not yet set
294 void wxGDIPlusPath::GetCurrentPoint( wxDouble& x, wxDouble&y)
295 {
296 PointF start;
297 m_path->GetLastPoint(&start);
298 x = start.X ;
299 y = start.Y ;
300 }
301
302 void wxGDIPlusPath::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
303 {
304 double sweepAngle = endAngle - startAngle ;
305 if( abs(sweepAngle) >= 2*M_PI)
306 {
307 sweepAngle = 2 * M_PI;
308 }
309 else
310 {
311 if ( clockwise )
312 {
313 if( sweepAngle < 0 )
314 sweepAngle += 2 * M_PI;
315 }
316 else
317 {
318 if( sweepAngle > 0 )
319 sweepAngle -= 2 * M_PI;
320
321 }
322 }
323 m_path->AddArc((REAL) (x-r),(REAL) (y-r),(REAL) (2*r),(REAL) (2*r),RadToDeg(startAngle),RadToDeg(sweepAngle));
324 }
325
326 void wxGDIPlusPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
327 {
328 m_path->AddRectangle(RectF(x,y,w,h));
329 }
330
331 //
332 //
333 //
334 /*
335 // closes the current subpath
336 void wxGDIPlusPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
337 {
338 // CGPathAddArcToPoint( m_path, NULL , x1, y1, x2, y2, r);
339 }
340
341 */
342
343 //-----------------------------------------------------------------------------
344 // wxGDIPlusContext implementation
345 //-----------------------------------------------------------------------------
346
347 wxGDIPlusContext::wxGDIPlusContext( WXHDC hdc )
348 {
349 gGDILoader.EnsureIsLoaded();
350 m_context = new Graphics( (HDC) hdc);
351 m_state1 = m_context->Save();
352 m_state2 = m_context->Save();
353
354 // set defaults
355
356 m_penTransparent = false;
357 m_pen = new Pen((ARGB)Color::Black);
358 m_penImage = NULL;
359 m_penBrush = NULL;
360
361 m_brushTransparent = false;
362 m_brush = new SolidBrush((ARGB)Color::White);
363 m_brushImage = NULL;
364 m_brushPath = NULL;
365 m_textBrush = new SolidBrush((ARGB)Color::Black);
366 m_font = new Font( L"Arial" , 9 , FontStyleRegular );
367 }
368
369 wxGDIPlusContext::~wxGDIPlusContext()
370 {
371 if ( m_context )
372 {
373 m_context->Restore( m_state2 );
374 m_context->Restore( m_state1 );
375 delete m_context;
376 delete m_pen;
377 delete m_penImage;
378 delete m_penBrush;
379 delete m_brush;
380 delete m_brushImage;
381 delete m_brushPath;
382 delete m_textBrush;
383 delete m_font;
384 }
385 }
386
387
388 void wxGDIPlusContext::Clip( const wxRegion & WXUNUSED(region) )
389 {
390 // ClipCGContextToRegion ( m_context, &bounds , (RgnHandle) dc->m_macCurrentClipRgn );
391 }
392
393 void wxGDIPlusContext::StrokePath( const wxGraphicsPath *p )
394 {
395 if ( m_penTransparent )
396 return;
397
398 const wxGDIPlusPath* path = dynamic_cast< const wxGDIPlusPath*>( p );
399 m_context->DrawPath( m_pen , path->GetPath() );
400 }
401
402 void wxGDIPlusContext::FillPath( const wxGraphicsPath *p , int fillStyle )
403 {
404 if ( !m_brushTransparent )
405 {
406 const wxGDIPlusPath* path = dynamic_cast< const wxGDIPlusPath*>( p );
407 path->GetPath()->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding);
408 m_context->FillPath( m_brush , path->GetPath() );
409 }
410 }
411
412 wxGraphicsPath* wxGDIPlusContext::CreatePath()
413 {
414 return new wxGDIPlusPath();
415 }
416
417 void wxGDIPlusContext::Rotate( wxDouble angle )
418 {
419 m_context->RotateTransform( angle );
420 }
421
422 void wxGDIPlusContext::Translate( wxDouble dx , wxDouble dy )
423 {
424 m_context->TranslateTransform( dx , dy );
425 }
426
427 void wxGDIPlusContext::Scale( wxDouble xScale , wxDouble yScale )
428 {
429 PointF penWidth( m_pen->GetWidth(), 0);
430 Matrix matrix ;
431 if ( !m_penTransparent )
432 {
433 m_context->GetTransform(&matrix);
434 matrix.TransformVectors(&penWidth);
435 }
436 m_context->ScaleTransform(xScale,yScale);
437 if ( !m_penTransparent )
438 {
439 m_context->GetTransform(&matrix);
440 matrix.Invert();
441 matrix.TransformVectors(&penWidth) ;
442 m_pen->SetWidth( sqrt( penWidth.X*penWidth.X + penWidth.Y*penWidth.Y));
443 }
444 }
445
446 void wxGDIPlusContext::PushState()
447 {
448 GraphicsState state = m_context->Save();
449 m_stateStack.push_back(state);
450 }
451
452 void wxGDIPlusContext::PopState()
453 {
454 GraphicsState state = m_stateStack.back();
455 m_stateStack.pop_back();
456 m_context->Restore(state);
457 }
458
459 void wxGDIPlusContext::SetTextColor( const wxColour &col )
460 {
461 delete m_textBrush;
462 m_textBrush = new SolidBrush( Color( col.Alpha() , col.Red() ,
463 col.Green() , col.Blue() ));
464 }
465
466 void wxGDIPlusContext::SetPen( const wxPen &pen )
467 {
468 m_penTransparent = pen.GetStyle() == wxTRANSPARENT;
469 if ( m_penTransparent )
470 return;
471
472 m_pen->SetColor( Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
473 pen.GetColour().Green() , pen.GetColour().Blue() ) );
474
475 // TODO: * m_dc->m_scaleX
476 double penWidth = pen.GetWidth();
477 if (penWidth <= 0.0)
478 penWidth = 0.1;
479
480 m_pen->SetWidth(penWidth);
481
482 LineCap cap;
483 switch ( pen.GetCap() )
484 {
485 case wxCAP_ROUND :
486 cap = LineCapRound;
487 break;
488
489 case wxCAP_PROJECTING :
490 cap = LineCapSquare;
491 break;
492
493 case wxCAP_BUTT :
494 cap = LineCapFlat; // TODO verify
495 break;
496
497 default :
498 cap = LineCapFlat;
499 break;
500 }
501 m_pen->SetLineCap(cap,cap, DashCapFlat);
502
503 LineJoin join;
504 switch ( pen.GetJoin() )
505 {
506 case wxJOIN_BEVEL :
507 join = LineJoinBevel;
508 break;
509
510 case wxJOIN_MITER :
511 join = LineJoinMiter;
512 break;
513
514 case wxJOIN_ROUND :
515 join = LineJoinRound;
516 break;
517
518 default :
519 join = LineJoinMiter;
520 break;
521 }
522
523 m_pen->SetLineJoin(join);
524
525 m_pen->SetDashStyle(DashStyleSolid);
526
527 DashStyle dashStyle = DashStyleSolid;
528 switch ( pen.GetStyle() )
529 {
530 case wxSOLID :
531 break;
532
533 case wxDOT :
534 dashStyle = DashStyleDot;
535 break;
536
537 case wxLONG_DASH :
538 dashStyle = DashStyleDash; // TODO verify
539 break;
540
541 case wxSHORT_DASH :
542 dashStyle = DashStyleDash;
543 break;
544
545 case wxDOT_DASH :
546 dashStyle = DashStyleDashDot;
547 break;
548 case wxUSER_DASH :
549 {
550 dashStyle = DashStyleCustom;
551 wxDash *dashes;
552 int count = pen.GetDashes( &dashes );
553 if ((dashes != NULL) && (count > 0))
554 {
555 REAL *userLengths = new REAL[count];
556 for ( int i = 0; i < count; ++i )
557 {
558 userLengths[i] = dashes[i];
559 }
560 m_pen->SetDashPattern( userLengths, count);
561 delete[] userLengths;
562 }
563 }
564 break;
565 case wxSTIPPLE :
566 {
567 wxBitmap* bmp = pen.GetStipple();
568 if ( bmp && bmp->Ok() )
569 {
570 wxDELETE( m_penImage );
571 wxDELETE( m_penBrush );
572 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
573 m_penBrush = new TextureBrush(m_penImage);
574 m_pen->SetBrush( m_penBrush );
575 }
576
577 }
578 break;
579 default :
580 if ( pen.GetStyle() >= wxFIRST_HATCH && pen.GetStyle() <= wxLAST_HATCH )
581 {
582 wxDELETE( m_penBrush );
583 HatchStyle style = HatchStyleHorizontal;
584 switch( pen.GetStyle() )
585 {
586 case wxBDIAGONAL_HATCH :
587 style = HatchStyleBackwardDiagonal;
588 break ;
589 case wxCROSSDIAG_HATCH :
590 style = HatchStyleDiagonalCross;
591 break ;
592 case wxFDIAGONAL_HATCH :
593 style = HatchStyleForwardDiagonal;
594 break ;
595 case wxCROSS_HATCH :
596 style = HatchStyleCross;
597 break ;
598 case wxHORIZONTAL_HATCH :
599 style = HatchStyleHorizontal;
600 break ;
601 case wxVERTICAL_HATCH :
602 style = HatchStyleVertical;
603 break ;
604
605 }
606 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
607 pen.GetColour().Green() , pen.GetColour().Blue() ), Color::Transparent );
608 m_pen->SetBrush( m_penBrush );
609 }
610 break;
611 }
612 if ( dashStyle != DashStyleSolid )
613 m_pen->SetDashStyle(dashStyle);
614 }
615
616 void wxGDIPlusContext::SetBrush( const wxBrush &brush )
617 {
618 // m_brush = brush;
619 if ( m_context == NULL )
620 return;
621
622 m_brushTransparent = brush.GetStyle() == wxTRANSPARENT;
623
624 if ( m_brushTransparent )
625 return;
626
627 wxDELETE(m_brush);
628
629 if ( brush.GetStyle() == wxSOLID)
630 {
631 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
632 brush.GetColour().Green() , brush.GetColour().Blue() ) );
633 }
634 else if ( brush.IsHatch() )
635 {
636 HatchStyle style = HatchStyleHorizontal;
637 switch( brush.GetStyle() )
638 {
639 case wxBDIAGONAL_HATCH :
640 style = HatchStyleBackwardDiagonal;
641 break ;
642 case wxCROSSDIAG_HATCH :
643 style = HatchStyleDiagonalCross;
644 break ;
645 case wxFDIAGONAL_HATCH :
646 style = HatchStyleForwardDiagonal;
647 break ;
648 case wxCROSS_HATCH :
649 style = HatchStyleCross;
650 break ;
651 case wxHORIZONTAL_HATCH :
652 style = HatchStyleHorizontal;
653 break ;
654 case wxVERTICAL_HATCH :
655 style = HatchStyleVertical;
656 break ;
657
658 }
659 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
660 brush.GetColour().Green() , brush.GetColour().Blue() ), Color::Transparent );
661 }
662 else
663 {
664 wxBitmap* bmp = brush.GetStipple();
665 if ( bmp && bmp->Ok() )
666 {
667 wxDELETE( m_brushImage );
668 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
669 m_brush = new TextureBrush(m_brushImage);
670 }
671 }
672 }
673
674 void wxGDIPlusContext::SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxColour&c1, const wxColour&c2)
675 {
676 m_brushTransparent = false ;
677
678 wxDELETE(m_brush);
679
680 m_brush = new LinearGradientBrush( PointF( x1,y1) , PointF( x2,y2),
681 Color( c1.Alpha(), c1.Red(),c1.Green() , c1.Blue() ),
682 Color( c2.Alpha(), c2.Red(),c2.Green() , c2.Blue() ));
683 }
684
685 void wxGDIPlusContext::SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
686 const wxColour &oColor, const wxColour &cColor)
687 {
688 m_brushTransparent = false ;
689
690 wxDELETE(m_brush);
691 wxDELETE(m_brushPath);
692
693 // Create a path that consists of a single circle.
694 m_brushPath = new GraphicsPath();
695 m_brushPath->AddEllipse( (REAL)(xc-radius), (REAL)(yc-radius), (REAL)(2*radius), (REAL)(2*radius));
696
697 PathGradientBrush *b = new PathGradientBrush(m_brushPath);
698 m_brush = b;
699 b->SetCenterPoint( PointF(xo,yo));
700 b->SetCenterColor(Color( oColor.Alpha(), oColor.Red(),oColor.Green() , oColor.Blue() ));
701
702 Color colors[] = {Color( cColor.Alpha(), cColor.Red(),cColor.Green() , cColor.Blue() )};
703 int count = 1;
704 b->SetSurroundColors(colors, &count);
705 }
706
707 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
708 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
709 // bytes as parameter
710
711 void wxGDIPlusContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
712 {
713 Bitmap* image = NULL;
714 Bitmap* helper = NULL;
715 if ( bmp.GetMask() )
716 {
717 Bitmap interim((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE()) ;
718
719 size_t width = interim.GetWidth();
720 size_t height = interim.GetHeight();
721 Rect bounds(0,0,width,height);
722
723 image = new Bitmap(width,height,PixelFormat32bppPARGB) ;
724
725 Bitmap interimMask((HBITMAP)bmp.GetMask()->GetMaskBitmap(),NULL);
726 wxASSERT(interimMask.GetPixelFormat() == PixelFormat1bppIndexed);
727
728 BitmapData dataMask ;
729 interimMask.LockBits(&bounds,ImageLockModeRead,
730 interimMask.GetPixelFormat(),&dataMask);
731
732
733 BitmapData imageData ;
734 image->LockBits(&bounds,ImageLockModeWrite, PixelFormat32bppPARGB, &imageData);
735
736 BYTE maskPattern = 0 ;
737 BYTE maskByte = 0;
738 size_t maskIndex ;
739
740 for ( size_t y = 0 ; y < height ; ++y)
741 {
742 maskIndex = 0 ;
743 for( size_t x = 0 ; x < width; ++x)
744 {
745 if ( x % 8 == 0)
746 {
747 maskPattern = 0x80;
748 maskByte = *((BYTE*)dataMask.Scan0 + dataMask.Stride*y + maskIndex);
749 maskIndex++;
750 }
751 else
752 maskPattern = maskPattern >> 1;
753
754 ARGB *dest = (ARGB*)((BYTE*)imageData.Scan0 + imageData.Stride*y + x*4);
755 if ( (maskByte & maskPattern) == 0 )
756 *dest = 0x00000000;
757 else
758 {
759 Color c ;
760 interim.GetPixel(x,y,&c) ;
761 *dest = (c.GetValue() | Color::AlphaMask);
762 }
763 }
764 }
765
766 image->UnlockBits(&imageData);
767
768 interimMask.UnlockBits(&dataMask);
769 interim.UnlockBits(&dataMask);
770 }
771 else
772 {
773 image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
774 if ( GetPixelFormatSize(image->GetPixelFormat()) == 32 )
775 {
776 size_t width = image->GetWidth();
777 size_t height = image->GetHeight();
778 Rect bounds(0,0,width,height);
779 BitmapData data ;
780
781 helper = image ;
782 image = NULL ;
783 helper->LockBits(&bounds, ImageLockModeRead,
784 helper->GetPixelFormat(),&data);
785
786 image = new Bitmap(data.Width, data.Height, data.Stride,
787 PixelFormat32bppARGB , (BYTE*) data.Scan0);
788
789 helper->UnlockBits(&data);
790 }
791 }
792 if ( image )
793 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
794 delete image ;
795 delete helper ;
796 }
797
798 void wxGDIPlusContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
799 {
800 HICON hIcon = (HICON)icon.GetHICON();
801 ICONINFO iconInfo ;
802 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
803 if (!GetIconInfo(hIcon,&iconInfo))
804 return;
805
806 BITMAP iconBmpData ;
807 GetObject(iconInfo.hbmColor,sizeof(BITMAP),&iconBmpData);
808 Bitmap interim(iconInfo.hbmColor,NULL);
809
810 Bitmap* image = NULL ;
811
812 if( GetPixelFormatSize(interim.GetPixelFormat())!= 32 )
813 {
814 image = Bitmap::FromHICON(hIcon);
815 }
816 else
817 {
818 size_t width = interim.GetWidth();
819 size_t height = interim.GetHeight();
820 Rect bounds(0,0,width,height);
821 BitmapData data ;
822
823 interim.LockBits(&bounds, ImageLockModeRead,
824 interim.GetPixelFormat(),&data);
825 image = new Bitmap(data.Width, data.Height, data.Stride,
826 PixelFormat32bppARGB , (BYTE*) data.Scan0);
827 interim.UnlockBits(&data);
828 }
829
830 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
831
832 delete image ;
833 DeleteObject(iconInfo.hbmColor);
834 DeleteObject(iconInfo.hbmMask);
835 }
836
837
838 void wxGDIPlusContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
839 {
840 if ( str.IsEmpty())
841 return ;
842
843 wxWCharBuffer s = str.wc_str( *wxConvUI );
844 m_context->DrawString( s , -1 , m_font , PointF( x , y ) , m_textBrush );
845 // TODO m_backgroundMode == wxSOLID
846 }
847
848 void wxGDIPlusContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
849 wxDouble *descent, wxDouble *externalLeading ) const
850 {
851 wxWCharBuffer s = str.wc_str( *wxConvUI );
852 FontFamily ffamily ;
853
854 m_font->GetFamily(&ffamily) ;
855
856 REAL factorY = m_context->GetDpiY() / 72.0 ;
857
858 REAL rDescent = ffamily.GetCellDescent(FontStyleRegular) *
859 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
860 REAL rAscent = ffamily.GetCellAscent(FontStyleRegular) *
861 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
862 REAL rHeight = ffamily.GetLineSpacing(FontStyleRegular) *
863 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
864
865 if ( height )
866 *height = rHeight * factorY + 0.5 ;
867 if ( descent )
868 *descent = rDescent * factorY + 0.5 ;
869 if ( externalLeading )
870 *externalLeading = (rHeight - rAscent - rDescent) * factorY + 0.5 ;
871 // measuring empty strings is not guaranteed, so do it by hand
872 if ( str.IsEmpty())
873 {
874 if ( width )
875 *width = 0 ;
876 }
877 else
878 {
879 // MeasureString does return a rectangle that is way too large, so it is
880 // not usable here
881 RectF layoutRect(0,0, 100000.0f, 100000.0f);
882 StringFormat strFormat;
883 CharacterRange strRange(0,wcslen(s));
884 strFormat.SetMeasurableCharacterRanges(1,&strRange);
885 Region region ;
886 m_context->MeasureCharacterRanges(s, -1 , m_font,layoutRect, &strFormat,1,&region) ;
887 RectF bbox ;
888 region.GetBounds(&bbox,m_context);
889 if ( width )
890 *width = bbox.GetRight()-bbox.GetLeft()+0.5;
891 }
892 }
893
894 void wxGDIPlusContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
895 {
896 widths.Empty();
897 widths.Add(0, text.length());
898
899 if (text.empty())
900 return;
901
902 wxWCharBuffer ws = text.wc_str( *wxConvUI );
903 size_t len = wcslen( ws ) ;
904 wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations"));
905
906 RectF layoutRect(0,0, 100000.0f, 100000.0f);
907 StringFormat strFormat;
908
909 CharacterRange* ranges = new CharacterRange[len] ;
910 Region* regions = new Region[len];
911 for( size_t i = 0 ; i < len ; ++i)
912 {
913 ranges[i].First = i ;
914 ranges[i].Length = 1 ;
915 }
916 strFormat.SetMeasurableCharacterRanges(len,ranges);
917 m_context->MeasureCharacterRanges(ws, -1 , m_font,layoutRect, &strFormat,1,regions) ;
918
919 RectF bbox ;
920 for ( size_t i = 0 ; i < len ; ++i)
921 {
922 regions[i].GetBounds(&bbox,m_context);
923 widths[i] = bbox.GetRight()-bbox.GetLeft();
924 }
925 }
926
927 void wxGDIPlusContext::SetFont( const wxFont &font )
928 {
929 wxASSERT( font.Ok());
930 delete m_font;
931 wxWCharBuffer s = font.GetFaceName().wc_str( *wxConvUI );
932 int size = font.GetPointSize();
933 int style = FontStyleRegular;
934 if ( font.GetStyle() == wxFONTSTYLE_ITALIC )
935 style |= FontStyleItalic;
936 if ( font.GetUnderlined() )
937 style |= FontStyleUnderline;
938 if ( font.GetWeight() == wxFONTWEIGHT_BOLD )
939 style |= FontStyleBold;
940 m_font = new Font( s , size , style );
941 }
942
943 wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC& dc)
944 {
945 return new wxGDIPlusContext( (HDC) dc.GetHDC() );
946 }
947
948
949 #endif // wxUSE_GRAPHICS_CONTEXT