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/dcbuffer.h" // for IMPLEMENT_DYNAMIC_CLASS
33 #include "wx/prntbase.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
::SetDCFactory( wxDCFactory
*factory
)
71 if (wxDCFactory
::m_factory
)
72 delete wxDCFactory
::m_factory
;
74 wxDCFactory
::m_factory
= factory
;
77 wxDCFactory
*wxDCFactory
::GetFactory()
79 if (!wxDCFactory
::m_factory
)
80 wxDCFactory
::m_factory
= new wxNativeDCFactory
;
82 return wxDCFactory
::m_factory
;
85 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
89 wxDCImpl
* wxNativeDCFactory
::CreateWindowDC( wxWindowDC
*owner
)
91 return new wxWindowDCImpl( owner
);
94 wxDCImpl
* wxNativeDCFactory
::CreateWindowDC( wxWindowDC
*owner
, wxWindow
*window
)
96 return new wxWindowDCImpl( owner
, window
);
99 wxDCImpl
* wxNativeDCFactory
::CreateClientDC( wxClientDC
*owner
)
101 return new wxClientDCImpl( owner
);
104 wxDCImpl
* wxNativeDCFactory
::CreateClientDC( wxClientDC
*owner
, wxWindow
*window
)
106 return new wxClientDCImpl( owner
, window
);
109 wxDCImpl
* wxNativeDCFactory
::CreatePaintDC( wxPaintDC
*owner
)
111 return new wxPaintDCImpl( owner
);
114 wxDCImpl
* wxNativeDCFactory
::CreatePaintDC( wxPaintDC
*owner
, wxWindow
*window
)
116 return new wxPaintDCImpl( owner
, window
);
119 wxDCImpl
* wxNativeDCFactory
::CreateMemoryDC( wxMemoryDC
*owner
)
121 return new wxMemoryDCImpl( owner
);
124 wxDCImpl
* wxNativeDCFactory
::CreateMemoryDC( wxMemoryDC
*owner
, wxBitmap
&bitmap
)
126 return new wxMemoryDCImpl( owner
, bitmap
);
129 wxDCImpl
* wxNativeDCFactory
::CreateMemoryDC( wxMemoryDC
*owner
, wxDC
*dc
)
131 return new wxMemoryDCImpl( owner
, dc
);
134 wxDCImpl
* wxNativeDCFactory
::CreateScreenDC( wxScreenDC
*owner
)
136 return new wxScreenDCImpl( owner
);
139 #if wxUSE_PRINTING_ARCHITECTURE
140 wxDCImpl
*wxNativeDCFactory
::CreatePrinterDC( wxPrinterDC
*owner
, const wxPrintData
&data
)
142 wxPrintFactory
*factory
= wxPrintFactory
::GetFactory();
143 return factory
->CreatePrinterDCImpl( owner
, data
);
147 //-----------------------------------------------------------------------------
149 //-----------------------------------------------------------------------------
151 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC
, wxDC
)
153 wxWindowDC
::wxWindowDC()
157 wxWindowDC
::wxWindowDC( wxWindow
*win
)
159 wxDCFactory
*factory
= wxDCFactory
::GetFactory();
160 m_pimpl
= factory
->CreateWindowDC( this, win
);
163 //-----------------------------------------------------------------------------
165 //-----------------------------------------------------------------------------
167 IMPLEMENT_DYNAMIC_CLASS(wxClientDC
, wxWindowDC
)
169 wxClientDC
::wxClientDC()
173 wxClientDC
::wxClientDC( wxWindow
*win
)
175 wxDCFactory
*factory
= wxDCFactory
::GetFactory();
176 m_pimpl
= factory
->CreateClientDC( this, win
);
179 //-----------------------------------------------------------------------------
181 //-----------------------------------------------------------------------------
183 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC
, wxDC
)
185 wxMemoryDC
::wxMemoryDC()
187 wxDCFactory
*factory
= wxDCFactory
::GetFactory();
188 m_pimpl
= factory
->CreateMemoryDC( this );
191 wxMemoryDC
::wxMemoryDC( wxBitmap
& bitmap
)
193 wxDCFactory
*factory
= wxDCFactory
::GetFactory();
194 m_pimpl
= factory
->CreateMemoryDC( this, bitmap
);
197 wxMemoryDC
::wxMemoryDC( wxDC
*dc
)
199 wxDCFactory
*factory
= wxDCFactory
::GetFactory();
200 m_pimpl
= factory
->CreateMemoryDC( this, dc
);
203 void wxMemoryDC
::SelectObject(wxBitmap
& bmp
)
205 // make sure that the given wxBitmap is not sharing its data with other
206 // wxBitmap instances as its contents will be modified by any drawing
207 // operation done on this DC
211 GetImpl()->DoSelect(bmp
);
214 void wxMemoryDC
::SelectObjectAsSource(const wxBitmap
& bmp
)
216 GetImpl()->DoSelect(bmp
);
219 const wxBitmap
& wxMemoryDC
::GetSelectedBitmap() const
221 return GetImpl()->GetSelectedBitmap();
224 wxBitmap
& wxMemoryDC
::GetSelectedBitmap()
226 return GetImpl()->GetSelectedBitmap();
230 //-----------------------------------------------------------------------------
232 //-----------------------------------------------------------------------------
234 IMPLEMENT_DYNAMIC_CLASS(wxPaintDC
, wxClientDC
)
236 wxPaintDC
::wxPaintDC()
240 wxPaintDC
::wxPaintDC( wxWindow
*win
)
242 wxDCFactory
*factory
= wxDCFactory
::GetFactory();
243 m_pimpl
= factory
->CreatePaintDC( this, win
);
246 //-----------------------------------------------------------------------------
248 //-----------------------------------------------------------------------------
250 IMPLEMENT_DYNAMIC_CLASS(wxScreenDC
, wxWindowDC
)
252 wxScreenDC
::wxScreenDC()
254 wxDCFactory
*factory
= wxDCFactory
::GetFactory();
255 m_pimpl
= factory
->CreateScreenDC( this );
258 //-----------------------------------------------------------------------------
260 //-----------------------------------------------------------------------------
262 #if wxUSE_PRINTING_ARCHITECTURE
264 IMPLEMENT_DYNAMIC_CLASS(wxPrinterDC
, wxDC
)
266 wxPrinterDC
::wxPrinterDC()
268 wxPrintData data
; // Does this make sense?
269 wxDCFactory
*factory
= wxDCFactory
::GetFactory();
270 m_pimpl
= factory
->CreatePrinterDC( this, data
);
273 wxPrinterDC
::wxPrinterDC( const wxPrintData
&data
)
275 wxDCFactory
*factory
= wxDCFactory
::GetFactory();
276 m_pimpl
= factory
->CreatePrinterDC( this, data
);
279 wxPrinterDC
::~wxPrinterDC()
283 wxRect wxPrinterDC
::GetPaperRect()
285 return GetImpl()->GetPaperRect();
288 int wxPrinterDC
::GetResolution()
290 return GetImpl()->GetResolution();
295 //-----------------------------------------------------------------------------
297 //-----------------------------------------------------------------------------
299 IMPLEMENT_ABSTRACT_CLASS(wxDCImpl
, wxObject
)
301 wxDCImpl
::wxDCImpl( wxDC
*owner
)
303 , m_colour(wxColourDisplay())
307 , m_isBBoxValid(false)
308 , m_logicalOriginX(0), m_logicalOriginY(0)
309 , m_deviceOriginX(0), m_deviceOriginY(0)
310 , m_deviceLocalOriginX(0), m_deviceLocalOriginY(0)
311 , m_logicalScaleX(1.0), m_logicalScaleY(1.0)
312 , m_userScaleX(1.0), m_userScaleY(1.0)
313 , m_scaleX(1.0), m_scaleY(1.0)
314 , m_signX(1), m_signY(1)
315 , m_minX(0), m_minY(0), m_maxX(0), m_maxY(0)
316 , m_clipX1(0), m_clipY1(0), m_clipX2(0), m_clipY2(0)
317 , m_logicalFunction(wxCOPY
)
318 , m_backgroundMode(wxTRANSPARENT
)
319 , m_mappingMode(wxMM_TEXT
)
322 , m_backgroundBrush(*wxTRANSPARENT_BRUSH
)
323 , m_textForegroundColour(*wxBLACK
)
324 , m_textBackgroundColour(*wxWHITE
)
328 , m_hasCustomPalette(false)
329 #endif // wxUSE_PALETTE
333 m_mm_to_pix_x
= (double)wxGetDisplaySize().GetWidth() /
334 (double)wxGetDisplaySizeMM().GetWidth();
335 m_mm_to_pix_y
= (double)wxGetDisplaySize().GetHeight() /
336 (double)wxGetDisplaySizeMM().GetHeight();
342 wxDCImpl
::~wxDCImpl()
346 // ----------------------------------------------------------------------------
347 // coordinate conversions and transforms
348 // ----------------------------------------------------------------------------
350 wxCoord wxDCImpl
::DeviceToLogicalX(wxCoord x
) const
352 return wxRound((double)(x
- m_deviceOriginX
- m_deviceLocalOriginX
) / m_scaleX
) * m_signX
+ m_logicalOriginX
;
355 wxCoord wxDCImpl
::DeviceToLogicalY(wxCoord y
) const
357 return wxRound((double)(y
- m_deviceOriginY
- m_deviceLocalOriginY
) / m_scaleY
) * m_signY
+ m_logicalOriginY
;
360 wxCoord wxDCImpl
::DeviceToLogicalXRel(wxCoord x
) const
362 return wxRound((double)(x
) / m_scaleX
);
365 wxCoord wxDCImpl
::DeviceToLogicalYRel(wxCoord y
) const
367 return wxRound((double)(y
) / m_scaleY
);
370 wxCoord wxDCImpl
::LogicalToDeviceX(wxCoord x
) const
372 return wxRound((double)(x
- m_logicalOriginX
) * m_scaleX
) * m_signX
+ m_deviceOriginX
* m_signY
+ m_deviceLocalOriginX
;
375 wxCoord wxDCImpl
::LogicalToDeviceY(wxCoord y
) const
377 return wxRound((double)(y
- m_logicalOriginY
) * m_scaleY
) * m_signY
+ m_deviceOriginY
* m_signY
+ m_deviceLocalOriginY
;
380 wxCoord wxDCImpl
::LogicalToDeviceXRel(wxCoord x
) const
382 return wxRound((double)(x
) * m_scaleX
);
385 wxCoord wxDCImpl
::LogicalToDeviceYRel(wxCoord y
) const
387 return wxRound((double)(y
) * m_scaleY
);
390 void wxDCImpl
::ComputeScaleAndOrigin()
392 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
393 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
396 void wxDCImpl
::SetMapMode( int mode
)
401 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
404 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
407 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
410 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
414 SetLogicalScale( 1.0, 1.0 );
417 m_mappingMode
= mode
;
420 void wxDCImpl
::SetUserScale( double x
, double y
)
422 // allow negative ? -> no
425 ComputeScaleAndOrigin();
428 void wxDCImpl
::SetLogicalScale( double x
, double y
)
433 ComputeScaleAndOrigin();
436 void wxDCImpl
::SetLogicalOrigin( wxCoord x
, wxCoord y
)
438 m_logicalOriginX
= x
* m_signX
;
439 m_logicalOriginY
= y
* m_signY
;
440 ComputeScaleAndOrigin();
443 void wxDCImpl
::SetDeviceOrigin( wxCoord x
, wxCoord y
)
447 ComputeScaleAndOrigin();
450 void wxDCImpl
::SetDeviceLocalOrigin( wxCoord x
, wxCoord y
)
452 m_deviceLocalOriginX
= x
;
453 m_deviceLocalOriginY
= y
;
454 ComputeScaleAndOrigin();
457 void wxDCImpl
::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
459 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
460 // wxWidgets 2.9: no longer override it
461 m_signX
= (xLeftRight ?
1 : -1);
462 m_signY
= (yBottomUp ?
-1 : 1);
463 ComputeScaleAndOrigin();
467 // Each element of the widths array will be the width of the string up to and
468 // including the corresponding character in text. This is the generic
469 // implementation, the port-specific classes should do this with native APIs
470 // if available and if faster. Note: pango_layout_index_to_pos is much slower
471 // than calling GetTextExtent!!
478 FontWidthCache() : m_scaleX(1), m_widths(NULL
) { }
479 ~FontWidthCache() { delete []m_widths
; }
484 m_widths
= new int[FWC_SIZE
];
486 memset(m_widths
, 0, sizeof(int)*FWC_SIZE
);
494 static FontWidthCache s_fontWidthCache
;
496 bool wxDCImpl
::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
500 const size_t len
= text
.length();
504 // reset the cache if font or horizontal scale have changed
505 if ( !s_fontWidthCache
.m_widths
||
506 !wxIsSameDouble(s_fontWidthCache
.m_scaleX
, m_scaleX
) ||
507 (s_fontWidthCache
.m_font
!= GetFont()) )
509 s_fontWidthCache
.Reset();
510 s_fontWidthCache
.m_font
= GetFont();
511 s_fontWidthCache
.m_scaleX
= m_scaleX
;
514 // Calculate the position of each character based on the widths of
515 // the previous characters
517 for ( size_t i
= 0; i
< len
; i
++ )
519 const wxChar c
= text
[i
];
520 unsigned int c_int
= (unsigned int)c
;
522 if ((c_int
< FWC_SIZE
) && (s_fontWidthCache
.m_widths
[c_int
] != 0))
524 w
= s_fontWidthCache
.m_widths
[c_int
];
528 DoGetTextExtent(c
, &w
, &h
);
529 if (c_int
< FWC_SIZE
)
530 s_fontWidthCache
.m_widths
[c_int
] = w
;
534 widths
[i
] = totalWidth
;
540 void wxDCImpl
::GetMultiLineTextExtent(const wxString
& text
,
544 const wxFont
*font
) const
546 wxCoord widthTextMax
= 0, widthLine
,
547 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
550 for ( wxString
::const_iterator pc
= text
.begin(); ; ++pc
)
552 if ( pc
== text
.end() || *pc
== _T('\n') )
554 if ( curLine
.empty() )
556 // we can't use GetTextExtent - it will return 0 for both width
557 // and height and an empty line should count in height
560 // assume that this line has the same height as the previous
562 if ( !heightLineDefault
)
563 heightLineDefault
= heightLine
;
565 if ( !heightLineDefault
)
567 // but we don't know it yet - choose something reasonable
568 DoGetTextExtent(_T("W"), NULL
, &heightLineDefault
,
572 heightTextTotal
+= heightLineDefault
;
576 DoGetTextExtent(curLine
, &widthLine
, &heightLine
,
578 if ( widthLine
> widthTextMax
)
579 widthTextMax
= widthLine
;
580 heightTextTotal
+= heightLine
;
583 if ( pc
== text
.end() )
601 *y
= heightTextTotal
;
606 void wxDCImpl
::DoDrawCheckMark(wxCoord x1
, wxCoord y1
,
607 wxCoord width
, wxCoord height
)
609 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
611 wxCoord x2
= x1
+ width
,
614 // the pen width is calibrated to give 3 for width == height == 10
615 wxDCPenChanger
pen( *m_owner
, wxPen(GetTextForeground(), (width
+ height
+ 1)/7));
617 // we're drawing a scaled version of wx/generic/tick.xpm here
618 wxCoord x3
= x1
+ (4*width
) / 10, // x of the tick bottom
619 y3
= y1
+ height
/ 2; // y of the left tick branch
620 DoDrawLine(x1
, y3
, x3
, y2
);
621 DoDrawLine(x3
, y2
, x2
, y1
);
623 CalcBoundingBox(x1
, y1
);
624 CalcBoundingBox(x2
, y2
);
628 wxDCImpl
::DoStretchBlit(wxCoord xdest
, wxCoord ydest
,
629 wxCoord dstWidth
, wxCoord dstHeight
,
631 wxCoord xsrc
, wxCoord ysrc
,
632 wxCoord srcWidth
, wxCoord srcHeight
,
638 wxCHECK_MSG( srcWidth
&& srcHeight
&& dstWidth
&& dstHeight
, false,
639 _T("invalid blit size") );
641 // emulate the stretching by modifying the DC scale
642 double xscale
= (double)srcWidth
/dstWidth
,
643 yscale
= (double)srcHeight
/dstHeight
;
645 double xscaleOld
, yscaleOld
;
646 GetUserScale(&xscaleOld
, &yscaleOld
);
647 SetUserScale(xscaleOld
/xscale
, yscaleOld
/yscale
);
649 bool rc
= DoBlit(wxCoord(xdest
*xscale
), wxCoord(ydest
*yscale
),
650 wxCoord(dstWidth
*xscale
), wxCoord(dstHeight
*yscale
),
652 xsrc
, ysrc
, rop
, useMask
, xsrcMask
, ysrcMask
);
654 SetUserScale(xscaleOld
, yscaleOld
);
659 void wxDCImpl
::DrawLines(const wxPointList
*list
, wxCoord xoffset
, wxCoord yoffset
)
661 int n
= list
->GetCount();
662 wxPoint
*points
= new wxPoint
[n
];
665 for ( wxPointList
::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
667 wxPoint
*point
= node
->GetData();
668 points
[i
].x
= point
->x
;
669 points
[i
].y
= point
->y
;
672 DoDrawLines(n
, points
, xoffset
, yoffset
);
677 void wxDCImpl
::DrawPolygon(const wxPointList
*list
,
678 wxCoord xoffset
, wxCoord yoffset
,
681 int n
= list
->GetCount();
682 wxPoint
*points
= new wxPoint
[n
];
685 for ( wxPointList
::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
687 wxPoint
*point
= node
->GetData();
688 points
[i
].x
= point
->x
;
689 points
[i
].y
= point
->y
;
692 DoDrawPolygon(n
, points
, xoffset
, yoffset
, fillStyle
);
698 wxDCImpl
::DoDrawPolyPolygon(int n
,
701 wxCoord xoffset
, wxCoord yoffset
,
706 DoDrawPolygon(count
[0], points
, xoffset
, yoffset
, fillStyle
);
714 for (i
= j
= lastOfs
= 0; i
< n
; i
++)
719 pts
= new wxPoint
[j
+n
-1];
720 for (i
= 0; i
< j
; i
++)
722 for (i
= 2; i
<= n
; i
++)
724 lastOfs
-= count
[n
-i
];
725 pts
[j
++] = pts
[lastOfs
];
729 SetPen(wxPen(*wxBLACK
, 0, wxTRANSPARENT
));
730 DoDrawPolygon(j
, pts
, xoffset
, yoffset
, fillStyle
);
732 for (i
= j
= 0; i
< n
; i
++)
734 DoDrawLines(count
[i
], pts
+j
, xoffset
, yoffset
);
742 void wxDCImpl
::DoDrawSpline(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
, wxCoord x3
, wxCoord y3
)
744 wxPointList point_list
;
746 wxPoint
*point1
= new wxPoint
;
747 point1
->x
= x1
; point1
->y
= y1
;
748 point_list
.Append( point1
);
750 wxPoint
*point2
= new wxPoint
;
751 point2
->x
= x2
; point2
->y
= y2
;
752 point_list
.Append( point2
);
754 wxPoint
*point3
= new wxPoint
;
755 point3
->x
= x3
; point3
->y
= y3
;
756 point_list
.Append( point3
);
758 DoDrawSpline(&point_list
);
760 for( wxPointList
::compatibility_iterator node
= point_list
.GetFirst(); node
; node
= node
->GetNext() )
762 wxPoint
*p
= node
->GetData();
767 void wxDCImpl
::DoDrawSpline(int n
, wxPoint points
[])
770 for (int i
=0; i
< n
; i
++)
771 list
.Append( &points
[i
] );
776 // ----------------------------------- spline code ----------------------------------------
778 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
,
779 double a3
, double b3
, double a4
, double b4
);
780 void wx_clear_stack();
781 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
, double *x3
,
782 double *y3
, double *x4
, double *y4
);
783 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
,
784 double x4
, double y4
);
785 static bool wx_spline_add_point(double x
, double y
);
786 static void wx_spline_draw_point_array(wxDC
*dc
);
788 wxPointList wx_spline_point_list
;
790 #define half(z1, z2) ((z1+z2)/2.0)
793 /* iterative version */
795 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
, double a3
, double b3
, double a4
,
798 register double xmid
, ymid
;
799 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
802 wx_spline_push(a1
, b1
, a2
, b2
, a3
, b3
, a4
, b4
);
804 while (wx_spline_pop(&x1
, &y1
, &x2
, &y2
, &x3
, &y3
, &x4
, &y4
)) {
805 xmid
= (double)half(x2
, x3
);
806 ymid
= (double)half(y2
, y3
);
807 if (fabs(x1
- xmid
) < THRESHOLD
&& fabs(y1
- ymid
) < THRESHOLD
&&
808 fabs(xmid
- x4
) < THRESHOLD
&& fabs(ymid
- y4
) < THRESHOLD
) {
809 wx_spline_add_point( x1
, y1
);
810 wx_spline_add_point( xmid
, ymid
);
812 wx_spline_push(xmid
, ymid
, (double)half(xmid
, x3
), (double)half(ymid
, y3
),
813 (double)half(x3
, x4
), (double)half(y3
, y4
), x4
, y4
);
814 wx_spline_push(x1
, y1
, (double)half(x1
, x2
), (double)half(y1
, y2
),
815 (double)half(x2
, xmid
), (double)half(y2
, ymid
), xmid
, ymid
);
820 /* utilities used by spline drawing routines */
822 typedef struct wx_spline_stack_struct
{
823 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
826 #define SPLINE_STACK_DEPTH 20
827 static Stack wx_spline_stack
[SPLINE_STACK_DEPTH
];
828 static Stack
*wx_stack_top
;
829 static int wx_stack_count
;
831 void wx_clear_stack()
833 wx_stack_top
= wx_spline_stack
;
837 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
, double x4
, double y4
)
839 wx_stack_top
->x1
= x1
;
840 wx_stack_top
->y1
= y1
;
841 wx_stack_top
->x2
= x2
;
842 wx_stack_top
->y2
= y2
;
843 wx_stack_top
->x3
= x3
;
844 wx_stack_top
->y3
= y3
;
845 wx_stack_top
->x4
= x4
;
846 wx_stack_top
->y4
= y4
;
851 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
,
852 double *x3
, double *y3
, double *x4
, double *y4
)
854 if (wx_stack_count
== 0)
858 *x1
= wx_stack_top
->x1
;
859 *y1
= wx_stack_top
->y1
;
860 *x2
= wx_stack_top
->x2
;
861 *y2
= wx_stack_top
->y2
;
862 *x3
= wx_stack_top
->x3
;
863 *y3
= wx_stack_top
->y3
;
864 *x4
= wx_stack_top
->x4
;
865 *y4
= wx_stack_top
->y4
;
869 static bool wx_spline_add_point(double x
, double y
)
871 wxPoint
*point
= new wxPoint( wxRound(x
), wxRound(y
) );
872 wx_spline_point_list
.Append(point
);
876 static void wx_spline_draw_point_array(wxDC
*dc
)
878 dc
->DrawLines(&wx_spline_point_list
, 0, 0 );
879 wxPointList
::compatibility_iterator node
= wx_spline_point_list
.GetFirst();
882 wxPoint
*point
= node
->GetData();
884 wx_spline_point_list
.Erase(node
);
885 node
= wx_spline_point_list
.GetFirst();
889 void wxDCImpl
::DoDrawSpline( const wxPointList
*points
)
891 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
894 double cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
;
895 double x1
, y1
, x2
, y2
;
897 wxPointList
::compatibility_iterator node
= points
->GetFirst();
902 p
= (wxPoint
*)node
->GetData();
907 node
= node
->GetNext();
912 cx1
= (double)((x1
+ x2
) / 2);
913 cy1
= (double)((y1
+ y2
) / 2);
914 cx2
= (double)((cx1
+ x2
) / 2);
915 cy2
= (double)((cy1
+ y2
) / 2);
917 wx_spline_add_point(x1
, y1
);
919 while ((node
= node
->GetNext())
930 cx4
= (double)(x1
+ x2
) / 2;
931 cy4
= (double)(y1
+ y2
) / 2;
932 cx3
= (double)(x1
+ cx4
) / 2;
933 cy3
= (double)(y1
+ cy4
) / 2;
935 wx_quadratic_spline(cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
);
939 cx2
= (double)(cx1
+ x2
) / 2;
940 cy2
= (double)(cy1
+ y2
) / 2;
943 wx_spline_add_point( cx1
, cy1
);
944 wx_spline_add_point( x2
, y2
);
946 wx_spline_draw_point_array( m_owner
);
949 #endif // wxUSE_SPLINES
953 void wxDCImpl
::DoGradientFillLinear(const wxRect
& rect
,
954 const wxColour
& initialColour
,
955 const wxColour
& destColour
,
956 wxDirection nDirection
)
959 wxPen oldPen
= m_pen
;
960 wxBrush oldBrush
= m_brush
;
962 wxUint8 nR1
= initialColour
.Red();
963 wxUint8 nG1
= initialColour
.Green();
964 wxUint8 nB1
= initialColour
.Blue();
965 wxUint8 nR2
= destColour
.Red();
966 wxUint8 nG2
= destColour
.Green();
967 wxUint8 nB2
= destColour
.Blue();
970 if ( nDirection
== wxEAST
|| nDirection
== wxWEST
)
972 wxInt32 x
= rect
.GetWidth();
973 wxInt32 w
= x
; // width of area to shade
974 wxInt32 xDelta
= w
/256; // height of one shade bend
982 nR
= nR1
- (nR1
-nR2
)*(w
-x
)/w
;
984 nR
= nR1
+ (nR2
-nR1
)*(w
-x
)/w
;
987 nG
= nG1
- (nG1
-nG2
)*(w
-x
)/w
;
989 nG
= nG1
+ (nG2
-nG1
)*(w
-x
)/w
;
992 nB
= nB1
- (nB1
-nB2
)*(w
-x
)/w
;
994 nB
= nB1
+ (nB2
-nB1
)*(w
-x
)/w
;
996 wxColour
colour(nR
,nG
,nB
);
997 SetPen(wxPen(colour
, 1, wxSOLID
));
998 SetBrush(wxBrush(colour
));
999 if(nDirection
== wxEAST
)
1000 DoDrawRectangle(rect
.GetRight()-x
-xDelta
+1, rect
.GetTop(),
1001 xDelta
, rect
.GetHeight());
1002 else //nDirection == wxWEST
1003 DoDrawRectangle(rect
.GetLeft()+x
, rect
.GetTop(),
1004 xDelta
, rect
.GetHeight());
1007 else // nDirection == wxNORTH || nDirection == wxSOUTH
1009 wxInt32 y
= rect
.GetHeight();
1010 wxInt32 w
= y
; // height of area to shade
1011 wxInt32 yDelta
= w
/255; // height of one shade bend
1019 nR
= nR1
- (nR1
-nR2
)*(w
-y
)/w
;
1021 nR
= nR1
+ (nR2
-nR1
)*(w
-y
)/w
;
1024 nG
= nG1
- (nG1
-nG2
)*(w
-y
)/w
;
1026 nG
= nG1
+ (nG2
-nG1
)*(w
-y
)/w
;
1029 nB
= nB1
- (nB1
-nB2
)*(w
-y
)/w
;
1031 nB
= nB1
+ (nB2
-nB1
)*(w
-y
)/w
;
1033 wxColour
colour(nR
,nG
,nB
);
1034 SetPen(wxPen(colour
, 1, wxSOLID
));
1035 SetBrush(wxBrush(colour
));
1036 if(nDirection
== wxNORTH
)
1037 DoDrawRectangle(rect
.GetLeft(), rect
.GetTop()+y
,
1038 rect
.GetWidth(), yDelta
);
1039 else //nDirection == wxSOUTH
1040 DoDrawRectangle(rect
.GetLeft(), rect
.GetBottom()-y
-yDelta
+1,
1041 rect
.GetWidth(), yDelta
);
1049 void wxDCImpl
::DoGradientFillConcentric(const wxRect
& rect
,
1050 const wxColour
& initialColour
,
1051 const wxColour
& destColour
,
1052 const wxPoint
& circleCenter
)
1054 //save the old pen color
1055 wxColour oldPenColour
= m_pen
.GetColour();
1057 wxUint8 nR1
= destColour
.Red();
1058 wxUint8 nG1
= destColour
.Green();
1059 wxUint8 nB1
= destColour
.Blue();
1060 wxUint8 nR2
= initialColour
.Red();
1061 wxUint8 nG2
= initialColour
.Green();
1062 wxUint8 nB2
= initialColour
.Blue();
1067 wxInt32 cx
= rect
.GetWidth() / 2;
1068 wxInt32 cy
= rect
.GetHeight() / 2;
1076 wxInt32 nCircleOffX
= circleCenter
.x
- (rect
.GetWidth() / 2);
1077 wxInt32 nCircleOffY
= circleCenter
.y
- (rect
.GetHeight() / 2);
1079 for ( wxInt32 x
= 0; x
< rect
.GetWidth(); x
++ )
1081 for ( wxInt32 y
= 0; y
< rect
.GetHeight(); y
++ )
1083 //get color difference
1084 wxInt32 nGradient
= ((nRadius
-
1086 pow((double)(x
- cx
- nCircleOffX
), 2) +
1087 pow((double)(y
- cy
- nCircleOffY
), 2)
1088 )) * 100) / nRadius
;
1090 //normalize Gradient
1095 nR
= (wxUint8
)(nR1
+ ((nR2
- nR1
) * nGradient
/ 100));
1096 nG
= (wxUint8
)(nG1
+ ((nG2
- nG1
) * nGradient
/ 100));
1097 nB
= (wxUint8
)(nB1
+ ((nB2
- nB1
) * nGradient
/ 100));
1100 m_pen
.SetColour(wxColour(nR
,nG
,nB
));
1101 DoDrawPoint(x
+ rect
.GetLeft(), y
+ rect
.GetTop());
1104 //return old pen color
1105 m_pen
.SetColour(oldPenColour
);
1108 //-----------------------------------------------------------------------------
1110 //-----------------------------------------------------------------------------
1112 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxObject
)
1114 void wxDC
::DrawLabel(const wxString
& text
,
1115 const wxBitmap
& bitmap
,
1119 wxRect
*rectBounding
)
1121 // find the text position
1122 wxCoord widthText
, heightText
, heightLine
;
1123 GetMultiLineTextExtent(text
, &widthText
, &heightText
, &heightLine
);
1125 wxCoord width
, height
;
1128 width
= widthText
+ bitmap
.GetWidth();
1129 height
= bitmap
.GetHeight();
1134 height
= heightText
;
1138 if ( alignment
& wxALIGN_RIGHT
)
1140 x
= rect
.GetRight() - width
;
1142 else if ( alignment
& wxALIGN_CENTRE_HORIZONTAL
)
1144 x
= (rect
.GetLeft() + rect
.GetRight() + 1 - width
) / 2;
1146 else // alignment & wxALIGN_LEFT
1151 if ( alignment
& wxALIGN_BOTTOM
)
1153 y
= rect
.GetBottom() - height
;
1155 else if ( alignment
& wxALIGN_CENTRE_VERTICAL
)
1157 y
= (rect
.GetTop() + rect
.GetBottom() + 1 - height
) / 2;
1159 else // alignment & wxALIGN_TOP
1164 // draw the bitmap first
1170 DrawBitmap(bitmap
, x
, y
, true /* use mask */);
1172 wxCoord offset
= bitmap
.GetWidth() + 4;
1176 y
+= (height
- heightText
) / 2;
1179 // we will draw the underscore under the accel char later
1180 wxCoord startUnderscore
= 0,
1184 // split the string into lines and draw each of them separately
1186 for ( wxString
::const_iterator pc
= text
.begin(); ; ++pc
)
1188 if ( *pc
== _T('\n') || pc
== text
.end() )
1190 int xRealStart
= x
; // init it here to avoid compielr warnings
1192 if ( !curLine
.empty() )
1194 // NB: can't test for !(alignment & wxALIGN_LEFT) because
1195 // wxALIGN_LEFT is 0
1196 if ( alignment
& (wxALIGN_RIGHT
| wxALIGN_CENTRE_HORIZONTAL
) )
1199 GetTextExtent(curLine
, &widthLine
, NULL
);
1201 if ( alignment
& wxALIGN_RIGHT
)
1203 xRealStart
+= width
- widthLine
;
1205 else // if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
1207 xRealStart
+= (width
- widthLine
) / 2;
1210 //else: left aligned, nothing to do
1212 DrawText(curLine
, xRealStart
, y
);
1217 // do we have underscore in this line? we can check yUnderscore
1218 // because it is set below to just y + heightLine if we do
1219 if ( y
== yUnderscore
)
1221 // adjust the horz positions to account for the shift
1222 startUnderscore
+= xRealStart
;
1223 endUnderscore
+= xRealStart
;
1226 if ( pc
== text
.end() )
1231 else // not end of line
1233 if ( pc
- text
.begin() == indexAccel
)
1235 // remeber to draw underscore here
1236 GetTextExtent(curLine
, &startUnderscore
, NULL
);
1238 GetTextExtent(curLine
, &endUnderscore
, NULL
);
1240 yUnderscore
= y
+ heightLine
;
1249 // draw the underscore if found
1250 if ( startUnderscore
!= endUnderscore
)
1252 // it should be of the same colour as text
1253 SetPen(wxPen(GetTextForeground(), 0, wxSOLID
));
1257 DrawLine(startUnderscore
, yUnderscore
, endUnderscore
, yUnderscore
);
1260 // return bounding rect if requested
1263 *rectBounding
= wxRect(x
, y
- heightText
, widthText
, heightText
);
1266 CalcBoundingBox(x0
, y0
);
1267 CalcBoundingBox(x0
+ width0
, y0
+ height
);
1270 #if WXWIN_COMPATIBILITY_2_8
1271 // for compatibility with the old code when wxCoord was long everywhere
1272 void wxDC
::GetTextExtent(const wxString
& string
,
1275 long *externalLeading
,
1276 const wxFont
*theFont
) const
1278 wxCoord x2
, y2
, descent2
, externalLeading2
;
1279 m_pimpl
->DoGetTextExtent(string
, &x2
, &y2
,
1280 &descent2
, &externalLeading2
,
1287 *descent
= descent2
;
1288 if ( externalLeading
)
1289 *externalLeading
= externalLeading2
;
1292 void wxDC
::GetLogicalOrigin(long *x
, long *y
) const
1295 m_pimpl
->DoGetLogicalOrigin(&x2
, &y2
);
1302 void wxDC
::GetDeviceOrigin(long *x
, long *y
) const
1305 m_pimpl
->DoGetDeviceOrigin(&x2
, &y2
);
1312 void wxDC
::GetClippingBox(long *x
, long *y
, long *w
, long *h
) const
1314 wxCoord xx
,yy
,ww
,hh
;
1315 m_pimpl
->DoGetClippingBox(&xx
, &yy
, &ww
, &hh
);
1322 #endif // WXWIN_COMPATIBILITY_2_8