From 77ec05d4776a5210c766fcd4b6b2c63a23b1f86a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 30 Jan 2005 16:57:57 +0000 Subject: [PATCH] added ClassRegistrar (wraps Register/UnregisterClass Win32 API calls) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@31657 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/msw/private.h | 70 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 6 deletions(-) diff --git a/include/wx/msw/private.h b/include/wx/msw/private.h index bd6eec5dc8..ff47704cfa 100644 --- a/include/wx/msw/private.h +++ b/include/wx/msw/private.h @@ -54,6 +54,12 @@ extern WXDLLEXPORT_DATA(HFONT) wxSTATUS_LINE_FONT; #endif // wxUSE_GUI +// --------------------------------------------------------------------------- +// global data +// --------------------------------------------------------------------------- + +extern WXDLLIMPEXP_DATA_BASE(HINSTANCE) wxhInstance; + // --------------------------------------------------------------------------- // define things missing from some compilers' headers // --------------------------------------------------------------------------- @@ -451,6 +457,64 @@ private: 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; +}; + // --------------------------------------------------------------------------- // macros to make casting between WXFOO and FOO a bit easier: the GetFoo() // returns Foo cast to the Windows type for oruselves, while GetFooOf() takes @@ -498,12 +562,6 @@ private: #endif // wxUSE_GUI -// --------------------------------------------------------------------------- -// global data -// --------------------------------------------------------------------------- - -extern WXDLLIMPEXP_DATA_BASE(HINSTANCE) wxhInstance; - // --------------------------------------------------------------------------- // global functions // --------------------------------------------------------------------------- -- 2.47.2