actually make filepicker work for saving the image
authorRobert Kaiser <kairo@kairo.at>
Sun, 7 Sep 2008 00:47:38 +0000 (02:47 +0200)
committerRobert Kaiser <kairo@kairo.at>
Sun, 7 Sep 2008 00:47:38 +0000 (02:47 +0200)
xulapp/chrome/mandelbrot/content/mandelbrot.js
xulapp/chrome/mandelbrot/locales/en-US/mandelbrot.dtd
xulapp/chrome/mandelbrot/locales/en-US/mandelbrot.properties

index aab0f02b884861fc98ea39afac4be8a042c97c73..e3c36ae00e8278d53e33b4645f6f7c1b45ce176c 100644 (file)
@@ -283,8 +283,23 @@ function drawPoint(context, img_x, img_y, C, iterMax, algorithm) {
 /***** pure UI functions *****/
 
 function saveImage() {
 /***** 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() {
 }
 
 function updateIterMenu() {
@@ -391,18 +406,16 @@ function setAlgorithm(algoID) {
 
 /***** helper functions from external sources *****/
 
 
 /***** 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);
   // 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"]
 
   // 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;
 
   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
   // 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
 }
 
 // function below is from http://developer.mozilla.org/en/docs/How_to_Quit_a_XUL_Application
index 272bfd11890428f9d026955a9e88a4691c98792e..61cd3601e7d7b79b52b0a753ca702fa5f92ae71c 100644 (file)
@@ -39,7 +39,7 @@
 
 <!ENTITY fileMenu.label   "File">
 <!ENTITY fileDraw.label   "Draw Image">
 
 <!ENTITY fileMenu.label   "File">
 <!ENTITY fileDraw.label   "Draw Image">
-<!ENTITY fileSave.label   "Save Image">
+<!ENTITY fileSave.label   "Save Image">
 <!ENTITY fileQuit.label   "Quit">
 
 <!ENTITY prefMenu.label   "Settings">
 <!ENTITY fileQuit.label   "Quit">
 
 <!ENTITY prefMenu.label   "Settings">
index 218473443caff44f67c1f4399d9c6f9972529ad3..2af8f0521a47747fe11223770e56011490f09579 100644 (file)
@@ -36,5 +36,7 @@
 # ***** END LICENSE BLOCK *****
 
 statusEmpty=Click Draw Image for starting
 # ***** END LICENSE BLOCK *****
 
 statusEmpty=Click Draw Image for starting
-statusDrawing=Drawing image...
+statusDrawing=Drawing image
 statusTime=Time: %S seconds
 statusTime=Time: %S seconds
+savePrompt=Save Image As…
+pngFilterName=PNG Files