2001-09-29 00:14:13 +04: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/. */
|
2001-02-14 22:39:20 +03:00
|
|
|
|
2014-03-17 10:56:54 +04:00
|
|
|
#include "mozilla/AsyncEventDispatcher.h"
|
2001-02-14 22:39:20 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2002-03-29 05:46:01 +03:00
|
|
|
#include "nsTreeSelection.h"
|
2002-01-16 06:01:28 +03:00
|
|
|
#include "nsIBoxObject.h"
|
2002-03-29 05:46:01 +03:00
|
|
|
#include "nsITreeBoxObject.h"
|
|
|
|
#include "nsITreeView.h"
|
2001-02-15 12:43:11 +03:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsIDOMElement.h"
|
2011-10-03 23:11:31 +04:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2001-02-15 12:43:11 +03:00
|
|
|
#include "nsIContent.h"
|
2014-02-28 03:04:46 +04:00
|
|
|
#include "nsNameSpaceManager.h"
|
2006-12-26 20:47:52 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2013-09-06 21:50:24 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2001-02-14 22:39:20 +03:00
|
|
|
|
2014-03-17 10:56:54 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2001-02-14 23:16:02 +03:00
|
|
|
// A helper class for managing our ranges of selection.
|
2002-03-29 05:46:01 +03:00
|
|
|
struct nsTreeRange
|
2001-02-14 23:16:02 +03:00
|
|
|
{
|
2002-03-29 05:46:01 +03:00
|
|
|
nsTreeSelection* mSelection;
|
2001-02-14 23:16:02 +03:00
|
|
|
|
2002-03-29 05:46:01 +03:00
|
|
|
nsTreeRange* mPrev;
|
|
|
|
nsTreeRange* mNext;
|
2001-02-14 23:16:02 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mMin;
|
|
|
|
int32_t mMax;
|
2001-02-15 00:38:17 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTreeRange(nsTreeSelection* aSel, int32_t aSingleVal)
|
2012-07-30 18:20:58 +04:00
|
|
|
:mSelection(aSel), mPrev(nullptr), mNext(nullptr), mMin(aSingleVal), mMax(aSingleVal) {}
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTreeRange(nsTreeSelection* aSel, int32_t aMin, int32_t aMax)
|
2012-07-30 18:20:58 +04:00
|
|
|
:mSelection(aSel), mPrev(nullptr), mNext(nullptr), mMin(aMin), mMax(aMax) {}
|
2001-02-14 23:16:02 +03:00
|
|
|
|
2007-04-23 18:21:53 +04:00
|
|
|
~nsTreeRange() { delete mNext; }
|
2001-02-14 23:16:02 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
void Connect(nsTreeRange* aPrev = nullptr, nsTreeRange* aNext = nullptr) {
|
2001-02-15 12:43:11 +03:00
|
|
|
if (aPrev)
|
|
|
|
aPrev->mNext = this;
|
|
|
|
else
|
|
|
|
mSelection->mFirstRange = this;
|
|
|
|
|
|
|
|
if (aNext)
|
|
|
|
aNext->mPrev = this;
|
|
|
|
|
2001-02-14 23:16:02 +03:00
|
|
|
mPrev = aPrev;
|
|
|
|
mNext = aNext;
|
2007-04-23 18:21:53 +04:00
|
|
|
}
|
2001-02-14 23:26:18 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult RemoveRange(int32_t aStart, int32_t aEnd) {
|
2004-05-22 02:48:12 +04:00
|
|
|
// This should so be a loop... sigh...
|
|
|
|
// We start past the range to remove, so no more to remove
|
|
|
|
if (aEnd < mMin)
|
|
|
|
return NS_OK;
|
|
|
|
// We are the last range to be affected
|
|
|
|
if (aEnd < mMax) {
|
|
|
|
if (aStart <= mMin) {
|
|
|
|
// Just chop the start of the range off
|
|
|
|
mMin = aEnd + 1;
|
|
|
|
} else {
|
|
|
|
// We need to split the range
|
|
|
|
nsTreeRange* range = new nsTreeRange(mSelection, aEnd + 1, mMax);
|
2003-12-24 21:06:20 +03:00
|
|
|
if (!range)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2004-05-22 02:48:12 +04:00
|
|
|
mMax = aStart - 1;
|
2001-02-16 00:15:21 +03:00
|
|
|
range->Connect(this, mNext);
|
|
|
|
}
|
2004-05-22 02:48:12 +04:00
|
|
|
return NS_OK;
|
2001-02-16 00:15:21 +03:00
|
|
|
}
|
2004-05-22 02:48:12 +04:00
|
|
|
nsTreeRange* next = mNext;
|
|
|
|
if (aStart <= mMin) {
|
|
|
|
// The remove includes us, remove ourselves from the list
|
|
|
|
if (mPrev)
|
|
|
|
mPrev->mNext = next;
|
|
|
|
else
|
|
|
|
mSelection->mFirstRange = next;
|
|
|
|
|
|
|
|
if (next)
|
|
|
|
next->mPrev = mPrev;
|
2012-07-30 18:20:58 +04:00
|
|
|
mPrev = mNext = nullptr;
|
2004-05-22 02:48:12 +04:00
|
|
|
delete this;
|
|
|
|
} else if (aStart <= mMax) {
|
|
|
|
// Just chop the end of the range off
|
|
|
|
mMax = aStart - 1;
|
2001-02-16 00:15:21 +03:00
|
|
|
}
|
2004-05-22 02:48:12 +04:00
|
|
|
return next ? next->RemoveRange(aStart, aEnd) : NS_OK;
|
2007-04-23 18:21:53 +04:00
|
|
|
}
|
2001-02-16 00:15:21 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult Remove(int32_t aIndex) {
|
2001-02-15 12:43:11 +03:00
|
|
|
if (aIndex >= mMin && aIndex <= mMax) {
|
|
|
|
// We have found the range that contains us.
|
|
|
|
if (mMin == mMax) {
|
|
|
|
// Delete the whole range.
|
|
|
|
if (mPrev)
|
|
|
|
mPrev->mNext = mNext;
|
|
|
|
if (mNext)
|
|
|
|
mNext->mPrev = mPrev;
|
2002-03-29 05:46:01 +03:00
|
|
|
nsTreeRange* first = mSelection->mFirstRange;
|
2001-02-15 12:43:11 +03:00
|
|
|
if (first == this)
|
|
|
|
mSelection->mFirstRange = mNext;
|
2012-07-30 18:20:58 +04:00
|
|
|
mNext = mPrev = nullptr;
|
2001-02-15 12:43:11 +03:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
else if (aIndex == mMin)
|
|
|
|
mMin++;
|
|
|
|
else if (aIndex == mMax)
|
|
|
|
mMax--;
|
2001-03-06 05:27:50 +03:00
|
|
|
else {
|
|
|
|
// We have to break this range.
|
2002-03-29 05:46:01 +03:00
|
|
|
nsTreeRange* newRange = new nsTreeRange(mSelection, aIndex + 1, mMax);
|
2003-12-24 21:06:20 +03:00
|
|
|
if (!newRange)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2001-03-06 05:27:50 +03:00
|
|
|
newRange->Connect(this, mNext);
|
|
|
|
mMax = aIndex - 1;
|
|
|
|
}
|
2001-02-15 12:43:11 +03:00
|
|
|
}
|
|
|
|
else if (mNext)
|
2003-12-24 21:06:20 +03:00
|
|
|
return mNext->Remove(aIndex);
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-04-23 18:21:53 +04:00
|
|
|
}
|
2001-02-15 12:43:11 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult Add(int32_t aIndex) {
|
2001-02-15 12:43:11 +03:00
|
|
|
if (aIndex < mMin) {
|
|
|
|
// We have found a spot to insert.
|
|
|
|
if (aIndex + 1 == mMin)
|
|
|
|
mMin = aIndex;
|
|
|
|
else if (mPrev && mPrev->mMax+1 == aIndex)
|
|
|
|
mPrev->mMax = aIndex;
|
|
|
|
else {
|
|
|
|
// We have to create a new range.
|
2002-03-29 05:46:01 +03:00
|
|
|
nsTreeRange* newRange = new nsTreeRange(mSelection, aIndex);
|
2003-12-24 21:06:20 +03:00
|
|
|
if (!newRange)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2001-02-15 12:43:11 +03:00
|
|
|
newRange->Connect(mPrev, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (mNext)
|
|
|
|
mNext->Add(aIndex);
|
|
|
|
else {
|
|
|
|
// Insert on to the end.
|
|
|
|
if (mMax+1 == aIndex)
|
|
|
|
mMax = aIndex;
|
|
|
|
else {
|
|
|
|
// We have to create a new range.
|
2002-03-29 05:46:01 +03:00
|
|
|
nsTreeRange* newRange = new nsTreeRange(mSelection, aIndex);
|
2003-12-24 21:06:20 +03:00
|
|
|
if (!newRange)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
newRange->Connect(this, nullptr);
|
2001-02-15 12:43:11 +03:00
|
|
|
}
|
|
|
|
}
|
2003-12-24 21:06:20 +03:00
|
|
|
return NS_OK;
|
2007-04-23 18:21:53 +04:00
|
|
|
}
|
2001-02-15 12:43:11 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
bool Contains(int32_t aIndex) {
|
2001-02-14 23:26:18 +03:00
|
|
|
if (aIndex >= mMin && aIndex <= mMax)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2001-02-14 23:26:18 +03:00
|
|
|
|
|
|
|
if (mNext)
|
|
|
|
return mNext->Contains(aIndex);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-04-23 18:21:53 +04:00
|
|
|
}
|
2001-02-15 00:38:17 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t Count() {
|
|
|
|
int32_t total = mMax - mMin + 1;
|
2001-02-15 00:38:17 +03:00
|
|
|
if (mNext)
|
|
|
|
total += mNext->Count();
|
|
|
|
return total;
|
2007-04-23 18:21:53 +04:00
|
|
|
}
|
2001-02-15 00:38:17 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static void CollectRanges(nsTreeRange* aRange, nsTArray<int32_t>& aRanges)
|
2011-03-01 15:23:26 +03:00
|
|
|
{
|
|
|
|
nsTreeRange* cur = aRange;
|
|
|
|
while (cur) {
|
|
|
|
aRanges.AppendElement(cur->mMin);
|
|
|
|
aRanges.AppendElement(cur->mMax);
|
|
|
|
cur = cur->mNext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void InvalidateRanges(nsITreeBoxObject* aTree,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<int32_t>& aRanges)
|
2011-03-01 15:23:26 +03:00
|
|
|
{
|
|
|
|
if (aTree) {
|
|
|
|
nsCOMPtr<nsITreeBoxObject> tree = aTree;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < aRanges.Length(); i += 2) {
|
2011-03-01 15:23:26 +03:00
|
|
|
aTree->InvalidateRange(aRanges[i], aRanges[i + 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-15 00:38:17 +03:00
|
|
|
void Invalidate() {
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<int32_t> ranges;
|
2011-03-01 15:23:26 +03:00
|
|
|
CollectRanges(this, ranges);
|
|
|
|
InvalidateRanges(mSelection->mTree, ranges);
|
|
|
|
|
2007-04-23 18:21:53 +04:00
|
|
|
}
|
2001-02-15 00:38:17 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void RemoveAllBut(int32_t aIndex) {
|
2001-02-15 00:38:17 +03:00
|
|
|
if (aIndex >= mMin && aIndex <= mMax) {
|
2001-02-15 12:43:11 +03:00
|
|
|
|
|
|
|
// Invalidate everything in this list.
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<int32_t> ranges;
|
2011-03-01 15:23:26 +03:00
|
|
|
CollectRanges(mSelection->mFirstRange, ranges);
|
2001-02-15 12:43:11 +03:00
|
|
|
|
2001-02-15 00:38:17 +03:00
|
|
|
mMin = aIndex;
|
|
|
|
mMax = aIndex;
|
|
|
|
|
2002-03-29 05:46:01 +03:00
|
|
|
nsTreeRange* first = mSelection->mFirstRange;
|
2001-02-15 00:38:17 +03:00
|
|
|
if (mPrev)
|
|
|
|
mPrev->mNext = mNext;
|
|
|
|
if (mNext)
|
|
|
|
mNext->mPrev = mPrev;
|
2012-07-30 18:20:58 +04:00
|
|
|
mNext = mPrev = nullptr;
|
2001-02-15 00:38:17 +03:00
|
|
|
|
2001-02-15 12:43:11 +03:00
|
|
|
if (first != this) {
|
|
|
|
delete mSelection->mFirstRange;
|
|
|
|
mSelection->mFirstRange = this;
|
|
|
|
}
|
2011-03-01 15:23:26 +03:00
|
|
|
InvalidateRanges(mSelection->mTree, ranges);
|
2001-02-15 00:38:17 +03:00
|
|
|
}
|
|
|
|
else if (mNext)
|
|
|
|
mNext->RemoveAllBut(aIndex);
|
2007-04-23 18:21:53 +04:00
|
|
|
}
|
2001-02-16 00:15:21 +03:00
|
|
|
|
2002-03-29 05:46:01 +03:00
|
|
|
void Insert(nsTreeRange* aRange) {
|
2001-02-16 00:15:21 +03:00
|
|
|
if (mMin >= aRange->mMax)
|
|
|
|
aRange->Connect(mPrev, this);
|
|
|
|
else if (mNext)
|
|
|
|
mNext->Insert(aRange);
|
|
|
|
else
|
2012-07-30 18:20:58 +04:00
|
|
|
aRange->Connect(this, nullptr);
|
2007-04-23 18:21:53 +04:00
|
|
|
}
|
2001-02-14 23:16:02 +03:00
|
|
|
};
|
|
|
|
|
2002-03-29 05:46:01 +03:00
|
|
|
nsTreeSelection::nsTreeSelection(nsITreeBoxObject* aTree)
|
2006-06-07 00:05:13 +04:00
|
|
|
: mTree(aTree),
|
2011-10-17 18:59:28 +04:00
|
|
|
mSuppressed(false),
|
2006-06-07 00:05:13 +04:00
|
|
|
mCurrentIndex(-1),
|
|
|
|
mShiftSelectPivot(-1),
|
2012-07-30 18:20:58 +04:00
|
|
|
mFirstRange(nullptr)
|
2001-02-14 22:39:20 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-03-29 05:46:01 +03:00
|
|
|
nsTreeSelection::~nsTreeSelection()
|
2001-02-14 22:39:20 +03:00
|
|
|
{
|
2001-02-15 00:38:17 +03:00
|
|
|
delete mFirstRange;
|
2009-03-24 18:43:15 +03:00
|
|
|
if (mSelectTimer)
|
|
|
|
mSelectTimer->Cancel();
|
2001-02-14 22:39:20 +03:00
|
|
|
}
|
|
|
|
|
2014-04-25 20:49:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(nsTreeSelection, mTree, mCurrentColumn)
|
2009-08-21 16:50:15 +04:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsTreeSelection)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsTreeSelection)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsTreeSelection)
|
2002-03-29 05:46:01 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsITreeSelection)
|
2010-07-31 22:22:29 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsINativeTreeSelection)
|
2001-05-16 12:13:12 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2009-02-17 17:08:08 +03:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(TreeSelection)
|
2001-05-16 12:13:12 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2002-03-29 05:46:01 +03:00
|
|
|
NS_IMETHODIMP nsTreeSelection::GetTree(nsITreeBoxObject * *aTree)
|
2001-02-14 22:39:20 +03:00
|
|
|
{
|
2009-09-15 22:15:16 +04:00
|
|
|
NS_IF_ADDREF(*aTree = mTree);
|
2001-02-14 22:39:20 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-03-29 05:46:01 +03:00
|
|
|
NS_IMETHODIMP nsTreeSelection::SetTree(nsITreeBoxObject * aTree)
|
2001-02-14 22:39:20 +03:00
|
|
|
{
|
2003-12-21 07:27:50 +03:00
|
|
|
if (mSelectTimer) {
|
|
|
|
mSelectTimer->Cancel();
|
2012-07-30 18:20:58 +04:00
|
|
|
mSelectTimer = nullptr;
|
2003-12-21 07:27:50 +03:00
|
|
|
}
|
2009-09-15 22:15:16 +04:00
|
|
|
|
|
|
|
// Make sure aTree really implements nsITreeBoxObject and nsIBoxObject!
|
|
|
|
nsCOMPtr<nsIBoxObject> bo = do_QueryInterface(aTree);
|
|
|
|
mTree = do_QueryInterface(bo);
|
|
|
|
NS_ENSURE_STATE(mTree == aTree);
|
2001-02-14 22:39:20 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP nsTreeSelection::GetSingle(bool* aSingle)
|
2003-03-12 12:00:28 +03:00
|
|
|
{
|
2008-12-22 18:32:19 +03:00
|
|
|
if (!mTree)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2003-03-12 12:00:28 +03:00
|
|
|
nsCOMPtr<nsIBoxObject> boxObject = do_QueryInterface(mTree);
|
2006-06-07 00:05:13 +04:00
|
|
|
|
2003-03-12 12:00:28 +03:00
|
|
|
nsCOMPtr<nsIDOMElement> element;
|
|
|
|
boxObject->GetElement(getter_AddRefs(element));
|
2006-06-07 00:05:13 +04:00
|
|
|
|
2003-03-12 12:00:28 +03:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(element);
|
2006-06-07 00:05:13 +04:00
|
|
|
|
|
|
|
static nsIContent::AttrValuesArray strings[] =
|
2012-07-30 18:20:58 +04:00
|
|
|
{&nsGkAtoms::single, &nsGkAtoms::cell, &nsGkAtoms::text, nullptr};
|
2006-06-07 00:05:13 +04:00
|
|
|
|
|
|
|
*aSingle = content->FindAttrValueIn(kNameSpaceID_None,
|
2006-12-26 20:47:52 +03:00
|
|
|
nsGkAtoms::seltype,
|
2006-06-07 00:05:13 +04:00
|
|
|
strings, eCaseMatters) >= 0;
|
|
|
|
|
2003-03-12 12:00:28 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsTreeSelection::IsSelected(int32_t aIndex, bool* aResult)
|
2001-02-14 22:39:20 +03:00
|
|
|
{
|
2001-02-14 23:26:18 +03:00
|
|
|
if (mFirstRange)
|
|
|
|
*aResult = mFirstRange->Contains(aIndex);
|
|
|
|
else
|
2011-10-17 18:59:28 +04:00
|
|
|
*aResult = false;
|
2001-02-14 23:26:18 +03:00
|
|
|
return NS_OK;
|
2001-02-14 22:39:20 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsTreeSelection::TimedSelect(int32_t aIndex, int32_t aMsec)
|
2001-02-16 05:21:34 +03:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool suppressSelect = mSuppressed;
|
2001-02-16 05:21:34 +03:00
|
|
|
|
|
|
|
if (aMsec != -1)
|
2011-10-17 18:59:28 +04:00
|
|
|
mSuppressed = true;
|
2001-02-16 05:21:34 +03:00
|
|
|
|
|
|
|
nsresult rv = Select(aIndex);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (aMsec != -1) {
|
|
|
|
mSuppressed = suppressSelect;
|
|
|
|
if (!mSuppressed) {
|
|
|
|
if (mSelectTimer)
|
|
|
|
mSelectTimer->Cancel();
|
|
|
|
|
|
|
|
mSelectTimer = do_CreateInstance("@mozilla.org/timer;1");
|
2002-09-07 09:38:16 +04:00
|
|
|
mSelectTimer->InitWithFuncCallback(SelectCallback, this, aMsec,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
2001-02-16 05:21:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsTreeSelection::Select(int32_t aIndex)
|
2001-02-14 22:39:20 +03:00
|
|
|
{
|
2001-02-16 05:21:34 +03:00
|
|
|
mShiftSelectPivot = -1;
|
|
|
|
|
2006-09-24 19:16:31 +04:00
|
|
|
nsresult rv = SetCurrentIndex(aIndex);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2001-03-06 05:27:50 +03:00
|
|
|
|
2001-02-15 00:38:17 +03:00
|
|
|
if (mFirstRange) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool alreadySelected = mFirstRange->Contains(aIndex);
|
2001-02-15 00:38:17 +03:00
|
|
|
|
|
|
|
if (alreadySelected) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t count = mFirstRange->Count();
|
2001-02-15 00:38:17 +03:00
|
|
|
if (count > 1) {
|
|
|
|
// We need to deselect everything but our item.
|
|
|
|
mFirstRange->RemoveAllBut(aIndex);
|
2001-02-15 12:43:11 +03:00
|
|
|
FireOnSelectHandler();
|
2001-02-15 00:38:17 +03:00
|
|
|
}
|
2001-02-15 12:43:11 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else {
|
2003-12-24 21:06:20 +03:00
|
|
|
// Clear out our selection.
|
|
|
|
mFirstRange->Invalidate();
|
|
|
|
delete mFirstRange;
|
2001-02-15 00:38:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-15 12:43:11 +03:00
|
|
|
// Create our new selection.
|
2002-03-29 05:46:01 +03:00
|
|
|
mFirstRange = new nsTreeRange(this, aIndex);
|
2003-12-24 21:06:20 +03:00
|
|
|
if (!mFirstRange)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2001-02-15 12:43:11 +03:00
|
|
|
mFirstRange->Invalidate();
|
2001-02-15 00:38:17 +03:00
|
|
|
|
2001-02-15 12:43:11 +03:00
|
|
|
// Fire the select event
|
|
|
|
FireOnSelectHandler();
|
2001-02-15 00:38:17 +03:00
|
|
|
return NS_OK;
|
2001-02-14 22:39:20 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsTreeSelection::ToggleSelect(int32_t aIndex)
|
2001-02-14 22:39:20 +03:00
|
|
|
{
|
2001-02-15 12:43:11 +03:00
|
|
|
// There are six cases that can occur on a ToggleSelect with our
|
|
|
|
// range code.
|
|
|
|
// (1) A new range should be made for a selection.
|
|
|
|
// (2) A single range is removed from the selection.
|
|
|
|
// (3) The item is added to an existing range.
|
|
|
|
// (4) The item is removed from an existing range.
|
|
|
|
// (5) The addition of the item causes two ranges to be merged.
|
|
|
|
// (6) The removal of the item causes two ranges to be split.
|
2001-02-16 05:21:34 +03:00
|
|
|
mShiftSelectPivot = -1;
|
2006-09-24 19:16:31 +04:00
|
|
|
nsresult rv = SetCurrentIndex(aIndex);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2001-02-16 05:21:34 +03:00
|
|
|
|
2001-02-15 12:43:11 +03:00
|
|
|
if (!mFirstRange)
|
|
|
|
Select(aIndex);
|
|
|
|
else {
|
2001-08-19 12:24:42 +04:00
|
|
|
if (!mFirstRange->Contains(aIndex)) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool single;
|
2008-12-22 18:32:19 +03:00
|
|
|
rv = GetSingle(&single);
|
|
|
|
if (NS_SUCCEEDED(rv) && !single)
|
2003-12-24 21:06:20 +03:00
|
|
|
rv = mFirstRange->Add(aIndex);
|
2001-08-19 12:24:42 +04:00
|
|
|
}
|
2002-01-24 04:22:33 +03:00
|
|
|
else
|
2003-12-24 21:06:20 +03:00
|
|
|
rv = mFirstRange->Remove(aIndex);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2006-06-30 22:22:36 +04:00
|
|
|
if (mTree)
|
|
|
|
mTree->InvalidateRow(aIndex);
|
2001-02-15 12:43:11 +03:00
|
|
|
|
2003-12-24 21:06:20 +03:00
|
|
|
FireOnSelectHandler();
|
|
|
|
}
|
2001-02-15 12:43:11 +03:00
|
|
|
}
|
|
|
|
|
2003-12-24 21:06:20 +03:00
|
|
|
return rv;
|
2001-02-14 22:39:20 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsTreeSelection::RangedSelect(int32_t aStartIndex, int32_t aEndIndex, bool aAugment)
|
2001-02-14 22:39:20 +03:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool single;
|
2008-12-22 18:32:19 +03:00
|
|
|
nsresult rv = GetSingle(&single);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2003-03-12 12:00:28 +03:00
|
|
|
if ((mFirstRange || (aStartIndex != aEndIndex)) && single)
|
2001-08-19 12:24:42 +04:00
|
|
|
return NS_OK;
|
|
|
|
|
2001-02-16 00:15:21 +03:00
|
|
|
if (!aAugment) {
|
|
|
|
// Clear our selection.
|
2001-03-06 05:27:50 +03:00
|
|
|
if (mFirstRange) {
|
|
|
|
mFirstRange->Invalidate();
|
|
|
|
delete mFirstRange;
|
2012-07-30 18:20:58 +04:00
|
|
|
mFirstRange = nullptr;
|
2001-03-06 05:27:50 +03:00
|
|
|
}
|
2001-02-16 00:15:21 +03:00
|
|
|
}
|
|
|
|
|
2001-02-16 05:21:34 +03:00
|
|
|
if (aStartIndex == -1) {
|
|
|
|
if (mShiftSelectPivot != -1)
|
|
|
|
aStartIndex = mShiftSelectPivot;
|
2007-03-07 16:25:13 +03:00
|
|
|
else if (mCurrentIndex != -1)
|
|
|
|
aStartIndex = mCurrentIndex;
|
|
|
|
else
|
|
|
|
aStartIndex = aEndIndex;
|
2001-02-16 05:21:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mShiftSelectPivot = aStartIndex;
|
2008-12-22 18:32:19 +03:00
|
|
|
rv = SetCurrentIndex(aEndIndex);
|
2006-09-24 19:16:31 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2001-02-16 05:21:34 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t start = aStartIndex < aEndIndex ? aStartIndex : aEndIndex;
|
|
|
|
int32_t end = aStartIndex < aEndIndex ? aEndIndex : aStartIndex;
|
2001-02-15 12:43:11 +03:00
|
|
|
|
2001-03-06 05:27:50 +03:00
|
|
|
if (aAugment && mFirstRange) {
|
2001-02-16 00:15:21 +03:00
|
|
|
// We need to remove all the items within our selected range from the selection,
|
|
|
|
// and then we insert our new range into the list.
|
2003-12-24 21:06:20 +03:00
|
|
|
nsresult rv = mFirstRange->RemoveRange(start, end);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2001-02-16 00:15:21 +03:00
|
|
|
}
|
|
|
|
|
2002-03-29 05:46:01 +03:00
|
|
|
nsTreeRange* range = new nsTreeRange(this, start, end);
|
2003-12-24 21:06:20 +03:00
|
|
|
if (!range)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2001-02-16 00:15:21 +03:00
|
|
|
range->Invalidate();
|
|
|
|
|
|
|
|
if (aAugment && mFirstRange)
|
|
|
|
mFirstRange->Insert(range);
|
|
|
|
else
|
|
|
|
mFirstRange = range;
|
2001-02-15 12:43:11 +03:00
|
|
|
|
|
|
|
FireOnSelectHandler();
|
|
|
|
|
|
|
|
return NS_OK;
|
2001-02-14 22:39:20 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsTreeSelection::ClearRange(int32_t aStartIndex, int32_t aEndIndex)
|
2001-03-06 05:27:50 +03:00
|
|
|
{
|
2006-09-24 19:16:31 +04:00
|
|
|
nsresult rv = SetCurrentIndex(aEndIndex);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2001-03-06 05:27:50 +03:00
|
|
|
|
|
|
|
if (mFirstRange) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t start = aStartIndex < aEndIndex ? aStartIndex : aEndIndex;
|
|
|
|
int32_t end = aStartIndex < aEndIndex ? aEndIndex : aStartIndex;
|
2001-03-06 05:27:50 +03:00
|
|
|
|
|
|
|
mFirstRange->RemoveRange(start, end);
|
|
|
|
|
2006-06-30 22:22:36 +04:00
|
|
|
if (mTree)
|
|
|
|
mTree->InvalidateRange(start, end);
|
2001-03-06 05:27:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-03-29 05:46:01 +03:00
|
|
|
NS_IMETHODIMP nsTreeSelection::ClearSelection()
|
2001-02-14 22:39:20 +03:00
|
|
|
{
|
2001-03-06 05:27:50 +03:00
|
|
|
if (mFirstRange) {
|
|
|
|
mFirstRange->Invalidate();
|
|
|
|
delete mFirstRange;
|
2012-07-30 18:20:58 +04:00
|
|
|
mFirstRange = nullptr;
|
2001-03-06 05:27:50 +03:00
|
|
|
}
|
2001-02-16 05:21:34 +03:00
|
|
|
mShiftSelectPivot = -1;
|
2001-02-15 12:43:11 +03:00
|
|
|
|
|
|
|
FireOnSelectHandler();
|
|
|
|
|
|
|
|
return NS_OK;
|
2001-02-14 22:39:20 +03:00
|
|
|
}
|
|
|
|
|
2002-03-29 05:46:01 +03:00
|
|
|
NS_IMETHODIMP nsTreeSelection::InvertSelection()
|
2001-02-14 22:39:20 +03:00
|
|
|
{
|
2001-02-15 12:43:11 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2001-02-14 22:39:20 +03:00
|
|
|
}
|
|
|
|
|
2002-03-29 05:46:01 +03:00
|
|
|
NS_IMETHODIMP nsTreeSelection::SelectAll()
|
2001-02-14 22:39:20 +03:00
|
|
|
{
|
2006-06-30 22:22:36 +04:00
|
|
|
if (!mTree)
|
|
|
|
return NS_OK;
|
|
|
|
|
2002-03-29 05:46:01 +03:00
|
|
|
nsCOMPtr<nsITreeView> view;
|
|
|
|
mTree->GetView(getter_AddRefs(view));
|
2001-02-15 12:43:11 +03:00
|
|
|
if (!view)
|
|
|
|
return NS_OK;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t rowCount;
|
2001-02-15 12:43:11 +03:00
|
|
|
view->GetRowCount(&rowCount);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool single;
|
2008-12-22 18:32:19 +03:00
|
|
|
nsresult rv = GetSingle(&single);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2003-03-12 12:00:28 +03:00
|
|
|
if (rowCount == 0 || (rowCount > 1 && single))
|
2001-02-15 12:43:11 +03:00
|
|
|
return NS_OK;
|
|
|
|
|
2001-08-19 12:24:42 +04:00
|
|
|
mShiftSelectPivot = -1;
|
|
|
|
|
|
|
|
// Invalidate not necessary when clearing selection, since
|
|
|
|
// we're going to invalidate the world on the SelectAll.
|
|
|
|
delete mFirstRange;
|
|
|
|
|
2002-03-29 05:46:01 +03:00
|
|
|
mFirstRange = new nsTreeRange(this, 0, rowCount-1);
|
2001-02-15 12:43:11 +03:00
|
|
|
mFirstRange->Invalidate();
|
|
|
|
|
|
|
|
FireOnSelectHandler();
|
|
|
|
|
|
|
|
return NS_OK;
|
2001-02-14 22:39:20 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsTreeSelection::GetRangeCount(int32_t* aResult)
|
2001-02-14 22:39:20 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t count = 0;
|
2002-03-29 05:46:01 +03:00
|
|
|
nsTreeRange* curr = mFirstRange;
|
2001-02-16 00:15:21 +03:00
|
|
|
while (curr) {
|
|
|
|
count++;
|
|
|
|
curr = curr->mNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = count;
|
|
|
|
return NS_OK;
|
2001-02-14 22:39:20 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsTreeSelection::GetRangeAt(int32_t aIndex, int32_t* aMin, int32_t* aMax)
|
2001-02-14 22:39:20 +03:00
|
|
|
{
|
2001-02-16 00:15:21 +03:00
|
|
|
*aMin = *aMax = -1;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t i = -1;
|
2002-03-29 05:46:01 +03:00
|
|
|
nsTreeRange* curr = mFirstRange;
|
2001-02-16 00:15:21 +03:00
|
|
|
while (curr) {
|
|
|
|
i++;
|
|
|
|
if (i == aIndex) {
|
|
|
|
*aMin = curr->mMin;
|
|
|
|
*aMax = curr->mMax;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
curr = curr->mNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2001-02-14 22:39:20 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsTreeSelection::GetCount(int32_t *count)
|
2001-04-12 03:15:49 +04:00
|
|
|
{
|
|
|
|
if (mFirstRange)
|
|
|
|
*count = mFirstRange->Count();
|
|
|
|
else // No range available, so there's no selected row.
|
|
|
|
*count = 0;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP nsTreeSelection::GetSelectEventsSuppressed(bool *aSelectEventsSuppressed)
|
2001-02-14 22:44:38 +03:00
|
|
|
{
|
|
|
|
*aSelectEventsSuppressed = mSuppressed;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP nsTreeSelection::SetSelectEventsSuppressed(bool aSelectEventsSuppressed)
|
2001-02-14 22:44:38 +03:00
|
|
|
{
|
|
|
|
mSuppressed = aSelectEventsSuppressed;
|
2001-02-16 05:21:34 +03:00
|
|
|
if (!mSuppressed)
|
|
|
|
FireOnSelectHandler();
|
2001-02-14 22:44:38 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsTreeSelection::GetCurrentIndex(int32_t *aCurrentIndex)
|
2001-02-14 22:39:20 +03:00
|
|
|
{
|
2001-02-14 22:44:38 +03:00
|
|
|
*aCurrentIndex = mCurrentIndex;
|
|
|
|
return NS_OK;
|
2001-02-14 22:39:20 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsTreeSelection::SetCurrentIndex(int32_t aIndex)
|
2001-02-14 22:39:20 +03:00
|
|
|
{
|
2006-09-24 19:16:31 +04:00
|
|
|
if (!mTree) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2005-07-28 21:18:28 +04:00
|
|
|
if (mCurrentIndex == aIndex) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-06-30 22:22:36 +04:00
|
|
|
if (mCurrentIndex != -1 && mTree)
|
2002-03-29 05:46:01 +03:00
|
|
|
mTree->InvalidateRow(mCurrentIndex);
|
2001-02-16 05:21:34 +03:00
|
|
|
|
|
|
|
mCurrentIndex = aIndex;
|
2006-06-30 22:22:36 +04:00
|
|
|
if (!mTree)
|
|
|
|
return NS_OK;
|
2001-02-16 05:21:34 +03:00
|
|
|
|
|
|
|
if (aIndex != -1)
|
2002-03-29 05:46:01 +03:00
|
|
|
mTree->InvalidateRow(aIndex);
|
2001-02-16 05:21:34 +03:00
|
|
|
|
2011-09-28 05:46:11 +04:00
|
|
|
// Fire DOMMenuItemActive or DOMMenuItemInactive event for tree.
|
2005-07-28 21:18:28 +04:00
|
|
|
nsCOMPtr<nsIBoxObject> boxObject = do_QueryInterface(mTree);
|
|
|
|
NS_ASSERTION(boxObject, "no box object!");
|
|
|
|
if (!boxObject)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
nsCOMPtr<nsIDOMElement> treeElt;
|
|
|
|
boxObject->GetElement(getter_AddRefs(treeElt));
|
|
|
|
|
2009-03-06 21:12:20 +03:00
|
|
|
nsCOMPtr<nsINode> treeDOMNode(do_QueryInterface(treeElt));
|
|
|
|
NS_ENSURE_STATE(treeDOMNode);
|
2005-07-28 21:18:28 +04:00
|
|
|
|
2011-09-28 05:46:11 +04:00
|
|
|
NS_NAMED_LITERAL_STRING(DOMMenuItemActive, "DOMMenuItemActive");
|
|
|
|
NS_NAMED_LITERAL_STRING(DOMMenuItemInactive, "DOMMenuItemInactive");
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
2014-03-17 10:56:54 +04:00
|
|
|
new AsyncEventDispatcher(treeDOMNode,
|
|
|
|
(aIndex != -1 ? DOMMenuItemActive :
|
|
|
|
DOMMenuItemInactive),
|
|
|
|
true, false);
|
|
|
|
return asyncDispatcher->PostDOMEvent();
|
2001-02-14 22:39:20 +03:00
|
|
|
}
|
|
|
|
|
2006-06-07 00:05:13 +04:00
|
|
|
NS_IMETHODIMP nsTreeSelection::GetCurrentColumn(nsITreeColumn** aCurrentColumn)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aCurrentColumn = mCurrentColumn);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsTreeSelection::SetCurrentColumn(nsITreeColumn* aCurrentColumn)
|
|
|
|
{
|
2008-12-22 18:32:19 +03:00
|
|
|
if (!mTree) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2006-06-07 00:05:13 +04:00
|
|
|
if (mCurrentColumn == aCurrentColumn) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mCurrentColumn) {
|
|
|
|
if (mFirstRange)
|
|
|
|
mTree->InvalidateCell(mFirstRange->mMin, mCurrentColumn);
|
|
|
|
if (mCurrentIndex != -1)
|
|
|
|
mTree->InvalidateCell(mCurrentIndex, mCurrentColumn);
|
|
|
|
}
|
|
|
|
|
|
|
|
mCurrentColumn = aCurrentColumn;
|
|
|
|
|
|
|
|
if (mCurrentColumn) {
|
|
|
|
if (mFirstRange)
|
|
|
|
mTree->InvalidateCell(mFirstRange->mMin, mCurrentColumn);
|
|
|
|
if (mCurrentIndex != -1)
|
|
|
|
mTree->InvalidateCell(mCurrentIndex, mCurrentColumn);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-03-31 08:55:34 +04:00
|
|
|
#define ADD_NEW_RANGE(macro_range, macro_selection, macro_start, macro_end) \
|
|
|
|
{ \
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t start = macro_start; \
|
|
|
|
int32_t end = macro_end; \
|
2010-06-16 20:38:37 +04:00
|
|
|
if (start > end) { \
|
|
|
|
end = start; \
|
|
|
|
} \
|
|
|
|
nsTreeRange* macro_new_range = new nsTreeRange(macro_selection, start, end); \
|
2001-03-31 08:55:34 +04:00
|
|
|
if (macro_range) \
|
|
|
|
macro_range->Insert(macro_new_range); \
|
|
|
|
else \
|
|
|
|
macro_range = macro_new_range; \
|
|
|
|
}
|
|
|
|
|
2001-03-06 05:27:50 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTreeSelection::AdjustSelection(int32_t aIndex, int32_t aCount)
|
2001-03-06 05:27:50 +03:00
|
|
|
{
|
2001-03-31 08:55:34 +04:00
|
|
|
NS_ASSERTION(aCount != 0, "adjusting by zero");
|
|
|
|
if (!aCount) return NS_OK;
|
|
|
|
|
|
|
|
// adjust mShiftSelectPivot, if necessary
|
|
|
|
if ((mShiftSelectPivot != 1) && (aIndex <= mShiftSelectPivot)) {
|
|
|
|
// if we are deleting and the delete includes the shift select pivot, reset it
|
|
|
|
if (aCount < 0 && (mShiftSelectPivot <= (aIndex -aCount -1))) {
|
|
|
|
mShiftSelectPivot = -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mShiftSelectPivot += aCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust mCurrentIndex, if necessary
|
|
|
|
if ((mCurrentIndex != -1) && (aIndex <= mCurrentIndex)) {
|
|
|
|
// if we are deleting and the delete includes the current index, reset it
|
|
|
|
if (aCount < 0 && (mCurrentIndex <= (aIndex -aCount -1))) {
|
|
|
|
mCurrentIndex = -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mCurrentIndex += aCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no selection, so nothing to do.
|
|
|
|
if (!mFirstRange) return NS_OK;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool selChanged = false;
|
2010-08-12 16:05:43 +04:00
|
|
|
nsTreeRange* oldFirstRange = mFirstRange;
|
2002-03-29 05:46:01 +03:00
|
|
|
nsTreeRange* curr = mFirstRange;
|
2012-07-30 18:20:58 +04:00
|
|
|
mFirstRange = nullptr;
|
2001-03-31 08:55:34 +04:00
|
|
|
while (curr) {
|
|
|
|
if (aCount > 0) {
|
|
|
|
// inserting
|
|
|
|
if (aIndex > curr->mMax) {
|
|
|
|
// adjustment happens after the range, so no change
|
2010-08-12 16:05:43 +04:00
|
|
|
ADD_NEW_RANGE(mFirstRange, this, curr->mMin, curr->mMax);
|
2001-03-31 08:55:34 +04:00
|
|
|
}
|
|
|
|
else if (aIndex <= curr->mMin) {
|
|
|
|
// adjustment happens before the start of the range, so shift down
|
2010-08-12 16:05:43 +04:00
|
|
|
ADD_NEW_RANGE(mFirstRange, this, curr->mMin + aCount, curr->mMax + aCount);
|
2011-10-17 18:59:28 +04:00
|
|
|
selChanged = true;
|
2001-03-31 08:55:34 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// adjustment happen inside the range.
|
|
|
|
// break apart the range and create two ranges
|
2010-08-12 16:05:43 +04:00
|
|
|
ADD_NEW_RANGE(mFirstRange, this, curr->mMin, aIndex - 1);
|
|
|
|
ADD_NEW_RANGE(mFirstRange, this, aIndex + aCount, curr->mMax + aCount);
|
2011-10-17 18:59:28 +04:00
|
|
|
selChanged = true;
|
2001-03-31 08:55:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// deleting
|
|
|
|
if (aIndex > curr->mMax) {
|
|
|
|
// adjustment happens after the range, so no change
|
2010-08-12 16:05:43 +04:00
|
|
|
ADD_NEW_RANGE(mFirstRange, this, curr->mMin, curr->mMax);
|
2001-03-31 08:55:34 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// remember, aCount is negative
|
2011-10-17 18:59:28 +04:00
|
|
|
selChanged = true;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t lastIndexOfAdjustment = aIndex - aCount - 1;
|
2001-03-31 08:55:34 +04:00
|
|
|
if (aIndex <= curr->mMin) {
|
|
|
|
if (lastIndexOfAdjustment < curr->mMin) {
|
|
|
|
// adjustment happens before the start of the range, so shift up
|
2010-08-12 16:05:43 +04:00
|
|
|
ADD_NEW_RANGE(mFirstRange, this, curr->mMin + aCount, curr->mMax + aCount);
|
2001-03-31 08:55:34 +04:00
|
|
|
}
|
|
|
|
else if (lastIndexOfAdjustment >= curr->mMax) {
|
|
|
|
// adjustment contains the range. remove the range by not adding it to the newRange
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// adjustment starts before the range, and ends in the middle of it, so trim the range
|
2010-08-12 16:05:43 +04:00
|
|
|
ADD_NEW_RANGE(mFirstRange, this, aIndex, curr->mMax + aCount)
|
2001-03-31 08:55:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (lastIndexOfAdjustment >= curr->mMax) {
|
|
|
|
// adjustment starts in the middle of the current range, and contains the end of the range, so trim the range
|
2010-08-12 16:05:43 +04:00
|
|
|
ADD_NEW_RANGE(mFirstRange, this, curr->mMin, aIndex - 1)
|
2001-03-31 08:55:34 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// range contains the adjustment, so shorten the range
|
2010-08-12 16:05:43 +04:00
|
|
|
ADD_NEW_RANGE(mFirstRange, this, curr->mMin, curr->mMax + aCount)
|
2001-03-31 08:55:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curr = curr->mNext;
|
|
|
|
}
|
|
|
|
|
2010-08-12 16:05:43 +04:00
|
|
|
delete oldFirstRange;
|
2001-03-31 08:55:34 +04:00
|
|
|
|
|
|
|
// Fire the select event
|
2001-04-12 08:40:16 +04:00
|
|
|
if (selChanged)
|
|
|
|
FireOnSelectHandler();
|
2001-03-31 08:55:34 +04:00
|
|
|
|
2001-03-08 03:46:58 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-29 05:46:01 +03:00
|
|
|
nsTreeSelection::InvalidateSelection()
|
2001-03-08 03:46:58 +03:00
|
|
|
{
|
|
|
|
if (mFirstRange)
|
|
|
|
mFirstRange->Invalidate();
|
2001-03-06 05:27:50 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-01-24 04:22:33 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTreeSelection::GetShiftSelectPivot(int32_t* aIndex)
|
2002-01-24 04:22:33 +03:00
|
|
|
{
|
|
|
|
*aIndex = mShiftSelectPivot;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-04-17 09:53:38 +04:00
|
|
|
|
2001-02-15 12:43:11 +03:00
|
|
|
nsresult
|
2002-03-29 05:46:01 +03:00
|
|
|
nsTreeSelection::FireOnSelectHandler()
|
2001-02-15 12:43:11 +03:00
|
|
|
{
|
2006-06-30 22:22:36 +04:00
|
|
|
if (mSuppressed || !mTree)
|
2001-02-15 12:43:11 +03:00
|
|
|
return NS_OK;
|
|
|
|
|
2002-03-29 05:46:01 +03:00
|
|
|
nsCOMPtr<nsIBoxObject> boxObject = do_QueryInterface(mTree);
|
2003-04-27 01:57:39 +04:00
|
|
|
NS_ASSERTION(boxObject, "no box object!");
|
|
|
|
if (!boxObject)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2001-02-15 12:43:11 +03:00
|
|
|
nsCOMPtr<nsIDOMElement> elt;
|
2002-01-16 06:01:28 +03:00
|
|
|
boxObject->GetElement(getter_AddRefs(elt));
|
2008-12-03 14:11:34 +03:00
|
|
|
NS_ENSURE_STATE(elt);
|
2001-02-15 12:43:11 +03:00
|
|
|
|
2009-03-06 21:12:20 +03:00
|
|
|
nsCOMPtr<nsINode> node(do_QueryInterface(elt));
|
|
|
|
NS_ENSURE_STATE(node);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
2014-03-17 10:56:54 +04:00
|
|
|
new AsyncEventDispatcher(node, NS_LITERAL_STRING("select"), true, false);
|
|
|
|
asyncDispatcher->RunDOMEventWhenSafe();
|
2001-02-15 12:43:11 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-04-17 09:53:38 +04:00
|
|
|
void
|
|
|
|
nsTreeSelection::SelectCallback(nsITimer *aTimer, void *aClosure)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsTreeSelection> self = static_cast<nsTreeSelection*>(aClosure);
|
2004-04-17 09:53:38 +04:00
|
|
|
if (self) {
|
|
|
|
self->FireOnSelectHandler();
|
|
|
|
aTimer->Cancel();
|
2012-07-30 18:20:58 +04:00
|
|
|
self->mSelectTimer = nullptr;
|
2004-04-17 09:53:38 +04:00
|
|
|
}
|
|
|
|
}
|
2001-02-15 12:43:11 +03:00
|
|
|
|
2001-02-14 22:39:20 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsresult
|
2002-03-29 05:46:01 +03:00
|
|
|
NS_NewTreeSelection(nsITreeBoxObject* aTree, nsITreeSelection** aResult)
|
2001-02-14 22:39:20 +03:00
|
|
|
{
|
2002-03-29 05:46:01 +03:00
|
|
|
*aResult = new nsTreeSelection(aTree);
|
2001-02-14 22:39:20 +03:00
|
|
|
if (!*aResult)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|