/src/mozilla-central/dom/base/nsQueryContentEventResult.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "nsIWidget.h" |
8 | | #include "nsPoint.h" |
9 | | #include "nsQueryContentEventResult.h" |
10 | | #include "mozilla/Move.h" |
11 | | #include "mozilla/TextEvents.h" |
12 | | |
13 | | using namespace mozilla; |
14 | | |
15 | | /****************************************************************************** |
16 | | * Is*PropertyAvailable() methods which check if the property is available |
17 | | * (valid) with the event message. |
18 | | ******************************************************************************/ |
19 | | |
20 | | static bool IsNotFoundPropertyAvailable(EventMessage aEventMessage) |
21 | 0 | { |
22 | 0 | return aEventMessage == eQuerySelectedText || |
23 | 0 | aEventMessage == eQueryCharacterAtPoint; |
24 | 0 | } |
25 | | |
26 | | static bool IsOffsetPropertyAvailable(EventMessage aEventMessage) |
27 | 0 | { |
28 | 0 | return aEventMessage == eQueryTextContent || |
29 | 0 | aEventMessage == eQueryTextRect || |
30 | 0 | aEventMessage == eQueryCaretRect || |
31 | 0 | IsNotFoundPropertyAvailable(aEventMessage); |
32 | 0 | } |
33 | | |
34 | | static bool IsRectRelatedPropertyAvailable(EventMessage aEventMessage) |
35 | 0 | { |
36 | 0 | return aEventMessage == eQueryCaretRect || |
37 | 0 | aEventMessage == eQueryTextRect || |
38 | 0 | aEventMessage == eQueryEditorRect || |
39 | 0 | aEventMessage == eQueryCharacterAtPoint; |
40 | 0 | } |
41 | | |
42 | | /****************************************************************************** |
43 | | * nsQueryContentEventResult |
44 | | ******************************************************************************/ |
45 | | |
46 | 0 | NS_INTERFACE_MAP_BEGIN(nsQueryContentEventResult) |
47 | 0 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIQueryContentEventResult) |
48 | 0 | NS_INTERFACE_MAP_ENTRY(nsIQueryContentEventResult) |
49 | 0 | NS_INTERFACE_MAP_END |
50 | | |
51 | | NS_IMPL_ADDREF(nsQueryContentEventResult) |
52 | | NS_IMPL_RELEASE(nsQueryContentEventResult) |
53 | | |
54 | | nsQueryContentEventResult::nsQueryContentEventResult(mozilla::WidgetQueryContentEvent &aEvent) |
55 | | : mEventMessage(aEvent.mMessage) |
56 | | , mOffset(aEvent.mReply.mOffset) |
57 | | , mTentativeCaretOffset(aEvent.mReply.mTentativeCaretOffset) |
58 | | , mString(aEvent.mReply.mString) |
59 | | , mRect(aEvent.mReply.mRect) |
60 | | , mRectArray(std::move(aEvent.mReply.mRectArray)) |
61 | | , mSucceeded(aEvent.mSucceeded) |
62 | | , mReversed(aEvent.mReply.mReversed) |
63 | 0 | { |
64 | 0 | // Mark as result that is longer used. |
65 | 0 | aEvent.mSucceeded = false; |
66 | 0 | } |
67 | | |
68 | | nsQueryContentEventResult::~nsQueryContentEventResult() |
69 | 0 | { |
70 | 0 | } |
71 | | |
72 | | NS_IMETHODIMP |
73 | | nsQueryContentEventResult::GetOffset(uint32_t* aOffset) |
74 | 0 | { |
75 | 0 | if (NS_WARN_IF(!mSucceeded)) { |
76 | 0 | return NS_ERROR_NOT_AVAILABLE; |
77 | 0 | } |
78 | 0 | |
79 | 0 | if (NS_WARN_IF(!IsOffsetPropertyAvailable(mEventMessage))) { |
80 | 0 | return NS_ERROR_NOT_AVAILABLE; |
81 | 0 | } |
82 | 0 | |
83 | 0 | // With some event message, both offset and notFound properties are available. |
84 | 0 | // In that case, offset value may mean "not found". If so, this method |
85 | 0 | // shouldn't return mOffset as the result because it's a special value for |
86 | 0 | // "not found". |
87 | 0 | if (IsNotFoundPropertyAvailable(mEventMessage)) { |
88 | 0 | bool notFound; |
89 | 0 | nsresult rv = GetNotFound(¬Found); |
90 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
91 | 0 | return rv; // Just an unexpected case... |
92 | 0 | } |
93 | 0 | // As said above, if mOffset means "not found", offset property shouldn't |
94 | 0 | // return its value without any errors. |
95 | 0 | if (NS_WARN_IF(notFound)) { |
96 | 0 | return NS_ERROR_NOT_AVAILABLE; |
97 | 0 | } |
98 | 0 | } |
99 | 0 | |
100 | 0 | *aOffset = mOffset; |
101 | 0 | return NS_OK; |
102 | 0 | } |
103 | | |
104 | | NS_IMETHODIMP |
105 | | nsQueryContentEventResult::GetTentativeCaretOffset(uint32_t* aOffset) |
106 | 0 | { |
107 | 0 | bool notFound; |
108 | 0 | nsresult rv = GetTentativeCaretOffsetNotFound(¬Found); |
109 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
110 | 0 | return rv; |
111 | 0 | } |
112 | 0 | if (NS_WARN_IF(notFound)) { |
113 | 0 | return NS_ERROR_NOT_AVAILABLE; |
114 | 0 | } |
115 | 0 | *aOffset = mTentativeCaretOffset; |
116 | 0 | return NS_OK; |
117 | 0 | } |
118 | | |
119 | | NS_IMETHODIMP |
120 | | nsQueryContentEventResult::GetReversed(bool *aReversed) |
121 | 0 | { |
122 | 0 | NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE); |
123 | 0 | NS_ENSURE_TRUE(mEventMessage == eQuerySelectedText, NS_ERROR_NOT_AVAILABLE); |
124 | 0 | *aReversed = mReversed; |
125 | 0 | return NS_OK; |
126 | 0 | } |
127 | | |
128 | | NS_IMETHODIMP |
129 | | nsQueryContentEventResult::GetLeft(int32_t *aLeft) |
130 | 0 | { |
131 | 0 | NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE); |
132 | 0 | NS_ENSURE_TRUE(IsRectRelatedPropertyAvailable(mEventMessage), |
133 | 0 | NS_ERROR_NOT_AVAILABLE); |
134 | 0 | *aLeft = mRect.x; |
135 | 0 | return NS_OK; |
136 | 0 | } |
137 | | |
138 | | NS_IMETHODIMP |
139 | | nsQueryContentEventResult::GetWidth(int32_t *aWidth) |
140 | 0 | { |
141 | 0 | NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE); |
142 | 0 | NS_ENSURE_TRUE(IsRectRelatedPropertyAvailable(mEventMessage), |
143 | 0 | NS_ERROR_NOT_AVAILABLE); |
144 | 0 | *aWidth = mRect.Width(); |
145 | 0 | return NS_OK; |
146 | 0 | } |
147 | | |
148 | | NS_IMETHODIMP |
149 | | nsQueryContentEventResult::GetTop(int32_t *aTop) |
150 | 0 | { |
151 | 0 | NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE); |
152 | 0 | NS_ENSURE_TRUE(IsRectRelatedPropertyAvailable(mEventMessage), |
153 | 0 | NS_ERROR_NOT_AVAILABLE); |
154 | 0 | *aTop = mRect.y; |
155 | 0 | return NS_OK; |
156 | 0 | } |
157 | | |
158 | | NS_IMETHODIMP |
159 | | nsQueryContentEventResult::GetHeight(int32_t *aHeight) |
160 | 0 | { |
161 | 0 | NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE); |
162 | 0 | NS_ENSURE_TRUE(IsRectRelatedPropertyAvailable(mEventMessage), |
163 | 0 | NS_ERROR_NOT_AVAILABLE); |
164 | 0 | *aHeight = mRect.Height(); |
165 | 0 | return NS_OK; |
166 | 0 | } |
167 | | |
168 | | NS_IMETHODIMP |
169 | | nsQueryContentEventResult::GetText(nsAString &aText) |
170 | 0 | { |
171 | 0 | NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE); |
172 | 0 | NS_ENSURE_TRUE(mEventMessage == eQuerySelectedText || |
173 | 0 | mEventMessage == eQueryTextContent || |
174 | 0 | mEventMessage == eQueryTextRect, |
175 | 0 | NS_ERROR_NOT_AVAILABLE); |
176 | 0 | aText = mString; |
177 | 0 | return NS_OK; |
178 | 0 | } |
179 | | |
180 | | NS_IMETHODIMP |
181 | | nsQueryContentEventResult::GetSucceeded(bool *aSucceeded) |
182 | 0 | { |
183 | 0 | NS_ENSURE_TRUE(mEventMessage != eVoidEvent, NS_ERROR_NOT_INITIALIZED); |
184 | 0 | *aSucceeded = mSucceeded; |
185 | 0 | return NS_OK; |
186 | 0 | } |
187 | | |
188 | | NS_IMETHODIMP |
189 | | nsQueryContentEventResult::GetNotFound(bool* aNotFound) |
190 | 0 | { |
191 | 0 | if (NS_WARN_IF(!mSucceeded) || |
192 | 0 | NS_WARN_IF(!IsNotFoundPropertyAvailable(mEventMessage))) { |
193 | 0 | return NS_ERROR_NOT_AVAILABLE; |
194 | 0 | } |
195 | 0 | *aNotFound = (mOffset == WidgetQueryContentEvent::NOT_FOUND); |
196 | 0 | return NS_OK; |
197 | 0 | } |
198 | | |
199 | | NS_IMETHODIMP |
200 | | nsQueryContentEventResult::GetTentativeCaretOffsetNotFound(bool* aNotFound) |
201 | 0 | { |
202 | 0 | if (NS_WARN_IF(!mSucceeded)) { |
203 | 0 | return NS_ERROR_NOT_AVAILABLE; |
204 | 0 | } |
205 | 0 | if (NS_WARN_IF(mEventMessage != eQueryCharacterAtPoint)) { |
206 | 0 | return NS_ERROR_NOT_AVAILABLE; |
207 | 0 | } |
208 | 0 | *aNotFound = (mTentativeCaretOffset == WidgetQueryContentEvent::NOT_FOUND); |
209 | 0 | return NS_OK; |
210 | 0 | } |
211 | | |
212 | | NS_IMETHODIMP |
213 | | nsQueryContentEventResult::GetCharacterRect(int32_t aOffset, |
214 | | int32_t* aLeft, int32_t* aTop, |
215 | | int32_t* aWidth, int32_t* aHeight) |
216 | 0 | { |
217 | 0 | NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE); |
218 | 0 | NS_ENSURE_TRUE(mEventMessage == eQueryTextRectArray, |
219 | 0 | NS_ERROR_NOT_AVAILABLE); |
220 | 0 |
|
221 | 0 | if (NS_WARN_IF(mRectArray.Length() <= static_cast<uint32_t>(aOffset))) { |
222 | 0 | return NS_ERROR_FAILURE; |
223 | 0 | } |
224 | 0 | |
225 | 0 | *aLeft = mRectArray[aOffset].x; |
226 | 0 | *aTop = mRectArray[aOffset].y; |
227 | 0 | *aWidth = mRectArray[aOffset].Width(); |
228 | 0 | *aHeight = mRectArray[aOffset].Height(); |
229 | 0 |
|
230 | 0 | return NS_OK; |
231 | 0 | } |
232 | | |
233 | | void |
234 | | nsQueryContentEventResult::SetEventResult(nsIWidget* aWidget) |
235 | 0 | { |
236 | 0 | if (!IsRectRelatedPropertyAvailable(mEventMessage) || |
237 | 0 | !aWidget || !mSucceeded) { |
238 | 0 | return; |
239 | 0 | } |
240 | 0 | |
241 | 0 | nsIWidget* topWidget = aWidget->GetTopLevelWidget(); |
242 | 0 | if (!topWidget || topWidget == aWidget) { |
243 | 0 | return; |
244 | 0 | } |
245 | 0 | |
246 | 0 | // Convert the top widget related coordinates to the given widget's. |
247 | 0 | LayoutDeviceIntPoint offset = |
248 | 0 | aWidget->WidgetToScreenOffset() - topWidget->WidgetToScreenOffset(); |
249 | 0 | mRect.MoveBy(-offset); |
250 | 0 | for (size_t i = 0; i < mRectArray.Length(); i++) { |
251 | 0 | mRectArray[i].MoveBy(-offset); |
252 | 0 | } |
253 | 0 | } |