From 740b86d148283d0cb4a66df542d081ab89278279 Mon Sep 17 00:00:00 2001 From: Robert Kaiser Date: Sun, 7 Sep 2008 02:47:38 +0200 Subject: [PATCH] actually make filepicker work for saving the image --- .../chrome/mandelbrot/content/mandelbrot.js | 43 +++++++++++-------- .../mandelbrot/locales/en-US/mandelbrot.dtd | 2 +- .../locales/en-US/mandelbrot.properties | 4 +- 3 files changed, 29 insertions(+), 20 deletions(-) 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 diff --git a/xulapp/chrome/mandelbrot/locales/en-US/mandelbrot.dtd b/xulapp/chrome/mandelbrot/locales/en-US/mandelbrot.dtd index 272bfd1..61cd360 100644 --- a/xulapp/chrome/mandelbrot/locales/en-US/mandelbrot.dtd +++ b/xulapp/chrome/mandelbrot/locales/en-US/mandelbrot.dtd @@ -39,7 +39,7 @@ - + diff --git a/xulapp/chrome/mandelbrot/locales/en-US/mandelbrot.properties b/xulapp/chrome/mandelbrot/locales/en-US/mandelbrot.properties index 2184734..2af8f05 100644 --- a/xulapp/chrome/mandelbrot/locales/en-US/mandelbrot.properties +++ b/xulapp/chrome/mandelbrot/locales/en-US/mandelbrot.properties @@ -36,5 +36,7 @@ # ***** END LICENSE BLOCK ***** statusEmpty=Click Draw Image for starting -statusDrawing=Drawing image... +statusDrawing=Drawing image… statusTime=Time: %S seconds +savePrompt=Save Image As… +pngFilterName=PNG Files -- 2.43.0