+//-----------------------------------------------------------------------------
+// wxWindowDC
+//-----------------------------------------------------------------------------
+
+IMPLEMENT_ABSTRACT_CLASS(wxWindowDC, wxDC)
+
+wxWindowDC::wxWindowDC(wxWindow *win)
+ : wxDC(wxDCFactory::Get()->CreateWindowDC(this, win))
+{
+}
+
+//-----------------------------------------------------------------------------
+// wxClientDC
+//-----------------------------------------------------------------------------
+
+IMPLEMENT_ABSTRACT_CLASS(wxClientDC, wxWindowDC)
+
+wxClientDC::wxClientDC(wxWindow *win)
+ : wxWindowDC(wxDCFactory::Get()->CreateClientDC(this, win))
+{
+}
+
+//-----------------------------------------------------------------------------
+// wxMemoryDC
+//-----------------------------------------------------------------------------
+
+IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC, wxDC)
+
+wxMemoryDC::wxMemoryDC()
+ : wxDC(wxDCFactory::Get()->CreateMemoryDC(this))
+{
+}
+
+wxMemoryDC::wxMemoryDC(wxBitmap& bitmap)
+ : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, bitmap))
+{
+}
+
+wxMemoryDC::wxMemoryDC(wxDC *dc)
+ : wxDC(wxDCFactory::Get()->CreateMemoryDC(this, dc))
+{
+}
+
+void wxMemoryDC::SelectObject(wxBitmap& bmp)
+{
+ if ( bmp.IsSameAs(GetSelectedBitmap()) )
+ {
+ // Nothing to do, this bitmap is already selected.
+ return;
+ }
+
+ // make sure that the given wxBitmap is not sharing its data with other
+ // wxBitmap instances as its contents will be modified by any drawing
+ // operation done on this DC
+ if (bmp.IsOk())
+ bmp.UnShare();
+
+ GetImpl()->DoSelect(bmp);
+}
+
+void wxMemoryDC::SelectObjectAsSource(const wxBitmap& bmp)
+{
+ GetImpl()->DoSelect(bmp);
+}
+
+const wxBitmap& wxMemoryDC::GetSelectedBitmap() const
+{
+ return GetImpl()->GetSelectedBitmap();
+}
+
+wxBitmap& wxMemoryDC::GetSelectedBitmap()
+{
+ return GetImpl()->GetSelectedBitmap();
+}
+
+
+//-----------------------------------------------------------------------------
+// wxPaintDC
+//-----------------------------------------------------------------------------
+
+IMPLEMENT_ABSTRACT_CLASS(wxPaintDC, wxClientDC)
+
+wxPaintDC::wxPaintDC(wxWindow *win)
+ : wxClientDC(wxDCFactory::Get()->CreatePaintDC(this, win))
+{
+}
+
+//-----------------------------------------------------------------------------
+// wxScreenDC
+//-----------------------------------------------------------------------------
+
+IMPLEMENT_DYNAMIC_CLASS(wxScreenDC, wxWindowDC)
+
+wxScreenDC::wxScreenDC()
+ : wxDC(wxDCFactory::Get()->CreateScreenDC(this))
+{
+}
+
+//-----------------------------------------------------------------------------
+// wxPrinterDC
+//-----------------------------------------------------------------------------
+
+#if wxUSE_PRINTING_ARCHITECTURE
+
+IMPLEMENT_DYNAMIC_CLASS(wxPrinterDC, wxDC)
+
+wxPrinterDC::wxPrinterDC()
+ : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, wxPrintData()))
+{
+}
+
+wxPrinterDC::wxPrinterDC(const wxPrintData& data)
+ : wxDC(wxDCFactory::Get()->CreatePrinterDC(this, data))
+{
+}
+
+wxRect wxPrinterDC::GetPaperRect() const
+{
+ return GetImpl()->GetPaperRect();
+}
+
+int wxPrinterDC::GetResolution() const
+{
+ return GetImpl()->GetResolution();
+}
+
+#endif // wxUSE_PRINTING_ARCHITECTURE
+
+//-----------------------------------------------------------------------------
+// wxDCImpl
+//-----------------------------------------------------------------------------
+
+IMPLEMENT_ABSTRACT_CLASS(wxDCImpl, wxObject)
+
+wxDCImpl::wxDCImpl( wxDC *owner )
+ : m_window(NULL)
+ , m_colour(wxColourDisplay())
+ , m_ok(true)
+ , m_clipping(false)
+ , m_isInteractive(0)
+ , m_isBBoxValid(false)
+ , m_logicalOriginX(0), m_logicalOriginY(0)
+ , m_deviceOriginX(0), m_deviceOriginY(0)
+ , m_deviceLocalOriginX(0), m_deviceLocalOriginY(0)
+ , m_logicalScaleX(1.0), m_logicalScaleY(1.0)
+ , m_userScaleX(1.0), m_userScaleY(1.0)
+ , m_scaleX(1.0), m_scaleY(1.0)
+ , m_signX(1), m_signY(1)
+ , m_minX(0), m_minY(0), m_maxX(0), m_maxY(0)
+ , m_clipX1(0), m_clipY1(0), m_clipX2(0), m_clipY2(0)
+ , m_logicalFunction(wxCOPY)
+ , m_backgroundMode(wxBRUSHSTYLE_TRANSPARENT)
+ , m_mappingMode(wxMM_TEXT)
+ , m_pen()
+ , m_brush()
+ , m_backgroundBrush()
+ , m_textForegroundColour(*wxBLACK)
+ , m_textBackgroundColour(*wxWHITE)
+ , m_font()
+#if wxUSE_PALETTE
+ , m_palette()
+ , m_hasCustomPalette(false)
+#endif // wxUSE_PALETTE
+{
+ m_owner = owner;
+
+ m_mm_to_pix_x = (double)wxGetDisplaySize().GetWidth() /
+ (double)wxGetDisplaySizeMM().GetWidth();
+ m_mm_to_pix_y = (double)wxGetDisplaySize().GetHeight() /
+ (double)wxGetDisplaySizeMM().GetHeight();
+
+ ResetBoundingBox();
+ ResetClipping();
+}
+
+wxDCImpl::~wxDCImpl()
+{
+}
+
+// ----------------------------------------------------------------------------
+// coordinate conversions and transforms
+// ----------------------------------------------------------------------------
+
+wxCoord wxDCImpl::DeviceToLogicalX(wxCoord x) const
+{
+ return wxRound( (double)((x - m_deviceOriginX - m_deviceLocalOriginX) * m_signX) / m_scaleX ) + m_logicalOriginX ;
+}
+
+wxCoord wxDCImpl::DeviceToLogicalY(wxCoord y) const
+{
+ return wxRound( (double)((y - m_deviceOriginY - m_deviceLocalOriginY) * m_signY) / m_scaleY ) + m_logicalOriginY ;
+}
+
+wxCoord wxDCImpl::DeviceToLogicalXRel(wxCoord x) const
+{
+ return wxRound((double)(x) / m_scaleX);
+}
+
+wxCoord wxDCImpl::DeviceToLogicalYRel(wxCoord y) const
+{
+ return wxRound((double)(y) / m_scaleY);
+}
+
+wxCoord wxDCImpl::LogicalToDeviceX(wxCoord x) const
+{
+ return wxRound( (double)((x - m_logicalOriginX) * m_signX) * m_scaleX) + m_deviceOriginX + m_deviceLocalOriginX;
+}
+
+wxCoord wxDCImpl::LogicalToDeviceY(wxCoord y) const
+{
+ return wxRound( (double)((y - m_logicalOriginY) * m_signY) * m_scaleY) + m_deviceOriginY + m_deviceLocalOriginY;
+}
+
+wxCoord wxDCImpl::LogicalToDeviceXRel(wxCoord x) const
+{
+ return wxRound((double)(x) * m_scaleX);
+}
+
+wxCoord wxDCImpl::LogicalToDeviceYRel(wxCoord y) const
+{
+ return wxRound((double)(y) * m_scaleY);
+}
+
+void wxDCImpl::ComputeScaleAndOrigin()
+{
+ m_scaleX = m_logicalScaleX * m_userScaleX;
+ m_scaleY = m_logicalScaleY * m_userScaleY;
+}
+
+void wxDCImpl::SetMapMode( wxMappingMode mode )
+{
+ switch (mode)
+ {
+ case wxMM_TWIPS:
+ SetLogicalScale( twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y );
+ break;
+ case wxMM_POINTS:
+ SetLogicalScale( pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y );
+ break;
+ case wxMM_METRIC:
+ SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
+ break;
+ case wxMM_LOMETRIC:
+ SetLogicalScale( m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0 );
+ break;
+ default:
+ case wxMM_TEXT:
+ SetLogicalScale( 1.0, 1.0 );
+ break;
+ }
+ m_mappingMode = mode;
+}
+
+void wxDCImpl::SetUserScale( double x, double y )
+{
+ // allow negative ? -> no
+ m_userScaleX = x;
+ m_userScaleY = y;
+ ComputeScaleAndOrigin();
+}
+
+void wxDCImpl::SetLogicalScale( double x, double y )
+{
+ // allow negative ?
+ m_logicalScaleX = x;
+ m_logicalScaleY = y;
+ ComputeScaleAndOrigin();
+}
+
+void wxDCImpl::SetLogicalOrigin( wxCoord x, wxCoord y )
+{
+ m_logicalOriginX = x * m_signX;
+ m_logicalOriginY = y * m_signY;
+ ComputeScaleAndOrigin();
+}
+
+void wxDCImpl::SetDeviceOrigin( wxCoord x, wxCoord y )
+{
+ m_deviceOriginX = x;
+ m_deviceOriginY = y;
+ ComputeScaleAndOrigin();
+}
+
+void wxDCImpl::SetDeviceLocalOrigin( wxCoord x, wxCoord y )
+{
+ m_deviceLocalOriginX = x;
+ m_deviceLocalOriginY = y;
+ ComputeScaleAndOrigin();
+}
+
+void wxDCImpl::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
+{
+ // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
+ // wxWidgets 2.9: no longer override it
+ m_signX = (xLeftRight ? 1 : -1);
+ m_signY = (yBottomUp ? -1 : 1);
+ ComputeScaleAndOrigin();
+}
+
+
+// Each element of the widths array will be the width of the string up to and
+// including the corresponding character in text. This is the generic
+// implementation, the port-specific classes should do this with native APIs
+// if available and if faster. Note: pango_layout_index_to_pos is much slower
+// than calling GetTextExtent!!
+
+#define FWC_SIZE 256
+
+class FontWidthCache
+{
+public:
+ FontWidthCache() : m_scaleX(1), m_widths(NULL) { }
+ ~FontWidthCache() { delete []m_widths; }
+
+ void Reset()
+ {
+ if (!m_widths)
+ m_widths = new int[FWC_SIZE];
+
+ memset(m_widths, 0, sizeof(int)*FWC_SIZE);
+ }
+
+ wxFont m_font;
+ double m_scaleX;
+ int *m_widths;
+};
+
+static FontWidthCache s_fontWidthCache;
+
+bool wxDCImpl::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
+{
+ int totalWidth = 0;
+
+ const size_t len = text.length();
+ widths.Empty();
+ widths.Add(0, len);
+
+ // reset the cache if font or horizontal scale have changed
+ if ( !s_fontWidthCache.m_widths ||
+ !wxIsSameDouble(s_fontWidthCache.m_scaleX, m_scaleX) ||
+ (s_fontWidthCache.m_font != GetFont()) )
+ {
+ s_fontWidthCache.Reset();
+ s_fontWidthCache.m_font = GetFont();
+ s_fontWidthCache.m_scaleX = m_scaleX;
+ }
+
+ // Calculate the position of each character based on the widths of
+ // the previous characters
+ int w, h;
+ for ( size_t i = 0; i < len; i++ )
+ {
+ const wxChar c = text[i];
+ unsigned int c_int = (unsigned int)c;
+
+ if ((c_int < FWC_SIZE) && (s_fontWidthCache.m_widths[c_int] != 0))
+ {
+ w = s_fontWidthCache.m_widths[c_int];
+ }
+ else
+ {
+ DoGetTextExtent(c, &w, &h);
+ if (c_int < FWC_SIZE)
+ s_fontWidthCache.m_widths[c_int] = w;
+ }
+
+ totalWidth += w;
+ widths[i] = totalWidth;
+ }
+
+ return true;
+}
+
+void wxDCImpl::GetMultiLineTextExtent(const wxString& text,
+ wxCoord *x,
+ wxCoord *y,
+ wxCoord *h,
+ const wxFont *font) const
+{
+ wxCoord widthTextMax = 0, widthLine,
+ heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
+
+ wxString curLine;
+ for ( wxString::const_iterator pc = text.begin(); ; ++pc )
+ {
+ if ( pc == text.end() || *pc == wxT('\n') )
+ {
+ if ( curLine.empty() )
+ {
+ // we can't use GetTextExtent - it will return 0 for both width
+ // and height and an empty line should count in height
+ // calculation
+
+ // assume that this line has the same height as the previous
+ // one
+ if ( !heightLineDefault )
+ heightLineDefault = heightLine;