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