16# define IMELANG_ENGLISH "en"
17# define IMELANG_CHINESE "zh"
18# define IMELANG_JAPANESE "ja"
19# define IMELANG_KOREAN "ko"
21GHOST_ImeWin32::GHOST_ImeWin32()
22 : is_composing_(
false),
23 language_(IMELANG_ENGLISH),
24 conversion_modes_(IME_CMODE_ALPHANUMERIC),
25 sentence_mode_(IME_SMODE_NONE),
27 caret_rect_(-1, -1, 0, 0),
33GHOST_ImeWin32::~GHOST_ImeWin32() {}
35void GHOST_ImeWin32::UpdateInputLanguage()
38 WCHAR locale[LOCALE_NAME_MAX_LENGTH];
40 MAKELCID(LOWORD(::GetKeyboardLayout(0)), SORT_DEFAULT), locale, LOCALE_NAME_MAX_LENGTH, 0);
42 WCHAR language_u16[W32_ISO639_LEN];
43 GetLocaleInfoEx(locale, LOCALE_SISO639LANGNAME, language_u16, W32_ISO639_LEN);
46 CP_UTF8, 0, language_u16, W32_ISO639_LEN, language_, W32_ISO639_LEN,
nullptr,
nullptr);
49BOOL GHOST_ImeWin32::IsLanguage(
const char name[W32_ISO639_LEN])
51 return (strcmp(name, language_) == 0);
54void GHOST_ImeWin32::UpdateConversionStatus(HWND window_handle)
56 HIMC imm_context = ::ImmGetContext(window_handle);
58 if (::ImmGetOpenStatus(imm_context)) {
59 ::ImmGetConversionStatus(imm_context, &conversion_modes_, &sentence_mode_);
62 conversion_modes_ = IME_CMODE_ALPHANUMERIC;
63 sentence_mode_ = IME_SMODE_NONE;
65 ::ImmReleaseContext(window_handle, imm_context);
68 conversion_modes_ = IME_CMODE_ALPHANUMERIC;
69 sentence_mode_ = IME_SMODE_NONE;
73bool GHOST_ImeWin32::IsEnglishMode()
75 return (conversion_modes_ & IME_CMODE_NOCONVERSION) ||
76 !(conversion_modes_ & (IME_CMODE_NATIVE | IME_CMODE_FULLSHAPE));
79bool GHOST_ImeWin32::IsImeKeyEvent(
char ascii,
GHOST_TKey key)
81 if (!(IsEnglishMode())) {
83 if ((ascii >=
'A' && ascii <=
'Z') || (ascii >=
'a' && ascii <=
'z')) {
86 if (IsLanguage(IMELANG_JAPANESE) && (ascii >=
' ' && ascii <=
'~')) {
89 if (IsLanguage(IMELANG_CHINESE)) {
93 if (conversion_modes_ & IME_CMODE_FULLSHAPE && (ascii >=
'0' && ascii <=
'9')) {
102void GHOST_ImeWin32::CreateImeWindow(HWND window_handle)
116 if (!system_caret_ && (IsLanguage(IMELANG_CHINESE) || IsLanguage(IMELANG_JAPANESE))) {
117 system_caret_ = ::CreateCaret(window_handle,
nullptr, 1, 1);
120 UpdateImeWindow(window_handle);
123void GHOST_ImeWin32::SetImeWindowStyle(
124 HWND window_handle, UINT message, WPARAM wparam, LPARAM lparam, BOOL *handled)
137 lparam &= ~ISC_SHOWUICOMPOSITIONWINDOW;
138 ::DefWindowProc(window_handle, message, wparam, lparam);
141void GHOST_ImeWin32::DestroyImeWindow(HWND )
146 system_caret_ =
false;
150void GHOST_ImeWin32::MoveImeWindow(HWND , HIMC imm_context)
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);
172 if (IsLanguage(IMELANG_KOREAN)) {
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);
192void GHOST_ImeWin32::UpdateImeWindow(HWND window_handle)
195 if (caret_rect_.l_ >= 0 && caret_rect_.t_ >= 0) {
196 HIMC imm_context = ::ImmGetContext(window_handle);
198 MoveImeWindow(window_handle, imm_context);
199 ::ImmReleaseContext(window_handle, imm_context);
204void GHOST_ImeWin32::CleanupComposition(HWND window_handle)
212 HIMC imm_context = ::ImmGetContext(window_handle);
214 ::ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
215 ::ImmReleaseContext(window_handle, imm_context);
217 ResetComposition(window_handle);
221void GHOST_ImeWin32::CheckFirst(HWND window_handle)
224 this->EndIME(window_handle);
229void GHOST_ImeWin32::ResetComposition(HWND )
232 is_composing_ =
false;
235void GHOST_ImeWin32::CompleteComposition(HWND window_handle, HIMC imm_context)
243 ::ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
244 ResetComposition(window_handle);
248void GHOST_ImeWin32::GetCaret(HIMC imm_context, LPARAM lparam, ImeComposition *composition)
260 int target_start = -1;
262 if (IsLanguage(IMELANG_KOREAN)) {
263 if (lparam & CS_NOMOVECARET) {
268 else if (IsLanguage(IMELANG_CHINESE)) {
269 int clause_size = ImmGetCompositionStringW(imm_context, GCS_COMPCLAUSE,
nullptr, 0);
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];
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];
291 if (composition->cursor_position != -1) {
292 target_start = composition->cursor_position;
293 target_end = composition->ime_string.size();
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])) {
315 for (target_end = target_start; target_end < attribute_size; ++target_end) {
316 if (!IsTargetAttribute(attribute_data[target_end])) {
320 if (target_start == attribute_size) {
326 target_end = target_start;
329 if (target_start != -1 && target_start < attribute_size &&
330 attribute_data[target_start] == ATTR_TARGET_NOTCONVERTED)
332 composition->cursor_position = target_start;
335 delete[] attribute_data;
339 composition->target_start = target_start;
340 composition->target_end = target_end;
343bool GHOST_ImeWin32::GetString(HIMC imm_context,
346 ImeComposition *composition)
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';
357 ::ImmGetCompositionStringW(imm_context, type, string_data, string_size);
358 composition->string_type = type;
359 composition->ime_string = string_data;
362 delete[] string_data;
368bool GHOST_ImeWin32::GetResult(HWND window_handle, LPARAM lparam, ImeComposition *composition)
371 HIMC imm_context = ::ImmGetContext(window_handle);
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);
387bool GHOST_ImeWin32::GetComposition(HWND window_handle, LPARAM lparam, ImeComposition *composition)
390 HIMC imm_context = ::ImmGetContext(window_handle);
393 result = GetString(imm_context, lparam, GCS_COMPSTR, 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;
402 GetCaret(imm_context, lparam, composition);
405 is_composing_ =
true;
407 ::ImmReleaseContext(window_handle, imm_context);
412void GHOST_ImeWin32::EndIME(HWND window_handle)
426 HIMC imm_context = ::ImmGetContext(window_handle);
429 ::ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
430 ::ImmNotifyIME(imm_context, NI_CLOSECANDIDATE, 0, 0);
431 ::ImmReleaseContext(window_handle, imm_context);
433 ResetComposition(window_handle);
435 ::ImmAssociateContextEx(window_handle,
nullptr, 0);
436 eventImeData.composite.clear();
439void GHOST_ImeWin32::BeginIME(HWND window_handle,
const GHOST_Rect &caret_rect,
bool complete)
441 if (is_enable && complete) {
451 ::ImmAssociateContextEx(window_handle,
nullptr, IACE_DEFAULT);
453 HIMC imm_context = ::ImmGetContext(window_handle);
464 CompleteComposition(window_handle, imm_context);
471 if (caret_rect.
l_ >= 0 && caret_rect.
t_ >= 0) {
472 caret_rect_ = caret_rect;
473 MoveImeWindow(window_handle, imm_context);
475 ::ImmReleaseContext(window_handle, imm_context);
479static void convert_utf16_to_utf8_len(std::wstring s,
int &
len)
481 if (
len >= 0 &&
len <= s.size()) {
489static size_t updateUtf8Buf(ImeComposition &info)
492 info.utf8_buf.resize(
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);
500void GHOST_ImeWin32::UpdateInfo(HWND window_handle)
502 int res = this->GetResult(window_handle, GCS_RESULTSTR, &resultInfo);
503 int comp = this->GetComposition(window_handle, GCS_COMPSTR | GCS_COMPATTR, &compInfo);
506 updateUtf8Buf(resultInfo);
507 eventImeData.result = std::string(&resultInfo.utf8_buf[0]);
510 eventImeData.result =
"";
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;
520 eventImeData.composite =
"";
521 eventImeData.cursor_position = -1;
522 eventImeData.target_start = -1;
523 eventImeData.target_end = -1;
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
PipelineCompute comp(comp_main)
size_t count_utf_8_from_16(const wchar_t *string16)
int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)