1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/base/nsQueryContentEventResult.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,153 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsQueryContentEventResult.h" 1.10 +#include "nsIWidget.h" 1.11 +#include "nsPoint.h" 1.12 +#include "mozilla/TextEvents.h" 1.13 + 1.14 +using namespace mozilla; 1.15 + 1.16 +NS_INTERFACE_MAP_BEGIN(nsQueryContentEventResult) 1.17 + NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIQueryContentEventResult) 1.18 + NS_INTERFACE_MAP_ENTRY(nsIQueryContentEventResult) 1.19 +NS_INTERFACE_MAP_END 1.20 + 1.21 +NS_IMPL_ADDREF(nsQueryContentEventResult) 1.22 +NS_IMPL_RELEASE(nsQueryContentEventResult) 1.23 + 1.24 +nsQueryContentEventResult::nsQueryContentEventResult() : 1.25 + mEventID(0), mSucceeded(false) 1.26 +{ 1.27 +} 1.28 + 1.29 +nsQueryContentEventResult::~nsQueryContentEventResult() 1.30 +{ 1.31 +} 1.32 + 1.33 +NS_IMETHODIMP 1.34 +nsQueryContentEventResult::GetOffset(uint32_t *aOffset) 1.35 +{ 1.36 + bool notFound; 1.37 + nsresult rv = GetNotFound(¬Found); 1.38 + NS_ENSURE_SUCCESS(rv, rv); 1.39 + NS_ENSURE_TRUE(!notFound, NS_ERROR_NOT_AVAILABLE); 1.40 + *aOffset = mOffset; 1.41 + return NS_OK; 1.42 +} 1.43 + 1.44 +static bool IsRectEnabled(uint32_t aEventID) 1.45 +{ 1.46 + return aEventID == NS_QUERY_CARET_RECT || 1.47 + aEventID == NS_QUERY_TEXT_RECT || 1.48 + aEventID == NS_QUERY_EDITOR_RECT || 1.49 + aEventID == NS_QUERY_CHARACTER_AT_POINT; 1.50 +} 1.51 + 1.52 +NS_IMETHODIMP 1.53 +nsQueryContentEventResult::GetReversed(bool *aReversed) 1.54 +{ 1.55 + NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE); 1.56 + NS_ENSURE_TRUE(mEventID == NS_QUERY_SELECTED_TEXT, 1.57 + NS_ERROR_NOT_AVAILABLE); 1.58 + *aReversed = mReversed; 1.59 + return NS_OK; 1.60 +} 1.61 + 1.62 +NS_IMETHODIMP 1.63 +nsQueryContentEventResult::GetLeft(int32_t *aLeft) 1.64 +{ 1.65 + NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE); 1.66 + NS_ENSURE_TRUE(IsRectEnabled(mEventID), 1.67 + NS_ERROR_NOT_AVAILABLE); 1.68 + *aLeft = mRect.x; 1.69 + return NS_OK; 1.70 +} 1.71 + 1.72 +NS_IMETHODIMP 1.73 +nsQueryContentEventResult::GetWidth(int32_t *aWidth) 1.74 +{ 1.75 + NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE); 1.76 + NS_ENSURE_TRUE(IsRectEnabled(mEventID), 1.77 + NS_ERROR_NOT_AVAILABLE); 1.78 + *aWidth = mRect.width; 1.79 + return NS_OK; 1.80 +} 1.81 + 1.82 +NS_IMETHODIMP 1.83 +nsQueryContentEventResult::GetTop(int32_t *aTop) 1.84 +{ 1.85 + NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE); 1.86 + NS_ENSURE_TRUE(IsRectEnabled(mEventID), 1.87 + NS_ERROR_NOT_AVAILABLE); 1.88 + *aTop = mRect.y; 1.89 + return NS_OK; 1.90 +} 1.91 + 1.92 +NS_IMETHODIMP 1.93 +nsQueryContentEventResult::GetHeight(int32_t *aHeight) 1.94 +{ 1.95 + NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE); 1.96 + NS_ENSURE_TRUE(IsRectEnabled(mEventID), 1.97 + NS_ERROR_NOT_AVAILABLE); 1.98 + *aHeight = mRect.height; 1.99 + return NS_OK; 1.100 +} 1.101 + 1.102 +NS_IMETHODIMP 1.103 +nsQueryContentEventResult::GetText(nsAString &aText) 1.104 +{ 1.105 + NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE); 1.106 + NS_ENSURE_TRUE(mEventID == NS_QUERY_SELECTED_TEXT || 1.107 + mEventID == NS_QUERY_TEXT_CONTENT, 1.108 + NS_ERROR_NOT_AVAILABLE); 1.109 + aText = mString; 1.110 + return NS_OK; 1.111 +} 1.112 + 1.113 +NS_IMETHODIMP 1.114 +nsQueryContentEventResult::GetSucceeded(bool *aSucceeded) 1.115 +{ 1.116 + NS_ENSURE_TRUE(mEventID != 0, NS_ERROR_NOT_INITIALIZED); 1.117 + *aSucceeded = mSucceeded; 1.118 + return NS_OK; 1.119 +} 1.120 + 1.121 +NS_IMETHODIMP 1.122 +nsQueryContentEventResult::GetNotFound(bool *aNotFound) 1.123 +{ 1.124 + NS_ENSURE_TRUE(mSucceeded, NS_ERROR_NOT_AVAILABLE); 1.125 + NS_ENSURE_TRUE(mEventID == NS_QUERY_SELECTED_TEXT || 1.126 + mEventID == NS_QUERY_CHARACTER_AT_POINT, 1.127 + NS_ERROR_NOT_AVAILABLE); 1.128 + *aNotFound = (mOffset == WidgetQueryContentEvent::NOT_FOUND); 1.129 + return NS_OK; 1.130 +} 1.131 + 1.132 +void 1.133 +nsQueryContentEventResult::SetEventResult(nsIWidget* aWidget, 1.134 + const WidgetQueryContentEvent &aEvent) 1.135 +{ 1.136 + mEventID = aEvent.message; 1.137 + mSucceeded = aEvent.mSucceeded; 1.138 + mReversed = aEvent.mReply.mReversed; 1.139 + mRect = aEvent.mReply.mRect; 1.140 + mOffset = aEvent.mReply.mOffset; 1.141 + mString = aEvent.mReply.mString; 1.142 + 1.143 + if (!IsRectEnabled(mEventID) || !aWidget || !mSucceeded) { 1.144 + return; 1.145 + } 1.146 + 1.147 + nsIWidget* topWidget = aWidget->GetTopLevelWidget(); 1.148 + if (!topWidget || topWidget == aWidget) { 1.149 + return; 1.150 + } 1.151 + 1.152 + // Convert the top widget related coordinates to the given widget's. 1.153 + nsIntPoint offset = 1.154 + aWidget->WidgetToScreenOffset() - topWidget->WidgetToScreenOffset(); 1.155 + mRect.MoveBy(-offset); 1.156 +}