Displaying OkCancel window

Hi, a C++ and JUCE newbie is here.

I need to display a OkCancel message box from within a component.

The NativeMessageBox::showOkCancelBox demands a ModalComponentManager::Callback* parameter. As I understand I could use ModalCallbackFunction::create function to get the pointer, however the function I specify to create function must be a static function. However if it’s a static function it can’t access class instance that shows the message dialog.

I don’t understand how to resolve this… An example would be very helpful. I thought of something like this:

        ModalComponentManager::Callback* callback =
            ModalCallbackFunction::create(&this->modalStateFinished);

        NativeMessageBox::showOkCancelBox(
            AlertWindow::AlertIconType::QuestionIcon,
            "Clear current list?",
            "Do you want to clear currently displayed track",
            this->getParentComponent(),
            callback
        );

But making modalStateFinished static kills the idea of accessing the object instance… Please advise.

Usually you would provide a lambda as the callback:

const auto callback = juce::ModalCallbackFunction::create ([this] (int result) {
    // Can write code here as though it were a member function.
});

If you’d rather use an actual member function, you can use std::bind to bind it to a lambda:

const auto callback = juce::ModalCallbackFunction::create (std::bind (&MyClass::modalStateFinished, this));
3 Likes

Thank you! I just solved this problem. For anyone who encountered the same problem, here’s the detailed solution.

const auto callback = juce::ModalCallbackFunction::create ([this](int result) {
    if (result == 0) {}// result == 0 means you click Cancel
    if (result == 1) {}// result == 1 means you click OK
});

juce::NativeMessageBox::showOkCancelBox(juce::AlertWindow::InfoIcon,
                    "Title", "Message", nullptr, callback);
1 Like