It’s basically a wrapper for enterModalState() so that I can do extra stuff, such as count the number of open modal dialogs of this sort (and be able to kill them from elsewhere), disable my menuBar, etc.
void KContainer::myEnterModalState(Component* comp)
{
modalComponents.add(comp); // count the number of open modals
// disable all menubar items, and mainComponent accessibility
auto* mainComponent = getMainComponent();
if (mainComponent && modalComponents.size())
mainComponent->enableMenuBarAndAccessibility(false);
comp->enterModalState(comp->isShowing());
// make Visible (was added with addChildComponent()) and bring to front
comp->setVisible(true);
comp->toFront(true);
}
You’re probably wondering where the callback is. In my previous code, I guess I forgot to show an example of the handling of the callback (if it’s a type of dialog that requires action):
// buttons go in the order you want them, left to right
aw->addButton ("OK", 1, KeyPress (KeyPress::returnKey));
aw->addButton ("Cancel", 0, KeyPress (KeyPress::escapeKey));
aw->callback = [safeThis = SafePointer<MainComponent>(this)] (int result)
{
if (safeThis)
DBG(String::formatted("here we are - result %d", result));
};
aw->display(); // put it up, disable background
The problem is, this doesn’t make sense without getting into the fact that I store the callback in the KAlertWindow class:
public:
std::function<void(int result)> callback;
…and then provide a way to replace the lambda functions of the OK and cancel buttons so that they call a function in the KAlertWindow class and I can execute the callback. I’m sorry that I can’t post all the source for this as it’s spread out through the project, but here’s some more pieces:
Modification to juce_AlertWindow.h to allow getting the buttons:
public:
// provide access to the buttons to replace onClick() lambdas
OwnedArray<TextButton>* getButtons() { return &buttons; }
Override of AlertWindow::addButton() to allow reassigning the lambda:
void KAlertWindow::addButton (const String& name,
int returnValue,
const KeyPress& shortcutKey1,
const KeyPress& shortcutKey2)
{
// do the normal stuff
AlertWindow::addButton(name, returnValue, shortcutKey1, shortcutKey2);
// overwite the button onClick() lambdas to go to my function
// get the most recent one just assigned
if (auto b = getButtons()->getLast())
b->onClick = [this, b] { exitKAlert (b); };
}
The function that gets called when the buttons are clicked:
//==============================================================================
void KAlertWindow::exitKAlert (Button* button)
{
// the button onClick() lambdas are redirected here
kc->myExitModalState(safeThis);
// execute the callback with the result
callback(button->getCommandID());
// destroy the KAlertWindow
delete safeThis;
}
myExitModalState:
void KContainer::myExitModalState(Component* comp)
{
// store pointers to open alerts for deleting if open when quit
// do not delete object, we are already doing it
modalComponents.removeObject(comp, false);
// reenable all menuBar items when the last open "modal" is closed, and accessibility
auto* mainComponent = getMainComponent();
if (mainComponent && modalComponents.size() == 0)
mainComponent->enableMenuBarAndAccessibility(true);
// simply exit the modal state; we already handled the callback
comp->exitModalState();
}
I’ve removed extraneous stuff that wouldn’t make any sense, so I hope it still works. This may be just my convoluted way of doing it so it may not be useful, but I hope it is.