+
+ DECLARE_NO_COPY_CLASS(MemoryHDC)
+};
+
+// a class which selects a GDI object into a DC in its ctor and deselects in
+// dtor
+class SelectInHDC
+{
+public:
+ SelectInHDC(HDC hdc, HGDIOBJ hgdiobj) : m_hdc(hdc)
+ { m_hgdiobj = ::SelectObject(hdc, hgdiobj); }
+
+ ~SelectInHDC() { ::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 for temporary bitmaps
+class CompatibleBitmap
+{
+public:
+ CompatibleBitmap(HDC hdc, int w, int h)
+ {
+ m_hbmp = ::CreateCompatibleBitmap(hdc, w, h);
+ }
+
+ ~CompatibleBitmap()
+ {
+ if ( m_hbmp )
+ ::DeleteObject(m_hbmp);
+ }
+
+ operator HBITMAP() const { return m_hbmp; }
+
+private:
+ HBITMAP m_hbmp;
+};
+
+// when working with global pointers (which is unfortunately still necessary
+// sometimes, e.g. for clipboard) it is important to unlock them exactly as
+// many times as we lock them which just asks for using a "smart lock" class
+class GlobalPtr
+{
+public:
+ GlobalPtr(HGLOBAL hGlobal) : m_hGlobal(hGlobal)
+ {
+ m_ptr = GlobalLock(hGlobal);
+ if ( !m_ptr )
+ {
+ wxLogLastError(_T("GlobalLock"));
+ }
+ }
+
+ ~GlobalPtr()
+ {
+ if ( !GlobalUnlock(m_hGlobal) )
+ {
+#ifdef __WXDEBUG__
+ // this might happen simply because the block became unlocked
+ DWORD dwLastError = ::GetLastError();
+ if ( dwLastError != NO_ERROR )
+ {
+ wxLogApiError(_T("GlobalUnlock"), dwLastError);
+ }
+#endif // __WXDEBUG__
+ }
+ }
+
+ operator void *() const { return m_ptr; }
+
+private:
+ HGLOBAL m_hGlobal;
+ void *m_ptr;
+
+ DECLARE_NO_COPY_CLASS(GlobalPtr)
+};
+
+// register the class when it is first needed and unregister it in dtor
+class ClassRegistrar
+{
+public:
+ // ctor doesn't register the class, call Initialize() for this
+ ClassRegistrar() { m_registered = -1; }
+
+ // return true if the class is already registered
+ bool IsInitialized() const { return m_registered != -1; }
+
+ // return true if the class had been already registered
+ bool IsRegistered() const { return m_registered == 1; }
+
+ // try to register the class if not done yet, return true on success
+ bool Register(const WNDCLASS& wc)
+ {
+ // we should only be called if we hadn't been initialized yet
+ wxASSERT_MSG( m_registered == -1,
+ _T("calling ClassRegistrar::Register() twice?") );
+
+ m_registered = ::RegisterClass(&wc) ? 1 : 0;
+ if ( !IsRegistered() )
+ {
+ wxLogLastError(_T("RegisterClassEx()"));
+ }
+ else
+ {
+ m_clsname = wc.lpszClassName;
+ }
+
+ return m_registered == 1;
+ }
+
+ // get the name of the registered class (returns empty string if not
+ // registered)
+ const wxString& GetName() const { return m_clsname; }
+
+ // unregister the class if it had been registered
+ ~ClassRegistrar()
+ {
+ if ( IsRegistered() )
+ {
+ if ( !::UnregisterClass(m_clsname, wxhInstance) )
+ {
+ wxLogLastError(_T("UnregisterClass"));
+ }
+ }
+ }
+
+private:
+ // initial value is -1 which means that we hadn't tried registering the
+ // class yet, it becomes true or false (1 or 0) when Initialize() is called
+ int m_registered;
+
+ // the name of the class, only non empty if it had been registered
+ wxString m_clsname;