FileChooser


#1

Hi Jules,

I observed the following behavior of FileChooser:

When I call browseForFileToSave(…) method the dialog box does not show any files of the specified type/extension even though those files do exist in the current directory. When I type in the filename of one of the existing file, the dialog box showed overwrite warning, I click yes and the dialog box closed. But the file that I’ve selected to be overwritten was not overwritten, it still contains old data.

FYI, I’m using Windows


#2

…eh? Were you expecting it to delete the file for you or something? It just supplies a filename, it doesn’t actually write to it!


#3

And BTW, I just tried the save dialog in Windows, and it does correctly show files of the extension you specify, so no idea what you’re talking about there. You probably just gave it the wrong file extension or something?


#4

Hi gndpoints ,
It would be helpful if you could post your code here, we would be able to help you better.


#5

I’m using the fileChooser to save a snapshot of my plugin to a .png file.
When I call browseFileForSave() the first time to write a new file the dialog box works fine, but when call that method the next time the file that I previously saved does not appear in the dialog box, so I type in the same filename again and the dialog box asked me if I want to overwrite that old file (so obviously fileChooser is aware that a file with the same name and extension exists!, but somehow does not show it). I chose to overwrite, but that file did not get overwritten.
Here’s the code

FileChooser fc (T("Set file name and directory."), File::nonexistent, T(".png"), true); 
if (fc.browseForFileToSave (true))
{
	File             imageFile(fc.getResult());	
	PNGImageFormat   imageFormatter;
	FileOutputStream outputFileStream(imageFile);		
	Rectangle        snapshotArea(0, 0, getWidth(), getHeight());		
        Image*           snapshotImage = createComponentSnapshot (snapshotArea, true);
	imageFormatter.writeImageToStream (*snapshotImage, outputFileStream);	
	deleteAndZero (snapshotImage);
}

Hope that makes it clear


#6

Ok, found the problem why existing files didn’t show up, missed the “*” while specifying the extension in the constructor.
But the overwritting problem is still there…


#7

Like I said in my first post, the filechooser isn’t going to delete the file for you!! You’re opening a stream to it and dumping your data onto the end of the file that’s already there!


#8
FileChooser fc (T("Set file name and directory."), File::nonexistent, T(".png"), true);
if (fc.browseForFileToSave (true))
{
File imageFile(fc.getResult());
// add this-delete the file if it exists &write the new data
if (imageFile.existsAsFile())
    imageFile.deleteFile();
PNGImageFormat imageFormatter;
FileOutputStream outputFileStream(imageFile);
Rectangle snapshotArea(0, 0, getWidth(), getHeight());
Image* snapshotImage = createComponentSnapshot (snapshotArea, true);
imageFormatter.writeImageToStream (*snapshotImage, outputFileStream);
deleteAndZero (snapshotImage);
}

This should solve the problem.


#9

That fixes it, thanks vishvesh


#10