1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/xul/templates/src/nsXULSortService.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,186 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.8 + * 1.9 + * This Original Code has been modified by IBM Corporation. 1.10 + * Modifications made by IBM described herein are 1.11 + * Copyright (c) International Business Machines 1.12 + * Corporation, 2000 1.13 + * 1.14 + * Modifications to Mozilla code or documentation 1.15 + * identified per MPL Section 3.3 1.16 + * 1.17 + * Date Modified by Description of modification 1.18 + * 03/27/2000 IBM Corp. Added PR_CALLBACK for Optlink 1.19 + * use in OS2 1.20 + */ 1.21 + 1.22 +/* 1.23 + This sort service is used to sort template built content or content by attribute. 1.24 + */ 1.25 + 1.26 +#ifndef nsXULTemplateResultSetRDF_h 1.27 +#define nsXULTemplateResultSetRDF_h 1.28 + 1.29 +#include "nsCOMPtr.h" 1.30 +#include "nsCOMArray.h" 1.31 +#include "nsTArray.h" 1.32 +#include "nsIContent.h" 1.33 +#include "nsIXULTemplateResult.h" 1.34 +#include "nsIXULTemplateQueryProcessor.h" 1.35 +#include "nsIXULSortService.h" 1.36 +#include "nsCycleCollectionParticipant.h" 1.37 + 1.38 +enum nsSortState_direction { 1.39 + nsSortState_descending, 1.40 + nsSortState_ascending, 1.41 + nsSortState_natural 1.42 +}; 1.43 + 1.44 +// the sort state holds info about the current sort 1.45 +struct nsSortState 1.46 +{ 1.47 + bool initialized; 1.48 + bool invertSort; 1.49 + bool inbetweenSeparatorSort; 1.50 + bool sortStaticsLast; 1.51 + bool isContainerRDFSeq; 1.52 + 1.53 + uint32_t sortHints; 1.54 + 1.55 + nsSortState_direction direction; 1.56 + nsAutoString sort; 1.57 + nsCOMArray<nsIAtom> sortKeys; 1.58 + 1.59 + nsCOMPtr<nsIXULTemplateQueryProcessor> processor; 1.60 + nsCOMPtr<nsIContent> lastContainer; 1.61 + bool lastWasFirst, lastWasLast; 1.62 + 1.63 + nsSortState() 1.64 + : initialized(false), 1.65 + sortHints(0) 1.66 + { 1.67 + } 1.68 + void Traverse(nsCycleCollectionTraversalCallback &cb) const 1.69 + { 1.70 + cb.NoteXPCOMChild(processor); 1.71 + cb.NoteXPCOMChild(lastContainer); 1.72 + } 1.73 +}; 1.74 + 1.75 +// information about a particular item to be sorted 1.76 +struct contentSortInfo { 1.77 + nsCOMPtr<nsIContent> content; 1.78 + nsCOMPtr<nsIContent> parent; 1.79 + nsCOMPtr<nsIXULTemplateResult> result; 1.80 + void swap(contentSortInfo& other) 1.81 + { 1.82 + content.swap(other.content); 1.83 + parent.swap(other.parent); 1.84 + result.swap(other.result); 1.85 + } 1.86 +}; 1.87 + 1.88 +//////////////////////////////////////////////////////////////////////// 1.89 +// ServiceImpl 1.90 +// 1.91 +// This is the sort service. 1.92 +// 1.93 +class XULSortServiceImpl : public nsIXULSortService 1.94 +{ 1.95 +protected: 1.96 + XULSortServiceImpl(void) {} 1.97 + virtual ~XULSortServiceImpl(void) {} 1.98 + 1.99 + friend nsresult NS_NewXULSortService(nsIXULSortService** mgr); 1.100 + 1.101 +private: 1.102 + 1.103 +public: 1.104 + // nsISupports 1.105 + NS_DECL_ISUPPORTS 1.106 + 1.107 + // nsISortService 1.108 + NS_DECL_NSIXULSORTSERVICE 1.109 + 1.110 + /** 1.111 + * Set sort and sortDirection attributes when a sort is done. 1.112 + */ 1.113 + void 1.114 + SetSortHints(nsIContent *aNode, nsSortState* aSortState); 1.115 + 1.116 + /** 1.117 + * Set sortActive and sortDirection attributes on a tree column when a sort 1.118 + * is done. The column to change is the one with a sort attribute that 1.119 + * matches the sort key. The sort attributes are removed from the other 1.120 + * columns. 1.121 + */ 1.122 + void 1.123 + SetSortColumnHints(nsIContent *content, 1.124 + const nsAString &sortResource, 1.125 + const nsAString &sortDirection); 1.126 + 1.127 + /** 1.128 + * Determine the list of items which need to be sorted. This is determined 1.129 + * in the following way: 1.130 + * - for elements that have a content builder, get its list of generated 1.131 + * results 1.132 + * - otherwise, for trees, get the child treeitems 1.133 + * - otherwise, get the direct children 1.134 + */ 1.135 + nsresult 1.136 + GetItemsToSort(nsIContent *aContainer, 1.137 + nsSortState* aSortState, 1.138 + nsTArray<contentSortInfo>& aSortItems); 1.139 + 1.140 + /** 1.141 + * Get the list of items to sort for template built content 1.142 + */ 1.143 + nsresult 1.144 + GetTemplateItemsToSort(nsIContent* aContainer, 1.145 + nsIXULTemplateBuilder* aBuilder, 1.146 + nsSortState* aSortState, 1.147 + nsTArray<contentSortInfo>& aSortItems); 1.148 + 1.149 + /** 1.150 + * Sort a container using the supplied sort state details. 1.151 + */ 1.152 + nsresult 1.153 + SortContainer(nsIContent *aContainer, nsSortState* aSortState); 1.154 + 1.155 + /** 1.156 + * Given a list of sortable items, reverse the list. This is done 1.157 + * when simply changing the sort direction for the same key. 1.158 + */ 1.159 + nsresult 1.160 + InvertSortInfo(nsTArray<contentSortInfo>& aData, 1.161 + int32_t aStart, int32_t aNumItems); 1.162 + 1.163 + /** 1.164 + * Initialize sort information from attributes specified on the container, 1.165 + * the sort key and sort direction. 1.166 + * 1.167 + * @param aRootElement the element that contains sort attributes 1.168 + * @param aContainer the container to sort, usually equal to aRootElement 1.169 + * @param aSortKey space separated list of sort keys 1.170 + * @param aSortDirection direction to sort in 1.171 + * @param aSortState structure filled in with sort data 1.172 + */ 1.173 + static nsresult 1.174 + InitializeSortState(nsIContent* aRootElement, 1.175 + nsIContent* aContainer, 1.176 + const nsAString& aSortKey, 1.177 + const nsAString& aSortDirection, 1.178 + nsSortState* aSortState); 1.179 + 1.180 + /** 1.181 + * Compares aLeft and aRight and returns < 0, 0, or > 0. The sort 1.182 + * hints are checked for case matching and integer sorting. 1.183 + */ 1.184 + static int32_t CompareValues(const nsAString& aLeft, 1.185 + const nsAString& aRight, 1.186 + uint32_t aSortHints); 1.187 +}; 1.188 + 1.189 +#endif