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