From a1ed5374638cae61b60bcfb97489d08cbfc5b5bd Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 6 Mar 2013 11:03:05 +0000 Subject: [PATCH] Added callback to AlertWindow::showMessageBoxAsync --- .../windows/juce_AlertWindow.cpp | 69 ++++++++----------- .../windows/juce_AlertWindow.h | 26 +++---- 2 files changed, 44 insertions(+), 51 deletions(-) diff --git a/modules/juce_gui_basics/windows/juce_AlertWindow.cpp b/modules/juce_gui_basics/windows/juce_AlertWindow.cpp index 1d70d97e17..06aa40e713 100644 --- a/modules/juce_gui_basics/windows/juce_AlertWindow.cpp +++ b/modules/juce_gui_basics/windows/juce_AlertWindow.cpp @@ -80,8 +80,8 @@ void AlertWindow::setMessage (const String& message) //============================================================================== void AlertWindow::buttonClicked (Button* button) { - if (button->getParentComponent() != nullptr) - button->getParentComponent()->exitModalState (button->getCommandID()); + if (Component* parent = button->getParentComponent()) + parent->exitModalState (button->getCommandID()); } //============================================================================== @@ -163,8 +163,10 @@ TextEditor* AlertWindow::getTextEditor (const String& nameOfTextEditor) const String AlertWindow::getTextEditorContents (const String& nameOfTextEditor) const { - TextEditor* const t = getTextEditor (nameOfTextEditor); - return t != nullptr ? t->getText() : String::empty; + if (TextEditor* const t = getTextEditor (nameOfTextEditor)) + return t->getText(); + + return String::empty; } @@ -562,12 +564,12 @@ int AlertWindow::getDesktopWindowStyleFlags() const class AlertWindowInfo { public: - AlertWindowInfo (const String& title_, const String& message_, Component* component, - AlertWindow::AlertIconType iconType_, int numButtons_, - ModalComponentManager::Callback* callback_, bool modal_) - : title (title_), message (message_), iconType (iconType_), - numButtons (numButtons_), returnValue (0), associatedComponent (component), - callback (callback_), modal (modal_) + AlertWindowInfo (const String& t, const String& m, Component* component, + AlertWindow::AlertIconType icon, int numButts, + ModalComponentManager::Callback* cb, bool runModally) + : title (t), message (m), iconType (icon), numButtons (numButts), + returnValue (0), associatedComponent (component), + callback (cb), modal (runModally) { } @@ -641,15 +643,16 @@ void AlertWindow::showMessageBoxAsync (AlertIconType iconType, const String& title, const String& message, const String& buttonText, - Component* associatedComponent) + Component* associatedComponent, + ModalComponentManager::Callback* callback) { if (LookAndFeel::getDefaultLookAndFeel().isUsingNativeAlertWindows()) { - return NativeMessageBox::showMessageBoxAsync (iconType, title, message, associatedComponent); + NativeMessageBox::showMessageBoxAsync (iconType, title, message, associatedComponent); } else { - AlertWindowInfo info (title, message, associatedComponent, iconType, 1, 0, false); + AlertWindowInfo info (title, message, associatedComponent, iconType, 1, callback, false); info.button1 = buttonText.isEmpty() ? TRANS("ok") : buttonText; info.invoke(); @@ -665,17 +668,13 @@ bool AlertWindow::showOkCancelBox (AlertIconType iconType, ModalComponentManager::Callback* callback) { if (LookAndFeel::getDefaultLookAndFeel().isUsingNativeAlertWindows()) - { return NativeMessageBox::showOkCancelBox (iconType, title, message, associatedComponent, callback); - } - else - { - AlertWindowInfo info (title, message, associatedComponent, iconType, 2, callback, callback == nullptr); - info.button1 = button1Text.isEmpty() ? TRANS("ok") : button1Text; - info.button2 = button2Text.isEmpty() ? TRANS("cancel") : button2Text; - return info.invoke() != 0; - } + AlertWindowInfo info (title, message, associatedComponent, iconType, 2, callback, callback == nullptr); + info.button1 = button1Text.isEmpty() ? TRANS("ok") : button1Text; + info.button2 = button2Text.isEmpty() ? TRANS("cancel") : button2Text; + + return info.invoke() != 0; } int AlertWindow::showYesNoCancelBox (AlertIconType iconType, @@ -688,18 +687,14 @@ int AlertWindow::showYesNoCancelBox (AlertIconType iconType, ModalComponentManager::Callback* callback) { if (LookAndFeel::getDefaultLookAndFeel().isUsingNativeAlertWindows()) - { return NativeMessageBox::showYesNoCancelBox (iconType, title, message, associatedComponent, callback); - } - else - { - AlertWindowInfo info (title, message, associatedComponent, iconType, 3, callback, callback == nullptr); - info.button1 = button1Text.isEmpty() ? TRANS("yes") : button1Text; - info.button2 = button2Text.isEmpty() ? TRANS("no") : button2Text; - info.button3 = button3Text.isEmpty() ? TRANS("cancel") : button3Text; - return info.invoke(); - } + AlertWindowInfo info (title, message, associatedComponent, iconType, 3, callback, callback == nullptr); + info.button1 = button1Text.isEmpty() ? TRANS("yes") : button1Text; + info.button2 = button2Text.isEmpty() ? TRANS("no") : button2Text; + info.button3 = button3Text.isEmpty() ? TRANS("cancel") : button3Text; + + return info.invoke(); } #if JUCE_MODAL_LOOPS_PERMITTED @@ -708,13 +703,9 @@ bool AlertWindow::showNativeDialogBox (const String& title, bool isOkCancel) { if (isOkCancel) - { return NativeMessageBox::showOkCancelBox (AlertWindow::NoIcon, title, bodyText); - } - else - { - NativeMessageBox::showMessageBox (AlertWindow::NoIcon, title, bodyText); - return true; - } + + NativeMessageBox::showMessageBox (AlertWindow::NoIcon, title, bodyText); + return true; } #endif diff --git a/modules/juce_gui_basics/windows/juce_AlertWindow.h b/modules/juce_gui_basics/windows/juce_AlertWindow.h index c2e872b1e3..1ba5b12de5 100644 --- a/modules/juce_gui_basics/windows/juce_AlertWindow.h +++ b/modules/juce_gui_basics/windows/juce_AlertWindow.h @@ -237,13 +237,11 @@ public: //============================================================================== // easy-to-use message box functions: + #if JUCE_MODAL_LOOPS_PERMITTED /** Shows a dialog box that just has a message and a single button to get rid of it. - If the callback parameter is null, the box is shown modally, and the method will - block until the user has clicked the button (or pressed the escape or return keys). - If the callback parameter is non-null, the box will be displayed and placed into a - modal state, but this method will return immediately, and the callback will be invoked - later when the user dismisses the box. + The box is shown modally, and the method will block until the user has clicked the + button (or pressed the escape or return keys). @param iconType the type of icon to show @param title the headline to show at the top of the box @@ -255,7 +253,6 @@ public: alert window should be associated with. Depending on the look and feel, this might be used for positioning of the alert window. */ - #if JUCE_MODAL_LOOPS_PERMITTED static void JUCE_CALLTYPE showMessageBox (AlertIconType iconType, const String& title, const String& message, @@ -265,11 +262,9 @@ public: /** Shows a dialog box that just has a message and a single button to get rid of it. - If the callback parameter is null, the box is shown modally, and the method will - block until the user has clicked the button (or pressed the escape or return keys). - If the callback parameter is non-null, the box will be displayed and placed into a - modal state, but this method will return immediately, and the callback will be invoked - later when the user dismisses the box. + The box will be displayed and placed into a modal state, but this method will + return immediately, and if a callback was supplied, it will be invoked later + when the user dismisses the box. @param iconType the type of icon to show @param title the headline to show at the top of the box @@ -280,12 +275,19 @@ public: @param associatedComponent if this is non-null, it specifies the component that the alert window should be associated with. Depending on the look and feel, this might be used for positioning of the alert window. + @param callback if this is non-null, the callback will receive a call to its + modalStateFinished() when the box is dismissed, with its parameter + being 1 if the ok button was pressed, or 0 for cancel, The callback object + will be owned and deleted by the system, so make sure that it works + safely and doesn't keep any references to objects that might be deleted + before it gets called. */ static void JUCE_CALLTYPE showMessageBoxAsync (AlertIconType iconType, const String& title, const String& message, const String& buttonText = String::empty, - Component* associatedComponent = nullptr); + Component* associatedComponent = nullptr, + ModalComponentManager::Callback* callback = nullptr); /** Shows a dialog box with two buttons.