diff --git a/include/wx/generic/progdlgg.h b/include/wx/generic/progdlgg.h index 9c957e7ebf..0840acceff 100644 --- a/include/wx/generic/progdlgg.h +++ b/include/wx/generic/progdlgg.h @@ -62,7 +62,8 @@ public: Uncancelable = -1, // dialog can't be canceled Canceled, // can be cancelled and, in fact, was Continue, // can be cancelled but wasn't - Finished // finished, waiting to be removed from screen + Finished, // finished, waiting to be removed from screen + Dismissed // was closed by user after finishing }; protected: diff --git a/interface/wx/progdlg.h b/interface/wx/progdlg.h index ac23bb7fff..517202bac5 100644 --- a/interface/wx/progdlg.h +++ b/interface/wx/progdlg.h @@ -29,9 +29,8 @@ Causes the progress dialog to disappear from screen as soon as the maximum value of the progress meter has been reached. If this style is not present, the dialog will become a modal dialog - (see wxDialog::ShowModal) once the maximum value has been reached; - this results in processing of pending events and may cause - unwanted re-entrancies. + (see wxDialog::ShowModal) once the maximum value has been reached + and wait for the user to dismiss it. @style{wxPD_SMOOTH} Causes smooth progress of the gauge control (uses a wxGauge with the @c wxGA_SMOOTH style). @@ -173,6 +172,13 @@ public: dialog or ask the user for the confirmation and if the abort is not confirmed the dialog may be resumed with Resume() function. + If @a value is the maximum value for the dialog, the behaviour of the + function depends on whether @c wxPD_AUTO_HIDE was used when the dialog + was created. If it was, the dialog is hidden and the function returns + immediately. If it was not, the dialog becomes a modal dialog and waits + for the user to dismiss it, meaning that this function does not return + until this happens. + Notice that you may want to call Fit() to change the dialog size to conform to the length of the new message if desired. The dialog does not do this automatically. @@ -180,8 +186,6 @@ public: @param value The new value of the progress meter. It should be less than or equal to the maximum value given to the constructor. - See @c wxPD_AUTO_HIDE style for more info about the behaviour of - wxProgressDialog when @a value is the maximum value given in the ctor. @param newmsg The new messages for the progress dialog text, if it is empty (which is the default) the message is not changed. diff --git a/src/msw/progdlg.cpp b/src/msw/progdlg.cpp index 1aa5298b24..59cf103824 100644 --- a/src/msw/progdlg.cpp +++ b/src/msw/progdlg.cpp @@ -27,6 +27,7 @@ #include "wx/msw/private/msgdlg.h" #include "wx/progdlg.h" +#include "wx/evtloop.h" using namespace wxMSWMessageDialog; @@ -119,13 +120,37 @@ private: LONG_PTR dwRefData); }; +namespace +{ + +// A custom event loop which runs until the state of the dialog becomes +// "Dismissed". +class wxProgressDialogModalLoop : public wxEventLoop +{ +public: + wxProgressDialogModalLoop(wxProgressDialogSharedData& data) + : m_data(data) + { + } + +protected: + virtual void OnNextIteration() + { + wxCriticalSectionLocker locker(m_data.m_cs); + + if ( m_data.m_state == wxProgressDialog::Dismissed ) + Exit(); + } + + wxProgressDialogSharedData& m_data; + + wxDECLARE_NO_COPY_CLASS(wxProgressDialogModalLoop); +}; + // ============================================================================ // Helper functions // ============================================================================ -namespace -{ - // This function returns true if the progress dialog with the given style // (combination of wxPD_XXX constants) needs the "Close" button and this button // only, i.e. not a "Cancel" one. @@ -324,53 +349,62 @@ bool wxProgressDialog::Update(int value, const wxString& newmsg, bool *skip) #ifdef wxHAS_MSW_TASKDIALOG if ( HasNativeProgressDialog() ) { - wxCriticalSectionLocker locker(m_sharedData->m_cs); - - // Do nothing in canceled state. - if ( !DoNativeBeforeUpdate(skip) ) - return false; - - value /= m_factor; - - wxASSERT_MSG( value <= m_maximum, wxT("invalid progress value") ); - - m_sharedData->m_value = value; - m_sharedData->m_notifications |= wxSPDD_VALUE_CHANGED; - - if ( !newmsg.empty() ) { - m_message = newmsg; - m_sharedData->m_message = newmsg; - m_sharedData->m_notifications |= wxSPDD_MESSAGE_CHANGED; - } + wxCriticalSectionLocker locker(m_sharedData->m_cs); - if ( m_sharedData->m_progressBarMarquee ) - { - m_sharedData->m_progressBarMarquee = false; - m_sharedData->m_notifications |= wxSPDD_PBMARQUEE_CHANGED; - } + // Do nothing in canceled state. + if ( !DoNativeBeforeUpdate(skip) ) + return false; - UpdateExpandedInformation( value ); + value /= m_factor; - // Has the progress bar finished? - if ( value == m_maximum ) - { - if ( m_state == Finished ) - return true; + wxASSERT_MSG( value <= m_maximum, wxT("invalid progress value") ); + m_sharedData->m_value = value; + m_sharedData->m_notifications |= wxSPDD_VALUE_CHANGED; + + if ( !newmsg.empty() ) + { + m_message = newmsg; + m_sharedData->m_message = newmsg; + m_sharedData->m_notifications |= wxSPDD_MESSAGE_CHANGED; + } + + if ( m_sharedData->m_progressBarMarquee ) + { + m_sharedData->m_progressBarMarquee = false; + m_sharedData->m_notifications |= wxSPDD_PBMARQUEE_CHANGED; + } + + UpdateExpandedInformation( value ); + + // If we didn't just reach the finish, all we have to do is to + // return true if the dialog wasn't cancelled and false otherwise. + if ( value != m_maximum || m_state == Finished ) + return m_sharedData->m_state != Canceled; + + + // On finishing, the dialog without wxPD_AUTO_HIDE style becomes a + // modal one meaning that we must block here until the user + // dismisses it. m_state = Finished; m_sharedData->m_state = Finished; m_sharedData->m_notifications |= wxSPDD_FINISHED; - if( !HasPDFlag(wxPD_AUTO_HIDE) && newmsg.empty() ) + if ( !HasPDFlag(wxPD_AUTO_HIDE) && newmsg.empty() ) { // Provide the finishing message if the application didn't. m_message = _("Done."); m_sharedData->m_message = m_message; m_sharedData->m_notifications |= wxSPDD_MESSAGE_CHANGED; } - } + } // unlock m_sharedData->m_cs - return m_sharedData->m_state != Canceled; + // We only get here when we need to wait for the dialog to terminate so + // do just this by running a custom event loop until the dialog is + // dismissed. + wxProgressDialogModalLoop loop(*m_sharedData); + loop.Run(); + return true; } #endif // wxHAS_MSW_TASKDIALOG @@ -774,6 +808,10 @@ void* wxProgressDialogTaskRunner::Entry() if ( FAILED(hr) ) wxLogApiError( "TaskDialogIndirect", hr ); + // If the main thread is waiting for us to exit inside the event loop in + // Update(), wake it up so that it checks our status again. + wxWakeUpIdle(); + return NULL; } @@ -820,6 +858,11 @@ wxProgressDialogTaskRunner::TaskDialogCallbackProc case IDCANCEL: if ( sharedData->m_state == wxProgressDialog::Finished ) { + // If the main thread is waiting for us, tell it that + // we're gone (and if it doesn't wait, it's harmless). + sharedData->m_state = wxProgressDialog::Dismissed; + + // Let Windows close the dialog. return FALSE; }