michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: var Ci = Components.interfaces; michael@0: michael@0: var gSetBackground = { michael@0: #ifndef XP_MACOSX michael@0: _position : "", michael@0: _backgroundColor : 0, michael@0: #else michael@0: _position : "STRETCH", michael@0: #endif michael@0: _screenWidth : 0, michael@0: _screenHeight : 0, michael@0: _image : null, michael@0: _canvas : null, michael@0: michael@0: get _shell() michael@0: { michael@0: return Components.classes["@mozilla.org/browser/shell-service;1"] michael@0: .getService(Ci.nsIShellService); michael@0: }, michael@0: michael@0: load: function () michael@0: { michael@0: this._canvas = document.getElementById("screen"); michael@0: this._screenWidth = screen.width; michael@0: this._screenHeight = screen.height; michael@0: #ifdef XP_MACOSX michael@0: document.documentElement.getButton("accept").hidden = true; michael@0: #endif michael@0: if (this._screenWidth / this._screenHeight >= 1.6) michael@0: document.getElementById("monitor").setAttribute("aspectratio", "16:10"); michael@0: michael@0: #ifdef XP_WIN michael@0: // hide fill + fit options if = 6.1); michael@0: if (!isWindows7OrHigher) { michael@0: document.getElementById("fillPosition").hidden = true; michael@0: document.getElementById("fitPosition").hidden = true; michael@0: } michael@0: #endif michael@0: michael@0: // make sure that the correct dimensions will be used michael@0: setTimeout(function(self) { michael@0: self.init(window.arguments[0]); michael@0: }, 0, this); michael@0: }, michael@0: michael@0: init: function (aImage) michael@0: { michael@0: this._image = aImage; michael@0: michael@0: // set the size of the coordinate space michael@0: this._canvas.width = this._canvas.clientWidth; michael@0: this._canvas.height = this._canvas.clientHeight; michael@0: michael@0: var ctx = this._canvas.getContext("2d"); michael@0: ctx.scale(this._canvas.clientWidth / this._screenWidth, this._canvas.clientHeight / this._screenHeight); michael@0: michael@0: #ifndef XP_MACOSX michael@0: this._initColor(); michael@0: #else michael@0: // Make sure to reset the button state in case the user has already michael@0: // set an image as their desktop background. michael@0: var setDesktopBackground = document.getElementById("setDesktopBackground"); michael@0: setDesktopBackground.hidden = false; michael@0: var bundle = document.getElementById("backgroundBundle"); michael@0: setDesktopBackground.label = bundle.getString("DesktopBackgroundSet"); michael@0: setDesktopBackground.disabled = false; michael@0: michael@0: document.getElementById("showDesktopPreferences").hidden = true; michael@0: #endif michael@0: this.updatePosition(); michael@0: }, michael@0: michael@0: #ifndef XP_MACOSX michael@0: _initColor: function () michael@0: { michael@0: var color = this._shell.desktopBackgroundColor; michael@0: michael@0: const rMask = 4294901760; michael@0: const gMask = 65280; michael@0: const bMask = 255; michael@0: var r = (color & rMask) >> 16; michael@0: var g = (color & gMask) >> 8; michael@0: var b = (color & bMask); michael@0: this.updateColor(this._rgbToHex(r, g, b)); michael@0: michael@0: var colorpicker = document.getElementById("desktopColor"); michael@0: colorpicker.color = this._backgroundColor; michael@0: }, michael@0: michael@0: updateColor: function (aColor) michael@0: { michael@0: this._backgroundColor = aColor; michael@0: this._canvas.style.backgroundColor = aColor; michael@0: }, michael@0: michael@0: // Converts a color string in the format "#RRGGBB" to an integer. michael@0: _hexStringToLong: function (aString) michael@0: { michael@0: return parseInt(aString.substring(1,3), 16) << 16 | michael@0: parseInt(aString.substring(3,5), 16) << 8 | michael@0: parseInt(aString.substring(5,7), 16); michael@0: }, michael@0: michael@0: _rgbToHex: function (aR, aG, aB) michael@0: { michael@0: return "#" + [aR, aG, aB].map(function(aInt) aInt.toString(16).replace(/^(.)$/, "0$1")) michael@0: .join("").toUpperCase(); michael@0: }, michael@0: #else michael@0: observe: function (aSubject, aTopic, aData) michael@0: { michael@0: if (aTopic == "shell:desktop-background-changed") { michael@0: document.getElementById("setDesktopBackground").hidden = true; michael@0: document.getElementById("showDesktopPreferences").hidden = false; michael@0: michael@0: Components.classes["@mozilla.org/observer-service;1"] michael@0: .getService(Ci.nsIObserverService) michael@0: .removeObserver(this, "shell:desktop-background-changed"); michael@0: } michael@0: }, michael@0: michael@0: showDesktopPrefs: function() michael@0: { michael@0: this._shell.openApplication(Ci.nsIMacShellService.APPLICATION_DESKTOP); michael@0: }, michael@0: #endif michael@0: michael@0: setDesktopBackground: function () michael@0: { michael@0: #ifndef XP_MACOSX michael@0: document.persist("menuPosition", "value"); michael@0: this._shell.desktopBackgroundColor = this._hexStringToLong(this._backgroundColor); michael@0: #else michael@0: Components.classes["@mozilla.org/observer-service;1"] michael@0: .getService(Ci.nsIObserverService) michael@0: .addObserver(this, "shell:desktop-background-changed", false); michael@0: michael@0: var bundle = document.getElementById("backgroundBundle"); michael@0: var setDesktopBackground = document.getElementById("setDesktopBackground"); michael@0: setDesktopBackground.disabled = true; michael@0: setDesktopBackground.label = bundle.getString("DesktopBackgroundDownloading"); michael@0: #endif michael@0: this._shell.setDesktopBackground(this._image, michael@0: Ci.nsIShellService["BACKGROUND_" + this._position]); michael@0: }, michael@0: michael@0: updatePosition: function () michael@0: { michael@0: var ctx = this._canvas.getContext("2d"); michael@0: ctx.clearRect(0, 0, this._screenWidth, this._screenHeight); michael@0: michael@0: #ifndef XP_MACOSX michael@0: this._position = document.getElementById("menuPosition").value; michael@0: #endif michael@0: michael@0: switch (this._position) { michael@0: case "TILE": michael@0: ctx.save(); michael@0: ctx.fillStyle = ctx.createPattern(this._image, "repeat"); michael@0: ctx.fillRect(0, 0, this._screenWidth, this._screenHeight); michael@0: ctx.restore(); michael@0: break; michael@0: case "STRETCH": michael@0: ctx.drawImage(this._image, 0, 0, this._screenWidth, this._screenHeight); michael@0: break; michael@0: case "CENTER": michael@0: var x = (this._screenWidth - this._image.naturalWidth) / 2; michael@0: var y = (this._screenHeight - this._image.naturalHeight) / 2; michael@0: ctx.drawImage(this._image, x, y); michael@0: break; michael@0: case "FILL": michael@0: //Try maxing width first, overflow height michael@0: var widthRatio = this._screenWidth / this._image.naturalWidth; michael@0: var width = this._image.naturalWidth * widthRatio; michael@0: var height = this._image.naturalHeight * widthRatio; michael@0: if (height < this._screenHeight) { michael@0: //height less than screen, max height and overflow width michael@0: var heightRatio = this._screenHeight / this._image.naturalHeight; michael@0: width = this._image.naturalWidth * heightRatio; michael@0: height = this._image.naturalHeight * heightRatio; michael@0: } michael@0: var x = (this._screenWidth - width) / 2; michael@0: var y = (this._screenHeight - height) / 2; michael@0: ctx.drawImage(this._image, x, y, width, height); michael@0: break; michael@0: case "FIT": michael@0: //Try maxing width first, top and bottom borders michael@0: var widthRatio = this._screenWidth / this._image.naturalWidth; michael@0: var width = this._image.naturalWidth * widthRatio; michael@0: var height = this._image.naturalHeight * widthRatio; michael@0: var x = 0; michael@0: var y = (this._screenHeight - height) / 2; michael@0: if (height > this._screenHeight) { michael@0: //height overflow, maximise height, side borders michael@0: var heightRatio = this._screenHeight / this._image.naturalHeight; michael@0: width = this._image.naturalWidth * heightRatio; michael@0: height = this._image.naturalHeight * heightRatio; michael@0: x = (this._screenWidth - width) / 2; michael@0: y = 0; michael@0: } michael@0: ctx.drawImage(this._image, x, y, width, height); michael@0: break; michael@0: } michael@0: } michael@0: };