|
1 # This Source Code Form is subject to the terms of the Mozilla Public |
|
2 # License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
|
4 |
|
5 var Ci = Components.interfaces; |
|
6 |
|
7 var gSetBackground = { |
|
8 #ifndef XP_MACOSX |
|
9 _position : "", |
|
10 _backgroundColor : 0, |
|
11 #else |
|
12 _position : "STRETCH", |
|
13 #endif |
|
14 _screenWidth : 0, |
|
15 _screenHeight : 0, |
|
16 _image : null, |
|
17 _canvas : null, |
|
18 |
|
19 get _shell() |
|
20 { |
|
21 return Components.classes["@mozilla.org/browser/shell-service;1"] |
|
22 .getService(Ci.nsIShellService); |
|
23 }, |
|
24 |
|
25 load: function () |
|
26 { |
|
27 this._canvas = document.getElementById("screen"); |
|
28 this._screenWidth = screen.width; |
|
29 this._screenHeight = screen.height; |
|
30 #ifdef XP_MACOSX |
|
31 document.documentElement.getButton("accept").hidden = true; |
|
32 #endif |
|
33 if (this._screenWidth / this._screenHeight >= 1.6) |
|
34 document.getElementById("monitor").setAttribute("aspectratio", "16:10"); |
|
35 |
|
36 #ifdef XP_WIN |
|
37 // hide fill + fit options if <win7 since don't work |
|
38 var version = Components.classes["@mozilla.org/system-info;1"] |
|
39 .getService(Ci.nsIPropertyBag2) |
|
40 .getProperty("version"); |
|
41 var isWindows7OrHigher = (parseFloat(version) >= 6.1); |
|
42 if (!isWindows7OrHigher) { |
|
43 document.getElementById("fillPosition").hidden = true; |
|
44 document.getElementById("fitPosition").hidden = true; |
|
45 } |
|
46 #endif |
|
47 |
|
48 // make sure that the correct dimensions will be used |
|
49 setTimeout(function(self) { |
|
50 self.init(window.arguments[0]); |
|
51 }, 0, this); |
|
52 }, |
|
53 |
|
54 init: function (aImage) |
|
55 { |
|
56 this._image = aImage; |
|
57 |
|
58 // set the size of the coordinate space |
|
59 this._canvas.width = this._canvas.clientWidth; |
|
60 this._canvas.height = this._canvas.clientHeight; |
|
61 |
|
62 var ctx = this._canvas.getContext("2d"); |
|
63 ctx.scale(this._canvas.clientWidth / this._screenWidth, this._canvas.clientHeight / this._screenHeight); |
|
64 |
|
65 #ifndef XP_MACOSX |
|
66 this._initColor(); |
|
67 #else |
|
68 // Make sure to reset the button state in case the user has already |
|
69 // set an image as their desktop background. |
|
70 var setDesktopBackground = document.getElementById("setDesktopBackground"); |
|
71 setDesktopBackground.hidden = false; |
|
72 var bundle = document.getElementById("backgroundBundle"); |
|
73 setDesktopBackground.label = bundle.getString("DesktopBackgroundSet"); |
|
74 setDesktopBackground.disabled = false; |
|
75 |
|
76 document.getElementById("showDesktopPreferences").hidden = true; |
|
77 #endif |
|
78 this.updatePosition(); |
|
79 }, |
|
80 |
|
81 #ifndef XP_MACOSX |
|
82 _initColor: function () |
|
83 { |
|
84 var color = this._shell.desktopBackgroundColor; |
|
85 |
|
86 const rMask = 4294901760; |
|
87 const gMask = 65280; |
|
88 const bMask = 255; |
|
89 var r = (color & rMask) >> 16; |
|
90 var g = (color & gMask) >> 8; |
|
91 var b = (color & bMask); |
|
92 this.updateColor(this._rgbToHex(r, g, b)); |
|
93 |
|
94 var colorpicker = document.getElementById("desktopColor"); |
|
95 colorpicker.color = this._backgroundColor; |
|
96 }, |
|
97 |
|
98 updateColor: function (aColor) |
|
99 { |
|
100 this._backgroundColor = aColor; |
|
101 this._canvas.style.backgroundColor = aColor; |
|
102 }, |
|
103 |
|
104 // Converts a color string in the format "#RRGGBB" to an integer. |
|
105 _hexStringToLong: function (aString) |
|
106 { |
|
107 return parseInt(aString.substring(1,3), 16) << 16 | |
|
108 parseInt(aString.substring(3,5), 16) << 8 | |
|
109 parseInt(aString.substring(5,7), 16); |
|
110 }, |
|
111 |
|
112 _rgbToHex: function (aR, aG, aB) |
|
113 { |
|
114 return "#" + [aR, aG, aB].map(function(aInt) aInt.toString(16).replace(/^(.)$/, "0$1")) |
|
115 .join("").toUpperCase(); |
|
116 }, |
|
117 #else |
|
118 observe: function (aSubject, aTopic, aData) |
|
119 { |
|
120 if (aTopic == "shell:desktop-background-changed") { |
|
121 document.getElementById("setDesktopBackground").hidden = true; |
|
122 document.getElementById("showDesktopPreferences").hidden = false; |
|
123 |
|
124 Components.classes["@mozilla.org/observer-service;1"] |
|
125 .getService(Ci.nsIObserverService) |
|
126 .removeObserver(this, "shell:desktop-background-changed"); |
|
127 } |
|
128 }, |
|
129 |
|
130 showDesktopPrefs: function() |
|
131 { |
|
132 this._shell.openApplication(Ci.nsIMacShellService.APPLICATION_DESKTOP); |
|
133 }, |
|
134 #endif |
|
135 |
|
136 setDesktopBackground: function () |
|
137 { |
|
138 #ifndef XP_MACOSX |
|
139 document.persist("menuPosition", "value"); |
|
140 this._shell.desktopBackgroundColor = this._hexStringToLong(this._backgroundColor); |
|
141 #else |
|
142 Components.classes["@mozilla.org/observer-service;1"] |
|
143 .getService(Ci.nsIObserverService) |
|
144 .addObserver(this, "shell:desktop-background-changed", false); |
|
145 |
|
146 var bundle = document.getElementById("backgroundBundle"); |
|
147 var setDesktopBackground = document.getElementById("setDesktopBackground"); |
|
148 setDesktopBackground.disabled = true; |
|
149 setDesktopBackground.label = bundle.getString("DesktopBackgroundDownloading"); |
|
150 #endif |
|
151 this._shell.setDesktopBackground(this._image, |
|
152 Ci.nsIShellService["BACKGROUND_" + this._position]); |
|
153 }, |
|
154 |
|
155 updatePosition: function () |
|
156 { |
|
157 var ctx = this._canvas.getContext("2d"); |
|
158 ctx.clearRect(0, 0, this._screenWidth, this._screenHeight); |
|
159 |
|
160 #ifndef XP_MACOSX |
|
161 this._position = document.getElementById("menuPosition").value; |
|
162 #endif |
|
163 |
|
164 switch (this._position) { |
|
165 case "TILE": |
|
166 ctx.save(); |
|
167 ctx.fillStyle = ctx.createPattern(this._image, "repeat"); |
|
168 ctx.fillRect(0, 0, this._screenWidth, this._screenHeight); |
|
169 ctx.restore(); |
|
170 break; |
|
171 case "STRETCH": |
|
172 ctx.drawImage(this._image, 0, 0, this._screenWidth, this._screenHeight); |
|
173 break; |
|
174 case "CENTER": |
|
175 var x = (this._screenWidth - this._image.naturalWidth) / 2; |
|
176 var y = (this._screenHeight - this._image.naturalHeight) / 2; |
|
177 ctx.drawImage(this._image, x, y); |
|
178 break; |
|
179 case "FILL": |
|
180 //Try maxing width first, overflow height |
|
181 var widthRatio = this._screenWidth / this._image.naturalWidth; |
|
182 var width = this._image.naturalWidth * widthRatio; |
|
183 var height = this._image.naturalHeight * widthRatio; |
|
184 if (height < this._screenHeight) { |
|
185 //height less than screen, max height and overflow width |
|
186 var heightRatio = this._screenHeight / this._image.naturalHeight; |
|
187 width = this._image.naturalWidth * heightRatio; |
|
188 height = this._image.naturalHeight * heightRatio; |
|
189 } |
|
190 var x = (this._screenWidth - width) / 2; |
|
191 var y = (this._screenHeight - height) / 2; |
|
192 ctx.drawImage(this._image, x, y, width, height); |
|
193 break; |
|
194 case "FIT": |
|
195 //Try maxing width first, top and bottom borders |
|
196 var widthRatio = this._screenWidth / this._image.naturalWidth; |
|
197 var width = this._image.naturalWidth * widthRatio; |
|
198 var height = this._image.naturalHeight * widthRatio; |
|
199 var x = 0; |
|
200 var y = (this._screenHeight - height) / 2; |
|
201 if (height > this._screenHeight) { |
|
202 //height overflow, maximise height, side borders |
|
203 var heightRatio = this._screenHeight / this._image.naturalHeight; |
|
204 width = this._image.naturalWidth * heightRatio; |
|
205 height = this._image.naturalHeight * heightRatio; |
|
206 x = (this._screenWidth - width) / 2; |
|
207 y = 0; |
|
208 } |
|
209 ctx.drawImage(this._image, x, y, width, height); |
|
210 break; |
|
211 } |
|
212 } |
|
213 }; |