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/cocoa/dcclient.h"
59 #include "wx/cocoa/dcmemory.h"
60 #include "wx/cocoa/dcscreen.h"
64 #include "wx/motif/dcclient.h"
65 #include "wx/motif/dcmemory.h"
66 #include "wx/motif/dcscreen.h"
70 #include "wx/x11/dcclient.h"
71 #include "wx/x11/dcmemory.h"
72 #include "wx/x11/dcscreen.h"
76 #include "wx/dfb/dcclient.h"
77 #include "wx/dfb/dcmemory.h"
78 #include "wx/dfb/dcscreen.h"
81 //----------------------------------------------------------------------------
83 //----------------------------------------------------------------------------
85 wxDCFactory
*wxDCFactory::m_factory
= NULL
;
87 void wxDCFactory::Set(wxDCFactory
*factory
)
94 wxDCFactory
*wxDCFactory::Get()
97 m_factory
= new wxNativeDCFactory
;
102 class wxDCFactoryCleanupModule
: public wxModule
105 virtual bool OnInit() { return true; }
106 virtual void OnExit() { wxDCFactory::Set(NULL
); }
109 DECLARE_DYNAMIC_CLASS(wxDCFactoryCleanupModule
)
112 IMPLEMENT_DYNAMIC_CLASS(wxDCFactoryCleanupModule
, wxModule
)
114 //-----------------------------------------------------------------------------
116 //-----------------------------------------------------------------------------
118 wxDCImpl
* wxNativeDCFactory::CreateWindowDC( wxWindowDC
*owner
)
120 return new wxWindowDCImpl( owner
);
123 wxDCImpl
* wxNativeDCFactory::CreateWindowDC( wxWindowDC
*owner
, wxWindow
*window
)
125 return new wxWindowDCImpl( owner
, window
);
128 wxDCImpl
* wxNativeDCFactory::CreateClientDC( wxClientDC
*owner
)
130 return new wxClientDCImpl( owner
);
133 wxDCImpl
* wxNativeDCFactory::CreateClientDC( wxClientDC
*owner
, wxWindow
*window
)
135 return new wxClientDCImpl( owner
, window
);
138 wxDCImpl
* wxNativeDCFactory::CreatePaintDC( wxPaintDC
*owner
)
140 return new wxPaintDCImpl( owner
);
143 wxDCImpl
* wxNativeDCFactory::CreatePaintDC( wxPaintDC
*owner
, wxWindow
*window
)
145 return new wxPaintDCImpl( owner
, window
);
148 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
)
150 return new wxMemoryDCImpl( owner
);
153 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
, wxBitmap
&bitmap
)
155 return new wxMemoryDCImpl( owner
, bitmap
);
158 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
, wxDC
*dc
)
160 return new wxMemoryDCImpl( owner
, dc
);
163 wxDCImpl
* wxNativeDCFactory::CreateScreenDC( wxScreenDC
*owner
)
165 return new wxScreenDCImpl( owner
);
168 #if wxUSE_PRINTING_ARCHITECTURE
169 wxDCImpl
*wxNativeDCFactory::CreatePrinterDC( wxPrinterDC
*owner
, const wxPrintData
&data
)
171 wxPrintFactory
*factory
= wxPrintFactory::GetFactory();
172 return factory
->CreatePrinterDCImpl( owner
, data
);
176 //-----------------------------------------------------------------------------
178 //-----------------------------------------------------------------------------
180 IMPLEMENT_ABSTRACT_CLASS(wxWindowDC
, wxDC
)
182 wxWindowDC::wxWindowDC(wxWindow
*win
)
183 : wxDC(wxDCFactory::Get()->CreateWindowDC(this, win
))
187 //-----------------------------------------------------------------------------
189 //-----------------------------------------------------------------------------
191 IMPLEMENT_ABSTRACT_CLASS(wxClientDC
, wxWindowDC
)
193 wxClientDC::wxClientDC(wxWindow
*win
)
194 : wxWindowDC(wxDCFactory::Get()->CreateClientDC(this, win
))
198 //-----------------------------------------------------------------------------
200 //-----------------------------------------------------------------------------
202 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC
, wxDC
)
204 wxMemoryDC::wxMemoryDC()
205 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this))
209 wxMemoryDC::wxMemoryDC(wxBitmap
& bitmap
)
210 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, bitmap
))
214 wxMemoryDC::wxMemoryDC(wxDC
*dc
)
215 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, dc
))
219 void wxMemoryDC::SelectObject(wxBitmap
& bmp
)
221 // make sure that the given wxBitmap is not sharing its data with other
222 // wxBitmap instances as its contents will be modified by any drawing
223 // operation done on this DC
227 GetImpl()->DoSelect(bmp
);
230 void wxMemoryDC::SelectObjectAsSource(const wxBitmap
& bmp
)
232 GetImpl()->DoSelect(bmp
);
235 const wxBitmap
& wxMemoryDC::GetSelectedBitmap() const
237 return GetImpl()->GetSelectedBitmap();
240 wxBitmap
& wxMemoryDC::GetSelectedBitmap()
242 return GetImpl()->GetSelectedBitmap();
246 //-----------------------------------------------------------------------------
248 //-----------------------------------------------------------------------------
250 IMPLEMENT_ABSTRACT_CLASS(wxPaintDC
, wxClientDC
)
252 wxPaintDC::wxPaintDC(wxWindow
*win
)
253 : wxClientDC(wxDCFactory::Get()->CreatePaintDC(this, win
))
257 //-----------------------------------------------------------------------------
259 //-----------------------------------------------------------------------------
261 IMPLEMENT_DYNAMIC_CLASS(wxScreenDC
, wxWindowDC
)
263 wxScreenDC::wxScreenDC()
264 : wxDC(wxDCFactory::Get()->CreateScreenDC(this))
268 //-----------------------------------------------------------------------------
270 //-----------------------------------------------------------------------------
272 #if wxUSE_PRINTING_ARCHITECTURE
274 IMPLEMENT_DYNAMIC_CLASS(wxPrinterDC
, wxDC
)
276 wxPrinterDC::wxPrinterDC()
277 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, wxPrintData()))
281 wxPrinterDC::wxPrinterDC(const wxPrintData
& data
)
282 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, data
))
286 wxRect
wxPrinterDC::GetPaperRect()
288 return GetImpl()->GetPaperRect();
291 int wxPrinterDC::GetResolution()
293 return GetImpl()->GetResolution();
296 #endif // wxUSE_PRINTING_ARCHITECTURE
298 //-----------------------------------------------------------------------------
300 //-----------------------------------------------------------------------------
302 IMPLEMENT_ABSTRACT_CLASS(wxDCImpl
, wxObject
)
304 wxDCImpl::wxDCImpl( wxDC
*owner
)
306 , m_colour(wxColourDisplay())
310 , m_isBBoxValid(false)
311 , m_logicalOriginX(0), m_logicalOriginY(0)
312 , m_deviceOriginX(0), m_deviceOriginY(0)
313 , m_deviceLocalOriginX(0), m_deviceLocalOriginY(0)
314 , m_logicalScaleX(1.0), m_logicalScaleY(1.0)
315 , m_userScaleX(1.0), m_userScaleY(1.0)
316 , m_scaleX(1.0), m_scaleY(1.0)
317 , m_signX(1), m_signY(1)
318 , m_minX(0), m_minY(0), m_maxX(0), m_maxY(0)
319 , m_clipX1(0), m_clipY1(0), m_clipX2(0), m_clipY2(0)
320 , m_logicalFunction(wxCOPY
)
321 , m_backgroundMode(wxTRANSPARENT
)
322 , m_mappingMode(wxMM_TEXT
)
325 , m_backgroundBrush(*wxTRANSPARENT_BRUSH
)
326 , m_textForegroundColour(*wxBLACK
)
327 , m_textBackgroundColour(*wxWHITE
)
331 , m_hasCustomPalette(false)
332 #endif // wxUSE_PALETTE
336 m_mm_to_pix_x
= (double)wxGetDisplaySize().GetWidth() /
337 (double)wxGetDisplaySizeMM().GetWidth();
338 m_mm_to_pix_y
= (double)wxGetDisplaySize().GetHeight() /
339 (double)wxGetDisplaySizeMM().GetHeight();
345 wxDCImpl::~wxDCImpl()
349 // ----------------------------------------------------------------------------
350 // coordinate conversions and transforms
351 // ----------------------------------------------------------------------------
353 wxCoord
wxDCImpl::DeviceToLogicalX(wxCoord x
) const
355 return wxRound((double)(x
- m_deviceOriginX
- m_deviceLocalOriginX
) / m_scaleX
) * m_signX
+ m_logicalOriginX
;
358 wxCoord
wxDCImpl::DeviceToLogicalY(wxCoord y
) const
360 return wxRound((double)(y
- m_deviceOriginY
- m_deviceLocalOriginY
) / m_scaleY
) * m_signY
+ m_logicalOriginY
;
363 wxCoord
wxDCImpl::DeviceToLogicalXRel(wxCoord x
) const
365 return wxRound((double)(x
) / m_scaleX
);
368 wxCoord
wxDCImpl::DeviceToLogicalYRel(wxCoord y
) const
370 return wxRound((double)(y
) / m_scaleY
);
373 wxCoord
wxDCImpl::LogicalToDeviceX(wxCoord x
) const
375 return wxRound((double)(x
- m_logicalOriginX
) * m_scaleX
) * m_signX
+ m_deviceOriginX
* m_signY
+ m_deviceLocalOriginX
;
378 wxCoord
wxDCImpl::LogicalToDeviceY(wxCoord y
) const
380 return wxRound((double)(y
- m_logicalOriginY
) * m_scaleY
) * m_signY
+ m_deviceOriginY
* m_signY
+ m_deviceLocalOriginY
;
383 wxCoord
wxDCImpl::LogicalToDeviceXRel(wxCoord x
) const
385 return wxRound((double)(x
) * m_scaleX
);
388 wxCoord
wxDCImpl::LogicalToDeviceYRel(wxCoord y
) const
390 return wxRound((double)(y
) * m_scaleY
);
393 void wxDCImpl::ComputeScaleAndOrigin()
395 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
396 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
399 void wxDCImpl::SetMapMode( int mode
)
404 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
407 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
410 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
413 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
417 SetLogicalScale( 1.0, 1.0 );
420 m_mappingMode
= mode
;
423 void wxDCImpl::SetUserScale( double x
, double y
)
425 // allow negative ? -> no
428 ComputeScaleAndOrigin();
431 void wxDCImpl::SetLogicalScale( double x
, double y
)
436 ComputeScaleAndOrigin();
439 void wxDCImpl::SetLogicalOrigin( wxCoord x
, wxCoord y
)
441 m_logicalOriginX
= x
* m_signX
;
442 m_logicalOriginY
= y
* m_signY
;
443 ComputeScaleAndOrigin();
446 void wxDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
450 ComputeScaleAndOrigin();
453 void wxDCImpl::SetDeviceLocalOrigin( wxCoord x
, wxCoord y
)
455 m_deviceLocalOriginX
= x
;
456 m_deviceLocalOriginY
= y
;
457 ComputeScaleAndOrigin();
460 void wxDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
462 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
463 // wxWidgets 2.9: no longer override it
464 m_signX
= (xLeftRight
? 1 : -1);
465 m_signY
= (yBottomUp
? -1 : 1);
466 ComputeScaleAndOrigin();
470 // Each element of the widths array will be the width of the string up to and
471 // including the corresponding character in text. This is the generic
472 // implementation, the port-specific classes should do this with native APIs
473 // if available and if faster. Note: pango_layout_index_to_pos is much slower
474 // than calling GetTextExtent!!
481 FontWidthCache() : m_scaleX(1), m_widths(NULL
) { }
482 ~FontWidthCache() { delete []m_widths
; }
487 m_widths
= new int[FWC_SIZE
];
489 memset(m_widths
, 0, sizeof(int)*FWC_SIZE
);
497 static FontWidthCache s_fontWidthCache
;
499 bool wxDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
503 const size_t len
= text
.length();
507 // reset the cache if font or horizontal scale have changed
508 if ( !s_fontWidthCache
.m_widths
||
509 !wxIsSameDouble(s_fontWidthCache
.m_scaleX
, m_scaleX
) ||
510 (s_fontWidthCache
.m_font
!= GetFont()) )
512 s_fontWidthCache
.Reset();
513 s_fontWidthCache
.m_font
= GetFont();
514 s_fontWidthCache
.m_scaleX
= m_scaleX
;
517 // Calculate the position of each character based on the widths of
518 // the previous characters
520 for ( size_t i
= 0; i
< len
; i
++ )
522 const wxChar c
= text
[i
];
523 unsigned int c_int
= (unsigned int)c
;
525 if ((c_int
< FWC_SIZE
) && (s_fontWidthCache
.m_widths
[c_int
] != 0))
527 w
= s_fontWidthCache
.m_widths
[c_int
];
531 DoGetTextExtent(c
, &w
, &h
);
532 if (c_int
< FWC_SIZE
)
533 s_fontWidthCache
.m_widths
[c_int
] = w
;
537 widths
[i
] = totalWidth
;
543 void wxDCImpl::GetMultiLineTextExtent(const wxString
& text
,
547 const wxFont
*font
) const
549 wxCoord widthTextMax
= 0, widthLine
,
550 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
553 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
555 if ( pc
== text
.end() || *pc
== _T('\n') )
557 if ( curLine
.empty() )
559 // we can't use GetTextExtent - it will return 0 for both width
560 // and height and an empty line should count in height
563 // assume that this line has the same height as the previous
565 if ( !heightLineDefault
)
566 heightLineDefault
= heightLine
;
568 if ( !heightLineDefault
)
570 // but we don't know it yet - choose something reasonable
571 DoGetTextExtent(_T("W"), NULL
, &heightLineDefault
,
575 heightTextTotal
+= heightLineDefault
;
579 DoGetTextExtent(curLine
, &widthLine
, &heightLine
,
581 if ( widthLine
> widthTextMax
)
582 widthTextMax
= widthLine
;
583 heightTextTotal
+= heightLine
;
586 if ( pc
== text
.end() )
604 *y
= heightTextTotal
;
609 void wxDCImpl::DoDrawCheckMark(wxCoord x1
, wxCoord y1
,
610 wxCoord width
, wxCoord height
)
612 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
614 wxCoord x2
= x1
+ width
,
617 // the pen width is calibrated to give 3 for width == height == 10
618 wxDCPenChanger
pen( *m_owner
, wxPen(GetTextForeground(), (width
+ height
+ 1)/7));
620 // we're drawing a scaled version of wx/generic/tick.xpm here
621 wxCoord x3
= x1
+ (4*width
) / 10, // x of the tick bottom
622 y3
= y1
+ height
/ 2; // y of the left tick branch
623 DoDrawLine(x1
, y3
, x3
, y2
);
624 DoDrawLine(x3
, y2
, x2
, y1
);
626 CalcBoundingBox(x1
, y1
);
627 CalcBoundingBox(x2
, y2
);
631 wxDCImpl::DoStretchBlit(wxCoord xdest
, wxCoord ydest
,
632 wxCoord dstWidth
, wxCoord dstHeight
,
634 wxCoord xsrc
, wxCoord ysrc
,
635 wxCoord srcWidth
, wxCoord srcHeight
,
641 wxCHECK_MSG( srcWidth
&& srcHeight
&& dstWidth
&& dstHeight
, false,
642 _T("invalid blit size") );
644 // emulate the stretching by modifying the DC scale
645 double xscale
= (double)srcWidth
/dstWidth
,
646 yscale
= (double)srcHeight
/dstHeight
;
648 double xscaleOld
, yscaleOld
;
649 GetUserScale(&xscaleOld
, &yscaleOld
);
650 SetUserScale(xscaleOld
/xscale
, yscaleOld
/yscale
);
652 bool rc
= DoBlit(wxCoord(xdest
*xscale
), wxCoord(ydest
*yscale
),
653 wxCoord(dstWidth
*xscale
), wxCoord(dstHeight
*yscale
),
655 xsrc
, ysrc
, rop
, useMask
, xsrcMask
, ysrcMask
);
657 SetUserScale(xscaleOld
, yscaleOld
);
662 void wxDCImpl::DrawLines(const wxPointList
*list
, wxCoord xoffset
, wxCoord yoffset
)
664 int n
= list
->GetCount();
665 wxPoint
*points
= new wxPoint
[n
];
668 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
670 wxPoint
*point
= node
->GetData();
671 points
[i
].x
= point
->x
;
672 points
[i
].y
= point
->y
;
675 DoDrawLines(n
, points
, xoffset
, yoffset
);
680 void wxDCImpl::DrawPolygon(const wxPointList
*list
,
681 wxCoord xoffset
, wxCoord yoffset
,
684 int n
= list
->GetCount();
685 wxPoint
*points
= new wxPoint
[n
];
688 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
690 wxPoint
*point
= node
->GetData();
691 points
[i
].x
= point
->x
;
692 points
[i
].y
= point
->y
;
695 DoDrawPolygon(n
, points
, xoffset
, yoffset
, fillStyle
);
701 wxDCImpl::DoDrawPolyPolygon(int n
,
704 wxCoord xoffset
, wxCoord yoffset
,
709 DoDrawPolygon(count
[0], points
, xoffset
, yoffset
, fillStyle
);
717 for (i
= j
= lastOfs
= 0; i
< n
; i
++)
722 pts
= new wxPoint
[j
+n
-1];
723 for (i
= 0; i
< j
; i
++)
725 for (i
= 2; i
<= n
; i
++)
727 lastOfs
-= count
[n
-i
];
728 pts
[j
++] = pts
[lastOfs
];
732 SetPen(wxPen(*wxBLACK
, 0, wxTRANSPARENT
));
733 DoDrawPolygon(j
, pts
, xoffset
, yoffset
, fillStyle
);
735 for (i
= j
= 0; i
< n
; i
++)
737 DoDrawLines(count
[i
], pts
+j
, xoffset
, yoffset
);
745 void wxDCImpl::DoDrawSpline(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
, wxCoord x3
, wxCoord y3
)
747 wxPointList point_list
;
749 wxPoint
*point1
= new wxPoint
;
750 point1
->x
= x1
; point1
->y
= y1
;
751 point_list
.Append( point1
);
753 wxPoint
*point2
= new wxPoint
;
754 point2
->x
= x2
; point2
->y
= y2
;
755 point_list
.Append( point2
);
757 wxPoint
*point3
= new wxPoint
;
758 point3
->x
= x3
; point3
->y
= y3
;
759 point_list
.Append( point3
);
761 DoDrawSpline(&point_list
);
763 for( wxPointList::compatibility_iterator node
= point_list
.GetFirst(); node
; node
= node
->GetNext() )
765 wxPoint
*p
= node
->GetData();
770 void wxDCImpl::DoDrawSpline(int n
, wxPoint points
[])
773 for (int i
=0; i
< n
; i
++)
774 list
.Append( &points
[i
] );
779 // ----------------------------------- spline code ----------------------------------------
781 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
,
782 double a3
, double b3
, double a4
, double b4
);
783 void wx_clear_stack();
784 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
, double *x3
,
785 double *y3
, double *x4
, double *y4
);
786 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
,
787 double x4
, double y4
);
788 static bool wx_spline_add_point(double x
, double y
);
789 static void wx_spline_draw_point_array(wxDC
*dc
);
791 wxPointList wx_spline_point_list
;
793 #define half(z1, z2) ((z1+z2)/2.0)
796 /* iterative version */
798 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
, double a3
, double b3
, double a4
,
801 register double xmid
, ymid
;
802 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
805 wx_spline_push(a1
, b1
, a2
, b2
, a3
, b3
, a4
, b4
);
807 while (wx_spline_pop(&x1
, &y1
, &x2
, &y2
, &x3
, &y3
, &x4
, &y4
)) {
808 xmid
= (double)half(x2
, x3
);
809 ymid
= (double)half(y2
, y3
);
810 if (fabs(x1
- xmid
) < THRESHOLD
&& fabs(y1
- ymid
) < THRESHOLD
&&
811 fabs(xmid
- x4
) < THRESHOLD
&& fabs(ymid
- y4
) < THRESHOLD
) {
812 wx_spline_add_point( x1
, y1
);
813 wx_spline_add_point( xmid
, ymid
);
815 wx_spline_push(xmid
, ymid
, (double)half(xmid
, x3
), (double)half(ymid
, y3
),
816 (double)half(x3
, x4
), (double)half(y3
, y4
), x4
, y4
);
817 wx_spline_push(x1
, y1
, (double)half(x1
, x2
), (double)half(y1
, y2
),
818 (double)half(x2
, xmid
), (double)half(y2
, ymid
), xmid
, ymid
);
823 /* utilities used by spline drawing routines */
825 typedef struct wx_spline_stack_struct
{
826 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
829 #define SPLINE_STACK_DEPTH 20
830 static Stack wx_spline_stack
[SPLINE_STACK_DEPTH
];
831 static Stack
*wx_stack_top
;
832 static int wx_stack_count
;
834 void wx_clear_stack()
836 wx_stack_top
= wx_spline_stack
;
840 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
, double x4
, double y4
)
842 wx_stack_top
->x1
= x1
;
843 wx_stack_top
->y1
= y1
;
844 wx_stack_top
->x2
= x2
;
845 wx_stack_top
->y2
= y2
;
846 wx_stack_top
->x3
= x3
;
847 wx_stack_top
->y3
= y3
;
848 wx_stack_top
->x4
= x4
;
849 wx_stack_top
->y4
= y4
;
854 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
,
855 double *x3
, double *y3
, double *x4
, double *y4
)
857 if (wx_stack_count
== 0)
861 *x1
= wx_stack_top
->x1
;
862 *y1
= wx_stack_top
->y1
;
863 *x2
= wx_stack_top
->x2
;
864 *y2
= wx_stack_top
->y2
;
865 *x3
= wx_stack_top
->x3
;
866 *y3
= wx_stack_top
->y3
;
867 *x4
= wx_stack_top
->x4
;
868 *y4
= wx_stack_top
->y4
;
872 static bool wx_spline_add_point(double x
, double y
)
874 wxPoint
*point
= new wxPoint( wxRound(x
), wxRound(y
) );
875 wx_spline_point_list
.Append(point
);
879 static void wx_spline_draw_point_array(wxDC
*dc
)
881 dc
->DrawLines(&wx_spline_point_list
, 0, 0 );
882 wxPointList::compatibility_iterator node
= wx_spline_point_list
.GetFirst();
885 wxPoint
*point
= node
->GetData();
887 wx_spline_point_list
.Erase(node
);
888 node
= wx_spline_point_list
.GetFirst();
892 void wxDCImpl::DoDrawSpline( const wxPointList
*points
)
894 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
897 double cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
;
898 double x1
, y1
, x2
, y2
;
900 wxPointList::compatibility_iterator node
= points
->GetFirst();
905 p
= (wxPoint
*)node
->GetData();
910 node
= node
->GetNext();
915 cx1
= (double)((x1
+ x2
) / 2);
916 cy1
= (double)((y1
+ y2
) / 2);
917 cx2
= (double)((cx1
+ x2
) / 2);
918 cy2
= (double)((cy1
+ y2
) / 2);
920 wx_spline_add_point(x1
, y1
);
922 while ((node
= node
->GetNext())
933 cx4
= (double)(x1
+ x2
) / 2;
934 cy4
= (double)(y1
+ y2
) / 2;
935 cx3
= (double)(x1
+ cx4
) / 2;
936 cy3
= (double)(y1
+ cy4
) / 2;
938 wx_quadratic_spline(cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
);
942 cx2
= (double)(cx1
+ x2
) / 2;
943 cy2
= (double)(cy1
+ y2
) / 2;
946 wx_spline_add_point( cx1
, cy1
);
947 wx_spline_add_point( x2
, y2
);
949 wx_spline_draw_point_array( m_owner
);
952 #endif // wxUSE_SPLINES
956 void wxDCImpl::DoGradientFillLinear(const wxRect
& rect
,
957 const wxColour
& initialColour
,
958 const wxColour
& destColour
,
959 wxDirection nDirection
)
962 wxPen oldPen
= m_pen
;
963 wxBrush oldBrush
= m_brush
;
965 wxUint8 nR1
= initialColour
.Red();
966 wxUint8 nG1
= initialColour
.Green();
967 wxUint8 nB1
= initialColour
.Blue();
968 wxUint8 nR2
= destColour
.Red();
969 wxUint8 nG2
= destColour
.Green();
970 wxUint8 nB2
= destColour
.Blue();
973 if ( nDirection
== wxEAST
|| nDirection
== wxWEST
)
975 wxInt32 x
= rect
.GetWidth();
976 wxInt32 w
= x
; // width of area to shade
977 wxInt32 xDelta
= w
/256; // height of one shade bend
985 nR
= nR1
- (nR1
-nR2
)*(w
-x
)/w
;
987 nR
= nR1
+ (nR2
-nR1
)*(w
-x
)/w
;
990 nG
= nG1
- (nG1
-nG2
)*(w
-x
)/w
;
992 nG
= nG1
+ (nG2
-nG1
)*(w
-x
)/w
;
995 nB
= nB1
- (nB1
-nB2
)*(w
-x
)/w
;
997 nB
= nB1
+ (nB2
-nB1
)*(w
-x
)/w
;
999 wxColour
colour(nR
,nG
,nB
);
1000 SetPen(wxPen(colour
, 1, wxSOLID
));
1001 SetBrush(wxBrush(colour
));
1002 if(nDirection
== wxEAST
)
1003 DoDrawRectangle(rect
.GetRight()-x
-xDelta
+1, rect
.GetTop(),
1004 xDelta
, rect
.GetHeight());
1005 else //nDirection == wxWEST
1006 DoDrawRectangle(rect
.GetLeft()+x
, rect
.GetTop(),
1007 xDelta
, rect
.GetHeight());
1010 else // nDirection == wxNORTH || nDirection == wxSOUTH
1012 wxInt32 y
= rect
.GetHeight();
1013 wxInt32 w
= y
; // height of area to shade
1014 wxInt32 yDelta
= w
/255; // height of one shade bend
1022 nR
= nR1
- (nR1
-nR2
)*(w
-y
)/w
;
1024 nR
= nR1
+ (nR2
-nR1
)*(w
-y
)/w
;
1027 nG
= nG1
- (nG1
-nG2
)*(w
-y
)/w
;
1029 nG
= nG1
+ (nG2
-nG1
)*(w
-y
)/w
;
1032 nB
= nB1
- (nB1
-nB2
)*(w
-y
)/w
;
1034 nB
= nB1
+ (nB2
-nB1
)*(w
-y
)/w
;
1036 wxColour
colour(nR
,nG
,nB
);
1037 SetPen(wxPen(colour
, 1, wxSOLID
));
1038 SetBrush(wxBrush(colour
));
1039 if(nDirection
== wxNORTH
)
1040 DoDrawRectangle(rect
.GetLeft(), rect
.GetTop()+y
,
1041 rect
.GetWidth(), yDelta
);
1042 else //nDirection == wxSOUTH
1043 DoDrawRectangle(rect
.GetLeft(), rect
.GetBottom()-y
-yDelta
+1,
1044 rect
.GetWidth(), yDelta
);
1052 void wxDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1053 const wxColour
& initialColour
,
1054 const wxColour
& destColour
,
1055 const wxPoint
& circleCenter
)
1057 //save the old pen color
1058 wxColour oldPenColour
= m_pen
.GetColour();
1060 wxUint8 nR1
= destColour
.Red();
1061 wxUint8 nG1
= destColour
.Green();
1062 wxUint8 nB1
= destColour
.Blue();
1063 wxUint8 nR2
= initialColour
.Red();
1064 wxUint8 nG2
= initialColour
.Green();
1065 wxUint8 nB2
= initialColour
.Blue();
1070 wxInt32 cx
= rect
.GetWidth() / 2;
1071 wxInt32 cy
= rect
.GetHeight() / 2;
1079 wxInt32 nCircleOffX
= circleCenter
.x
- (rect
.GetWidth() / 2);
1080 wxInt32 nCircleOffY
= circleCenter
.y
- (rect
.GetHeight() / 2);
1082 for ( wxInt32 x
= 0; x
< rect
.GetWidth(); x
++ )
1084 for ( wxInt32 y
= 0; y
< rect
.GetHeight(); y
++ )
1086 //get color difference
1087 wxInt32 nGradient
= ((nRadius
-
1089 pow((double)(x
- cx
- nCircleOffX
), 2) +
1090 pow((double)(y
- cy
- nCircleOffY
), 2)
1091 )) * 100) / nRadius
;
1093 //normalize Gradient
1098 nR
= (wxUint8
)(nR1
+ ((nR2
- nR1
) * nGradient
/ 100));
1099 nG
= (wxUint8
)(nG1
+ ((nG2
- nG1
) * nGradient
/ 100));
1100 nB
= (wxUint8
)(nB1
+ ((nB2
- nB1
) * nGradient
/ 100));
1103 m_pen
.SetColour(wxColour(nR
,nG
,nB
));
1104 DoDrawPoint(x
+ rect
.GetLeft(), y
+ rect
.GetTop());
1107 //return old pen color
1108 m_pen
.SetColour(oldPenColour
);
1111 //-----------------------------------------------------------------------------
1113 //-----------------------------------------------------------------------------
1115 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxObject
)
1117 void wxDC::DrawLabel(const wxString
& text
,
1118 const wxBitmap
& bitmap
,
1122 wxRect
*rectBounding
)
1124 // find the text position
1125 wxCoord widthText
, heightText
, heightLine
;
1126 GetMultiLineTextExtent(text
, &widthText
, &heightText
, &heightLine
);
1128 wxCoord width
, height
;
1131 width
= widthText
+ bitmap
.GetWidth();
1132 height
= bitmap
.GetHeight();
1137 height
= heightText
;
1141 if ( alignment
& wxALIGN_RIGHT
)
1143 x
= rect
.GetRight() - width
;
1145 else if ( alignment
& wxALIGN_CENTRE_HORIZONTAL
)
1147 x
= (rect
.GetLeft() + rect
.GetRight() + 1 - width
) / 2;
1149 else // alignment & wxALIGN_LEFT
1154 if ( alignment
& wxALIGN_BOTTOM
)
1156 y
= rect
.GetBottom() - height
;
1158 else if ( alignment
& wxALIGN_CENTRE_VERTICAL
)
1160 y
= (rect
.GetTop() + rect
.GetBottom() + 1 - height
) / 2;
1162 else // alignment & wxALIGN_TOP
1167 // draw the bitmap first
1173 DrawBitmap(bitmap
, x
, y
, true /* use mask */);
1175 wxCoord offset
= bitmap
.GetWidth() + 4;
1179 y
+= (height
- heightText
) / 2;
1182 // we will draw the underscore under the accel char later
1183 wxCoord startUnderscore
= 0,
1187 // split the string into lines and draw each of them separately
1189 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
1191 if ( *pc
== _T('\n') || pc
== text
.end() )
1193 int xRealStart
= x
; // init it here to avoid compielr warnings
1195 if ( !curLine
.empty() )
1197 // NB: can't test for !(alignment & wxALIGN_LEFT) because
1198 // wxALIGN_LEFT is 0
1199 if ( alignment
& (wxALIGN_RIGHT
| wxALIGN_CENTRE_HORIZONTAL
) )
1202 GetTextExtent(curLine
, &widthLine
, NULL
);
1204 if ( alignment
& wxALIGN_RIGHT
)
1206 xRealStart
+= width
- widthLine
;
1208 else // if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
1210 xRealStart
+= (width
- widthLine
) / 2;
1213 //else: left aligned, nothing to do
1215 DrawText(curLine
, xRealStart
, y
);
1220 // do we have underscore in this line? we can check yUnderscore
1221 // because it is set below to just y + heightLine if we do
1222 if ( y
== yUnderscore
)
1224 // adjust the horz positions to account for the shift
1225 startUnderscore
+= xRealStart
;
1226 endUnderscore
+= xRealStart
;
1229 if ( pc
== text
.end() )
1234 else // not end of line
1236 if ( pc
- text
.begin() == indexAccel
)
1238 // remeber to draw underscore here
1239 GetTextExtent(curLine
, &startUnderscore
, NULL
);
1241 GetTextExtent(curLine
, &endUnderscore
, NULL
);
1243 yUnderscore
= y
+ heightLine
;
1252 // draw the underscore if found
1253 if ( startUnderscore
!= endUnderscore
)
1255 // it should be of the same colour as text
1256 SetPen(wxPen(GetTextForeground(), 0, wxSOLID
));
1260 DrawLine(startUnderscore
, yUnderscore
, endUnderscore
, yUnderscore
);
1263 // return bounding rect if requested
1266 *rectBounding
= wxRect(x
, y
- heightText
, widthText
, heightText
);
1269 CalcBoundingBox(x0
, y0
);
1270 CalcBoundingBox(x0
+ width0
, y0
+ height
);
1273 #if WXWIN_COMPATIBILITY_2_8
1274 // for compatibility with the old code when wxCoord was long everywhere
1275 void wxDC::GetTextExtent(const wxString
& string
,
1278 long *externalLeading
,
1279 const wxFont
*theFont
) const
1281 wxCoord x2
, y2
, descent2
, externalLeading2
;
1282 m_pimpl
->DoGetTextExtent(string
, &x2
, &y2
,
1283 &descent2
, &externalLeading2
,
1290 *descent
= descent2
;
1291 if ( externalLeading
)
1292 *externalLeading
= externalLeading2
;
1295 void wxDC::GetLogicalOrigin(long *x
, long *y
) const
1298 m_pimpl
->DoGetLogicalOrigin(&x2
, &y2
);
1305 void wxDC::GetDeviceOrigin(long *x
, long *y
) const
1308 m_pimpl
->DoGetDeviceOrigin(&x2
, &y2
);
1315 void wxDC::GetClippingBox(long *x
, long *y
, long *w
, long *h
) const
1317 wxCoord xx
,yy
,ww
,hh
;
1318 m_pimpl
->DoGetClippingBox(&xx
, &yy
, &ww
, &hh
);
1325 #endif // WXWIN_COMPATIBILITY_2_8