I need a file open browser for a GUI App (or plugin), and I want to try using the non-native juice version.
If you use the FileChooser class with the non-native flag, you have no real control over things like the size of the dialogBox when it opens.
So I see there is a FileChooserDialogBox class that directly makes a juce open/save browser. So I’m attempting to try the example code provided in FileChooserDialogBox.h.
There are a few errors in it, maybe it’s old. But here is my modified example.
This works but eventually crashes after trying to open it a few times in a row. Something with Listeners being deleted and crashing… I’m really not sure what I’m doing wrong.
// in mainComponent class:
std::unique_ptr<juce::FileChooserDialogBox> dialogBox;
std::unique_ptr<juce::FileBrowserComponent> browser;
std::unique_ptr<juce::WildcardFileFilter> wildcardFilter;
// in code:
void MainComponent::loadFile()
{
wildcardFilter.reset( new WildcardFileFilter ("*", String(), "Foo files"));
browser.reset( new FileBrowserComponent (FileBrowserComponent::canSelectFiles | FileBrowserComponent::openMode,
File(),
wildcardFilter.get(),
nullptr));
dialogBox.reset( new FileChooserDialogBox ("Open some kind of file",
"Please choose some kind of file that you want to open...",
*browser,
false,
findColour(ResizableWindow::backgroundColourId)));
auto onFileSelected = [this] (int r)
{
// this seems some sort of error in the example, makes no sense
// modalStateFinished (r); //<== HERE
auto theFile = browser->getSelectedFile (0);
if (theFile.exists())
DBG("GOOD!" + theFile.getFullPathName());
else
{
DBG("CANCELLED!");
return;
}
};
dialogBox->centreWithDefaultSize (nullptr);
dialogBox->enterModalState (true,
ModalCallbackFunction::create (onFileSelected),
true);
}