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.
This commit is contained in:
Jose Lorenzo
2017-04-15 16:31:13 +01:00
committed by Vadim Zeitlin
parent d2c2274baa
commit b6f2973c41
3 changed files with 41 additions and 7 deletions

View File

@@ -161,10 +161,10 @@ int wxDialog::ShowModal()
// NOTE: this will cause a gtk_grab_add() during Show() // NOTE: this will cause a gtk_grab_add() during Show()
gtk_window_set_modal(GTK_WINDOW(m_widget), true); gtk_window_set_modal(GTK_WINDOW(m_widget), true);
Show( true );
m_modalShowing = true; m_modalShowing = true;
Show( true );
wxOpenModalDialogLocker modalLock; wxOpenModalDialogLocker modalLock;
// Prevent the widget from being destroyed if the user closes the window. // Prevent the widget from being destroyed if the user closes the window.

View File

@@ -181,17 +181,17 @@ int wxDialog::ShowModal()
wxASSERT_MSG( !IsModal(), wxT("ShowModal() can't be called twice") ); wxASSERT_MSG( !IsModal(), wxT("ShowModal() can't be called twice") );
wxDialogModalDataTiedPtr modalData(&m_modalData,
new wxDialogModalData(this));
Show(); Show();
// EndModal may have been called from InitDialog handler (called from // EndModal may have been called from InitDialog handler (called from
// inside Show()) and hidden the dialog back again // inside Show()) and hidden the dialog back again
if ( IsShown() ) if ( IsShown() )
{
// enter and run the modal loop
wxDialogModalDataTiedPtr modalData(&m_modalData,
new wxDialogModalData(this));
modalData->RunLoop(); modalData->RunLoop();
} else
m_modalData->ExitLoop();
return GetReturnCode(); return GetReturnCode();
} }

View File

@@ -36,11 +36,13 @@ private:
#endif #endif
CPPUNIT_TEST( FileDialog ); CPPUNIT_TEST( FileDialog );
CPPUNIT_TEST( CustomDialog ); CPPUNIT_TEST( CustomDialog );
CPPUNIT_TEST( InitDialog );
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
void MessageDialog(); void MessageDialog();
void FileDialog(); void FileDialog();
void CustomDialog(); void CustomDialog();
void InitDialog();
wxDECLARE_NO_COPY_CLASS(ModalDialogsTestCase); wxDECLARE_NO_COPY_CLASS(ModalDialogsTestCase);
}; };
@@ -132,4 +134,36 @@ void ModalDialogsTestCase::CustomDialog()
CPPUNIT_ASSERT_EQUAL( 42, dlg.m_value ); 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 #endif // HAVE_VARIADIC_MACROS