1 /* -*- Mode: C++; tab-width: 2; 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/. */
6 #ifndef mozilla_IMEStateManager_h_
7 #define mozilla_IMEStateManager_h_
9 #include "mozilla/EventForwards.h"
10 #include "nsIWidget.h"
13 class nsIDOMMouseEvent
;
22 class EventDispatchingCallback
;
23 class IMEContentObserver
;
24 class TextCompositionArray
;
25 class TextComposition
;
28 * IMEStateManager manages InputContext (e.g., active editor type, IME enabled
29 * state and IME open state) of nsIWidget instances, manages IMEContentObserver
30 * and provides useful API for IME.
35 typedef widget::IMEMessage IMEMessage
;
36 typedef widget::IMEState IMEState
;
37 typedef widget::InputContext InputContext
;
38 typedef widget::InputContextAction InputContextAction
;
42 static void Shutdown();
44 static nsresult
OnDestroyPresContext(nsPresContext
* aPresContext
);
45 static nsresult
OnRemoveContent(nsPresContext
* aPresContext
,
46 nsIContent
* aContent
);
48 * OnChangeFocus() should be called when focused content is changed or
49 * IME enabled state is changed. If nobody has focus, set both aPresContext
50 * and aContent nullptr. E.g., all windows are deactivated.
52 static nsresult
OnChangeFocus(nsPresContext
* aPresContext
,
54 InputContextAction::Cause aCause
);
55 static void OnInstalledMenuKeyboardListener(bool aInstalling
);
57 // These two methods manage focus and selection/text observers.
58 // They are separate from OnChangeFocus above because this offers finer
59 // control compared to having the two methods incorporated into OnChangeFocus
61 // Get the focused editor's selection and root
62 static nsresult
GetFocusSelectionAndRoot(nsISelection
** aSel
,
64 // This method updates the current IME state. However, if the enabled state
65 // isn't changed by the new state, this method does nothing.
66 // Note that this method changes the IME state of the active element in the
67 // widget. So, the caller must have focus.
68 static void UpdateIMEState(const IMEState
&aNewIMEState
,
72 // This method is called when user operates mouse button in focused editor
73 // and before the editor handles it.
74 // Returns true if IME consumes the event. Otherwise, false.
75 static bool OnMouseButtonEventInEditor(nsPresContext
* aPresContext
,
77 nsIDOMMouseEvent
* aMouseEvent
);
79 // This method is called when user clicked in an editor.
81 // If the editor is for <input> or <textarea>, the element.
82 // If the editor is for contenteditable, the active editinghost.
83 // If the editor is for designMode, nullptr.
84 static void OnClickInEditor(nsPresContext
* aPresContext
,
86 nsIDOMMouseEvent
* aMouseEvent
);
88 // This method is called when editor actually gets focus.
90 // If the editor is for <input> or <textarea>, the element.
91 // If the editor is for contenteditable, the active editinghost.
92 // If the editor is for designMode, nullptr.
93 static void OnFocusInEditor(nsPresContext
* aPresContext
,
98 * All composition events must be dispatched via DispatchCompositionEvent()
99 * for storing the composition target and ensuring a set of composition
100 * events must be fired the stored target. If the stored composition event
101 * target is destroying, this removes the stored composition automatically.
103 static void DispatchCompositionEvent(
104 nsINode
* aEventTargetNode
,
105 nsPresContext
* aPresContext
,
106 WidgetCompositionEvent
* aCompositionEvent
,
107 nsEventStatus
* aStatus
,
108 EventDispatchingCallback
* aCallBack
,
109 bool aIsSynthesized
= false);
112 * This is called when PresShell ignores a composition event due to not safe
113 * to dispatch events.
115 static void OnCompositionEventDiscarded(
116 const WidgetCompositionEvent
* aCompositionEvent
);
119 * Get TextComposition from widget.
121 static already_AddRefed
<TextComposition
>
122 GetTextCompositionFor(nsIWidget
* aWidget
);
125 * Returns TextComposition instance for the event.
127 * @param aGUIEvent Should be a composition event which is being dispatched.
129 static already_AddRefed
<TextComposition
>
130 GetTextCompositionFor(WidgetGUIEvent
* aGUIEvent
);
133 * Send a notification to IME. It depends on the IME or platform spec what
134 * will occur (or not occur).
136 static nsresult
NotifyIME(IMEMessage aMessage
, nsIWidget
* aWidget
);
137 static nsresult
NotifyIME(IMEMessage aMessage
, nsPresContext
* aPresContext
);
139 static nsINode
* GetRootEditableNode(nsPresContext
* aPresContext
,
140 nsIContent
* aContent
);
141 static bool IsTestingIME() { return sIsTestingIME
; }
144 static nsresult
OnChangeFocusInternal(nsPresContext
* aPresContext
,
145 nsIContent
* aContent
,
146 InputContextAction aAction
);
147 static void SetIMEState(const IMEState
&aState
,
148 nsIContent
* aContent
,
150 InputContextAction aAction
);
151 static IMEState
GetNewIMEState(nsPresContext
* aPresContext
,
152 nsIContent
* aContent
);
154 static void EnsureTextCompositionArray();
155 static void CreateIMEContentObserver(nsIEditor
* aEditor
);
156 static void DestroyIMEContentObserver();
158 static bool IsEditable(nsINode
* node
);
160 static bool IsEditableIMEState(nsIWidget
* aWidget
);
162 static nsIContent
* sContent
;
163 static nsPresContext
* sPresContext
;
164 static bool sInstalledMenuKeyboardListener
;
165 static bool sIsTestingIME
;
166 static bool sIsGettingNewIMEState
;
168 class MOZ_STACK_CLASS GettingNewIMEStateBlocker MOZ_FINAL
171 GettingNewIMEStateBlocker()
172 : mOldValue(IMEStateManager::sIsGettingNewIMEState
)
174 IMEStateManager::sIsGettingNewIMEState
= true;
176 ~GettingNewIMEStateBlocker()
178 IMEStateManager::sIsGettingNewIMEState
= mOldValue
;
184 static IMEContentObserver
* sActiveIMEContentObserver
;
186 // All active compositions in the process are stored by this array.
187 // When you get an item of this array and use it, please be careful.
188 // The instances in this array can be destroyed automatically if you do
189 // something to cause committing or canceling the composition.
190 static TextCompositionArray
* sTextCompositions
;
193 } // namespace mozilla
195 #endif // mozilla_IMEStateManager_h_