]> git.saurik.com Git - wxWidgets.git/blob - src/msw/graphics.cpp
using GetNativePath instead of dynamic_cast
[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 // returns the native path
186 virtual void * GetNativePath() const { return m_path; }
187
188 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
189 virtual void UnGetNativePath(void *p) {}
190
191 private :
192 GraphicsPath* m_path;
193 };
194
195 class WXDLLEXPORT wxGDIPlusContext : public wxGraphicsContext
196 {
197 DECLARE_NO_COPY_CLASS(wxGDIPlusContext)
198
199 public:
200 wxGDIPlusContext( WXHDC hdc );
201 wxGDIPlusContext();
202 virtual ~wxGDIPlusContext();
203
204 virtual void Clip( const wxRegion &region );
205 // clips drawings to the rect
206 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
207
208 // resets the clipping to original extent
209 virtual void ResetClip();
210
211 virtual void * GetNativeContext();
212
213 virtual void StrokePath( const wxGraphicsPath *p );
214 virtual void FillPath( const wxGraphicsPath *p , int fillStyle = wxWINDING_RULE );
215
216 virtual wxGraphicsPath* CreatePath();
217 virtual void SetPen( const wxPen &pen );
218 virtual void SetBrush( const wxBrush &brush );
219 virtual void SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxColour&c1, const wxColour&c2) ;
220 virtual void SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
221 const wxColour &oColor, const wxColour &cColor);
222
223 virtual void Translate( wxDouble dx , wxDouble dy );
224 virtual void Scale( wxDouble xScale , wxDouble yScale );
225 virtual void Rotate( wxDouble angle );
226
227 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
228 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
229 virtual void PushState();
230 virtual void PopState();
231
232 virtual void SetFont( const wxFont &font );
233 virtual void SetTextColor( const wxColour &col );
234 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y);
235 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
236 wxDouble *descent, wxDouble *externalLeading ) const;
237 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
238
239 private:
240 Graphics* m_context;
241 vector<GraphicsState> m_stateStack;
242 GraphicsState m_state1;
243 GraphicsState m_state2;
244
245 Pen* m_pen;
246 bool m_penTransparent;
247 Image* m_penImage;
248 Brush* m_penBrush;
249
250 Brush* m_brush;
251 bool m_brushTransparent;
252 Image* m_brushImage;
253 GraphicsPath* m_brushPath;
254
255 Brush* m_textBrush;
256 Font* m_font;
257 // wxPen m_pen;
258 // wxBrush m_brush;
259 };
260
261 wxGDIPlusPath::wxGDIPlusPath()
262 {
263 m_path = new GraphicsPath();
264 }
265
266 wxGDIPlusPath::~wxGDIPlusPath()
267 {
268 delete 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);
359 m_context->SetSmoothingMode(SmoothingModeHighQuality);
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 {
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
410 }
411
412 void wxGDIPlusContext::StrokePath( const wxGraphicsPath *path )
413 {
414 if ( m_penTransparent )
415 return;
416
417 m_context->DrawPath( m_pen , (GraphicsPath*) path->GetNativePath() );
418 }
419
420 void wxGDIPlusContext::FillPath( const wxGraphicsPath *path , int fillStyle )
421 {
422 if ( !m_brushTransparent )
423 {
424 ((GraphicsPath*) path->GetNativePath())->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding);
425 m_context->FillPath( m_brush , (GraphicsPath*) path->GetNativePath());
426 }
427 }
428
429 wxGraphicsPath* wxGDIPlusContext::CreatePath()
430 {
431 return new wxGDIPlusPath();
432 }
433
434 void wxGDIPlusContext::Rotate( wxDouble angle )
435 {
436 m_context->RotateTransform( RadToDeg(angle) );
437 }
438
439 void wxGDIPlusContext::Translate( wxDouble dx , wxDouble dy )
440 {
441 m_context->TranslateTransform( dx , dy );
442 }
443
444 void wxGDIPlusContext::Scale( wxDouble xScale , wxDouble yScale )
445 {
446 m_context->ScaleTransform(xScale,yScale);
447 }
448
449 void wxGDIPlusContext::PushState()
450 {
451 GraphicsState state = m_context->Save();
452 m_stateStack.push_back(state);
453 }
454
455 void wxGDIPlusContext::PopState()
456 {
457 GraphicsState state = m_stateStack.back();
458 m_stateStack.pop_back();
459 m_context->Restore(state);
460 }
461
462 void wxGDIPlusContext::SetTextColor( const wxColour &col )
463 {
464 delete m_textBrush;
465 m_textBrush = new SolidBrush( Color( col.Alpha() , col.Red() ,
466 col.Green() , col.Blue() ));
467 }
468
469 void wxGDIPlusContext::SetPen( const wxPen &pen )
470 {
471 m_penTransparent = pen.GetStyle() == wxTRANSPARENT;
472 if ( m_penTransparent )
473 return;
474
475 m_pen->SetColor( Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
476 pen.GetColour().Green() , pen.GetColour().Blue() ) );
477
478 // TODO: * m_dc->m_scaleX
479 double penWidth = pen.GetWidth();
480 if (penWidth <= 0.0)
481 penWidth = 0.1;
482
483 m_pen->SetWidth(penWidth);
484
485 LineCap cap;
486 switch ( pen.GetCap() )
487 {
488 case wxCAP_ROUND :
489 cap = LineCapRound;
490 break;
491
492 case wxCAP_PROJECTING :
493 cap = LineCapSquare;
494 break;
495
496 case wxCAP_BUTT :
497 cap = LineCapFlat; // TODO verify
498 break;
499
500 default :
501 cap = LineCapFlat;
502 break;
503 }
504 m_pen->SetLineCap(cap,cap, DashCapFlat);
505
506 LineJoin join;
507 switch ( pen.GetJoin() )
508 {
509 case wxJOIN_BEVEL :
510 join = LineJoinBevel;
511 break;
512
513 case wxJOIN_MITER :
514 join = LineJoinMiter;
515 break;
516
517 case wxJOIN_ROUND :
518 join = LineJoinRound;
519 break;
520
521 default :
522 join = LineJoinMiter;
523 break;
524 }
525
526 m_pen->SetLineJoin(join);
527
528 m_pen->SetDashStyle(DashStyleSolid);
529
530 DashStyle dashStyle = DashStyleSolid;
531 switch ( pen.GetStyle() )
532 {
533 case wxSOLID :
534 break;
535
536 case wxDOT :
537 dashStyle = DashStyleDot;
538 break;
539
540 case wxLONG_DASH :
541 dashStyle = DashStyleDash; // TODO verify
542 break;
543
544 case wxSHORT_DASH :
545 dashStyle = DashStyleDash;
546 break;
547
548 case wxDOT_DASH :
549 dashStyle = DashStyleDashDot;
550 break;
551 case wxUSER_DASH :
552 {
553 dashStyle = DashStyleCustom;
554 wxDash *dashes;
555 int count = pen.GetDashes( &dashes );
556 if ((dashes != NULL) && (count > 0))
557 {
558 REAL *userLengths = new REAL[count];
559 for ( int i = 0; i < count; ++i )
560 {
561 userLengths[i] = dashes[i];
562 }
563 m_pen->SetDashPattern( userLengths, count);
564 delete[] userLengths;
565 }
566 }
567 break;
568 case wxSTIPPLE :
569 {
570 wxBitmap* bmp = pen.GetStipple();
571 if ( bmp && bmp->Ok() )
572 {
573 wxDELETE( m_penImage );
574 wxDELETE( m_penBrush );
575 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
576 m_penBrush = new TextureBrush(m_penImage);
577 m_pen->SetBrush( m_penBrush );
578 }
579
580 }
581 break;
582 default :
583 if ( pen.GetStyle() >= wxFIRST_HATCH && pen.GetStyle() <= wxLAST_HATCH )
584 {
585 wxDELETE( m_penBrush );
586 HatchStyle style = HatchStyleHorizontal;
587 switch( pen.GetStyle() )
588 {
589 case wxBDIAGONAL_HATCH :
590 style = HatchStyleBackwardDiagonal;
591 break ;
592 case wxCROSSDIAG_HATCH :
593 style = HatchStyleDiagonalCross;
594 break ;
595 case wxFDIAGONAL_HATCH :
596 style = HatchStyleForwardDiagonal;
597 break ;
598 case wxCROSS_HATCH :
599 style = HatchStyleCross;
600 break ;
601 case wxHORIZONTAL_HATCH :
602 style = HatchStyleHorizontal;
603 break ;
604 case wxVERTICAL_HATCH :
605 style = HatchStyleVertical;
606 break ;
607
608 }
609 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
610 pen.GetColour().Green() , pen.GetColour().Blue() ), Color::Transparent );
611 m_pen->SetBrush( m_penBrush );
612 }
613 break;
614 }
615 if ( dashStyle != DashStyleSolid )
616 m_pen->SetDashStyle(dashStyle);
617 }
618
619 void wxGDIPlusContext::SetBrush( const wxBrush &brush )
620 {
621 // m_brush = brush;
622 if ( m_context == NULL )
623 return;
624
625 m_brushTransparent = brush.GetStyle() == wxTRANSPARENT;
626
627 if ( m_brushTransparent )
628 return;
629
630 wxDELETE(m_brush);
631
632 if ( brush.GetStyle() == wxSOLID)
633 {
634 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
635 brush.GetColour().Green() , brush.GetColour().Blue() ) );
636 }
637 else if ( brush.IsHatch() )
638 {
639 HatchStyle style = HatchStyleHorizontal;
640 switch( brush.GetStyle() )
641 {
642 case wxBDIAGONAL_HATCH :
643 style = HatchStyleBackwardDiagonal;
644 break ;
645 case wxCROSSDIAG_HATCH :
646 style = HatchStyleDiagonalCross;
647 break ;
648 case wxFDIAGONAL_HATCH :
649 style = HatchStyleForwardDiagonal;
650 break ;
651 case wxCROSS_HATCH :
652 style = HatchStyleCross;
653 break ;
654 case wxHORIZONTAL_HATCH :
655 style = HatchStyleHorizontal;
656 break ;
657 case wxVERTICAL_HATCH :
658 style = HatchStyleVertical;
659 break ;
660
661 }
662 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
663 brush.GetColour().Green() , brush.GetColour().Blue() ), Color::Transparent );
664 }
665 else
666 {
667 wxBitmap* bmp = brush.GetStipple();
668 if ( bmp && bmp->Ok() )
669 {
670 wxDELETE( m_brushImage );
671 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
672 m_brush = new TextureBrush(m_brushImage);
673 }
674 }
675 }
676
677 void wxGDIPlusContext::SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxColour&c1, const wxColour&c2)
678 {
679 m_brushTransparent = false ;
680
681 wxDELETE(m_brush);
682
683 m_brush = new LinearGradientBrush( PointF( x1,y1) , PointF( x2,y2),
684 Color( c1.Alpha(), c1.Red(),c1.Green() , c1.Blue() ),
685 Color( c2.Alpha(), c2.Red(),c2.Green() , c2.Blue() ));
686 }
687
688 void wxGDIPlusContext::SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
689 const wxColour &oColor, const wxColour &cColor)
690 {
691 m_brushTransparent = false ;
692
693 wxDELETE(m_brush);
694 wxDELETE(m_brushPath);
695
696 // Create a path that consists of a single circle.
697 m_brushPath = new GraphicsPath();
698 m_brushPath->AddEllipse( (REAL)(xc-radius), (REAL)(yc-radius), (REAL)(2*radius), (REAL)(2*radius));
699
700 PathGradientBrush *b = new PathGradientBrush(m_brushPath);
701 m_brush = b;
702 b->SetCenterPoint( PointF(xo,yo));
703 b->SetCenterColor(Color( oColor.Alpha(), oColor.Red(),oColor.Green() , oColor.Blue() ));
704
705 Color colors[] = {Color( cColor.Alpha(), cColor.Red(),cColor.Green() , cColor.Blue() )};
706 int count = 1;
707 b->SetSurroundColors(colors, &count);
708 }
709
710 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
711 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
712 // bytes as parameter
713
714 void wxGDIPlusContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
715 {
716 Bitmap* image = NULL;
717 Bitmap* helper = NULL;
718 if ( bmp.GetMask() )
719 {
720 Bitmap interim((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE()) ;
721
722 size_t width = interim.GetWidth();
723 size_t height = interim.GetHeight();
724 Rect bounds(0,0,width,height);
725
726 image = new Bitmap(width,height,PixelFormat32bppPARGB) ;
727
728 Bitmap interimMask((HBITMAP)bmp.GetMask()->GetMaskBitmap(),NULL);
729 wxASSERT(interimMask.GetPixelFormat() == PixelFormat1bppIndexed);
730
731 BitmapData dataMask ;
732 interimMask.LockBits(&bounds,ImageLockModeRead,
733 interimMask.GetPixelFormat(),&dataMask);
734
735
736 BitmapData imageData ;
737 image->LockBits(&bounds,ImageLockModeWrite, PixelFormat32bppPARGB, &imageData);
738
739 BYTE maskPattern = 0 ;
740 BYTE maskByte = 0;
741 size_t maskIndex ;
742
743 for ( size_t y = 0 ; y < height ; ++y)
744 {
745 maskIndex = 0 ;
746 for( size_t x = 0 ; x < width; ++x)
747 {
748 if ( x % 8 == 0)
749 {
750 maskPattern = 0x80;
751 maskByte = *((BYTE*)dataMask.Scan0 + dataMask.Stride*y + maskIndex);
752 maskIndex++;
753 }
754 else
755 maskPattern = maskPattern >> 1;
756
757 ARGB *dest = (ARGB*)((BYTE*)imageData.Scan0 + imageData.Stride*y + x*4);
758 if ( (maskByte & maskPattern) == 0 )
759 *dest = 0x00000000;
760 else
761 {
762 Color c ;
763 interim.GetPixel(x,y,&c) ;
764 *dest = (c.GetValue() | Color::AlphaMask);
765 }
766 }
767 }
768
769 image->UnlockBits(&imageData);
770
771 interimMask.UnlockBits(&dataMask);
772 interim.UnlockBits(&dataMask);
773 }
774 else
775 {
776 image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
777 if ( GetPixelFormatSize(image->GetPixelFormat()) == 32 )
778 {
779 size_t width = image->GetWidth();
780 size_t height = image->GetHeight();
781 Rect bounds(0,0,width,height);
782 BitmapData data ;
783
784 helper = image ;
785 image = NULL ;
786 helper->LockBits(&bounds, ImageLockModeRead,
787 helper->GetPixelFormat(),&data);
788
789 image = new Bitmap(data.Width, data.Height, data.Stride,
790 PixelFormat32bppARGB , (BYTE*) data.Scan0);
791
792 helper->UnlockBits(&data);
793 }
794 }
795 if ( image )
796 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
797 delete image ;
798 delete helper ;
799 }
800
801 void wxGDIPlusContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
802 {
803 HICON hIcon = (HICON)icon.GetHICON();
804 ICONINFO iconInfo ;
805 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
806 if (!GetIconInfo(hIcon,&iconInfo))
807 return;
808
809 BITMAP iconBmpData ;
810 GetObject(iconInfo.hbmColor,sizeof(BITMAP),&iconBmpData);
811 Bitmap interim(iconInfo.hbmColor,NULL);
812
813 Bitmap* image = NULL ;
814
815 if( GetPixelFormatSize(interim.GetPixelFormat())!= 32 )
816 {
817 image = Bitmap::FromHICON(hIcon);
818 }
819 else
820 {
821 size_t width = interim.GetWidth();
822 size_t height = interim.GetHeight();
823 Rect bounds(0,0,width,height);
824 BitmapData data ;
825
826 interim.LockBits(&bounds, ImageLockModeRead,
827 interim.GetPixelFormat(),&data);
828 image = new Bitmap(data.Width, data.Height, data.Stride,
829 PixelFormat32bppARGB , (BYTE*) data.Scan0);
830 interim.UnlockBits(&data);
831 }
832
833 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
834
835 delete image ;
836 DeleteObject(iconInfo.hbmColor);
837 DeleteObject(iconInfo.hbmMask);
838 }
839
840
841 void wxGDIPlusContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
842 {
843 if ( str.IsEmpty())
844 return ;
845
846 wxWCharBuffer s = str.wc_str( *wxConvUI );
847 m_context->DrawString( s , -1 , m_font , PointF( x , y ) , m_textBrush );
848 // TODO m_backgroundMode == wxSOLID
849 }
850
851 void wxGDIPlusContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
852 wxDouble *descent, wxDouble *externalLeading ) const
853 {
854 wxWCharBuffer s = str.wc_str( *wxConvUI );
855 FontFamily ffamily ;
856
857 m_font->GetFamily(&ffamily) ;
858
859 REAL factorY = m_context->GetDpiY() / 72.0 ;
860
861 REAL rDescent = ffamily.GetCellDescent(FontStyleRegular) *
862 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
863 REAL rAscent = ffamily.GetCellAscent(FontStyleRegular) *
864 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
865 REAL rHeight = ffamily.GetLineSpacing(FontStyleRegular) *
866 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
867
868 if ( height )
869 *height = rHeight * factorY + 0.5 ;
870 if ( descent )
871 *descent = rDescent * factorY + 0.5 ;
872 if ( externalLeading )
873 *externalLeading = (rHeight - rAscent - rDescent) * factorY + 0.5 ;
874 // measuring empty strings is not guaranteed, so do it by hand
875 if ( str.IsEmpty())
876 {
877 if ( width )
878 *width = 0 ;
879 }
880 else
881 {
882 // MeasureString does return a rectangle that is way too large, so it is
883 // not usable here
884 RectF layoutRect(0,0, 100000.0f, 100000.0f);
885 StringFormat strFormat;
886 CharacterRange strRange(0,wcslen(s));
887 strFormat.SetMeasurableCharacterRanges(1,&strRange);
888 Region region ;
889 m_context->MeasureCharacterRanges(s, -1 , m_font,layoutRect, &strFormat,1,&region) ;
890 RectF bbox ;
891 region.GetBounds(&bbox,m_context);
892 if ( width )
893 *width = bbox.GetRight()-bbox.GetLeft()+0.5;
894 }
895 }
896
897 void wxGDIPlusContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
898 {
899 widths.Empty();
900 widths.Add(0, text.length());
901
902 if (text.empty())
903 return;
904
905 wxWCharBuffer ws = text.wc_str( *wxConvUI );
906 size_t len = wcslen( ws ) ;
907 wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations"));
908
909 RectF layoutRect(0,0, 100000.0f, 100000.0f);
910 StringFormat strFormat;
911
912 CharacterRange* ranges = new CharacterRange[len] ;
913 Region* regions = new Region[len];
914 for( size_t i = 0 ; i < len ; ++i)
915 {
916 ranges[i].First = i ;
917 ranges[i].Length = 1 ;
918 }
919 strFormat.SetMeasurableCharacterRanges(len,ranges);
920 m_context->MeasureCharacterRanges(ws, -1 , m_font,layoutRect, &strFormat,1,regions) ;
921
922 RectF bbox ;
923 for ( size_t i = 0 ; i < len ; ++i)
924 {
925 regions[i].GetBounds(&bbox,m_context);
926 widths[i] = bbox.GetRight()-bbox.GetLeft();
927 }
928 }
929
930 void wxGDIPlusContext::SetFont( const wxFont &font )
931 {
932 wxASSERT( font.Ok());
933 delete m_font;
934 wxWCharBuffer s = font.GetFaceName().wc_str( *wxConvUI );
935 int size = font.GetPointSize();
936 int style = FontStyleRegular;
937 if ( font.GetStyle() == wxFONTSTYLE_ITALIC )
938 style |= FontStyleItalic;
939 if ( font.GetUnderlined() )
940 style |= FontStyleUnderline;
941 if ( font.GetWeight() == wxFONTWEIGHT_BOLD )
942 style |= FontStyleBold;
943 m_font = new Font( s , size , style );
944 }
945
946 void* wxGDIPlusContext::GetNativeContext()
947 {
948 return m_context;
949 }
950
951 wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC& dc)
952 {
953 return new wxGDIPlusContext( (HDC) dc.GetHDC() );
954 }
955
956 wxGraphicsContext* wxGraphicsContext::Create( wxWindow * window )
957 {
958 return NULL; // TODO
959 }
960
961 wxGraphicsContext* wxGraphicsContext::CreateFromNative( void * context )
962 {
963 return NULL; // TODO
964 }
965
966
967
968 #endif // wxUSE_GRAPHICS_CONTEXT