AlertWindow and callback


#1

Hi,


This seems really obvious but I can't find what I need, sorry if this question sounds stupid.


I want to have a modal AlertWindow with one Text field. The static methods of AlertWindow do not allow the addition of this field, so I have to instantiate an AlertWindow and add the TextEditor and two Buttons (OK/Cancel).


This part is easy, however, I do NOT want to use the "runModalLoop" method. I would like to use a callback mechanism, however it seems there is no way I can set it via the constructor or a setter, as it is possible using the static method "showMessageBoxASync".


How to set a callback with the normal constructor of AlertWindow?


Thanks,
Julien.
 


#2

The reason there's no easy method for that is that if you've added fields to the box then you'll need to have access to the AlertWindow after it has finished, so that you can read + use the values that were entered.

So the best plan is probably to just create an instance of your window on the heap, run it modally asynchronously, and then when the callback arrives, read your text field and delete the window.


#3


Thanks. I agree with what you say, however HOW to run the modal loop asynchronously isn't obvious. I used the "enterModalState" method, is it correct?
Anyway, I managed to get something to work, but I think it is not very clean, because I can't get rid of the raw pointers. If I use std::unique_ptrs, it crashes at some point. This is the working version with raw pointers:


class Myclass
{
    // Arg, raw pointers ahead!!
    AlertWindow* alertWindow;
    AlertWindowCallback* alertWindowCallback;    // Derives from ModalComponentManager::Callback
    
    void Myclass::onRenameButtonClicked() noexcept
    {
        // Let's create our modal window and instantiate its callback.
        alertWindow = new AlertWindow(..., AlertWindow::QuestionIcon);
        // Adds custom fields.
        alertWindow->addTextEditor(...);
        alertWindow->addButton(...);
        alertWindow->addButton(...);
        
        alertWindowCallback = new AlertWindowCallback(*this);
        // Is this the right way to show a modal window??
        alertWindow->enterModalState(true, alertWindowCallback, true);
    }
    
    ...
}

 

It works, however like I said, there are raw pointers.


The problem is that if I use std::unique_ptrs, it crashes. That is I guess because of the last parameter of "enterModalState" set to true, to let Juce destroying the objects. But if I put it to false, the program crashes *on exiting*, stating that I leaked resources (which was just why I wanted to use unique_ptr!)...


It may come from the way I dismiss the AlertWindow in the callback (I use "removeFromDesktop", is it correct)?


Thanks!
Julien.