2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsListBoxLayout.h"
|
|
|
|
|
|
|
|
#include "nsListBoxBodyFrame.h"
|
|
|
|
#include "nsBox.h"
|
|
|
|
#include "nsBoxLayoutState.h"
|
|
|
|
#include "nsIScrollableFrame.h"
|
|
|
|
#include "nsIReflowCallback.h"
|
2014-06-19 19:01:40 -07:00
|
|
|
#include "mozilla/dom/NameSpaceConstants.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
2009-01-19 10:31:33 -08:00
|
|
|
nsListBoxLayout::nsListBoxLayout() : nsGridRowGroupLayout()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-07-11 07:05:10 -07:00
|
|
|
////////// nsBoxLayout //////////////
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-08 23:28:39 -08:00
|
|
|
nsSize
|
2012-08-05 20:00:57 -07:00
|
|
|
nsListBoxLayout::GetPrefSize(nsIFrame* aBox, nsBoxLayoutState& aBoxLayoutState)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-01-08 23:28:39 -08:00
|
|
|
nsSize pref = nsGridRowGroupLayout::GetPrefSize(aBox, aBoxLayoutState);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
nsListBoxBodyFrame* frame = static_cast<nsListBoxBodyFrame*>(aBox);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (frame) {
|
|
|
|
nscoord rowheight = frame->GetRowHeightAppUnits();
|
2008-01-08 23:28:39 -08:00
|
|
|
pref.height = frame->GetRowCount() * rowheight;
|
2007-03-22 10:30:00 -07:00
|
|
|
// Pad the height.
|
|
|
|
nscoord y = frame->GetAvailableHeight();
|
2008-01-08 23:28:39 -08:00
|
|
|
if (pref.height > y && y > 0 && rowheight > 0) {
|
|
|
|
nscoord m = (pref.height-y)%rowheight;
|
2007-03-22 10:30:00 -07:00
|
|
|
nscoord remainder = m == 0 ? 0 : rowheight - m;
|
2008-01-08 23:28:39 -08:00
|
|
|
pref.height += remainder;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (nsContentUtils::HasNonEmptyAttr(frame->GetContent(), kNameSpaceID_None,
|
|
|
|
nsGkAtoms::sizemode)) {
|
|
|
|
nscoord width = frame->ComputeIntrinsicWidth(aBoxLayoutState);
|
2008-01-08 23:28:39 -08:00
|
|
|
if (width > pref.width)
|
|
|
|
pref.width = width;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2008-01-08 23:28:39 -08:00
|
|
|
return pref;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-01-08 23:28:39 -08:00
|
|
|
nsSize
|
2012-08-05 20:00:57 -07:00
|
|
|
nsListBoxLayout::GetMinSize(nsIFrame* aBox, nsBoxLayoutState& aBoxLayoutState)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-01-08 23:28:39 -08:00
|
|
|
nsSize minSize = nsGridRowGroupLayout::GetMinSize(aBox, aBoxLayoutState);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
nsListBoxBodyFrame* frame = static_cast<nsListBoxBodyFrame*>(aBox);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (frame) {
|
|
|
|
nscoord rowheight = frame->GetRowHeightAppUnits();
|
2008-01-08 23:28:39 -08:00
|
|
|
minSize.height = frame->GetRowCount() * rowheight;
|
2007-03-22 10:30:00 -07:00
|
|
|
// Pad the height.
|
|
|
|
nscoord y = frame->GetAvailableHeight();
|
2008-01-08 23:28:39 -08:00
|
|
|
if (minSize.height > y && y > 0 && rowheight > 0) {
|
|
|
|
nscoord m = (minSize.height-y)%rowheight;
|
2007-03-22 10:30:00 -07:00
|
|
|
nscoord remainder = m == 0 ? 0 : rowheight - m;
|
2008-01-08 23:28:39 -08:00
|
|
|
minSize.height += remainder;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (nsContentUtils::HasNonEmptyAttr(frame->GetContent(), kNameSpaceID_None,
|
|
|
|
nsGkAtoms::sizemode)) {
|
|
|
|
nscoord width = frame->ComputeIntrinsicWidth(aBoxLayoutState);
|
2008-01-08 23:28:39 -08:00
|
|
|
if (width > minSize.width)
|
|
|
|
minSize.width = width;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2008-01-08 23:28:39 -08:00
|
|
|
return minSize;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-01-08 23:28:39 -08:00
|
|
|
nsSize
|
2012-08-05 20:00:57 -07:00
|
|
|
nsListBoxLayout::GetMaxSize(nsIFrame* aBox, nsBoxLayoutState& aBoxLayoutState)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-01-08 23:28:39 -08:00
|
|
|
nsSize maxSize = nsGridRowGroupLayout::GetMaxSize(aBox, aBoxLayoutState);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
nsListBoxBodyFrame* frame = static_cast<nsListBoxBodyFrame*>(aBox);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (frame) {
|
|
|
|
nscoord rowheight = frame->GetRowHeightAppUnits();
|
2008-01-08 23:28:39 -08:00
|
|
|
maxSize.height = frame->GetRowCount() * rowheight;
|
2007-03-22 10:30:00 -07:00
|
|
|
// Pad the height.
|
|
|
|
nscoord y = frame->GetAvailableHeight();
|
2008-01-08 23:28:39 -08:00
|
|
|
if (maxSize.height > y && y > 0 && rowheight > 0) {
|
|
|
|
nscoord m = (maxSize.height-y)%rowheight;
|
2007-03-22 10:30:00 -07:00
|
|
|
nscoord remainder = m == 0 ? 0 : rowheight - m;
|
2008-01-08 23:28:39 -08:00
|
|
|
maxSize.height += remainder;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2008-01-08 23:28:39 -08:00
|
|
|
return maxSize;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-05 20:00:57 -07:00
|
|
|
nsListBoxLayout::Layout(nsIFrame* aBox, nsBoxLayoutState& aState)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-04-28 10:47:23 -07:00
|
|
|
return LayoutInternal(aBox, aState);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////// nsListBoxLayout /////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called to layout our our children. Does no frame construction
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
2012-08-05 20:00:57 -07:00
|
|
|
nsListBoxLayout::LayoutInternal(nsIFrame* aBox, nsBoxLayoutState& aState)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t redrawStart = -1;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Get the start y position.
|
2007-07-08 00:08:04 -07:00
|
|
|
nsListBoxBodyFrame* body = static_cast<nsListBoxBodyFrame*>(aBox);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!body) {
|
2010-06-17 13:28:38 -07:00
|
|
|
NS_ERROR("Frame encountered that isn't a listboxbody!");
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMargin margin;
|
|
|
|
|
|
|
|
// Get our client rect.
|
|
|
|
nsRect clientRect;
|
|
|
|
aBox->GetClientRect(clientRect);
|
|
|
|
|
|
|
|
// Get the starting y position and the remaining available
|
|
|
|
// height.
|
|
|
|
nscoord availableHeight = body->GetAvailableHeight();
|
|
|
|
nscoord yOffset = body->GetYPosition();
|
|
|
|
|
|
|
|
if (availableHeight <= 0) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool fixed = (body->GetFixedRowSize() != -1);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (fixed)
|
|
|
|
availableHeight = 10;
|
|
|
|
else
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// run through all our currently created children
|
2014-05-24 15:20:41 -07:00
|
|
|
nsIFrame* box = nsBox::GetChildBox(body);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// if the reason is resize or initial we must relayout.
|
|
|
|
nscoord rowHeight = body->GetRowHeightAppUnits();
|
|
|
|
|
|
|
|
while (box) {
|
|
|
|
// If this box is dirty or if it has dirty children, we
|
|
|
|
// call layout on it.
|
|
|
|
nsRect childRect(box->GetRect());
|
|
|
|
box->GetMargin(margin);
|
|
|
|
|
|
|
|
// relayout if we must or we are dirty or some of our children are dirty
|
|
|
|
// or the client area is wider than us
|
|
|
|
// XXXldb There should probably be a resize check here too!
|
2007-05-06 12:16:51 -07:00
|
|
|
if (NS_SUBTREE_DIRTY(box) || childRect.width < clientRect.width) {
|
2007-03-22 10:30:00 -07:00
|
|
|
childRect.x = 0;
|
|
|
|
childRect.y = yOffset;
|
|
|
|
childRect.width = clientRect.width;
|
|
|
|
|
|
|
|
nsSize size = box->GetPrefSize(aState);
|
|
|
|
body->SetRowHeight(size.height);
|
|
|
|
|
|
|
|
childRect.height = rowHeight;
|
|
|
|
|
|
|
|
childRect.Deflate(margin);
|
|
|
|
box->SetBounds(aState, childRect);
|
|
|
|
box->Layout(aState);
|
|
|
|
} else {
|
|
|
|
// if the child did not need to be relayed out. Then its easy.
|
|
|
|
// Place the child by just grabbing its rect and adjusting the y.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t newPos = yOffset+margin.top;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// are we pushing down or pulling up any rows?
|
|
|
|
// Then we may have to redraw everything below the moved
|
|
|
|
// rows.
|
|
|
|
if (redrawStart == -1 && childRect.y != newPos)
|
|
|
|
redrawStart = newPos;
|
|
|
|
|
|
|
|
childRect.y = newPos;
|
|
|
|
box->SetBounds(aState, childRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok now the available size gets smaller and we move the
|
|
|
|
// starting position of the next child down some.
|
|
|
|
nscoord size = childRect.height + margin.top + margin.bottom;
|
|
|
|
|
|
|
|
yOffset += size;
|
|
|
|
availableHeight -= size;
|
|
|
|
|
2014-05-24 15:20:41 -07:00
|
|
|
box = nsBox::GetNextBox(box);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// We have enough available height left to add some more rows
|
|
|
|
// Since we can't do this during layout, we post a callback
|
|
|
|
// that will be processed after the reflow completes.
|
|
|
|
body->PostReflowCallback();
|
|
|
|
|
|
|
|
// if rows were pushed down or pulled up because some rows were added
|
|
|
|
// before them then redraw everything under the inserted rows. The inserted
|
|
|
|
// rows will automatically be redrawn because the were marked dirty on insertion.
|
|
|
|
if (redrawStart > -1) {
|
2012-08-28 22:39:31 -07:00
|
|
|
aBox->Redraw(aState);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creation Routines ///////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-07-11 07:05:10 -07:00
|
|
|
already_AddRefed<nsBoxLayout> NS_NewListBoxLayout()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-04-22 04:15:59 -07:00
|
|
|
nsRefPtr<nsBoxLayout> layout = new nsListBoxLayout();
|
|
|
|
return layout.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|