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 wxDCImpl
*wxNativeDCFactory::CreatePrinterDC( wxPrinterDC
*owner
, const wxPrintData
&data
)
141 wxPrintFactory
*factory
= wxPrintFactory::GetFactory();
142 return factory
->CreatePrinterDCImpl( owner
, data
);
145 //-----------------------------------------------------------------------------
147 //-----------------------------------------------------------------------------
149 IMPLEMENT_DYNAMIC_CLASS(wxWindowDC
, wxDC
)
151 wxWindowDC::wxWindowDC()
155 wxWindowDC::wxWindowDC( wxWindow
*win
)
157 wxDCFactory
*factory
= wxDCFactory::GetFactory();
158 m_pimpl
= factory
->CreateWindowDC( this, win
);
161 //-----------------------------------------------------------------------------
163 //-----------------------------------------------------------------------------
165 IMPLEMENT_DYNAMIC_CLASS(wxClientDC
, wxWindowDC
)
167 wxClientDC::wxClientDC()
171 wxClientDC::wxClientDC( wxWindow
*win
)
173 wxDCFactory
*factory
= wxDCFactory::GetFactory();
174 m_pimpl
= factory
->CreateClientDC( this, win
);
177 //-----------------------------------------------------------------------------
179 //-----------------------------------------------------------------------------
181 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC
, wxDC
)
183 wxMemoryDC::wxMemoryDC()
185 wxDCFactory
*factory
= wxDCFactory::GetFactory();
186 m_pimpl
= factory
->CreateMemoryDC( this );
189 wxMemoryDC::wxMemoryDC( wxBitmap
& bitmap
)
191 wxDCFactory
*factory
= wxDCFactory::GetFactory();
192 m_pimpl
= factory
->CreateMemoryDC( this, bitmap
);
195 wxMemoryDC::wxMemoryDC( wxDC
*dc
)
197 wxDCFactory
*factory
= wxDCFactory::GetFactory();
198 m_pimpl
= factory
->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_DYNAMIC_CLASS(wxPaintDC
, wxClientDC
)
234 wxPaintDC::wxPaintDC()
238 wxPaintDC::wxPaintDC( wxWindow
*win
)
240 wxDCFactory
*factory
= wxDCFactory::GetFactory();
241 m_pimpl
= factory
->CreatePaintDC( this, win
);
244 //-----------------------------------------------------------------------------
246 //-----------------------------------------------------------------------------
248 IMPLEMENT_DYNAMIC_CLASS(wxScreenDC
, wxWindowDC
)
250 wxScreenDC::wxScreenDC()
252 wxDCFactory
*factory
= wxDCFactory::GetFactory();
253 m_pimpl
= factory
->CreateScreenDC( this );
256 //-----------------------------------------------------------------------------
258 //-----------------------------------------------------------------------------
260 IMPLEMENT_DYNAMIC_CLASS(wxPrinterDC
, wxDC
)
262 wxPrinterDC::wxPrinterDC()
264 wxPrintData data
; // Does this make sense?
265 wxDCFactory
*factory
= wxDCFactory::GetFactory();
266 m_pimpl
= factory
->CreatePrinterDC( this, data
);
269 wxPrinterDC::wxPrinterDC( const wxPrintData
&data
)
271 wxDCFactory
*factory
= wxDCFactory::GetFactory();
272 m_pimpl
= factory
->CreatePrinterDC( this, data
);
275 wxPrinterDC::~wxPrinterDC()
279 wxRect
wxPrinterDC::GetPaperRect()
281 return GetImpl()->GetPaperRect();
284 int wxPrinterDC::GetResolution()
286 return GetImpl()->GetResolution();
290 //-----------------------------------------------------------------------------
292 //-----------------------------------------------------------------------------
294 IMPLEMENT_ABSTRACT_CLASS(wxDCImpl
, wxObject
)
296 wxDCImpl::wxDCImpl( wxDC
*owner
)
298 , m_colour(wxColourDisplay())
302 , m_isBBoxValid(false)
303 , m_logicalOriginX(0), m_logicalOriginY(0)
304 , m_deviceOriginX(0), m_deviceOriginY(0)
305 , m_deviceLocalOriginX(0), m_deviceLocalOriginY(0)
306 , m_logicalScaleX(1.0), m_logicalScaleY(1.0)
307 , m_userScaleX(1.0), m_userScaleY(1.0)
308 , m_scaleX(1.0), m_scaleY(1.0)
309 , m_signX(1), m_signY(1)
310 , m_minX(0), m_minY(0), m_maxX(0), m_maxY(0)
311 , m_clipX1(0), m_clipY1(0), m_clipX2(0), m_clipY2(0)
312 , m_logicalFunction(wxCOPY
)
313 , m_backgroundMode(wxTRANSPARENT
)
314 , m_mappingMode(wxMM_TEXT
)
317 , m_backgroundBrush(*wxTRANSPARENT_BRUSH
)
318 , m_textForegroundColour(*wxBLACK
)
319 , m_textBackgroundColour(*wxWHITE
)
323 , m_hasCustomPalette(false)
324 #endif // wxUSE_PALETTE
328 m_mm_to_pix_x
= (double)wxGetDisplaySize().GetWidth() /
329 (double)wxGetDisplaySizeMM().GetWidth();
330 m_mm_to_pix_y
= (double)wxGetDisplaySize().GetHeight() /
331 (double)wxGetDisplaySizeMM().GetHeight();
337 wxDCImpl::~wxDCImpl()
341 // ----------------------------------------------------------------------------
342 // coordinate conversions and transforms
343 // ----------------------------------------------------------------------------
345 wxCoord
wxDCImpl::DeviceToLogicalX(wxCoord x
) const
347 return wxRound((double)(x
- m_deviceOriginX
- m_deviceLocalOriginX
) / m_scaleX
) * m_signX
+ m_logicalOriginX
;
350 wxCoord
wxDCImpl::DeviceToLogicalY(wxCoord y
) const
352 return wxRound((double)(y
- m_deviceOriginY
- m_deviceLocalOriginY
) / m_scaleY
) * m_signY
+ m_logicalOriginY
;
355 wxCoord
wxDCImpl::DeviceToLogicalXRel(wxCoord x
) const
357 return wxRound((double)(x
) / m_scaleX
);
360 wxCoord
wxDCImpl::DeviceToLogicalYRel(wxCoord y
) const
362 return wxRound((double)(y
) / m_scaleY
);
365 wxCoord
wxDCImpl::LogicalToDeviceX(wxCoord x
) const
367 return wxRound((double)(x
- m_logicalOriginX
) * m_scaleX
) * m_signX
+ m_deviceOriginX
* m_signY
+ m_deviceLocalOriginX
;
370 wxCoord
wxDCImpl::LogicalToDeviceY(wxCoord y
) const
372 return wxRound((double)(y
- m_logicalOriginY
) * m_scaleY
) * m_signY
+ m_deviceOriginY
* m_signY
+ m_deviceLocalOriginY
;
375 wxCoord
wxDCImpl::LogicalToDeviceXRel(wxCoord x
) const
377 return wxRound((double)(x
) * m_scaleX
);
380 wxCoord
wxDCImpl::LogicalToDeviceYRel(wxCoord y
) const
382 return wxRound((double)(y
) * m_scaleY
);
385 void wxDCImpl::ComputeScaleAndOrigin()
387 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
388 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
391 void wxDCImpl::SetMapMode( int mode
)
396 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
399 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
402 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
405 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
409 SetLogicalScale( 1.0, 1.0 );
412 m_mappingMode
= mode
;
415 void wxDCImpl::SetUserScale( double x
, double y
)
417 // allow negative ? -> no
420 ComputeScaleAndOrigin();
423 void wxDCImpl::SetLogicalScale( double x
, double y
)
428 ComputeScaleAndOrigin();
431 void wxDCImpl::SetLogicalOrigin( wxCoord x
, wxCoord y
)
433 m_logicalOriginX
= x
* m_signX
;
434 m_logicalOriginY
= y
* m_signY
;
435 ComputeScaleAndOrigin();
438 void wxDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
442 ComputeScaleAndOrigin();
445 void wxDCImpl::SetDeviceLocalOrigin( wxCoord x
, wxCoord y
)
447 m_deviceLocalOriginX
= x
;
448 m_deviceLocalOriginY
= y
;
449 ComputeScaleAndOrigin();
452 void wxDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
454 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
455 // wxWidgets 2.9: no longer override it
456 m_signX
= (xLeftRight
? 1 : -1);
457 m_signY
= (yBottomUp
? -1 : 1);
458 ComputeScaleAndOrigin();
462 // Each element of the widths array will be the width of the string up to and
463 // including the corresponding character in text. This is the generic
464 // implementation, the port-specific classes should do this with native APIs
465 // if available and if faster. Note: pango_layout_index_to_pos is much slower
466 // than calling GetTextExtent!!
473 FontWidthCache() : m_scaleX(1), m_widths(NULL
) { }
474 ~FontWidthCache() { delete []m_widths
; }
479 m_widths
= new int[FWC_SIZE
];
481 memset(m_widths
, 0, sizeof(int)*FWC_SIZE
);
489 static FontWidthCache s_fontWidthCache
;
491 bool wxDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
495 const size_t len
= text
.length();
499 // reset the cache if font or horizontal scale have changed
500 if ( !s_fontWidthCache
.m_widths
||
501 !wxIsSameDouble(s_fontWidthCache
.m_scaleX
, m_scaleX
) ||
502 (s_fontWidthCache
.m_font
!= GetFont()) )
504 s_fontWidthCache
.Reset();
505 s_fontWidthCache
.m_font
= GetFont();
506 s_fontWidthCache
.m_scaleX
= m_scaleX
;
509 // Calculate the position of each character based on the widths of
510 // the previous characters
512 for ( size_t i
= 0; i
< len
; i
++ )
514 const wxChar c
= text
[i
];
515 unsigned int c_int
= (unsigned int)c
;
517 if ((c_int
< FWC_SIZE
) && (s_fontWidthCache
.m_widths
[c_int
] != 0))
519 w
= s_fontWidthCache
.m_widths
[c_int
];
523 DoGetTextExtent(c
, &w
, &h
);
524 if (c_int
< FWC_SIZE
)
525 s_fontWidthCache
.m_widths
[c_int
] = w
;
529 widths
[i
] = totalWidth
;
535 void wxDCImpl::GetMultiLineTextExtent(const wxString
& text
,
539 const wxFont
*font
) const
541 wxCoord widthTextMax
= 0, widthLine
,
542 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
545 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
547 if ( pc
== text
.end() || *pc
== _T('\n') )
549 if ( curLine
.empty() )
551 // we can't use GetTextExtent - it will return 0 for both width
552 // and height and an empty line should count in height
555 // assume that this line has the same height as the previous
557 if ( !heightLineDefault
)
558 heightLineDefault
= heightLine
;
560 if ( !heightLineDefault
)
562 // but we don't know it yet - choose something reasonable
563 DoGetTextExtent(_T("W"), NULL
, &heightLineDefault
,
567 heightTextTotal
+= heightLineDefault
;
571 DoGetTextExtent(curLine
, &widthLine
, &heightLine
,
573 if ( widthLine
> widthTextMax
)
574 widthTextMax
= widthLine
;
575 heightTextTotal
+= heightLine
;
578 if ( pc
== text
.end() )
596 *y
= heightTextTotal
;
601 void wxDCImpl::DoDrawCheckMark(wxCoord x1
, wxCoord y1
,
602 wxCoord width
, wxCoord height
)
604 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
606 wxCoord x2
= x1
+ width
,
609 // the pen width is calibrated to give 3 for width == height == 10
610 wxDCPenChanger
pen( *m_owner
, wxPen(GetTextForeground(), (width
+ height
+ 1)/7));
612 // we're drawing a scaled version of wx/generic/tick.xpm here
613 wxCoord x3
= x1
+ (4*width
) / 10, // x of the tick bottom
614 y3
= y1
+ height
/ 2; // y of the left tick branch
615 DoDrawLine(x1
, y3
, x3
, y2
);
616 DoDrawLine(x3
, y2
, x2
, y1
);
618 CalcBoundingBox(x1
, y1
);
619 CalcBoundingBox(x2
, y2
);
623 wxDCImpl::DoStretchBlit(wxCoord xdest
, wxCoord ydest
,
624 wxCoord dstWidth
, wxCoord dstHeight
,
626 wxCoord xsrc
, wxCoord ysrc
,
627 wxCoord srcWidth
, wxCoord srcHeight
,
633 wxCHECK_MSG( srcWidth
&& srcHeight
&& dstWidth
&& dstHeight
, false,
634 _T("invalid blit size") );
636 // emulate the stretching by modifying the DC scale
637 double xscale
= (double)srcWidth
/dstWidth
,
638 yscale
= (double)srcHeight
/dstHeight
;
640 double xscaleOld
, yscaleOld
;
641 GetUserScale(&xscaleOld
, &yscaleOld
);
642 SetUserScale(xscaleOld
/xscale
, yscaleOld
/yscale
);
644 bool rc
= DoBlit(wxCoord(xdest
*xscale
), wxCoord(ydest
*yscale
),
645 wxCoord(dstWidth
*xscale
), wxCoord(dstHeight
*yscale
),
647 xsrc
, ysrc
, rop
, useMask
, xsrcMask
, ysrcMask
);
649 SetUserScale(xscaleOld
, yscaleOld
);
654 void wxDCImpl::DrawLines(const wxPointList
*list
, wxCoord xoffset
, wxCoord yoffset
)
656 int n
= list
->GetCount();
657 wxPoint
*points
= new wxPoint
[n
];
660 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
662 wxPoint
*point
= node
->GetData();
663 points
[i
].x
= point
->x
;
664 points
[i
].y
= point
->y
;
667 DoDrawLines(n
, points
, xoffset
, yoffset
);
672 void wxDCImpl::DrawPolygon(const wxPointList
*list
,
673 wxCoord xoffset
, wxCoord yoffset
,
676 int n
= list
->GetCount();
677 wxPoint
*points
= new wxPoint
[n
];
680 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
682 wxPoint
*point
= node
->GetData();
683 points
[i
].x
= point
->x
;
684 points
[i
].y
= point
->y
;
687 DoDrawPolygon(n
, points
, xoffset
, yoffset
, fillStyle
);
693 wxDCImpl::DoDrawPolyPolygon(int n
,
696 wxCoord xoffset
, wxCoord yoffset
,
701 DoDrawPolygon(count
[0], points
, xoffset
, yoffset
, fillStyle
);
709 for (i
= j
= lastOfs
= 0; i
< n
; i
++)
714 pts
= new wxPoint
[j
+n
-1];
715 for (i
= 0; i
< j
; i
++)
717 for (i
= 2; i
<= n
; i
++)
719 lastOfs
-= count
[n
-i
];
720 pts
[j
++] = pts
[lastOfs
];
724 SetPen(wxPen(*wxBLACK
, 0, wxTRANSPARENT
));
725 DoDrawPolygon(j
, pts
, xoffset
, yoffset
, fillStyle
);
727 for (i
= j
= 0; i
< n
; i
++)
729 DoDrawLines(count
[i
], pts
+j
, xoffset
, yoffset
);
737 void wxDCImpl::DoDrawSpline(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
, wxCoord x3
, wxCoord y3
)
739 wxPointList point_list
;
741 wxPoint
*point1
= new wxPoint
;
742 point1
->x
= x1
; point1
->y
= y1
;
743 point_list
.Append( point1
);
745 wxPoint
*point2
= new wxPoint
;
746 point2
->x
= x2
; point2
->y
= y2
;
747 point_list
.Append( point2
);
749 wxPoint
*point3
= new wxPoint
;
750 point3
->x
= x3
; point3
->y
= y3
;
751 point_list
.Append( point3
);
753 DoDrawSpline(&point_list
);
755 for( wxPointList::compatibility_iterator node
= point_list
.GetFirst(); node
; node
= node
->GetNext() )
757 wxPoint
*p
= node
->GetData();
762 void wxDCImpl::DoDrawSpline(int n
, wxPoint points
[])
765 for (int i
=0; i
< n
; i
++)
766 list
.Append( &points
[i
] );
771 // ----------------------------------- spline code ----------------------------------------
773 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
,
774 double a3
, double b3
, double a4
, double b4
);
775 void wx_clear_stack();
776 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
, double *x3
,
777 double *y3
, double *x4
, double *y4
);
778 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
,
779 double x4
, double y4
);
780 static bool wx_spline_add_point(double x
, double y
);
781 static void wx_spline_draw_point_array(wxDC
*dc
);
783 wxPointList wx_spline_point_list
;
785 #define half(z1, z2) ((z1+z2)/2.0)
788 /* iterative version */
790 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
, double a3
, double b3
, double a4
,
793 register double xmid
, ymid
;
794 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
797 wx_spline_push(a1
, b1
, a2
, b2
, a3
, b3
, a4
, b4
);
799 while (wx_spline_pop(&x1
, &y1
, &x2
, &y2
, &x3
, &y3
, &x4
, &y4
)) {
800 xmid
= (double)half(x2
, x3
);
801 ymid
= (double)half(y2
, y3
);
802 if (fabs(x1
- xmid
) < THRESHOLD
&& fabs(y1
- ymid
) < THRESHOLD
&&
803 fabs(xmid
- x4
) < THRESHOLD
&& fabs(ymid
- y4
) < THRESHOLD
) {
804 wx_spline_add_point( x1
, y1
);
805 wx_spline_add_point( xmid
, ymid
);
807 wx_spline_push(xmid
, ymid
, (double)half(xmid
, x3
), (double)half(ymid
, y3
),
808 (double)half(x3
, x4
), (double)half(y3
, y4
), x4
, y4
);
809 wx_spline_push(x1
, y1
, (double)half(x1
, x2
), (double)half(y1
, y2
),
810 (double)half(x2
, xmid
), (double)half(y2
, ymid
), xmid
, ymid
);
815 /* utilities used by spline drawing routines */
817 typedef struct wx_spline_stack_struct
{
818 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
821 #define SPLINE_STACK_DEPTH 20
822 static Stack wx_spline_stack
[SPLINE_STACK_DEPTH
];
823 static Stack
*wx_stack_top
;
824 static int wx_stack_count
;
826 void wx_clear_stack()
828 wx_stack_top
= wx_spline_stack
;
832 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
, double x4
, double y4
)
834 wx_stack_top
->x1
= x1
;
835 wx_stack_top
->y1
= y1
;
836 wx_stack_top
->x2
= x2
;
837 wx_stack_top
->y2
= y2
;
838 wx_stack_top
->x3
= x3
;
839 wx_stack_top
->y3
= y3
;
840 wx_stack_top
->x4
= x4
;
841 wx_stack_top
->y4
= y4
;
846 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
,
847 double *x3
, double *y3
, double *x4
, double *y4
)
849 if (wx_stack_count
== 0)
853 *x1
= wx_stack_top
->x1
;
854 *y1
= wx_stack_top
->y1
;
855 *x2
= wx_stack_top
->x2
;
856 *y2
= wx_stack_top
->y2
;
857 *x3
= wx_stack_top
->x3
;
858 *y3
= wx_stack_top
->y3
;
859 *x4
= wx_stack_top
->x4
;
860 *y4
= wx_stack_top
->y4
;
864 static bool wx_spline_add_point(double x
, double y
)
866 wxPoint
*point
= new wxPoint( wxRound(x
), wxRound(y
) );
867 wx_spline_point_list
.Append(point
);
871 static void wx_spline_draw_point_array(wxDC
*dc
)
873 dc
->DrawLines(&wx_spline_point_list
, 0, 0 );
874 wxPointList::compatibility_iterator node
= wx_spline_point_list
.GetFirst();
877 wxPoint
*point
= node
->GetData();
879 wx_spline_point_list
.Erase(node
);
880 node
= wx_spline_point_list
.GetFirst();
884 void wxDCImpl::DoDrawSpline( const wxPointList
*points
)
886 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
889 double cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
;
890 double x1
, y1
, x2
, y2
;
892 wxPointList::compatibility_iterator node
= points
->GetFirst();
897 p
= (wxPoint
*)node
->GetData();
902 node
= node
->GetNext();
907 cx1
= (double)((x1
+ x2
) / 2);
908 cy1
= (double)((y1
+ y2
) / 2);
909 cx2
= (double)((cx1
+ x2
) / 2);
910 cy2
= (double)((cy1
+ y2
) / 2);
912 wx_spline_add_point(x1
, y1
);
914 while ((node
= node
->GetNext())
925 cx4
= (double)(x1
+ x2
) / 2;
926 cy4
= (double)(y1
+ y2
) / 2;
927 cx3
= (double)(x1
+ cx4
) / 2;
928 cy3
= (double)(y1
+ cy4
) / 2;
930 wx_quadratic_spline(cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
);
934 cx2
= (double)(cx1
+ x2
) / 2;
935 cy2
= (double)(cy1
+ y2
) / 2;
938 wx_spline_add_point( cx1
, cy1
);
939 wx_spline_add_point( x2
, y2
);
941 wx_spline_draw_point_array( m_owner
);
944 #endif // wxUSE_SPLINES
948 void wxDCImpl::DoGradientFillLinear(const wxRect
& rect
,
949 const wxColour
& initialColour
,
950 const wxColour
& destColour
,
951 wxDirection nDirection
)
954 wxPen oldPen
= m_pen
;
955 wxBrush oldBrush
= m_brush
;
957 wxUint8 nR1
= initialColour
.Red();
958 wxUint8 nG1
= initialColour
.Green();
959 wxUint8 nB1
= initialColour
.Blue();
960 wxUint8 nR2
= destColour
.Red();
961 wxUint8 nG2
= destColour
.Green();
962 wxUint8 nB2
= destColour
.Blue();
965 if ( nDirection
== wxEAST
|| nDirection
== wxWEST
)
967 wxInt32 x
= rect
.GetWidth();
968 wxInt32 w
= x
; // width of area to shade
969 wxInt32 xDelta
= w
/256; // height of one shade bend
977 nR
= nR1
- (nR1
-nR2
)*(w
-x
)/w
;
979 nR
= nR1
+ (nR2
-nR1
)*(w
-x
)/w
;
982 nG
= nG1
- (nG1
-nG2
)*(w
-x
)/w
;
984 nG
= nG1
+ (nG2
-nG1
)*(w
-x
)/w
;
987 nB
= nB1
- (nB1
-nB2
)*(w
-x
)/w
;
989 nB
= nB1
+ (nB2
-nB1
)*(w
-x
)/w
;
991 wxColour
colour(nR
,nG
,nB
);
992 SetPen(wxPen(colour
, 1, wxSOLID
));
993 SetBrush(wxBrush(colour
));
994 if(nDirection
== wxEAST
)
995 DoDrawRectangle(rect
.GetRight()-x
-xDelta
+1, rect
.GetTop(),
996 xDelta
, rect
.GetHeight());
997 else //nDirection == wxWEST
998 DoDrawRectangle(rect
.GetLeft()+x
, rect
.GetTop(),
999 xDelta
, rect
.GetHeight());
1002 else // nDirection == wxNORTH || nDirection == wxSOUTH
1004 wxInt32 y
= rect
.GetHeight();
1005 wxInt32 w
= y
; // height of area to shade
1006 wxInt32 yDelta
= w
/255; // height of one shade bend
1014 nR
= nR1
- (nR1
-nR2
)*(w
-y
)/w
;
1016 nR
= nR1
+ (nR2
-nR1
)*(w
-y
)/w
;
1019 nG
= nG1
- (nG1
-nG2
)*(w
-y
)/w
;
1021 nG
= nG1
+ (nG2
-nG1
)*(w
-y
)/w
;
1024 nB
= nB1
- (nB1
-nB2
)*(w
-y
)/w
;
1026 nB
= nB1
+ (nB2
-nB1
)*(w
-y
)/w
;
1028 wxColour
colour(nR
,nG
,nB
);
1029 SetPen(wxPen(colour
, 1, wxSOLID
));
1030 SetBrush(wxBrush(colour
));
1031 if(nDirection
== wxNORTH
)
1032 DoDrawRectangle(rect
.GetLeft(), rect
.GetTop()+y
,
1033 rect
.GetWidth(), yDelta
);
1034 else //nDirection == wxSOUTH
1035 DoDrawRectangle(rect
.GetLeft(), rect
.GetBottom()-y
-yDelta
+1,
1036 rect
.GetWidth(), yDelta
);
1044 void wxDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1045 const wxColour
& initialColour
,
1046 const wxColour
& destColour
,
1047 const wxPoint
& circleCenter
)
1049 //save the old pen color
1050 wxColour oldPenColour
= m_pen
.GetColour();
1052 wxUint8 nR1
= destColour
.Red();
1053 wxUint8 nG1
= destColour
.Green();
1054 wxUint8 nB1
= destColour
.Blue();
1055 wxUint8 nR2
= initialColour
.Red();
1056 wxUint8 nG2
= initialColour
.Green();
1057 wxUint8 nB2
= initialColour
.Blue();
1062 wxInt32 cx
= rect
.GetWidth() / 2;
1063 wxInt32 cy
= rect
.GetHeight() / 2;
1071 wxInt32 nCircleOffX
= circleCenter
.x
- (rect
.GetWidth() / 2);
1072 wxInt32 nCircleOffY
= circleCenter
.y
- (rect
.GetHeight() / 2);
1074 for ( wxInt32 x
= 0; x
< rect
.GetWidth(); x
++ )
1076 for ( wxInt32 y
= 0; y
< rect
.GetHeight(); y
++ )
1078 //get color difference
1079 wxInt32 nGradient
= ((nRadius
-
1081 pow((double)(x
- cx
- nCircleOffX
), 2) +
1082 pow((double)(y
- cy
- nCircleOffY
), 2)
1083 )) * 100) / nRadius
;
1085 //normalize Gradient
1090 nR
= (wxUint8
)(nR1
+ ((nR2
- nR1
) * nGradient
/ 100));
1091 nG
= (wxUint8
)(nG1
+ ((nG2
- nG1
) * nGradient
/ 100));
1092 nB
= (wxUint8
)(nB1
+ ((nB2
- nB1
) * nGradient
/ 100));
1095 m_pen
.SetColour(wxColour(nR
,nG
,nB
));
1096 DoDrawPoint(x
+ rect
.GetLeft(), y
+ rect
.GetTop());
1099 //return old pen color
1100 m_pen
.SetColour(oldPenColour
);
1103 //-----------------------------------------------------------------------------
1105 //-----------------------------------------------------------------------------
1107 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxObject
)
1109 void wxDC::DrawLabel(const wxString
& text
,
1110 const wxBitmap
& bitmap
,
1114 wxRect
*rectBounding
)
1116 // find the text position
1117 wxCoord widthText
, heightText
, heightLine
;
1118 GetMultiLineTextExtent(text
, &widthText
, &heightText
, &heightLine
);
1120 wxCoord width
, height
;
1123 width
= widthText
+ bitmap
.GetWidth();
1124 height
= bitmap
.GetHeight();
1129 height
= heightText
;
1133 if ( alignment
& wxALIGN_RIGHT
)
1135 x
= rect
.GetRight() - width
;
1137 else if ( alignment
& wxALIGN_CENTRE_HORIZONTAL
)
1139 x
= (rect
.GetLeft() + rect
.GetRight() + 1 - width
) / 2;
1141 else // alignment & wxALIGN_LEFT
1146 if ( alignment
& wxALIGN_BOTTOM
)
1148 y
= rect
.GetBottom() - height
;
1150 else if ( alignment
& wxALIGN_CENTRE_VERTICAL
)
1152 y
= (rect
.GetTop() + rect
.GetBottom() + 1 - height
) / 2;
1154 else // alignment & wxALIGN_TOP
1159 // draw the bitmap first
1165 DrawBitmap(bitmap
, x
, y
, true /* use mask */);
1167 wxCoord offset
= bitmap
.GetWidth() + 4;
1171 y
+= (height
- heightText
) / 2;
1174 // we will draw the underscore under the accel char later
1175 wxCoord startUnderscore
= 0,
1179 // split the string into lines and draw each of them separately
1181 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
1183 if ( *pc
== _T('\n') || pc
== text
.end() )
1185 int xRealStart
= x
; // init it here to avoid compielr warnings
1187 if ( !curLine
.empty() )
1189 // NB: can't test for !(alignment & wxALIGN_LEFT) because
1190 // wxALIGN_LEFT is 0
1191 if ( alignment
& (wxALIGN_RIGHT
| wxALIGN_CENTRE_HORIZONTAL
) )
1194 GetTextExtent(curLine
, &widthLine
, NULL
);
1196 if ( alignment
& wxALIGN_RIGHT
)
1198 xRealStart
+= width
- widthLine
;
1200 else // if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
1202 xRealStart
+= (width
- widthLine
) / 2;
1205 //else: left aligned, nothing to do
1207 DrawText(curLine
, xRealStart
, y
);
1212 // do we have underscore in this line? we can check yUnderscore
1213 // because it is set below to just y + heightLine if we do
1214 if ( y
== yUnderscore
)
1216 // adjust the horz positions to account for the shift
1217 startUnderscore
+= xRealStart
;
1218 endUnderscore
+= xRealStart
;
1221 if ( pc
== text
.end() )
1226 else // not end of line
1228 if ( pc
- text
.begin() == indexAccel
)
1230 // remeber to draw underscore here
1231 GetTextExtent(curLine
, &startUnderscore
, NULL
);
1233 GetTextExtent(curLine
, &endUnderscore
, NULL
);
1235 yUnderscore
= y
+ heightLine
;
1244 // draw the underscore if found
1245 if ( startUnderscore
!= endUnderscore
)
1247 // it should be of the same colour as text
1248 SetPen(wxPen(GetTextForeground(), 0, wxSOLID
));
1252 DrawLine(startUnderscore
, yUnderscore
, endUnderscore
, yUnderscore
);
1255 // return bounding rect if requested
1258 *rectBounding
= wxRect(x
, y
- heightText
, widthText
, heightText
);
1261 CalcBoundingBox(x0
, y0
);
1262 CalcBoundingBox(x0
+ width0
, y0
+ height
);
1265 #if WXWIN_COMPATIBILITY_2_8
1266 // for compatibility with the old code when wxCoord was long everywhere
1267 void wxDC::GetTextExtent(const wxString
& string
,
1270 long *externalLeading
,
1271 const wxFont
*theFont
) const
1273 wxCoord x2
, y2
, descent2
, externalLeading2
;
1274 m_pimpl
->DoGetTextExtent(string
, &x2
, &y2
,
1275 &descent2
, &externalLeading2
,
1282 *descent
= descent2
;
1283 if ( externalLeading
)
1284 *externalLeading
= externalLeading2
;
1287 void wxDC::GetLogicalOrigin(long *x
, long *y
) const
1290 m_pimpl
->DoGetLogicalOrigin(&x2
, &y2
);
1297 void wxDC::GetDeviceOrigin(long *x
, long *y
) const
1300 m_pimpl
->DoGetDeviceOrigin(&x2
, &y2
);
1307 void wxDC::GetClippingBox(long *x
, long *y
, long *w
, long *h
) const
1309 wxCoord xx
,yy
,ww
,hh
;
1310 m_pimpl
->DoGetClippingBox(&xx
, &yy
, &ww
, &hh
);
1317 #endif // WXWIN_COMPATIBILITY_2_8