Blender V5.3
GHOST_ImeWin32.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2010 The Chromium Authors. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#ifdef WITH_INPUT_IME
10
11# include "GHOST_ImeWin32.hh"
12# include "GHOST_WindowWin32.hh"
13# include "utfconv.hh"
14
15/* ISO_639-1 2-Letter Abbreviations. */
16# define IMELANG_ENGLISH "en"
17# define IMELANG_CHINESE "zh"
18# define IMELANG_JAPANESE "ja"
19# define IMELANG_KOREAN "ko"
20
21GHOST_ImeWin32::GHOST_ImeWin32()
22 : is_composing_(false),
23 language_(IMELANG_ENGLISH),
24 conversion_modes_(IME_CMODE_ALPHANUMERIC),
25 sentence_mode_(IME_SMODE_NONE),
26 system_caret_(false),
27 caret_rect_(-1, -1, 0, 0),
28 is_first(true),
29 is_enable(true)
30{
31}
32
33GHOST_ImeWin32::~GHOST_ImeWin32() {}
34
35void GHOST_ImeWin32::UpdateInputLanguage()
36{
37 /* Get the current input locale full name. */
38 WCHAR locale[LOCALE_NAME_MAX_LENGTH];
39 LCIDToLocaleName(
40 MAKELCID(LOWORD(::GetKeyboardLayout(0)), SORT_DEFAULT), locale, LOCALE_NAME_MAX_LENGTH, 0);
41 /* Get the 2-letter ISO-63901 abbreviation of the input locale name. */
42 WCHAR language_u16[W32_ISO639_LEN];
43 GetLocaleInfoEx(locale, LOCALE_SISO639LANGNAME, language_u16, W32_ISO639_LEN);
44 /* Store this as a UTF8 string. */
45 WideCharToMultiByte(
46 CP_UTF8, 0, language_u16, W32_ISO639_LEN, language_, W32_ISO639_LEN, nullptr, nullptr);
47}
48
49BOOL GHOST_ImeWin32::IsLanguage(const char name[W32_ISO639_LEN])
50{
51 return (strcmp(name, language_) == 0);
52}
53
54void GHOST_ImeWin32::UpdateConversionStatus(HWND window_handle)
55{
56 HIMC imm_context = ::ImmGetContext(window_handle);
57 if (imm_context) {
58 if (::ImmGetOpenStatus(imm_context)) {
59 ::ImmGetConversionStatus(imm_context, &conversion_modes_, &sentence_mode_);
60 }
61 else {
62 conversion_modes_ = IME_CMODE_ALPHANUMERIC;
63 sentence_mode_ = IME_SMODE_NONE;
64 }
65 ::ImmReleaseContext(window_handle, imm_context);
66 }
67 else {
68 conversion_modes_ = IME_CMODE_ALPHANUMERIC;
69 sentence_mode_ = IME_SMODE_NONE;
70 }
71}
72
73bool GHOST_ImeWin32::IsEnglishMode()
74{
75 return (conversion_modes_ & IME_CMODE_NOCONVERSION) ||
76 !(conversion_modes_ & (IME_CMODE_NATIVE | IME_CMODE_FULLSHAPE));
77}
78
79bool GHOST_ImeWin32::IsImeKeyEvent(char ascii, GHOST_TKey key)
80{
81 if (!(IsEnglishMode())) {
82 /* In Chinese, Japanese, Korean, all alpha keys are processed by IME. */
83 if ((ascii >= 'A' && ascii <= 'Z') || (ascii >= 'a' && ascii <= 'z')) {
84 return true;
85 }
86 if (IsLanguage(IMELANG_JAPANESE) && (ascii >= ' ' && ascii <= '~')) {
87 return true;
88 }
89 if (IsLanguage(IMELANG_CHINESE)) {
90 if (ascii && strchr("!\"$'(),.:;<>?[\\]^_`/", ascii) && !(key == GHOST_kKeyNumpadPeriod)) {
91 return true;
92 }
93 if (conversion_modes_ & IME_CMODE_FULLSHAPE && (ascii >= '0' && ascii <= '9')) {
94 /* When in Full Width mode the number keys are also converted. */
95 return true;
96 }
97 }
98 }
99 return false;
100}
101
102void GHOST_ImeWin32::CreateImeWindow(HWND window_handle)
103{
116 if (!system_caret_ && (IsLanguage(IMELANG_CHINESE) || IsLanguage(IMELANG_JAPANESE))) {
117 system_caret_ = ::CreateCaret(window_handle, nullptr, 1, 1);
118 }
119 /* Restore the positions of the IME windows. */
120 UpdateImeWindow(window_handle);
121}
122
123void GHOST_ImeWin32::SetImeWindowStyle(
124 HWND window_handle, UINT message, WPARAM wparam, LPARAM lparam, BOOL *handled)
125{
136 *handled = TRUE;
137 lparam &= ~ISC_SHOWUICOMPOSITIONWINDOW;
138 ::DefWindowProc(window_handle, message, wparam, lparam);
139}
140
141void GHOST_ImeWin32::DestroyImeWindow(HWND /*window_handle*/)
142{
143 /* Destroy the system caret if we have created for this IME input context. */
144 if (system_caret_) {
145 ::DestroyCaret();
146 system_caret_ = false;
147 }
148}
149
150void GHOST_ImeWin32::MoveImeWindow(HWND /*window_handle*/, HIMC imm_context)
151{
152 int x = caret_rect_.l_;
153 int y = caret_rect_.t_;
154 const int kCaretMargin = 1;
167 CANDIDATEFORM candidate_position = {0, CFS_CANDIDATEPOS, {x, y}, {0, 0, 0, 0}};
168 ::ImmSetCandidateWindow(imm_context, &candidate_position);
169 if (system_caret_) {
170 ::SetCaretPos(x, y);
171 }
172 if (IsLanguage(IMELANG_KOREAN)) {
179 y += kCaretMargin;
180 }
187 CANDIDATEFORM exclude_rectangle = {
188 0, CFS_EXCLUDE, {x, y}, {x, y, x + caret_rect_.getWidth(), y + caret_rect_.getHeight()}};
189 ::ImmSetCandidateWindow(imm_context, &exclude_rectangle);
190}
191
192void GHOST_ImeWin32::UpdateImeWindow(HWND window_handle)
193{
194 /* Just move the IME window attached to the given window. */
195 if (caret_rect_.l_ >= 0 && caret_rect_.t_ >= 0) {
196 HIMC imm_context = ::ImmGetContext(window_handle);
197 if (imm_context) {
198 MoveImeWindow(window_handle, imm_context);
199 ::ImmReleaseContext(window_handle, imm_context);
200 }
201 }
202}
203
204void GHOST_ImeWin32::CleanupComposition(HWND window_handle)
205{
211 if (is_composing_) {
212 HIMC imm_context = ::ImmGetContext(window_handle);
213 if (imm_context) {
214 ::ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
215 ::ImmReleaseContext(window_handle, imm_context);
216 }
217 ResetComposition(window_handle);
218 }
219}
220
221void GHOST_ImeWin32::CheckFirst(HWND window_handle)
222{
223 if (is_first) {
224 this->EndIME(window_handle);
225 is_first = false;
226 }
227}
228
229void GHOST_ImeWin32::ResetComposition(HWND /*window_handle*/)
230{
231 /* Currently, just reset the composition status. */
232 is_composing_ = false;
233}
234
235void GHOST_ImeWin32::CompleteComposition(HWND window_handle, HIMC imm_context)
236{
242 if (is_composing_) {
243 ::ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
244 ResetComposition(window_handle);
245 }
246}
247
248void GHOST_ImeWin32::GetCaret(HIMC imm_context, LPARAM lparam, ImeComposition *composition)
249{
260 int target_start = -1;
261 int target_end = -1;
262 if (IsLanguage(IMELANG_KOREAN)) {
263 if (lparam & CS_NOMOVECARET) {
264 target_start = 0;
265 target_end = 1;
266 }
267 }
268 else if (IsLanguage(IMELANG_CHINESE)) {
269 int clause_size = ImmGetCompositionStringW(imm_context, GCS_COMPCLAUSE, nullptr, 0);
270 if (clause_size) {
271 static std::vector<ulong> clauses;
272 clause_size = clause_size / sizeof(clauses[0]);
273 clauses.resize(clause_size);
274 ImmGetCompositionStringW(
275 imm_context, GCS_COMPCLAUSE, &clauses[0], sizeof(clauses[0]) * clause_size);
276 if (composition->cursor_position == composition->ime_string.size()) {
277 target_start = clauses[clause_size - 2];
278 target_end = clauses[clause_size - 1];
279 }
280 else {
281 for (int i = 0; i < clause_size - 1; i++) {
282 if (clauses[i] == composition->cursor_position) {
283 target_start = clauses[i];
284 target_end = clauses[i + 1];
285 break;
286 }
287 }
288 }
289 }
290 else {
291 if (composition->cursor_position != -1) {
292 target_start = composition->cursor_position;
293 target_end = composition->ime_string.size();
294 }
295 }
296 }
297 else if (IsLanguage(IMELANG_JAPANESE)) {
304 if (lparam & GCS_COMPATTR) {
305 int attribute_size = ::ImmGetCompositionStringW(imm_context, GCS_COMPATTR, nullptr, 0);
306 if (attribute_size > 0) {
307 char *attribute_data = new char[attribute_size];
308 if (attribute_data) {
309 ::ImmGetCompositionStringW(imm_context, GCS_COMPATTR, attribute_data, attribute_size);
310 for (target_start = 0; target_start < attribute_size; ++target_start) {
311 if (IsTargetAttribute(attribute_data[target_start])) {
312 break;
313 }
314 }
315 for (target_end = target_start; target_end < attribute_size; ++target_end) {
316 if (!IsTargetAttribute(attribute_data[target_end])) {
317 break;
318 }
319 }
320 if (target_start == attribute_size) {
326 target_end = target_start;
327 target_start = 0;
328 }
329 if (target_start != -1 && target_start < attribute_size &&
330 attribute_data[target_start] == ATTR_TARGET_NOTCONVERTED)
331 {
332 composition->cursor_position = target_start;
333 }
334 }
335 delete[] attribute_data;
336 }
337 }
338 }
339 composition->target_start = target_start;
340 composition->target_end = target_end;
341}
342
343bool GHOST_ImeWin32::GetString(HIMC imm_context,
344 WPARAM lparam,
345 int type,
346 ImeComposition *composition)
347{
348 bool result = false;
349 if (lparam & type) {
350 int string_size = ::ImmGetCompositionStringW(imm_context, type, nullptr, 0);
351 if (string_size > 0) {
352 int string_length = string_size / sizeof(wchar_t);
353 wchar_t *string_data = new wchar_t[string_length + 1];
354 string_data[string_length] = '\0';
355 if (string_data) {
356 /* Fill the given ImeComposition object. */
357 ::ImmGetCompositionStringW(imm_context, type, string_data, string_size);
358 composition->string_type = type;
359 composition->ime_string = string_data;
360 result = true;
361 }
362 delete[] string_data;
363 }
364 }
365 return result;
366}
367
368bool GHOST_ImeWin32::GetResult(HWND window_handle, LPARAM lparam, ImeComposition *composition)
369{
370 bool result = false;
371 HIMC imm_context = ::ImmGetContext(window_handle);
372 if (imm_context) {
373 /* Copy the result string to the ImeComposition object. */
374 result = GetString(imm_context, lparam, GCS_RESULTSTR, composition);
379 composition->cursor_position = -1;
380 composition->target_start = -1;
381 composition->target_end = -1;
382 ::ImmReleaseContext(window_handle, imm_context);
383 }
384 return result;
385}
386
387bool GHOST_ImeWin32::GetComposition(HWND window_handle, LPARAM lparam, ImeComposition *composition)
388{
389 bool result = false;
390 HIMC imm_context = ::ImmGetContext(window_handle);
391 if (imm_context) {
392 /* Copy the composition string to the ImeComposition object. */
393 result = GetString(imm_context, lparam, GCS_COMPSTR, composition);
394
395 /* Retrieve the cursor position in the IME composition. */
396 int cursor_position = ::ImmGetCompositionStringW(imm_context, GCS_CURSORPOS, nullptr, 0);
397 composition->cursor_position = cursor_position;
398 composition->target_start = -1;
399 composition->target_end = -1;
400
401 /* Retrieve the target selection and Update the ImeComposition object. */
402 GetCaret(imm_context, lparam, composition);
403
404 /* Mark that there is an ongoing composition. */
405 is_composing_ = true;
406
407 ::ImmReleaseContext(window_handle, imm_context);
408 }
409 return result;
410}
411
412void GHOST_ImeWin32::EndIME(HWND window_handle)
413{
421 if (!is_enable) {
422 return;
423 }
424 is_enable = false;
425 if (is_composing_) {
426 HIMC imm_context = ::ImmGetContext(window_handle);
427 if (imm_context) {
428 /* Cancel composition */
429 ::ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
430 ::ImmNotifyIME(imm_context, NI_CLOSECANDIDATE, 0, 0);
431 ::ImmReleaseContext(window_handle, imm_context);
432 }
433 ResetComposition(window_handle);
434 }
435 ::ImmAssociateContextEx(window_handle, nullptr, 0);
436 eventImeData.composite.clear();
437}
438
439void GHOST_ImeWin32::BeginIME(HWND window_handle, const GHOST_Rect &caret_rect, bool complete)
440{
441 if (is_enable && complete) {
442 return;
443 }
444 is_enable = true;
451 ::ImmAssociateContextEx(window_handle, nullptr, IACE_DEFAULT);
452 /* Complete the ongoing composition and move the IME windows. */
453 HIMC imm_context = ::ImmGetContext(window_handle);
454 if (imm_context) {
455 if (complete) {
464 CompleteComposition(window_handle, imm_context);
465 }
471 if (caret_rect.l_ >= 0 && caret_rect.t_ >= 0) {
472 caret_rect_ = caret_rect;
473 MoveImeWindow(window_handle, imm_context);
474 }
475 ::ImmReleaseContext(window_handle, imm_context);
476 }
477}
478
479static void convert_utf16_to_utf8_len(std::wstring s, int &len)
480{
481 if (len >= 0 && len <= s.size()) {
482 len = count_utf_8_from_16(s.substr(0, len).c_str()) - 1;
483 }
484 else {
485 len = -1;
486 }
487}
488
489static size_t updateUtf8Buf(ImeComposition &info)
490{
491 size_t len = count_utf_8_from_16(info.ime_string.c_str());
492 info.utf8_buf.resize(len);
493 conv_utf_16_to_8(info.ime_string.c_str(), &info.utf8_buf[0], len);
494 convert_utf16_to_utf8_len(info.ime_string, info.cursor_position);
495 convert_utf16_to_utf8_len(info.ime_string, info.target_start);
496 convert_utf16_to_utf8_len(info.ime_string, info.target_end);
497 return len - 1;
498}
499
500void GHOST_ImeWin32::UpdateInfo(HWND window_handle)
501{
502 int res = this->GetResult(window_handle, GCS_RESULTSTR, &resultInfo);
503 int comp = this->GetComposition(window_handle, GCS_COMPSTR | GCS_COMPATTR, &compInfo);
504 /* Convert wchar to UTF8. */
505 if (res) {
506 updateUtf8Buf(resultInfo);
507 eventImeData.result = std::string(&resultInfo.utf8_buf[0]);
508 }
509 else {
510 eventImeData.result = "";
511 }
512 if (comp) {
513 updateUtf8Buf(compInfo);
514 eventImeData.composite = std::string(&compInfo.utf8_buf[0]);
515 eventImeData.cursor_position = compInfo.cursor_position;
516 eventImeData.target_start = compInfo.target_start;
517 eventImeData.target_end = compInfo.target_end;
518 }
519 else {
520 eventImeData.composite = "";
521 eventImeData.cursor_position = -1;
522 eventImeData.target_start = -1;
523 eventImeData.target_end = -1;
524 }
525}
526
527#endif /* WITH_INPUT_IME */
GHOST_TKey
@ GHOST_kKeyNumpadPeriod
int32_t t_
int32_t l_
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
PipelineCompute comp(comp_main)
size_t count_utf_8_from_16(const wchar_t *string16)
Definition utfconv.cc:11
int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)
Definition utfconv.cc:116