diff --git a/include/wx/qt/msgdlg.h b/include/wx/qt/msgdlg.h index c4b718696d..43616817bd 100644 --- a/include/wx/qt/msgdlg.h +++ b/include/wx/qt/msgdlg.h @@ -10,8 +10,6 @@ #include "wx/msgdlg.h" -class QMessageBox; - class WXDLLIMPEXP_CORE wxMessageDialog : public wxMessageDialogBase { public: diff --git a/src/qt/msgdlg.cpp b/src/qt/msgdlg.cpp index 22251b0533..cade4e8010 100644 --- a/src/qt/msgdlg.cpp +++ b/src/qt/msgdlg.cpp @@ -14,13 +14,26 @@ #include "wx/qt/private/winevent.h" #include +#include class wxQtMessageDialog : public wxQtEventSignalHandler< QMessageBox, wxMessageDialog > { +public: + wxQtMessageDialog( wxWindow *parent, wxMessageDialog *handler, const wxPoint& pos ); - public: - wxQtMessageDialog( wxWindow *parent, wxMessageDialog *handler ); + // 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; }; @@ -28,90 +41,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 ); - - PostCreation(false); - - Centre(wxBOTH | wxCENTER_FRAME); + // The rest of the initialization will be done in InitializeIfNeeded() + // called from ShowModal(). } wxIMPLEMENT_CLASS(wxMessageDialog,wxDialog); @@ -121,8 +54,11 @@ int wxMessageDialog::ShowModal() WX_HOOK_MODAL_DIALOG(); wxCHECK_MSG( m_qtWindow, -1, "Invalid dialog" ); + auto dlg = static_cast(m_qtWindow); + dlg->InitializeIfNeeded(*this); + // Exec may return a wx identifier if a close event is generated - int ret = static_cast(m_qtWindow)->exec(); + int ret = dlg->exec(); switch ( ret ) { case QMessageBox::Ok: @@ -145,7 +81,106 @@ 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() ) ); + + const wxString& extendedMessage = msgdlg.GetExtendedMessage(); + if ( !extendedMessage.empty() ) + { + setInformativeText( wxQtConvertString( extendedMessage ) ); + } + + // Buttons + const long style = msgdlg.GetMessageDialogStyle(); + + const auto applyCustomLabelIfSet = [](QPushButton* button, const wxString& label) + { + if ( !label.empty() ) + button->setText( wxQtConvertString( label ) ); + }; + + if ( style & wxOK ) + applyCustomLabelIfSet(addButton( QMessageBox::Ok ), msgdlg.GetOKLabel()); + if ( style & wxCANCEL ) + applyCustomLabelIfSet(addButton( QMessageBox::Cancel ), msgdlg.GetCancelLabel()); + if ( style & wxYES_NO ) + { + applyCustomLabelIfSet(addButton( QMessageBox::Yes ), msgdlg.GetYesLabel()); + applyCustomLabelIfSet(addButton( QMessageBox::No ), msgdlg.GetNoLabel()); + } + + // 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); +}