CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / widget / src / windows / nsIMM32Handler.h
blobd28b32c8ef4a0a16d35f3d48ea6c5a680e0950ac
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Masayuki Nakano <masayuki@d-toybox.com>
25 * Original nsWindow.h Contributor(s):
26 * Robert O'Callahan <roc+moz@cs.cmu.edu>
27 * Dean Tessman <dean_tessman@hotmail.com>
28 * Makoto Kato <m_kato@ga2.so-net.ne.jp>
29 * Dainis Jonitis <Dainis_Jonitis@swh-t.lv>
30 * Masayuki Nakano <masayuki@d-toybox.com>
31 * Ningjie Chen <chenn@email.uc.edu>
33 * Alternatively, the contents of this file may be used under the terms of
34 * either the GNU General Public License Version 2 or later (the "GPL"), or
35 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
36 * in which case the provisions of the GPL or the LGPL are applicable instead
37 * of those above. If you wish to allow use of your version of this file only
38 * under the terms of either the GPL or the LGPL, and not to allow others to
39 * use your version of this file under the terms of the MPL, indicate your
40 * decision by deleting the provisions above and replace them with the notice
41 * and other provisions required by the GPL or the LGPL. If you do not delete
42 * the provisions above, a recipient may use your version of this file under
43 * the terms of any one of the MPL, the GPL or the LGPL.
45 * ***** END LICENSE BLOCK ***** */
47 #ifndef nsIMM32Handler_h__
48 #define nsIMM32Handler_h__
50 #include "nscore.h"
51 #include <windows.h>
52 #include "nsString.h"
53 #include "nsGUIEvent.h"
54 #include "nsTArray.h"
56 class nsIWidget;
57 class nsWindow;
58 struct nsIntRect;
60 #ifndef WINCE
61 #define ENABLE_IME_MOUSE_HANDLING 1
62 #endif // WINCE
64 #define NS_WM_IMEFIRST WM_IME_SETCONTEXT
65 #define NS_WM_IMELAST WM_IME_KEYUP
67 class nsIMEContext
69 public:
70 nsIMEContext(HWND aWnd) : mWnd(aWnd)
72 mIMC = ::ImmGetContext(mWnd);
75 ~nsIMEContext()
77 if (mIMC) {
78 ::ImmReleaseContext(mWnd, mIMC);
79 mIMC = nsnull;
83 HIMC get() const
85 return mIMC;
88 PRBool IsValid() const
90 return !!mIMC;
93 protected:
94 nsIMEContext()
96 NS_ERROR("Don't create nsIMEContext without window handle");
99 nsIMEContext(const nsIMEContext &aSrc) : mWnd(nsnull), mIMC(nsnull)
101 NS_ERROR("Don't copy nsIMEContext");
104 HWND mWnd;
105 HIMC mIMC;
108 class nsIMM32Handler
110 public:
111 static void Initialize();
112 static void Terminate();
113 // The result of Process* method mean "The message was processed, don't
114 // process the message in the caller (nsWindow)" when it's TRUE. At that
115 // time, aEatMessage means that the message should be passed to next WndProc
116 // when it's FALSE, otherwise, the message should be eaten by us. When the
117 // result is FALSE, aEatMessage doesn't have any meaning. Then, the caller
118 // should continue to process the message.
119 static PRBool ProcessMessage(nsWindow* aWindow, UINT msg,
120 WPARAM &wParam, LPARAM &lParam,
121 LRESULT *aRetValue, PRBool &aEatMessage);
122 static PRBool IsComposing()
124 return IsComposingOnOurEditor() || IsComposingOnPlugin();
126 static PRBool IsComposingOn(nsWindow* aWindow)
128 return IsComposing() && IsComposingWindow(aWindow);
130 static PRBool IsStatusChanged() { return sIsStatusChanged; }
132 static PRBool IsDoingKakuteiUndo(HWND aWnd);
134 static void NotifyEndStatusChange() { sIsStatusChanged = PR_FALSE; }
136 static PRBool CanOptimizeKeyAndIMEMessages(MSG *aNextKeyOrIMEMessage);
138 // If aForce is TRUE, these methods doesn't check whether we have composition
139 // or not. If you don't set it to TRUE, these method doesn't commit/cancel
140 // the composition on uexpected window.
141 static void CommitComposition(nsWindow* aWindow, PRBool aForce = PR_FALSE);
142 static void CancelComposition(nsWindow* aWindow, PRBool aForce = PR_FALSE);
144 protected:
145 static void EnsureHandlerInstance();
147 static PRBool IsComposingOnOurEditor();
148 static PRBool IsComposingOnPlugin();
149 static PRBool IsComposingWindow(nsWindow* aWindow);
151 static PRBool ShouldDrawCompositionStringOurselves();
152 static void InitKeyboardLayout(HKL aKeyboardLayout);
153 static UINT GetKeyboardCodePage();
156 * Checks whether the window is top level window of the composing window.
157 * In this method, the top level window means in all windows, not only in all
158 * OUR windows. I.e., if the aWindow is embedded, this always returns FALSE.
160 static PRBool IsTopLevelWindowOfComposition(nsWindow* aWindow);
162 static PRBool ProcessInputLangChangeMessage(nsWindow* aWindow,
163 WPARAM wParam,
164 LPARAM lParam,
165 LRESULT *aRetValue,
166 PRBool &aEatMessage);
167 static PRBool ProcessMessageForPlugin(nsWindow* aWindow, UINT msg,
168 WPARAM &wParam, LPARAM &lParam,
169 LRESULT *aRetValue,
170 PRBool &aEatMessage);
172 nsIMM32Handler();
173 ~nsIMM32Handler();
175 // The result of following On*Event methods means "The message was processed,
176 // don't process the message in the caller (nsWindow)".
177 #ifdef ENABLE_IME_MOUSE_HANDLING
178 PRBool OnMouseEvent(nsWindow* aWindow, LPARAM lParam, int aAction);
179 #endif // ENABLE_IME_MOUSE_HANDLING
180 static PRBool OnKeyDownEvent(nsWindow* aWindow, WPARAM wParam, LPARAM lParam,
181 PRBool &aEatMessage);
183 // The result of On* methods mean "eat this message" when it's TRUE.
184 PRBool OnIMEStartComposition(nsWindow* aWindow);
185 PRBool OnIMEStartCompositionOnPlugin(nsWindow* aWindow,
186 WPARAM wParam, LPARAM lParam);
187 PRBool OnIMEComposition(nsWindow* aWindow, WPARAM wParam, LPARAM lParam);
188 PRBool OnIMECompositionOnPlugin(nsWindow* aWindow,
189 WPARAM wParam, LPARAM lParam);
190 PRBool OnIMEEndComposition(nsWindow* aWindow);
191 PRBool OnIMEEndCompositionOnPlugin(nsWindow* aWindow,
192 WPARAM wParam, LPARAM lParam);
193 PRBool OnIMERequest(nsWindow* aWindow, WPARAM wParam, LPARAM lParam,
194 LRESULT *aResult);
195 PRBool OnIMECharOnPlugin(nsWindow* aWindow, WPARAM wParam, LPARAM lParam);
196 PRBool OnChar(nsWindow* aWindow, WPARAM wParam, LPARAM lParam);
197 PRBool OnCharOnPlugin(nsWindow* aWindow, WPARAM wParam, LPARAM lParam);
198 PRBool OnInputLangChange(nsWindow* aWindow, WPARAM wParam, LPARAM lParam);
200 // These message handlers don't use instance members, we should not create
201 // the instance by the messages. So, they should be static.
202 static PRBool OnIMEChar(nsWindow* aWindow, WPARAM wParam, LPARAM lParam);
203 static PRBool OnIMESetContext(nsWindow* aWindow,
204 WPARAM wParam, LPARAM lParam,
205 LRESULT *aResult);
206 static PRBool OnIMESetContextOnPlugin(nsWindow* aWindow,
207 WPARAM wParam, LPARAM lParam,
208 LRESULT *aResult);
209 static PRBool OnIMECompositionFull(nsWindow* aWindow);
210 static PRBool OnIMENotify(nsWindow* aWindow, WPARAM wParam, LPARAM lParam);
211 static PRBool OnIMESelect(nsWindow* aWindow, WPARAM wParam, LPARAM lParam);
213 // The result of Handle* method mean "Processed" when it's TRUE.
214 void HandleStartComposition(nsWindow* aWindow,
215 const nsIMEContext &aIMEContext);
216 PRBool HandleComposition(nsWindow* aWindow, const nsIMEContext &aIMEContext,
217 LPARAM lParam);
218 void HandleEndComposition(nsWindow* aWindow);
219 PRBool HandleReconvert(nsWindow* aWindow, LPARAM lParam, LRESULT *oResult);
220 PRBool HandleQueryCharPosition(nsWindow* aWindow, LPARAM lParam,
221 LRESULT *oResult);
222 PRBool HandleDocumentFeed(nsWindow* aWindow, LPARAM lParam, LRESULT *oResult);
225 * When a window's IME context is activating but we have composition on
226 * another window, we should commit our composition because IME context is
227 * shared by all our windows (including plug-ins).
228 * @param aWindow is a new activated window.
229 * If aWindow is our composing window, this method does nothing.
230 * Otherwise, this commits the composition on the previous window.
231 * If this method did commit a composition, this returns TRUE.
233 PRBool CommitCompositionOnPreviousWindow(nsWindow* aWindow);
236 * ResolveIMECaretPos
237 * Convert the caret rect of a composition event to another widget's
238 * coordinate system.
240 * @param aReferenceWidget The origin widget of aCursorRect.
241 * Typically, this is mReferenceWidget of the
242 * composing events. If the aCursorRect is in screen
243 * coordinates, set nsnull.
244 * @param aCursorRect The cursor rect.
245 * @param aNewOriginWidget aOutRect will be in this widget's coordinates. If
246 * this is nsnull, aOutRect will be in screen
247 * coordinates.
248 * @param aOutRect The converted cursor rect.
250 void ResolveIMECaretPos(nsIWidget* aReferenceWidget,
251 nsIntRect& aCursorRect,
252 nsIWidget* aNewOriginWidget,
253 nsIntRect& aOutRect);
255 PRBool ConvertToANSIString(const nsAFlatString& aStr,
256 UINT aCodePage,
257 nsACString& aANSIStr);
259 PRBool SetIMERelatedWindowsPos(nsWindow* aWindow,
260 const nsIMEContext &aIMEContext);
261 PRBool GetCharacterRectOfSelectedTextAt(nsWindow* aWindow,
262 PRUint32 aOffset,
263 nsIntRect &aCharRect);
264 PRBool GetCaretRect(nsWindow* aWindow, nsIntRect &aCaretRect);
265 void GetCompositionString(const nsIMEContext &aIMEContext, DWORD aIndex);
267 * Get the current target clause of composition string.
268 * If there are one or more characters whose attribute is ATTR_TARGET_*,
269 * this returns the first character's offset and its length.
270 * Otherwise, e.g., the all characters are ATTR_INPUT, this returns
271 * the composition string range because the all is the current target.
273 * aLength can be null (default), but aOffset must not be null.
275 * The aOffset value is offset in the contents. So, when you need offset
276 * in the composition string, you need to subtract mCompositionStart from it.
278 PRBool GetTargetClauseRange(PRUint32 *aOffset, PRUint32 *aLength = nsnull);
279 void DispatchTextEvent(nsWindow* aWindow, const nsIMEContext &aIMEContext,
280 PRBool aCheckAttr = PR_TRUE);
281 void SetTextRangeList(nsTArray<nsTextRange> &aTextRangeList);
283 nsresult EnsureClauseArray(PRInt32 aCount);
284 nsresult EnsureAttributeArray(PRInt32 aCount);
287 * When WM_IME_CHAR is received and passed to DefWindowProc, we need to
288 * record the messages. In other words, we should record the messages
289 * when we receive WM_IME_CHAR on windowless plug-in (if we have focus,
290 * we always eat them). When focus is moved from a windowless plug-in to
291 * our window during composition, WM_IME_CHAR messages were received when
292 * the plug-in has focus. However, WM_CHAR messages are received after the
293 * plug-in lost focus. So, we need to ignore the WM_CHAR messages because
294 * they make unexpected text input events on us.
296 nsTArray<MSG> mPassedIMEChar;
298 PRBool IsIMECharRecordsEmpty()
300 return mPassedIMEChar.IsEmpty();
302 void ResetIMECharRecords()
304 mPassedIMEChar.Clear();
306 void DequeueIMECharRecords(WPARAM &wParam, LPARAM &lParam)
308 MSG msg = mPassedIMEChar.ElementAt(0);
309 wParam = msg.wParam;
310 lParam = msg.lParam;
311 mPassedIMEChar.RemoveElementAt(0);
313 void EnqueueIMECharRecords(WPARAM wParam, LPARAM lParam)
315 MSG msg;
316 msg.wParam = wParam;
317 msg.lParam = lParam;
318 mPassedIMEChar.AppendElement(msg);
321 nsWindow* mComposingWindow;
322 nsString mCompositionString;
323 nsTArray<PRUint32> mClauseArray;
324 nsTArray<PRUint8> mAttributeArray;
326 PRInt32 mCursorPosition;
327 PRUint32 mCompositionStart;
329 PRPackedBool mIsComposing;
330 PRPackedBool mIsComposingOnPlugin;
331 PRPackedBool mNativeCaretIsCreated;
333 static PRPackedBool sIsStatusChanged;
334 static PRPackedBool sIsIME;
335 static PRPackedBool sIsIMEOpening;
337 #ifndef WINCE
338 static UINT sCodePage;
339 static DWORD sIMEProperty;
340 #endif // #ifndef WINCE
343 #endif // nsIMM32Handler_h__