Line data Source code
1 : /* This Source Code Form is subject to the terms of the Mozilla Public
2 : * License, v. 2.0. If a copy of the MPL was not distributed with this
3 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 :
5 : #ifndef __nsAutoCompleteController__
6 : #define __nsAutoCompleteController__
7 :
8 : #include "nsIAutoCompleteController.h"
9 :
10 : #include "nsCOMPtr.h"
11 : #include "nsIAutoCompleteInput.h"
12 : #include "nsIAutoCompletePopup.h"
13 : #include "nsIAutoCompleteResult.h"
14 : #include "nsIAutoCompleteSearch.h"
15 : #include "nsString.h"
16 : #include "nsITreeView.h"
17 : #include "nsITreeSelection.h"
18 : #include "nsITimer.h"
19 : #include "nsTArray.h"
20 : #include "nsCOMArray.h"
21 : #include "nsCycleCollectionParticipant.h"
22 :
23 : class nsAutoCompleteController final : public nsIAutoCompleteController,
24 : public nsIAutoCompleteObserver,
25 : public nsITimerCallback,
26 : public nsITreeView
27 : {
28 : public:
29 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
30 29 : NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsAutoCompleteController,
31 : nsIAutoCompleteController)
32 : NS_DECL_NSIAUTOCOMPLETECONTROLLER
33 : NS_DECL_NSIAUTOCOMPLETEOBSERVER
34 : NS_DECL_NSITREEVIEW
35 : NS_DECL_NSITIMERCALLBACK
36 :
37 : nsAutoCompleteController();
38 :
39 : protected:
40 : virtual ~nsAutoCompleteController();
41 :
42 : nsresult OpenPopup();
43 : nsresult ClosePopup();
44 :
45 : nsresult StartSearch(uint16_t aSearchType);
46 :
47 : nsresult BeforeSearches();
48 : nsresult StartSearches();
49 : void AfterSearches();
50 : nsresult ClearSearchTimer();
51 : void MaybeCompletePlaceholder();
52 :
53 : void HandleSearchResult(nsIAutoCompleteSearch *aSearch,
54 : nsIAutoCompleteResult *aResult);
55 : nsresult ProcessResult(int32_t aSearchIndex, nsIAutoCompleteResult *aResult);
56 : nsresult PostSearchCleanup();
57 :
58 : nsresult EnterMatch(bool aIsPopupSelection,
59 : nsIDOMEvent *aEvent);
60 : nsresult RevertTextValue();
61 :
62 : nsresult CompleteDefaultIndex(int32_t aResultIndex);
63 : nsresult CompleteValue(nsString &aValue);
64 :
65 : nsresult GetResultAt(int32_t aIndex, nsIAutoCompleteResult** aResult,
66 : int32_t* aRowIndex);
67 : nsresult GetResultValueAt(int32_t aIndex, bool aGetFinalValue,
68 : nsAString & _retval);
69 : nsresult GetResultLabelAt(int32_t aIndex, nsAString & _retval);
70 : private:
71 : nsresult GetResultValueLabelAt(int32_t aIndex, bool aGetFinalValue,
72 : bool aGetValue, nsAString & _retval);
73 : protected:
74 :
75 : /**
76 : * Gets and validates the defaultComplete result and the relative
77 : * defaultIndex value.
78 : *
79 : * @param aResultIndex
80 : * Index of the defaultComplete result to be used. Pass -1 to search
81 : * for the first result providing a valid defaultIndex.
82 : * @param _result
83 : * The found result.
84 : * @param _defaultIndex
85 : * The defaultIndex relative to _result.
86 : */
87 : nsresult GetDefaultCompleteResult(int32_t aResultIndex,
88 : nsIAutoCompleteResult** _result,
89 : int32_t* _defaultIndex);
90 :
91 : /**
92 : * Gets the defaultComplete value to be suggested to the user.
93 : *
94 : * @param aResultIndex
95 : * Index of the defaultComplete result to be used.
96 : * @param aPreserveCasing
97 : * Whether user casing should be preserved.
98 : * @param _retval
99 : * The value to be completed.
100 : */
101 : nsresult GetDefaultCompleteValue(int32_t aResultIndex, bool aPreserveCasing,
102 : nsAString &_retval);
103 :
104 : /**
105 : * Gets the defaultComplete value to be used when the user confirms the
106 : * current match.
107 : * The value is returned only if it case-insensitively matches the current
108 : * input text, otherwise the method returns NS_ERROR_FAILURE.
109 : * This happens because we don't want to replace text if the user backspaces
110 : * just before Enter.
111 : *
112 : * @param _retval
113 : * The value to be completed.
114 : */
115 : nsresult GetFinalDefaultCompleteValue(nsAString &_retval);
116 :
117 : nsresult ClearResults();
118 :
119 : nsresult RowIndexToSearch(int32_t aRowIndex,
120 : int32_t *aSearchIndex, int32_t *aItemIndex);
121 :
122 : // members //////////////////////////////////////////
123 :
124 : nsCOMPtr<nsIAutoCompleteInput> mInput;
125 :
126 : nsCOMArray<nsIAutoCompleteSearch> mSearches;
127 : // This is used as a sparse array, always use SafeObjectAt to access it.
128 : nsCOMArray<nsIAutoCompleteResult> mResults;
129 : // Temporarily keeps the results alive while invoking startSearch() for each
130 : // search. This is needed to allow the searches to reuse the previous result,
131 : // since otherwise the first search clears mResults.
132 : nsCOMArray<nsIAutoCompleteResult> mResultCache;
133 :
134 : nsCOMPtr<nsITimer> mTimer;
135 : nsCOMPtr<nsITreeSelection> mSelection;
136 : nsCOMPtr<nsITreeBoxObject> mTree;
137 :
138 : nsString mSearchString;
139 : nsString mPlaceholderCompletionString;
140 : bool mDefaultIndexCompleted;
141 : bool mPopupClosedByCompositionStart;
142 :
143 : // Whether autofill is allowed for the next search. May be retrieved by the
144 : // search through the "prohibit-autofill" searchParam.
145 : bool mProhibitAutoFill;
146 :
147 : // Indicates whether the user cleared the autofilled part, returning to the
148 : // originally entered search string.
149 : bool mUserClearedAutoFill;
150 :
151 : // Indicates whether clearing the autofilled string should issue a new search.
152 : bool mClearingAutoFillSearchesAgain;
153 :
154 : enum CompositionState {
155 : eCompositionState_None,
156 : eCompositionState_Composing,
157 : eCompositionState_Committing
158 : };
159 : CompositionState mCompositionState;
160 : uint16_t mSearchStatus;
161 : uint32_t mRowCount;
162 : uint32_t mSearchesOngoing;
163 : uint32_t mSearchesFailed;
164 : bool mFirstSearchResult;
165 : uint32_t mImmediateSearchesCount;
166 : // The index of the match on the popup that was selected using the keyboard,
167 : // if the completeselectedindex attribute is set.
168 : // This is used to distinguish that selection (which would have been put in
169 : // the input on being selected) from a moused-over selectedIndex value. This
170 : // distinction is used to prevent mouse moves from inadvertently changing
171 : // what happens once the user hits Enter on the keyboard.
172 : // See bug 1043584 for more details.
173 : int32_t mCompletedSelectionIndex;
174 : };
175 :
176 : #endif /* __nsAutoCompleteController__ */
|