+ ScreenHDC() { m_hdc = ::GetDC(NULL); }
+ ~ScreenHDC() { ::ReleaseDC(NULL, m_hdc); }
+
+ operator HDC() const { return m_hdc; }
+
+private:
+ HDC m_hdc;
+
+ DECLARE_NO_COPY_CLASS(ScreenHDC)
+};
+
+// the same as ScreenHDC but for window DCs
+class WindowHDC
+{
+public:
+ WindowHDC(HWND hwnd) { m_hdc = ::GetDC(m_hwnd = hwnd); }
+ ~WindowHDC() { ::ReleaseDC(m_hwnd, m_hdc); }
+
+ operator HDC() const { return m_hdc; }
+
+private:
+ HWND m_hwnd;
+ HDC m_hdc;
+
+ DECLARE_NO_COPY_CLASS(WindowHDC)
+};
+
+// the same as ScreenHDC but for memory DCs: creates the HDC compatible with
+// the given one (screen by default) in ctor and destroys it in dtor
+class MemoryHDC
+{
+public:
+ MemoryHDC(HDC hdc = 0) { m_hdc = ::CreateCompatibleDC(hdc); }
+ ~MemoryHDC() { ::DeleteDC(m_hdc); }
+
+ operator HDC() const { return m_hdc; }
+
+private:
+ HDC m_hdc;
+
+ DECLARE_NO_COPY_CLASS(MemoryHDC)
+};
+
+// a class which selects a GDI object into a DC in its ctor and deselects in
+// dtor
+class SelectInHDC
+{
+private:
+ void DoInit(HGDIOBJ hgdiobj) { m_hgdiobj = ::SelectObject(m_hdc, hgdiobj); }
+
+public:
+ SelectInHDC() : m_hdc(NULL) { }
+ SelectInHDC(HDC hdc, HGDIOBJ hgdiobj) : m_hdc(hdc) { DoInit(hgdiobj); }
+
+ void Init(HDC hdc, HGDIOBJ hgdiobj)
+ {
+ wxASSERT_MSG( !m_hdc, _T("initializing twice?") );
+
+ m_hdc = hdc;
+
+ DoInit(hgdiobj);
+ }
+
+ ~SelectInHDC() { if ( m_hdc ) ::SelectObject(m_hdc, m_hgdiobj); }
+
+ // return true if the object was successfully selected
+ operator bool() const { return m_hgdiobj != 0; }
+
+private:
+ HDC m_hdc;
+ HGDIOBJ m_hgdiobj;
+
+ DECLARE_NO_COPY_CLASS(SelectInHDC)
+};
+
+// a class which cleans up any GDI object
+class AutoGDIObject
+{
+protected:
+ AutoGDIObject() { m_gdiobj = NULL; }
+ AutoGDIObject(HGDIOBJ gdiobj) : m_gdiobj(gdiobj) { }
+ ~AutoGDIObject() { if ( m_gdiobj ) ::DeleteObject(m_gdiobj); }
+
+ void InitGdiobj(HGDIOBJ gdiobj)
+ {
+ wxASSERT_MSG( !m_gdiobj, _T("initializing twice?") );
+
+ m_gdiobj = gdiobj;
+ }
+
+ HGDIOBJ GetObject() const { return m_gdiobj; }
+
+private:
+ HGDIOBJ m_gdiobj;
+};
+
+// TODO: all this asks for using a AutoHandler<T, CreateFunc> template...
+
+// a class for temporary brushes
+class AutoHBRUSH : private AutoGDIObject
+{
+public:
+ AutoHBRUSH(COLORREF col)
+ : AutoGDIObject(::CreateSolidBrush(col)) { }
+
+ operator HBRUSH() const { return (HBRUSH)GetObject(); }
+};
+
+// a class for temporary fonts
+class AutoHFONT : private AutoGDIObject
+{
+private:
+public:
+ AutoHFONT()
+ : AutoGDIObject() { }
+
+ AutoHFONT(const LOGFONT& lf)
+ : AutoGDIObject(::CreateFontIndirect(&lf)) { }
+
+ void Init(const LOGFONT& lf) { InitGdiobj(::CreateFontIndirect(&lf)); }
+
+ operator HFONT() const { return (HFONT)GetObject(); }
+};
+
+// a class for temporary pens
+class AutoHPEN : private AutoGDIObject
+{
+public:
+ AutoHPEN(COLORREF col)
+ : AutoGDIObject(::CreatePen(PS_SOLID, 0, col)) { }
+
+ operator HPEN() const { return (HPEN)GetObject(); }
+};
+
+// classes for temporary bitmaps
+class AutoHBITMAP : private AutoGDIObject
+{
+public:
+ AutoHBITMAP(HBITMAP hbmp) : AutoGDIObject(hbmp) { }
+
+ operator HBITMAP() const { return (HBITMAP)GetObject(); }
+};
+
+class CompatibleBitmap : public AutoHBITMAP
+{
+public:
+ CompatibleBitmap(HDC hdc, int w, int h)
+ : AutoHBITMAP(::CreateCompatibleBitmap(hdc, w, h))
+ {
+ }
+};
+
+class MonoBitmap : public AutoHBITMAP
+{
+public:
+ MonoBitmap(int w, int h)
+ : AutoHBITMAP(::CreateBitmap(w, h, 1, 1, 0))
+ {
+ }
+};
+
+// class automatically destroys the region object
+class AutoHRGN : private AutoGDIObject
+{
+public:
+ AutoHRGN(HRGN hrgn) : AutoGDIObject(hrgn) { }
+
+ operator HRGN() const { return (HRGN)GetObject(); }
+};
+
+// class sets the specified clipping region during its life time
+class HDCClipper
+{
+public:
+ HDCClipper(HDC hdc, HRGN hrgn)
+ : m_hdc(hdc)
+ {
+ if ( !::SelectClipRgn(hdc, hrgn) )
+ wxLogLastError(_T("SelectClipRgn"));
+ }
+
+ ~HDCClipper()
+ {
+ ::SelectClipRgn(m_hdc, NULL);
+ }