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"
39 #include "wx/msw/dcclient.h"
40 #include "wx/msw/dcmemory.h"
41 #include "wx/msw/dcscreen.h"
45 #include "wx/gtk/dcclient.h"
46 #include "wx/gtk/dcmemory.h"
47 #include "wx/gtk/dcscreen.h"
51 #include "wx/mac/dcclient.h"
52 #include "wx/mac/dcmemory.h"
53 #include "wx/mac/dcscreen.h"
57 #include "wx/x11/dcclient.h"
58 #include "wx/x11/dcmemory.h"
59 #include "wx/x11/dcscreen.h"
62 //----------------------------------------------------------------------------
64 //----------------------------------------------------------------------------
66 wxDCFactory
*wxDCFactory::m_factory
= NULL
;
68 void wxDCFactory::SetDCFactory( wxDCFactory
*factory
)
70 if (wxDCFactory::m_factory
)
71 delete wxDCFactory::m_factory
;
73 wxDCFactory::m_factory
= factory
;
76 wxDCFactory
*wxDCFactory::GetFactory()
78 if (!wxDCFactory::m_factory
)
79 wxDCFactory::m_factory
= new wxNativeDCFactory
;
81 return wxDCFactory::m_factory
;
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
88 wxDCImpl
* wxNativeDCFactory::CreateWindowDC( wxWindowDC
*owner
)
90 return new wxWindowDCImpl( owner
);
93 wxDCImpl
* wxNativeDCFactory::CreateWindowDC( wxWindowDC
*owner
, wxWindow
*window
)
95 return new wxWindowDCImpl( owner
, window
);
98 wxDCImpl
* wxNativeDCFactory::CreateClientDC( wxClientDC
*owner
)
100 return new wxClientDCImpl( owner
);
103 wxDCImpl
* wxNativeDCFactory::CreateClientDC( wxClientDC
*owner
, wxWindow
*window
)
105 return new wxClientDCImpl( owner
, window
);
108 wxDCImpl
* wxNativeDCFactory::CreatePaintDC( wxPaintDC
*owner
)
110 return new wxPaintDCImpl( owner
);
113 wxDCImpl
* wxNativeDCFactory::CreatePaintDC( wxPaintDC
*owner
, wxWindow
*window
)
115 return new wxPaintDCImpl( owner
, window
);
118 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
)
120 return new wxMemoryDCImpl( owner
);
123 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
, wxBitmap
&bitmap
)
125 return new wxMemoryDCImpl( owner
, bitmap
);
128 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
, wxDC
*dc
)
130 return new wxMemoryDCImpl( owner
, dc
);
133 wxDCImpl
* wxNativeDCFactory::CreateScreenDC( wxScreenDC
*owner
)
135 return new wxScreenDCImpl( owner
);
138 #if wxUSE_PRINTING_ARCHITECTURE
139 wxDCImpl
*wxNativeDCFactory::CreatePrinterDC( wxPrinterDC
*owner
, const wxPrintData
&data
)
141 wxPrintFactory
*factory
= wxPrintFactory::GetFactory();
142 return factory
->CreatePrinterDCImpl( owner
, data
);
146 //-----------------------------------------------------------------------------
148 //-----------------------------------------------------------------------------
150 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC
, wxDC
)
152 wxWindowDC::wxWindowDC()
156 wxWindowDC::wxWindowDC( wxWindow
*win
)
158 wxDCFactory
*factory
= wxDCFactory::GetFactory();
159 m_pimpl
= factory
->CreateWindowDC( this, win
);
162 //-----------------------------------------------------------------------------
164 //-----------------------------------------------------------------------------
166 IMPLEMENT_DYNAMIC_CLASS(wxClientDC
, wxWindowDC
)
168 wxClientDC::wxClientDC()
172 wxClientDC::wxClientDC( wxWindow
*win
)
174 wxDCFactory
*factory
= wxDCFactory::GetFactory();
175 m_pimpl
= factory
->CreateClientDC( this, win
);
178 //-----------------------------------------------------------------------------
180 //-----------------------------------------------------------------------------
182 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC
, wxDC
)
184 wxMemoryDC::wxMemoryDC()
186 wxDCFactory
*factory
= wxDCFactory::GetFactory();
187 m_pimpl
= factory
->CreateMemoryDC( this );
190 wxMemoryDC::wxMemoryDC( wxBitmap
& bitmap
)
192 wxDCFactory
*factory
= wxDCFactory::GetFactory();
193 m_pimpl
= factory
->CreateMemoryDC( this, bitmap
);
196 wxMemoryDC::wxMemoryDC( wxDC
*dc
)
198 wxDCFactory
*factory
= wxDCFactory::GetFactory();
199 m_pimpl
= factory
->CreateMemoryDC( this, dc
);
202 void wxMemoryDC::SelectObject(wxBitmap
& bmp
)
204 // make sure that the given wxBitmap is not sharing its data with other
205 // wxBitmap instances as its contents will be modified by any drawing
206 // operation done on this DC
210 GetImpl()->DoSelect(bmp
);
213 void wxMemoryDC::SelectObjectAsSource(const wxBitmap
& bmp
)
215 GetImpl()->DoSelect(bmp
);
218 const wxBitmap
& wxMemoryDC::GetSelectedBitmap() const
220 return GetImpl()->GetSelectedBitmap();
223 wxBitmap
& wxMemoryDC::GetSelectedBitmap()
225 return GetImpl()->GetSelectedBitmap();
229 //-----------------------------------------------------------------------------
231 //-----------------------------------------------------------------------------
233 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
, wxClientDC
)
235 wxPaintDC::wxPaintDC()
239 wxPaintDC::wxPaintDC( wxWindow
*win
)
241 wxDCFactory
*factory
= wxDCFactory::GetFactory();
242 m_pimpl
= factory
->CreatePaintDC( this, win
);
245 //-----------------------------------------------------------------------------
247 //-----------------------------------------------------------------------------
249 IMPLEMENT_DYNAMIC_CLASS(wxScreenDC
, wxWindowDC
)
251 wxScreenDC::wxScreenDC()
253 wxDCFactory
*factory
= wxDCFactory::GetFactory();
254 m_pimpl
= factory
->CreateScreenDC( this );
257 //-----------------------------------------------------------------------------
259 //-----------------------------------------------------------------------------
261 #if wxUSE_PRINTING_ARCHITECTURE
263 IMPLEMENT_DYNAMIC_CLASS(wxPrinterDC
, wxDC
)
265 wxPrinterDC::wxPrinterDC()
267 wxPrintData data
; // Does this make sense?
268 wxDCFactory
*factory
= wxDCFactory::GetFactory();
269 m_pimpl
= factory
->CreatePrinterDC( this, data
);
272 wxPrinterDC::wxPrinterDC( const wxPrintData
&data
)
274 wxDCFactory
*factory
= wxDCFactory::GetFactory();
275 m_pimpl
= factory
->CreatePrinterDC( this, data
);
278 wxPrinterDC::~wxPrinterDC()
282 wxRect
wxPrinterDC::GetPaperRect()
284 return GetImpl()->GetPaperRect();
287 int wxPrinterDC::GetResolution()
289 return GetImpl()->GetResolution();
294 //-----------------------------------------------------------------------------
296 //-----------------------------------------------------------------------------
298 IMPLEMENT_ABSTRACT_CLASS(wxDCImpl
, wxObject
)
300 wxDCImpl::wxDCImpl( wxDC
*owner
)
302 , m_colour(wxColourDisplay())
306 , m_isBBoxValid(false)
307 , m_logicalOriginX(0), m_logicalOriginY(0)
308 , m_deviceOriginX(0), m_deviceOriginY(0)
309 , m_deviceLocalOriginX(0), m_deviceLocalOriginY(0)
310 , m_logicalScaleX(1.0), m_logicalScaleY(1.0)
311 , m_userScaleX(1.0), m_userScaleY(1.0)
312 , m_scaleX(1.0), m_scaleY(1.0)
313 , m_signX(1), m_signY(1)
314 , m_minX(0), m_minY(0), m_maxX(0), m_maxY(0)
315 , m_clipX1(0), m_clipY1(0), m_clipX2(0), m_clipY2(0)
316 , m_logicalFunction(wxCOPY
)
317 , m_backgroundMode(wxTRANSPARENT
)
318 , m_mappingMode(wxMM_TEXT
)
321 , m_backgroundBrush(*wxTRANSPARENT_BRUSH
)
322 , m_textForegroundColour(*wxBLACK
)
323 , m_textBackgroundColour(*wxWHITE
)
327 , m_hasCustomPalette(false)
328 #endif // wxUSE_PALETTE
332 m_mm_to_pix_x
= (double)wxGetDisplaySize().GetWidth() /
333 (double)wxGetDisplaySizeMM().GetWidth();
334 m_mm_to_pix_y
= (double)wxGetDisplaySize().GetHeight() /
335 (double)wxGetDisplaySizeMM().GetHeight();
341 wxDCImpl::~wxDCImpl()
345 // ----------------------------------------------------------------------------
346 // coordinate conversions and transforms
347 // ----------------------------------------------------------------------------
349 wxCoord
wxDCImpl::DeviceToLogicalX(wxCoord x
) const
351 return wxRound((double)(x
- m_deviceOriginX
- m_deviceLocalOriginX
) / m_scaleX
) * m_signX
+ m_logicalOriginX
;
354 wxCoord
wxDCImpl::DeviceToLogicalY(wxCoord y
) const
356 return wxRound((double)(y
- m_deviceOriginY
- m_deviceLocalOriginY
) / m_scaleY
) * m_signY
+ m_logicalOriginY
;
359 wxCoord
wxDCImpl::DeviceToLogicalXRel(wxCoord x
) const
361 return wxRound((double)(x
) / m_scaleX
);
364 wxCoord
wxDCImpl::DeviceToLogicalYRel(wxCoord y
) const
366 return wxRound((double)(y
) / m_scaleY
);
369 wxCoord
wxDCImpl::LogicalToDeviceX(wxCoord x
) const
371 return wxRound((double)(x
- m_logicalOriginX
) * m_scaleX
) * m_signX
+ m_deviceOriginX
* m_signY
+ m_deviceLocalOriginX
;
374 wxCoord
wxDCImpl::LogicalToDeviceY(wxCoord y
) const
376 return wxRound((double)(y
- m_logicalOriginY
) * m_scaleY
) * m_signY
+ m_deviceOriginY
* m_signY
+ m_deviceLocalOriginY
;
379 wxCoord
wxDCImpl::LogicalToDeviceXRel(wxCoord x
) const
381 return wxRound((double)(x
) * m_scaleX
);
384 wxCoord
wxDCImpl::LogicalToDeviceYRel(wxCoord y
) const
386 return wxRound((double)(y
) * m_scaleY
);
389 void wxDCImpl::ComputeScaleAndOrigin()
391 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
392 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
395 void wxDCImpl::SetMapMode( int mode
)
400 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
403 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
406 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
409 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
413 SetLogicalScale( 1.0, 1.0 );
416 m_mappingMode
= mode
;
419 void wxDCImpl::SetUserScale( double x
, double y
)
421 // allow negative ? -> no
424 ComputeScaleAndOrigin();
427 void wxDCImpl::SetLogicalScale( double x
, double y
)
432 ComputeScaleAndOrigin();
435 void wxDCImpl::SetLogicalOrigin( wxCoord x
, wxCoord y
)
437 m_logicalOriginX
= x
* m_signX
;
438 m_logicalOriginY
= y
* m_signY
;
439 ComputeScaleAndOrigin();
442 void wxDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
446 ComputeScaleAndOrigin();
449 void wxDCImpl::SetDeviceLocalOrigin( wxCoord x
, wxCoord y
)
451 m_deviceLocalOriginX
= x
;
452 m_deviceLocalOriginY
= y
;
453 ComputeScaleAndOrigin();
456 void wxDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
458 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
459 // wxWidgets 2.9: no longer override it
460 m_signX
= (xLeftRight
? 1 : -1);
461 m_signY
= (yBottomUp
? -1 : 1);
462 ComputeScaleAndOrigin();
466 // Each element of the widths array will be the width of the string up to and
467 // including the corresponding character in text. This is the generic
468 // implementation, the port-specific classes should do this with native APIs
469 // if available and if faster. Note: pango_layout_index_to_pos is much slower
470 // than calling GetTextExtent!!
477 FontWidthCache() : m_scaleX(1), m_widths(NULL
) { }
478 ~FontWidthCache() { delete []m_widths
; }
483 m_widths
= new int[FWC_SIZE
];
485 memset(m_widths
, 0, sizeof(int)*FWC_SIZE
);
493 static FontWidthCache s_fontWidthCache
;
495 bool wxDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
499 const size_t len
= text
.length();
503 // reset the cache if font or horizontal scale have changed
504 if ( !s_fontWidthCache
.m_widths
||
505 !wxIsSameDouble(s_fontWidthCache
.m_scaleX
, m_scaleX
) ||
506 (s_fontWidthCache
.m_font
!= GetFont()) )
508 s_fontWidthCache
.Reset();
509 s_fontWidthCache
.m_font
= GetFont();
510 s_fontWidthCache
.m_scaleX
= m_scaleX
;
513 // Calculate the position of each character based on the widths of
514 // the previous characters
516 for ( size_t i
= 0; i
< len
; i
++ )
518 const wxChar c
= text
[i
];
519 unsigned int c_int
= (unsigned int)c
;
521 if ((c_int
< FWC_SIZE
) && (s_fontWidthCache
.m_widths
[c_int
] != 0))
523 w
= s_fontWidthCache
.m_widths
[c_int
];
527 DoGetTextExtent(c
, &w
, &h
);
528 if (c_int
< FWC_SIZE
)
529 s_fontWidthCache
.m_widths
[c_int
] = w
;
533 widths
[i
] = totalWidth
;
539 void wxDCImpl::GetMultiLineTextExtent(const wxString
& text
,
543 const wxFont
*font
) const
545 wxCoord widthTextMax
= 0, widthLine
,
546 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
549 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
551 if ( pc
== text
.end() || *pc
== _T('\n') )
553 if ( curLine
.empty() )
555 // we can't use GetTextExtent - it will return 0 for both width
556 // and height and an empty line should count in height
559 // assume that this line has the same height as the previous
561 if ( !heightLineDefault
)
562 heightLineDefault
= heightLine
;
564 if ( !heightLineDefault
)
566 // but we don't know it yet - choose something reasonable
567 DoGetTextExtent(_T("W"), NULL
, &heightLineDefault
,
571 heightTextTotal
+= heightLineDefault
;
575 DoGetTextExtent(curLine
, &widthLine
, &heightLine
,
577 if ( widthLine
> widthTextMax
)
578 widthTextMax
= widthLine
;
579 heightTextTotal
+= heightLine
;
582 if ( pc
== text
.end() )
600 *y
= heightTextTotal
;
605 void wxDCImpl::DoDrawCheckMark(wxCoord x1
, wxCoord y1
,
606 wxCoord width
, wxCoord height
)
608 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
610 wxCoord x2
= x1
+ width
,
613 // the pen width is calibrated to give 3 for width == height == 10
614 wxDCPenChanger
pen( *m_owner
, wxPen(GetTextForeground(), (width
+ height
+ 1)/7));
616 // we're drawing a scaled version of wx/generic/tick.xpm here
617 wxCoord x3
= x1
+ (4*width
) / 10, // x of the tick bottom
618 y3
= y1
+ height
/ 2; // y of the left tick branch
619 DoDrawLine(x1
, y3
, x3
, y2
);
620 DoDrawLine(x3
, y2
, x2
, y1
);
622 CalcBoundingBox(x1
, y1
);
623 CalcBoundingBox(x2
, y2
);
627 wxDCImpl::DoStretchBlit(wxCoord xdest
, wxCoord ydest
,
628 wxCoord dstWidth
, wxCoord dstHeight
,
630 wxCoord xsrc
, wxCoord ysrc
,
631 wxCoord srcWidth
, wxCoord srcHeight
,
637 wxCHECK_MSG( srcWidth
&& srcHeight
&& dstWidth
&& dstHeight
, false,
638 _T("invalid blit size") );
640 // emulate the stretching by modifying the DC scale
641 double xscale
= (double)srcWidth
/dstWidth
,
642 yscale
= (double)srcHeight
/dstHeight
;
644 double xscaleOld
, yscaleOld
;
645 GetUserScale(&xscaleOld
, &yscaleOld
);
646 SetUserScale(xscaleOld
/xscale
, yscaleOld
/yscale
);
648 bool rc
= DoBlit(wxCoord(xdest
*xscale
), wxCoord(ydest
*yscale
),
649 wxCoord(dstWidth
*xscale
), wxCoord(dstHeight
*yscale
),
651 xsrc
, ysrc
, rop
, useMask
, xsrcMask
, ysrcMask
);
653 SetUserScale(xscaleOld
, yscaleOld
);
658 void wxDCImpl::DrawLines(const wxPointList
*list
, wxCoord xoffset
, wxCoord yoffset
)
660 int n
= list
->GetCount();
661 wxPoint
*points
= new wxPoint
[n
];
664 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
666 wxPoint
*point
= node
->GetData();
667 points
[i
].x
= point
->x
;
668 points
[i
].y
= point
->y
;
671 DoDrawLines(n
, points
, xoffset
, yoffset
);
676 void wxDCImpl::DrawPolygon(const wxPointList
*list
,
677 wxCoord xoffset
, wxCoord yoffset
,
680 int n
= list
->GetCount();
681 wxPoint
*points
= new wxPoint
[n
];
684 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
686 wxPoint
*point
= node
->GetData();
687 points
[i
].x
= point
->x
;
688 points
[i
].y
= point
->y
;
691 DoDrawPolygon(n
, points
, xoffset
, yoffset
, fillStyle
);
697 wxDCImpl::DoDrawPolyPolygon(int n
,
700 wxCoord xoffset
, wxCoord yoffset
,
705 DoDrawPolygon(count
[0], points
, xoffset
, yoffset
, fillStyle
);
713 for (i
= j
= lastOfs
= 0; i
< n
; i
++)
718 pts
= new wxPoint
[j
+n
-1];
719 for (i
= 0; i
< j
; i
++)
721 for (i
= 2; i
<= n
; i
++)
723 lastOfs
-= count
[n
-i
];
724 pts
[j
++] = pts
[lastOfs
];
728 SetPen(wxPen(*wxBLACK
, 0, wxTRANSPARENT
));
729 DoDrawPolygon(j
, pts
, xoffset
, yoffset
, fillStyle
);
731 for (i
= j
= 0; i
< n
; i
++)
733 DoDrawLines(count
[i
], pts
+j
, xoffset
, yoffset
);
741 void wxDCImpl::DoDrawSpline(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
, wxCoord x3
, wxCoord y3
)
743 wxPointList point_list
;
745 wxPoint
*point1
= new wxPoint
;
746 point1
->x
= x1
; point1
->y
= y1
;
747 point_list
.Append( point1
);
749 wxPoint
*point2
= new wxPoint
;
750 point2
->x
= x2
; point2
->y
= y2
;
751 point_list
.Append( point2
);
753 wxPoint
*point3
= new wxPoint
;
754 point3
->x
= x3
; point3
->y
= y3
;
755 point_list
.Append( point3
);
757 DoDrawSpline(&point_list
);
759 for( wxPointList::compatibility_iterator node
= point_list
.GetFirst(); node
; node
= node
->GetNext() )
761 wxPoint
*p
= node
->GetData();
766 void wxDCImpl::DoDrawSpline(int n
, wxPoint points
[])
769 for (int i
=0; i
< n
; i
++)
770 list
.Append( &points
[i
] );
775 // ----------------------------------- spline code ----------------------------------------
777 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
,
778 double a3
, double b3
, double a4
, double b4
);
779 void wx_clear_stack();
780 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
, double *x3
,
781 double *y3
, double *x4
, double *y4
);
782 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
,
783 double x4
, double y4
);
784 static bool wx_spline_add_point(double x
, double y
);
785 static void wx_spline_draw_point_array(wxDC
*dc
);
787 wxPointList wx_spline_point_list
;
789 #define half(z1, z2) ((z1+z2)/2.0)
792 /* iterative version */
794 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
, double a3
, double b3
, double a4
,
797 register double xmid
, ymid
;
798 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
801 wx_spline_push(a1
, b1
, a2
, b2
, a3
, b3
, a4
, b4
);
803 while (wx_spline_pop(&x1
, &y1
, &x2
, &y2
, &x3
, &y3
, &x4
, &y4
)) {
804 xmid
= (double)half(x2
, x3
);
805 ymid
= (double)half(y2
, y3
);
806 if (fabs(x1
- xmid
) < THRESHOLD
&& fabs(y1
- ymid
) < THRESHOLD
&&
807 fabs(xmid
- x4
) < THRESHOLD
&& fabs(ymid
- y4
) < THRESHOLD
) {
808 wx_spline_add_point( x1
, y1
);
809 wx_spline_add_point( xmid
, ymid
);
811 wx_spline_push(xmid
, ymid
, (double)half(xmid
, x3
), (double)half(ymid
, y3
),
812 (double)half(x3
, x4
), (double)half(y3
, y4
), x4
, y4
);
813 wx_spline_push(x1
, y1
, (double)half(x1
, x2
), (double)half(y1
, y2
),
814 (double)half(x2
, xmid
), (double)half(y2
, ymid
), xmid
, ymid
);
819 /* utilities used by spline drawing routines */
821 typedef struct wx_spline_stack_struct
{
822 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
825 #define SPLINE_STACK_DEPTH 20
826 static Stack wx_spline_stack
[SPLINE_STACK_DEPTH
];
827 static Stack
*wx_stack_top
;
828 static int wx_stack_count
;
830 void wx_clear_stack()
832 wx_stack_top
= wx_spline_stack
;
836 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
, double x4
, double y4
)
838 wx_stack_top
->x1
= x1
;
839 wx_stack_top
->y1
= y1
;
840 wx_stack_top
->x2
= x2
;
841 wx_stack_top
->y2
= y2
;
842 wx_stack_top
->x3
= x3
;
843 wx_stack_top
->y3
= y3
;
844 wx_stack_top
->x4
= x4
;
845 wx_stack_top
->y4
= y4
;
850 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
,
851 double *x3
, double *y3
, double *x4
, double *y4
)
853 if (wx_stack_count
== 0)
857 *x1
= wx_stack_top
->x1
;
858 *y1
= wx_stack_top
->y1
;
859 *x2
= wx_stack_top
->x2
;
860 *y2
= wx_stack_top
->y2
;
861 *x3
= wx_stack_top
->x3
;
862 *y3
= wx_stack_top
->y3
;
863 *x4
= wx_stack_top
->x4
;
864 *y4
= wx_stack_top
->y4
;
868 static bool wx_spline_add_point(double x
, double y
)
870 wxPoint
*point
= new wxPoint( wxRound(x
), wxRound(y
) );
871 wx_spline_point_list
.Append(point
);
875 static void wx_spline_draw_point_array(wxDC
*dc
)
877 dc
->DrawLines(&wx_spline_point_list
, 0, 0 );
878 wxPointList::compatibility_iterator node
= wx_spline_point_list
.GetFirst();
881 wxPoint
*point
= node
->GetData();
883 wx_spline_point_list
.Erase(node
);
884 node
= wx_spline_point_list
.GetFirst();
888 void wxDCImpl::DoDrawSpline( const wxPointList
*points
)
890 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
893 double cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
;
894 double x1
, y1
, x2
, y2
;
896 wxPointList::compatibility_iterator node
= points
->GetFirst();
901 p
= (wxPoint
*)node
->GetData();
906 node
= node
->GetNext();
911 cx1
= (double)((x1
+ x2
) / 2);
912 cy1
= (double)((y1
+ y2
) / 2);
913 cx2
= (double)((cx1
+ x2
) / 2);
914 cy2
= (double)((cy1
+ y2
) / 2);
916 wx_spline_add_point(x1
, y1
);
918 while ((node
= node
->GetNext())
929 cx4
= (double)(x1
+ x2
) / 2;
930 cy4
= (double)(y1
+ y2
) / 2;
931 cx3
= (double)(x1
+ cx4
) / 2;
932 cy3
= (double)(y1
+ cy4
) / 2;
934 wx_quadratic_spline(cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
);
938 cx2
= (double)(cx1
+ x2
) / 2;
939 cy2
= (double)(cy1
+ y2
) / 2;
942 wx_spline_add_point( cx1
, cy1
);
943 wx_spline_add_point( x2
, y2
);
945 wx_spline_draw_point_array( m_owner
);
948 #endif // wxUSE_SPLINES
952 void wxDCImpl::DoGradientFillLinear(const wxRect
& rect
,
953 const wxColour
& initialColour
,
954 const wxColour
& destColour
,
955 wxDirection nDirection
)
958 wxPen oldPen
= m_pen
;
959 wxBrush oldBrush
= m_brush
;
961 wxUint8 nR1
= initialColour
.Red();
962 wxUint8 nG1
= initialColour
.Green();
963 wxUint8 nB1
= initialColour
.Blue();
964 wxUint8 nR2
= destColour
.Red();
965 wxUint8 nG2
= destColour
.Green();
966 wxUint8 nB2
= destColour
.Blue();
969 if ( nDirection
== wxEAST
|| nDirection
== wxWEST
)
971 wxInt32 x
= rect
.GetWidth();
972 wxInt32 w
= x
; // width of area to shade
973 wxInt32 xDelta
= w
/256; // height of one shade bend
981 nR
= nR1
- (nR1
-nR2
)*(w
-x
)/w
;
983 nR
= nR1
+ (nR2
-nR1
)*(w
-x
)/w
;
986 nG
= nG1
- (nG1
-nG2
)*(w
-x
)/w
;
988 nG
= nG1
+ (nG2
-nG1
)*(w
-x
)/w
;
991 nB
= nB1
- (nB1
-nB2
)*(w
-x
)/w
;
993 nB
= nB1
+ (nB2
-nB1
)*(w
-x
)/w
;
995 wxColour
colour(nR
,nG
,nB
);
996 SetPen(wxPen(colour
, 1, wxSOLID
));
997 SetBrush(wxBrush(colour
));
998 if(nDirection
== wxEAST
)
999 DoDrawRectangle(rect
.GetRight()-x
-xDelta
+1, rect
.GetTop(),
1000 xDelta
, rect
.GetHeight());
1001 else //nDirection == wxWEST
1002 DoDrawRectangle(rect
.GetLeft()+x
, rect
.GetTop(),
1003 xDelta
, rect
.GetHeight());
1006 else // nDirection == wxNORTH || nDirection == wxSOUTH
1008 wxInt32 y
= rect
.GetHeight();
1009 wxInt32 w
= y
; // height of area to shade
1010 wxInt32 yDelta
= w
/255; // height of one shade bend
1018 nR
= nR1
- (nR1
-nR2
)*(w
-y
)/w
;
1020 nR
= nR1
+ (nR2
-nR1
)*(w
-y
)/w
;
1023 nG
= nG1
- (nG1
-nG2
)*(w
-y
)/w
;
1025 nG
= nG1
+ (nG2
-nG1
)*(w
-y
)/w
;
1028 nB
= nB1
- (nB1
-nB2
)*(w
-y
)/w
;
1030 nB
= nB1
+ (nB2
-nB1
)*(w
-y
)/w
;
1032 wxColour
colour(nR
,nG
,nB
);
1033 SetPen(wxPen(colour
, 1, wxSOLID
));
1034 SetBrush(wxBrush(colour
));
1035 if(nDirection
== wxNORTH
)
1036 DoDrawRectangle(rect
.GetLeft(), rect
.GetTop()+y
,
1037 rect
.GetWidth(), yDelta
);
1038 else //nDirection == wxSOUTH
1039 DoDrawRectangle(rect
.GetLeft(), rect
.GetBottom()-y
-yDelta
+1,
1040 rect
.GetWidth(), yDelta
);
1048 void wxDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1049 const wxColour
& initialColour
,
1050 const wxColour
& destColour
,
1051 const wxPoint
& circleCenter
)
1053 //save the old pen color
1054 wxColour oldPenColour
= m_pen
.GetColour();
1056 wxUint8 nR1
= destColour
.Red();
1057 wxUint8 nG1
= destColour
.Green();
1058 wxUint8 nB1
= destColour
.Blue();
1059 wxUint8 nR2
= initialColour
.Red();
1060 wxUint8 nG2
= initialColour
.Green();
1061 wxUint8 nB2
= initialColour
.Blue();
1066 wxInt32 cx
= rect
.GetWidth() / 2;
1067 wxInt32 cy
= rect
.GetHeight() / 2;
1075 wxInt32 nCircleOffX
= circleCenter
.x
- (rect
.GetWidth() / 2);
1076 wxInt32 nCircleOffY
= circleCenter
.y
- (rect
.GetHeight() / 2);
1078 for ( wxInt32 x
= 0; x
< rect
.GetWidth(); x
++ )
1080 for ( wxInt32 y
= 0; y
< rect
.GetHeight(); y
++ )
1082 //get color difference
1083 wxInt32 nGradient
= ((nRadius
-
1085 pow((double)(x
- cx
- nCircleOffX
), 2) +
1086 pow((double)(y
- cy
- nCircleOffY
), 2)
1087 )) * 100) / nRadius
;
1089 //normalize Gradient
1094 nR
= (wxUint8
)(nR1
+ ((nR2
- nR1
) * nGradient
/ 100));
1095 nG
= (wxUint8
)(nG1
+ ((nG2
- nG1
) * nGradient
/ 100));
1096 nB
= (wxUint8
)(nB1
+ ((nB2
- nB1
) * nGradient
/ 100));
1099 m_pen
.SetColour(wxColour(nR
,nG
,nB
));
1100 DoDrawPoint(x
+ rect
.GetLeft(), y
+ rect
.GetTop());
1103 //return old pen color
1104 m_pen
.SetColour(oldPenColour
);
1107 //-----------------------------------------------------------------------------
1109 //-----------------------------------------------------------------------------
1111 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxObject
)
1113 void wxDC::DrawLabel(const wxString
& text
,
1114 const wxBitmap
& bitmap
,
1118 wxRect
*rectBounding
)
1120 // find the text position
1121 wxCoord widthText
, heightText
, heightLine
;
1122 GetMultiLineTextExtent(text
, &widthText
, &heightText
, &heightLine
);
1124 wxCoord width
, height
;
1127 width
= widthText
+ bitmap
.GetWidth();
1128 height
= bitmap
.GetHeight();
1133 height
= heightText
;
1137 if ( alignment
& wxALIGN_RIGHT
)
1139 x
= rect
.GetRight() - width
;
1141 else if ( alignment
& wxALIGN_CENTRE_HORIZONTAL
)
1143 x
= (rect
.GetLeft() + rect
.GetRight() + 1 - width
) / 2;
1145 else // alignment & wxALIGN_LEFT
1150 if ( alignment
& wxALIGN_BOTTOM
)
1152 y
= rect
.GetBottom() - height
;
1154 else if ( alignment
& wxALIGN_CENTRE_VERTICAL
)
1156 y
= (rect
.GetTop() + rect
.GetBottom() + 1 - height
) / 2;
1158 else // alignment & wxALIGN_TOP
1163 // draw the bitmap first
1169 DrawBitmap(bitmap
, x
, y
, true /* use mask */);
1171 wxCoord offset
= bitmap
.GetWidth() + 4;
1175 y
+= (height
- heightText
) / 2;
1178 // we will draw the underscore under the accel char later
1179 wxCoord startUnderscore
= 0,
1183 // split the string into lines and draw each of them separately
1185 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
1187 if ( *pc
== _T('\n') || pc
== text
.end() )
1189 int xRealStart
= x
; // init it here to avoid compielr warnings
1191 if ( !curLine
.empty() )
1193 // NB: can't test for !(alignment & wxALIGN_LEFT) because
1194 // wxALIGN_LEFT is 0
1195 if ( alignment
& (wxALIGN_RIGHT
| wxALIGN_CENTRE_HORIZONTAL
) )
1198 GetTextExtent(curLine
, &widthLine
, NULL
);
1200 if ( alignment
& wxALIGN_RIGHT
)
1202 xRealStart
+= width
- widthLine
;
1204 else // if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
1206 xRealStart
+= (width
- widthLine
) / 2;
1209 //else: left aligned, nothing to do
1211 DrawText(curLine
, xRealStart
, y
);
1216 // do we have underscore in this line? we can check yUnderscore
1217 // because it is set below to just y + heightLine if we do
1218 if ( y
== yUnderscore
)
1220 // adjust the horz positions to account for the shift
1221 startUnderscore
+= xRealStart
;
1222 endUnderscore
+= xRealStart
;
1225 if ( pc
== text
.end() )
1230 else // not end of line
1232 if ( pc
- text
.begin() == indexAccel
)
1234 // remeber to draw underscore here
1235 GetTextExtent(curLine
, &startUnderscore
, NULL
);
1237 GetTextExtent(curLine
, &endUnderscore
, NULL
);
1239 yUnderscore
= y
+ heightLine
;
1248 // draw the underscore if found
1249 if ( startUnderscore
!= endUnderscore
)
1251 // it should be of the same colour as text
1252 SetPen(wxPen(GetTextForeground(), 0, wxSOLID
));
1256 DrawLine(startUnderscore
, yUnderscore
, endUnderscore
, yUnderscore
);
1259 // return bounding rect if requested
1262 *rectBounding
= wxRect(x
, y
- heightText
, widthText
, heightText
);
1265 CalcBoundingBox(x0
, y0
);
1266 CalcBoundingBox(x0
+ width0
, y0
+ height
);
1269 #if WXWIN_COMPATIBILITY_2_8
1270 // for compatibility with the old code when wxCoord was long everywhere
1271 void wxDC::GetTextExtent(const wxString
& string
,
1274 long *externalLeading
,
1275 const wxFont
*theFont
) const
1277 wxCoord x2
, y2
, descent2
, externalLeading2
;
1278 m_pimpl
->DoGetTextExtent(string
, &x2
, &y2
,
1279 &descent2
, &externalLeading2
,
1286 *descent
= descent2
;
1287 if ( externalLeading
)
1288 *externalLeading
= externalLeading2
;
1291 void wxDC::GetLogicalOrigin(long *x
, long *y
) const
1294 m_pimpl
->DoGetLogicalOrigin(&x2
, &y2
);
1301 void wxDC::GetDeviceOrigin(long *x
, long *y
) const
1304 m_pimpl
->DoGetDeviceOrigin(&x2
, &y2
);
1311 void wxDC::GetClippingBox(long *x
, long *y
, long *w
, long *h
) const
1313 wxCoord xx
,yy
,ww
,hh
;
1314 m_pimpl
->DoGetClippingBox(&xx
, &yy
, &ww
, &hh
);
1321 #endif // WXWIN_COMPATIBILITY_2_8