Commit | Line | Data |
---|---|---|
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 |
38 | var gColorPalette = []; |
39 | var gPref = Components.classes["@mozilla.org/preferences-service;1"] | |
40 | .getService(Components.interfaces.nsIPrefService) | |
41 | .getBranch(null); | |
42 | var gStartTime = 0; | |
fa4ecb24 | 43 | var gMbrotBundle; |
6e98af87 RK |
44 | |
45 | function Startup() { | |
46 | updateIterMenu(); | |
47 | updatePaletteMenu(); | |
fa4ecb24 RK |
48 | gMbrotBundle = document.getElementById("mbrotBundle"); |
49 | document.getElementById("statusLabel").value = gMbrotBundle.getString("statusEmpty"); | |
6e98af87 | 50 | } |
37b05b56 RK |
51 | |
52 | function drawImage() { | |
8a9c8e3f | 53 | let canvas = document.getElementById("mbrotImage"); |
2cb9a6b5 | 54 | let context = canvas.getContext("2d"); |
37b05b56 | 55 | |
5366c7d6 RK |
56 | document.getElementById("drawButton").hidden = true; |
57 | ||
fa4ecb24 | 58 | document.getElementById("statusLabel").value = gMbrotBundle.getString("statusDrawing"); |
2cb9a6b5 | 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 | 122 | function drawLine(line, dimensions, canvas, context, iterMax, algorithm) { |
aad35028 RK |
123 | let Cr_min = dimensions[0]; |
124 | let Cr_max = dimensions[1]; | |
125 | let Cr_scale = Cr_max - Cr_min; | |
126 | ||
127 | let Ci_min = dimensions[2]; | |
128 | let Ci_max = dimensions[3]; | |
129 | let Ci_scale = Ci_max - Ci_min; | |
130 | ||
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++) { | |
134 | let C = new complex(Cr_min + (img_x / canvas.width) * Cr_scale, | |
135 | Ci_min + (img_y / canvas.height) * Ci_scale); | |
136 | pixels.push.apply(pixels, drawPoint(context, img_x, img_y, C, iterMax, algorithm)); | |
137 | } | |
138 | context.putImageData({width: canvas.width, height: pixels.length/4/canvas.width, data: pixels}, 0, line); | |
2cb9a6b5 | 139 | |
aad35028 RK |
140 | if (img_y < canvas.height) |
141 | setTimeout(drawLine, 0, img_y, dimensions, canvas, context, iterMax, algorithm); | |
142 | else if (gStartTime) | |
143 | EndCalc(); | |
37b05b56 RK |
144 | } |
145 | ||
6e98af87 RK |
146 | function EndCalc() { |
147 | let endTime = new Date(); | |
148 | let timeUsed = (endTime.getTime() - gStartTime.getTime()) / 1000; | |
149 | document.getElementById("statusLabel").value = | |
fa4ecb24 | 150 | gMbrotBundle.getFormattedString("statusTime", [timeUsed.toFixed(3)]); |
287a980b | 151 | gStartTime = 0; |
6e98af87 RK |
152 | } |
153 | ||
37b05b56 RK |
154 | function complex(aReal, aImag) { |
155 | this.r = aReal; | |
156 | this.i = aImag; | |
2cb9a6b5 RK |
157 | } |
158 | complex.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 |
171 | function 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 |
181 | function 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 | ||
196 | function 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 | ||
208 | function 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 | 260 | break; |
72eff464 RK |
261 | case 'rainbow-squared1': |
262 | for (let i = 0; i < 34; i++) { | |
263 | let cc = Math.floor(i * 255 / 33); | |
264 | palette[i] = [cc, 0, 0, 255]; | |
265 | } | |
266 | for (let i = 0; i < 137; i++) { | |
267 | let cc = Math.floor(i * 255 / 136); | |
268 | palette[34 + i] = [255, cc, 0, 255]; | |
269 | } | |
270 | for (let i = 0; i < 307; i++) { | |
271 | let cc = Math.floor(i * 255 / 306); | |
272 | palette[171 + i] = [255 - cc, 255, cc, 255]; | |
273 | } | |
274 | for (let i = 0; i < 546; i++) { | |
275 | let cc = Math.floor(i * 255 / 545); | |
276 | palette[478 + i] = [cc, 255 - cc, 255, 255]; | |
277 | } | |
278 | palette[1024] = [0, 0, 0, 255]; | |
279 | break; | |
280 | case 'rainbow-linear2': | |
281 | for (let i = 0; i < 205; i++) { | |
282 | let cc = Math.floor(i * 255 / 204); | |
283 | palette[i] = [255, cc, 0, 255]; | |
284 | palette[204 + i] = [255 - cc, 255, 0, 255]; | |
285 | palette[409 + i] = [0, 255, cc, 255]; | |
286 | palette[614 + i] = [0, 255 - cc, 255, 255]; | |
287 | palette[819 + i] = [cc, 0, 255, 255]; | |
288 | } | |
289 | palette[1024] = [0, 0, 0, 255]; | |
290 | break; | |
291 | case 'rainbow-squared2': | |
292 | for (let i = 0; i < 19; i++) { | |
293 | let cc = Math.floor(i * 255 / 18); | |
294 | palette[i] = [255, cc, 0, 255]; | |
295 | } | |
296 | for (let i = 0; i < 74; i++) { | |
297 | let cc = Math.floor(i * 255 / 73); | |
298 | palette[19 + i] = [255 - cc, 255, 0, 255]; | |
299 | } | |
300 | for (let i = 0; i < 168; i++) { | |
301 | let cc = Math.floor(i * 255 / 167); | |
302 | palette[93 + i] = [0, 255, cc, 255]; | |
303 | } | |
304 | for (let i = 0; i < 298; i++) { | |
305 | let cc = Math.floor(i * 255 / 297); | |
306 | palette[261 + i] = [0, 255 - cc, 255, 255]; | |
307 | } | |
308 | for (let i = 0; i < 465; i++) { | |
309 | let cc = Math.floor(i * 255 / 464); | |
310 | palette[559 + i] = [cc, 0, 255, 255]; | |
311 | } | |
312 | palette[1024] = [0, 0, 0, 255]; | |
313 | break; | |
37b05b56 | 314 | } |
72eff464 RK |
315 | /* |
316 | 'Standard-Palette (QB-Colors) | |
37b05b56 RK |
317 | For i = 0 To 1024 |
318 | xx = CInt(i * 500 / 1024 + 2) | |
319 | If xx <= 15 Then clr = xx | |
320 | If xx > 15 Then clr = CInt(Sqr((xx - 15 + 1) * 15 ^ 2 / 485)) | |
321 | If xx >= 500 Then clr = 0 | |
322 | palette(i) = QBColor(clr) | |
323 | Next | |
72eff464 | 324 | */ |
37b05b56 RK |
325 | return palette; |
326 | } | |
327 | ||
6e98af87 RK |
328 | function drawPoint(context, img_x, img_y, C, iterMax, algorithm) { |
329 | var itVal; | |
330 | switch (algorithm) { | |
331 | case 'oo': | |
332 | itVal = mandelbrotValueOO(C, iterMax); | |
333 | break; | |
334 | case 'numeric': | |
335 | default: | |
336 | itVal = mandelbrotValueNumeric(C, iterMax); | |
337 | break; | |
338 | } | |
2cb9a6b5 | 339 | return getColor(itVal, iterMax); |
37b05b56 RK |
340 | } |
341 | ||
6e98af87 RK |
342 | /***** pure UI functions *****/ |
343 | ||
4d8e7dcb RK |
344 | var zoomstart; |
345 | ||
346 | function mouseevent(etype, event) { | |
347 | let canvas = document.getElementById("mbrotImage"); | |
348 | switch (etype) { | |
349 | case 'down': | |
350 | if (event.button == 0) | |
351 | // left button - start dragzoom | |
352 | zoomstart = {x: event.clientX - canvas.offsetLeft, | |
353 | y: event.clientY - canvas.offsetTop}; | |
354 | break; | |
355 | case 'up': | |
aad35028 RK |
356 | if (event.button == 0 && zoomstart) { |
357 | let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"] | |
9c2ca9fa | 358 | .getService(Components.interfaces.nsIPromptService); |
aad35028 | 359 | let ok = prompts.confirm(null, gMbrotBundle.getString("zoomConfirmTitle"), |
fa4ecb24 | 360 | gMbrotBundle.getString("zoomConfirmLabel") + ' --- ' + |
9c2ca9fa RK |
361 | zoomstart.x + ',' + zoomstart.y + '-' + |
362 | (event.clientX - canvas.offsetLeft) + ',' + | |
363 | (event.clientY - canvas.offsetTop)); | |
364 | // ok is now true if OK was clicked, and false if cancel was clicked | |
365 | } | |
4d8e7dcb RK |
366 | zoomstart = undefined; |
367 | break; | |
368 | } | |
369 | } | |
370 | ||
37b05b56 | 371 | function saveImage() { |
740b86d1 | 372 | const nsIFilePicker = Components.interfaces.nsIFilePicker; |
aad35028 | 373 | let fp = null; |
740b86d1 RK |
374 | try { |
375 | fp = Components.classes["@mozilla.org/filepicker;1"] | |
376 | .createInstance(nsIFilePicker); | |
377 | } catch (e) {} | |
378 | if (!fp) return; | |
aad35028 | 379 | let promptString = gMbrotBundle.getString("savePrompt"); |
740b86d1 | 380 | fp.init(window, promptString, nsIFilePicker.modeSave); |
fa4ecb24 | 381 | fp.appendFilter(gMbrotBundle.getString("pngFilterName"), "*.png"); |
740b86d1 RK |
382 | fp.defaultString = "mandelbrot.png"; |
383 | ||
aad35028 | 384 | let fpResult = fp.show(); |
740b86d1 RK |
385 | if (fpResult != nsIFilePicker.returnCancel) { |
386 | saveCanvas(document.getElementById("mbrotImage"), fp.file); | |
387 | } | |
37b05b56 RK |
388 | } |
389 | ||
287a980b RK |
390 | function updateBookmarkMenu(aParent) { |
391 | document.getElementById("bookmarkSave").disabled = | |
392 | (!document.getElementById("drawButton").hidden || (gStartTime > 0)); | |
393 | ||
394 | while (aParent.hasChildNodes() && | |
395 | aParent.lastChild.id != 'bookmarkSeparator') | |
396 | aParent.removeChild(aParent.lastChild); | |
397 | ||
aad35028 | 398 | let file = Components.classes["@mozilla.org/file/directory_service;1"] |
287a980b RK |
399 | .getService(Components.interfaces.nsIProperties) |
400 | .get("ProfD", Components.interfaces.nsIFile); | |
401 | file.append("mandelbookmarks.sqlite"); | |
402 | if (file.exists()) { | |
aad35028 | 403 | let connection = Components.classes["@mozilla.org/storage/service;1"] |
287a980b RK |
404 | .getService(Components.interfaces.mozIStorageService) |
405 | .openDatabase(file); | |
406 | try { | |
407 | if (connection.tableExists("bookmarks")) { | |
aad35028 | 408 | let statement = connection.createStatement( |
287a980b RK |
409 | "SELECT name FROM bookmarks ORDER BY ROWID DESC"); |
410 | while (statement.executeStep()) | |
411 | aParent.appendChild(document.createElement("menuitem")) | |
412 | .setAttribute("label", statement.getString(0)); | |
413 | statement.reset(); | |
414 | statement.finalize(); | |
415 | return; | |
2eed6617 | 416 | } |
287a980b RK |
417 | } finally { |
418 | connection.close(); | |
2eed6617 | 419 | } |
287a980b RK |
420 | } |
421 | // Create the "Nothing Available" Menu item and disable it. | |
aad35028 | 422 | let na = aParent.appendChild(document.createElement("menuitem")); |
fa4ecb24 | 423 | na.setAttribute("label", gMbrotBundle.getString("noBookmarks")); |
287a980b | 424 | na.setAttribute("disabled", "true"); |
2eed6617 RK |
425 | } |
426 | ||
427 | function callBookmark(evtarget) { | |
428 | } | |
429 | ||
430 | function saveBookmark() { | |
9c2ca9fa | 431 | // retrieve wanted bookmark name with a prompt |
aad35028 | 432 | let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"] |
9c2ca9fa | 433 | .getService(Components.interfaces.nsIPromptService); |
aad35028 RK |
434 | let input = {value: ""}; // empty default value |
435 | let ok = prompts.prompt(null, gMbrotBundle.getString("saveBookmarkTitle"), gMbrotBundle.getString("saveBookmarkLabel"), input, null, {}); | |
9c2ca9fa RK |
436 | // ok is true if OK is pressed, false if Cancel. input.value holds the value of the edit field if "OK" was pressed. |
437 | if (!ok || !input.value) | |
438 | return | |
439 | ||
aad35028 | 440 | let bmName = input.value; |
287a980b RK |
441 | |
442 | // Open or create the bookmarks database. | |
aad35028 | 443 | let file = Components.classes["@mozilla.org/file/directory_service;1"] |
287a980b RK |
444 | .getService(Components.interfaces.nsIProperties) |
445 | .get("ProfD", Components.interfaces.nsIFile); | |
446 | file.append("mandelbookmarks.sqlite"); | |
aad35028 | 447 | let connection = Components.classes["@mozilla.org/storage/service;1"] |
287a980b RK |
448 | .getService(Components.interfaces.mozIStorageService) |
449 | .openDatabase(file); | |
450 | connection.beginTransaction(); | |
451 | if (!connection.tableExists("bookmarks")) | |
452 | connection.createTable("bookmarks", "name TEXT, iteration_max INTEGER, Cr_min REAL, Cr_max REAL, Ci_min REAL, Ci_max REAL"); | |
453 | // NULL. The value is a NULL value. | |
454 | // INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value. | |
455 | // REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number. | |
456 | // TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16-LE). | |
457 | ||
458 | // Put value of the current image into the bookmarks table | |
aad35028 | 459 | let statement = connection.createStatement( |
287a980b RK |
460 | "INSERT INTO bookmarks (name,iteration_max,Cr_min,Cr_max,Ci_min,Ci_max) VALUES (?1,?2,?3,?4,?5,?6)"); |
461 | statement.bindStringParameter(0, bmName); | |
462 | statement.bindStringParameter(1, gPref.getIntPref("mandelbrot.iteration_max")); | |
463 | statement.bindStringParameter(2, parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_min"))); | |
464 | statement.bindStringParameter(3, parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_max"))); | |
465 | statement.bindStringParameter(4, parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_min"))); | |
466 | statement.bindStringParameter(5, parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_max"))); | |
467 | statement.execute(); | |
468 | statement.finalize(); | |
469 | connection.commitTransaction(); | |
470 | connection.close(); | |
2eed6617 RK |
471 | } |
472 | ||
6e98af87 | 473 | function updateIterMenu() { |
aad35028 | 474 | let currentIter = 0; |
6e98af87 | 475 | try { |
aad35028 | 476 | currentIter = gPref.getIntPref("mandelbrot.iteration_max"); |
6e98af87 | 477 | } |
aad35028 | 478 | catch(e) { } |
6e98af87 RK |
479 | if (currentIter < 10) { |
480 | currentIter = 500; | |
481 | setIter(currentIter); | |
482 | } | |
483 | ||
aad35028 RK |
484 | let popup = document.getElementById("menu_iterPopup"); |
485 | let item = popup.firstChild; | |
6e98af87 RK |
486 | while (item) { |
487 | if (item.getAttribute("name") == "iter") { | |
488 | if (item.getAttribute("value") == currentIter) | |
489 | item.setAttribute("checked","true"); | |
490 | else | |
491 | item.removeAttribute("checked"); | |
492 | } | |
493 | item = item.nextSibling; | |
494 | } | |
495 | } | |
496 | ||
497 | function setIter(aIter) { | |
498 | gPref.setIntPref("mandelbrot.iteration_max", aIter); | |
499 | } | |
500 | ||
501 | function updatePaletteMenu() { | |
aad35028 | 502 | let currentPalette = ''; |
6e98af87 | 503 | try { |
aad35028 | 504 | currentPalette = gPref.getCharPref("mandelbrot.color_palette"); |
6e98af87 | 505 | } |
aad35028 | 506 | catch(e) { } |
6e98af87 RK |
507 | if (!currentPalette.length) { |
508 | currentPalette = 'kairo'; | |
509 | setPalette(currentPalette); | |
510 | } | |
511 | if (!gColorPalette || !gColorPalette.length) | |
512 | gColorPalette = getColorPalette(currentPalette); | |
513 | ||
aad35028 RK |
514 | let popup = document.getElementById("menu_palettePopup"); |
515 | let item = popup.firstChild; | |
6e98af87 RK |
516 | while (item) { |
517 | if (item.getAttribute("name") == "palette") { | |
518 | if (item.getAttribute("value") == currentPalette) | |
519 | item.setAttribute("checked", "true"); | |
520 | else | |
521 | item.removeAttribute("checked"); | |
522 | } | |
523 | item = item.nextSibling; | |
524 | } | |
525 | } | |
526 | ||
527 | function setPalette(aPaletteID) { | |
528 | gPref.setCharPref("mandelbrot.color_palette", aPaletteID); | |
529 | gColorPalette = getColorPalette(aPaletteID); | |
530 | } | |
531 | ||
6403d662 RK |
532 | function imgSettings() { |
533 | window.openDialog("chrome://mandelbrot/content/image-settings.xul"); | |
534 | } | |
535 | ||
6e98af87 RK |
536 | function updateDebugMenu() { |
537 | var jitMenuItem = document.getElementById("jitEnabled"); | |
538 | jitMenuItem.setAttribute("checked", gPref.getBoolPref("javascript.options.jit.chrome")); | |
539 | } | |
540 | ||
541 | function toggleJITState(jitMenuItem) { | |
542 | var jitEnabled = !gPref.getBoolPref("javascript.options.jit.chrome"); | |
543 | gPref.setBoolPref("javascript.options.jit.chrome", jitEnabled) | |
544 | jitMenuItem.setAttribute("checked", jitEnabled? "true" : "false"); | |
545 | } | |
546 | ||
547 | function updateAlgoMenu() { | |
aad35028 | 548 | let currentAlgo = ''; |
6e98af87 | 549 | try { |
aad35028 | 550 | currentAlgo = gPref.getCharPref("mandelbrot.use_algorithm"); |
6e98af87 | 551 | } |
aad35028 | 552 | catch(e) { } |
6e98af87 RK |
553 | if (!currentAlgo.length) { |
554 | currentAlgo = 'numeric'; | |
555 | setAlgorithm(currentAlgo); | |
556 | } | |
557 | ||
aad35028 RK |
558 | let popup = document.getElementById("menu_algoPopup"); |
559 | let item = popup.firstChild; | |
6e98af87 RK |
560 | while (item) { |
561 | if (item.getAttribute("name") == "algorithm") { | |
562 | if (item.getAttribute("value") == currentAlgo) | |
563 | item.setAttribute("checked", "true"); | |
564 | else | |
565 | item.removeAttribute("checked"); | |
566 | } | |
567 | item = item.nextSibling; | |
568 | } | |
569 | } | |
570 | ||
571 | function setAlgorithm(algoID) { | |
572 | gPref.setCharPref("mandelbrot.use_algorithm", algoID); | |
573 | } | |
574 | ||
af3c147c | 575 | function addonsManager(aPane) { |
aad35028 | 576 | let theEM = Components.classes["@mozilla.org/appshell/window-mediator;1"] |
af3c147c RK |
577 | .getService(Components.interfaces.nsIWindowMediator) |
578 | .getMostRecentWindow("Extension:Manager"); | |
579 | if (theEM) { | |
580 | theEM.focus(); | |
581 | if (aPane) | |
582 | theEM.showView(aPane); | |
583 | return; | |
584 | } | |
585 | ||
586 | const EMURL = "chrome://mozapps/content/extensions/extensions.xul"; | |
587 | const EMFEATURES = "all,dialog=no"; | |
588 | if (aPane) | |
589 | window.openDialog(EMURL, "", EMFEATURES, aPane); | |
590 | else | |
591 | window.openDialog(EMURL, "", EMFEATURES); | |
592 | } | |
593 | ||
594 | function errorConsole() { | |
595 | toOpenWindowByType("global:console", "chrome://global/content/console.xul"); | |
596 | } | |
597 | ||
6e98af87 RK |
598 | /***** helper functions from external sources *****/ |
599 | ||
740b86d1 RK |
600 | // function below is based on http://developer.mozilla.org/en/docs/Code_snippets:Canvas |
601 | // custom modifications: | |
602 | // - use "a"-prefix on function arguments | |
603 | // - take an nsILocalFile as aDestFile argument | |
604 | // - always do silent download | |
605 | function saveCanvas(aCanvas, aDestFile) { | |
37b05b56 RK |
606 | // create a data url from the canvas and then create URIs of the source and targets |
607 | var io = Components.classes["@mozilla.org/network/io-service;1"] | |
608 | .getService(Components.interfaces.nsIIOService); | |
740b86d1 | 609 | var source = io.newURI(aCanvas.toDataURL("image/png", ""), "UTF8", null); |
37b05b56 RK |
610 | |
611 | // prepare to save the canvas data | |
612 | var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"] | |
613 | .createInstance(Components.interfaces.nsIWebBrowserPersist); | |
614 | ||
615 | persist.persistFlags = Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES; | |
616 | persist.persistFlags |= Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION; | |
617 | ||
37b05b56 | 618 | // save the canvas data to the file |
740b86d1 | 619 | persist.saveURI(source, null, null, null, null, aDestFile); |
37b05b56 RK |
620 | } |
621 | ||
622 | // function below is from http://developer.mozilla.org/en/docs/How_to_Quit_a_XUL_Application | |
623 | function quitApp(aForceQuit) { | |
624 | var appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1'] | |
625 | .getService(Components.interfaces.nsIAppStartup); | |
626 | ||
627 | // eAttemptQuit will try to close each XUL window, but the XUL window can cancel the quit | |
628 | // process if there is unsaved data. eForceQuit will quit no matter what. | |
629 | var quitSeverity = aForceQuit ? Components.interfaces.nsIAppStartup.eForceQuit : | |
630 | Components.interfaces.nsIAppStartup.eAttemptQuit; | |
631 | appStartup.quit(quitSeverity); | |
632 | } | |
af3c147c RK |
633 | |
634 | // functions below are from comm-central/suite/common/tasksOverlay.js | |
635 | function toOpenWindow(aWindow) { | |
636 | try { | |
637 | // Try to focus the previously focused window e.g. message compose body | |
638 | aWindow.document.commandDispatcher.focusedWindow.focus(); | |
639 | } catch (e) { | |
640 | // e.g. full-page plugin or non-XUL document; just raise the top window | |
641 | aWindow.focus(); | |
642 | } | |
643 | } | |
644 | ||
645 | function toOpenWindowByType(inType, uri, features) { | |
646 | // don't do several loads in parallel | |
647 | if (uri in window) | |
648 | return; | |
649 | ||
650 | var topWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"] | |
651 | .getService(Components.interfaces.nsIWindowMediator) | |
652 | .getMostRecentWindow(inType); | |
653 | if ( topWindow ) | |
654 | toOpenWindow( topWindow ); | |
655 | else { | |
656 | // open the requested window, but block it until it's fully loaded | |
657 | function newWindowLoaded(event) { | |
658 | // make sure that this handler is called only once | |
659 | window.removeEventListener("unload", newWindowLoaded, false); | |
660 | window[uri].removeEventListener("load", newWindowLoaded, false); | |
661 | delete window[uri]; | |
662 | } | |
663 | // remember the newly loading window until it's fully loaded | |
664 | // or until the current window passes away | |
665 | window[uri] = window.openDialog(uri, "", features || "all,dialog=no"); | |
666 | window[uri].addEventListener("load", newWindowLoaded, false); | |
667 | window.addEventListener("unload", newWindowLoaded, false); | |
668 | } | |
669 | } |