Back to index

lightning-sunbird  0.9+nobinonly
nsScreen.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Travis Bogard <travis@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nscore.h"
00040 #include "nsScreen.h"
00041 #include "nsIDOMWindow.h"
00042 #include "nsIScriptGlobalObject.h"
00043 #include "nsIDocShell.h"
00044 #include "nsIDeviceContext.h"
00045 #include "nsPresContext.h"
00046 #include "nsCOMPtr.h"
00047 #include "nsIDocumentViewer.h"
00048 #include "nsIDocumentLoader.h"
00049 #include "nsDOMClassInfo.h"
00050 
00051 
00052 //
00053 //  Screen class implementation
00054 //
00055 nsScreen::nsScreen(nsIDocShell* aDocShell)
00056   : mDocShell(aDocShell)
00057 {
00058 }
00059 
00060 nsScreen::~nsScreen()
00061 {
00062 }
00063 
00064 
00065 // QueryInterface implementation for nsScreen
00066 NS_INTERFACE_MAP_BEGIN(nsScreen)
00067   NS_INTERFACE_MAP_ENTRY(nsISupports)
00068   NS_INTERFACE_MAP_ENTRY(nsIDOMScreen)
00069   NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(Screen)
00070 NS_INTERFACE_MAP_END
00071 
00072 
00073 NS_IMPL_ADDREF(nsScreen)
00074 NS_IMPL_RELEASE(nsScreen)
00075 
00076 
00077 NS_IMETHODIMP
00078 nsScreen::SetDocShell(nsIDocShell* aDocShell)
00079 {
00080    mDocShell = aDocShell; // Weak Reference
00081    return NS_OK;
00082 }
00083 
00084 NS_IMETHODIMP
00085 nsScreen::GetTop(PRInt32* aTop)
00086 {
00087   nsRect rect;
00088   nsresult rv = GetRect(rect);
00089 
00090   *aTop = rect.y;
00091 
00092   return rv;
00093 }
00094 
00095 
00096 NS_IMETHODIMP
00097 nsScreen::GetLeft(PRInt32* aLeft)
00098 {
00099   nsRect rect;
00100   nsresult rv = GetRect(rect);
00101 
00102   *aLeft = rect.x;
00103 
00104   return rv;
00105 }
00106 
00107 
00108 NS_IMETHODIMP
00109 nsScreen::GetWidth(PRInt32* aWidth)
00110 {
00111   nsRect rect;
00112   nsresult rv = GetRect(rect);
00113 
00114   *aWidth = rect.width;
00115 
00116   return rv;
00117 }
00118 
00119 NS_IMETHODIMP
00120 nsScreen::GetHeight(PRInt32* aHeight)
00121 {
00122   nsRect rect;
00123   nsresult rv = GetRect(rect);
00124 
00125   *aHeight = rect.height;
00126 
00127   return rv;
00128 }
00129 
00130 NS_IMETHODIMP
00131 nsScreen::GetPixelDepth(PRInt32* aPixelDepth)
00132 {
00133   nsIDeviceContext* context = GetDeviceContext();
00134 
00135   if (!context) {
00136     *aPixelDepth = -1;
00137 
00138     return NS_ERROR_FAILURE;
00139   }
00140 
00141   PRUint32 depth;
00142   context->GetDepth(depth);
00143 
00144   *aPixelDepth = depth;
00145 
00146   return NS_OK;
00147 }
00148 
00149 NS_IMETHODIMP
00150 nsScreen::GetColorDepth(PRInt32* aColorDepth)
00151 {
00152   return GetPixelDepth(aColorDepth);
00153 }
00154 
00155 NS_IMETHODIMP
00156 nsScreen::GetAvailWidth(PRInt32* aAvailWidth)
00157 {
00158   nsRect rect;
00159   nsresult rv = GetAvailRect(rect);
00160 
00161   *aAvailWidth = rect.width;
00162 
00163   return rv;
00164 }
00165 
00166 NS_IMETHODIMP
00167 nsScreen::GetAvailHeight(PRInt32* aAvailHeight)
00168 {
00169   nsRect rect;
00170   nsresult rv = GetAvailRect(rect);
00171 
00172   *aAvailHeight = rect.height;
00173 
00174   return rv;
00175 }
00176 
00177 NS_IMETHODIMP
00178 nsScreen::GetAvailLeft(PRInt32* aAvailLeft)
00179 {
00180   nsRect rect;
00181   nsresult rv = GetAvailRect(rect);
00182 
00183   *aAvailLeft = rect.x;
00184 
00185   return rv;
00186 }
00187 
00188 NS_IMETHODIMP
00189 nsScreen::GetAvailTop(PRInt32* aAvailTop)
00190 {
00191   nsRect rect;
00192   nsresult rv = GetAvailRect(rect);
00193 
00194   *aAvailTop = rect.y;
00195 
00196   return rv;
00197 }
00198 
00199 nsIDeviceContext*
00200 nsScreen::GetDeviceContext()
00201 {
00202   if(!mDocShell)
00203     return nsnull;
00204 
00205   nsCOMPtr<nsIContentViewer> contentViewer;
00206   mDocShell->GetContentViewer(getter_AddRefs(contentViewer));
00207 
00208   nsCOMPtr<nsIDocumentViewer> docViewer(do_QueryInterface(contentViewer));
00209   if(!docViewer)
00210     return nsnull;
00211 
00212   nsCOMPtr<nsPresContext> presContext;
00213   docViewer->GetPresContext(getter_AddRefs(presContext));
00214 
00215   nsIDeviceContext* context = nsnull;
00216   if(presContext)
00217     context = presContext->DeviceContext();
00218 
00219   return context;
00220 }
00221 
00222 nsresult
00223 nsScreen::GetRect(nsRect& aRect)
00224 {
00225   nsIDeviceContext *context = GetDeviceContext();
00226 
00227   if (!context) {
00228     return NS_ERROR_FAILURE;
00229   }
00230 
00231   context->GetRect(aRect);
00232 
00233   float devUnits;
00234   devUnits = context->DevUnitsToAppUnits();
00235 
00236   aRect.x = NSToIntRound(float(aRect.x) / devUnits);
00237   aRect.y = NSToIntRound(float(aRect.y) / devUnits);
00238 
00239   context->GetDeviceSurfaceDimensions(aRect.width, aRect.height);
00240 
00241   aRect.height = NSToIntRound(float(aRect.height) / devUnits);
00242   aRect.width = NSToIntRound(float(aRect.width) / devUnits);
00243 
00244   return NS_OK;
00245 }
00246 
00247 nsresult
00248 nsScreen::GetAvailRect(nsRect& aRect)
00249 {
00250   nsIDeviceContext *context = GetDeviceContext();
00251 
00252   if (!context) {
00253     return NS_ERROR_FAILURE;
00254   }
00255 
00256   context->GetClientRect(aRect);
00257 
00258   float devUnits;
00259   devUnits = context->DevUnitsToAppUnits();
00260 
00261   aRect.x = NSToIntRound(float(aRect.x) / devUnits);
00262   aRect.y = NSToIntRound(float(aRect.y) / devUnits);
00263   aRect.height = NSToIntRound(float(aRect.height) / devUnits);
00264   aRect.width = NSToIntRound(float(aRect.width) / devUnits);
00265 
00266   return NS_OK;
00267 }