CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / widget / src / cocoa / nsCocoaTextInputHandler.h
blob5ffb5be92898e99b4b925097907811c29da19313
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 sw=2 et tw=80: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Mozilla Japan.
20 * Portions created by the Initial Developer are Copyright (C) 2009
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Masayuki Nakano <masayuki@d-toybox.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef nsCocoaTextInputHandler_h_
41 #define nsCocoaTextInputHandler_h_
43 #include "nsCocoaUtils.h"
45 #import <Carbon/Carbon.h>
46 #import <Cocoa/Cocoa.h>
47 #include "mozView.h"
48 #include "nsString.h"
49 #include "nsCOMPtr.h"
50 #include "nsITimer.h"
51 #include "npapi.h"
53 struct PRLogModuleInfo;
54 class nsChildView;
56 /**
57 * nsTISInputSource is a wrapper for the TISInputSourceRef. If we get the
58 * TISInputSourceRef from InputSourceID, we need to release the CFArray instance
59 * which is returned by TISCreateInputSourceList. However, when we release the
60 * list, we cannot access the TISInputSourceRef. So, it's not usable, and it
61 * may cause the memory leak bugs. nsTISInputSource automatically releases the
62 * list when the instance is destroyed.
64 class nsTISInputSource
66 public:
67 static nsTISInputSource& CurrentKeyboardLayout();
69 nsTISInputSource()
71 mInputSourceList = nsnull;
72 Clear();
75 nsTISInputSource(const char* aID)
77 mInputSourceList = nsnull;
78 InitByInputSourceID(aID);
81 nsTISInputSource(SInt32 aLayoutID)
83 mInputSourceList = nsnull;
84 InitByLayoutID(aLayoutID);
87 nsTISInputSource(TISInputSourceRef aInputSource)
89 mInputSourceList = nsnull;
90 InitByTISInputSourceRef(aInputSource);
93 ~nsTISInputSource() { Clear(); }
95 void InitByInputSourceID(const char* aID);
96 void InitByInputSourceID(const nsAFlatString &aID);
97 void InitByInputSourceID(const CFStringRef aID);
98 void InitByLayoutID(SInt32 aLayoutID);
99 void InitByCurrentInputSource();
100 void InitByCurrentKeyboardLayout();
101 void InitByCurrentASCIICapableInputSource();
102 void InitByCurrentASCIICapableKeyboardLayout();
103 void InitByTISInputSourceRef(TISInputSourceRef aInputSource);
104 void InitByLanguage(CFStringRef aLanguage);
106 const UCKeyboardLayout* GetUCKeyboardLayout();
108 PRBool IsOpenedIMEMode();
109 PRBool IsIMEMode();
111 PRBool IsASCIICapable()
113 NS_ENSURE_TRUE(mInputSource, PR_FALSE);
114 return GetBoolProperty(kTISPropertyInputSourceIsASCIICapable);
117 PRBool IsEnabled()
119 NS_ENSURE_TRUE(mInputSource, PR_FALSE);
120 return GetBoolProperty(kTISPropertyInputSourceIsEnabled);
123 PRBool GetLanguageList(CFArrayRef &aLanguageList);
124 PRBool GetPrimaryLanguage(CFStringRef &aPrimaryLanguage);
125 PRBool GetPrimaryLanguage(nsAString &aPrimaryLanguage);
127 PRBool GetLocalizedName(CFStringRef &aName)
129 NS_ENSURE_TRUE(mInputSource, PR_FALSE);
130 return GetStringProperty(kTISPropertyLocalizedName, aName);
133 PRBool GetLocalizedName(nsAString &aName)
135 NS_ENSURE_TRUE(mInputSource, PR_FALSE);
136 return GetStringProperty(kTISPropertyLocalizedName, aName);
139 PRBool GetInputSourceID(CFStringRef &aID)
141 NS_ENSURE_TRUE(mInputSource, PR_FALSE);
142 return GetStringProperty(kTISPropertyInputSourceID, aID);
145 PRBool GetInputSourceID(nsAString &aID)
147 NS_ENSURE_TRUE(mInputSource, PR_FALSE);
148 return GetStringProperty(kTISPropertyInputSourceID, aID);
151 PRBool GetBundleID(CFStringRef &aBundleID)
153 NS_ENSURE_TRUE(mInputSource, PR_FALSE);
154 return GetStringProperty(kTISPropertyBundleID, aBundleID);
157 PRBool GetBundleID(nsAString &aBundleID)
159 NS_ENSURE_TRUE(mInputSource, PR_FALSE);
160 return GetStringProperty(kTISPropertyBundleID, aBundleID);
163 PRBool GetInputSourceType(CFStringRef &aType)
165 NS_ENSURE_TRUE(mInputSource, PR_FALSE);
166 return GetStringProperty(kTISPropertyInputSourceType, aType);
169 PRBool GetInputSourceType(nsAString &aType)
171 NS_ENSURE_TRUE(mInputSource, PR_FALSE);
172 return GetStringProperty(kTISPropertyInputSourceType, aType);
175 PRBool IsForRTLLanguage();
176 PRBool IsInitializedByCurrentKeyboardLayout();
178 enum {
179 // 40 is an actual result of the ::LMGetKbdType() when we connect an
180 // unknown keyboard and set the keyboard type to ANSI manually on the
181 // set up dialog.
182 eKbdType_ANSI = 40
185 PRBool TranslateToString(UInt32 aKeyCode, UInt32 aModifiers, UInt32 aKbdType,
186 nsAString &aStr);
188 void Select();
189 void Clear();
191 protected:
192 static PRBool UCKeyTranslateToString(const UCKeyboardLayout* aHandle,
193 UInt32 aKeyCode, UInt32 aModifiers,
194 UInt32 aKbType, nsAString &aStr);
196 PRBool GetBoolProperty(const CFStringRef aKey);
197 PRBool GetStringProperty(const CFStringRef aKey, CFStringRef &aStr);
198 PRBool GetStringProperty(const CFStringRef aKey, nsAString &aStr);
200 TISInputSourceRef mInputSource;
201 CFArrayRef mInputSourceList;
202 const UCKeyboardLayout* mUCKeyboardLayout;
203 PRInt8 mIsRTL;
207 * nsCocoaIMEHandler manages:
208 * 1. The IME/keyboard layout statement of nsChildView.
209 * 2. The IME composition statement of nsChildView.
210 * And also provides the methods which controls the current IME transaction of
211 * the instance.
213 * Note that an nsChildView handles one or more NSView's events. E.g., even if
214 * a text editor on XUL panel element, the input events handled on the parent
215 * (or its ancestor) widget handles it (the native focus is set to it). The
216 * actual focused view is notified by OnFocusChangeInGecko.
218 * NOTE: This class must not be used directly. The purpose of this class is
219 * to protect the IME related code from non-IME using developers.
220 * Use nsCocoaTextInputHandler class which inherits this class.
223 class nsCocoaIMEHandler
225 public:
226 nsCocoaIMEHandler();
227 virtual ~nsCocoaIMEHandler();
229 virtual void Init(nsChildView* aOwner);
231 virtual void OnFocusChangeInGecko(PRBool aFocus);
232 virtual void OnDestroyView(NSView<mozView> *aDestroyingView);
234 void OnStartIMEComposition(NSView<mozView> *aView);
235 void OnUpdateIMEComposition(NSString* aIMECompositionString);
236 void OnEndIMEComposition();
238 PRBool IsIMEComposing() { return mIsIMEComposing; }
239 PRBool IsIMEOpened();
240 PRBool IsIMEEnabled() { return mIsIMEEnabled; }
241 PRBool IsASCIICapableOnly() { return mIsASCIICapableOnly; }
242 PRBool IgnoreIMECommit() { return mIgnoreIMECommit; }
244 PRBool IgnoreIMEComposition()
246 // Ignore the IME composition events when we're pending to discard the
247 // composition and we are not to handle the IME composition now.
248 return (mPendingMethods & kDiscardIMEComposition) &&
249 (mIsInFocusProcessing || !IsFocused());
252 void CommitIMEComposition();
253 void CancelIMEComposition();
255 void EnableIME(PRBool aEnableIME);
256 void SetIMEOpenState(PRBool aOpen);
257 void SetASCIICapableOnly(PRBool aASCIICapableOnly);
259 static CFArrayRef CreateAllIMEModeList();
260 static void DebugPrintAllIMEModes(PRLogModuleInfo* aLogModuleInfo);
262 // Don't use ::TSMGetActiveDocument() API directly, the document may not
263 // be what you want.
264 static TSMDocumentID GetCurrentTSMDocumentID();
266 protected:
267 // The owner of this instance. The result of mOwnerWidget->TextInputHandler
268 // returns this instance. This must not be null after initialized.
269 nsChildView* mOwnerWidget;
271 // The native focused view, this is the native NSView of mOwnerWidget.
272 // This view handling the actual text inputting.
273 NSView<mozView>* mView;
275 // We cannot do some jobs in the given stack by some reasons.
276 // Following flags and the timer provide the execution pending mechanism,
277 // See the comment in nsCocoaTextInputHandler.mm.
278 nsCOMPtr<nsITimer> mTimer;
279 enum {
280 kResetIMEWindowLevel = 1,
281 kDiscardIMEComposition = 2,
282 kSyncASCIICapableOnly = 4
284 PRUint32 mPendingMethods;
286 PRBool IsFocused();
287 void ResetTimer();
289 virtual void ExecutePendingMethods();
291 private:
292 // If mIsIMEComposing is true, the composition string is stored here.
293 NSString* mIMECompositionString;
295 PRPackedBool mIsIMEComposing;
296 PRPackedBool mIsIMEEnabled;
297 PRPackedBool mIsASCIICapableOnly;
298 PRPackedBool mIgnoreIMECommit;
299 // This flag is enabled by OnFocusChangeInGecko, and will be cleared by
300 // ExecutePendingMethods. When this is true, IsFocus() returns TRUE. At
301 // that time, the focus processing in Gecko might not be finished yet. So,
302 // you cannot use nsQueryContentEvent or something.
303 PRPackedBool mIsInFocusProcessing;
305 void KillIMEComposition();
306 void SendCommittedText(NSString *aString);
307 void OpenSystemPreferredLanguageIME();
309 // Pending methods
310 void ResetIMEWindowLevel();
311 void DiscardIMEComposition();
312 void SyncASCIICapableOnly();
314 static PRBool sStaticMembersInitialized;
315 static CFStringRef sLatestIMEOpenedModeInputSourceID;
316 static void InitStaticMembers();
317 static void OnCurrentTextInputSourceChange(CFNotificationCenterRef aCenter,
318 void* aObserver,
319 CFStringRef aName,
320 const void* aObject,
321 CFDictionaryRef aUserInfo);
323 static void FlushPendingMethods(nsITimer* aTimer, void* aClosure);
325 // The focused IME handler. Please note that the handler might lost the
326 // actual focus by deactivating the application. If we are active, this
327 // must have the actual focused handle.
328 // We cannot access to the NSInputManager during we aren't active, so, the
329 // focused handler can have an IME transaction even if we are deactive.
330 static nsCocoaIMEHandler* sFocusedIMEHandler;
334 * nsCocoaTextInputHandler is going to implement the NSTextInput protocol.
336 class nsCocoaTextInputHandler : public nsCocoaIMEHandler
338 public:
339 static CFArrayRef CreateAllKeyboardLayoutList();
340 static void DebugPrintAllKeyboardLayouts(PRLogModuleInfo* aLogModuleInfo);
342 nsCocoaTextInputHandler();
343 virtual ~nsCocoaTextInputHandler();
346 #endif // nsCocoaTextInputHandler_h_