UIEvent.h (3832B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_UIEvent_h_ 8 #define mozilla_dom_UIEvent_h_ 9 10 #include "mozilla/Attributes.h" 11 #include "mozilla/dom/Event.h" 12 #include "mozilla/dom/Nullable.h" 13 #include "mozilla/dom/UIEventBinding.h" 14 #include "mozilla/dom/WindowProxyHolder.h" 15 #include "nsDeviceContext.h" 16 #include "nsDocShell.h" 17 #include "nsIContent.h" 18 #include "nsPresContext.h" 19 20 class nsINode; 21 22 namespace mozilla::dom { 23 24 class UIEvent : public Event { 25 public: 26 UIEvent(EventTarget* aOwner, nsPresContext* aPresContext, 27 WidgetGUIEvent* aEvent); 28 29 NS_DECL_ISUPPORTS_INHERITED 30 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(UIEvent, Event) 31 32 void DuplicatePrivateData() override; 33 void Serialize(IPC::MessageWriter* aWriter, 34 bool aSerializeInterfaceType) override; 35 bool Deserialize(IPC::MessageReader* aReader) override; 36 37 static already_AddRefed<UIEvent> Constructor(const GlobalObject& aGlobal, 38 const nsAString& aType, 39 const UIEventInit& aParam); 40 41 virtual JSObject* WrapObjectInternal( 42 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override { 43 return UIEvent_Binding::Wrap(aCx, this, aGivenProto); 44 } 45 46 UIEvent* AsUIEvent() override { return this; } 47 48 void InitUIEvent(const nsAString& typeArg, bool canBubbleArg, 49 bool cancelableArg, nsGlobalWindowInner* viewArg, 50 int32_t detailArg); 51 52 Nullable<WindowProxyHolder> GetView() const { 53 if (!mView) { 54 return nullptr; 55 } 56 return WindowProxyHolder(mView->GetBrowsingContext()); 57 } 58 59 int32_t Detail() const { return mDetail; } 60 61 int32_t LayerX() const { return GetLayerPoint().x; } 62 63 int32_t LayerY() const { return GetLayerPoint().y; } 64 65 virtual uint32_t Which(CallerType aCallerType = CallerType::System) { 66 MOZ_ASSERT(mEvent->mClass != eKeyboardEventClass, 67 "Key events should override Which()"); 68 MOZ_ASSERT(mEvent->mClass != eMouseEventClass, 69 "Mouse events should override Which()"); 70 return 0; 71 } 72 73 /** 74 * GetRangeParent() should be used only by JS. C++ callers should use 75 * GetRangeParentContent() or GetRangeParentContentAndOffset() instead. 76 */ 77 MOZ_CAN_RUN_SCRIPT already_AddRefed<nsINode> GetRangeParent() { 78 return GetRangeParentContent(); 79 } 80 MOZ_CAN_RUN_SCRIPT already_AddRefed<nsIContent> GetRangeParentContent() { 81 return GetRangeParentContentAndOffset(nullptr); 82 } 83 /** 84 * aOffset is optional (i.e., can be nullptr), but when you call this with 85 * nullptr, you should use GetRangeParentContent() instead. 86 */ 87 MOZ_CAN_RUN_SCRIPT already_AddRefed<nsIContent> 88 GetRangeParentContentAndOffset(int32_t* aOffset) const; 89 90 /** 91 * If you also need to compute range parent in C++ code, you should use 92 * GetRangeParentContentAndOffset() instead. 93 */ 94 MOZ_CAN_RUN_SCRIPT int32_t RangeOffset() const; 95 96 protected: 97 ~UIEvent() = default; 98 99 // Internal helper functions 100 nsIntPoint GetLayerPoint() const; 101 102 nsCOMPtr<nsPIDOMWindowOuter> mView; 103 int32_t mDetail; 104 nsIntPoint mLayerPoint; 105 106 static Modifiers ComputeModifierState(const nsAString& aModifiersList); 107 bool GetModifierStateInternal(const nsAString& aKey); 108 void InitModifiers(const EventModifierInit& aParam); 109 }; 110 111 } // namespace mozilla::dom 112 113 already_AddRefed<mozilla::dom::UIEvent> NS_NewDOMUIEvent( 114 mozilla::dom::EventTarget* aOwner, nsPresContext* aPresContext, 115 mozilla::WidgetGUIEvent* aEvent); 116 117 #endif // mozilla_dom_UIEvent_h_