1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dcbase.cpp
3 // Purpose: generic methods of the wxDC Class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/dcclient.h"
29 #include "wx/dcmemory.h"
30 #include "wx/dcscreen.h"
31 #include "wx/dcprint.h"
32 #include "wx/prntbase.h"
36 #include "wx/module.h"
40 #include "wx/msw/dcclient.h"
41 #include "wx/msw/dcmemory.h"
42 #include "wx/msw/dcscreen.h"
46 #include "wx/gtk/dcclient.h"
47 #include "wx/gtk/dcmemory.h"
48 #include "wx/gtk/dcscreen.h"
49 #elif defined(__WXGTK__)
50 #include "wx/gtk1/dcclient.h"
51 #include "wx/gtk1/dcmemory.h"
52 #include "wx/gtk1/dcscreen.h"
56 #include "wx/osx/dcclient.h"
57 #include "wx/osx/dcmemory.h"
58 #include "wx/osx/dcscreen.h"
62 #include "wx/os2/dcclient.h"
63 #include "wx/os2/dcmemory.h"
64 #include "wx/os2/dcscreen.h"
68 #include "wx/cocoa/dcclient.h"
69 #include "wx/cocoa/dcmemory.h"
70 #include "wx/cocoa/dcscreen.h"
74 #include "wx/motif/dcclient.h"
75 #include "wx/motif/dcmemory.h"
76 #include "wx/motif/dcscreen.h"
80 #include "wx/x11/dcclient.h"
81 #include "wx/x11/dcmemory.h"
82 #include "wx/x11/dcscreen.h"
86 #include "wx/dfb/dcclient.h"
87 #include "wx/dfb/dcmemory.h"
88 #include "wx/dfb/dcscreen.h"
92 #include "wx/palmos/dcclient.h"
93 #include "wx/palmos/dcmemory.h"
94 #include "wx/palmos/dcscreen.h"
97 //----------------------------------------------------------------------------
99 //----------------------------------------------------------------------------
101 wxDCFactory
*wxDCFactory::m_factory
= NULL
;
103 void wxDCFactory::Set(wxDCFactory
*factory
)
110 wxDCFactory
*wxDCFactory::Get()
113 m_factory
= new wxNativeDCFactory
;
118 class wxDCFactoryCleanupModule
: public wxModule
121 virtual bool OnInit() { return true; }
122 virtual void OnExit() { wxDCFactory::Set(NULL
); }
125 DECLARE_DYNAMIC_CLASS(wxDCFactoryCleanupModule
)
128 IMPLEMENT_DYNAMIC_CLASS(wxDCFactoryCleanupModule
, wxModule
)
130 //-----------------------------------------------------------------------------
132 //-----------------------------------------------------------------------------
134 wxDCImpl
* wxNativeDCFactory::CreateWindowDC( wxWindowDC
*owner
, wxWindow
*window
)
136 wxDCImpl
* const impl
= new wxWindowDCImpl( owner
, window
);
137 impl
->InheritAttributes(window
);
141 wxDCImpl
* wxNativeDCFactory::CreateClientDC( wxClientDC
*owner
, wxWindow
*window
)
143 wxDCImpl
* const impl
= new wxClientDCImpl( owner
, window
);
144 impl
->InheritAttributes(window
);
148 wxDCImpl
* wxNativeDCFactory::CreatePaintDC( wxPaintDC
*owner
, wxWindow
*window
)
150 wxDCImpl
* const impl
= new wxPaintDCImpl( owner
, window
);
151 impl
->InheritAttributes(window
);
155 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
)
157 return new wxMemoryDCImpl( owner
);
160 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
, wxBitmap
&bitmap
)
162 return new wxMemoryDCImpl( owner
, bitmap
);
165 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
, wxDC
*dc
)
167 return new wxMemoryDCImpl( owner
, dc
);
170 wxDCImpl
* wxNativeDCFactory::CreateScreenDC( wxScreenDC
*owner
)
172 return new wxScreenDCImpl( owner
);
175 #if wxUSE_PRINTING_ARCHITECTURE
176 wxDCImpl
*wxNativeDCFactory::CreatePrinterDC( wxPrinterDC
*owner
, const wxPrintData
&data
)
178 wxPrintFactory
*factory
= wxPrintFactory::GetFactory();
179 return factory
->CreatePrinterDCImpl( owner
, data
);
183 //-----------------------------------------------------------------------------
185 //-----------------------------------------------------------------------------
187 IMPLEMENT_ABSTRACT_CLASS(wxWindowDC
, wxDC
)
189 wxWindowDC::wxWindowDC(wxWindow
*win
)
190 : wxDC(wxDCFactory::Get()->CreateWindowDC(this, win
))
194 //-----------------------------------------------------------------------------
196 //-----------------------------------------------------------------------------
198 IMPLEMENT_ABSTRACT_CLASS(wxClientDC
, wxWindowDC
)
200 wxClientDC::wxClientDC(wxWindow
*win
)
201 : wxWindowDC(wxDCFactory::Get()->CreateClientDC(this, win
))
205 //-----------------------------------------------------------------------------
207 //-----------------------------------------------------------------------------
209 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC
, wxDC
)
211 wxMemoryDC::wxMemoryDC()
212 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this))
216 wxMemoryDC::wxMemoryDC(wxBitmap
& bitmap
)
217 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, bitmap
))
221 wxMemoryDC::wxMemoryDC(wxDC
*dc
)
222 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, dc
))
226 void wxMemoryDC::SelectObject(wxBitmap
& bmp
)
228 // make sure that the given wxBitmap is not sharing its data with other
229 // wxBitmap instances as its contents will be modified by any drawing
230 // operation done on this DC
234 GetImpl()->DoSelect(bmp
);
237 void wxMemoryDC::SelectObjectAsSource(const wxBitmap
& bmp
)
239 GetImpl()->DoSelect(bmp
);
242 const wxBitmap
& wxMemoryDC::GetSelectedBitmap() const
244 return GetImpl()->GetSelectedBitmap();
247 wxBitmap
& wxMemoryDC::GetSelectedBitmap()
249 return GetImpl()->GetSelectedBitmap();
253 //-----------------------------------------------------------------------------
255 //-----------------------------------------------------------------------------
257 IMPLEMENT_ABSTRACT_CLASS(wxPaintDC
, wxClientDC
)
259 wxPaintDC::wxPaintDC(wxWindow
*win
)
260 : wxClientDC(wxDCFactory::Get()->CreatePaintDC(this, win
))
264 //-----------------------------------------------------------------------------
266 //-----------------------------------------------------------------------------
268 IMPLEMENT_DYNAMIC_CLASS(wxScreenDC
, wxWindowDC
)
270 wxScreenDC::wxScreenDC()
271 : wxDC(wxDCFactory::Get()->CreateScreenDC(this))
275 //-----------------------------------------------------------------------------
277 //-----------------------------------------------------------------------------
279 #if wxUSE_PRINTING_ARCHITECTURE
281 IMPLEMENT_DYNAMIC_CLASS(wxPrinterDC
, wxDC
)
283 wxPrinterDC::wxPrinterDC()
284 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, wxPrintData()))
288 wxPrinterDC::wxPrinterDC(const wxPrintData
& data
)
289 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, data
))
293 wxRect
wxPrinterDC::GetPaperRect()
295 return GetImpl()->GetPaperRect();
298 int wxPrinterDC::GetResolution()
300 return GetImpl()->GetResolution();
303 #endif // wxUSE_PRINTING_ARCHITECTURE
305 //-----------------------------------------------------------------------------
307 //-----------------------------------------------------------------------------
309 IMPLEMENT_ABSTRACT_CLASS(wxDCImpl
, wxObject
)
311 wxDCImpl::wxDCImpl( wxDC
*owner
)
313 , m_colour(wxColourDisplay())
317 , m_isBBoxValid(false)
318 , m_logicalOriginX(0), m_logicalOriginY(0)
319 , m_deviceOriginX(0), m_deviceOriginY(0)
320 , m_deviceLocalOriginX(0), m_deviceLocalOriginY(0)
321 , m_logicalScaleX(1.0), m_logicalScaleY(1.0)
322 , m_userScaleX(1.0), m_userScaleY(1.0)
323 , m_scaleX(1.0), m_scaleY(1.0)
324 , m_signX(1), m_signY(1)
325 , m_minX(0), m_minY(0), m_maxX(0), m_maxY(0)
326 , m_clipX1(0), m_clipY1(0), m_clipX2(0), m_clipY2(0)
327 , m_logicalFunction(wxCOPY
)
328 , m_backgroundMode(wxBRUSHSTYLE_TRANSPARENT
)
329 , m_mappingMode(wxMM_TEXT
)
332 , m_backgroundBrush()
333 , m_textForegroundColour(*wxBLACK
)
334 , m_textBackgroundColour(*wxWHITE
)
338 , m_hasCustomPalette(false)
339 #endif // wxUSE_PALETTE
343 m_mm_to_pix_x
= (double)wxGetDisplaySize().GetWidth() /
344 (double)wxGetDisplaySizeMM().GetWidth();
345 m_mm_to_pix_y
= (double)wxGetDisplaySize().GetHeight() /
346 (double)wxGetDisplaySizeMM().GetHeight();
352 wxDCImpl::~wxDCImpl()
356 // ----------------------------------------------------------------------------
357 // coordinate conversions and transforms
358 // ----------------------------------------------------------------------------
360 wxCoord
wxDCImpl::DeviceToLogicalX(wxCoord x
) const
362 return wxRound( (double)((x
- m_deviceOriginX
- m_deviceLocalOriginX
) * m_signX
) / m_scaleX
) + m_logicalOriginX
;
365 wxCoord
wxDCImpl::DeviceToLogicalY(wxCoord y
) const
367 return wxRound( (double)((y
- m_deviceOriginY
- m_deviceLocalOriginY
) * m_signY
) / m_scaleY
) + m_logicalOriginY
;
370 wxCoord
wxDCImpl::DeviceToLogicalXRel(wxCoord x
) const
372 return wxRound((double)(x
) / m_scaleX
);
375 wxCoord
wxDCImpl::DeviceToLogicalYRel(wxCoord y
) const
377 return wxRound((double)(y
) / m_scaleY
);
380 wxCoord
wxDCImpl::LogicalToDeviceX(wxCoord x
) const
382 return wxRound( (double)((x
- m_logicalOriginX
) * m_signX
) * m_scaleX
) + m_deviceOriginX
+ m_deviceLocalOriginX
;
385 wxCoord
wxDCImpl::LogicalToDeviceY(wxCoord y
) const
387 return wxRound( (double)((y
- m_logicalOriginY
) * m_signY
) * m_scaleY
) + m_deviceOriginY
+ m_deviceLocalOriginY
;
390 wxCoord
wxDCImpl::LogicalToDeviceXRel(wxCoord x
) const
392 return wxRound((double)(x
) * m_scaleX
);
395 wxCoord
wxDCImpl::LogicalToDeviceYRel(wxCoord y
) const
397 return wxRound((double)(y
) * m_scaleY
);
400 void wxDCImpl::ComputeScaleAndOrigin()
402 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
403 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
406 void wxDCImpl::SetMapMode( int mode
)
411 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
414 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
417 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
420 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
424 SetLogicalScale( 1.0, 1.0 );
427 m_mappingMode
= mode
;
430 void wxDCImpl::SetUserScale( double x
, double y
)
432 // allow negative ? -> no
435 ComputeScaleAndOrigin();
438 void wxDCImpl::SetLogicalScale( double x
, double y
)
443 ComputeScaleAndOrigin();
446 void wxDCImpl::SetLogicalOrigin( wxCoord x
, wxCoord y
)
448 m_logicalOriginX
= x
* m_signX
;
449 m_logicalOriginY
= y
* m_signY
;
450 ComputeScaleAndOrigin();
453 void wxDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
457 ComputeScaleAndOrigin();
460 void wxDCImpl::SetDeviceLocalOrigin( wxCoord x
, wxCoord y
)
462 m_deviceLocalOriginX
= x
;
463 m_deviceLocalOriginY
= y
;
464 ComputeScaleAndOrigin();
467 void wxDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
469 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
470 // wxWidgets 2.9: no longer override it
471 m_signX
= (xLeftRight
? 1 : -1);
472 m_signY
= (yBottomUp
? -1 : 1);
473 ComputeScaleAndOrigin();
477 // Each element of the widths array will be the width of the string up to and
478 // including the corresponding character in text. This is the generic
479 // implementation, the port-specific classes should do this with native APIs
480 // if available and if faster. Note: pango_layout_index_to_pos is much slower
481 // than calling GetTextExtent!!
488 FontWidthCache() : m_scaleX(1), m_widths(NULL
) { }
489 ~FontWidthCache() { delete []m_widths
; }
494 m_widths
= new int[FWC_SIZE
];
496 memset(m_widths
, 0, sizeof(int)*FWC_SIZE
);
504 static FontWidthCache s_fontWidthCache
;
506 bool wxDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
510 const size_t len
= text
.length();
514 // reset the cache if font or horizontal scale have changed
515 if ( !s_fontWidthCache
.m_widths
||
516 !wxIsSameDouble(s_fontWidthCache
.m_scaleX
, m_scaleX
) ||
517 (s_fontWidthCache
.m_font
!= GetFont()) )
519 s_fontWidthCache
.Reset();
520 s_fontWidthCache
.m_font
= GetFont();
521 s_fontWidthCache
.m_scaleX
= m_scaleX
;
524 // Calculate the position of each character based on the widths of
525 // the previous characters
527 for ( size_t i
= 0; i
< len
; i
++ )
529 const wxChar c
= text
[i
];
530 unsigned int c_int
= (unsigned int)c
;
532 if ((c_int
< FWC_SIZE
) && (s_fontWidthCache
.m_widths
[c_int
] != 0))
534 w
= s_fontWidthCache
.m_widths
[c_int
];
538 DoGetTextExtent(c
, &w
, &h
);
539 if (c_int
< FWC_SIZE
)
540 s_fontWidthCache
.m_widths
[c_int
] = w
;
544 widths
[i
] = totalWidth
;
550 void wxDCImpl::GetMultiLineTextExtent(const wxString
& text
,
554 const wxFont
*font
) const
556 wxCoord widthTextMax
= 0, widthLine
,
557 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
560 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
562 if ( pc
== text
.end() || *pc
== _T('\n') )
564 if ( curLine
.empty() )
566 // we can't use GetTextExtent - it will return 0 for both width
567 // and height and an empty line should count in height
570 // assume that this line has the same height as the previous
572 if ( !heightLineDefault
)
573 heightLineDefault
= heightLine
;
575 if ( !heightLineDefault
)
577 // but we don't know it yet - choose something reasonable
578 DoGetTextExtent(_T("W"), NULL
, &heightLineDefault
,
582 heightTextTotal
+= heightLineDefault
;
586 DoGetTextExtent(curLine
, &widthLine
, &heightLine
,
588 if ( widthLine
> widthTextMax
)
589 widthTextMax
= widthLine
;
590 heightTextTotal
+= heightLine
;
593 if ( pc
== text
.end() )
611 *y
= heightTextTotal
;
616 void wxDCImpl::DoDrawCheckMark(wxCoord x1
, wxCoord y1
,
617 wxCoord width
, wxCoord height
)
619 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
621 wxCoord x2
= x1
+ width
,
624 // the pen width is calibrated to give 3 for width == height == 10
625 wxDCPenChanger
pen( *m_owner
, wxPen(GetTextForeground(), (width
+ height
+ 1)/7));
627 // we're drawing a scaled version of wx/generic/tick.xpm here
628 wxCoord x3
= x1
+ (4*width
) / 10, // x of the tick bottom
629 y3
= y1
+ height
/ 2; // y of the left tick branch
630 DoDrawLine(x1
, y3
, x3
, y2
);
631 DoDrawLine(x3
, y2
, x2
, y1
);
633 CalcBoundingBox(x1
, y1
);
634 CalcBoundingBox(x2
, y2
);
638 wxDCImpl::DoStretchBlit(wxCoord xdest
, wxCoord ydest
,
639 wxCoord dstWidth
, wxCoord dstHeight
,
641 wxCoord xsrc
, wxCoord ysrc
,
642 wxCoord srcWidth
, wxCoord srcHeight
,
648 wxCHECK_MSG( srcWidth
&& srcHeight
&& dstWidth
&& dstHeight
, false,
649 _T("invalid blit size") );
651 // emulate the stretching by modifying the DC scale
652 double xscale
= (double)srcWidth
/dstWidth
,
653 yscale
= (double)srcHeight
/dstHeight
;
655 double xscaleOld
, yscaleOld
;
656 GetUserScale(&xscaleOld
, &yscaleOld
);
657 SetUserScale(xscaleOld
/xscale
, yscaleOld
/yscale
);
659 bool rc
= DoBlit(wxCoord(xdest
*xscale
), wxCoord(ydest
*yscale
),
660 wxCoord(dstWidth
*xscale
), wxCoord(dstHeight
*yscale
),
662 xsrc
, ysrc
, rop
, useMask
, xsrcMask
, ysrcMask
);
664 SetUserScale(xscaleOld
, yscaleOld
);
669 void wxDCImpl::DrawLines(const wxPointList
*list
, wxCoord xoffset
, wxCoord yoffset
)
671 int n
= list
->GetCount();
672 wxPoint
*points
= new wxPoint
[n
];
675 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
677 wxPoint
*point
= node
->GetData();
678 points
[i
].x
= point
->x
;
679 points
[i
].y
= point
->y
;
682 DoDrawLines(n
, points
, xoffset
, yoffset
);
687 void wxDCImpl::DrawPolygon(const wxPointList
*list
,
688 wxCoord xoffset
, wxCoord yoffset
,
691 int n
= list
->GetCount();
692 wxPoint
*points
= new wxPoint
[n
];
695 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
697 wxPoint
*point
= node
->GetData();
698 points
[i
].x
= point
->x
;
699 points
[i
].y
= point
->y
;
702 DoDrawPolygon(n
, points
, xoffset
, yoffset
, fillStyle
);
708 wxDCImpl::DoDrawPolyPolygon(int n
,
711 wxCoord xoffset
, wxCoord yoffset
,
716 DoDrawPolygon(count
[0], points
, xoffset
, yoffset
, fillStyle
);
724 for (i
= j
= lastOfs
= 0; i
< n
; i
++)
729 pts
= new wxPoint
[j
+n
-1];
730 for (i
= 0; i
< j
; i
++)
732 for (i
= 2; i
<= n
; i
++)
734 lastOfs
-= count
[n
-i
];
735 pts
[j
++] = pts
[lastOfs
];
739 SetPen(wxPen(*wxBLACK
, 0, wxPENSTYLE_TRANSPARENT
));
740 DoDrawPolygon(j
, pts
, xoffset
, yoffset
, fillStyle
);
742 for (i
= j
= 0; i
< n
; i
++)
744 DoDrawLines(count
[i
], pts
+j
, xoffset
, yoffset
);
752 void wxDCImpl::DrawSpline(wxCoord x1
, wxCoord y1
,
753 wxCoord x2
, wxCoord y2
,
754 wxCoord x3
, wxCoord y3
)
756 wxPoint points
[] = { wxPoint(x1
, y1
), wxPoint(x2
, y2
), wxPoint(x3
, y3
) };
757 DrawSpline(WXSIZEOF(points
), points
);
760 void wxDCImpl::DrawSpline(int n
, wxPoint points
[])
763 for ( int i
= 0; i
< n
; i
++ )
764 list
.Append(&points
[i
]);
769 // ----------------------------------- spline code ----------------------------------------
771 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
,
772 double a3
, double b3
, double a4
, double b4
);
773 void wx_clear_stack();
774 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
, double *x3
,
775 double *y3
, double *x4
, double *y4
);
776 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
,
777 double x4
, double y4
);
778 static bool wx_spline_add_point(double x
, double y
);
779 static void wx_spline_draw_point_array(wxDC
*dc
);
781 wxPointList wx_spline_point_list
;
783 #define half(z1, z2) ((z1+z2)/2.0)
786 /* iterative version */
788 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
, double a3
, double b3
, double a4
,
791 register double xmid
, ymid
;
792 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
795 wx_spline_push(a1
, b1
, a2
, b2
, a3
, b3
, a4
, b4
);
797 while (wx_spline_pop(&x1
, &y1
, &x2
, &y2
, &x3
, &y3
, &x4
, &y4
)) {
798 xmid
= (double)half(x2
, x3
);
799 ymid
= (double)half(y2
, y3
);
800 if (fabs(x1
- xmid
) < THRESHOLD
&& fabs(y1
- ymid
) < THRESHOLD
&&
801 fabs(xmid
- x4
) < THRESHOLD
&& fabs(ymid
- y4
) < THRESHOLD
) {
802 wx_spline_add_point( x1
, y1
);
803 wx_spline_add_point( xmid
, ymid
);
805 wx_spline_push(xmid
, ymid
, (double)half(xmid
, x3
), (double)half(ymid
, y3
),
806 (double)half(x3
, x4
), (double)half(y3
, y4
), x4
, y4
);
807 wx_spline_push(x1
, y1
, (double)half(x1
, x2
), (double)half(y1
, y2
),
808 (double)half(x2
, xmid
), (double)half(y2
, ymid
), xmid
, ymid
);
813 /* utilities used by spline drawing routines */
815 typedef struct wx_spline_stack_struct
{
816 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
819 #define SPLINE_STACK_DEPTH 20
820 static Stack wx_spline_stack
[SPLINE_STACK_DEPTH
];
821 static Stack
*wx_stack_top
;
822 static int wx_stack_count
;
824 void wx_clear_stack()
826 wx_stack_top
= wx_spline_stack
;
830 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
, double x4
, double y4
)
832 wx_stack_top
->x1
= x1
;
833 wx_stack_top
->y1
= y1
;
834 wx_stack_top
->x2
= x2
;
835 wx_stack_top
->y2
= y2
;
836 wx_stack_top
->x3
= x3
;
837 wx_stack_top
->y3
= y3
;
838 wx_stack_top
->x4
= x4
;
839 wx_stack_top
->y4
= y4
;
844 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
,
845 double *x3
, double *y3
, double *x4
, double *y4
)
847 if (wx_stack_count
== 0)
851 *x1
= wx_stack_top
->x1
;
852 *y1
= wx_stack_top
->y1
;
853 *x2
= wx_stack_top
->x2
;
854 *y2
= wx_stack_top
->y2
;
855 *x3
= wx_stack_top
->x3
;
856 *y3
= wx_stack_top
->y3
;
857 *x4
= wx_stack_top
->x4
;
858 *y4
= wx_stack_top
->y4
;
862 static bool wx_spline_add_point(double x
, double y
)
864 wxPoint
*point
= new wxPoint( wxRound(x
), wxRound(y
) );
865 wx_spline_point_list
.Append(point
);
869 static void wx_spline_draw_point_array(wxDC
*dc
)
871 dc
->DrawLines(&wx_spline_point_list
, 0, 0 );
872 wxPointList::compatibility_iterator node
= wx_spline_point_list
.GetFirst();
875 wxPoint
*point
= node
->GetData();
877 wx_spline_point_list
.Erase(node
);
878 node
= wx_spline_point_list
.GetFirst();
882 void wxDCImpl::DoDrawSpline( const wxPointList
*points
)
884 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
887 double cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
;
888 double x1
, y1
, x2
, y2
;
890 wxPointList::compatibility_iterator node
= points
->GetFirst();
895 p
= (wxPoint
*)node
->GetData();
900 node
= node
->GetNext();
905 cx1
= (double)((x1
+ x2
) / 2);
906 cy1
= (double)((y1
+ y2
) / 2);
907 cx2
= (double)((cx1
+ x2
) / 2);
908 cy2
= (double)((cy1
+ y2
) / 2);
910 wx_spline_add_point(x1
, y1
);
912 while ((node
= node
->GetNext())
923 cx4
= (double)(x1
+ x2
) / 2;
924 cy4
= (double)(y1
+ y2
) / 2;
925 cx3
= (double)(x1
+ cx4
) / 2;
926 cy3
= (double)(y1
+ cy4
) / 2;
928 wx_quadratic_spline(cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
);
932 cx2
= (double)(cx1
+ x2
) / 2;
933 cy2
= (double)(cy1
+ y2
) / 2;
936 wx_spline_add_point( cx1
, cy1
);
937 wx_spline_add_point( x2
, y2
);
939 wx_spline_draw_point_array( m_owner
);
942 #endif // wxUSE_SPLINES
946 void wxDCImpl::DoGradientFillLinear(const wxRect
& rect
,
947 const wxColour
& initialColour
,
948 const wxColour
& destColour
,
949 wxDirection nDirection
)
952 wxPen oldPen
= m_pen
;
953 wxBrush oldBrush
= m_brush
;
955 wxUint8 nR1
= initialColour
.Red();
956 wxUint8 nG1
= initialColour
.Green();
957 wxUint8 nB1
= initialColour
.Blue();
958 wxUint8 nR2
= destColour
.Red();
959 wxUint8 nG2
= destColour
.Green();
960 wxUint8 nB2
= destColour
.Blue();
963 if ( nDirection
== wxEAST
|| nDirection
== wxWEST
)
965 wxInt32 x
= rect
.GetWidth();
966 wxInt32 w
= x
; // width of area to shade
967 wxInt32 xDelta
= w
/256; // height of one shade bend
975 nR
= nR1
- (nR1
-nR2
)*(w
-x
)/w
;
977 nR
= nR1
+ (nR2
-nR1
)*(w
-x
)/w
;
980 nG
= nG1
- (nG1
-nG2
)*(w
-x
)/w
;
982 nG
= nG1
+ (nG2
-nG1
)*(w
-x
)/w
;
985 nB
= nB1
- (nB1
-nB2
)*(w
-x
)/w
;
987 nB
= nB1
+ (nB2
-nB1
)*(w
-x
)/w
;
989 wxColour
colour(nR
,nG
,nB
);
990 SetPen(wxPen(colour
, 1, wxPENSTYLE_SOLID
));
991 SetBrush(wxBrush(colour
));
992 if(nDirection
== wxEAST
)
993 DoDrawRectangle(rect
.GetRight()-x
-xDelta
+1, rect
.GetTop(),
994 xDelta
, rect
.GetHeight());
995 else //nDirection == wxWEST
996 DoDrawRectangle(rect
.GetLeft()+x
, rect
.GetTop(),
997 xDelta
, rect
.GetHeight());
1000 else // nDirection == wxNORTH || nDirection == wxSOUTH
1002 wxInt32 y
= rect
.GetHeight();
1003 wxInt32 w
= y
; // height of area to shade
1004 wxInt32 yDelta
= w
/255; // height of one shade bend
1012 nR
= nR1
- (nR1
-nR2
)*(w
-y
)/w
;
1014 nR
= nR1
+ (nR2
-nR1
)*(w
-y
)/w
;
1017 nG
= nG1
- (nG1
-nG2
)*(w
-y
)/w
;
1019 nG
= nG1
+ (nG2
-nG1
)*(w
-y
)/w
;
1022 nB
= nB1
- (nB1
-nB2
)*(w
-y
)/w
;
1024 nB
= nB1
+ (nB2
-nB1
)*(w
-y
)/w
;
1026 wxColour
colour(nR
,nG
,nB
);
1027 SetPen(wxPen(colour
, 1, wxPENSTYLE_SOLID
));
1028 SetBrush(wxBrush(colour
));
1029 if(nDirection
== wxNORTH
)
1030 DoDrawRectangle(rect
.GetLeft(), rect
.GetTop()+y
,
1031 rect
.GetWidth(), yDelta
);
1032 else //nDirection == wxSOUTH
1033 DoDrawRectangle(rect
.GetLeft(), rect
.GetBottom()-y
-yDelta
+1,
1034 rect
.GetWidth(), yDelta
);
1042 void wxDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1043 const wxColour
& initialColour
,
1044 const wxColour
& destColour
,
1045 const wxPoint
& circleCenter
)
1047 //save the old pen color
1048 wxColour oldPenColour
= m_pen
.GetColour();
1050 wxUint8 nR1
= destColour
.Red();
1051 wxUint8 nG1
= destColour
.Green();
1052 wxUint8 nB1
= destColour
.Blue();
1053 wxUint8 nR2
= initialColour
.Red();
1054 wxUint8 nG2
= initialColour
.Green();
1055 wxUint8 nB2
= initialColour
.Blue();
1060 wxInt32 cx
= rect
.GetWidth() / 2;
1061 wxInt32 cy
= rect
.GetHeight() / 2;
1069 wxInt32 nCircleOffX
= circleCenter
.x
- (rect
.GetWidth() / 2);
1070 wxInt32 nCircleOffY
= circleCenter
.y
- (rect
.GetHeight() / 2);
1072 for ( wxInt32 x
= 0; x
< rect
.GetWidth(); x
++ )
1074 for ( wxInt32 y
= 0; y
< rect
.GetHeight(); y
++ )
1076 //get color difference
1077 wxInt32 nGradient
= ((nRadius
-
1079 pow((double)(x
- cx
- nCircleOffX
), 2) +
1080 pow((double)(y
- cy
- nCircleOffY
), 2)
1081 )) * 100) / nRadius
;
1083 //normalize Gradient
1088 nR
= (wxUint8
)(nR1
+ ((nR2
- nR1
) * nGradient
/ 100));
1089 nG
= (wxUint8
)(nG1
+ ((nG2
- nG1
) * nGradient
/ 100));
1090 nB
= (wxUint8
)(nB1
+ ((nB2
- nB1
) * nGradient
/ 100));
1093 m_pen
.SetColour(wxColour(nR
,nG
,nB
));
1094 DoDrawPoint(x
+ rect
.GetLeft(), y
+ rect
.GetTop());
1097 //return old pen color
1098 m_pen
.SetColour(oldPenColour
);
1101 void wxDCImpl::InheritAttributes(wxWindow
*win
)
1103 wxCHECK_RET( win
, "window can't be NULL" );
1105 SetFont(win
->GetFont());
1106 SetTextForeground(win
->GetForegroundColour());
1107 SetTextBackground(win
->GetBackgroundColour());
1108 SetBackground(wxBrush(win
->GetBackgroundColour()));
1111 //-----------------------------------------------------------------------------
1113 //-----------------------------------------------------------------------------
1115 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxObject
)
1117 void wxDC::DrawLabel(const wxString
& text
,
1118 const wxBitmap
& bitmap
,
1122 wxRect
*rectBounding
)
1124 // find the text position
1125 wxCoord widthText
, heightText
, heightLine
;
1126 GetMultiLineTextExtent(text
, &widthText
, &heightText
, &heightLine
);
1128 wxCoord width
, height
;
1131 width
= widthText
+ bitmap
.GetWidth();
1132 height
= bitmap
.GetHeight();
1137 height
= heightText
;
1141 if ( alignment
& wxALIGN_RIGHT
)
1143 x
= rect
.GetRight() - width
;
1145 else if ( alignment
& wxALIGN_CENTRE_HORIZONTAL
)
1147 x
= (rect
.GetLeft() + rect
.GetRight() + 1 - width
) / 2;
1149 else // alignment & wxALIGN_LEFT
1154 if ( alignment
& wxALIGN_BOTTOM
)
1156 y
= rect
.GetBottom() - height
;
1158 else if ( alignment
& wxALIGN_CENTRE_VERTICAL
)
1160 y
= (rect
.GetTop() + rect
.GetBottom() + 1 - height
) / 2;
1162 else // alignment & wxALIGN_TOP
1167 // draw the bitmap first
1173 DrawBitmap(bitmap
, x
, y
, true /* use mask */);
1175 wxCoord offset
= bitmap
.GetWidth() + 4;
1179 y
+= (height
- heightText
) / 2;
1182 // we will draw the underscore under the accel char later
1183 wxCoord startUnderscore
= 0,
1187 // split the string into lines and draw each of them separately
1189 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
1191 if ( pc
== text
.end() || *pc
== '\n' )
1193 int xRealStart
= x
; // init it here to avoid compielr warnings
1195 if ( !curLine
.empty() )
1197 // NB: can't test for !(alignment & wxALIGN_LEFT) because
1198 // wxALIGN_LEFT is 0
1199 if ( alignment
& (wxALIGN_RIGHT
| wxALIGN_CENTRE_HORIZONTAL
) )
1202 GetTextExtent(curLine
, &widthLine
, NULL
);
1204 if ( alignment
& wxALIGN_RIGHT
)
1206 xRealStart
+= width
- widthLine
;
1208 else // if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
1210 xRealStart
+= (width
- widthLine
) / 2;
1213 //else: left aligned, nothing to do
1215 DrawText(curLine
, xRealStart
, y
);
1220 // do we have underscore in this line? we can check yUnderscore
1221 // because it is set below to just y + heightLine if we do
1222 if ( y
== yUnderscore
)
1224 // adjust the horz positions to account for the shift
1225 startUnderscore
+= xRealStart
;
1226 endUnderscore
+= xRealStart
;
1229 if ( pc
== text
.end() )
1234 else // not end of line
1236 if ( pc
- text
.begin() == indexAccel
)
1238 // remeber to draw underscore here
1239 GetTextExtent(curLine
, &startUnderscore
, NULL
);
1241 GetTextExtent(curLine
, &endUnderscore
, NULL
);
1243 yUnderscore
= y
+ heightLine
;
1252 // draw the underscore if found
1253 if ( startUnderscore
!= endUnderscore
)
1255 // it should be of the same colour as text
1256 SetPen(wxPen(GetTextForeground(), 0, wxPENSTYLE_SOLID
));
1260 DrawLine(startUnderscore
, yUnderscore
, endUnderscore
, yUnderscore
);
1263 // return bounding rect if requested
1266 *rectBounding
= wxRect(x
, y
- heightText
, widthText
, heightText
);
1269 CalcBoundingBox(x0
, y0
);
1270 CalcBoundingBox(x0
+ width0
, y0
+ height
);
1273 #if WXWIN_COMPATIBILITY_2_8
1274 // for compatibility with the old code when wxCoord was long everywhere
1275 void wxDC::GetTextExtent(const wxString
& string
,
1278 long *externalLeading
,
1279 const wxFont
*theFont
) const
1281 wxCoord x2
, y2
, descent2
, externalLeading2
;
1282 m_pimpl
->DoGetTextExtent(string
, &x2
, &y2
,
1283 &descent2
, &externalLeading2
,
1290 *descent
= descent2
;
1291 if ( externalLeading
)
1292 *externalLeading
= externalLeading2
;
1295 void wxDC::GetLogicalOrigin(long *x
, long *y
) const
1298 m_pimpl
->DoGetLogicalOrigin(&x2
, &y2
);
1305 void wxDC::GetDeviceOrigin(long *x
, long *y
) const
1308 m_pimpl
->DoGetDeviceOrigin(&x2
, &y2
);
1315 void wxDC::GetClippingBox(long *x
, long *y
, long *w
, long *h
) const
1317 wxCoord xx
,yy
,ww
,hh
;
1318 m_pimpl
->DoGetClippingBox(&xx
, &yy
, &ww
, &hh
);
1325 #endif // WXWIN_COMPATIBILITY_2_8
1328 Notes for wxWidgets DrawEllipticArcRot(...)
1330 wxDCBase::DrawEllipticArcRot(...) draws a rotated elliptic arc or an ellipse.
1331 It uses wxDCBase::CalculateEllipticPoints(...) and wxDCBase::Rotate(...),
1334 All methods are generic, so they can be implemented in wxDCBase.
1335 DoDrawEllipticArcRot(...) is virtual, so it can be called from deeper
1336 methods like (WinCE) wxDC::DoDrawArc(...).
1338 CalculateEllipticPoints(...) fills a given list of wxPoints with some points
1339 of an elliptic arc. The algorithm is pixel-based: In every row (in flat
1340 parts) or every column (in steep parts) only one pixel is calculated.
1341 Trigonometric calculation (sin, cos, tan, atan) is only done if the
1342 starting angle is not equal to the ending angle. The calculation of the
1343 pixels is done using simple arithmetic only and should perform not too
1344 bad even on devices without floating point processor. I didn't test this yet.
1346 Rotate(...) rotates a list of point pixel-based, you will see rounding errors.
1347 For instance: an ellipse rotated 180 degrees is drawn
1348 slightly different from the original.
1350 The points are then moved to an array and used to draw a polyline and/or polygon
1351 (with center added, the pie).
1352 The result looks quite similar to the native ellipse, only e few pixels differ.
1354 The performance on a desktop system (Athlon 1800, WinXP) is about 7 times
1355 slower as DrawEllipse(...), which calls the native API.
1356 An rotated ellipse outside the clipping region takes nearly the same time,
1357 while an native ellipse outside takes nearly no time to draw.
1359 If you draw an arc with this new method, you will see the starting and ending angles
1360 are calculated properly.
1361 If you use DrawEllipticArc(...), you will see they are only correct for circles
1362 and not properly calculated for ellipses.
1365 p.lenhard@t-online.de
1369 void wxDCImpl::DoDrawEllipticArcRot( wxCoord x
, wxCoord y
,
1370 wxCoord w
, wxCoord h
,
1371 double sa
, double ea
, double angle
)
1375 CalculateEllipticPoints( &list
, x
, y
, w
, h
, sa
, ea
);
1376 Rotate( &list
, angle
, wxPoint( x
+w
/2, y
+h
/2 ) );
1378 // Add center (for polygon/pie)
1379 list
.Append( new wxPoint( x
+w
/2, y
+h
/2 ) );
1381 // copy list into array and delete list elements
1382 int n
= list
.GetCount();
1383 wxPoint
*points
= new wxPoint
[n
];
1385 wxPointList::compatibility_iterator node
;
1386 for ( node
= list
.GetFirst(); node
; node
= node
->GetNext(), i
++ )
1388 wxPoint
*point
= node
->GetData();
1389 points
[i
].x
= point
->x
;
1390 points
[i
].y
= point
->y
;
1394 // first draw the pie without pen, if necessary
1395 if( GetBrush() != *wxTRANSPARENT_BRUSH
)
1397 wxPen
tempPen( GetPen() );
1398 SetPen( *wxTRANSPARENT_PEN
);
1399 DoDrawPolygon( n
, points
, 0, 0 );
1403 // then draw the arc without brush, if necessary
1404 if( GetPen() != *wxTRANSPARENT_PEN
)
1407 DoDrawLines( n
-1, points
, 0, 0 );
1412 } // DrawEllipticArcRot
1414 void wxDCImpl::Rotate( wxPointList
* points
, double angle
, wxPoint center
)
1419 double dSinA
= -sin(angle
*2.0*pi
/360.0);
1420 double dCosA
= cos(angle
*2.0*pi
/360.0);
1421 wxPointList::compatibility_iterator node
;
1422 for ( node
= points
->GetFirst(); node
; node
= node
->GetNext() )
1424 wxPoint
* point
= node
->GetData();
1426 // transform coordinates, if necessary
1427 if( center
.x
) point
->x
-= center
.x
;
1428 if( center
.y
) point
->y
-= center
.y
;
1430 // calculate rotation, rounding simply by implicit cast to integer
1431 int xTemp
= point
->x
* dCosA
- point
->y
* dSinA
;
1432 point
->y
= point
->x
* dSinA
+ point
->y
* dCosA
;
1435 // back transform coordinates, if necessary
1436 if( center
.x
) point
->x
+= center
.x
;
1437 if( center
.y
) point
->y
+= center
.y
;
1442 void wxDCImpl::CalculateEllipticPoints( wxPointList
* points
,
1443 wxCoord xStart
, wxCoord yStart
,
1444 wxCoord w
, wxCoord h
,
1445 double sa
, double ea
)
1456 bool bUseAngles
= false;
1462 // decrement 1 pixel if ellipse is smaller than 2*a, 2*b
1464 if( 2*a
== w
) decrX
= 1;
1466 if( 2*b
== h
) decrY
= 1;
1468 wxCoord xCenter
= xStart
+ a
;
1469 wxCoord yCenter
= yStart
+ b
;
1470 // calculate data for start and end, if necessary
1474 // normalisation of angles
1475 while( sa
<0 ) sa
+= 360;
1476 while( ea
<0 ) ea
+= 360;
1477 while( sa
>=360 ) sa
-= 360;
1478 while( ea
>=360 ) ea
-= 360;
1479 // calculate quadrant numbers
1480 if( sa
> 270 ) sq
= 3;
1481 else if( sa
> 180 ) sq
= 2;
1482 else if( sa
> 90 ) sq
= 1;
1483 if( ea
> 270 ) eq
= 3;
1484 else if( ea
> 180 ) eq
= 2;
1485 else if( ea
> 90 ) eq
= 1;
1486 sar
= sa
* pi
/ 180.0;
1487 ear
= ea
* pi
/ 180.0;
1488 // correct angle circle -> ellipse
1489 sar
= atan( -a
/(double)b
* tan( sar
) );
1490 if ( sq
== 1 || sq
== 2 ) sar
+= pi
;
1491 ear
= atan( -a
/(double)b
* tan( ear
) );
1492 if ( eq
== 1 || eq
== 2 ) ear
+= pi
;
1493 // coordinates of points
1494 xsa
= xCenter
+ a
* cos( sar
);
1495 if( sq
== 0 || sq
== 3 ) xsa
-= decrX
;
1496 ysa
= yCenter
+ b
* sin( sar
);
1497 if( sq
== 2 || sq
== 3 ) ysa
-= decrY
;
1498 xea
= xCenter
+ a
* cos( ear
);
1499 if( eq
== 0 || eq
== 3 ) xea
-= decrX
;
1500 yea
= yCenter
+ b
* sin( ear
);
1501 if( eq
== 2 || eq
== 3 ) yea
-= decrY
;
1503 // calculate c1 = b^2, c2 = b^2/a^2 with a = w/2, b = h/2
1505 double c2
= 2.0 / w
;
1514 // Lists for quadrant 1 to 4
1515 wxPointList pointsarray
[4];
1516 // Calculate points for first quadrant and set in all quadrants
1517 for( x
= 0; x
<= a
; ++x
)
1522 bool bNewPoint
= false;
1523 while( y2
> c1
- c2
* x2
&& y
> 0 )
1529 // old y now to big: set point with old y, old x
1530 if( bNewPoint
&& x
>1)
1533 // remove points on the same line
1534 pointsarray
[0].Insert( new wxPoint( xCenter
+ x1
- decrX
, yCenter
- y_old
) );
1535 pointsarray
[1].Append( new wxPoint( xCenter
- x1
, yCenter
- y_old
) );
1536 pointsarray
[2].Insert( new wxPoint( xCenter
- x1
, yCenter
+ y_old
- decrY
) );
1537 pointsarray
[3].Append( new wxPoint( xCenter
+ x1
- decrX
, yCenter
+ y_old
- decrY
) );
1539 } // calculate point
1541 // Starting and/or ending points for the quadrants, first quadrant gets both.
1542 pointsarray
[0].Insert( new wxPoint( xCenter
+ a
- decrX
, yCenter
) );
1543 pointsarray
[0].Append( new wxPoint( xCenter
, yCenter
- b
) );
1544 pointsarray
[1].Append( new wxPoint( xCenter
- a
, yCenter
) );
1545 pointsarray
[2].Append( new wxPoint( xCenter
, yCenter
+ b
- decrY
) );
1546 pointsarray
[3].Append( new wxPoint( xCenter
+ a
- decrX
, yCenter
) );
1548 // copy quadrants in original list
1551 // Copy the right part of the points in the lists
1552 // and delete the wxPoints, because they do not leave this method.
1553 points
->Append( new wxPoint( xsa
, ysa
) );
1555 bool bStarted
= false;
1556 bool bReady
= false;
1557 bool bForceTurn
= ( sq
== eq
&& sa
> ea
);
1560 wxPointList::compatibility_iterator node
;
1561 for( node
= pointsarray
[q
].GetFirst(); node
; node
= node
->GetNext() )
1563 // once: go to starting point in start quadrant
1566 node
->GetData()->x
< xsa
+1 && q
<= 1
1568 node
->GetData()->x
> xsa
-1 && q
>= 2
1575 // copy point, if not at ending point
1578 if( q
!= eq
|| bForceTurn
1580 ( (wxPoint
*) node
->GetData() )->x
> xea
+1 && q
<= 1
1582 ( (wxPoint
*) node
->GetData() )->x
< xea
-1 && q
>= 2
1586 wxPoint
* pPoint
= new wxPoint( *(node
->GetData()) );
1587 points
->Append( pPoint
);
1589 else if( q
== eq
&& !bForceTurn
|| node
->GetData()->x
== xea
)
1599 } // while not bReady
1600 points
->Append( new wxPoint( xea
, yea
) );
1603 for( q
= 0; q
< 4; ++q
)
1605 wxPointList::compatibility_iterator node
;
1606 for( node
= pointsarray
[q
].GetFirst(); node
; node
= node
->GetNext() )
1608 wxPoint
*p
= node
->GetData();
1615 wxPointList::compatibility_iterator node
;
1616 // copy whole ellipse, wxPoints will be deleted outside
1617 for( node
= pointsarray
[0].GetFirst(); node
; node
= node
->GetNext() )
1619 wxPoint
*p
= node
->GetData();
1620 points
->Append( p
);
1622 for( node
= pointsarray
[1].GetFirst(); node
; node
= node
->GetNext() )
1624 wxPoint
*p
= node
->GetData();
1625 points
->Append( p
);
1627 for( node
= pointsarray
[2].GetFirst(); node
; node
= node
->GetNext() )
1629 wxPoint
*p
= node
->GetData();
1630 points
->Append( p
);
1632 for( node
= pointsarray
[3].GetFirst(); node
; node
= node
->GetNext() )
1634 wxPoint
*p
= node
->GetData();
1635 points
->Append( p
);
1638 } // CalculateEllipticPoints
1640 #endif // __WXWINCE__