summaryrefslogtreecommitdiffstats
path: root/dom/base/nsQueryContentEventResult.cpp
blob: 9996d1bf6b6fc2a32f93e518a6ecb14bc148f49c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsQueryContentEventResult.h"

#include <utility>

#include "mozilla/TextEvents.h"
#include "nsIWidget.h"
#include "nsPoint.h"

using namespace mozilla;

/******************************************************************************
 * Is*PropertyAvailable() methods which check if the property is available
 * (valid) with the event message.
 ******************************************************************************/

static bool IsNotFoundPropertyAvailable(EventMessage aEventMessage) {
  return aEventMessage == eQuerySelectedText ||
         aEventMessage == eQueryCharacterAtPoint;
}

static bool IsOffsetPropertyAvailable(EventMessage aEventMessage) {
  return aEventMessage == eQueryTextContent ||
         aEventMessage == eQueryTextRect || aEventMessage == eQueryCaretRect ||
         IsNotFoundPropertyAvailable(aEventMessage);
}

static bool IsRectRelatedPropertyAvailable(EventMessage aEventMessage) {
  return aEventMessage == eQueryCaretRect || aEventMessage == eQueryTextRect ||
         aEventMessage == eQueryEditorRect ||
         aEventMessage == eQueryCharacterAtPoint;
}

/******************************************************************************
 * nsQueryContentEventResult
 ******************************************************************************/

NS_INTERFACE_MAP_BEGIN(nsQueryContentEventResult)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIQueryContentEventResult)
  NS_INTERFACE_MAP_ENTRY(nsIQueryContentEventResult)
NS_INTERFACE_MAP_END

NS_IMPL_ADDREF(nsQueryContentEventResult)
NS_IMPL_RELEASE(nsQueryContentEventResult)

nsQueryContentEventResult::nsQueryContentEventResult(
    mozilla::WidgetQueryContentEvent&& aEvent)
    : mEventMessage(aEvent.mMessage),
      mSucceeded(aEvent.Succeeded()),
      mReversed(false) {
  if (mSucceeded) {
    mOffsetAndData = std::move(aEvent.mReply->mOffsetAndData);
    mTentativeCaretOffset = std::move(aEvent.mReply->mTentativeCaretOffset);
    mRect = std::move(aEvent.mReply->mRect);
    mRectArray = std::move(aEvent.mReply->mRectArray);
    mReversed = aEvent.mReply->mReversed;
  }
  // Mark as result that is longer used.
  aEvent.mReply.reset();
}

NS_IMETHODIMP
nsQueryContentEventResult::GetOffset(uint32_t* aOffset) {
  if (NS_WARN_IF(!mSucceeded)) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  if (NS_WARN_IF(!IsOffsetPropertyAvailable(mEventMessage))) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  // With some event message, both offset and notFound properties are available.
  // In that case, offset value may mean "not found".  If so, this method
  // shouldn't return mOffset as the result because it's a special value for
  // "not found".
  if (IsNotFoundPropertyAvailable(mEventMessage)) {
    bool notFound;
    nsresult rv = GetNotFound(&notFound);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;  // Just an unexpected case...
    }
    // As said above, if mOffset means "not found", offset property shouldn't
    // return its value without any errors.
    if (NS_WARN_IF(notFound)) {
      return NS_ERROR_NOT_AVAILABLE;
    }
  }

  *aOffset = mOffsetAndData->StartOffset();
  return NS_OK;
}

NS_IMETHODIMP
nsQueryContentEventResult::GetTentativeCaretOffset(uint32_t* aOffset) {
  bool notFound;
  nsresult rv = GetTentativeCaretOffsetNotFound(&notFound);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }
  if (NS_WARN_IF(notFound)) {
    return NS_ERROR_NOT_AVAILABLE;
  }
  *aOffset = mTentativeCaretOffset.value();
  return NS_OK;
}

NS_IMETHODIMP
nsQueryContentEventResult::GetReversed(bool* aReversed) {
  NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
  NS_ENSURE_TRUE(mEventMessage == eQuerySelectedText, NS_ERROR_NOT_AVAILABLE);
  *aReversed = mReversed;
  return NS_OK;
}

NS_IMETHODIMP
nsQueryContentEventResult::GetLeft(int32_t* aLeft) {
  NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
  NS_ENSURE_TRUE(IsRectRelatedPropertyAvailable(mEventMessage),
                 NS_ERROR_NOT_AVAILABLE);
  *aLeft = mRect.x;
  return NS_OK;
}

NS_IMETHODIMP
nsQueryContentEventResult::GetWidth(int32_t* aWidth) {
  NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
  NS_ENSURE_TRUE(IsRectRelatedPropertyAvailable(mEventMessage),
                 NS_ERROR_NOT_AVAILABLE);
  *aWidth = mRect.Width();
  return NS_OK;
}

NS_IMETHODIMP
nsQueryContentEventResult::GetTop(int32_t* aTop) {
  NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
  NS_ENSURE_TRUE(IsRectRelatedPropertyAvailable(mEventMessage),
                 NS_ERROR_NOT_AVAILABLE);
  *aTop = mRect.y;
  return NS_OK;
}

NS_IMETHODIMP
nsQueryContentEventResult::GetHeight(int32_t* aHeight) {
  NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
  NS_ENSURE_TRUE(IsRectRelatedPropertyAvailable(mEventMessage),
                 NS_ERROR_NOT_AVAILABLE);
  *aHeight = mRect.Height();
  return NS_OK;
}

NS_IMETHODIMP
nsQueryContentEventResult::GetText(nsAString& aText) {
  NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
  NS_ENSURE_TRUE(mEventMessage == eQuerySelectedText ||
                     mEventMessage == eQueryTextContent ||
                     mEventMessage == eQueryTextRect,
                 NS_ERROR_NOT_AVAILABLE);
  NS_ENSURE_TRUE(mOffsetAndData.isSome(), NS_ERROR_NOT_AVAILABLE);
  aText = mOffsetAndData->DataRef();
  return NS_OK;
}

NS_IMETHODIMP
nsQueryContentEventResult::GetSucceeded(bool* aSucceeded) {
  NS_ENSURE_TRUE(mEventMessage != eVoidEvent, NS_ERROR_NOT_INITIALIZED);
  *aSucceeded = mSucceeded;
  return NS_OK;
}

NS_IMETHODIMP
nsQueryContentEventResult::GetNotFound(bool* aNotFound) {
  if (NS_WARN_IF(!mSucceeded) ||
      NS_WARN_IF(!IsNotFoundPropertyAvailable(mEventMessage))) {
    return NS_ERROR_NOT_AVAILABLE;
  }
  *aNotFound = mOffsetAndData.isNothing();
  return NS_OK;
}

NS_IMETHODIMP
nsQueryContentEventResult::GetTentativeCaretOffsetNotFound(bool* aNotFound) {
  if (NS_WARN_IF(!mSucceeded)) {
    return NS_ERROR_NOT_AVAILABLE;
  }
  if (NS_WARN_IF(mEventMessage != eQueryCharacterAtPoint)) {
    return NS_ERROR_NOT_AVAILABLE;
  }
  *aNotFound = mTentativeCaretOffset.isNothing();
  return NS_OK;
}

NS_IMETHODIMP
nsQueryContentEventResult::GetCharacterRect(int32_t aOffset, int32_t* aLeft,
                                            int32_t* aTop, int32_t* aWidth,
                                            int32_t* aHeight) {
  NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
  NS_ENSURE_TRUE(mEventMessage == eQueryTextRectArray, NS_ERROR_NOT_AVAILABLE);

  if (NS_WARN_IF(mRectArray.Length() <= static_cast<size_t>(aOffset))) {
    return NS_ERROR_FAILURE;
  }

  *aLeft = mRectArray[aOffset].x;
  *aTop = mRectArray[aOffset].y;
  *aWidth = mRectArray[aOffset].Width();
  *aHeight = mRectArray[aOffset].Height();

  return NS_OK;
}

void nsQueryContentEventResult::SetEventResult(nsIWidget* aWidget) {
  if (!IsRectRelatedPropertyAvailable(mEventMessage) || !aWidget ||
      !mSucceeded) {
    return;
  }

  nsIWidget* topWidget = aWidget->GetTopLevelWidget();
  if (!topWidget || topWidget == aWidget) {
    return;
  }

  // Convert the top widget related coordinates to the given widget's.
  LayoutDeviceIntPoint offset =
      aWidget->WidgetToScreenOffset() - topWidget->WidgetToScreenOffset();
  mRect.MoveBy(-offset);
  for (size_t i = 0; i < mRectArray.Length(); i++) {
    mRectArray[i].MoveBy(-offset);
  }
}