declare compat with Gecko 12 versions
[mandelbrot.git] / content / mandelbrot.js
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is KaiRo.at Mandelbrot, XULRunner version.
15  *
16  * The Initial Developer of the Original Code is
17  * Robert Kaiser <kairo@kairo.at>.
18  * Portions created by the Initial Developer are Copyright (C) 2008-2011
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  *   Robert Kaiser <kairo@kairo.at>
23  *   prefiks (patch for some speedups)
24  *   Boris Zbarsky <bzbarsky@mit.edu> (use imageData for canvas interaction)
25  *
26  * Alternatively, the contents of this file may be used under the terms of
27  * either the GNU General Public License Version 2 or later (the "GPL"), or
28  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29  * in which case the provisions of the GPL or the LGPL are applicable instead
30  * of those above. If you wish to allow use of your version of this file only
31  * under the terms of either the GPL or the LGPL, and not to allow others to
32  * use your version of this file under the terms of the MPL, indicate your
33  * decision by deleting the provisions above and replace them with the notice
34  * and other provisions required by the GPL or the LGPL. If you do not delete
35  * the provisions above, a recipient may use your version of this file under
36  * the terms of any one of the MPL, the GPL or the LGPL.
37  *
38  * ***** END LICENSE BLOCK ***** */
39
40 Components.utils.import("resource://gre/modules/Services.jsm");
41
42 var gColorPalette = [];
43 var gStartTime = 0;
44 var gMbrotBundle;
45 var gCurrentImageData;
46 var gLastImageData;
47
48 function Startup() {
49   gMbrotBundle = document.getElementById("mbrotBundle");
50   document.getElementById("statusLabel").value = gMbrotBundle.getString("statusEmpty");
51
52   let img = document.getElementById("mbrotImage");
53   img.addEventListener("mouseup", imgEvHandler, false);
54   img.addEventListener("mousedown", imgEvHandler, false);
55   img.addEventListener("mousemove", imgEvHandler, false);
56   img.addEventListener("touchstart", imgEvHandler, false);
57   img.addEventListener("touchend", imgEvHandler, false);
58   img.addEventListener("touchcancel", imgEvHandler, false);
59   img.addEventListener("touchleave", imgEvHandler, false);
60   img.addEventListener("touchmove", imgEvHandler, false);
61
62   Services.obs.notifyObservers(window, "mandelbrot-loaded", null);
63 }
64
65 function getAdjustPref(prefname) {
66   let value;
67   switch (prefname) {
68     case "image.width":
69     case "image.height":
70       value = 0;
71       try {
72         value = Services.prefs.getIntPref("mandelbrot." + prefname);
73       }
74       catch (e) { }
75       if ((value < 10) || (value > 5000)) {
76         value = 300;
77         Services.prefs.setIntPref("mandelbrot." + prefname, value);
78       }
79       return value;
80     case "last_image.Cr_*":
81       let Cr_min = -2.0;
82       let Cr_max = 1.0;
83       try {
84         Cr_min = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Cr_min"));
85         Cr_max = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Cr_max"));
86       }
87       catch (e) { }
88       if ((Cr_min < -3) || (Cr_min > 2) ||
89           (Cr_max < -3) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
90         Cr_min = -2.0; Cr_max = 1.0;
91       }
92       Services.prefs.setCharPref("mandelbrot.last_image.Cr_min", Cr_min);
93       Services.prefs.setCharPref("mandelbrot.last_image.Cr_max", Cr_max);
94       return {Cr_min: Cr_min, Cr_max: Cr_max};
95     case "last_image.Ci_*":
96       let Ci_min = -1.5;
97       let Ci_max = 1.5;
98       try {
99         Ci_min = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Ci_min"));
100         Ci_max = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Ci_max"));
101       }
102       catch (e) { }
103       if ((Ci_min < -2.5) || (Ci_min > 2.5) ||
104           (Ci_max < -2.5) || (Ci_max > 2.5) || (Ci_min >= Ci_max)) {
105         Ci_min = -1.5; Ci_max = 1.5;
106       }
107       Services.prefs.setCharPref("mandelbrot.last_image.Ci_min", Ci_min);
108       Services.prefs.setCharPref("mandelbrot.last_image.Ci_max", Ci_max);
109       return {Ci_min: Ci_min, Ci_max: Ci_max};
110     case "iteration_max":
111       value = 500;
112       try {
113         value = Services.prefs.getIntPref("mandelbrot." + prefname);
114       }
115       catch (e) {
116         setIter(value);
117       }
118       if (value < 10 || value > 10000) {
119         value = 500;
120         setIter(value);
121       }
122       return value;
123     case "use_algorithm":
124       value = "numeric";
125       try {
126         value = Services.prefs.getCharPref("mandelbrot." + prefname);
127       }
128       catch (e) {
129         setAlgorithm(value);
130       }
131       return value;
132    case "color_palette":
133       value = "kairo";
134       try {
135         value = Services.prefs.getCharPref("mandelbrot." + prefname);
136       }
137       catch(e) {
138         setPalette(value);
139       }
140       return value;
141    case "syncProportions":
142       value = true;
143       try {
144         value = Services.prefs.getBoolPref("mandelbrot." + prefname);
145       }
146       catch(e) {
147         Services.prefs.setBoolPref("mandelbrot." + prefname, value);
148       }
149       return value;
150     default:
151       return false;
152   }
153 }
154
155 function adjustCoordsAndDraw(aC_min, aC_max) {
156   let iWidth = getAdjustPref("image.width");
157   let iHeight = getAdjustPref("image.height");
158
159   // correct coordinates
160   if (aC_min.r < -2)
161     aC_min.r = -2;
162   if (aC_max.r > 2)
163     aC_max.r = 2;
164   if ((aC_min.r > 2) || (aC_max.r < -2) || (aC_min.r >= aC_max.r)) {
165     aC_min.r = -2.0; aC_max.r = 1.0;
166   }
167   if (aC_min.i < -2)
168     aC_min.i = -2;
169   if (aC_max.i > 2)
170     aC_max.i = 2;
171   if ((aC_min.i > 2) || (aC_max.i < -2) || (aC_min.i >= aC_max.i)) {
172     aC_min.i = -1.3; aC_max.i = 1.3;
173   }
174
175   let CWidth = aC_max.r - aC_min.r;
176   let CHeight = aC_max.i - aC_min.i;
177   let C_mid = new complex(aC_min.r + CWidth / 2, aC_min.i + CHeight / 2);
178
179   let CRatio = Math.max(CWidth / iWidth, CHeight / iHeight);
180
181   Services.prefs.setCharPref("mandelbrot.last_image.Cr_min", C_mid.r - iWidth * CRatio / 2);
182   Services.prefs.setCharPref("mandelbrot.last_image.Cr_max", C_mid.r + iWidth * CRatio / 2);
183   Services.prefs.setCharPref("mandelbrot.last_image.Ci_min", C_mid.i - iHeight * CRatio / 2);
184   Services.prefs.setCharPref("mandelbrot.last_image.Ci_max", C_mid.i + iHeight * CRatio / 2);
185
186   drawImage();
187 }
188
189 function drawImage() {
190   let canvas = document.getElementById("mbrotImage");
191   let context = canvas.getContext("2d");
192
193   document.getElementById("drawButton").hidden = true;
194
195   document.getElementById("statusLabel").value = gMbrotBundle.getString("statusDrawing");
196
197   if (gCurrentImageData) {
198     gLastImageData = gCurrentImageData;
199   }
200
201   let Cr_vals = getAdjustPref("last_image.Cr_*");
202   let Cr_min = Cr_vals.Cr_min;
203   let Cr_max = Cr_vals.Cr_max;
204
205   let Ci_vals = getAdjustPref("last_image.Ci_*");
206   let Ci_min = Ci_vals.Ci_min;
207   let Ci_max = Ci_vals.Ci_max;
208
209   let iterMax = getAdjustPref("iteration_max");
210   let algorithm = getAdjustPref("use_algorithm");
211
212   let currentPalette = getAdjustPref("color_palette");
213   if (!gColorPalette || !gColorPalette.length)
214     gColorPalette = getColorPalette(currentPalette);
215
216   let iWidth = getAdjustPref("image.width");
217   let iHeight = getAdjustPref("image.height");
218
219   gCurrentImageData = {
220     C_min: new complex(Cr_min, Ci_min),
221     C_max: new complex(Cr_max, Ci_max),
222     iWidth: iWidth,
223     iHeight: iHeight,
224     iterMax: iterMax
225   };
226
227   canvas.width = iWidth;
228   canvas.height = iHeight;
229
230   context.fillStyle = "rgba(255, 255, 255, 127)";
231   context.fillRect(0, 0, canvas.width, canvas.height);
232
233   gStartTime = new Date();
234
235   drawLine(0, [Cr_min, Cr_max, Ci_min, Ci_max],
236               canvas, context, iterMax, algorithm);
237 }
238
239 function drawLine(line, dimensions, canvas, context, iterMax, algorithm) {
240   let Cr_min = dimensions[0];
241   let Cr_max = dimensions[1];
242   let Cr_scale = Cr_max - Cr_min;
243
244   let Ci_min = dimensions[2];
245   let Ci_max = dimensions[3];
246   let Ci_scale = Ci_max - Ci_min;
247
248   let lines = Math.min(canvas.height - line, 8);
249   let imageData = context.createImageData(canvas.width, lines);
250   let pixels = imageData.data;
251   let idx = 0;
252   for (var img_y = line; img_y < canvas.height && img_y < line+8; img_y++)
253     for (let img_x = 0; img_x < canvas.width; img_x++) {
254       let C = new complex(Cr_min + (img_x / canvas.width) * Cr_scale,
255                           Ci_min + (img_y / canvas.height) * Ci_scale);
256       let colors = drawPoint(context, img_x, img_y, C, iterMax, algorithm);
257       pixels[idx++] = colors[0];
258       pixels[idx++] = colors[1];
259       pixels[idx++] = colors[2];
260       pixels[idx++] = colors[3];
261     }
262   context.putImageData(imageData, 0, line);
263
264   if (img_y < canvas.height)
265     setTimeout(drawLine, 0, img_y, dimensions, canvas, context, iterMax, algorithm);
266   else if (gStartTime)
267     EndCalc();
268 }
269
270 function EndCalc() {
271   let endTime = new Date();
272   let timeUsed = (endTime.getTime() - gStartTime.getTime()) / 1000;
273   document.getElementById("statusLabel").value =
274       gMbrotBundle.getFormattedString("statusTime", [timeUsed.toFixed(3)]);
275   gStartTime = 0;
276 }
277
278 function complex(aReal, aImag) {
279   this.r = aReal;
280   this.i = aImag;
281 }
282 complex.prototype = {
283   square: function() {
284     return new complex(this.r * this.r - this.i * this.i,
285                        2 * this.r * this.i);
286   },
287   dist: function() {
288     return Math.sqrt(this.r * this.r + this.i * this.i);
289   },
290   add: function(aComplex) {
291     return new complex(this.r + aComplex.r, this.i + aComplex.i);
292   }
293 }
294
295 function mandelbrotValueOO (aC, aIterMax) {
296   // this would be nice code in general but it looks like JS objects are too heavy for normal use.
297   let Z = new complex(0.0, 0.0);
298   for (var iter = 0; iter < aIterMax; iter++) {
299     Z = Z.square().add(aC);
300     if (Z.r * Z.r + Z.i * Z.i > 256) { break; }
301   }
302   return iter;
303 }
304
305 function mandelbrotValueNumeric (aC, aIterMax) {
306   // optimized numeric code for fast calculation
307   let Cr = aC.r, Ci = aC.i;
308   let Zr = 0.0, Zi = 0.0;
309   let Zr2 = Zr * Zr, Zi2 = Zi * Zi;
310   for (var iter = 0; iter < aIterMax; iter++) {
311     Zi = 2 * Zr * Zi + Ci;
312     Zr = Zr2 - Zi2 + Cr;
313
314     Zr2 = Zr * Zr; Zi2 = Zi * Zi;
315     if (Zr2 + Zi2 > 256) { break; }
316   }
317   return iter;
318 }
319
320 function getColor(aIterValue, aIterMax) {
321   let standardizedValue = Math.round(aIterValue * 1024 / aIterMax);
322   if (gColorPalette && gColorPalette.length)
323     return gColorPalette[standardizedValue];
324
325   // fallback to simple b/w if for some reason we don't have a palette
326   if (aIterValue == aIterMax)
327     return [0, 0, 0, 255];
328   else
329     return [255, 255, 255, 255];
330 }
331
332 function getColorPalette(palName) {
333   var palette = [];
334   switch (palName) {
335     case 'bw':
336       for (let i = 0; i < 1024; i++) {
337         palette[i] = [255, 255, 255, 255];
338       }
339       palette[1024] = [0, 0, 0, 255];
340       break;
341     case 'kairo':
342       // outer areas
343       for (let i = 0; i < 32; i++) {
344         let cc1 = Math.floor(i * 127 / 31);
345         let cc2 = 170 - Math.floor(i * 43 / 31);
346         palette[i] = [cc1, cc2, cc1, 255];
347       }
348       // inner areas
349       for (let i = 0; i < 51; i++) {
350         let cc = Math.floor(i * 170 / 50);
351         palette[32 + i] = [cc, 0, (170-cc), 255];
352       }
353       // corona
354       for (let i = 0; i < 101; i++) {
355         let cc = Math.floor(i * 200 / 100);
356         palette[83 + i] = [255, cc, 0, 255];
357       }
358       // inner corona
359       for (let i = 0; i < 201; i++) {
360         let cc1 = 255 - Math.floor(i * 85 / 200);
361         let cc2 = 200 - Math.floor(i * 30 / 200);
362         let cc3 = Math.floor(i * 170 / 200);
363         palette[184 + i] = [cc1, cc2, cc3, 255];
364       }
365       for (let i = 0; i < 301; i++) {
366         let cc1 = 170 - Math.floor(i * 43 / 300);
367         let cc2 = 170 + Math.floor(i * 85 / 300);
368         palette[385 + i] = [cc1, cc1, cc2, 255];
369       }
370       for (let i = 0; i < 338; i++) {
371         let cc = 127 + Math.floor(i * 128 / 337);
372         palette[686 + i] = [cc, cc, 255, 255];
373       }
374       palette[1024] = [0, 0, 0, 255];
375       break;
376     case 'rainbow-linear1':
377       for (let i = 0; i < 256; i++) {
378         palette[i] = [i, 0, 0, 255];
379         palette[256 + i] = [255, i, 0, 255];
380         palette[512 + i] = [255 - i, 255, i, 255];
381         palette[768 + i] = [i, 255-i, 255, 255];
382       }
383       palette[1024] = [0, 0, 0, 255];
384       break;
385     case 'rainbow-squared1':
386       for (let i = 0; i < 34; i++) {
387         let cc = Math.floor(i * 255 / 33);
388         palette[i] = [cc, 0, 0, 255];
389       }
390       for (let i = 0; i < 137; i++) {
391         let cc = Math.floor(i * 255 / 136);
392         palette[34 + i] = [255, cc, 0, 255];
393       }
394       for (let i = 0; i < 307; i++) {
395         let cc = Math.floor(i * 255 / 306);
396         palette[171 + i] = [255 - cc, 255, cc, 255];
397       }
398       for (let i = 0; i < 546; i++) {
399         let cc = Math.floor(i * 255 / 545);
400         palette[478 + i] = [cc, 255 - cc, 255, 255];
401       }
402       palette[1024] = [0, 0, 0, 255];
403       break;
404     case 'rainbow-linear2':
405       for (let i = 0; i < 205; i++) {
406         let cc = Math.floor(i * 255 / 204);
407         palette[i] = [255, cc, 0, 255];
408         palette[204 + i] = [255 - cc, 255, 0, 255];
409         palette[409 + i] = [0, 255, cc, 255];
410         palette[614 + i] = [0, 255 - cc, 255, 255];
411         palette[819 + i] = [cc, 0, 255, 255];
412       }
413       palette[1024] = [0, 0, 0, 255];
414       break;
415     case 'rainbow-squared2':
416       for (let i = 0; i < 19; i++) {
417         let cc = Math.floor(i * 255 / 18);
418         palette[i] = [255, cc, 0, 255];
419       }
420       for (let i = 0; i < 74; i++) {
421         let cc = Math.floor(i * 255 / 73);
422         palette[19 + i] = [255 - cc, 255, 0, 255];
423       }
424       for (let i = 0; i < 168; i++) {
425         let cc = Math.floor(i * 255 / 167);
426         palette[93 + i] = [0, 255, cc, 255];
427       }
428       for (let i = 0; i < 298; i++) {
429         let cc = Math.floor(i * 255 / 297);
430         palette[261 + i] = [0, 255 - cc, 255, 255];
431       }
432       for (let i = 0; i < 465; i++) {
433         let cc = Math.floor(i * 255 / 464);
434         palette[559 + i] = [cc, 0, 255, 255];
435       }
436       palette[1024] = [0, 0, 0, 255];
437       break;
438   }
439   /*
440      'Standard-Palette (QB-Colors)
441      For i = 0 To 1024
442          xx = CInt(i * 500 / 1024 + 2)
443          If xx <= 15 Then clr = xx
444          If xx > 15 Then clr = CInt(Sqr((xx - 15 + 1) * 15 ^ 2 / 485))
445          If xx >= 500 Then clr = 0
446          palette(i) = QBColor(clr)
447      Next
448   */
449   return palette;
450 }
451
452 function drawPoint(context, img_x, img_y, C, iterMax, algorithm) {
453   var itVal;
454   switch (algorithm) {
455     case 'oo':
456       itVal = mandelbrotValueOO(C, iterMax);
457       break;
458     case 'numeric':
459     default:
460       itVal = mandelbrotValueNumeric(C, iterMax);
461       break;
462   }
463   return getColor(itVal, iterMax);
464 }
465
466 /***** pure UI functions *****/
467
468 var zoomstart;
469 var imgBackup;
470 var zoomTouchID;
471
472 let imgEvHandler = {
473   handleEvent: function(aEvent) {
474     let canvas = document.getElementById("mbrotImage");
475     let context = canvas.getContext("2d");
476     let touchEvent = aEvent.type.indexOf('touch') != -1;
477
478     // Bail out if this is neither a touch nor left-click.
479     if (!touchEvent && aEvent.button != 0)
480       return;
481
482     // Bail out if the started touch can't be found.
483     if (touchEvent && zoomstart &&
484         !aEvent.changedTouches.identifiedTouch(zoomTouchID))
485       return;
486
487     let coordObj = touchEvent ?
488                    aEvent.changedTouches.identifiedTouch(zoomTouchID) :
489                    aEvent;
490
491     switch (aEvent.type) {
492       case 'mousedown':
493       case 'touchstart':
494         if (touchEvent) {
495           zoomTouchID = aEvent.changedTouches.item(0).identifier;
496           coordObj = aEvent.changedTouches.identifiedTouch(zoomTouchID);
497         }
498         // left button - start dragzoom
499         zoomstart = {x: coordObj.clientX - canvas.offsetLeft,
500                      y: coordObj.clientY - canvas.offsetTop};
501         imgBackup = context.getImageData(0, 0, canvas.width, canvas.height);
502         break;
503       case 'mouseup':
504       case 'touchend':
505         if (zoomstart) {
506           context.putImageData(imgBackup, 0, 0);
507           let zoomend = {x: coordObj.clientX - canvas.offsetLeft,
508                          y: coordObj.clientY - canvas.offsetTop};
509
510           // make sure zoomend is bigger than zoomstart
511           if ((zoomend.x == zoomstart.x) || (zoomend.y == zoomstart.y)) {
512             // cannot zoom what has no area, discard it
513             zoomstart = undefined;
514             return;
515           }
516           if (zoomend.x < zoomstart.x)
517             [zoomend.x, zoomstart.x] = [zoomstart.x, zoomend.x];
518           if (zoomend.y < zoomstart.y)
519             [zoomend.y, zoomstart.y] = [zoomstart.y, zoomend.y];
520
521           // determine new "coordinates"
522           let CWidth = gCurrentImageData.C_max.r - gCurrentImageData.C_min.r;
523           let CHeight = gCurrentImageData.C_max.i - gCurrentImageData.C_min.i;
524           let newC_min = new complex(
525               gCurrentImageData.C_min.r + zoomstart.x / gCurrentImageData.iWidth * CWidth,
526               gCurrentImageData.C_min.i + zoomstart.y / gCurrentImageData.iHeight * CHeight);
527           let newC_max = new complex(
528               gCurrentImageData.C_min.r + zoomend.x / gCurrentImageData.iWidth * CWidth,
529               gCurrentImageData.C_min.i + zoomend.y / gCurrentImageData.iHeight * CHeight);
530
531           adjustCoordsAndDraw(newC_min, newC_max);
532         }
533         zoomstart = undefined;
534         break;
535       case 'mousemove':
536       case 'touchmove':
537         if (zoomstart) {
538           context.putImageData(imgBackup, 0, 0);
539           context.strokeStyle = "rgb(255,255,31)";
540           context.strokeRect(zoomstart.x, zoomstart.y,
541                              coordObj.clientX - canvas.offsetLeft - zoomstart.x,
542                              coordObj.clientY - canvas.offsetTop - zoomstart.y);
543         }
544       break;
545     }
546   }
547 };
548
549 function saveImage() {
550   const nsIFilePicker = Components.interfaces.nsIFilePicker;
551   let fp = null;
552   try {
553     fp = Components.classes["@mozilla.org/filepicker;1"]
554                    .createInstance(nsIFilePicker);
555   } catch (e) {}
556   if (!fp) return;
557   let promptString = gMbrotBundle.getString("savePrompt");
558   fp.init(window, promptString, nsIFilePicker.modeSave);
559   fp.appendFilter(gMbrotBundle.getString("pngFilterName"), "*.png");
560   fp.defaultString = "mandelbrot.png";
561
562   let fpResult = fp.show();
563   if (fpResult != nsIFilePicker.returnCancel) {
564     saveCanvas(document.getElementById("mbrotImage"), fp.file);
565   }
566 }
567
568 function updateBookmarkMenu(aParent) {
569   document.getElementById("bookmarkSave").disabled =
570       (!document.getElementById("drawButton").hidden || (gStartTime > 0));
571
572   document.getElementById("bookmarkBack").disabled = !gLastImageData;
573
574   while (aParent.hasChildNodes() &&
575          aParent.lastChild.id != "bookmarkSeparator")
576     aParent.removeChild(aParent.lastChild);
577
578   let file = Components.classes["@mozilla.org/file/directory_service;1"]
579                        .getService(Components.interfaces.nsIProperties)
580                        .get("ProfD", Components.interfaces.nsIFile);
581   file.append("mandelbookmarks.sqlite");
582   if (file.exists()) {
583     let connection = Components.classes["@mozilla.org/storage/service;1"]
584                                .getService(Components.interfaces.mozIStorageService)
585                                .openDatabase(file);
586     try {
587       if (connection.tableExists("bookmarks")) {
588         let statement = connection.createStatement(
589             "SELECT name,ROWID FROM bookmarks ORDER BY ROWID ASC");
590         while (statement.executeStep()) {
591           let newItem = aParent.appendChild(document.createElement("menuitem"));
592           newItem.setAttribute("label", statement.getString(0));
593           newItem.setAttribute("bmRowID", statement.getString(1));
594         }
595         statement.reset();
596         statement.finalize();
597         return;
598       }
599     } finally {
600       connection.close();
601     }
602   }
603   // Create the "Nothing Available" Menu item and disable it.
604   let na = aParent.appendChild(document.createElement("menuitem"));
605   na.setAttribute("label", gMbrotBundle.getString("noBookmarks"));
606   na.setAttribute("disabled", "true");
607 }
608
609 function goBack() {
610   if (gLastImageData) {
611     Services.prefs.setIntPref("mandelbrot.iteration_max",
612                               gLastImageData.iterMax);
613     // use gLastImageData.iWidth, gLastImageData.iHeight ???
614     adjustCoordsAndDraw(gLastImageData.C_min, gLastImageData.C_max);
615     gLastImageData = undefined;
616   }
617 }
618
619 function callBookmark(evtarget) {
620   if (evtarget.id == "bookmarkSave" || evtarget.id == "bookmarkSeparator")
621     return;
622   if (evtarget.id == "bookmarkOverview") {
623     adjustCoordsAndDraw(new complex(0,0), new complex(0,0));
624     return;
625   }
626
627   if (evtarget.getAttribute('bmRowID')) {
628     let iterMax = 0;
629     let C_min = null;
630     let C_max = null;
631
632     let file = Components.classes["@mozilla.org/file/directory_service;1"]
633                          .getService(Components.interfaces.nsIProperties)
634                          .get("ProfD", Components.interfaces.nsIFile);
635     file.append("mandelbookmarks.sqlite");
636     let connection = Components.classes["@mozilla.org/storage/service;1"]
637                                .getService(Components.interfaces.mozIStorageService)
638                                .openDatabase(file);
639     let statement = connection.createStatement(
640         "SELECT iteration_max,Cr_min,Cr_max,Ci_min,Ci_max FROM bookmarks WHERE ROWID=?1");
641     statement.bindStringParameter(0, evtarget.getAttribute('bmRowID'));
642     while (statement.executeStep()) {
643       iterMax = statement.getInt32(0);
644       C_min = new complex(statement.getDouble(1), statement.getDouble(3));
645       C_max = new complex(statement.getDouble(2), statement.getDouble(4));
646     }
647     statement.finalize();
648     connection.close();
649
650     if (iterMax && C_min && C_max) {
651       Services.prefs.setIntPref("mandelbrot.iteration_max", iterMax);
652       adjustCoordsAndDraw(C_min, C_max);
653     }
654   }
655 }
656
657 function saveBookmark() {
658   // retrieve wanted bookmark name with a prompt
659   let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
660                           .getService(Components.interfaces.nsIPromptService);
661   let input = {value: ""}; // empty default value
662   let ok = prompts.prompt(null, gMbrotBundle.getString("saveBookmarkTitle"), gMbrotBundle.getString("saveBookmarkLabel"), input, null, {});
663   // ok is true if OK is pressed, false if Cancel. input.value holds the value of the edit field if "OK" was pressed.
664   if (!ok || !input.value)
665     return
666
667   let bmName = input.value;
668
669   // Open or create the bookmarks database.
670   let file = Components.classes["@mozilla.org/file/directory_service;1"]
671                        .getService(Components.interfaces.nsIProperties)
672                        .get("ProfD", Components.interfaces.nsIFile);
673   file.append("mandelbookmarks.sqlite");
674   let connection = Components.classes["@mozilla.org/storage/service;1"]
675                              .getService(Components.interfaces.mozIStorageService)
676                              .openDatabase(file);
677   connection.beginTransaction();
678   if (!connection.tableExists("bookmarks"))
679     connection.createTable("bookmarks", "name TEXT, iteration_max INTEGER, Cr_min REAL, Cr_max REAL, Ci_min REAL, Ci_max REAL");
680   // NULL. The value is a NULL value.
681   // INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
682   // REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
683   // TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16-LE).
684
685   // Put value of the current image into the bookmarks table
686   let statement = connection.createStatement(
687       "INSERT INTO bookmarks (name,iteration_max,Cr_min,Cr_max,Ci_min,Ci_max) VALUES (?1,?2,?3,?4,?5,?6)");
688   statement.bindStringParameter(0, bmName);
689   statement.bindStringParameter(1, gCurrentImageData.iterMax);
690   statement.bindStringParameter(2, gCurrentImageData.C_min.r);
691   statement.bindStringParameter(3, gCurrentImageData.C_max.r);
692   statement.bindStringParameter(4, gCurrentImageData.C_min.i);
693   statement.bindStringParameter(5, gCurrentImageData.C_max.i);
694   statement.execute();
695   statement.finalize();
696   connection.commitTransaction();
697   connection.close();
698 }
699
700 function imgSettings() {
701   let anchor = null;
702   let position = "before_start";
703   if (document.getElementById("mandelbrot-page").nodeName == "page") {
704     anchor = document.getElementById("mandelbrotToolbar");
705   }
706   else {
707     anchor = document.getElementById("mandelbrotMenubar");
708     position = "after_start";
709   }
710   document.getElementById("imgSettingsPanel").showPopup(anchor, position);
711 }
712
713 function initImgSettings() {
714   // Get values from prefs.
715   for each (let coord in ["Cr", "Ci"]) {
716     let coord_vals = getAdjustPref("last_image." + coord + "_*");
717     document.getElementById("is_" + coord + "_min").value = coord_vals[coord + "_min"];
718     document.getElementById("is_" + coord + "_max").value = coord_vals[coord + "_max"];
719   }
720   for each (let dim in ["width", "height"]) {
721     document.getElementById("is_img_" + dim).value = getAdjustPref("image." + dim);
722   }
723   document.getElementById("is_syncProp").checked = getAdjustPref("syncProportions");
724
725   // Calculate scales.
726   recalcCoord("Cr", "scale");
727   recalcCoord("Ci", "scale");
728
729   // Clear the preview.
730   let canvas = document.getElementById("is_mbrotPreview");
731   let context = canvas.getContext("2d");
732   context.fillStyle = "rgba(255, 255, 255, 127)";
733   context.fillRect(0, 0, canvas.width, canvas.height);
734
735   // Set lists to correct values.
736   updateIterList();
737   updatePaletteList();
738   updateAlgoList();
739 }
740
741 function closeImgSettings() {
742   // Hide popup, which will automatically make a call to save values.
743   document.getElementById("imgSettingsPanel").hidePopup();
744 }
745
746 function saveImgSettings() {
747   // Get values to prefs.
748   for each (let coord in ["Cr_min", "Cr_max", "Ci_min", "Ci_max"]) {
749     Services.prefs.setCharPref("mandelbrot.last_image." + coord,
750                                document.getElementById("is_" + coord).value);
751   }
752   for each (let dim in ["width", "height"]) {
753     Services.prefs.setIntPref("mandelbrot.image." + dim,
754                               document.getElementById("is_img_" + dim).value);
755   }
756   Services.prefs.setBoolPref("mandelbrot.syncProportions",
757                              document.getElementById("is_syncProp").checked);
758 }
759
760 function checkISValue(textbox, type) {
761   if (type == "coord") {
762     textbox.value = roundCoord(parseFloat(textbox.value));
763   }
764   else if (type == "dim") {
765     textbox.value = parseInt(textbox.value);
766   }
767 }
768
769 function drawPreview() {
770   let canvas = document.getElementById("is_mbrotPreview");
771   let context = canvas.getContext("2d");
772
773   if (document.getElementById("is_img_width").value /
774       document.getElementById("is_img_height").value
775         < 80 / 50) {
776     canvas.height = 50;
777     canvas.width = canvas.height *
778       document.getElementById("is_img_width").value /
779       document.getElementById("is_img_height").value;
780   }
781   else {
782     canvas.width = 80;
783     canvas.height = canvas.width *
784       document.getElementById("is_imgHeight").value /
785       document.getElementById("is_imgWidth").value;
786   }
787
788   let Cr_min = parseFloat(document.getElementById("is_Cr_min").value);
789   let Cr_max = parseFloat(document.getElementById("is_Cr_max").value);
790   if ((Cr_min < -2) || (Cr_min > 2) ||
791       (Cr_max < -2) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
792     Cr_min = -2.0; Cr_max = 1.0;
793   }
794
795   let Ci_min = parseFloat(document.getElementById("is_Ci_min").value);
796   let Ci_max = parseFloat(document.getElementById("is_Ci_max").value);
797   if ((Ci_min < -2) || (Ci_min > 2) ||
798       (Ci_max < -2) || (Ci_max > 2) || (Ci_min >= Ci_max)) {
799     Ci_min = -2.0; Ci_max = 1.0;
800   }
801
802   let iterMax = getAdjustPref("iteration_max");
803   let algorithm = getAdjustPref("use_algorithm");
804
805   context.fillStyle = "rgba(255, 255, 255, 127)";
806   context.fillRect(0, 0, canvas.width, canvas.height);
807
808   let currentPalette = getAdjustPref("color_palette");
809   gColorPalette = getColorPalette(currentPalette);
810
811   drawLine(0, [Cr_min, Cr_max, Ci_min, Ci_max],
812               canvas, context, iterMax, algorithm);
813 }
814
815 function recalcCoord(coord, target) {
816   let othercoord = (coord == "Ci") ? "Cr" : "Ci";
817   let owndim = (coord == "Ci") ? "height" : "width";
818   let otherdim = (coord == "Ci") ? "width" : "height";
819   let myscale;
820   if (target == "scale") {
821     myscale =
822       parseFloat(document.getElementById("is_" + coord + "_max").value) -
823       parseFloat(document.getElementById("is_" + coord + "_min").value);
824     document.getElementById("is_" + coord + "_scale").value = roundCoord(myscale);
825   }
826   else if (target == 'max') {
827     let mymax =
828       parseFloat(document.getElementById("is_" + coord + "_min").value) +
829       parseFloat(document.getElementById("is_" + coord + "_scale").value);
830     document.getElementById("is_" + coord + "_max").value = roundCoord(mymax);
831     myscale = document.getElementById("is_" + coord + "_scale").value;
832   }
833   if (document.getElementById("is_syncProp").checked) {
834     let otherscale = myscale *
835       document.getElementById("is_img_" + otherdim).value /
836       document.getElementById("is_img_" + owndim).value;
837     document.getElementById("is_" + othercoord + "_scale").value = roundCoord(otherscale);
838     let othermax =
839       parseFloat(document.getElementById("is_" + othercoord + "_min").value) +
840       parseFloat(document.getElementById("is_" + othercoord + "_scale").value);
841     document.getElementById("is_" + othercoord + "_max").value = roundCoord(othermax);
842   }
843 }
844
845 function checkProportions() {
846   if (!document.getElementById("is_syncProp").checked) {
847     recalcCoord("Cr", "scale");
848   }
849 }
850
851 function roundCoord(floatval) {
852   // We should round to 10 decimals here or so
853   return parseFloat(floatval.toFixed(10));
854 }
855
856 function updateIterList() {
857   let currentIter = getAdjustPref("iteration_max");
858   document.getElementById("iterList").value = currentIter;
859 }
860
861 function updatePaletteList() {
862   let currentPalette = getAdjustPref("color_palette");
863   if (!gColorPalette || !gColorPalette.length)
864     gColorPalette = getColorPalette(currentPalette);
865   document.getElementById("colorList").value = currentPalette;
866 }
867
868 function updateAlgoList() {
869   let currentAlgo = getAdjustPref("use_algorithm");
870   document.getElementById("algoList").value = currentAlgo;
871 }
872
873 function setIter(aIter) {
874   Services.prefs.setIntPref("mandelbrot.iteration_max", aIter);
875 }
876
877 function setPalette(aPaletteID) {
878   Services.prefs.setCharPref("mandelbrot.color_palette", aPaletteID);
879   gColorPalette = getColorPalette(aPaletteID);
880 }
881
882 function setAlgorithm(algoID) {
883   Services.prefs.setCharPref("mandelbrot.use_algorithm", algoID);
884 }
885
886
887 /***** helper functions from external sources *****/
888
889 // function below is based on http://developer.mozilla.org/en/docs/Code_snippets:Canvas
890 // custom modifications:
891 //   - use "a"-prefix on function arguments
892 //   - take an nsILocalFile as aDestFile argument
893 //   - always do silent download
894 function saveCanvas(aCanvas, aDestFile) {
895   // create a data url from the canvas and then create URIs of the source and targets  
896   var io = Components.classes["@mozilla.org/network/io-service;1"]
897                      .getService(Components.interfaces.nsIIOService);
898   var source = io.newURI(aCanvas.toDataURL("image/png", ""), "UTF8", null);
899
900   // prepare to save the canvas data
901   var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
902                           .createInstance(Components.interfaces.nsIWebBrowserPersist);
903
904   persist.persistFlags = Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
905   persist.persistFlags |= Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
906
907   // save the canvas data to the file
908   persist.saveURI(source, null, null, null, null, aDestFile);
909 }