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"
39 #include "wx/msw/dcclient.h"
40 #include "wx/msw/dcmemory.h"
41 #include "wx/msw/dcscreen.h"
45 #include "wx/gtk/dcclient.h"
46 #include "wx/gtk/dcmemory.h"
47 #include "wx/gtk/dcscreen.h"
51 #include "wx/mac/dcclient.h"
52 #include "wx/mac/dcmemory.h"
53 #include "wx/mac/dcscreen.h"
57 #include "wx/x11/dcclient.h"
58 #include "wx/x11/dcmemory.h"
59 #include "wx/x11/dcscreen.h"
62 //----------------------------------------------------------------------------
64 //----------------------------------------------------------------------------
66 wxDCFactory
*wxDCFactory::m_factory
= NULL
;
68 void wxDCFactory::SetDCFactory( wxDCFactory
*factory
)
70 if (wxDCFactory::m_factory
)
71 delete wxDCFactory::m_factory
;
73 wxDCFactory::m_factory
= factory
;
76 wxDCFactory
*wxDCFactory::Get()
78 if (!wxDCFactory::m_factory
)
79 wxDCFactory::m_factory
= new wxNativeDCFactory
;
81 return wxDCFactory::m_factory
;
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
88 wxDCImpl
* wxNativeDCFactory::CreateWindowDC( wxWindowDC
*owner
)
90 return new wxWindowDCImpl( owner
);
93 wxDCImpl
* wxNativeDCFactory::CreateWindowDC( wxWindowDC
*owner
, wxWindow
*window
)
95 return new wxWindowDCImpl( owner
, window
);
98 wxDCImpl
* wxNativeDCFactory::CreateClientDC( wxClientDC
*owner
)
100 return new wxClientDCImpl( owner
);
103 wxDCImpl
* wxNativeDCFactory::CreateClientDC( wxClientDC
*owner
, wxWindow
*window
)
105 return new wxClientDCImpl( owner
, window
);
108 wxDCImpl
* wxNativeDCFactory::CreatePaintDC( wxPaintDC
*owner
)
110 return new wxPaintDCImpl( owner
);
113 wxDCImpl
* wxNativeDCFactory::CreatePaintDC( wxPaintDC
*owner
, wxWindow
*window
)
115 return new wxPaintDCImpl( owner
, window
);
118 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
)
120 return new wxMemoryDCImpl( owner
);
123 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
, wxBitmap
&bitmap
)
125 return new wxMemoryDCImpl( owner
, bitmap
);
128 wxDCImpl
* wxNativeDCFactory::CreateMemoryDC( wxMemoryDC
*owner
, wxDC
*dc
)
130 return new wxMemoryDCImpl( owner
, dc
);
133 wxDCImpl
* wxNativeDCFactory::CreateScreenDC( wxScreenDC
*owner
)
135 return new wxScreenDCImpl( owner
);
138 #if wxUSE_PRINTING_ARCHITECTURE
139 wxDCImpl
*wxNativeDCFactory::CreatePrinterDC( wxPrinterDC
*owner
, const wxPrintData
&data
)
141 wxPrintFactory
*factory
= wxPrintFactory::GetFactory();
142 return factory
->CreatePrinterDCImpl( owner
, data
);
146 //-----------------------------------------------------------------------------
148 //-----------------------------------------------------------------------------
150 IMPLEMENT_ABSTRACT_CLASS(wxWindowDC
, wxDC
)
152 wxWindowDC::wxWindowDC(wxWindow
*win
)
153 : wxDC(wxDCFactory::Get()->CreateWindowDC(this, win
))
157 //-----------------------------------------------------------------------------
159 //-----------------------------------------------------------------------------
161 IMPLEMENT_ABSTRACT_CLASS(wxClientDC
, wxWindowDC
)
163 wxClientDC::wxClientDC(wxWindow
*win
)
164 : wxWindowDC(wxDCFactory::Get()->CreateClientDC(this, win
))
168 //-----------------------------------------------------------------------------
170 //-----------------------------------------------------------------------------
172 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC
, wxDC
)
174 wxMemoryDC::wxMemoryDC()
175 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this))
179 wxMemoryDC::wxMemoryDC(wxBitmap
& bitmap
)
180 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, bitmap
))
184 wxMemoryDC::wxMemoryDC(wxDC
*dc
)
185 : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, dc
))
189 void wxMemoryDC::SelectObject(wxBitmap
& bmp
)
191 // make sure that the given wxBitmap is not sharing its data with other
192 // wxBitmap instances as its contents will be modified by any drawing
193 // operation done on this DC
197 GetImpl()->DoSelect(bmp
);
200 void wxMemoryDC::SelectObjectAsSource(const wxBitmap
& bmp
)
202 GetImpl()->DoSelect(bmp
);
205 const wxBitmap
& wxMemoryDC::GetSelectedBitmap() const
207 return GetImpl()->GetSelectedBitmap();
210 wxBitmap
& wxMemoryDC::GetSelectedBitmap()
212 return GetImpl()->GetSelectedBitmap();
216 //-----------------------------------------------------------------------------
218 //-----------------------------------------------------------------------------
220 IMPLEMENT_ABSTRACT_CLASS(wxPaintDC
, wxClientDC
)
222 wxPaintDC::wxPaintDC(wxWindow
*win
)
223 : wxClientDC(wxDCFactory::Get()->CreatePaintDC(this, win
))
227 //-----------------------------------------------------------------------------
229 //-----------------------------------------------------------------------------
231 IMPLEMENT_DYNAMIC_CLASS(wxScreenDC
, wxWindowDC
)
233 wxScreenDC::wxScreenDC()
234 : wxDC(wxDCFactory::Get()->CreateScreenDC(this))
238 //-----------------------------------------------------------------------------
240 //-----------------------------------------------------------------------------
242 #if wxUSE_PRINTING_ARCHITECTURE
244 IMPLEMENT_DYNAMIC_CLASS(wxPrinterDC
, wxDC
)
246 wxPrinterDC::wxPrinterDC()
247 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, wxPrintData()))
251 wxPrinterDC::wxPrinterDC(const wxPrintData
& data
)
252 : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, data
))
256 wxRect
wxPrinterDC::GetPaperRect()
258 return GetImpl()->GetPaperRect();
261 int wxPrinterDC::GetResolution()
263 return GetImpl()->GetResolution();
266 #endif // wxUSE_PRINTING_ARCHITECTURE
268 //-----------------------------------------------------------------------------
270 //-----------------------------------------------------------------------------
272 IMPLEMENT_ABSTRACT_CLASS(wxDCImpl
, wxObject
)
274 wxDCImpl::wxDCImpl( wxDC
*owner
)
276 , m_colour(wxColourDisplay())
280 , m_isBBoxValid(false)
281 , m_logicalOriginX(0), m_logicalOriginY(0)
282 , m_deviceOriginX(0), m_deviceOriginY(0)
283 , m_deviceLocalOriginX(0), m_deviceLocalOriginY(0)
284 , m_logicalScaleX(1.0), m_logicalScaleY(1.0)
285 , m_userScaleX(1.0), m_userScaleY(1.0)
286 , m_scaleX(1.0), m_scaleY(1.0)
287 , m_signX(1), m_signY(1)
288 , m_minX(0), m_minY(0), m_maxX(0), m_maxY(0)
289 , m_clipX1(0), m_clipY1(0), m_clipX2(0), m_clipY2(0)
290 , m_logicalFunction(wxCOPY
)
291 , m_backgroundMode(wxTRANSPARENT
)
292 , m_mappingMode(wxMM_TEXT
)
295 , m_backgroundBrush(*wxTRANSPARENT_BRUSH
)
296 , m_textForegroundColour(*wxBLACK
)
297 , m_textBackgroundColour(*wxWHITE
)
301 , m_hasCustomPalette(false)
302 #endif // wxUSE_PALETTE
306 m_mm_to_pix_x
= (double)wxGetDisplaySize().GetWidth() /
307 (double)wxGetDisplaySizeMM().GetWidth();
308 m_mm_to_pix_y
= (double)wxGetDisplaySize().GetHeight() /
309 (double)wxGetDisplaySizeMM().GetHeight();
315 wxDCImpl::~wxDCImpl()
319 // ----------------------------------------------------------------------------
320 // coordinate conversions and transforms
321 // ----------------------------------------------------------------------------
323 wxCoord
wxDCImpl::DeviceToLogicalX(wxCoord x
) const
325 return wxRound((double)(x
- m_deviceOriginX
- m_deviceLocalOriginX
) / m_scaleX
) * m_signX
+ m_logicalOriginX
;
328 wxCoord
wxDCImpl::DeviceToLogicalY(wxCoord y
) const
330 return wxRound((double)(y
- m_deviceOriginY
- m_deviceLocalOriginY
) / m_scaleY
) * m_signY
+ m_logicalOriginY
;
333 wxCoord
wxDCImpl::DeviceToLogicalXRel(wxCoord x
) const
335 return wxRound((double)(x
) / m_scaleX
);
338 wxCoord
wxDCImpl::DeviceToLogicalYRel(wxCoord y
) const
340 return wxRound((double)(y
) / m_scaleY
);
343 wxCoord
wxDCImpl::LogicalToDeviceX(wxCoord x
) const
345 return wxRound((double)(x
- m_logicalOriginX
) * m_scaleX
) * m_signX
+ m_deviceOriginX
* m_signY
+ m_deviceLocalOriginX
;
348 wxCoord
wxDCImpl::LogicalToDeviceY(wxCoord y
) const
350 return wxRound((double)(y
- m_logicalOriginY
) * m_scaleY
) * m_signY
+ m_deviceOriginY
* m_signY
+ m_deviceLocalOriginY
;
353 wxCoord
wxDCImpl::LogicalToDeviceXRel(wxCoord x
) const
355 return wxRound((double)(x
) * m_scaleX
);
358 wxCoord
wxDCImpl::LogicalToDeviceYRel(wxCoord y
) const
360 return wxRound((double)(y
) * m_scaleY
);
363 void wxDCImpl::ComputeScaleAndOrigin()
365 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
366 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
369 void wxDCImpl::SetMapMode( int mode
)
374 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
377 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
380 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
383 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
387 SetLogicalScale( 1.0, 1.0 );
390 m_mappingMode
= mode
;
393 void wxDCImpl::SetUserScale( double x
, double y
)
395 // allow negative ? -> no
398 ComputeScaleAndOrigin();
401 void wxDCImpl::SetLogicalScale( double x
, double y
)
406 ComputeScaleAndOrigin();
409 void wxDCImpl::SetLogicalOrigin( wxCoord x
, wxCoord y
)
411 m_logicalOriginX
= x
* m_signX
;
412 m_logicalOriginY
= y
* m_signY
;
413 ComputeScaleAndOrigin();
416 void wxDCImpl::SetDeviceOrigin( wxCoord x
, wxCoord y
)
420 ComputeScaleAndOrigin();
423 void wxDCImpl::SetDeviceLocalOrigin( wxCoord x
, wxCoord y
)
425 m_deviceLocalOriginX
= x
;
426 m_deviceLocalOriginY
= y
;
427 ComputeScaleAndOrigin();
430 void wxDCImpl::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
432 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
433 // wxWidgets 2.9: no longer override it
434 m_signX
= (xLeftRight
? 1 : -1);
435 m_signY
= (yBottomUp
? -1 : 1);
436 ComputeScaleAndOrigin();
440 // Each element of the widths array will be the width of the string up to and
441 // including the corresponding character in text. This is the generic
442 // implementation, the port-specific classes should do this with native APIs
443 // if available and if faster. Note: pango_layout_index_to_pos is much slower
444 // than calling GetTextExtent!!
451 FontWidthCache() : m_scaleX(1), m_widths(NULL
) { }
452 ~FontWidthCache() { delete []m_widths
; }
457 m_widths
= new int[FWC_SIZE
];
459 memset(m_widths
, 0, sizeof(int)*FWC_SIZE
);
467 static FontWidthCache s_fontWidthCache
;
469 bool wxDCImpl::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
473 const size_t len
= text
.length();
477 // reset the cache if font or horizontal scale have changed
478 if ( !s_fontWidthCache
.m_widths
||
479 !wxIsSameDouble(s_fontWidthCache
.m_scaleX
, m_scaleX
) ||
480 (s_fontWidthCache
.m_font
!= GetFont()) )
482 s_fontWidthCache
.Reset();
483 s_fontWidthCache
.m_font
= GetFont();
484 s_fontWidthCache
.m_scaleX
= m_scaleX
;
487 // Calculate the position of each character based on the widths of
488 // the previous characters
490 for ( size_t i
= 0; i
< len
; i
++ )
492 const wxChar c
= text
[i
];
493 unsigned int c_int
= (unsigned int)c
;
495 if ((c_int
< FWC_SIZE
) && (s_fontWidthCache
.m_widths
[c_int
] != 0))
497 w
= s_fontWidthCache
.m_widths
[c_int
];
501 DoGetTextExtent(c
, &w
, &h
);
502 if (c_int
< FWC_SIZE
)
503 s_fontWidthCache
.m_widths
[c_int
] = w
;
507 widths
[i
] = totalWidth
;
513 void wxDCImpl::GetMultiLineTextExtent(const wxString
& text
,
517 const wxFont
*font
) const
519 wxCoord widthTextMax
= 0, widthLine
,
520 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
523 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
525 if ( pc
== text
.end() || *pc
== _T('\n') )
527 if ( curLine
.empty() )
529 // we can't use GetTextExtent - it will return 0 for both width
530 // and height and an empty line should count in height
533 // assume that this line has the same height as the previous
535 if ( !heightLineDefault
)
536 heightLineDefault
= heightLine
;
538 if ( !heightLineDefault
)
540 // but we don't know it yet - choose something reasonable
541 DoGetTextExtent(_T("W"), NULL
, &heightLineDefault
,
545 heightTextTotal
+= heightLineDefault
;
549 DoGetTextExtent(curLine
, &widthLine
, &heightLine
,
551 if ( widthLine
> widthTextMax
)
552 widthTextMax
= widthLine
;
553 heightTextTotal
+= heightLine
;
556 if ( pc
== text
.end() )
574 *y
= heightTextTotal
;
579 void wxDCImpl::DoDrawCheckMark(wxCoord x1
, wxCoord y1
,
580 wxCoord width
, wxCoord height
)
582 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
584 wxCoord x2
= x1
+ width
,
587 // the pen width is calibrated to give 3 for width == height == 10
588 wxDCPenChanger
pen( *m_owner
, wxPen(GetTextForeground(), (width
+ height
+ 1)/7));
590 // we're drawing a scaled version of wx/generic/tick.xpm here
591 wxCoord x3
= x1
+ (4*width
) / 10, // x of the tick bottom
592 y3
= y1
+ height
/ 2; // y of the left tick branch
593 DoDrawLine(x1
, y3
, x3
, y2
);
594 DoDrawLine(x3
, y2
, x2
, y1
);
596 CalcBoundingBox(x1
, y1
);
597 CalcBoundingBox(x2
, y2
);
601 wxDCImpl::DoStretchBlit(wxCoord xdest
, wxCoord ydest
,
602 wxCoord dstWidth
, wxCoord dstHeight
,
604 wxCoord xsrc
, wxCoord ysrc
,
605 wxCoord srcWidth
, wxCoord srcHeight
,
611 wxCHECK_MSG( srcWidth
&& srcHeight
&& dstWidth
&& dstHeight
, false,
612 _T("invalid blit size") );
614 // emulate the stretching by modifying the DC scale
615 double xscale
= (double)srcWidth
/dstWidth
,
616 yscale
= (double)srcHeight
/dstHeight
;
618 double xscaleOld
, yscaleOld
;
619 GetUserScale(&xscaleOld
, &yscaleOld
);
620 SetUserScale(xscaleOld
/xscale
, yscaleOld
/yscale
);
622 bool rc
= DoBlit(wxCoord(xdest
*xscale
), wxCoord(ydest
*yscale
),
623 wxCoord(dstWidth
*xscale
), wxCoord(dstHeight
*yscale
),
625 xsrc
, ysrc
, rop
, useMask
, xsrcMask
, ysrcMask
);
627 SetUserScale(xscaleOld
, yscaleOld
);
632 void wxDCImpl::DrawLines(const wxPointList
*list
, wxCoord xoffset
, wxCoord yoffset
)
634 int n
= list
->GetCount();
635 wxPoint
*points
= new wxPoint
[n
];
638 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
640 wxPoint
*point
= node
->GetData();
641 points
[i
].x
= point
->x
;
642 points
[i
].y
= point
->y
;
645 DoDrawLines(n
, points
, xoffset
, yoffset
);
650 void wxDCImpl::DrawPolygon(const wxPointList
*list
,
651 wxCoord xoffset
, wxCoord yoffset
,
654 int n
= list
->GetCount();
655 wxPoint
*points
= new wxPoint
[n
];
658 for ( wxPointList::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
660 wxPoint
*point
= node
->GetData();
661 points
[i
].x
= point
->x
;
662 points
[i
].y
= point
->y
;
665 DoDrawPolygon(n
, points
, xoffset
, yoffset
, fillStyle
);
671 wxDCImpl::DoDrawPolyPolygon(int n
,
674 wxCoord xoffset
, wxCoord yoffset
,
679 DoDrawPolygon(count
[0], points
, xoffset
, yoffset
, fillStyle
);
687 for (i
= j
= lastOfs
= 0; i
< n
; i
++)
692 pts
= new wxPoint
[j
+n
-1];
693 for (i
= 0; i
< j
; i
++)
695 for (i
= 2; i
<= n
; i
++)
697 lastOfs
-= count
[n
-i
];
698 pts
[j
++] = pts
[lastOfs
];
702 SetPen(wxPen(*wxBLACK
, 0, wxTRANSPARENT
));
703 DoDrawPolygon(j
, pts
, xoffset
, yoffset
, fillStyle
);
705 for (i
= j
= 0; i
< n
; i
++)
707 DoDrawLines(count
[i
], pts
+j
, xoffset
, yoffset
);
715 void wxDCImpl::DoDrawSpline(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
, wxCoord x3
, wxCoord y3
)
717 wxPointList point_list
;
719 wxPoint
*point1
= new wxPoint
;
720 point1
->x
= x1
; point1
->y
= y1
;
721 point_list
.Append( point1
);
723 wxPoint
*point2
= new wxPoint
;
724 point2
->x
= x2
; point2
->y
= y2
;
725 point_list
.Append( point2
);
727 wxPoint
*point3
= new wxPoint
;
728 point3
->x
= x3
; point3
->y
= y3
;
729 point_list
.Append( point3
);
731 DoDrawSpline(&point_list
);
733 for( wxPointList::compatibility_iterator node
= point_list
.GetFirst(); node
; node
= node
->GetNext() )
735 wxPoint
*p
= node
->GetData();
740 void wxDCImpl::DoDrawSpline(int n
, wxPoint points
[])
743 for (int i
=0; i
< n
; i
++)
744 list
.Append( &points
[i
] );
749 // ----------------------------------- spline code ----------------------------------------
751 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
,
752 double a3
, double b3
, double a4
, double b4
);
753 void wx_clear_stack();
754 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
, double *x3
,
755 double *y3
, double *x4
, double *y4
);
756 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
,
757 double x4
, double y4
);
758 static bool wx_spline_add_point(double x
, double y
);
759 static void wx_spline_draw_point_array(wxDC
*dc
);
761 wxPointList wx_spline_point_list
;
763 #define half(z1, z2) ((z1+z2)/2.0)
766 /* iterative version */
768 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
, double a3
, double b3
, double a4
,
771 register double xmid
, ymid
;
772 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
775 wx_spline_push(a1
, b1
, a2
, b2
, a3
, b3
, a4
, b4
);
777 while (wx_spline_pop(&x1
, &y1
, &x2
, &y2
, &x3
, &y3
, &x4
, &y4
)) {
778 xmid
= (double)half(x2
, x3
);
779 ymid
= (double)half(y2
, y3
);
780 if (fabs(x1
- xmid
) < THRESHOLD
&& fabs(y1
- ymid
) < THRESHOLD
&&
781 fabs(xmid
- x4
) < THRESHOLD
&& fabs(ymid
- y4
) < THRESHOLD
) {
782 wx_spline_add_point( x1
, y1
);
783 wx_spline_add_point( xmid
, ymid
);
785 wx_spline_push(xmid
, ymid
, (double)half(xmid
, x3
), (double)half(ymid
, y3
),
786 (double)half(x3
, x4
), (double)half(y3
, y4
), x4
, y4
);
787 wx_spline_push(x1
, y1
, (double)half(x1
, x2
), (double)half(y1
, y2
),
788 (double)half(x2
, xmid
), (double)half(y2
, ymid
), xmid
, ymid
);
793 /* utilities used by spline drawing routines */
795 typedef struct wx_spline_stack_struct
{
796 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
799 #define SPLINE_STACK_DEPTH 20
800 static Stack wx_spline_stack
[SPLINE_STACK_DEPTH
];
801 static Stack
*wx_stack_top
;
802 static int wx_stack_count
;
804 void wx_clear_stack()
806 wx_stack_top
= wx_spline_stack
;
810 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
, double x4
, double y4
)
812 wx_stack_top
->x1
= x1
;
813 wx_stack_top
->y1
= y1
;
814 wx_stack_top
->x2
= x2
;
815 wx_stack_top
->y2
= y2
;
816 wx_stack_top
->x3
= x3
;
817 wx_stack_top
->y3
= y3
;
818 wx_stack_top
->x4
= x4
;
819 wx_stack_top
->y4
= y4
;
824 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
,
825 double *x3
, double *y3
, double *x4
, double *y4
)
827 if (wx_stack_count
== 0)
831 *x1
= wx_stack_top
->x1
;
832 *y1
= wx_stack_top
->y1
;
833 *x2
= wx_stack_top
->x2
;
834 *y2
= wx_stack_top
->y2
;
835 *x3
= wx_stack_top
->x3
;
836 *y3
= wx_stack_top
->y3
;
837 *x4
= wx_stack_top
->x4
;
838 *y4
= wx_stack_top
->y4
;
842 static bool wx_spline_add_point(double x
, double y
)
844 wxPoint
*point
= new wxPoint( wxRound(x
), wxRound(y
) );
845 wx_spline_point_list
.Append(point
);
849 static void wx_spline_draw_point_array(wxDC
*dc
)
851 dc
->DrawLines(&wx_spline_point_list
, 0, 0 );
852 wxPointList::compatibility_iterator node
= wx_spline_point_list
.GetFirst();
855 wxPoint
*point
= node
->GetData();
857 wx_spline_point_list
.Erase(node
);
858 node
= wx_spline_point_list
.GetFirst();
862 void wxDCImpl::DoDrawSpline( const wxPointList
*points
)
864 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
867 double cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
;
868 double x1
, y1
, x2
, y2
;
870 wxPointList::compatibility_iterator node
= points
->GetFirst();
875 p
= (wxPoint
*)node
->GetData();
880 node
= node
->GetNext();
885 cx1
= (double)((x1
+ x2
) / 2);
886 cy1
= (double)((y1
+ y2
) / 2);
887 cx2
= (double)((cx1
+ x2
) / 2);
888 cy2
= (double)((cy1
+ y2
) / 2);
890 wx_spline_add_point(x1
, y1
);
892 while ((node
= node
->GetNext())
903 cx4
= (double)(x1
+ x2
) / 2;
904 cy4
= (double)(y1
+ y2
) / 2;
905 cx3
= (double)(x1
+ cx4
) / 2;
906 cy3
= (double)(y1
+ cy4
) / 2;
908 wx_quadratic_spline(cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
);
912 cx2
= (double)(cx1
+ x2
) / 2;
913 cy2
= (double)(cy1
+ y2
) / 2;
916 wx_spline_add_point( cx1
, cy1
);
917 wx_spline_add_point( x2
, y2
);
919 wx_spline_draw_point_array( m_owner
);
922 #endif // wxUSE_SPLINES
926 void wxDCImpl::DoGradientFillLinear(const wxRect
& rect
,
927 const wxColour
& initialColour
,
928 const wxColour
& destColour
,
929 wxDirection nDirection
)
932 wxPen oldPen
= m_pen
;
933 wxBrush oldBrush
= m_brush
;
935 wxUint8 nR1
= initialColour
.Red();
936 wxUint8 nG1
= initialColour
.Green();
937 wxUint8 nB1
= initialColour
.Blue();
938 wxUint8 nR2
= destColour
.Red();
939 wxUint8 nG2
= destColour
.Green();
940 wxUint8 nB2
= destColour
.Blue();
943 if ( nDirection
== wxEAST
|| nDirection
== wxWEST
)
945 wxInt32 x
= rect
.GetWidth();
946 wxInt32 w
= x
; // width of area to shade
947 wxInt32 xDelta
= w
/256; // height of one shade bend
955 nR
= nR1
- (nR1
-nR2
)*(w
-x
)/w
;
957 nR
= nR1
+ (nR2
-nR1
)*(w
-x
)/w
;
960 nG
= nG1
- (nG1
-nG2
)*(w
-x
)/w
;
962 nG
= nG1
+ (nG2
-nG1
)*(w
-x
)/w
;
965 nB
= nB1
- (nB1
-nB2
)*(w
-x
)/w
;
967 nB
= nB1
+ (nB2
-nB1
)*(w
-x
)/w
;
969 wxColour
colour(nR
,nG
,nB
);
970 SetPen(wxPen(colour
, 1, wxSOLID
));
971 SetBrush(wxBrush(colour
));
972 if(nDirection
== wxEAST
)
973 DoDrawRectangle(rect
.GetRight()-x
-xDelta
+1, rect
.GetTop(),
974 xDelta
, rect
.GetHeight());
975 else //nDirection == wxWEST
976 DoDrawRectangle(rect
.GetLeft()+x
, rect
.GetTop(),
977 xDelta
, rect
.GetHeight());
980 else // nDirection == wxNORTH || nDirection == wxSOUTH
982 wxInt32 y
= rect
.GetHeight();
983 wxInt32 w
= y
; // height of area to shade
984 wxInt32 yDelta
= w
/255; // height of one shade bend
992 nR
= nR1
- (nR1
-nR2
)*(w
-y
)/w
;
994 nR
= nR1
+ (nR2
-nR1
)*(w
-y
)/w
;
997 nG
= nG1
- (nG1
-nG2
)*(w
-y
)/w
;
999 nG
= nG1
+ (nG2
-nG1
)*(w
-y
)/w
;
1002 nB
= nB1
- (nB1
-nB2
)*(w
-y
)/w
;
1004 nB
= nB1
+ (nB2
-nB1
)*(w
-y
)/w
;
1006 wxColour
colour(nR
,nG
,nB
);
1007 SetPen(wxPen(colour
, 1, wxSOLID
));
1008 SetBrush(wxBrush(colour
));
1009 if(nDirection
== wxNORTH
)
1010 DoDrawRectangle(rect
.GetLeft(), rect
.GetTop()+y
,
1011 rect
.GetWidth(), yDelta
);
1012 else //nDirection == wxSOUTH
1013 DoDrawRectangle(rect
.GetLeft(), rect
.GetBottom()-y
-yDelta
+1,
1014 rect
.GetWidth(), yDelta
);
1022 void wxDCImpl::DoGradientFillConcentric(const wxRect
& rect
,
1023 const wxColour
& initialColour
,
1024 const wxColour
& destColour
,
1025 const wxPoint
& circleCenter
)
1027 //save the old pen color
1028 wxColour oldPenColour
= m_pen
.GetColour();
1030 wxUint8 nR1
= destColour
.Red();
1031 wxUint8 nG1
= destColour
.Green();
1032 wxUint8 nB1
= destColour
.Blue();
1033 wxUint8 nR2
= initialColour
.Red();
1034 wxUint8 nG2
= initialColour
.Green();
1035 wxUint8 nB2
= initialColour
.Blue();
1040 wxInt32 cx
= rect
.GetWidth() / 2;
1041 wxInt32 cy
= rect
.GetHeight() / 2;
1049 wxInt32 nCircleOffX
= circleCenter
.x
- (rect
.GetWidth() / 2);
1050 wxInt32 nCircleOffY
= circleCenter
.y
- (rect
.GetHeight() / 2);
1052 for ( wxInt32 x
= 0; x
< rect
.GetWidth(); x
++ )
1054 for ( wxInt32 y
= 0; y
< rect
.GetHeight(); y
++ )
1056 //get color difference
1057 wxInt32 nGradient
= ((nRadius
-
1059 pow((double)(x
- cx
- nCircleOffX
), 2) +
1060 pow((double)(y
- cy
- nCircleOffY
), 2)
1061 )) * 100) / nRadius
;
1063 //normalize Gradient
1068 nR
= (wxUint8
)(nR1
+ ((nR2
- nR1
) * nGradient
/ 100));
1069 nG
= (wxUint8
)(nG1
+ ((nG2
- nG1
) * nGradient
/ 100));
1070 nB
= (wxUint8
)(nB1
+ ((nB2
- nB1
) * nGradient
/ 100));
1073 m_pen
.SetColour(wxColour(nR
,nG
,nB
));
1074 DoDrawPoint(x
+ rect
.GetLeft(), y
+ rect
.GetTop());
1077 //return old pen color
1078 m_pen
.SetColour(oldPenColour
);
1081 //-----------------------------------------------------------------------------
1083 //-----------------------------------------------------------------------------
1085 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxObject
)
1087 void wxDC::DrawLabel(const wxString
& text
,
1088 const wxBitmap
& bitmap
,
1092 wxRect
*rectBounding
)
1094 // find the text position
1095 wxCoord widthText
, heightText
, heightLine
;
1096 GetMultiLineTextExtent(text
, &widthText
, &heightText
, &heightLine
);
1098 wxCoord width
, height
;
1101 width
= widthText
+ bitmap
.GetWidth();
1102 height
= bitmap
.GetHeight();
1107 height
= heightText
;
1111 if ( alignment
& wxALIGN_RIGHT
)
1113 x
= rect
.GetRight() - width
;
1115 else if ( alignment
& wxALIGN_CENTRE_HORIZONTAL
)
1117 x
= (rect
.GetLeft() + rect
.GetRight() + 1 - width
) / 2;
1119 else // alignment & wxALIGN_LEFT
1124 if ( alignment
& wxALIGN_BOTTOM
)
1126 y
= rect
.GetBottom() - height
;
1128 else if ( alignment
& wxALIGN_CENTRE_VERTICAL
)
1130 y
= (rect
.GetTop() + rect
.GetBottom() + 1 - height
) / 2;
1132 else // alignment & wxALIGN_TOP
1137 // draw the bitmap first
1143 DrawBitmap(bitmap
, x
, y
, true /* use mask */);
1145 wxCoord offset
= bitmap
.GetWidth() + 4;
1149 y
+= (height
- heightText
) / 2;
1152 // we will draw the underscore under the accel char later
1153 wxCoord startUnderscore
= 0,
1157 // split the string into lines and draw each of them separately
1159 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
1161 if ( *pc
== _T('\n') || pc
== text
.end() )
1163 int xRealStart
= x
; // init it here to avoid compielr warnings
1165 if ( !curLine
.empty() )
1167 // NB: can't test for !(alignment & wxALIGN_LEFT) because
1168 // wxALIGN_LEFT is 0
1169 if ( alignment
& (wxALIGN_RIGHT
| wxALIGN_CENTRE_HORIZONTAL
) )
1172 GetTextExtent(curLine
, &widthLine
, NULL
);
1174 if ( alignment
& wxALIGN_RIGHT
)
1176 xRealStart
+= width
- widthLine
;
1178 else // if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
1180 xRealStart
+= (width
- widthLine
) / 2;
1183 //else: left aligned, nothing to do
1185 DrawText(curLine
, xRealStart
, y
);
1190 // do we have underscore in this line? we can check yUnderscore
1191 // because it is set below to just y + heightLine if we do
1192 if ( y
== yUnderscore
)
1194 // adjust the horz positions to account for the shift
1195 startUnderscore
+= xRealStart
;
1196 endUnderscore
+= xRealStart
;
1199 if ( pc
== text
.end() )
1204 else // not end of line
1206 if ( pc
- text
.begin() == indexAccel
)
1208 // remeber to draw underscore here
1209 GetTextExtent(curLine
, &startUnderscore
, NULL
);
1211 GetTextExtent(curLine
, &endUnderscore
, NULL
);
1213 yUnderscore
= y
+ heightLine
;
1222 // draw the underscore if found
1223 if ( startUnderscore
!= endUnderscore
)
1225 // it should be of the same colour as text
1226 SetPen(wxPen(GetTextForeground(), 0, wxSOLID
));
1230 DrawLine(startUnderscore
, yUnderscore
, endUnderscore
, yUnderscore
);
1233 // return bounding rect if requested
1236 *rectBounding
= wxRect(x
, y
- heightText
, widthText
, heightText
);
1239 CalcBoundingBox(x0
, y0
);
1240 CalcBoundingBox(x0
+ width0
, y0
+ height
);
1243 #if WXWIN_COMPATIBILITY_2_8
1244 // for compatibility with the old code when wxCoord was long everywhere
1245 void wxDC::GetTextExtent(const wxString
& string
,
1248 long *externalLeading
,
1249 const wxFont
*theFont
) const
1251 wxCoord x2
, y2
, descent2
, externalLeading2
;
1252 m_pimpl
->DoGetTextExtent(string
, &x2
, &y2
,
1253 &descent2
, &externalLeading2
,
1260 *descent
= descent2
;
1261 if ( externalLeading
)
1262 *externalLeading
= externalLeading2
;
1265 void wxDC::GetLogicalOrigin(long *x
, long *y
) const
1268 m_pimpl
->DoGetLogicalOrigin(&x2
, &y2
);
1275 void wxDC::GetDeviceOrigin(long *x
, long *y
) const
1278 m_pimpl
->DoGetDeviceOrigin(&x2
, &y2
);
1285 void wxDC::GetClippingBox(long *x
, long *y
, long *w
, long *h
) const
1287 wxCoord xx
,yy
,ww
,hh
;
1288 m_pimpl
->DoGetClippingBox(&xx
, &yy
, &ww
, &hh
);
1295 #endif // WXWIN_COMPATIBILITY_2_8