]> git.saurik.com Git - wxWidgets.git/blob - src/msw/graphics.cpp
fixed unused var warning
[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 #if wxUSE_GRAPHICS_CONTEXT
42
43 #include <vector>
44
45 using namespace std;
46
47 //-----------------------------------------------------------------------------
48 // constants
49 //-----------------------------------------------------------------------------
50
51 const double RAD2DEG = 180.0 / M_PI;
52
53 //-----------------------------------------------------------------------------
54 // Local functions
55 //-----------------------------------------------------------------------------
56
57 static inline double dmin(double a, double b) { return a < b ? a : b; }
58 static inline double dmax(double a, double b) { return a > b ? a : b; }
59
60 static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
61 static inline double RadToDeg(double deg) { return (deg * 180.0) / M_PI; }
62
63 //-----------------------------------------------------------------------------
64 // device context implementation
65 //
66 // more and more of the dc functionality should be implemented by calling
67 // the appropricate wxGDIPlusContext, but we will have to do that step by step
68 // also coordinate conversions should be moved to native matrix ops
69 //-----------------------------------------------------------------------------
70
71 // we always stock two context states, one at entry, to be able to preserve the
72 // state we were called with, the other one after changing to HI Graphics orientation
73 // (this one is used for getting back clippings etc)
74
75 //-----------------------------------------------------------------------------
76 // wxGraphicsPath implementation
77 //-----------------------------------------------------------------------------
78
79 #include "wx/msw/private.h" // needs to be before #include <commdlg.h>
80
81 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
82 #include <commdlg.h>
83 #endif
84
85 // TODO remove this dependency (gdiplus needs the macros)
86
87 #ifndef max
88 #define max(a,b) (((a) > (b)) ? (a) : (b))
89 #endif
90
91 #ifndef min
92 #define min(a,b) (((a) < (b)) ? (a) : (b))
93 #endif
94
95 #include "gdiplus.h"
96 using namespace Gdiplus;
97
98 class GDILoader
99 {
100 public :
101 GDILoader()
102 {
103 m_loaded = false;
104 m_gditoken = NULL;
105 }
106
107 ~GDILoader()
108 {
109 if (m_loaded)
110 {
111 Unload();
112 }
113 }
114 void EnsureIsLoaded()
115 {
116 if (!m_loaded)
117 {
118 Load();
119 }
120 }
121 void Load()
122 {
123 GdiplusStartupInput input;
124 GdiplusStartupOutput output;
125 GdiplusStartup(&m_gditoken,&input,&output);
126 m_loaded = true;
127 }
128 void Unload()
129 {
130 if ( m_gditoken )
131 GdiplusShutdown(m_gditoken);
132 }
133 private :
134 bool m_loaded;
135 DWORD m_gditoken;
136
137 };
138
139 static GDILoader gGDILoader;
140
141 class WXDLLEXPORT wxGDIPlusPath : public wxGraphicsPath
142 {
143 DECLARE_NO_COPY_CLASS(wxGDIPlusPath)
144 public :
145 wxGDIPlusPath();
146 ~wxGDIPlusPath();
147
148
149 //
150 // These are the path primitives from which everything else can be constructed
151 //
152
153 // begins a new subpath at (x,y)
154 virtual void MoveToPoint( wxDouble x, wxDouble y );
155
156 // adds a straight line from the current point to (x,y)
157 virtual void AddLineToPoint( wxDouble x, wxDouble y );
158
159 // adds a cubic Bezier curve from the current point, using two control points and an end point
160 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
161
162
163 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
164 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ;
165
166 // gets the last point of the current path, (0,0) if not yet set
167 virtual void GetCurrentPoint( wxDouble& x, wxDouble&y) ;
168
169 // closes the current sub-path
170 virtual void CloseSubpath();
171
172 //
173 // These are convenience functions which - if not available natively will be assembled
174 // using the primitives from above
175 //
176
177 // appends a rectangle as a new closed subpath
178 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
179 /*
180
181 // appends an ellipsis as a new closed subpath fitting the passed rectangle
182 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
183
184 // 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)
185 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
186 */
187
188 GraphicsPath* GetPath() const;
189 private :
190 GraphicsPath* m_path;
191 };
192
193 class WXDLLEXPORT wxGDIPlusContext : public wxGraphicsContext
194 {
195 DECLARE_NO_COPY_CLASS(wxGDIPlusContext)
196
197 public:
198 wxGDIPlusContext( WXHDC hdc );
199 wxGDIPlusContext();
200 virtual ~wxGDIPlusContext();
201
202 virtual void Clip( const wxRegion &region );
203 virtual void StrokePath( const wxGraphicsPath *p );
204 virtual void FillPath( const wxGraphicsPath *p , int fillStyle = wxWINDING_RULE );
205
206 virtual wxGraphicsPath* CreatePath();
207 virtual void SetPen( const wxPen &pen );
208 virtual void SetBrush( const wxBrush &brush );
209 virtual void SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxColour&c1, const wxColour&c2) ;
210 virtual void SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
211 const wxColour &oColor, const wxColour &cColor);
212
213 virtual void Translate( wxDouble dx , wxDouble dy );
214 virtual void Scale( wxDouble xScale , wxDouble yScale );
215 virtual void Rotate( wxDouble angle );
216
217 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
218 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
219 virtual void PushState();
220 virtual void PopState();
221
222 virtual void SetFont( const wxFont &font );
223 virtual void SetTextColor( const wxColour &col );
224 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y);
225 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
226 wxDouble *descent, wxDouble *externalLeading ) const;
227 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
228
229 private:
230 Graphics* m_context;
231 vector<GraphicsState> m_stateStack;
232 GraphicsState m_state1;
233 GraphicsState m_state2;
234
235 Pen* m_pen;
236 bool m_penTransparent;
237 Image* m_penImage;
238 Brush* m_penBrush;
239
240 Brush* m_brush;
241 bool m_brushTransparent;
242 Image* m_brushImage;
243 GraphicsPath* m_brushPath;
244
245 Brush* m_textBrush;
246 Font* m_font;
247 // wxPen m_pen;
248 // wxBrush m_brush;
249 };
250
251 wxGDIPlusPath::wxGDIPlusPath()
252 {
253 m_path = new GraphicsPath();
254 }
255
256 wxGDIPlusPath::~wxGDIPlusPath()
257 {
258 delete m_path;
259 }
260
261 GraphicsPath* wxGDIPlusPath::GetPath() const
262 {
263 return m_path;
264 }
265
266 //
267 // The Primitives
268 //
269
270 void wxGDIPlusPath::MoveToPoint( wxDouble x , wxDouble y )
271 {
272 m_path->StartFigure();
273 m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y);
274 }
275
276 void wxGDIPlusPath::AddLineToPoint( wxDouble x , wxDouble y )
277 {
278 m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y);
279 }
280
281 void wxGDIPlusPath::CloseSubpath()
282 {
283 m_path->CloseFigure();
284 }
285
286 void wxGDIPlusPath::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
287 {
288 PointF c1(cx1,cy1);
289 PointF c2(cx2,cy2);
290 PointF end(x,y);
291 PointF start;
292 m_path->GetLastPoint(&start);
293 m_path->AddBezier(start,c1,c2,end);
294 }
295
296 // gets the last point of the current path, (0,0) if not yet set
297 void wxGDIPlusPath::GetCurrentPoint( wxDouble& x, wxDouble&y)
298 {
299 PointF start;
300 m_path->GetLastPoint(&start);
301 x = start.X ;
302 y = start.Y ;
303 }
304
305 void wxGDIPlusPath::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
306 {
307 double sweepAngle = endAngle - startAngle ;
308 if( abs(sweepAngle) >= 2*M_PI)
309 {
310 sweepAngle = 2 * M_PI;
311 }
312 else
313 {
314 if ( clockwise )
315 {
316 if( sweepAngle < 0 )
317 sweepAngle += 2 * M_PI;
318 }
319 else
320 {
321 if( sweepAngle > 0 )
322 sweepAngle -= 2 * M_PI;
323
324 }
325 }
326 m_path->AddArc((REAL) (x-r),(REAL) (y-r),(REAL) (2*r),(REAL) (2*r),RadToDeg(startAngle),RadToDeg(sweepAngle));
327 }
328
329 void wxGDIPlusPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
330 {
331 m_path->AddRectangle(RectF(x,y,w,h));
332 }
333
334 //
335 //
336 //
337 /*
338 // closes the current subpath
339 void wxGDIPlusPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
340 {
341 // CGPathAddArcToPoint( m_path, NULL , x1, y1, x2, y2, r);
342 }
343
344 */
345
346 //-----------------------------------------------------------------------------
347 // wxGDIPlusContext implementation
348 //-----------------------------------------------------------------------------
349
350 wxGDIPlusContext::wxGDIPlusContext( WXHDC hdc )
351 {
352 gGDILoader.EnsureIsLoaded();
353 m_context = new Graphics( (HDC) hdc);
354 m_state1 = m_context->Save();
355 m_state2 = m_context->Save();
356
357 // set defaults
358
359 m_penTransparent = false;
360 m_pen = new Pen((ARGB)Color::Black);
361 m_penImage = NULL;
362 m_penBrush = NULL;
363
364 m_brushTransparent = false;
365 m_brush = new SolidBrush((ARGB)Color::White);
366 m_brushImage = NULL;
367 m_brushPath = NULL;
368 m_textBrush = new SolidBrush((ARGB)Color::Black);
369 m_font = new Font( L"Arial" , 9 , FontStyleRegular );
370 }
371
372 wxGDIPlusContext::~wxGDIPlusContext()
373 {
374 if ( m_context )
375 {
376 m_context->Restore( m_state2 );
377 m_context->Restore( m_state1 );
378 delete m_context;
379 delete m_pen;
380 delete m_penImage;
381 delete m_penBrush;
382 delete m_brush;
383 delete m_brushImage;
384 delete m_brushPath;
385 delete m_textBrush;
386 delete m_font;
387 }
388 }
389
390
391 void wxGDIPlusContext::Clip( const wxRegion & WXUNUSED(region) )
392 {
393 // ClipCGContextToRegion ( m_context, &bounds , (RgnHandle) dc->m_macCurrentClipRgn );
394 }
395
396 void wxGDIPlusContext::StrokePath( const wxGraphicsPath *p )
397 {
398 if ( m_penTransparent )
399 return;
400
401 const wxGDIPlusPath* path = dynamic_cast< const wxGDIPlusPath*>( p );
402 m_context->DrawPath( m_pen , path->GetPath() );
403 }
404
405 void wxGDIPlusContext::FillPath( const wxGraphicsPath *p , int fillStyle )
406 {
407 if ( !m_brushTransparent )
408 {
409 const wxGDIPlusPath* path = dynamic_cast< const wxGDIPlusPath*>( p );
410 path->GetPath()->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding);
411 m_context->FillPath( m_brush , path->GetPath() );
412 }
413 }
414
415 wxGraphicsPath* wxGDIPlusContext::CreatePath()
416 {
417 return new wxGDIPlusPath();
418 }
419
420 void wxGDIPlusContext::Rotate( wxDouble angle )
421 {
422 m_context->RotateTransform( angle );
423 }
424
425 void wxGDIPlusContext::Translate( wxDouble dx , wxDouble dy )
426 {
427 m_context->TranslateTransform( dx , dy );
428 }
429
430 void wxGDIPlusContext::Scale( wxDouble xScale , wxDouble yScale )
431 {
432 PointF penWidth( m_pen->GetWidth(), 0);
433 Matrix matrix ;
434 if ( !m_penTransparent )
435 {
436 m_context->GetTransform(&matrix);
437 matrix.TransformVectors(&penWidth);
438 }
439 m_context->ScaleTransform(xScale,yScale);
440 if ( !m_penTransparent )
441 {
442 m_context->GetTransform(&matrix);
443 matrix.Invert();
444 matrix.TransformVectors(&penWidth) ;
445 m_pen->SetWidth( sqrt( penWidth.X*penWidth.X + penWidth.Y*penWidth.Y));
446 }
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 wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC& dc)
947 {
948 return new wxGDIPlusContext( (HDC) dc.GetHDC() );
949 }
950
951
952 #endif // wxUSE_GRAPHICS_CONTEXT