1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14 * The Original Code is KaiRo.at Mandelbrot, XULRunner version.
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.
22 * Robert Kaiser <kairo@kairo.at>
23 * prefiks (patch for some speedups)
24 * Boris Zbarsky <bzbarsky@mit.edu> (use imageData for canvas interaction)
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.
38 * ***** END LICENSE BLOCK ***** */
40 var gColorPalette = [];
41 var gPref = Components.classes["@mozilla.org/preferences-service;1"]
42 .getService(Components.interfaces.nsIPrefService)
46 var gCurrentImageData;
52 gMbrotBundle = document.getElementById("mbrotBundle");
53 document.getElementById("statusLabel").value = gMbrotBundle.getString("statusEmpty");
56 function adjustCoordsAndDraw(aC_min, aC_max) {
59 iWidth = gPref.getIntPref("mandelbrot.image.width");
62 if ((iWidth < 10) || (iWidth > 5000)) {
64 gPref.setIntPref("mandelbrot.image.width", iWidth);
68 iHeight = gPref.getIntPref("mandelbrot.image.height");
71 if ((iHeight < 10) || (iHeight > 5000)) {
73 gPref.setIntPref("mandelbrot.image.height", iHeight);
76 // correct coordinates
81 if ((aC_min.r > 2) || (aC_max.r < -2) || (aC_min.r >= aC_max.r)) {
82 aC_min.r = -2.0; aC_max.r = 1.0;
88 if ((aC_min.i > 2) || (aC_max.i < -2) || (aC_min.i >= aC_max.i)) {
89 aC_min.i = -1.3; aC_max.i = 1.3;
92 let CWidth = aC_max.r - aC_min.r;
93 let CHeight = aC_max.i - aC_min.i;
94 let C_mid = new complex(aC_min.r + CWidth / 2, aC_min.i + CHeight / 2);
96 let CRatio = Math.max(CWidth / iWidth, CHeight / iHeight);
98 gPref.setCharPref("mandelbrot.last_image.Cr_min", C_mid.r - iWidth * CRatio / 2);
99 gPref.setCharPref("mandelbrot.last_image.Cr_max", C_mid.r + iWidth * CRatio / 2);
100 gPref.setCharPref("mandelbrot.last_image.Ci_min", C_mid.i - iHeight * CRatio / 2);
101 gPref.setCharPref("mandelbrot.last_image.Ci_max", C_mid.i + iHeight * CRatio / 2);
106 function drawImage() {
107 let canvas = document.getElementById("mbrotImage");
108 let context = canvas.getContext("2d");
110 document.getElementById("drawButton").hidden = true;
112 document.getElementById("statusLabel").value = gMbrotBundle.getString("statusDrawing");
117 Cr_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_min"));
118 Cr_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_max"));
121 if ((Cr_min < -3) || (Cr_min > 2) ||
122 (Cr_max < -3) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
123 Cr_min = -2.0; Cr_max = 1.0;
125 gPref.setCharPref("mandelbrot.last_image.Cr_min", Cr_min);
126 gPref.setCharPref("mandelbrot.last_image.Cr_max", Cr_max);
131 Ci_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_min"));
132 Ci_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_max"));
135 if ((Ci_min < -2.5) || (Ci_min > 2.5) ||
136 (Ci_max < -2.5) || (Ci_max > 2.5) || (Ci_min >= Ci_max)) {
137 Ci_min = -1.5; Ci_max = 1.5;
139 gPref.setCharPref("mandelbrot.last_image.Ci_min", Ci_min);
140 gPref.setCharPref("mandelbrot.last_image.Ci_max", Ci_max);
142 let iterMax = gPref.getIntPref("mandelbrot.iteration_max");
143 let algorithm = gPref.getCharPref("mandelbrot.use_algorithm");
147 iWidth = gPref.getIntPref("mandelbrot.image.width");
150 if ((iWidth < 10) || (iWidth > 5000)) {
152 gPref.setIntPref("mandelbrot.image.width", iWidth);
156 iHeight = gPref.getIntPref("mandelbrot.image.height");
159 if ((iHeight < 10) || (iHeight > 5000)) {
161 gPref.setIntPref("mandelbrot.image.height", iHeight);
164 gCurrentImageData = {
165 C_min: new complex(Cr_min, Ci_min),
166 C_max: new complex(Cr_max, Ci_max),
172 canvas.width = iWidth;
173 canvas.height = iHeight;
175 context.fillStyle = "rgba(255, 255, 255, 127)";
176 context.fillRect(0, 0, canvas.width, canvas.height);
178 gStartTime = new Date();
180 drawLine(0, [Cr_min, Cr_max, Ci_min, Ci_max],
181 canvas, context, iterMax, algorithm);
184 function drawLine(line, dimensions, canvas, context, iterMax, algorithm) {
185 let Cr_min = dimensions[0];
186 let Cr_max = dimensions[1];
187 let Cr_scale = Cr_max - Cr_min;
189 let Ci_min = dimensions[2];
190 let Ci_max = dimensions[3];
191 let Ci_scale = Ci_max - Ci_min;
193 let lines = Math.min(canvas.height - line, 8);
194 let imageData = context.createImageData(canvas.width, lines);
195 let pixels = imageData.data;
197 for (var img_y = line; img_y < canvas.height && img_y < line+8; img_y++)
198 for (let img_x = 0; img_x < canvas.width; img_x++) {
199 let C = new complex(Cr_min + (img_x / canvas.width) * Cr_scale,
200 Ci_min + (img_y / canvas.height) * Ci_scale);
201 let colors = drawPoint(context, img_x, img_y, C, iterMax, algorithm);
202 pixels[idx++] = colors[0];
203 pixels[idx++] = colors[1];
204 pixels[idx++] = colors[2];
205 pixels[idx++] = colors[3];
207 context.putImageData(imageData, 0, line);
209 if (img_y < canvas.height)
210 setTimeout(drawLine, 0, img_y, dimensions, canvas, context, iterMax, algorithm);
216 let endTime = new Date();
217 let timeUsed = (endTime.getTime() - gStartTime.getTime()) / 1000;
218 document.getElementById("statusLabel").value =
219 gMbrotBundle.getFormattedString("statusTime", [timeUsed.toFixed(3)]);
223 function complex(aReal, aImag) {
227 complex.prototype = {
229 return new complex(this.r * this.r - this.i * this.i,
230 2 * this.r * this.i);
233 return Math.sqrt(this.r * this.r + this.i * this.i);
235 add: function(aComplex) {
236 return new complex(this.r + aComplex.r, this.i + aComplex.i);
240 function mandelbrotValueOO (aC, aIterMax) {
241 // this would be nice code in general but it looks like JS objects are too heavy for normal use.
242 let Z = new complex(0.0, 0.0);
243 for (var iter = 0; iter < aIterMax; iter++) {
244 Z = Z.square().add(aC);
245 if (Z.r * Z.r + Z.i * Z.i > 256) { break; }
250 function mandelbrotValueNumeric (aC, aIterMax) {
251 // optimized numeric code for fast calculation
252 let Cr = aC.r, Ci = aC.i;
253 let Zr = 0.0, Zi = 0.0;
254 let Zr2 = Zr * Zr, Zi2 = Zi * Zi;
255 for (var iter = 0; iter < aIterMax; iter++) {
256 Zi = 2 * Zr * Zi + Ci;
259 Zr2 = Zr * Zr; Zi2 = Zi * Zi;
260 if (Zr2 + Zi2 > 256) { break; }
265 function getColor(aIterValue, aIterMax) {
266 let standardizedValue = Math.round(aIterValue * 1024 / aIterMax);
267 if (gColorPalette && gColorPalette.length)
268 return gColorPalette[standardizedValue];
270 // fallback to simple b/w if for some reason we don't have a palette
271 if (aIterValue == aIterMax)
272 return [0, 0, 0, 255];
274 return [255, 255, 255, 255];
277 function getColorPalette(palName) {
281 for (let i = 0; i < 1024; i++) {
282 palette[i] = [255, 255, 255, 255];
284 palette[1024] = [0, 0, 0, 255];
288 for (let i = 0; i < 32; i++) {
289 let cc1 = Math.floor(i * 127 / 31);
290 let cc2 = 170 - Math.floor(i * 43 / 31);
291 palette[i] = [cc1, cc2, cc1, 255];
294 for (let i = 0; i < 51; i++) {
295 let cc = Math.floor(i * 170 / 50);
296 palette[32 + i] = [cc, 0, (170-cc), 255];
299 for (let i = 0; i < 101; i++) {
300 let cc = Math.floor(i * 200 / 100);
301 palette[83 + i] = [255, cc, 0, 255];
304 for (let i = 0; i < 201; i++) {
305 let cc1 = 255 - Math.floor(i * 85 / 200);
306 let cc2 = 200 - Math.floor(i * 30 / 200);
307 let cc3 = Math.floor(i * 170 / 200);
308 palette[184 + i] = [cc1, cc2, cc3, 255];
310 for (let i = 0; i < 301; i++) {
311 let cc1 = 170 - Math.floor(i * 43 / 300);
312 let cc2 = 170 + Math.floor(i * 85 / 300);
313 palette[385 + i] = [cc1, cc1, cc2, 255];
315 for (let i = 0; i < 338; i++) {
316 let cc = 127 + Math.floor(i * 128 / 337);
317 palette[686 + i] = [cc, cc, 255, 255];
319 palette[1024] = [0, 0, 0, 255];
321 case 'rainbow-linear1':
322 for (let i = 0; i < 256; i++) {
323 palette[i] = [i, 0, 0, 255];
324 palette[256 + i] = [255, i, 0, 255];
325 palette[512 + i] = [255 - i, 255, i, 255];
326 palette[768 + i] = [i, 255-i, 255, 255];
328 palette[1024] = [0, 0, 0, 255];
330 case 'rainbow-squared1':
331 for (let i = 0; i < 34; i++) {
332 let cc = Math.floor(i * 255 / 33);
333 palette[i] = [cc, 0, 0, 255];
335 for (let i = 0; i < 137; i++) {
336 let cc = Math.floor(i * 255 / 136);
337 palette[34 + i] = [255, cc, 0, 255];
339 for (let i = 0; i < 307; i++) {
340 let cc = Math.floor(i * 255 / 306);
341 palette[171 + i] = [255 - cc, 255, cc, 255];
343 for (let i = 0; i < 546; i++) {
344 let cc = Math.floor(i * 255 / 545);
345 palette[478 + i] = [cc, 255 - cc, 255, 255];
347 palette[1024] = [0, 0, 0, 255];
349 case 'rainbow-linear2':
350 for (let i = 0; i < 205; i++) {
351 let cc = Math.floor(i * 255 / 204);
352 palette[i] = [255, cc, 0, 255];
353 palette[204 + i] = [255 - cc, 255, 0, 255];
354 palette[409 + i] = [0, 255, cc, 255];
355 palette[614 + i] = [0, 255 - cc, 255, 255];
356 palette[819 + i] = [cc, 0, 255, 255];
358 palette[1024] = [0, 0, 0, 255];
360 case 'rainbow-squared2':
361 for (let i = 0; i < 19; i++) {
362 let cc = Math.floor(i * 255 / 18);
363 palette[i] = [255, cc, 0, 255];
365 for (let i = 0; i < 74; i++) {
366 let cc = Math.floor(i * 255 / 73);
367 palette[19 + i] = [255 - cc, 255, 0, 255];
369 for (let i = 0; i < 168; i++) {
370 let cc = Math.floor(i * 255 / 167);
371 palette[93 + i] = [0, 255, cc, 255];
373 for (let i = 0; i < 298; i++) {
374 let cc = Math.floor(i * 255 / 297);
375 palette[261 + i] = [0, 255 - cc, 255, 255];
377 for (let i = 0; i < 465; i++) {
378 let cc = Math.floor(i * 255 / 464);
379 palette[559 + i] = [cc, 0, 255, 255];
381 palette[1024] = [0, 0, 0, 255];
385 'Standard-Palette (QB-Colors)
387 xx = CInt(i * 500 / 1024 + 2)
388 If xx <= 15 Then clr = xx
389 If xx > 15 Then clr = CInt(Sqr((xx - 15 + 1) * 15 ^ 2 / 485))
390 If xx >= 500 Then clr = 0
391 palette(i) = QBColor(clr)
397 function drawPoint(context, img_x, img_y, C, iterMax, algorithm) {
401 itVal = mandelbrotValueOO(C, iterMax);
405 itVal = mandelbrotValueNumeric(C, iterMax);
408 return getColor(itVal, iterMax);
411 /***** pure UI functions *****/
416 function mouseevent(etype, event) {
417 let canvas = document.getElementById("mbrotImage");
418 let context = canvas.getContext("2d");
421 if (event.button == 0) {
422 // left button - start dragzoom
423 zoomstart = {x: event.clientX - canvas.offsetLeft,
424 y: event.clientY - canvas.offsetTop};
425 imgBackup = context.getImageData(0, 0, canvas.width, canvas.height);
429 if (event.button == 0 && zoomstart) {
430 context.putImageData(imgBackup, 0, 0);
431 let zoomend = {x: event.clientX - canvas.offsetLeft,
432 y: event.clientY - canvas.offsetTop};
434 // make sure zoomend is bigger than zoomstart
435 if ((zoomend.x == zoomstart.x) || (zoomend.y == zoomstart.y)) {
436 // cannot zoom what has no area, discard it
437 zoomstart = undefined;
440 if (zoomend.x < zoomstart.x)
441 [zoomend.x, zoomstart.x] = [zoomstart.x, zoomend.x];
442 if (zoomend.y < zoomstart.y)
443 [zoomend.y, zoomstart.y] = [zoomstart.y, zoomend.y];
445 // determine new "coordinates"
446 let CWidth = gCurrentImageData.C_max.r - gCurrentImageData.C_min.r;
447 let CHeight = gCurrentImageData.C_max.i - gCurrentImageData.C_min.i;
448 let newC_min = new complex(
449 gCurrentImageData.C_min.r + zoomstart.x / gCurrentImageData.iWidth * CWidth,
450 gCurrentImageData.C_min.i + zoomstart.y / gCurrentImageData.iHeight * CHeight);
451 let newC_max = new complex(
452 gCurrentImageData.C_min.r + zoomend.x / gCurrentImageData.iWidth * CWidth,
453 gCurrentImageData.C_min.i + zoomend.y / gCurrentImageData.iHeight * CHeight);
455 adjustCoordsAndDraw(newC_min, newC_max);
457 zoomstart = undefined;
460 if (event.button == 0 && zoomstart) {
461 context.putImageData(imgBackup, 0, 0);
462 context.strokeStyle = "rgb(255,255,31)";
463 context.strokeRect(zoomstart.x, zoomstart.y,
464 event.clientX - canvas.offsetLeft - zoomstart.x,
465 event.clientY - canvas.offsetTop - zoomstart.y);
471 function saveImage() {
472 const nsIFilePicker = Components.interfaces.nsIFilePicker;
475 fp = Components.classes["@mozilla.org/filepicker;1"]
476 .createInstance(nsIFilePicker);
479 let promptString = gMbrotBundle.getString("savePrompt");
480 fp.init(window, promptString, nsIFilePicker.modeSave);
481 fp.appendFilter(gMbrotBundle.getString("pngFilterName"), "*.png");
482 fp.defaultString = "mandelbrot.png";
484 let fpResult = fp.show();
485 if (fpResult != nsIFilePicker.returnCancel) {
486 saveCanvas(document.getElementById("mbrotImage"), fp.file);
490 function exitMandelbrot() {
491 var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
492 .getService(Components.interfaces.nsIXULAppInfo);
493 if (appInfo.ID == "mandelbrot@kairo.at")
499 function updateBookmarkMenu(aParent) {
500 document.getElementById("bookmarkSave").disabled =
501 (!document.getElementById("drawButton").hidden || (gStartTime > 0));
503 while (aParent.hasChildNodes() &&
504 aParent.lastChild.id != "bookmarkSeparator")
505 aParent.removeChild(aParent.lastChild);
507 let file = Components.classes["@mozilla.org/file/directory_service;1"]
508 .getService(Components.interfaces.nsIProperties)
509 .get("ProfD", Components.interfaces.nsIFile);
510 file.append("mandelbookmarks.sqlite");
512 let connection = Components.classes["@mozilla.org/storage/service;1"]
513 .getService(Components.interfaces.mozIStorageService)
516 if (connection.tableExists("bookmarks")) {
517 let statement = connection.createStatement(
518 "SELECT name,ROWID FROM bookmarks ORDER BY ROWID ASC");
519 while (statement.executeStep()) {
520 let newItem = aParent.appendChild(document.createElement("menuitem"));
521 newItem.setAttribute("label", statement.getString(0));
522 newItem.setAttribute("bmRowID", statement.getString(1));
525 statement.finalize();
532 // Create the "Nothing Available" Menu item and disable it.
533 let na = aParent.appendChild(document.createElement("menuitem"));
534 na.setAttribute("label", gMbrotBundle.getString("noBookmarks"));
535 na.setAttribute("disabled", "true");
538 function callBookmark(evtarget) {
539 if (evtarget.id == "bookmarkSave" || evtarget.id == "bookmarkSeparator")
541 if (evtarget.id == "bookmarkOverview") {
542 adjustCoordsAndDraw(new complex(0,0), new complex(0,0));
546 if (evtarget.getAttribute('bmRowID')) {
551 let file = Components.classes["@mozilla.org/file/directory_service;1"]
552 .getService(Components.interfaces.nsIProperties)
553 .get("ProfD", Components.interfaces.nsIFile);
554 file.append("mandelbookmarks.sqlite");
555 let connection = Components.classes["@mozilla.org/storage/service;1"]
556 .getService(Components.interfaces.mozIStorageService)
558 let statement = connection.createStatement(
559 "SELECT iteration_max,Cr_min,Cr_max,Ci_min,Ci_max FROM bookmarks WHERE ROWID=?1");
560 statement.bindStringParameter(0, evtarget.getAttribute('bmRowID'));
561 while (statement.executeStep()) {
562 iterMax = statement.getInt32(0);
563 C_min = new complex(statement.getDouble(1), statement.getDouble(3));
564 C_max = new complex(statement.getDouble(2), statement.getDouble(4));
566 statement.finalize();
569 if (iterMax && C_min && C_max) {
570 gPref.setIntPref("mandelbrot.iteration_max", iterMax);
571 adjustCoordsAndDraw(C_min, C_max);
576 function saveBookmark() {
577 // retrieve wanted bookmark name with a prompt
578 let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
579 .getService(Components.interfaces.nsIPromptService);
580 let input = {value: ""}; // empty default value
581 let ok = prompts.prompt(null, gMbrotBundle.getString("saveBookmarkTitle"), gMbrotBundle.getString("saveBookmarkLabel"), input, null, {});
582 // ok is true if OK is pressed, false if Cancel. input.value holds the value of the edit field if "OK" was pressed.
583 if (!ok || !input.value)
586 let bmName = input.value;
588 // Open or create the bookmarks database.
589 let file = Components.classes["@mozilla.org/file/directory_service;1"]
590 .getService(Components.interfaces.nsIProperties)
591 .get("ProfD", Components.interfaces.nsIFile);
592 file.append("mandelbookmarks.sqlite");
593 let connection = Components.classes["@mozilla.org/storage/service;1"]
594 .getService(Components.interfaces.mozIStorageService)
596 connection.beginTransaction();
597 if (!connection.tableExists("bookmarks"))
598 connection.createTable("bookmarks", "name TEXT, iteration_max INTEGER, Cr_min REAL, Cr_max REAL, Ci_min REAL, Ci_max REAL");
599 // NULL. The value is a NULL value.
600 // INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
601 // REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
602 // TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16-LE).
604 // Put value of the current image into the bookmarks table
605 let statement = connection.createStatement(
606 "INSERT INTO bookmarks (name,iteration_max,Cr_min,Cr_max,Ci_min,Ci_max) VALUES (?1,?2,?3,?4,?5,?6)");
607 statement.bindStringParameter(0, bmName);
608 statement.bindStringParameter(1, gCurrentImageData.iterMax);
609 statement.bindStringParameter(2, gCurrentImageData.C_min.r);
610 statement.bindStringParameter(3, gCurrentImageData.C_max.r);
611 statement.bindStringParameter(4, gCurrentImageData.C_min.i);
612 statement.bindStringParameter(5, gCurrentImageData.C_max.i);
614 statement.finalize();
615 connection.commitTransaction();
619 function updateIterMenu() {
622 currentIter = gPref.getIntPref("mandelbrot.iteration_max");
625 if (currentIter < 10) {
627 setIter(currentIter);
630 let popup = document.getElementById("menu_iterPopup");
631 let item = popup.firstChild;
633 if (item.getAttribute("name") == "iter") {
634 if (item.getAttribute("value") == currentIter)
635 item.setAttribute("checked","true");
637 item.removeAttribute("checked");
639 item = item.nextSibling;
643 function setIter(aIter) {
644 gPref.setIntPref("mandelbrot.iteration_max", aIter);
647 function updatePaletteMenu() {
648 let currentPalette = '';
650 currentPalette = gPref.getCharPref("mandelbrot.color_palette");
653 if (!currentPalette.length) {
654 currentPalette = 'kairo';
655 setPalette(currentPalette);
657 if (!gColorPalette || !gColorPalette.length)
658 gColorPalette = getColorPalette(currentPalette);
660 let popup = document.getElementById("menu_palettePopup");
661 let item = popup.firstChild;
663 if (item.getAttribute("name") == "palette") {
664 if (item.getAttribute("value") == currentPalette)
665 item.setAttribute("checked", "true");
667 item.removeAttribute("checked");
669 item = item.nextSibling;
673 function setPalette(aPaletteID) {
674 gPref.setCharPref("mandelbrot.color_palette", aPaletteID);
675 gColorPalette = getColorPalette(aPaletteID);
678 function imgSettings() {
680 let position = "before_start";
681 if (document.getElementById("mandelbrotWindow").nodeName == "page") {
682 anchor = document.getElementById("mandelbrotToolbar");
685 anchor = document.getElementById("mandelbrotMenubar");
686 position = "after_start";
688 document.getElementById("imgSettingsPanel").showPopup(anchor, position);
691 function updateDebugMenu() {
692 let scope = (document.getElementById("mandelbrotWindow").nodeName == "page") ? "content" : "chrome";
693 for each (let type in ["tracejit", "methodjit"]) {
694 let jitMenuItem = document.getElementById(type + "Enabled");
695 jitMenuItem.setAttribute("checked", gPref.getBoolPref("javascript.options." + type + "." + scope));
699 function toggleJITState(jitMenuItem, jittype) {
700 let scope = (document.getElementById("mandelbrotWindow").nodeName == "page") ? "content" : "chrome";
701 let jitpref = "javascript.options." + jittype + "jit." + scope;
702 let jitEnabled = !gPref.getBoolPref(jitpref);
703 gPref.setBoolPref(jitpref, jitEnabled)
704 jitMenuItem.setAttribute("checked", jitEnabled ? "true" : "false");
707 function updateAlgoMenu() {
708 let currentAlgo = '';
710 currentAlgo = gPref.getCharPref("mandelbrot.use_algorithm");
713 if (!currentAlgo.length) {
714 currentAlgo = 'numeric';
715 setAlgorithm(currentAlgo);
718 let popup = document.getElementById("menu_algoPopup");
719 let item = popup.firstChild;
721 if (item.getAttribute("name") == "algorithm") {
722 if (item.getAttribute("value") == currentAlgo)
723 item.setAttribute("checked", "true");
725 item.removeAttribute("checked");
727 item = item.nextSibling;
731 function setAlgorithm(algoID) {
732 gPref.setCharPref("mandelbrot.use_algorithm", algoID);
735 function addonsManager(aPane) {
736 let theEM = Components.classes["@mozilla.org/appshell/window-mediator;1"]
737 .getService(Components.interfaces.nsIWindowMediator)
738 .getMostRecentWindow("Extension:Manager");
742 theEM.showView(aPane);
746 const EMURL = "chrome://mozapps/content/extensions/extensions.xul";
747 const EMFEATURES = "all,dialog=no";
749 window.openDialog(EMURL, "", EMFEATURES, aPane);
751 window.openDialog(EMURL, "", EMFEATURES);
754 function errorConsole() {
755 toOpenWindowByType("global:console", "chrome://global/content/console.xul");
758 function initImgSettings() {
759 // Get values from prefs.
760 for each (let coord in ["Cr_min", "Cr_max", "Ci_min", "Ci_max"]) {
761 document.getElementById("is_" + coord).value =
762 roundCoord(parseFloat(gPref.getCharPref("mandelbrot.last_image." + coord)));
764 for each (let dim in ["width", "height"]) {
765 document.getElementById("is_img_" + dim).value =
766 gPref.getIntPref("mandelbrot.image." + dim);
768 document.getElementById("is_syncProp").checked =
769 gPref.getBoolPref("mandelbrot.syncProportions");
772 recalcCoord("Cr", "scale");
773 recalcCoord("Ci", "scale");
775 // Clear the preview.
776 let canvas = document.getElementById("is_mbrotPreview");
777 let context = canvas.getContext("2d");
778 context.fillStyle = "rgba(255, 255, 255, 127)";
779 context.fillRect(0, 0, canvas.width, canvas.height);
782 function closeImgSettings() {
783 // Hide popup, which will automatically make a call to save values.
784 document.getElementById("imgSettingsPanel").hidePopup();
787 function saveImgSettings() {
788 // Get values to prefs.
789 for each (let coord in ["Cr_min", "Cr_max", "Ci_min", "Ci_max"]) {
790 gPref.setCharPref("mandelbrot.last_image." + coord,
791 document.getElementById("is_" + coord).value);
793 for each (let dim in ["width", "height"]) {
794 gPref.setIntPref("mandelbrot.image." + dim,
795 document.getElementById("is_img_" + dim).value);
797 gPref.setBoolPref("mandelbrot.syncProportions",
798 document.getElementById("is_syncProp").checked);
801 function checkISValue(textbox, type) {
802 if (type == "coord") {
803 textbox.value = roundCoord(parseFloat(textbox.value));
805 else if (type == "dim") {
806 textbox.value = parseInt(textbox.value);
810 function drawPreview() {
811 let canvas = document.getElementById("is_mbrotPreview");
812 let context = canvas.getContext("2d");
814 if (document.getElementById("is_img_width").value /
815 document.getElementById("is_img_height").value
818 canvas.width = canvas.height *
819 document.getElementById("is_img_width").value /
820 document.getElementById("is_img_height").value;
824 canvas.height = canvas.width *
825 document.getElementById("is_imgHeight").value /
826 document.getElementById("is_imgWidth").value;
829 let Cr_min = parseFloat(document.getElementById("is_Cr_min").value);
830 let Cr_max = parseFloat(document.getElementById("is_Cr_max").value);
831 if ((Cr_min < -2) || (Cr_min > 2) ||
832 (Cr_max < -2) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
833 Cr_min = -2.0; Cr_max = 1.0;
836 let Ci_min = parseFloat(document.getElementById("is_Ci_min").value);
837 let Ci_max = parseFloat(document.getElementById("is_Ci_max").value);
838 if ((Ci_min < -2) || (Ci_min > 2) ||
839 (Ci_max < -2) || (Ci_max > 2) || (Ci_min >= Ci_max)) {
840 Ci_min = -2.0; Ci_max = 1.0;
843 let iterMax = gPref.getIntPref("mandelbrot.iteration_max");
844 let algorithm = gPref.getCharPref("mandelbrot.use_algorithm");
846 context.fillStyle = "rgba(255, 255, 255, 127)";
847 context.fillRect(0, 0, canvas.width, canvas.height);
850 var currentPalette = gPref.getCharPref("mandelbrot.color_palette");
853 var currentPalette = "";
855 if (!currentPalette.length) {
856 currentPalette = "kairo";
858 gColorPalette = getColorPalette(currentPalette);
860 drawLine(0, [Cr_min, Cr_max, Ci_min, Ci_max],
861 canvas, context, iterMax, algorithm);
864 function recalcCoord(coord, target) {
865 let othercoord = (coord == "Ci") ? "Cr" : "Ci";
866 let owndim = (coord == "Ci") ? "height" : "width";
867 let otherdim = (coord == "Ci") ? "width" : "height";
868 if (target == "scale") {
870 parseFloat(document.getElementById("is_" + coord + "_max").value) -
871 parseFloat(document.getElementById("is_" + coord + "_min").value);
872 document.getElementById("is_" + coord + "_scale").value = roundCoord(myscale);
874 else if (target == 'max') {
876 parseFloat(document.getElementById("is_" + coord + "_min").value) +
877 parseFloat(document.getElementById("is_" + coord + "_scale").value);
878 document.getElementById("is_" + coord + "_max").value = roundCoord(mymax);
879 var myscale = document.getElementById("is_" + coord + "_scale").value;
881 if (document.getElementById("is_syncProp").checked) {
882 let otherscale = myscale *
883 document.getElementById("is_img_" + otherdim).value /
884 document.getElementById("is_img_" + owndim).value;
885 document.getElementById("is_" + othercoord + "_scale").value = roundCoord(otherscale);
887 parseFloat(document.getElementById("is_" + othercoord + "_min").value) +
888 parseFloat(document.getElementById("is_" + othercoord + "_scale").value);
889 document.getElementById("is_" + othercoord + "_max").value = roundCoord(othermax);
893 function checkProportions() {
894 if (!document.getElementById("is_syncProp").checked) {
895 recalcCoord("Cr", "scale");
899 function roundCoord(floatval) {
900 // We should round to 10 decimals here or so
901 return parseFloat(floatval.toFixed(10));
904 /***** helper functions from external sources *****/
906 // function below is based on http://developer.mozilla.org/en/docs/Code_snippets:Canvas
907 // custom modifications:
908 // - use "a"-prefix on function arguments
909 // - take an nsILocalFile as aDestFile argument
910 // - always do silent download
911 function saveCanvas(aCanvas, aDestFile) {
912 // create a data url from the canvas and then create URIs of the source and targets
913 var io = Components.classes["@mozilla.org/network/io-service;1"]
914 .getService(Components.interfaces.nsIIOService);
915 var source = io.newURI(aCanvas.toDataURL("image/png", ""), "UTF8", null);
917 // prepare to save the canvas data
918 var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
919 .createInstance(Components.interfaces.nsIWebBrowserPersist);
921 persist.persistFlags = Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
922 persist.persistFlags |= Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
924 // save the canvas data to the file
925 persist.saveURI(source, null, null, null, null, aDestFile);
928 // function below is from http://developer.mozilla.org/en/docs/How_to_Quit_a_XUL_Application
929 function quitApp(aForceQuit) {
930 var appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1']
931 .getService(Components.interfaces.nsIAppStartup);
933 // eAttemptQuit will try to close each XUL window, but the XUL window can cancel the quit
934 // process if there is unsaved data. eForceQuit will quit no matter what.
935 var quitSeverity = aForceQuit ? Components.interfaces.nsIAppStartup.eForceQuit :
936 Components.interfaces.nsIAppStartup.eAttemptQuit;
937 appStartup.quit(quitSeverity);
940 // functions below are from comm-central/suite/common/tasksOverlay.js
941 function toOpenWindow(aWindow) {
943 // Try to focus the previously focused window e.g. message compose body
944 aWindow.document.commandDispatcher.focusedWindow.focus();
946 // e.g. full-page plugin or non-XUL document; just raise the top window
951 function toOpenWindowByType(inType, uri, features) {
952 // don't do several loads in parallel
956 var topWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"]
957 .getService(Components.interfaces.nsIWindowMediator)
958 .getMostRecentWindow(inType);
960 toOpenWindow( topWindow );
962 // open the requested window, but block it until it's fully loaded
963 function newWindowLoaded(event) {
964 // make sure that this handler is called only once
965 window.removeEventListener("unload", newWindowLoaded, false);
966 window[uri].removeEventListener("load", newWindowLoaded, false);
969 // remember the newly loading window until it's fully loaded
970 // or until the current window passes away
971 window[uri] = window.openDialog(uri, "", features || "all,dialog=no");
972 window[uri].addEventListener("load", newWindowLoaded, false);
973 window.addEventListener("unload", newWindowLoaded, false);