Bumping manifests a=b2g-bump
[gecko.git] / dom / base / nsQueryContentEventResult.cpp
blobbd2b7aee63b4282fe921b98538f904ef81cd47b4
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsQueryContentEventResult.h"
7 #include "nsIWidget.h"
8 #include "nsPoint.h"
9 #include "mozilla/TextEvents.h"
11 using namespace mozilla;
13 NS_INTERFACE_MAP_BEGIN(nsQueryContentEventResult)
14 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIQueryContentEventResult)
15 NS_INTERFACE_MAP_ENTRY(nsIQueryContentEventResult)
16 NS_INTERFACE_MAP_END
18 NS_IMPL_ADDREF(nsQueryContentEventResult)
19 NS_IMPL_RELEASE(nsQueryContentEventResult)
21 nsQueryContentEventResult::nsQueryContentEventResult() :
22 mEventID(0), mSucceeded(false)
26 nsQueryContentEventResult::~nsQueryContentEventResult()
30 NS_IMETHODIMP
31 nsQueryContentEventResult::GetOffset(uint32_t *aOffset)
33 bool notFound;
34 nsresult rv = GetNotFound(&notFound);
35 NS_ENSURE_SUCCESS(rv, rv);
36 NS_ENSURE_TRUE(!notFound, NS_ERROR_NOT_AVAILABLE);
37 *aOffset = mOffset;
38 return NS_OK;
41 static bool IsRectEnabled(uint32_t aEventID)
43 return aEventID == NS_QUERY_CARET_RECT ||
44 aEventID == NS_QUERY_TEXT_RECT ||
45 aEventID == NS_QUERY_EDITOR_RECT ||
46 aEventID == NS_QUERY_CHARACTER_AT_POINT;
49 NS_IMETHODIMP
50 nsQueryContentEventResult::GetReversed(bool *aReversed)
52 NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
53 NS_ENSURE_TRUE(mEventID == NS_QUERY_SELECTED_TEXT,
54 NS_ERROR_NOT_AVAILABLE);
55 *aReversed = mReversed;
56 return NS_OK;
59 NS_IMETHODIMP
60 nsQueryContentEventResult::GetLeft(int32_t *aLeft)
62 NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
63 NS_ENSURE_TRUE(IsRectEnabled(mEventID),
64 NS_ERROR_NOT_AVAILABLE);
65 *aLeft = mRect.x;
66 return NS_OK;
69 NS_IMETHODIMP
70 nsQueryContentEventResult::GetWidth(int32_t *aWidth)
72 NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
73 NS_ENSURE_TRUE(IsRectEnabled(mEventID),
74 NS_ERROR_NOT_AVAILABLE);
75 *aWidth = mRect.width;
76 return NS_OK;
79 NS_IMETHODIMP
80 nsQueryContentEventResult::GetTop(int32_t *aTop)
82 NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
83 NS_ENSURE_TRUE(IsRectEnabled(mEventID),
84 NS_ERROR_NOT_AVAILABLE);
85 *aTop = mRect.y;
86 return NS_OK;
89 NS_IMETHODIMP
90 nsQueryContentEventResult::GetHeight(int32_t *aHeight)
92 NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
93 NS_ENSURE_TRUE(IsRectEnabled(mEventID),
94 NS_ERROR_NOT_AVAILABLE);
95 *aHeight = mRect.height;
96 return NS_OK;
99 NS_IMETHODIMP
100 nsQueryContentEventResult::GetText(nsAString &aText)
102 NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
103 NS_ENSURE_TRUE(mEventID == NS_QUERY_SELECTED_TEXT ||
104 mEventID == NS_QUERY_TEXT_CONTENT,
105 NS_ERROR_NOT_AVAILABLE);
106 aText = mString;
107 return NS_OK;
110 NS_IMETHODIMP
111 nsQueryContentEventResult::GetSucceeded(bool *aSucceeded)
113 NS_ENSURE_TRUE(mEventID != 0, NS_ERROR_NOT_INITIALIZED);
114 *aSucceeded = mSucceeded;
115 return NS_OK;
118 NS_IMETHODIMP
119 nsQueryContentEventResult::GetNotFound(bool *aNotFound)
121 NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
122 NS_ENSURE_TRUE(mEventID == NS_QUERY_SELECTED_TEXT ||
123 mEventID == NS_QUERY_CHARACTER_AT_POINT,
124 NS_ERROR_NOT_AVAILABLE);
125 *aNotFound = (mOffset == WidgetQueryContentEvent::NOT_FOUND);
126 return NS_OK;
129 void
130 nsQueryContentEventResult::SetEventResult(nsIWidget* aWidget,
131 const WidgetQueryContentEvent &aEvent)
133 mEventID = aEvent.message;
134 mSucceeded = aEvent.mSucceeded;
135 mReversed = aEvent.mReply.mReversed;
136 mRect = aEvent.mReply.mRect;
137 mOffset = aEvent.mReply.mOffset;
138 mString = aEvent.mReply.mString;
140 if (!IsRectEnabled(mEventID) || !aWidget || !mSucceeded) {
141 return;
144 nsIWidget* topWidget = aWidget->GetTopLevelWidget();
145 if (!topWidget || topWidget == aWidget) {
146 return;
149 // Convert the top widget related coordinates to the given widget's.
150 nsIntPoint offset =
151 aWidget->WidgetToScreenOffset() - topWidget->WidgetToScreenOffset();
152 mRect.MoveBy(-offset);