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"
82 #include "wx/palmos/dcclient.h"
83 #include "wx/palmos/dcmemory.h"
84 #include "wx/palmos/dcscreen.h"
87 //----------------------------------------------------------------------------
89 //----------------------------------------------------------------------------
91 wxDCFactory
*wxDCFactory::m_factory
= NULL
;
93 void wxDCFactory::Set(wxDCFactory
*factory
)
100 wxDCFactory
*wxDCFactory::Get()
103 m_factory
= new wxNativeDCFactory
;
108 class wxDCFactoryCleanupModule
: public wxModule
111 virtual bool OnInit() { return true; }
112 virtual void OnExit() { wxDCFactory::Set(NULL
); }
115 DECLARE_DYNAMIC_CLASS(wxDCFactoryCleanupModule
)
118 IMPLEMENT_DYNAMIC_CLASS(wxDCFactoryCleanupModule
, wxModule
)
120 //-----------------------------------------------------------------------------
122 //-----------------------------------------------------------------------------
124 wxDCImpl
* wxNativeDCFactory::CreateWindowDC( wxWindowDC
*owner
)
126 return new wxWindowDCImpl( owner
);
129 wxDCImpl
* wxNativeDCFactory::CreateWindowDC( wxWindowDC
*owner
, wxWindow
*window
)
131 return new wxWindowDCImpl( owner
, window
);
134 wxDCImpl
* wxNativeDCFactory::CreateClientDC( wxClientDC
*owner
)
136 return new wxClientDCImpl( owner
);
139 wxDCImpl
* wxNativeDCFactory::CreateClientDC( wxClientDC
*owner
, wxWindow
*window
)
141 return new wxClientDCImpl( owner
, window
);
144 wxDCImpl
* wxNativeDCFactory::CreatePaintDC( wxPaintDC
*owner
)
146 return new wxPaintDCImpl( owner
);
149 wxDCImpl
* wxNativeDCFactory::CreatePaintDC( wxPaintDC
*owner
, wxWindow
*window
)
151 return new wxPaintDCImpl( owner
, window
);
154 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
)
156 return new wxMemoryDCImpl( owner
);
159 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
, wxBitmap
&bitmap
)
161 return new wxMemoryDCImpl( owner
, bitmap
);
164 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
, wxDC
*dc
)
166 return new wxMemoryDCImpl( owner
, dc
);
169 wxDCImpl
* wxNativeDCFactory::CreateScreenDC( wxScreenDC
*owner
)
171 return new wxScreenDCImpl( owner
);
174 #if wxUSE_PRINTING_ARCHITECTURE
175 wxDCImpl
*wxNativeDCFactory::CreatePrinterDC( wxPrinterDC
*owner
, const wxPrintData
&data
)
177 wxPrintFactory
*factory
= wxPrintFactory::GetFactory();
178 return factory
->CreatePrinterDCImpl( owner
, data
);
182 //-----------------------------------------------------------------------------
184 //-----------------------------------------------------------------------------
186 IMPLEMENT_ABSTRACT_CLASS(wxWindowDC
, wxDC
)
188 wxWindowDC::wxWindowDC(wxWindow
*win
)
189 : wxDC(wxDCFactory::Get()->CreateWindowDC(this, win
))
193 //-----------------------------------------------------------------------------
195 //-----------------------------------------------------------------------------
197 IMPLEMENT_ABSTRACT_CLASS(wxClientDC
, wxWindowDC
)
199 wxClientDC::wxClientDC(wxWindow
*win
)
200 : wxWindowDC(wxDCFactory::Get()->CreateClientDC(this, win
))
204 //-----------------------------------------------------------------------------
206 //-----------------------------------------------------------------------------
208 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC
, wxDC
)
210 wxMemoryDC::wxMemoryDC()
211 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this))
215 wxMemoryDC::wxMemoryDC(wxBitmap
& bitmap
)
216 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, bitmap
))
220 wxMemoryDC::wxMemoryDC(wxDC
*dc
)
221 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, dc
))
225 void wxMemoryDC::SelectObject(wxBitmap
& bmp
)
227 // make sure that the given wxBitmap is not sharing its data with other
228 // wxBitmap instances as its contents will be modified by any drawing
229 // operation done on this DC
233 GetImpl()->DoSelect(bmp
);
236 void wxMemoryDC::SelectObjectAsSource(const wxBitmap
& bmp
)
238 GetImpl()->DoSelect(bmp
);
241 const wxBitmap
& wxMemoryDC::GetSelectedBitmap() const
243 return GetImpl()->GetSelectedBitmap();
246 wxBitmap
& wxMemoryDC::GetSelectedBitmap()
248 return GetImpl()->GetSelectedBitmap();
252 //-----------------------------------------------------------------------------
254 //-----------------------------------------------------------------------------
256 IMPLEMENT_ABSTRACT_CLASS(wxPaintDC
, wxClientDC
)
258 wxPaintDC::wxPaintDC(wxWindow
*win
)
259 : wxClientDC(wxDCFactory::Get()->CreatePaintDC(this, win
))
263 //-----------------------------------------------------------------------------
265 //-----------------------------------------------------------------------------
267 IMPLEMENT_DYNAMIC_CLASS(wxScreenDC
, wxWindowDC
)
269 wxScreenDC::wxScreenDC()
270 : wxDC(wxDCFactory::Get()->CreateScreenDC(this))
274 //-----------------------------------------------------------------------------
276 //-----------------------------------------------------------------------------
278 #if wxUSE_PRINTING_ARCHITECTURE
280 IMPLEMENT_DYNAMIC_CLASS(wxPrinterDC
, wxDC
)
282 wxPrinterDC::wxPrinterDC()
283 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, wxPrintData()))
287 wxPrinterDC::wxPrinterDC(const wxPrintData
& data
)
288 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, data
))
292 wxRect
wxPrinterDC::GetPaperRect()
294 return GetImpl()->GetPaperRect();
297 int wxPrinterDC::GetResolution()
299 return GetImpl()->GetResolution();
302 #endif // wxUSE_PRINTING_ARCHITECTURE
304 //-----------------------------------------------------------------------------
306 //-----------------------------------------------------------------------------
308 IMPLEMENT_ABSTRACT_CLASS(wxDCImpl
, wxObject
)
310 wxDCImpl::wxDCImpl( wxDC
*owner
)
312 , m_colour(wxColourDisplay())
316 , m_isBBoxValid(false)
317 , m_logicalOriginX(0), m_logicalOriginY(0)
318 , m_deviceOriginX(0), m_deviceOriginY(0)
319 , m_deviceLocalOriginX(0), m_deviceLocalOriginY(0)
320 , m_logicalScaleX(1.0), m_logicalScaleY(1.0)
321 , m_userScaleX(1.0), m_userScaleY(1.0)
322 , m_scaleX(1.0), m_scaleY(1.0)
323 , m_signX(1), m_signY(1)
324 , m_minX(0), m_minY(0), m_maxX(0), m_maxY(0)
325 , m_clipX1(0), m_clipY1(0), m_clipX2(0), m_clipY2(0)
326 , m_logicalFunction(wxCOPY
)
327 , m_backgroundMode(wxTRANSPARENT
)
328 , m_mappingMode(wxMM_TEXT
)
331 , m_backgroundBrush(*wxTRANSPARENT_BRUSH
)
332 , m_textForegroundColour(*wxBLACK
)
333 , m_textBackgroundColour(*wxWHITE
)
337 , m_hasCustomPalette(false)
338 #endif // wxUSE_PALETTE
342 m_mm_to_pix_x
= (double)wxGetDisplaySize().GetWidth() /
343 (double)wxGetDisplaySizeMM().GetWidth();
344 m_mm_to_pix_y
= (double)wxGetDisplaySize().GetHeight() /
345 (double)wxGetDisplaySizeMM().GetHeight();
351 wxDCImpl::~wxDCImpl()
355 // ----------------------------------------------------------------------------
356 // coordinate conversions and transforms
357 // ----------------------------------------------------------------------------
359 wxCoord
wxDCImpl::DeviceToLogicalX(wxCoord x
) const
361 return wxRound((double)(x
- m_deviceOriginX
- m_deviceLocalOriginX
) / m_scaleX
) * m_signX
+ m_logicalOriginX
;
364 wxCoord
wxDCImpl::DeviceToLogicalY(wxCoord y
) const
366 return wxRound((double)(y
- m_deviceOriginY
- m_deviceLocalOriginY
) / m_scaleY
) * m_signY
+ m_logicalOriginY
;
369 wxCoord
wxDCImpl::DeviceToLogicalXRel(wxCoord x
) const
371 return wxRound((double)(x
) / m_scaleX
);
374 wxCoord
wxDCImpl::DeviceToLogicalYRel(wxCoord y
) const
376 return wxRound((double)(y
) / m_scaleY
);
379 wxCoord
wxDCImpl::LogicalToDeviceX(wxCoord x
) const
381 return wxRound((double)(x
- m_logicalOriginX
) * m_scaleX
) * m_signX
+ m_deviceOriginX
* m_signY
+ m_deviceLocalOriginX
;
384 wxCoord
wxDCImpl::LogicalToDeviceY(wxCoord y
) const
386 return wxRound((double)(y
- m_logicalOriginY
) * m_scaleY
) * m_signY
+ m_deviceOriginY
* m_signY
+ m_deviceLocalOriginY
;
389 wxCoord
wxDCImpl::LogicalToDeviceXRel(wxCoord x
) const
391 return wxRound((double)(x
) * m_scaleX
);
394 wxCoord
wxDCImpl::LogicalToDeviceYRel(wxCoord y
) const
396 return wxRound((double)(y
) * m_scaleY
);
399 void wxDCImpl::ComputeScaleAndOrigin()
401 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
402 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
405 void wxDCImpl::SetMapMode( int mode
)
410 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
413 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
416 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
419 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
423 SetLogicalScale( 1.0, 1.0 );
426 m_mappingMode
= mode
;
429 void wxDCImpl::SetUserScale( double x
, double y
)
431 // allow negative ? -> no
434 ComputeScaleAndOrigin();
437 void wxDCImpl::SetLogicalScale( double x
, double y
)
442 ComputeScaleAndOrigin();
445 void wxDCImpl::SetLogicalOrigin( wxCoord x
, wxCoord y
)
447 m_logicalOriginX
= x
* m_signX
;
448 m_logicalOriginY
= y
* m_signY
;
449 ComputeScaleAndOrigin();
452 void wxDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
456 ComputeScaleAndOrigin();
459 void wxDCImpl::SetDeviceLocalOrigin( wxCoord x
, wxCoord y
)
461 m_deviceLocalOriginX
= x
;
462 m_deviceLocalOriginY
= y
;
463 ComputeScaleAndOrigin();
466 void wxDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
468 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
469 // wxWidgets 2.9: no longer override it
470 m_signX
= (xLeftRight
? 1 : -1);
471 m_signY
= (yBottomUp
? -1 : 1);
472 ComputeScaleAndOrigin();
476 // Each element of the widths array will be the width of the string up to and
477 // including the corresponding character in text. This is the generic
478 // implementation, the port-specific classes should do this with native APIs
479 // if available and if faster. Note: pango_layout_index_to_pos is much slower
480 // than calling GetTextExtent!!
487 FontWidthCache() : m_scaleX(1), m_widths(NULL
) { }
488 ~FontWidthCache() { delete []m_widths
; }
493 m_widths
= new int[FWC_SIZE
];
495 memset(m_widths
, 0, sizeof(int)*FWC_SIZE
);
503 static FontWidthCache s_fontWidthCache
;
505 bool wxDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
509 const size_t len
= text
.length();
513 // reset the cache if font or horizontal scale have changed
514 if ( !s_fontWidthCache
.m_widths
||
515 !wxIsSameDouble(s_fontWidthCache
.m_scaleX
, m_scaleX
) ||
516 (s_fontWidthCache
.m_font
!= GetFont()) )
518 s_fontWidthCache
.Reset();
519 s_fontWidthCache
.m_font
= GetFont();
520 s_fontWidthCache
.m_scaleX
= m_scaleX
;
523 // Calculate the position of each character based on the widths of
524 // the previous characters
526 for ( size_t i
= 0; i
< len
; i
++ )
528 const wxChar c
= text
[i
];
529 unsigned int c_int
= (unsigned int)c
;
531 if ((c_int
< FWC_SIZE
) && (s_fontWidthCache
.m_widths
[c_int
] != 0))
533 w
= s_fontWidthCache
.m_widths
[c_int
];
537 DoGetTextExtent(c
, &w
, &h
);
538 if (c_int
< FWC_SIZE
)
539 s_fontWidthCache
.m_widths
[c_int
] = w
;
543 widths
[i
] = totalWidth
;
549 void wxDCImpl::GetMultiLineTextExtent(const wxString
& text
,
553 const wxFont
*font
) const
555 wxCoord widthTextMax
= 0, widthLine
,
556 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
559 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
561 if ( pc
== text
.end() || *pc
== _T('\n') )
563 if ( curLine
.empty() )
565 // we can't use GetTextExtent - it will return 0 for both width
566 // and height and an empty line should count in height
569 // assume that this line has the same height as the previous
571 if ( !heightLineDefault
)
572 heightLineDefault
= heightLine
;
574 if ( !heightLineDefault
)
576 // but we don't know it yet - choose something reasonable
577 DoGetTextExtent(_T("W"), NULL
, &heightLineDefault
,
581 heightTextTotal
+= heightLineDefault
;
585 DoGetTextExtent(curLine
, &widthLine
, &heightLine
,
587 if ( widthLine
> widthTextMax
)
588 widthTextMax
= widthLine
;
589 heightTextTotal
+= heightLine
;
592 if ( pc
== text
.end() )
610 *y
= heightTextTotal
;
615 void wxDCImpl::DoDrawCheckMark(wxCoord x1
, wxCoord y1
,
616 wxCoord width
, wxCoord height
)
618 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
620 wxCoord x2
= x1
+ width
,
623 // the pen width is calibrated to give 3 for width == height == 10
624 wxDCPenChanger
pen( *m_owner
, wxPen(GetTextForeground(), (width
+ height
+ 1)/7));
626 // we're drawing a scaled version of wx/generic/tick.xpm here
627 wxCoord x3
= x1
+ (4*width
) / 10, // x of the tick bottom
628 y3
= y1
+ height
/ 2; // y of the left tick branch
629 DoDrawLine(x1
, y3
, x3
, y2
);
630 DoDrawLine(x3
, y2
, x2
, y1
);
632 CalcBoundingBox(x1
, y1
);
633 CalcBoundingBox(x2
, y2
);
637 wxDCImpl::DoStretchBlit(wxCoord xdest
, wxCoord ydest
,
638 wxCoord dstWidth
, wxCoord dstHeight
,
640 wxCoord xsrc
, wxCoord ysrc
,
641 wxCoord srcWidth
, wxCoord srcHeight
,
647 wxCHECK_MSG( srcWidth
&& srcHeight
&& dstWidth
&& dstHeight
, false,
648 _T("invalid blit size") );
650 // emulate the stretching by modifying the DC scale
651 double xscale
= (double)srcWidth
/dstWidth
,
652 yscale
= (double)srcHeight
/dstHeight
;
654 double xscaleOld
, yscaleOld
;
655 GetUserScale(&xscaleOld
, &yscaleOld
);
656 SetUserScale(xscaleOld
/xscale
, yscaleOld
/yscale
);
658 bool rc
= DoBlit(wxCoord(xdest
*xscale
), wxCoord(ydest
*yscale
),
659 wxCoord(dstWidth
*xscale
), wxCoord(dstHeight
*yscale
),
661 xsrc
, ysrc
, rop
, useMask
, xsrcMask
, ysrcMask
);
663 SetUserScale(xscaleOld
, yscaleOld
);
668 void wxDCImpl::DrawLines(const wxPointList
*list
, wxCoord xoffset
, wxCoord yoffset
)
670 int n
= list
->GetCount();
671 wxPoint
*points
= new wxPoint
[n
];
674 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
676 wxPoint
*point
= node
->GetData();
677 points
[i
].x
= point
->x
;
678 points
[i
].y
= point
->y
;
681 DoDrawLines(n
, points
, xoffset
, yoffset
);
686 void wxDCImpl::DrawPolygon(const wxPointList
*list
,
687 wxCoord xoffset
, wxCoord yoffset
,
690 int n
= list
->GetCount();
691 wxPoint
*points
= new wxPoint
[n
];
694 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
696 wxPoint
*point
= node
->GetData();
697 points
[i
].x
= point
->x
;
698 points
[i
].y
= point
->y
;
701 DoDrawPolygon(n
, points
, xoffset
, yoffset
, fillStyle
);
707 wxDCImpl::DoDrawPolyPolygon(int n
,
710 wxCoord xoffset
, wxCoord yoffset
,
715 DoDrawPolygon(count
[0], points
, xoffset
, yoffset
, fillStyle
);
723 for (i
= j
= lastOfs
= 0; i
< n
; i
++)
728 pts
= new wxPoint
[j
+n
-1];
729 for (i
= 0; i
< j
; i
++)
731 for (i
= 2; i
<= n
; i
++)
733 lastOfs
-= count
[n
-i
];
734 pts
[j
++] = pts
[lastOfs
];
738 SetPen(wxPen(*wxBLACK
, 0, wxTRANSPARENT
));
739 DoDrawPolygon(j
, pts
, xoffset
, yoffset
, fillStyle
);
741 for (i
= j
= 0; i
< n
; i
++)
743 DoDrawLines(count
[i
], pts
+j
, xoffset
, yoffset
);
751 void wxDCImpl::DoDrawSpline(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
, wxCoord x3
, wxCoord y3
)
753 wxPointList point_list
;
755 wxPoint
*point1
= new wxPoint
;
756 point1
->x
= x1
; point1
->y
= y1
;
757 point_list
.Append( point1
);
759 wxPoint
*point2
= new wxPoint
;
760 point2
->x
= x2
; point2
->y
= y2
;
761 point_list
.Append( point2
);
763 wxPoint
*point3
= new wxPoint
;
764 point3
->x
= x3
; point3
->y
= y3
;
765 point_list
.Append( point3
);
767 DoDrawSpline(&point_list
);
769 for( wxPointList::compatibility_iterator node
= point_list
.GetFirst(); node
; node
= node
->GetNext() )
771 wxPoint
*p
= node
->GetData();
776 void wxDCImpl::DoDrawSpline(int n
, wxPoint points
[])
779 for (int i
=0; i
< n
; i
++)
780 list
.Append( &points
[i
] );
785 // ----------------------------------- spline code ----------------------------------------
787 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
,
788 double a3
, double b3
, double a4
, double b4
);
789 void wx_clear_stack();
790 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
, double *x3
,
791 double *y3
, double *x4
, double *y4
);
792 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
,
793 double x4
, double y4
);
794 static bool wx_spline_add_point(double x
, double y
);
795 static void wx_spline_draw_point_array(wxDC
*dc
);
797 wxPointList wx_spline_point_list
;
799 #define half(z1, z2) ((z1+z2)/2.0)
802 /* iterative version */
804 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
, double a3
, double b3
, double a4
,
807 register double xmid
, ymid
;
808 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
811 wx_spline_push(a1
, b1
, a2
, b2
, a3
, b3
, a4
, b4
);
813 while (wx_spline_pop(&x1
, &y1
, &x2
, &y2
, &x3
, &y3
, &x4
, &y4
)) {
814 xmid
= (double)half(x2
, x3
);
815 ymid
= (double)half(y2
, y3
);
816 if (fabs(x1
- xmid
) < THRESHOLD
&& fabs(y1
- ymid
) < THRESHOLD
&&
817 fabs(xmid
- x4
) < THRESHOLD
&& fabs(ymid
- y4
) < THRESHOLD
) {
818 wx_spline_add_point( x1
, y1
);
819 wx_spline_add_point( xmid
, ymid
);
821 wx_spline_push(xmid
, ymid
, (double)half(xmid
, x3
), (double)half(ymid
, y3
),
822 (double)half(x3
, x4
), (double)half(y3
, y4
), x4
, y4
);
823 wx_spline_push(x1
, y1
, (double)half(x1
, x2
), (double)half(y1
, y2
),
824 (double)half(x2
, xmid
), (double)half(y2
, ymid
), xmid
, ymid
);
829 /* utilities used by spline drawing routines */
831 typedef struct wx_spline_stack_struct
{
832 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
835 #define SPLINE_STACK_DEPTH 20
836 static Stack wx_spline_stack
[SPLINE_STACK_DEPTH
];
837 static Stack
*wx_stack_top
;
838 static int wx_stack_count
;
840 void wx_clear_stack()
842 wx_stack_top
= wx_spline_stack
;
846 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
, double x4
, double y4
)
848 wx_stack_top
->x1
= x1
;
849 wx_stack_top
->y1
= y1
;
850 wx_stack_top
->x2
= x2
;
851 wx_stack_top
->y2
= y2
;
852 wx_stack_top
->x3
= x3
;
853 wx_stack_top
->y3
= y3
;
854 wx_stack_top
->x4
= x4
;
855 wx_stack_top
->y4
= y4
;
860 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
,
861 double *x3
, double *y3
, double *x4
, double *y4
)
863 if (wx_stack_count
== 0)
867 *x1
= wx_stack_top
->x1
;
868 *y1
= wx_stack_top
->y1
;
869 *x2
= wx_stack_top
->x2
;
870 *y2
= wx_stack_top
->y2
;
871 *x3
= wx_stack_top
->x3
;
872 *y3
= wx_stack_top
->y3
;
873 *x4
= wx_stack_top
->x4
;
874 *y4
= wx_stack_top
->y4
;
878 static bool wx_spline_add_point(double x
, double y
)
880 wxPoint
*point
= new wxPoint( wxRound(x
), wxRound(y
) );
881 wx_spline_point_list
.Append(point
);
885 static void wx_spline_draw_point_array(wxDC
*dc
)
887 dc
->DrawLines(&wx_spline_point_list
, 0, 0 );
888 wxPointList::compatibility_iterator node
= wx_spline_point_list
.GetFirst();
891 wxPoint
*point
= node
->GetData();
893 wx_spline_point_list
.Erase(node
);
894 node
= wx_spline_point_list
.GetFirst();
898 void wxDCImpl::DoDrawSpline( const wxPointList
*points
)
900 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
903 double cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
;
904 double x1
, y1
, x2
, y2
;
906 wxPointList::compatibility_iterator node
= points
->GetFirst();
911 p
= (wxPoint
*)node
->GetData();
916 node
= node
->GetNext();
921 cx1
= (double)((x1
+ x2
) / 2);
922 cy1
= (double)((y1
+ y2
) / 2);
923 cx2
= (double)((cx1
+ x2
) / 2);
924 cy2
= (double)((cy1
+ y2
) / 2);
926 wx_spline_add_point(x1
, y1
);
928 while ((node
= node
->GetNext())
939 cx4
= (double)(x1
+ x2
) / 2;
940 cy4
= (double)(y1
+ y2
) / 2;
941 cx3
= (double)(x1
+ cx4
) / 2;
942 cy3
= (double)(y1
+ cy4
) / 2;
944 wx_quadratic_spline(cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
);
948 cx2
= (double)(cx1
+ x2
) / 2;
949 cy2
= (double)(cy1
+ y2
) / 2;
952 wx_spline_add_point( cx1
, cy1
);
953 wx_spline_add_point( x2
, y2
);
955 wx_spline_draw_point_array( m_owner
);
958 #endif // wxUSE_SPLINES
962 void wxDCImpl::DoGradientFillLinear(const wxRect
& rect
,
963 const wxColour
& initialColour
,
964 const wxColour
& destColour
,
965 wxDirection nDirection
)
968 wxPen oldPen
= m_pen
;
969 wxBrush oldBrush
= m_brush
;
971 wxUint8 nR1
= initialColour
.Red();
972 wxUint8 nG1
= initialColour
.Green();
973 wxUint8 nB1
= initialColour
.Blue();
974 wxUint8 nR2
= destColour
.Red();
975 wxUint8 nG2
= destColour
.Green();
976 wxUint8 nB2
= destColour
.Blue();
979 if ( nDirection
== wxEAST
|| nDirection
== wxWEST
)
981 wxInt32 x
= rect
.GetWidth();
982 wxInt32 w
= x
; // width of area to shade
983 wxInt32 xDelta
= w
/256; // height of one shade bend
991 nR
= nR1
- (nR1
-nR2
)*(w
-x
)/w
;
993 nR
= nR1
+ (nR2
-nR1
)*(w
-x
)/w
;
996 nG
= nG1
- (nG1
-nG2
)*(w
-x
)/w
;
998 nG
= nG1
+ (nG2
-nG1
)*(w
-x
)/w
;
1001 nB
= nB1
- (nB1
-nB2
)*(w
-x
)/w
;
1003 nB
= nB1
+ (nB2
-nB1
)*(w
-x
)/w
;
1005 wxColour
colour(nR
,nG
,nB
);
1006 SetPen(wxPen(colour
, 1, wxSOLID
));
1007 SetBrush(wxBrush(colour
));
1008 if(nDirection
== wxEAST
)
1009 DoDrawRectangle(rect
.GetRight()-x
-xDelta
+1, rect
.GetTop(),
1010 xDelta
, rect
.GetHeight());
1011 else //nDirection == wxWEST
1012 DoDrawRectangle(rect
.GetLeft()+x
, rect
.GetTop(),
1013 xDelta
, rect
.GetHeight());
1016 else // nDirection == wxNORTH || nDirection == wxSOUTH
1018 wxInt32 y
= rect
.GetHeight();
1019 wxInt32 w
= y
; // height of area to shade
1020 wxInt32 yDelta
= w
/255; // height of one shade bend
1028 nR
= nR1
- (nR1
-nR2
)*(w
-y
)/w
;
1030 nR
= nR1
+ (nR2
-nR1
)*(w
-y
)/w
;
1033 nG
= nG1
- (nG1
-nG2
)*(w
-y
)/w
;
1035 nG
= nG1
+ (nG2
-nG1
)*(w
-y
)/w
;
1038 nB
= nB1
- (nB1
-nB2
)*(w
-y
)/w
;
1040 nB
= nB1
+ (nB2
-nB1
)*(w
-y
)/w
;
1042 wxColour
colour(nR
,nG
,nB
);
1043 SetPen(wxPen(colour
, 1, wxSOLID
));
1044 SetBrush(wxBrush(colour
));
1045 if(nDirection
== wxNORTH
)
1046 DoDrawRectangle(rect
.GetLeft(), rect
.GetTop()+y
,
1047 rect
.GetWidth(), yDelta
);
1048 else //nDirection == wxSOUTH
1049 DoDrawRectangle(rect
.GetLeft(), rect
.GetBottom()-y
-yDelta
+1,
1050 rect
.GetWidth(), yDelta
);
1058 void wxDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1059 const wxColour
& initialColour
,
1060 const wxColour
& destColour
,
1061 const wxPoint
& circleCenter
)
1063 //save the old pen color
1064 wxColour oldPenColour
= m_pen
.GetColour();
1066 wxUint8 nR1
= destColour
.Red();
1067 wxUint8 nG1
= destColour
.Green();
1068 wxUint8 nB1
= destColour
.Blue();
1069 wxUint8 nR2
= initialColour
.Red();
1070 wxUint8 nG2
= initialColour
.Green();
1071 wxUint8 nB2
= initialColour
.Blue();
1076 wxInt32 cx
= rect
.GetWidth() / 2;
1077 wxInt32 cy
= rect
.GetHeight() / 2;
1085 wxInt32 nCircleOffX
= circleCenter
.x
- (rect
.GetWidth() / 2);
1086 wxInt32 nCircleOffY
= circleCenter
.y
- (rect
.GetHeight() / 2);
1088 for ( wxInt32 x
= 0; x
< rect
.GetWidth(); x
++ )
1090 for ( wxInt32 y
= 0; y
< rect
.GetHeight(); y
++ )
1092 //get color difference
1093 wxInt32 nGradient
= ((nRadius
-
1095 pow((double)(x
- cx
- nCircleOffX
), 2) +
1096 pow((double)(y
- cy
- nCircleOffY
), 2)
1097 )) * 100) / nRadius
;
1099 //normalize Gradient
1104 nR
= (wxUint8
)(nR1
+ ((nR2
- nR1
) * nGradient
/ 100));
1105 nG
= (wxUint8
)(nG1
+ ((nG2
- nG1
) * nGradient
/ 100));
1106 nB
= (wxUint8
)(nB1
+ ((nB2
- nB1
) * nGradient
/ 100));
1109 m_pen
.SetColour(wxColour(nR
,nG
,nB
));
1110 DoDrawPoint(x
+ rect
.GetLeft(), y
+ rect
.GetTop());
1113 //return old pen color
1114 m_pen
.SetColour(oldPenColour
);
1117 //-----------------------------------------------------------------------------
1119 //-----------------------------------------------------------------------------
1121 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxObject
)
1123 void wxDC::DrawLabel(const wxString
& text
,
1124 const wxBitmap
& bitmap
,
1128 wxRect
*rectBounding
)
1130 // find the text position
1131 wxCoord widthText
, heightText
, heightLine
;
1132 GetMultiLineTextExtent(text
, &widthText
, &heightText
, &heightLine
);
1134 wxCoord width
, height
;
1137 width
= widthText
+ bitmap
.GetWidth();
1138 height
= bitmap
.GetHeight();
1143 height
= heightText
;
1147 if ( alignment
& wxALIGN_RIGHT
)
1149 x
= rect
.GetRight() - width
;
1151 else if ( alignment
& wxALIGN_CENTRE_HORIZONTAL
)
1153 x
= (rect
.GetLeft() + rect
.GetRight() + 1 - width
) / 2;
1155 else // alignment & wxALIGN_LEFT
1160 if ( alignment
& wxALIGN_BOTTOM
)
1162 y
= rect
.GetBottom() - height
;
1164 else if ( alignment
& wxALIGN_CENTRE_VERTICAL
)
1166 y
= (rect
.GetTop() + rect
.GetBottom() + 1 - height
) / 2;
1168 else // alignment & wxALIGN_TOP
1173 // draw the bitmap first
1179 DrawBitmap(bitmap
, x
, y
, true /* use mask */);
1181 wxCoord offset
= bitmap
.GetWidth() + 4;
1185 y
+= (height
- heightText
) / 2;
1188 // we will draw the underscore under the accel char later
1189 wxCoord startUnderscore
= 0,
1193 // split the string into lines and draw each of them separately
1195 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
1197 if ( *pc
== _T('\n') || pc
== text
.end() )
1199 int xRealStart
= x
; // init it here to avoid compielr warnings
1201 if ( !curLine
.empty() )
1203 // NB: can't test for !(alignment & wxALIGN_LEFT) because
1204 // wxALIGN_LEFT is 0
1205 if ( alignment
& (wxALIGN_RIGHT
| wxALIGN_CENTRE_HORIZONTAL
) )
1208 GetTextExtent(curLine
, &widthLine
, NULL
);
1210 if ( alignment
& wxALIGN_RIGHT
)
1212 xRealStart
+= width
- widthLine
;
1214 else // if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
1216 xRealStart
+= (width
- widthLine
) / 2;
1219 //else: left aligned, nothing to do
1221 DrawText(curLine
, xRealStart
, y
);
1226 // do we have underscore in this line? we can check yUnderscore
1227 // because it is set below to just y + heightLine if we do
1228 if ( y
== yUnderscore
)
1230 // adjust the horz positions to account for the shift
1231 startUnderscore
+= xRealStart
;
1232 endUnderscore
+= xRealStart
;
1235 if ( pc
== text
.end() )
1240 else // not end of line
1242 if ( pc
- text
.begin() == indexAccel
)
1244 // remeber to draw underscore here
1245 GetTextExtent(curLine
, &startUnderscore
, NULL
);
1247 GetTextExtent(curLine
, &endUnderscore
, NULL
);
1249 yUnderscore
= y
+ heightLine
;
1258 // draw the underscore if found
1259 if ( startUnderscore
!= endUnderscore
)
1261 // it should be of the same colour as text
1262 SetPen(wxPen(GetTextForeground(), 0, wxSOLID
));
1266 DrawLine(startUnderscore
, yUnderscore
, endUnderscore
, yUnderscore
);
1269 // return bounding rect if requested
1272 *rectBounding
= wxRect(x
, y
- heightText
, widthText
, heightText
);
1275 CalcBoundingBox(x0
, y0
);
1276 CalcBoundingBox(x0
+ width0
, y0
+ height
);
1279 #if WXWIN_COMPATIBILITY_2_8
1280 // for compatibility with the old code when wxCoord was long everywhere
1281 void wxDC::GetTextExtent(const wxString
& string
,
1284 long *externalLeading
,
1285 const wxFont
*theFont
) const
1287 wxCoord x2
, y2
, descent2
, externalLeading2
;
1288 m_pimpl
->DoGetTextExtent(string
, &x2
, &y2
,
1289 &descent2
, &externalLeading2
,
1296 *descent
= descent2
;
1297 if ( externalLeading
)
1298 *externalLeading
= externalLeading2
;
1301 void wxDC::GetLogicalOrigin(long *x
, long *y
) const
1304 m_pimpl
->DoGetLogicalOrigin(&x2
, &y2
);
1311 void wxDC::GetDeviceOrigin(long *x
, long *y
) const
1314 m_pimpl
->DoGetDeviceOrigin(&x2
, &y2
);
1321 void wxDC::GetClippingBox(long *x
, long *y
, long *w
, long *h
) const
1323 wxCoord xx
,yy
,ww
,hh
;
1324 m_pimpl
->DoGetClippingBox(&xx
, &yy
, &ww
, &hh
);
1331 #endif // WXWIN_COMPATIBILITY_2_8