setDesktopBackground.js
Go to the documentation of this file.
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 Mozilla Firebird about dialog.
15 #
16 # The Initial Developer of the Original Code is
17 # Blake Ross (blake@blakeross.com).
18 # Portions created by the Initial Developer are Copyright (C) 2002
19 # the Initial Developer. All Rights Reserved.
20 #
21 # Contributor(s):
22 # Blake Ross <blake@blakeross.com>
23 # Ben Goodger <ben@mozilla.org>
24 # Dão Gottwald <dao@design-noir.de>
25 #
26 # Alternatively, the contents of this file may be used under the terms of
27 # either the GNU General Public License Version 2 or later (the "GPL"), or
28 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 # in which case the provisions of the GPL or the LGPL are applicable instead
30 # of those above. If you wish to allow use of your version of this file only
31 # under the terms of either the GPL or the LGPL, and not to allow others to
32 # use your version of this file under the terms of the MPL, indicate your
33 # decision by deleting the provisions above and replace them with the notice
34 # and other provisions required by the LGPL or the GPL. If you do not delete
35 # the provisions above, a recipient may use your version of this file under
36 # the terms of any one of the MPL, the GPL or the LGPL.
37 #
38 # ***** END LICENSE BLOCK *****
39 
40 var Ci = Components.interfaces;
41 
43 #ifndef XP_MACOSX
44  _position : "",
45  _backgroundColor : 0,
46 #else
47  _position : "STRETCH",
48 #endif
49  _screenWidth : 0,
50  _screenHeight : 0,
51  _image : null,
52  _canvas : null,
53 
54  get _shell()
55  {
56  return Components.classes["@mozilla.org/browser/shell-service;1"]
57  .getService(Ci.nsIShellService);
58  },
59 
60  load: function ()
61  {
62  this._canvas = document.getElementById("screen");
63  this._screenWidth = screen.width;
64  this._screenHeight = screen.height;
65 #ifdef XP_MACOSX
66  document.documentElement.getButton("accept").hidden = true;
67 #endif
68  if (this._screenWidth / this._screenHeight >= 1.6)
69  document.getElementById("monitor").setAttribute("aspectratio", "16:10");
70 
71  // make sure that the correct dimensions will be used
72  setTimeout(function(self) {
73  self.init(window.arguments[0]);
74  }, 0, this);
75  },
76 
77  init: function (aImage)
78  {
79  this._image = aImage;
80 
81  // set the size of the coordinate space
82  this._canvas.width = this._canvas.clientWidth;
83  this._canvas.height = this._canvas.clientHeight;
84 
85  var ctx = this._canvas.getContext("2d");
86  ctx.scale(this._canvas.clientWidth / this._screenWidth, this._canvas.clientHeight / this._screenHeight);
87 
88 #ifndef XP_MACOSX
89  this._initColor();
90 #else
91  // Make sure to reset the button state in case the user has already
92  // set an image as their desktop background.
93  var setDesktopBackground = document.getElementById("setDesktopBackground");
94  setDesktopBackground.hidden = false;
95  var bundle = document.getElementById("backgroundBundle");
96  setDesktopBackground.label = bundle.getString("DesktopBackgroundSet");
97  setDesktopBackground.disabled = false;
98 
99  document.getElementById("showDesktopPreferences").hidden = true;
100 #endif
101  this.updatePosition();
102  },
103 
104 #ifndef XP_MACOSX
105  _initColor: function ()
106  {
107  var color = this._shell.desktopBackgroundColor;
108 
109  const rMask = 4294901760;
110  const gMask = 65280;
111  const bMask = 255;
112  var r = (color & rMask) >> 16;
113  var g = (color & gMask) >> 8;
114  var b = (color & bMask);
115  this.updateColor(this._rgbToHex(r, g, b));
116 
117  var colorpicker = document.getElementById("desktopColor");
118  colorpicker.color = this._backgroundColor;
119  },
120 
121  updateColor: function (aColor)
122  {
123  this._backgroundColor = aColor;
124  this._canvas.style.backgroundColor = aColor;
125  },
126 
127  // Converts a color string in the format "#RRGGBB" to an integer.
128  _hexStringToLong: function (aString)
129  {
130  return parseInt(aString.substring(1,3), 16) << 16 |
131  parseInt(aString.substring(3,5), 16) << 8 |
132  parseInt(aString.substring(5,7), 16);
133  },
134 
135  _rgbToHex: function (aR, aG, aB)
136  {
137  return "#" + [aR, aG, aB].map(function(aInt) aInt.toString(16).replace(/^(.)$/, "0$1"))
138  .join("").toUpperCase();
139  },
140 #else
141  observe: function (aSubject, aTopic, aData)
142  {
143  if (aTopic == "shell:desktop-background-changed") {
144  document.getElementById("setDesktopBackground").hidden = true;
145  document.getElementById("showDesktopPreferences").hidden = false;
146 
147  Components.classes["@mozilla.org/observer-service;1"]
148  .getService(Ci.nsIObserverService)
149  .removeObserver(this, "shell:desktop-background-changed");
150  }
151  },
152 
153  showDesktopPrefs: function()
154  {
155  this._shell.openApplication(Ci.nsIMacShellService.APPLICATION_DESKTOP);
156  },
157 #endif
158 
159  setDesktopBackground: function ()
160  {
161 #ifndef XP_MACOSX
162  document.persist("menuPosition", "value");
163  this._shell.desktopBackgroundColor = this._hexStringToLong(this._backgroundColor);
164 #else
165  Components.classes["@mozilla.org/observer-service;1"]
166  .getService(Ci.nsIObserverService)
167  .addObserver(this, "shell:desktop-background-changed", false);
168 
169  var bundle = document.getElementById("backgroundBundle");
170  var setDesktopBackground = document.getElementById("setDesktopBackground");
171  setDesktopBackground.disabled = true;
172  setDesktopBackground.label = bundle.getString("DesktopBackgroundDownloading");
173 #endif
174  this._shell.setDesktopBackground(this._image,
175  Ci.nsIShellService["BACKGROUND_" + this._position]);
176  },
177 
178  updatePosition: function ()
179  {
180  var ctx = this._canvas.getContext("2d");
181  ctx.clearRect(0, 0, this._screenWidth, this._screenHeight);
182 
183 #ifndef XP_MACOSX
184  this._position = document.getElementById("menuPosition").value;
185 #endif
186 
187  switch (this._position) {
188  case "TILE":
189  ctx.save();
190  ctx.fillStyle = ctx.createPattern(this._image, "repeat");
191  ctx.fillRect(0, 0, this._screenWidth, this._screenHeight);
192  ctx.restore();
193  break;
194  case "STRETCH":
195  ctx.drawImage(this._image, 0, 0, this._screenWidth, this._screenHeight);
196  break;
197  case "CENTER":
198  var x = (this._screenWidth - this._image.naturalWidth) / 2;
199  var y = (this._screenHeight - this._image.naturalHeight) / 2;
200  ctx.drawImage(this._image, x, y);
201  }
202  }
203 };
var gSetBackground
let window
_window init
Definition: FeedWriter.js:1144
var bundle
Element Properties load
aWindow setTimeout(function(){_this.restoreHistory(aWindow, aTabs, aTabData, aIdMap);}, 0)
return null
Definition: FeedWriter.js:1143
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe