add item to new Firefox button menu, make Firefox and SeaMonkey open Mandelbrot in...
[mandelbrot.git] / xulapp / chrome / mandelbrot / content / mandelbrot.js
index 0a4a31aeeb64a771a9ea20466ccb6ffe07f481a9..c2bfba29e800c03a1ef0c7b9d40df704d944850f 100644 (file)
@@ -53,25 +53,99 @@ function Startup() {
   document.getElementById("statusLabel").value = gMbrotBundle.getString("statusEmpty");
 }
 
-function adjustCoordsAndDraw(aC_min, aC_max) {
-  let iWidth = 0;
-  try {
-    iWidth = gPref.getIntPref("mandelbrot.image.width");
-  }
-  catch (e) { }
-  if ((iWidth < 10) || (iWidth > 5000)) {
-    iWidth = 300;
-    gPref.setIntPref("mandelbrot.image.width", iWidth);
-  }
-  let iHeight = 0;
-  try {
-    iHeight = gPref.getIntPref("mandelbrot.image.height");
-  }
-  catch (e) { }
-  if ((iHeight < 10) || (iHeight > 5000)) {
-    iHeight = 300;
-    gPref.setIntPref("mandelbrot.image.height", iHeight);
+function getAdjustPref(prefname) {
+  let value;
+  switch (prefname) {
+    case "image.width":
+    case "image.height":
+      value = 0;
+      try {
+        value = gPref.getIntPref("mandelbrot." + prefname);
+      }
+      catch (e) { }
+      if ((value < 10) || (value > 5000)) {
+        value = 300;
+        gPref.setIntPref("mandelbrot." + prefname, value);
+      }
+      return value;
+    case "last_image.Cr_*":
+      let Cr_min = -2.0;
+      let Cr_max = 1.0;
+      try {
+        Cr_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_min"));
+        Cr_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_max"));
+      }
+      catch (e) { }
+      if ((Cr_min < -3) || (Cr_min > 2) ||
+          (Cr_max < -3) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
+        Cr_min = -2.0; Cr_max = 1.0;
+      }
+      gPref.setCharPref("mandelbrot.last_image.Cr_min", Cr_min);
+      gPref.setCharPref("mandelbrot.last_image.Cr_max", Cr_max);
+      return {Cr_min: Cr_min, Cr_max: Cr_max};
+    case "last_image.Ci_*":
+      let Ci_min = -1.5;
+      let Ci_max = 1.5;
+      try {
+        Ci_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_min"));
+        Ci_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_max"));
+      }
+      catch (e) { }
+      if ((Ci_min < -2.5) || (Ci_min > 2.5) ||
+          (Ci_max < -2.5) || (Ci_max > 2.5) || (Ci_min >= Ci_max)) {
+        Ci_min = -1.5; Ci_max = 1.5;
+      }
+      gPref.setCharPref("mandelbrot.last_image.Ci_min", Ci_min);
+      gPref.setCharPref("mandelbrot.last_image.Ci_max", Ci_max);
+      return {Ci_min: Ci_min, Ci_max: Ci_max};
+    case "iteration_max":
+      value = 500;
+      try {
+        value = gPref.getIntPref("mandelbrot." + prefname);
+      }
+      catch (e) {
+        setIter(value);
+      }
+      if (value < 10 || value > 10000) {
+        value = 500;
+        setIter(value);
+      }
+      return value;
+    case "use_algorithm":
+      value = "numeric";
+      try {
+        value = gPref.getCharPref("mandelbrot." + prefname);
+      }
+      catch (e) {
+        setAlgorithm(value);
+      }
+      return value;
+   case "color_palette":
+      value = "kairo";
+      try {
+        value = gPref.getCharPref("mandelbrot." + prefname);
+      }
+      catch(e) {
+        setPalette(value);
+      }
+      return value;
+   case "syncProportions":
+      value = true;
+      try {
+        value = gPref.getBoolPref("mandelbrot." + prefname);
+      }
+      catch(e) {
+        gPref.setBoolPref("mandelbrot." + prefname, value);
+      }
+      return value;
+    default:
+      return false;
   }
+}
+
+function adjustCoordsAndDraw(aC_min, aC_max) {
+  let iWidth = getAdjustPref("image.width");
+  let iHeight = getAdjustPref("image.height");
 
   // correct coordinates
   if (aC_min.r < -2)
@@ -111,55 +185,19 @@ function drawImage() {
 
   document.getElementById("statusLabel").value = gMbrotBundle.getString("statusDrawing");
 
-  let Cr_min = -2.0;
-  let Cr_max = 1.0;
-  try {
-    Cr_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_min"));
-    Cr_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_max"));
-  }
-  catch (e) { }
-  if ((Cr_min < -3) || (Cr_min > 2) ||
-      (Cr_max < -3) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
-    Cr_min = -2.0; Cr_max = 1.0;
-  }
-  gPref.setCharPref("mandelbrot.last_image.Cr_min", Cr_min);
-  gPref.setCharPref("mandelbrot.last_image.Cr_max", Cr_max);
+  let Cr_vals = getAdjustPref("last_image.Cr_*");
+  let Cr_min = Cr_vals.Cr_min;
+  let Cr_max = Cr_vals.Cr_max;
 
-  let Ci_min = -1.5;
-  let Ci_max = 1.5;
-  try {
-    Ci_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_min"));
-    Ci_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_max"));
-  }
-  catch (e) { }
-  if ((Ci_min < -2.5) || (Ci_min > 2.5) ||
-      (Ci_max < -2.5) || (Ci_max > 2.5) || (Ci_min >= Ci_max)) {
-    Ci_min = -1.5; Ci_max = 1.5;
-  }
-  gPref.setCharPref("mandelbrot.last_image.Ci_min", Ci_min);
-  gPref.setCharPref("mandelbrot.last_image.Ci_max", Ci_max);
+  let Ci_vals = getAdjustPref("last_image.Ci_*");
+  let Ci_min = Ci_vals.Ci_min;
+  let Ci_max = Ci_vals.Ci_max;
 
-  let iterMax = gPref.getIntPref("mandelbrot.iteration_max");
-  let algorithm = gPref.getCharPref("mandelbrot.use_algorithm");
+  let iterMax = getAdjustPref("iteration_max");
+  let algorithm = getAdjustPref("use_algorithm");
 
-  let iWidth = 0;
-  try {
-    iWidth = gPref.getIntPref("mandelbrot.image.width");
-  }
-  catch (e) { }
-  if ((iWidth < 10) || (iWidth > 5000)) {
-    iWidth = 300;
-    gPref.setIntPref("mandelbrot.image.width", iWidth);
-  }
-  let iHeight = 0;
-  try {
-    iHeight = gPref.getIntPref("mandelbrot.image.height");
-  }
-  catch (e) { }
-  if ((iHeight < 10) || (iHeight > 5000)) {
-    iHeight = 300;
-    gPref.setIntPref("mandelbrot.image.height", iHeight);
-  }
+  let iWidth = getAdjustPref("image.width");
+  let iHeight = getAdjustPref("image.height");
 
   gCurrentImageData = {
     C_min: new complex(Cr_min, Ci_min),
@@ -617,15 +655,7 @@ function saveBookmark() {
 }
 
 function updateIterMenu() {
-  let currentIter = 0;
-  try {
-    currentIter = gPref.getIntPref("mandelbrot.iteration_max");
-  }
-  catch(e) { }
-  if (currentIter < 10) {
-    currentIter = 500;
-    setIter(currentIter);
-  }
+  let currentIter = getAdjustPref("iteration_max");
 
   let popup = document.getElementById("menu_iterPopup");
   let item = popup.firstChild;
@@ -645,15 +675,7 @@ function setIter(aIter) {
 }
 
 function updatePaletteMenu() {
-  let currentPalette = '';
-  try {
-    currentPalette = gPref.getCharPref("mandelbrot.color_palette");
-  }
-  catch(e) { }
-  if (!currentPalette.length) {
-    currentPalette = 'kairo';
-    setPalette(currentPalette);
-  }
+  let currentPalette = getAdjustPref("color_palette");
   if (!gColorPalette || !gColorPalette.length)
     gColorPalette = getColorPalette(currentPalette);
 
@@ -705,15 +727,7 @@ function toggleJITState(jitMenuItem, jittype) {
 }
 
 function updateAlgoMenu() {
-  let currentAlgo = '';
-  try {
-    currentAlgo = gPref.getCharPref("mandelbrot.use_algorithm");
-  }
-  catch(e) { }
-  if (!currentAlgo.length) {
-    currentAlgo = 'numeric';
-    setAlgorithm(currentAlgo);
-  }
+  let currentAlgo = getAdjustPref("use_algorithm");
 
   let popup = document.getElementById("menu_algoPopup");
   let item = popup.firstChild;
@@ -757,16 +771,15 @@ function errorConsole() {
 
 function initImgSettings() {
   // Get values from prefs.
-  for each (let coord in ["Cr_min", "Cr_max", "Ci_min", "Ci_max"]) {
-    document.getElementById("is_" + coord).value =
-        roundCoord(parseFloat(gPref.getCharPref("mandelbrot.last_image." + coord)));
+  for each (let coord in ["Cr", "Ci"]) {
+    let coord_vals = getAdjustPref("last_image." + coord + "_*");
+    document.getElementById("is_" + coord + "_min").value = coord_vals[coord + "_min"];
+    document.getElementById("is_" + coord + "_max").value = coord_vals[coord + "_max"];
   }
   for each (let dim in ["width", "height"]) {
-    document.getElementById("is_img_" + dim).value =
-        gPref.getIntPref("mandelbrot.image." + dim);
+    document.getElementById("is_img_" + dim).value = getAdjustPref("image." + dim);
   }
-  document.getElementById("is_syncProp").checked =
-        gPref.getBoolPref("mandelbrot.syncProportions");
+  document.getElementById("is_syncProp").checked = getAdjustPref("syncProportions");
 
   // Calculate scales.
   recalcCoord("Cr", "scale");
@@ -840,21 +853,13 @@ function drawPreview() {
     Ci_min = -2.0; Ci_max = 1.0;
   }
 
-  let iterMax = gPref.getIntPref("mandelbrot.iteration_max");
-  let algorithm = gPref.getCharPref("mandelbrot.use_algorithm");
+  let iterMax = getAdjustPref("iteration_max");
+  let algorithm = getAdjustPref("use_algorithm");
 
   context.fillStyle = "rgba(255, 255, 255, 127)";
   context.fillRect(0, 0, canvas.width, canvas.height);
 
-  try {
-    var currentPalette = gPref.getCharPref("mandelbrot.color_palette");
-  }
-  catch(e) {
-    var currentPalette = "";
-  }
-  if (!currentPalette.length) {
-    currentPalette = "kairo";
-  }
+  let currentPalette = getAdjustPref("color_palette");
   gColorPalette = getColorPalette(currentPalette);
 
   drawLine(0, [Cr_min, Cr_max, Ci_min, Ci_max],