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/x11/dcclient.h"
65 #include "wx/x11/dcmemory.h"
66 #include "wx/x11/dcscreen.h"
69 //----------------------------------------------------------------------------
71 //----------------------------------------------------------------------------
73 wxDCFactory
*wxDCFactory::m_factory
= NULL
;
75 void wxDCFactory::Set(wxDCFactory
*factory
)
82 wxDCFactory
*wxDCFactory::Get()
85 m_factory
= new wxNativeDCFactory
;
90 class wxDCFactoryCleanupModule
: public wxModule
93 virtual bool OnInit() { return true; }
94 virtual void OnExit() { wxDCFactory::Set(NULL
); }
97 DECLARE_DYNAMIC_CLASS(wxDCFactoryCleanupModule
)
100 IMPLEMENT_DYNAMIC_CLASS(wxDCFactoryCleanupModule
, wxModule
)
102 //-----------------------------------------------------------------------------
104 //-----------------------------------------------------------------------------
106 wxDCImpl
* wxNativeDCFactory::CreateWindowDC( wxWindowDC
*owner
)
108 return new wxWindowDCImpl( owner
);
111 wxDCImpl
* wxNativeDCFactory::CreateWindowDC( wxWindowDC
*owner
, wxWindow
*window
)
113 return new wxWindowDCImpl( owner
, window
);
116 wxDCImpl
* wxNativeDCFactory::CreateClientDC( wxClientDC
*owner
)
118 return new wxClientDCImpl( owner
);
121 wxDCImpl
* wxNativeDCFactory::CreateClientDC( wxClientDC
*owner
, wxWindow
*window
)
123 return new wxClientDCImpl( owner
, window
);
126 wxDCImpl
* wxNativeDCFactory::CreatePaintDC( wxPaintDC
*owner
)
128 return new wxPaintDCImpl( owner
);
131 wxDCImpl
* wxNativeDCFactory::CreatePaintDC( wxPaintDC
*owner
, wxWindow
*window
)
133 return new wxPaintDCImpl( owner
, window
);
136 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
)
138 return new wxMemoryDCImpl( owner
);
141 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
, wxBitmap
&bitmap
)
143 return new wxMemoryDCImpl( owner
, bitmap
);
146 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
, wxDC
*dc
)
148 return new wxMemoryDCImpl( owner
, dc
);
151 wxDCImpl
* wxNativeDCFactory::CreateScreenDC( wxScreenDC
*owner
)
153 return new wxScreenDCImpl( owner
);
156 #if wxUSE_PRINTING_ARCHITECTURE
157 wxDCImpl
*wxNativeDCFactory::CreatePrinterDC( wxPrinterDC
*owner
, const wxPrintData
&data
)
159 wxPrintFactory
*factory
= wxPrintFactory::GetFactory();
160 return factory
->CreatePrinterDCImpl( owner
, data
);
164 //-----------------------------------------------------------------------------
166 //-----------------------------------------------------------------------------
168 IMPLEMENT_ABSTRACT_CLASS(wxWindowDC
, wxDC
)
170 wxWindowDC::wxWindowDC(wxWindow
*win
)
171 : wxDC(wxDCFactory::Get()->CreateWindowDC(this, win
))
175 //-----------------------------------------------------------------------------
177 //-----------------------------------------------------------------------------
179 IMPLEMENT_ABSTRACT_CLASS(wxClientDC
, wxWindowDC
)
181 wxClientDC::wxClientDC(wxWindow
*win
)
182 : wxWindowDC(wxDCFactory::Get()->CreateClientDC(this, win
))
186 //-----------------------------------------------------------------------------
188 //-----------------------------------------------------------------------------
190 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC
, wxDC
)
192 wxMemoryDC::wxMemoryDC()
193 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this))
197 wxMemoryDC::wxMemoryDC(wxBitmap
& bitmap
)
198 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, bitmap
))
202 wxMemoryDC::wxMemoryDC(wxDC
*dc
)
203 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, dc
))
207 void wxMemoryDC::SelectObject(wxBitmap
& bmp
)
209 // make sure that the given wxBitmap is not sharing its data with other
210 // wxBitmap instances as its contents will be modified by any drawing
211 // operation done on this DC
215 GetImpl()->DoSelect(bmp
);
218 void wxMemoryDC::SelectObjectAsSource(const wxBitmap
& bmp
)
220 GetImpl()->DoSelect(bmp
);
223 const wxBitmap
& wxMemoryDC::GetSelectedBitmap() const
225 return GetImpl()->GetSelectedBitmap();
228 wxBitmap
& wxMemoryDC::GetSelectedBitmap()
230 return GetImpl()->GetSelectedBitmap();
234 //-----------------------------------------------------------------------------
236 //-----------------------------------------------------------------------------
238 IMPLEMENT_ABSTRACT_CLASS(wxPaintDC
, wxClientDC
)
240 wxPaintDC::wxPaintDC(wxWindow
*win
)
241 : wxClientDC(wxDCFactory::Get()->CreatePaintDC(this, win
))
245 //-----------------------------------------------------------------------------
247 //-----------------------------------------------------------------------------
249 IMPLEMENT_DYNAMIC_CLASS(wxScreenDC
, wxWindowDC
)
251 wxScreenDC::wxScreenDC()
252 : wxDC(wxDCFactory::Get()->CreateScreenDC(this))
256 //-----------------------------------------------------------------------------
258 //-----------------------------------------------------------------------------
260 #if wxUSE_PRINTING_ARCHITECTURE
262 IMPLEMENT_DYNAMIC_CLASS(wxPrinterDC
, wxDC
)
264 wxPrinterDC::wxPrinterDC()
265 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, wxPrintData()))
269 wxPrinterDC::wxPrinterDC(const wxPrintData
& data
)
270 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, data
))
274 wxRect
wxPrinterDC::GetPaperRect()
276 return GetImpl()->GetPaperRect();
279 int wxPrinterDC::GetResolution()
281 return GetImpl()->GetResolution();
284 #endif // wxUSE_PRINTING_ARCHITECTURE
286 //-----------------------------------------------------------------------------
288 //-----------------------------------------------------------------------------
290 IMPLEMENT_ABSTRACT_CLASS(wxDCImpl
, wxObject
)
292 wxDCImpl::wxDCImpl( wxDC
*owner
)
294 , m_colour(wxColourDisplay())
298 , m_isBBoxValid(false)
299 , m_logicalOriginX(0), m_logicalOriginY(0)
300 , m_deviceOriginX(0), m_deviceOriginY(0)
301 , m_deviceLocalOriginX(0), m_deviceLocalOriginY(0)
302 , m_logicalScaleX(1.0), m_logicalScaleY(1.0)
303 , m_userScaleX(1.0), m_userScaleY(1.0)
304 , m_scaleX(1.0), m_scaleY(1.0)
305 , m_signX(1), m_signY(1)
306 , m_minX(0), m_minY(0), m_maxX(0), m_maxY(0)
307 , m_clipX1(0), m_clipY1(0), m_clipX2(0), m_clipY2(0)
308 , m_logicalFunction(wxCOPY
)
309 , m_backgroundMode(wxTRANSPARENT
)
310 , m_mappingMode(wxMM_TEXT
)
313 , m_backgroundBrush(*wxTRANSPARENT_BRUSH
)
314 , m_textForegroundColour(*wxBLACK
)
315 , m_textBackgroundColour(*wxWHITE
)
319 , m_hasCustomPalette(false)
320 #endif // wxUSE_PALETTE
324 m_mm_to_pix_x
= (double)wxGetDisplaySize().GetWidth() /
325 (double)wxGetDisplaySizeMM().GetWidth();
326 m_mm_to_pix_y
= (double)wxGetDisplaySize().GetHeight() /
327 (double)wxGetDisplaySizeMM().GetHeight();
333 wxDCImpl::~wxDCImpl()
337 // ----------------------------------------------------------------------------
338 // coordinate conversions and transforms
339 // ----------------------------------------------------------------------------
341 wxCoord
wxDCImpl::DeviceToLogicalX(wxCoord x
) const
343 return wxRound((double)(x
- m_deviceOriginX
- m_deviceLocalOriginX
) / m_scaleX
) * m_signX
+ m_logicalOriginX
;
346 wxCoord
wxDCImpl::DeviceToLogicalY(wxCoord y
) const
348 return wxRound((double)(y
- m_deviceOriginY
- m_deviceLocalOriginY
) / m_scaleY
) * m_signY
+ m_logicalOriginY
;
351 wxCoord
wxDCImpl::DeviceToLogicalXRel(wxCoord x
) const
353 return wxRound((double)(x
) / m_scaleX
);
356 wxCoord
wxDCImpl::DeviceToLogicalYRel(wxCoord y
) const
358 return wxRound((double)(y
) / m_scaleY
);
361 wxCoord
wxDCImpl::LogicalToDeviceX(wxCoord x
) const
363 return wxRound((double)(x
- m_logicalOriginX
) * m_scaleX
) * m_signX
+ m_deviceOriginX
* m_signY
+ m_deviceLocalOriginX
;
366 wxCoord
wxDCImpl::LogicalToDeviceY(wxCoord y
) const
368 return wxRound((double)(y
- m_logicalOriginY
) * m_scaleY
) * m_signY
+ m_deviceOriginY
* m_signY
+ m_deviceLocalOriginY
;
371 wxCoord
wxDCImpl::LogicalToDeviceXRel(wxCoord x
) const
373 return wxRound((double)(x
) * m_scaleX
);
376 wxCoord
wxDCImpl::LogicalToDeviceYRel(wxCoord y
) const
378 return wxRound((double)(y
) * m_scaleY
);
381 void wxDCImpl::ComputeScaleAndOrigin()
383 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
384 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
387 void wxDCImpl::SetMapMode( int mode
)
392 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
395 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
398 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
401 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
405 SetLogicalScale( 1.0, 1.0 );
408 m_mappingMode
= mode
;
411 void wxDCImpl::SetUserScale( double x
, double y
)
413 // allow negative ? -> no
416 ComputeScaleAndOrigin();
419 void wxDCImpl::SetLogicalScale( double x
, double y
)
424 ComputeScaleAndOrigin();
427 void wxDCImpl::SetLogicalOrigin( wxCoord x
, wxCoord y
)
429 m_logicalOriginX
= x
* m_signX
;
430 m_logicalOriginY
= y
* m_signY
;
431 ComputeScaleAndOrigin();
434 void wxDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
438 ComputeScaleAndOrigin();
441 void wxDCImpl::SetDeviceLocalOrigin( wxCoord x
, wxCoord y
)
443 m_deviceLocalOriginX
= x
;
444 m_deviceLocalOriginY
= y
;
445 ComputeScaleAndOrigin();
448 void wxDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
450 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
451 // wxWidgets 2.9: no longer override it
452 m_signX
= (xLeftRight
? 1 : -1);
453 m_signY
= (yBottomUp
? -1 : 1);
454 ComputeScaleAndOrigin();
458 // Each element of the widths array will be the width of the string up to and
459 // including the corresponding character in text. This is the generic
460 // implementation, the port-specific classes should do this with native APIs
461 // if available and if faster. Note: pango_layout_index_to_pos is much slower
462 // than calling GetTextExtent!!
469 FontWidthCache() : m_scaleX(1), m_widths(NULL
) { }
470 ~FontWidthCache() { delete []m_widths
; }
475 m_widths
= new int[FWC_SIZE
];
477 memset(m_widths
, 0, sizeof(int)*FWC_SIZE
);
485 static FontWidthCache s_fontWidthCache
;
487 bool wxDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
491 const size_t len
= text
.length();
495 // reset the cache if font or horizontal scale have changed
496 if ( !s_fontWidthCache
.m_widths
||
497 !wxIsSameDouble(s_fontWidthCache
.m_scaleX
, m_scaleX
) ||
498 (s_fontWidthCache
.m_font
!= GetFont()) )
500 s_fontWidthCache
.Reset();
501 s_fontWidthCache
.m_font
= GetFont();
502 s_fontWidthCache
.m_scaleX
= m_scaleX
;
505 // Calculate the position of each character based on the widths of
506 // the previous characters
508 for ( size_t i
= 0; i
< len
; i
++ )
510 const wxChar c
= text
[i
];
511 unsigned int c_int
= (unsigned int)c
;
513 if ((c_int
< FWC_SIZE
) && (s_fontWidthCache
.m_widths
[c_int
] != 0))
515 w
= s_fontWidthCache
.m_widths
[c_int
];
519 DoGetTextExtent(c
, &w
, &h
);
520 if (c_int
< FWC_SIZE
)
521 s_fontWidthCache
.m_widths
[c_int
] = w
;
525 widths
[i
] = totalWidth
;
531 void wxDCImpl::GetMultiLineTextExtent(const wxString
& text
,
535 const wxFont
*font
) const
537 wxCoord widthTextMax
= 0, widthLine
,
538 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
541 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
543 if ( pc
== text
.end() || *pc
== _T('\n') )
545 if ( curLine
.empty() )
547 // we can't use GetTextExtent - it will return 0 for both width
548 // and height and an empty line should count in height
551 // assume that this line has the same height as the previous
553 if ( !heightLineDefault
)
554 heightLineDefault
= heightLine
;
556 if ( !heightLineDefault
)
558 // but we don't know it yet - choose something reasonable
559 DoGetTextExtent(_T("W"), NULL
, &heightLineDefault
,
563 heightTextTotal
+= heightLineDefault
;
567 DoGetTextExtent(curLine
, &widthLine
, &heightLine
,
569 if ( widthLine
> widthTextMax
)
570 widthTextMax
= widthLine
;
571 heightTextTotal
+= heightLine
;
574 if ( pc
== text
.end() )
592 *y
= heightTextTotal
;
597 void wxDCImpl::DoDrawCheckMark(wxCoord x1
, wxCoord y1
,
598 wxCoord width
, wxCoord height
)
600 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
602 wxCoord x2
= x1
+ width
,
605 // the pen width is calibrated to give 3 for width == height == 10
606 wxDCPenChanger
pen( *m_owner
, wxPen(GetTextForeground(), (width
+ height
+ 1)/7));
608 // we're drawing a scaled version of wx/generic/tick.xpm here
609 wxCoord x3
= x1
+ (4*width
) / 10, // x of the tick bottom
610 y3
= y1
+ height
/ 2; // y of the left tick branch
611 DoDrawLine(x1
, y3
, x3
, y2
);
612 DoDrawLine(x3
, y2
, x2
, y1
);
614 CalcBoundingBox(x1
, y1
);
615 CalcBoundingBox(x2
, y2
);
619 wxDCImpl::DoStretchBlit(wxCoord xdest
, wxCoord ydest
,
620 wxCoord dstWidth
, wxCoord dstHeight
,
622 wxCoord xsrc
, wxCoord ysrc
,
623 wxCoord srcWidth
, wxCoord srcHeight
,
629 wxCHECK_MSG( srcWidth
&& srcHeight
&& dstWidth
&& dstHeight
, false,
630 _T("invalid blit size") );
632 // emulate the stretching by modifying the DC scale
633 double xscale
= (double)srcWidth
/dstWidth
,
634 yscale
= (double)srcHeight
/dstHeight
;
636 double xscaleOld
, yscaleOld
;
637 GetUserScale(&xscaleOld
, &yscaleOld
);
638 SetUserScale(xscaleOld
/xscale
, yscaleOld
/yscale
);
640 bool rc
= DoBlit(wxCoord(xdest
*xscale
), wxCoord(ydest
*yscale
),
641 wxCoord(dstWidth
*xscale
), wxCoord(dstHeight
*yscale
),
643 xsrc
, ysrc
, rop
, useMask
, xsrcMask
, ysrcMask
);
645 SetUserScale(xscaleOld
, yscaleOld
);
650 void wxDCImpl::DrawLines(const wxPointList
*list
, wxCoord xoffset
, wxCoord yoffset
)
652 int n
= list
->GetCount();
653 wxPoint
*points
= new wxPoint
[n
];
656 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
658 wxPoint
*point
= node
->GetData();
659 points
[i
].x
= point
->x
;
660 points
[i
].y
= point
->y
;
663 DoDrawLines(n
, points
, xoffset
, yoffset
);
668 void wxDCImpl::DrawPolygon(const wxPointList
*list
,
669 wxCoord xoffset
, wxCoord yoffset
,
672 int n
= list
->GetCount();
673 wxPoint
*points
= new wxPoint
[n
];
676 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
678 wxPoint
*point
= node
->GetData();
679 points
[i
].x
= point
->x
;
680 points
[i
].y
= point
->y
;
683 DoDrawPolygon(n
, points
, xoffset
, yoffset
, fillStyle
);
689 wxDCImpl::DoDrawPolyPolygon(int n
,
692 wxCoord xoffset
, wxCoord yoffset
,
697 DoDrawPolygon(count
[0], points
, xoffset
, yoffset
, fillStyle
);
705 for (i
= j
= lastOfs
= 0; i
< n
; i
++)
710 pts
= new wxPoint
[j
+n
-1];
711 for (i
= 0; i
< j
; i
++)
713 for (i
= 2; i
<= n
; i
++)
715 lastOfs
-= count
[n
-i
];
716 pts
[j
++] = pts
[lastOfs
];
720 SetPen(wxPen(*wxBLACK
, 0, wxTRANSPARENT
));
721 DoDrawPolygon(j
, pts
, xoffset
, yoffset
, fillStyle
);
723 for (i
= j
= 0; i
< n
; i
++)
725 DoDrawLines(count
[i
], pts
+j
, xoffset
, yoffset
);
733 void wxDCImpl::DoDrawSpline(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
, wxCoord x3
, wxCoord y3
)
735 wxPointList point_list
;
737 wxPoint
*point1
= new wxPoint
;
738 point1
->x
= x1
; point1
->y
= y1
;
739 point_list
.Append( point1
);
741 wxPoint
*point2
= new wxPoint
;
742 point2
->x
= x2
; point2
->y
= y2
;
743 point_list
.Append( point2
);
745 wxPoint
*point3
= new wxPoint
;
746 point3
->x
= x3
; point3
->y
= y3
;
747 point_list
.Append( point3
);
749 DoDrawSpline(&point_list
);
751 for( wxPointList::compatibility_iterator node
= point_list
.GetFirst(); node
; node
= node
->GetNext() )
753 wxPoint
*p
= node
->GetData();
758 void wxDCImpl::DoDrawSpline(int n
, wxPoint points
[])
761 for (int i
=0; i
< n
; i
++)
762 list
.Append( &points
[i
] );
767 // ----------------------------------- spline code ----------------------------------------
769 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
,
770 double a3
, double b3
, double a4
, double b4
);
771 void wx_clear_stack();
772 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
, double *x3
,
773 double *y3
, double *x4
, double *y4
);
774 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
,
775 double x4
, double y4
);
776 static bool wx_spline_add_point(double x
, double y
);
777 static void wx_spline_draw_point_array(wxDC
*dc
);
779 wxPointList wx_spline_point_list
;
781 #define half(z1, z2) ((z1+z2)/2.0)
784 /* iterative version */
786 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
, double a3
, double b3
, double a4
,
789 register double xmid
, ymid
;
790 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
793 wx_spline_push(a1
, b1
, a2
, b2
, a3
, b3
, a4
, b4
);
795 while (wx_spline_pop(&x1
, &y1
, &x2
, &y2
, &x3
, &y3
, &x4
, &y4
)) {
796 xmid
= (double)half(x2
, x3
);
797 ymid
= (double)half(y2
, y3
);
798 if (fabs(x1
- xmid
) < THRESHOLD
&& fabs(y1
- ymid
) < THRESHOLD
&&
799 fabs(xmid
- x4
) < THRESHOLD
&& fabs(ymid
- y4
) < THRESHOLD
) {
800 wx_spline_add_point( x1
, y1
);
801 wx_spline_add_point( xmid
, ymid
);
803 wx_spline_push(xmid
, ymid
, (double)half(xmid
, x3
), (double)half(ymid
, y3
),
804 (double)half(x3
, x4
), (double)half(y3
, y4
), x4
, y4
);
805 wx_spline_push(x1
, y1
, (double)half(x1
, x2
), (double)half(y1
, y2
),
806 (double)half(x2
, xmid
), (double)half(y2
, ymid
), xmid
, ymid
);
811 /* utilities used by spline drawing routines */
813 typedef struct wx_spline_stack_struct
{
814 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
817 #define SPLINE_STACK_DEPTH 20
818 static Stack wx_spline_stack
[SPLINE_STACK_DEPTH
];
819 static Stack
*wx_stack_top
;
820 static int wx_stack_count
;
822 void wx_clear_stack()
824 wx_stack_top
= wx_spline_stack
;
828 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
, double x4
, double y4
)
830 wx_stack_top
->x1
= x1
;
831 wx_stack_top
->y1
= y1
;
832 wx_stack_top
->x2
= x2
;
833 wx_stack_top
->y2
= y2
;
834 wx_stack_top
->x3
= x3
;
835 wx_stack_top
->y3
= y3
;
836 wx_stack_top
->x4
= x4
;
837 wx_stack_top
->y4
= y4
;
842 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
,
843 double *x3
, double *y3
, double *x4
, double *y4
)
845 if (wx_stack_count
== 0)
849 *x1
= wx_stack_top
->x1
;
850 *y1
= wx_stack_top
->y1
;
851 *x2
= wx_stack_top
->x2
;
852 *y2
= wx_stack_top
->y2
;
853 *x3
= wx_stack_top
->x3
;
854 *y3
= wx_stack_top
->y3
;
855 *x4
= wx_stack_top
->x4
;
856 *y4
= wx_stack_top
->y4
;
860 static bool wx_spline_add_point(double x
, double y
)
862 wxPoint
*point
= new wxPoint( wxRound(x
), wxRound(y
) );
863 wx_spline_point_list
.Append(point
);
867 static void wx_spline_draw_point_array(wxDC
*dc
)
869 dc
->DrawLines(&wx_spline_point_list
, 0, 0 );
870 wxPointList::compatibility_iterator node
= wx_spline_point_list
.GetFirst();
873 wxPoint
*point
= node
->GetData();
875 wx_spline_point_list
.Erase(node
);
876 node
= wx_spline_point_list
.GetFirst();
880 void wxDCImpl::DoDrawSpline( const wxPointList
*points
)
882 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
885 double cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
;
886 double x1
, y1
, x2
, y2
;
888 wxPointList::compatibility_iterator node
= points
->GetFirst();
893 p
= (wxPoint
*)node
->GetData();
898 node
= node
->GetNext();
903 cx1
= (double)((x1
+ x2
) / 2);
904 cy1
= (double)((y1
+ y2
) / 2);
905 cx2
= (double)((cx1
+ x2
) / 2);
906 cy2
= (double)((cy1
+ y2
) / 2);
908 wx_spline_add_point(x1
, y1
);
910 while ((node
= node
->GetNext())
921 cx4
= (double)(x1
+ x2
) / 2;
922 cy4
= (double)(y1
+ y2
) / 2;
923 cx3
= (double)(x1
+ cx4
) / 2;
924 cy3
= (double)(y1
+ cy4
) / 2;
926 wx_quadratic_spline(cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
);
930 cx2
= (double)(cx1
+ x2
) / 2;
931 cy2
= (double)(cy1
+ y2
) / 2;
934 wx_spline_add_point( cx1
, cy1
);
935 wx_spline_add_point( x2
, y2
);
937 wx_spline_draw_point_array( m_owner
);
940 #endif // wxUSE_SPLINES
944 void wxDCImpl::DoGradientFillLinear(const wxRect
& rect
,
945 const wxColour
& initialColour
,
946 const wxColour
& destColour
,
947 wxDirection nDirection
)
950 wxPen oldPen
= m_pen
;
951 wxBrush oldBrush
= m_brush
;
953 wxUint8 nR1
= initialColour
.Red();
954 wxUint8 nG1
= initialColour
.Green();
955 wxUint8 nB1
= initialColour
.Blue();
956 wxUint8 nR2
= destColour
.Red();
957 wxUint8 nG2
= destColour
.Green();
958 wxUint8 nB2
= destColour
.Blue();
961 if ( nDirection
== wxEAST
|| nDirection
== wxWEST
)
963 wxInt32 x
= rect
.GetWidth();
964 wxInt32 w
= x
; // width of area to shade
965 wxInt32 xDelta
= w
/256; // height of one shade bend
973 nR
= nR1
- (nR1
-nR2
)*(w
-x
)/w
;
975 nR
= nR1
+ (nR2
-nR1
)*(w
-x
)/w
;
978 nG
= nG1
- (nG1
-nG2
)*(w
-x
)/w
;
980 nG
= nG1
+ (nG2
-nG1
)*(w
-x
)/w
;
983 nB
= nB1
- (nB1
-nB2
)*(w
-x
)/w
;
985 nB
= nB1
+ (nB2
-nB1
)*(w
-x
)/w
;
987 wxColour
colour(nR
,nG
,nB
);
988 SetPen(wxPen(colour
, 1, wxSOLID
));
989 SetBrush(wxBrush(colour
));
990 if(nDirection
== wxEAST
)
991 DoDrawRectangle(rect
.GetRight()-x
-xDelta
+1, rect
.GetTop(),
992 xDelta
, rect
.GetHeight());
993 else //nDirection == wxWEST
994 DoDrawRectangle(rect
.GetLeft()+x
, rect
.GetTop(),
995 xDelta
, rect
.GetHeight());
998 else // nDirection == wxNORTH || nDirection == wxSOUTH
1000 wxInt32 y
= rect
.GetHeight();
1001 wxInt32 w
= y
; // height of area to shade
1002 wxInt32 yDelta
= w
/255; // height of one shade bend
1010 nR
= nR1
- (nR1
-nR2
)*(w
-y
)/w
;
1012 nR
= nR1
+ (nR2
-nR1
)*(w
-y
)/w
;
1015 nG
= nG1
- (nG1
-nG2
)*(w
-y
)/w
;
1017 nG
= nG1
+ (nG2
-nG1
)*(w
-y
)/w
;
1020 nB
= nB1
- (nB1
-nB2
)*(w
-y
)/w
;
1022 nB
= nB1
+ (nB2
-nB1
)*(w
-y
)/w
;
1024 wxColour
colour(nR
,nG
,nB
);
1025 SetPen(wxPen(colour
, 1, wxSOLID
));
1026 SetBrush(wxBrush(colour
));
1027 if(nDirection
== wxNORTH
)
1028 DoDrawRectangle(rect
.GetLeft(), rect
.GetTop()+y
,
1029 rect
.GetWidth(), yDelta
);
1030 else //nDirection == wxSOUTH
1031 DoDrawRectangle(rect
.GetLeft(), rect
.GetBottom()-y
-yDelta
+1,
1032 rect
.GetWidth(), yDelta
);
1040 void wxDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1041 const wxColour
& initialColour
,
1042 const wxColour
& destColour
,
1043 const wxPoint
& circleCenter
)
1045 //save the old pen color
1046 wxColour oldPenColour
= m_pen
.GetColour();
1048 wxUint8 nR1
= destColour
.Red();
1049 wxUint8 nG1
= destColour
.Green();
1050 wxUint8 nB1
= destColour
.Blue();
1051 wxUint8 nR2
= initialColour
.Red();
1052 wxUint8 nG2
= initialColour
.Green();
1053 wxUint8 nB2
= initialColour
.Blue();
1058 wxInt32 cx
= rect
.GetWidth() / 2;
1059 wxInt32 cy
= rect
.GetHeight() / 2;
1067 wxInt32 nCircleOffX
= circleCenter
.x
- (rect
.GetWidth() / 2);
1068 wxInt32 nCircleOffY
= circleCenter
.y
- (rect
.GetHeight() / 2);
1070 for ( wxInt32 x
= 0; x
< rect
.GetWidth(); x
++ )
1072 for ( wxInt32 y
= 0; y
< rect
.GetHeight(); y
++ )
1074 //get color difference
1075 wxInt32 nGradient
= ((nRadius
-
1077 pow((double)(x
- cx
- nCircleOffX
), 2) +
1078 pow((double)(y
- cy
- nCircleOffY
), 2)
1079 )) * 100) / nRadius
;
1081 //normalize Gradient
1086 nR
= (wxUint8
)(nR1
+ ((nR2
- nR1
) * nGradient
/ 100));
1087 nG
= (wxUint8
)(nG1
+ ((nG2
- nG1
) * nGradient
/ 100));
1088 nB
= (wxUint8
)(nB1
+ ((nB2
- nB1
) * nGradient
/ 100));
1091 m_pen
.SetColour(wxColour(nR
,nG
,nB
));
1092 DoDrawPoint(x
+ rect
.GetLeft(), y
+ rect
.GetTop());
1095 //return old pen color
1096 m_pen
.SetColour(oldPenColour
);
1099 //-----------------------------------------------------------------------------
1101 //-----------------------------------------------------------------------------
1103 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxObject
)
1105 void wxDC::DrawLabel(const wxString
& text
,
1106 const wxBitmap
& bitmap
,
1110 wxRect
*rectBounding
)
1112 // find the text position
1113 wxCoord widthText
, heightText
, heightLine
;
1114 GetMultiLineTextExtent(text
, &widthText
, &heightText
, &heightLine
);
1116 wxCoord width
, height
;
1119 width
= widthText
+ bitmap
.GetWidth();
1120 height
= bitmap
.GetHeight();
1125 height
= heightText
;
1129 if ( alignment
& wxALIGN_RIGHT
)
1131 x
= rect
.GetRight() - width
;
1133 else if ( alignment
& wxALIGN_CENTRE_HORIZONTAL
)
1135 x
= (rect
.GetLeft() + rect
.GetRight() + 1 - width
) / 2;
1137 else // alignment & wxALIGN_LEFT
1142 if ( alignment
& wxALIGN_BOTTOM
)
1144 y
= rect
.GetBottom() - height
;
1146 else if ( alignment
& wxALIGN_CENTRE_VERTICAL
)
1148 y
= (rect
.GetTop() + rect
.GetBottom() + 1 - height
) / 2;
1150 else // alignment & wxALIGN_TOP
1155 // draw the bitmap first
1161 DrawBitmap(bitmap
, x
, y
, true /* use mask */);
1163 wxCoord offset
= bitmap
.GetWidth() + 4;
1167 y
+= (height
- heightText
) / 2;
1170 // we will draw the underscore under the accel char later
1171 wxCoord startUnderscore
= 0,
1175 // split the string into lines and draw each of them separately
1177 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
1179 if ( *pc
== _T('\n') || pc
== text
.end() )
1181 int xRealStart
= x
; // init it here to avoid compielr warnings
1183 if ( !curLine
.empty() )
1185 // NB: can't test for !(alignment & wxALIGN_LEFT) because
1186 // wxALIGN_LEFT is 0
1187 if ( alignment
& (wxALIGN_RIGHT
| wxALIGN_CENTRE_HORIZONTAL
) )
1190 GetTextExtent(curLine
, &widthLine
, NULL
);
1192 if ( alignment
& wxALIGN_RIGHT
)
1194 xRealStart
+= width
- widthLine
;
1196 else // if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
1198 xRealStart
+= (width
- widthLine
) / 2;
1201 //else: left aligned, nothing to do
1203 DrawText(curLine
, xRealStart
, y
);
1208 // do we have underscore in this line? we can check yUnderscore
1209 // because it is set below to just y + heightLine if we do
1210 if ( y
== yUnderscore
)
1212 // adjust the horz positions to account for the shift
1213 startUnderscore
+= xRealStart
;
1214 endUnderscore
+= xRealStart
;
1217 if ( pc
== text
.end() )
1222 else // not end of line
1224 if ( pc
- text
.begin() == indexAccel
)
1226 // remeber to draw underscore here
1227 GetTextExtent(curLine
, &startUnderscore
, NULL
);
1229 GetTextExtent(curLine
, &endUnderscore
, NULL
);
1231 yUnderscore
= y
+ heightLine
;
1240 // draw the underscore if found
1241 if ( startUnderscore
!= endUnderscore
)
1243 // it should be of the same colour as text
1244 SetPen(wxPen(GetTextForeground(), 0, wxSOLID
));
1248 DrawLine(startUnderscore
, yUnderscore
, endUnderscore
, yUnderscore
);
1251 // return bounding rect if requested
1254 *rectBounding
= wxRect(x
, y
- heightText
, widthText
, heightText
);
1257 CalcBoundingBox(x0
, y0
);
1258 CalcBoundingBox(x0
+ width0
, y0
+ height
);
1261 #if WXWIN_COMPATIBILITY_2_8
1262 // for compatibility with the old code when wxCoord was long everywhere
1263 void wxDC::GetTextExtent(const wxString
& string
,
1266 long *externalLeading
,
1267 const wxFont
*theFont
) const
1269 wxCoord x2
, y2
, descent2
, externalLeading2
;
1270 m_pimpl
->DoGetTextExtent(string
, &x2
, &y2
,
1271 &descent2
, &externalLeading2
,
1278 *descent
= descent2
;
1279 if ( externalLeading
)
1280 *externalLeading
= externalLeading2
;
1283 void wxDC::GetLogicalOrigin(long *x
, long *y
) const
1286 m_pimpl
->DoGetLogicalOrigin(&x2
, &y2
);
1293 void wxDC::GetDeviceOrigin(long *x
, long *y
) const
1296 m_pimpl
->DoGetDeviceOrigin(&x2
, &y2
);
1303 void wxDC::GetClippingBox(long *x
, long *y
, long *w
, long *h
) const
1305 wxCoord xx
,yy
,ww
,hh
;
1306 m_pimpl
->DoGetClippingBox(&xx
, &yy
, &ww
, &hh
);
1313 #endif // WXWIN_COMPATIBILITY_2_8