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 "nsCOMPtr.h"
|
|
|
|
#include "nsPIListBoxObject.h"
|
|
|
|
#include "nsBoxObject.h"
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsBindingManager.h"
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIDOMNodeList.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsIScrollableFrame.h"
|
2008-08-24 08:14:09 -07:00
|
|
|
#include "nsListBoxBodyFrame.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
class nsListBoxObject : public nsPIListBoxObject, public nsBoxObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_NSILISTBOXOBJECT
|
|
|
|
|
|
|
|
// nsPIListBoxObject
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual nsListBoxBodyFrame* GetListBoxBody(bool aFlush);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsListBoxObject();
|
|
|
|
|
|
|
|
// nsPIBoxObject
|
|
|
|
virtual void Clear();
|
|
|
|
virtual void ClearCachedValues();
|
|
|
|
|
|
|
|
protected:
|
2008-10-27 21:47:19 -07:00
|
|
|
nsListBoxBodyFrame *mListBoxBody;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED2(nsListBoxObject, nsBoxObject, nsIListBoxObject,
|
|
|
|
nsPIListBoxObject)
|
|
|
|
|
|
|
|
nsListBoxObject::nsListBoxObject()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mListBoxBody(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIListBoxObject
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsListBoxObject::GetRowCount(PRInt32 *aResult)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
nsListBoxBodyFrame* body = GetListBoxBody(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (body)
|
|
|
|
return body->GetRowCount(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsListBoxObject::GetNumberOfVisibleRows(PRInt32 *aResult)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
nsListBoxBodyFrame* body = GetListBoxBody(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (body)
|
|
|
|
return body->GetNumberOfVisibleRows(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsListBoxObject::GetIndexOfFirstVisibleRow(PRInt32 *aResult)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
nsListBoxBodyFrame* body = GetListBoxBody(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (body)
|
|
|
|
return body->GetIndexOfFirstVisibleRow(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsListBoxObject::EnsureIndexIsVisible(PRInt32 aRowIndex)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
nsListBoxBodyFrame* body = GetListBoxBody(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (body)
|
|
|
|
return body->EnsureIndexIsVisible(aRowIndex);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsListBoxObject::ScrollToIndex(PRInt32 aRowIndex)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
nsListBoxBodyFrame* body = GetListBoxBody(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (body)
|
|
|
|
return body->ScrollToIndex(aRowIndex);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsListBoxObject::ScrollByLines(PRInt32 aNumLines)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
nsListBoxBodyFrame* body = GetListBoxBody(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (body)
|
|
|
|
return body->ScrollByLines(aNumLines);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsListBoxObject::GetItemAtIndex(PRInt32 index, nsIDOMElement **_retval)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
nsListBoxBodyFrame* body = GetListBoxBody(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (body)
|
|
|
|
return body->GetItemAtIndex(index, _retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsListBoxObject::GetIndexOfItem(nsIDOMElement* aElement, PRInt32 *aResult)
|
|
|
|
{
|
|
|
|
*aResult = 0;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
nsListBoxBodyFrame* body = GetListBoxBody(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (body)
|
|
|
|
return body->GetIndexOfItem(aElement, aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////
|
|
|
|
|
|
|
|
static void
|
|
|
|
FindBodyContent(nsIContent* aParent, nsIContent** aResult)
|
|
|
|
{
|
|
|
|
if (aParent->Tag() == nsGkAtoms::listboxbody) {
|
|
|
|
*aResult = aParent;
|
|
|
|
NS_IF_ADDREF(*aResult);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nsCOMPtr<nsIDOMNodeList> kids;
|
2011-10-18 03:53:36 -07:00
|
|
|
aParent->OwnerDoc()->BindingManager()->GetXBLChildNodesFor(aParent, getter_AddRefs(kids));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!kids) return;
|
|
|
|
|
|
|
|
PRUint32 i;
|
|
|
|
kids->GetLength(&i);
|
|
|
|
// start from the end, cuz we're smart and we know the listboxbody is probably at the end
|
|
|
|
while (i > 0) {
|
|
|
|
nsCOMPtr<nsIDOMNode> childNode;
|
|
|
|
kids->Item(--i, getter_AddRefs(childNode));
|
|
|
|
nsCOMPtr<nsIContent> childContent(do_QueryInterface(childNode));
|
|
|
|
FindBodyContent(childContent, aResult);
|
|
|
|
if (*aResult)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-27 21:47:19 -07:00
|
|
|
nsListBoxBodyFrame*
|
2011-09-28 23:19:26 -07:00
|
|
|
nsListBoxObject::GetListBoxBody(bool aFlush)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mListBoxBody) {
|
|
|
|
return mListBoxBody;
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
nsIPresShell* shell = GetPresShell(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!shell) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-10-06 08:03:35 -07:00
|
|
|
nsIFrame* frame = aFlush ?
|
2011-10-17 07:59:28 -07:00
|
|
|
GetFrame(false) /* does Flush_Frames */ :
|
2009-12-24 13:20:06 -08:00
|
|
|
mContent->GetPrimaryFrame();
|
2007-10-06 08:03:35 -07:00
|
|
|
if (!frame)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-10-06 08:03:35 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Iterate over our content model children looking for the body.
|
|
|
|
nsCOMPtr<nsIContent> content;
|
|
|
|
FindBodyContent(frame->GetContent(), getter_AddRefs(content));
|
|
|
|
|
2009-12-28 11:47:27 -08:00
|
|
|
if (!content)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2009-12-28 11:47:27 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// this frame will be a nsGFXScrollFrame
|
2009-12-24 13:20:06 -08:00
|
|
|
frame = content->GetPrimaryFrame();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!frame)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2009-01-12 11:20:59 -08:00
|
|
|
nsIScrollableFrame* scrollFrame = do_QueryFrame(frame);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!scrollFrame)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// this frame will be the one we want
|
|
|
|
nsIFrame* yeahBaby = scrollFrame->GetScrolledFrame();
|
|
|
|
if (!yeahBaby)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// It's a frame. Refcounts are irrelevant.
|
2009-01-12 11:20:59 -08:00
|
|
|
nsListBoxBodyFrame* listBoxBody = do_QueryFrame(yeahBaby);
|
2008-08-24 08:14:09 -07:00
|
|
|
NS_ENSURE_TRUE(listBoxBody &&
|
2008-10-27 21:47:19 -07:00
|
|
|
listBoxBody->SetBoxObject(this),
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr);
|
2008-08-24 08:14:09 -07:00
|
|
|
mListBoxBody = listBoxBody;
|
2007-03-22 10:30:00 -07:00
|
|
|
return mListBoxBody;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsListBoxObject::Clear()
|
|
|
|
{
|
|
|
|
ClearCachedValues();
|
|
|
|
|
|
|
|
nsBoxObject::Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsListBoxObject::ClearCachedValues()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mListBoxBody = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creation Routine ///////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewListBoxObject(nsIBoxObject** aResult)
|
|
|
|
{
|
|
|
|
*aResult = new nsListBoxObject;
|
|
|
|
if (!*aResult)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|