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"
52 #include "wx/mac/dcclient.h"
53 #include "wx/mac/dcmemory.h"
54 #include "wx/mac/dcscreen.h"
58 #include "wx/x11/dcclient.h"
59 #include "wx/x11/dcmemory.h"
60 #include "wx/x11/dcscreen.h"
63 //----------------------------------------------------------------------------
65 //----------------------------------------------------------------------------
67 wxDCFactory
*wxDCFactory::m_factory
= NULL
;
69 void wxDCFactory::Set(wxDCFactory
*factory
)
76 wxDCFactory
*wxDCFactory::Get()
79 m_factory
= new wxNativeDCFactory
;
84 class wxDCFactoryCleanupModule
: public wxModule
87 virtual bool OnInit() { return true; }
88 virtual void OnExit() { wxDCFactory::Set(NULL
); }
91 DECLARE_DYNAMIC_CLASS(wxDCFactoryCleanupModule
)
94 IMPLEMENT_DYNAMIC_CLASS(wxDCFactoryCleanupModule
, wxModule
)
96 //-----------------------------------------------------------------------------
98 //-----------------------------------------------------------------------------
100 wxDCImpl
* wxNativeDCFactory::CreateWindowDC( wxWindowDC
*owner
)
102 return new wxWindowDCImpl( owner
);
105 wxDCImpl
* wxNativeDCFactory::CreateWindowDC( wxWindowDC
*owner
, wxWindow
*window
)
107 return new wxWindowDCImpl( owner
, window
);
110 wxDCImpl
* wxNativeDCFactory::CreateClientDC( wxClientDC
*owner
)
112 return new wxClientDCImpl( owner
);
115 wxDCImpl
* wxNativeDCFactory::CreateClientDC( wxClientDC
*owner
, wxWindow
*window
)
117 return new wxClientDCImpl( owner
, window
);
120 wxDCImpl
* wxNativeDCFactory::CreatePaintDC( wxPaintDC
*owner
)
122 return new wxPaintDCImpl( owner
);
125 wxDCImpl
* wxNativeDCFactory::CreatePaintDC( wxPaintDC
*owner
, wxWindow
*window
)
127 return new wxPaintDCImpl( owner
, window
);
130 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
)
132 return new wxMemoryDCImpl( owner
);
135 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
, wxBitmap
&bitmap
)
137 return new wxMemoryDCImpl( owner
, bitmap
);
140 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
, wxDC
*dc
)
142 return new wxMemoryDCImpl( owner
, dc
);
145 wxDCImpl
* wxNativeDCFactory::CreateScreenDC( wxScreenDC
*owner
)
147 return new wxScreenDCImpl( owner
);
150 #if wxUSE_PRINTING_ARCHITECTURE
151 wxDCImpl
*wxNativeDCFactory::CreatePrinterDC( wxPrinterDC
*owner
, const wxPrintData
&data
)
153 wxPrintFactory
*factory
= wxPrintFactory::GetFactory();
154 return factory
->CreatePrinterDCImpl( owner
, data
);
158 //-----------------------------------------------------------------------------
160 //-----------------------------------------------------------------------------
162 IMPLEMENT_ABSTRACT_CLASS(wxWindowDC
, wxDC
)
164 wxWindowDC::wxWindowDC(wxWindow
*win
)
165 : wxDC(wxDCFactory::Get()->CreateWindowDC(this, win
))
169 //-----------------------------------------------------------------------------
171 //-----------------------------------------------------------------------------
173 IMPLEMENT_ABSTRACT_CLASS(wxClientDC
, wxWindowDC
)
175 wxClientDC::wxClientDC(wxWindow
*win
)
176 : wxWindowDC(wxDCFactory::Get()->CreateClientDC(this, win
))
180 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
184 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC
, wxDC
)
186 wxMemoryDC::wxMemoryDC()
187 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this))
191 wxMemoryDC::wxMemoryDC(wxBitmap
& bitmap
)
192 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, bitmap
))
196 wxMemoryDC::wxMemoryDC(wxDC
*dc
)
197 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, dc
))
201 void wxMemoryDC::SelectObject(wxBitmap
& bmp
)
203 // make sure that the given wxBitmap is not sharing its data with other
204 // wxBitmap instances as its contents will be modified by any drawing
205 // operation done on this DC
209 GetImpl()->DoSelect(bmp
);
212 void wxMemoryDC::SelectObjectAsSource(const wxBitmap
& bmp
)
214 GetImpl()->DoSelect(bmp
);
217 const wxBitmap
& wxMemoryDC::GetSelectedBitmap() const
219 return GetImpl()->GetSelectedBitmap();
222 wxBitmap
& wxMemoryDC::GetSelectedBitmap()
224 return GetImpl()->GetSelectedBitmap();
228 //-----------------------------------------------------------------------------
230 //-----------------------------------------------------------------------------
232 IMPLEMENT_ABSTRACT_CLASS(wxPaintDC
, wxClientDC
)
234 wxPaintDC::wxPaintDC(wxWindow
*win
)
235 : wxClientDC(wxDCFactory::Get()->CreatePaintDC(this, win
))
239 //-----------------------------------------------------------------------------
241 //-----------------------------------------------------------------------------
243 IMPLEMENT_DYNAMIC_CLASS(wxScreenDC
, wxWindowDC
)
245 wxScreenDC::wxScreenDC()
246 : wxDC(wxDCFactory::Get()->CreateScreenDC(this))
250 //-----------------------------------------------------------------------------
252 //-----------------------------------------------------------------------------
254 #if wxUSE_PRINTING_ARCHITECTURE
256 IMPLEMENT_DYNAMIC_CLASS(wxPrinterDC
, wxDC
)
258 wxPrinterDC::wxPrinterDC()
259 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, wxPrintData()))
263 wxPrinterDC::wxPrinterDC(const wxPrintData
& data
)
264 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, data
))
268 wxRect
wxPrinterDC::GetPaperRect()
270 return GetImpl()->GetPaperRect();
273 int wxPrinterDC::GetResolution()
275 return GetImpl()->GetResolution();
278 #endif // wxUSE_PRINTING_ARCHITECTURE
280 //-----------------------------------------------------------------------------
282 //-----------------------------------------------------------------------------
284 IMPLEMENT_ABSTRACT_CLASS(wxDCImpl
, wxObject
)
286 wxDCImpl::wxDCImpl( wxDC
*owner
)
288 , m_colour(wxColourDisplay())
292 , m_isBBoxValid(false)
293 , m_logicalOriginX(0), m_logicalOriginY(0)
294 , m_deviceOriginX(0), m_deviceOriginY(0)
295 , m_deviceLocalOriginX(0), m_deviceLocalOriginY(0)
296 , m_logicalScaleX(1.0), m_logicalScaleY(1.0)
297 , m_userScaleX(1.0), m_userScaleY(1.0)
298 , m_scaleX(1.0), m_scaleY(1.0)
299 , m_signX(1), m_signY(1)
300 , m_minX(0), m_minY(0), m_maxX(0), m_maxY(0)
301 , m_clipX1(0), m_clipY1(0), m_clipX2(0), m_clipY2(0)
302 , m_logicalFunction(wxCOPY
)
303 , m_backgroundMode(wxTRANSPARENT
)
304 , m_mappingMode(wxMM_TEXT
)
307 , m_backgroundBrush(*wxTRANSPARENT_BRUSH
)
308 , m_textForegroundColour(*wxBLACK
)
309 , m_textBackgroundColour(*wxWHITE
)
313 , m_hasCustomPalette(false)
314 #endif // wxUSE_PALETTE
318 m_mm_to_pix_x
= (double)wxGetDisplaySize().GetWidth() /
319 (double)wxGetDisplaySizeMM().GetWidth();
320 m_mm_to_pix_y
= (double)wxGetDisplaySize().GetHeight() /
321 (double)wxGetDisplaySizeMM().GetHeight();
327 wxDCImpl::~wxDCImpl()
331 // ----------------------------------------------------------------------------
332 // coordinate conversions and transforms
333 // ----------------------------------------------------------------------------
335 wxCoord
wxDCImpl::DeviceToLogicalX(wxCoord x
) const
337 return wxRound((double)(x
- m_deviceOriginX
- m_deviceLocalOriginX
) / m_scaleX
) * m_signX
+ m_logicalOriginX
;
340 wxCoord
wxDCImpl::DeviceToLogicalY(wxCoord y
) const
342 return wxRound((double)(y
- m_deviceOriginY
- m_deviceLocalOriginY
) / m_scaleY
) * m_signY
+ m_logicalOriginY
;
345 wxCoord
wxDCImpl::DeviceToLogicalXRel(wxCoord x
) const
347 return wxRound((double)(x
) / m_scaleX
);
350 wxCoord
wxDCImpl::DeviceToLogicalYRel(wxCoord y
) const
352 return wxRound((double)(y
) / m_scaleY
);
355 wxCoord
wxDCImpl::LogicalToDeviceX(wxCoord x
) const
357 return wxRound((double)(x
- m_logicalOriginX
) * m_scaleX
) * m_signX
+ m_deviceOriginX
* m_signY
+ m_deviceLocalOriginX
;
360 wxCoord
wxDCImpl::LogicalToDeviceY(wxCoord y
) const
362 return wxRound((double)(y
- m_logicalOriginY
) * m_scaleY
) * m_signY
+ m_deviceOriginY
* m_signY
+ m_deviceLocalOriginY
;
365 wxCoord
wxDCImpl::LogicalToDeviceXRel(wxCoord x
) const
367 return wxRound((double)(x
) * m_scaleX
);
370 wxCoord
wxDCImpl::LogicalToDeviceYRel(wxCoord y
) const
372 return wxRound((double)(y
) * m_scaleY
);
375 void wxDCImpl::ComputeScaleAndOrigin()
377 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
378 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
381 void wxDCImpl::SetMapMode( int mode
)
386 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
389 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
392 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
395 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
399 SetLogicalScale( 1.0, 1.0 );
402 m_mappingMode
= mode
;
405 void wxDCImpl::SetUserScale( double x
, double y
)
407 // allow negative ? -> no
410 ComputeScaleAndOrigin();
413 void wxDCImpl::SetLogicalScale( double x
, double y
)
418 ComputeScaleAndOrigin();
421 void wxDCImpl::SetLogicalOrigin( wxCoord x
, wxCoord y
)
423 m_logicalOriginX
= x
* m_signX
;
424 m_logicalOriginY
= y
* m_signY
;
425 ComputeScaleAndOrigin();
428 void wxDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
432 ComputeScaleAndOrigin();
435 void wxDCImpl::SetDeviceLocalOrigin( wxCoord x
, wxCoord y
)
437 m_deviceLocalOriginX
= x
;
438 m_deviceLocalOriginY
= y
;
439 ComputeScaleAndOrigin();
442 void wxDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
444 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
445 // wxWidgets 2.9: no longer override it
446 m_signX
= (xLeftRight
? 1 : -1);
447 m_signY
= (yBottomUp
? -1 : 1);
448 ComputeScaleAndOrigin();
452 // Each element of the widths array will be the width of the string up to and
453 // including the corresponding character in text. This is the generic
454 // implementation, the port-specific classes should do this with native APIs
455 // if available and if faster. Note: pango_layout_index_to_pos is much slower
456 // than calling GetTextExtent!!
463 FontWidthCache() : m_scaleX(1), m_widths(NULL
) { }
464 ~FontWidthCache() { delete []m_widths
; }
469 m_widths
= new int[FWC_SIZE
];
471 memset(m_widths
, 0, sizeof(int)*FWC_SIZE
);
479 static FontWidthCache s_fontWidthCache
;
481 bool wxDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
485 const size_t len
= text
.length();
489 // reset the cache if font or horizontal scale have changed
490 if ( !s_fontWidthCache
.m_widths
||
491 !wxIsSameDouble(s_fontWidthCache
.m_scaleX
, m_scaleX
) ||
492 (s_fontWidthCache
.m_font
!= GetFont()) )
494 s_fontWidthCache
.Reset();
495 s_fontWidthCache
.m_font
= GetFont();
496 s_fontWidthCache
.m_scaleX
= m_scaleX
;
499 // Calculate the position of each character based on the widths of
500 // the previous characters
502 for ( size_t i
= 0; i
< len
; i
++ )
504 const wxChar c
= text
[i
];
505 unsigned int c_int
= (unsigned int)c
;
507 if ((c_int
< FWC_SIZE
) && (s_fontWidthCache
.m_widths
[c_int
] != 0))
509 w
= s_fontWidthCache
.m_widths
[c_int
];
513 DoGetTextExtent(c
, &w
, &h
);
514 if (c_int
< FWC_SIZE
)
515 s_fontWidthCache
.m_widths
[c_int
] = w
;
519 widths
[i
] = totalWidth
;
525 void wxDCImpl::GetMultiLineTextExtent(const wxString
& text
,
529 const wxFont
*font
) const
531 wxCoord widthTextMax
= 0, widthLine
,
532 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
535 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
537 if ( pc
== text
.end() || *pc
== _T('\n') )
539 if ( curLine
.empty() )
541 // we can't use GetTextExtent - it will return 0 for both width
542 // and height and an empty line should count in height
545 // assume that this line has the same height as the previous
547 if ( !heightLineDefault
)
548 heightLineDefault
= heightLine
;
550 if ( !heightLineDefault
)
552 // but we don't know it yet - choose something reasonable
553 DoGetTextExtent(_T("W"), NULL
, &heightLineDefault
,
557 heightTextTotal
+= heightLineDefault
;
561 DoGetTextExtent(curLine
, &widthLine
, &heightLine
,
563 if ( widthLine
> widthTextMax
)
564 widthTextMax
= widthLine
;
565 heightTextTotal
+= heightLine
;
568 if ( pc
== text
.end() )
586 *y
= heightTextTotal
;
591 void wxDCImpl::DoDrawCheckMark(wxCoord x1
, wxCoord y1
,
592 wxCoord width
, wxCoord height
)
594 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
596 wxCoord x2
= x1
+ width
,
599 // the pen width is calibrated to give 3 for width == height == 10
600 wxDCPenChanger
pen( *m_owner
, wxPen(GetTextForeground(), (width
+ height
+ 1)/7));
602 // we're drawing a scaled version of wx/generic/tick.xpm here
603 wxCoord x3
= x1
+ (4*width
) / 10, // x of the tick bottom
604 y3
= y1
+ height
/ 2; // y of the left tick branch
605 DoDrawLine(x1
, y3
, x3
, y2
);
606 DoDrawLine(x3
, y2
, x2
, y1
);
608 CalcBoundingBox(x1
, y1
);
609 CalcBoundingBox(x2
, y2
);
613 wxDCImpl::DoStretchBlit(wxCoord xdest
, wxCoord ydest
,
614 wxCoord dstWidth
, wxCoord dstHeight
,
616 wxCoord xsrc
, wxCoord ysrc
,
617 wxCoord srcWidth
, wxCoord srcHeight
,
623 wxCHECK_MSG( srcWidth
&& srcHeight
&& dstWidth
&& dstHeight
, false,
624 _T("invalid blit size") );
626 // emulate the stretching by modifying the DC scale
627 double xscale
= (double)srcWidth
/dstWidth
,
628 yscale
= (double)srcHeight
/dstHeight
;
630 double xscaleOld
, yscaleOld
;
631 GetUserScale(&xscaleOld
, &yscaleOld
);
632 SetUserScale(xscaleOld
/xscale
, yscaleOld
/yscale
);
634 bool rc
= DoBlit(wxCoord(xdest
*xscale
), wxCoord(ydest
*yscale
),
635 wxCoord(dstWidth
*xscale
), wxCoord(dstHeight
*yscale
),
637 xsrc
, ysrc
, rop
, useMask
, xsrcMask
, ysrcMask
);
639 SetUserScale(xscaleOld
, yscaleOld
);
644 void wxDCImpl::DrawLines(const wxPointList
*list
, wxCoord xoffset
, wxCoord yoffset
)
646 int n
= list
->GetCount();
647 wxPoint
*points
= new wxPoint
[n
];
650 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
652 wxPoint
*point
= node
->GetData();
653 points
[i
].x
= point
->x
;
654 points
[i
].y
= point
->y
;
657 DoDrawLines(n
, points
, xoffset
, yoffset
);
662 void wxDCImpl::DrawPolygon(const wxPointList
*list
,
663 wxCoord xoffset
, wxCoord yoffset
,
666 int n
= list
->GetCount();
667 wxPoint
*points
= new wxPoint
[n
];
670 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
672 wxPoint
*point
= node
->GetData();
673 points
[i
].x
= point
->x
;
674 points
[i
].y
= point
->y
;
677 DoDrawPolygon(n
, points
, xoffset
, yoffset
, fillStyle
);
683 wxDCImpl::DoDrawPolyPolygon(int n
,
686 wxCoord xoffset
, wxCoord yoffset
,
691 DoDrawPolygon(count
[0], points
, xoffset
, yoffset
, fillStyle
);
699 for (i
= j
= lastOfs
= 0; i
< n
; i
++)
704 pts
= new wxPoint
[j
+n
-1];
705 for (i
= 0; i
< j
; i
++)
707 for (i
= 2; i
<= n
; i
++)
709 lastOfs
-= count
[n
-i
];
710 pts
[j
++] = pts
[lastOfs
];
714 SetPen(wxPen(*wxBLACK
, 0, wxTRANSPARENT
));
715 DoDrawPolygon(j
, pts
, xoffset
, yoffset
, fillStyle
);
717 for (i
= j
= 0; i
< n
; i
++)
719 DoDrawLines(count
[i
], pts
+j
, xoffset
, yoffset
);
727 void wxDCImpl::DoDrawSpline(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
, wxCoord x3
, wxCoord y3
)
729 wxPointList point_list
;
731 wxPoint
*point1
= new wxPoint
;
732 point1
->x
= x1
; point1
->y
= y1
;
733 point_list
.Append( point1
);
735 wxPoint
*point2
= new wxPoint
;
736 point2
->x
= x2
; point2
->y
= y2
;
737 point_list
.Append( point2
);
739 wxPoint
*point3
= new wxPoint
;
740 point3
->x
= x3
; point3
->y
= y3
;
741 point_list
.Append( point3
);
743 DoDrawSpline(&point_list
);
745 for( wxPointList::compatibility_iterator node
= point_list
.GetFirst(); node
; node
= node
->GetNext() )
747 wxPoint
*p
= node
->GetData();
752 void wxDCImpl::DoDrawSpline(int n
, wxPoint points
[])
755 for (int i
=0; i
< n
; i
++)
756 list
.Append( &points
[i
] );
761 // ----------------------------------- spline code ----------------------------------------
763 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
,
764 double a3
, double b3
, double a4
, double b4
);
765 void wx_clear_stack();
766 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
, double *x3
,
767 double *y3
, double *x4
, double *y4
);
768 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
,
769 double x4
, double y4
);
770 static bool wx_spline_add_point(double x
, double y
);
771 static void wx_spline_draw_point_array(wxDC
*dc
);
773 wxPointList wx_spline_point_list
;
775 #define half(z1, z2) ((z1+z2)/2.0)
778 /* iterative version */
780 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
, double a3
, double b3
, double a4
,
783 register double xmid
, ymid
;
784 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
787 wx_spline_push(a1
, b1
, a2
, b2
, a3
, b3
, a4
, b4
);
789 while (wx_spline_pop(&x1
, &y1
, &x2
, &y2
, &x3
, &y3
, &x4
, &y4
)) {
790 xmid
= (double)half(x2
, x3
);
791 ymid
= (double)half(y2
, y3
);
792 if (fabs(x1
- xmid
) < THRESHOLD
&& fabs(y1
- ymid
) < THRESHOLD
&&
793 fabs(xmid
- x4
) < THRESHOLD
&& fabs(ymid
- y4
) < THRESHOLD
) {
794 wx_spline_add_point( x1
, y1
);
795 wx_spline_add_point( xmid
, ymid
);
797 wx_spline_push(xmid
, ymid
, (double)half(xmid
, x3
), (double)half(ymid
, y3
),
798 (double)half(x3
, x4
), (double)half(y3
, y4
), x4
, y4
);
799 wx_spline_push(x1
, y1
, (double)half(x1
, x2
), (double)half(y1
, y2
),
800 (double)half(x2
, xmid
), (double)half(y2
, ymid
), xmid
, ymid
);
805 /* utilities used by spline drawing routines */
807 typedef struct wx_spline_stack_struct
{
808 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
811 #define SPLINE_STACK_DEPTH 20
812 static Stack wx_spline_stack
[SPLINE_STACK_DEPTH
];
813 static Stack
*wx_stack_top
;
814 static int wx_stack_count
;
816 void wx_clear_stack()
818 wx_stack_top
= wx_spline_stack
;
822 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
, double x4
, double y4
)
824 wx_stack_top
->x1
= x1
;
825 wx_stack_top
->y1
= y1
;
826 wx_stack_top
->x2
= x2
;
827 wx_stack_top
->y2
= y2
;
828 wx_stack_top
->x3
= x3
;
829 wx_stack_top
->y3
= y3
;
830 wx_stack_top
->x4
= x4
;
831 wx_stack_top
->y4
= y4
;
836 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
,
837 double *x3
, double *y3
, double *x4
, double *y4
)
839 if (wx_stack_count
== 0)
843 *x1
= wx_stack_top
->x1
;
844 *y1
= wx_stack_top
->y1
;
845 *x2
= wx_stack_top
->x2
;
846 *y2
= wx_stack_top
->y2
;
847 *x3
= wx_stack_top
->x3
;
848 *y3
= wx_stack_top
->y3
;
849 *x4
= wx_stack_top
->x4
;
850 *y4
= wx_stack_top
->y4
;
854 static bool wx_spline_add_point(double x
, double y
)
856 wxPoint
*point
= new wxPoint( wxRound(x
), wxRound(y
) );
857 wx_spline_point_list
.Append(point
);
861 static void wx_spline_draw_point_array(wxDC
*dc
)
863 dc
->DrawLines(&wx_spline_point_list
, 0, 0 );
864 wxPointList::compatibility_iterator node
= wx_spline_point_list
.GetFirst();
867 wxPoint
*point
= node
->GetData();
869 wx_spline_point_list
.Erase(node
);
870 node
= wx_spline_point_list
.GetFirst();
874 void wxDCImpl::DoDrawSpline( const wxPointList
*points
)
876 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
879 double cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
;
880 double x1
, y1
, x2
, y2
;
882 wxPointList::compatibility_iterator node
= points
->GetFirst();
887 p
= (wxPoint
*)node
->GetData();
892 node
= node
->GetNext();
897 cx1
= (double)((x1
+ x2
) / 2);
898 cy1
= (double)((y1
+ y2
) / 2);
899 cx2
= (double)((cx1
+ x2
) / 2);
900 cy2
= (double)((cy1
+ y2
) / 2);
902 wx_spline_add_point(x1
, y1
);
904 while ((node
= node
->GetNext())
915 cx4
= (double)(x1
+ x2
) / 2;
916 cy4
= (double)(y1
+ y2
) / 2;
917 cx3
= (double)(x1
+ cx4
) / 2;
918 cy3
= (double)(y1
+ cy4
) / 2;
920 wx_quadratic_spline(cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
);
924 cx2
= (double)(cx1
+ x2
) / 2;
925 cy2
= (double)(cy1
+ y2
) / 2;
928 wx_spline_add_point( cx1
, cy1
);
929 wx_spline_add_point( x2
, y2
);
931 wx_spline_draw_point_array( m_owner
);
934 #endif // wxUSE_SPLINES
938 void wxDCImpl::DoGradientFillLinear(const wxRect
& rect
,
939 const wxColour
& initialColour
,
940 const wxColour
& destColour
,
941 wxDirection nDirection
)
944 wxPen oldPen
= m_pen
;
945 wxBrush oldBrush
= m_brush
;
947 wxUint8 nR1
= initialColour
.Red();
948 wxUint8 nG1
= initialColour
.Green();
949 wxUint8 nB1
= initialColour
.Blue();
950 wxUint8 nR2
= destColour
.Red();
951 wxUint8 nG2
= destColour
.Green();
952 wxUint8 nB2
= destColour
.Blue();
955 if ( nDirection
== wxEAST
|| nDirection
== wxWEST
)
957 wxInt32 x
= rect
.GetWidth();
958 wxInt32 w
= x
; // width of area to shade
959 wxInt32 xDelta
= w
/256; // height of one shade bend
967 nR
= nR1
- (nR1
-nR2
)*(w
-x
)/w
;
969 nR
= nR1
+ (nR2
-nR1
)*(w
-x
)/w
;
972 nG
= nG1
- (nG1
-nG2
)*(w
-x
)/w
;
974 nG
= nG1
+ (nG2
-nG1
)*(w
-x
)/w
;
977 nB
= nB1
- (nB1
-nB2
)*(w
-x
)/w
;
979 nB
= nB1
+ (nB2
-nB1
)*(w
-x
)/w
;
981 wxColour
colour(nR
,nG
,nB
);
982 SetPen(wxPen(colour
, 1, wxSOLID
));
983 SetBrush(wxBrush(colour
));
984 if(nDirection
== wxEAST
)
985 DoDrawRectangle(rect
.GetRight()-x
-xDelta
+1, rect
.GetTop(),
986 xDelta
, rect
.GetHeight());
987 else //nDirection == wxWEST
988 DoDrawRectangle(rect
.GetLeft()+x
, rect
.GetTop(),
989 xDelta
, rect
.GetHeight());
992 else // nDirection == wxNORTH || nDirection == wxSOUTH
994 wxInt32 y
= rect
.GetHeight();
995 wxInt32 w
= y
; // height of area to shade
996 wxInt32 yDelta
= w
/255; // height of one shade bend
1004 nR
= nR1
- (nR1
-nR2
)*(w
-y
)/w
;
1006 nR
= nR1
+ (nR2
-nR1
)*(w
-y
)/w
;
1009 nG
= nG1
- (nG1
-nG2
)*(w
-y
)/w
;
1011 nG
= nG1
+ (nG2
-nG1
)*(w
-y
)/w
;
1014 nB
= nB1
- (nB1
-nB2
)*(w
-y
)/w
;
1016 nB
= nB1
+ (nB2
-nB1
)*(w
-y
)/w
;
1018 wxColour
colour(nR
,nG
,nB
);
1019 SetPen(wxPen(colour
, 1, wxSOLID
));
1020 SetBrush(wxBrush(colour
));
1021 if(nDirection
== wxNORTH
)
1022 DoDrawRectangle(rect
.GetLeft(), rect
.GetTop()+y
,
1023 rect
.GetWidth(), yDelta
);
1024 else //nDirection == wxSOUTH
1025 DoDrawRectangle(rect
.GetLeft(), rect
.GetBottom()-y
-yDelta
+1,
1026 rect
.GetWidth(), yDelta
);
1034 void wxDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1035 const wxColour
& initialColour
,
1036 const wxColour
& destColour
,
1037 const wxPoint
& circleCenter
)
1039 //save the old pen color
1040 wxColour oldPenColour
= m_pen
.GetColour();
1042 wxUint8 nR1
= destColour
.Red();
1043 wxUint8 nG1
= destColour
.Green();
1044 wxUint8 nB1
= destColour
.Blue();
1045 wxUint8 nR2
= initialColour
.Red();
1046 wxUint8 nG2
= initialColour
.Green();
1047 wxUint8 nB2
= initialColour
.Blue();
1052 wxInt32 cx
= rect
.GetWidth() / 2;
1053 wxInt32 cy
= rect
.GetHeight() / 2;
1061 wxInt32 nCircleOffX
= circleCenter
.x
- (rect
.GetWidth() / 2);
1062 wxInt32 nCircleOffY
= circleCenter
.y
- (rect
.GetHeight() / 2);
1064 for ( wxInt32 x
= 0; x
< rect
.GetWidth(); x
++ )
1066 for ( wxInt32 y
= 0; y
< rect
.GetHeight(); y
++ )
1068 //get color difference
1069 wxInt32 nGradient
= ((nRadius
-
1071 pow((double)(x
- cx
- nCircleOffX
), 2) +
1072 pow((double)(y
- cy
- nCircleOffY
), 2)
1073 )) * 100) / nRadius
;
1075 //normalize Gradient
1080 nR
= (wxUint8
)(nR1
+ ((nR2
- nR1
) * nGradient
/ 100));
1081 nG
= (wxUint8
)(nG1
+ ((nG2
- nG1
) * nGradient
/ 100));
1082 nB
= (wxUint8
)(nB1
+ ((nB2
- nB1
) * nGradient
/ 100));
1085 m_pen
.SetColour(wxColour(nR
,nG
,nB
));
1086 DoDrawPoint(x
+ rect
.GetLeft(), y
+ rect
.GetTop());
1089 //return old pen color
1090 m_pen
.SetColour(oldPenColour
);
1093 //-----------------------------------------------------------------------------
1095 //-----------------------------------------------------------------------------
1097 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxObject
)
1099 void wxDC::DrawLabel(const wxString
& text
,
1100 const wxBitmap
& bitmap
,
1104 wxRect
*rectBounding
)
1106 // find the text position
1107 wxCoord widthText
, heightText
, heightLine
;
1108 GetMultiLineTextExtent(text
, &widthText
, &heightText
, &heightLine
);
1110 wxCoord width
, height
;
1113 width
= widthText
+ bitmap
.GetWidth();
1114 height
= bitmap
.GetHeight();
1119 height
= heightText
;
1123 if ( alignment
& wxALIGN_RIGHT
)
1125 x
= rect
.GetRight() - width
;
1127 else if ( alignment
& wxALIGN_CENTRE_HORIZONTAL
)
1129 x
= (rect
.GetLeft() + rect
.GetRight() + 1 - width
) / 2;
1131 else // alignment & wxALIGN_LEFT
1136 if ( alignment
& wxALIGN_BOTTOM
)
1138 y
= rect
.GetBottom() - height
;
1140 else if ( alignment
& wxALIGN_CENTRE_VERTICAL
)
1142 y
= (rect
.GetTop() + rect
.GetBottom() + 1 - height
) / 2;
1144 else // alignment & wxALIGN_TOP
1149 // draw the bitmap first
1155 DrawBitmap(bitmap
, x
, y
, true /* use mask */);
1157 wxCoord offset
= bitmap
.GetWidth() + 4;
1161 y
+= (height
- heightText
) / 2;
1164 // we will draw the underscore under the accel char later
1165 wxCoord startUnderscore
= 0,
1169 // split the string into lines and draw each of them separately
1171 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
1173 if ( *pc
== _T('\n') || pc
== text
.end() )
1175 int xRealStart
= x
; // init it here to avoid compielr warnings
1177 if ( !curLine
.empty() )
1179 // NB: can't test for !(alignment & wxALIGN_LEFT) because
1180 // wxALIGN_LEFT is 0
1181 if ( alignment
& (wxALIGN_RIGHT
| wxALIGN_CENTRE_HORIZONTAL
) )
1184 GetTextExtent(curLine
, &widthLine
, NULL
);
1186 if ( alignment
& wxALIGN_RIGHT
)
1188 xRealStart
+= width
- widthLine
;
1190 else // if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
1192 xRealStart
+= (width
- widthLine
) / 2;
1195 //else: left aligned, nothing to do
1197 DrawText(curLine
, xRealStart
, y
);
1202 // do we have underscore in this line? we can check yUnderscore
1203 // because it is set below to just y + heightLine if we do
1204 if ( y
== yUnderscore
)
1206 // adjust the horz positions to account for the shift
1207 startUnderscore
+= xRealStart
;
1208 endUnderscore
+= xRealStart
;
1211 if ( pc
== text
.end() )
1216 else // not end of line
1218 if ( pc
- text
.begin() == indexAccel
)
1220 // remeber to draw underscore here
1221 GetTextExtent(curLine
, &startUnderscore
, NULL
);
1223 GetTextExtent(curLine
, &endUnderscore
, NULL
);
1225 yUnderscore
= y
+ heightLine
;
1234 // draw the underscore if found
1235 if ( startUnderscore
!= endUnderscore
)
1237 // it should be of the same colour as text
1238 SetPen(wxPen(GetTextForeground(), 0, wxSOLID
));
1242 DrawLine(startUnderscore
, yUnderscore
, endUnderscore
, yUnderscore
);
1245 // return bounding rect if requested
1248 *rectBounding
= wxRect(x
, y
- heightText
, widthText
, heightText
);
1251 CalcBoundingBox(x0
, y0
);
1252 CalcBoundingBox(x0
+ width0
, y0
+ height
);
1255 #if WXWIN_COMPATIBILITY_2_8
1256 // for compatibility with the old code when wxCoord was long everywhere
1257 void wxDC::GetTextExtent(const wxString
& string
,
1260 long *externalLeading
,
1261 const wxFont
*theFont
) const
1263 wxCoord x2
, y2
, descent2
, externalLeading2
;
1264 m_pimpl
->DoGetTextExtent(string
, &x2
, &y2
,
1265 &descent2
, &externalLeading2
,
1272 *descent
= descent2
;
1273 if ( externalLeading
)
1274 *externalLeading
= externalLeading2
;
1277 void wxDC::GetLogicalOrigin(long *x
, long *y
) const
1280 m_pimpl
->DoGetLogicalOrigin(&x2
, &y2
);
1287 void wxDC::GetDeviceOrigin(long *x
, long *y
) const
1290 m_pimpl
->DoGetDeviceOrigin(&x2
, &y2
);
1297 void wxDC::GetClippingBox(long *x
, long *y
, long *w
, long *h
) const
1299 wxCoord xx
,yy
,ww
,hh
;
1300 m_pimpl
->DoGetClippingBox(&xx
, &yy
, &ww
, &hh
);
1307 #endif // WXWIN_COMPATIBILITY_2_8