gecko-dev/layout/xul/nsStackLayout.cpp

386 строки
11 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2012-05-21 15:12:37 +04:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
2000-03-31 11:02:06 +04:00
//
// Eric Vaughan
// Netscape Communications
//
// See documentation in associated header file
//
#include "nsStackLayout.h"
#include "nsCOMPtr.h"
#include "nsBoxLayoutState.h"
#include "nsBox.h"
2001-09-06 05:07:40 +04:00
#include "nsBoxFrame.h"
#include "nsGkAtoms.h"
2001-09-06 05:07:40 +04:00
#include "nsIContent.h"
#include "nsNameSpaceManager.h"
2000-03-31 11:02:06 +04:00
using namespace mozilla;
nsBoxLayout* nsStackLayout::gInstance = nullptr;
#define SPECIFIED_LEFT (1 << eSideLeft)
#define SPECIFIED_RIGHT (1 << eSideRight)
#define SPECIFIED_TOP (1 << eSideTop)
#define SPECIFIED_BOTTOM (1 << eSideBottom)
nsresult
NS_NewStackLayout(nsCOMPtr<nsBoxLayout>& aNewLayout)
{
if (!nsStackLayout::gInstance) {
nsStackLayout::gInstance = new nsStackLayout();
NS_IF_ADDREF(nsStackLayout::gInstance);
}
// we have not instance variables so just return our static one.
aNewLayout = nsStackLayout::gInstance;
return NS_OK;
}
/*static*/ void
nsStackLayout::Shutdown()
{
NS_IF_RELEASE(gInstance);
}
nsStackLayout::nsStackLayout()
2000-03-31 11:02:06 +04:00
{
}
/*
* Sizing: we are as wide as the widest child plus its left offset
* we are tall as the tallest child plus its top offset.
*
* Only children which have -moz-stack-sizing set to stretch-to-fit
* (the default) will be included in the size computations.
*/
nsSize
nsStackLayout::GetXULPrefSize(nsIFrame* aBox, nsBoxLayoutState& aState)
2000-03-31 11:02:06 +04:00
{
nsSize prefSize (0, 0);
nsIFrame* child = nsBox::GetChildXULBox(aBox);
while (child) {
if (child->StyleXUL()->mStretchStack) {
nsSize pref = child->GetXULPrefSize(aState);
2000-03-31 11:02:06 +04:00
AddMargin(child, pref);
nsMargin offset;
GetOffset(child, offset);
pref.width += offset.LeftRight();
pref.height += offset.TopBottom();
AddLargestSize(prefSize, pref);
}
2001-09-06 05:07:40 +04:00
child = nsBox::GetNextXULBox(child);
2001-09-06 05:07:40 +04:00
}
2000-03-31 11:02:06 +04:00
AddBorderAndPadding(aBox, prefSize);
2000-03-31 11:02:06 +04:00
return prefSize;
2000-03-31 11:02:06 +04:00
}
nsSize
nsStackLayout::GetXULMinSize(nsIFrame* aBox, nsBoxLayoutState& aState)
2000-03-31 11:02:06 +04:00
{
nsSize minSize (0, 0);
nsIFrame* child = nsBox::GetChildXULBox(aBox);
while (child) {
if (child->StyleXUL()->mStretchStack) {
nsSize min = child->GetXULMinSize(aState);
AddMargin(child, min);
nsMargin offset;
GetOffset(child, offset);
min.width += offset.LeftRight();
min.height += offset.TopBottom();
AddLargestSize(minSize, min);
}
2001-09-06 05:07:40 +04:00
child = nsBox::GetNextXULBox(child);
2001-09-06 05:07:40 +04:00
}
2000-03-31 11:02:06 +04:00
AddBorderAndPadding(aBox, minSize);
2000-03-31 11:02:06 +04:00
return minSize;
2000-03-31 11:02:06 +04:00
}
nsSize
nsStackLayout::GetXULMaxSize(nsIFrame* aBox, nsBoxLayoutState& aState)
2000-03-31 11:02:06 +04:00
{
nsSize maxSize (NS_INTRINSICSIZE, NS_INTRINSICSIZE);
2000-03-31 11:02:06 +04:00
nsIFrame* child = nsBox::GetChildXULBox(aBox);
while (child) {
if (child->StyleXUL()->mStretchStack) {
nsSize min = child->GetXULMinSize(aState);
nsSize max = child->GetXULMaxSize(aState);
2000-03-31 11:02:06 +04:00
max = nsBox::BoundsCheckMinMax(min, max);
AddMargin(child, max);
nsMargin offset;
GetOffset(child, offset);
max.width += offset.LeftRight();
max.height += offset.TopBottom();
AddSmallestSize(maxSize, max);
}
2000-03-31 11:02:06 +04:00
child = nsBox::GetNextXULBox(child);
2001-09-06 05:07:40 +04:00
}
2000-03-31 11:02:06 +04:00
AddBorderAndPadding(aBox, maxSize);
2000-03-31 11:02:06 +04:00
return maxSize;
2000-03-31 11:02:06 +04:00
}
nscoord
nsStackLayout::GetAscent(nsIFrame* aBox, nsBoxLayoutState& aState)
2000-03-31 11:02:06 +04:00
{
nscoord vAscent = 0;
nsIFrame* child = nsBox::GetChildXULBox(aBox);
2001-09-06 05:07:40 +04:00
while (child) {
nscoord ascent = child->GetXULBoxAscent(aState);
2001-09-06 05:07:40 +04:00
nsMargin margin;
child->GetXULMargin(margin);
ascent += margin.top;
if (ascent > vAscent)
vAscent = ascent;
child = nsBox::GetNextXULBox(child);
2001-09-06 05:07:40 +04:00
}
2000-03-31 11:02:06 +04:00
return vAscent;
2000-03-31 11:02:06 +04:00
}
uint8_t
nsStackLayout::GetOffset(nsIFrame* aChild, nsMargin& aOffset)
2000-03-31 11:02:06 +04:00
{
aOffset = nsMargin(0, 0, 0, 0);
// get the left, right, top and bottom offsets
2001-09-06 05:07:40 +04:00
// As an optimization, we cache the fact that we are not positioned to avoid
// wasting time fetching attributes.
if (aChild->IsXULBoxFrame() &&
(aChild->GetStateBits() & NS_STATE_STACK_NOT_POSITIONED))
return 0;
2000-03-31 11:02:06 +04:00
uint8_t offsetSpecified = 0;
nsIContent* content = aChild->GetContent();
2001-09-06 05:07:40 +04:00
if (content) {
bool ltr = aChild->StyleVisibility()->mDirection == NS_STYLE_DIRECTION_LTR;
2001-09-06 05:07:40 +04:00
nsAutoString value;
nsresult error;
2001-09-06 05:07:40 +04:00
content->GetAttr(kNameSpaceID_None, nsGkAtoms::start, value);
if (!value.IsEmpty()) {
value.Trim("%");
if (ltr) {
aOffset.left =
nsPresContext::CSSPixelsToAppUnits(value.ToInteger(&error));
offsetSpecified |= SPECIFIED_LEFT;
} else {
aOffset.right =
nsPresContext::CSSPixelsToAppUnits(value.ToInteger(&error));
offsetSpecified |= SPECIFIED_RIGHT;
}
}
content->GetAttr(kNameSpaceID_None, nsGkAtoms::end, value);
if (!value.IsEmpty()) {
value.Trim("%");
if (ltr) {
aOffset.right =
nsPresContext::CSSPixelsToAppUnits(value.ToInteger(&error));
offsetSpecified |= SPECIFIED_RIGHT;
} else {
aOffset.left =
nsPresContext::CSSPixelsToAppUnits(value.ToInteger(&error));
offsetSpecified |= SPECIFIED_LEFT;
}
}
content->GetAttr(kNameSpaceID_None, nsGkAtoms::left, value);
if (!value.IsEmpty()) {
2001-09-06 05:07:40 +04:00
value.Trim("%");
aOffset.left =
nsPresContext::CSSPixelsToAppUnits(value.ToInteger(&error));
offsetSpecified |= SPECIFIED_LEFT;
}
content->GetAttr(kNameSpaceID_None, nsGkAtoms::right, value);
if (!value.IsEmpty()) {
value.Trim("%");
aOffset.right =
nsPresContext::CSSPixelsToAppUnits(value.ToInteger(&error));
offsetSpecified |= SPECIFIED_RIGHT;
2001-09-06 05:07:40 +04:00
}
content->GetAttr(kNameSpaceID_None, nsGkAtoms::top, value);
if (!value.IsEmpty()) {
2001-09-06 05:07:40 +04:00
value.Trim("%");
aOffset.top =
nsPresContext::CSSPixelsToAppUnits(value.ToInteger(&error));
offsetSpecified |= SPECIFIED_TOP;
2001-09-06 05:07:40 +04:00
}
content->GetAttr(kNameSpaceID_None, nsGkAtoms::bottom, value);
if (!value.IsEmpty()) {
value.Trim("%");
aOffset.bottom =
nsPresContext::CSSPixelsToAppUnits(value.ToInteger(&error));
offsetSpecified |= SPECIFIED_BOTTOM;
}
}
if (!offsetSpecified && aChild->IsXULBoxFrame()) {
2001-09-06 05:07:40 +04:00
// If no offset was specified at all, then we cache this fact to avoid requerying
// CSS or the content model.
aChild->AddStateBits(NS_STATE_STACK_NOT_POSITIONED);
2001-09-06 05:07:40 +04:00
}
return offsetSpecified;
2001-09-06 05:07:40 +04:00
}
2001-09-06 05:07:40 +04:00
NS_IMETHODIMP
nsStackLayout::XULLayout(nsIFrame* aBox, nsBoxLayoutState& aState)
2001-09-06 05:07:40 +04:00
{
nsRect clientRect;
aBox->GetXULClientRect(clientRect);
2001-09-06 05:07:40 +04:00
bool grow;
2001-09-06 05:07:40 +04:00
do {
nsIFrame* child = nsBox::GetChildXULBox(aBox);
grow = false;
2001-09-06 05:07:40 +04:00
while (child)
{
nsMargin margin;
child->GetXULMargin(margin);
2001-09-06 05:07:40 +04:00
nsRect childRect(clientRect);
childRect.Deflate(margin);
if (childRect.width < 0)
childRect.width = 0;
if (childRect.height < 0)
childRect.height = 0;
nsRect oldRect(child->GetRect());
bool sizeChanged = !oldRect.IsEqualEdges(childRect);
2001-09-06 05:07:40 +04:00
// only lay out dirty children or children whose sizes have changed
if (sizeChanged || NS_SUBTREE_DIRTY(child)) {
2001-09-06 05:07:40 +04:00
// add in the child's margin
nsMargin margin;
child->GetXULMargin(margin);
2001-09-06 05:07:40 +04:00
// obtain our offset from the top left border of the stack's content box.
nsMargin offset;
uint8_t offsetSpecified = GetOffset(child, offset);
// Set the position and size based on which offsets have been specified:
// left only - offset from left edge, preferred width
// right only - offset from right edge, preferred width
// left and right - offset from left and right edges, width in between this
// neither - no offset, full width of stack
// Vertical direction is similar.
//
// Margins on the child are also included in the edge offsets
if (offsetSpecified) {
nsSize min = child->GetXULMinSize(aState);
nsSize max = child->GetXULMaxSize(aState);
if (offsetSpecified & SPECIFIED_LEFT) {
childRect.x = clientRect.x + offset.left + margin.left;
if (offsetSpecified & SPECIFIED_RIGHT) {
nscoord width = clientRect.width - offset.LeftRight() - margin.LeftRight();
childRect.width = clamped(width, min.width, max.width);
}
else {
nscoord width = child->GetXULPrefSize(aState).width;
childRect.width = clamped(width, min.width, max.width);
}
}
else if (offsetSpecified & SPECIFIED_RIGHT) {
nscoord width = child->GetXULPrefSize(aState).width;
childRect.width = clamped(width, min.width, max.width);
childRect.x = clientRect.XMost() - offset.right - margin.right - childRect.width;
}
if (offsetSpecified & SPECIFIED_TOP) {
childRect.y = clientRect.y + offset.top + margin.top;
if (offsetSpecified & SPECIFIED_BOTTOM) {
nscoord height = clientRect.height - offset.TopBottom() - margin.TopBottom();
childRect.height = clamped(height, min.height, max.height);
}
else {
nscoord height = child->GetXULPrefSize(aState).height;
childRect.height = clamped(height, min.height, max.height);
}
}
else if (offsetSpecified & SPECIFIED_BOTTOM) {
nscoord height = child->GetXULPrefSize(aState).height;
childRect.height = clamped(height, min.height, max.height);
childRect.y = clientRect.YMost() - offset.bottom - margin.bottom - childRect.height;
}
2001-09-06 05:07:40 +04:00
}
2001-09-06 05:07:40 +04:00
// Now place the child.
child->SetXULBounds(aState, childRect);
2001-09-06 05:07:40 +04:00
// Flow the child.
child->XULLayout(aState);
2001-09-06 05:07:40 +04:00
// Get the child's new rect.
childRect = child->GetRect();
childRect.Inflate(margin);
if (child->StyleXUL()->mStretchStack) {
// Did the child push back on us and get bigger?
if (offset.LeftRight() + childRect.width > clientRect.width) {
clientRect.width = childRect.width + offset.LeftRight();
grow = true;
}
if (offset.TopBottom() + childRect.height > clientRect.height) {
clientRect.height = childRect.height + offset.TopBottom();
grow = true;
}
}
2001-09-06 05:07:40 +04:00
}
2000-03-31 11:02:06 +04:00
child = nsBox::GetNextXULBox(child);
2001-09-06 05:07:40 +04:00
}
} while (grow);
2000-03-31 11:02:06 +04:00
// if some HTML inside us got bigger we need to force ourselves to
// get bigger
nsRect bounds(aBox->GetRect());
2000-03-31 11:02:06 +04:00
nsMargin bp;
aBox->GetXULBorderAndPadding(bp);
2000-03-31 11:02:06 +04:00
clientRect.Inflate(bp);
if (clientRect.width > bounds.width || clientRect.height > bounds.height)
{
2001-09-06 05:07:40 +04:00
if (clientRect.width > bounds.width)
bounds.width = clientRect.width;
if (clientRect.height > bounds.height)
bounds.height = clientRect.height;
2000-03-31 11:02:06 +04:00
aBox->SetXULBounds(aState, bounds);
2000-03-31 11:02:06 +04:00
}
return NS_OK;
}