make mandelbrot build and roughly work as an extension
[mandelbrot.git] / xulapp / chrome / mandelbrot / content / mandelbrot.js
... / ...
CommitLineData
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
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Robert Kaiser <kairo@kairo.at>
23 *
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.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38var gColorPalette = [];
39var gPref = Components.classes["@mozilla.org/preferences-service;1"]
40 .getService(Components.interfaces.nsIPrefService)
41 .getBranch(null);
42var gStartTime = 0;
43var gMbrotBundle;
44var gCurrentImageData;
45
46function Startup() {
47 updateIterMenu();
48 updateAlgoMenu();
49 updatePaletteMenu();
50 gMbrotBundle = document.getElementById("mbrotBundle");
51 document.getElementById("statusLabel").value = gMbrotBundle.getString("statusEmpty");
52}
53
54function adjustCoordsAndDraw(aC_min, aC_max) {
55 let iWidth = 0;
56 try {
57 iWidth = gPref.getIntPref("mandelbrot.image.width");
58 }
59 catch (e) { }
60 if ((iWidth < 10) || (iWidth > 5000)) {
61 iWidth = 300;
62 gPref.setIntPref("mandelbrot.image.width", iWidth);
63 }
64 let iHeight = 0;
65 try {
66 iHeight = gPref.getIntPref("mandelbrot.image.height");
67 }
68 catch (e) { }
69 if ((iHeight < 10) || (iHeight > 5000)) {
70 iHeight = 300;
71 gPref.setIntPref("mandelbrot.image.height", iHeight);
72 }
73
74 // correct coordinates
75 if (aC_min.r < -2)
76 aC_min.r = -2;
77 if (aC_max.r > 2)
78 aC_max.r = 2;
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;
81 }
82 if (aC_min.i < -2)
83 aC_min.i = -2;
84 if (aC_max.i > 2)
85 aC_max.i = 2;
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;
88 }
89
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);
93
94 let CRatio = Math.max(CWidth / iWidth, CHeight / iHeight);
95
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);
100
101 drawImage();
102}
103
104function drawImage() {
105 let canvas = document.getElementById("mbrotImage");
106 let context = canvas.getContext("2d");
107
108 document.getElementById("drawButton").hidden = true;
109
110 document.getElementById("statusLabel").value = gMbrotBundle.getString("statusDrawing");
111
112 let Cr_min = -2.0;
113 let Cr_max = 1.0;
114 try {
115 Cr_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_min"));
116 Cr_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_max"));
117 }
118 catch (e) { }
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;
122 }
123 gPref.setCharPref("mandelbrot.last_image.Cr_min", Cr_min);
124 gPref.setCharPref("mandelbrot.last_image.Cr_max", Cr_max);
125
126 let Ci_min = -1.5;
127 let Ci_max = 1.5;
128 try {
129 Ci_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_min"));
130 Ci_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_max"));
131 }
132 catch (e) { }
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;
136 }
137 gPref.setCharPref("mandelbrot.last_image.Ci_min", Ci_min);
138 gPref.setCharPref("mandelbrot.last_image.Ci_max", Ci_max);
139
140 let iterMax = gPref.getIntPref("mandelbrot.iteration_max");
141 let algorithm = gPref.getCharPref("mandelbrot.use_algorithm");
142
143 let iWidth = 0;
144 try {
145 iWidth = gPref.getIntPref("mandelbrot.image.width");
146 }
147 catch (e) { }
148 if ((iWidth < 10) || (iWidth > 5000)) {
149 iWidth = 300;
150 gPref.setIntPref("mandelbrot.image.width", iWidth);
151 }
152 let iHeight = 0;
153 try {
154 iHeight = gPref.getIntPref("mandelbrot.image.height");
155 }
156 catch (e) { }
157 if ((iHeight < 10) || (iHeight > 5000)) {
158 iHeight = 300;
159 gPref.setIntPref("mandelbrot.image.height", iHeight);
160 }
161
162 gCurrentImageData = {
163 C_min: new complex(Cr_min, Ci_min),
164 C_max: new complex(Cr_max, Ci_max),
165 iWidth: iWidth,
166 iHeight: iHeight,
167 iterMax: iterMax
168 };
169
170 canvas.width = iWidth;
171 canvas.height = iHeight;
172
173 context.fillStyle = "rgba(255, 255, 255, 127)";
174 context.fillRect(0, 0, canvas.width, canvas.height);
175
176 gStartTime = new Date();
177
178 drawLine(0, [Cr_min, Cr_max, Ci_min, Ci_max],
179 canvas, context, iterMax, algorithm);
180}
181
182function 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;
186
187 let Ci_min = dimensions[2];
188 let Ci_max = dimensions[3];
189 let Ci_scale = Ci_max - Ci_min;
190
191 let pixels = [];
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));
197 }
198 context.putImageData({width: canvas.width, height: pixels.length/4/canvas.width, data: pixels}, 0, line);
199
200 if (img_y < canvas.height)
201 setTimeout(drawLine, 0, img_y, dimensions, canvas, context, iterMax, algorithm);
202 else if (gStartTime)
203 EndCalc();
204}
205
206function EndCalc() {
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)]);
211 gStartTime = 0;
212}
213
214function complex(aReal, aImag) {
215 this.r = aReal;
216 this.i = aImag;
217}
218complex.prototype = {
219 square: function() {
220 return new complex(this.r * this.r - this.i * this.i,
221 2 * this.r * this.i);
222 },
223 dist: function() {
224 return Math.sqrt(this.r * this.r + this.i * this.i);
225 },
226 add: function(aComplex) {
227 return new complex(this.r + aComplex.r, this.i + aComplex.i);
228 }
229}
230
231function 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; }
237 }
238 return iter;
239}
240
241function 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;
248 Zr = Zr2 - Zi2 + Cr;
249
250 Zr2 = Zr * Zr; Zi2 = Zi * Zi;
251 if (Zr2 + Zi2 > 256) { break; }
252 }
253 return iter;
254}
255
256function getColor(aIterValue, aIterMax) {
257 let standardizedValue = Math.round(aIterValue * 1024 / aIterMax);
258 if (gColorPalette && gColorPalette.length)
259 return gColorPalette[standardizedValue];
260
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];
264 else
265 return [255, 255, 255, 255];
266}
267
268function getColorPalette(palName) {
269 var palette = [];
270 switch (palName) {
271 case 'bw':
272 for (let i = 0; i < 1024; i++) {
273 palette[i] = [255, 255, 255, 255];
274 }
275 palette[1024] = [0, 0, 0, 255];
276 break;
277 case 'kairo':
278 // outer areas
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];
283 }
284 // inner areas
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];
288 }
289 // corona
290 for (let i = 0; i < 101; i++) {
291 let cc = Math.floor(i * 200 / 100);
292 palette[83 + i] = [255, cc, 0, 255];
293 }
294 // inner corona
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];
300 }
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];
305 }
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];
309 }
310 palette[1024] = [0, 0, 0, 255];
311 break;
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];
318 }
319 palette[1024] = [0, 0, 0, 255];
320 break;
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];
325 }
326 for (let i = 0; i < 137; i++) {
327 let cc = Math.floor(i * 255 / 136);
328 palette[34 + i] = [255, cc, 0, 255];
329 }
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];
333 }
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];
337 }
338 palette[1024] = [0, 0, 0, 255];
339 break;
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];
348 }
349 palette[1024] = [0, 0, 0, 255];
350 break;
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];
355 }
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];
359 }
360 for (let i = 0; i < 168; i++) {
361 let cc = Math.floor(i * 255 / 167);
362 palette[93 + i] = [0, 255, cc, 255];
363 }
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];
367 }
368 for (let i = 0; i < 465; i++) {
369 let cc = Math.floor(i * 255 / 464);
370 palette[559 + i] = [cc, 0, 255, 255];
371 }
372 palette[1024] = [0, 0, 0, 255];
373 break;
374 }
375 /*
376 'Standard-Palette (QB-Colors)
377 For i = 0 To 1024
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)
383 Next
384 */
385 return palette;
386}
387
388function drawPoint(context, img_x, img_y, C, iterMax, algorithm) {
389 var itVal;
390 switch (algorithm) {
391 case 'oo':
392 itVal = mandelbrotValueOO(C, iterMax);
393 break;
394 case 'numeric':
395 default:
396 itVal = mandelbrotValueNumeric(C, iterMax);
397 break;
398 }
399 return getColor(itVal, iterMax);
400}
401
402/***** pure UI functions *****/
403
404var zoomstart;
405var imgBackup;
406
407function mouseevent(etype, event) {
408 let canvas = document.getElementById("mbrotImage");
409 let context = canvas.getContext("2d");
410 switch (etype) {
411 case 'down':
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);
417 }
418 break;
419 case 'up':
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};
424
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;
429 return;
430 }
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];
435
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
441 if (ok) {
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);
451
452 adjustCoordsAndDraw(newC_min, newC_max);
453 }
454 }
455 zoomstart = undefined;
456 break;
457 case 'move':
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);
464 }
465 break;
466 }
467}
468
469function saveImage() {
470 const nsIFilePicker = Components.interfaces.nsIFilePicker;
471 let fp = null;
472 try {
473 fp = Components.classes["@mozilla.org/filepicker;1"]
474 .createInstance(nsIFilePicker);
475 } catch (e) {}
476 if (!fp) return;
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";
481
482 let fpResult = fp.show();
483 if (fpResult != nsIFilePicker.returnCancel) {
484 saveCanvas(document.getElementById("mbrotImage"), fp.file);
485 }
486}
487
488function updateBookmarkMenu(aParent) {
489 document.getElementById("bookmarkSave").disabled =
490 (!document.getElementById("drawButton").hidden || (gStartTime > 0));
491
492 while (aParent.hasChildNodes() &&
493 aParent.lastChild.id != "bookmarkSeparator")
494 aParent.removeChild(aParent.lastChild);
495
496 let file = Components.classes["@mozilla.org/file/directory_service;1"]
497 .getService(Components.interfaces.nsIProperties)
498 .get("ProfD", Components.interfaces.nsIFile);
499 file.append("mandelbookmarks.sqlite");
500 if (file.exists()) {
501 let connection = Components.classes["@mozilla.org/storage/service;1"]
502 .getService(Components.interfaces.mozIStorageService)
503 .openDatabase(file);
504 try {
505 if (connection.tableExists("bookmarks")) {
506 let statement = connection.createStatement(
507 "SELECT name,ROWID FROM bookmarks ORDER BY ROWID ASC");
508 while (statement.executeStep()) {
509 let newItem = aParent.appendChild(document.createElement("menuitem"));
510 newItem.setAttribute("label", statement.getString(0));
511 newItem.setAttribute("bmRowID", statement.getString(1));
512 }
513 statement.reset();
514 statement.finalize();
515 return;
516 }
517 } finally {
518 connection.close();
519 }
520 }
521 // Create the "Nothing Available" Menu item and disable it.
522 let na = aParent.appendChild(document.createElement("menuitem"));
523 na.setAttribute("label", gMbrotBundle.getString("noBookmarks"));
524 na.setAttribute("disabled", "true");
525}
526
527function callBookmark(evtarget) {
528 if (evtarget.id == "bookmarkSave" || evtarget.id == "bookmarkSeparator")
529 return;
530 if (evtarget.id == "bookmarkOverview") {
531 adjustCoordsAndDraw(new complex(0,0), new complex(0,0));
532 return;
533 }
534
535 if (evtarget.getAttribute('bmRowID')) {
536 let iterMax = 0;
537 let C_min = null;
538 let C_max = null;
539
540 let file = Components.classes["@mozilla.org/file/directory_service;1"]
541 .getService(Components.interfaces.nsIProperties)
542 .get("ProfD", Components.interfaces.nsIFile);
543 file.append("mandelbookmarks.sqlite");
544 let connection = Components.classes["@mozilla.org/storage/service;1"]
545 .getService(Components.interfaces.mozIStorageService)
546 .openDatabase(file);
547 let statement = connection.createStatement(
548 "SELECT iteration_max,Cr_min,Cr_max,Ci_min,Ci_max FROM bookmarks WHERE ROWID=?1");
549 statement.bindStringParameter(0, evtarget.getAttribute('bmRowID'));
550 while (statement.executeStep()) {
551 iterMax = statement.getInt32(0);
552 C_min = new complex(statement.getDouble(1), statement.getDouble(3));
553 C_max = new complex(statement.getDouble(2), statement.getDouble(4));
554 }
555 statement.finalize();
556 connection.close();
557
558 if (iterMax && C_min && C_max) {
559 gPref.setIntPref("mandelbrot.iteration_max", iterMax);
560 adjustCoordsAndDraw(C_min, C_max);
561 }
562 }
563}
564
565function saveBookmark() {
566 // retrieve wanted bookmark name with a prompt
567 let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
568 .getService(Components.interfaces.nsIPromptService);
569 let input = {value: ""}; // empty default value
570 let ok = prompts.prompt(null, gMbrotBundle.getString("saveBookmarkTitle"), gMbrotBundle.getString("saveBookmarkLabel"), input, null, {});
571 // ok is true if OK is pressed, false if Cancel. input.value holds the value of the edit field if "OK" was pressed.
572 if (!ok || !input.value)
573 return
574
575 let bmName = input.value;
576
577 // Open or create the bookmarks database.
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 let connection = Components.classes["@mozilla.org/storage/service;1"]
583 .getService(Components.interfaces.mozIStorageService)
584 .openDatabase(file);
585 connection.beginTransaction();
586 if (!connection.tableExists("bookmarks"))
587 connection.createTable("bookmarks", "name TEXT, iteration_max INTEGER, Cr_min REAL, Cr_max REAL, Ci_min REAL, Ci_max REAL");
588 // NULL. The value is a NULL value.
589 // INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
590 // REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
591 // TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16-LE).
592
593 // Put value of the current image into the bookmarks table
594 let statement = connection.createStatement(
595 "INSERT INTO bookmarks (name,iteration_max,Cr_min,Cr_max,Ci_min,Ci_max) VALUES (?1,?2,?3,?4,?5,?6)");
596 statement.bindStringParameter(0, bmName);
597 statement.bindStringParameter(1, gCurrentImageData.iterMax);
598 statement.bindStringParameter(2, gCurrentImageData.C_min.r);
599 statement.bindStringParameter(3, gCurrentImageData.C_max.r);
600 statement.bindStringParameter(4, gCurrentImageData.C_min.i);
601 statement.bindStringParameter(5, gCurrentImageData.C_max.i);
602 statement.execute();
603 statement.finalize();
604 connection.commitTransaction();
605 connection.close();
606}
607
608function updateIterMenu() {
609 let currentIter = 0;
610 try {
611 currentIter = gPref.getIntPref("mandelbrot.iteration_max");
612 }
613 catch(e) { }
614 if (currentIter < 10) {
615 currentIter = 500;
616 setIter(currentIter);
617 }
618
619 let popup = document.getElementById("menu_iterPopup");
620 let item = popup.firstChild;
621 while (item) {
622 if (item.getAttribute("name") == "iter") {
623 if (item.getAttribute("value") == currentIter)
624 item.setAttribute("checked","true");
625 else
626 item.removeAttribute("checked");
627 }
628 item = item.nextSibling;
629 }
630}
631
632function setIter(aIter) {
633 gPref.setIntPref("mandelbrot.iteration_max", aIter);
634}
635
636function updatePaletteMenu() {
637 let currentPalette = '';
638 try {
639 currentPalette = gPref.getCharPref("mandelbrot.color_palette");
640 }
641 catch(e) { }
642 if (!currentPalette.length) {
643 currentPalette = 'kairo';
644 setPalette(currentPalette);
645 }
646 if (!gColorPalette || !gColorPalette.length)
647 gColorPalette = getColorPalette(currentPalette);
648
649 let popup = document.getElementById("menu_palettePopup");
650 let item = popup.firstChild;
651 while (item) {
652 if (item.getAttribute("name") == "palette") {
653 if (item.getAttribute("value") == currentPalette)
654 item.setAttribute("checked", "true");
655 else
656 item.removeAttribute("checked");
657 }
658 item = item.nextSibling;
659 }
660}
661
662function setPalette(aPaletteID) {
663 gPref.setCharPref("mandelbrot.color_palette", aPaletteID);
664 gColorPalette = getColorPalette(aPaletteID);
665}
666
667function imgSettings() {
668 window.openDialog("chrome://mandelbrot/content/image-settings.xul");
669}
670
671function updateDebugMenu() {
672 var jitMenuItem = document.getElementById("jitEnabled");
673 jitMenuItem.setAttribute("checked", gPref.getBoolPref("javascript.options.jit.chrome"));
674}
675
676function toggleJITState(jitMenuItem) {
677 var jitEnabled = !gPref.getBoolPref("javascript.options.jit.chrome");
678 gPref.setBoolPref("javascript.options.jit.chrome", jitEnabled)
679 jitMenuItem.setAttribute("checked", jitEnabled? "true" : "false");
680}
681
682function updateAlgoMenu() {
683 let currentAlgo = '';
684 try {
685 currentAlgo = gPref.getCharPref("mandelbrot.use_algorithm");
686 }
687 catch(e) { }
688 if (!currentAlgo.length) {
689 currentAlgo = 'numeric';
690 setAlgorithm(currentAlgo);
691 }
692
693 let popup = document.getElementById("menu_algoPopup");
694 let item = popup.firstChild;
695 while (item) {
696 if (item.getAttribute("name") == "algorithm") {
697 if (item.getAttribute("value") == currentAlgo)
698 item.setAttribute("checked", "true");
699 else
700 item.removeAttribute("checked");
701 }
702 item = item.nextSibling;
703 }
704}
705
706function setAlgorithm(algoID) {
707 gPref.setCharPref("mandelbrot.use_algorithm", algoID);
708}
709
710function addonsManager(aPane) {
711 let theEM = Components.classes["@mozilla.org/appshell/window-mediator;1"]
712 .getService(Components.interfaces.nsIWindowMediator)
713 .getMostRecentWindow("Extension:Manager");
714 if (theEM) {
715 theEM.focus();
716 if (aPane)
717 theEM.showView(aPane);
718 return;
719 }
720
721 const EMURL = "chrome://mozapps/content/extensions/extensions.xul";
722 const EMFEATURES = "all,dialog=no";
723 if (aPane)
724 window.openDialog(EMURL, "", EMFEATURES, aPane);
725 else
726 window.openDialog(EMURL, "", EMFEATURES);
727}
728
729function errorConsole() {
730 toOpenWindowByType("global:console", "chrome://global/content/console.xul");
731}
732
733/***** helper functions from external sources *****/
734
735// function below is based on http://developer.mozilla.org/en/docs/Code_snippets:Canvas
736// custom modifications:
737// - use "a"-prefix on function arguments
738// - take an nsILocalFile as aDestFile argument
739// - always do silent download
740function saveCanvas(aCanvas, aDestFile) {
741 // create a data url from the canvas and then create URIs of the source and targets
742 var io = Components.classes["@mozilla.org/network/io-service;1"]
743 .getService(Components.interfaces.nsIIOService);
744 var source = io.newURI(aCanvas.toDataURL("image/png", ""), "UTF8", null);
745
746 // prepare to save the canvas data
747 var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
748 .createInstance(Components.interfaces.nsIWebBrowserPersist);
749
750 persist.persistFlags = Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
751 persist.persistFlags |= Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
752
753 // save the canvas data to the file
754 persist.saveURI(source, null, null, null, null, aDestFile);
755}
756
757// function below is from http://developer.mozilla.org/en/docs/How_to_Quit_a_XUL_Application
758function quitApp(aForceQuit) {
759 var appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1']
760 .getService(Components.interfaces.nsIAppStartup);
761
762 // eAttemptQuit will try to close each XUL window, but the XUL window can cancel the quit
763 // process if there is unsaved data. eForceQuit will quit no matter what.
764 var quitSeverity = aForceQuit ? Components.interfaces.nsIAppStartup.eForceQuit :
765 Components.interfaces.nsIAppStartup.eAttemptQuit;
766 appStartup.quit(quitSeverity);
767}
768
769// functions below are from comm-central/suite/common/tasksOverlay.js
770function toOpenWindow(aWindow) {
771 try {
772 // Try to focus the previously focused window e.g. message compose body
773 aWindow.document.commandDispatcher.focusedWindow.focus();
774 } catch (e) {
775 // e.g. full-page plugin or non-XUL document; just raise the top window
776 aWindow.focus();
777 }
778}
779
780function toOpenWindowByType(inType, uri, features) {
781 // don't do several loads in parallel
782 if (uri in window)
783 return;
784
785 var topWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"]
786 .getService(Components.interfaces.nsIWindowMediator)
787 .getMostRecentWindow(inType);
788 if ( topWindow )
789 toOpenWindow( topWindow );
790 else {
791 // open the requested window, but block it until it's fully loaded
792 function newWindowLoaded(event) {
793 // make sure that this handler is called only once
794 window.removeEventListener("unload", newWindowLoaded, false);
795 window[uri].removeEventListener("load", newWindowLoaded, false);
796 delete window[uri];
797 }
798 // remember the newly loading window until it's fully loaded
799 // or until the current window passes away
800 window[uri] = window.openDialog(uri, "", features || "all,dialog=no");
801 window[uri].addEventListener("load", newWindowLoaded, false);
802 window.addEventListener("unload", newWindowLoaded, false);
803 }
804}