From b6f2973c41674f2a1b63d59a5c347915243bd0ca Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Date: Sat, 15 Apr 2017 16:31:13 +0100 Subject: [PATCH] Return true from wxDialog::IsModal() in wxEVT_INIT_DIALOG handler Ensure that checking for dialog modality in wxEVT_INIT_DIALOG handler returns true when the dialog is being shown modally in MSW and GTK. Add a unit test checking that this is the case. Closes #10385. --- src/gtk/dialog.cpp | 4 ++-- src/msw/dialog.cpp | 10 +++++----- tests/controls/dialogtest.cpp | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/gtk/dialog.cpp b/src/gtk/dialog.cpp index b57b9b4992..e3accac65f 100644 --- a/src/gtk/dialog.cpp +++ b/src/gtk/dialog.cpp @@ -161,10 +161,10 @@ int wxDialog::ShowModal() // NOTE: this will cause a gtk_grab_add() during Show() gtk_window_set_modal(GTK_WINDOW(m_widget), true); - Show( true ); - m_modalShowing = true; + Show( true ); + wxOpenModalDialogLocker modalLock; // Prevent the widget from being destroyed if the user closes the window. diff --git a/src/msw/dialog.cpp b/src/msw/dialog.cpp index 577805af5f..803853d8c4 100644 --- a/src/msw/dialog.cpp +++ b/src/msw/dialog.cpp @@ -181,17 +181,17 @@ int wxDialog::ShowModal() wxASSERT_MSG( !IsModal(), wxT("ShowModal() can't be called twice") ); + wxDialogModalDataTiedPtr modalData(&m_modalData, + new wxDialogModalData(this)); + Show(); // EndModal may have been called from InitDialog handler (called from // inside Show()) and hidden the dialog back again if ( IsShown() ) - { - // enter and run the modal loop - wxDialogModalDataTiedPtr modalData(&m_modalData, - new wxDialogModalData(this)); modalData->RunLoop(); - } + else + m_modalData->ExitLoop(); return GetReturnCode(); } diff --git a/tests/controls/dialogtest.cpp b/tests/controls/dialogtest.cpp index 3c2decde87..031757a259 100644 --- a/tests/controls/dialogtest.cpp +++ b/tests/controls/dialogtest.cpp @@ -36,11 +36,13 @@ private: #endif CPPUNIT_TEST( FileDialog ); CPPUNIT_TEST( CustomDialog ); + CPPUNIT_TEST( InitDialog ); CPPUNIT_TEST_SUITE_END(); void MessageDialog(); void FileDialog(); void CustomDialog(); + void InitDialog(); wxDECLARE_NO_COPY_CLASS(ModalDialogsTestCase); }; @@ -132,4 +134,36 @@ void ModalDialogsTestCase::CustomDialog() CPPUNIT_ASSERT_EQUAL( 42, dlg.m_value ); } + +class MyModalDialog : public wxDialog +{ +public: + MyModalDialog() : wxDialog (NULL, wxID_ANY, "Modal Dialog") + { + m_wasModal = false; + Bind( wxEVT_INIT_DIALOG, &MyModalDialog::OnInit, this ); + } + + void OnInit(wxInitDialogEvent& WXUNUSED(event)) + { + m_wasModal = IsModal(); + CallAfter( &MyModalDialog::EndModal, wxID_OK ); + } + + bool WasModal() const + { + return m_wasModal; + } + +private: + bool m_wasModal; +}; + +void ModalDialogsTestCase::InitDialog() +{ + MyModalDialog dlg; + dlg.ShowModal(); + CPPUNIT_ASSERT( dlg.WasModal() ); +} + #endif // HAVE_VARIADIC_MACROS