From 2aef6570bbaa11505e97d9b89f1fc15316d9ec3c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 25 Apr 2019 17:12:41 +0200 Subject: [PATCH] Add TempHWNDSetter RAII helper to wxMSW and use it No real changes, just replace pairs of SetHWND(hwnd)/SetHWND(0) calls with the use of TempHWNDSetter. --- include/wx/msw/private.h | 23 +++++++++++++++++++++++ src/msw/colordlg.cpp | 5 +---- src/msw/filedlg.cpp | 11 +++-------- src/msw/msgdlg.cpp | 5 +---- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/include/wx/msw/private.h b/include/wx/msw/private.h index 9b47c6f528..95dbe03b9e 100644 --- a/include/wx/msw/private.h +++ b/include/wx/msw/private.h @@ -374,6 +374,29 @@ inline RECT wxGetClientRect(HWND hwnd) // small helper classes // --------------------------------------------------------------------------- +// Temporarily assign the given HWND to the window in ctor and unset it back to +// the original value (usually 0) in dtor. +class TempHWNDSetter +{ +public: + TempHWNDSetter(wxWindow* win, WXHWND hWnd) + : m_win(win), m_hWndOrig(m_win->GetHWND()) + { + m_win->SetHWND(hWnd); + } + + ~TempHWNDSetter() + { + m_win->SetHWND(m_hWndOrig); + } + +private: + wxWindow* const m_win; + WXHWND const m_hWndOrig; + + wxDECLARE_NO_COPY_CLASS(TempHWNDSetter); +}; + // create an instance of this class and use it as the HDC for screen, will // automatically release the DC going out of scope class ScreenHDC diff --git a/src/msw/colordlg.cpp b/src/msw/colordlg.cpp index fb9c89d16c..9b4d9e55ee 100644 --- a/src/msw/colordlg.cpp +++ b/src/msw/colordlg.cpp @@ -255,7 +255,7 @@ void wxColourDialog::DoGetClientSize(int *width, int *height) const void wxColourDialog::MSWOnInitDone(WXHWND hDlg) { // set HWND so that our DoMoveWindow() works correctly - SetHWND(hDlg); + TempHWNDSetter set(this, hDlg); if ( m_centreDir ) { @@ -272,9 +272,6 @@ void wxColourDialog::MSWOnInitDone(WXHWND hDlg) { SetPosition(GetPosition()); } - - // we shouldn't destroy hDlg, so disassociate from it - SetHWND(NULL); } #endif // wxUSE_COLOURDLG diff --git a/src/msw/filedlg.cpp b/src/msw/filedlg.cpp index 5a0b7bd17a..2e96f37d5c 100644 --- a/src/msw/filedlg.cpp +++ b/src/msw/filedlg.cpp @@ -312,7 +312,7 @@ void wxFileDialog::MSWOnInitDone(WXHWND hDlg) HWND hFileDlg = ::GetParent((HWND)hDlg); // set HWND so that our DoMoveWindow() works correctly - SetHWND((WXHWND)hFileDlg); + TempHWNDSetter set(this, (WXHWND)hFileDlg); if ( m_centreDir ) { @@ -333,9 +333,6 @@ void wxFileDialog::MSWOnInitDone(WXHWND hDlg) // Call selection change handler so that update handler will be // called once with no selection. MSWOnSelChange(hDlg); - - // we shouldn't destroy this HWND - SetHWND(NULL); } void wxFileDialog::MSWOnSelChange(WXHWND hDlg) @@ -402,11 +399,9 @@ static bool ShowCommFileDialog(OPENFILENAME *of, long style) void wxFileDialog::MSWOnInitDialogHook(WXHWND hwnd) { - SetHWND(hwnd); + TempHWNDSetter set(this, hwnd); - CreateExtraControl(); - - SetHWND(NULL); + CreateExtraControl(); } int wxFileDialog::ShowModal() diff --git a/src/msw/msgdlg.cpp b/src/msw/msgdlg.cpp index 6ffc80b43d..6da44d822b 100644 --- a/src/msw/msgdlg.cpp +++ b/src/msw/msgdlg.cpp @@ -136,7 +136,7 @@ wxMessageDialog::HookFunction(int code, WXWPARAM wParam, WXLPARAM lParam) wnd->m_hook = NULL; HookMap().erase(tid); - wnd->SetHWND((HWND)wParam); + TempHWNDSetter set(wnd, (WXHWND)wParam); // replace the static text with an edit control if the message box is // too big to fit the display @@ -151,9 +151,6 @@ wxMessageDialog::HookFunction(int code, WXWPARAM wParam, WXLPARAM lParam) if ( wnd->GetMessageDialogStyle() & wxCENTER ) wnd->Center(); // center on parent //else: default behaviour, center on screen - - // there seems to be no reason to leave it set - wnd->SetHWND(NULL); } return rc;