From 7969ec361193f8f2d56803bdfe2c360724febe4a Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 6 Mar 2013 20:56:33 +0000 Subject: [PATCH] NativeAlertWindow callback implementations. --- .../native/juce_android_Windowing.cpp | 32 +++++++-------- .../native/juce_ios_Windowing.mm | 15 +++---- .../native/juce_linux_Windowing.cpp | 5 ++- .../native/juce_mac_Windowing.mm | 39 ++++++++++--------- .../native/juce_win32_Windowing.cpp | 5 ++- .../windows/juce_AlertWindow.cpp | 4 +- .../windows/juce_AlertWindow.h | 13 +++---- .../windows/juce_NativeMessageBox.h | 12 ++++-- 8 files changed, 66 insertions(+), 59 deletions(-) diff --git a/modules/juce_gui_basics/native/juce_android_Windowing.cpp b/modules/juce_gui_basics/native/juce_android_Windowing.cpp index 124c6cb4d4..2af79ab89b 100644 --- a/modules/juce_gui_basics/native/juce_android_Windowing.cpp +++ b/modules/juce_gui_basics/native/juce_android_Windowing.cpp @@ -51,15 +51,13 @@ JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, launchApp, void, (JNIEnv* en JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, suspendApp, void, (JNIEnv* env, jobject activity)) { - JUCEApplicationBase* const app = JUCEApplicationBase::getInstance(); - if (app != nullptr) + if (JUCEApplicationBase* const app = JUCEApplicationBase::getInstance()) app->suspended(); } JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, resumeApp, void, (JNIEnv* env, jobject activity)) { - JUCEApplicationBase* const app = JUCEApplicationBase::getInstance(); - if (app != nullptr) + if (JUCEApplicationBase* const app = JUCEApplicationBase::getInstance()) app->resumed(); } @@ -415,9 +413,7 @@ public: buffer = GlobalRef (env->NewIntArray (sizeNeeded)); } - jint* dest = env->GetIntArrayElements ((jintArray) buffer.get(), 0); - - if (dest != nullptr) + if (jint* dest = env->GetIntArrayElements ((jintArray) buffer.get(), 0)) { { Image temp (new PreallocatedImage (clip.getWidth(), clip.getHeight(), @@ -646,9 +642,11 @@ void Process::makeForegroundProcess() //============================================================================== void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType iconType, const String& title, const String& message, - Component* associatedComponent) + Component* associatedComponent, + ModalComponentManager::Callback* callback) { - android.activity.callVoidMethod (JuceAppActivity.showMessageBox, javaString (title).get(), javaString (message).get(), (jlong) 0); + android.activity.callVoidMethod (JuceAppActivity.showMessageBox, javaString (title).get(), + javaString (message).get(), (jlong) (pointer_sized_int) callback); } bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType iconType, @@ -656,10 +654,10 @@ bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType Component* associatedComponent, ModalComponentManager::Callback* callback) { - jassert (callback != 0); // on android, all alerts must be non-modal!! + jassert (callback != nullptr); // on android, all alerts must be non-modal!! - android.activity.callVoidMethod (JuceAppActivity.showOkCancelBox, javaString (title).get(), javaString (message).get(), - (jlong) (pointer_sized_int) callback); + android.activity.callVoidMethod (JuceAppActivity.showOkCancelBox, javaString (title).get(), + javaString (message).get(), (jlong) (pointer_sized_int) callback); return false; } @@ -668,19 +666,17 @@ int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (AlertWindow::AlertIconTy Component* associatedComponent, ModalComponentManager::Callback* callback) { - jassert (callback != 0); // on android, all alerts must be non-modal!! + jassert (callback != nullptr); // on android, all alerts must be non-modal!! - android.activity.callVoidMethod (JuceAppActivity.showYesNoCancelBox, javaString (title).get(), javaString (message).get(), - (jlong) (pointer_sized_int) callback); + android.activity.callVoidMethod (JuceAppActivity.showYesNoCancelBox, javaString (title).get(), + javaString (message).get(), (jlong) (pointer_sized_int) callback); return 0; } JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, alertDismissed, void, (JNIEnv* env, jobject activity, jlong callbackAsLong, jint result)) { - ModalComponentManager::Callback* callback = (ModalComponentManager::Callback*) callbackAsLong; - - if (callback != 0) + if (ModalComponentManager::Callback* callback = (ModalComponentManager::Callback*) callbackAsLong) callback->modalStateFinished (result); } diff --git a/modules/juce_gui_basics/native/juce_ios_Windowing.mm b/modules/juce_gui_basics/native/juce_ios_Windowing.mm index 8ef80c319e..1980de6e0e 100644 --- a/modules/juce_gui_basics/native/juce_ios_Windowing.mm +++ b/modules/juce_gui_basics/native/juce_ios_Windowing.mm @@ -106,9 +106,9 @@ class iOSMessageBox public: iOSMessageBox (const String& title, const String& message, NSString* button1, NSString* button2, NSString* button3, - ModalComponentManager::Callback* callback_, const bool isAsync_) + ModalComponentManager::Callback* cb, const bool async) : result (0), delegate (nil), alert (nil), - callback (callback_), isYesNo (button3 != nil), isAsync (isAsync_) + callback (cb), isYesNo (button3 != nil), isAsync (async) { delegate = [[JuceAlertBoxDelegate alloc] init]; delegate->owner = this; @@ -181,16 +181,16 @@ void JUCE_CALLTYPE NativeMessageBox::showMessageBox (AlertWindow::AlertIconType Component* associatedComponent) { JUCE_AUTORELEASEPOOL - iOSMessageBox mb (title, message, @"OK", nil, nil, 0, false); + iOSMessageBox mb (title, message, @"OK", nil, nil, nullptr, false); (void) mb.getResult(); } void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType iconType, const String& title, const String& message, - Component* associatedComponent) + Component* associatedComponent, + ModalComponentManager::Callback* callback) { - JUCE_AUTORELEASEPOOL - new iOSMessageBox (title, message, @"OK", nil, nil, 0, true); + new iOSMessageBox (title, message, @"OK", nil, nil, callback, true); } bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType iconType, @@ -198,7 +198,8 @@ bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType Component* associatedComponent, ModalComponentManager::Callback* callback) { - ScopedPointer mb (new iOSMessageBox (title, message, @"Cancel", @"OK", nil, callback, callback != nullptr)); + ScopedPointer mb (new iOSMessageBox (title, message, @"Cancel", @"OK", + nil, callback, callback != nullptr)); if (callback == nullptr) return mb->getResult() == 1; diff --git a/modules/juce_gui_basics/native/juce_linux_Windowing.cpp b/modules/juce_gui_basics/native/juce_linux_Windowing.cpp index e183bf6d7b..611c485c78 100644 --- a/modules/juce_gui_basics/native/juce_linux_Windowing.cpp +++ b/modules/juce_gui_basics/native/juce_linux_Windowing.cpp @@ -3386,9 +3386,10 @@ void JUCE_CALLTYPE NativeMessageBox::showMessageBox (AlertWindow::AlertIconType void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType iconType, const String& title, const String& message, - Component* /* associatedComponent */) + Component* associatedComponent, + ModalComponentManager::Callback* callback) { - AlertWindow::showMessageBoxAsync (iconType, title, message); + AlertWindow::showMessageBoxAsync (iconType, title, message, String::empty, associatedComponent, callback); } bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType iconType, diff --git a/modules/juce_gui_basics/native/juce_mac_Windowing.mm b/modules/juce_gui_basics/native/juce_mac_Windowing.mm index 9a72ed421e..9e44939139 100644 --- a/modules/juce_gui_basics/native/juce_mac_Windowing.mm +++ b/modules/juce_gui_basics/native/juce_mac_Windowing.mm @@ -32,13 +32,10 @@ void LookAndFeel::playAlertSound() class OSXMessageBox : private AsyncUpdater { public: - OSXMessageBox (AlertWindow::AlertIconType type, - const String& title_, const String& message_, + OSXMessageBox (AlertWindow::AlertIconType type, const String& t, const String& m, const char* b1, const char* b2, const char* b3, - ModalComponentManager::Callback* callback_, - const bool runAsync) - : iconType (type), title (title_), - message (message_), callback (callback_), + ModalComponentManager::Callback* c, const bool runAsync) + : iconType (type), title (t), message (m), callback (c), button1 (b1), button2 (b2), button3 (b3) { if (runAsync) @@ -47,17 +44,21 @@ public: int getResult() const { - JUCE_AUTORELEASEPOOL - NSInteger r = getRawResult(); - return r == NSAlertDefaultReturn ? 1 : (r == NSAlertOtherReturn ? 2 : 0); + switch (getRawResult()) + { + case NSAlertDefaultReturn: return 1; + case NSAlertOtherReturn: return 2; + default: return 0; + } } static int show (AlertWindow::AlertIconType iconType, const String& title, const String& message, - ModalComponentManager::Callback* callback, const char* b1, const char* b2, const char* b3) + ModalComponentManager::Callback* callback, const char* b1, const char* b2, const char* b3, + bool runAsync) { ScopedPointer mb (new OSXMessageBox (iconType, title, message, b1, b2, b3, - callback, callback != nullptr)); - if (callback == nullptr) + callback, runAsync)); + if (! runAsync) return mb->getResult(); mb.release(); @@ -109,15 +110,15 @@ void JUCE_CALLTYPE NativeMessageBox::showMessageBox (AlertWindow::AlertIconType const String& title, const String& message, Component* /*associatedComponent*/) { - OSXMessageBox box (iconType, title, message, "OK", nullptr, nullptr, nullptr, false); - (void) box.getResult(); + OSXMessageBox::show (iconType, title, message, nullptr, "OK", nullptr, nullptr, false); } void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType iconType, const String& title, const String& message, - Component* /*associatedComponent*/) + Component* /*associatedComponent*/, + ModalComponentManager::Callback* callback) { - new OSXMessageBox (iconType, title, message, "OK", nullptr, nullptr, nullptr, true); + OSXMessageBox::show (iconType, title, message, callback, "OK", nullptr, nullptr, true); } bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType iconType, @@ -125,7 +126,8 @@ bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType Component* /*associatedComponent*/, ModalComponentManager::Callback* callback) { - return OSXMessageBox::show (iconType, title, message, callback, "OK", "Cancel", nullptr) == 1; + return OSXMessageBox::show (iconType, title, message, callback, + "OK", "Cancel", nullptr, callback != nullptr) == 1; } int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (AlertWindow::AlertIconType iconType, @@ -133,7 +135,8 @@ int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (AlertWindow::AlertIconTy Component* /*associatedComponent*/, ModalComponentManager::Callback* callback) { - return OSXMessageBox::show (iconType, title, message, callback, "Yes", "Cancel", "No"); + return OSXMessageBox::show (iconType, title, message, callback, + "Yes", "Cancel", "No", callback != nullptr); } diff --git a/modules/juce_gui_basics/native/juce_win32_Windowing.cpp b/modules/juce_gui_basics/native/juce_win32_Windowing.cpp index 45dab29709..0942105ae9 100644 --- a/modules/juce_gui_basics/native/juce_win32_Windowing.cpp +++ b/modules/juce_gui_basics/native/juce_win32_Windowing.cpp @@ -2891,9 +2891,10 @@ void JUCE_CALLTYPE NativeMessageBox::showMessageBox (AlertWindow::AlertIconType void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType iconType, const String& title, const String& message, - Component* associatedComponent) + Component* associatedComponent, + ModalComponentManager::Callback* callback) { - new WindowsMessageBox (iconType, title, message, associatedComponent, MB_OK, 0, true); + new WindowsMessageBox (iconType, title, message, associatedComponent, MB_OK, callback, true); } bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType iconType, diff --git a/modules/juce_gui_basics/windows/juce_AlertWindow.cpp b/modules/juce_gui_basics/windows/juce_AlertWindow.cpp index 06aa40e713..dd0dc72507 100644 --- a/modules/juce_gui_basics/windows/juce_AlertWindow.cpp +++ b/modules/juce_gui_basics/windows/juce_AlertWindow.cpp @@ -631,7 +631,7 @@ void AlertWindow::showMessageBox (AlertIconType iconType, } else { - AlertWindowInfo info (title, message, associatedComponent, iconType, 1, 0, true); + AlertWindowInfo info (title, message, associatedComponent, iconType, 1, nullptr, true); info.button1 = buttonText.isEmpty() ? TRANS("ok") : buttonText; info.invoke(); @@ -648,7 +648,7 @@ void AlertWindow::showMessageBoxAsync (AlertIconType iconType, { if (LookAndFeel::getDefaultLookAndFeel().isUsingNativeAlertWindows()) { - NativeMessageBox::showMessageBoxAsync (iconType, title, message, associatedComponent); + NativeMessageBox::showMessageBoxAsync (iconType, title, message, associatedComponent, callback); } else { diff --git a/modules/juce_gui_basics/windows/juce_AlertWindow.h b/modules/juce_gui_basics/windows/juce_AlertWindow.h index 1ba5b12de5..9bb375fd58 100644 --- a/modules/juce_gui_basics/windows/juce_AlertWindow.h +++ b/modules/juce_gui_basics/windows/juce_AlertWindow.h @@ -248,7 +248,7 @@ public: @param message a longer, more descriptive message to show underneath the headline @param buttonText the text to show in the button - if this string is empty, the - default string "ok" (or a localised version) will be used. + default string "OK" (or a localised version) will be used. @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. @@ -271,13 +271,12 @@ public: @param message a longer, more descriptive message to show underneath the headline @param buttonText the text to show in the button - if this string is empty, the - default string "ok" (or a localised version) will be used. + default string "OK" (or a localised version) will be used. @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 + modalStateFinished() when the box is dismissed. 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. @@ -305,7 +304,7 @@ public: @param message a longer, more descriptive message to show underneath the headline @param button1Text the text to show in the first button - if this string is - empty, the default string "ok" (or a localised version of it) + empty, the default string "OK" (or a localised version of it) will be used. @param button2Text the text to show in the second button - if this string is empty, the default string "cancel" (or a localised version of it) @@ -316,7 +315,7 @@ public: @param callback if this is non-null, the menu will be launched asynchronously, returning immediately, and 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 + 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. @@ -368,7 +367,7 @@ public: returning immediately, and the callback will receive a call to its modalStateFinished() when the box is dismissed, with its parameter being 1 if the "yes" button was pressed, 2 for the "no" button, or 0 - if it was cancelled, The callback object will be owned and deleted by the + if it was cancelled. 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. diff --git a/modules/juce_gui_basics/windows/juce_NativeMessageBox.h b/modules/juce_gui_basics/windows/juce_NativeMessageBox.h index 8f2e0a7e3f..65dc809835 100644 --- a/modules/juce_gui_basics/windows/juce_NativeMessageBox.h +++ b/modules/juce_gui_basics/windows/juce_NativeMessageBox.h @@ -62,11 +62,17 @@ 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. 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 (AlertWindow::AlertIconType iconType, - const String& title, - const String& message, - Component* associatedComponent = nullptr); + const String& title, + const String& message, + Component* associatedComponent = nullptr, + ModalComponentManager::Callback* callback = nullptr); /** Shows a dialog box with two buttons.