+// convert degrees to radians
+static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
+
+// call AlphaBlend() to blit contents of hdcSrc to hdcDst using alpha
+//
+// NB: bmpSrc is the bitmap selected in hdcSrc, it is not really needed
+// to pass it to this function but as we already have it at the point
+// of call anyhow we do
+//
+// return true if we could draw the bitmap in one way or the other, false
+// otherwise
+static bool AlphaBlt(HDC hdcDst,
+ int x, int y, int dstWidth, int dstHeight,
+ int srcX, int srcY,
+ int srcWidth, int srcHeight,
+ HDC hdcSrc,
+ const wxBitmap& bmp);
+
+#ifdef wxHAS_RAW_BITMAP
+
+// our (limited) AlphaBlend() replacement for Windows versions not providing it
+static void
+wxAlphaBlend(HDC hdcDst, int xDst, int yDst,
+ int dstWidth, int dstHeight,
+ int srcX, int srcY,
+ int srcWidth, int srcHeight,
+ const wxBitmap& bmpSrc);
+
+#endif // wxHAS_RAW_BITMAP
+
+// ----------------------------------------------------------------------------
+// private classes
+// ----------------------------------------------------------------------------
+
+// various classes to change some DC property temporarily
+
+// text background and foreground colours
+class wxTextColoursChanger
+{
+public:
+ wxTextColoursChanger(HDC hdc, const wxMSWDCImpl& dc)
+ : m_hdc(hdc)
+ {
+ Change(dc.GetTextForeground(), dc.GetTextBackground());
+ }
+
+ wxTextColoursChanger(HDC hdc, const wxColour& colFg, const wxColour& colBg)
+ : m_hdc(hdc)
+ {
+ Change(colFg, colBg);
+ }
+
+ ~wxTextColoursChanger()
+ {
+ if ( m_oldColFg != CLR_INVALID )
+ ::SetTextColor(m_hdc, m_oldColFg);
+ if ( m_oldColBg != CLR_INVALID )
+ ::SetBkColor(m_hdc, m_oldColBg);
+ }
+
+protected:
+ // this ctor doesn't change mode immediately, call Change() later to do it
+ // only if needed
+ wxTextColoursChanger(HDC hdc)
+ : m_hdc(hdc)
+ {
+ m_oldColFg =
+ m_oldColBg = CLR_INVALID;
+ }
+
+ void Change(const wxColour& colFg, const wxColour& colBg)
+ {
+ if ( colFg.IsOk() )
+ {
+ m_oldColFg = ::SetTextColor(m_hdc, colFg.GetPixel());
+ if ( m_oldColFg == CLR_INVALID )
+ {
+ wxLogLastError(_T("SetTextColor"));
+ }
+ }
+ else
+ {
+ m_oldColFg = CLR_INVALID;
+ }
+
+ if ( colBg.IsOk() )
+ {
+ m_oldColBg = ::SetBkColor(m_hdc, colBg.GetPixel());
+ if ( m_oldColBg == CLR_INVALID )
+ {
+ wxLogLastError(_T("SetBkColor"));
+ }
+ }
+ else
+ {
+ m_oldColBg = CLR_INVALID;
+ }
+ }
+
+private:
+ const HDC m_hdc;
+ COLORREF m_oldColFg,
+ m_oldColBg;
+
+ DECLARE_NO_COPY_CLASS(wxTextColoursChanger)
+};
+
+// background mode
+class wxBkModeChanger
+{
+public:
+ // set background mode to opaque if mode != wxBRUSHSTYLE_TRANSPARENT
+ wxBkModeChanger(HDC hdc, int mode)
+ : m_hdc(hdc)
+ {
+ Change(mode);
+ }
+
+ ~wxBkModeChanger()
+ {
+ if ( m_oldMode )
+ ::SetBkMode(m_hdc, m_oldMode);
+ }
+
+protected:
+ // this ctor doesn't change mode immediately, call Change() later to do it
+ // only if needed
+ wxBkModeChanger(HDC hdc) : m_hdc(hdc) { m_oldMode = 0; }
+
+ void Change(int mode)
+ {
+ m_oldMode = ::SetBkMode(m_hdc, mode == wxBRUSHSTYLE_TRANSPARENT
+ ? TRANSPARENT
+ : OPAQUE);
+ if ( !m_oldMode )
+ {
+ wxLogLastError(_T("SetBkMode"));
+ }
+ }
+
+private:
+ const HDC m_hdc;
+ int m_oldMode;
+
+ DECLARE_NO_COPY_CLASS(wxBkModeChanger)
+};
+
+// instead of duplicating the same code which sets and then restores text
+// colours in each wxDC method working with wxSTIPPLE_MASK_OPAQUE brushes,
+// encapsulate this in a small helper class
+
+// wxBrushAttrsSetter: changes the text colours in the ctor if required and
+// restores them in the dtor
+class wxBrushAttrsSetter : private wxBkModeChanger,
+ private wxTextColoursChanger
+{
+public:
+ wxBrushAttrsSetter(wxMSWDCImpl& dc);
+
+private:
+ DECLARE_NO_COPY_CLASS(wxBrushAttrsSetter)
+};
+
+// this class saves the old stretch blit mode during its life time
+class StretchBltModeChanger
+{
+public:
+ StretchBltModeChanger(HDC hdc,
+ int WXUNUSED_IN_WINCE(mode))
+ : m_hdc(hdc)
+ {
+#ifndef __WXWINCE__
+ m_modeOld = ::SetStretchBltMode(m_hdc, mode);
+ if ( !m_modeOld )
+ wxLogLastError(_T("SetStretchBltMode"));
+#endif
+ }
+
+ ~StretchBltModeChanger()
+ {
+#ifndef __WXWINCE__
+ if ( !::SetStretchBltMode(m_hdc, m_modeOld) )
+ wxLogLastError(_T("SetStretchBltMode"));
+#endif
+ }
+
+private:
+ const HDC m_hdc;
+
+ int m_modeOld;
+
+ DECLARE_NO_COPY_CLASS(StretchBltModeChanger)
+};
+
+#if wxUSE_DYNLIB_CLASS
+
+// helper class to cache dynamically loaded libraries and not attempt reloading
+// them if it fails
+class wxOnceOnlyDLLLoader
+{
+public:
+ // ctor argument must be a literal string as we don't make a copy of it!
+ wxOnceOnlyDLLLoader(const wxChar *dllName)
+ : m_dllName(dllName)
+ {
+ }
+
+
+ // return the symbol with the given name or NULL if the DLL not loaded
+ // or symbol not present
+ void *GetSymbol(const wxChar *name)
+ {
+ // we're prepared to handle errors here
+ wxLogNull noLog;
+
+ if ( m_dllName )
+ {
+ m_dll.Load(m_dllName);
+
+ // reset the name whether we succeeded or failed so that we don't
+ // try again the next time
+ m_dllName = NULL;
+ }
+
+ return m_dll.IsLoaded() ? m_dll.GetSymbol(name) : NULL;
+ }
+
+ void Unload()
+ {
+ if ( m_dll.IsLoaded() )
+ {
+ m_dll.Unload();
+ }
+ }
+
+private:
+ wxDynamicLibrary m_dll;
+ const wxChar *m_dllName;
+};
+
+static wxOnceOnlyDLLLoader wxMSIMG32DLL(_T("msimg32"));
+
+// we must ensure that DLLs are unloaded before the static objects cleanup time
+// because we may hit the notorious DllMain() dead lock in this case if wx is
+// used as a DLL (attempting to unload another DLL from inside DllMain() hangs
+// under Windows because it tries to reacquire the same lock)
+class wxGDIDLLsCleanupModule : public wxModule