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.
This commit is contained in:
Vadim Zeitlin
2019-04-25 17:12:41 +02:00
parent 87318ec4ee
commit 2aef6570bb
4 changed files with 28 additions and 16 deletions

View File

@@ -374,6 +374,29 @@ inline RECT wxGetClientRect(HWND hwnd)
// small helper classes // 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 // create an instance of this class and use it as the HDC for screen, will
// automatically release the DC going out of scope // automatically release the DC going out of scope
class ScreenHDC class ScreenHDC

View File

@@ -255,7 +255,7 @@ void wxColourDialog::DoGetClientSize(int *width, int *height) const
void wxColourDialog::MSWOnInitDone(WXHWND hDlg) void wxColourDialog::MSWOnInitDone(WXHWND hDlg)
{ {
// set HWND so that our DoMoveWindow() works correctly // set HWND so that our DoMoveWindow() works correctly
SetHWND(hDlg); TempHWNDSetter set(this, hDlg);
if ( m_centreDir ) if ( m_centreDir )
{ {
@@ -272,9 +272,6 @@ void wxColourDialog::MSWOnInitDone(WXHWND hDlg)
{ {
SetPosition(GetPosition()); SetPosition(GetPosition());
} }
// we shouldn't destroy hDlg, so disassociate from it
SetHWND(NULL);
} }
#endif // wxUSE_COLOURDLG #endif // wxUSE_COLOURDLG

View File

@@ -312,7 +312,7 @@ void wxFileDialog::MSWOnInitDone(WXHWND hDlg)
HWND hFileDlg = ::GetParent((HWND)hDlg); HWND hFileDlg = ::GetParent((HWND)hDlg);
// set HWND so that our DoMoveWindow() works correctly // set HWND so that our DoMoveWindow() works correctly
SetHWND((WXHWND)hFileDlg); TempHWNDSetter set(this, (WXHWND)hFileDlg);
if ( m_centreDir ) if ( m_centreDir )
{ {
@@ -333,9 +333,6 @@ void wxFileDialog::MSWOnInitDone(WXHWND hDlg)
// Call selection change handler so that update handler will be // Call selection change handler so that update handler will be
// called once with no selection. // called once with no selection.
MSWOnSelChange(hDlg); MSWOnSelChange(hDlg);
// we shouldn't destroy this HWND
SetHWND(NULL);
} }
void wxFileDialog::MSWOnSelChange(WXHWND hDlg) void wxFileDialog::MSWOnSelChange(WXHWND hDlg)
@@ -402,11 +399,9 @@ static bool ShowCommFileDialog(OPENFILENAME *of, long style)
void wxFileDialog::MSWOnInitDialogHook(WXHWND hwnd) void wxFileDialog::MSWOnInitDialogHook(WXHWND hwnd)
{ {
SetHWND(hwnd); TempHWNDSetter set(this, hwnd);
CreateExtraControl(); CreateExtraControl();
SetHWND(NULL);
} }
int wxFileDialog::ShowModal() int wxFileDialog::ShowModal()

View File

@@ -136,7 +136,7 @@ wxMessageDialog::HookFunction(int code, WXWPARAM wParam, WXLPARAM lParam)
wnd->m_hook = NULL; wnd->m_hook = NULL;
HookMap().erase(tid); 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 // replace the static text with an edit control if the message box is
// too big to fit the display // too big to fit the display
@@ -151,9 +151,6 @@ wxMessageDialog::HookFunction(int code, WXWPARAM wParam, WXLPARAM lParam)
if ( wnd->GetMessageDialogStyle() & wxCENTER ) if ( wnd->GetMessageDialogStyle() & wxCENTER )
wnd->Center(); // center on parent wnd->Center(); // center on parent
//else: default behaviour, center on screen //else: default behaviour, center on screen
// there seems to be no reason to leave it set
wnd->SetHWND(NULL);
} }
return rc; return rc;