1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Scott Putterman <putterman@netscape.com>
24 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Chase Tingley <tingley@sundell.net>
26 * Neil Deakin <enndeakin@sympatico.ca>
28 * Alternatively, the contents of this file may be used under the terms of
29 * either of the GNU General Public License Version 2 or later (the "GPL"),
30 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
40 * ***** END LICENSE BLOCK *****
42 * This Original Code has been modified by IBM Corporation.
43 * Modifications made by IBM described herein are
44 * Copyright (c) International Business Machines
47 * Modifications to Mozilla code or documentation
48 * identified per MPL Section 3.3
50 * Date Modified by Description of modification
51 * 03/27/2000 IBM Corp. Added PR_CALLBACK for Optlink
56 This sort service is used to sort template built content or content by attribute.
60 #include "nsCOMArray.h"
62 #include "nsIContent.h"
63 #include "nsIXULTemplateResult.h"
64 #include "nsIXULTemplateQueryProcessor.h"
65 #include "nsIXULSortService.h"
66 #include "nsCycleCollectionParticipant.h"
68 enum nsSortState_direction
{
69 nsSortState_descending
,
70 nsSortState_ascending
,
74 // the sort state holds info about the current sort
79 PRBool inbetweenSeparatorSort
;
80 PRBool sortStaticsLast
;
81 PRBool isContainerRDFSeq
;
83 nsSortState_direction direction
;
85 nsCOMArray
<nsIAtom
> sortKeys
;
87 nsCOMPtr
<nsIXULTemplateQueryProcessor
> processor
;
88 nsCOMPtr
<nsIContent
> lastContainer
;
89 PRBool lastWasFirst
, lastWasLast
;
92 : initialized(PR_FALSE
)
95 void Traverse(nsCycleCollectionTraversalCallback
&cb
) const
97 cb
.NoteXPCOMChild(processor
);
98 cb
.NoteXPCOMChild(lastContainer
);
102 // information about a particular item to be sorted
103 struct contentSortInfo
{
104 nsCOMPtr
<nsIContent
> content
;
105 nsCOMPtr
<nsIContent
> parent
;
106 nsCOMPtr
<nsIXULTemplateResult
> result
;
107 void swap(contentSortInfo
& other
)
109 content
.swap(other
.content
);
110 parent
.swap(other
.parent
);
111 result
.swap(other
.result
);
115 ////////////////////////////////////////////////////////////////////////
118 // This is the sort service.
120 class XULSortServiceImpl
: public nsIXULSortService
123 XULSortServiceImpl(void) {}
124 virtual ~XULSortServiceImpl(void) {}
126 friend nsresult
NS_NewXULSortService(nsIXULSortService
** mgr
);
135 NS_DECL_NSIXULSORTSERVICE
138 * Set sort and sortDirection attributes when a sort is done.
141 SetSortHints(nsIContent
*aNode
, nsSortState
* aSortState
);
144 * Set sortActive and sortDirection attributes on a tree column when a sort
145 * is done. The column to change is the one with a sort attribute that
146 * matches the sort key. The sort attributes are removed from the other
150 SetSortColumnHints(nsIContent
*content
,
151 const nsAString
&sortResource
,
152 const nsAString
&sortDirection
);
155 * Determine the list of items which need to be sorted. This is determined
156 * in the following way:
157 * - for elements that have a content builder, get its list of generated
159 * - otherwise, for trees, get the child treeitems
160 * - otherwise, get the direct children
163 GetItemsToSort(nsIContent
*aContainer
,
164 nsSortState
* aSortState
,
165 nsTArray
<contentSortInfo
>& aSortItems
);
168 * Get the list of items to sort for template built content
171 GetTemplateItemsToSort(nsIContent
* aContainer
,
172 nsIXULTemplateBuilder
* aBuilder
,
173 nsSortState
* aSortState
,
174 nsTArray
<contentSortInfo
>& aSortItems
);
177 * Sort a container using the supplied sort state details.
180 SortContainer(nsIContent
*aContainer
, nsSortState
* aSortState
);
183 * Given a list of sortable items, reverse the list. This is done
184 * when simply changing the sort direction for the same key.
187 InvertSortInfo(nsTArray
<contentSortInfo
>& aData
,
188 PRInt32 aStart
, PRInt32 aNumItems
);
191 * Initialize sort information from attributes specified on the container,
192 * the sort key and sort direction.
194 * @param aRootElement the element that contains sort attributes
195 * @param aContainer the container to sort, usually equal to aRootElement
196 * @param aSortKey space separated list of sort keys
197 * @param aSortDirection direction to sort in
198 * @param aSortState structure filled in with sort data
201 InitializeSortState(nsIContent
* aRootElement
,
202 nsIContent
* aContainer
,
203 const nsAString
& aSortKey
,
204 const nsAString
& aSortDirection
,
205 nsSortState
* aSortState
);