Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / dom / base / nsScreen.cpp
blobf73239734e2bf1a09ae4a6b2506ec26e4ca8736e
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Travis Bogard <travis@netscape.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nscore.h"
40 #include "nsScreen.h"
41 #include "nsIDocShell.h"
42 #include "nsIDeviceContext.h"
43 #include "nsPresContext.h"
44 #include "nsCOMPtr.h"
45 #include "nsDOMClassInfo.h"
46 #include "nsIInterfaceRequestorUtils.h"
47 #include "nsLayoutUtils.h"
50 // Screen class implementation
52 nsScreen::nsScreen(nsIDocShell* aDocShell)
53 : mDocShell(aDocShell)
57 nsScreen::~nsScreen()
62 DOMCI_DATA(Screen, nsScreen)
64 // QueryInterface implementation for nsScreen
65 NS_INTERFACE_MAP_BEGIN(nsScreen)
66 NS_INTERFACE_MAP_ENTRY(nsISupports)
67 NS_INTERFACE_MAP_ENTRY(nsIDOMScreen)
68 NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(Screen)
69 NS_INTERFACE_MAP_END
72 NS_IMPL_ADDREF(nsScreen)
73 NS_IMPL_RELEASE(nsScreen)
76 NS_IMETHODIMP
77 nsScreen::SetDocShell(nsIDocShell* aDocShell)
79 mDocShell = aDocShell; // Weak Reference
80 return NS_OK;
83 NS_IMETHODIMP
84 nsScreen::GetTop(PRInt32* aTop)
86 nsRect rect;
87 nsresult rv = GetRect(rect);
89 *aTop = rect.y;
91 return rv;
95 NS_IMETHODIMP
96 nsScreen::GetLeft(PRInt32* aLeft)
98 nsRect rect;
99 nsresult rv = GetRect(rect);
101 *aLeft = rect.x;
103 return rv;
107 NS_IMETHODIMP
108 nsScreen::GetWidth(PRInt32* aWidth)
110 nsRect rect;
111 nsresult rv = GetRect(rect);
113 *aWidth = rect.width;
115 return rv;
118 NS_IMETHODIMP
119 nsScreen::GetHeight(PRInt32* aHeight)
121 nsRect rect;
122 nsresult rv = GetRect(rect);
124 *aHeight = rect.height;
126 return rv;
129 NS_IMETHODIMP
130 nsScreen::GetPixelDepth(PRInt32* aPixelDepth)
132 nsIDeviceContext* context = GetDeviceContext();
134 if (!context) {
135 *aPixelDepth = -1;
137 return NS_ERROR_FAILURE;
140 PRUint32 depth;
141 context->GetDepth(depth);
143 *aPixelDepth = depth;
145 return NS_OK;
148 NS_IMETHODIMP
149 nsScreen::GetColorDepth(PRInt32* aColorDepth)
151 return GetPixelDepth(aColorDepth);
154 NS_IMETHODIMP
155 nsScreen::GetAvailWidth(PRInt32* aAvailWidth)
157 nsRect rect;
158 nsresult rv = GetAvailRect(rect);
160 *aAvailWidth = rect.width;
162 return rv;
165 NS_IMETHODIMP
166 nsScreen::GetAvailHeight(PRInt32* aAvailHeight)
168 nsRect rect;
169 nsresult rv = GetAvailRect(rect);
171 *aAvailHeight = rect.height;
173 return rv;
176 NS_IMETHODIMP
177 nsScreen::GetAvailLeft(PRInt32* aAvailLeft)
179 nsRect rect;
180 nsresult rv = GetAvailRect(rect);
182 *aAvailLeft = rect.x;
184 return rv;
187 NS_IMETHODIMP
188 nsScreen::GetAvailTop(PRInt32* aAvailTop)
190 nsRect rect;
191 nsresult rv = GetAvailRect(rect);
193 *aAvailTop = rect.y;
195 return rv;
198 nsIDeviceContext*
199 nsScreen::GetDeviceContext()
201 return nsLayoutUtils::GetDeviceContextForScreenInfo(mDocShell);
204 nsresult
205 nsScreen::GetRect(nsRect& aRect)
207 nsIDeviceContext *context = GetDeviceContext();
209 if (!context) {
210 return NS_ERROR_FAILURE;
213 context->GetRect(aRect);
215 aRect.x = nsPresContext::AppUnitsToIntCSSPixels(aRect.x);
216 aRect.y = nsPresContext::AppUnitsToIntCSSPixels(aRect.y);
217 aRect.height = nsPresContext::AppUnitsToIntCSSPixels(aRect.height);
218 aRect.width = nsPresContext::AppUnitsToIntCSSPixels(aRect.width);
220 return NS_OK;
223 nsresult
224 nsScreen::GetAvailRect(nsRect& aRect)
226 nsIDeviceContext *context = GetDeviceContext();
228 if (!context) {
229 return NS_ERROR_FAILURE;
232 context->GetClientRect(aRect);
234 aRect.x = nsPresContext::AppUnitsToIntCSSPixels(aRect.x);
235 aRect.y = nsPresContext::AppUnitsToIntCSSPixels(aRect.y);
236 aRect.height = nsPresContext::AppUnitsToIntCSSPixels(aRect.height);
237 aRect.width = nsPresContext::AppUnitsToIntCSSPixels(aRect.width);
239 return NS_OK;