]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/dc.cpp
extracted duplicated code from src/*/data.cpp in a common file
[wxWidgets.git] / src / msw / dc.cpp
index 0a75fced54be38aa24f82bd810bfb889e74a8edb..191d5c33961cb691993506c889eea22f2f30c13e 100644 (file)
@@ -5,7 +5,7 @@
 // Modified by:
 // Created:     01/02/97
 // RCS-ID:      $Id$
-// Copyright:   (c) Julian Smart and Markus Holzem
+// Copyright:   (c) Julian Smart
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
@@ -43,6 +43,7 @@
 #include "wx/sysopt.h"
 #include "wx/dcprint.h"
 #include "wx/module.h"
+#include "wx/dynload.h"
 
 #include <string.h>
 #include <math.h>
     #include <print.h>
 #endif
 
+/* Quaternary raster codes */
+#ifndef MAKEROP4
+#define MAKEROP4(fore,back) (DWORD)((((back) << 8) & 0xFF000000) | (fore))
+#endif
+
 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxDCBase)
 
 // ---------------------------------------------------------------------------
@@ -127,6 +133,30 @@ private:
     bool m_changed;
 };
 
+// this class saves the old stretch blit mode during its life time
+class StretchBltModeChanger
+{
+public:
+    StretchBltModeChanger(HDC hdc, int mode)
+        : m_hdc(hdc)
+    {
+        m_modeOld = ::SetStretchBltMode(m_hdc, mode);
+        if ( !m_modeOld )
+            wxLogLastError(_T("SetStretchBltMode"));
+    }
+
+    ~StretchBltModeChanger()
+    {
+        if ( !::SetStretchBltMode(m_hdc, m_modeOld) )
+            wxLogLastError(_T("SetStretchBltMode"));
+    }
+
+private:
+    const HDC m_hdc;
+
+    int m_modeOld;
+};
+
 // ===========================================================================
 // implementation
 // ===========================================================================
@@ -243,10 +273,12 @@ void wxDC::SelectOldObjects(WXHDC dc)
         if (m_oldBitmap)
         {
             ::SelectObject((HDC) dc, (HBITMAP) m_oldBitmap);
+#ifdef __WXDEBUG__
             if (m_selectedBitmap.Ok())
             {
                 m_selectedBitmap.SetSelectedInto(NULL);
             }
+#endif
         }
         m_oldBitmap = 0;
         if (m_oldPen)
@@ -468,16 +500,17 @@ void wxDC::Clear()
     ::SetWindowOrgEx(GetHdc(), (int)m_logicalOriginX, (int)m_logicalOriginY, NULL);
 }
 
-void wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
+bool wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
 {
 #ifdef __WXMICROWIN__
-    if (!GetHDC()) return;
+    if (!GetHDC()) return FALSE;
 #endif
 
-    if ( !::ExtFloodFill(GetHdc(), XLOG2DEV(x), YLOG2DEV(y),
+    bool success = (0 != ::ExtFloodFill(GetHdc(), XLOG2DEV(x), YLOG2DEV(y),
                          col.GetPixel(),
                          style == wxFLOOD_SURFACE ? FLOODFILLSURFACE
-                                                  : FLOODFILLBORDER) )
+                                                  : FLOODFILLBORDER) ) ;
+    if (!success)
     {
         // quoting from the MSDN docs:
         //
@@ -495,6 +528,8 @@ void wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style)
     }
 
     CalcBoundingBox(x, y);
+    
+    return success;
 }
 
 bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
@@ -907,6 +942,70 @@ void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask
     HPALETTE oldPal = 0;
 #endif // wxUSE_PALETTE
 
+    // do we have AlphaBlend() and company in the headers?
+#ifdef AC_SRC_OVER
+    if ( bmp.HasAlpha() )
+    {
+        // yes, now try to see if we have it during run-time
+
+        typedef BOOL (WINAPI *AlphaBlend_t)(HDC,int,int,int,int,
+                                            HDC,int,int,int,int,
+                                            BLENDFUNCTION);
+
+        // bitmaps can be drawn only from GUI thread so there is no need to
+        // protect this static variable from multiple threads
+        static bool s_triedToLoad = FALSE;
+        static AlphaBlend_t pfnAlphaBlend = NULL;
+        if ( !s_triedToLoad )
+        {
+            s_triedToLoad = TRUE;
+
+            // don't give errors about the DLL being unavailable, we're
+            // prepared to handle this
+            wxLogNull nolog;
+
+            wxDynamicLibrary dll(_T("msimg32.dll"));
+            if ( dll.IsLoaded() )
+            {
+                pfnAlphaBlend = (AlphaBlend_t)dll.GetSymbol(_T("AlphaBlend"));
+                if ( pfnAlphaBlend )
+                {
+                    // we must keep the DLL loaded if we want to be able to
+                    // call AlphaBlend() so just never unload it at all, not a
+                    // big deal
+                    dll.Detach();
+                }
+            }
+        }
+
+        if ( pfnAlphaBlend )
+        {
+            MemoryHDC hdcMem;
+            SelectInHDC select(hdcMem, GetHbitmapOf(bmp));
+
+#ifndef AC_SRC_ALPHA
+    #define AC_SRC_ALPHA 1
+#endif
+
+            BLENDFUNCTION bf;
+            bf.BlendOp = AC_SRC_OVER;
+            bf.BlendFlags = 0;
+            bf.SourceConstantAlpha = 0xff;
+            bf.AlphaFormat = AC_SRC_ALPHA;
+
+            if ( !pfnAlphaBlend(GetHdc(), x, y, width, height,
+                                hdcMem, 0, 0, width, height,
+                                bf) )
+            {
+                wxLogLastError(_T("AlphaBlend"));
+            }
+
+            return;
+        }
+        //else: AlphaBlend() not available
+    }
+#endif // defined(AC_SRC_OVER)
+
     if ( useMask )
     {
         wxMask *mask = bmp.GetMask();
@@ -1126,13 +1225,13 @@ void wxDC::DoDrawRotatedText(const wxString& text,
 
         // "upper left" and "upper right"
         CalcBoundingBox(x, y);
-        CalcBoundingBox(x + w*cos(rad), y - h*sin(rad));
+        CalcBoundingBox(x + wxCoord(w*cos(rad)), y - wxCoord(h*sin(rad)));
 
         // "bottom left" and "bottom right"
         x += (wxCoord)(h*sin(rad));
         y += (wxCoord)(h*cos(rad));
         CalcBoundingBox(x, y);
-        CalcBoundingBox(x + h*sin(rad), y + h*cos(rad));
+        CalcBoundingBox(x + wxCoord(h*sin(rad)), y + wxCoord(h*cos(rad)));
     }
 #endif
 }
@@ -1729,13 +1828,14 @@ bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest,
     if (!GetHDC()) return FALSE;
 #endif
 
+    const wxBitmap& bmpSrc = source->m_selectedBitmap;
+
     wxMask *mask = NULL;
     if ( useMask )
     {
-        const wxBitmap& bmp = source->m_selectedBitmap;
-        mask = bmp.GetMask();
+        mask = bmpSrc.GetMask();
 
-        if ( !(bmp.Ok() && mask && mask->GetMaskBitmap()) )
+        if ( !(bmpSrc.Ok() && mask && mask->GetMaskBitmap()) )
         {
             // don't give assert here because this would break existing
             // programs - just silently ignore useMask parameter
@@ -1800,10 +1900,16 @@ bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest,
         if (wxSystemOptions::GetOptionInt(wxT("no-maskblt")) == 0)
 #endif
         {
-           success = ::MaskBlt(GetHdc(), xdest, ydest, width, height,
-                            GetHdcOf(*source), xsrc, ysrc,
-                            (HBITMAP)mask->GetMaskBitmap(), xsrcMask, ysrcMask,
-                            MAKEROP4(dwRop, DSTCOPY)) != 0;
+           success = ::MaskBlt
+                       (
+                            GetHdc(),
+                            xdest, ydest, width, height,
+                            GetHdcOf(*source),
+                            xsrc, ysrc,
+                            (HBITMAP)mask->GetMaskBitmap(),
+                            xsrcMask, ysrcMask,
+                            MAKEROP4(dwRop, DSTCOPY)
+                        ) != 0;
         }
 
         if ( !success )
@@ -1893,14 +1999,82 @@ bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest,
     }
     else // no mask, just BitBlt() it
     {
-        success = ::BitBlt(GetHdc(), xdest, ydest,
-                           (int)width, (int)height,
-                           GetHdcOf(*source), xsrc, ysrc, dwRop) != 0;
+        // if we already have a DIB, draw it using StretchDIBits(), otherwise
+        // use StretchBlt() if available and finally fall back to BitBlt()
+        const int caps = ::GetDeviceCaps(GetHdc(), RASTERCAPS);
+        if ( bmpSrc.Ok() && (caps & RC_STRETCHDIB) )
+        {
+            DIBSECTION ds;
+            wxZeroMemory(ds);
+
+            if ( ::GetObject(GetHbitmapOf(bmpSrc),
+                             sizeof(ds),
+                             &ds) == sizeof(ds) )
+            {
+                StretchBltModeChanger changeMode(GetHdc(), COLORONCOLOR);
+
+                if ( ::StretchDIBits(GetHdc(),
+                                     xdest, ydest,
+                                     width, height,
+                                     0, 0,
+                                     width, height,
+                                     ds.dsBm.bmBits,
+                                     (LPBITMAPINFO)&ds.dsBmih,
+                                     DIB_RGB_COLORS,
+                                     SRCCOPY
+                                     ) == (int)GDI_ERROR )
+                {
+                    wxLogLastError(wxT("StretchDIBits"));
+                }
+                else
+                {
+                    success = TRUE;
+                }
+            }
+        }
+
+        if ( !success && (caps & RC_STRETCHBLT) )
+        {
+            StretchBltModeChanger changeMode(GetHdc(), COLORONCOLOR);
+
+            if ( !::StretchBlt
+                    (
+                        GetHdc(),
+                        xdest, ydest, width, height,
+                        GetHdcOf(*source),
+                        xsrc, ysrc, width, height,
+                        dwRop
+                    ) )
+            {
+                wxLogLastError(_T("StretchBlt"));
+            }
+            else
+            {
+                success = TRUE;
+            }
+        }
+
         if ( !success )
         {
-            wxLogLastError(wxT("BitBlt"));
+            if ( !::BitBlt
+                    (
+                        GetHdc(),
+                        xdest, ydest,
+                        (int)width, (int)height,
+                        GetHdcOf(*source),
+                        xsrc, ysrc,
+                        dwRop
+                    ) )
+            {
+                wxLogLastError(_T("BitBlt"));
+            }
+            else
+            {
+                success = TRUE;
+            }
         }
     }
+
     ::SetTextColor(GetHdc(), old_textground);
     ::SetBkColor(GetHdc(), old_background);
 
@@ -1923,8 +2097,30 @@ void wxDC::DoGetSizeMM(int *w, int *h) const
     if (!GetHDC()) return;
 #endif
 
-    if ( w ) *w = ::GetDeviceCaps(GetHdc(), HORZSIZE);
-    if ( h ) *h = ::GetDeviceCaps(GetHdc(), VERTSIZE);
+    // if we implement it in terms of DoGetSize() instead of directly using the
+    // results returned by GetDeviceCaps(HORZ/VERTSIZE) as was done before, it
+    // will also work for wxWindowDC and wxClientDC even though their size is
+    // not the same as the total size of the screen
+    int wPixels, hPixels;
+    DoGetSize(&wPixels, &hPixels);
+
+    if ( w )
+    {
+        int wTotal = ::GetDeviceCaps(GetHdc(), HORZRES);
+
+        wxCHECK_RET( wTotal, _T("0 width device?") );
+
+        *w = (wPixels * ::GetDeviceCaps(GetHdc(), HORZSIZE)) / wTotal;
+    }
+
+    if ( h )
+    {
+        int hTotal = ::GetDeviceCaps(GetHdc(), VERTRES);
+
+        wxCHECK_RET( hTotal, _T("0 height device?") );
+
+        *h = (hPixels * ::GetDeviceCaps(GetHdc(), VERTSIZE)) / hTotal;
+    }
 }
 
 wxSize wxDC::GetPPI() const
@@ -1950,25 +2146,6 @@ void wxDC::SetLogicalScale(double x, double y)
     m_logicalScaleY = y;
 }
 
-#if WXWIN_COMPATIBILITY
-void wxDC::DoGetTextExtent(const wxString& string, float *x, float *y,
-                         float *descent, float *externalLeading,
-                         wxFont *theFont, bool use16bit) const
-{
-#ifdef __WXMICROWIN__
-    if (!GetHDC()) return;
-#endif
-
-    wxCoord x1, y1, descent1, externalLeading1;
-    GetTextExtent(string, & x1, & y1, & descent1, & externalLeading1, theFont, use16bit);
-    *x = x1; *y = y1;
-    if (descent)
-        *descent = descent1;
-    if (externalLeading)
-        *externalLeading = externalLeading1;
-}
-#endif
-
 #if wxUSE_DC_CACHEING
 
 /*
@@ -2009,10 +2186,10 @@ wxDCCacheEntry::~wxDCCacheEntry()
 wxDCCacheEntry* wxDC::FindBitmapInCache(WXHDC dc, int w, int h)
 {
     int depth = ::GetDeviceCaps((HDC) dc, PLANES) * ::GetDeviceCaps((HDC) dc, BITSPIXEL);
-    wxNode* node = sm_bitmapCache.First();
+    wxNode* node = sm_bitmapCache.GetFirst();
     while (node)
     {
-        wxDCCacheEntry* entry = (wxDCCacheEntry*) node->Data();
+        wxDCCacheEntry* entry = (wxDCCacheEntry*) node->GetData();
 
         if (entry->m_depth == depth)
         {
@@ -2030,7 +2207,7 @@ wxDCCacheEntry* wxDC::FindBitmapInCache(WXHDC dc, int w, int h)
             return entry;
         }
 
-        node = node->Next();
+        node = node->GetNext();
     }
     WXHBITMAP hBitmap = (WXHBITMAP) ::CreateCompatibleBitmap((HDC) dc, w, h);
     if ( !hBitmap)
@@ -2045,10 +2222,10 @@ wxDCCacheEntry* wxDC::FindBitmapInCache(WXHDC dc, int w, int h)
 wxDCCacheEntry* wxDC::FindDCInCache(wxDCCacheEntry* notThis, WXHDC dc)
 {
     int depth = ::GetDeviceCaps((HDC) dc, PLANES) * ::GetDeviceCaps((HDC) dc, BITSPIXEL);
-    wxNode* node = sm_dcCache.First();
+    wxNode* node = sm_dcCache.GetFirst();
     while (node)
     {
-        wxDCCacheEntry* entry = (wxDCCacheEntry*) node->Data();
+        wxDCCacheEntry* entry = (wxDCCacheEntry*) node->GetData();
 
         // Don't return the same one as we already have
         if (!notThis || (notThis != entry))
@@ -2059,7 +2236,7 @@ wxDCCacheEntry* wxDC::FindDCInCache(wxDCCacheEntry* notThis, WXHDC dc)
             }
         }
 
-        node = node->Next();
+        node = node->GetNext();
     }
     WXHDC hDC = (WXHDC) ::CreateCompatibleDC((HDC) dc);
     if ( !hDC)
@@ -2083,12 +2260,12 @@ void wxDC::AddToDCCache(wxDCCacheEntry* entry)
 
 void wxDC::ClearCache()
 {
-    sm_bitmapCache.DeleteContents(TRUE);
-    sm_bitmapCache.Clear();
-    sm_bitmapCache.DeleteContents(FALSE);
     sm_dcCache.DeleteContents(TRUE);
     sm_dcCache.Clear();
     sm_dcCache.DeleteContents(FALSE);
+    sm_bitmapCache.DeleteContents(TRUE);
+    sm_bitmapCache.Clear();
+    sm_bitmapCache.DeleteContents(FALSE);
 }
 
 // Clean up cache at app exit