/***** pure UI functions *****/
function saveImage() {
- // XXX: should call filepicker!
- saveCanvas(document.getElementById("mbrotImage"), "/home/robert/temp/canvas-save.png")
+ const bundle = document.getElementById("mbrotBundle");
+ const nsIFilePicker = Components.interfaces.nsIFilePicker;
+ var fp = null;
+ try {
+ fp = Components.classes["@mozilla.org/filepicker;1"]
+ .createInstance(nsIFilePicker);
+ } catch (e) {}
+ if (!fp) return;
+ var promptString = bundle.getString("savePrompt");
+ fp.init(window, promptString, nsIFilePicker.modeSave);
+ fp.appendFilter(bundle.getString("pngFilterName"), "*.png");
+ fp.defaultString = "mandelbrot.png";
+
+ var fpResult = fp.show();
+ if (fpResult != nsIFilePicker.returnCancel) {
+ saveCanvas(document.getElementById("mbrotImage"), fp.file);
+ }
}
function updateIterMenu() {
/***** helper functions from external sources *****/
-// function below is from from http://developer.mozilla.org/en/docs/Code_snippets:Canvas
-function saveCanvas(canvas, destFile) {
- // convert string filepath to an nsIFile
- var file = Components.classes["@mozilla.org/file/local;1"]
- .createInstance(Components.interfaces.nsILocalFile);
- file.initWithPath(destFile);
-
+// function below is based on http://developer.mozilla.org/en/docs/Code_snippets:Canvas
+// custom modifications:
+// - use "a"-prefix on function arguments
+// - take an nsILocalFile as aDestFile argument
+// - always do silent download
+function saveCanvas(aCanvas, aDestFile) {
// create a data url from the canvas and then create URIs of the source and targets
var io = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
- var source = io.newURI(canvas.toDataURL("image/png", ""), "UTF8", null);
- var target = io.newFileURI(file);
+ var source = io.newURI(aCanvas.toDataURL("image/png", ""), "UTF8", null);
// prepare to save the canvas data
var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
persist.persistFlags = Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
persist.persistFlags |= Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
- // displays a download dialog (remove these 3 lines for silent download)
- var xfer = Components.classes["@mozilla.org/transfer;1"]
- .createInstance(Components.interfaces.nsITransfer);
- xfer.init(source, target, "", null, null, null, persist);
- persist.progressListener = xfer;
-
// save the canvas data to the file
- persist.saveURI(source, null, null, null, null, file);
+ persist.saveURI(source, null, null, null, null, aDestFile);
}
// function below is from http://developer.mozilla.org/en/docs/How_to_Quit_a_XUL_Application