michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 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 file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/dom/WheelEvent.h" michael@0: #include "mozilla/MouseEvents.h" michael@0: #include "prtime.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: WheelEvent::WheelEvent(EventTarget* aOwner, michael@0: nsPresContext* aPresContext, michael@0: WidgetWheelEvent* aWheelEvent) michael@0: : MouseEvent(aOwner, aPresContext, michael@0: aWheelEvent ? aWheelEvent : michael@0: new WidgetWheelEvent(false, 0, nullptr)) michael@0: , mAppUnitsPerDevPixel(0) michael@0: { michael@0: if (aWheelEvent) { michael@0: mEventIsInternal = false; michael@0: // If the delta mode is pixel, the WidgetWheelEvent's delta values are in michael@0: // device pixels. However, JS contents need the delta values in CSS pixels. michael@0: // We should store the value of mAppUnitsPerDevPixel here because michael@0: // it might be changed by changing zoom or something. michael@0: if (aWheelEvent->deltaMode == nsIDOMWheelEvent::DOM_DELTA_PIXEL) { michael@0: mAppUnitsPerDevPixel = aPresContext->AppUnitsPerDevPixel(); michael@0: } michael@0: } else { michael@0: mEventIsInternal = true; michael@0: mEvent->time = PR_Now(); michael@0: mEvent->refPoint.x = mEvent->refPoint.y = 0; michael@0: mEvent->AsWheelEvent()->inputSource = nsIDOMMouseEvent::MOZ_SOURCE_UNKNOWN; michael@0: } michael@0: } michael@0: michael@0: NS_IMPL_ADDREF_INHERITED(WheelEvent, MouseEvent) michael@0: NS_IMPL_RELEASE_INHERITED(WheelEvent, MouseEvent) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN(WheelEvent) michael@0: NS_INTERFACE_MAP_ENTRY(nsIDOMWheelEvent) michael@0: NS_INTERFACE_MAP_END_INHERITING(MouseEvent) michael@0: michael@0: NS_IMETHODIMP michael@0: WheelEvent::InitWheelEvent(const nsAString& aType, michael@0: bool aCanBubble, michael@0: bool aCancelable, michael@0: nsIDOMWindow* aView, michael@0: int32_t aDetail, michael@0: int32_t aScreenX, michael@0: int32_t aScreenY, michael@0: int32_t aClientX, michael@0: int32_t aClientY, michael@0: uint16_t aButton, michael@0: nsIDOMEventTarget* aRelatedTarget, michael@0: const nsAString& aModifiersList, michael@0: double aDeltaX, michael@0: double aDeltaY, michael@0: double aDeltaZ, michael@0: uint32_t aDeltaMode) michael@0: { michael@0: nsresult rv = michael@0: MouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable, aView, aDetail, michael@0: aScreenX, aScreenY, aClientX, aClientY, aButton, michael@0: aRelatedTarget, aModifiersList); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: WidgetWheelEvent* wheelEvent = mEvent->AsWheelEvent(); michael@0: wheelEvent->deltaX = aDeltaX; michael@0: wheelEvent->deltaY = aDeltaY; michael@0: wheelEvent->deltaZ = aDeltaZ; michael@0: wheelEvent->deltaMode = aDeltaMode; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: double michael@0: WheelEvent::DeltaX() michael@0: { michael@0: if (!mAppUnitsPerDevPixel) { michael@0: return mEvent->AsWheelEvent()->deltaX; michael@0: } michael@0: return mEvent->AsWheelEvent()->deltaX * michael@0: mAppUnitsPerDevPixel / nsPresContext::AppUnitsPerCSSPixel(); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: WheelEvent::GetDeltaX(double* aDeltaX) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aDeltaX); michael@0: michael@0: *aDeltaX = DeltaX(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: double michael@0: WheelEvent::DeltaY() michael@0: { michael@0: if (!mAppUnitsPerDevPixel) { michael@0: return mEvent->AsWheelEvent()->deltaY; michael@0: } michael@0: return mEvent->AsWheelEvent()->deltaY * michael@0: mAppUnitsPerDevPixel / nsPresContext::AppUnitsPerCSSPixel(); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: WheelEvent::GetDeltaY(double* aDeltaY) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aDeltaY); michael@0: michael@0: *aDeltaY = DeltaY(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: double michael@0: WheelEvent::DeltaZ() michael@0: { michael@0: if (!mAppUnitsPerDevPixel) { michael@0: return mEvent->AsWheelEvent()->deltaZ; michael@0: } michael@0: return mEvent->AsWheelEvent()->deltaZ * michael@0: mAppUnitsPerDevPixel / nsPresContext::AppUnitsPerCSSPixel(); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: WheelEvent::GetDeltaZ(double* aDeltaZ) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aDeltaZ); michael@0: michael@0: *aDeltaZ = DeltaZ(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: uint32_t michael@0: WheelEvent::DeltaMode() michael@0: { michael@0: return mEvent->AsWheelEvent()->deltaMode; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: WheelEvent::GetDeltaMode(uint32_t* aDeltaMode) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aDeltaMode); michael@0: michael@0: *aDeltaMode = DeltaMode(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: static void michael@0: GetModifierList(bool aCtrl, bool aShift, bool aAlt, bool aMeta, michael@0: nsAString& aModifierList) michael@0: { michael@0: if (aCtrl) { michael@0: aModifierList.AppendLiteral(NS_DOM_KEYNAME_CONTROL); michael@0: } michael@0: if (aShift) { michael@0: if (!aModifierList.IsEmpty()) { michael@0: aModifierList.AppendLiteral(" "); michael@0: } michael@0: aModifierList.AppendLiteral(NS_DOM_KEYNAME_SHIFT); michael@0: } michael@0: if (aAlt) { michael@0: if (!aModifierList.IsEmpty()) { michael@0: aModifierList.AppendLiteral(" "); michael@0: } michael@0: aModifierList.AppendLiteral(NS_DOM_KEYNAME_ALT); michael@0: } michael@0: if (aMeta) { michael@0: if (!aModifierList.IsEmpty()) { michael@0: aModifierList.AppendLiteral(" "); michael@0: } michael@0: aModifierList.AppendLiteral(NS_DOM_KEYNAME_META); michael@0: } michael@0: } michael@0: michael@0: already_AddRefed michael@0: WheelEvent::Constructor(const GlobalObject& aGlobal, michael@0: const nsAString& aType, michael@0: const WheelEventInit& aParam, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsCOMPtr t = do_QueryInterface(aGlobal.GetAsSupports()); michael@0: nsRefPtr e = new WheelEvent(t, nullptr, nullptr); michael@0: bool trusted = e->Init(t); michael@0: nsAutoString modifierList; michael@0: GetModifierList(aParam.mCtrlKey, aParam.mShiftKey, michael@0: aParam.mAltKey, aParam.mMetaKey, michael@0: modifierList); michael@0: aRv = e->InitWheelEvent(aType, aParam.mBubbles, aParam.mCancelable, michael@0: aParam.mView, aParam.mDetail, michael@0: aParam.mScreenX, aParam.mScreenY, michael@0: aParam.mClientX, aParam.mClientY, michael@0: aParam.mButton, aParam.mRelatedTarget, michael@0: modifierList, aParam.mDeltaX, michael@0: aParam.mDeltaY, aParam.mDeltaZ, aParam.mDeltaMode); michael@0: e->mEvent->AsWheelEvent()->buttons = aParam.mButtons; michael@0: e->SetTrusted(trusted); michael@0: return e.forget(); michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::dom; michael@0: michael@0: nsresult michael@0: NS_NewDOMWheelEvent(nsIDOMEvent** aInstancePtrResult, michael@0: EventTarget* aOwner, michael@0: nsPresContext* aPresContext, michael@0: WidgetWheelEvent* aEvent) michael@0: { michael@0: WheelEvent* it = new WheelEvent(aOwner, aPresContext, aEvent); michael@0: NS_ADDREF(it); michael@0: *aInstancePtrResult = static_cast(it); michael@0: return NS_OK; michael@0: }