Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / dom / base / nsQueryContentEventResult.cpp
bloba3b4c9caf9bdab30a22a7990291bf08a8361556f
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Mozilla Japan.
19 * Portions created by the Initial Developer are Copyright (C) 2009
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Masayuki Nakano <masayuki@d-toybox.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsQueryContentEventResult.h"
40 #include "nsGUIEvent.h"
41 #include "nsIWidget.h"
42 #include "nsPoint.h"
44 NS_INTERFACE_MAP_BEGIN(nsQueryContentEventResult)
45 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIQueryContentEventResult)
46 NS_INTERFACE_MAP_ENTRY(nsIQueryContentEventResult)
47 NS_INTERFACE_MAP_END
49 NS_IMPL_ADDREF(nsQueryContentEventResult)
50 NS_IMPL_RELEASE(nsQueryContentEventResult)
52 nsQueryContentEventResult::nsQueryContentEventResult() :
53 mEventID(0), mSucceeded(PR_FALSE)
57 nsQueryContentEventResult::~nsQueryContentEventResult()
61 NS_IMETHODIMP
62 nsQueryContentEventResult::GetOffset(PRUint32 *aOffset)
64 PRBool notFound;
65 nsresult rv = GetNotFound(&notFound);
66 NS_ENSURE_SUCCESS(rv, rv);
67 NS_ENSURE_TRUE(!notFound, NS_ERROR_NOT_AVAILABLE);
68 *aOffset = mOffset;
69 return NS_OK;
72 static PRBool IsRectEnabled(PRUint32 aEventID)
74 return aEventID == NS_QUERY_CARET_RECT ||
75 aEventID == NS_QUERY_TEXT_RECT ||
76 aEventID == NS_QUERY_EDITOR_RECT ||
77 aEventID == NS_QUERY_CHARACTER_AT_POINT;
80 NS_IMETHODIMP
81 nsQueryContentEventResult::GetReversed(PRBool *aReversed)
83 NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
84 NS_ENSURE_TRUE(mEventID == NS_QUERY_SELECTED_TEXT,
85 NS_ERROR_NOT_AVAILABLE);
86 *aReversed = mReversed;
87 return NS_OK;
90 NS_IMETHODIMP
91 nsQueryContentEventResult::GetLeft(PRInt32 *aLeft)
93 NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
94 NS_ENSURE_TRUE(IsRectEnabled(mEventID),
95 NS_ERROR_NOT_AVAILABLE);
96 *aLeft = mRect.x;
97 return NS_OK;
100 NS_IMETHODIMP
101 nsQueryContentEventResult::GetWidth(PRInt32 *aWidth)
103 NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
104 NS_ENSURE_TRUE(IsRectEnabled(mEventID),
105 NS_ERROR_NOT_AVAILABLE);
106 *aWidth = mRect.width;
107 return NS_OK;
110 NS_IMETHODIMP
111 nsQueryContentEventResult::GetTop(PRInt32 *aTop)
113 NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
114 NS_ENSURE_TRUE(IsRectEnabled(mEventID),
115 NS_ERROR_NOT_AVAILABLE);
116 *aTop = mRect.y;
117 return NS_OK;
120 NS_IMETHODIMP
121 nsQueryContentEventResult::GetHeight(PRInt32 *aHeight)
123 NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
124 NS_ENSURE_TRUE(IsRectEnabled(mEventID),
125 NS_ERROR_NOT_AVAILABLE);
126 *aHeight = mRect.height;
127 return NS_OK;
130 NS_IMETHODIMP
131 nsQueryContentEventResult::GetText(nsAString &aText)
133 NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
134 NS_ENSURE_TRUE(mEventID == NS_QUERY_SELECTED_TEXT ||
135 mEventID == NS_QUERY_TEXT_CONTENT,
136 NS_ERROR_NOT_AVAILABLE);
137 aText = mString;
138 return NS_OK;
141 NS_IMETHODIMP
142 nsQueryContentEventResult::GetSucceeded(PRBool *aSucceeded)
144 NS_ENSURE_TRUE(mEventID != 0, NS_ERROR_NOT_INITIALIZED);
145 *aSucceeded = mSucceeded;
146 return NS_OK;
149 NS_IMETHODIMP
150 nsQueryContentEventResult::GetNotFound(PRBool *aNotFound)
152 NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE);
153 NS_ENSURE_TRUE(mEventID == NS_QUERY_SELECTED_TEXT ||
154 mEventID == NS_QUERY_CHARACTER_AT_POINT,
155 NS_ERROR_NOT_AVAILABLE);
156 *aNotFound = (mOffset == nsQueryContentEvent::NOT_FOUND);
157 return NS_OK;
160 void
161 nsQueryContentEventResult::SetEventResult(nsIWidget* aWidget,
162 const nsQueryContentEvent &aEvent)
164 mEventID = aEvent.message;
165 mSucceeded = aEvent.mSucceeded;
166 mReversed = aEvent.mReply.mReversed;
167 mRect = aEvent.mReply.mRect;
168 mOffset = aEvent.mReply.mOffset;
169 mString = aEvent.mReply.mString;
171 if (!IsRectEnabled(mEventID) || !aWidget || !mSucceeded) {
172 return;
175 nsIWidget* topWidget = aWidget->GetTopLevelWidget();
176 if (!topWidget || topWidget == aWidget) {
177 return;
180 // Convert the top widget related coordinates to the given widget's.
181 nsIntPoint offset =
182 aWidget->WidgetToScreenOffset() - topWidget->WidgetToScreenOffset();
183 mRect.MoveBy(-offset);