From ab0981bac525ed75f166497834b518dfcbb1abd2 Mon Sep 17 00:00:00 2001 From: Felipe Gomes Date: Fri, 8 Oct 2010 15:51:50 -0300 Subject: [PATCH] Bug 593307. Ensure centerscreen windows are initially created in a valid screen position. r=jst,jmathies a=blocking-final --- widget/src/windows/nsWindow.cpp | 3 +- widget/tests/Makefile.in | 3 ++ widget/tests/test_bug593307.xul | 50 +++++++++++++++++++ .../tests/window_bug593307_centerscreen.xul | 27 ++++++++++ widget/tests/window_bug593307_offscreen.xul | 34 +++++++++++++ xpfe/appshell/src/nsWebShellWindow.cpp | 37 +++++++++++++- xpfe/appshell/src/nsWebShellWindow.h | 1 + 7 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 widget/tests/test_bug593307.xul create mode 100644 widget/tests/window_bug593307_centerscreen.xul create mode 100644 widget/tests/window_bug593307_offscreen.xul diff --git a/widget/src/windows/nsWindow.cpp b/widget/src/windows/nsWindow.cpp index 1f5bc91e5778..de290fe2425e 100644 --- a/widget/src/windows/nsWindow.cpp +++ b/widget/src/windows/nsWindow.cpp @@ -540,8 +540,7 @@ nsWindow::Create(nsIWidget *aParent, nsnull : aParent; mIsTopWidgetWindow = (nsnull == baseParent); - mBounds.width = aRect.width; - mBounds.height = aRect.height; + mBounds = aRect; BaseCreate(baseParent, aRect, aHandleEventFunction, aContext, aAppShell, aToolkit, aInitData); diff --git a/widget/tests/Makefile.in b/widget/tests/Makefile.in index 023a75d188f1..51e5e38d070c 100644 --- a/widget/tests/Makefile.in +++ b/widget/tests/Makefile.in @@ -68,6 +68,9 @@ _CHROME_FILES = test_bug343416.xul \ test_bug478536.xul \ window_bug478536.xul \ test_bug517396.xul \ + test_bug593307.xul \ + window_bug593307_offscreen.xul \ + window_bug593307_centerscreen.xul \ test_keycodes.xul \ test_wheeltransaction.xul \ window_wheeltransaction.xul \ diff --git a/widget/tests/test_bug593307.xul b/widget/tests/test_bug593307.xul new file mode 100644 index 000000000000..88a22fb49819 --- /dev/null +++ b/widget/tests/test_bug593307.xul @@ -0,0 +1,50 @@ + + + + + + + Test for Bug 593307 + + + + diff --git a/widget/tests/window_bug593307_centerscreen.xul b/widget/tests/window_bug593307_centerscreen.xul new file mode 100644 index 000000000000..24d708760854 --- /dev/null +++ b/widget/tests/window_bug593307_centerscreen.xul @@ -0,0 +1,27 @@ + + + + + + + diff --git a/widget/tests/window_bug593307_offscreen.xul b/widget/tests/window_bug593307_offscreen.xul new file mode 100644 index 000000000000..0857c73a60c7 --- /dev/null +++ b/widget/tests/window_bug593307_offscreen.xul @@ -0,0 +1,34 @@ + + + + + + + diff --git a/xpfe/appshell/src/nsWebShellWindow.cpp b/xpfe/appshell/src/nsWebShellWindow.cpp index 2e633bb1434b..e515044910e4 100644 --- a/xpfe/appshell/src/nsWebShellWindow.cpp +++ b/xpfe/appshell/src/nsWebShellWindow.cpp @@ -95,6 +95,9 @@ #include "nsIObserverService.h" #include "prprf.h" +#include "nsIScreenManager.h" +#include "nsIScreen.h" + #include "nsIContent.h" // for menus // For calculating size @@ -162,6 +165,7 @@ nsresult nsWebShellWindow::Initialize(nsIXULWindow* aParent, mIsHiddenWindow = aIsHiddenWindow; + PRInt32 initialX = 0, initialY = 0; nsCOMPtr base(do_QueryInterface(aOpener)); if (base) { rv = base->GetPositionAndSize(&mOpenerScreenRect.x, @@ -170,12 +174,16 @@ nsresult nsWebShellWindow::Initialize(nsIXULWindow* aParent, &mOpenerScreenRect.height); if (NS_FAILED(rv)) { mOpenerScreenRect.Empty(); + } else { + initialX = mOpenerScreenRect.x; + initialY = mOpenerScreenRect.y; + ConstrainToOpenerScreen(&initialX, &initialY); } } // XXX: need to get the default window size from prefs... // Doesn't come from prefs... will come from CSS/XUL/RDF - nsIntRect r(mOpenerScreenRect.x, mOpenerScreenRect.y, aInitialWidth, aInitialHeight); + nsIntRect r(initialX, initialY, aInitialWidth, aInitialHeight); // Create top level window mWindow = do_CreateInstance(kWindowCID, &rv); @@ -772,6 +780,33 @@ PRBool nsWebShellWindow::ExecuteCloseHandler() return PR_FALSE; } // ExecuteCloseHandler +void nsWebShellWindow::ConstrainToOpenerScreen(PRInt32* aX, PRInt32* aY) +{ + if (mOpenerScreenRect.IsEmpty()) { + *aX = *aY = 0; + return; + } + + PRInt32 left, top, width, height; + // Constrain initial positions to the same screen as opener + nsCOMPtr screenmgr = do_GetService("@mozilla.org/gfx/screenmanager;1"); + if (screenmgr) { + nsCOMPtr screen; + screenmgr->ScreenForRect(mOpenerScreenRect.x, mOpenerScreenRect.y, + mOpenerScreenRect.width, mOpenerScreenRect.height, + getter_AddRefs(screen)); + if (screen) { + screen->GetAvailRect(&left, &top, &width, &height); + if (*aX < left || *aY > left + width) { + *aX = left; + } + if (*aY < top || *aY > top + height) { + *aY = top; + } + } + } +} + // nsIBaseWindow NS_IMETHODIMP nsWebShellWindow::Destroy() { diff --git a/xpfe/appshell/src/nsWebShellWindow.h b/xpfe/appshell/src/nsWebShellWindow.h index d67d3a679a81..974c8cb97121 100644 --- a/xpfe/appshell/src/nsWebShellWindow.h +++ b/xpfe/appshell/src/nsWebShellWindow.h @@ -84,6 +84,7 @@ protected: void LoadContentAreas(); PRBool ExecuteCloseHandler(); + void ConstrainToOpenerScreen(PRInt32* aX, PRInt32* aY); static nsEventStatus HandleEvent(nsGUIEvent *aEvent);