Allow changing wxMessageDialog after creating it

Refactor the code to use wxMessageDialog attributes (such as label,
style etc) values at the moment when the dialog is shown, not when it is
created. This still doesn't handle changing them after showing the
dialog before showing them again, but this is a much more rare use case
than creating the dialog and then setting its attributes later.

There should be no changes in behaviour.
This commit is contained in:
Vadim Zeitlin 2024-02-11 13:45:42 +01:00
parent 4b577f2932
commit b960271f97

View file

@ -19,7 +19,20 @@
class wxQtMessageDialog : public wxQtEventSignalHandler< QMessageBox, wxMessageDialog >
{
public:
wxQtMessageDialog( wxWindow *parent, wxMessageDialog *handler );
wxQtMessageDialog( wxWindow *parent, wxMessageDialog *handler, const wxPoint& pos );
// Initialize the dialog to match the state of the associated wxMessageDialog.
//
// Does nothing if it had been already initialized.
void InitializeIfNeeded(wxMessageDialog& msgdlg);
private:
// Initial position, may be wxDefaultPosition.
wxPoint m_pos;
// Set to true once the dialog has been initialized, which happens before
// it's shown for the first time.
bool m_initialized = false;
};
@ -27,88 +40,10 @@ wxMessageDialog::wxMessageDialog( wxWindow *parent, const wxString& message,
const wxString& caption, long style, const wxPoint& pos )
: wxMessageDialogBase( parent, message, caption, style )
{
wxQtMessageDialog *dlg = new wxQtMessageDialog( parent, this );
m_qtWindow = dlg;
m_qtWindow = new wxQtMessageDialog( parent, this, pos );
// Set properties
Move( pos );
dlg->setText( wxQtConvertString( message ) );
dlg->setWindowTitle( wxQtConvertString( caption ) );
// Apply the style
SetWindowStyleFlag( style );
// Buttons
if ( style & wxOK )
dlg->addButton( QMessageBox::Ok );
if ( style & wxCANCEL )
dlg->addButton( QMessageBox::Cancel );
if ( style & wxYES_NO )
{
dlg->addButton( QMessageBox::Yes );
dlg->addButton( QMessageBox::No );
}
// Default button
if ( style & wxNO_DEFAULT )
dlg->setDefaultButton( QMessageBox::No );
else if ( style & wxCANCEL_DEFAULT )
dlg->setDefaultButton( QMessageBox::Cancel );
else
{
// Default to OK or Yes
if ( style & wxOK )
dlg->setDefaultButton( QMessageBox::Ok );
else
dlg->setDefaultButton( QMessageBox::Yes );
}
// Icon
int numIcons = 0;
if ( style & wxICON_NONE )
{
numIcons++;
dlg->setIcon( QMessageBox::NoIcon );
}
if ( style & wxICON_EXCLAMATION )
{
numIcons++;
dlg->setIcon( QMessageBox::Warning );
}
if ( style & wxICON_ERROR )
{
numIcons++;
dlg->setIcon( QMessageBox::Critical );
}
if ( style & wxICON_QUESTION )
{
numIcons++;
dlg->setIcon( QMessageBox::Question );
}
if ( style & wxICON_INFORMATION )
{
numIcons++;
dlg->setIcon( QMessageBox::Information );
}
wxCHECK_RET( numIcons <= 1, "Multiple icon definitions" );
if ( numIcons == 0 )
{
// Use default
if ( style & wxYES_NO )
dlg->setIcon( QMessageBox::Question );
else
dlg->setIcon( QMessageBox::Information );
}
if ( style & wxSTAY_ON_TOP )
dlg->setWindowModality( Qt::ApplicationModal );
Centre(wxBOTH | wxCENTER_FRAME);
// The rest of the initialization will be done in InitializeIfNeeded()
// called from ShowModal().
}
wxIMPLEMENT_CLASS(wxMessageDialog,wxDialog);
@ -118,8 +53,11 @@ int wxMessageDialog::ShowModal()
WX_HOOK_MODAL_DIALOG();
wxCHECK_MSG( m_qtWindow, -1, "Invalid dialog" );
auto dlg = static_cast<wxQtMessageDialog*>(m_qtWindow);
dlg->InitializeIfNeeded(*this);
// Exec may return a wx identifier if a close event is generated
int ret = static_cast<QDialog*>(m_qtWindow)->exec();
int ret = dlg->exec();
switch ( ret )
{
case QMessageBox::Ok:
@ -142,7 +80,94 @@ wxMessageDialog::~wxMessageDialog()
//=============================================================================
wxQtMessageDialog::wxQtMessageDialog( wxWindow *parent, wxMessageDialog *handler )
: wxQtEventSignalHandler< QMessageBox, wxMessageDialog >( parent, handler )
wxQtMessageDialog::wxQtMessageDialog(wxWindow *parent,
wxMessageDialog *handler,
const wxPoint& pos)
: wxQtEventSignalHandler< QMessageBox, wxMessageDialog >( parent, handler ),
m_pos(pos)
{
}
void wxQtMessageDialog::InitializeIfNeeded(wxMessageDialog& msgdlg)
{
setText( wxQtConvertString( msgdlg.GetMessage() ) );
setWindowTitle( wxQtConvertString( msgdlg.GetTitle() ) );
// Buttons
const long style = msgdlg.GetMessageDialogStyle();
if ( style & wxOK )
addButton( QMessageBox::Ok );
if ( style & wxCANCEL )
addButton( QMessageBox::Cancel );
if ( style & wxYES_NO )
{
addButton( QMessageBox::Yes );
addButton( QMessageBox::No );
}
// Default button
if ( style & wxNO_DEFAULT )
setDefaultButton( QMessageBox::No );
else if ( style & wxCANCEL_DEFAULT )
setDefaultButton( QMessageBox::Cancel );
else
{
// Default to OK or Yes
if ( style & wxOK )
setDefaultButton( QMessageBox::Ok );
else
setDefaultButton( QMessageBox::Yes );
}
// Icon
int numIcons = 0;
if ( style & wxICON_NONE )
{
numIcons++;
setIcon( QMessageBox::NoIcon );
}
if ( style & wxICON_EXCLAMATION )
{
numIcons++;
setIcon( QMessageBox::Warning );
}
if ( style & wxICON_ERROR )
{
numIcons++;
setIcon( QMessageBox::Critical );
}
if ( style & wxICON_QUESTION )
{
numIcons++;
setIcon( QMessageBox::Question );
}
if ( style & wxICON_INFORMATION )
{
numIcons++;
setIcon( QMessageBox::Information );
}
wxCHECK_RET( numIcons <= 1, "Multiple icon definitions" );
if ( numIcons == 0 )
{
// Use default
if ( style & wxYES_NO )
setIcon( QMessageBox::Question );
else
setIcon( QMessageBox::Information );
}
if ( msgdlg.HasFlag(wxSTAY_ON_TOP) )
setWindowModality( Qt::ApplicationModal );
// Now that the dialog has its size, we can position it.
if ( m_pos != wxDefaultPosition )
msgdlg.Move( m_pos );
else
msgdlg.Centre(wxBOTH | wxCENTER_FRAME);
}