make overview item work
[mandelbrot.git] / xulapp / chrome / mandelbrot / content / mandelbrot.js
CommitLineData
5b823560
RK
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
6e98af87
RK
38var gColorPalette = [];
39var gPref = Components.classes["@mozilla.org/preferences-service;1"]
40 .getService(Components.interfaces.nsIPrefService)
41 .getBranch(null);
42var gStartTime = 0;
fa4ecb24 43var gMbrotBundle;
86e67c44 44var gCurrentImageData;
6e98af87
RK
45
46function Startup() {
47 updateIterMenu();
48 updatePaletteMenu();
fa4ecb24
RK
49 gMbrotBundle = document.getElementById("mbrotBundle");
50 document.getElementById("statusLabel").value = gMbrotBundle.getString("statusEmpty");
6e98af87 51}
37b05b56 52
86e67c44
RK
53function adjustCoordsAndDraw(aC_min, aC_max) {
54 let iWidth = 0;
55 try {
56 iWidth = gPref.getIntPref("mandelbrot.image.width");
57 }
58 catch (e) { }
59 if ((iWidth < 10) || (iWidth > 5000)) {
60 iWidth = 300;
61 gPref.setIntPref("mandelbrot.image.width", iWidth);
62 }
63 let iHeight = 0;
64 try {
65 iHeight = gPref.getIntPref("mandelbrot.image.height");
66 }
67 catch (e) { }
68 if ((iHeight < 10) || (iHeight > 5000)) {
69 iHeight = 300;
70 gPref.setIntPref("mandelbrot.image.height", iHeight);
71 }
72
73 // correct coordinates
74 if (aC_min.r < -2)
75 aC_min.r = -2;
76 if (aC_max.r > 2)
77 aC_max.r = 2;
78 if ((aC_min.r > 2) || (aC_max.r < -2) || (aC_min.r >= aC_max.r)) {
79 aC_min.r = -2.0; aC_max.r = 1.0;
80 }
81 if (aC_min.i < -2)
82 aC_min.i = -2;
83 if (aC_max.i > 2)
84 aC_max.i = 2;
85 if ((aC_min.i > 2) || (aC_max.i < -2) || (aC_min.i >= aC_max.i)) {
86 aC_min.i = -1.5; aC_max.i = 1.5;
87 }
88
89 let CWidth = aC_max.r - aC_min.r;
90 let CHeight = aC_max.i - aC_min.i;
91 let C_mid = new complex(aC_min.r + CWidth / 2, aC_min.i + CHeight / 2);
92
93 let CRatio = Math.max(CWidth / iWidth, CHeight / iHeight);
94
95 gPref.setCharPref("mandelbrot.last_image.Cr_min", C_mid.r - iWidth * CRatio / 2);
96 gPref.setCharPref("mandelbrot.last_image.Cr_max", C_mid.r + iWidth * CRatio / 2);
97 gPref.setCharPref("mandelbrot.last_image.Ci_min", C_mid.i - iHeight * CRatio / 2);
98 gPref.setCharPref("mandelbrot.last_image.Ci_max", C_mid.i + iHeight * CRatio / 2);
99
100 drawImage();
101}
102
37b05b56 103function drawImage() {
8a9c8e3f 104 let canvas = document.getElementById("mbrotImage");
2cb9a6b5 105 let context = canvas.getContext("2d");
37b05b56 106
5366c7d6
RK
107 document.getElementById("drawButton").hidden = true;
108
fa4ecb24 109 document.getElementById("statusLabel").value = gMbrotBundle.getString("statusDrawing");
2cb9a6b5 110
eceff1c9
RK
111 let Cr_min = -2.0;
112 let Cr_max = 1.0;
113 try {
114 Cr_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_min"));
115 Cr_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_max"));
116 }
117 catch (e) { }
118 if ((Cr_min < -2) || (Cr_min > 2) ||
119 (Cr_max < -2) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
120 Cr_min = -2.0; Cr_max = 1.0;
121 }
122 gPref.setCharPref("mandelbrot.last_image.Cr_min", Cr_min);
123 gPref.setCharPref("mandelbrot.last_image.Cr_max", Cr_max);
124
125 let Ci_min = -1.5;
126 let Ci_max = 1.5;
127 try {
128 Ci_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_min"));
129 Ci_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_max"));
130 }
131 catch (e) { }
132 if ((Ci_min < -2) || (Ci_min > 2) ||
133 (Ci_max < -2) || (Ci_max > 2) || (Ci_min >= Ci_max)) {
86e67c44 134 Ci_min = -1.5; Ci_max = 1.5;
eceff1c9
RK
135 }
136 gPref.setCharPref("mandelbrot.last_image.Ci_min", Ci_min);
137 gPref.setCharPref("mandelbrot.last_image.Ci_max", Ci_max);
138
2cb9a6b5
RK
139 let iterMax = gPref.getIntPref("mandelbrot.iteration_max");
140 let algorithm = gPref.getCharPref("mandelbrot.use_algorithm");
6e98af87 141
eceff1c9
RK
142 let iWidth = 0;
143 try {
144 iWidth = gPref.getIntPref("mandelbrot.image.width");
145 }
146 catch (e) { }
147 if ((iWidth < 10) || (iWidth > 5000)) {
148 iWidth = 300;
149 gPref.setIntPref("mandelbrot.image.width", iWidth);
150 }
151 let iHeight = 0;
152 try {
153 iHeight = gPref.getIntPref("mandelbrot.image.height");
154 }
155 catch (e) { }
156 if ((iHeight < 10) || (iHeight > 5000)) {
157 iHeight = 300;
158 gPref.setIntPref("mandelbrot.image.height", iHeight);
159 }
160
86e67c44
RK
161 gCurrentImageData = {
162 C_min: new complex(Cr_min, Ci_min),
163 C_max: new complex(Cr_max, Ci_max),
164 iWidth: iWidth,
165 iHeight: iHeight,
166 iterMax: iterMax
167 };
168
eceff1c9
RK
169 canvas.width = iWidth;
170 canvas.height = iHeight;
171
172 context.fillStyle = "rgba(255, 255, 255, 127)";
2cb9a6b5 173 context.fillRect(0, 0, canvas.width, canvas.height);
37b05b56 174
2cb9a6b5
RK
175 gStartTime = new Date();
176
eceff1c9
RK
177 drawLine(0, [Cr_min, Cr_max, Ci_min, Ci_max],
178 canvas, context, iterMax, algorithm);
2cb9a6b5
RK
179}
180
eceff1c9 181function drawLine(line, dimensions, canvas, context, iterMax, algorithm) {
aad35028
RK
182 let Cr_min = dimensions[0];
183 let Cr_max = dimensions[1];
184 let Cr_scale = Cr_max - Cr_min;
185
186 let Ci_min = dimensions[2];
187 let Ci_max = dimensions[3];
188 let Ci_scale = Ci_max - Ci_min;
189
190 let pixels = [];
191 for (var img_y = line; img_y < canvas.height && img_y < line+8; img_y++)
192 for (let img_x = 0; img_x < canvas.width; img_x++) {
193 let C = new complex(Cr_min + (img_x / canvas.width) * Cr_scale,
194 Ci_min + (img_y / canvas.height) * Ci_scale);
195 pixels.push.apply(pixels, drawPoint(context, img_x, img_y, C, iterMax, algorithm));
196 }
197 context.putImageData({width: canvas.width, height: pixels.length/4/canvas.width, data: pixels}, 0, line);
2cb9a6b5 198
aad35028
RK
199 if (img_y < canvas.height)
200 setTimeout(drawLine, 0, img_y, dimensions, canvas, context, iterMax, algorithm);
201 else if (gStartTime)
202 EndCalc();
37b05b56
RK
203}
204
6e98af87
RK
205function EndCalc() {
206 let endTime = new Date();
207 let timeUsed = (endTime.getTime() - gStartTime.getTime()) / 1000;
208 document.getElementById("statusLabel").value =
fa4ecb24 209 gMbrotBundle.getFormattedString("statusTime", [timeUsed.toFixed(3)]);
287a980b 210 gStartTime = 0;
6e98af87
RK
211}
212
37b05b56
RK
213function complex(aReal, aImag) {
214 this.r = aReal;
215 this.i = aImag;
2cb9a6b5
RK
216}
217complex.prototype = {
218 square: function() {
37b05b56
RK
219 return new complex(this.r * this.r - this.i * this.i,
220 2 * this.r * this.i);
2cb9a6b5
RK
221 },
222 dist: function() {
37b05b56 223 return Math.sqrt(this.r * this.r + this.i * this.i);
2cb9a6b5
RK
224 },
225 add: function(aComplex) {
37b05b56
RK
226 return new complex(this.r + aComplex.r, this.i + aComplex.i);
227 }
228}
229
6e98af87
RK
230function mandelbrotValueOO (aC, aIterMax) {
231 // this would be nice code in general but it looks like JS objects are too heavy for normal use.
8444612a 232 let Z = new complex(0.0, 0.0);
37b05b56
RK
233 for (var iter = 0; iter < aIterMax; iter++) {
234 Z = Z.square().add(aC);
235 if (Z.r * Z.r + Z.i * Z.i > 256) { break; }
236 }
6e98af87
RK
237 return iter;
238}
8444612a 239
6e98af87
RK
240function mandelbrotValueNumeric (aC, aIterMax) {
241 // optimized numeric code for fast calculation
8444612a
RK
242 let Cr = aC.r, Ci = aC.i;
243 let Zr = 0.0, Zi = 0.0;
244 let Zr2 = Zr * Zr, Zi2 = Zi * Zi;
245 for (var iter = 0; iter < aIterMax; iter++) {
246 Zi = 2 * Zr * Zi + Ci;
247 Zr = Zr2 - Zi2 + Cr;
248
249 Zr2 = Zr * Zr; Zi2 = Zi * Zi;
250 if (Zr2 + Zi2 > 256) { break; }
251 }
37b05b56
RK
252 return iter;
253}
254
255function getColor(aIterValue, aIterMax) {
8a9c8e3f 256 let standardizedValue = Math.round(aIterValue * 1024 / aIterMax);
6e98af87
RK
257 if (gColorPalette && gColorPalette.length)
258 return gColorPalette[standardizedValue];
259
260 // fallback to simple b/w if for some reason we don't have a palette
261 if (aIterValue == aIterMax)
2cb9a6b5 262 return [0, 0, 0, 255];
6e98af87 263 else
2cb9a6b5 264 return [255, 255, 255, 255];
37b05b56
RK
265}
266
267function getColorPalette(palName) {
268 var palette = [];
269 switch (palName) {
270 case 'bw':
8a9c8e3f 271 for (let i = 0; i < 1024; i++) {
2cb9a6b5 272 palette[i] = [255, 255, 255, 255];
37b05b56 273 }
2cb9a6b5 274 palette[1024] = [0, 0, 0, 255];
37b05b56
RK
275 break;
276 case 'kairo':
277 // outer areas
8a9c8e3f
RK
278 for (let i = 0; i < 32; i++) {
279 let cc1 = Math.floor(i * 127 / 31);
280 let cc2 = 170 - Math.floor(i * 43 / 31);
2cb9a6b5 281 palette[i] = [cc1, cc2, cc1, 255];
37b05b56
RK
282 }
283 // inner areas
8a9c8e3f
RK
284 for (let i = 0; i < 51; i++) {
285 let cc = Math.floor(i * 170 / 50);
2cb9a6b5 286 palette[32 + i] = [cc, 0, (170-cc), 255];
37b05b56
RK
287 }
288 // corona
8a9c8e3f
RK
289 for (let i = 0; i < 101; i++) {
290 let cc = Math.floor(i * 200 / 100);
2cb9a6b5 291 palette[83 + i] = [255, cc, 0, 255];
37b05b56
RK
292 }
293 // inner corona
8a9c8e3f
RK
294 for (let i = 0; i < 201; i++) {
295 let cc1 = 255 - Math.floor(i * 85 / 200);
296 let cc2 = 200 - Math.floor(i * 30 / 200);
297 let cc3 = Math.floor(i * 170 / 200);
2cb9a6b5 298 palette[184 + i] = [cc1, cc2, cc3, 255];
37b05b56 299 }
8a9c8e3f
RK
300 for (let i = 0; i < 301; i++) {
301 let cc1 = 170 - Math.floor(i * 43 / 300);
302 let cc2 = 170 + Math.floor(i * 85 / 300);
2cb9a6b5 303 palette[385 + i] = [cc1, cc1, cc2, 255];
37b05b56 304 }
8a9c8e3f
RK
305 for (let i = 0; i < 338; i++) {
306 let cc = 127 + Math.floor(i * 128 / 337);
2cb9a6b5 307 palette[686 + i] = [cc, cc, 255, 255];
37b05b56 308 }
2cb9a6b5 309 palette[1024] = [0, 0, 0, 255];
37b05b56
RK
310 break;
311 case 'rainbow-linear1':
8a9c8e3f 312 for (let i = 0; i < 256; i++) {
2cb9a6b5
RK
313 palette[i] = [i, 0, 0, 255];
314 palette[256 + i] = [255, i, 0, 255];
315 palette[512 + i] = [255 - i, 255, i, 255];
316 palette[768 + i] = [i, 255-i, 255, 255];
37b05b56 317 }
2cb9a6b5 318 palette[1024] = [0, 0, 0, 255];
37b05b56 319 break;
72eff464
RK
320 case 'rainbow-squared1':
321 for (let i = 0; i < 34; i++) {
322 let cc = Math.floor(i * 255 / 33);
323 palette[i] = [cc, 0, 0, 255];
324 }
325 for (let i = 0; i < 137; i++) {
326 let cc = Math.floor(i * 255 / 136);
327 palette[34 + i] = [255, cc, 0, 255];
328 }
329 for (let i = 0; i < 307; i++) {
330 let cc = Math.floor(i * 255 / 306);
331 palette[171 + i] = [255 - cc, 255, cc, 255];
332 }
333 for (let i = 0; i < 546; i++) {
334 let cc = Math.floor(i * 255 / 545);
335 palette[478 + i] = [cc, 255 - cc, 255, 255];
336 }
337 palette[1024] = [0, 0, 0, 255];
338 break;
339 case 'rainbow-linear2':
340 for (let i = 0; i < 205; i++) {
341 let cc = Math.floor(i * 255 / 204);
342 palette[i] = [255, cc, 0, 255];
343 palette[204 + i] = [255 - cc, 255, 0, 255];
344 palette[409 + i] = [0, 255, cc, 255];
345 palette[614 + i] = [0, 255 - cc, 255, 255];
346 palette[819 + i] = [cc, 0, 255, 255];
347 }
348 palette[1024] = [0, 0, 0, 255];
349 break;
350 case 'rainbow-squared2':
351 for (let i = 0; i < 19; i++) {
352 let cc = Math.floor(i * 255 / 18);
353 palette[i] = [255, cc, 0, 255];
354 }
355 for (let i = 0; i < 74; i++) {
356 let cc = Math.floor(i * 255 / 73);
357 palette[19 + i] = [255 - cc, 255, 0, 255];
358 }
359 for (let i = 0; i < 168; i++) {
360 let cc = Math.floor(i * 255 / 167);
361 palette[93 + i] = [0, 255, cc, 255];
362 }
363 for (let i = 0; i < 298; i++) {
364 let cc = Math.floor(i * 255 / 297);
365 palette[261 + i] = [0, 255 - cc, 255, 255];
366 }
367 for (let i = 0; i < 465; i++) {
368 let cc = Math.floor(i * 255 / 464);
369 palette[559 + i] = [cc, 0, 255, 255];
370 }
371 palette[1024] = [0, 0, 0, 255];
372 break;
37b05b56 373 }
72eff464
RK
374 /*
375 'Standard-Palette (QB-Colors)
37b05b56
RK
376 For i = 0 To 1024
377 xx = CInt(i * 500 / 1024 + 2)
378 If xx <= 15 Then clr = xx
379 If xx > 15 Then clr = CInt(Sqr((xx - 15 + 1) * 15 ^ 2 / 485))
380 If xx >= 500 Then clr = 0
381 palette(i) = QBColor(clr)
382 Next
72eff464 383 */
37b05b56
RK
384 return palette;
385}
386
6e98af87
RK
387function drawPoint(context, img_x, img_y, C, iterMax, algorithm) {
388 var itVal;
389 switch (algorithm) {
390 case 'oo':
391 itVal = mandelbrotValueOO(C, iterMax);
392 break;
393 case 'numeric':
394 default:
395 itVal = mandelbrotValueNumeric(C, iterMax);
396 break;
397 }
2cb9a6b5 398 return getColor(itVal, iterMax);
37b05b56
RK
399}
400
6e98af87
RK
401/***** pure UI functions *****/
402
4d8e7dcb
RK
403var zoomstart;
404
405function mouseevent(etype, event) {
406 let canvas = document.getElementById("mbrotImage");
407 switch (etype) {
408 case 'down':
409 if (event.button == 0)
410 // left button - start dragzoom
411 zoomstart = {x: event.clientX - canvas.offsetLeft,
412 y: event.clientY - canvas.offsetTop};
413 break;
414 case 'up':
aad35028 415 if (event.button == 0 && zoomstart) {
86e67c44
RK
416 let zoomend = {x: event.clientX - canvas.offsetLeft,
417 y: event.clientY - canvas.offsetTop};
418
419 // make sure zoomend is bigger than zoomstart
420 if ((zoomend.x == zoomstart.x) || (zoomend.y == zoomstart.y)) {
421 // cannot zoom what has no area, discard it
422 zoomstart = undefined;
423 return;
424 }
425 if (zoomend.x < zoomstart.x)
426 [zoomend.x, zoomstart.x] = [zoomstart.x, zoomend.x];
427 if (zoomend.y < zoomstart.y)
428 [zoomend.y, zoomstart.y] = [zoomstart.y, zoomend.y];
429
aad35028 430 let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
9c2ca9fa 431 .getService(Components.interfaces.nsIPromptService);
aad35028 432 let ok = prompts.confirm(null, gMbrotBundle.getString("zoomConfirmTitle"),
86e67c44 433 gMbrotBundle.getString("zoomConfirmLabel"));
9c2ca9fa 434 // ok is now true if OK was clicked, and false if cancel was clicked
86e67c44
RK
435 if (ok) {
436 // determine new "coordinates"
437 let CWidth = gCurrentImageData.C_max.r - gCurrentImageData.C_min.r;
438 let CHeight = gCurrentImageData.C_max.i - gCurrentImageData.C_min.i;
439 let newC_min = new complex(
440 gCurrentImageData.C_min.r + zoomstart.x / gCurrentImageData.iWidth * CWidth,
441 gCurrentImageData.C_min.i + zoomstart.y / gCurrentImageData.iHeight * CHeight);
442 let newC_max = new complex(
443 gCurrentImageData.C_min.r + zoomend.x / gCurrentImageData.iWidth * CWidth,
444 gCurrentImageData.C_min.i + zoomend.y / gCurrentImageData.iHeight * CHeight);
445
446 adjustCoordsAndDraw(newC_min, newC_max);
447 }
9c2ca9fa 448 }
4d8e7dcb
RK
449 zoomstart = undefined;
450 break;
451 }
452}
453
37b05b56 454function saveImage() {
740b86d1 455 const nsIFilePicker = Components.interfaces.nsIFilePicker;
aad35028 456 let fp = null;
740b86d1
RK
457 try {
458 fp = Components.classes["@mozilla.org/filepicker;1"]
459 .createInstance(nsIFilePicker);
460 } catch (e) {}
461 if (!fp) return;
aad35028 462 let promptString = gMbrotBundle.getString("savePrompt");
740b86d1 463 fp.init(window, promptString, nsIFilePicker.modeSave);
fa4ecb24 464 fp.appendFilter(gMbrotBundle.getString("pngFilterName"), "*.png");
740b86d1
RK
465 fp.defaultString = "mandelbrot.png";
466
aad35028 467 let fpResult = fp.show();
740b86d1
RK
468 if (fpResult != nsIFilePicker.returnCancel) {
469 saveCanvas(document.getElementById("mbrotImage"), fp.file);
470 }
37b05b56
RK
471}
472
287a980b
RK
473function updateBookmarkMenu(aParent) {
474 document.getElementById("bookmarkSave").disabled =
475 (!document.getElementById("drawButton").hidden || (gStartTime > 0));
476
477 while (aParent.hasChildNodes() &&
86e67c44 478 aParent.lastChild.id != "bookmarkSeparator")
287a980b
RK
479 aParent.removeChild(aParent.lastChild);
480
aad35028 481 let file = Components.classes["@mozilla.org/file/directory_service;1"]
287a980b
RK
482 .getService(Components.interfaces.nsIProperties)
483 .get("ProfD", Components.interfaces.nsIFile);
484 file.append("mandelbookmarks.sqlite");
485 if (file.exists()) {
aad35028 486 let connection = Components.classes["@mozilla.org/storage/service;1"]
287a980b
RK
487 .getService(Components.interfaces.mozIStorageService)
488 .openDatabase(file);
489 try {
490 if (connection.tableExists("bookmarks")) {
aad35028 491 let statement = connection.createStatement(
86e67c44
RK
492 "SELECT name,ROWID FROM bookmarks ORDER BY ROWID ASC");
493 while (statement.executeStep()) {
494 let newItem = aParent.appendChild(document.createElement("menuitem"));
495 newItem.setAttribute("label", statement.getString(0));
496 newItem.setAttribute("bmRowID", statement.getString(1));
497 }
287a980b
RK
498 statement.reset();
499 statement.finalize();
500 return;
2eed6617 501 }
287a980b
RK
502 } finally {
503 connection.close();
2eed6617 504 }
287a980b
RK
505 }
506 // Create the "Nothing Available" Menu item and disable it.
aad35028 507 let na = aParent.appendChild(document.createElement("menuitem"));
fa4ecb24 508 na.setAttribute("label", gMbrotBundle.getString("noBookmarks"));
287a980b 509 na.setAttribute("disabled", "true");
2eed6617
RK
510}
511
512function callBookmark(evtarget) {
86e67c44 513 if (evtarget.id == "bookmarkSave" || evtarget.id == "bookmarkSeparator")
b8f9a76f
RK
514 return;
515 if (evtarget.id == "bookmarkOverview") {
516 adjustCoordsAndDraw(new complex(0,0), new complex(0,0));
517 return;
518 }
86e67c44
RK
519 alert(evtarget.getAttribute('label') + ', ' + evtarget.getAttribute('bmRowID'));
520 //gPref.setIntPref("mandelbrot.iteration_max", iterMax);
2eed6617
RK
521}
522
523function saveBookmark() {
9c2ca9fa 524 // retrieve wanted bookmark name with a prompt
aad35028 525 let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
9c2ca9fa 526 .getService(Components.interfaces.nsIPromptService);
aad35028
RK
527 let input = {value: ""}; // empty default value
528 let ok = prompts.prompt(null, gMbrotBundle.getString("saveBookmarkTitle"), gMbrotBundle.getString("saveBookmarkLabel"), input, null, {});
9c2ca9fa
RK
529 // ok is true if OK is pressed, false if Cancel. input.value holds the value of the edit field if "OK" was pressed.
530 if (!ok || !input.value)
531 return
532
aad35028 533 let bmName = input.value;
287a980b
RK
534
535 // Open or create the bookmarks database.
aad35028 536 let file = Components.classes["@mozilla.org/file/directory_service;1"]
287a980b
RK
537 .getService(Components.interfaces.nsIProperties)
538 .get("ProfD", Components.interfaces.nsIFile);
539 file.append("mandelbookmarks.sqlite");
aad35028 540 let connection = Components.classes["@mozilla.org/storage/service;1"]
287a980b
RK
541 .getService(Components.interfaces.mozIStorageService)
542 .openDatabase(file);
543 connection.beginTransaction();
544 if (!connection.tableExists("bookmarks"))
545 connection.createTable("bookmarks", "name TEXT, iteration_max INTEGER, Cr_min REAL, Cr_max REAL, Ci_min REAL, Ci_max REAL");
546 // NULL. The value is a NULL value.
547 // INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
548 // REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
549 // TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16-LE).
550
551 // Put value of the current image into the bookmarks table
aad35028 552 let statement = connection.createStatement(
287a980b
RK
553 "INSERT INTO bookmarks (name,iteration_max,Cr_min,Cr_max,Ci_min,Ci_max) VALUES (?1,?2,?3,?4,?5,?6)");
554 statement.bindStringParameter(0, bmName);
86e67c44
RK
555 statement.bindStringParameter(1, gCurrentImageData.iterMax);
556 statement.bindStringParameter(2, gCurrentImageData.C_min.r);
557 statement.bindStringParameter(3, gCurrentImageData.C_max.r);
558 statement.bindStringParameter(4, gCurrentImageData.C_min.i);
559 statement.bindStringParameter(5, gCurrentImageData.C_max.i);
287a980b
RK
560 statement.execute();
561 statement.finalize();
562 connection.commitTransaction();
563 connection.close();
2eed6617
RK
564}
565
6e98af87 566function updateIterMenu() {
aad35028 567 let currentIter = 0;
6e98af87 568 try {
aad35028 569 currentIter = gPref.getIntPref("mandelbrot.iteration_max");
6e98af87 570 }
aad35028 571 catch(e) { }
6e98af87
RK
572 if (currentIter < 10) {
573 currentIter = 500;
574 setIter(currentIter);
575 }
576
aad35028
RK
577 let popup = document.getElementById("menu_iterPopup");
578 let item = popup.firstChild;
6e98af87
RK
579 while (item) {
580 if (item.getAttribute("name") == "iter") {
581 if (item.getAttribute("value") == currentIter)
582 item.setAttribute("checked","true");
583 else
584 item.removeAttribute("checked");
585 }
586 item = item.nextSibling;
587 }
588}
589
590function setIter(aIter) {
591 gPref.setIntPref("mandelbrot.iteration_max", aIter);
592}
593
594function updatePaletteMenu() {
aad35028 595 let currentPalette = '';
6e98af87 596 try {
aad35028 597 currentPalette = gPref.getCharPref("mandelbrot.color_palette");
6e98af87 598 }
aad35028 599 catch(e) { }
6e98af87
RK
600 if (!currentPalette.length) {
601 currentPalette = 'kairo';
602 setPalette(currentPalette);
603 }
604 if (!gColorPalette || !gColorPalette.length)
605 gColorPalette = getColorPalette(currentPalette);
606
aad35028
RK
607 let popup = document.getElementById("menu_palettePopup");
608 let item = popup.firstChild;
6e98af87
RK
609 while (item) {
610 if (item.getAttribute("name") == "palette") {
611 if (item.getAttribute("value") == currentPalette)
612 item.setAttribute("checked", "true");
613 else
614 item.removeAttribute("checked");
615 }
616 item = item.nextSibling;
617 }
618}
619
620function setPalette(aPaletteID) {
621 gPref.setCharPref("mandelbrot.color_palette", aPaletteID);
622 gColorPalette = getColorPalette(aPaletteID);
623}
624
6403d662
RK
625function imgSettings() {
626 window.openDialog("chrome://mandelbrot/content/image-settings.xul");
627}
628
6e98af87
RK
629function updateDebugMenu() {
630 var jitMenuItem = document.getElementById("jitEnabled");
631 jitMenuItem.setAttribute("checked", gPref.getBoolPref("javascript.options.jit.chrome"));
632}
633
634function toggleJITState(jitMenuItem) {
635 var jitEnabled = !gPref.getBoolPref("javascript.options.jit.chrome");
636 gPref.setBoolPref("javascript.options.jit.chrome", jitEnabled)
637 jitMenuItem.setAttribute("checked", jitEnabled? "true" : "false");
638}
639
640function updateAlgoMenu() {
aad35028 641 let currentAlgo = '';
6e98af87 642 try {
aad35028 643 currentAlgo = gPref.getCharPref("mandelbrot.use_algorithm");
6e98af87 644 }
aad35028 645 catch(e) { }
6e98af87
RK
646 if (!currentAlgo.length) {
647 currentAlgo = 'numeric';
648 setAlgorithm(currentAlgo);
649 }
650
aad35028
RK
651 let popup = document.getElementById("menu_algoPopup");
652 let item = popup.firstChild;
6e98af87
RK
653 while (item) {
654 if (item.getAttribute("name") == "algorithm") {
655 if (item.getAttribute("value") == currentAlgo)
656 item.setAttribute("checked", "true");
657 else
658 item.removeAttribute("checked");
659 }
660 item = item.nextSibling;
661 }
662}
663
664function setAlgorithm(algoID) {
665 gPref.setCharPref("mandelbrot.use_algorithm", algoID);
666}
667
af3c147c 668function addonsManager(aPane) {
aad35028 669 let theEM = Components.classes["@mozilla.org/appshell/window-mediator;1"]
af3c147c
RK
670 .getService(Components.interfaces.nsIWindowMediator)
671 .getMostRecentWindow("Extension:Manager");
672 if (theEM) {
673 theEM.focus();
674 if (aPane)
675 theEM.showView(aPane);
676 return;
677 }
678
679 const EMURL = "chrome://mozapps/content/extensions/extensions.xul";
680 const EMFEATURES = "all,dialog=no";
681 if (aPane)
682 window.openDialog(EMURL, "", EMFEATURES, aPane);
683 else
684 window.openDialog(EMURL, "", EMFEATURES);
685}
686
687function errorConsole() {
688 toOpenWindowByType("global:console", "chrome://global/content/console.xul");
689}
690
6e98af87
RK
691/***** helper functions from external sources *****/
692
740b86d1
RK
693// function below is based on http://developer.mozilla.org/en/docs/Code_snippets:Canvas
694// custom modifications:
695// - use "a"-prefix on function arguments
696// - take an nsILocalFile as aDestFile argument
697// - always do silent download
698function saveCanvas(aCanvas, aDestFile) {
37b05b56
RK
699 // create a data url from the canvas and then create URIs of the source and targets
700 var io = Components.classes["@mozilla.org/network/io-service;1"]
701 .getService(Components.interfaces.nsIIOService);
740b86d1 702 var source = io.newURI(aCanvas.toDataURL("image/png", ""), "UTF8", null);
37b05b56
RK
703
704 // prepare to save the canvas data
705 var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
706 .createInstance(Components.interfaces.nsIWebBrowserPersist);
707
708 persist.persistFlags = Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
709 persist.persistFlags |= Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
710
37b05b56 711 // save the canvas data to the file
740b86d1 712 persist.saveURI(source, null, null, null, null, aDestFile);
37b05b56
RK
713}
714
715// function below is from http://developer.mozilla.org/en/docs/How_to_Quit_a_XUL_Application
716function quitApp(aForceQuit) {
717 var appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1']
718 .getService(Components.interfaces.nsIAppStartup);
719
720 // eAttemptQuit will try to close each XUL window, but the XUL window can cancel the quit
721 // process if there is unsaved data. eForceQuit will quit no matter what.
722 var quitSeverity = aForceQuit ? Components.interfaces.nsIAppStartup.eForceQuit :
723 Components.interfaces.nsIAppStartup.eAttemptQuit;
724 appStartup.quit(quitSeverity);
725}
af3c147c
RK
726
727// functions below are from comm-central/suite/common/tasksOverlay.js
728function toOpenWindow(aWindow) {
729 try {
730 // Try to focus the previously focused window e.g. message compose body
731 aWindow.document.commandDispatcher.focusedWindow.focus();
732 } catch (e) {
733 // e.g. full-page plugin or non-XUL document; just raise the top window
734 aWindow.focus();
735 }
736}
737
738function toOpenWindowByType(inType, uri, features) {
739 // don't do several loads in parallel
740 if (uri in window)
741 return;
742
743 var topWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"]
744 .getService(Components.interfaces.nsIWindowMediator)
745 .getMostRecentWindow(inType);
746 if ( topWindow )
747 toOpenWindow( topWindow );
748 else {
749 // open the requested window, but block it until it's fully loaded
750 function newWindowLoaded(event) {
751 // make sure that this handler is called only once
752 window.removeEventListener("unload", newWindowLoaded, false);
753 window[uri].removeEventListener("load", newWindowLoaded, false);
754 delete window[uri];
755 }
756 // remember the newly loading window until it's fully loaded
757 // or until the current window passes away
758 window[uri] = window.openDialog(uri, "", features || "all,dialog=no");
759 window[uri].addEventListener("load", newWindowLoaded, false);
760 window.addEventListener("unload", newWindowLoaded, false);
761 }
762}