]> git.saurik.com Git - wxWidgets.git/blobdiff - src/dfb/dc.cpp
temporary fix for wxDFB compilation in Unicode mode
[wxWidgets.git] / src / dfb / dc.cpp
index f5fac4bcc074ce081418b526b8f26d9242f722bd..49aa202b638f27c2143afb7495215ed0ec05352f 100644 (file)
@@ -25,6 +25,7 @@
 
 #ifndef WX_PRECOMP
     #include "wx/dc.h"
+    #include "wx/dcmemory.h"
     #include "wx/log.h"
 #endif
 
@@ -53,10 +54,10 @@ wxDC::wxDC()
 
 wxDC::wxDC(const wxIDirectFBSurfacePtr& surface)
 {
-    Init(surface);
+    DFBInit(surface);
 }
 
-void wxDC::Init(const wxIDirectFBSurfacePtr& surface)
+void wxDC::DFBInit(const wxIDirectFBSurfacePtr& surface)
 {
     m_ok = (surface != NULL);
     wxCHECK_RET( surface != NULL, _T("invalid surface") );
@@ -78,22 +79,15 @@ void wxDC::Init(const wxIDirectFBSurfacePtr& surface)
 // clipping
 // ---------------------------------------------------------------------------
 
-
-#define DO_SET_CLIPPING_BOX(rg)                      \
-{                                                    \
-    wxRect rect = rg.GetBox();                       \
-    m_clipX1 = (wxCoord) XDEV2LOG(rect.GetLeft());   \
-    m_clipY1 = (wxCoord) YDEV2LOG(rect.GetTop());    \
-    m_clipX2 = (wxCoord) XDEV2LOG(rect.GetRight());  \
-    m_clipY2 = (wxCoord) YDEV2LOG(rect.GetBottom()); \
-}
-
 void wxDC::DoSetClippingRegion(wxCoord cx, wxCoord cy, wxCoord cw, wxCoord ch)
 {
     wxCHECK_RET( Ok(), wxT("invalid dc") );
 
     wxSize size(GetSize());
 
+    wxASSERT_MSG( !m_clipping,
+                  _T("narrowing clipping region not implemented yet") );
+
     // NB: We intersect the clipping rectangle with surface's area here because
     //     DirectFB will return an error if you try to set clipping rectangle
     //     that is partially outside of the surface.
@@ -115,8 +109,7 @@ void wxDC::DoSetClippingRegion(wxCoord cx, wxCoord cy, wxCoord cw, wxCoord ch)
 
 void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region)
 {
-    // NB: this can be done because wxDFB only supports
-    //     rectangular regions
+    // NB: this can be done because wxDFB only supports rectangular regions
     SetClippingRegion(region.AsRect());
 }
 
@@ -188,8 +181,32 @@ void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
     if ( m_pen.GetStyle() == wxTRANSPARENT )
         return;
 
-    m_surface->DrawLine(XLOG2DEV(x1), YLOG2DEV(y1),
-                        XLOG2DEV(x2), YLOG2DEV(y2));
+    wxCoord xx1 = XLOG2DEV(x1);
+    wxCoord yy1 = YLOG2DEV(y1);
+    wxCoord xx2 = XLOG2DEV(x2);
+    wxCoord yy2 = YLOG2DEV(y2);
+
+    // FIXME: DrawLine() shouldn't draw the last pixel, but DFB's DrawLine()
+    //        does draw it. We should undo any change to the last pixel by
+    //        using GetPixel() and PutPixel(), but until they are implemented,
+    //        handle at least the special case of vertical and horizontal
+    //        lines correctly:
+    if ( xx1 == xx2 )
+    {
+        if ( yy1 < yy2 )
+            yy2--;
+        else if ( yy1 > yy2 )
+            yy2++;
+    }
+    if ( yy1 == yy2 )
+    {
+        if ( xx1 < xx2 )
+            xx2--;
+        else if ( xx1 > xx2 )
+            xx2++;
+    }
+
+    m_surface->DrawLine(xx1, yy1, xx2, yy2);
 
     CalcBoundingBox(x1, y1);
     CalcBoundingBox(x2, y2);
@@ -259,7 +276,8 @@ void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
     {
         SelectColour(m_brush.GetColour());
         m_surface->FillRectangle(xx, yy, ww, hh);
-        // restore pen's colour
+        // restore pen's colour, because other drawing functions expect the
+        // colour to be set to the pen:
         SelectColour(m_pen.GetColour());
     }
 
@@ -309,16 +327,22 @@ void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
     // if background mode is solid, DrawText must paint text's background:
     if ( m_backgroundMode == wxSOLID )
     {
-        wxCHECK_RET( m_backgroundBrush.Ok(), wxT("invalid background brush") );
+        wxCHECK_RET( m_textBackgroundColour.Ok(),
+                     wxT("invalid background color") );
 
-        SelectColour(m_backgroundBrush.GetColour());
+        SelectColour(m_textBackgroundColour);
         m_surface->FillRectangle(xx, yy, XLOG2DEVREL(w), YLOG2DEVREL(h));
-        // restore pen's colour
-        SelectColour(m_pen.GetColour());
     }
 
     // finally draw the text itself:
-    m_surface->DrawString(wxSTR_TO_DFB(text), -1, xx, yy, DSTF_LEFT | DSTF_TOP);
+    wxCHECK_RET( m_textForegroundColour.Ok(),
+                 wxT("invalid foreground color") );
+    SelectColour(m_textForegroundColour);
+    m_surface->DrawString(text.utf8_str(), -1, xx, yy, DSTF_LEFT | DSTF_TOP);
+
+    // restore pen's colour, because other drawing functions expect the colour
+    // to be set to the pen:
+    SelectColour(m_pen.GetColour());
 }
 
 void wxDC::DoDrawRotatedText(const wxString& text,
@@ -367,10 +391,21 @@ void wxDC::SetFont(const wxFont& font)
 
     wxFont f(font.Ok() ? font : DEFAULT_FONT);
 
-    if ( !m_surface->SetFont(f.GetDirectFBFont()) )
-        return;
+    wxFont oldfont(m_font);
 
     m_font = f;
+
+    if ( !m_surface->SetFont(GetCurrentFont()) )
+    {
+        m_font = oldfont;
+        return;
+    }
+}
+
+wxIDirectFBFontPtr wxDC::GetCurrentFont() const
+{
+    bool aa = (GetDepth() > 8);
+    return m_font.GetDirectFBFont(aa);
 }
 
 void wxDC::SetBackground(const wxBrush& brush)
@@ -427,7 +462,7 @@ wxCoord wxDC::GetCharHeight() const
     wxCHECK_MSG( m_font.Ok(), -1, wxT("no font selected") );
 
     int h = -1;
-    m_font.GetDirectFBFont()->GetHeight(&h);
+    GetCurrentFont()->GetHeight(&h);
     return YDEV2LOGREL(h);
 }
 
@@ -437,7 +472,7 @@ wxCoord wxDC::GetCharWidth() const
     wxCHECK_MSG( m_font.Ok(), -1, wxT("no font selected") );
 
     int w = -1;
-    m_font.GetDirectFBFont()->GetStringWidth("H", 1, &w);
+    GetCurrentFont()->GetStringWidth("H", 1, &w);
     // VS: YDEV is corrent, it should *not* be XDEV, because font's are only
     //     scaled according to m_scaleY
     return YDEV2LOGREL(w);
@@ -445,7 +480,7 @@ wxCoord wxDC::GetCharWidth() const
 
 void wxDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
                            wxCoord *descent, wxCoord *externalLeading,
-                           wxFont *theFont) const
+                           const wxFont *theFont) const
 {
     wxCHECK_RET( Ok(), wxT("invalid dc") );
     wxCHECK_RET( m_font.Ok(), wxT("no font selected") );
@@ -460,9 +495,9 @@ void wxDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
 
     wxCoord xx = 0, yy = 0;
     DFBRectangle rect;
-    wxIDirectFBFontPtr f = m_font.GetDirectFBFont();
+    wxIDirectFBFontPtr f = GetCurrentFont();
 
-    if ( f->GetStringExtents(wxSTR_TO_DFB(string), -1, &rect, NULL) )
+    if ( f->GetStringExtents(string.utf8_str(), -1, &rect, NULL) )
     {
         // VS: YDEV is corrent, it should *not* be XDEV, because font's are
         //     only scaled according to m_scaleY
@@ -493,132 +528,10 @@ void wxDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
 // mapping modes
 // ---------------------------------------------------------------------------
 
-void wxDC::ComputeScaleAndOrigin()
-{
-    m_scaleX = m_logicalScaleX * m_userScaleX;
-    m_scaleY = m_logicalScaleY * m_userScaleY;
-
-    // FIXME_DFB: scaling affects pixel size of font, pens, brushes, which
-    //            is not currently implemented here; probably makes sense to
-    //            switch to Cairo instead of implementing everything for DFB
-    wxASSERT_MSG( m_scaleX == 1.0 && m_scaleY == 1.0,
-                  _T("scaling is not implemented in wxDFB") );
-}
-
-void wxDC::SetMapMode(int mode)
-{
-    #warning "move this to common code, it's shared by almost all ports!"
-    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 wxDC::SetUserScale(double x, double y)
-{
-    #warning "move this to common code?"
-    // allow negative ? -> no
-    m_userScaleX = x;
-    m_userScaleY = y;
-    ComputeScaleAndOrigin();
-}
-
-void wxDC::SetLogicalScale(double x, double y)
-{
-    #warning "move this to common code?"
-    // allow negative ?
-    m_logicalScaleX = x;
-    m_logicalScaleY = y;
-    ComputeScaleAndOrigin();
-}
-
-void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
-{
-    #warning "move this to common code?"
-    m_logicalOriginX = x * m_signX;   // is this still correct ?
-    m_logicalOriginY = y * m_signY;
-    ComputeScaleAndOrigin();
-}
-
-void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
-{
-    #warning "move this to common code?"
-    // only wxPostScripDC has m_signX = -1, we override SetDeviceOrigin there
-    m_deviceOriginX = x;
-    m_deviceOriginY = y;
-    ComputeScaleAndOrigin();
-}
-
-void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
-{
-    #warning "move this to common code?"
-    // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
-    m_signX = (xLeftRight ?  1 : -1);
-    m_signY = (yBottomUp  ? -1 :  1);
-    ComputeScaleAndOrigin();
-}
-
-// ---------------------------------------------------------------------------
-// coordinates transformations
-// ---------------------------------------------------------------------------
-
-wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
-{
-    return ((wxDC *)this)->XDEV2LOG(x);
-}
-
-wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
-{
-    return ((wxDC *)this)->YDEV2LOG(y);
-}
-
-wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
-{
-    return ((wxDC *)this)->XDEV2LOGREL(x);
-}
-
-wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
-{
-    return ((wxDC *)this)->YDEV2LOGREL(y);
-}
-
-wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
-{
-    return ((wxDC *)this)->XLOG2DEV(x);
-}
-
-wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
-{
-    return ((wxDC *)this)->YLOG2DEV(y);
-}
-
-wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
-{
-    return ((wxDC *)this)->XLOG2DEVREL(x);
-}
-
-wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
-{
-    return ((wxDC *)this)->YLOG2DEVREL(y);
-}
-
-
+// FIXME_DFB: scaling affects pixel size of font, pens, brushes, which
+//            is not currently implemented here; probably makes sense to
+//            switch to Cairo instead of implementing everything for DFB
+    
 void wxDC::DoGetSize(int *w, int *h) const
 {
     wxCHECK_RET( Ok(), wxT("invalid dc") );
@@ -758,6 +671,16 @@ bool wxDC::DoBlitFromSurface(const wxIDirectFBSurfacePtr& src,
                              wxCoord w, wxCoord h,
                              wxCoord dstx, wxCoord dsty)
 {
+    // don't do anything if the source rectangle is outside of source surface,
+    // DirectFB would assert in that case:
+    wxSize srcsize;
+    src->GetSize(&srcsize.x, &srcsize.y);
+    if ( !wxRect(srcx, srcy, w, h).Intersects(wxRect(srcsize)) )
+    {
+        wxLogDebug(_T("Blitting from area outside of the source surface, caller code needs fixing."));
+        return false;
+    }
+
     CalcBoundingBox(dstx, dsty);
     CalcBoundingBox(dstx + w, dsty + h);