diff --git a/dom/src/base/nsGlobalWindow.cpp b/dom/src/base/nsGlobalWindow.cpp index c43541baaab0..61850fec1d38 100644 --- a/dom/src/base/nsGlobalWindow.cpp +++ b/dom/src/base/nsGlobalWindow.cpp @@ -392,7 +392,7 @@ NS_IMETHODIMP GlobalWindowImpl::GetScreen(nsIDOMScreen** aScreen) { if (nsnull == mScreen) { - mScreen = new ScreenImpl(); + mScreen = new ScreenImpl( mWebShell ); NS_IF_ADDREF(mScreen); } @@ -948,7 +948,7 @@ GlobalWindowImpl::Alert(const nsString& aStr) nsresult ret; nsIWebShell *rootWebShell; - ret = mWebShell->GetRootWebShell(rootWebShell); + ret = mWebShell->GetRootWebShellEvenIfChrome(rootWebShell); if (nsnull != rootWebShell) { nsIWebShellContainer *rootContainer; ret = rootWebShell->GetContainer(rootContainer); diff --git a/dom/src/base/nsScreen.cpp b/dom/src/base/nsScreen.cpp index c55f710ac0da..baefbe846ac2 100644 --- a/dom/src/base/nsScreen.cpp +++ b/dom/src/base/nsScreen.cpp @@ -20,6 +20,15 @@ #include "nsScreen.h" #include "nsIDOMWindow.h" #include "nsIScriptGlobalObject.h" +#include "nsIWebShell.h" +#include "nsIDeviceContext.h" +#include "nsIPresContext.h" +#include "nsCOMPtr.h" +#include "nsIDocumentViewer.h" +#include "nsIDocumentLoader.h" + +static NS_DEFINE_IID(kIDocumentViewerIID, NS_IDOCUMENT_VIEWER_IID); +static NS_DEFINE_IID(kIDocumentLoaderIID, NS_IDOCUMENTLOADER_IID); static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID); static NS_DEFINE_IID(kIDOMScreenIID, NS_IDOMSCREEN_IID); @@ -28,14 +37,16 @@ static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); // // Screen class implementation // -ScreenImpl::ScreenImpl() +ScreenImpl::ScreenImpl( nsIWebShell* aWebShell): mWebShell( aWebShell ) { NS_INIT_REFCNT(); mScriptObject = nsnull; + NS_IF_ADDREF( mWebShell ); } ScreenImpl::~ScreenImpl() { + NS_IF_RELEASE( mWebShell ); } NS_IMPL_ADDREF(ScreenImpl) @@ -92,56 +103,99 @@ ScreenImpl::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject) NS_IMETHODIMP ScreenImpl::GetWidth(PRInt32* aWidth) { - //XXX not implmented + nsIDeviceContext* context = GetDeviceContext(); + if ( context ) + { + PRInt32 height; + context->GetDeviceSurfaceDimensions( *aWidth, height ); + float devUnits; + context->GetDevUnitsToAppUnits(devUnits); + *aWidth = NSToIntRound(float( *aWidth) / devUnits ); + NS_RELEASE( context ); + return NS_OK; + } + *aWidth = -1; - return NS_OK; + return NS_ERROR_FAILURE; } NS_IMETHODIMP ScreenImpl::GetHeight(PRInt32* aHeight) { - //XXX not implmented + nsIDeviceContext* context = GetDeviceContext(); + if ( context ) + { + PRInt32 width; + context->GetDeviceSurfaceDimensions( width, *aHeight ); + float devUnits; + context->GetDevUnitsToAppUnits(devUnits); + *aHeight = NSToIntRound(float( *aHeight) / devUnits ); + NS_RELEASE( context ); + return NS_OK; + } + *aHeight = -1; - return NS_OK; + return NS_ERROR_FAILURE; } NS_IMETHODIMP ScreenImpl::GetPixelDepth(PRInt32* aPixelDepth) { + nsIDeviceContext* context = GetDeviceContext(); + if ( context ) + { + PRUint32 depth; + context->GetDepth( depth ); + *aPixelDepth = depth; + NS_RELEASE( context ); + return NS_OK; + } //XXX not implmented *aPixelDepth = -1; - return NS_OK; + return NS_ERROR_FAILURE; } NS_IMETHODIMP ScreenImpl::GetColorDepth(PRInt32* aColorDepth) { - //XXX not implmented + nsIDeviceContext* context = GetDeviceContext(); + if ( context ) + { + PRUint32 depth; + context->GetDepth( depth ); + *aColorDepth = depth; + NS_RELEASE( context ); + return NS_OK; + } + *aColorDepth = -1; - return NS_OK; + return NS_ERROR_FAILURE; } NS_IMETHODIMP ScreenImpl::GetAvailWidth(PRInt32* aAvailWidth) { - //XXX not implmented - *aAvailWidth = -1; - return NS_OK; + //XXX not implmented + // Really should subtract out chrome + return GetWidth( aAvailWidth); + } NS_IMETHODIMP ScreenImpl::GetAvailHeight(PRInt32* aAvailHeight) { - //XXX not implmented - *aAvailHeight = -1; - return NS_OK; + //XXX not implmented + // Really should subtract out chrome + return GetHeight(aAvailHeight); + } NS_IMETHODIMP ScreenImpl::GetAvailLeft(PRInt32* aAvailLeft) { - //XXX not implmented - *aAvailLeft = -1; + //XXX not implmented + // have to factor in chrome someday + *aAvailLeft = 0; return NS_OK; } @@ -149,8 +203,30 @@ NS_IMETHODIMP ScreenImpl::GetAvailTop(PRInt32* aAvailTop) { //XXX not implmented - *aAvailTop = -1; + // have to factor in chrome someday + *aAvailTop = 0; return NS_OK; } +nsIDeviceContext* ScreenImpl::GetDeviceContext() +{ + nsCOMPtr docViewer; + nsCOMPtr contentViewer; + nsIPresContext* presContext = nsnull; + nsIDeviceContext* context = nsnull; + + if( mWebShell && NS_SUCCEEDED( mWebShell->GetContentViewer( getter_AddRefs(contentViewer) )) ) + { + if ( NS_SUCCEEDED( contentViewer->QueryInterface(kIDocumentViewerIID, getter_AddRefs(docViewer) ) ) ) + { + if (NS_SUCCEEDED( docViewer->GetPresContext( presContext ) ) ) + { + presContext->GetDeviceContext( &context ); + } + NS_IF_RELEASE( presContext ); + } + } + return context; +} + diff --git a/dom/src/base/nsScreen.h b/dom/src/base/nsScreen.h index d7e2003bdd01..911e84cbeede 100644 --- a/dom/src/base/nsScreen.h +++ b/dom/src/base/nsScreen.h @@ -23,11 +23,13 @@ #include "nsISupports.h" #include "nscore.h" #include "nsIScriptContext.h" +class nsIWebShell; +class nsIDeviceContext; // Script "screen" object class ScreenImpl : public nsIScriptObjectOwner, public nsIDOMScreen { public: - ScreenImpl(); + ScreenImpl( nsIWebShell* aWebShell ); virtual ~ScreenImpl(); NS_DECL_ISUPPORTS @@ -45,7 +47,10 @@ public: NS_IMETHOD GetAvailTop(PRInt32* aAvailTop); protected: + nsIDeviceContext* GetDeviceContext(); + void *mScriptObject; + nsIWebShell* mWebShell; }; #endif /* nsScreen_h___ */