2002-01-12 04:18:10 +03:00
|
|
|
/* -*- 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/. */
|
2002-01-12 04:18:10 +03:00
|
|
|
|
2014-10-15 00:15:21 +04:00
|
|
|
#include "mozilla/dom/ListBoxObject.h"
|
2002-01-12 04:18:10 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIFrame.h"
|
2006-12-26 20:47:52 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2005-04-04 01:02:47 +04:00
|
|
|
#include "nsIScrollableFrame.h"
|
2008-08-24 19:14:09 +04:00
|
|
|
#include "nsListBoxBodyFrame.h"
|
2013-05-02 02:50:08 +04:00
|
|
|
#include "ChildIterator.h"
|
2014-10-15 00:15:21 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "mozilla/dom/ListBoxObjectBinding.h"
|
2002-01-12 04:18:10 +03:00
|
|
|
|
2014-10-15 00:15:21 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2014-07-09 01:23:18 +04:00
|
|
|
|
2014-10-15 00:15:21 +04:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(ListBoxObject, BoxObject, nsIListBoxObject,
|
2014-04-27 11:06:00 +04:00
|
|
|
nsPIListBoxObject)
|
2002-01-12 04:18:10 +03:00
|
|
|
|
2014-10-15 00:15:21 +04:00
|
|
|
ListBoxObject::ListBoxObject()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mListBoxBody(nullptr)
|
2002-01-12 04:18:10 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-10-15 00:15:21 +04:00
|
|
|
ListBoxObject::~ListBoxObject()
|
|
|
|
{
|
|
|
|
}
|
2002-01-12 04:18:10 +03:00
|
|
|
|
2014-10-15 00:15:21 +04:00
|
|
|
JSObject* ListBoxObject::WrapObject(JSContext* aCx)
|
|
|
|
{
|
|
|
|
return ListBoxObjectBinding::Wrap(aCx, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIListBoxObject
|
2002-01-12 04:18:10 +03:00
|
|
|
NS_IMETHODIMP
|
2014-10-15 00:15:21 +04:00
|
|
|
ListBoxObject::GetRowCount(int32_t *aResult)
|
2002-01-12 04:18:10 +03:00
|
|
|
{
|
2014-10-15 00:15:21 +04:00
|
|
|
*aResult = GetRowCount();
|
2002-01-12 04:18:10 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-15 00:15:21 +04:00
|
|
|
ListBoxObject::GetItemAtIndex(int32_t index, nsIDOMElement **_retval)
|
2002-01-12 04:18:10 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
nsListBoxBodyFrame* body = GetListBoxBody(true);
|
2014-10-15 00:15:21 +04:00
|
|
|
if (body) {
|
|
|
|
return body->GetItemAtIndex(index, _retval);
|
|
|
|
}
|
2002-01-12 04:18:10 +03:00
|
|
|
return NS_OK;
|
2014-10-15 00:15:21 +04:00
|
|
|
}
|
2002-01-12 04:18:10 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-15 00:15:21 +04:00
|
|
|
ListBoxObject::GetIndexOfItem(nsIDOMElement* aElement, int32_t *aResult)
|
2002-01-12 04:18:10 +03:00
|
|
|
{
|
2014-10-15 00:15:21 +04:00
|
|
|
*aResult = 0;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
nsListBoxBodyFrame* body = GetListBoxBody(true);
|
2014-10-15 00:15:21 +04:00
|
|
|
if (body) {
|
|
|
|
return body->GetIndexOfItem(aElement, aResult);
|
|
|
|
}
|
2002-01-12 04:18:10 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-15 00:15:21 +04:00
|
|
|
// ListBoxObject
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
ListBoxObject::GetRowCount()
|
2002-01-12 04:18:10 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
nsListBoxBodyFrame* body = GetListBoxBody(true);
|
2014-10-15 00:15:21 +04:00
|
|
|
if (body) {
|
|
|
|
return body->GetRowCount();
|
|
|
|
}
|
|
|
|
return 0;
|
2002-01-12 04:18:10 +03:00
|
|
|
}
|
|
|
|
|
2014-10-15 00:15:21 +04:00
|
|
|
int32_t
|
|
|
|
ListBoxObject::GetNumberOfVisibleRows()
|
2002-01-12 04:18:10 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
nsListBoxBodyFrame* body = GetListBoxBody(true);
|
2014-10-15 00:15:21 +04:00
|
|
|
if (body) {
|
|
|
|
return body->GetNumberOfVisibleRows();
|
|
|
|
}
|
|
|
|
return 0;
|
2002-01-12 04:18:10 +03:00
|
|
|
}
|
|
|
|
|
2014-10-15 00:15:21 +04:00
|
|
|
int32_t
|
|
|
|
ListBoxObject::GetIndexOfFirstVisibleRow()
|
2002-01-12 04:18:10 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
nsListBoxBodyFrame* body = GetListBoxBody(true);
|
2014-10-15 00:15:21 +04:00
|
|
|
if (body) {
|
|
|
|
return body->GetIndexOfFirstVisibleRow();
|
|
|
|
}
|
|
|
|
return 0;
|
2002-01-12 04:18:10 +03:00
|
|
|
}
|
|
|
|
|
2014-10-15 00:15:21 +04:00
|
|
|
void
|
|
|
|
ListBoxObject::EnsureIndexIsVisible(int32_t aRowIndex)
|
2002-01-12 04:18:10 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
nsListBoxBodyFrame* body = GetListBoxBody(true);
|
2014-10-15 00:15:21 +04:00
|
|
|
if (body) {
|
|
|
|
body->EnsureIndexIsVisible(aRowIndex);
|
|
|
|
}
|
2002-01-12 04:18:10 +03:00
|
|
|
}
|
|
|
|
|
2014-10-15 00:15:21 +04:00
|
|
|
void
|
|
|
|
ListBoxObject::ScrollToIndex(int32_t aRowIndex)
|
2002-01-12 04:18:10 +03:00
|
|
|
{
|
2014-10-15 00:15:21 +04:00
|
|
|
nsListBoxBodyFrame* body = GetListBoxBody(true);
|
|
|
|
if (body) {
|
|
|
|
body->ScrollToIndex(aRowIndex);
|
|
|
|
}
|
|
|
|
}
|
2002-01-12 04:18:10 +03:00
|
|
|
|
2014-10-15 00:15:21 +04:00
|
|
|
void
|
|
|
|
ListBoxObject::ScrollByLines(int32_t aNumLines)
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
nsListBoxBodyFrame* body = GetListBoxBody(true);
|
2014-10-15 00:15:21 +04:00
|
|
|
if (body) {
|
|
|
|
body->ScrollByLines(aNumLines);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Element>
|
|
|
|
ListBoxObject::GetItemAtIndex(int32_t index)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMElement> el;
|
|
|
|
GetItemAtIndex(index, getter_AddRefs(el));
|
|
|
|
nsCOMPtr<Element> ret(do_QueryInterface(el));
|
|
|
|
return ret.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
ListBoxObject::GetIndexOfItem(Element& aElement)
|
|
|
|
{
|
|
|
|
int32_t ret;
|
|
|
|
nsCOMPtr<nsIDOMElement> el(do_QueryInterface(&aElement));
|
|
|
|
GetIndexOfItem(el, &ret);
|
|
|
|
return ret;
|
2002-01-12 04:18:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////
|
|
|
|
|
2013-05-02 02:50:08 +04:00
|
|
|
static nsIContent*
|
|
|
|
FindBodyContent(nsIContent* aParent)
|
2002-01-12 04:18:10 +03:00
|
|
|
{
|
2015-03-03 14:09:00 +03:00
|
|
|
if (aParent->IsXULElement(nsGkAtoms::listboxbody)) {
|
2013-05-02 02:50:08 +04:00
|
|
|
return aParent;
|
2002-01-12 04:18:10 +03:00
|
|
|
}
|
2013-05-02 02:50:08 +04:00
|
|
|
|
|
|
|
mozilla::dom::FlattenedChildIterator iter(aParent);
|
|
|
|
for (nsIContent* child = iter.GetNextChild(); child; child = iter.GetNextChild()) {
|
|
|
|
nsIContent* result = FindBodyContent(child);
|
|
|
|
if (result) {
|
|
|
|
return result;
|
2002-01-12 04:18:10 +03:00
|
|
|
}
|
|
|
|
}
|
2013-05-02 02:50:08 +04:00
|
|
|
|
|
|
|
return nullptr;
|
2002-01-12 04:18:10 +03:00
|
|
|
}
|
|
|
|
|
2008-10-28 07:47:19 +03:00
|
|
|
nsListBoxBodyFrame*
|
2014-10-15 00:15:21 +04:00
|
|
|
ListBoxObject::GetListBoxBody(bool aFlush)
|
2002-01-12 04:18:10 +03:00
|
|
|
{
|
2006-01-31 00:29:10 +03:00
|
|
|
if (mListBoxBody) {
|
|
|
|
return mListBoxBody;
|
2002-01-12 04:18:10 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
nsIPresShell* shell = GetPresShell(false);
|
2006-03-06 04:46:46 +03:00
|
|
|
if (!shell) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2006-03-06 04:46:46 +03:00
|
|
|
}
|
|
|
|
|
2014-10-15 00:15:21 +04:00
|
|
|
nsIFrame* frame = aFlush ?
|
2011-10-17 18:59:28 +04:00
|
|
|
GetFrame(false) /* does Flush_Frames */ :
|
2009-12-25 00:20:06 +03:00
|
|
|
mContent->GetPrimaryFrame();
|
2014-10-15 00:15:21 +04:00
|
|
|
if (!frame) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2014-10-15 00:15:21 +04:00
|
|
|
}
|
2007-10-06 19:03:35 +04:00
|
|
|
|
2002-01-12 04:18:10 +03:00
|
|
|
// Iterate over our content model children looking for the body.
|
2013-05-02 02:50:08 +04:00
|
|
|
nsCOMPtr<nsIContent> content = FindBodyContent(frame->GetContent());
|
2002-01-12 04:18:10 +03:00
|
|
|
|
2014-10-15 00:15:21 +04:00
|
|
|
if (!content) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2014-10-15 00:15:21 +04:00
|
|
|
}
|
2009-12-28 22:47:27 +03:00
|
|
|
|
2002-01-12 04:18:10 +03:00
|
|
|
// this frame will be a nsGFXScrollFrame
|
2009-12-25 00:20:06 +03:00
|
|
|
frame = content->GetPrimaryFrame();
|
2014-10-15 00:15:21 +04:00
|
|
|
if (!frame) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2014-10-15 00:15:21 +04:00
|
|
|
}
|
|
|
|
|
2009-01-12 22:20:59 +03:00
|
|
|
nsIScrollableFrame* scrollFrame = do_QueryFrame(frame);
|
2014-10-15 00:15:21 +04:00
|
|
|
if (!scrollFrame) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2014-10-15 00:15:21 +04:00
|
|
|
}
|
2002-01-12 04:18:10 +03:00
|
|
|
|
|
|
|
// this frame will be the one we want
|
2005-04-04 01:02:47 +04:00
|
|
|
nsIFrame* yeahBaby = scrollFrame->GetScrolledFrame();
|
2014-10-15 00:15:21 +04:00
|
|
|
if (!yeahBaby) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2014-10-15 00:15:21 +04:00
|
|
|
}
|
2002-01-12 04:18:10 +03:00
|
|
|
|
|
|
|
// It's a frame. Refcounts are irrelevant.
|
2009-01-12 22:20:59 +03:00
|
|
|
nsListBoxBodyFrame* listBoxBody = do_QueryFrame(yeahBaby);
|
2008-08-24 19:14:09 +04:00
|
|
|
NS_ENSURE_TRUE(listBoxBody &&
|
2008-10-28 07:47:19 +03:00
|
|
|
listBoxBody->SetBoxObject(this),
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr);
|
2008-08-24 19:14:09 +04:00
|
|
|
mListBoxBody = listBoxBody;
|
2006-01-31 00:29:10 +03:00
|
|
|
return mListBoxBody;
|
2002-01-12 04:18:10 +03:00
|
|
|
}
|
|
|
|
|
2006-03-07 06:20:39 +03:00
|
|
|
void
|
2014-10-15 00:15:21 +04:00
|
|
|
ListBoxObject::Clear()
|
2002-04-23 03:48:14 +04:00
|
|
|
{
|
2006-07-13 13:02:05 +04:00
|
|
|
ClearCachedValues();
|
2014-10-15 00:15:21 +04:00
|
|
|
BoxObject::Clear();
|
2002-04-23 03:48:14 +04:00
|
|
|
}
|
|
|
|
|
2006-07-13 13:02:05 +04:00
|
|
|
void
|
2014-10-15 00:15:21 +04:00
|
|
|
ListBoxObject::ClearCachedValues()
|
2006-07-13 13:02:05 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mListBoxBody = nullptr;
|
2006-07-13 13:02:05 +04:00
|
|
|
}
|
|
|
|
|
2014-10-15 00:15:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-12 04:18:10 +03:00
|
|
|
// Creation Routine ///////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewListBoxObject(nsIBoxObject** aResult)
|
|
|
|
{
|
2014-10-15 00:15:21 +04:00
|
|
|
NS_ADDREF(*aResult = new mozilla::dom::ListBoxObject());
|
2002-01-12 04:18:10 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|