Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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)
23 {
24 }
26 nsQueryContentEventResult::~nsQueryContentEventResult()
27 {
28 }
30 NS_IMETHODIMP
31 nsQueryContentEventResult::GetOffset(uint32_t *aOffset)
32 {
33 bool notFound;
34 nsresult rv = GetNotFound(¬Found);
35 NS_ENSURE_SUCCESS(rv, rv);
36 NS_ENSURE_TRUE(!notFound, NS_ERROR_NOT_AVAILABLE);
37 *aOffset = mOffset;
38 return NS_OK;
39 }
41 static bool IsRectEnabled(uint32_t aEventID)
42 {
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;
47 }
49 NS_IMETHODIMP
50 nsQueryContentEventResult::GetReversed(bool *aReversed)
51 {
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;
57 }
59 NS_IMETHODIMP
60 nsQueryContentEventResult::GetLeft(int32_t *aLeft)
61 {
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;
67 }
69 NS_IMETHODIMP
70 nsQueryContentEventResult::GetWidth(int32_t *aWidth)
71 {
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;
77 }
79 NS_IMETHODIMP
80 nsQueryContentEventResult::GetTop(int32_t *aTop)
81 {
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;
87 }
89 NS_IMETHODIMP
90 nsQueryContentEventResult::GetHeight(int32_t *aHeight)
91 {
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;
97 }
99 NS_IMETHODIMP
100 nsQueryContentEventResult::GetText(nsAString &aText)
101 {
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;
108 }
110 NS_IMETHODIMP
111 nsQueryContentEventResult::GetSucceeded(bool *aSucceeded)
112 {
113 NS_ENSURE_TRUE(mEventID != 0, NS_ERROR_NOT_INITIALIZED);
114 *aSucceeded = mSucceeded;
115 return NS_OK;
116 }
118 NS_IMETHODIMP
119 nsQueryContentEventResult::GetNotFound(bool *aNotFound)
120 {
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;
127 }
129 void
130 nsQueryContentEventResult::SetEventResult(nsIWidget* aWidget,
131 const WidgetQueryContentEvent &aEvent)
132 {
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;
142 }
144 nsIWidget* topWidget = aWidget->GetTopLevelWidget();
145 if (!topWidget || topWidget == aWidget) {
146 return;
147 }
149 // Convert the top widget related coordinates to the given widget's.
150 nsIntPoint offset =
151 aWidget->WidgetToScreenOffset() - topWidget->WidgetToScreenOffset();
152 mRect.MoveBy(-offset);
153 }