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
19 * the Initial Developer. All Rights Reserved.
22 * Robert Kaiser <kairo@kairo.at>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 var gColorPalette = [];
39 var gPref = Components.classes["@mozilla.org/preferences-service;1"]
40 .getService(Components.interfaces.nsIPrefService)
44 var gCurrentImageData;
50 gMbrotBundle = document.getElementById("mbrotBundle");
51 document.getElementById("statusLabel").value = gMbrotBundle.getString("statusEmpty");
54 function adjustCoordsAndDraw(aC_min, aC_max) {
57 iWidth = gPref.getIntPref("mandelbrot.image.width");
60 if ((iWidth < 10) || (iWidth > 5000)) {
62 gPref.setIntPref("mandelbrot.image.width", iWidth);
66 iHeight = gPref.getIntPref("mandelbrot.image.height");
69 if ((iHeight < 10) || (iHeight > 5000)) {
71 gPref.setIntPref("mandelbrot.image.height", iHeight);
74 // correct coordinates
79 if ((aC_min.r > 2) || (aC_max.r < -2) || (aC_min.r >= aC_max.r)) {
80 aC_min.r = -2.0; aC_max.r = 1.0;
86 if ((aC_min.i > 2) || (aC_max.i < -2) || (aC_min.i >= aC_max.i)) {
87 aC_min.i = -1.3; aC_max.i = 1.3;
90 let CWidth = aC_max.r - aC_min.r;
91 let CHeight = aC_max.i - aC_min.i;
92 let C_mid = new complex(aC_min.r + CWidth / 2, aC_min.i + CHeight / 2);
94 let CRatio = Math.max(CWidth / iWidth, CHeight / iHeight);
96 gPref.setCharPref("mandelbrot.last_image.Cr_min", C_mid.r - iWidth * CRatio / 2);
97 gPref.setCharPref("mandelbrot.last_image.Cr_max", C_mid.r + iWidth * CRatio / 2);
98 gPref.setCharPref("mandelbrot.last_image.Ci_min", C_mid.i - iHeight * CRatio / 2);
99 gPref.setCharPref("mandelbrot.last_image.Ci_max", C_mid.i + iHeight * CRatio / 2);
104 function drawImage() {
105 let canvas = document.getElementById("mbrotImage");
106 let context = canvas.getContext("2d");
108 document.getElementById("drawButton").hidden = true;
110 document.getElementById("statusLabel").value = gMbrotBundle.getString("statusDrawing");
115 Cr_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_min"));
116 Cr_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_max"));
119 if ((Cr_min < -3) || (Cr_min > 2) ||
120 (Cr_max < -3) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
121 Cr_min = -2.0; Cr_max = 1.0;
123 gPref.setCharPref("mandelbrot.last_image.Cr_min", Cr_min);
124 gPref.setCharPref("mandelbrot.last_image.Cr_max", Cr_max);
129 Ci_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_min"));
130 Ci_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_max"));
133 if ((Ci_min < -2.5) || (Ci_min > 2.5) ||
134 (Ci_max < -2.5) || (Ci_max > 2.5) || (Ci_min >= Ci_max)) {
135 Ci_min = -1.5; Ci_max = 1.5;
137 gPref.setCharPref("mandelbrot.last_image.Ci_min", Ci_min);
138 gPref.setCharPref("mandelbrot.last_image.Ci_max", Ci_max);
140 let iterMax = gPref.getIntPref("mandelbrot.iteration_max");
141 let algorithm = gPref.getCharPref("mandelbrot.use_algorithm");
145 iWidth = gPref.getIntPref("mandelbrot.image.width");
148 if ((iWidth < 10) || (iWidth > 5000)) {
150 gPref.setIntPref("mandelbrot.image.width", iWidth);
154 iHeight = gPref.getIntPref("mandelbrot.image.height");
157 if ((iHeight < 10) || (iHeight > 5000)) {
159 gPref.setIntPref("mandelbrot.image.height", iHeight);
162 gCurrentImageData = {
163 C_min: new complex(Cr_min, Ci_min),
164 C_max: new complex(Cr_max, Ci_max),
170 canvas.width = iWidth;
171 canvas.height = iHeight;
173 context.fillStyle = "rgba(255, 255, 255, 127)";
174 context.fillRect(0, 0, canvas.width, canvas.height);
176 gStartTime = new Date();
178 drawLine(0, [Cr_min, Cr_max, Ci_min, Ci_max],
179 canvas, context, iterMax, algorithm);
182 function drawLine(line, dimensions, canvas, context, iterMax, algorithm) {
183 let Cr_min = dimensions[0];
184 let Cr_max = dimensions[1];
185 let Cr_scale = Cr_max - Cr_min;
187 let Ci_min = dimensions[2];
188 let Ci_max = dimensions[3];
189 let Ci_scale = Ci_max - Ci_min;
192 for (var img_y = line; img_y < canvas.height && img_y < line+8; img_y++)
193 for (let img_x = 0; img_x < canvas.width; img_x++) {
194 let C = new complex(Cr_min + (img_x / canvas.width) * Cr_scale,
195 Ci_min + (img_y / canvas.height) * Ci_scale);
196 pixels.push.apply(pixels, drawPoint(context, img_x, img_y, C, iterMax, algorithm));
198 context.putImageData({width: canvas.width, height: pixels.length/4/canvas.width, data: pixels}, 0, line);
200 if (img_y < canvas.height)
201 setTimeout(drawLine, 0, img_y, dimensions, canvas, context, iterMax, algorithm);
207 let endTime = new Date();
208 let timeUsed = (endTime.getTime() - gStartTime.getTime()) / 1000;
209 document.getElementById("statusLabel").value =
210 gMbrotBundle.getFormattedString("statusTime", [timeUsed.toFixed(3)]);
214 function complex(aReal, aImag) {
218 complex.prototype = {
220 return new complex(this.r * this.r - this.i * this.i,
221 2 * this.r * this.i);
224 return Math.sqrt(this.r * this.r + this.i * this.i);
226 add: function(aComplex) {
227 return new complex(this.r + aComplex.r, this.i + aComplex.i);
231 function mandelbrotValueOO (aC, aIterMax) {
232 // this would be nice code in general but it looks like JS objects are too heavy for normal use.
233 let Z = new complex(0.0, 0.0);
234 for (var iter = 0; iter < aIterMax; iter++) {
235 Z = Z.square().add(aC);
236 if (Z.r * Z.r + Z.i * Z.i > 256) { break; }
241 function mandelbrotValueNumeric (aC, aIterMax) {
242 // optimized numeric code for fast calculation
243 let Cr = aC.r, Ci = aC.i;
244 let Zr = 0.0, Zi = 0.0;
245 let Zr2 = Zr * Zr, Zi2 = Zi * Zi;
246 for (var iter = 0; iter < aIterMax; iter++) {
247 Zi = 2 * Zr * Zi + Ci;
250 Zr2 = Zr * Zr; Zi2 = Zi * Zi;
251 if (Zr2 + Zi2 > 256) { break; }
256 function getColor(aIterValue, aIterMax) {
257 let standardizedValue = Math.round(aIterValue * 1024 / aIterMax);
258 if (gColorPalette && gColorPalette.length)
259 return gColorPalette[standardizedValue];
261 // fallback to simple b/w if for some reason we don't have a palette
262 if (aIterValue == aIterMax)
263 return [0, 0, 0, 255];
265 return [255, 255, 255, 255];
268 function getColorPalette(palName) {
272 for (let i = 0; i < 1024; i++) {
273 palette[i] = [255, 255, 255, 255];
275 palette[1024] = [0, 0, 0, 255];
279 for (let i = 0; i < 32; i++) {
280 let cc1 = Math.floor(i * 127 / 31);
281 let cc2 = 170 - Math.floor(i * 43 / 31);
282 palette[i] = [cc1, cc2, cc1, 255];
285 for (let i = 0; i < 51; i++) {
286 let cc = Math.floor(i * 170 / 50);
287 palette[32 + i] = [cc, 0, (170-cc), 255];
290 for (let i = 0; i < 101; i++) {
291 let cc = Math.floor(i * 200 / 100);
292 palette[83 + i] = [255, cc, 0, 255];
295 for (let i = 0; i < 201; i++) {
296 let cc1 = 255 - Math.floor(i * 85 / 200);
297 let cc2 = 200 - Math.floor(i * 30 / 200);
298 let cc3 = Math.floor(i * 170 / 200);
299 palette[184 + i] = [cc1, cc2, cc3, 255];
301 for (let i = 0; i < 301; i++) {
302 let cc1 = 170 - Math.floor(i * 43 / 300);
303 let cc2 = 170 + Math.floor(i * 85 / 300);
304 palette[385 + i] = [cc1, cc1, cc2, 255];
306 for (let i = 0; i < 338; i++) {
307 let cc = 127 + Math.floor(i * 128 / 337);
308 palette[686 + i] = [cc, cc, 255, 255];
310 palette[1024] = [0, 0, 0, 255];
312 case 'rainbow-linear1':
313 for (let i = 0; i < 256; i++) {
314 palette[i] = [i, 0, 0, 255];
315 palette[256 + i] = [255, i, 0, 255];
316 palette[512 + i] = [255 - i, 255, i, 255];
317 palette[768 + i] = [i, 255-i, 255, 255];
319 palette[1024] = [0, 0, 0, 255];
321 case 'rainbow-squared1':
322 for (let i = 0; i < 34; i++) {
323 let cc = Math.floor(i * 255 / 33);
324 palette[i] = [cc, 0, 0, 255];
326 for (let i = 0; i < 137; i++) {
327 let cc = Math.floor(i * 255 / 136);
328 palette[34 + i] = [255, cc, 0, 255];
330 for (let i = 0; i < 307; i++) {
331 let cc = Math.floor(i * 255 / 306);
332 palette[171 + i] = [255 - cc, 255, cc, 255];
334 for (let i = 0; i < 546; i++) {
335 let cc = Math.floor(i * 255 / 545);
336 palette[478 + i] = [cc, 255 - cc, 255, 255];
338 palette[1024] = [0, 0, 0, 255];
340 case 'rainbow-linear2':
341 for (let i = 0; i < 205; i++) {
342 let cc = Math.floor(i * 255 / 204);
343 palette[i] = [255, cc, 0, 255];
344 palette[204 + i] = [255 - cc, 255, 0, 255];
345 palette[409 + i] = [0, 255, cc, 255];
346 palette[614 + i] = [0, 255 - cc, 255, 255];
347 palette[819 + i] = [cc, 0, 255, 255];
349 palette[1024] = [0, 0, 0, 255];
351 case 'rainbow-squared2':
352 for (let i = 0; i < 19; i++) {
353 let cc = Math.floor(i * 255 / 18);
354 palette[i] = [255, cc, 0, 255];
356 for (let i = 0; i < 74; i++) {
357 let cc = Math.floor(i * 255 / 73);
358 palette[19 + i] = [255 - cc, 255, 0, 255];
360 for (let i = 0; i < 168; i++) {
361 let cc = Math.floor(i * 255 / 167);
362 palette[93 + i] = [0, 255, cc, 255];
364 for (let i = 0; i < 298; i++) {
365 let cc = Math.floor(i * 255 / 297);
366 palette[261 + i] = [0, 255 - cc, 255, 255];
368 for (let i = 0; i < 465; i++) {
369 let cc = Math.floor(i * 255 / 464);
370 palette[559 + i] = [cc, 0, 255, 255];
372 palette[1024] = [0, 0, 0, 255];
376 'Standard-Palette (QB-Colors)
378 xx = CInt(i * 500 / 1024 + 2)
379 If xx <= 15 Then clr = xx
380 If xx > 15 Then clr = CInt(Sqr((xx - 15 + 1) * 15 ^ 2 / 485))
381 If xx >= 500 Then clr = 0
382 palette(i) = QBColor(clr)
388 function drawPoint(context, img_x, img_y, C, iterMax, algorithm) {
392 itVal = mandelbrotValueOO(C, iterMax);
396 itVal = mandelbrotValueNumeric(C, iterMax);
399 return getColor(itVal, iterMax);
402 /***** pure UI functions *****/
407 function mouseevent(etype, event) {
408 let canvas = document.getElementById("mbrotImage");
409 let context = canvas.getContext("2d");
412 if (event.button == 0) {
413 // left button - start dragzoom
414 zoomstart = {x: event.clientX - canvas.offsetLeft,
415 y: event.clientY - canvas.offsetTop};
416 imgBackup = context.getImageData(0, 0, canvas.width, canvas.height);
420 if (event.button == 0 && zoomstart) {
421 context.putImageData(imgBackup, 0, 0);
422 let zoomend = {x: event.clientX - canvas.offsetLeft,
423 y: event.clientY - canvas.offsetTop};
425 // make sure zoomend is bigger than zoomstart
426 if ((zoomend.x == zoomstart.x) || (zoomend.y == zoomstart.y)) {
427 // cannot zoom what has no area, discard it
428 zoomstart = undefined;
431 if (zoomend.x < zoomstart.x)
432 [zoomend.x, zoomstart.x] = [zoomstart.x, zoomend.x];
433 if (zoomend.y < zoomstart.y)
434 [zoomend.y, zoomstart.y] = [zoomstart.y, zoomend.y];
436 let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
437 .getService(Components.interfaces.nsIPromptService);
438 let ok = prompts.confirm(null, gMbrotBundle.getString("zoomConfirmTitle"),
439 gMbrotBundle.getString("zoomConfirmLabel"));
440 // ok is now true if OK was clicked, and false if cancel was clicked
442 // determine new "coordinates"
443 let CWidth = gCurrentImageData.C_max.r - gCurrentImageData.C_min.r;
444 let CHeight = gCurrentImageData.C_max.i - gCurrentImageData.C_min.i;
445 let newC_min = new complex(
446 gCurrentImageData.C_min.r + zoomstart.x / gCurrentImageData.iWidth * CWidth,
447 gCurrentImageData.C_min.i + zoomstart.y / gCurrentImageData.iHeight * CHeight);
448 let newC_max = new complex(
449 gCurrentImageData.C_min.r + zoomend.x / gCurrentImageData.iWidth * CWidth,
450 gCurrentImageData.C_min.i + zoomend.y / gCurrentImageData.iHeight * CHeight);
452 adjustCoordsAndDraw(newC_min, newC_max);
455 zoomstart = undefined;
458 if (event.button == 0 && zoomstart) {
459 context.putImageData(imgBackup, 0, 0);
460 context.strokeStyle = "rgb(255,255,31)";
461 context.strokeRect(zoomstart.x, zoomstart.y,
462 event.clientX - canvas.offsetLeft - zoomstart.x,
463 event.clientY - canvas.offsetTop - zoomstart.y);
469 function saveImage() {
470 const nsIFilePicker = Components.interfaces.nsIFilePicker;
473 fp = Components.classes["@mozilla.org/filepicker;1"]
474 .createInstance(nsIFilePicker);
477 let promptString = gMbrotBundle.getString("savePrompt");
478 fp.init(window, promptString, nsIFilePicker.modeSave);
479 fp.appendFilter(gMbrotBundle.getString("pngFilterName"), "*.png");
480 fp.defaultString = "mandelbrot.png";
482 let fpResult = fp.show();
483 if (fpResult != nsIFilePicker.returnCancel) {
484 saveCanvas(document.getElementById("mbrotImage"), fp.file);
488 function exitMandelbrot() {
489 var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
490 .getService(Components.interfaces.nsIXULAppInfo);
491 if (appInfo.ID == "mandelbrot@kairo.at")
497 function updateBookmarkMenu(aParent) {
498 document.getElementById("bookmarkSave").disabled =
499 (!document.getElementById("drawButton").hidden || (gStartTime > 0));
501 while (aParent.hasChildNodes() &&
502 aParent.lastChild.id != "bookmarkSeparator")
503 aParent.removeChild(aParent.lastChild);
505 let file = Components.classes["@mozilla.org/file/directory_service;1"]
506 .getService(Components.interfaces.nsIProperties)
507 .get("ProfD", Components.interfaces.nsIFile);
508 file.append("mandelbookmarks.sqlite");
510 let connection = Components.classes["@mozilla.org/storage/service;1"]
511 .getService(Components.interfaces.mozIStorageService)
514 if (connection.tableExists("bookmarks")) {
515 let statement = connection.createStatement(
516 "SELECT name,ROWID FROM bookmarks ORDER BY ROWID ASC");
517 while (statement.executeStep()) {
518 let newItem = aParent.appendChild(document.createElement("menuitem"));
519 newItem.setAttribute("label", statement.getString(0));
520 newItem.setAttribute("bmRowID", statement.getString(1));
523 statement.finalize();
530 // Create the "Nothing Available" Menu item and disable it.
531 let na = aParent.appendChild(document.createElement("menuitem"));
532 na.setAttribute("label", gMbrotBundle.getString("noBookmarks"));
533 na.setAttribute("disabled", "true");
536 function callBookmark(evtarget) {
537 if (evtarget.id == "bookmarkSave" || evtarget.id == "bookmarkSeparator")
539 if (evtarget.id == "bookmarkOverview") {
540 adjustCoordsAndDraw(new complex(0,0), new complex(0,0));
544 if (evtarget.getAttribute('bmRowID')) {
549 let file = Components.classes["@mozilla.org/file/directory_service;1"]
550 .getService(Components.interfaces.nsIProperties)
551 .get("ProfD", Components.interfaces.nsIFile);
552 file.append("mandelbookmarks.sqlite");
553 let connection = Components.classes["@mozilla.org/storage/service;1"]
554 .getService(Components.interfaces.mozIStorageService)
556 let statement = connection.createStatement(
557 "SELECT iteration_max,Cr_min,Cr_max,Ci_min,Ci_max FROM bookmarks WHERE ROWID=?1");
558 statement.bindStringParameter(0, evtarget.getAttribute('bmRowID'));
559 while (statement.executeStep()) {
560 iterMax = statement.getInt32(0);
561 C_min = new complex(statement.getDouble(1), statement.getDouble(3));
562 C_max = new complex(statement.getDouble(2), statement.getDouble(4));
564 statement.finalize();
567 if (iterMax && C_min && C_max) {
568 gPref.setIntPref("mandelbrot.iteration_max", iterMax);
569 adjustCoordsAndDraw(C_min, C_max);
574 function saveBookmark() {
575 // retrieve wanted bookmark name with a prompt
576 let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
577 .getService(Components.interfaces.nsIPromptService);
578 let input = {value: ""}; // empty default value
579 let ok = prompts.prompt(null, gMbrotBundle.getString("saveBookmarkTitle"), gMbrotBundle.getString("saveBookmarkLabel"), input, null, {});
580 // ok is true if OK is pressed, false if Cancel. input.value holds the value of the edit field if "OK" was pressed.
581 if (!ok || !input.value)
584 let bmName = input.value;
586 // Open or create the bookmarks database.
587 let file = Components.classes["@mozilla.org/file/directory_service;1"]
588 .getService(Components.interfaces.nsIProperties)
589 .get("ProfD", Components.interfaces.nsIFile);
590 file.append("mandelbookmarks.sqlite");
591 let connection = Components.classes["@mozilla.org/storage/service;1"]
592 .getService(Components.interfaces.mozIStorageService)
594 connection.beginTransaction();
595 if (!connection.tableExists("bookmarks"))
596 connection.createTable("bookmarks", "name TEXT, iteration_max INTEGER, Cr_min REAL, Cr_max REAL, Ci_min REAL, Ci_max REAL");
597 // NULL. The value is a NULL value.
598 // INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
599 // REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
600 // TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16-LE).
602 // Put value of the current image into the bookmarks table
603 let statement = connection.createStatement(
604 "INSERT INTO bookmarks (name,iteration_max,Cr_min,Cr_max,Ci_min,Ci_max) VALUES (?1,?2,?3,?4,?5,?6)");
605 statement.bindStringParameter(0, bmName);
606 statement.bindStringParameter(1, gCurrentImageData.iterMax);
607 statement.bindStringParameter(2, gCurrentImageData.C_min.r);
608 statement.bindStringParameter(3, gCurrentImageData.C_max.r);
609 statement.bindStringParameter(4, gCurrentImageData.C_min.i);
610 statement.bindStringParameter(5, gCurrentImageData.C_max.i);
612 statement.finalize();
613 connection.commitTransaction();
617 function updateIterMenu() {
620 currentIter = gPref.getIntPref("mandelbrot.iteration_max");
623 if (currentIter < 10) {
625 setIter(currentIter);
628 let popup = document.getElementById("menu_iterPopup");
629 let item = popup.firstChild;
631 if (item.getAttribute("name") == "iter") {
632 if (item.getAttribute("value") == currentIter)
633 item.setAttribute("checked","true");
635 item.removeAttribute("checked");
637 item = item.nextSibling;
641 function setIter(aIter) {
642 gPref.setIntPref("mandelbrot.iteration_max", aIter);
645 function updatePaletteMenu() {
646 let currentPalette = '';
648 currentPalette = gPref.getCharPref("mandelbrot.color_palette");
651 if (!currentPalette.length) {
652 currentPalette = 'kairo';
653 setPalette(currentPalette);
655 if (!gColorPalette || !gColorPalette.length)
656 gColorPalette = getColorPalette(currentPalette);
658 let popup = document.getElementById("menu_palettePopup");
659 let item = popup.firstChild;
661 if (item.getAttribute("name") == "palette") {
662 if (item.getAttribute("value") == currentPalette)
663 item.setAttribute("checked", "true");
665 item.removeAttribute("checked");
667 item = item.nextSibling;
671 function setPalette(aPaletteID) {
672 gPref.setCharPref("mandelbrot.color_palette", aPaletteID);
673 gColorPalette = getColorPalette(aPaletteID);
676 function imgSettings() {
677 window.openDialog("chrome://mandelbrot/content/image-settings.xul");
680 function updateDebugMenu() {
681 let scope = (document.getElementById("mandelbrotWindow").nodeName == "page") ? "content" : "chrome";
682 for each (let type in ["tracejit", "methodjit"]) {
683 let jitMenuItem = document.getElementById(type + "Enabled");
684 jitMenuItem.setAttribute("checked", gPref.getBoolPref("javascript.options." + type + "." + scope));
688 function toggleJITState(jitMenuItem, jittype) {
689 let scope = (document.getElementById("mandelbrotWindow").nodeName == "page") ? "content" : "chrome";
690 let jitpref = "javascript.options." + jittype + "jit." + scope;
691 let jitEnabled = !gPref.getBoolPref(jitpref);
692 gPref.setBoolPref(jitpref, jitEnabled)
693 jitMenuItem.setAttribute("checked", jitEnabled ? "true" : "false");
696 function updateAlgoMenu() {
697 let currentAlgo = '';
699 currentAlgo = gPref.getCharPref("mandelbrot.use_algorithm");
702 if (!currentAlgo.length) {
703 currentAlgo = 'numeric';
704 setAlgorithm(currentAlgo);
707 let popup = document.getElementById("menu_algoPopup");
708 let item = popup.firstChild;
710 if (item.getAttribute("name") == "algorithm") {
711 if (item.getAttribute("value") == currentAlgo)
712 item.setAttribute("checked", "true");
714 item.removeAttribute("checked");
716 item = item.nextSibling;
720 function setAlgorithm(algoID) {
721 gPref.setCharPref("mandelbrot.use_algorithm", algoID);
724 function addonsManager(aPane) {
725 let theEM = Components.classes["@mozilla.org/appshell/window-mediator;1"]
726 .getService(Components.interfaces.nsIWindowMediator)
727 .getMostRecentWindow("Extension:Manager");
731 theEM.showView(aPane);
735 const EMURL = "chrome://mozapps/content/extensions/extensions.xul";
736 const EMFEATURES = "all,dialog=no";
738 window.openDialog(EMURL, "", EMFEATURES, aPane);
740 window.openDialog(EMURL, "", EMFEATURES);
743 function errorConsole() {
744 toOpenWindowByType("global:console", "chrome://global/content/console.xul");
747 /***** helper functions from external sources *****/
749 // function below is based on http://developer.mozilla.org/en/docs/Code_snippets:Canvas
750 // custom modifications:
751 // - use "a"-prefix on function arguments
752 // - take an nsILocalFile as aDestFile argument
753 // - always do silent download
754 function saveCanvas(aCanvas, aDestFile) {
755 // create a data url from the canvas and then create URIs of the source and targets
756 var io = Components.classes["@mozilla.org/network/io-service;1"]
757 .getService(Components.interfaces.nsIIOService);
758 var source = io.newURI(aCanvas.toDataURL("image/png", ""), "UTF8", null);
760 // prepare to save the canvas data
761 var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
762 .createInstance(Components.interfaces.nsIWebBrowserPersist);
764 persist.persistFlags = Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
765 persist.persistFlags |= Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
767 // save the canvas data to the file
768 persist.saveURI(source, null, null, null, null, aDestFile);
771 // function below is from http://developer.mozilla.org/en/docs/How_to_Quit_a_XUL_Application
772 function quitApp(aForceQuit) {
773 var appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1']
774 .getService(Components.interfaces.nsIAppStartup);
776 // eAttemptQuit will try to close each XUL window, but the XUL window can cancel the quit
777 // process if there is unsaved data. eForceQuit will quit no matter what.
778 var quitSeverity = aForceQuit ? Components.interfaces.nsIAppStartup.eForceQuit :
779 Components.interfaces.nsIAppStartup.eAttemptQuit;
780 appStartup.quit(quitSeverity);
783 // functions below are from comm-central/suite/common/tasksOverlay.js
784 function toOpenWindow(aWindow) {
786 // Try to focus the previously focused window e.g. message compose body
787 aWindow.document.commandDispatcher.focusedWindow.focus();
789 // e.g. full-page plugin or non-XUL document; just raise the top window
794 function toOpenWindowByType(inType, uri, features) {
795 // don't do several loads in parallel
799 var topWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"]
800 .getService(Components.interfaces.nsIWindowMediator)
801 .getMostRecentWindow(inType);
803 toOpenWindow( topWindow );
805 // open the requested window, but block it until it's fully loaded
806 function newWindowLoaded(event) {
807 // make sure that this handler is called only once
808 window.removeEventListener("unload", newWindowLoaded, false);
809 window[uri].removeEventListener("load", newWindowLoaded, false);
812 // remember the newly loading window until it's fully loaded
813 // or until the current window passes away
814 window[uri] = window.openDialog(uri, "", features || "all,dialog=no");
815 window[uri].addEventListener("load", newWindowLoaded, false);
816 window.addEventListener("unload", newWindowLoaded, false);