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 =

            "Clear current list?",
            "Do you want to clear currently displayed track",

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));

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

                    "Title", "Message", nullptr, callback);
1 Like