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