X-Git-Url: https://git-public.kairo.at/?p=mandelbrot.git;a=blobdiff_plain;f=xulapp%2Fchrome%2Fmandelbrot%2Fcontent%2Fmandelbrot.js;h=e3c36ae00e8278d53e33b4645f6f7c1b45ce176c;hp=aab0f02b884861fc98ea39afac4be8a042c97c73;hb=740b86d148283d0cb4a66df542d081ab89278279;hpb=2cb9a6b558e687bc39584fb763a9455b8db00204 diff --git a/xulapp/chrome/mandelbrot/content/mandelbrot.js b/xulapp/chrome/mandelbrot/content/mandelbrot.js index aab0f02..e3c36ae 100644 --- a/xulapp/chrome/mandelbrot/content/mandelbrot.js +++ b/xulapp/chrome/mandelbrot/content/mandelbrot.js @@ -283,8 +283,23 @@ function drawPoint(context, img_x, img_y, C, iterMax, algorithm) { /***** 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() { @@ -391,18 +406,16 @@ function setAlgorithm(algoID) { /***** 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"] @@ -411,14 +424,8 @@ function saveCanvas(canvas, destFile) { 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