|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtGui module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qwininputcontext_p.h" |
|
43 #include "qinputcontext_p.h" |
|
44 |
|
45 #include "qfont.h" |
|
46 #include "qwidget.h" |
|
47 #include "qapplication.h" |
|
48 #include "qevent.h" |
|
49 #include "qtextformat.h" |
|
50 #include "qtextboundaryfinder.h" |
|
51 |
|
52 //#define Q_IME_DEBUG |
|
53 |
|
54 #ifdef Q_IME_DEBUG |
|
55 #include "qdebug.h" |
|
56 #endif |
|
57 |
|
58 #if defined(Q_WS_WINCE) |
|
59 extern void qt_wince_show_SIP(bool show); // defined in qguifunctions_wince.cpp |
|
60 #endif |
|
61 |
|
62 QT_BEGIN_NAMESPACE |
|
63 |
|
64 extern bool qt_sendSpontaneousEvent(QObject*, QEvent*); |
|
65 |
|
66 |
|
67 DEFINE_GUID(IID_IActiveIMMApp, |
|
68 0x08c0e040, 0x62d1, 0x11d1, 0x93, 0x26, 0x0, 0x60, 0xb0, 0x67, 0xb8, 0x6e); |
|
69 |
|
70 |
|
71 |
|
72 DEFINE_GUID(CLSID_CActiveIMM, |
|
73 0x4955DD33, 0xB159, 0x11d0, 0x8F, 0xCF, 0x0, 0xAA, 0x00, 0x6B, 0xCC, 0x59); |
|
74 |
|
75 |
|
76 |
|
77 DEFINE_GUID(IID_IActiveIMMMessagePumpOwner, |
|
78 0xb5cf2cfa, 0x8aeb, 0x11d1, 0x93, 0x64, 0x0, 0x60, 0xb0, 0x67, 0xb8, 0x6e); |
|
79 |
|
80 |
|
81 |
|
82 interface IEnumRegisterWordW; |
|
83 interface IEnumInputContext; |
|
84 |
|
85 |
|
86 bool qt_sendSpontaneousEvent(QObject*, QEvent*); |
|
87 |
|
88 |
|
89 #define IFMETHOD HRESULT STDMETHODCALLTYPE |
|
90 |
|
91 interface IActiveIMMApp : public IUnknown |
|
92 { |
|
93 public: |
|
94 virtual IFMETHOD AssociateContext(HWND hWnd, HIMC hIME, HIMC __RPC_FAR *phPrev) = 0; |
|
95 virtual IFMETHOD dummy_ConfigureIMEA() = 0; |
|
96 virtual IFMETHOD ConfigureIMEW(HKL hKL, HWND hWnd, DWORD dwMode, REGISTERWORDW __RPC_FAR *pData) = 0; |
|
97 virtual IFMETHOD CreateContext(HIMC __RPC_FAR *phIMC) = 0; |
|
98 virtual IFMETHOD DestroyContext(HIMC hIME) = 0; |
|
99 virtual IFMETHOD dummy_EnumRegisterWordA() = 0; |
|
100 virtual IFMETHOD EnumRegisterWordW(HKL hKL, LPWSTR szReading, DWORD dwStyle, LPWSTR szRegister, LPVOID pData, |
|
101 IEnumRegisterWordW __RPC_FAR *__RPC_FAR *pEnum) = 0; |
|
102 virtual IFMETHOD dummy_EscapeA() = 0; |
|
103 virtual IFMETHOD EscapeW(HKL hKL, HIMC hIMC, UINT uEscape, LPVOID pData, LRESULT __RPC_FAR *plResult) = 0; |
|
104 virtual IFMETHOD dummy_GetCandidateListA() = 0; |
|
105 virtual IFMETHOD GetCandidateListW(HIMC hIMC, DWORD dwIndex, UINT uBufLen, CANDIDATELIST __RPC_FAR *pCandList, |
|
106 UINT __RPC_FAR *puCopied) = 0; |
|
107 virtual IFMETHOD dummy_GetCandidateListCountA() = 0; |
|
108 virtual IFMETHOD GetCandidateListCountW(HIMC hIMC, DWORD __RPC_FAR *pdwListSize, DWORD __RPC_FAR *pdwBufLen) = 0; |
|
109 virtual IFMETHOD GetCandidateWindow(HIMC hIMC, DWORD dwIndex, CANDIDATEFORM __RPC_FAR *pCandidate) = 0; |
|
110 virtual IFMETHOD dummy_GetCompositionFontA() = 0; |
|
111 virtual IFMETHOD GetCompositionFontW(HIMC hIMC, LOGFONTW __RPC_FAR *plf) = 0; |
|
112 virtual IFMETHOD dummy_GetCompositionStringA() = 0; |
|
113 virtual IFMETHOD GetCompositionStringW(HIMC hIMC, DWORD dwIndex, DWORD dwBufLen, LONG __RPC_FAR *plCopied, LPVOID pBuf) = 0; |
|
114 virtual IFMETHOD GetCompositionWindow(HIMC hIMC, COMPOSITIONFORM __RPC_FAR *pCompForm) = 0; |
|
115 virtual IFMETHOD GetContext(HWND hWnd, HIMC __RPC_FAR *phIMC) = 0; |
|
116 virtual IFMETHOD dummy_GetConversionListA() = 0; |
|
117 virtual IFMETHOD GetConversionListW(HKL hKL, HIMC hIMC, LPWSTR pSrc, UINT uBufLen, UINT uFlag, |
|
118 CANDIDATELIST __RPC_FAR *pDst, UINT __RPC_FAR *puCopied) = 0; |
|
119 virtual IFMETHOD GetConversionStatus(HIMC hIMC, DWORD __RPC_FAR *pfdwConversion, DWORD __RPC_FAR *pfdwSentence) = 0; |
|
120 virtual IFMETHOD GetDefaultIMEWnd(HWND hWnd, HWND __RPC_FAR *phDefWnd) = 0; |
|
121 virtual IFMETHOD dummy_GetDescriptionA() = 0; |
|
122 virtual IFMETHOD GetDescriptionW(HKL hKL, UINT uBufLen, LPWSTR szDescription, UINT __RPC_FAR *puCopied) = 0; |
|
123 virtual IFMETHOD dummy_GetGuideLineA() = 0; |
|
124 virtual IFMETHOD GetGuideLineW(HIMC hIMC, DWORD dwIndex, DWORD dwBufLen, LPWSTR pBuf, DWORD __RPC_FAR *pdwResult) = 0; |
|
125 virtual IFMETHOD dummy_GetIMEFileNameA() = 0; |
|
126 virtual IFMETHOD GetIMEFileNameW(HKL hKL, UINT uBufLen, LPWSTR szFileName, UINT __RPC_FAR *puCopied) = 0; |
|
127 virtual IFMETHOD GetOpenStatus(HIMC hIMC) = 0; |
|
128 virtual IFMETHOD GetProperty(HKL hKL, DWORD fdwIndex, DWORD __RPC_FAR *pdwProperty) = 0; |
|
129 virtual IFMETHOD dummy_GetRegisterWordStyleA() = 0; |
|
130 virtual IFMETHOD GetRegisterWordStyleW(HKL hKL, UINT nItem, STYLEBUFW __RPC_FAR *pStyleBuf, UINT __RPC_FAR *puCopied) = 0; |
|
131 virtual IFMETHOD GetStatusWindowPos(HIMC hIMC, POINT __RPC_FAR *pptPos) = 0; |
|
132 virtual IFMETHOD GetVirtualKey(HWND hWnd, UINT __RPC_FAR *puVirtualKey) = 0; |
|
133 virtual IFMETHOD dummy_InstallIMEA() = 0; |
|
134 virtual IFMETHOD InstallIMEW(LPWSTR szIMEFileName, LPWSTR szLayoutText, HKL __RPC_FAR *phKL) = 0; |
|
135 virtual IFMETHOD IsIME(HKL hKL) = 0; |
|
136 virtual IFMETHOD dummy_IsUIMessageA() = 0; |
|
137 virtual IFMETHOD IsUIMessageW(HWND hWndIME, UINT msg, WPARAM wParam, LPARAM lParam) = 0; |
|
138 virtual IFMETHOD NotifyIME(HIMC hIMC, DWORD dwAction, DWORD dwIndex, DWORD dwValue) = 0; |
|
139 virtual IFMETHOD dummy_RegisterWordA() = 0; |
|
140 virtual IFMETHOD RegisterWordW(HKL hKL, LPWSTR szReading, DWORD dwStyle, LPWSTR szRegister) = 0; |
|
141 virtual IFMETHOD ReleaseContext(HWND hWnd, HIMC hIMC) = 0; |
|
142 virtual IFMETHOD SetCandidateWindow(HIMC hIMC, CANDIDATEFORM __RPC_FAR *pCandidate) = 0; |
|
143 virtual IFMETHOD SetCompositionFontA(HIMC hIMC, LOGFONTA __RPC_FAR *plf) = 0; |
|
144 virtual IFMETHOD SetCompositionFontW(HIMC hIMC, LOGFONTW __RPC_FAR *plf) = 0; |
|
145 virtual IFMETHOD dummy_SetCompositionStringA() = 0; |
|
146 virtual IFMETHOD SetCompositionStringW(HIMC hIMC, DWORD dwIndex, LPVOID pComp, DWORD dwCompLen, |
|
147 LPVOID pRead, DWORD dwReadLen) = 0; |
|
148 virtual IFMETHOD SetCompositionWindow(HIMC hIMC, COMPOSITIONFORM __RPC_FAR *pCompForm) = 0; |
|
149 virtual IFMETHOD SetConversionStatus(HIMC hIMC, DWORD fdwConversion, DWORD fdwSentence) = 0; |
|
150 virtual IFMETHOD SetOpenStatus(HIMC hIMC, BOOL fOpen) = 0; |
|
151 virtual IFMETHOD SetStatusWindowPos(HIMC hIMC, POINT __RPC_FAR *pptPos) = 0; |
|
152 virtual IFMETHOD SimulateHotKey(HWND hWnd, DWORD dwHotKeyID) = 0; |
|
153 virtual IFMETHOD dummy_UnregisterWordA() = 0; |
|
154 virtual IFMETHOD UnregisterWordW(HKL hKL, LPWSTR szReading, DWORD dwStyle, LPWSTR szUnregister) = 0; |
|
155 virtual IFMETHOD Activate(BOOL fRestoreLayout) = 0; |
|
156 virtual IFMETHOD Deactivate(void) = 0; |
|
157 virtual IFMETHOD OnDefWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, LRESULT __RPC_FAR *plResult) = 0; |
|
158 virtual IFMETHOD FilterClientWindows(ATOM __RPC_FAR *aaClassList, UINT uSize) = 0; |
|
159 virtual IFMETHOD dummy_GetCodePageA() = 0; |
|
160 virtual IFMETHOD GetLangId(HKL hKL, LANGID __RPC_FAR *plid) = 0; |
|
161 virtual IFMETHOD AssociateContextEx(HWND hWnd, HIMC hIMC, DWORD dwFlags) = 0; |
|
162 virtual IFMETHOD DisableIME(DWORD idThread) = 0; |
|
163 virtual IFMETHOD dummy_GetImeMenuItemsA() = 0; |
|
164 virtual IFMETHOD GetImeMenuItemsW(HIMC hIMC, DWORD dwFlags, DWORD dwType, /*IMEMENUITEMINFOW*/ void __RPC_FAR *pImeParentMenu, |
|
165 /*IMEMENUITEMINFOW*/ void __RPC_FAR *pImeMenu, DWORD dwSize, DWORD __RPC_FAR *pdwResult) = 0; |
|
166 virtual IFMETHOD EnumInputContext(DWORD idThread, IEnumInputContext __RPC_FAR *__RPC_FAR *ppEnum) = 0; |
|
167 }; |
|
168 |
|
169 interface IActiveIMMMessagePumpOwner : public IUnknown |
|
170 { |
|
171 public: |
|
172 virtual IFMETHOD Start(void) = 0; |
|
173 virtual IFMETHOD End(void) = 0; |
|
174 virtual IFMETHOD OnTranslateMessage(const MSG __RPC_FAR *pMsg) = 0; |
|
175 virtual IFMETHOD Pause(DWORD __RPC_FAR *pdwCookie) = 0; |
|
176 virtual IFMETHOD Resume(DWORD dwCookie) = 0; |
|
177 }; |
|
178 |
|
179 |
|
180 static IActiveIMMApp *aimm = 0; |
|
181 static IActiveIMMMessagePumpOwner *aimmpump = 0; |
|
182 static QString *imeComposition = 0; |
|
183 static int imePosition = -1; |
|
184 bool qt_use_rtl_extensions = false; |
|
185 static bool haveCaret = false; |
|
186 |
|
187 #ifndef LGRPID_INSTALLED |
|
188 #define LGRPID_INSTALLED 0x00000001 // installed language group ids |
|
189 #define LGRPID_SUPPORTED 0x00000002 // supported language group ids |
|
190 #endif |
|
191 |
|
192 #ifndef LGRPID_ARABIC |
|
193 #define LGRPID_WESTERN_EUROPE 0x0001 // Western Europe & U.S. |
|
194 #define LGRPID_CENTRAL_EUROPE 0x0002 // Central Europe |
|
195 #define LGRPID_BALTIC 0x0003 // Baltic |
|
196 #define LGRPID_GREEK 0x0004 // Greek |
|
197 #define LGRPID_CYRILLIC 0x0005 // Cyrillic |
|
198 #define LGRPID_TURKISH 0x0006 // Turkish |
|
199 #define LGRPID_JAPANESE 0x0007 // Japanese |
|
200 #define LGRPID_KOREAN 0x0008 // Korean |
|
201 #define LGRPID_TRADITIONAL_CHINESE 0x0009 // Traditional Chinese |
|
202 #define LGRPID_SIMPLIFIED_CHINESE 0x000a // Simplified Chinese |
|
203 #define LGRPID_THAI 0x000b // Thai |
|
204 #define LGRPID_HEBREW 0x000c // Hebrew |
|
205 #define LGRPID_ARABIC 0x000d // Arabic |
|
206 #define LGRPID_VIETNAMESE 0x000e // Vietnamese |
|
207 #define LGRPID_INDIC 0x000f // Indic |
|
208 #define LGRPID_GEORGIAN 0x0010 // Georgian |
|
209 #define LGRPID_ARMENIAN 0x0011 // Armenian |
|
210 #endif |
|
211 |
|
212 static DWORD WM_MSIME_MOUSE = 0; |
|
213 |
|
214 QWinInputContext::QWinInputContext(QObject *parent) |
|
215 : QInputContext(parent), recursionGuard(false) |
|
216 { |
|
217 #ifndef Q_WS_WINCE |
|
218 QSysInfo::WinVersion ver = QSysInfo::windowsVersion(); |
|
219 if (ver & QSysInfo::WV_NT_based && ver >= QSysInfo::WV_VISTA) { |
|
220 // Since the IsValidLanguageGroup/IsValidLocale functions always return true on |
|
221 // Vista, check the Keyboard Layouts for enabling RTL. |
|
222 UINT nLayouts = GetKeyboardLayoutList(0, 0); |
|
223 if (nLayouts) { |
|
224 HKL *lpList = new HKL[nLayouts]; |
|
225 GetKeyboardLayoutList(nLayouts, lpList); |
|
226 for (int i = 0; i<(int)nLayouts; i++) { |
|
227 WORD plangid = PRIMARYLANGID((quintptr)lpList[i]); |
|
228 if (plangid == LANG_ARABIC |
|
229 || plangid == LANG_HEBREW |
|
230 || plangid == LANG_FARSI |
|
231 #ifdef LANG_SYRIAC |
|
232 || plangid == LANG_SYRIAC |
|
233 #endif |
|
234 ) { |
|
235 qt_use_rtl_extensions = true; |
|
236 break; |
|
237 } |
|
238 } |
|
239 delete []lpList; |
|
240 } |
|
241 } else { |
|
242 // figure out whether a RTL language is installed |
|
243 qt_use_rtl_extensions = IsValidLanguageGroup(LGRPID_ARABIC, LGRPID_INSTALLED) |
|
244 || IsValidLanguageGroup(LGRPID_HEBREW, LGRPID_INSTALLED) |
|
245 || IsValidLocale(MAKELCID(MAKELANGID(LANG_ARABIC, SUBLANG_DEFAULT), SORT_DEFAULT), LCID_INSTALLED) |
|
246 || IsValidLocale(MAKELCID(MAKELANGID(LANG_HEBREW, SUBLANG_DEFAULT), SORT_DEFAULT), LCID_INSTALLED) |
|
247 #ifdef LANG_SYRIAC |
|
248 || IsValidLocale(MAKELCID(MAKELANGID(LANG_SYRIAC, SUBLANG_DEFAULT), SORT_DEFAULT), LCID_INSTALLED) |
|
249 #endif |
|
250 || IsValidLocale(MAKELCID(MAKELANGID(LANG_FARSI, SUBLANG_DEFAULT), SORT_DEFAULT), LCID_INSTALLED); |
|
251 } |
|
252 #else |
|
253 qt_use_rtl_extensions = false; |
|
254 #endif |
|
255 |
|
256 WM_MSIME_MOUSE = RegisterWindowMessage(L"MSIMEMouseOperation"); |
|
257 } |
|
258 |
|
259 QWinInputContext::~QWinInputContext() |
|
260 { |
|
261 // release active input method if we have one |
|
262 if (aimm) { |
|
263 aimmpump->End(); |
|
264 aimmpump->Release(); |
|
265 aimm->Deactivate(); |
|
266 aimm->Release(); |
|
267 aimm = 0; |
|
268 aimmpump = 0; |
|
269 } |
|
270 delete imeComposition; |
|
271 imeComposition = 0; |
|
272 } |
|
273 |
|
274 static HWND getDefaultIMEWnd(HWND wnd) |
|
275 { |
|
276 HWND ime_wnd; |
|
277 if(aimm) |
|
278 aimm->GetDefaultIMEWnd(wnd, &ime_wnd); |
|
279 else |
|
280 ime_wnd = ImmGetDefaultIMEWnd(wnd); |
|
281 return ime_wnd; |
|
282 } |
|
283 |
|
284 static HIMC getContext(HWND wnd) |
|
285 { |
|
286 HIMC imc; |
|
287 if (aimm) |
|
288 aimm->GetContext(wnd, &imc); |
|
289 else |
|
290 imc = ImmGetContext(wnd); |
|
291 |
|
292 return imc; |
|
293 } |
|
294 |
|
295 static void releaseContext(HWND wnd, HIMC imc) |
|
296 { |
|
297 if (aimm) |
|
298 aimm->ReleaseContext(wnd, imc); |
|
299 else |
|
300 ImmReleaseContext(wnd, imc); |
|
301 } |
|
302 |
|
303 static void notifyIME(HIMC imc, DWORD dwAction, DWORD dwIndex, DWORD dwValue) |
|
304 { |
|
305 if (!imc) |
|
306 return; |
|
307 if (aimm) |
|
308 aimm->NotifyIME(imc, dwAction, dwIndex, dwValue); |
|
309 else |
|
310 ImmNotifyIME(imc, dwAction, dwIndex, dwValue); |
|
311 } |
|
312 |
|
313 static LONG getCompositionString(HIMC himc, DWORD dwIndex, LPVOID lpbuf, DWORD dBufLen) |
|
314 { |
|
315 LONG len = 0; |
|
316 if (aimm) |
|
317 aimm->GetCompositionStringW(himc, dwIndex, dBufLen, &len, lpbuf); |
|
318 else |
|
319 len = ImmGetCompositionString(himc, dwIndex, lpbuf, dBufLen); |
|
320 return len; |
|
321 } |
|
322 |
|
323 static int getCursorPosition(HIMC himc) |
|
324 { |
|
325 return getCompositionString(himc, GCS_CURSORPOS, 0, 0); |
|
326 } |
|
327 |
|
328 static QString getString(HIMC himc, DWORD dwindex, int *selStart = 0, int *selLength = 0) |
|
329 { |
|
330 static wchar_t *buffer = 0; |
|
331 static int buflen = 0; |
|
332 |
|
333 int len = getCompositionString(himc, dwindex, 0, 0) + 1; |
|
334 if (!buffer || len > buflen) { |
|
335 delete [] buffer; |
|
336 buflen = qMin(len, 256); |
|
337 buffer = new wchar_t[buflen]; |
|
338 } |
|
339 |
|
340 len = getCompositionString(himc, dwindex, buffer, buflen * sizeof(wchar_t)); |
|
341 |
|
342 if (selStart) { |
|
343 static wchar_t *attrbuffer = 0; |
|
344 static int attrbuflen = 0; |
|
345 int attrlen = getCompositionString(himc, dwindex, 0, 0) + 1; |
|
346 if (!attrbuffer || attrlen> attrbuflen) { |
|
347 delete [] attrbuffer; |
|
348 attrbuflen = qMin(attrlen, 256); |
|
349 attrbuffer = new wchar_t[attrbuflen]; |
|
350 } |
|
351 attrlen = getCompositionString(himc, GCS_COMPATTR, attrbuffer, attrbuflen * sizeof(wchar_t)); |
|
352 *selStart = attrlen+1; |
|
353 *selLength = -1; |
|
354 for (int i = 0; i < attrlen; i++) { |
|
355 if (attrbuffer[i] & ATTR_TARGET_CONVERTED) { |
|
356 *selStart = qMin(*selStart, i); |
|
357 *selLength = qMax(*selLength, i); |
|
358 } |
|
359 } |
|
360 *selLength = qMax(0, *selLength - *selStart + 1); |
|
361 } |
|
362 |
|
363 if (len <= 0) |
|
364 return QString(); |
|
365 |
|
366 return QString((QChar*)buffer, len / sizeof(QChar)); |
|
367 } |
|
368 |
|
369 void QWinInputContext::TranslateMessage(const MSG *msg) |
|
370 { |
|
371 if (!aimmpump || aimmpump->OnTranslateMessage(msg) != S_OK) |
|
372 ::TranslateMessage(msg); |
|
373 } |
|
374 |
|
375 LRESULT QWinInputContext::DefWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) |
|
376 { |
|
377 LRESULT retval; |
|
378 if (!aimm || aimm->OnDefWindowProc(hwnd, msg, wParam, lParam, &retval) != S_OK) |
|
379 { |
|
380 retval = ::DefWindowProc(hwnd, msg, wParam, lParam); |
|
381 } |
|
382 return retval; |
|
383 } |
|
384 |
|
385 |
|
386 void QWinInputContext::update() |
|
387 { |
|
388 QWidget *w = focusWidget(); |
|
389 if(!w) |
|
390 return; |
|
391 |
|
392 Q_ASSERT(w->testAttribute(Qt::WA_WState_Created)); |
|
393 HIMC imc = getContext(w->effectiveWinId()); |
|
394 |
|
395 if (!imc) |
|
396 return; |
|
397 |
|
398 QFont f = qvariant_cast<QFont>(w->inputMethodQuery(Qt::ImFont)); |
|
399 HFONT hf; |
|
400 hf = f.handle(); |
|
401 |
|
402 LOGFONT lf; |
|
403 if (GetObject(hf, sizeof(lf), &lf)) { |
|
404 if (aimm) |
|
405 aimm->SetCompositionFontW(imc, &lf); |
|
406 else |
|
407 ImmSetCompositionFont(imc, &lf); |
|
408 } |
|
409 |
|
410 QRect r = w->inputMethodQuery(Qt::ImMicroFocus).toRect(); |
|
411 |
|
412 // The ime window positions are based on the WinId with active focus. |
|
413 QWidget *imeWnd = QWidget::find(::GetFocus()); |
|
414 if (imeWnd && !aimm) { |
|
415 QPoint pt (r.topLeft()); |
|
416 pt = w->mapToGlobal(pt); |
|
417 pt = imeWnd->mapFromGlobal(pt); |
|
418 r.moveTo(pt); |
|
419 } |
|
420 |
|
421 COMPOSITIONFORM cf; |
|
422 // ### need X-like inputStyle config settings |
|
423 cf.dwStyle = CFS_FORCE_POSITION; |
|
424 cf.ptCurrentPos.x = r.x(); |
|
425 cf.ptCurrentPos.y = r.y(); |
|
426 |
|
427 CANDIDATEFORM candf; |
|
428 candf.dwIndex = 0; |
|
429 candf.dwStyle = CFS_EXCLUDE; |
|
430 candf.ptCurrentPos.x = r.x(); |
|
431 candf.ptCurrentPos.y = r.y() + r.height(); |
|
432 candf.rcArea.left = r.x(); |
|
433 candf.rcArea.top = r.y(); |
|
434 candf.rcArea.right = r.x() + r.width(); |
|
435 candf.rcArea.bottom = r.y() + r.height(); |
|
436 |
|
437 if(haveCaret) |
|
438 SetCaretPos(r.x(), r.y()); |
|
439 |
|
440 if (aimm) { |
|
441 aimm->SetCompositionWindow(imc, &cf); |
|
442 aimm->SetCandidateWindow(imc, &candf); |
|
443 } else { |
|
444 ImmSetCompositionWindow(imc, &cf); |
|
445 ImmSetCandidateWindow(imc, &candf); |
|
446 } |
|
447 |
|
448 releaseContext(w->effectiveWinId(), imc); |
|
449 } |
|
450 |
|
451 |
|
452 bool QWinInputContext::endComposition() |
|
453 { |
|
454 QWidget *fw = focusWidget(); |
|
455 #ifdef Q_IME_DEBUG |
|
456 qDebug("endComposition! fw = %s", fw ? fw->className() : "(null)"); |
|
457 #endif |
|
458 bool result = true; |
|
459 if(imePosition == -1 || recursionGuard) |
|
460 return result; |
|
461 |
|
462 // Googles Pinyin Input Method likes to call endComposition again |
|
463 // when we call notifyIME with CPS_CANCEL, so protect ourselves |
|
464 // against that. |
|
465 recursionGuard = true; |
|
466 |
|
467 if (fw) { |
|
468 Q_ASSERT(fw->testAttribute(Qt::WA_WState_Created)); |
|
469 HIMC imc = getContext(fw->effectiveWinId()); |
|
470 notifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0); |
|
471 releaseContext(fw->effectiveWinId(), imc); |
|
472 if(haveCaret) { |
|
473 DestroyCaret(); |
|
474 haveCaret = false; |
|
475 } |
|
476 } |
|
477 |
|
478 if (!fw) |
|
479 fw = QApplication::focusWidget(); |
|
480 |
|
481 if (fw) { |
|
482 QInputMethodEvent e; |
|
483 result = qt_sendSpontaneousEvent(fw, &e); |
|
484 } |
|
485 |
|
486 if (imeComposition) |
|
487 imeComposition->clear(); |
|
488 imePosition = -1; |
|
489 |
|
490 recursionGuard = false; |
|
491 |
|
492 return result; |
|
493 } |
|
494 |
|
495 void QWinInputContext::reset() |
|
496 { |
|
497 QWidget *fw = focusWidget(); |
|
498 |
|
499 #ifdef Q_IME_DEBUG |
|
500 qDebug("sending accept to focus widget %s", fw ? fw->className() : "(null)"); |
|
501 #endif |
|
502 |
|
503 if (fw && imePosition != -1) { |
|
504 QInputMethodEvent e; |
|
505 if (imeComposition) |
|
506 e.setCommitString(*imeComposition); |
|
507 imePosition = -1; |
|
508 qt_sendSpontaneousEvent(fw, &e); |
|
509 } |
|
510 |
|
511 if (imeComposition) |
|
512 imeComposition->clear(); |
|
513 imePosition = -1; |
|
514 |
|
515 if (fw) { |
|
516 Q_ASSERT(fw->testAttribute(Qt::WA_WState_Created)); |
|
517 HIMC imc = getContext(fw->effectiveWinId()); |
|
518 notifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0); |
|
519 releaseContext(fw->effectiveWinId(), imc); |
|
520 } |
|
521 |
|
522 } |
|
523 |
|
524 |
|
525 bool QWinInputContext::startComposition() |
|
526 { |
|
527 #ifdef Q_IME_DEBUG |
|
528 qDebug("startComposition"); |
|
529 #endif |
|
530 |
|
531 if (!imeComposition) |
|
532 imeComposition = new QString(); |
|
533 |
|
534 QWidget *fw = focusWidget(); |
|
535 if (fw) { |
|
536 Q_ASSERT(fw->testAttribute(Qt::WA_WState_Created)); |
|
537 imePosition = 0; |
|
538 haveCaret = CreateCaret(fw->effectiveWinId(), 0, 1, 1); |
|
539 HideCaret(fw->effectiveWinId()); |
|
540 update(); |
|
541 } |
|
542 return fw != 0; |
|
543 } |
|
544 |
|
545 enum StandardFormat { |
|
546 PreeditFormat, |
|
547 SelectionFormat |
|
548 }; |
|
549 |
|
550 bool QWinInputContext::composition(LPARAM lParam) |
|
551 { |
|
552 #ifdef Q_IME_DEBUG |
|
553 QString str; |
|
554 if (lParam & GCS_RESULTSTR) |
|
555 str += "RESULTSTR "; |
|
556 if (lParam & GCS_COMPSTR) |
|
557 str += "COMPSTR "; |
|
558 if (lParam & GCS_COMPATTR) |
|
559 str += "COMPATTR "; |
|
560 if (lParam & GCS_CURSORPOS) |
|
561 str += "CURSORPOS "; |
|
562 if (lParam & GCS_COMPCLAUSE) |
|
563 str += "COMPCLAUSE "; |
|
564 if (lParam & CS_INSERTCHAR) |
|
565 str += "INSERTCHAR "; |
|
566 if (lParam & CS_NOMOVECARET) |
|
567 str += "NOMOVECARET "; |
|
568 qDebug("composition, lParam=(%x) %s imePosition=%d", lParam, str.latin1(), imePosition); |
|
569 #endif |
|
570 |
|
571 bool result = true; |
|
572 |
|
573 if(!lParam) |
|
574 // bogus event |
|
575 return true; |
|
576 |
|
577 QWidget *fw = QApplication::focusWidget(); |
|
578 if (fw) { |
|
579 Q_ASSERT(fw->testAttribute(Qt::WA_WState_Created)); |
|
580 HIMC imc = getContext(fw->effectiveWinId()); |
|
581 QInputMethodEvent e; |
|
582 if (lParam & (GCS_COMPSTR | GCS_COMPATTR | GCS_CURSORPOS)) { |
|
583 if (imePosition == -1) |
|
584 // need to send a start event |
|
585 startComposition(); |
|
586 |
|
587 // some intermediate composition result |
|
588 int selStart, selLength; |
|
589 *imeComposition = getString(imc, GCS_COMPSTR, &selStart, &selLength); |
|
590 imePosition = getCursorPosition(imc); |
|
591 if (lParam & CS_INSERTCHAR && lParam & CS_NOMOVECARET) { |
|
592 // make korean work correctly. Hope this is correct for all IMEs |
|
593 selStart = 0; |
|
594 selLength = imeComposition->length(); |
|
595 } |
|
596 if(selLength == 0) |
|
597 selStart = 0; |
|
598 |
|
599 QList<QInputMethodEvent::Attribute> attrs; |
|
600 if (selStart > 0) |
|
601 attrs << QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, 0, selStart, |
|
602 standardFormat(PreeditFormat)); |
|
603 if (selLength) |
|
604 attrs << QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, selStart, selLength, |
|
605 standardFormat(SelectionFormat)); |
|
606 if (selStart + selLength < imeComposition->length()) |
|
607 attrs << QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, selStart + selLength, |
|
608 imeComposition->length() - selStart - selLength, |
|
609 standardFormat(PreeditFormat)); |
|
610 if(imePosition >= 0) |
|
611 attrs << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, imePosition, selLength ? 0 : 1, QVariant()); |
|
612 |
|
613 e = QInputMethodEvent(*imeComposition, attrs); |
|
614 } |
|
615 if (lParam & GCS_RESULTSTR) { |
|
616 if(imePosition == -1) |
|
617 startComposition(); |
|
618 // a fixed result, return the converted string |
|
619 *imeComposition = getString(imc, GCS_RESULTSTR); |
|
620 imePosition = -1; |
|
621 e.setCommitString(*imeComposition); |
|
622 imeComposition->clear(); |
|
623 } |
|
624 result = qt_sendSpontaneousEvent(fw, &e); |
|
625 update(); |
|
626 releaseContext(fw->effectiveWinId(), imc); |
|
627 } |
|
628 #ifdef Q_IME_DEBUG |
|
629 qDebug("imecomposition: cursor pos at %d, str=%x", imePosition, str[0].unicode()); |
|
630 #endif |
|
631 return result; |
|
632 } |
|
633 |
|
634 static HIMC defaultContext = 0; |
|
635 |
|
636 // checks whether widget is a popup |
|
637 inline bool isPopup(QWidget *w) |
|
638 { |
|
639 if (w && (w->windowFlags() & Qt::Popup) == Qt::Popup) |
|
640 return true; |
|
641 else |
|
642 return false; |
|
643 } |
|
644 // checks whether widget is in a popup |
|
645 inline bool isInPopup(QWidget *w) |
|
646 { |
|
647 if (w && (isPopup(w) || isPopup(w->window()))) |
|
648 return true; |
|
649 else |
|
650 return false; |
|
651 } |
|
652 |
|
653 // find the parent widget, which is a non popup toplevel |
|
654 // this is valid only if the widget is/in a popup |
|
655 inline QWidget *findParentforPopup(QWidget *w) |
|
656 { |
|
657 QWidget *e = QWidget::find(w->effectiveWinId()); |
|
658 // check if this or its parent is a popup |
|
659 while (isInPopup(e)) { |
|
660 e = e->window()->parentWidget(); |
|
661 if (!e) |
|
662 break; |
|
663 e = QWidget::find(e->effectiveWinId()); |
|
664 } |
|
665 if (e) |
|
666 return e->window(); |
|
667 else |
|
668 return 0; |
|
669 } |
|
670 |
|
671 // enables or disables the ime |
|
672 inline void enableIme(QWidget *w, bool value) |
|
673 { |
|
674 if (value) { |
|
675 // enable ime |
|
676 if (defaultContext) |
|
677 ImmAssociateContext(w->effectiveWinId(), defaultContext); |
|
678 #ifdef Q_WS_WINCE |
|
679 if (qApp->autoSipEnabled()) |
|
680 qt_wince_show_SIP(true); |
|
681 #endif |
|
682 } else { |
|
683 // disable ime |
|
684 HIMC oldimc = ImmAssociateContext(w->effectiveWinId(), 0); |
|
685 if (!defaultContext) |
|
686 defaultContext = oldimc; |
|
687 #ifdef Q_WS_WINCE |
|
688 if (qApp->autoSipEnabled()) |
|
689 qt_wince_show_SIP(false); |
|
690 #endif |
|
691 } |
|
692 } |
|
693 |
|
694 |
|
695 void QWinInputContext::updateImeStatus(QWidget *w, bool hasFocus) |
|
696 { |
|
697 if (!w) |
|
698 return; |
|
699 // It's always the proxy that carries the hints. |
|
700 QWidget *focusProxyWidget = w->focusProxy(); |
|
701 if (!focusProxyWidget) |
|
702 focusProxyWidget = w; |
|
703 bool e = w->testAttribute(Qt::WA_InputMethodEnabled) && w->isEnabled() |
|
704 && !(focusProxyWidget->inputMethodHints() & (Qt::ImhExclusiveInputMask | Qt::ImhHiddenText)); |
|
705 bool hasIme = e && hasFocus; |
|
706 #ifdef Q_IME_DEBUG |
|
707 qDebug("%s HasFocus = %d hasIme = %d e = %d ", w->className(), hasFocus, hasIme, e); |
|
708 #endif |
|
709 if (hasFocus || e) { |
|
710 if (isInPopup(w)) |
|
711 QWinInputContext::enablePopupChild(w, hasIme); |
|
712 else |
|
713 QWinInputContext::enable(w, hasIme); |
|
714 } |
|
715 } |
|
716 |
|
717 void QWinInputContext::enablePopupChild(QWidget *w, bool e) |
|
718 { |
|
719 if (aimm) { |
|
720 enable(w, e); |
|
721 return; |
|
722 } |
|
723 |
|
724 if (!w || !isInPopup(w)) |
|
725 return; |
|
726 #ifdef Q_IME_DEBUG |
|
727 qDebug("enablePopupChild: w=%s, enable = %s", w ? w->className() : "(null)" , e ? "true" : "false"); |
|
728 #endif |
|
729 QWidget *parent = findParentforPopup(w); |
|
730 if (parent) { |
|
731 // update ime status of the normal toplevel parent of the popup |
|
732 enableIme(parent, e); |
|
733 } |
|
734 QWidget *toplevel = w->window(); |
|
735 if (toplevel) { |
|
736 // update ime status of the toplevel popup |
|
737 enableIme(toplevel, e); |
|
738 } |
|
739 } |
|
740 |
|
741 void QWinInputContext::enable(QWidget *w, bool e) |
|
742 { |
|
743 if(w) { |
|
744 #ifdef Q_IME_DEBUG |
|
745 qDebug("enable: w=%s, enable = %s", w ? w->className() : "(null)" , e ? "true" : "false"); |
|
746 #endif |
|
747 if (!w->testAttribute(Qt::WA_WState_Created)) |
|
748 return; |
|
749 if(aimm) { |
|
750 HIMC oldimc; |
|
751 if (!e) { |
|
752 aimm->AssociateContext(w->effectiveWinId(), 0, &oldimc); |
|
753 if (!defaultContext) |
|
754 defaultContext = oldimc; |
|
755 } else if (defaultContext) { |
|
756 aimm->AssociateContext(w->effectiveWinId(), defaultContext, &oldimc); |
|
757 } |
|
758 } else { |
|
759 // update ime status on the widget |
|
760 QWidget *p = QWidget::find(w->effectiveWinId()); |
|
761 if (p) |
|
762 enableIme(p, e); |
|
763 } |
|
764 } |
|
765 } |
|
766 |
|
767 void QWinInputContext::setFocusWidget(QWidget *w) |
|
768 { |
|
769 QWidget *oldFocus = focusWidget(); |
|
770 if (oldFocus == w) |
|
771 return; |
|
772 if (w) { |
|
773 QWinInputContext::updateImeStatus(w, true); |
|
774 } else { |
|
775 if (oldFocus) |
|
776 QWinInputContext::updateImeStatus(oldFocus , false); |
|
777 } |
|
778 QInputContext::setFocusWidget(w); |
|
779 update(); |
|
780 } |
|
781 |
|
782 bool QWinInputContext::isComposing() const |
|
783 { |
|
784 return imeComposition && !imeComposition->isEmpty(); |
|
785 } |
|
786 |
|
787 void QWinInputContext::mouseHandler(int pos, QMouseEvent *e) |
|
788 { |
|
789 if(e->type() != QEvent::MouseButtonPress) |
|
790 return; |
|
791 |
|
792 if (pos < 0 || pos > imeComposition->length()) |
|
793 reset(); |
|
794 |
|
795 // Probably should pass the correct button, but it seems to work fine like this. |
|
796 DWORD button = MK_LBUTTON; |
|
797 |
|
798 QWidget *fw = focusWidget(); |
|
799 if (fw) { |
|
800 Q_ASSERT(fw->testAttribute(Qt::WA_WState_Created)); |
|
801 HIMC himc = getContext(fw->effectiveWinId()); |
|
802 HWND ime_wnd = getDefaultIMEWnd(fw->effectiveWinId()); |
|
803 SendMessage(ime_wnd, WM_MSIME_MOUSE, MAKELONG(MAKEWORD(button, pos == 0 ? 2 : 1), pos), (LPARAM)himc); |
|
804 releaseContext(fw->effectiveWinId(), himc); |
|
805 } |
|
806 //qDebug("mouseHandler: got value %d pos=%d", ret,pos); |
|
807 } |
|
808 |
|
809 QString QWinInputContext::language() |
|
810 { |
|
811 return QString(); |
|
812 } |
|
813 |
|
814 int QWinInputContext::reconvertString(RECONVERTSTRING *reconv) |
|
815 { |
|
816 QWidget *w = focusWidget(); |
|
817 if(!w) |
|
818 return -1; |
|
819 |
|
820 Q_ASSERT(w->testAttribute(Qt::WA_WState_Created)); |
|
821 QString surroundingText = qvariant_cast<QString>(w->inputMethodQuery(Qt::ImSurroundingText)); |
|
822 int memSize = sizeof(RECONVERTSTRING)+(surroundingText.length()+1)*sizeof(ushort); |
|
823 // If memory is not allocated, return the required size. |
|
824 if (!reconv) { |
|
825 if (surroundingText.isEmpty()) |
|
826 return -1; |
|
827 else |
|
828 return memSize; |
|
829 } |
|
830 int pos = qvariant_cast<int>(w->inputMethodQuery(Qt::ImCursorPosition)); |
|
831 // find the word in the surrounding text. |
|
832 QTextBoundaryFinder bounds(QTextBoundaryFinder::Word, surroundingText); |
|
833 bounds.setPosition(pos); |
|
834 if (bounds.isAtBoundary()) { |
|
835 if (QTextBoundaryFinder::EndWord == bounds.boundaryReasons()) |
|
836 bounds.toPreviousBoundary(); |
|
837 } else { |
|
838 bounds.toPreviousBoundary(); |
|
839 } |
|
840 int startPos = bounds.position(); |
|
841 bounds.toNextBoundary(); |
|
842 int endPos = bounds.position(); |
|
843 // select the text, this will be overwritten by following ime events. |
|
844 QList<QInputMethodEvent::Attribute> attrs; |
|
845 attrs << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, startPos, endPos-startPos, QVariant()); |
|
846 QInputMethodEvent e(QString(), attrs); |
|
847 qt_sendSpontaneousEvent(w, &e); |
|
848 |
|
849 reconv->dwSize = memSize; |
|
850 reconv->dwVersion = 0; |
|
851 |
|
852 reconv->dwStrLen = surroundingText.length(); |
|
853 reconv->dwStrOffset = sizeof(RECONVERTSTRING); |
|
854 reconv->dwCompStrLen = endPos-startPos; |
|
855 reconv->dwCompStrOffset = startPos*sizeof(ushort); |
|
856 reconv->dwTargetStrLen = reconv->dwCompStrLen; |
|
857 reconv->dwTargetStrOffset = reconv->dwCompStrOffset; |
|
858 memcpy((char*)(reconv+1), surroundingText.utf16(), surroundingText.length()*sizeof(ushort)); |
|
859 return memSize; |
|
860 } |
|
861 |
|
862 QT_END_NAMESPACE |