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"
70 #include "wx/dfb/dcclient.h"
71 #include "wx/dfb/dcmemory.h"
72 #include "wx/dfb/dcscreen.h"
75 //----------------------------------------------------------------------------
77 //----------------------------------------------------------------------------
79 wxDCFactory
*wxDCFactory
::m_factory
= NULL
;
81 void wxDCFactory
::Set(wxDCFactory
*factory
)
88 wxDCFactory
*wxDCFactory
::Get()
91 m_factory
= new wxNativeDCFactory
;
96 class wxDCFactoryCleanupModule
: public wxModule
99 virtual bool OnInit() { return true; }
100 virtual void OnExit() { wxDCFactory
::Set(NULL
); }
103 DECLARE_DYNAMIC_CLASS(wxDCFactoryCleanupModule
)
106 IMPLEMENT_DYNAMIC_CLASS(wxDCFactoryCleanupModule
, wxModule
)
108 //-----------------------------------------------------------------------------
110 //-----------------------------------------------------------------------------
112 wxDCImpl
* wxNativeDCFactory
::CreateWindowDC( wxWindowDC
*owner
)
114 return new wxWindowDCImpl( owner
);
117 wxDCImpl
* wxNativeDCFactory
::CreateWindowDC( wxWindowDC
*owner
, wxWindow
*window
)
119 return new wxWindowDCImpl( owner
, window
);
122 wxDCImpl
* wxNativeDCFactory
::CreateClientDC( wxClientDC
*owner
)
124 return new wxClientDCImpl( owner
);
127 wxDCImpl
* wxNativeDCFactory
::CreateClientDC( wxClientDC
*owner
, wxWindow
*window
)
129 return new wxClientDCImpl( owner
, window
);
132 wxDCImpl
* wxNativeDCFactory
::CreatePaintDC( wxPaintDC
*owner
)
134 return new wxPaintDCImpl( owner
);
137 wxDCImpl
* wxNativeDCFactory
::CreatePaintDC( wxPaintDC
*owner
, wxWindow
*window
)
139 return new wxPaintDCImpl( owner
, window
);
142 wxDCImpl
* wxNativeDCFactory
::CreateMemoryDC( wxMemoryDC
*owner
)
144 return new wxMemoryDCImpl( owner
);
147 wxDCImpl
* wxNativeDCFactory
::CreateMemoryDC( wxMemoryDC
*owner
, wxBitmap
&bitmap
)
149 return new wxMemoryDCImpl( owner
, bitmap
);
152 wxDCImpl
* wxNativeDCFactory
::CreateMemoryDC( wxMemoryDC
*owner
, wxDC
*dc
)
154 return new wxMemoryDCImpl( owner
, dc
);
157 wxDCImpl
* wxNativeDCFactory
::CreateScreenDC( wxScreenDC
*owner
)
159 return new wxScreenDCImpl( owner
);
162 #if wxUSE_PRINTING_ARCHITECTURE
163 wxDCImpl
*wxNativeDCFactory
::CreatePrinterDC( wxPrinterDC
*owner
, const wxPrintData
&data
)
165 wxPrintFactory
*factory
= wxPrintFactory
::GetFactory();
166 return factory
->CreatePrinterDCImpl( owner
, data
);
170 //-----------------------------------------------------------------------------
172 //-----------------------------------------------------------------------------
174 IMPLEMENT_ABSTRACT_CLASS(wxWindowDC
, wxDC
)
176 wxWindowDC
::wxWindowDC(wxWindow
*win
)
177 : wxDC(wxDCFactory
::Get()->CreateWindowDC(this, win
))
181 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
185 IMPLEMENT_ABSTRACT_CLASS(wxClientDC
, wxWindowDC
)
187 wxClientDC
::wxClientDC(wxWindow
*win
)
188 : wxWindowDC(wxDCFactory
::Get()->CreateClientDC(this, win
))
192 //-----------------------------------------------------------------------------
194 //-----------------------------------------------------------------------------
196 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC
, wxDC
)
198 wxMemoryDC
::wxMemoryDC()
199 : wxDC(wxDCFactory
::Get()->CreateMemoryDC(this))
203 wxMemoryDC
::wxMemoryDC(wxBitmap
& bitmap
)
204 : wxDC(wxDCFactory
::Get()->CreateMemoryDC(this, bitmap
))
208 wxMemoryDC
::wxMemoryDC(wxDC
*dc
)
209 : wxDC(wxDCFactory
::Get()->CreateMemoryDC(this, dc
))
213 void wxMemoryDC
::SelectObject(wxBitmap
& bmp
)
215 // make sure that the given wxBitmap is not sharing its data with other
216 // wxBitmap instances as its contents will be modified by any drawing
217 // operation done on this DC
221 GetImpl()->DoSelect(bmp
);
224 void wxMemoryDC
::SelectObjectAsSource(const wxBitmap
& bmp
)
226 GetImpl()->DoSelect(bmp
);
229 const wxBitmap
& wxMemoryDC
::GetSelectedBitmap() const
231 return GetImpl()->GetSelectedBitmap();
234 wxBitmap
& wxMemoryDC
::GetSelectedBitmap()
236 return GetImpl()->GetSelectedBitmap();
240 //-----------------------------------------------------------------------------
242 //-----------------------------------------------------------------------------
244 IMPLEMENT_ABSTRACT_CLASS(wxPaintDC
, wxClientDC
)
246 wxPaintDC
::wxPaintDC(wxWindow
*win
)
247 : wxClientDC(wxDCFactory
::Get()->CreatePaintDC(this, win
))
251 //-----------------------------------------------------------------------------
253 //-----------------------------------------------------------------------------
255 IMPLEMENT_DYNAMIC_CLASS(wxScreenDC
, wxWindowDC
)
257 wxScreenDC
::wxScreenDC()
258 : wxDC(wxDCFactory
::Get()->CreateScreenDC(this))
262 //-----------------------------------------------------------------------------
264 //-----------------------------------------------------------------------------
266 #if wxUSE_PRINTING_ARCHITECTURE
268 IMPLEMENT_DYNAMIC_CLASS(wxPrinterDC
, wxDC
)
270 wxPrinterDC
::wxPrinterDC()
271 : wxDC(wxDCFactory
::Get()->CreatePrinterDC(this, wxPrintData()))
275 wxPrinterDC
::wxPrinterDC(const wxPrintData
& data
)
276 : wxDC(wxDCFactory
::Get()->CreatePrinterDC(this, data
))
280 wxRect wxPrinterDC
::GetPaperRect()
282 return GetImpl()->GetPaperRect();
285 int wxPrinterDC
::GetResolution()
287 return GetImpl()->GetResolution();
290 #endif // wxUSE_PRINTING_ARCHITECTURE
292 //-----------------------------------------------------------------------------
294 //-----------------------------------------------------------------------------
296 IMPLEMENT_ABSTRACT_CLASS(wxDCImpl
, wxObject
)
298 wxDCImpl
::wxDCImpl( wxDC
*owner
)
300 , m_colour(wxColourDisplay())
304 , m_isBBoxValid(false)
305 , m_logicalOriginX(0), m_logicalOriginY(0)
306 , m_deviceOriginX(0), m_deviceOriginY(0)
307 , m_deviceLocalOriginX(0), m_deviceLocalOriginY(0)
308 , m_logicalScaleX(1.0), m_logicalScaleY(1.0)
309 , m_userScaleX(1.0), m_userScaleY(1.0)
310 , m_scaleX(1.0), m_scaleY(1.0)
311 , m_signX(1), m_signY(1)
312 , m_minX(0), m_minY(0), m_maxX(0), m_maxY(0)
313 , m_clipX1(0), m_clipY1(0), m_clipX2(0), m_clipY2(0)
314 , m_logicalFunction(wxCOPY
)
315 , m_backgroundMode(wxTRANSPARENT
)
316 , m_mappingMode(wxMM_TEXT
)
319 , m_backgroundBrush(*wxTRANSPARENT_BRUSH
)
320 , m_textForegroundColour(*wxBLACK
)
321 , m_textBackgroundColour(*wxWHITE
)
325 , m_hasCustomPalette(false)
326 #endif // wxUSE_PALETTE
330 m_mm_to_pix_x
= (double)wxGetDisplaySize().GetWidth() /
331 (double)wxGetDisplaySizeMM().GetWidth();
332 m_mm_to_pix_y
= (double)wxGetDisplaySize().GetHeight() /
333 (double)wxGetDisplaySizeMM().GetHeight();
339 wxDCImpl
::~wxDCImpl()
343 // ----------------------------------------------------------------------------
344 // coordinate conversions and transforms
345 // ----------------------------------------------------------------------------
347 wxCoord wxDCImpl
::DeviceToLogicalX(wxCoord x
) const
349 return wxRound((double)(x
- m_deviceOriginX
- m_deviceLocalOriginX
) / m_scaleX
) * m_signX
+ m_logicalOriginX
;
352 wxCoord wxDCImpl
::DeviceToLogicalY(wxCoord y
) const
354 return wxRound((double)(y
- m_deviceOriginY
- m_deviceLocalOriginY
) / m_scaleY
) * m_signY
+ m_logicalOriginY
;
357 wxCoord wxDCImpl
::DeviceToLogicalXRel(wxCoord x
) const
359 return wxRound((double)(x
) / m_scaleX
);
362 wxCoord wxDCImpl
::DeviceToLogicalYRel(wxCoord y
) const
364 return wxRound((double)(y
) / m_scaleY
);
367 wxCoord wxDCImpl
::LogicalToDeviceX(wxCoord x
) const
369 return wxRound((double)(x
- m_logicalOriginX
) * m_scaleX
) * m_signX
+ m_deviceOriginX
* m_signY
+ m_deviceLocalOriginX
;
372 wxCoord wxDCImpl
::LogicalToDeviceY(wxCoord y
) const
374 return wxRound((double)(y
- m_logicalOriginY
) * m_scaleY
) * m_signY
+ m_deviceOriginY
* m_signY
+ m_deviceLocalOriginY
;
377 wxCoord wxDCImpl
::LogicalToDeviceXRel(wxCoord x
) const
379 return wxRound((double)(x
) * m_scaleX
);
382 wxCoord wxDCImpl
::LogicalToDeviceYRel(wxCoord y
) const
384 return wxRound((double)(y
) * m_scaleY
);
387 void wxDCImpl
::ComputeScaleAndOrigin()
389 m_scaleX
= m_logicalScaleX
* m_userScaleX
;
390 m_scaleY
= m_logicalScaleY
* m_userScaleY
;
393 void wxDCImpl
::SetMapMode( int mode
)
398 SetLogicalScale( twips2mm
*m_mm_to_pix_x
, twips2mm
*m_mm_to_pix_y
);
401 SetLogicalScale( pt2mm
*m_mm_to_pix_x
, pt2mm
*m_mm_to_pix_y
);
404 SetLogicalScale( m_mm_to_pix_x
, m_mm_to_pix_y
);
407 SetLogicalScale( m_mm_to_pix_x
/10.0, m_mm_to_pix_y
/10.0 );
411 SetLogicalScale( 1.0, 1.0 );
414 m_mappingMode
= mode
;
417 void wxDCImpl
::SetUserScale( double x
, double y
)
419 // allow negative ? -> no
422 ComputeScaleAndOrigin();
425 void wxDCImpl
::SetLogicalScale( double x
, double y
)
430 ComputeScaleAndOrigin();
433 void wxDCImpl
::SetLogicalOrigin( wxCoord x
, wxCoord y
)
435 m_logicalOriginX
= x
* m_signX
;
436 m_logicalOriginY
= y
* m_signY
;
437 ComputeScaleAndOrigin();
440 void wxDCImpl
::SetDeviceOrigin( wxCoord x
, wxCoord y
)
444 ComputeScaleAndOrigin();
447 void wxDCImpl
::SetDeviceLocalOrigin( wxCoord x
, wxCoord y
)
449 m_deviceLocalOriginX
= x
;
450 m_deviceLocalOriginY
= y
;
451 ComputeScaleAndOrigin();
454 void wxDCImpl
::SetAxisOrientation( bool xLeftRight
, bool yBottomUp
)
456 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
457 // wxWidgets 2.9: no longer override it
458 m_signX
= (xLeftRight ?
1 : -1);
459 m_signY
= (yBottomUp ?
-1 : 1);
460 ComputeScaleAndOrigin();
464 // Each element of the widths array will be the width of the string up to and
465 // including the corresponding character in text. This is the generic
466 // implementation, the port-specific classes should do this with native APIs
467 // if available and if faster. Note: pango_layout_index_to_pos is much slower
468 // than calling GetTextExtent!!
475 FontWidthCache() : m_scaleX(1), m_widths(NULL
) { }
476 ~FontWidthCache() { delete []m_widths
; }
481 m_widths
= new int[FWC_SIZE
];
483 memset(m_widths
, 0, sizeof(int)*FWC_SIZE
);
491 static FontWidthCache s_fontWidthCache
;
493 bool wxDCImpl
::DoGetPartialTextExtents(const wxString
& text
, wxArrayInt
& widths
) const
497 const size_t len
= text
.length();
501 // reset the cache if font or horizontal scale have changed
502 if ( !s_fontWidthCache
.m_widths
||
503 !wxIsSameDouble(s_fontWidthCache
.m_scaleX
, m_scaleX
) ||
504 (s_fontWidthCache
.m_font
!= GetFont()) )
506 s_fontWidthCache
.Reset();
507 s_fontWidthCache
.m_font
= GetFont();
508 s_fontWidthCache
.m_scaleX
= m_scaleX
;
511 // Calculate the position of each character based on the widths of
512 // the previous characters
514 for ( size_t i
= 0; i
< len
; i
++ )
516 const wxChar c
= text
[i
];
517 unsigned int c_int
= (unsigned int)c
;
519 if ((c_int
< FWC_SIZE
) && (s_fontWidthCache
.m_widths
[c_int
] != 0))
521 w
= s_fontWidthCache
.m_widths
[c_int
];
525 DoGetTextExtent(c
, &w
, &h
);
526 if (c_int
< FWC_SIZE
)
527 s_fontWidthCache
.m_widths
[c_int
] = w
;
531 widths
[i
] = totalWidth
;
537 void wxDCImpl
::GetMultiLineTextExtent(const wxString
& text
,
541 const wxFont
*font
) const
543 wxCoord widthTextMax
= 0, widthLine
,
544 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
547 for ( wxString
::const_iterator pc
= text
.begin(); ; ++pc
)
549 if ( pc
== text
.end() || *pc
== _T('\n') )
551 if ( curLine
.empty() )
553 // we can't use GetTextExtent - it will return 0 for both width
554 // and height and an empty line should count in height
557 // assume that this line has the same height as the previous
559 if ( !heightLineDefault
)
560 heightLineDefault
= heightLine
;
562 if ( !heightLineDefault
)
564 // but we don't know it yet - choose something reasonable
565 DoGetTextExtent(_T("W"), NULL
, &heightLineDefault
,
569 heightTextTotal
+= heightLineDefault
;
573 DoGetTextExtent(curLine
, &widthLine
, &heightLine
,
575 if ( widthLine
> widthTextMax
)
576 widthTextMax
= widthLine
;
577 heightTextTotal
+= heightLine
;
580 if ( pc
== text
.end() )
598 *y
= heightTextTotal
;
603 void wxDCImpl
::DoDrawCheckMark(wxCoord x1
, wxCoord y1
,
604 wxCoord width
, wxCoord height
)
606 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
608 wxCoord x2
= x1
+ width
,
611 // the pen width is calibrated to give 3 for width == height == 10
612 wxDCPenChanger
pen( *m_owner
, wxPen(GetTextForeground(), (width
+ height
+ 1)/7));
614 // we're drawing a scaled version of wx/generic/tick.xpm here
615 wxCoord x3
= x1
+ (4*width
) / 10, // x of the tick bottom
616 y3
= y1
+ height
/ 2; // y of the left tick branch
617 DoDrawLine(x1
, y3
, x3
, y2
);
618 DoDrawLine(x3
, y2
, x2
, y1
);
620 CalcBoundingBox(x1
, y1
);
621 CalcBoundingBox(x2
, y2
);
625 wxDCImpl
::DoStretchBlit(wxCoord xdest
, wxCoord ydest
,
626 wxCoord dstWidth
, wxCoord dstHeight
,
628 wxCoord xsrc
, wxCoord ysrc
,
629 wxCoord srcWidth
, wxCoord srcHeight
,
635 wxCHECK_MSG( srcWidth
&& srcHeight
&& dstWidth
&& dstHeight
, false,
636 _T("invalid blit size") );
638 // emulate the stretching by modifying the DC scale
639 double xscale
= (double)srcWidth
/dstWidth
,
640 yscale
= (double)srcHeight
/dstHeight
;
642 double xscaleOld
, yscaleOld
;
643 GetUserScale(&xscaleOld
, &yscaleOld
);
644 SetUserScale(xscaleOld
/xscale
, yscaleOld
/yscale
);
646 bool rc
= DoBlit(wxCoord(xdest
*xscale
), wxCoord(ydest
*yscale
),
647 wxCoord(dstWidth
*xscale
), wxCoord(dstHeight
*yscale
),
649 xsrc
, ysrc
, rop
, useMask
, xsrcMask
, ysrcMask
);
651 SetUserScale(xscaleOld
, yscaleOld
);
656 void wxDCImpl
::DrawLines(const wxPointList
*list
, wxCoord xoffset
, wxCoord yoffset
)
658 int n
= list
->GetCount();
659 wxPoint
*points
= new wxPoint
[n
];
662 for ( wxPointList
::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
664 wxPoint
*point
= node
->GetData();
665 points
[i
].x
= point
->x
;
666 points
[i
].y
= point
->y
;
669 DoDrawLines(n
, points
, xoffset
, yoffset
);
674 void wxDCImpl
::DrawPolygon(const wxPointList
*list
,
675 wxCoord xoffset
, wxCoord yoffset
,
678 int n
= list
->GetCount();
679 wxPoint
*points
= new wxPoint
[n
];
682 for ( wxPointList
::compatibility_iterator node
= list
->GetFirst(); node
; node
= node
->GetNext(), i
++ )
684 wxPoint
*point
= node
->GetData();
685 points
[i
].x
= point
->x
;
686 points
[i
].y
= point
->y
;
689 DoDrawPolygon(n
, points
, xoffset
, yoffset
, fillStyle
);
695 wxDCImpl
::DoDrawPolyPolygon(int n
,
698 wxCoord xoffset
, wxCoord yoffset
,
703 DoDrawPolygon(count
[0], points
, xoffset
, yoffset
, fillStyle
);
711 for (i
= j
= lastOfs
= 0; i
< n
; i
++)
716 pts
= new wxPoint
[j
+n
-1];
717 for (i
= 0; i
< j
; i
++)
719 for (i
= 2; i
<= n
; i
++)
721 lastOfs
-= count
[n
-i
];
722 pts
[j
++] = pts
[lastOfs
];
726 SetPen(wxPen(*wxBLACK
, 0, wxTRANSPARENT
));
727 DoDrawPolygon(j
, pts
, xoffset
, yoffset
, fillStyle
);
729 for (i
= j
= 0; i
< n
; i
++)
731 DoDrawLines(count
[i
], pts
+j
, xoffset
, yoffset
);
739 void wxDCImpl
::DoDrawSpline(wxCoord x1
, wxCoord y1
, wxCoord x2
, wxCoord y2
, wxCoord x3
, wxCoord y3
)
741 wxPointList point_list
;
743 wxPoint
*point1
= new wxPoint
;
744 point1
->x
= x1
; point1
->y
= y1
;
745 point_list
.Append( point1
);
747 wxPoint
*point2
= new wxPoint
;
748 point2
->x
= x2
; point2
->y
= y2
;
749 point_list
.Append( point2
);
751 wxPoint
*point3
= new wxPoint
;
752 point3
->x
= x3
; point3
->y
= y3
;
753 point_list
.Append( point3
);
755 DoDrawSpline(&point_list
);
757 for( wxPointList
::compatibility_iterator node
= point_list
.GetFirst(); node
; node
= node
->GetNext() )
759 wxPoint
*p
= node
->GetData();
764 void wxDCImpl
::DoDrawSpline(int n
, wxPoint points
[])
767 for (int i
=0; i
< n
; i
++)
768 list
.Append( &points
[i
] );
773 // ----------------------------------- spline code ----------------------------------------
775 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
,
776 double a3
, double b3
, double a4
, double b4
);
777 void wx_clear_stack();
778 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
, double *x3
,
779 double *y3
, double *x4
, double *y4
);
780 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
,
781 double x4
, double y4
);
782 static bool wx_spline_add_point(double x
, double y
);
783 static void wx_spline_draw_point_array(wxDC
*dc
);
785 wxPointList wx_spline_point_list
;
787 #define half(z1, z2) ((z1+z2)/2.0)
790 /* iterative version */
792 void wx_quadratic_spline(double a1
, double b1
, double a2
, double b2
, double a3
, double b3
, double a4
,
795 register double xmid
, ymid
;
796 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
799 wx_spline_push(a1
, b1
, a2
, b2
, a3
, b3
, a4
, b4
);
801 while (wx_spline_pop(&x1
, &y1
, &x2
, &y2
, &x3
, &y3
, &x4
, &y4
)) {
802 xmid
= (double)half(x2
, x3
);
803 ymid
= (double)half(y2
, y3
);
804 if (fabs(x1
- xmid
) < THRESHOLD
&& fabs(y1
- ymid
) < THRESHOLD
&&
805 fabs(xmid
- x4
) < THRESHOLD
&& fabs(ymid
- y4
) < THRESHOLD
) {
806 wx_spline_add_point( x1
, y1
);
807 wx_spline_add_point( xmid
, ymid
);
809 wx_spline_push(xmid
, ymid
, (double)half(xmid
, x3
), (double)half(ymid
, y3
),
810 (double)half(x3
, x4
), (double)half(y3
, y4
), x4
, y4
);
811 wx_spline_push(x1
, y1
, (double)half(x1
, x2
), (double)half(y1
, y2
),
812 (double)half(x2
, xmid
), (double)half(y2
, ymid
), xmid
, ymid
);
817 /* utilities used by spline drawing routines */
819 typedef struct wx_spline_stack_struct
{
820 double x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
;
823 #define SPLINE_STACK_DEPTH 20
824 static Stack wx_spline_stack
[SPLINE_STACK_DEPTH
];
825 static Stack
*wx_stack_top
;
826 static int wx_stack_count
;
828 void wx_clear_stack()
830 wx_stack_top
= wx_spline_stack
;
834 void wx_spline_push(double x1
, double y1
, double x2
, double y2
, double x3
, double y3
, double x4
, double y4
)
836 wx_stack_top
->x1
= x1
;
837 wx_stack_top
->y1
= y1
;
838 wx_stack_top
->x2
= x2
;
839 wx_stack_top
->y2
= y2
;
840 wx_stack_top
->x3
= x3
;
841 wx_stack_top
->y3
= y3
;
842 wx_stack_top
->x4
= x4
;
843 wx_stack_top
->y4
= y4
;
848 int wx_spline_pop(double *x1
, double *y1
, double *x2
, double *y2
,
849 double *x3
, double *y3
, double *x4
, double *y4
)
851 if (wx_stack_count
== 0)
855 *x1
= wx_stack_top
->x1
;
856 *y1
= wx_stack_top
->y1
;
857 *x2
= wx_stack_top
->x2
;
858 *y2
= wx_stack_top
->y2
;
859 *x3
= wx_stack_top
->x3
;
860 *y3
= wx_stack_top
->y3
;
861 *x4
= wx_stack_top
->x4
;
862 *y4
= wx_stack_top
->y4
;
866 static bool wx_spline_add_point(double x
, double y
)
868 wxPoint
*point
= new wxPoint( wxRound(x
), wxRound(y
) );
869 wx_spline_point_list
.Append(point
);
873 static void wx_spline_draw_point_array(wxDC
*dc
)
875 dc
->DrawLines(&wx_spline_point_list
, 0, 0 );
876 wxPointList
::compatibility_iterator node
= wx_spline_point_list
.GetFirst();
879 wxPoint
*point
= node
->GetData();
881 wx_spline_point_list
.Erase(node
);
882 node
= wx_spline_point_list
.GetFirst();
886 void wxDCImpl
::DoDrawSpline( const wxPointList
*points
)
888 wxCHECK_RET( IsOk(), wxT("invalid window dc") );
891 double cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
;
892 double x1
, y1
, x2
, y2
;
894 wxPointList
::compatibility_iterator node
= points
->GetFirst();
899 p
= (wxPoint
*)node
->GetData();
904 node
= node
->GetNext();
909 cx1
= (double)((x1
+ x2
) / 2);
910 cy1
= (double)((y1
+ y2
) / 2);
911 cx2
= (double)((cx1
+ x2
) / 2);
912 cy2
= (double)((cy1
+ y2
) / 2);
914 wx_spline_add_point(x1
, y1
);
916 while ((node
= node
->GetNext())
927 cx4
= (double)(x1
+ x2
) / 2;
928 cy4
= (double)(y1
+ y2
) / 2;
929 cx3
= (double)(x1
+ cx4
) / 2;
930 cy3
= (double)(y1
+ cy4
) / 2;
932 wx_quadratic_spline(cx1
, cy1
, cx2
, cy2
, cx3
, cy3
, cx4
, cy4
);
936 cx2
= (double)(cx1
+ x2
) / 2;
937 cy2
= (double)(cy1
+ y2
) / 2;
940 wx_spline_add_point( cx1
, cy1
);
941 wx_spline_add_point( x2
, y2
);
943 wx_spline_draw_point_array( m_owner
);
946 #endif // wxUSE_SPLINES
950 void wxDCImpl
::DoGradientFillLinear(const wxRect
& rect
,
951 const wxColour
& initialColour
,
952 const wxColour
& destColour
,
953 wxDirection nDirection
)
956 wxPen oldPen
= m_pen
;
957 wxBrush oldBrush
= m_brush
;
959 wxUint8 nR1
= initialColour
.Red();
960 wxUint8 nG1
= initialColour
.Green();
961 wxUint8 nB1
= initialColour
.Blue();
962 wxUint8 nR2
= destColour
.Red();
963 wxUint8 nG2
= destColour
.Green();
964 wxUint8 nB2
= destColour
.Blue();
967 if ( nDirection
== wxEAST
|| nDirection
== wxWEST
)
969 wxInt32 x
= rect
.GetWidth();
970 wxInt32 w
= x
; // width of area to shade
971 wxInt32 xDelta
= w
/256; // height of one shade bend
979 nR
= nR1
- (nR1
-nR2
)*(w
-x
)/w
;
981 nR
= nR1
+ (nR2
-nR1
)*(w
-x
)/w
;
984 nG
= nG1
- (nG1
-nG2
)*(w
-x
)/w
;
986 nG
= nG1
+ (nG2
-nG1
)*(w
-x
)/w
;
989 nB
= nB1
- (nB1
-nB2
)*(w
-x
)/w
;
991 nB
= nB1
+ (nB2
-nB1
)*(w
-x
)/w
;
993 wxColour
colour(nR
,nG
,nB
);
994 SetPen(wxPen(colour
, 1, wxSOLID
));
995 SetBrush(wxBrush(colour
));
996 if(nDirection
== wxEAST
)
997 DoDrawRectangle(rect
.GetRight()-x
-xDelta
+1, rect
.GetTop(),
998 xDelta
, rect
.GetHeight());
999 else //nDirection == wxWEST
1000 DoDrawRectangle(rect
.GetLeft()+x
, rect
.GetTop(),
1001 xDelta
, rect
.GetHeight());
1004 else // nDirection == wxNORTH || nDirection == wxSOUTH
1006 wxInt32 y
= rect
.GetHeight();
1007 wxInt32 w
= y
; // height of area to shade
1008 wxInt32 yDelta
= w
/255; // height of one shade bend
1016 nR
= nR1
- (nR1
-nR2
)*(w
-y
)/w
;
1018 nR
= nR1
+ (nR2
-nR1
)*(w
-y
)/w
;
1021 nG
= nG1
- (nG1
-nG2
)*(w
-y
)/w
;
1023 nG
= nG1
+ (nG2
-nG1
)*(w
-y
)/w
;
1026 nB
= nB1
- (nB1
-nB2
)*(w
-y
)/w
;
1028 nB
= nB1
+ (nB2
-nB1
)*(w
-y
)/w
;
1030 wxColour
colour(nR
,nG
,nB
);
1031 SetPen(wxPen(colour
, 1, wxSOLID
));
1032 SetBrush(wxBrush(colour
));
1033 if(nDirection
== wxNORTH
)
1034 DoDrawRectangle(rect
.GetLeft(), rect
.GetTop()+y
,
1035 rect
.GetWidth(), yDelta
);
1036 else //nDirection == wxSOUTH
1037 DoDrawRectangle(rect
.GetLeft(), rect
.GetBottom()-y
-yDelta
+1,
1038 rect
.GetWidth(), yDelta
);
1046 void wxDCImpl
::DoGradientFillConcentric(const wxRect
& rect
,
1047 const wxColour
& initialColour
,
1048 const wxColour
& destColour
,
1049 const wxPoint
& circleCenter
)
1051 //save the old pen color
1052 wxColour oldPenColour
= m_pen
.GetColour();
1054 wxUint8 nR1
= destColour
.Red();
1055 wxUint8 nG1
= destColour
.Green();
1056 wxUint8 nB1
= destColour
.Blue();
1057 wxUint8 nR2
= initialColour
.Red();
1058 wxUint8 nG2
= initialColour
.Green();
1059 wxUint8 nB2
= initialColour
.Blue();
1064 wxInt32 cx
= rect
.GetWidth() / 2;
1065 wxInt32 cy
= rect
.GetHeight() / 2;
1073 wxInt32 nCircleOffX
= circleCenter
.x
- (rect
.GetWidth() / 2);
1074 wxInt32 nCircleOffY
= circleCenter
.y
- (rect
.GetHeight() / 2);
1076 for ( wxInt32 x
= 0; x
< rect
.GetWidth(); x
++ )
1078 for ( wxInt32 y
= 0; y
< rect
.GetHeight(); y
++ )
1080 //get color difference
1081 wxInt32 nGradient
= ((nRadius
-
1083 pow((double)(x
- cx
- nCircleOffX
), 2) +
1084 pow((double)(y
- cy
- nCircleOffY
), 2)
1085 )) * 100) / nRadius
;
1087 //normalize Gradient
1092 nR
= (wxUint8
)(nR1
+ ((nR2
- nR1
) * nGradient
/ 100));
1093 nG
= (wxUint8
)(nG1
+ ((nG2
- nG1
) * nGradient
/ 100));
1094 nB
= (wxUint8
)(nB1
+ ((nB2
- nB1
) * nGradient
/ 100));
1097 m_pen
.SetColour(wxColour(nR
,nG
,nB
));
1098 DoDrawPoint(x
+ rect
.GetLeft(), y
+ rect
.GetTop());
1101 //return old pen color
1102 m_pen
.SetColour(oldPenColour
);
1105 //-----------------------------------------------------------------------------
1107 //-----------------------------------------------------------------------------
1109 IMPLEMENT_ABSTRACT_CLASS(wxDC
, wxObject
)
1111 void wxDC
::DrawLabel(const wxString
& text
,
1112 const wxBitmap
& bitmap
,
1116 wxRect
*rectBounding
)
1118 // find the text position
1119 wxCoord widthText
, heightText
, heightLine
;
1120 GetMultiLineTextExtent(text
, &widthText
, &heightText
, &heightLine
);
1122 wxCoord width
, height
;
1125 width
= widthText
+ bitmap
.GetWidth();
1126 height
= bitmap
.GetHeight();
1131 height
= heightText
;
1135 if ( alignment
& wxALIGN_RIGHT
)
1137 x
= rect
.GetRight() - width
;
1139 else if ( alignment
& wxALIGN_CENTRE_HORIZONTAL
)
1141 x
= (rect
.GetLeft() + rect
.GetRight() + 1 - width
) / 2;
1143 else // alignment & wxALIGN_LEFT
1148 if ( alignment
& wxALIGN_BOTTOM
)
1150 y
= rect
.GetBottom() - height
;
1152 else if ( alignment
& wxALIGN_CENTRE_VERTICAL
)
1154 y
= (rect
.GetTop() + rect
.GetBottom() + 1 - height
) / 2;
1156 else // alignment & wxALIGN_TOP
1161 // draw the bitmap first
1167 DrawBitmap(bitmap
, x
, y
, true /* use mask */);
1169 wxCoord offset
= bitmap
.GetWidth() + 4;
1173 y
+= (height
- heightText
) / 2;
1176 // we will draw the underscore under the accel char later
1177 wxCoord startUnderscore
= 0,
1181 // split the string into lines and draw each of them separately
1183 for ( wxString
::const_iterator pc
= text
.begin(); ; ++pc
)
1185 if ( *pc
== _T('\n') || pc
== text
.end() )
1187 int xRealStart
= x
; // init it here to avoid compielr warnings
1189 if ( !curLine
.empty() )
1191 // NB: can't test for !(alignment & wxALIGN_LEFT) because
1192 // wxALIGN_LEFT is 0
1193 if ( alignment
& (wxALIGN_RIGHT
| wxALIGN_CENTRE_HORIZONTAL
) )
1196 GetTextExtent(curLine
, &widthLine
, NULL
);
1198 if ( alignment
& wxALIGN_RIGHT
)
1200 xRealStart
+= width
- widthLine
;
1202 else // if ( alignment & wxALIGN_CENTRE_HORIZONTAL )
1204 xRealStart
+= (width
- widthLine
) / 2;
1207 //else: left aligned, nothing to do
1209 DrawText(curLine
, xRealStart
, y
);
1214 // do we have underscore in this line? we can check yUnderscore
1215 // because it is set below to just y + heightLine if we do
1216 if ( y
== yUnderscore
)
1218 // adjust the horz positions to account for the shift
1219 startUnderscore
+= xRealStart
;
1220 endUnderscore
+= xRealStart
;
1223 if ( pc
== text
.end() )
1228 else // not end of line
1230 if ( pc
- text
.begin() == indexAccel
)
1232 // remeber to draw underscore here
1233 GetTextExtent(curLine
, &startUnderscore
, NULL
);
1235 GetTextExtent(curLine
, &endUnderscore
, NULL
);
1237 yUnderscore
= y
+ heightLine
;
1246 // draw the underscore if found
1247 if ( startUnderscore
!= endUnderscore
)
1249 // it should be of the same colour as text
1250 SetPen(wxPen(GetTextForeground(), 0, wxSOLID
));
1254 DrawLine(startUnderscore
, yUnderscore
, endUnderscore
, yUnderscore
);
1257 // return bounding rect if requested
1260 *rectBounding
= wxRect(x
, y
- heightText
, widthText
, heightText
);
1263 CalcBoundingBox(x0
, y0
);
1264 CalcBoundingBox(x0
+ width0
, y0
+ height
);
1267 #if WXWIN_COMPATIBILITY_2_8
1268 // for compatibility with the old code when wxCoord was long everywhere
1269 void wxDC
::GetTextExtent(const wxString
& string
,
1272 long *externalLeading
,
1273 const wxFont
*theFont
) const
1275 wxCoord x2
, y2
, descent2
, externalLeading2
;
1276 m_pimpl
->DoGetTextExtent(string
, &x2
, &y2
,
1277 &descent2
, &externalLeading2
,
1284 *descent
= descent2
;
1285 if ( externalLeading
)
1286 *externalLeading
= externalLeading2
;
1289 void wxDC
::GetLogicalOrigin(long *x
, long *y
) const
1292 m_pimpl
->DoGetLogicalOrigin(&x2
, &y2
);
1299 void wxDC
::GetDeviceOrigin(long *x
, long *y
) const
1302 m_pimpl
->DoGetDeviceOrigin(&x2
, &y2
);
1309 void wxDC
::GetClippingBox(long *x
, long *y
, long *w
, long *h
) const
1311 wxCoord xx
,yy
,ww
,hh
;
1312 m_pimpl
->DoGetClippingBox(&xx
, &yy
, &ww
, &hh
);
1319 #endif // WXWIN_COMPATIBILITY_2_8