1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef EditorEventListener_h
7 #define EditorEventListener_h
8 
9 #include "mozilla/Attributes.h"
10 #include "mozilla/EventForwards.h"
11 #include "nsCOMPtr.h"
12 #include "nsError.h"
13 #include "nsIDOMEventListener.h"
14 #include "nsISupportsImpl.h"
15 #include "nscore.h"
16 
17 class nsCaret;
18 class nsIContent;
19 class nsPresContext;
20 
21 // X.h defines KeyPress
22 #ifdef KeyPress
23 #  undef KeyPress
24 #endif
25 
26 #ifdef XP_WIN
27 // On Windows, we support switching the text direction by pressing Ctrl+Shift
28 #  define HANDLE_NATIVE_TEXT_DIRECTION_SWITCH
29 #endif
30 
31 namespace mozilla {
32 
33 class EditorBase;
34 class PresShell;
35 
36 namespace dom {
37 class DragEvent;
38 class MouseEvent;
39 }  // namespace dom
40 
41 class EditorEventListener : public nsIDOMEventListener {
42  public:
43   EditorEventListener();
44 
45   virtual nsresult Connect(EditorBase* aEditorBase);
46 
47   virtual void Disconnect();
48 
49   NS_DECL_ISUPPORTS
50 
51   // nsIDOMEventListener
52   MOZ_CAN_RUN_SCRIPT NS_IMETHOD HandleEvent(dom::Event* aEvent) override;
53 
54   MOZ_CAN_RUN_SCRIPT void SpellCheckIfNeeded();
55 
56  protected:
57   virtual ~EditorEventListener();
58 
59   nsresult InstallToEditor();
60   void UninstallFromEditor();
61 
62 #ifdef HANDLE_NATIVE_TEXT_DIRECTION_SWITCH
63   nsresult KeyDown(const WidgetKeyboardEvent* aKeyboardEvent);
64   MOZ_CAN_RUN_SCRIPT nsresult KeyUp(const WidgetKeyboardEvent* aKeyboardEvent);
65 #endif
66   MOZ_CAN_RUN_SCRIPT nsresult KeyPress(WidgetKeyboardEvent* aKeyboardEvent);
67   MOZ_CAN_RUN_SCRIPT nsresult
68   HandleChangeComposition(WidgetCompositionEvent* aCompositionEvent);
69   nsresult HandleStartComposition(WidgetCompositionEvent* aCompositionEvent);
70   MOZ_CAN_RUN_SCRIPT void HandleEndComposition(
71       WidgetCompositionEvent* aCompositionEvent);
72   MOZ_CAN_RUN_SCRIPT virtual nsresult MouseDown(dom::MouseEvent* aMouseEvent);
MouseUp(dom::MouseEvent * aMouseEvent)73   MOZ_CAN_RUN_SCRIPT virtual nsresult MouseUp(dom::MouseEvent* aMouseEvent) {
74     return NS_OK;
75   }
76   MOZ_CAN_RUN_SCRIPT virtual nsresult MouseClick(
77       WidgetMouseEvent* aMouseClickEvent);
78   MOZ_CAN_RUN_SCRIPT nsresult Focus(InternalFocusEvent* aFocusEvent);
79   nsresult Blur(InternalFocusEvent* aBlurEvent);
80   MOZ_CAN_RUN_SCRIPT nsresult DragOverOrDrop(dom::DragEvent* aDragEvent);
81   nsresult DragLeave(dom::DragEvent* aDragEvent);
82 
83   void RefuseToDropAndHideCaret(dom::DragEvent* aDragEvent);
84   bool DragEventHasSupportingData(dom::DragEvent* aDragEvent) const;
85   MOZ_CAN_RUN_SCRIPT bool CanInsertAtDropPosition(dom::DragEvent* aDragEvent);
86   void InitializeDragDropCaret();
87   void CleanupDragDropCaret();
88   PresShell* GetPresShell() const;
89   nsPresContext* GetPresContext() const;
90   // Returns true if IME consumes the mouse event.
91   MOZ_CAN_RUN_SCRIPT bool NotifyIMEOfMouseButtonEvent(
92       WidgetMouseEvent* aMouseEvent);
93   bool EditorHasFocus();
94   bool IsFileControlTextBox();
95   bool ShouldHandleNativeKeyBindings(WidgetKeyboardEvent* aKeyboardEvent);
96 
97   /**
98    * DetachedFromEditor() returns true if editor was detached.
99    * Otherwise, false.
100    */
101   bool DetachedFromEditor() const;
102 
103   /**
104    * DetachedFromEditorOrDefaultPrevented() returns true if editor was detached
105    * and/or the event was consumed.  Otherwise, i.e., attached editor can
106    * handle the event, returns true.
107    */
108   bool DetachedFromEditorOrDefaultPrevented(WidgetEvent* aEvent) const;
109 
110   /**
111    * EnsureCommitComposition() requests to commit composition if there is.
112    * Returns false if the editor is detached from the listener, i.e.,
113    * impossible to continue to handle the event.  Otherwise, true.
114    */
115   [[nodiscard]] bool EnsureCommitComposition();
116 
117   EditorBase* mEditorBase;  // weak
118   RefPtr<nsCaret> mCaret;
119   bool mCommitText;
120   bool mInTransaction;
121   bool mMouseDownOrUpConsumedByIME;
122 #ifdef HANDLE_NATIVE_TEXT_DIRECTION_SWITCH
123   bool mHaveBidiKeyboards;
124   bool mShouldSwitchTextDirection;
125   bool mSwitchToRTL;
126 #endif
127 };
128 
129 }  // namespace mozilla
130 
131 #endif  // #ifndef EditorEventListener_h
132