nsTreeSelection.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/AsyncEventDispatcher.h"
  6. #include "nsCOMPtr.h"
  7. #include "nsTreeSelection.h"
  8. #include "nsIBoxObject.h"
  9. #include "nsITreeBoxObject.h"
  10. #include "nsITreeView.h"
  11. #include "nsString.h"
  12. #include "nsIDOMElement.h"
  13. #include "nsDOMClassInfoID.h"
  14. #include "nsIContent.h"
  15. #include "nsNameSpaceManager.h"
  16. #include "nsGkAtoms.h"
  17. #include "nsComponentManagerUtils.h"
  18. using namespace mozilla;
  19. // A helper class for managing our ranges of selection.
  20. struct nsTreeRange
  21. {
  22. nsTreeSelection* mSelection;
  23. nsTreeRange* mPrev;
  24. nsTreeRange* mNext;
  25. int32_t mMin;
  26. int32_t mMax;
  27. nsTreeRange(nsTreeSelection* aSel, int32_t aSingleVal)
  28. :mSelection(aSel), mPrev(nullptr), mNext(nullptr), mMin(aSingleVal), mMax(aSingleVal) {}
  29. nsTreeRange(nsTreeSelection* aSel, int32_t aMin, int32_t aMax)
  30. :mSelection(aSel), mPrev(nullptr), mNext(nullptr), mMin(aMin), mMax(aMax) {}
  31. ~nsTreeRange() { delete mNext; }
  32. void Connect(nsTreeRange* aPrev = nullptr, nsTreeRange* aNext = nullptr) {
  33. if (aPrev)
  34. aPrev->mNext = this;
  35. else
  36. mSelection->mFirstRange = this;
  37. if (aNext)
  38. aNext->mPrev = this;
  39. mPrev = aPrev;
  40. mNext = aNext;
  41. }
  42. nsresult RemoveRange(int32_t aStart, int32_t aEnd) {
  43. // This should so be a loop... sigh...
  44. // We start past the range to remove, so no more to remove
  45. if (aEnd < mMin)
  46. return NS_OK;
  47. // We are the last range to be affected
  48. if (aEnd < mMax) {
  49. if (aStart <= mMin) {
  50. // Just chop the start of the range off
  51. mMin = aEnd + 1;
  52. } else {
  53. // We need to split the range
  54. nsTreeRange* range = new nsTreeRange(mSelection, aEnd + 1, mMax);
  55. if (!range)
  56. return NS_ERROR_OUT_OF_MEMORY;
  57. mMax = aStart - 1;
  58. range->Connect(this, mNext);
  59. }
  60. return NS_OK;
  61. }
  62. nsTreeRange* next = mNext;
  63. if (aStart <= mMin) {
  64. // The remove includes us, remove ourselves from the list
  65. if (mPrev)
  66. mPrev->mNext = next;
  67. else
  68. mSelection->mFirstRange = next;
  69. if (next)
  70. next->mPrev = mPrev;
  71. mPrev = mNext = nullptr;
  72. delete this;
  73. } else if (aStart <= mMax) {
  74. // Just chop the end of the range off
  75. mMax = aStart - 1;
  76. }
  77. return next ? next->RemoveRange(aStart, aEnd) : NS_OK;
  78. }
  79. nsresult Remove(int32_t aIndex) {
  80. if (aIndex >= mMin && aIndex <= mMax) {
  81. // We have found the range that contains us.
  82. if (mMin == mMax) {
  83. // Delete the whole range.
  84. if (mPrev)
  85. mPrev->mNext = mNext;
  86. if (mNext)
  87. mNext->mPrev = mPrev;
  88. nsTreeRange* first = mSelection->mFirstRange;
  89. if (first == this)
  90. mSelection->mFirstRange = mNext;
  91. mNext = mPrev = nullptr;
  92. delete this;
  93. }
  94. else if (aIndex == mMin)
  95. mMin++;
  96. else if (aIndex == mMax)
  97. mMax--;
  98. else {
  99. // We have to break this range.
  100. nsTreeRange* newRange = new nsTreeRange(mSelection, aIndex + 1, mMax);
  101. if (!newRange)
  102. return NS_ERROR_OUT_OF_MEMORY;
  103. newRange->Connect(this, mNext);
  104. mMax = aIndex - 1;
  105. }
  106. }
  107. else if (mNext)
  108. return mNext->Remove(aIndex);
  109. return NS_OK;
  110. }
  111. nsresult Add(int32_t aIndex) {
  112. if (aIndex < mMin) {
  113. // We have found a spot to insert.
  114. if (aIndex + 1 == mMin)
  115. mMin = aIndex;
  116. else if (mPrev && mPrev->mMax+1 == aIndex)
  117. mPrev->mMax = aIndex;
  118. else {
  119. // We have to create a new range.
  120. nsTreeRange* newRange = new nsTreeRange(mSelection, aIndex);
  121. if (!newRange)
  122. return NS_ERROR_OUT_OF_MEMORY;
  123. newRange->Connect(mPrev, this);
  124. }
  125. }
  126. else if (mNext)
  127. mNext->Add(aIndex);
  128. else {
  129. // Insert on to the end.
  130. if (mMax+1 == aIndex)
  131. mMax = aIndex;
  132. else {
  133. // We have to create a new range.
  134. nsTreeRange* newRange = new nsTreeRange(mSelection, aIndex);
  135. if (!newRange)
  136. return NS_ERROR_OUT_OF_MEMORY;
  137. newRange->Connect(this, nullptr);
  138. }
  139. }
  140. return NS_OK;
  141. }
  142. bool Contains(int32_t aIndex) {
  143. if (aIndex >= mMin && aIndex <= mMax)
  144. return true;
  145. if (mNext)
  146. return mNext->Contains(aIndex);
  147. return false;
  148. }
  149. int32_t Count() {
  150. int32_t total = mMax - mMin + 1;
  151. if (mNext)
  152. total += mNext->Count();
  153. return total;
  154. }
  155. static void CollectRanges(nsTreeRange* aRange, nsTArray<int32_t>& aRanges)
  156. {
  157. nsTreeRange* cur = aRange;
  158. while (cur) {
  159. aRanges.AppendElement(cur->mMin);
  160. aRanges.AppendElement(cur->mMax);
  161. cur = cur->mNext;
  162. }
  163. }
  164. static void InvalidateRanges(nsITreeBoxObject* aTree,
  165. nsTArray<int32_t>& aRanges)
  166. {
  167. if (aTree) {
  168. nsCOMPtr<nsITreeBoxObject> tree = aTree;
  169. for (uint32_t i = 0; i < aRanges.Length(); i += 2) {
  170. aTree->InvalidateRange(aRanges[i], aRanges[i + 1]);
  171. }
  172. }
  173. }
  174. void Invalidate() {
  175. nsTArray<int32_t> ranges;
  176. CollectRanges(this, ranges);
  177. InvalidateRanges(mSelection->mTree, ranges);
  178. }
  179. void RemoveAllBut(int32_t aIndex) {
  180. if (aIndex >= mMin && aIndex <= mMax) {
  181. // Invalidate everything in this list.
  182. nsTArray<int32_t> ranges;
  183. CollectRanges(mSelection->mFirstRange, ranges);
  184. mMin = aIndex;
  185. mMax = aIndex;
  186. nsTreeRange* first = mSelection->mFirstRange;
  187. if (mPrev)
  188. mPrev->mNext = mNext;
  189. if (mNext)
  190. mNext->mPrev = mPrev;
  191. mNext = mPrev = nullptr;
  192. if (first != this) {
  193. delete mSelection->mFirstRange;
  194. mSelection->mFirstRange = this;
  195. }
  196. InvalidateRanges(mSelection->mTree, ranges);
  197. }
  198. else if (mNext)
  199. mNext->RemoveAllBut(aIndex);
  200. }
  201. void Insert(nsTreeRange* aRange) {
  202. if (mMin >= aRange->mMax)
  203. aRange->Connect(mPrev, this);
  204. else if (mNext)
  205. mNext->Insert(aRange);
  206. else
  207. aRange->Connect(this, nullptr);
  208. }
  209. };
  210. nsTreeSelection::nsTreeSelection(nsITreeBoxObject* aTree)
  211. : mTree(aTree),
  212. mSuppressed(false),
  213. mCurrentIndex(-1),
  214. mShiftSelectPivot(-1),
  215. mFirstRange(nullptr)
  216. {
  217. }
  218. nsTreeSelection::~nsTreeSelection()
  219. {
  220. delete mFirstRange;
  221. if (mSelectTimer)
  222. mSelectTimer->Cancel();
  223. }
  224. NS_IMPL_CYCLE_COLLECTION(nsTreeSelection, mTree, mCurrentColumn)
  225. NS_IMPL_CYCLE_COLLECTING_ADDREF(nsTreeSelection)
  226. NS_IMPL_CYCLE_COLLECTING_RELEASE(nsTreeSelection)
  227. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsTreeSelection)
  228. NS_INTERFACE_MAP_ENTRY(nsITreeSelection)
  229. NS_INTERFACE_MAP_ENTRY(nsINativeTreeSelection)
  230. NS_INTERFACE_MAP_ENTRY(nsISupports)
  231. NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(TreeSelection)
  232. NS_INTERFACE_MAP_END
  233. NS_IMETHODIMP nsTreeSelection::GetTree(nsITreeBoxObject * *aTree)
  234. {
  235. NS_IF_ADDREF(*aTree = mTree);
  236. return NS_OK;
  237. }
  238. NS_IMETHODIMP nsTreeSelection::SetTree(nsITreeBoxObject * aTree)
  239. {
  240. if (mSelectTimer) {
  241. mSelectTimer->Cancel();
  242. mSelectTimer = nullptr;
  243. }
  244. // Make sure aTree really implements nsITreeBoxObject and nsIBoxObject!
  245. nsCOMPtr<nsIBoxObject> bo = do_QueryInterface(aTree);
  246. mTree = do_QueryInterface(bo);
  247. NS_ENSURE_STATE(mTree == aTree);
  248. return NS_OK;
  249. }
  250. NS_IMETHODIMP nsTreeSelection::GetSingle(bool* aSingle)
  251. {
  252. if (!mTree)
  253. return NS_ERROR_NULL_POINTER;
  254. nsCOMPtr<nsIBoxObject> boxObject = do_QueryInterface(mTree);
  255. nsCOMPtr<nsIDOMElement> element;
  256. boxObject->GetElement(getter_AddRefs(element));
  257. nsCOMPtr<nsIContent> content = do_QueryInterface(element);
  258. static nsIContent::AttrValuesArray strings[] =
  259. {&nsGkAtoms::single, &nsGkAtoms::cell, &nsGkAtoms::text, nullptr};
  260. *aSingle = content->FindAttrValueIn(kNameSpaceID_None,
  261. nsGkAtoms::seltype,
  262. strings, eCaseMatters) >= 0;
  263. return NS_OK;
  264. }
  265. NS_IMETHODIMP nsTreeSelection::IsSelected(int32_t aIndex, bool* aResult)
  266. {
  267. if (mFirstRange)
  268. *aResult = mFirstRange->Contains(aIndex);
  269. else
  270. *aResult = false;
  271. return NS_OK;
  272. }
  273. NS_IMETHODIMP nsTreeSelection::TimedSelect(int32_t aIndex, int32_t aMsec)
  274. {
  275. bool suppressSelect = mSuppressed;
  276. if (aMsec != -1)
  277. mSuppressed = true;
  278. nsresult rv = Select(aIndex);
  279. if (NS_FAILED(rv))
  280. return rv;
  281. if (aMsec != -1) {
  282. mSuppressed = suppressSelect;
  283. if (!mSuppressed) {
  284. if (mSelectTimer)
  285. mSelectTimer->Cancel();
  286. mSelectTimer = do_CreateInstance("@mozilla.org/timer;1");
  287. mSelectTimer->InitWithFuncCallback(SelectCallback, this, aMsec,
  288. nsITimer::TYPE_ONE_SHOT);
  289. }
  290. }
  291. return NS_OK;
  292. }
  293. NS_IMETHODIMP nsTreeSelection::Select(int32_t aIndex)
  294. {
  295. mShiftSelectPivot = -1;
  296. nsresult rv = SetCurrentIndex(aIndex);
  297. if (NS_FAILED(rv))
  298. return rv;
  299. if (mFirstRange) {
  300. bool alreadySelected = mFirstRange->Contains(aIndex);
  301. if (alreadySelected) {
  302. int32_t count = mFirstRange->Count();
  303. if (count > 1) {
  304. // We need to deselect everything but our item.
  305. mFirstRange->RemoveAllBut(aIndex);
  306. FireOnSelectHandler();
  307. }
  308. return NS_OK;
  309. }
  310. else {
  311. // Clear out our selection.
  312. mFirstRange->Invalidate();
  313. delete mFirstRange;
  314. }
  315. }
  316. // Create our new selection.
  317. mFirstRange = new nsTreeRange(this, aIndex);
  318. if (!mFirstRange)
  319. return NS_ERROR_OUT_OF_MEMORY;
  320. mFirstRange->Invalidate();
  321. // Fire the select event
  322. FireOnSelectHandler();
  323. return NS_OK;
  324. }
  325. NS_IMETHODIMP nsTreeSelection::ToggleSelect(int32_t aIndex)
  326. {
  327. // There are six cases that can occur on a ToggleSelect with our
  328. // range code.
  329. // (1) A new range should be made for a selection.
  330. // (2) A single range is removed from the selection.
  331. // (3) The item is added to an existing range.
  332. // (4) The item is removed from an existing range.
  333. // (5) The addition of the item causes two ranges to be merged.
  334. // (6) The removal of the item causes two ranges to be split.
  335. mShiftSelectPivot = -1;
  336. nsresult rv = SetCurrentIndex(aIndex);
  337. if (NS_FAILED(rv))
  338. return rv;
  339. if (!mFirstRange)
  340. Select(aIndex);
  341. else {
  342. if (!mFirstRange->Contains(aIndex)) {
  343. bool single;
  344. rv = GetSingle(&single);
  345. if (NS_SUCCEEDED(rv) && !single)
  346. rv = mFirstRange->Add(aIndex);
  347. }
  348. else
  349. rv = mFirstRange->Remove(aIndex);
  350. if (NS_SUCCEEDED(rv)) {
  351. if (mTree)
  352. mTree->InvalidateRow(aIndex);
  353. FireOnSelectHandler();
  354. }
  355. }
  356. return rv;
  357. }
  358. NS_IMETHODIMP nsTreeSelection::RangedSelect(int32_t aStartIndex, int32_t aEndIndex, bool aAugment)
  359. {
  360. bool single;
  361. nsresult rv = GetSingle(&single);
  362. if (NS_FAILED(rv))
  363. return rv;
  364. if ((mFirstRange || (aStartIndex != aEndIndex)) && single)
  365. return NS_OK;
  366. if (!aAugment) {
  367. // Clear our selection.
  368. if (mFirstRange) {
  369. mFirstRange->Invalidate();
  370. delete mFirstRange;
  371. mFirstRange = nullptr;
  372. }
  373. }
  374. if (aStartIndex == -1) {
  375. if (mShiftSelectPivot != -1)
  376. aStartIndex = mShiftSelectPivot;
  377. else if (mCurrentIndex != -1)
  378. aStartIndex = mCurrentIndex;
  379. else
  380. aStartIndex = aEndIndex;
  381. }
  382. mShiftSelectPivot = aStartIndex;
  383. rv = SetCurrentIndex(aEndIndex);
  384. if (NS_FAILED(rv))
  385. return rv;
  386. int32_t start = aStartIndex < aEndIndex ? aStartIndex : aEndIndex;
  387. int32_t end = aStartIndex < aEndIndex ? aEndIndex : aStartIndex;
  388. if (aAugment && mFirstRange) {
  389. // We need to remove all the items within our selected range from the selection,
  390. // and then we insert our new range into the list.
  391. nsresult rv = mFirstRange->RemoveRange(start, end);
  392. if (NS_FAILED(rv))
  393. return rv;
  394. }
  395. nsTreeRange* range = new nsTreeRange(this, start, end);
  396. if (!range)
  397. return NS_ERROR_OUT_OF_MEMORY;
  398. range->Invalidate();
  399. if (aAugment && mFirstRange)
  400. mFirstRange->Insert(range);
  401. else
  402. mFirstRange = range;
  403. FireOnSelectHandler();
  404. return NS_OK;
  405. }
  406. NS_IMETHODIMP nsTreeSelection::ClearRange(int32_t aStartIndex, int32_t aEndIndex)
  407. {
  408. nsresult rv = SetCurrentIndex(aEndIndex);
  409. if (NS_FAILED(rv))
  410. return rv;
  411. if (mFirstRange) {
  412. int32_t start = aStartIndex < aEndIndex ? aStartIndex : aEndIndex;
  413. int32_t end = aStartIndex < aEndIndex ? aEndIndex : aStartIndex;
  414. mFirstRange->RemoveRange(start, end);
  415. if (mTree)
  416. mTree->InvalidateRange(start, end);
  417. }
  418. return NS_OK;
  419. }
  420. NS_IMETHODIMP nsTreeSelection::ClearSelection()
  421. {
  422. if (mFirstRange) {
  423. mFirstRange->Invalidate();
  424. delete mFirstRange;
  425. mFirstRange = nullptr;
  426. }
  427. mShiftSelectPivot = -1;
  428. FireOnSelectHandler();
  429. return NS_OK;
  430. }
  431. NS_IMETHODIMP nsTreeSelection::InvertSelection()
  432. {
  433. return NS_ERROR_NOT_IMPLEMENTED;
  434. }
  435. NS_IMETHODIMP nsTreeSelection::SelectAll()
  436. {
  437. if (!mTree)
  438. return NS_OK;
  439. nsCOMPtr<nsITreeView> view;
  440. mTree->GetView(getter_AddRefs(view));
  441. if (!view)
  442. return NS_OK;
  443. int32_t rowCount;
  444. view->GetRowCount(&rowCount);
  445. bool single;
  446. nsresult rv = GetSingle(&single);
  447. if (NS_FAILED(rv))
  448. return rv;
  449. if (rowCount == 0 || (rowCount > 1 && single))
  450. return NS_OK;
  451. mShiftSelectPivot = -1;
  452. // Invalidate not necessary when clearing selection, since
  453. // we're going to invalidate the world on the SelectAll.
  454. delete mFirstRange;
  455. mFirstRange = new nsTreeRange(this, 0, rowCount-1);
  456. mFirstRange->Invalidate();
  457. FireOnSelectHandler();
  458. return NS_OK;
  459. }
  460. NS_IMETHODIMP nsTreeSelection::GetRangeCount(int32_t* aResult)
  461. {
  462. int32_t count = 0;
  463. nsTreeRange* curr = mFirstRange;
  464. while (curr) {
  465. count++;
  466. curr = curr->mNext;
  467. }
  468. *aResult = count;
  469. return NS_OK;
  470. }
  471. NS_IMETHODIMP nsTreeSelection::GetRangeAt(int32_t aIndex, int32_t* aMin, int32_t* aMax)
  472. {
  473. *aMin = *aMax = -1;
  474. int32_t i = -1;
  475. nsTreeRange* curr = mFirstRange;
  476. while (curr) {
  477. i++;
  478. if (i == aIndex) {
  479. *aMin = curr->mMin;
  480. *aMax = curr->mMax;
  481. break;
  482. }
  483. curr = curr->mNext;
  484. }
  485. return NS_OK;
  486. }
  487. NS_IMETHODIMP nsTreeSelection::GetCount(int32_t *count)
  488. {
  489. if (mFirstRange)
  490. *count = mFirstRange->Count();
  491. else // No range available, so there's no selected row.
  492. *count = 0;
  493. return NS_OK;
  494. }
  495. NS_IMETHODIMP nsTreeSelection::GetSelectEventsSuppressed(bool *aSelectEventsSuppressed)
  496. {
  497. *aSelectEventsSuppressed = mSuppressed;
  498. return NS_OK;
  499. }
  500. NS_IMETHODIMP nsTreeSelection::SetSelectEventsSuppressed(bool aSelectEventsSuppressed)
  501. {
  502. mSuppressed = aSelectEventsSuppressed;
  503. if (!mSuppressed)
  504. FireOnSelectHandler();
  505. return NS_OK;
  506. }
  507. NS_IMETHODIMP nsTreeSelection::GetCurrentIndex(int32_t *aCurrentIndex)
  508. {
  509. *aCurrentIndex = mCurrentIndex;
  510. return NS_OK;
  511. }
  512. NS_IMETHODIMP nsTreeSelection::SetCurrentIndex(int32_t aIndex)
  513. {
  514. if (!mTree) {
  515. return NS_ERROR_UNEXPECTED;
  516. }
  517. if (mCurrentIndex == aIndex) {
  518. return NS_OK;
  519. }
  520. if (mCurrentIndex != -1 && mTree)
  521. mTree->InvalidateRow(mCurrentIndex);
  522. mCurrentIndex = aIndex;
  523. if (!mTree)
  524. return NS_OK;
  525. if (aIndex != -1)
  526. mTree->InvalidateRow(aIndex);
  527. // Fire DOMMenuItemActive or DOMMenuItemInactive event for tree.
  528. nsCOMPtr<nsIBoxObject> boxObject = do_QueryInterface(mTree);
  529. NS_ASSERTION(boxObject, "no box object!");
  530. if (!boxObject)
  531. return NS_ERROR_UNEXPECTED;
  532. nsCOMPtr<nsIDOMElement> treeElt;
  533. boxObject->GetElement(getter_AddRefs(treeElt));
  534. nsCOMPtr<nsINode> treeDOMNode(do_QueryInterface(treeElt));
  535. NS_ENSURE_STATE(treeDOMNode);
  536. NS_NAMED_LITERAL_STRING(DOMMenuItemActive, "DOMMenuItemActive");
  537. NS_NAMED_LITERAL_STRING(DOMMenuItemInactive, "DOMMenuItemInactive");
  538. RefPtr<AsyncEventDispatcher> asyncDispatcher =
  539. new AsyncEventDispatcher(treeDOMNode,
  540. (aIndex != -1 ? DOMMenuItemActive :
  541. DOMMenuItemInactive),
  542. true, false);
  543. return asyncDispatcher->PostDOMEvent();
  544. }
  545. NS_IMETHODIMP nsTreeSelection::GetCurrentColumn(nsITreeColumn** aCurrentColumn)
  546. {
  547. NS_IF_ADDREF(*aCurrentColumn = mCurrentColumn);
  548. return NS_OK;
  549. }
  550. NS_IMETHODIMP nsTreeSelection::SetCurrentColumn(nsITreeColumn* aCurrentColumn)
  551. {
  552. if (!mTree) {
  553. return NS_ERROR_UNEXPECTED;
  554. }
  555. if (mCurrentColumn == aCurrentColumn) {
  556. return NS_OK;
  557. }
  558. if (mCurrentColumn) {
  559. if (mFirstRange)
  560. mTree->InvalidateCell(mFirstRange->mMin, mCurrentColumn);
  561. if (mCurrentIndex != -1)
  562. mTree->InvalidateCell(mCurrentIndex, mCurrentColumn);
  563. }
  564. mCurrentColumn = aCurrentColumn;
  565. if (mCurrentColumn) {
  566. if (mFirstRange)
  567. mTree->InvalidateCell(mFirstRange->mMin, mCurrentColumn);
  568. if (mCurrentIndex != -1)
  569. mTree->InvalidateCell(mCurrentIndex, mCurrentColumn);
  570. }
  571. return NS_OK;
  572. }
  573. #define ADD_NEW_RANGE(macro_range, macro_selection, macro_start, macro_end) \
  574. { \
  575. int32_t start = macro_start; \
  576. int32_t end = macro_end; \
  577. if (start > end) { \
  578. end = start; \
  579. } \
  580. nsTreeRange* macro_new_range = new nsTreeRange(macro_selection, start, end); \
  581. if (macro_range) \
  582. macro_range->Insert(macro_new_range); \
  583. else \
  584. macro_range = macro_new_range; \
  585. }
  586. NS_IMETHODIMP
  587. nsTreeSelection::AdjustSelection(int32_t aIndex, int32_t aCount)
  588. {
  589. NS_ASSERTION(aCount != 0, "adjusting by zero");
  590. if (!aCount) return NS_OK;
  591. // adjust mShiftSelectPivot, if necessary
  592. if ((mShiftSelectPivot != 1) && (aIndex <= mShiftSelectPivot)) {
  593. // if we are deleting and the delete includes the shift select pivot, reset it
  594. if (aCount < 0 && (mShiftSelectPivot <= (aIndex -aCount -1))) {
  595. mShiftSelectPivot = -1;
  596. }
  597. else {
  598. mShiftSelectPivot += aCount;
  599. }
  600. }
  601. // adjust mCurrentIndex, if necessary
  602. if ((mCurrentIndex != -1) && (aIndex <= mCurrentIndex)) {
  603. // if we are deleting and the delete includes the current index, reset it
  604. if (aCount < 0 && (mCurrentIndex <= (aIndex -aCount -1))) {
  605. mCurrentIndex = -1;
  606. }
  607. else {
  608. mCurrentIndex += aCount;
  609. }
  610. }
  611. // no selection, so nothing to do.
  612. if (!mFirstRange) return NS_OK;
  613. bool selChanged = false;
  614. nsTreeRange* oldFirstRange = mFirstRange;
  615. nsTreeRange* curr = mFirstRange;
  616. mFirstRange = nullptr;
  617. while (curr) {
  618. if (aCount > 0) {
  619. // inserting
  620. if (aIndex > curr->mMax) {
  621. // adjustment happens after the range, so no change
  622. ADD_NEW_RANGE(mFirstRange, this, curr->mMin, curr->mMax);
  623. }
  624. else if (aIndex <= curr->mMin) {
  625. // adjustment happens before the start of the range, so shift down
  626. ADD_NEW_RANGE(mFirstRange, this, curr->mMin + aCount, curr->mMax + aCount);
  627. selChanged = true;
  628. }
  629. else {
  630. // adjustment happen inside the range.
  631. // break apart the range and create two ranges
  632. ADD_NEW_RANGE(mFirstRange, this, curr->mMin, aIndex - 1);
  633. ADD_NEW_RANGE(mFirstRange, this, aIndex + aCount, curr->mMax + aCount);
  634. selChanged = true;
  635. }
  636. }
  637. else {
  638. // deleting
  639. if (aIndex > curr->mMax) {
  640. // adjustment happens after the range, so no change
  641. ADD_NEW_RANGE(mFirstRange, this, curr->mMin, curr->mMax);
  642. }
  643. else {
  644. // remember, aCount is negative
  645. selChanged = true;
  646. int32_t lastIndexOfAdjustment = aIndex - aCount - 1;
  647. if (aIndex <= curr->mMin) {
  648. if (lastIndexOfAdjustment < curr->mMin) {
  649. // adjustment happens before the start of the range, so shift up
  650. ADD_NEW_RANGE(mFirstRange, this, curr->mMin + aCount, curr->mMax + aCount);
  651. }
  652. else if (lastIndexOfAdjustment >= curr->mMax) {
  653. // adjustment contains the range. remove the range by not adding it to the newRange
  654. }
  655. else {
  656. // adjustment starts before the range, and ends in the middle of it, so trim the range
  657. ADD_NEW_RANGE(mFirstRange, this, aIndex, curr->mMax + aCount)
  658. }
  659. }
  660. else if (lastIndexOfAdjustment >= curr->mMax) {
  661. // adjustment starts in the middle of the current range, and contains the end of the range, so trim the range
  662. ADD_NEW_RANGE(mFirstRange, this, curr->mMin, aIndex - 1)
  663. }
  664. else {
  665. // range contains the adjustment, so shorten the range
  666. ADD_NEW_RANGE(mFirstRange, this, curr->mMin, curr->mMax + aCount)
  667. }
  668. }
  669. }
  670. curr = curr->mNext;
  671. }
  672. delete oldFirstRange;
  673. // Fire the select event
  674. if (selChanged)
  675. FireOnSelectHandler();
  676. return NS_OK;
  677. }
  678. NS_IMETHODIMP
  679. nsTreeSelection::InvalidateSelection()
  680. {
  681. if (mFirstRange)
  682. mFirstRange->Invalidate();
  683. return NS_OK;
  684. }
  685. NS_IMETHODIMP
  686. nsTreeSelection::GetShiftSelectPivot(int32_t* aIndex)
  687. {
  688. *aIndex = mShiftSelectPivot;
  689. return NS_OK;
  690. }
  691. nsresult
  692. nsTreeSelection::FireOnSelectHandler()
  693. {
  694. if (mSuppressed || !mTree)
  695. return NS_OK;
  696. nsCOMPtr<nsIBoxObject> boxObject = do_QueryInterface(mTree);
  697. NS_ASSERTION(boxObject, "no box object!");
  698. if (!boxObject)
  699. return NS_ERROR_UNEXPECTED;
  700. nsCOMPtr<nsIDOMElement> elt;
  701. boxObject->GetElement(getter_AddRefs(elt));
  702. NS_ENSURE_STATE(elt);
  703. nsCOMPtr<nsINode> node(do_QueryInterface(elt));
  704. NS_ENSURE_STATE(node);
  705. RefPtr<AsyncEventDispatcher> asyncDispatcher =
  706. new AsyncEventDispatcher(node, NS_LITERAL_STRING("select"), true, false);
  707. asyncDispatcher->RunDOMEventWhenSafe();
  708. return NS_OK;
  709. }
  710. void
  711. nsTreeSelection::SelectCallback(nsITimer *aTimer, void *aClosure)
  712. {
  713. RefPtr<nsTreeSelection> self = static_cast<nsTreeSelection*>(aClosure);
  714. if (self) {
  715. self->FireOnSelectHandler();
  716. aTimer->Cancel();
  717. self->mSelectTimer = nullptr;
  718. }
  719. }
  720. ///////////////////////////////////////////////////////////////////////////////////
  721. nsresult
  722. NS_NewTreeSelection(nsITreeBoxObject* aTree, nsITreeSelection** aResult)
  723. {
  724. *aResult = new nsTreeSelection(aTree);
  725. if (!*aResult)
  726. return NS_ERROR_OUT_OF_MEMORY;
  727. NS_ADDREF(*aResult);
  728. return NS_OK;
  729. }