implemented intrinsic sizing method on DOM Window

This commit is contained in:
danm%netscape.com 1999-09-03 22:41:56 +00:00
parent 5dbc2f05f7
commit e68f60f671
11 changed files with 194 additions and 9 deletions

View File

@ -71,6 +71,7 @@
#include "nsIDOMHTMLDocument.h" #include "nsIDOMHTMLDocument.h"
#include "nsLayoutCID.h" #include "nsLayoutCID.h"
#include "nsIDOMRange.h" #include "nsIDOMRange.h"
#include "nsIFrameReflow.h"
#include "nsMultiMixedConv.h" // for #include "nsMultiMixedConv.h" // for
#include "nsIRegistry.h" #include "nsIRegistry.h"
@ -186,6 +187,7 @@ public:
NS_IMETHOD Destroy(void); NS_IMETHOD Destroy(void);
NS_IMETHOD GetBounds(PRInt32 &x, PRInt32 &y, PRInt32 &w, PRInt32 &h); NS_IMETHOD GetBounds(PRInt32 &x, PRInt32 &y, PRInt32 &w, PRInt32 &h);
NS_IMETHOD SetBounds(PRInt32 x, PRInt32 y, PRInt32 w, PRInt32 h); NS_IMETHOD SetBounds(PRInt32 x, PRInt32 y, PRInt32 w, PRInt32 h);
NS_IMETHOD SizeToContent();
NS_IMETHOD MoveTo(PRInt32 aX, PRInt32 aY); NS_IMETHOD MoveTo(PRInt32 aX, PRInt32 aY);
NS_IMETHOD Show(); NS_IMETHOD Show();
NS_IMETHOD Hide(); NS_IMETHOD Hide();
@ -1354,6 +1356,62 @@ nsWebShell::SetBounds(PRInt32 x, PRInt32 y, PRInt32 w, PRInt32 h)
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP
nsWebShell::SizeToContent()
{
nsresult rv;
// get the presentation shell
nsCOMPtr<nsIContentViewer> cv;
GetContentViewer(getter_AddRefs(cv));
if (cv) {
nsCOMPtr<nsIDocumentViewer> dv = do_QueryInterface(cv);
if (dv) {
nsCOMPtr<nsIPresContext> pcx;
dv->GetPresContext(*getter_AddRefs(pcx));
if (pcx) {
nsCOMPtr<nsIPresShell> pshell;
pcx->GetShell(getter_AddRefs(pshell));
// whew! so resize the presentation shell
if (pshell) {
nsRect shellArea;
PRInt32 width, height;
float pixelScale;
rv = pshell->ResizeReflow(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE);
// so how big is it?
pcx->GetVisibleArea(shellArea);
pcx->GetTwipsToPixels(&pixelScale);
width = PRInt32((float)shellArea.width*pixelScale);
height = PRInt32((float)shellArea.height*pixelScale);
// if we're the outermost webshell for this window, size the window
if (mContainer) {
nsCOMPtr<nsIBrowserWindow> browser = do_QueryInterface(mContainer);
if (browser) {
nsCOMPtr<nsIWebShell> browserWebShell;
PRInt32 oldX, oldY, oldWidth, oldHeight,
widthDelta, heightDelta;
nsRect windowBounds;
GetBounds(oldX, oldY, oldWidth, oldHeight);
widthDelta = width - oldWidth;
heightDelta = height - oldHeight;
browser->GetWindowBounds(windowBounds);
browser->SizeWindowTo(windowBounds.width + widthDelta,
windowBounds.height + heightDelta);
}
}
}
}
}
}
return rv;
}
NS_IMETHODIMP NS_IMETHODIMP
nsWebShell::MoveTo(PRInt32 aX, PRInt32 aY) nsWebShell::MoveTo(PRInt32 aX, PRInt32 aY)
{ {

View File

@ -146,6 +146,8 @@ public:
NS_IMETHOD ResizeBy(PRInt32 aWidthDif, PRInt32 aHeightDif)=0; NS_IMETHOD ResizeBy(PRInt32 aWidthDif, PRInt32 aHeightDif)=0;
NS_IMETHOD SizeToContent()=0;
NS_IMETHOD ScrollTo(PRInt32 aXScroll, PRInt32 aYScroll)=0; NS_IMETHOD ScrollTo(PRInt32 aXScroll, PRInt32 aYScroll)=0;
NS_IMETHOD ScrollBy(PRInt32 aXScrollDif, PRInt32 aYScrollDif)=0; NS_IMETHOD ScrollBy(PRInt32 aXScrollDif, PRInt32 aYScrollDif)=0;
@ -225,6 +227,7 @@ public:
NS_IMETHOD MoveBy(PRInt32 aXDif, PRInt32 aYDif); \ NS_IMETHOD MoveBy(PRInt32 aXDif, PRInt32 aYDif); \
NS_IMETHOD ResizeTo(PRInt32 aWidth, PRInt32 aHeight); \ NS_IMETHOD ResizeTo(PRInt32 aWidth, PRInt32 aHeight); \
NS_IMETHOD ResizeBy(PRInt32 aWidthDif, PRInt32 aHeightDif); \ NS_IMETHOD ResizeBy(PRInt32 aWidthDif, PRInt32 aHeightDif); \
NS_IMETHOD SizeToContent(); \
NS_IMETHOD ScrollTo(PRInt32 aXScroll, PRInt32 aYScroll); \ NS_IMETHOD ScrollTo(PRInt32 aXScroll, PRInt32 aYScroll); \
NS_IMETHOD ScrollBy(PRInt32 aXScrollDif, PRInt32 aYScrollDif); \ NS_IMETHOD ScrollBy(PRInt32 aXScrollDif, PRInt32 aYScrollDif); \
NS_IMETHOD ClearTimeout(PRInt32 aTimerID); \ NS_IMETHOD ClearTimeout(PRInt32 aTimerID); \
@ -296,6 +299,7 @@ public:
NS_IMETHOD MoveBy(PRInt32 aXDif, PRInt32 aYDif) { return _to MoveBy(aXDif, aYDif); } \ NS_IMETHOD MoveBy(PRInt32 aXDif, PRInt32 aYDif) { return _to MoveBy(aXDif, aYDif); } \
NS_IMETHOD ResizeTo(PRInt32 aWidth, PRInt32 aHeight) { return _to ResizeTo(aWidth, aHeight); } \ NS_IMETHOD ResizeTo(PRInt32 aWidth, PRInt32 aHeight) { return _to ResizeTo(aWidth, aHeight); } \
NS_IMETHOD ResizeBy(PRInt32 aWidthDif, PRInt32 aHeightDif) { return _to ResizeBy(aWidthDif, aHeightDif); } \ NS_IMETHOD ResizeBy(PRInt32 aWidthDif, PRInt32 aHeightDif) { return _to ResizeBy(aWidthDif, aHeightDif); } \
NS_IMETHOD SizeToContent() { return _to SizeToContent(); } \
NS_IMETHOD ScrollTo(PRInt32 aXScroll, PRInt32 aYScroll) { return _to ScrollTo(aXScroll, aYScroll); } \ NS_IMETHOD ScrollTo(PRInt32 aXScroll, PRInt32 aYScroll) { return _to ScrollTo(aXScroll, aYScroll); } \
NS_IMETHOD ScrollBy(PRInt32 aXScrollDif, PRInt32 aYScrollDif) { return _to ScrollBy(aXScrollDif, aYScrollDif); } \ NS_IMETHOD ScrollBy(PRInt32 aXScrollDif, PRInt32 aYScrollDif) { return _to ScrollBy(aXScrollDif, aYScrollDif); } \
NS_IMETHOD ClearTimeout(PRInt32 aTimerID) { return _to ClearTimeout(aTimerID); } \ NS_IMETHOD ClearTimeout(PRInt32 aTimerID) { return _to ClearTimeout(aTimerID); } \

View File

@ -48,20 +48,21 @@
void moveTo(in long xPos, in long yPos); void moveTo(in long xPos, in long yPos);
void moveBy(in long xDif, in long yDif); void moveBy(in long xDif, in long yDif);
void resizeTo(in long width, in long height); void resizeTo(in long width, in long height);
void resizeBy(in long widthDif, in long heightDif); void resizeBy(in long widthDif, in long heightDif);
void sizeToContent();
void scrollTo(in long xScroll, in long yScroll); void scrollTo(in long xScroll, in long yScroll);
void scrollBy(in long xScrollDif, in long yScrollDif); void scrollBy(in long xScrollDif, in long yScrollDif);
void clearTimeout(in long timerID); void clearTimeout(in long timerID);
void clearInterval(in long timerID); void clearInterval(in long timerID);
long setTimeout(/* ... */); long setTimeout(/* ... */);
long setInterval(/* ... */); long setInterval(/* ... */);
Window createPopup(in Element element, in Element popupContent, Window createPopup(in Element element, in Element popupContent,
in long xPos, in long yPos, in long xPos, in long yPos,
in DOMString popupType, in DOMString anchorAlignment, in DOMString popupType, in DOMString anchorAlignment,
in DOMString popupAlignment); in DOMString popupAlignment);
Window open(/* ... */); Window open(/* ... */);
Window openDialog(/* ... */); Window openDialog(/* ... */);
}; };

View File

@ -1476,6 +1476,14 @@ GlobalWindowImpl::ResizeBy(PRInt32 aWidthDif, PRInt32 aHeightDif)
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP
GlobalWindowImpl::SizeToContent()
{
if (mWebShell)
return mWebShell->SizeToContent();
return NS_ERROR_NOT_INITIALIZED;
}
NS_IMETHODIMP NS_IMETHODIMP
GlobalWindowImpl::ScrollTo(PRInt32 aXScroll, PRInt32 aYScroll) GlobalWindowImpl::ScrollTo(PRInt32 aXScroll, PRInt32 aYScroll)
{ {

View File

@ -153,6 +153,7 @@ public:
NS_IMETHOD MoveBy(PRInt32 aXDif, PRInt32 aYDif); NS_IMETHOD MoveBy(PRInt32 aXDif, PRInt32 aYDif);
NS_IMETHOD ResizeTo(PRInt32 aWidth, PRInt32 aHeight); NS_IMETHOD ResizeTo(PRInt32 aWidth, PRInt32 aHeight);
NS_IMETHOD ResizeBy(PRInt32 aWidthDif, PRInt32 aHeightDif); NS_IMETHOD ResizeBy(PRInt32 aWidthDif, PRInt32 aHeightDif);
NS_IMETHOD SizeToContent();
NS_IMETHOD ScrollTo(PRInt32 aXScroll, PRInt32 aYScroll); NS_IMETHOD ScrollTo(PRInt32 aXScroll, PRInt32 aYScroll);
NS_IMETHOD ScrollBy(PRInt32 aXScrollDif, PRInt32 aYScrollDif); NS_IMETHOD ScrollBy(PRInt32 aXScrollDif, PRInt32 aYScrollDif);

View File

@ -1708,6 +1708,48 @@ WindowResizeBy(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rva
} }
//
// Native method SizeToContent
//
PR_STATIC_CALLBACK(JSBool)
WindowSizeToContent(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
nsIDOMWindow *nativeThis = (nsIDOMWindow*)nsJSUtils::nsGetNativeThis(cx, obj);
*rval = JSVAL_NULL;
nsIScriptContext *scriptCX = (nsIScriptContext *)JS_GetContextPrivate(cx);
nsCOMPtr<nsIScriptSecurityManager> secMan;
if (NS_OK != scriptCX->GetSecurityManager(getter_AddRefs(secMan))) {
return JS_FALSE;
}
{
PRBool ok;
secMan->CheckScriptAccess(scriptCX, obj, "window.sizetocontent",PR_FALSE , &ok);
if (!ok) {
//Need to throw error here
return JS_FALSE;
}
}
// If there's no private data, this must be the prototype, so ignore
if (nsnull == nativeThis) {
return JS_TRUE;
}
{
if (NS_OK != nativeThis->SizeToContent()) {
return JS_FALSE;
}
*rval = JSVAL_VOID;
}
return JS_TRUE;
}
// //
// Native method ScrollTo // Native method ScrollTo
// //
@ -2498,6 +2540,7 @@ static JSFunctionSpec WindowMethods[] =
{"moveBy", WindowMoveBy, 2}, {"moveBy", WindowMoveBy, 2},
{"resizeTo", WindowResizeTo, 2}, {"resizeTo", WindowResizeTo, 2},
{"resizeBy", WindowResizeBy, 2}, {"resizeBy", WindowResizeBy, 2},
{"sizeToContent", WindowSizeToContent, 0},
{"scrollTo", WindowScrollTo, 2}, {"scrollTo", WindowScrollTo, 2},
{"scrollBy", WindowScrollBy, 2}, {"scrollBy", WindowScrollBy, 2},
{"clearTimeout", WindowClearTimeout, 1}, {"clearTimeout", WindowClearTimeout, 1},

View File

@ -533,7 +533,8 @@ ViewportFrame::Reflow(nsIPresContext& aPresContext,
// If we were flowed initially at both an unconstrained width and height, // If we were flowed initially at both an unconstrained width and height,
// this is a hint that we should return our child's intrinsic size. // this is a hint that we should return our child's intrinsic size.
if (eReflowReason_Initial == aReflowState.reason && if ((eReflowReason_Initial == aReflowState.reason ||
eReflowReason_Resize == aReflowState.reason) &&
aReflowState.availableWidth == NS_UNCONSTRAINEDSIZE && aReflowState.availableWidth == NS_UNCONSTRAINEDSIZE &&
aReflowState.availableHeight == NS_UNCONSTRAINEDSIZE) { aReflowState.availableHeight == NS_UNCONSTRAINEDSIZE) {
aDesiredSize.width = kidRect.width; aDesiredSize.width = kidRect.width;

View File

@ -533,7 +533,8 @@ ViewportFrame::Reflow(nsIPresContext& aPresContext,
// If we were flowed initially at both an unconstrained width and height, // If we were flowed initially at both an unconstrained width and height,
// this is a hint that we should return our child's intrinsic size. // this is a hint that we should return our child's intrinsic size.
if (eReflowReason_Initial == aReflowState.reason && if ((eReflowReason_Initial == aReflowState.reason ||
eReflowReason_Resize == aReflowState.reason) &&
aReflowState.availableWidth == NS_UNCONSTRAINEDSIZE && aReflowState.availableWidth == NS_UNCONSTRAINEDSIZE &&
aReflowState.availableHeight == NS_UNCONSTRAINEDSIZE) { aReflowState.availableHeight == NS_UNCONSTRAINEDSIZE) {
aDesiredSize.width = kidRect.width; aDesiredSize.width = kidRect.width;

View File

@ -174,6 +174,11 @@ public:
*/ */
NS_IMETHOD SetBounds(PRInt32 x, PRInt32 y, PRInt32 w, PRInt32 h) = 0; NS_IMETHOD SetBounds(PRInt32 x, PRInt32 y, PRInt32 w, PRInt32 h) = 0;
/**
* Shrink-to-fit the WebShell to its contents
*/
NS_IMETHOD SizeToContent() = 0;
NS_IMETHOD MoveTo(PRInt32 aX, PRInt32 aY) = 0; NS_IMETHOD MoveTo(PRInt32 aX, PRInt32 aY) = 0;
/** /**

View File

@ -71,6 +71,7 @@
#include "nsIDOMHTMLDocument.h" #include "nsIDOMHTMLDocument.h"
#include "nsLayoutCID.h" #include "nsLayoutCID.h"
#include "nsIDOMRange.h" #include "nsIDOMRange.h"
#include "nsIFrameReflow.h"
#include "nsMultiMixedConv.h" // for #include "nsMultiMixedConv.h" // for
#include "nsIRegistry.h" #include "nsIRegistry.h"
@ -186,6 +187,7 @@ public:
NS_IMETHOD Destroy(void); NS_IMETHOD Destroy(void);
NS_IMETHOD GetBounds(PRInt32 &x, PRInt32 &y, PRInt32 &w, PRInt32 &h); NS_IMETHOD GetBounds(PRInt32 &x, PRInt32 &y, PRInt32 &w, PRInt32 &h);
NS_IMETHOD SetBounds(PRInt32 x, PRInt32 y, PRInt32 w, PRInt32 h); NS_IMETHOD SetBounds(PRInt32 x, PRInt32 y, PRInt32 w, PRInt32 h);
NS_IMETHOD SizeToContent();
NS_IMETHOD MoveTo(PRInt32 aX, PRInt32 aY); NS_IMETHOD MoveTo(PRInt32 aX, PRInt32 aY);
NS_IMETHOD Show(); NS_IMETHOD Show();
NS_IMETHOD Hide(); NS_IMETHOD Hide();
@ -1354,6 +1356,62 @@ nsWebShell::SetBounds(PRInt32 x, PRInt32 y, PRInt32 w, PRInt32 h)
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP
nsWebShell::SizeToContent()
{
nsresult rv;
// get the presentation shell
nsCOMPtr<nsIContentViewer> cv;
GetContentViewer(getter_AddRefs(cv));
if (cv) {
nsCOMPtr<nsIDocumentViewer> dv = do_QueryInterface(cv);
if (dv) {
nsCOMPtr<nsIPresContext> pcx;
dv->GetPresContext(*getter_AddRefs(pcx));
if (pcx) {
nsCOMPtr<nsIPresShell> pshell;
pcx->GetShell(getter_AddRefs(pshell));
// whew! so resize the presentation shell
if (pshell) {
nsRect shellArea;
PRInt32 width, height;
float pixelScale;
rv = pshell->ResizeReflow(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE);
// so how big is it?
pcx->GetVisibleArea(shellArea);
pcx->GetTwipsToPixels(&pixelScale);
width = PRInt32((float)shellArea.width*pixelScale);
height = PRInt32((float)shellArea.height*pixelScale);
// if we're the outermost webshell for this window, size the window
if (mContainer) {
nsCOMPtr<nsIBrowserWindow> browser = do_QueryInterface(mContainer);
if (browser) {
nsCOMPtr<nsIWebShell> browserWebShell;
PRInt32 oldX, oldY, oldWidth, oldHeight,
widthDelta, heightDelta;
nsRect windowBounds;
GetBounds(oldX, oldY, oldWidth, oldHeight);
widthDelta = width - oldWidth;
heightDelta = height - oldHeight;
browser->GetWindowBounds(windowBounds);
browser->SizeWindowTo(windowBounds.width + widthDelta,
windowBounds.height + heightDelta);
}
}
}
}
}
}
return rv;
}
NS_IMETHODIMP NS_IMETHODIMP
nsWebShell::MoveTo(PRInt32 aX, PRInt32 aY) nsWebShell::MoveTo(PRInt32 aX, PRInt32 aY)
{ {

View File

@ -213,6 +213,11 @@
<html:button onclick="SetFromParams()">Startup from Params</html:button> <html:button onclick="SetFromParams()">Startup from Params</html:button>
</html:td> </html:td>
</html:tr> </html:tr>
<html:tr>
<html:td>
<html:button onclick="sizeToContent()">Size To Content</html:button>
</html:td>
</html:tr>
<html:tr> <html:tr>
<html:td> <html:td>
<html:button onclick="DumpWindow()">Dump Window</html:button> <html:button onclick="DumpWindow()">Dump Window</html:button>