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/mac/dcclient.h"
57 #include "wx/mac/dcmemory.h"
58 #include "wx/mac/dcscreen.h"
62 #include "wx/cocoa/dcclient.h"
63 #include "wx/cocoa/dcmemory.h"
64 #include "wx/cocoa/dcscreen.h"
68 #include "wx/motif/dcclient.h"
69 #include "wx/motif/dcmemory.h"
70 #include "wx/motif/dcscreen.h"
74 #include "wx/x11/dcclient.h"
75 #include "wx/x11/dcmemory.h"
76 #include "wx/x11/dcscreen.h"
80 #include "wx/dfb/dcclient.h"
81 #include "wx/dfb/dcmemory.h"
82 #include "wx/dfb/dcscreen.h"
86 #include "wx/palmos/dcclient.h"
87 #include "wx/palmos/dcmemory.h"
88 #include "wx/palmos/dcscreen.h"
91 //----------------------------------------------------------------------------
93 //----------------------------------------------------------------------------
95 wxDCFactory
*wxDCFactory::m_factory
= NULL
;
97 void wxDCFactory::Set(wxDCFactory
*factory
)
104 wxDCFactory
*wxDCFactory::Get()
107 m_factory
= new wxNativeDCFactory
;
112 class wxDCFactoryCleanupModule
: public wxModule
115 virtual bool OnInit() { return true; }
116 virtual void OnExit() { wxDCFactory::Set(NULL
); }
119 DECLARE_DYNAMIC_CLASS(wxDCFactoryCleanupModule
)
122 IMPLEMENT_DYNAMIC_CLASS(wxDCFactoryCleanupModule
, wxModule
)
124 //-----------------------------------------------------------------------------
126 //-----------------------------------------------------------------------------
128 wxDCImpl
* wxNativeDCFactory::CreateWindowDC( wxWindowDC
*owner
)
130 return new wxWindowDCImpl( owner
);
133 wxDCImpl
* wxNativeDCFactory::CreateWindowDC( wxWindowDC
*owner
, wxWindow
*window
)
135 return new wxWindowDCImpl( owner
, window
);
138 wxDCImpl
* wxNativeDCFactory::CreateClientDC( wxClientDC
*owner
)
140 return new wxClientDCImpl( owner
);
143 wxDCImpl
* wxNativeDCFactory::CreateClientDC( wxClientDC
*owner
, wxWindow
*window
)
145 return new wxClientDCImpl( owner
, window
);
148 wxDCImpl
* wxNativeDCFactory::CreatePaintDC( wxPaintDC
*owner
)
150 return new wxPaintDCImpl( owner
);
153 wxDCImpl
* wxNativeDCFactory::CreatePaintDC( wxPaintDC
*owner
, wxWindow
*window
)
155 return new wxPaintDCImpl( owner
, window
);
158 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
)
160 return new wxMemoryDCImpl( owner
);
163 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
, wxBitmap
&bitmap
)
165 return new wxMemoryDCImpl( owner
, bitmap
);
168 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
, wxDC
*dc
)
170 return new wxMemoryDCImpl( owner
, dc
);
173 wxDCImpl
* wxNativeDCFactory::CreateScreenDC( wxScreenDC
*owner
)
175 return new wxScreenDCImpl( owner
);
178 #if wxUSE_PRINTING_ARCHITECTURE
179 wxDCImpl
*wxNativeDCFactory::CreatePrinterDC( wxPrinterDC
*owner
, const wxPrintData
&data
)
181 wxPrintFactory
*factory
= wxPrintFactory::GetFactory();
182 return factory
->CreatePrinterDCImpl( owner
, data
);
186 //-----------------------------------------------------------------------------
188 //-----------------------------------------------------------------------------
190 IMPLEMENT_ABSTRACT_CLASS(wxWindowDC
, wxDC
)
192 wxWindowDC::wxWindowDC(wxWindow
*win
)
193 : wxDC(wxDCFactory::Get()->CreateWindowDC(this, win
))
197 //-----------------------------------------------------------------------------
199 //-----------------------------------------------------------------------------
201 IMPLEMENT_ABSTRACT_CLASS(wxClientDC
, wxWindowDC
)
203 wxClientDC::wxClientDC(wxWindow
*win
)
204 : wxWindowDC(wxDCFactory::Get()->CreateClientDC(this, win
))
208 //-----------------------------------------------------------------------------
210 //-----------------------------------------------------------------------------
212 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC
, wxDC
)
214 wxMemoryDC::wxMemoryDC()
215 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this))
219 wxMemoryDC::wxMemoryDC(wxBitmap
& bitmap
)
220 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, bitmap
))
224 wxMemoryDC::wxMemoryDC(wxDC
*dc
)
225 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, dc
))
229 void wxMemoryDC::SelectObject(wxBitmap
& bmp
)
231 // make sure that the given wxBitmap is not sharing its data with other
232 // wxBitmap instances as its contents will be modified by any drawing
233 // operation done on this DC
237 GetImpl()->DoSelect(bmp
);
240 void wxMemoryDC::SelectObjectAsSource(const wxBitmap
& bmp
)
242 GetImpl()->DoSelect(bmp
);
245 const wxBitmap
& wxMemoryDC::GetSelectedBitmap() const
247 return GetImpl()->GetSelectedBitmap();
250 wxBitmap
& wxMemoryDC::GetSelectedBitmap()
252 return GetImpl()->GetSelectedBitmap();
256 //-----------------------------------------------------------------------------
258 //-----------------------------------------------------------------------------
260 IMPLEMENT_ABSTRACT_CLASS(wxPaintDC
, wxClientDC
)
262 wxPaintDC::wxPaintDC(wxWindow
*win
)
263 : wxClientDC(wxDCFactory::Get()->CreatePaintDC(this, win
))
267 //-----------------------------------------------------------------------------
269 //-----------------------------------------------------------------------------
271 IMPLEMENT_DYNAMIC_CLASS(wxScreenDC
, wxWindowDC
)
273 wxScreenDC::wxScreenDC()
274 : wxDC(wxDCFactory::Get()->CreateScreenDC(this))
278 //-----------------------------------------------------------------------------
280 //-----------------------------------------------------------------------------
282 #if wxUSE_PRINTING_ARCHITECTURE
284 IMPLEMENT_DYNAMIC_CLASS(wxPrinterDC
, wxDC
)
286 wxPrinterDC::wxPrinterDC()
287 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, wxPrintData()))
291 wxPrinterDC::wxPrinterDC(const wxPrintData
& data
)
292 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, data
))
296 wxRect
wxPrinterDC::GetPaperRect()
298 return GetImpl()->GetPaperRect();
301 int wxPrinterDC::GetResolution()
303 return GetImpl()->GetResolution();
306 #endif // wxUSE_PRINTING_ARCHITECTURE
308 //-----------------------------------------------------------------------------
310 //-----------------------------------------------------------------------------
312 IMPLEMENT_ABSTRACT_CLASS(wxDCImpl
, wxObject
)
314 wxDCImpl::wxDCImpl( wxDC
*owner
)
316 , m_colour(wxColourDisplay())
320 , m_isBBoxValid(false)
321 , m_logicalOriginX(0), m_logicalOriginY(0)
322 , m_deviceOriginX(0), m_deviceOriginY(0)
323 , m_deviceLocalOriginX(0), m_deviceLocalOriginY(0)
324 , m_logicalScaleX(1.0), m_logicalScaleY(1.0)
325 , m_userScaleX(1.0), m_userScaleY(1.0)
326 , m_scaleX(1.0), m_scaleY(1.0)
327 , m_signX(1), m_signY(1)
328 , m_minX(0), m_minY(0), m_maxX(0), m_maxY(0)
329 , m_clipX1(0), m_clipY1(0), m_clipX2(0), m_clipY2(0)
330 , m_logicalFunction(wxCOPY
)
331 , m_backgroundMode(wxTRANSPARENT
)
332 , m_mappingMode(wxMM_TEXT
)
335 , m_backgroundBrush(*wxTRANSPARENT_BRUSH
)
336 , m_textForegroundColour(*wxBLACK
)
337 , m_textBackgroundColour(*wxWHITE
)
341 , m_hasCustomPalette(false)
342 #endif // wxUSE_PALETTE
346 m_mm_to_pix_x
= (double)wxGetDisplaySize().GetWidth() /
347 (double)wxGetDisplaySizeMM().GetWidth();
348 m_mm_to_pix_y
= (double)wxGetDisplaySize().GetHeight() /
349 (double)wxGetDisplaySizeMM().GetHeight();
355 wxDCImpl::~wxDCImpl()
359 // ----------------------------------------------------------------------------
360 // coordinate conversions and transforms
361 // ----------------------------------------------------------------------------
363 wxCoord
wxDCImpl::DeviceToLogicalX(wxCoord x
) const
365 return wxRound((double)(x
- m_deviceOriginX
- m_deviceLocalOriginX
) / m_scaleX
) * m_signX
+ m_logicalOriginX
;
368 wxCoord
wxDCImpl::DeviceToLogicalY(wxCoord y
) const
370 return wxRound((double)(y
- m_deviceOriginY
- m_deviceLocalOriginY
) / m_scaleY
) * m_signY
+ m_logicalOriginY
;
373 wxCoord
wxDCImpl::DeviceToLogicalXRel(wxCoord x
) const
375 return wxRound((double)(x
) / m_scaleX
);
378 wxCoord
wxDCImpl::DeviceToLogicalYRel(wxCoord y
) const
380 return wxRound((double)(y
) / m_scaleY
);
383 wxCoord
wxDCImpl::LogicalToDeviceX(wxCoord x
) const
385 return wxRound((double)(x
- m_logicalOriginX
) * m_scaleX
) * m_signX
+ m_deviceOriginX
* m_signY
+ m_deviceLocalOriginX
;
388 wxCoord
wxDCImpl::LogicalToDeviceY(wxCoord y
) const
390 return wxRound((double)(y
- m_logicalOriginY
) * m_scaleY
) * m_signY
+ m_deviceOriginY
* m_signY
+ m_deviceLocalOriginY
;
393 wxCoord
wxDCImpl::LogicalToDeviceXRel(wxCoord x
) const
395 return wxRound((double)(x
) * m_scaleX
);
398 wxCoord
wxDCImpl::LogicalToDeviceYRel(wxCoord y
) const
400 return wxRound((double)(y
) * m_scaleY
);
403 void wxDCImpl::ComputeScaleAndOrigin()
405 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
406 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
409 void wxDCImpl::SetMapMode( int mode
)
414 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
417 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
420 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
423 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
427 SetLogicalScale( 1.0, 1.0 );
430 m_mappingMode
= mode
;
433 void wxDCImpl::SetUserScale( double x
, double y
)
435 // allow negative ? -> no
438 ComputeScaleAndOrigin();
441 void wxDCImpl::SetLogicalScale( double x
, double y
)
446 ComputeScaleAndOrigin();
449 void wxDCImpl::SetLogicalOrigin( wxCoord x
, wxCoord y
)
451 m_logicalOriginX
= x
* m_signX
;
452 m_logicalOriginY
= y
* m_signY
;
453 ComputeScaleAndOrigin();
456 void wxDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
460 ComputeScaleAndOrigin();
463 void wxDCImpl::SetDeviceLocalOrigin( wxCoord x
, wxCoord y
)
465 m_deviceLocalOriginX
= x
;
466 m_deviceLocalOriginY
= y
;
467 ComputeScaleAndOrigin();
470 void wxDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
472 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
473 // wxWidgets 2.9: no longer override it
474 m_signX
= (xLeftRight
? 1 : -1);
475 m_signY
= (yBottomUp
? -1 : 1);
476 ComputeScaleAndOrigin();
480 // Each element of the widths array will be the width of the string up to and
481 // including the corresponding character in text. This is the generic
482 // implementation, the port-specific classes should do this with native APIs
483 // if available and if faster. Note: pango_layout_index_to_pos is much slower
484 // than calling GetTextExtent!!
491 FontWidthCache() : m_scaleX(1), m_widths(NULL
) { }
492 ~FontWidthCache() { delete []m_widths
; }
497 m_widths
= new int[FWC_SIZE
];
499 memset(m_widths
, 0, sizeof(int)*FWC_SIZE
);
507 static FontWidthCache s_fontWidthCache
;
509 bool wxDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
513 const size_t len
= text
.length();
517 // reset the cache if font or horizontal scale have changed
518 if ( !s_fontWidthCache
.m_widths
||
519 !wxIsSameDouble(s_fontWidthCache
.m_scaleX
, m_scaleX
) ||
520 (s_fontWidthCache
.m_font
!= GetFont()) )
522 s_fontWidthCache
.Reset();
523 s_fontWidthCache
.m_font
= GetFont();
524 s_fontWidthCache
.m_scaleX
= m_scaleX
;
527 // Calculate the position of each character based on the widths of
528 // the previous characters
530 for ( size_t i
= 0; i
< len
; i
++ )
532 const wxChar c
= text
[i
];
533 unsigned int c_int
= (unsigned int)c
;
535 if ((c_int
< FWC_SIZE
) && (s_fontWidthCache
.m_widths
[c_int
] != 0))
537 w
= s_fontWidthCache
.m_widths
[c_int
];
541 DoGetTextExtent(c
, &w
, &h
);
542 if (c_int
< FWC_SIZE
)
543 s_fontWidthCache
.m_widths
[c_int
] = w
;
547 widths
[i
] = totalWidth
;
553 void wxDCImpl::GetMultiLineTextExtent(const wxString
& text
,
557 const wxFont
*font
) const
559 wxCoord widthTextMax
= 0, widthLine
,
560 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
563 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
565 if ( pc
== text
.end() || *pc
== _T('\n') )
567 if ( curLine
.empty() )
569 // we can't use GetTextExtent - it will return 0 for both width
570 // and height and an empty line should count in height
573 // assume that this line has the same height as the previous
575 if ( !heightLineDefault
)
576 heightLineDefault
= heightLine
;
578 if ( !heightLineDefault
)
580 // but we don't know it yet - choose something reasonable
581 DoGetTextExtent(_T("W"), NULL
, &heightLineDefault
,
585 heightTextTotal
+= heightLineDefault
;
589 DoGetTextExtent(curLine
, &widthLine
, &heightLine
,
591 if ( widthLine
> widthTextMax
)
592 widthTextMax
= widthLine
;
593 heightTextTotal
+= heightLine
;
596 if ( pc
== text
.end() )
614 *y
= heightTextTotal
;
619 void wxDCImpl::DoDrawCheckMark(wxCoord x1
, wxCoord y1
,
620 wxCoord width
, wxCoord height
)
622 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
624 wxCoord x2
= x1
+ width
,
627 // the pen width is calibrated to give 3 for width == height == 10
628 wxDCPenChanger
pen( *m_owner
, wxPen(GetTextForeground(), (width
+ height
+ 1)/7));
630 // we're drawing a scaled version of wx/generic/tick.xpm here
631 wxCoord x3
= x1
+ (4*width
) / 10, // x of the tick bottom
632 y3
= y1
+ height
/ 2; // y of the left tick branch
633 DoDrawLine(x1
, y3
, x3
, y2
);
634 DoDrawLine(x3
, y2
, x2
, y1
);
636 CalcBoundingBox(x1
, y1
);
637 CalcBoundingBox(x2
, y2
);
641 wxDCImpl::DoStretchBlit(wxCoord xdest
, wxCoord ydest
,
642 wxCoord dstWidth
, wxCoord dstHeight
,
644 wxCoord xsrc
, wxCoord ysrc
,
645 wxCoord srcWidth
, wxCoord srcHeight
,
651 wxCHECK_MSG( srcWidth
&& srcHeight
&& dstWidth
&& dstHeight
, false,
652 _T("invalid blit size") );
654 // emulate the stretching by modifying the DC scale
655 double xscale
= (double)srcWidth
/dstWidth
,
656 yscale
= (double)srcHeight
/dstHeight
;
658 double xscaleOld
, yscaleOld
;
659 GetUserScale(&xscaleOld
, &yscaleOld
);
660 SetUserScale(xscaleOld
/xscale
, yscaleOld
/yscale
);
662 bool rc
= DoBlit(wxCoord(xdest
*xscale
), wxCoord(ydest
*yscale
),
663 wxCoord(dstWidth
*xscale
), wxCoord(dstHeight
*yscale
),
665 xsrc
, ysrc
, rop
, useMask
, xsrcMask
, ysrcMask
);
667 SetUserScale(xscaleOld
, yscaleOld
);
672 void wxDCImpl::DrawLines(const wxPointList
*list
, wxCoord xoffset
, wxCoord yoffset
)
674 int n
= list
->GetCount();
675 wxPoint
*points
= new wxPoint
[n
];
678 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
680 wxPoint
*point
= node
->GetData();
681 points
[i
].x
= point
->x
;
682 points
[i
].y
= point
->y
;
685 DoDrawLines(n
, points
, xoffset
, yoffset
);
690 void wxDCImpl::DrawPolygon(const wxPointList
*list
,
691 wxCoord xoffset
, wxCoord yoffset
,
694 int n
= list
->GetCount();
695 wxPoint
*points
= new wxPoint
[n
];
698 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
700 wxPoint
*point
= node
->GetData();
701 points
[i
].x
= point
->x
;
702 points
[i
].y
= point
->y
;
705 DoDrawPolygon(n
, points
, xoffset
, yoffset
, fillStyle
);
711 wxDCImpl::DoDrawPolyPolygon(int n
,
714 wxCoord xoffset
, wxCoord yoffset
,
719 DoDrawPolygon(count
[0], points
, xoffset
, yoffset
, fillStyle
);
727 for (i
= j
= lastOfs
= 0; i
< n
; i
++)
732 pts
= new wxPoint
[j
+n
-1];
733 for (i
= 0; i
< j
; i
++)
735 for (i
= 2; i
<= n
; i
++)
737 lastOfs
-= count
[n
-i
];
738 pts
[j
++] = pts
[lastOfs
];
742 SetPen(wxPen(*wxBLACK
, 0, wxTRANSPARENT
));
743 DoDrawPolygon(j
, pts
, xoffset
, yoffset
, fillStyle
);
745 for (i
= j
= 0; i
< n
; i
++)
747 DoDrawLines(count
[i
], pts
+j
, xoffset
, yoffset
);
755 void wxDCImpl::DoDrawSpline(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
, wxCoord x3
, wxCoord y3
)
757 wxPointList point_list
;
759 wxPoint
*point1
= new wxPoint
;
760 point1
->x
= x1
; point1
->y
= y1
;
761 point_list
.Append( point1
);
763 wxPoint
*point2
= new wxPoint
;
764 point2
->x
= x2
; point2
->y
= y2
;
765 point_list
.Append( point2
);
767 wxPoint
*point3
= new wxPoint
;
768 point3
->x
= x3
; point3
->y
= y3
;
769 point_list
.Append( point3
);
771 DoDrawSpline(&point_list
);
773 for( wxPointList::compatibility_iterator node
= point_list
.GetFirst(); node
; node
= node
->GetNext() )
775 wxPoint
*p
= node
->GetData();
780 void wxDCImpl::DoDrawSpline(int n
, wxPoint points
[])
783 for (int i
=0; i
< n
; i
++)
784 list
.Append( &points
[i
] );
789 // ----------------------------------- spline code ----------------------------------------
791 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
,
792 double a3
, double b3
, double a4
, double b4
);
793 void wx_clear_stack();
794 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
, double *x3
,
795 double *y3
, double *x4
, double *y4
);
796 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
,
797 double x4
, double y4
);
798 static bool wx_spline_add_point(double x
, double y
);
799 static void wx_spline_draw_point_array(wxDC
*dc
);
801 wxPointList wx_spline_point_list
;
803 #define half(z1, z2) ((z1+z2)/2.0)
806 /* iterative version */
808 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
, double a3
, double b3
, double a4
,
811 register double xmid
, ymid
;
812 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
815 wx_spline_push(a1
, b1
, a2
, b2
, a3
, b3
, a4
, b4
);
817 while (wx_spline_pop(&x1
, &y1
, &x2
, &y2
, &x3
, &y3
, &x4
, &y4
)) {
818 xmid
= (double)half(x2
, x3
);
819 ymid
= (double)half(y2
, y3
);
820 if (fabs(x1
- xmid
) < THRESHOLD
&& fabs(y1
- ymid
) < THRESHOLD
&&
821 fabs(xmid
- x4
) < THRESHOLD
&& fabs(ymid
- y4
) < THRESHOLD
) {
822 wx_spline_add_point( x1
, y1
);
823 wx_spline_add_point( xmid
, ymid
);
825 wx_spline_push(xmid
, ymid
, (double)half(xmid
, x3
), (double)half(ymid
, y3
),
826 (double)half(x3
, x4
), (double)half(y3
, y4
), x4
, y4
);
827 wx_spline_push(x1
, y1
, (double)half(x1
, x2
), (double)half(y1
, y2
),
828 (double)half(x2
, xmid
), (double)half(y2
, ymid
), xmid
, ymid
);
833 /* utilities used by spline drawing routines */
835 typedef struct wx_spline_stack_struct
{
836 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
839 #define SPLINE_STACK_DEPTH 20
840 static Stack wx_spline_stack
[SPLINE_STACK_DEPTH
];
841 static Stack
*wx_stack_top
;
842 static int wx_stack_count
;
844 void wx_clear_stack()
846 wx_stack_top
= wx_spline_stack
;
850 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
, double x4
, double y4
)
852 wx_stack_top
->x1
= x1
;
853 wx_stack_top
->y1
= y1
;
854 wx_stack_top
->x2
= x2
;
855 wx_stack_top
->y2
= y2
;
856 wx_stack_top
->x3
= x3
;
857 wx_stack_top
->y3
= y3
;
858 wx_stack_top
->x4
= x4
;
859 wx_stack_top
->y4
= y4
;
864 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
,
865 double *x3
, double *y3
, double *x4
, double *y4
)
867 if (wx_stack_count
== 0)
871 *x1
= wx_stack_top
->x1
;
872 *y1
= wx_stack_top
->y1
;
873 *x2
= wx_stack_top
->x2
;
874 *y2
= wx_stack_top
->y2
;
875 *x3
= wx_stack_top
->x3
;
876 *y3
= wx_stack_top
->y3
;
877 *x4
= wx_stack_top
->x4
;
878 *y4
= wx_stack_top
->y4
;
882 static bool wx_spline_add_point(double x
, double y
)
884 wxPoint
*point
= new wxPoint( wxRound(x
), wxRound(y
) );
885 wx_spline_point_list
.Append(point
);
889 static void wx_spline_draw_point_array(wxDC
*dc
)
891 dc
->DrawLines(&wx_spline_point_list
, 0, 0 );
892 wxPointList::compatibility_iterator node
= wx_spline_point_list
.GetFirst();
895 wxPoint
*point
= node
->GetData();
897 wx_spline_point_list
.Erase(node
);
898 node
= wx_spline_point_list
.GetFirst();
902 void wxDCImpl::DoDrawSpline( const wxPointList
*points
)
904 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
907 double cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
;
908 double x1
, y1
, x2
, y2
;
910 wxPointList::compatibility_iterator node
= points
->GetFirst();
915 p
= (wxPoint
*)node
->GetData();
920 node
= node
->GetNext();
925 cx1
= (double)((x1
+ x2
) / 2);
926 cy1
= (double)((y1
+ y2
) / 2);
927 cx2
= (double)((cx1
+ x2
) / 2);
928 cy2
= (double)((cy1
+ y2
) / 2);
930 wx_spline_add_point(x1
, y1
);
932 while ((node
= node
->GetNext())
943 cx4
= (double)(x1
+ x2
) / 2;
944 cy4
= (double)(y1
+ y2
) / 2;
945 cx3
= (double)(x1
+ cx4
) / 2;
946 cy3
= (double)(y1
+ cy4
) / 2;
948 wx_quadratic_spline(cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
);
952 cx2
= (double)(cx1
+ x2
) / 2;
953 cy2
= (double)(cy1
+ y2
) / 2;
956 wx_spline_add_point( cx1
, cy1
);
957 wx_spline_add_point( x2
, y2
);
959 wx_spline_draw_point_array( m_owner
);
962 #endif // wxUSE_SPLINES
966 void wxDCImpl::DoGradientFillLinear(const wxRect
& rect
,
967 const wxColour
& initialColour
,
968 const wxColour
& destColour
,
969 wxDirection nDirection
)
972 wxPen oldPen
= m_pen
;
973 wxBrush oldBrush
= m_brush
;
975 wxUint8 nR1
= initialColour
.Red();
976 wxUint8 nG1
= initialColour
.Green();
977 wxUint8 nB1
= initialColour
.Blue();
978 wxUint8 nR2
= destColour
.Red();
979 wxUint8 nG2
= destColour
.Green();
980 wxUint8 nB2
= destColour
.Blue();
983 if ( nDirection
== wxEAST
|| nDirection
== wxWEST
)
985 wxInt32 x
= rect
.GetWidth();
986 wxInt32 w
= x
; // width of area to shade
987 wxInt32 xDelta
= w
/256; // height of one shade bend
995 nR
= nR1
- (nR1
-nR2
)*(w
-x
)/w
;
997 nR
= nR1
+ (nR2
-nR1
)*(w
-x
)/w
;
1000 nG
= nG1
- (nG1
-nG2
)*(w
-x
)/w
;
1002 nG
= nG1
+ (nG2
-nG1
)*(w
-x
)/w
;
1005 nB
= nB1
- (nB1
-nB2
)*(w
-x
)/w
;
1007 nB
= nB1
+ (nB2
-nB1
)*(w
-x
)/w
;
1009 wxColour
colour(nR
,nG
,nB
);
1010 SetPen(wxPen(colour
, 1, wxSOLID
));
1011 SetBrush(wxBrush(colour
));
1012 if(nDirection
== wxEAST
)
1013 DoDrawRectangle(rect
.GetRight()-x
-xDelta
+1, rect
.GetTop(),
1014 xDelta
, rect
.GetHeight());
1015 else //nDirection == wxWEST
1016 DoDrawRectangle(rect
.GetLeft()+x
, rect
.GetTop(),
1017 xDelta
, rect
.GetHeight());
1020 else // nDirection == wxNORTH || nDirection == wxSOUTH
1022 wxInt32 y
= rect
.GetHeight();
1023 wxInt32 w
= y
; // height of area to shade
1024 wxInt32 yDelta
= w
/255; // height of one shade bend
1032 nR
= nR1
- (nR1
-nR2
)*(w
-y
)/w
;
1034 nR
= nR1
+ (nR2
-nR1
)*(w
-y
)/w
;
1037 nG
= nG1
- (nG1
-nG2
)*(w
-y
)/w
;
1039 nG
= nG1
+ (nG2
-nG1
)*(w
-y
)/w
;
1042 nB
= nB1
- (nB1
-nB2
)*(w
-y
)/w
;
1044 nB
= nB1
+ (nB2
-nB1
)*(w
-y
)/w
;
1046 wxColour
colour(nR
,nG
,nB
);
1047 SetPen(wxPen(colour
, 1, wxSOLID
));
1048 SetBrush(wxBrush(colour
));
1049 if(nDirection
== wxNORTH
)
1050 DoDrawRectangle(rect
.GetLeft(), rect
.GetTop()+y
,
1051 rect
.GetWidth(), yDelta
);
1052 else //nDirection == wxSOUTH
1053 DoDrawRectangle(rect
.GetLeft(), rect
.GetBottom()-y
-yDelta
+1,
1054 rect
.GetWidth(), yDelta
);
1062 void wxDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1063 const wxColour
& initialColour
,
1064 const wxColour
& destColour
,
1065 const wxPoint
& circleCenter
)
1067 //save the old pen color
1068 wxColour oldPenColour
= m_pen
.GetColour();
1070 wxUint8 nR1
= destColour
.Red();
1071 wxUint8 nG1
= destColour
.Green();
1072 wxUint8 nB1
= destColour
.Blue();
1073 wxUint8 nR2
= initialColour
.Red();
1074 wxUint8 nG2
= initialColour
.Green();
1075 wxUint8 nB2
= initialColour
.Blue();
1080 wxInt32 cx
= rect
.GetWidth() / 2;
1081 wxInt32 cy
= rect
.GetHeight() / 2;
1089 wxInt32 nCircleOffX
= circleCenter
.x
- (rect
.GetWidth() / 2);
1090 wxInt32 nCircleOffY
= circleCenter
.y
- (rect
.GetHeight() / 2);
1092 for ( wxInt32 x
= 0; x
< rect
.GetWidth(); x
++ )
1094 for ( wxInt32 y
= 0; y
< rect
.GetHeight(); y
++ )
1096 //get color difference
1097 wxInt32 nGradient
= ((nRadius
-
1099 pow((double)(x
- cx
- nCircleOffX
), 2) +
1100 pow((double)(y
- cy
- nCircleOffY
), 2)
1101 )) * 100) / nRadius
;
1103 //normalize Gradient
1108 nR
= (wxUint8
)(nR1
+ ((nR2
- nR1
) * nGradient
/ 100));
1109 nG
= (wxUint8
)(nG1
+ ((nG2
- nG1
) * nGradient
/ 100));
1110 nB
= (wxUint8
)(nB1
+ ((nB2
- nB1
) * nGradient
/ 100));
1113 m_pen
.SetColour(wxColour(nR
,nG
,nB
));
1114 DoDrawPoint(x
+ rect
.GetLeft(), y
+ rect
.GetTop());
1117 //return old pen color
1118 m_pen
.SetColour(oldPenColour
);
1121 //-----------------------------------------------------------------------------
1123 //-----------------------------------------------------------------------------
1125 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxObject
)
1127 void wxDC::DrawLabel(const wxString
& text
,
1128 const wxBitmap
& bitmap
,
1132 wxRect
*rectBounding
)
1134 // find the text position
1135 wxCoord widthText
, heightText
, heightLine
;
1136 GetMultiLineTextExtent(text
, &widthText
, &heightText
, &heightLine
);
1138 wxCoord width
, height
;
1141 width
= widthText
+ bitmap
.GetWidth();
1142 height
= bitmap
.GetHeight();
1147 height
= heightText
;
1151 if ( alignment
& wxALIGN_RIGHT
)
1153 x
= rect
.GetRight() - width
;
1155 else if ( alignment
& wxALIGN_CENTRE_HORIZONTAL
)
1157 x
= (rect
.GetLeft() + rect
.GetRight() + 1 - width
) / 2;
1159 else // alignment & wxALIGN_LEFT
1164 if ( alignment
& wxALIGN_BOTTOM
)
1166 y
= rect
.GetBottom() - height
;
1168 else if ( alignment
& wxALIGN_CENTRE_VERTICAL
)
1170 y
= (rect
.GetTop() + rect
.GetBottom() + 1 - height
) / 2;
1172 else // alignment & wxALIGN_TOP
1177 // draw the bitmap first
1183 DrawBitmap(bitmap
, x
, y
, true /* use mask */);
1185 wxCoord offset
= bitmap
.GetWidth() + 4;
1189 y
+= (height
- heightText
) / 2;
1192 // we will draw the underscore under the accel char later
1193 wxCoord startUnderscore
= 0,
1197 // split the string into lines and draw each of them separately
1199 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
1201 if ( *pc
== _T('\n') || pc
== text
.end() )
1203 int xRealStart
= x
; // init it here to avoid compielr warnings
1205 if ( !curLine
.empty() )
1207 // NB: can't test for !(alignment & wxALIGN_LEFT) because
1208 // wxALIGN_LEFT is 0
1209 if ( alignment
& (wxALIGN_RIGHT
| wxALIGN_CENTRE_HORIZONTAL
) )
1212 GetTextExtent(curLine
, &widthLine
, NULL
);
1214 if ( alignment
& wxALIGN_RIGHT
)
1216 xRealStart
+= width
- widthLine
;
1218 else // if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
1220 xRealStart
+= (width
- widthLine
) / 2;
1223 //else: left aligned, nothing to do
1225 DrawText(curLine
, xRealStart
, y
);
1230 // do we have underscore in this line? we can check yUnderscore
1231 // because it is set below to just y + heightLine if we do
1232 if ( y
== yUnderscore
)
1234 // adjust the horz positions to account for the shift
1235 startUnderscore
+= xRealStart
;
1236 endUnderscore
+= xRealStart
;
1239 if ( pc
== text
.end() )
1244 else // not end of line
1246 if ( pc
- text
.begin() == indexAccel
)
1248 // remeber to draw underscore here
1249 GetTextExtent(curLine
, &startUnderscore
, NULL
);
1251 GetTextExtent(curLine
, &endUnderscore
, NULL
);
1253 yUnderscore
= y
+ heightLine
;
1262 // draw the underscore if found
1263 if ( startUnderscore
!= endUnderscore
)
1265 // it should be of the same colour as text
1266 SetPen(wxPen(GetTextForeground(), 0, wxSOLID
));
1270 DrawLine(startUnderscore
, yUnderscore
, endUnderscore
, yUnderscore
);
1273 // return bounding rect if requested
1276 *rectBounding
= wxRect(x
, y
- heightText
, widthText
, heightText
);
1279 CalcBoundingBox(x0
, y0
);
1280 CalcBoundingBox(x0
+ width0
, y0
+ height
);
1283 #if WXWIN_COMPATIBILITY_2_8
1284 // for compatibility with the old code when wxCoord was long everywhere
1285 void wxDC::GetTextExtent(const wxString
& string
,
1288 long *externalLeading
,
1289 const wxFont
*theFont
) const
1291 wxCoord x2
, y2
, descent2
, externalLeading2
;
1292 m_pimpl
->DoGetTextExtent(string
, &x2
, &y2
,
1293 &descent2
, &externalLeading2
,
1300 *descent
= descent2
;
1301 if ( externalLeading
)
1302 *externalLeading
= externalLeading2
;
1305 void wxDC::GetLogicalOrigin(long *x
, long *y
) const
1308 m_pimpl
->DoGetLogicalOrigin(&x2
, &y2
);
1315 void wxDC::GetDeviceOrigin(long *x
, long *y
) const
1318 m_pimpl
->DoGetDeviceOrigin(&x2
, &y2
);
1325 void wxDC::GetClippingBox(long *x
, long *y
, long *w
, long *h
) const
1327 wxCoord xx
,yy
,ww
,hh
;
1328 m_pimpl
->DoGetClippingBox(&xx
, &yy
, &ww
, &hh
);
1335 #endif // WXWIN_COMPATIBILITY_2_8