nsXULSortService.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. *
  6. * This Original Code has been modified by IBM Corporation.
  7. * Modifications made by IBM described herein are
  8. * Copyright (c) International Business Machines
  9. * Corporation, 2000
  10. *
  11. * Modifications to Mozilla code or documentation
  12. * identified per MPL Section 3.3
  13. *
  14. * Date Modified by Description of modification
  15. * 03/27/2000 IBM Corp. Added PR_CALLBACK for Optlink
  16. * use in OS2
  17. */
  18. /*
  19. This sort service is used to sort template built content or content by attribute.
  20. */
  21. #ifndef nsXULTemplateResultSetRDF_h
  22. #define nsXULTemplateResultSetRDF_h
  23. #include "nsCOMPtr.h"
  24. #include "nsCOMArray.h"
  25. #include "nsTArray.h"
  26. #include "nsIContent.h"
  27. #include "nsIXULTemplateResult.h"
  28. #include "nsIXULTemplateQueryProcessor.h"
  29. #include "nsIXULSortService.h"
  30. #include "nsCycleCollectionParticipant.h"
  31. enum nsSortState_direction {
  32. nsSortState_descending,
  33. nsSortState_ascending,
  34. nsSortState_natural
  35. };
  36. // the sort state holds info about the current sort
  37. struct nsSortState
  38. {
  39. bool initialized;
  40. MOZ_INIT_OUTSIDE_CTOR bool invertSort;
  41. MOZ_INIT_OUTSIDE_CTOR bool inbetweenSeparatorSort;
  42. MOZ_INIT_OUTSIDE_CTOR bool sortStaticsLast;
  43. MOZ_INIT_OUTSIDE_CTOR bool isContainerRDFSeq;
  44. uint32_t sortHints;
  45. MOZ_INIT_OUTSIDE_CTOR nsSortState_direction direction;
  46. nsAutoString sort;
  47. nsCOMArray<nsIAtom> sortKeys;
  48. nsCOMPtr<nsIXULTemplateQueryProcessor> processor;
  49. nsCOMPtr<nsIContent> lastContainer;
  50. MOZ_INIT_OUTSIDE_CTOR bool lastWasFirst, lastWasLast;
  51. nsSortState()
  52. : initialized(false),
  53. isContainerRDFSeq(false),
  54. sortHints(0)
  55. {
  56. }
  57. void Traverse(nsCycleCollectionTraversalCallback &cb) const
  58. {
  59. cb.NoteXPCOMChild(processor);
  60. cb.NoteXPCOMChild(lastContainer);
  61. }
  62. };
  63. // information about a particular item to be sorted
  64. struct contentSortInfo {
  65. nsCOMPtr<nsIContent> content;
  66. nsCOMPtr<nsIContent> parent;
  67. nsCOMPtr<nsIXULTemplateResult> result;
  68. void swap(contentSortInfo& other)
  69. {
  70. content.swap(other.content);
  71. parent.swap(other.parent);
  72. result.swap(other.result);
  73. }
  74. };
  75. ////////////////////////////////////////////////////////////////////////
  76. // ServiceImpl
  77. //
  78. // This is the sort service.
  79. //
  80. class XULSortServiceImpl : public nsIXULSortService
  81. {
  82. protected:
  83. XULSortServiceImpl(void) {}
  84. virtual ~XULSortServiceImpl(void) {}
  85. friend nsresult NS_NewXULSortService(nsIXULSortService** mgr);
  86. private:
  87. public:
  88. // nsISupports
  89. NS_DECL_ISUPPORTS
  90. // nsISortService
  91. NS_DECL_NSIXULSORTSERVICE
  92. /**
  93. * Set sort and sortDirection attributes when a sort is done.
  94. */
  95. void
  96. SetSortHints(nsIContent *aNode, nsSortState* aSortState);
  97. /**
  98. * Set sortActive and sortDirection attributes on a tree column when a sort
  99. * is done. The column to change is the one with a sort attribute that
  100. * matches the sort key. The sort attributes are removed from the other
  101. * columns.
  102. */
  103. void
  104. SetSortColumnHints(nsIContent *content,
  105. const nsAString &sortResource,
  106. const nsAString &sortDirection);
  107. /**
  108. * Determine the list of items which need to be sorted. This is determined
  109. * in the following way:
  110. * - for elements that have a content builder, get its list of generated
  111. * results
  112. * - otherwise, for trees, get the child treeitems
  113. * - otherwise, get the direct children
  114. */
  115. nsresult
  116. GetItemsToSort(nsIContent *aContainer,
  117. nsSortState* aSortState,
  118. nsTArray<contentSortInfo>& aSortItems);
  119. /**
  120. * Get the list of items to sort for template built content
  121. */
  122. nsresult
  123. GetTemplateItemsToSort(nsIContent* aContainer,
  124. nsIXULTemplateBuilder* aBuilder,
  125. nsSortState* aSortState,
  126. nsTArray<contentSortInfo>& aSortItems);
  127. /**
  128. * Sort a container using the supplied sort state details.
  129. */
  130. nsresult
  131. SortContainer(nsIContent *aContainer, nsSortState* aSortState);
  132. /**
  133. * Given a list of sortable items, reverse the list. This is done
  134. * when simply changing the sort direction for the same key.
  135. */
  136. nsresult
  137. InvertSortInfo(nsTArray<contentSortInfo>& aData,
  138. int32_t aStart, int32_t aNumItems);
  139. /**
  140. * Initialize sort information from attributes specified on the container,
  141. * the sort key and sort direction.
  142. *
  143. * @param aRootElement the element that contains sort attributes
  144. * @param aContainer the container to sort, usually equal to aRootElement
  145. * @param aSortKey space separated list of sort keys
  146. * @param aSortDirection direction to sort in
  147. * @param aSortState structure filled in with sort data
  148. */
  149. static nsresult
  150. InitializeSortState(nsIContent* aRootElement,
  151. nsIContent* aContainer,
  152. const nsAString& aSortKey,
  153. const nsAString& aSortDirection,
  154. nsSortState* aSortState);
  155. /**
  156. * Compares aLeft and aRight and returns < 0, 0, or > 0. The sort
  157. * hints are checked for case matching and integer sorting.
  158. */
  159. static int32_t CompareValues(const nsAString& aLeft,
  160. const nsAString& aRight,
  161. uint32_t aSortHints);
  162. };
  163. #endif