michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 sw=2 et tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/EventDispatcher.h" michael@0: #include "mozilla/EventStateManager.h" michael@0: #include "mozilla/EventStates.h" michael@0: #include "mozilla/IMEStateManager.h" michael@0: #include "mozilla/MiscEvents.h" michael@0: #include "mozilla/MathAlgorithms.h" michael@0: #include "mozilla/MouseEvents.h" michael@0: #include "mozilla/TextComposition.h" michael@0: #include "mozilla/TextEvents.h" michael@0: #include "mozilla/TouchEvents.h" michael@0: #include "mozilla/dom/Event.h" michael@0: #include "mozilla/dom/TabParent.h" michael@0: #include "mozilla/dom/UIEvent.h" michael@0: michael@0: #include "ContentEventHandler.h" michael@0: #include "IMEContentObserver.h" michael@0: #include "WheelHandlingHelper.h" michael@0: michael@0: #include "nsCOMPtr.h" michael@0: #include "nsFocusManager.h" michael@0: #include "nsIContent.h" michael@0: #include "nsINodeInfo.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsIFrame.h" michael@0: #include "nsIWidget.h" michael@0: #include "nsPresContext.h" michael@0: #include "nsIPresShell.h" michael@0: #include "nsGkAtoms.h" michael@0: #include "nsIFormControl.h" michael@0: #include "nsIComboboxControlFrame.h" michael@0: #include "nsIScrollableFrame.h" michael@0: #include "nsIDOMHTMLElement.h" michael@0: #include "nsIDOMXULControlElement.h" michael@0: #include "nsNameSpaceManager.h" michael@0: #include "nsIBaseWindow.h" michael@0: #include "nsISelection.h" michael@0: #include "nsITextControlElement.h" michael@0: #include "nsFrameSelection.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsPIWindowRoot.h" michael@0: #include "nsIWebNavigation.h" michael@0: #include "nsIContentViewer.h" michael@0: #include "nsFrameManager.h" michael@0: michael@0: #include "nsIDOMXULElement.h" michael@0: #include "nsIDOMKeyEvent.h" michael@0: #include "nsIObserverService.h" michael@0: #include "nsIDocShell.h" michael@0: #include "nsIMarkupDocumentViewer.h" michael@0: #include "nsIDOMWheelEvent.h" michael@0: #include "nsIDOMDragEvent.h" michael@0: #include "nsIDOMUIEvent.h" michael@0: #include "nsIMozBrowserFrame.h" michael@0: michael@0: #include "nsSubDocumentFrame.h" michael@0: #include "nsLayoutUtils.h" michael@0: #include "nsIInterfaceRequestorUtils.h" michael@0: #include "nsUnicharUtils.h" michael@0: #include "nsContentUtils.h" michael@0: michael@0: #include "imgIContainer.h" michael@0: #include "nsIProperties.h" michael@0: #include "nsISupportsPrimitives.h" michael@0: michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsITimer.h" michael@0: #include "nsFontMetrics.h" michael@0: #include "nsIDOMXULDocument.h" michael@0: #include "nsIDragService.h" michael@0: #include "nsIDragSession.h" michael@0: #include "mozilla/dom/DataTransfer.h" michael@0: #include "nsContentAreaDragDrop.h" michael@0: #ifdef MOZ_XUL michael@0: #include "nsTreeBodyFrame.h" michael@0: #endif michael@0: #include "nsIController.h" michael@0: #include "nsICommandParams.h" michael@0: #include "mozilla/Services.h" michael@0: #include "mozilla/dom/HTMLLabelElement.h" michael@0: michael@0: #include "mozilla/Preferences.h" michael@0: #include "mozilla/LookAndFeel.h" michael@0: #include "GeckoProfiler.h" michael@0: #include "Units.h" michael@0: michael@0: #ifdef XP_MACOSX michael@0: #import michael@0: #endif michael@0: michael@0: namespace mozilla { michael@0: michael@0: using namespace dom; michael@0: michael@0: //#define DEBUG_DOCSHELL_FOCUS michael@0: michael@0: #define NS_USER_INTERACTION_INTERVAL 5000 // ms michael@0: michael@0: static const LayoutDeviceIntPoint kInvalidRefPoint = LayoutDeviceIntPoint(-1,-1); michael@0: michael@0: static uint32_t gMouseOrKeyboardEventCounter = 0; michael@0: static nsITimer* gUserInteractionTimer = nullptr; michael@0: static nsITimerCallback* gUserInteractionTimerCallback = nullptr; michael@0: michael@0: static inline int32_t michael@0: RoundDown(double aDouble) michael@0: { michael@0: return (aDouble > 0) ? static_cast(floor(aDouble)) : michael@0: static_cast(ceil(aDouble)); michael@0: } michael@0: michael@0: #ifdef DEBUG_DOCSHELL_FOCUS michael@0: static void michael@0: PrintDocTree(nsIDocShellTreeItem* aParentItem, int aLevel) michael@0: { michael@0: for (int32_t i=0;iGetChildCount(&childWebshellCount); michael@0: nsCOMPtr parentAsDocShell(do_QueryInterface(aParentItem)); michael@0: int32_t type = aParentItem->ItemType(); michael@0: nsCOMPtr presShell = parentAsDocShell->GetPresShell(); michael@0: nsRefPtr presContext; michael@0: parentAsDocShell->GetPresContext(getter_AddRefs(presContext)); michael@0: nsCOMPtr cv; michael@0: parentAsDocShell->GetContentViewer(getter_AddRefs(cv)); michael@0: nsCOMPtr domDoc; michael@0: if (cv) michael@0: cv->GetDOMDocument(getter_AddRefs(domDoc)); michael@0: nsCOMPtr doc = do_QueryInterface(domDoc); michael@0: nsCOMPtr domwin = doc ? doc->GetWindow() : nullptr; michael@0: nsIURI* uri = doc ? doc->GetDocumentURI() : nullptr; michael@0: michael@0: printf("DS %p Type %s Cnt %d Doc %p DW %p EM %p%c", michael@0: static_cast(parentAsDocShell.get()), michael@0: type==nsIDocShellTreeItem::typeChrome?"Chrome":"Content", michael@0: childWebshellCount, static_cast(doc.get()), michael@0: static_cast(domwin.get()), michael@0: static_cast(presContext ? presContext->EventStateManager() : nullptr), michael@0: uri ? ' ' : '\n'); michael@0: if (uri) { michael@0: nsAutoCString spec; michael@0: uri->GetSpec(spec); michael@0: printf("\"%s\"\n", spec.get()); michael@0: } michael@0: michael@0: if (childWebshellCount > 0) { michael@0: for (int32_t i = 0; i < childWebshellCount; i++) { michael@0: nsCOMPtr child; michael@0: aParentItem->GetChildAt(i, getter_AddRefs(child)); michael@0: PrintDocTree(child, aLevel + 1); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void michael@0: PrintDocTreeAll(nsIDocShellTreeItem* aItem) michael@0: { michael@0: nsCOMPtr item = aItem; michael@0: for(;;) { michael@0: nsCOMPtr parent; michael@0: item->GetParent(getter_AddRefs(parent)); michael@0: if (!parent) michael@0: break; michael@0: item = parent; michael@0: } michael@0: michael@0: PrintDocTree(item, 0); michael@0: } michael@0: #endif michael@0: michael@0: // mask values for ui.key.chromeAccess and ui.key.contentAccess michael@0: #define NS_MODIFIER_SHIFT 1 michael@0: #define NS_MODIFIER_CONTROL 2 michael@0: #define NS_MODIFIER_ALT 4 michael@0: #define NS_MODIFIER_META 8 michael@0: #define NS_MODIFIER_OS 16 michael@0: michael@0: static nsIDocument * michael@0: GetDocumentFromWindow(nsIDOMWindow *aWindow) michael@0: { michael@0: nsCOMPtr win = do_QueryInterface(aWindow); michael@0: return win ? win->GetExtantDoc() : nullptr; michael@0: } michael@0: michael@0: /******************************************************************/ michael@0: /* mozilla::UITimerCallback */ michael@0: /******************************************************************/ michael@0: michael@0: class UITimerCallback MOZ_FINAL : public nsITimerCallback michael@0: { michael@0: public: michael@0: UITimerCallback() : mPreviousCount(0) {} michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSITIMERCALLBACK michael@0: private: michael@0: uint32_t mPreviousCount; michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(UITimerCallback, nsITimerCallback) michael@0: michael@0: // If aTimer is nullptr, this method always sends "user-interaction-inactive" michael@0: // notification. michael@0: NS_IMETHODIMP michael@0: UITimerCallback::Notify(nsITimer* aTimer) michael@0: { michael@0: nsCOMPtr obs = michael@0: mozilla::services::GetObserverService(); michael@0: if (!obs) michael@0: return NS_ERROR_FAILURE; michael@0: if ((gMouseOrKeyboardEventCounter == mPreviousCount) || !aTimer) { michael@0: gMouseOrKeyboardEventCounter = 0; michael@0: obs->NotifyObservers(nullptr, "user-interaction-inactive", nullptr); michael@0: if (gUserInteractionTimer) { michael@0: gUserInteractionTimer->Cancel(); michael@0: NS_RELEASE(gUserInteractionTimer); michael@0: } michael@0: } else { michael@0: obs->NotifyObservers(nullptr, "user-interaction-active", nullptr); michael@0: EventStateManager::UpdateUserActivityTimer(); michael@0: } michael@0: mPreviousCount = gMouseOrKeyboardEventCounter; michael@0: return NS_OK; michael@0: } michael@0: michael@0: /******************************************************************/ michael@0: /* mozilla::OverOutElementsWrapper */ michael@0: /******************************************************************/ michael@0: michael@0: OverOutElementsWrapper::OverOutElementsWrapper() michael@0: : mLastOverFrame(nullptr) michael@0: { michael@0: } michael@0: michael@0: OverOutElementsWrapper::~OverOutElementsWrapper() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION(OverOutElementsWrapper, michael@0: mLastOverElement, michael@0: mFirstOverEventElement, michael@0: mFirstOutEventElement) michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(OverOutElementsWrapper) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(OverOutElementsWrapper) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(OverOutElementsWrapper) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: /******************************************************************/ michael@0: /* mozilla::EventStateManager */ michael@0: /******************************************************************/ michael@0: michael@0: static uint32_t sESMInstanceCount = 0; michael@0: michael@0: int32_t EventStateManager::sUserInputEventDepth = 0; michael@0: bool EventStateManager::sNormalLMouseEventInProcess = false; michael@0: EventStateManager* EventStateManager::sActiveESM = nullptr; michael@0: nsIDocument* EventStateManager::sMouseOverDocument = nullptr; michael@0: nsWeakFrame EventStateManager::sLastDragOverFrame = nullptr; michael@0: LayoutDeviceIntPoint EventStateManager::sLastRefPoint = kInvalidRefPoint; michael@0: nsIntPoint EventStateManager::sLastScreenPoint = nsIntPoint(0, 0); michael@0: LayoutDeviceIntPoint EventStateManager::sSynthCenteringPoint = kInvalidRefPoint; michael@0: CSSIntPoint EventStateManager::sLastClientPoint = CSSIntPoint(0, 0); michael@0: bool EventStateManager::sIsPointerLocked = false; michael@0: // Reference to the pointer locked element. michael@0: nsWeakPtr EventStateManager::sPointerLockedElement; michael@0: // Reference to the document which requested pointer lock. michael@0: nsWeakPtr EventStateManager::sPointerLockedDoc; michael@0: nsCOMPtr EventStateManager::sDragOverContent = nullptr; michael@0: TimeStamp EventStateManager::sHandlingInputStart; michael@0: michael@0: EventStateManager::WheelPrefs* michael@0: EventStateManager::WheelPrefs::sInstance = nullptr; michael@0: EventStateManager::DeltaAccumulator* michael@0: EventStateManager::DeltaAccumulator::sInstance = nullptr; michael@0: michael@0: EventStateManager::EventStateManager() michael@0: : mLockCursor(0) michael@0: , mPreLockPoint(0,0) michael@0: , mCurrentTarget(nullptr) michael@0: // init d&d gesture state machine variables michael@0: , mGestureDownPoint(0,0) michael@0: , mPresContext(nullptr) michael@0: , mLClickCount(0) michael@0: , mMClickCount(0) michael@0: , mRClickCount(0) michael@0: , m_haveShutdown(false) michael@0: { michael@0: if (sESMInstanceCount == 0) { michael@0: gUserInteractionTimerCallback = new UITimerCallback(); michael@0: if (gUserInteractionTimerCallback) michael@0: NS_ADDREF(gUserInteractionTimerCallback); michael@0: UpdateUserActivityTimer(); michael@0: } michael@0: ++sESMInstanceCount; michael@0: } michael@0: michael@0: nsresult michael@0: EventStateManager::UpdateUserActivityTimer() michael@0: { michael@0: if (!gUserInteractionTimerCallback) michael@0: return NS_OK; michael@0: michael@0: if (!gUserInteractionTimer) michael@0: CallCreateInstance("@mozilla.org/timer;1", &gUserInteractionTimer); michael@0: michael@0: if (gUserInteractionTimer) { michael@0: gUserInteractionTimer->InitWithCallback(gUserInteractionTimerCallback, michael@0: NS_USER_INTERACTION_INTERVAL, michael@0: nsITimer::TYPE_ONE_SHOT); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: EventStateManager::Init() michael@0: { michael@0: nsCOMPtr observerService = michael@0: mozilla::services::GetObserverService(); michael@0: if (!observerService) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, true); michael@0: michael@0: if (sESMInstanceCount == 1) { michael@0: Prefs::Init(); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: EventStateManager::~EventStateManager() michael@0: { michael@0: ReleaseCurrentIMEContentObserver(); michael@0: michael@0: if (sActiveESM == this) { michael@0: sActiveESM = nullptr; michael@0: } michael@0: if (Prefs::ClickHoldContextMenu()) michael@0: KillClickHoldTimer(); michael@0: michael@0: if (mDocument == sMouseOverDocument) michael@0: sMouseOverDocument = nullptr; michael@0: michael@0: --sESMInstanceCount; michael@0: if(sESMInstanceCount == 0) { michael@0: WheelTransaction::Shutdown(); michael@0: if (gUserInteractionTimerCallback) { michael@0: gUserInteractionTimerCallback->Notify(nullptr); michael@0: NS_RELEASE(gUserInteractionTimerCallback); michael@0: } michael@0: if (gUserInteractionTimer) { michael@0: gUserInteractionTimer->Cancel(); michael@0: NS_RELEASE(gUserInteractionTimer); michael@0: } michael@0: Prefs::Shutdown(); michael@0: WheelPrefs::Shutdown(); michael@0: DeltaAccumulator::Shutdown(); michael@0: } michael@0: michael@0: if (sDragOverContent && sDragOverContent->OwnerDoc() == mDocument) { michael@0: sDragOverContent = nullptr; michael@0: } michael@0: michael@0: if (!m_haveShutdown) { michael@0: Shutdown(); michael@0: michael@0: // Don't remove from Observer service in Shutdown because Shutdown also michael@0: // gets called from xpcom shutdown observer. And we don't want to remove michael@0: // from the service in that case. michael@0: michael@0: nsCOMPtr observerService = michael@0: mozilla::services::GetObserverService(); michael@0: if (observerService) { michael@0: observerService->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID); michael@0: } michael@0: } michael@0: michael@0: } michael@0: michael@0: nsresult michael@0: EventStateManager::Shutdown() michael@0: { michael@0: m_haveShutdown = true; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: EventStateManager::Observe(nsISupports* aSubject, michael@0: const char* aTopic, michael@0: const char16_t *someData) michael@0: { michael@0: if (!nsCRT::strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) { michael@0: Shutdown(); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(EventStateManager) michael@0: NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver) michael@0: NS_INTERFACE_MAP_ENTRY(nsIObserver) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(EventStateManager) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(EventStateManager) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION(EventStateManager, michael@0: mCurrentTargetContent, michael@0: mGestureDownContent, michael@0: mGestureDownFrameOwner, michael@0: mLastLeftMouseDownContent, michael@0: mLastLeftMouseDownContentParent, michael@0: mLastMiddleMouseDownContent, michael@0: mLastMiddleMouseDownContentParent, michael@0: mLastRightMouseDownContent, michael@0: mLastRightMouseDownContentParent, michael@0: mActiveContent, michael@0: mHoverContent, michael@0: mURLTargetContent, michael@0: mMouseEnterLeaveHelper, michael@0: mPointersEnterLeaveHelper, michael@0: mDocument, michael@0: mIMEContentObserver, michael@0: mAccessKeys) michael@0: michael@0: void michael@0: EventStateManager::ReleaseCurrentIMEContentObserver() michael@0: { michael@0: if (mIMEContentObserver) { michael@0: mIMEContentObserver->DisconnectFromEventStateManager(); michael@0: } michael@0: mIMEContentObserver = nullptr; michael@0: } michael@0: michael@0: void michael@0: EventStateManager::OnStartToObserveContent( michael@0: IMEContentObserver* aIMEContentObserver) michael@0: { michael@0: ReleaseCurrentIMEContentObserver(); michael@0: mIMEContentObserver = aIMEContentObserver; michael@0: } michael@0: michael@0: void michael@0: EventStateManager::OnStopObservingContent( michael@0: IMEContentObserver* aIMEContentObserver) michael@0: { michael@0: aIMEContentObserver->DisconnectFromEventStateManager(); michael@0: NS_ENSURE_TRUE_VOID(mIMEContentObserver == aIMEContentObserver); michael@0: mIMEContentObserver = nullptr; michael@0: } michael@0: michael@0: nsresult michael@0: EventStateManager::PreHandleEvent(nsPresContext* aPresContext, michael@0: WidgetEvent* aEvent, michael@0: nsIFrame* aTargetFrame, michael@0: nsEventStatus* aStatus) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aStatus); michael@0: NS_ENSURE_ARG(aPresContext); michael@0: if (!aEvent) { michael@0: NS_ERROR("aEvent is null. This should never happen."); michael@0: return NS_ERROR_NULL_POINTER; michael@0: } michael@0: michael@0: mCurrentTarget = aTargetFrame; michael@0: mCurrentTargetContent = nullptr; michael@0: michael@0: // Focus events don't necessarily need a frame. michael@0: if (NS_EVENT_NEEDS_FRAME(aEvent)) { michael@0: NS_ASSERTION(mCurrentTarget, "mCurrentTarget is null. this should not happen. see bug #13007"); michael@0: if (!mCurrentTarget) return NS_ERROR_NULL_POINTER; michael@0: } michael@0: #ifdef DEBUG michael@0: if (aEvent->HasDragEventMessage() && sIsPointerLocked) { michael@0: NS_ASSERTION(sIsPointerLocked, michael@0: "sIsPointerLocked is true. Drag events should be suppressed when the pointer is locked."); michael@0: } michael@0: #endif michael@0: // Store last known screenPoint and clientPoint so pointer lock michael@0: // can use these values as constants. michael@0: WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent(); michael@0: if (aEvent->mFlags.mIsTrusted && michael@0: ((mouseEvent && mouseEvent->IsReal()) || michael@0: aEvent->eventStructType == NS_WHEEL_EVENT) && michael@0: !sIsPointerLocked) { michael@0: sLastScreenPoint = michael@0: UIEvent::CalculateScreenPoint(aPresContext, aEvent); michael@0: sLastClientPoint = michael@0: UIEvent::CalculateClientPoint(aPresContext, aEvent, nullptr); michael@0: } michael@0: michael@0: // Do not take account NS_MOUSE_ENTER/EXIT so that loading a page michael@0: // when user is not active doesn't change the state to active. michael@0: if (aEvent->mFlags.mIsTrusted && michael@0: ((mouseEvent && mouseEvent->IsReal() && michael@0: mouseEvent->message != NS_MOUSE_ENTER && michael@0: mouseEvent->message != NS_MOUSE_EXIT) || michael@0: aEvent->eventStructType == NS_WHEEL_EVENT || michael@0: aEvent->eventStructType == NS_KEY_EVENT)) { michael@0: if (gMouseOrKeyboardEventCounter == 0) { michael@0: nsCOMPtr obs = michael@0: mozilla::services::GetObserverService(); michael@0: if (obs) { michael@0: obs->NotifyObservers(nullptr, "user-interaction-active", nullptr); michael@0: UpdateUserActivityTimer(); michael@0: } michael@0: } michael@0: ++gMouseOrKeyboardEventCounter; michael@0: } michael@0: michael@0: *aStatus = nsEventStatus_eIgnore; michael@0: michael@0: WheelTransaction::OnEvent(aEvent); michael@0: michael@0: switch (aEvent->message) { michael@0: case NS_MOUSE_BUTTON_DOWN: { michael@0: switch (mouseEvent->button) { michael@0: case WidgetMouseEvent::eLeftButton: michael@0: BeginTrackingDragGesture(aPresContext, mouseEvent, aTargetFrame); michael@0: mLClickCount = mouseEvent->clickCount; michael@0: SetClickCount(aPresContext, mouseEvent, aStatus); michael@0: sNormalLMouseEventInProcess = true; michael@0: break; michael@0: case WidgetMouseEvent::eMiddleButton: michael@0: mMClickCount = mouseEvent->clickCount; michael@0: SetClickCount(aPresContext, mouseEvent, aStatus); michael@0: break; michael@0: case WidgetMouseEvent::eRightButton: michael@0: mRClickCount = mouseEvent->clickCount; michael@0: SetClickCount(aPresContext, mouseEvent, aStatus); michael@0: break; michael@0: } michael@0: break; michael@0: } michael@0: case NS_MOUSE_BUTTON_UP: { michael@0: switch (mouseEvent->button) { michael@0: case WidgetMouseEvent::eLeftButton: michael@0: if (Prefs::ClickHoldContextMenu()) { michael@0: KillClickHoldTimer(); michael@0: } michael@0: StopTrackingDragGesture(); michael@0: sNormalLMouseEventInProcess = false; michael@0: // then fall through... michael@0: case WidgetMouseEvent::eRightButton: michael@0: case WidgetMouseEvent::eMiddleButton: michael@0: SetClickCount(aPresContext, mouseEvent, aStatus); michael@0: break; michael@0: } michael@0: break; michael@0: } michael@0: case NS_POINTER_CANCEL: michael@0: { michael@0: GenerateMouseEnterExit(mouseEvent); michael@0: break; michael@0: } michael@0: case NS_MOUSE_EXIT: michael@0: // If the event is not a top-level window exit, then it's not michael@0: // really an exit --- we may have traversed widget boundaries but michael@0: // we're still in our toplevel window. michael@0: if (mouseEvent->exit != WidgetMouseEvent::eTopLevel) { michael@0: // Treat it as a synthetic move so we don't generate spurious michael@0: // "exit" or "move" events. Any necessary "out" or "over" events michael@0: // will be generated by GenerateMouseEnterExit michael@0: mouseEvent->message = NS_MOUSE_MOVE; michael@0: mouseEvent->reason = WidgetMouseEvent::eSynthesized; michael@0: // then fall through... michael@0: } else { michael@0: GenerateMouseEnterExit(mouseEvent); michael@0: //This is a window level mouse exit event and should stop here michael@0: aEvent->message = 0; michael@0: break; michael@0: } michael@0: case NS_MOUSE_MOVE: michael@0: case NS_POINTER_DOWN: michael@0: case NS_POINTER_MOVE: { michael@0: // on the Mac, GenerateDragGesture() may not return until the drag michael@0: // has completed and so |aTargetFrame| may have been deleted (moving michael@0: // a bookmark, for example). If this is the case, however, we know michael@0: // that ClearFrameRefs() has been called and it cleared out michael@0: // |mCurrentTarget|. As a result, we should pass |mCurrentTarget| michael@0: // into UpdateCursor(). michael@0: GenerateDragGesture(aPresContext, mouseEvent); michael@0: UpdateCursor(aPresContext, aEvent, mCurrentTarget, aStatus); michael@0: GenerateMouseEnterExit(mouseEvent); michael@0: // Flush pending layout changes, so that later mouse move events michael@0: // will go to the right nodes. michael@0: FlushPendingEvents(aPresContext); michael@0: break; michael@0: } michael@0: case NS_DRAGDROP_GESTURE: michael@0: if (Prefs::ClickHoldContextMenu()) { michael@0: // an external drag gesture event came in, not generated internally michael@0: // by Gecko. Make sure we get rid of the click-hold timer. michael@0: KillClickHoldTimer(); michael@0: } michael@0: break; michael@0: case NS_DRAGDROP_OVER: michael@0: // NS_DRAGDROP_DROP is fired before NS_DRAGDROP_DRAGDROP so send michael@0: // the enter/exit events before NS_DRAGDROP_DROP. michael@0: GenerateDragDropEnterExit(aPresContext, aEvent->AsDragEvent()); michael@0: break; michael@0: michael@0: case NS_KEY_PRESS: michael@0: { michael@0: WidgetKeyboardEvent* keyEvent = aEvent->AsKeyboardEvent(); michael@0: michael@0: int32_t modifierMask = 0; michael@0: if (keyEvent->IsShift()) michael@0: modifierMask |= NS_MODIFIER_SHIFT; michael@0: if (keyEvent->IsControl()) michael@0: modifierMask |= NS_MODIFIER_CONTROL; michael@0: if (keyEvent->IsAlt()) michael@0: modifierMask |= NS_MODIFIER_ALT; michael@0: if (keyEvent->IsMeta()) michael@0: modifierMask |= NS_MODIFIER_META; michael@0: if (keyEvent->IsOS()) michael@0: modifierMask |= NS_MODIFIER_OS; michael@0: michael@0: // Prevent keyboard scrolling while an accesskey modifier is in use. michael@0: if (modifierMask && michael@0: (modifierMask == Prefs::ChromeAccessModifierMask() || michael@0: modifierMask == Prefs::ContentAccessModifierMask())) { michael@0: HandleAccessKey(aPresContext, keyEvent, aStatus, nullptr, michael@0: eAccessKeyProcessingNormal, modifierMask); michael@0: } michael@0: } michael@0: // then fall through... michael@0: case NS_KEY_DOWN: michael@0: case NS_KEY_UP: michael@0: { michael@0: nsIContent* content = GetFocusedContent(); michael@0: if (content) michael@0: mCurrentTargetContent = content; michael@0: michael@0: // NOTE: Don't refer TextComposition::IsComposing() since DOM Level 3 michael@0: // Events defines that KeyboardEvent.isComposing is true when it's michael@0: // dispatched after compositionstart and compositionend. michael@0: // TextComposition::IsComposing() is false even before michael@0: // compositionend if there is no composing string. michael@0: WidgetKeyboardEvent* keyEvent = aEvent->AsKeyboardEvent(); michael@0: nsRefPtr composition = michael@0: IMEStateManager::GetTextCompositionFor(keyEvent); michael@0: keyEvent->mIsComposing = !!composition; michael@0: } michael@0: break; michael@0: case NS_WHEEL_WHEEL: michael@0: case NS_WHEEL_START: michael@0: case NS_WHEEL_STOP: michael@0: { michael@0: NS_ASSERTION(aEvent->mFlags.mIsTrusted, michael@0: "Untrusted wheel event shouldn't be here"); michael@0: michael@0: nsIContent* content = GetFocusedContent(); michael@0: if (content) { michael@0: mCurrentTargetContent = content; michael@0: } michael@0: michael@0: if (aEvent->message != NS_WHEEL_WHEEL) { michael@0: break; michael@0: } michael@0: michael@0: WidgetWheelEvent* wheelEvent = aEvent->AsWheelEvent(); michael@0: WheelPrefs::GetInstance()->ApplyUserPrefsToDelta(wheelEvent); michael@0: michael@0: // If we won't dispatch a DOM event for this event, nothing to do anymore. michael@0: if (!wheelEvent->IsAllowedToDispatchDOMEvent()) { michael@0: break; michael@0: } michael@0: michael@0: // Init lineOrPageDelta values for line scroll events for some devices michael@0: // on some platforms which might dispatch wheel events which don't have michael@0: // lineOrPageDelta values. And also, if delta values are customized by michael@0: // prefs, this recomputes them. michael@0: DeltaAccumulator::GetInstance()-> michael@0: InitLineOrPageDelta(aTargetFrame, this, wheelEvent); michael@0: } michael@0: break; michael@0: case NS_QUERY_SELECTED_TEXT: michael@0: DoQuerySelectedText(aEvent->AsQueryContentEvent()); michael@0: break; michael@0: case NS_QUERY_TEXT_CONTENT: michael@0: { michael@0: if (RemoteQueryContentEvent(aEvent)) { michael@0: break; michael@0: } michael@0: ContentEventHandler handler(mPresContext); michael@0: handler.OnQueryTextContent(aEvent->AsQueryContentEvent()); michael@0: } michael@0: break; michael@0: case NS_QUERY_CARET_RECT: michael@0: { michael@0: if (RemoteQueryContentEvent(aEvent)) { michael@0: break; michael@0: } michael@0: ContentEventHandler handler(mPresContext); michael@0: handler.OnQueryCaretRect(aEvent->AsQueryContentEvent()); michael@0: } michael@0: break; michael@0: case NS_QUERY_TEXT_RECT: michael@0: { michael@0: if (RemoteQueryContentEvent(aEvent)) { michael@0: break; michael@0: } michael@0: ContentEventHandler handler(mPresContext); michael@0: handler.OnQueryTextRect(aEvent->AsQueryContentEvent()); michael@0: } michael@0: break; michael@0: case NS_QUERY_EDITOR_RECT: michael@0: { michael@0: // XXX remote event michael@0: ContentEventHandler handler(mPresContext); michael@0: handler.OnQueryEditorRect(aEvent->AsQueryContentEvent()); michael@0: } michael@0: break; michael@0: case NS_QUERY_CONTENT_STATE: michael@0: { michael@0: // XXX remote event michael@0: ContentEventHandler handler(mPresContext); michael@0: handler.OnQueryContentState(aEvent->AsQueryContentEvent()); michael@0: } michael@0: break; michael@0: case NS_QUERY_SELECTION_AS_TRANSFERABLE: michael@0: { michael@0: // XXX remote event michael@0: ContentEventHandler handler(mPresContext); michael@0: handler.OnQuerySelectionAsTransferable(aEvent->AsQueryContentEvent()); michael@0: } michael@0: break; michael@0: case NS_QUERY_CHARACTER_AT_POINT: michael@0: { michael@0: // XXX remote event michael@0: ContentEventHandler handler(mPresContext); michael@0: handler.OnQueryCharacterAtPoint(aEvent->AsQueryContentEvent()); michael@0: } michael@0: break; michael@0: case NS_QUERY_DOM_WIDGET_HITTEST: michael@0: { michael@0: // XXX remote event michael@0: ContentEventHandler handler(mPresContext); michael@0: handler.OnQueryDOMWidgetHittest(aEvent->AsQueryContentEvent()); michael@0: } michael@0: break; michael@0: case NS_SELECTION_SET: michael@0: { michael@0: WidgetSelectionEvent* selectionEvent = aEvent->AsSelectionEvent(); michael@0: if (IsTargetCrossProcess(selectionEvent)) { michael@0: // Will not be handled locally, remote the event michael@0: if (GetCrossProcessTarget()->SendSelectionEvent(*selectionEvent)) { michael@0: selectionEvent->mSucceeded = true; michael@0: } michael@0: break; michael@0: } michael@0: ContentEventHandler handler(mPresContext); michael@0: handler.OnSelectionEvent(selectionEvent); michael@0: } michael@0: break; michael@0: case NS_CONTENT_COMMAND_CUT: michael@0: case NS_CONTENT_COMMAND_COPY: michael@0: case NS_CONTENT_COMMAND_PASTE: michael@0: case NS_CONTENT_COMMAND_DELETE: michael@0: case NS_CONTENT_COMMAND_UNDO: michael@0: case NS_CONTENT_COMMAND_REDO: michael@0: case NS_CONTENT_COMMAND_PASTE_TRANSFERABLE: michael@0: { michael@0: DoContentCommandEvent(aEvent->AsContentCommandEvent()); michael@0: } michael@0: break; michael@0: case NS_CONTENT_COMMAND_SCROLL: michael@0: { michael@0: DoContentCommandScrollEvent(aEvent->AsContentCommandEvent()); michael@0: } michael@0: break; michael@0: case NS_TEXT_TEXT: michael@0: { michael@0: WidgetTextEvent *textEvent = aEvent->AsTextEvent(); michael@0: if (IsTargetCrossProcess(textEvent)) { michael@0: // Will not be handled locally, remote the event michael@0: if (GetCrossProcessTarget()->SendTextEvent(*textEvent)) { michael@0: // Cancel local dispatching michael@0: aEvent->mFlags.mPropagationStopped = true; michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: case NS_COMPOSITION_START: michael@0: if (aEvent->mFlags.mIsTrusted) { michael@0: // If the event is trusted event, set the selected text to data of michael@0: // composition event. michael@0: WidgetCompositionEvent* compositionEvent = aEvent->AsCompositionEvent(); michael@0: WidgetQueryContentEvent selectedText(true, NS_QUERY_SELECTED_TEXT, michael@0: compositionEvent->widget); michael@0: DoQuerySelectedText(&selectedText); michael@0: NS_ASSERTION(selectedText.mSucceeded, "Failed to get selected text"); michael@0: compositionEvent->data = selectedText.mReply.mString; michael@0: } michael@0: // through to compositionend handling michael@0: case NS_COMPOSITION_UPDATE: michael@0: case NS_COMPOSITION_END: michael@0: { michael@0: WidgetCompositionEvent* compositionEvent = aEvent->AsCompositionEvent(); michael@0: if (IsTargetCrossProcess(compositionEvent)) { michael@0: // Will not be handled locally, remote the event michael@0: if (GetCrossProcessTarget()->SendCompositionEvent(*compositionEvent)) { michael@0: // Cancel local dispatching michael@0: aEvent->mFlags.mPropagationStopped = true; michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: // static michael@0: int32_t michael@0: EventStateManager::GetAccessModifierMaskFor(nsISupports* aDocShell) michael@0: { michael@0: nsCOMPtr treeItem(do_QueryInterface(aDocShell)); michael@0: if (!treeItem) michael@0: return -1; // invalid modifier michael@0: michael@0: switch (treeItem->ItemType()) { michael@0: case nsIDocShellTreeItem::typeChrome: michael@0: return Prefs::ChromeAccessModifierMask(); michael@0: michael@0: case nsIDocShellTreeItem::typeContent: michael@0: return Prefs::ContentAccessModifierMask(); michael@0: michael@0: default: michael@0: return -1; // invalid modifier michael@0: } michael@0: } michael@0: michael@0: static bool michael@0: IsAccessKeyTarget(nsIContent* aContent, nsIFrame* aFrame, nsAString& aKey) michael@0: { michael@0: // Use GetAttr because we want Unicode case=insensitive matching michael@0: // XXXbz shouldn't this be case-sensitive, per spec? michael@0: nsString contentKey; michael@0: if (!aContent->GetAttr(kNameSpaceID_None, nsGkAtoms::accesskey, contentKey) || michael@0: !contentKey.Equals(aKey, nsCaseInsensitiveStringComparator())) michael@0: return false; michael@0: michael@0: nsCOMPtr xulDoc = michael@0: do_QueryInterface(aContent->OwnerDoc()); michael@0: if (!xulDoc && !aContent->IsXUL()) michael@0: return true; michael@0: michael@0: // For XUL we do visibility checks. michael@0: if (!aFrame) michael@0: return false; michael@0: michael@0: if (aFrame->IsFocusable()) michael@0: return true; michael@0: michael@0: if (!aFrame->IsVisibleConsideringAncestors()) michael@0: return false; michael@0: michael@0: // XUL controls can be activated. michael@0: nsCOMPtr control(do_QueryInterface(aContent)); michael@0: if (control) michael@0: return true; michael@0: michael@0: if (aContent->IsHTML()) { michael@0: nsIAtom* tag = aContent->Tag(); michael@0: michael@0: // HTML area, label and legend elements are never focusable, so michael@0: // we need to check for them explicitly before giving up. michael@0: if (tag == nsGkAtoms::area || michael@0: tag == nsGkAtoms::label || michael@0: tag == nsGkAtoms::legend) michael@0: return true; michael@0: michael@0: } else if (aContent->IsXUL()) { michael@0: // XUL label elements are never focusable, so we need to check for them michael@0: // explicitly before giving up. michael@0: if (aContent->Tag() == nsGkAtoms::label) michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool michael@0: EventStateManager::ExecuteAccessKey(nsTArray& aAccessCharCodes, michael@0: bool aIsTrustedEvent) michael@0: { michael@0: int32_t count, start = -1; michael@0: nsIContent* focusedContent = GetFocusedContent(); michael@0: if (focusedContent) { michael@0: start = mAccessKeys.IndexOf(focusedContent); michael@0: if (start == -1 && focusedContent->GetBindingParent()) michael@0: start = mAccessKeys.IndexOf(focusedContent->GetBindingParent()); michael@0: } michael@0: nsIContent *content; michael@0: nsIFrame *frame; michael@0: int32_t length = mAccessKeys.Count(); michael@0: for (uint32_t i = 0; i < aAccessCharCodes.Length(); ++i) { michael@0: uint32_t ch = aAccessCharCodes[i]; michael@0: nsAutoString accessKey; michael@0: AppendUCS4ToUTF16(ch, accessKey); michael@0: for (count = 1; count <= length; ++count) { michael@0: content = mAccessKeys[(start + count) % length]; michael@0: frame = content->GetPrimaryFrame(); michael@0: if (IsAccessKeyTarget(content, frame, accessKey)) { michael@0: bool shouldActivate = Prefs::KeyCausesActivation(); michael@0: while (shouldActivate && ++count <= length) { michael@0: nsIContent *oc = mAccessKeys[(start + count) % length]; michael@0: nsIFrame *of = oc->GetPrimaryFrame(); michael@0: if (IsAccessKeyTarget(oc, of, accessKey)) michael@0: shouldActivate = false; michael@0: } michael@0: if (shouldActivate) michael@0: content->PerformAccesskey(shouldActivate, aIsTrustedEvent); michael@0: else { michael@0: nsIFocusManager* fm = nsFocusManager::GetFocusManager(); michael@0: if (fm) { michael@0: nsCOMPtr element = do_QueryInterface(content); michael@0: fm->SetFocus(element, nsIFocusManager::FLAG_BYKEY); michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool michael@0: EventStateManager::GetAccessKeyLabelPrefix(nsAString& aPrefix) michael@0: { michael@0: aPrefix.Truncate(); michael@0: nsAutoString separator, modifierText; michael@0: nsContentUtils::GetModifierSeparatorText(separator); michael@0: michael@0: nsCOMPtr container = mPresContext->GetContainerWeak(); michael@0: int32_t modifierMask = GetAccessModifierMaskFor(container); michael@0: michael@0: if (modifierMask & NS_MODIFIER_CONTROL) { michael@0: nsContentUtils::GetControlText(modifierText); michael@0: aPrefix.Append(modifierText + separator); michael@0: } michael@0: if (modifierMask & NS_MODIFIER_META) { michael@0: nsContentUtils::GetMetaText(modifierText); michael@0: aPrefix.Append(modifierText + separator); michael@0: } michael@0: if (modifierMask & NS_MODIFIER_OS) { michael@0: nsContentUtils::GetOSText(modifierText); michael@0: aPrefix.Append(modifierText + separator); michael@0: } michael@0: if (modifierMask & NS_MODIFIER_ALT) { michael@0: nsContentUtils::GetAltText(modifierText); michael@0: aPrefix.Append(modifierText + separator); michael@0: } michael@0: if (modifierMask & NS_MODIFIER_SHIFT) { michael@0: nsContentUtils::GetShiftText(modifierText); michael@0: aPrefix.Append(modifierText + separator); michael@0: } michael@0: return !aPrefix.IsEmpty(); michael@0: } michael@0: michael@0: void michael@0: EventStateManager::HandleAccessKey(nsPresContext* aPresContext, michael@0: WidgetKeyboardEvent* aEvent, michael@0: nsEventStatus* aStatus, michael@0: nsIDocShellTreeItem* aBubbledFrom, michael@0: ProcessingAccessKeyState aAccessKeyState, michael@0: int32_t aModifierMask) michael@0: { michael@0: nsCOMPtr docShell = aPresContext->GetDocShell(); michael@0: michael@0: // Alt or other accesskey modifier is down, we may need to do an accesskey michael@0: if (mAccessKeys.Count() > 0 && michael@0: aModifierMask == GetAccessModifierMaskFor(docShell)) { michael@0: // Someone registered an accesskey. Find and activate it. michael@0: nsAutoTArray accessCharCodes; michael@0: nsContentUtils::GetAccessKeyCandidates(aEvent, accessCharCodes); michael@0: if (ExecuteAccessKey(accessCharCodes, aEvent->mFlags.mIsTrusted)) { michael@0: *aStatus = nsEventStatus_eConsumeNoDefault; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: // after the local accesskey handling michael@0: if (nsEventStatus_eConsumeNoDefault != *aStatus) { michael@0: // checking all sub docshells michael@0: michael@0: if (!docShell) { michael@0: NS_WARNING("no docShellTreeNode for presContext"); michael@0: return; michael@0: } michael@0: michael@0: int32_t childCount; michael@0: docShell->GetChildCount(&childCount); michael@0: for (int32_t counter = 0; counter < childCount; counter++) { michael@0: // Not processing the child which bubbles up the handling michael@0: nsCOMPtr subShellItem; michael@0: docShell->GetChildAt(counter, getter_AddRefs(subShellItem)); michael@0: if (aAccessKeyState == eAccessKeyProcessingUp && michael@0: subShellItem == aBubbledFrom) michael@0: continue; michael@0: michael@0: nsCOMPtr subDS = do_QueryInterface(subShellItem); michael@0: if (subDS && IsShellVisible(subDS)) { michael@0: nsCOMPtr subPS = subDS->GetPresShell(); michael@0: michael@0: // Docshells need not have a presshell (eg. display:none michael@0: // iframes, docshells in transition between documents, etc). michael@0: if (!subPS) { michael@0: // Oh, well. Just move on to the next child michael@0: continue; michael@0: } michael@0: michael@0: nsPresContext *subPC = subPS->GetPresContext(); michael@0: michael@0: EventStateManager* esm = michael@0: static_cast(subPC->EventStateManager()); michael@0: michael@0: if (esm) michael@0: esm->HandleAccessKey(subPC, aEvent, aStatus, nullptr, michael@0: eAccessKeyProcessingDown, aModifierMask); michael@0: michael@0: if (nsEventStatus_eConsumeNoDefault == *aStatus) michael@0: break; michael@0: } michael@0: } michael@0: }// if end . checking all sub docshell ends here. michael@0: michael@0: // bubble up the process to the parent docshell if necessary michael@0: if (eAccessKeyProcessingDown != aAccessKeyState && nsEventStatus_eConsumeNoDefault != *aStatus) { michael@0: if (!docShell) { michael@0: NS_WARNING("no docShellTreeItem for presContext"); michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr parentShellItem; michael@0: docShell->GetParent(getter_AddRefs(parentShellItem)); michael@0: nsCOMPtr parentDS = do_QueryInterface(parentShellItem); michael@0: if (parentDS) { michael@0: nsCOMPtr parentPS = parentDS->GetPresShell(); michael@0: NS_ASSERTION(parentPS, "Our PresShell exists but the parent's does not?"); michael@0: michael@0: nsPresContext *parentPC = parentPS->GetPresContext(); michael@0: NS_ASSERTION(parentPC, "PresShell without PresContext"); michael@0: michael@0: EventStateManager* esm = michael@0: static_cast(parentPC->EventStateManager()); michael@0: michael@0: if (esm) michael@0: esm->HandleAccessKey(parentPC, aEvent, aStatus, docShell, michael@0: eAccessKeyProcessingUp, aModifierMask); michael@0: } michael@0: }// if end. bubble up process michael@0: }// end of HandleAccessKey michael@0: michael@0: bool michael@0: EventStateManager::DispatchCrossProcessEvent(WidgetEvent* aEvent, michael@0: nsFrameLoader* aFrameLoader, michael@0: nsEventStatus *aStatus) { michael@0: PBrowserParent* remoteBrowser = aFrameLoader->GetRemoteBrowser(); michael@0: TabParent* remote = static_cast(remoteBrowser); michael@0: if (!remote) { michael@0: return false; michael@0: } michael@0: michael@0: switch (aEvent->eventStructType) { michael@0: case NS_MOUSE_EVENT: { michael@0: return remote->SendRealMouseEvent(*aEvent->AsMouseEvent()); michael@0: } michael@0: case NS_KEY_EVENT: { michael@0: return remote->SendRealKeyEvent(*aEvent->AsKeyboardEvent()); michael@0: } michael@0: case NS_WHEEL_EVENT: { michael@0: return remote->SendMouseWheelEvent(*aEvent->AsWheelEvent()); michael@0: } michael@0: case NS_TOUCH_EVENT: { michael@0: // Let the child process synthesize a mouse event if needed, and michael@0: // ensure we don't synthesize one in this process. michael@0: *aStatus = nsEventStatus_eConsumeNoDefault; michael@0: return remote->SendRealTouchEvent(*aEvent->AsTouchEvent()); michael@0: } michael@0: default: { michael@0: MOZ_CRASH("Attempt to send non-whitelisted event?"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool michael@0: EventStateManager::IsRemoteTarget(nsIContent* target) { michael@0: if (!target) { michael@0: return false; michael@0: } michael@0: michael@0: // from XUL michael@0: if ((target->Tag() == nsGkAtoms::browser || michael@0: target->Tag() == nsGkAtoms::iframe) && michael@0: target->IsXUL() && michael@0: target->AttrValueIs(kNameSpaceID_None, nsGkAtoms::Remote, michael@0: nsGkAtoms::_true, eIgnoreCase)) { michael@0: return true; michael@0: } michael@0: michael@0: // michael@0: nsCOMPtr browserFrame = do_QueryInterface(target); michael@0: if (browserFrame && browserFrame->GetReallyIsBrowserOrApp()) { michael@0: return !!TabParent::GetFrom(target); michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: /*static*/ LayoutDeviceIntPoint michael@0: EventStateManager::GetChildProcessOffset(nsFrameLoader* aFrameLoader, michael@0: const WidgetEvent& aEvent) michael@0: { michael@0: // The "toplevel widget" in child processes is always at position michael@0: // 0,0. Map the event coordinates to match that. michael@0: nsIFrame* targetFrame = aFrameLoader->GetPrimaryFrameOfOwningContent(); michael@0: if (!targetFrame) { michael@0: return LayoutDeviceIntPoint(); michael@0: } michael@0: nsPresContext* presContext = targetFrame->PresContext(); michael@0: michael@0: // Find out how far we're offset from the nearest widget. michael@0: nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(&aEvent, michael@0: targetFrame); michael@0: return LayoutDeviceIntPoint::FromAppUnitsToNearest(pt, presContext->AppUnitsPerDevPixel()); michael@0: } michael@0: michael@0: bool michael@0: CrossProcessSafeEvent(const WidgetEvent& aEvent) michael@0: { michael@0: switch (aEvent.eventStructType) { michael@0: case NS_KEY_EVENT: michael@0: case NS_WHEEL_EVENT: michael@0: return true; michael@0: case NS_MOUSE_EVENT: michael@0: switch (aEvent.message) { michael@0: case NS_MOUSE_BUTTON_DOWN: michael@0: case NS_MOUSE_BUTTON_UP: michael@0: case NS_MOUSE_MOVE: michael@0: case NS_CONTEXTMENU: michael@0: return true; michael@0: default: michael@0: return false; michael@0: } michael@0: case NS_TOUCH_EVENT: michael@0: switch (aEvent.message) { michael@0: case NS_TOUCH_START: michael@0: case NS_TOUCH_MOVE: michael@0: case NS_TOUCH_END: michael@0: case NS_TOUCH_CANCEL: michael@0: return true; michael@0: default: michael@0: return false; michael@0: } michael@0: default: michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: bool michael@0: EventStateManager::HandleCrossProcessEvent(WidgetEvent* aEvent, michael@0: nsIFrame* aTargetFrame, michael@0: nsEventStatus *aStatus) { michael@0: if (*aStatus == nsEventStatus_eConsumeNoDefault || michael@0: aEvent->mFlags.mNoCrossProcessBoundaryForwarding || michael@0: !CrossProcessSafeEvent(*aEvent)) { michael@0: return false; michael@0: } michael@0: michael@0: // Collect the remote event targets we're going to forward this michael@0: // event to. michael@0: // michael@0: // NB: the elements of |targets| must be unique, for correctness. michael@0: nsAutoTArray, 1> targets; michael@0: if (aEvent->eventStructType != NS_TOUCH_EVENT || michael@0: aEvent->message == NS_TOUCH_START) { michael@0: // If this event only has one target, and it's remote, add it to michael@0: // the array. michael@0: nsIFrame* frame = GetEventTarget(); michael@0: nsIContent* target = frame ? frame->GetContent() : nullptr; michael@0: if (IsRemoteTarget(target)) { michael@0: targets.AppendElement(target); michael@0: } michael@0: } else { michael@0: // This is a touch event with possibly multiple touch points. michael@0: // Each touch point may have its own target. So iterate through michael@0: // all of them and collect the unique set of targets for event michael@0: // forwarding. michael@0: // michael@0: // This loop is similar to the one used in michael@0: // PresShell::DispatchTouchEvent(). michael@0: const nsTArray< nsRefPtr >& touches = michael@0: aEvent->AsTouchEvent()->touches; michael@0: for (uint32_t i = 0; i < touches.Length(); ++i) { michael@0: Touch* touch = touches[i]; michael@0: // NB: the |mChanged| check is an optimization, subprocesses can michael@0: // compute this for themselves. If the touch hasn't changed, we michael@0: // may be able to avoid forwarding the event entirely (which is michael@0: // not free). michael@0: if (!touch || !touch->mChanged) { michael@0: continue; michael@0: } michael@0: nsCOMPtr targetPtr = touch->mTarget; michael@0: if (!targetPtr) { michael@0: continue; michael@0: } michael@0: nsCOMPtr target = do_QueryInterface(targetPtr); michael@0: if (IsRemoteTarget(target) && !targets.Contains(target)) { michael@0: targets.AppendElement(target); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (targets.Length() == 0) { michael@0: return false; michael@0: } michael@0: michael@0: // Look up the frame loader for all the remote targets we found, and michael@0: // then dispatch the event to the remote content they represent. michael@0: bool dispatched = false; michael@0: for (uint32_t i = 0; i < targets.Length(); ++i) { michael@0: nsIContent* target = targets[i]; michael@0: nsCOMPtr loaderOwner = do_QueryInterface(target); michael@0: if (!loaderOwner) { michael@0: continue; michael@0: } michael@0: michael@0: nsRefPtr frameLoader = loaderOwner->GetFrameLoader(); michael@0: if (!frameLoader) { michael@0: continue; michael@0: } michael@0: michael@0: uint32_t eventMode; michael@0: frameLoader->GetEventMode(&eventMode); michael@0: if (eventMode == nsIFrameLoader::EVENT_MODE_DONT_FORWARD_TO_CHILD) { michael@0: continue; michael@0: } michael@0: michael@0: dispatched |= DispatchCrossProcessEvent(aEvent, frameLoader, aStatus); michael@0: } michael@0: return dispatched; michael@0: } michael@0: michael@0: // michael@0: // CreateClickHoldTimer michael@0: // michael@0: // Fire off a timer for determining if the user wants click-hold. This timer michael@0: // is a one-shot that will be cancelled when the user moves enough to fire michael@0: // a drag. michael@0: // michael@0: void michael@0: EventStateManager::CreateClickHoldTimer(nsPresContext* inPresContext, michael@0: nsIFrame* inDownFrame, michael@0: WidgetGUIEvent* inMouseDownEvent) michael@0: { michael@0: if (!inMouseDownEvent->mFlags.mIsTrusted || IsRemoteTarget(mGestureDownContent)) michael@0: return; michael@0: michael@0: // just to be anal (er, safe) michael@0: if (mClickHoldTimer) { michael@0: mClickHoldTimer->Cancel(); michael@0: mClickHoldTimer = nullptr; michael@0: } michael@0: michael@0: // if content clicked on has a popup, don't even start the timer michael@0: // since we'll end up conflicting and both will show. michael@0: if (mGestureDownContent) { michael@0: // check for the |popup| attribute michael@0: if (nsContentUtils::HasNonEmptyAttr(mGestureDownContent, kNameSpaceID_None, michael@0: nsGkAtoms::popup)) michael@0: return; michael@0: michael@0: // check for a like bookmarks michael@0: if (mGestureDownContent->Tag() == nsGkAtoms::menubutton) michael@0: return; michael@0: } michael@0: michael@0: mClickHoldTimer = do_CreateInstance("@mozilla.org/timer;1"); michael@0: if (mClickHoldTimer) { michael@0: int32_t clickHoldDelay = michael@0: Preferences::GetInt("ui.click_hold_context_menus.delay", 500); michael@0: mClickHoldTimer->InitWithFuncCallback(sClickHoldCallback, this, michael@0: clickHoldDelay, michael@0: nsITimer::TYPE_ONE_SHOT); michael@0: } michael@0: } // CreateClickHoldTimer michael@0: michael@0: michael@0: // michael@0: // KillClickHoldTimer michael@0: // michael@0: // Stop the timer that would show the context menu dead in its tracks michael@0: // michael@0: void michael@0: EventStateManager::KillClickHoldTimer() michael@0: { michael@0: if (mClickHoldTimer) { michael@0: mClickHoldTimer->Cancel(); michael@0: mClickHoldTimer = nullptr; michael@0: } michael@0: } michael@0: michael@0: michael@0: // michael@0: // sClickHoldCallback michael@0: // michael@0: // This fires after the mouse has been down for a certain length of time. michael@0: // michael@0: void michael@0: EventStateManager::sClickHoldCallback(nsITimer* aTimer, void* aESM) michael@0: { michael@0: nsRefPtr self = static_cast(aESM); michael@0: if (self) { michael@0: self->FireContextClick(); michael@0: } michael@0: michael@0: // NOTE: |aTimer| and |self->mAutoHideTimer| are invalid after calling ClosePopup(); michael@0: michael@0: } // sAutoHideCallback michael@0: michael@0: michael@0: // michael@0: // FireContextClick michael@0: // michael@0: // If we're this far, our timer has fired, which means the mouse has been down michael@0: // for a certain period of time and has not moved enough to generate a dragGesture. michael@0: // We can be certain the user wants a context-click at this stage, so generate michael@0: // a dom event and fire it in. michael@0: // michael@0: // After the event fires, check if PreventDefault() has been set on the event which michael@0: // means that someone either ate the event or put up a context menu. This is our cue michael@0: // to stop tracking the drag gesture. If we always did this, draggable items w/out michael@0: // a context menu wouldn't be draggable after a certain length of time, which is michael@0: // _not_ what we want. michael@0: // michael@0: void michael@0: EventStateManager::FireContextClick() michael@0: { michael@0: if (!mGestureDownContent || !mPresContext) { michael@0: return; michael@0: } michael@0: michael@0: #ifdef XP_MACOSX michael@0: // Hack to ensure that we don't show a context menu when the user michael@0: // let go of the mouse after a long cpu-hogging operation prevented michael@0: // us from handling any OS events. See bug 117589. michael@0: if (!CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState, kCGMouseButtonLeft)) michael@0: return; michael@0: #endif michael@0: michael@0: nsEventStatus status = nsEventStatus_eIgnore; michael@0: michael@0: // Dispatch to the DOM. We have to fake out the ESM and tell it that the michael@0: // current target frame is actually where the mouseDown occurred, otherwise it michael@0: // will use the frame the mouse is currently over which may or may not be michael@0: // the same. (Note: saari and I have decided that we don't have to reset |mCurrentTarget| michael@0: // when we're through because no one else is doing anything more with this michael@0: // event and it will get reset on the very next event to the correct frame). michael@0: mCurrentTarget = mPresContext->GetPrimaryFrameFor(mGestureDownContent); michael@0: // make sure the widget sticks around michael@0: nsCOMPtr targetWidget; michael@0: if (mCurrentTarget && (targetWidget = mCurrentTarget->GetNearestWidget())) { michael@0: NS_ASSERTION(mPresContext == mCurrentTarget->PresContext(), michael@0: "a prescontext returned a primary frame that didn't belong to it?"); michael@0: michael@0: // before dispatching, check that we're not on something that michael@0: // doesn't get a context menu michael@0: nsIAtom *tag = mGestureDownContent->Tag(); michael@0: bool allowedToDispatch = true; michael@0: michael@0: if (mGestureDownContent->IsXUL()) { michael@0: if (tag == nsGkAtoms::scrollbar || michael@0: tag == nsGkAtoms::scrollbarbutton || michael@0: tag == nsGkAtoms::button) michael@0: allowedToDispatch = false; michael@0: else if (tag == nsGkAtoms::toolbarbutton) { michael@0: // a that has the container attribute set michael@0: // will already have its own dropdown. michael@0: if (nsContentUtils::HasNonEmptyAttr(mGestureDownContent, michael@0: kNameSpaceID_None, nsGkAtoms::container)) { michael@0: allowedToDispatch = false; michael@0: } else { michael@0: // If the toolbar button has an open menu, don't attempt to open michael@0: // a second menu michael@0: if (mGestureDownContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::open, michael@0: nsGkAtoms::_true, eCaseMatters)) { michael@0: allowedToDispatch = false; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: else if (mGestureDownContent->IsHTML()) { michael@0: nsCOMPtr formCtrl(do_QueryInterface(mGestureDownContent)); michael@0: michael@0: if (formCtrl) { michael@0: allowedToDispatch = formCtrl->IsTextControl(false) || michael@0: formCtrl->GetType() == NS_FORM_INPUT_FILE; michael@0: } michael@0: else if (tag == nsGkAtoms::applet || michael@0: tag == nsGkAtoms::embed || michael@0: tag == nsGkAtoms::object) { michael@0: allowedToDispatch = false; michael@0: } michael@0: } michael@0: michael@0: if (allowedToDispatch) { michael@0: // init the event while mCurrentTarget is still good michael@0: WidgetMouseEvent event(true, NS_CONTEXTMENU, targetWidget, michael@0: WidgetMouseEvent::eReal); michael@0: event.clickCount = 1; michael@0: FillInEventFromGestureDown(&event); michael@0: michael@0: // stop selection tracking, we're in control now michael@0: if (mCurrentTarget) michael@0: { michael@0: nsRefPtr frameSel = michael@0: mCurrentTarget->GetFrameSelection(); michael@0: michael@0: if (frameSel && frameSel->GetMouseDownState()) { michael@0: // note that this can cause selection changed events to fire if we're in michael@0: // a text field, which will null out mCurrentTarget michael@0: frameSel->SetMouseDownState(false); michael@0: } michael@0: } michael@0: michael@0: nsIDocument* doc = mGestureDownContent->GetCurrentDoc(); michael@0: AutoHandlingUserInputStatePusher userInpStatePusher(true, &event, doc); michael@0: michael@0: // dispatch to DOM michael@0: EventDispatcher::Dispatch(mGestureDownContent, mPresContext, &event, michael@0: nullptr, &status); michael@0: michael@0: // We don't need to dispatch to frame handling because no frames michael@0: // watch NS_CONTEXTMENU except for nsMenuFrame and that's only for michael@0: // dismissal. That's just as well since we don't really know michael@0: // which frame to send it to. michael@0: } michael@0: } michael@0: michael@0: // now check if the event has been handled. If so, stop tracking a drag michael@0: if (status == nsEventStatus_eConsumeNoDefault) { michael@0: StopTrackingDragGesture(); michael@0: } michael@0: michael@0: KillClickHoldTimer(); michael@0: michael@0: } // FireContextClick michael@0: michael@0: michael@0: // michael@0: // BeginTrackingDragGesture michael@0: // michael@0: // Record that the mouse has gone down and that we should move to TRACKING state michael@0: // of d&d gesture tracker. michael@0: // michael@0: // We also use this to track click-hold context menus. When the mouse goes down, michael@0: // fire off a short timer. If the timer goes off and we have yet to fire the michael@0: // drag gesture (ie, the mouse hasn't moved a certain distance), then we can michael@0: // assume the user wants a click-hold, so fire a context-click event. We only michael@0: // want to cancel the drag gesture if the context-click event is handled. michael@0: // michael@0: void michael@0: EventStateManager::BeginTrackingDragGesture(nsPresContext* aPresContext, michael@0: WidgetMouseEvent* inDownEvent, michael@0: nsIFrame* inDownFrame) michael@0: { michael@0: if (!inDownEvent->widget) michael@0: return; michael@0: michael@0: // Note that |inDownEvent| could be either a mouse down event or a michael@0: // synthesized mouse move event. michael@0: mGestureDownPoint = inDownEvent->refPoint + michael@0: LayoutDeviceIntPoint::FromUntyped(inDownEvent->widget->WidgetToScreenOffset()); michael@0: michael@0: inDownFrame->GetContentForEvent(inDownEvent, michael@0: getter_AddRefs(mGestureDownContent)); michael@0: michael@0: mGestureDownFrameOwner = inDownFrame->GetContent(); michael@0: mGestureModifiers = inDownEvent->modifiers; michael@0: mGestureDownButtons = inDownEvent->buttons; michael@0: michael@0: if (Prefs::ClickHoldContextMenu()) { michael@0: // fire off a timer to track click-hold michael@0: CreateClickHoldTimer(aPresContext, inDownFrame, inDownEvent); michael@0: } michael@0: } michael@0: michael@0: michael@0: // michael@0: // StopTrackingDragGesture michael@0: // michael@0: // Record that the mouse has gone back up so that we should leave the TRACKING michael@0: // state of d&d gesture tracker and return to the START state. michael@0: // michael@0: void michael@0: EventStateManager::StopTrackingDragGesture() michael@0: { michael@0: mGestureDownContent = nullptr; michael@0: mGestureDownFrameOwner = nullptr; michael@0: } michael@0: michael@0: void michael@0: EventStateManager::FillInEventFromGestureDown(WidgetMouseEvent* aEvent) michael@0: { michael@0: NS_ASSERTION(aEvent->widget == mCurrentTarget->GetNearestWidget(), michael@0: "Incorrect widget in event"); michael@0: michael@0: // Set the coordinates in the new event to the coordinates of michael@0: // the old event, adjusted for the fact that the widget might be michael@0: // different michael@0: aEvent->refPoint = mGestureDownPoint - michael@0: LayoutDeviceIntPoint::FromUntyped(aEvent->widget->WidgetToScreenOffset()); michael@0: aEvent->modifiers = mGestureModifiers; michael@0: aEvent->buttons = mGestureDownButtons; michael@0: } michael@0: michael@0: // michael@0: // GenerateDragGesture michael@0: // michael@0: // If we're in the TRACKING state of the d&d gesture tracker, check the current position michael@0: // of the mouse in relation to the old one. If we've moved a sufficient amount from michael@0: // the mouse down, then fire off a drag gesture event. michael@0: void michael@0: EventStateManager::GenerateDragGesture(nsPresContext* aPresContext, michael@0: WidgetMouseEvent* aEvent) michael@0: { michael@0: NS_ASSERTION(aPresContext, "This shouldn't happen."); michael@0: if (IsTrackingDragGesture()) { michael@0: mCurrentTarget = mGestureDownFrameOwner->GetPrimaryFrame(); michael@0: michael@0: if (!mCurrentTarget) { michael@0: StopTrackingDragGesture(); michael@0: return; michael@0: } michael@0: michael@0: // Check if selection is tracking drag gestures, if so michael@0: // don't interfere! michael@0: if (mCurrentTarget) michael@0: { michael@0: nsRefPtr frameSel = mCurrentTarget->GetFrameSelection(); michael@0: if (frameSel && frameSel->GetMouseDownState()) { michael@0: StopTrackingDragGesture(); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: // If non-native code is capturing the mouse don't start a drag. michael@0: if (nsIPresShell::IsMouseCapturePreventingDrag()) { michael@0: StopTrackingDragGesture(); michael@0: return; michael@0: } michael@0: michael@0: static int32_t pixelThresholdX = 0; michael@0: static int32_t pixelThresholdY = 0; michael@0: michael@0: if (!pixelThresholdX) { michael@0: pixelThresholdX = michael@0: LookAndFeel::GetInt(LookAndFeel::eIntID_DragThresholdX, 0); michael@0: pixelThresholdY = michael@0: LookAndFeel::GetInt(LookAndFeel::eIntID_DragThresholdY, 0); michael@0: if (!pixelThresholdX) michael@0: pixelThresholdX = 5; michael@0: if (!pixelThresholdY) michael@0: pixelThresholdY = 5; michael@0: } michael@0: michael@0: // fire drag gesture if mouse has moved enough michael@0: LayoutDeviceIntPoint pt = aEvent->refPoint + michael@0: LayoutDeviceIntPoint::FromUntyped(aEvent->widget->WidgetToScreenOffset()); michael@0: if (DeprecatedAbs(pt.x - mGestureDownPoint.x) > pixelThresholdX || michael@0: DeprecatedAbs(pt.y - mGestureDownPoint.y) > pixelThresholdY) { michael@0: if (Prefs::ClickHoldContextMenu()) { michael@0: // stop the click-hold before we fire off the drag gesture, in case michael@0: // it takes a long time michael@0: KillClickHoldTimer(); michael@0: } michael@0: michael@0: nsCOMPtr container = aPresContext->GetContainerWeak(); michael@0: nsCOMPtr window = do_GetInterface(container); michael@0: if (!window) michael@0: return; michael@0: michael@0: nsRefPtr dataTransfer = michael@0: new DataTransfer(window, NS_DRAGDROP_START, false, -1); michael@0: michael@0: nsCOMPtr selection; michael@0: nsCOMPtr eventContent, targetContent; michael@0: mCurrentTarget->GetContentForEvent(aEvent, getter_AddRefs(eventContent)); michael@0: if (eventContent) michael@0: DetermineDragTarget(window, eventContent, dataTransfer, michael@0: getter_AddRefs(selection), getter_AddRefs(targetContent)); michael@0: michael@0: // Stop tracking the drag gesture now. This should stop us from michael@0: // reentering GenerateDragGesture inside DOM event processing. michael@0: StopTrackingDragGesture(); michael@0: michael@0: if (!targetContent) michael@0: return; michael@0: michael@0: // Use our targetContent, now that we've determined it, as the michael@0: // parent object of the DataTransfer. michael@0: dataTransfer->SetParentObject(targetContent); michael@0: michael@0: sLastDragOverFrame = nullptr; michael@0: nsCOMPtr widget = mCurrentTarget->GetNearestWidget(); michael@0: michael@0: // get the widget from the target frame michael@0: WidgetDragEvent startEvent(aEvent->mFlags.mIsTrusted, michael@0: NS_DRAGDROP_START, widget); michael@0: FillInEventFromGestureDown(&startEvent); michael@0: michael@0: WidgetDragEvent gestureEvent(aEvent->mFlags.mIsTrusted, michael@0: NS_DRAGDROP_GESTURE, widget); michael@0: FillInEventFromGestureDown(&gestureEvent); michael@0: michael@0: startEvent.dataTransfer = gestureEvent.dataTransfer = dataTransfer; michael@0: startEvent.inputSource = gestureEvent.inputSource = aEvent->inputSource; michael@0: michael@0: // Dispatch to the DOM. By setting mCurrentTarget we are faking michael@0: // out the ESM and telling it that the current target frame is michael@0: // actually where the mouseDown occurred, otherwise it will use michael@0: // the frame the mouse is currently over which may or may not be michael@0: // the same. (Note: saari and I have decided that we don't have michael@0: // to reset |mCurrentTarget| when we're through because no one michael@0: // else is doing anything more with this event and it will get michael@0: // reset on the very next event to the correct frame). michael@0: michael@0: // Hold onto old target content through the event and reset after. michael@0: nsCOMPtr targetBeforeEvent = mCurrentTargetContent; michael@0: michael@0: // Set the current target to the content for the mouse down michael@0: mCurrentTargetContent = targetContent; michael@0: michael@0: // Dispatch both the dragstart and draggesture events to the DOM. For michael@0: // elements in an editor, only fire the draggesture event so that the michael@0: // editor code can handle it but content doesn't see a dragstart. michael@0: nsEventStatus status = nsEventStatus_eIgnore; michael@0: EventDispatcher::Dispatch(targetContent, aPresContext, &startEvent, michael@0: nullptr, &status); michael@0: michael@0: WidgetDragEvent* event = &startEvent; michael@0: if (status != nsEventStatus_eConsumeNoDefault) { michael@0: status = nsEventStatus_eIgnore; michael@0: EventDispatcher::Dispatch(targetContent, aPresContext, &gestureEvent, michael@0: nullptr, &status); michael@0: event = &gestureEvent; michael@0: } michael@0: michael@0: nsCOMPtr observerService = michael@0: mozilla::services::GetObserverService(); michael@0: // Emit observer event to allow addons to modify the DataTransfer object. michael@0: if (observerService) { michael@0: observerService->NotifyObservers(dataTransfer, michael@0: "on-datatransfer-available", michael@0: nullptr); michael@0: } michael@0: michael@0: // now that the dataTransfer has been updated in the dragstart and michael@0: // draggesture events, make it read only so that the data doesn't michael@0: // change during the drag. michael@0: dataTransfer->SetReadOnly(); michael@0: michael@0: if (status != nsEventStatus_eConsumeNoDefault) { michael@0: bool dragStarted = DoDefaultDragStart(aPresContext, event, dataTransfer, michael@0: targetContent, selection); michael@0: if (dragStarted) { michael@0: sActiveESM = nullptr; michael@0: aEvent->mFlags.mPropagationStopped = true; michael@0: } michael@0: } michael@0: michael@0: // Note that frame event handling doesn't care about NS_DRAGDROP_GESTURE, michael@0: // which is just as well since we don't really know which frame to michael@0: // send it to michael@0: michael@0: // Reset mCurretTargetContent to what it was michael@0: mCurrentTargetContent = targetBeforeEvent; michael@0: } michael@0: michael@0: // Now flush all pending notifications, for better responsiveness michael@0: // while dragging. michael@0: FlushPendingEvents(aPresContext); michael@0: } michael@0: } // GenerateDragGesture michael@0: michael@0: void michael@0: EventStateManager::DetermineDragTarget(nsPIDOMWindow* aWindow, michael@0: nsIContent* aSelectionTarget, michael@0: DataTransfer* aDataTransfer, michael@0: nsISelection** aSelection, michael@0: nsIContent** aTargetNode) michael@0: { michael@0: *aTargetNode = nullptr; michael@0: michael@0: // GetDragData determines if a selection, link or image in the content michael@0: // should be dragged, and places the data associated with the drag in the michael@0: // data transfer. michael@0: // mGestureDownContent is the node where the mousedown event for the drag michael@0: // occurred, and aSelectionTarget is the node to use when a selection is used michael@0: bool canDrag; michael@0: nsCOMPtr dragDataNode; michael@0: bool wasAlt = (mGestureModifiers & MODIFIER_ALT) != 0; michael@0: nsresult rv = nsContentAreaDragDrop::GetDragData(aWindow, mGestureDownContent, michael@0: aSelectionTarget, wasAlt, michael@0: aDataTransfer, &canDrag, aSelection, michael@0: getter_AddRefs(dragDataNode)); michael@0: if (NS_FAILED(rv) || !canDrag) michael@0: return; michael@0: michael@0: // if GetDragData returned a node, use that as the node being dragged. michael@0: // Otherwise, if a selection is being dragged, use the node within the michael@0: // selection that was dragged. Otherwise, just use the mousedown target. michael@0: nsIContent* dragContent = mGestureDownContent; michael@0: if (dragDataNode) michael@0: dragContent = dragDataNode; michael@0: else if (*aSelection) michael@0: dragContent = aSelectionTarget; michael@0: michael@0: nsIContent* originalDragContent = dragContent; michael@0: michael@0: // If a selection isn't being dragged, look for an ancestor with the michael@0: // draggable property set. If one is found, use that as the target of the michael@0: // drag instead of the node that was clicked on. If a draggable node wasn't michael@0: // found, just use the clicked node. michael@0: if (!*aSelection) { michael@0: while (dragContent) { michael@0: nsCOMPtr htmlElement = do_QueryInterface(dragContent); michael@0: if (htmlElement) { michael@0: bool draggable = false; michael@0: htmlElement->GetDraggable(&draggable); michael@0: if (draggable) michael@0: break; michael@0: } michael@0: else { michael@0: nsCOMPtr xulElement = do_QueryInterface(dragContent); michael@0: if (xulElement) { michael@0: // All XUL elements are draggable, so if a XUL element is michael@0: // encountered, stop looking for draggable nodes and just use the michael@0: // original clicked node instead. michael@0: // XXXndeakin michael@0: // In the future, we will want to improve this so that XUL has a michael@0: // better way to specify whether something is draggable than just michael@0: // on/off. michael@0: dragContent = mGestureDownContent; michael@0: break; michael@0: } michael@0: // otherwise, it's not an HTML or XUL element, so just keep looking michael@0: } michael@0: dragContent = dragContent->GetParent(); michael@0: } michael@0: } michael@0: michael@0: // if no node in the hierarchy was found to drag, but the GetDragData method michael@0: // returned a node, use that returned node. Otherwise, nothing is draggable. michael@0: if (!dragContent && dragDataNode) michael@0: dragContent = dragDataNode; michael@0: michael@0: if (dragContent) { michael@0: // if an ancestor node was used instead, clear the drag data michael@0: // XXXndeakin rework this a bit. Find a way to just not call GetDragData if we don't need to. michael@0: if (dragContent != originalDragContent) michael@0: aDataTransfer->ClearAll(); michael@0: *aTargetNode = dragContent; michael@0: NS_ADDREF(*aTargetNode); michael@0: } michael@0: } michael@0: michael@0: bool michael@0: EventStateManager::DoDefaultDragStart(nsPresContext* aPresContext, michael@0: WidgetDragEvent* aDragEvent, michael@0: DataTransfer* aDataTransfer, michael@0: nsIContent* aDragTarget, michael@0: nsISelection* aSelection) michael@0: { michael@0: nsCOMPtr dragService = michael@0: do_GetService("@mozilla.org/widget/dragservice;1"); michael@0: if (!dragService) michael@0: return false; michael@0: michael@0: // Default handling for the draggesture/dragstart event. michael@0: // michael@0: // First, check if a drag session already exists. This means that the drag michael@0: // service was called directly within a draggesture handler. In this case, michael@0: // don't do anything more, as it is assumed that the handler is managing michael@0: // drag and drop manually. Make sure to return true to indicate that a drag michael@0: // began. michael@0: nsCOMPtr dragSession; michael@0: dragService->GetCurrentSession(getter_AddRefs(dragSession)); michael@0: if (dragSession) michael@0: return true; michael@0: michael@0: // No drag session is currently active, so check if a handler added michael@0: // any items to be dragged. If not, there isn't anything to drag. michael@0: uint32_t count = 0; michael@0: if (aDataTransfer) michael@0: aDataTransfer->GetMozItemCount(&count); michael@0: if (!count) michael@0: return false; michael@0: michael@0: // Get the target being dragged, which may not be the same as the michael@0: // target of the mouse event. If one wasn't set in the michael@0: // aDataTransfer during the event handler, just use the original michael@0: // target instead. michael@0: nsCOMPtr dragTarget = aDataTransfer->GetDragTarget(); michael@0: if (!dragTarget) { michael@0: dragTarget = aDragTarget; michael@0: if (!dragTarget) michael@0: return false; michael@0: } michael@0: michael@0: // check which drag effect should initially be used. If the effect was not michael@0: // set, just use all actions, otherwise Windows won't allow a drop. michael@0: uint32_t action; michael@0: aDataTransfer->GetEffectAllowedInt(&action); michael@0: if (action == nsIDragService::DRAGDROP_ACTION_UNINITIALIZED) michael@0: action = nsIDragService::DRAGDROP_ACTION_COPY | michael@0: nsIDragService::DRAGDROP_ACTION_MOVE | michael@0: nsIDragService::DRAGDROP_ACTION_LINK; michael@0: michael@0: // get any custom drag image that was set michael@0: int32_t imageX, imageY; michael@0: Element* dragImage = aDataTransfer->GetDragImage(&imageX, &imageY); michael@0: michael@0: nsCOMPtr transArray = michael@0: aDataTransfer->GetTransferables(dragTarget->AsDOMNode()); michael@0: if (!transArray) michael@0: return false; michael@0: michael@0: // XXXndeakin don't really want to create a new drag DOM event michael@0: // here, but we need something to pass to the InvokeDragSession michael@0: // methods. michael@0: nsCOMPtr domEvent; michael@0: NS_NewDOMDragEvent(getter_AddRefs(domEvent), dragTarget, michael@0: aPresContext, aDragEvent); michael@0: michael@0: nsCOMPtr domDragEvent = do_QueryInterface(domEvent); michael@0: // if creating a drag event failed, starting a drag session will michael@0: // just fail. michael@0: michael@0: // Use InvokeDragSessionWithSelection if a selection is being dragged, michael@0: // such that the image can be generated from the selected text. However, michael@0: // use InvokeDragSessionWithImage if a custom image was set or something michael@0: // other than a selection is being dragged. michael@0: if (!dragImage && aSelection) { michael@0: dragService->InvokeDragSessionWithSelection(aSelection, transArray, michael@0: action, domDragEvent, michael@0: aDataTransfer); michael@0: } michael@0: else { michael@0: // if dragging within a XUL tree and no custom drag image was michael@0: // set, the region argument to InvokeDragSessionWithImage needs michael@0: // to be set to the area encompassing the selected rows of the michael@0: // tree to ensure that the drag feedback gets clipped to those michael@0: // rows. For other content, region should be null. michael@0: nsCOMPtr region; michael@0: #ifdef MOZ_XUL michael@0: if (dragTarget && !dragImage) { michael@0: if (dragTarget->NodeInfo()->Equals(nsGkAtoms::treechildren, michael@0: kNameSpaceID_XUL)) { michael@0: nsTreeBodyFrame* treeBody = michael@0: do_QueryFrame(dragTarget->GetPrimaryFrame()); michael@0: if (treeBody) { michael@0: treeBody->GetSelectionRegion(getter_AddRefs(region)); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: dragService->InvokeDragSessionWithImage(dragTarget->AsDOMNode(), transArray, michael@0: region, action, michael@0: dragImage ? dragImage->AsDOMNode() : michael@0: nullptr, michael@0: imageX, michael@0: imageY, domDragEvent, michael@0: aDataTransfer); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: nsresult michael@0: EventStateManager::GetMarkupDocumentViewer(nsIMarkupDocumentViewer** aMv) michael@0: { michael@0: *aMv = nullptr; michael@0: michael@0: nsIFocusManager* fm = nsFocusManager::GetFocusManager(); michael@0: if(!fm) return NS_ERROR_FAILURE; michael@0: michael@0: nsCOMPtr focusedWindow; michael@0: fm->GetFocusedWindow(getter_AddRefs(focusedWindow)); michael@0: michael@0: nsCOMPtr ourWindow = do_QueryInterface(focusedWindow); michael@0: if(!ourWindow) return NS_ERROR_FAILURE; michael@0: michael@0: nsIDOMWindow *rootWindow = ourWindow->GetPrivateRoot(); michael@0: if(!rootWindow) return NS_ERROR_FAILURE; michael@0: michael@0: nsCOMPtr contentWindow; michael@0: rootWindow->GetContent(getter_AddRefs(contentWindow)); michael@0: if(!contentWindow) return NS_ERROR_FAILURE; michael@0: michael@0: nsIDocument *doc = GetDocumentFromWindow(contentWindow); michael@0: if(!doc) return NS_ERROR_FAILURE; michael@0: michael@0: nsIPresShell *presShell = doc->GetShell(); michael@0: if(!presShell) return NS_ERROR_FAILURE; michael@0: nsPresContext *presContext = presShell->GetPresContext(); michael@0: if(!presContext) return NS_ERROR_FAILURE; michael@0: michael@0: nsCOMPtr docshell(presContext->GetDocShell()); michael@0: if(!docshell) return NS_ERROR_FAILURE; michael@0: michael@0: nsCOMPtr cv; michael@0: docshell->GetContentViewer(getter_AddRefs(cv)); michael@0: if(!cv) return NS_ERROR_FAILURE; michael@0: michael@0: nsCOMPtr mv(do_QueryInterface(cv)); michael@0: if(!mv) return NS_ERROR_FAILURE; michael@0: michael@0: *aMv = mv; michael@0: NS_IF_ADDREF(*aMv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: EventStateManager::ChangeTextSize(int32_t change) michael@0: { michael@0: nsCOMPtr mv; michael@0: nsresult rv = GetMarkupDocumentViewer(getter_AddRefs(mv)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: float textzoom; michael@0: float zoomMin = ((float)Preferences::GetInt("zoom.minPercent", 50)) / 100; michael@0: float zoomMax = ((float)Preferences::GetInt("zoom.maxPercent", 300)) / 100; michael@0: mv->GetTextZoom(&textzoom); michael@0: textzoom += ((float)change) / 10; michael@0: if (textzoom < zoomMin) michael@0: textzoom = zoomMin; michael@0: else if (textzoom > zoomMax) michael@0: textzoom = zoomMax; michael@0: mv->SetTextZoom(textzoom); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: EventStateManager::ChangeFullZoom(int32_t change) michael@0: { michael@0: nsCOMPtr mv; michael@0: nsresult rv = GetMarkupDocumentViewer(getter_AddRefs(mv)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: float fullzoom; michael@0: float zoomMin = ((float)Preferences::GetInt("zoom.minPercent", 50)) / 100; michael@0: float zoomMax = ((float)Preferences::GetInt("zoom.maxPercent", 300)) / 100; michael@0: mv->GetFullZoom(&fullzoom); michael@0: fullzoom += ((float)change) / 10; michael@0: if (fullzoom < zoomMin) michael@0: fullzoom = zoomMin; michael@0: else if (fullzoom > zoomMax) michael@0: fullzoom = zoomMax; michael@0: mv->SetFullZoom(fullzoom); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: EventStateManager::DoScrollHistory(int32_t direction) michael@0: { michael@0: nsCOMPtr pcContainer(mPresContext->GetContainerWeak()); michael@0: if (pcContainer) { michael@0: nsCOMPtr webNav(do_QueryInterface(pcContainer)); michael@0: if (webNav) { michael@0: // positive direction to go back one step, nonpositive to go forward michael@0: if (direction > 0) michael@0: webNav->GoBack(); michael@0: else michael@0: webNav->GoForward(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: EventStateManager::DoScrollZoom(nsIFrame* aTargetFrame, michael@0: int32_t adjustment) michael@0: { michael@0: // Exclude form controls and content in chrome docshells. michael@0: nsIContent *content = aTargetFrame->GetContent(); michael@0: if (content && michael@0: !content->IsNodeOfType(nsINode::eHTML_FORM_CONTROL) && michael@0: !nsContentUtils::IsInChromeDocshell(content->OwnerDoc())) michael@0: { michael@0: // positive adjustment to decrease zoom, negative to increase michael@0: int32_t change = (adjustment > 0) ? -1 : 1; michael@0: michael@0: if (Preferences::GetBool("browser.zoom.full") || content->GetCurrentDoc()->IsSyntheticDocument()) { michael@0: ChangeFullZoom(change); michael@0: } else { michael@0: ChangeTextSize(change); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static nsIFrame* michael@0: GetParentFrameToScroll(nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame) michael@0: return nullptr; michael@0: michael@0: if (aFrame->StyleDisplay()->mPosition == NS_STYLE_POSITION_FIXED && michael@0: nsLayoutUtils::IsReallyFixedPos(aFrame)) michael@0: return aFrame->PresContext()->GetPresShell()->GetRootScrollFrame(); michael@0: michael@0: return aFrame->GetParent(); michael@0: } michael@0: michael@0: void michael@0: EventStateManager::DispatchLegacyMouseScrollEvents(nsIFrame* aTargetFrame, michael@0: WidgetWheelEvent* aEvent, michael@0: nsEventStatus* aStatus) michael@0: { michael@0: MOZ_ASSERT(aEvent); michael@0: MOZ_ASSERT(aStatus); michael@0: michael@0: if (!aTargetFrame || *aStatus == nsEventStatus_eConsumeNoDefault) { michael@0: return; michael@0: } michael@0: michael@0: // Ignore mouse wheel transaction for computing legacy mouse wheel michael@0: // events' delta value. michael@0: nsIScrollableFrame* scrollTarget = michael@0: ComputeScrollTarget(aTargetFrame, aEvent, michael@0: COMPUTE_LEGACY_MOUSE_SCROLL_EVENT_TARGET); michael@0: michael@0: nsIFrame* scrollFrame = do_QueryFrame(scrollTarget); michael@0: nsPresContext* pc = michael@0: scrollFrame ? scrollFrame->PresContext() : aTargetFrame->PresContext(); michael@0: michael@0: // DOM event's delta vales are computed from CSS pixels. michael@0: nsSize scrollAmount = GetScrollAmount(pc, aEvent, scrollTarget); michael@0: nsIntSize scrollAmountInCSSPixels( michael@0: nsPresContext::AppUnitsToIntCSSPixels(scrollAmount.width), michael@0: nsPresContext::AppUnitsToIntCSSPixels(scrollAmount.height)); michael@0: michael@0: // XXX We don't deal with fractional amount in legacy event, though the michael@0: // default action handler (DoScrollText()) deals with it. michael@0: // If we implemented such strict computation, we would need additional michael@0: // accumulated delta values. It would made the code more complicated. michael@0: // And also it would computes different delta values from older version. michael@0: // It doesn't make sense to implement such code for legacy events and michael@0: // rare cases. michael@0: int32_t scrollDeltaX, scrollDeltaY, pixelDeltaX, pixelDeltaY; michael@0: switch (aEvent->deltaMode) { michael@0: case nsIDOMWheelEvent::DOM_DELTA_PAGE: michael@0: scrollDeltaX = michael@0: !aEvent->lineOrPageDeltaX ? 0 : michael@0: (aEvent->lineOrPageDeltaX > 0 ? nsIDOMUIEvent::SCROLL_PAGE_DOWN : michael@0: nsIDOMUIEvent::SCROLL_PAGE_UP); michael@0: scrollDeltaY = michael@0: !aEvent->lineOrPageDeltaY ? 0 : michael@0: (aEvent->lineOrPageDeltaY > 0 ? nsIDOMUIEvent::SCROLL_PAGE_DOWN : michael@0: nsIDOMUIEvent::SCROLL_PAGE_UP); michael@0: pixelDeltaX = RoundDown(aEvent->deltaX * scrollAmountInCSSPixels.width); michael@0: pixelDeltaY = RoundDown(aEvent->deltaY * scrollAmountInCSSPixels.height); michael@0: break; michael@0: michael@0: case nsIDOMWheelEvent::DOM_DELTA_LINE: michael@0: scrollDeltaX = aEvent->lineOrPageDeltaX; michael@0: scrollDeltaY = aEvent->lineOrPageDeltaY; michael@0: pixelDeltaX = RoundDown(aEvent->deltaX * scrollAmountInCSSPixels.width); michael@0: pixelDeltaY = RoundDown(aEvent->deltaY * scrollAmountInCSSPixels.height); michael@0: break; michael@0: michael@0: case nsIDOMWheelEvent::DOM_DELTA_PIXEL: michael@0: scrollDeltaX = aEvent->lineOrPageDeltaX; michael@0: scrollDeltaY = aEvent->lineOrPageDeltaY; michael@0: pixelDeltaX = RoundDown(aEvent->deltaX); michael@0: pixelDeltaY = RoundDown(aEvent->deltaY); michael@0: break; michael@0: michael@0: default: michael@0: MOZ_CRASH("Invalid deltaMode value comes"); michael@0: } michael@0: michael@0: // Send the legacy events in following order: michael@0: // 1. Vertical scroll michael@0: // 2. Vertical pixel scroll (even if #1 isn't consumed) michael@0: // 3. Horizontal scroll (even if #1 and/or #2 are consumed) michael@0: // 4. Horizontal pixel scroll (even if #3 isn't consumed) michael@0: michael@0: nsWeakFrame targetFrame(aTargetFrame); michael@0: michael@0: MOZ_ASSERT(*aStatus != nsEventStatus_eConsumeNoDefault && michael@0: !aEvent->mFlags.mDefaultPrevented, michael@0: "If you make legacy events dispatched for default prevented wheel " michael@0: "event, you need to initialize stateX and stateY"); michael@0: EventState stateX, stateY; michael@0: if (scrollDeltaY) { michael@0: SendLineScrollEvent(aTargetFrame, aEvent, stateY, michael@0: scrollDeltaY, DELTA_DIRECTION_Y); michael@0: if (!targetFrame.IsAlive()) { michael@0: *aStatus = nsEventStatus_eConsumeNoDefault; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: if (pixelDeltaY) { michael@0: SendPixelScrollEvent(aTargetFrame, aEvent, stateY, michael@0: pixelDeltaY, DELTA_DIRECTION_Y); michael@0: if (!targetFrame.IsAlive()) { michael@0: *aStatus = nsEventStatus_eConsumeNoDefault; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: if (scrollDeltaX) { michael@0: SendLineScrollEvent(aTargetFrame, aEvent, stateX, michael@0: scrollDeltaX, DELTA_DIRECTION_X); michael@0: if (!targetFrame.IsAlive()) { michael@0: *aStatus = nsEventStatus_eConsumeNoDefault; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: if (pixelDeltaX) { michael@0: SendPixelScrollEvent(aTargetFrame, aEvent, stateX, michael@0: pixelDeltaX, DELTA_DIRECTION_X); michael@0: if (!targetFrame.IsAlive()) { michael@0: *aStatus = nsEventStatus_eConsumeNoDefault; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: if (stateY.mDefaultPrevented || stateX.mDefaultPrevented) { michael@0: *aStatus = nsEventStatus_eConsumeNoDefault; michael@0: aEvent->mFlags.mDefaultPrevented = true; michael@0: aEvent->mFlags.mDefaultPreventedByContent |= michael@0: stateY.mDefaultPreventedByContent || stateX.mDefaultPreventedByContent; michael@0: } michael@0: } michael@0: michael@0: void michael@0: EventStateManager::SendLineScrollEvent(nsIFrame* aTargetFrame, michael@0: WidgetWheelEvent* aEvent, michael@0: EventState& aState, michael@0: int32_t aDelta, michael@0: DeltaDirection aDeltaDirection) michael@0: { michael@0: nsCOMPtr targetContent = aTargetFrame->GetContent(); michael@0: if (!targetContent) michael@0: targetContent = GetFocusedContent(); michael@0: if (!targetContent) michael@0: return; michael@0: michael@0: while (targetContent->IsNodeOfType(nsINode::eTEXT)) { michael@0: targetContent = targetContent->GetParent(); michael@0: } michael@0: michael@0: WidgetMouseScrollEvent event(aEvent->mFlags.mIsTrusted, NS_MOUSE_SCROLL, michael@0: aEvent->widget); michael@0: event.mFlags.mDefaultPrevented = aState.mDefaultPrevented; michael@0: event.mFlags.mDefaultPreventedByContent = aState.mDefaultPreventedByContent; michael@0: event.refPoint = aEvent->refPoint; michael@0: event.widget = aEvent->widget; michael@0: event.time = aEvent->time; michael@0: event.modifiers = aEvent->modifiers; michael@0: event.buttons = aEvent->buttons; michael@0: event.isHorizontal = (aDeltaDirection == DELTA_DIRECTION_X); michael@0: event.delta = aDelta; michael@0: event.inputSource = aEvent->inputSource; michael@0: michael@0: nsEventStatus status = nsEventStatus_eIgnore; michael@0: EventDispatcher::Dispatch(targetContent, aTargetFrame->PresContext(), michael@0: &event, nullptr, &status); michael@0: aState.mDefaultPrevented = michael@0: event.mFlags.mDefaultPrevented || status == nsEventStatus_eConsumeNoDefault; michael@0: aState.mDefaultPreventedByContent = event.mFlags.mDefaultPreventedByContent; michael@0: } michael@0: michael@0: void michael@0: EventStateManager::SendPixelScrollEvent(nsIFrame* aTargetFrame, michael@0: WidgetWheelEvent* aEvent, michael@0: EventState& aState, michael@0: int32_t aPixelDelta, michael@0: DeltaDirection aDeltaDirection) michael@0: { michael@0: nsCOMPtr targetContent = aTargetFrame->GetContent(); michael@0: if (!targetContent) { michael@0: targetContent = GetFocusedContent(); michael@0: if (!targetContent) michael@0: return; michael@0: } michael@0: michael@0: while (targetContent->IsNodeOfType(nsINode::eTEXT)) { michael@0: targetContent = targetContent->GetParent(); michael@0: } michael@0: michael@0: WidgetMouseScrollEvent event(aEvent->mFlags.mIsTrusted, NS_MOUSE_PIXEL_SCROLL, michael@0: aEvent->widget); michael@0: event.mFlags.mDefaultPrevented = aState.mDefaultPrevented; michael@0: event.mFlags.mDefaultPreventedByContent = aState.mDefaultPreventedByContent; michael@0: event.refPoint = aEvent->refPoint; michael@0: event.widget = aEvent->widget; michael@0: event.time = aEvent->time; michael@0: event.modifiers = aEvent->modifiers; michael@0: event.buttons = aEvent->buttons; michael@0: event.isHorizontal = (aDeltaDirection == DELTA_DIRECTION_X); michael@0: event.delta = aPixelDelta; michael@0: event.inputSource = aEvent->inputSource; michael@0: michael@0: nsEventStatus status = nsEventStatus_eIgnore; michael@0: EventDispatcher::Dispatch(targetContent, aTargetFrame->PresContext(), michael@0: &event, nullptr, &status); michael@0: aState.mDefaultPrevented = michael@0: event.mFlags.mDefaultPrevented || status == nsEventStatus_eConsumeNoDefault; michael@0: aState.mDefaultPreventedByContent = event.mFlags.mDefaultPreventedByContent; michael@0: } michael@0: michael@0: nsIScrollableFrame* michael@0: EventStateManager::ComputeScrollTarget(nsIFrame* aTargetFrame, michael@0: WidgetWheelEvent* aEvent, michael@0: ComputeScrollTargetOptions aOptions) michael@0: { michael@0: return ComputeScrollTarget(aTargetFrame, aEvent->deltaX, aEvent->deltaY, michael@0: aEvent, aOptions); michael@0: } michael@0: michael@0: // Overload ComputeScrollTarget method to allow passing "test" dx and dy when looking michael@0: // for which scrollbarowners to activate when two finger down on trackpad michael@0: // and before any actual motion michael@0: nsIScrollableFrame* michael@0: EventStateManager::ComputeScrollTarget(nsIFrame* aTargetFrame, michael@0: double aDirectionX, michael@0: double aDirectionY, michael@0: WidgetWheelEvent* aEvent, michael@0: ComputeScrollTargetOptions aOptions) michael@0: { michael@0: if (aOptions & PREFER_MOUSE_WHEEL_TRANSACTION) { michael@0: // If the user recently scrolled with the mousewheel, then they probably michael@0: // want to scroll the same view as before instead of the view under the michael@0: // cursor. WheelTransaction tracks the frame currently being michael@0: // scrolled with the mousewheel. We consider the transaction ended when the michael@0: // mouse moves more than "mousewheel.transaction.ignoremovedelay" michael@0: // milliseconds after the last scroll operation, or any time the mouse moves michael@0: // out of the frame, or when more than "mousewheel.transaction.timeout" michael@0: // milliseconds have passed after the last operation, even if the mouse michael@0: // hasn't moved. michael@0: nsIFrame* lastScrollFrame = WheelTransaction::GetTargetFrame(); michael@0: if (lastScrollFrame) { michael@0: nsIScrollableFrame* frameToScroll = michael@0: lastScrollFrame->GetScrollTargetFrame(); michael@0: if (frameToScroll) { michael@0: return frameToScroll; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // If the event doesn't cause scroll actually, we cannot find scroll target michael@0: // because we check if the event can cause scroll actually on each found michael@0: // scrollable frame. michael@0: if (!aDirectionX && !aDirectionY) { michael@0: return nullptr; michael@0: } michael@0: michael@0: bool checkIfScrollableX = michael@0: aDirectionX && (aOptions & PREFER_ACTUAL_SCROLLABLE_TARGET_ALONG_X_AXIS); michael@0: bool checkIfScrollableY = michael@0: aDirectionY && (aOptions & PREFER_ACTUAL_SCROLLABLE_TARGET_ALONG_Y_AXIS); michael@0: michael@0: nsIScrollableFrame* frameToScroll = nullptr; michael@0: nsIFrame* scrollFrame = michael@0: !(aOptions & START_FROM_PARENT) ? aTargetFrame : michael@0: GetParentFrameToScroll(aTargetFrame); michael@0: for (; scrollFrame; scrollFrame = GetParentFrameToScroll(scrollFrame)) { michael@0: // Check whether the frame wants to provide us with a scrollable view. michael@0: frameToScroll = scrollFrame->GetScrollTargetFrame(); michael@0: if (!frameToScroll) { michael@0: continue; michael@0: } michael@0: michael@0: // Don't scroll vertically by mouse-wheel on a single-line text control. michael@0: if (checkIfScrollableY) { michael@0: nsIContent* c = scrollFrame->GetContent(); michael@0: nsCOMPtr ctrl = michael@0: do_QueryInterface(c->IsInAnonymousSubtree() ? c->GetBindingParent() : c); michael@0: if (ctrl && ctrl->IsSingleLineTextControl()) { michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: if (!checkIfScrollableX && !checkIfScrollableY) { michael@0: return frameToScroll; michael@0: } michael@0: michael@0: ScrollbarStyles ss = frameToScroll->GetScrollbarStyles(); michael@0: bool hiddenForV = (NS_STYLE_OVERFLOW_HIDDEN == ss.mVertical); michael@0: bool hiddenForH = (NS_STYLE_OVERFLOW_HIDDEN == ss.mHorizontal); michael@0: if ((hiddenForV && hiddenForH) || michael@0: (checkIfScrollableY && !checkIfScrollableX && hiddenForV) || michael@0: (checkIfScrollableX && !checkIfScrollableY && hiddenForH)) { michael@0: continue; michael@0: } michael@0: michael@0: // For default action, we should climb up the tree if cannot scroll it michael@0: // by the event actually. michael@0: bool canScroll = michael@0: WheelHandlingUtils::CanScrollOn(frameToScroll, aDirectionX, aDirectionY); michael@0: // Comboboxes need special care. michael@0: nsIComboboxControlFrame* comboBox = do_QueryFrame(scrollFrame); michael@0: if (comboBox) { michael@0: if (comboBox->IsDroppedDown()) { michael@0: // Don't propagate to parent when drop down menu is active. michael@0: return canScroll ? frameToScroll : nullptr; michael@0: } michael@0: // Always propagate when not dropped down (even if focused). michael@0: continue; michael@0: } michael@0: michael@0: if (canScroll) { michael@0: return frameToScroll; michael@0: } michael@0: } michael@0: michael@0: nsIFrame* newFrame = nsLayoutUtils::GetCrossDocParentFrame( michael@0: aTargetFrame->PresContext()->FrameManager()->GetRootFrame()); michael@0: aOptions = michael@0: static_cast(aOptions & ~START_FROM_PARENT); michael@0: return newFrame ? ComputeScrollTarget(newFrame, aEvent, aOptions) : nullptr; michael@0: } michael@0: michael@0: nsSize michael@0: EventStateManager::GetScrollAmount(nsPresContext* aPresContext, michael@0: WidgetWheelEvent* aEvent, michael@0: nsIScrollableFrame* aScrollableFrame) michael@0: { michael@0: MOZ_ASSERT(aPresContext); michael@0: MOZ_ASSERT(aEvent); michael@0: michael@0: bool isPage = (aEvent->deltaMode == nsIDOMWheelEvent::DOM_DELTA_PAGE); michael@0: if (aScrollableFrame) { michael@0: return isPage ? aScrollableFrame->GetPageScrollAmount() : michael@0: aScrollableFrame->GetLineScrollAmount(); michael@0: } michael@0: michael@0: // If there is no scrollable frame and page scrolling, use view port size. michael@0: if (isPage) { michael@0: return aPresContext->GetVisibleArea().Size(); michael@0: } michael@0: michael@0: // If there is no scrollable frame, we should use root frame's information. michael@0: nsIFrame* rootFrame = aPresContext->PresShell()->GetRootFrame(); michael@0: if (!rootFrame) { michael@0: return nsSize(0, 0); michael@0: } michael@0: nsRefPtr fm; michael@0: nsLayoutUtils::GetFontMetricsForFrame(rootFrame, getter_AddRefs(fm), michael@0: nsLayoutUtils::FontSizeInflationFor(rootFrame)); michael@0: NS_ENSURE_TRUE(fm, nsSize(0, 0)); michael@0: return nsSize(fm->AveCharWidth(), fm->MaxHeight()); michael@0: } michael@0: michael@0: void michael@0: EventStateManager::DoScrollText(nsIScrollableFrame* aScrollableFrame, michael@0: WidgetWheelEvent* aEvent) michael@0: { michael@0: MOZ_ASSERT(aScrollableFrame); michael@0: MOZ_ASSERT(aEvent); michael@0: michael@0: nsIFrame* scrollFrame = do_QueryFrame(aScrollableFrame); michael@0: MOZ_ASSERT(scrollFrame); michael@0: nsWeakFrame scrollFrameWeak(scrollFrame); michael@0: michael@0: nsIFrame* lastScrollFrame = WheelTransaction::GetTargetFrame(); michael@0: if (!lastScrollFrame) { michael@0: WheelTransaction::BeginTransaction(scrollFrame, aEvent); michael@0: } else if (lastScrollFrame != scrollFrame) { michael@0: WheelTransaction::EndTransaction(); michael@0: WheelTransaction::BeginTransaction(scrollFrame, aEvent); michael@0: } else { michael@0: WheelTransaction::UpdateTransaction(aEvent); michael@0: } michael@0: michael@0: // When the scroll event will not scroll any views, UpdateTransaction michael@0: // fired MozMouseScrollFailed event which is for automated testing. michael@0: // In the event handler, the target frame might be destroyed. Then, michael@0: // we should not try scrolling anything. michael@0: if (!scrollFrameWeak.IsAlive()) { michael@0: WheelTransaction::EndTransaction(); michael@0: return; michael@0: } michael@0: michael@0: // Default action's actual scroll amount should be computed from device michael@0: // pixels. michael@0: nsPresContext* pc = scrollFrame->PresContext(); michael@0: nsSize scrollAmount = GetScrollAmount(pc, aEvent, aScrollableFrame); michael@0: nsIntSize scrollAmountInDevPixels( michael@0: pc->AppUnitsToDevPixels(scrollAmount.width), michael@0: pc->AppUnitsToDevPixels(scrollAmount.height)); michael@0: nsIntPoint actualDevPixelScrollAmount = michael@0: DeltaAccumulator::GetInstance()-> michael@0: ComputeScrollAmountForDefaultAction(aEvent, scrollAmountInDevPixels); michael@0: michael@0: // Don't scroll around the axis whose overflow style is hidden. michael@0: ScrollbarStyles overflowStyle = aScrollableFrame->GetScrollbarStyles(); michael@0: if (overflowStyle.mHorizontal == NS_STYLE_OVERFLOW_HIDDEN) { michael@0: actualDevPixelScrollAmount.x = 0; michael@0: } michael@0: if (overflowStyle.mVertical == NS_STYLE_OVERFLOW_HIDDEN) { michael@0: actualDevPixelScrollAmount.y = 0; michael@0: } michael@0: michael@0: nsIAtom* origin = nullptr; michael@0: switch (aEvent->deltaMode) { michael@0: case nsIDOMWheelEvent::DOM_DELTA_LINE: michael@0: origin = nsGkAtoms::mouseWheel; michael@0: break; michael@0: case nsIDOMWheelEvent::DOM_DELTA_PAGE: michael@0: origin = nsGkAtoms::pages; michael@0: break; michael@0: case nsIDOMWheelEvent::DOM_DELTA_PIXEL: michael@0: origin = nsGkAtoms::pixels; michael@0: break; michael@0: default: michael@0: MOZ_CRASH("Invalid deltaMode value comes"); michael@0: } michael@0: michael@0: // We shouldn't scroll more one page at once except when over one page scroll michael@0: // is allowed for the event. michael@0: nsSize pageSize = aScrollableFrame->GetPageScrollAmount(); michael@0: nsIntSize devPixelPageSize(pc->AppUnitsToDevPixels(pageSize.width), michael@0: pc->AppUnitsToDevPixels(pageSize.height)); michael@0: if (!WheelPrefs::GetInstance()->IsOverOnePageScrollAllowedX(aEvent) && michael@0: DeprecatedAbs(actualDevPixelScrollAmount.x) > devPixelPageSize.width) { michael@0: actualDevPixelScrollAmount.x = michael@0: (actualDevPixelScrollAmount.x >= 0) ? devPixelPageSize.width : michael@0: -devPixelPageSize.width; michael@0: } michael@0: michael@0: if (!WheelPrefs::GetInstance()->IsOverOnePageScrollAllowedY(aEvent) && michael@0: DeprecatedAbs(actualDevPixelScrollAmount.y) > devPixelPageSize.height) { michael@0: actualDevPixelScrollAmount.y = michael@0: (actualDevPixelScrollAmount.y >= 0) ? devPixelPageSize.height : michael@0: -devPixelPageSize.height; michael@0: } michael@0: michael@0: bool isDeltaModePixel = michael@0: (aEvent->deltaMode == nsIDOMWheelEvent::DOM_DELTA_PIXEL); michael@0: michael@0: nsIScrollableFrame::ScrollMode mode; michael@0: switch (aEvent->scrollType) { michael@0: case WidgetWheelEvent::SCROLL_DEFAULT: michael@0: if (isDeltaModePixel) { michael@0: mode = nsIScrollableFrame::NORMAL; michael@0: } else { michael@0: mode = nsIScrollableFrame::SMOOTH; michael@0: } michael@0: break; michael@0: case WidgetWheelEvent::SCROLL_SYNCHRONOUSLY: michael@0: mode = nsIScrollableFrame::INSTANT; michael@0: break; michael@0: case WidgetWheelEvent::SCROLL_ASYNCHRONOUSELY: michael@0: mode = nsIScrollableFrame::NORMAL; michael@0: break; michael@0: case WidgetWheelEvent::SCROLL_SMOOTHLY: michael@0: mode = nsIScrollableFrame::SMOOTH; michael@0: break; michael@0: default: michael@0: MOZ_CRASH("Invalid scrollType value comes"); michael@0: } michael@0: michael@0: nsIntPoint overflow; michael@0: aScrollableFrame->ScrollBy(actualDevPixelScrollAmount, michael@0: nsIScrollableFrame::DEVICE_PIXELS, michael@0: mode, &overflow, origin); michael@0: michael@0: if (!scrollFrameWeak.IsAlive()) { michael@0: // If the scroll causes changing the layout, we can think that the event michael@0: // has been completely consumed by the content. Then, users probably don't michael@0: // want additional action. michael@0: aEvent->overflowDeltaX = aEvent->overflowDeltaY = 0; michael@0: } else if (isDeltaModePixel) { michael@0: aEvent->overflowDeltaX = overflow.x; michael@0: aEvent->overflowDeltaY = overflow.y; michael@0: } else { michael@0: aEvent->overflowDeltaX = michael@0: static_cast(overflow.x) / scrollAmountInDevPixels.width; michael@0: aEvent->overflowDeltaY = michael@0: static_cast(overflow.y) / scrollAmountInDevPixels.height; michael@0: } michael@0: michael@0: // If CSS overflow properties caused not to scroll, the overflowDelta* values michael@0: // should be same as delta* values since they may be used as gesture event by michael@0: // widget. However, if there is another scrollable element in the ancestor michael@0: // along the axis, probably users don't want the operation to cause michael@0: // additional action such as moving history. In such case, overflowDelta michael@0: // values should stay zero. michael@0: if (scrollFrameWeak.IsAlive()) { michael@0: if (aEvent->deltaX && michael@0: overflowStyle.mHorizontal == NS_STYLE_OVERFLOW_HIDDEN && michael@0: !ComputeScrollTarget(scrollFrame, aEvent, michael@0: COMPUTE_SCROLLABLE_ANCESTOR_ALONG_X_AXIS)) { michael@0: aEvent->overflowDeltaX = aEvent->deltaX; michael@0: } michael@0: if (aEvent->deltaY && michael@0: overflowStyle.mVertical == NS_STYLE_OVERFLOW_HIDDEN && michael@0: !ComputeScrollTarget(scrollFrame, aEvent, michael@0: COMPUTE_SCROLLABLE_ANCESTOR_ALONG_Y_AXIS)) { michael@0: aEvent->overflowDeltaY = aEvent->deltaY; michael@0: } michael@0: } michael@0: michael@0: NS_ASSERTION(aEvent->overflowDeltaX == 0 || michael@0: (aEvent->overflowDeltaX > 0) == (aEvent->deltaX > 0), michael@0: "The sign of overflowDeltaX is different from the scroll direction"); michael@0: NS_ASSERTION(aEvent->overflowDeltaY == 0 || michael@0: (aEvent->overflowDeltaY > 0) == (aEvent->deltaY > 0), michael@0: "The sign of overflowDeltaY is different from the scroll direction"); michael@0: michael@0: WheelPrefs::GetInstance()->CancelApplyingUserPrefsFromOverflowDelta(aEvent); michael@0: } michael@0: michael@0: void michael@0: EventStateManager::DecideGestureEvent(WidgetGestureNotifyEvent* aEvent, michael@0: nsIFrame* targetFrame) michael@0: { michael@0: michael@0: NS_ASSERTION(aEvent->message == NS_GESTURENOTIFY_EVENT_START, michael@0: "DecideGestureEvent called with a non-gesture event"); michael@0: michael@0: /* Check the ancestor tree to decide if any frame is willing* to receive michael@0: * a MozPixelScroll event. If that's the case, the current touch gesture michael@0: * will be used as a pan gesture; otherwise it will be a regular michael@0: * mousedown/mousemove/click event. michael@0: * michael@0: * *willing: determine if it makes sense to pan the element using scroll events: michael@0: * - For web content: if there are any visible scrollbars on the touch point michael@0: * - For XUL: if it's an scrollable element that can currently scroll in some michael@0: * direction. michael@0: * michael@0: * Note: we'll have to one-off various cases to ensure a good usable behavior michael@0: */ michael@0: WidgetGestureNotifyEvent::ePanDirection panDirection = michael@0: WidgetGestureNotifyEvent::ePanNone; michael@0: bool displayPanFeedback = false; michael@0: for (nsIFrame* current = targetFrame; current; michael@0: current = nsLayoutUtils::GetCrossDocParentFrame(current)) { michael@0: michael@0: nsIAtom* currentFrameType = current->GetType(); michael@0: michael@0: // Scrollbars should always be draggable michael@0: if (currentFrameType == nsGkAtoms::scrollbarFrame) { michael@0: panDirection = WidgetGestureNotifyEvent::ePanNone; michael@0: break; michael@0: } michael@0: michael@0: #ifdef MOZ_XUL michael@0: // Special check for trees michael@0: nsTreeBodyFrame* treeFrame = do_QueryFrame(current); michael@0: if (treeFrame) { michael@0: if (treeFrame->GetHorizontalOverflow()) { michael@0: panDirection = WidgetGestureNotifyEvent::ePanHorizontal; michael@0: } michael@0: if (treeFrame->GetVerticalOverflow()) { michael@0: panDirection = WidgetGestureNotifyEvent::ePanVertical; michael@0: } michael@0: break; michael@0: } michael@0: #endif michael@0: michael@0: nsIScrollableFrame* scrollableFrame = do_QueryFrame(current); michael@0: if (scrollableFrame) { michael@0: if (current->IsFrameOfType(nsIFrame::eXULBox)) { michael@0: displayPanFeedback = true; michael@0: michael@0: nsRect scrollRange = scrollableFrame->GetScrollRange(); michael@0: bool canScrollHorizontally = scrollRange.width > 0; michael@0: michael@0: if (targetFrame->GetType() == nsGkAtoms::menuFrame) { michael@0: // menu frames report horizontal scroll when they have submenus michael@0: // and we don't want that michael@0: canScrollHorizontally = false; michael@0: displayPanFeedback = false; michael@0: } michael@0: michael@0: // Vertical panning has priority over horizontal panning, so michael@0: // when vertical movement is possible we can just finish the loop. michael@0: if (scrollRange.height > 0) { michael@0: panDirection = WidgetGestureNotifyEvent::ePanVertical; michael@0: break; michael@0: } michael@0: michael@0: if (canScrollHorizontally) { michael@0: panDirection = WidgetGestureNotifyEvent::ePanHorizontal; michael@0: displayPanFeedback = false; michael@0: } michael@0: } else { //Not a XUL box michael@0: uint32_t scrollbarVisibility = scrollableFrame->GetScrollbarVisibility(); michael@0: michael@0: //Check if we have visible scrollbars michael@0: if (scrollbarVisibility & nsIScrollableFrame::VERTICAL) { michael@0: panDirection = WidgetGestureNotifyEvent::ePanVertical; michael@0: displayPanFeedback = true; michael@0: break; michael@0: } michael@0: michael@0: if (scrollbarVisibility & nsIScrollableFrame::HORIZONTAL) { michael@0: panDirection = WidgetGestureNotifyEvent::ePanHorizontal; michael@0: displayPanFeedback = true; michael@0: } michael@0: } michael@0: } //scrollableFrame michael@0: } //ancestor chain michael@0: michael@0: aEvent->displayPanFeedback = displayPanFeedback; michael@0: aEvent->panDirection = panDirection; michael@0: } michael@0: michael@0: #ifdef XP_MACOSX michael@0: static bool michael@0: NodeAllowsClickThrough(nsINode* aNode) michael@0: { michael@0: while (aNode) { michael@0: if (aNode->IsElement() && aNode->AsElement()->IsXUL()) { michael@0: mozilla::dom::Element* element = aNode->AsElement(); michael@0: static nsIContent::AttrValuesArray strings[] = michael@0: {&nsGkAtoms::always, &nsGkAtoms::never, nullptr}; michael@0: switch (element->FindAttrValueIn(kNameSpaceID_None, nsGkAtoms::clickthrough, michael@0: strings, eCaseMatters)) { michael@0: case 0: michael@0: return true; michael@0: case 1: michael@0: return false; michael@0: } michael@0: } michael@0: aNode = nsContentUtils::GetCrossDocParentNode(aNode); michael@0: } michael@0: return true; michael@0: } michael@0: #endif michael@0: michael@0: nsresult michael@0: EventStateManager::PostHandleEvent(nsPresContext* aPresContext, michael@0: WidgetEvent* aEvent, michael@0: nsIFrame* aTargetFrame, michael@0: nsEventStatus* aStatus) michael@0: { michael@0: NS_ENSURE_ARG(aPresContext); michael@0: NS_ENSURE_ARG_POINTER(aStatus); michael@0: michael@0: bool dispatchedToContentProcess = HandleCrossProcessEvent(aEvent, michael@0: aTargetFrame, michael@0: aStatus); michael@0: michael@0: mCurrentTarget = aTargetFrame; michael@0: mCurrentTargetContent = nullptr; michael@0: michael@0: // Most of the events we handle below require a frame. michael@0: // Add special cases here. michael@0: if (!mCurrentTarget && aEvent->message != NS_MOUSE_BUTTON_UP && michael@0: aEvent->message != NS_MOUSE_BUTTON_DOWN) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: //Keep the prescontext alive, we might need it after event dispatch michael@0: nsRefPtr presContext = aPresContext; michael@0: nsresult ret = NS_OK; michael@0: michael@0: switch (aEvent->message) { michael@0: case NS_MOUSE_BUTTON_DOWN: michael@0: { michael@0: WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent(); michael@0: if (mouseEvent->button == WidgetMouseEvent::eLeftButton && michael@0: !sNormalLMouseEventInProcess) { michael@0: // We got a mouseup event while a mousedown event was being processed. michael@0: // Make sure that the capturing content is cleared. michael@0: nsIPresShell::SetCapturingContent(nullptr, 0); michael@0: break; michael@0: } michael@0: michael@0: nsCOMPtr activeContent; michael@0: if (nsEventStatus_eConsumeNoDefault != *aStatus) { michael@0: nsCOMPtr newFocus; michael@0: bool suppressBlur = false; michael@0: if (mCurrentTarget) { michael@0: mCurrentTarget->GetContentForEvent(aEvent, getter_AddRefs(newFocus)); michael@0: const nsStyleUserInterface* ui = mCurrentTarget->StyleUserInterface(); michael@0: activeContent = mCurrentTarget->GetContent(); michael@0: michael@0: // In some cases, we do not want to even blur the current focused michael@0: // element. Those cases are: michael@0: // 1. -moz-user-focus CSS property is set to 'ignore'; michael@0: // 2. Element with NS_EVENT_STATE_DISABLED michael@0: // (aka :disabled pseudo-class for HTML element); michael@0: // 3. XUL control element has the disabled property set to 'true'. michael@0: // michael@0: // We can't use nsIFrame::IsFocusable() because we want to blur when michael@0: // we click on a visibility: none element. michael@0: // We can't use nsIContent::IsFocusable() because we want to blur when michael@0: // we click on a non-focusable element like a
. michael@0: // We have to use |aEvent->target| to not make sure we do not check an michael@0: // anonymous node of the targeted element. michael@0: suppressBlur = (ui->mUserFocus == NS_STYLE_USER_FOCUS_IGNORE); michael@0: michael@0: if (!suppressBlur) { michael@0: nsCOMPtr element = do_QueryInterface(aEvent->target); michael@0: suppressBlur = element && michael@0: element->State().HasState(NS_EVENT_STATE_DISABLED); michael@0: } michael@0: michael@0: if (!suppressBlur) { michael@0: nsCOMPtr xulControl = michael@0: do_QueryInterface(aEvent->target); michael@0: if (xulControl) { michael@0: bool disabled; michael@0: xulControl->GetDisabled(&disabled); michael@0: suppressBlur = disabled; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (!suppressBlur) { michael@0: suppressBlur = nsContentUtils::IsUserFocusIgnored(activeContent); michael@0: } michael@0: michael@0: nsIFrame* currFrame = mCurrentTarget; michael@0: michael@0: // When a root content which isn't editable but has an editable HTML michael@0: // element is clicked, we should redirect the focus to the michael@0: // the element. E.g., when an user click bottom of the editor michael@0: // where is outside of the element, the should be focused michael@0: // and the user can edit immediately after that. michael@0: // michael@0: // NOTE: The newFocus isn't editable that also means it's not in michael@0: // designMode. In designMode, all contents are not focusable. michael@0: if (newFocus && !newFocus->IsEditable()) { michael@0: nsIDocument *doc = newFocus->GetCurrentDoc(); michael@0: if (doc && newFocus == doc->GetRootElement()) { michael@0: nsIContent *bodyContent = michael@0: nsLayoutUtils::GetEditableRootContentByContentEditable(doc); michael@0: if (bodyContent) { michael@0: nsIFrame* bodyFrame = bodyContent->GetPrimaryFrame(); michael@0: if (bodyFrame) { michael@0: currFrame = bodyFrame; michael@0: newFocus = bodyContent; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // When the mouse is pressed, the default action is to focus the michael@0: // target. Look for the nearest enclosing focusable frame. michael@0: while (currFrame) { michael@0: // If the mousedown happened inside a popup, don't michael@0: // try to set focus on one of its containing elements michael@0: const nsStyleDisplay* display = currFrame->StyleDisplay(); michael@0: if (display->mDisplay == NS_STYLE_DISPLAY_POPUP) { michael@0: newFocus = nullptr; michael@0: break; michael@0: } michael@0: michael@0: int32_t tabIndexUnused; michael@0: if (currFrame->IsFocusable(&tabIndexUnused, true)) { michael@0: newFocus = currFrame->GetContent(); michael@0: nsCOMPtr domElement(do_QueryInterface(newFocus)); michael@0: if (domElement) michael@0: break; michael@0: } michael@0: currFrame = currFrame->GetParent(); michael@0: } michael@0: michael@0: nsIFocusManager* fm = nsFocusManager::GetFocusManager(); michael@0: if (fm) { michael@0: // if something was found to focus, focus it. Otherwise, if the michael@0: // element that was clicked doesn't have -moz-user-focus: ignore, michael@0: // clear the existing focus. For -moz-user-focus: ignore, the focus michael@0: // is just left as is. michael@0: // Another effect of mouse clicking, handled in nsSelection, is that michael@0: // it should update the caret position to where the mouse was michael@0: // clicked. Because the focus is cleared when clicking on a michael@0: // non-focusable node, the next press of the tab key will cause michael@0: // focus to be shifted from the caret position instead of the root. michael@0: if (newFocus && currFrame) { michael@0: // use the mouse flag and the noscroll flag so that the content michael@0: // doesn't unexpectedly scroll when clicking an element that is michael@0: // only hald visible michael@0: nsCOMPtr newFocusElement = do_QueryInterface(newFocus); michael@0: fm->SetFocus(newFocusElement, nsIFocusManager::FLAG_BYMOUSE | michael@0: nsIFocusManager::FLAG_NOSCROLL); michael@0: } michael@0: else if (!suppressBlur) { michael@0: // clear the focus within the frame and then set it as the michael@0: // focused frame michael@0: EnsureDocument(mPresContext); michael@0: if (mDocument) { michael@0: #ifdef XP_MACOSX michael@0: if (!activeContent || !activeContent->IsXUL()) michael@0: #endif michael@0: fm->ClearFocus(mDocument->GetWindow()); michael@0: fm->SetFocusedWindow(mDocument->GetWindow()); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // The rest is left button-specific. michael@0: if (mouseEvent->button != WidgetMouseEvent::eLeftButton) { michael@0: break; michael@0: } michael@0: michael@0: if (activeContent) { michael@0: // The nearest enclosing element goes into the michael@0: // :active state. If we fail the QI to DOMElement, michael@0: // then we know we're only a node, and that we need michael@0: // to obtain our parent element and put it into :active michael@0: // instead. michael@0: nsCOMPtr elt(do_QueryInterface(activeContent)); michael@0: if (!elt) { michael@0: nsIContent* par = activeContent->GetParent(); michael@0: if (par) michael@0: activeContent = par; michael@0: } michael@0: } michael@0: } michael@0: else { michael@0: // if we're here, the event handler returned false, so stop michael@0: // any of our own processing of a drag. Workaround for bug 43258. michael@0: StopTrackingDragGesture(); michael@0: michael@0: // When the event was cancelled, there is currently a chrome document michael@0: // focused and a mousedown just occurred on a content document, ensure michael@0: // that the window that was clicked is focused. michael@0: EnsureDocument(mPresContext); michael@0: nsIFocusManager* fm = nsFocusManager::GetFocusManager(); michael@0: if (mDocument && fm) { michael@0: nsCOMPtr currentWindow; michael@0: fm->GetFocusedWindow(getter_AddRefs(currentWindow)); michael@0: if (currentWindow && mDocument->GetWindow() && michael@0: currentWindow != mDocument->GetWindow() && michael@0: !nsContentUtils::IsChromeDoc(mDocument)) { michael@0: nsCOMPtr currentTop; michael@0: nsCOMPtr newTop; michael@0: currentWindow->GetTop(getter_AddRefs(currentTop)); michael@0: mDocument->GetWindow()->GetTop(getter_AddRefs(newTop)); michael@0: nsCOMPtr win = do_QueryInterface(currentWindow); michael@0: nsCOMPtr currentDoc = win->GetExtantDoc(); michael@0: if (nsContentUtils::IsChromeDoc(currentDoc) || michael@0: (currentTop && newTop && currentTop != newTop)) { michael@0: fm->SetFocusedWindow(mDocument->GetWindow()); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: SetActiveManager(this, activeContent); michael@0: } michael@0: break; michael@0: case NS_POINTER_CANCEL: michael@0: case NS_POINTER_UP: { michael@0: WidgetPointerEvent* pointerEvent = aEvent->AsPointerEvent(); michael@0: // After UP/Cancel Touch pointers become invalid so we can remove relevant helper from Table michael@0: // Mouse/Pen pointers are valid all the time (not only between down/up) michael@0: if (pointerEvent->inputSource == nsIDOMMouseEvent::MOZ_SOURCE_TOUCH) { michael@0: mPointersEnterLeaveHelper.Remove(pointerEvent->pointerId); michael@0: } michael@0: if (pointerEvent->inputSource != nsIDOMMouseEvent::MOZ_SOURCE_MOUSE) { michael@0: GenerateMouseEnterExit(pointerEvent); michael@0: } michael@0: break; michael@0: } michael@0: case NS_MOUSE_BUTTON_UP: michael@0: { michael@0: ClearGlobalActiveContent(this); michael@0: WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent(); michael@0: if (mouseEvent && mouseEvent->IsReal()) { michael@0: if (!mCurrentTarget) { michael@0: GetEventTarget(); michael@0: } michael@0: // Make sure to dispatch the click even if there is no frame for michael@0: // the current target element. This is required for Web compatibility. michael@0: ret = CheckForAndDispatchClick(presContext, mouseEvent, aStatus); michael@0: } michael@0: michael@0: nsIPresShell *shell = presContext->GetPresShell(); michael@0: if (shell) { michael@0: nsRefPtr frameSelection = shell->FrameSelection(); michael@0: frameSelection->SetMouseDownState(false); michael@0: } michael@0: } michael@0: break; michael@0: case NS_WHEEL_STOP: michael@0: { michael@0: MOZ_ASSERT(aEvent->mFlags.mIsTrusted); michael@0: ScrollbarsForWheel::MayInactivate(); michael@0: } michael@0: break; michael@0: case NS_WHEEL_WHEEL: michael@0: case NS_WHEEL_START: michael@0: { michael@0: MOZ_ASSERT(aEvent->mFlags.mIsTrusted); michael@0: michael@0: if (*aStatus == nsEventStatus_eConsumeNoDefault) { michael@0: ScrollbarsForWheel::Inactivate(); michael@0: break; michael@0: } michael@0: michael@0: WidgetWheelEvent* wheelEvent = aEvent->AsWheelEvent(); michael@0: switch (WheelPrefs::GetInstance()->ComputeActionFor(wheelEvent)) { michael@0: case WheelPrefs::ACTION_SCROLL: { michael@0: // For scrolling of default action, we should honor the mouse wheel michael@0: // transaction. michael@0: michael@0: ScrollbarsForWheel::PrepareToScrollText(this, aTargetFrame, wheelEvent); michael@0: michael@0: if (aEvent->message != NS_WHEEL_WHEEL || michael@0: (!wheelEvent->deltaX && !wheelEvent->deltaY)) { michael@0: break; michael@0: } michael@0: michael@0: nsIScrollableFrame* scrollTarget = michael@0: ComputeScrollTarget(aTargetFrame, wheelEvent, michael@0: COMPUTE_DEFAULT_ACTION_TARGET); michael@0: michael@0: ScrollbarsForWheel::SetActiveScrollTarget(scrollTarget); michael@0: michael@0: nsIFrame* rootScrollFrame = !aTargetFrame ? nullptr : michael@0: aTargetFrame->PresContext()->PresShell()->GetRootScrollFrame(); michael@0: nsIScrollableFrame* rootScrollableFrame = nullptr; michael@0: if (rootScrollFrame) { michael@0: rootScrollableFrame = do_QueryFrame(rootScrollFrame); michael@0: } michael@0: if (!scrollTarget || scrollTarget == rootScrollableFrame) { michael@0: wheelEvent->mViewPortIsOverscrolled = true; michael@0: } michael@0: wheelEvent->overflowDeltaX = wheelEvent->deltaX; michael@0: wheelEvent->overflowDeltaY = wheelEvent->deltaY; michael@0: WheelPrefs::GetInstance()-> michael@0: CancelApplyingUserPrefsFromOverflowDelta(wheelEvent); michael@0: if (scrollTarget) { michael@0: DoScrollText(scrollTarget, wheelEvent); michael@0: } else { michael@0: WheelTransaction::EndTransaction(); michael@0: ScrollbarsForWheel::Inactivate(); michael@0: } michael@0: break; michael@0: } michael@0: case WheelPrefs::ACTION_HISTORY: { michael@0: // If this event doesn't cause NS_MOUSE_SCROLL event or the direction michael@0: // is oblique, don't perform history back/forward. michael@0: int32_t intDelta = wheelEvent->GetPreferredIntDelta(); michael@0: if (!intDelta) { michael@0: break; michael@0: } michael@0: DoScrollHistory(intDelta); michael@0: break; michael@0: } michael@0: case WheelPrefs::ACTION_ZOOM: { michael@0: // If this event doesn't cause NS_MOUSE_SCROLL event or the direction michael@0: // is oblique, don't perform zoom in/out. michael@0: int32_t intDelta = wheelEvent->GetPreferredIntDelta(); michael@0: if (!intDelta) { michael@0: break; michael@0: } michael@0: DoScrollZoom(aTargetFrame, intDelta); michael@0: break; michael@0: } michael@0: case WheelPrefs::ACTION_NONE: michael@0: default: michael@0: // If we don't handle the wheel event, all of the delta values must michael@0: // be overflown delta values. michael@0: wheelEvent->overflowDeltaX = wheelEvent->deltaX; michael@0: wheelEvent->overflowDeltaY = wheelEvent->deltaY; michael@0: WheelPrefs::GetInstance()-> michael@0: CancelApplyingUserPrefsFromOverflowDelta(wheelEvent); michael@0: break; michael@0: } michael@0: *aStatus = nsEventStatus_eConsumeNoDefault; michael@0: } michael@0: break; michael@0: michael@0: case NS_GESTURENOTIFY_EVENT_START: michael@0: { michael@0: if (nsEventStatus_eConsumeNoDefault != *aStatus) { michael@0: DecideGestureEvent(aEvent->AsGestureNotifyEvent(), mCurrentTarget); michael@0: } michael@0: } michael@0: break; michael@0: michael@0: case NS_DRAGDROP_ENTER: michael@0: case NS_DRAGDROP_OVER: michael@0: { michael@0: NS_ASSERTION(aEvent->eventStructType == NS_DRAG_EVENT, "Expected a drag event"); michael@0: michael@0: nsCOMPtr dragSession = nsContentUtils::GetDragSession(); michael@0: if (!dragSession) michael@0: break; michael@0: michael@0: // Reset the flag. michael@0: dragSession->SetOnlyChromeDrop(false); michael@0: if (mPresContext) { michael@0: EnsureDocument(mPresContext); michael@0: } michael@0: bool isChromeDoc = nsContentUtils::IsChromeDoc(mDocument); michael@0: michael@0: // the initial dataTransfer is the one from the dragstart event that michael@0: // was set on the dragSession when the drag began. michael@0: nsCOMPtr dataTransfer; michael@0: nsCOMPtr initialDataTransfer; michael@0: dragSession->GetDataTransfer(getter_AddRefs(initialDataTransfer)); michael@0: michael@0: WidgetDragEvent *dragEvent = aEvent->AsDragEvent(); michael@0: michael@0: // collect any changes to moz cursor settings stored in the event's michael@0: // data transfer. michael@0: UpdateDragDataTransfer(dragEvent); michael@0: michael@0: // cancelling a dragenter or dragover event means that a drop should be michael@0: // allowed, so update the dropEffect and the canDrop state to indicate michael@0: // that a drag is allowed. If the event isn't cancelled, a drop won't be michael@0: // allowed. Essentially, to allow a drop somewhere, specify the effects michael@0: // using the effectAllowed and dropEffect properties in a dragenter or michael@0: // dragover event and cancel the event. To not allow a drop somewhere, michael@0: // don't cancel the event or set the effectAllowed or dropEffect to michael@0: // "none". This way, if the event is just ignored, no drop will be michael@0: // allowed. michael@0: uint32_t dropEffect = nsIDragService::DRAGDROP_ACTION_NONE; michael@0: if (nsEventStatus_eConsumeNoDefault == *aStatus) { michael@0: // if the event has a dataTransfer set, use it. michael@0: if (dragEvent->dataTransfer) { michael@0: // get the dataTransfer and the dropEffect that was set on it michael@0: dataTransfer = do_QueryInterface(dragEvent->dataTransfer); michael@0: dataTransfer->GetDropEffectInt(&dropEffect); michael@0: } michael@0: else { michael@0: // if dragEvent->dataTransfer is null, it means that no attempt was michael@0: // made to access the dataTransfer during the event, yet the event michael@0: // was cancelled. Instead, use the initial data transfer available michael@0: // from the drag session. The drop effect would not have been michael@0: // initialized (which is done in DragEvent::GetDataTransfer), michael@0: // so set it from the drag action. We'll still want to filter it michael@0: // based on the effectAllowed below. michael@0: dataTransfer = initialDataTransfer; michael@0: michael@0: uint32_t action; michael@0: dragSession->GetDragAction(&action); michael@0: michael@0: // filter the drop effect based on the action. Use UNINITIALIZED as michael@0: // any effect is allowed. michael@0: dropEffect = nsContentUtils::FilterDropEffect(action, michael@0: nsIDragService::DRAGDROP_ACTION_UNINITIALIZED); michael@0: } michael@0: michael@0: // At this point, if the dataTransfer is null, it means that the michael@0: // drag was originally started by directly calling the drag service. michael@0: // Just assume that all effects are allowed. michael@0: uint32_t effectAllowed = nsIDragService::DRAGDROP_ACTION_UNINITIALIZED; michael@0: if (dataTransfer) michael@0: dataTransfer->GetEffectAllowedInt(&effectAllowed); michael@0: michael@0: // set the drag action based on the drop effect and effect allowed. michael@0: // The drop effect field on the drag transfer object specifies the michael@0: // desired current drop effect. However, it cannot be used if the michael@0: // effectAllowed state doesn't include that type of action. If the michael@0: // dropEffect is "none", then the action will be 'none' so a drop will michael@0: // not be allowed. michael@0: uint32_t action = nsIDragService::DRAGDROP_ACTION_NONE; michael@0: if (effectAllowed == nsIDragService::DRAGDROP_ACTION_UNINITIALIZED || michael@0: dropEffect & effectAllowed) michael@0: action = dropEffect; michael@0: michael@0: if (action == nsIDragService::DRAGDROP_ACTION_NONE) michael@0: dropEffect = nsIDragService::DRAGDROP_ACTION_NONE; michael@0: michael@0: // inform the drag session that a drop is allowed on this node. michael@0: dragSession->SetDragAction(action); michael@0: dragSession->SetCanDrop(action != nsIDragService::DRAGDROP_ACTION_NONE); michael@0: michael@0: // For now, do this only for dragover. michael@0: //XXXsmaug dragenter needs some more work. michael@0: if (aEvent->message == NS_DRAGDROP_OVER && !isChromeDoc) { michael@0: // Someone has called preventDefault(), check whether is was on michael@0: // content or chrome. michael@0: dragSession->SetOnlyChromeDrop( michael@0: !dragEvent->mDefaultPreventedOnContent); michael@0: } michael@0: } else if (aEvent->message == NS_DRAGDROP_OVER && !isChromeDoc) { michael@0: // No one called preventDefault(), so handle drop only in chrome. michael@0: dragSession->SetOnlyChromeDrop(true); michael@0: } michael@0: michael@0: // now set the drop effect in the initial dataTransfer. This ensures michael@0: // that we can get the desired drop effect in the drop event. michael@0: if (initialDataTransfer) michael@0: initialDataTransfer->SetDropEffectInt(dropEffect); michael@0: } michael@0: break; michael@0: michael@0: case NS_DRAGDROP_DROP: michael@0: { michael@0: // now fire the dragdrop event, for compatibility with XUL michael@0: if (mCurrentTarget && nsEventStatus_eConsumeNoDefault != *aStatus) { michael@0: nsCOMPtr targetContent; michael@0: mCurrentTarget->GetContentForEvent(aEvent, michael@0: getter_AddRefs(targetContent)); michael@0: michael@0: nsCOMPtr widget = mCurrentTarget->GetNearestWidget(); michael@0: WidgetDragEvent event(aEvent->mFlags.mIsTrusted, michael@0: NS_DRAGDROP_DRAGDROP, widget); michael@0: michael@0: WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent(); michael@0: event.refPoint = mouseEvent->refPoint; michael@0: if (mouseEvent->widget) { michael@0: event.refPoint += LayoutDeviceIntPoint::FromUntyped(mouseEvent->widget->WidgetToScreenOffset()); michael@0: } michael@0: event.refPoint -= LayoutDeviceIntPoint::FromUntyped(widget->WidgetToScreenOffset()); michael@0: event.modifiers = mouseEvent->modifiers; michael@0: event.buttons = mouseEvent->buttons; michael@0: event.inputSource = mouseEvent->inputSource; michael@0: michael@0: nsEventStatus status = nsEventStatus_eIgnore; michael@0: nsCOMPtr presShell = mPresContext->GetPresShell(); michael@0: if (presShell) { michael@0: presShell->HandleEventWithTarget(&event, mCurrentTarget, michael@0: targetContent, &status); michael@0: } michael@0: } michael@0: sLastDragOverFrame = nullptr; michael@0: ClearGlobalActiveContent(this); michael@0: break; michael@0: } michael@0: case NS_DRAGDROP_EXIT: michael@0: // make sure to fire the enter and exit_synth events after the michael@0: // NS_DRAGDROP_EXIT event, otherwise we'll clean up too early michael@0: GenerateDragDropEnterExit(presContext, aEvent->AsDragEvent()); michael@0: break; michael@0: michael@0: case NS_KEY_UP: michael@0: break; michael@0: michael@0: case NS_KEY_PRESS: michael@0: if (nsEventStatus_eConsumeNoDefault != *aStatus) { michael@0: WidgetKeyboardEvent* keyEvent = aEvent->AsKeyboardEvent(); michael@0: //This is to prevent keyboard scrolling while alt modifier in use. michael@0: if (!keyEvent->IsAlt()) { michael@0: switch(keyEvent->keyCode) { michael@0: case NS_VK_TAB: michael@0: case NS_VK_F6: michael@0: // Handling the tab event after it was sent to content is bad, michael@0: // because to the FocusManager the remote-browser looks like one michael@0: // element, so we would just move the focus to the next element michael@0: // in chrome, instead of handling it in content. michael@0: if (dispatchedToContentProcess) michael@0: break; michael@0: michael@0: EnsureDocument(mPresContext); michael@0: nsIFocusManager* fm = nsFocusManager::GetFocusManager(); michael@0: if (fm && mDocument) { michael@0: // Shift focus forward or back depending on shift key michael@0: bool isDocMove = michael@0: keyEvent->IsControl() || keyEvent->keyCode == NS_VK_F6; michael@0: uint32_t dir = keyEvent->IsShift() ? michael@0: (isDocMove ? static_cast(nsIFocusManager::MOVEFOCUS_BACKWARDDOC) : michael@0: static_cast(nsIFocusManager::MOVEFOCUS_BACKWARD)) : michael@0: (isDocMove ? static_cast(nsIFocusManager::MOVEFOCUS_FORWARDDOC) : michael@0: static_cast(nsIFocusManager::MOVEFOCUS_FORWARD)); michael@0: nsCOMPtr result; michael@0: fm->MoveFocus(mDocument->GetWindow(), nullptr, dir, michael@0: nsIFocusManager::FLAG_BYKEY, michael@0: getter_AddRefs(result)); michael@0: } michael@0: *aStatus = nsEventStatus_eConsumeNoDefault; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: michael@0: case NS_MOUSE_ENTER: michael@0: if (mCurrentTarget) { michael@0: nsCOMPtr targetContent; michael@0: mCurrentTarget->GetContentForEvent(aEvent, getter_AddRefs(targetContent)); michael@0: SetContentState(targetContent, NS_EVENT_STATE_HOVER); michael@0: } michael@0: break; michael@0: michael@0: #ifdef XP_MACOSX michael@0: case NS_MOUSE_ACTIVATE: michael@0: if (mCurrentTarget) { michael@0: nsCOMPtr targetContent; michael@0: mCurrentTarget->GetContentForEvent(aEvent, getter_AddRefs(targetContent)); michael@0: if (!NodeAllowsClickThrough(targetContent)) { michael@0: *aStatus = nsEventStatus_eConsumeNoDefault; michael@0: } michael@0: } michael@0: break; michael@0: #endif michael@0: } michael@0: michael@0: //Reset target frame to null to avoid mistargeting after reentrant event michael@0: mCurrentTarget = nullptr; michael@0: mCurrentTargetContent = nullptr; michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: bool michael@0: EventStateManager::RemoteQueryContentEvent(WidgetEvent* aEvent) michael@0: { michael@0: WidgetQueryContentEvent* queryEvent = aEvent->AsQueryContentEvent(); michael@0: if (!IsTargetCrossProcess(queryEvent)) { michael@0: return false; michael@0: } michael@0: // Will not be handled locally, remote the event michael@0: GetCrossProcessTarget()->HandleQueryContentEvent(*queryEvent); michael@0: return true; michael@0: } michael@0: michael@0: TabParent* michael@0: EventStateManager::GetCrossProcessTarget() michael@0: { michael@0: return TabParent::GetIMETabParent(); michael@0: } michael@0: michael@0: bool michael@0: EventStateManager::IsTargetCrossProcess(WidgetGUIEvent* aEvent) michael@0: { michael@0: // Check to see if there is a focused, editable content in chrome, michael@0: // in that case, do not forward IME events to content michael@0: nsIContent *focusedContent = GetFocusedContent(); michael@0: if (focusedContent && focusedContent->IsEditable()) michael@0: return false; michael@0: return TabParent::GetIMETabParent() != nullptr; michael@0: } michael@0: michael@0: void michael@0: EventStateManager::NotifyDestroyPresContext(nsPresContext* aPresContext) michael@0: { michael@0: IMEStateManager::OnDestroyPresContext(aPresContext); michael@0: if (mHoverContent) { michael@0: // Bug 70855: Presentation is going away, possibly for a reframe. michael@0: // Reset the hover state so that if we're recreating the presentation, michael@0: // we won't have the old hover state still set in the new presentation, michael@0: // as if the new presentation is resized, a new element may be hovered. michael@0: SetContentState(nullptr, NS_EVENT_STATE_HOVER); michael@0: } michael@0: mPointersEnterLeaveHelper.Clear(); michael@0: } michael@0: michael@0: void michael@0: EventStateManager::SetPresContext(nsPresContext* aPresContext) michael@0: { michael@0: mPresContext = aPresContext; michael@0: } michael@0: michael@0: void michael@0: EventStateManager::ClearFrameRefs(nsIFrame* aFrame) michael@0: { michael@0: if (aFrame && aFrame == mCurrentTarget) { michael@0: mCurrentTargetContent = aFrame->GetContent(); michael@0: } michael@0: } michael@0: michael@0: void michael@0: EventStateManager::UpdateCursor(nsPresContext* aPresContext, michael@0: WidgetEvent* aEvent, michael@0: nsIFrame* aTargetFrame, michael@0: nsEventStatus* aStatus) michael@0: { michael@0: if (aTargetFrame && IsRemoteTarget(aTargetFrame->GetContent())) { michael@0: return; michael@0: } michael@0: michael@0: int32_t cursor = NS_STYLE_CURSOR_DEFAULT; michael@0: imgIContainer* container = nullptr; michael@0: bool haveHotspot = false; michael@0: float hotspotX = 0.0f, hotspotY = 0.0f; michael@0: michael@0: //If cursor is locked just use the locked one michael@0: if (mLockCursor) { michael@0: cursor = mLockCursor; michael@0: } michael@0: //If not locked, look for correct cursor michael@0: else if (aTargetFrame) { michael@0: nsIFrame::Cursor framecursor; michael@0: nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, michael@0: aTargetFrame); michael@0: if (NS_FAILED(aTargetFrame->GetCursor(pt, framecursor))) michael@0: return; // don't update the cursor if we failed to get it from the frame see bug 118877 michael@0: cursor = framecursor.mCursor; michael@0: container = framecursor.mContainer; michael@0: haveHotspot = framecursor.mHaveHotspot; michael@0: hotspotX = framecursor.mHotspotX; michael@0: hotspotY = framecursor.mHotspotY; michael@0: } michael@0: michael@0: if (Preferences::GetBool("ui.use_activity_cursor", false)) { michael@0: // Check whether or not to show the busy cursor michael@0: nsCOMPtr docShell(aPresContext->GetDocShell()); michael@0: if (!docShell) return; michael@0: uint32_t busyFlags = nsIDocShell::BUSY_FLAGS_NONE; michael@0: docShell->GetBusyFlags(&busyFlags); michael@0: michael@0: // Show busy cursor everywhere before page loads michael@0: // and just replace the arrow cursor after page starts loading michael@0: if (busyFlags & nsIDocShell::BUSY_FLAGS_BUSY && michael@0: (cursor == NS_STYLE_CURSOR_AUTO || cursor == NS_STYLE_CURSOR_DEFAULT)) michael@0: { michael@0: cursor = NS_STYLE_CURSOR_SPINNING; michael@0: container = nullptr; michael@0: } michael@0: } michael@0: michael@0: if (aTargetFrame) { michael@0: SetCursor(cursor, container, haveHotspot, hotspotX, hotspotY, michael@0: aTargetFrame->GetNearestWidget(), false); michael@0: } michael@0: michael@0: if (mLockCursor || NS_STYLE_CURSOR_AUTO != cursor) { michael@0: *aStatus = nsEventStatus_eConsumeDoDefault; michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: EventStateManager::SetCursor(int32_t aCursor, imgIContainer* aContainer, michael@0: bool aHaveHotspot, michael@0: float aHotspotX, float aHotspotY, michael@0: nsIWidget* aWidget, bool aLockCursor) michael@0: { michael@0: EnsureDocument(mPresContext); michael@0: NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE); michael@0: sMouseOverDocument = mDocument.get(); michael@0: michael@0: nsCursor c; michael@0: michael@0: NS_ENSURE_TRUE(aWidget, NS_ERROR_FAILURE); michael@0: if (aLockCursor) { michael@0: if (NS_STYLE_CURSOR_AUTO != aCursor) { michael@0: mLockCursor = aCursor; michael@0: } michael@0: else { michael@0: //If cursor style is set to auto we unlock the cursor again. michael@0: mLockCursor = 0; michael@0: } michael@0: } michael@0: switch (aCursor) { michael@0: default: michael@0: case NS_STYLE_CURSOR_AUTO: michael@0: case NS_STYLE_CURSOR_DEFAULT: michael@0: c = eCursor_standard; michael@0: break; michael@0: case NS_STYLE_CURSOR_POINTER: michael@0: c = eCursor_hyperlink; michael@0: break; michael@0: case NS_STYLE_CURSOR_CROSSHAIR: michael@0: c = eCursor_crosshair; michael@0: break; michael@0: case NS_STYLE_CURSOR_MOVE: michael@0: c = eCursor_move; michael@0: break; michael@0: case NS_STYLE_CURSOR_TEXT: michael@0: c = eCursor_select; michael@0: break; michael@0: case NS_STYLE_CURSOR_WAIT: michael@0: c = eCursor_wait; michael@0: break; michael@0: case NS_STYLE_CURSOR_HELP: michael@0: c = eCursor_help; michael@0: break; michael@0: case NS_STYLE_CURSOR_N_RESIZE: michael@0: c = eCursor_n_resize; michael@0: break; michael@0: case NS_STYLE_CURSOR_S_RESIZE: michael@0: c = eCursor_s_resize; michael@0: break; michael@0: case NS_STYLE_CURSOR_W_RESIZE: michael@0: c = eCursor_w_resize; michael@0: break; michael@0: case NS_STYLE_CURSOR_E_RESIZE: michael@0: c = eCursor_e_resize; michael@0: break; michael@0: case NS_STYLE_CURSOR_NW_RESIZE: michael@0: c = eCursor_nw_resize; michael@0: break; michael@0: case NS_STYLE_CURSOR_SE_RESIZE: michael@0: c = eCursor_se_resize; michael@0: break; michael@0: case NS_STYLE_CURSOR_NE_RESIZE: michael@0: c = eCursor_ne_resize; michael@0: break; michael@0: case NS_STYLE_CURSOR_SW_RESIZE: michael@0: c = eCursor_sw_resize; michael@0: break; michael@0: case NS_STYLE_CURSOR_COPY: // CSS3 michael@0: c = eCursor_copy; michael@0: break; michael@0: case NS_STYLE_CURSOR_ALIAS: michael@0: c = eCursor_alias; michael@0: break; michael@0: case NS_STYLE_CURSOR_CONTEXT_MENU: michael@0: c = eCursor_context_menu; michael@0: break; michael@0: case NS_STYLE_CURSOR_CELL: michael@0: c = eCursor_cell; michael@0: break; michael@0: case NS_STYLE_CURSOR_GRAB: michael@0: c = eCursor_grab; michael@0: break; michael@0: case NS_STYLE_CURSOR_GRABBING: michael@0: c = eCursor_grabbing; michael@0: break; michael@0: case NS_STYLE_CURSOR_SPINNING: michael@0: c = eCursor_spinning; michael@0: break; michael@0: case NS_STYLE_CURSOR_ZOOM_IN: michael@0: c = eCursor_zoom_in; michael@0: break; michael@0: case NS_STYLE_CURSOR_ZOOM_OUT: michael@0: c = eCursor_zoom_out; michael@0: break; michael@0: case NS_STYLE_CURSOR_NOT_ALLOWED: michael@0: c = eCursor_not_allowed; michael@0: break; michael@0: case NS_STYLE_CURSOR_COL_RESIZE: michael@0: c = eCursor_col_resize; michael@0: break; michael@0: case NS_STYLE_CURSOR_ROW_RESIZE: michael@0: c = eCursor_row_resize; michael@0: break; michael@0: case NS_STYLE_CURSOR_NO_DROP: michael@0: c = eCursor_no_drop; michael@0: break; michael@0: case NS_STYLE_CURSOR_VERTICAL_TEXT: michael@0: c = eCursor_vertical_text; michael@0: break; michael@0: case NS_STYLE_CURSOR_ALL_SCROLL: michael@0: c = eCursor_all_scroll; michael@0: break; michael@0: case NS_STYLE_CURSOR_NESW_RESIZE: michael@0: c = eCursor_nesw_resize; michael@0: break; michael@0: case NS_STYLE_CURSOR_NWSE_RESIZE: michael@0: c = eCursor_nwse_resize; michael@0: break; michael@0: case NS_STYLE_CURSOR_NS_RESIZE: michael@0: c = eCursor_ns_resize; michael@0: break; michael@0: case NS_STYLE_CURSOR_EW_RESIZE: michael@0: c = eCursor_ew_resize; michael@0: break; michael@0: case NS_STYLE_CURSOR_NONE: michael@0: c = eCursor_none; michael@0: break; michael@0: } michael@0: michael@0: // First, try the imgIContainer, if non-null michael@0: nsresult rv = NS_ERROR_FAILURE; michael@0: if (aContainer) { michael@0: uint32_t hotspotX, hotspotY; michael@0: michael@0: // css3-ui says to use the CSS-specified hotspot if present, michael@0: // otherwise use the intrinsic hotspot, otherwise use the top left michael@0: // corner. michael@0: if (aHaveHotspot) { michael@0: int32_t imgWidth, imgHeight; michael@0: aContainer->GetWidth(&imgWidth); michael@0: aContainer->GetHeight(&imgHeight); michael@0: michael@0: // XXX std::max(NS_lround(x), 0)? michael@0: hotspotX = aHotspotX > 0.0f michael@0: ? uint32_t(aHotspotX + 0.5f) : uint32_t(0); michael@0: if (hotspotX >= uint32_t(imgWidth)) michael@0: hotspotX = imgWidth - 1; michael@0: hotspotY = aHotspotY > 0.0f michael@0: ? uint32_t(aHotspotY + 0.5f) : uint32_t(0); michael@0: if (hotspotY >= uint32_t(imgHeight)) michael@0: hotspotY = imgHeight - 1; michael@0: } else { michael@0: hotspotX = 0; michael@0: hotspotY = 0; michael@0: nsCOMPtr props(do_QueryInterface(aContainer)); michael@0: if (props) { michael@0: nsCOMPtr hotspotXWrap, hotspotYWrap; michael@0: michael@0: props->Get("hotspotX", NS_GET_IID(nsISupportsPRUint32), getter_AddRefs(hotspotXWrap)); michael@0: props->Get("hotspotY", NS_GET_IID(nsISupportsPRUint32), getter_AddRefs(hotspotYWrap)); michael@0: michael@0: if (hotspotXWrap) michael@0: hotspotXWrap->GetData(&hotspotX); michael@0: if (hotspotYWrap) michael@0: hotspotYWrap->GetData(&hotspotY); michael@0: } michael@0: } michael@0: michael@0: rv = aWidget->SetCursor(aContainer, hotspotX, hotspotY); michael@0: } michael@0: michael@0: if (NS_FAILED(rv)) michael@0: aWidget->SetCursor(c); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: class MOZ_STACK_CLASS ESMEventCB : public EventDispatchingCallback michael@0: { michael@0: public: michael@0: ESMEventCB(nsIContent* aTarget) : mTarget(aTarget) {} michael@0: michael@0: virtual void HandleEvent(EventChainPostVisitor& aVisitor) michael@0: { michael@0: if (aVisitor.mPresContext) { michael@0: nsIFrame* frame = aVisitor.mPresContext->GetPrimaryFrameFor(mTarget); michael@0: if (frame) { michael@0: frame->HandleEvent(aVisitor.mPresContext, michael@0: aVisitor.mEvent->AsGUIEvent(), michael@0: &aVisitor.mEventStatus); michael@0: } michael@0: } michael@0: } michael@0: michael@0: nsCOMPtr mTarget; michael@0: }; michael@0: michael@0: /*static*/ bool michael@0: EventStateManager::IsHandlingUserInput() michael@0: { michael@0: if (sUserInputEventDepth <= 0) { michael@0: return false; michael@0: } michael@0: michael@0: TimeDuration timeout = nsContentUtils::HandlingUserInputTimeout(); michael@0: return timeout <= TimeDuration(0) || michael@0: (TimeStamp::Now() - sHandlingInputStart) <= timeout; michael@0: } michael@0: michael@0: nsIFrame* michael@0: EventStateManager::DispatchMouseOrPointerEvent(WidgetMouseEvent* aMouseEvent, michael@0: uint32_t aMessage, michael@0: nsIContent* aTargetContent, michael@0: nsIContent* aRelatedContent) michael@0: { michael@0: // http://dvcs.w3.org/hg/webevents/raw-file/default/mouse-lock.html#methods michael@0: // "[When the mouse is locked on an element...e]vents that require the concept michael@0: // of a mouse cursor must not be dispatched (for example: mouseover, mouseout). michael@0: if (sIsPointerLocked && michael@0: (aMessage == NS_MOUSELEAVE || michael@0: aMessage == NS_MOUSEENTER || michael@0: aMessage == NS_MOUSE_ENTER_SYNTH || michael@0: aMessage == NS_MOUSE_EXIT_SYNTH)) { michael@0: mCurrentTargetContent = nullptr; michael@0: nsCOMPtr pointerLockedElement = michael@0: do_QueryReferent(EventStateManager::sPointerLockedElement); michael@0: if (!pointerLockedElement) { michael@0: NS_WARNING("Should have pointer locked element, but didn't."); michael@0: return nullptr; michael@0: } michael@0: nsCOMPtr content = do_QueryInterface(pointerLockedElement); michael@0: return mPresContext->GetPrimaryFrameFor(content); michael@0: } michael@0: michael@0: nsEventStatus status = nsEventStatus_eIgnore; michael@0: nsAutoPtr event; michael@0: WidgetPointerEvent* sourcePointer = aMouseEvent->AsPointerEvent(); michael@0: if (sourcePointer) { michael@0: PROFILER_LABEL("Input", "DispatchPointerEvent"); michael@0: nsAutoPtr newPointerEvent; michael@0: newPointerEvent = michael@0: new WidgetPointerEvent(aMouseEvent->mFlags.mIsTrusted, aMessage, michael@0: aMouseEvent->widget); michael@0: newPointerEvent->isPrimary = sourcePointer->isPrimary; michael@0: newPointerEvent->pointerId = sourcePointer->pointerId; michael@0: newPointerEvent->width = sourcePointer->width; michael@0: newPointerEvent->height = sourcePointer->height; michael@0: newPointerEvent->inputSource = sourcePointer->inputSource; michael@0: newPointerEvent->relatedTarget = nsIPresShell::GetPointerCapturingContent(sourcePointer->pointerId) michael@0: ? nullptr michael@0: : aRelatedContent; michael@0: event = newPointerEvent.forget(); michael@0: } else { michael@0: PROFILER_LABEL("Input", "DispatchMouseEvent"); michael@0: event = michael@0: new WidgetMouseEvent(aMouseEvent->mFlags.mIsTrusted, aMessage, michael@0: aMouseEvent->widget, WidgetMouseEvent::eReal); michael@0: event->relatedTarget = aRelatedContent; michael@0: } michael@0: event->refPoint = aMouseEvent->refPoint; michael@0: event->modifiers = aMouseEvent->modifiers; michael@0: event->button = aMouseEvent->button; michael@0: event->buttons = aMouseEvent->buttons; michael@0: event->pressure = aMouseEvent->pressure; michael@0: event->pluginEvent = aMouseEvent->pluginEvent; michael@0: event->inputSource = aMouseEvent->inputSource; michael@0: michael@0: nsWeakFrame previousTarget = mCurrentTarget; michael@0: michael@0: mCurrentTargetContent = aTargetContent; michael@0: michael@0: nsIFrame* targetFrame = nullptr; michael@0: if (aTargetContent) { michael@0: ESMEventCB callback(aTargetContent); michael@0: EventDispatcher::Dispatch(aTargetContent, mPresContext, event, nullptr, michael@0: &status, &callback); michael@0: michael@0: // Although the primary frame was checked in event callback, michael@0: // it may not be the same object after event dispatching and handling. michael@0: // So we need to refetch it. michael@0: if (mPresContext) { michael@0: targetFrame = mPresContext->GetPrimaryFrameFor(aTargetContent); michael@0: } michael@0: } michael@0: michael@0: mCurrentTargetContent = nullptr; michael@0: mCurrentTarget = previousTarget; michael@0: michael@0: return targetFrame; michael@0: } michael@0: michael@0: class EnterLeaveDispatcher michael@0: { michael@0: public: michael@0: EnterLeaveDispatcher(EventStateManager* aESM, michael@0: nsIContent* aTarget, nsIContent* aRelatedTarget, michael@0: WidgetMouseEvent* aMouseEvent, uint32_t aType) michael@0: : mESM(aESM) michael@0: , mMouseEvent(aMouseEvent) michael@0: , mType(aType) michael@0: { michael@0: nsPIDOMWindow* win = michael@0: aTarget ? aTarget->OwnerDoc()->GetInnerWindow() : nullptr; michael@0: if (aMouseEvent->AsPointerEvent() ? win && win->HasPointerEnterLeaveEventListeners() : michael@0: win && win->HasMouseEnterLeaveEventListeners()) { michael@0: mRelatedTarget = aRelatedTarget ? michael@0: aRelatedTarget->FindFirstNonChromeOnlyAccessContent() : nullptr; michael@0: nsINode* commonParent = nullptr; michael@0: if (aTarget && aRelatedTarget) { michael@0: commonParent = michael@0: nsContentUtils::GetCommonAncestor(aTarget, aRelatedTarget); michael@0: } michael@0: nsIContent* current = aTarget; michael@0: // Note, it is ok if commonParent is null! michael@0: while (current && current != commonParent) { michael@0: if (!current->ChromeOnlyAccess()) { michael@0: mTargets.AppendObject(current); michael@0: } michael@0: // mouseenter/leave is fired only on elements. michael@0: current = current->GetParent(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: ~EnterLeaveDispatcher() michael@0: { michael@0: if (mType == NS_MOUSEENTER || michael@0: mType == NS_POINTER_ENTER) { michael@0: for (int32_t i = mTargets.Count() - 1; i >= 0; --i) { michael@0: mESM->DispatchMouseOrPointerEvent(mMouseEvent, mType, mTargets[i], michael@0: mRelatedTarget); michael@0: } michael@0: } else { michael@0: for (int32_t i = 0; i < mTargets.Count(); ++i) { michael@0: mESM->DispatchMouseOrPointerEvent(mMouseEvent, mType, mTargets[i], michael@0: mRelatedTarget); michael@0: } michael@0: } michael@0: } michael@0: michael@0: EventStateManager* mESM; michael@0: nsCOMArray mTargets; michael@0: nsCOMPtr mRelatedTarget; michael@0: WidgetMouseEvent* mMouseEvent; michael@0: uint32_t mType; michael@0: }; michael@0: michael@0: void michael@0: EventStateManager::NotifyMouseOut(WidgetMouseEvent* aMouseEvent, michael@0: nsIContent* aMovingInto) michael@0: { michael@0: OverOutElementsWrapper* wrapper = GetWrapperByEventID(aMouseEvent); michael@0: michael@0: if (!wrapper->mLastOverElement) michael@0: return; michael@0: // Before firing mouseout, check for recursion michael@0: if (wrapper->mLastOverElement == wrapper->mFirstOutEventElement) michael@0: return; michael@0: michael@0: if (wrapper->mLastOverFrame) { michael@0: // if the frame is associated with a subdocument, michael@0: // tell the subdocument that we're moving out of it michael@0: nsSubDocumentFrame* subdocFrame = do_QueryFrame(wrapper->mLastOverFrame.GetFrame()); michael@0: if (subdocFrame) { michael@0: nsCOMPtr docshell; michael@0: subdocFrame->GetDocShell(getter_AddRefs(docshell)); michael@0: if (docshell) { michael@0: nsRefPtr presContext; michael@0: docshell->GetPresContext(getter_AddRefs(presContext)); michael@0: michael@0: if (presContext) { michael@0: EventStateManager* kidESM = presContext->EventStateManager(); michael@0: // Not moving into any element in this subdocument michael@0: kidESM->NotifyMouseOut(aMouseEvent, nullptr); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: // That could have caused DOM events which could wreak havoc. Reverify michael@0: // things and be careful. michael@0: if (!wrapper->mLastOverElement) michael@0: return; michael@0: michael@0: // Store the first mouseOut event we fire and don't refire mouseOut michael@0: // to that element while the first mouseOut is still ongoing. michael@0: wrapper->mFirstOutEventElement = wrapper->mLastOverElement; michael@0: michael@0: // Don't touch hover state if aMovingInto is non-null. Caller will update michael@0: // hover state itself, and we have optimizations for hover switching between michael@0: // two nearby elements both deep in the DOM tree that would be defeated by michael@0: // switching the hover state to null here. michael@0: bool isPointer = aMouseEvent->eventStructType == NS_POINTER_EVENT; michael@0: if (!aMovingInto && !isPointer) { michael@0: // Unset :hover michael@0: SetContentState(nullptr, NS_EVENT_STATE_HOVER); michael@0: } michael@0: michael@0: EnterLeaveDispatcher leaveDispatcher(this, wrapper->mLastOverElement, michael@0: aMovingInto, aMouseEvent, michael@0: isPointer ? NS_POINTER_LEAVE : michael@0: NS_MOUSELEAVE); michael@0: michael@0: // Fire mouseout michael@0: DispatchMouseOrPointerEvent(aMouseEvent, isPointer ? NS_POINTER_OUT : NS_MOUSE_EXIT_SYNTH, michael@0: wrapper->mLastOverElement, aMovingInto); michael@0: michael@0: wrapper->mLastOverFrame = nullptr; michael@0: wrapper->mLastOverElement = nullptr; michael@0: michael@0: // Turn recursion protection back off michael@0: wrapper->mFirstOutEventElement = nullptr; michael@0: } michael@0: michael@0: void michael@0: EventStateManager::NotifyMouseOver(WidgetMouseEvent* aMouseEvent, michael@0: nsIContent* aContent) michael@0: { michael@0: NS_ASSERTION(aContent, "Mouse must be over something"); michael@0: michael@0: OverOutElementsWrapper* wrapper = GetWrapperByEventID(aMouseEvent); michael@0: michael@0: if (wrapper->mLastOverElement == aContent) michael@0: return; michael@0: michael@0: // Before firing mouseover, check for recursion michael@0: if (aContent == wrapper->mFirstOverEventElement) michael@0: return; michael@0: michael@0: // Check to see if we're a subdocument and if so update the parent michael@0: // document's ESM state to indicate that the mouse is over the michael@0: // content associated with our subdocument. michael@0: EnsureDocument(mPresContext); michael@0: nsIDocument *parentDoc = mDocument->GetParentDocument(); michael@0: if (parentDoc) { michael@0: nsIContent *docContent = parentDoc->FindContentForSubDocument(mDocument); michael@0: if (docContent) { michael@0: nsIPresShell *parentShell = parentDoc->GetShell(); michael@0: if (parentShell) { michael@0: EventStateManager* parentESM = michael@0: parentShell->GetPresContext()->EventStateManager(); michael@0: parentESM->NotifyMouseOver(aMouseEvent, docContent); michael@0: } michael@0: } michael@0: } michael@0: // Firing the DOM event in the parent document could cause all kinds michael@0: // of havoc. Reverify and take care. michael@0: if (wrapper->mLastOverElement == aContent) michael@0: return; michael@0: michael@0: // Remember mLastOverElement as the related content for the michael@0: // DispatchMouseOrPointerEvent() call below, since NotifyMouseOut() resets it, bug 298477. michael@0: nsCOMPtr lastOverElement = wrapper->mLastOverElement; michael@0: michael@0: bool isPointer = aMouseEvent->eventStructType == NS_POINTER_EVENT; michael@0: EnterLeaveDispatcher enterDispatcher(this, aContent, lastOverElement, michael@0: aMouseEvent, michael@0: isPointer ? NS_POINTER_ENTER : michael@0: NS_MOUSEENTER); michael@0: michael@0: NotifyMouseOut(aMouseEvent, aContent); michael@0: michael@0: // Store the first mouseOver event we fire and don't refire mouseOver michael@0: // to that element while the first mouseOver is still ongoing. michael@0: wrapper->mFirstOverEventElement = aContent; michael@0: michael@0: if (!isPointer) { michael@0: SetContentState(aContent, NS_EVENT_STATE_HOVER); michael@0: } michael@0: michael@0: // Fire mouseover michael@0: wrapper->mLastOverFrame = michael@0: DispatchMouseOrPointerEvent(aMouseEvent, michael@0: isPointer ? NS_POINTER_OVER : michael@0: NS_MOUSE_ENTER_SYNTH, michael@0: aContent, lastOverElement); michael@0: wrapper->mLastOverElement = aContent; michael@0: michael@0: // Turn recursion protection back off michael@0: wrapper->mFirstOverEventElement = nullptr; michael@0: } michael@0: michael@0: // Returns the center point of the window's inner content area. michael@0: // This is in widget coordinates, i.e. relative to the widget's top michael@0: // left corner, not in screen coordinates, the same units that michael@0: // UIEvent::refPoint is in. michael@0: // michael@0: // XXX Hack alert: XXX michael@0: // However, we do the computation in integer CSS pixels, NOT device pix, michael@0: // in order to fudge around the one-pixel error in innerHeight in fullscreen michael@0: // mode (see bug 799523 comment 35, and bug 729011). Using integer CSS pix michael@0: // makes us throw away the fractional error that results, rather than having michael@0: // it manifest as a potential one-device-pix discrepancy. michael@0: static LayoutDeviceIntPoint michael@0: GetWindowInnerRectCenter(nsPIDOMWindow* aWindow, michael@0: nsIWidget* aWidget, michael@0: nsPresContext* aContext) michael@0: { michael@0: NS_ENSURE_TRUE(aWindow && aWidget && aContext, LayoutDeviceIntPoint(0, 0)); michael@0: michael@0: float cssInnerX = 0.0; michael@0: aWindow->GetMozInnerScreenX(&cssInnerX); michael@0: int32_t innerX = int32_t(NS_round(cssInnerX)); michael@0: michael@0: float cssInnerY = 0.0; michael@0: aWindow->GetMozInnerScreenY(&cssInnerY); michael@0: int32_t innerY = int32_t(NS_round(cssInnerY)); michael@0: michael@0: int32_t innerWidth = 0; michael@0: aWindow->GetInnerWidth(&innerWidth); michael@0: michael@0: int32_t innerHeight = 0; michael@0: aWindow->GetInnerHeight(&innerHeight); michael@0: michael@0: nsIntRect screen; michael@0: aWidget->GetScreenBounds(screen); michael@0: michael@0: int32_t cssScreenX = aContext->DevPixelsToIntCSSPixels(screen.x); michael@0: int32_t cssScreenY = aContext->DevPixelsToIntCSSPixels(screen.y); michael@0: michael@0: return LayoutDeviceIntPoint( michael@0: aContext->CSSPixelsToDevPixels(innerX - cssScreenX + innerWidth / 2), michael@0: aContext->CSSPixelsToDevPixels(innerY - cssScreenY + innerHeight / 2)); michael@0: } michael@0: michael@0: void michael@0: EventStateManager::GenerateMouseEnterExit(WidgetMouseEvent* aMouseEvent) michael@0: { michael@0: EnsureDocument(mPresContext); michael@0: if (!mDocument) michael@0: return; michael@0: michael@0: // Hold onto old target content through the event and reset after. michael@0: nsCOMPtr targetBeforeEvent = mCurrentTargetContent; michael@0: michael@0: switch(aMouseEvent->message) { michael@0: case NS_MOUSE_MOVE: michael@0: { michael@0: // Mouse movement is reported on the MouseEvent.movement{X,Y} fields. michael@0: // Movement is calculated in UIEvent::GetMovementPoint() as: michael@0: // previous_mousemove_refPoint - current_mousemove_refPoint. michael@0: if (sIsPointerLocked && aMouseEvent->widget) { michael@0: // The pointer is locked. If the pointer is not located at the center of michael@0: // the window, dispatch a synthetic mousemove to return the pointer there. michael@0: // Doing this between "real" pointer moves gives the impression that the michael@0: // (locked) pointer can continue moving and won't stop at the screen michael@0: // boundary. We cancel the synthetic event so that we don't end up michael@0: // dispatching the centering move event to content. michael@0: LayoutDeviceIntPoint center = michael@0: GetWindowInnerRectCenter(mDocument->GetWindow(), aMouseEvent->widget, michael@0: mPresContext); michael@0: aMouseEvent->lastRefPoint = center; michael@0: if (aMouseEvent->refPoint != center) { michael@0: // Mouse move doesn't finish at the center of the window. Dispatch a michael@0: // synthetic native mouse event to move the pointer back to the center michael@0: // of the window, to faciliate more movement. But first, record that michael@0: // we've dispatched a synthetic mouse movement, so we can cancel it michael@0: // in the other branch here. michael@0: sSynthCenteringPoint = center; michael@0: aMouseEvent->widget->SynthesizeNativeMouseMove( michael@0: LayoutDeviceIntPoint::ToUntyped(center) + michael@0: aMouseEvent->widget->WidgetToScreenOffset()); michael@0: } else if (aMouseEvent->refPoint == sSynthCenteringPoint) { michael@0: // This is the "synthetic native" event we dispatched to re-center the michael@0: // pointer. Cancel it so we don't expose the centering move to content. michael@0: aMouseEvent->mFlags.mPropagationStopped = true; michael@0: // Clear sSynthCenteringPoint so we don't cancel other events michael@0: // targeted at the center. michael@0: sSynthCenteringPoint = kInvalidRefPoint; michael@0: } michael@0: } else if (sLastRefPoint == kInvalidRefPoint) { michael@0: // We don't have a valid previous mousemove refPoint. This is either michael@0: // the first move we've encountered, or the mouse has just re-entered michael@0: // the application window. We should report (0,0) movement for this michael@0: // case, so make the current and previous refPoints the same. michael@0: aMouseEvent->lastRefPoint = aMouseEvent->refPoint; michael@0: } else { michael@0: aMouseEvent->lastRefPoint = sLastRefPoint; michael@0: } michael@0: michael@0: // Update the last known refPoint with the current refPoint. michael@0: sLastRefPoint = aMouseEvent->refPoint; michael@0: michael@0: } michael@0: case NS_POINTER_MOVE: michael@0: case NS_POINTER_DOWN: michael@0: { michael@0: // Get the target content target (mousemove target == mouseover target) michael@0: nsCOMPtr targetElement = GetEventTargetContent(aMouseEvent); michael@0: if (!targetElement) { michael@0: // We're always over the document root, even if we're only michael@0: // over dead space in a page (whose frame is not associated with michael@0: // any content) or in print preview dead space michael@0: targetElement = mDocument->GetRootElement(); michael@0: } michael@0: if (targetElement) { michael@0: NotifyMouseOver(aMouseEvent, targetElement); michael@0: } michael@0: } michael@0: break; michael@0: case NS_POINTER_UP: michael@0: { michael@0: // Get the target content target (mousemove target == mouseover target) michael@0: nsCOMPtr targetElement = GetEventTargetContent(aMouseEvent); michael@0: if (!targetElement) { michael@0: // We're always over the document root, even if we're only michael@0: // over dead space in a page (whose frame is not associated with michael@0: // any content) or in print preview dead space michael@0: targetElement = mDocument->GetRootElement(); michael@0: } michael@0: if (targetElement) { michael@0: OverOutElementsWrapper* helper = GetWrapperByEventID(aMouseEvent); michael@0: if (helper) { michael@0: helper->mLastOverElement = targetElement; michael@0: } michael@0: NotifyMouseOut(aMouseEvent, nullptr); michael@0: } michael@0: } michael@0: break; michael@0: case NS_POINTER_LEAVE: michael@0: case NS_POINTER_CANCEL: michael@0: case NS_MOUSE_EXIT: michael@0: { michael@0: // This is actually the window mouse exit or pointer leave event. We're not moving michael@0: // into any new element. michael@0: michael@0: OverOutElementsWrapper* helper = GetWrapperByEventID(aMouseEvent); michael@0: if (helper->mLastOverFrame && michael@0: nsContentUtils::GetTopLevelWidget(aMouseEvent->widget) != michael@0: nsContentUtils::GetTopLevelWidget(helper->mLastOverFrame->GetNearestWidget())) { michael@0: // the Mouse/PointerOut event widget doesn't have same top widget with michael@0: // mLastOverFrame, it's a spurious event for mLastOverFrame michael@0: break; michael@0: } michael@0: michael@0: // Reset sLastRefPoint, so that we'll know not to report any michael@0: // movement the next time we re-enter the window. michael@0: sLastRefPoint = kInvalidRefPoint; michael@0: michael@0: NotifyMouseOut(aMouseEvent, nullptr); michael@0: } michael@0: break; michael@0: } michael@0: michael@0: // reset mCurretTargetContent to what it was michael@0: mCurrentTargetContent = targetBeforeEvent; michael@0: } michael@0: michael@0: OverOutElementsWrapper* michael@0: EventStateManager::GetWrapperByEventID(WidgetMouseEvent* aEvent) michael@0: { michael@0: WidgetPointerEvent* pointer = aEvent->AsPointerEvent(); michael@0: if (!pointer) { michael@0: MOZ_ASSERT(aEvent->AsMouseEvent() != nullptr); michael@0: if (!mMouseEnterLeaveHelper) { michael@0: mMouseEnterLeaveHelper = new OverOutElementsWrapper(); michael@0: } michael@0: return mMouseEnterLeaveHelper; michael@0: } michael@0: nsRefPtr helper; michael@0: if (!mPointersEnterLeaveHelper.Get(pointer->pointerId, getter_AddRefs(helper))) { michael@0: helper = new OverOutElementsWrapper(); michael@0: mPointersEnterLeaveHelper.Put(pointer->pointerId, helper); michael@0: } michael@0: michael@0: return helper; michael@0: } michael@0: michael@0: void michael@0: EventStateManager::SetPointerLock(nsIWidget* aWidget, michael@0: nsIContent* aElement) michael@0: { michael@0: // NOTE: aElement will be nullptr when unlocking. michael@0: sIsPointerLocked = !!aElement; michael@0: michael@0: if (!aWidget) { michael@0: return; michael@0: } michael@0: michael@0: // Reset mouse wheel transaction michael@0: WheelTransaction::EndTransaction(); michael@0: michael@0: // Deal with DnD events michael@0: nsCOMPtr dragService = michael@0: do_GetService("@mozilla.org/widget/dragservice;1"); michael@0: michael@0: if (sIsPointerLocked) { michael@0: // Store the last known ref point so we can reposition the pointer after unlock. michael@0: mPreLockPoint = sLastRefPoint; michael@0: michael@0: // Fire a synthetic mouse move to ensure event state is updated. We first michael@0: // set the mouse to the center of the window, so that the mouse event michael@0: // doesn't report any movement. michael@0: sLastRefPoint = GetWindowInnerRectCenter(aElement->OwnerDoc()->GetWindow(), michael@0: aWidget, michael@0: mPresContext); michael@0: aWidget->SynthesizeNativeMouseMove( michael@0: LayoutDeviceIntPoint::ToUntyped(sLastRefPoint) + aWidget->WidgetToScreenOffset()); michael@0: michael@0: // Retarget all events to this element via capture. michael@0: nsIPresShell::SetCapturingContent(aElement, CAPTURE_POINTERLOCK); michael@0: michael@0: // Suppress DnD michael@0: if (dragService) { michael@0: dragService->Suppress(); michael@0: } michael@0: } else { michael@0: // Unlocking, so return pointer to the original position by firing a michael@0: // synthetic mouse event. We first reset sLastRefPoint to its michael@0: // pre-pointerlock position, so that the synthetic mouse event reports michael@0: // no movement. michael@0: sLastRefPoint = mPreLockPoint; michael@0: aWidget->SynthesizeNativeMouseMove( michael@0: LayoutDeviceIntPoint::ToUntyped(mPreLockPoint) + aWidget->WidgetToScreenOffset()); michael@0: michael@0: // Don't retarget events to this element any more. michael@0: nsIPresShell::SetCapturingContent(nullptr, CAPTURE_POINTERLOCK); michael@0: michael@0: // Unsuppress DnD michael@0: if (dragService) { michael@0: dragService->Unsuppress(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: EventStateManager::GenerateDragDropEnterExit(nsPresContext* aPresContext, michael@0: WidgetDragEvent* aDragEvent) michael@0: { michael@0: //Hold onto old target content through the event and reset after. michael@0: nsCOMPtr targetBeforeEvent = mCurrentTargetContent; michael@0: michael@0: switch(aDragEvent->message) { michael@0: case NS_DRAGDROP_OVER: michael@0: { michael@0: // when dragging from one frame to another, events are fired in the michael@0: // order: dragexit, dragenter, dragleave michael@0: if (sLastDragOverFrame != mCurrentTarget) { michael@0: //We'll need the content, too, to check if it changed separately from the frames. michael@0: nsCOMPtr lastContent; michael@0: nsCOMPtr targetContent; michael@0: mCurrentTarget->GetContentForEvent(aDragEvent, michael@0: getter_AddRefs(targetContent)); michael@0: michael@0: if (sLastDragOverFrame) { michael@0: //The frame has changed but the content may not have. Check before dispatching to content michael@0: sLastDragOverFrame->GetContentForEvent(aDragEvent, michael@0: getter_AddRefs(lastContent)); michael@0: michael@0: FireDragEnterOrExit(sLastDragOverFrame->PresContext(), michael@0: aDragEvent, NS_DRAGDROP_EXIT_SYNTH, michael@0: targetContent, lastContent, sLastDragOverFrame); michael@0: } michael@0: michael@0: FireDragEnterOrExit(aPresContext, aDragEvent, NS_DRAGDROP_ENTER, michael@0: lastContent, targetContent, mCurrentTarget); michael@0: michael@0: if (sLastDragOverFrame) { michael@0: FireDragEnterOrExit(sLastDragOverFrame->PresContext(), michael@0: aDragEvent, NS_DRAGDROP_LEAVE_SYNTH, michael@0: targetContent, lastContent, sLastDragOverFrame); michael@0: } michael@0: michael@0: sLastDragOverFrame = mCurrentTarget; michael@0: } michael@0: } michael@0: break; michael@0: michael@0: case NS_DRAGDROP_EXIT: michael@0: { michael@0: //This is actually the window mouse exit event. michael@0: if (sLastDragOverFrame) { michael@0: nsCOMPtr lastContent; michael@0: sLastDragOverFrame->GetContentForEvent(aDragEvent, michael@0: getter_AddRefs(lastContent)); michael@0: michael@0: nsRefPtr lastDragOverFramePresContext = sLastDragOverFrame->PresContext(); michael@0: FireDragEnterOrExit(lastDragOverFramePresContext, michael@0: aDragEvent, NS_DRAGDROP_EXIT_SYNTH, michael@0: nullptr, lastContent, sLastDragOverFrame); michael@0: FireDragEnterOrExit(lastDragOverFramePresContext, michael@0: aDragEvent, NS_DRAGDROP_LEAVE_SYNTH, michael@0: nullptr, lastContent, sLastDragOverFrame); michael@0: michael@0: sLastDragOverFrame = nullptr; michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: michael@0: //reset mCurretTargetContent to what it was michael@0: mCurrentTargetContent = targetBeforeEvent; michael@0: michael@0: // Now flush all pending notifications, for better responsiveness. michael@0: FlushPendingEvents(aPresContext); michael@0: } michael@0: michael@0: void michael@0: EventStateManager::FireDragEnterOrExit(nsPresContext* aPresContext, michael@0: WidgetDragEvent* aDragEvent, michael@0: uint32_t aMsg, michael@0: nsIContent* aRelatedTarget, michael@0: nsIContent* aTargetContent, michael@0: nsWeakFrame& aTargetFrame) michael@0: { michael@0: nsEventStatus status = nsEventStatus_eIgnore; michael@0: WidgetDragEvent event(aDragEvent->mFlags.mIsTrusted, aMsg, michael@0: aDragEvent->widget); michael@0: event.refPoint = aDragEvent->refPoint; michael@0: event.modifiers = aDragEvent->modifiers; michael@0: event.buttons = aDragEvent->buttons; michael@0: event.relatedTarget = aRelatedTarget; michael@0: event.inputSource = aDragEvent->inputSource; michael@0: michael@0: mCurrentTargetContent = aTargetContent; michael@0: michael@0: if (aTargetContent != aRelatedTarget) { michael@0: //XXX This event should still go somewhere!! michael@0: if (aTargetContent) { michael@0: EventDispatcher::Dispatch(aTargetContent, aPresContext, &event, michael@0: nullptr, &status); michael@0: } michael@0: michael@0: // adjust the drag hover if the dragenter event was cancelled or this is a drag exit michael@0: if (status == nsEventStatus_eConsumeNoDefault || aMsg == NS_DRAGDROP_EXIT) michael@0: SetContentState((aMsg == NS_DRAGDROP_ENTER) ? aTargetContent : nullptr, michael@0: NS_EVENT_STATE_DRAGOVER); michael@0: michael@0: // collect any changes to moz cursor settings stored in the event's michael@0: // data transfer. michael@0: if (aMsg == NS_DRAGDROP_LEAVE_SYNTH || aMsg == NS_DRAGDROP_EXIT_SYNTH || michael@0: aMsg == NS_DRAGDROP_ENTER) michael@0: UpdateDragDataTransfer(&event); michael@0: } michael@0: michael@0: // Finally dispatch the event to the frame michael@0: if (aTargetFrame) michael@0: aTargetFrame->HandleEvent(aPresContext, &event, &status); michael@0: } michael@0: michael@0: void michael@0: EventStateManager::UpdateDragDataTransfer(WidgetDragEvent* dragEvent) michael@0: { michael@0: NS_ASSERTION(dragEvent, "drag event is null in UpdateDragDataTransfer!"); michael@0: if (!dragEvent->dataTransfer) michael@0: return; michael@0: michael@0: nsCOMPtr dragSession = nsContentUtils::GetDragSession(); michael@0: michael@0: if (dragSession) { michael@0: // the initial dataTransfer is the one from the dragstart event that michael@0: // was set on the dragSession when the drag began. michael@0: nsCOMPtr initialDataTransfer; michael@0: dragSession->GetDataTransfer(getter_AddRefs(initialDataTransfer)); michael@0: if (initialDataTransfer) { michael@0: // retrieve the current moz cursor setting and save it. michael@0: nsAutoString mozCursor; michael@0: dragEvent->dataTransfer->GetMozCursor(mozCursor); michael@0: initialDataTransfer->SetMozCursor(mozCursor); michael@0: } michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: EventStateManager::SetClickCount(nsPresContext* aPresContext, michael@0: WidgetMouseEvent* aEvent, michael@0: nsEventStatus* aStatus) michael@0: { michael@0: nsCOMPtr mouseContent; michael@0: nsIContent* mouseContentParent = nullptr; michael@0: mCurrentTarget->GetContentForEvent(aEvent, getter_AddRefs(mouseContent)); michael@0: if (mouseContent) { michael@0: if (mouseContent->IsNodeOfType(nsINode::eTEXT)) { michael@0: mouseContent = mouseContent->GetParent(); michael@0: } michael@0: if (mouseContent && mouseContent->IsRootOfNativeAnonymousSubtree()) { michael@0: mouseContentParent = mouseContent->GetParent(); michael@0: } michael@0: } michael@0: michael@0: switch (aEvent->button) { michael@0: case WidgetMouseEvent::eLeftButton: michael@0: if (aEvent->message == NS_MOUSE_BUTTON_DOWN) { michael@0: mLastLeftMouseDownContent = mouseContent; michael@0: mLastLeftMouseDownContentParent = mouseContentParent; michael@0: } else if (aEvent->message == NS_MOUSE_BUTTON_UP) { michael@0: if (mLastLeftMouseDownContent == mouseContent || michael@0: mLastLeftMouseDownContentParent == mouseContent || michael@0: mLastLeftMouseDownContent == mouseContentParent) { michael@0: aEvent->clickCount = mLClickCount; michael@0: mLClickCount = 0; michael@0: } else { michael@0: aEvent->clickCount = 0; michael@0: } michael@0: mLastLeftMouseDownContent = nullptr; michael@0: mLastLeftMouseDownContentParent = nullptr; michael@0: } michael@0: break; michael@0: michael@0: case WidgetMouseEvent::eMiddleButton: michael@0: if (aEvent->message == NS_MOUSE_BUTTON_DOWN) { michael@0: mLastMiddleMouseDownContent = mouseContent; michael@0: mLastMiddleMouseDownContentParent = mouseContentParent; michael@0: } else if (aEvent->message == NS_MOUSE_BUTTON_UP) { michael@0: if (mLastMiddleMouseDownContent == mouseContent || michael@0: mLastMiddleMouseDownContentParent == mouseContent || michael@0: mLastMiddleMouseDownContent == mouseContentParent) { michael@0: aEvent->clickCount = mMClickCount; michael@0: mMClickCount = 0; michael@0: } else { michael@0: aEvent->clickCount = 0; michael@0: } michael@0: mLastMiddleMouseDownContent = nullptr; michael@0: mLastMiddleMouseDownContentParent = nullptr; michael@0: } michael@0: break; michael@0: michael@0: case WidgetMouseEvent::eRightButton: michael@0: if (aEvent->message == NS_MOUSE_BUTTON_DOWN) { michael@0: mLastRightMouseDownContent = mouseContent; michael@0: mLastRightMouseDownContentParent = mouseContentParent; michael@0: } else if (aEvent->message == NS_MOUSE_BUTTON_UP) { michael@0: if (mLastRightMouseDownContent == mouseContent || michael@0: mLastRightMouseDownContentParent == mouseContent || michael@0: mLastRightMouseDownContent == mouseContentParent) { michael@0: aEvent->clickCount = mRClickCount; michael@0: mRClickCount = 0; michael@0: } else { michael@0: aEvent->clickCount = 0; michael@0: } michael@0: mLastRightMouseDownContent = nullptr; michael@0: mLastRightMouseDownContentParent = nullptr; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: EventStateManager::CheckForAndDispatchClick(nsPresContext* aPresContext, michael@0: WidgetMouseEvent* aEvent, michael@0: nsEventStatus* aStatus) michael@0: { michael@0: nsresult ret = NS_OK; michael@0: michael@0: //If mouse is still over same element, clickcount will be > 1. michael@0: //If it has moved it will be zero, so no click. michael@0: if (0 != aEvent->clickCount) { michael@0: //Check that the window isn't disabled before firing a click michael@0: //(see bug 366544). michael@0: if (aEvent->widget && !aEvent->widget->IsEnabled()) { michael@0: return ret; michael@0: } michael@0: //fire click michael@0: bool notDispatchToContents = michael@0: (aEvent->button == WidgetMouseEvent::eMiddleButton || michael@0: aEvent->button == WidgetMouseEvent::eRightButton); michael@0: michael@0: WidgetMouseEvent event(aEvent->mFlags.mIsTrusted, NS_MOUSE_CLICK, michael@0: aEvent->widget, WidgetMouseEvent::eReal); michael@0: event.refPoint = aEvent->refPoint; michael@0: event.clickCount = aEvent->clickCount; michael@0: event.modifiers = aEvent->modifiers; michael@0: event.buttons = aEvent->buttons; michael@0: event.time = aEvent->time; michael@0: event.mFlags.mNoContentDispatch = notDispatchToContents; michael@0: event.button = aEvent->button; michael@0: event.inputSource = aEvent->inputSource; michael@0: michael@0: nsCOMPtr presShell = mPresContext->GetPresShell(); michael@0: if (presShell) { michael@0: nsCOMPtr mouseContent = GetEventTargetContent(aEvent); michael@0: if (!mouseContent && !mCurrentTarget) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: // HandleEvent clears out mCurrentTarget which we might need again michael@0: nsWeakFrame currentTarget = mCurrentTarget; michael@0: ret = presShell->HandleEventWithTarget(&event, currentTarget, michael@0: mouseContent, aStatus); michael@0: if (NS_SUCCEEDED(ret) && aEvent->clickCount == 2) { michael@0: //fire double click michael@0: WidgetMouseEvent event2(aEvent->mFlags.mIsTrusted, NS_MOUSE_DOUBLECLICK, michael@0: aEvent->widget, WidgetMouseEvent::eReal); michael@0: event2.refPoint = aEvent->refPoint; michael@0: event2.clickCount = aEvent->clickCount; michael@0: event2.modifiers = aEvent->modifiers; michael@0: event2.buttons = aEvent->buttons; michael@0: event2.mFlags.mNoContentDispatch = notDispatchToContents; michael@0: event2.button = aEvent->button; michael@0: event2.inputSource = aEvent->inputSource; michael@0: michael@0: ret = presShell->HandleEventWithTarget(&event2, currentTarget, michael@0: mouseContent, aStatus); michael@0: } michael@0: } michael@0: } michael@0: return ret; michael@0: } michael@0: michael@0: nsIFrame* michael@0: EventStateManager::GetEventTarget() michael@0: { michael@0: nsIPresShell *shell; michael@0: if (mCurrentTarget || michael@0: !mPresContext || michael@0: !(shell = mPresContext->GetPresShell())) { michael@0: return mCurrentTarget; michael@0: } michael@0: michael@0: if (mCurrentTargetContent) { michael@0: mCurrentTarget = mPresContext->GetPrimaryFrameFor(mCurrentTargetContent); michael@0: if (mCurrentTarget) { michael@0: return mCurrentTarget; michael@0: } michael@0: } michael@0: michael@0: nsIFrame* frame = shell->GetEventTargetFrame(); michael@0: return (mCurrentTarget = frame); michael@0: } michael@0: michael@0: already_AddRefed michael@0: EventStateManager::GetEventTargetContent(WidgetEvent* aEvent) michael@0: { michael@0: if (aEvent && michael@0: (aEvent->message == NS_FOCUS_CONTENT || michael@0: aEvent->message == NS_BLUR_CONTENT)) { michael@0: nsCOMPtr content = GetFocusedContent(); michael@0: return content.forget(); michael@0: } michael@0: michael@0: if (mCurrentTargetContent) { michael@0: nsCOMPtr content = mCurrentTargetContent; michael@0: return content.forget(); michael@0: } michael@0: michael@0: nsCOMPtr content; michael@0: michael@0: nsIPresShell *presShell = mPresContext->GetPresShell(); michael@0: if (presShell) { michael@0: content = presShell->GetEventTargetContent(aEvent); michael@0: } michael@0: michael@0: // Some events here may set mCurrentTarget but not set the corresponding michael@0: // event target in the PresShell. michael@0: if (!content && mCurrentTarget) { michael@0: mCurrentTarget->GetContentForEvent(aEvent, getter_AddRefs(content)); michael@0: } michael@0: michael@0: return content.forget(); michael@0: } michael@0: michael@0: static Element* michael@0: GetLabelTarget(nsIContent* aPossibleLabel) michael@0: { michael@0: mozilla::dom::HTMLLabelElement* label = michael@0: mozilla::dom::HTMLLabelElement::FromContent(aPossibleLabel); michael@0: if (!label) michael@0: return nullptr; michael@0: michael@0: return label->GetLabeledElement(); michael@0: } michael@0: michael@0: static nsIContent* FindCommonAncestor(nsIContent *aNode1, nsIContent *aNode2) michael@0: { michael@0: // Find closest common ancestor michael@0: if (aNode1 && aNode2) { michael@0: // Find the nearest common ancestor by counting the distance to the michael@0: // root and then walking up again, in pairs. michael@0: int32_t offset = 0; michael@0: nsIContent *anc1 = aNode1; michael@0: for (;;) { michael@0: ++offset; michael@0: nsIContent* parent = anc1->GetParent(); michael@0: if (!parent) michael@0: break; michael@0: anc1 = parent; michael@0: } michael@0: nsIContent *anc2 = aNode2; michael@0: for (;;) { michael@0: --offset; michael@0: nsIContent* parent = anc2->GetParent(); michael@0: if (!parent) michael@0: break; michael@0: anc2 = parent; michael@0: } michael@0: if (anc1 == anc2) { michael@0: anc1 = aNode1; michael@0: anc2 = aNode2; michael@0: while (offset > 0) { michael@0: anc1 = anc1->GetParent(); michael@0: --offset; michael@0: } michael@0: while (offset < 0) { michael@0: anc2 = anc2->GetParent(); michael@0: ++offset; michael@0: } michael@0: while (anc1 != anc2) { michael@0: anc1 = anc1->GetParent(); michael@0: anc2 = anc2->GetParent(); michael@0: } michael@0: return anc1; michael@0: } michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: static Element* michael@0: GetParentElement(Element* aElement) michael@0: { michael@0: nsIContent* p = aElement->GetParent(); michael@0: return (p && p->IsElement()) ? p->AsElement() : nullptr; michael@0: } michael@0: michael@0: /* static */ michael@0: void michael@0: EventStateManager::SetFullScreenState(Element* aElement, bool aIsFullScreen) michael@0: { michael@0: DoStateChange(aElement, NS_EVENT_STATE_FULL_SCREEN, aIsFullScreen); michael@0: Element* ancestor = aElement; michael@0: while ((ancestor = GetParentElement(ancestor))) { michael@0: DoStateChange(ancestor, NS_EVENT_STATE_FULL_SCREEN_ANCESTOR, aIsFullScreen); michael@0: } michael@0: } michael@0: michael@0: /* static */ michael@0: inline void michael@0: EventStateManager::DoStateChange(Element* aElement, EventStates aState, michael@0: bool aAddState) michael@0: { michael@0: if (aAddState) { michael@0: aElement->AddStates(aState); michael@0: } else { michael@0: aElement->RemoveStates(aState); michael@0: } michael@0: } michael@0: michael@0: /* static */ michael@0: inline void michael@0: EventStateManager::DoStateChange(nsIContent* aContent, EventStates aState, michael@0: bool aStateAdded) michael@0: { michael@0: if (aContent->IsElement()) { michael@0: DoStateChange(aContent->AsElement(), aState, aStateAdded); michael@0: } michael@0: } michael@0: michael@0: /* static */ michael@0: void michael@0: EventStateManager::UpdateAncestorState(nsIContent* aStartNode, michael@0: nsIContent* aStopBefore, michael@0: EventStates aState, michael@0: bool aAddState) michael@0: { michael@0: for (; aStartNode && aStartNode != aStopBefore; michael@0: aStartNode = aStartNode->GetParent()) { michael@0: // We might be starting with a non-element (e.g. a text node) and michael@0: // if someone is doing something weird might be ending with a michael@0: // non-element too (e.g. a document fragment) michael@0: if (!aStartNode->IsElement()) { michael@0: continue; michael@0: } michael@0: Element* element = aStartNode->AsElement(); michael@0: DoStateChange(element, aState, aAddState); michael@0: Element* labelTarget = GetLabelTarget(element); michael@0: if (labelTarget) { michael@0: DoStateChange(labelTarget, aState, aAddState); michael@0: } michael@0: } michael@0: michael@0: if (aAddState) { michael@0: // We might be in a situation where a node was in hover both michael@0: // because it was hovered and because the label for it was michael@0: // hovered, and while we stopped hovering the node the label is michael@0: // still hovered. Or we might have had two nested labels for the michael@0: // same node, and while one is no longer hovered the other still michael@0: // is. In that situation, the label that's still hovered will be michael@0: // aStopBefore or some ancestor of it, and the call we just made michael@0: // to UpdateAncestorState with aAddState = false would have michael@0: // removed the hover state from the node. But the node should michael@0: // still be in hover state. To handle this situation we need to michael@0: // keep walking up the tree and any time we find a label mark its michael@0: // corresponding node as still in our state. michael@0: for ( ; aStartNode; aStartNode = aStartNode->GetParent()) { michael@0: if (!aStartNode->IsElement()) { michael@0: continue; michael@0: } michael@0: michael@0: Element* labelTarget = GetLabelTarget(aStartNode->AsElement()); michael@0: if (labelTarget && !labelTarget->State().HasState(aState)) { michael@0: DoStateChange(labelTarget, aState, true); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool michael@0: EventStateManager::SetContentState(nsIContent* aContent, EventStates aState) michael@0: { michael@0: // We manage 4 states here: ACTIVE, HOVER, DRAGOVER, URLTARGET michael@0: // The input must be exactly one of them. michael@0: NS_PRECONDITION(aState == NS_EVENT_STATE_ACTIVE || michael@0: aState == NS_EVENT_STATE_HOVER || michael@0: aState == NS_EVENT_STATE_DRAGOVER || michael@0: aState == NS_EVENT_STATE_URLTARGET, michael@0: "Unexpected state"); michael@0: michael@0: nsCOMPtr notifyContent1; michael@0: nsCOMPtr notifyContent2; michael@0: bool updateAncestors; michael@0: michael@0: if (aState == NS_EVENT_STATE_HOVER || aState == NS_EVENT_STATE_ACTIVE) { michael@0: // Hover and active are hierarchical michael@0: updateAncestors = true; michael@0: michael@0: // check to see that this state is allowed by style. Check dragover too? michael@0: // XXX Is this even what we want? michael@0: if (mCurrentTarget) michael@0: { michael@0: const nsStyleUserInterface* ui = mCurrentTarget->StyleUserInterface(); michael@0: if (ui->mUserInput == NS_STYLE_USER_INPUT_NONE) michael@0: return false; michael@0: } michael@0: michael@0: if (aState == NS_EVENT_STATE_ACTIVE) { michael@0: if (aContent != mActiveContent) { michael@0: notifyContent1 = aContent; michael@0: notifyContent2 = mActiveContent; michael@0: mActiveContent = aContent; michael@0: } michael@0: } else { michael@0: NS_ASSERTION(aState == NS_EVENT_STATE_HOVER, "How did that happen?"); michael@0: nsIContent* newHover; michael@0: michael@0: if (mPresContext->IsDynamic()) { michael@0: newHover = aContent; michael@0: } else { michael@0: NS_ASSERTION(!aContent || michael@0: aContent->GetCurrentDoc() == mPresContext->PresShell()->GetDocument(), michael@0: "Unexpected document"); michael@0: nsIFrame *frame = aContent ? aContent->GetPrimaryFrame() : nullptr; michael@0: if (frame && nsLayoutUtils::IsViewportScrollbarFrame(frame)) { michael@0: // The scrollbars of viewport should not ignore the hover state. michael@0: // Because they are *not* the content of the web page. michael@0: newHover = aContent; michael@0: } else { michael@0: // All contents of the web page should ignore the hover state. michael@0: newHover = nullptr; michael@0: } michael@0: } michael@0: michael@0: if (newHover != mHoverContent) { michael@0: notifyContent1 = newHover; michael@0: notifyContent2 = mHoverContent; michael@0: mHoverContent = newHover; michael@0: } michael@0: } michael@0: } else { michael@0: updateAncestors = false; michael@0: if (aState == NS_EVENT_STATE_DRAGOVER) { michael@0: if (aContent != sDragOverContent) { michael@0: notifyContent1 = aContent; michael@0: notifyContent2 = sDragOverContent; michael@0: sDragOverContent = aContent; michael@0: } michael@0: } else if (aState == NS_EVENT_STATE_URLTARGET) { michael@0: if (aContent != mURLTargetContent) { michael@0: notifyContent1 = aContent; michael@0: notifyContent2 = mURLTargetContent; michael@0: mURLTargetContent = aContent; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // We need to keep track of which of notifyContent1 and notifyContent2 is michael@0: // getting the state set and which is getting it unset. If both are michael@0: // non-null, then notifyContent1 is having the state set and notifyContent2 michael@0: // is having it unset. But if one of them is null, we need to keep track of michael@0: // the right thing for notifyContent1 explicitly. michael@0: bool content1StateSet = true; michael@0: if (!notifyContent1) { michael@0: // This is ok because FindCommonAncestor wouldn't find anything michael@0: // anyway if notifyContent1 is null. michael@0: notifyContent1 = notifyContent2; michael@0: notifyContent2 = nullptr; michael@0: content1StateSet = false; michael@0: } michael@0: michael@0: if (notifyContent1 && mPresContext) { michael@0: EnsureDocument(mPresContext); michael@0: if (mDocument) { michael@0: nsAutoScriptBlocker scriptBlocker; michael@0: michael@0: if (updateAncestors) { michael@0: nsCOMPtr commonAncestor = michael@0: FindCommonAncestor(notifyContent1, notifyContent2); michael@0: if (notifyContent2) { michael@0: // It's very important to first notify the state removal and michael@0: // then the state addition, because due to labels it's michael@0: // possible that we're removing state from some element but michael@0: // then adding it again (say because mHoverContent changed michael@0: // from a control to its label). michael@0: UpdateAncestorState(notifyContent2, commonAncestor, aState, false); michael@0: } michael@0: UpdateAncestorState(notifyContent1, commonAncestor, aState, michael@0: content1StateSet); michael@0: } else { michael@0: if (notifyContent2) { michael@0: DoStateChange(notifyContent2, aState, false); michael@0: } michael@0: DoStateChange(notifyContent1, aState, content1StateSet); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: PLDHashOperator michael@0: EventStateManager::ResetLastOverForContent( michael@0: const uint32_t& aIdx, michael@0: nsRefPtr& aElemWrapper, michael@0: void* aClosure) michael@0: { michael@0: nsIContent* content = static_cast(aClosure); michael@0: if (aElemWrapper && aElemWrapper->mLastOverElement && michael@0: nsContentUtils::ContentIsDescendantOf(aElemWrapper->mLastOverElement, content)) { michael@0: aElemWrapper->mLastOverElement = nullptr; michael@0: } michael@0: michael@0: return PL_DHASH_NEXT; michael@0: } michael@0: michael@0: void michael@0: EventStateManager::ContentRemoved(nsIDocument* aDocument, nsIContent* aContent) michael@0: { michael@0: /* michael@0: * Anchor and area elements when focused or hovered might make the UI to show michael@0: * the current link. We want to make sure that the UI gets informed when they michael@0: * are actually removed from the DOM. michael@0: */ michael@0: if (aContent->IsHTML() && michael@0: (aContent->Tag() == nsGkAtoms::a || aContent->Tag() == nsGkAtoms::area) && michael@0: (aContent->AsElement()->State().HasAtLeastOneOfStates(NS_EVENT_STATE_FOCUS | michael@0: NS_EVENT_STATE_HOVER))) { michael@0: nsGenericHTMLElement* element = static_cast(aContent); michael@0: element->LeaveLink(element->GetPresContext()); michael@0: } michael@0: michael@0: IMEStateManager::OnRemoveContent(mPresContext, aContent); michael@0: michael@0: // inform the focus manager that the content is being removed. If this michael@0: // content is focused, the focus will be removed without firing events. michael@0: nsFocusManager* fm = nsFocusManager::GetFocusManager(); michael@0: if (fm) michael@0: fm->ContentRemoved(aDocument, aContent); michael@0: michael@0: if (mHoverContent && michael@0: nsContentUtils::ContentIsDescendantOf(mHoverContent, aContent)) { michael@0: // Since hover is hierarchical, set the current hover to the michael@0: // content's parent node. michael@0: SetContentState(aContent->GetParent(), NS_EVENT_STATE_HOVER); michael@0: } michael@0: michael@0: if (mActiveContent && michael@0: nsContentUtils::ContentIsDescendantOf(mActiveContent, aContent)) { michael@0: // Active is hierarchical, so set the current active to the michael@0: // content's parent node. michael@0: SetContentState(aContent->GetParent(), NS_EVENT_STATE_ACTIVE); michael@0: } michael@0: michael@0: if (sDragOverContent && michael@0: sDragOverContent->OwnerDoc() == aContent->OwnerDoc() && michael@0: nsContentUtils::ContentIsDescendantOf(sDragOverContent, aContent)) { michael@0: sDragOverContent = nullptr; michael@0: } michael@0: michael@0: // See bug 292146 for why we want to null this out michael@0: ResetLastOverForContent(0, mMouseEnterLeaveHelper, aContent); michael@0: mPointersEnterLeaveHelper.Enumerate( michael@0: &EventStateManager::ResetLastOverForContent, aContent); michael@0: } michael@0: michael@0: bool michael@0: EventStateManager::EventStatusOK(WidgetGUIEvent* aEvent) michael@0: { michael@0: return !(aEvent->message == NS_MOUSE_BUTTON_DOWN && michael@0: aEvent->AsMouseEvent()->button == WidgetMouseEvent::eLeftButton && michael@0: !sNormalLMouseEventInProcess); michael@0: } michael@0: michael@0: //------------------------------------------- michael@0: // Access Key Registration michael@0: //------------------------------------------- michael@0: void michael@0: EventStateManager::RegisterAccessKey(nsIContent* aContent, uint32_t aKey) michael@0: { michael@0: if (aContent && mAccessKeys.IndexOf(aContent) == -1) michael@0: mAccessKeys.AppendObject(aContent); michael@0: } michael@0: michael@0: void michael@0: EventStateManager::UnregisterAccessKey(nsIContent* aContent, uint32_t aKey) michael@0: { michael@0: if (aContent) michael@0: mAccessKeys.RemoveObject(aContent); michael@0: } michael@0: michael@0: uint32_t michael@0: EventStateManager::GetRegisteredAccessKey(nsIContent* aContent) michael@0: { michael@0: MOZ_ASSERT(aContent); michael@0: michael@0: if (mAccessKeys.IndexOf(aContent) == -1) michael@0: return 0; michael@0: michael@0: nsAutoString accessKey; michael@0: aContent->GetAttr(kNameSpaceID_None, nsGkAtoms::accesskey, accessKey); michael@0: return accessKey.First(); michael@0: } michael@0: michael@0: void michael@0: EventStateManager::EnsureDocument(nsPresContext* aPresContext) michael@0: { michael@0: if (!mDocument) michael@0: mDocument = aPresContext->Document(); michael@0: } michael@0: michael@0: void michael@0: EventStateManager::FlushPendingEvents(nsPresContext* aPresContext) michael@0: { michael@0: NS_PRECONDITION(nullptr != aPresContext, "nullptr ptr"); michael@0: nsIPresShell *shell = aPresContext->GetPresShell(); michael@0: if (shell) { michael@0: shell->FlushPendingNotifications(Flush_InterruptibleLayout); michael@0: } michael@0: } michael@0: michael@0: nsIContent* michael@0: EventStateManager::GetFocusedContent() michael@0: { michael@0: nsIFocusManager* fm = nsFocusManager::GetFocusManager(); michael@0: if (!fm || !mDocument) michael@0: return nullptr; michael@0: michael@0: nsCOMPtr focusedWindow; michael@0: return nsFocusManager::GetFocusedDescendant(mDocument->GetWindow(), false, michael@0: getter_AddRefs(focusedWindow)); michael@0: } michael@0: michael@0: //------------------------------------------------------- michael@0: // Return true if the docshell is visible michael@0: michael@0: bool michael@0: EventStateManager::IsShellVisible(nsIDocShell* aShell) michael@0: { michael@0: NS_ASSERTION(aShell, "docshell is null"); michael@0: michael@0: nsCOMPtr basewin = do_QueryInterface(aShell); michael@0: if (!basewin) michael@0: return true; michael@0: michael@0: bool isVisible = true; michael@0: basewin->GetVisibility(&isVisible); michael@0: michael@0: // We should be doing some additional checks here so that michael@0: // we don't tab into hidden tabs of tabbrowser. -bryner michael@0: michael@0: return isVisible; michael@0: } michael@0: michael@0: nsresult michael@0: EventStateManager::DoContentCommandEvent(WidgetContentCommandEvent* aEvent) michael@0: { michael@0: EnsureDocument(mPresContext); michael@0: NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE); michael@0: nsCOMPtr window(mDocument->GetWindow()); michael@0: NS_ENSURE_TRUE(window, NS_ERROR_FAILURE); michael@0: michael@0: nsCOMPtr root = window->GetTopWindowRoot(); michael@0: NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); michael@0: const char* cmd; michael@0: switch (aEvent->message) { michael@0: case NS_CONTENT_COMMAND_CUT: michael@0: cmd = "cmd_cut"; michael@0: break; michael@0: case NS_CONTENT_COMMAND_COPY: michael@0: cmd = "cmd_copy"; michael@0: break; michael@0: case NS_CONTENT_COMMAND_PASTE: michael@0: cmd = "cmd_paste"; michael@0: break; michael@0: case NS_CONTENT_COMMAND_DELETE: michael@0: cmd = "cmd_delete"; michael@0: break; michael@0: case NS_CONTENT_COMMAND_UNDO: michael@0: cmd = "cmd_undo"; michael@0: break; michael@0: case NS_CONTENT_COMMAND_REDO: michael@0: cmd = "cmd_redo"; michael@0: break; michael@0: case NS_CONTENT_COMMAND_PASTE_TRANSFERABLE: michael@0: cmd = "cmd_pasteTransferable"; michael@0: break; michael@0: default: michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: nsCOMPtr controller; michael@0: nsresult rv = root->GetControllerForCommand(cmd, getter_AddRefs(controller)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (!controller) { michael@0: // When GetControllerForCommand succeeded but there is no controller, the michael@0: // command isn't supported. michael@0: aEvent->mIsEnabled = false; michael@0: } else { michael@0: bool canDoIt; michael@0: rv = controller->IsCommandEnabled(cmd, &canDoIt); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: aEvent->mIsEnabled = canDoIt; michael@0: if (canDoIt && !aEvent->mOnlyEnabledCheck) { michael@0: switch (aEvent->message) { michael@0: case NS_CONTENT_COMMAND_PASTE_TRANSFERABLE: { michael@0: nsCOMPtr commandController = do_QueryInterface(controller); michael@0: NS_ENSURE_STATE(commandController); michael@0: michael@0: nsCOMPtr params = do_CreateInstance("@mozilla.org/embedcomp/command-params;1", &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = params->SetISupportsValue("transferable", aEvent->mTransferable); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = commandController->DoCommandWithParams(cmd, params); michael@0: break; michael@0: } michael@0: michael@0: default: michael@0: rv = controller->DoCommand(cmd); michael@0: break; michael@0: } michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: } michael@0: aEvent->mSucceeded = true; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: EventStateManager::DoContentCommandScrollEvent( michael@0: WidgetContentCommandEvent* aEvent) michael@0: { michael@0: NS_ENSURE_TRUE(mPresContext, NS_ERROR_NOT_AVAILABLE); michael@0: nsIPresShell* ps = mPresContext->GetPresShell(); michael@0: NS_ENSURE_TRUE(ps, NS_ERROR_NOT_AVAILABLE); michael@0: NS_ENSURE_TRUE(aEvent->mScroll.mAmount != 0, NS_ERROR_INVALID_ARG); michael@0: michael@0: nsIScrollableFrame::ScrollUnit scrollUnit; michael@0: switch (aEvent->mScroll.mUnit) { michael@0: case WidgetContentCommandEvent::eCmdScrollUnit_Line: michael@0: scrollUnit = nsIScrollableFrame::LINES; michael@0: break; michael@0: case WidgetContentCommandEvent::eCmdScrollUnit_Page: michael@0: scrollUnit = nsIScrollableFrame::PAGES; michael@0: break; michael@0: case WidgetContentCommandEvent::eCmdScrollUnit_Whole: michael@0: scrollUnit = nsIScrollableFrame::WHOLE; michael@0: break; michael@0: default: michael@0: return NS_ERROR_INVALID_ARG; michael@0: } michael@0: michael@0: aEvent->mSucceeded = true; michael@0: michael@0: nsIScrollableFrame* sf = michael@0: ps->GetFrameToScrollAsScrollable(nsIPresShell::eEither); michael@0: aEvent->mIsEnabled = sf ? michael@0: (aEvent->mScroll.mIsHorizontal ? michael@0: WheelHandlingUtils::CanScrollOn(sf, aEvent->mScroll.mAmount, 0) : michael@0: WheelHandlingUtils::CanScrollOn(sf, 0, aEvent->mScroll.mAmount)) : false; michael@0: michael@0: if (!aEvent->mIsEnabled || aEvent->mOnlyEnabledCheck) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsIntPoint pt(0, 0); michael@0: if (aEvent->mScroll.mIsHorizontal) { michael@0: pt.x = aEvent->mScroll.mAmount; michael@0: } else { michael@0: pt.y = aEvent->mScroll.mAmount; michael@0: } michael@0: michael@0: // The caller may want synchronous scrolling. michael@0: sf->ScrollBy(pt, scrollUnit, nsIScrollableFrame::INSTANT); michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: EventStateManager::DoQuerySelectedText(WidgetQueryContentEvent* aEvent) michael@0: { michael@0: if (RemoteQueryContentEvent(aEvent)) { michael@0: return; michael@0: } michael@0: ContentEventHandler handler(mPresContext); michael@0: handler.OnQuerySelectedText(aEvent); michael@0: } michael@0: michael@0: void michael@0: EventStateManager::SetActiveManager(EventStateManager* aNewESM, michael@0: nsIContent* aContent) michael@0: { michael@0: if (sActiveESM && aNewESM != sActiveESM) { michael@0: sActiveESM->SetContentState(nullptr, NS_EVENT_STATE_ACTIVE); michael@0: } michael@0: sActiveESM = aNewESM; michael@0: if (sActiveESM && aContent) { michael@0: sActiveESM->SetContentState(aContent, NS_EVENT_STATE_ACTIVE); michael@0: } michael@0: } michael@0: michael@0: void michael@0: EventStateManager::ClearGlobalActiveContent(EventStateManager* aClearer) michael@0: { michael@0: if (aClearer) { michael@0: aClearer->SetContentState(nullptr, NS_EVENT_STATE_ACTIVE); michael@0: if (sDragOverContent) { michael@0: aClearer->SetContentState(nullptr, NS_EVENT_STATE_DRAGOVER); michael@0: } michael@0: } michael@0: if (sActiveESM && aClearer != sActiveESM) { michael@0: sActiveESM->SetContentState(nullptr, NS_EVENT_STATE_ACTIVE); michael@0: } michael@0: sActiveESM = nullptr; michael@0: } michael@0: michael@0: /******************************************************************/ michael@0: /* mozilla::EventStateManager::DeltaAccumulator */ michael@0: /******************************************************************/ michael@0: michael@0: void michael@0: EventStateManager::DeltaAccumulator::InitLineOrPageDelta( michael@0: nsIFrame* aTargetFrame, michael@0: EventStateManager* aESM, michael@0: WidgetWheelEvent* aEvent) michael@0: { michael@0: MOZ_ASSERT(aESM); michael@0: MOZ_ASSERT(aEvent); michael@0: michael@0: // Reset if the previous wheel event is too old. michael@0: if (!mLastTime.IsNull()) { michael@0: TimeDuration duration = TimeStamp::Now() - mLastTime; michael@0: if (duration.ToMilliseconds() > WheelTransaction::GetTimeoutTime()) { michael@0: Reset(); michael@0: } michael@0: } michael@0: // If we have accumulated delta, we may need to reset it. michael@0: if (IsInTransaction()) { michael@0: // If wheel event type is changed, reset the values. michael@0: if (mHandlingDeltaMode != aEvent->deltaMode || michael@0: mHandlingPixelOnlyDevice != aEvent->isPixelOnlyDevice) { michael@0: Reset(); michael@0: } else { michael@0: // If the delta direction is changed, we should reset only the michael@0: // accumulated values. michael@0: if (mX && aEvent->deltaX && ((aEvent->deltaX > 0.0) != (mX > 0.0))) { michael@0: mX = mPendingScrollAmountX = 0.0; michael@0: } michael@0: if (mY && aEvent->deltaY && ((aEvent->deltaY > 0.0) != (mY > 0.0))) { michael@0: mY = mPendingScrollAmountY = 0.0; michael@0: } michael@0: } michael@0: } michael@0: michael@0: mHandlingDeltaMode = aEvent->deltaMode; michael@0: mHandlingPixelOnlyDevice = aEvent->isPixelOnlyDevice; michael@0: michael@0: // If it's handling neither pixel scroll mode for pixel only device nor michael@0: // delta values multiplied by prefs, we must not modify lineOrPageDelta michael@0: // values. michael@0: if (!(mHandlingDeltaMode == nsIDOMWheelEvent::DOM_DELTA_PIXEL && michael@0: mHandlingPixelOnlyDevice) && michael@0: !EventStateManager::WheelPrefs::GetInstance()-> michael@0: NeedToComputeLineOrPageDelta(aEvent)) { michael@0: // Set the delta values to mX and mY. They would be used when above block michael@0: // resets mX/mY/mPendingScrollAmountX/mPendingScrollAmountY if the direction michael@0: // is changed. michael@0: // NOTE: We shouldn't accumulate the delta values, it might could cause michael@0: // overflow even though it's not a realistic situation. michael@0: if (aEvent->deltaX) { michael@0: mX = aEvent->deltaX; michael@0: } michael@0: if (aEvent->deltaY) { michael@0: mY = aEvent->deltaY; michael@0: } michael@0: mLastTime = TimeStamp::Now(); michael@0: return; michael@0: } michael@0: michael@0: mX += aEvent->deltaX; michael@0: mY += aEvent->deltaY; michael@0: michael@0: if (mHandlingDeltaMode == nsIDOMWheelEvent::DOM_DELTA_PIXEL) { michael@0: // Records pixel delta values and init lineOrPageDeltaX and michael@0: // lineOrPageDeltaY for wheel events which are caused by pixel only michael@0: // devices. Ignore mouse wheel transaction for computing this. The michael@0: // lineOrPageDelta values will be used by dispatching legacy michael@0: // NS_MOUSE_SCROLL_EVENT (DOMMouseScroll) but not be used for scrolling michael@0: // of default action. The transaction should be used only for the default michael@0: // action. michael@0: nsIScrollableFrame* scrollTarget = michael@0: aESM->ComputeScrollTarget(aTargetFrame, aEvent, michael@0: COMPUTE_LEGACY_MOUSE_SCROLL_EVENT_TARGET); michael@0: nsIFrame* frame = do_QueryFrame(scrollTarget); michael@0: nsPresContext* pc = michael@0: frame ? frame->PresContext() : aTargetFrame->PresContext(); michael@0: nsSize scrollAmount = aESM->GetScrollAmount(pc, aEvent, scrollTarget); michael@0: nsIntSize scrollAmountInCSSPixels( michael@0: nsPresContext::AppUnitsToIntCSSPixels(scrollAmount.width), michael@0: nsPresContext::AppUnitsToIntCSSPixels(scrollAmount.height)); michael@0: michael@0: aEvent->lineOrPageDeltaX = RoundDown(mX) / scrollAmountInCSSPixels.width; michael@0: aEvent->lineOrPageDeltaY = RoundDown(mY) / scrollAmountInCSSPixels.height; michael@0: michael@0: mX -= aEvent->lineOrPageDeltaX * scrollAmountInCSSPixels.width; michael@0: mY -= aEvent->lineOrPageDeltaY * scrollAmountInCSSPixels.height; michael@0: } else { michael@0: aEvent->lineOrPageDeltaX = RoundDown(mX); michael@0: aEvent->lineOrPageDeltaY = RoundDown(mY); michael@0: mX -= aEvent->lineOrPageDeltaX; michael@0: mY -= aEvent->lineOrPageDeltaY; michael@0: } michael@0: michael@0: mLastTime = TimeStamp::Now(); michael@0: } michael@0: michael@0: void michael@0: EventStateManager::DeltaAccumulator::Reset() michael@0: { michael@0: mX = mY = 0.0; michael@0: mPendingScrollAmountX = mPendingScrollAmountY = 0.0; michael@0: mHandlingDeltaMode = UINT32_MAX; michael@0: mHandlingPixelOnlyDevice = false; michael@0: } michael@0: michael@0: nsIntPoint michael@0: EventStateManager::DeltaAccumulator::ComputeScrollAmountForDefaultAction( michael@0: WidgetWheelEvent* aEvent, michael@0: const nsIntSize& aScrollAmountInDevPixels) michael@0: { michael@0: MOZ_ASSERT(aEvent); michael@0: michael@0: // If the wheel event is line scroll and the delta value is computed from michael@0: // system settings, allow to override the system speed. michael@0: bool allowScrollSpeedOverride = michael@0: (!aEvent->customizedByUserPrefs && michael@0: aEvent->deltaMode == nsIDOMWheelEvent::DOM_DELTA_LINE); michael@0: DeltaValues acceleratedDelta = michael@0: WheelTransaction::AccelerateWheelDelta(aEvent, allowScrollSpeedOverride); michael@0: michael@0: nsIntPoint result(0, 0); michael@0: if (aEvent->deltaMode == nsIDOMWheelEvent::DOM_DELTA_PIXEL) { michael@0: mPendingScrollAmountX += acceleratedDelta.deltaX; michael@0: mPendingScrollAmountY += acceleratedDelta.deltaY; michael@0: } else { michael@0: mPendingScrollAmountX += michael@0: aScrollAmountInDevPixels.width * acceleratedDelta.deltaX; michael@0: mPendingScrollAmountY += michael@0: aScrollAmountInDevPixels.height * acceleratedDelta.deltaY; michael@0: } michael@0: result.x = RoundDown(mPendingScrollAmountX); michael@0: result.y = RoundDown(mPendingScrollAmountY); michael@0: mPendingScrollAmountX -= result.x; michael@0: mPendingScrollAmountY -= result.y; michael@0: michael@0: return result; michael@0: } michael@0: michael@0: /******************************************************************/ michael@0: /* mozilla::EventStateManager::WheelPrefs */ michael@0: /******************************************************************/ michael@0: michael@0: // static michael@0: EventStateManager::WheelPrefs* michael@0: EventStateManager::WheelPrefs::GetInstance() michael@0: { michael@0: if (!sInstance) { michael@0: sInstance = new WheelPrefs(); michael@0: } michael@0: return sInstance; michael@0: } michael@0: michael@0: // static michael@0: void michael@0: EventStateManager::WheelPrefs::Shutdown() michael@0: { michael@0: delete sInstance; michael@0: sInstance = nullptr; michael@0: } michael@0: michael@0: // static michael@0: void michael@0: EventStateManager::WheelPrefs::OnPrefChanged(const char* aPrefName, michael@0: void* aClosure) michael@0: { michael@0: // forget all prefs, it's not problem for performance. michael@0: sInstance->Reset(); michael@0: DeltaAccumulator::GetInstance()->Reset(); michael@0: } michael@0: michael@0: EventStateManager::WheelPrefs::WheelPrefs() michael@0: { michael@0: Reset(); michael@0: Preferences::RegisterCallback(OnPrefChanged, "mousewheel.", nullptr); michael@0: } michael@0: michael@0: EventStateManager::WheelPrefs::~WheelPrefs() michael@0: { michael@0: Preferences::UnregisterCallback(OnPrefChanged, "mousewheel.", nullptr); michael@0: } michael@0: michael@0: void michael@0: EventStateManager::WheelPrefs::Reset() michael@0: { michael@0: memset(mInit, 0, sizeof(mInit)); michael@0: michael@0: } michael@0: michael@0: EventStateManager::WheelPrefs::Index michael@0: EventStateManager::WheelPrefs::GetIndexFor(WidgetWheelEvent* aEvent) michael@0: { michael@0: if (!aEvent) { michael@0: return INDEX_DEFAULT; michael@0: } michael@0: michael@0: Modifiers modifiers = michael@0: (aEvent->modifiers & (MODIFIER_ALT | michael@0: MODIFIER_CONTROL | michael@0: MODIFIER_META | michael@0: MODIFIER_SHIFT | michael@0: MODIFIER_OS)); michael@0: michael@0: switch (modifiers) { michael@0: case MODIFIER_ALT: michael@0: return INDEX_ALT; michael@0: case MODIFIER_CONTROL: michael@0: return INDEX_CONTROL; michael@0: case MODIFIER_META: michael@0: return INDEX_META; michael@0: case MODIFIER_SHIFT: michael@0: return INDEX_SHIFT; michael@0: case MODIFIER_OS: michael@0: return INDEX_OS; michael@0: default: michael@0: // If two or more modifier keys are pressed, we should use default michael@0: // settings. michael@0: return INDEX_DEFAULT; michael@0: } michael@0: } michael@0: michael@0: void michael@0: EventStateManager::WheelPrefs::GetBasePrefName( michael@0: EventStateManager::WheelPrefs::Index aIndex, michael@0: nsACString& aBasePrefName) michael@0: { michael@0: aBasePrefName.AssignLiteral("mousewheel."); michael@0: switch (aIndex) { michael@0: case INDEX_ALT: michael@0: aBasePrefName.AppendLiteral("with_alt."); michael@0: break; michael@0: case INDEX_CONTROL: michael@0: aBasePrefName.AppendLiteral("with_control."); michael@0: break; michael@0: case INDEX_META: michael@0: aBasePrefName.AppendLiteral("with_meta."); michael@0: break; michael@0: case INDEX_SHIFT: michael@0: aBasePrefName.AppendLiteral("with_shift."); michael@0: break; michael@0: case INDEX_OS: michael@0: aBasePrefName.AppendLiteral("with_win."); michael@0: break; michael@0: case INDEX_DEFAULT: michael@0: default: michael@0: aBasePrefName.AppendLiteral("default."); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: void michael@0: EventStateManager::WheelPrefs::Init(EventStateManager::WheelPrefs::Index aIndex) michael@0: { michael@0: if (mInit[aIndex]) { michael@0: return; michael@0: } michael@0: mInit[aIndex] = true; michael@0: michael@0: nsAutoCString basePrefName; michael@0: GetBasePrefName(aIndex, basePrefName); michael@0: michael@0: nsAutoCString prefNameX(basePrefName); michael@0: prefNameX.AppendLiteral("delta_multiplier_x"); michael@0: mMultiplierX[aIndex] = michael@0: static_cast(Preferences::GetInt(prefNameX.get(), 100)) / 100; michael@0: michael@0: nsAutoCString prefNameY(basePrefName); michael@0: prefNameY.AppendLiteral("delta_multiplier_y"); michael@0: mMultiplierY[aIndex] = michael@0: static_cast(Preferences::GetInt(prefNameY.get(), 100)) / 100; michael@0: michael@0: nsAutoCString prefNameZ(basePrefName); michael@0: prefNameZ.AppendLiteral("delta_multiplier_z"); michael@0: mMultiplierZ[aIndex] = michael@0: static_cast(Preferences::GetInt(prefNameZ.get(), 100)) / 100; michael@0: michael@0: nsAutoCString prefNameAction(basePrefName); michael@0: prefNameAction.AppendLiteral("action"); michael@0: int32_t action = Preferences::GetInt(prefNameAction.get(), ACTION_SCROLL); michael@0: if (action < int32_t(ACTION_NONE) || action > int32_t(ACTION_LAST)) { michael@0: NS_WARNING("Unsupported action pref value, replaced with 'Scroll'."); michael@0: action = ACTION_SCROLL; michael@0: } michael@0: mActions[aIndex] = static_cast(action); michael@0: michael@0: // Compute action values overridden by .override_x pref. michael@0: // At present, override is possible only for the x-direction michael@0: // because this pref is introduced mainly for tilt wheels. michael@0: prefNameAction.AppendLiteral(".override_x"); michael@0: int32_t actionOverrideX = Preferences::GetInt(prefNameAction.get(), -1); michael@0: if (actionOverrideX < -1 || actionOverrideX > int32_t(ACTION_LAST)) { michael@0: NS_WARNING("Unsupported action override pref value, didn't override."); michael@0: actionOverrideX = -1; michael@0: } michael@0: mOverriddenActionsX[aIndex] = (actionOverrideX == -1) michael@0: ? static_cast(action) michael@0: : static_cast(actionOverrideX); michael@0: } michael@0: michael@0: void michael@0: EventStateManager::WheelPrefs::ApplyUserPrefsToDelta(WidgetWheelEvent* aEvent) michael@0: { michael@0: Index index = GetIndexFor(aEvent); michael@0: Init(index); michael@0: michael@0: aEvent->deltaX *= mMultiplierX[index]; michael@0: aEvent->deltaY *= mMultiplierY[index]; michael@0: aEvent->deltaZ *= mMultiplierZ[index]; michael@0: michael@0: // If the multiplier is 1.0 or -1.0, i.e., it doesn't change the absolute michael@0: // value, we should use lineOrPageDelta values which were set by widget. michael@0: // Otherwise, we need to compute them from accumulated delta values. michael@0: if (!NeedToComputeLineOrPageDelta(aEvent)) { michael@0: aEvent->lineOrPageDeltaX *= static_cast(mMultiplierX[index]); michael@0: aEvent->lineOrPageDeltaY *= static_cast(mMultiplierY[index]); michael@0: } else { michael@0: aEvent->lineOrPageDeltaX = 0; michael@0: aEvent->lineOrPageDeltaY = 0; michael@0: } michael@0: michael@0: aEvent->customizedByUserPrefs = michael@0: ((mMultiplierX[index] != 1.0) || (mMultiplierY[index] != 1.0) || michael@0: (mMultiplierZ[index] != 1.0)); michael@0: } michael@0: michael@0: void michael@0: EventStateManager::WheelPrefs::CancelApplyingUserPrefsFromOverflowDelta( michael@0: WidgetWheelEvent* aEvent) michael@0: { michael@0: Index index = GetIndexFor(aEvent); michael@0: Init(index); michael@0: michael@0: // XXX If the multiplier pref value is negative, the scroll direction was michael@0: // changed and caused to scroll different direction. In such case, michael@0: // this method reverts the sign of overflowDelta. Does it make widget michael@0: // happy? Although, widget can know the pref applied delta values by michael@0: // referrencing the deltaX and deltaY of the event. michael@0: michael@0: if (mMultiplierX[index]) { michael@0: aEvent->overflowDeltaX /= mMultiplierX[index]; michael@0: } michael@0: if (mMultiplierY[index]) { michael@0: aEvent->overflowDeltaY /= mMultiplierY[index]; michael@0: } michael@0: } michael@0: michael@0: EventStateManager::WheelPrefs::Action michael@0: EventStateManager::WheelPrefs::ComputeActionFor(WidgetWheelEvent* aEvent) michael@0: { michael@0: Index index = GetIndexFor(aEvent); michael@0: Init(index); michael@0: michael@0: bool deltaXPreferred = michael@0: (Abs(aEvent->deltaX) > Abs(aEvent->deltaY) && michael@0: Abs(aEvent->deltaX) > Abs(aEvent->deltaZ)); michael@0: Action* actions = deltaXPreferred ? mOverriddenActionsX : mActions; michael@0: if (actions[index] == ACTION_NONE || actions[index] == ACTION_SCROLL) { michael@0: return actions[index]; michael@0: } michael@0: michael@0: // Momentum events shouldn't run special actions. michael@0: if (aEvent->isMomentum) { michael@0: // Use the default action. Note that user might kill the wheel scrolling. michael@0: Init(INDEX_DEFAULT); michael@0: return (actions[INDEX_DEFAULT] == ACTION_SCROLL) ? ACTION_SCROLL : michael@0: ACTION_NONE; michael@0: } michael@0: michael@0: return actions[index]; michael@0: } michael@0: michael@0: bool michael@0: EventStateManager::WheelPrefs::NeedToComputeLineOrPageDelta( michael@0: WidgetWheelEvent* aEvent) michael@0: { michael@0: Index index = GetIndexFor(aEvent); michael@0: Init(index); michael@0: michael@0: return (mMultiplierX[index] != 1.0 && mMultiplierX[index] != -1.0) || michael@0: (mMultiplierY[index] != 1.0 && mMultiplierY[index] != -1.0); michael@0: } michael@0: michael@0: bool michael@0: EventStateManager::WheelPrefs::IsOverOnePageScrollAllowedX( michael@0: WidgetWheelEvent* aEvent) michael@0: { michael@0: Index index = GetIndexFor(aEvent); michael@0: Init(index); michael@0: return Abs(mMultiplierX[index]) >= michael@0: MIN_MULTIPLIER_VALUE_ALLOWING_OVER_ONE_PAGE_SCROLL; michael@0: } michael@0: michael@0: bool michael@0: EventStateManager::WheelPrefs::IsOverOnePageScrollAllowedY( michael@0: WidgetWheelEvent* aEvent) michael@0: { michael@0: Index index = GetIndexFor(aEvent); michael@0: Init(index); michael@0: return Abs(mMultiplierY[index]) >= michael@0: MIN_MULTIPLIER_VALUE_ALLOWING_OVER_ONE_PAGE_SCROLL; michael@0: } michael@0: michael@0: /******************************************************************/ michael@0: /* mozilla::EventStateManager::Prefs */ michael@0: /******************************************************************/ michael@0: michael@0: bool EventStateManager::Prefs::sKeyCausesActivation = true; michael@0: bool EventStateManager::Prefs::sClickHoldContextMenu = false; michael@0: int32_t EventStateManager::Prefs::sGenericAccessModifierKey = -1; michael@0: int32_t EventStateManager::Prefs::sChromeAccessModifierMask = 0; michael@0: int32_t EventStateManager::Prefs::sContentAccessModifierMask = 0; michael@0: michael@0: // static michael@0: void michael@0: EventStateManager::Prefs::Init() michael@0: { michael@0: DebugOnly rv = michael@0: Preferences::AddBoolVarCache(&sKeyCausesActivation, michael@0: "accessibility.accesskeycausesactivation", michael@0: sKeyCausesActivation); michael@0: MOZ_ASSERT(NS_SUCCEEDED(rv), michael@0: "Failed to observe \"accessibility.accesskeycausesactivation\""); michael@0: rv = Preferences::AddBoolVarCache(&sClickHoldContextMenu, michael@0: "ui.click_hold_context_menus", michael@0: sClickHoldContextMenu); michael@0: MOZ_ASSERT(NS_SUCCEEDED(rv), michael@0: "Failed to observe \"ui.click_hold_context_menus\""); michael@0: rv = Preferences::AddIntVarCache(&sGenericAccessModifierKey, michael@0: "ui.key.generalAccessKey", michael@0: sGenericAccessModifierKey); michael@0: MOZ_ASSERT(NS_SUCCEEDED(rv), michael@0: "Failed to observe \"ui.key.generalAccessKey\""); michael@0: rv = Preferences::AddIntVarCache(&sChromeAccessModifierMask, michael@0: "ui.key.chromeAccess", michael@0: sChromeAccessModifierMask); michael@0: MOZ_ASSERT(NS_SUCCEEDED(rv), michael@0: "Failed to observe \"ui.key.chromeAccess\""); michael@0: rv = Preferences::AddIntVarCache(&sContentAccessModifierMask, michael@0: "ui.key.contentAccess", michael@0: sContentAccessModifierMask); michael@0: MOZ_ASSERT(NS_SUCCEEDED(rv), michael@0: "Failed to observe \"ui.key.contentAccess\""); michael@0: michael@0: rv = Preferences::RegisterCallback(OnChange, "dom.popup_allowed_events"); michael@0: MOZ_ASSERT(NS_SUCCEEDED(rv), michael@0: "Failed to observe \"dom.popup_allowed_events\""); michael@0: } michael@0: michael@0: // static michael@0: void michael@0: EventStateManager::Prefs::OnChange(const char* aPrefName, void*) michael@0: { michael@0: nsDependentCString prefName(aPrefName); michael@0: if (prefName.EqualsLiteral("dom.popup_allowed_events")) { michael@0: Event::PopupAllowedEventsChanged(); michael@0: } michael@0: } michael@0: michael@0: // static michael@0: void michael@0: EventStateManager::Prefs::Shutdown() michael@0: { michael@0: Preferences::UnregisterCallback(OnChange, "dom.popup_allowed_events"); michael@0: } michael@0: michael@0: // static michael@0: int32_t michael@0: EventStateManager::Prefs::ChromeAccessModifierMask() michael@0: { michael@0: return GetAccessModifierMask(nsIDocShellTreeItem::typeChrome); michael@0: } michael@0: michael@0: // static michael@0: int32_t michael@0: EventStateManager::Prefs::ContentAccessModifierMask() michael@0: { michael@0: return GetAccessModifierMask(nsIDocShellTreeItem::typeContent); michael@0: } michael@0: michael@0: // static michael@0: int32_t michael@0: EventStateManager::Prefs::GetAccessModifierMask(int32_t aItemType) michael@0: { michael@0: switch (sGenericAccessModifierKey) { michael@0: case -1: break; // use the individual prefs michael@0: case nsIDOMKeyEvent::DOM_VK_SHIFT: return NS_MODIFIER_SHIFT; michael@0: case nsIDOMKeyEvent::DOM_VK_CONTROL: return NS_MODIFIER_CONTROL; michael@0: case nsIDOMKeyEvent::DOM_VK_ALT: return NS_MODIFIER_ALT; michael@0: case nsIDOMKeyEvent::DOM_VK_META: return NS_MODIFIER_META; michael@0: case nsIDOMKeyEvent::DOM_VK_WIN: return NS_MODIFIER_OS; michael@0: default: return 0; michael@0: } michael@0: michael@0: switch (aItemType) { michael@0: case nsIDocShellTreeItem::typeChrome: michael@0: return sChromeAccessModifierMask; michael@0: case nsIDocShellTreeItem::typeContent: michael@0: return sContentAccessModifierMask; michael@0: default: michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: /******************************************************************/ michael@0: /* mozilla::AutoHandlingUserInputStatePusher */ michael@0: /******************************************************************/ michael@0: michael@0: AutoHandlingUserInputStatePusher::AutoHandlingUserInputStatePusher( michael@0: bool aIsHandlingUserInput, michael@0: WidgetEvent* aEvent, michael@0: nsIDocument* aDocument) : michael@0: mIsHandlingUserInput(aIsHandlingUserInput), michael@0: mIsMouseDown(aEvent && aEvent->message == NS_MOUSE_BUTTON_DOWN), michael@0: mResetFMMouseDownState(false) michael@0: { michael@0: if (!aIsHandlingUserInput) { michael@0: return; michael@0: } michael@0: EventStateManager::StartHandlingUserInput(); michael@0: if (!mIsMouseDown) { michael@0: return; michael@0: } michael@0: nsIPresShell::SetCapturingContent(nullptr, 0); michael@0: nsIPresShell::AllowMouseCapture(true); michael@0: if (!aDocument || !aEvent->mFlags.mIsTrusted) { michael@0: return; michael@0: } michael@0: nsFocusManager* fm = nsFocusManager::GetFocusManager(); michael@0: NS_ENSURE_TRUE_VOID(fm); michael@0: fm->SetMouseButtonDownHandlingDocument(aDocument); michael@0: mResetFMMouseDownState = true; michael@0: } michael@0: michael@0: AutoHandlingUserInputStatePusher::~AutoHandlingUserInputStatePusher() michael@0: { michael@0: if (!mIsHandlingUserInput) { michael@0: return; michael@0: } michael@0: EventStateManager::StopHandlingUserInput(); michael@0: if (!mIsMouseDown) { michael@0: return; michael@0: } michael@0: nsIPresShell::AllowMouseCapture(false); michael@0: if (!mResetFMMouseDownState) { michael@0: return; michael@0: } michael@0: nsFocusManager* fm = nsFocusManager::GetFocusManager(); michael@0: NS_ENSURE_TRUE_VOID(fm); michael@0: fm->SetMouseButtonDownHandlingDocument(nullptr); michael@0: } michael@0: michael@0: } // namespace mozilla michael@0: