This source file includes following definitions.
- IsTargetAttribute
- GetCompositionTargetRange
- GetCompositionUnderlines
- IsRTLPrimaryLangID
- use_composition_window_
- SetInputLanguage
- CreateImeWindow
- SetImeWindowStyle
- DestroyImeWindow
- MoveImeWindow
- UpdateImeWindow
- CleanupComposition
- ResetComposition
- CompleteComposition
- GetCompositionInfo
- GetString
- GetResult
- GetComposition
- DisableIME
- CancelIME
- EnableIME
- UpdateCaretRect
- SetUseCompositionWindow
- GetInputLanguageName
- SetTextInputMode
- IsRTLKeyboardLayoutInstalled
- IsCtrlShiftPressed
- ConvertInputModeToImmFlags
#include "ui/base/ime/win/imm32_manager.h"
#include <msctf.h>
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/scoped_comptr.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/ime/composition_text.h"
#pragma comment(lib, "imm32.lib")
COMPILE_ASSERT(sizeof(wchar_t) == sizeof(base::char16), wchar_t__char16_diff);
namespace {
bool IsTargetAttribute(char attribute) {
return (attribute == ATTR_TARGET_CONVERTED ||
attribute == ATTR_TARGET_NOTCONVERTED);
}
void GetCompositionTargetRange(HIMC imm_context, int* target_start,
int* target_end) {
int attribute_size = ::ImmGetCompositionString(imm_context, GCS_COMPATTR,
NULL, 0);
if (attribute_size > 0) {
int start = 0;
int end = 0;
scoped_ptr<char[]> attribute_data(new char[attribute_size]);
if (attribute_data.get()) {
::ImmGetCompositionString(imm_context, GCS_COMPATTR,
attribute_data.get(), attribute_size);
for (start = 0; start < attribute_size; ++start) {
if (IsTargetAttribute(attribute_data[start]))
break;
}
for (end = start; end < attribute_size; ++end) {
if (!IsTargetAttribute(attribute_data[end]))
break;
}
}
*target_start = start;
*target_end = end;
}
}
void GetCompositionUnderlines(HIMC imm_context,
int target_start,
int target_end,
ui::CompositionUnderlines* underlines) {
int clause_size = ::ImmGetCompositionString(imm_context, GCS_COMPCLAUSE,
NULL, 0);
int clause_length = clause_size / sizeof(uint32);
if (clause_length) {
scoped_ptr<uint32[]> clause_data(new uint32[clause_length]);
if (clause_data.get()) {
::ImmGetCompositionString(imm_context, GCS_COMPCLAUSE,
clause_data.get(), clause_size);
for (int i = 0; i < clause_length - 1; ++i) {
ui::CompositionUnderline underline;
underline.start_offset = clause_data[i];
underline.end_offset = clause_data[i+1];
underline.color = SK_ColorBLACK;
underline.thick = false;
if (underline.start_offset >= static_cast<unsigned>(target_start) &&
underline.end_offset <= static_cast<unsigned>(target_end)) {
underline.thick = true;
}
underlines->push_back(underline);
}
}
}
}
bool IsRTLPrimaryLangID(LANGID lang) {
switch (lang) {
case LANG_ARABIC:
case LANG_HEBREW:
case LANG_PERSIAN:
case LANG_SYRIAC:
case LANG_UIGHUR:
case LANG_URDU:
return true;
default:
return false;
}
}
}
namespace ui {
IMM32Manager::IMM32Manager()
: ime_status_(false),
input_language_id_(LANG_USER_DEFAULT),
is_composing_(false),
system_caret_(false),
caret_rect_(-1, -1, 0, 0),
use_composition_window_(false) {
}
IMM32Manager::~IMM32Manager() {
}
bool IMM32Manager::SetInputLanguage() {
HKL keyboard_layout = ::GetKeyboardLayout(0);
input_language_id_ = reinterpret_cast<LANGID>(keyboard_layout);
base::win::ScopedComPtr<ITfInputProcessorProfileMgr> prof_mgr;
TF_INPUTPROCESSORPROFILE prof;
if (SUCCEEDED(prof_mgr.CreateInstance(CLSID_TF_InputProcessorProfiles)) &&
SUCCEEDED(prof_mgr->GetActiveProfile(GUID_TFCAT_TIP_KEYBOARD, &prof)) &&
prof.hkl == NULL &&
prof.dwProfileType == TF_PROFILETYPE_INPUTPROCESSOR) {
ime_status_ = true;
} else {
ime_status_ = (ImmGetIMEFileName(keyboard_layout, NULL, 0) != 0);
}
return ime_status_;
}
void IMM32Manager::CreateImeWindow(HWND window_handle) {
if (PRIMARYLANGID(input_language_id_) == LANG_CHINESE ||
PRIMARYLANGID(input_language_id_) == LANG_JAPANESE) {
if (!system_caret_) {
if (::CreateCaret(window_handle, NULL, 1, 1)) {
system_caret_ = true;
}
}
}
UpdateImeWindow(window_handle);
}
LRESULT IMM32Manager::SetImeWindowStyle(HWND window_handle, UINT message,
WPARAM wparam, LPARAM lparam,
BOOL* handled) {
*handled = TRUE;
lparam &= ~ISC_SHOWUICOMPOSITIONWINDOW;
return ::DefWindowProc(window_handle, message, wparam, lparam);
}
void IMM32Manager::DestroyImeWindow(HWND window_handle) {
if (system_caret_) {
::DestroyCaret();
system_caret_ = false;
}
}
void IMM32Manager::MoveImeWindow(HWND window_handle, HIMC imm_context) {
if (GetFocus() != window_handle)
return;
int x = caret_rect_.x();
int y = caret_rect_.y();
const int kCaretMargin = 1;
if (!use_composition_window_ &&
PRIMARYLANGID(input_language_id_) == LANG_CHINESE) {
CANDIDATEFORM candidate_position = {0, CFS_CANDIDATEPOS, {x, y},
{0, 0, 0, 0}};
::ImmSetCandidateWindow(imm_context, &candidate_position);
}
if (system_caret_) {
switch (PRIMARYLANGID(input_language_id_)) {
case LANG_JAPANESE:
::SetCaretPos(x, y + caret_rect_.height());
break;
default:
::SetCaretPos(x, y);
break;
}
}
if (use_composition_window_) {
COMPOSITIONFORM cf = {CFS_POINT, {x, y}};
::ImmSetCompositionWindow(imm_context, &cf);
return;
}
if (PRIMARYLANGID(input_language_id_) == LANG_KOREAN) {
y += kCaretMargin;
}
CANDIDATEFORM exclude_rectangle = {0, CFS_EXCLUDE, {x, y},
{x, y, x + caret_rect_.width(), y + caret_rect_.height()}};
::ImmSetCandidateWindow(imm_context, &exclude_rectangle);
}
void IMM32Manager::UpdateImeWindow(HWND window_handle) {
if (caret_rect_.x() >= 0 && caret_rect_.y() >= 0) {
HIMC imm_context = ::ImmGetContext(window_handle);
if (imm_context) {
MoveImeWindow(window_handle, imm_context);
::ImmReleaseContext(window_handle, imm_context);
}
}
}
void IMM32Manager::CleanupComposition(HWND window_handle) {
if (is_composing_) {
HIMC imm_context = ::ImmGetContext(window_handle);
if (imm_context) {
::ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
::ImmReleaseContext(window_handle, imm_context);
}
ResetComposition(window_handle);
}
}
void IMM32Manager::ResetComposition(HWND window_handle) {
is_composing_ = false;
}
void IMM32Manager::CompleteComposition(HWND window_handle, HIMC imm_context) {
if (is_composing_) {
::ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
ResetComposition(window_handle);
}
}
void IMM32Manager::GetCompositionInfo(HIMC imm_context, LPARAM lparam,
CompositionText* composition) {
composition->underlines.clear();
int length = static_cast<int>(composition->text.length());
int target_start = length;
int target_end = length;
if (lparam & GCS_COMPATTR)
GetCompositionTargetRange(imm_context, &target_start, &target_end);
if (!(lparam & CS_NOMOVECARET) && (lparam & GCS_CURSORPOS)) {
int cursor = ::ImmGetCompositionString(imm_context, GCS_CURSORPOS, NULL, 0);
composition->selection = gfx::Range(cursor);
} else {
composition->selection = gfx::Range(0);
}
if (lparam & GCS_COMPCLAUSE) {
GetCompositionUnderlines(imm_context, target_start, target_end,
&composition->underlines);
}
if (!composition->underlines.size()) {
CompositionUnderline underline;
underline.color = SK_ColorBLACK;
if (target_start > 0) {
underline.start_offset = 0;
underline.end_offset = target_start;
underline.thick = false;
composition->underlines.push_back(underline);
}
if (target_end > target_start) {
underline.start_offset = target_start;
underline.end_offset = target_end;
underline.thick = true;
composition->underlines.push_back(underline);
}
if (target_end < length) {
underline.start_offset = target_end;
underline.end_offset = length;
underline.thick = false;
composition->underlines.push_back(underline);
}
}
}
bool IMM32Manager::GetString(HIMC imm_context,
WPARAM lparam,
int type,
base::string16* result) {
if (!(lparam & type))
return false;
LONG string_size = ::ImmGetCompositionString(imm_context, type, NULL, 0);
if (string_size <= 0)
return false;
DCHECK_EQ(0u, string_size % sizeof(wchar_t));
::ImmGetCompositionString(imm_context, type,
WriteInto(result, (string_size / sizeof(wchar_t)) + 1), string_size);
return true;
}
bool IMM32Manager::GetResult(
HWND window_handle, LPARAM lparam, base::string16* result) {
bool ret = false;
HIMC imm_context = ::ImmGetContext(window_handle);
if (imm_context) {
ret = GetString(imm_context, lparam, GCS_RESULTSTR, result);
::ImmReleaseContext(window_handle, imm_context);
}
return ret;
}
bool IMM32Manager::GetComposition(HWND window_handle, LPARAM lparam,
CompositionText* composition) {
bool ret = false;
HIMC imm_context = ::ImmGetContext(window_handle);
if (imm_context) {
ret = GetString(imm_context, lparam, GCS_COMPSTR, &composition->text);
if (ret) {
if (input_language_id_ == MAKELANGID(LANG_CHINESE,
SUBLANG_CHINESE_TRADITIONAL) &&
composition->text[0] == 0x3000) {
composition->text[0] = 0xFF3F;
}
GetCompositionInfo(imm_context, lparam, composition);
is_composing_ = true;
}
::ImmReleaseContext(window_handle, imm_context);
}
return ret;
}
void IMM32Manager::DisableIME(HWND window_handle) {
CleanupComposition(window_handle);
::ImmAssociateContextEx(window_handle, NULL, 0);
}
void IMM32Manager::CancelIME(HWND window_handle) {
if (is_composing_) {
HIMC imm_context = ::ImmGetContext(window_handle);
if (imm_context) {
::ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
::ImmReleaseContext(window_handle, imm_context);
}
ResetComposition(window_handle);
}
}
void IMM32Manager::EnableIME(HWND window_handle) {
::ImmAssociateContextEx(window_handle, NULL, IACE_DEFAULT);
}
void IMM32Manager::UpdateCaretRect(HWND window_handle,
const gfx::Rect& caret_rect) {
if (caret_rect_ != caret_rect) {
caret_rect_ = caret_rect;
HIMC imm_context = ::ImmGetContext(window_handle);
if (imm_context) {
MoveImeWindow(window_handle, imm_context);
::ImmReleaseContext(window_handle, imm_context);
}
}
}
void IMM32Manager::SetUseCompositionWindow(bool use_composition_window) {
use_composition_window_ = use_composition_window;
}
std::string IMM32Manager::GetInputLanguageName() const {
const LCID locale_id = MAKELCID(input_language_id_, SORT_DEFAULT);
wchar_t buffer[9];
int length = ::GetLocaleInfo(locale_id, LOCALE_SISO639LANGNAME, &buffer[0],
arraysize(buffer));
if (length <= 1)
return std::string();
std::string language;
base::WideToUTF8(buffer, length - 1, &language);
if (SUBLANGID(input_language_id_) == SUBLANG_NEUTRAL)
return language;
length = ::GetLocaleInfo(locale_id, LOCALE_SISO3166CTRYNAME, &buffer[0],
arraysize(buffer));
if (length <= 1)
return language;
std::string region;
base::WideToUTF8(buffer, length - 1, ®ion);
return language.append(1, '-').append(region);
}
void IMM32Manager::SetTextInputMode(HWND window_handle,
TextInputMode input_mode) {
if (input_mode == ui::TEXT_INPUT_MODE_DEFAULT)
return;
const HIMC imm_context = ::ImmGetContext(window_handle);
if (!imm_context)
return;
DWORD conversion_mode = 0;
DWORD sentence_mode = 0;
if (::ImmGetConversionStatus(imm_context, &conversion_mode, &sentence_mode)
== FALSE) {
return;
}
BOOL open = FALSE;
ConvertInputModeToImmFlags(input_mode, conversion_mode, &open,
&conversion_mode),
::ImmSetOpenStatus(imm_context, open);
if (open)
::ImmSetConversionStatus(imm_context, conversion_mode, sentence_mode);
::ImmReleaseContext(window_handle, imm_context);
}
bool IMM32Manager::IsRTLKeyboardLayoutInstalled() {
static enum {
RTL_KEYBOARD_LAYOUT_NOT_INITIALIZED,
RTL_KEYBOARD_LAYOUT_INSTALLED,
RTL_KEYBOARD_LAYOUT_NOT_INSTALLED,
RTL_KEYBOARD_LAYOUT_ERROR,
} layout = RTL_KEYBOARD_LAYOUT_NOT_INITIALIZED;
if (layout != RTL_KEYBOARD_LAYOUT_NOT_INITIALIZED)
return layout == RTL_KEYBOARD_LAYOUT_INSTALLED;
int size = GetKeyboardLayoutList(0, NULL);
if (size <= 0) {
layout = RTL_KEYBOARD_LAYOUT_ERROR;
return false;
}
scoped_ptr<HKL[]> layouts(new HKL[size]);
::GetKeyboardLayoutList(size, layouts.get());
for (int i = 0; i < size; ++i) {
if (IsRTLPrimaryLangID(PRIMARYLANGID(layouts[i]))) {
layout = RTL_KEYBOARD_LAYOUT_INSTALLED;
return true;
}
}
layout = RTL_KEYBOARD_LAYOUT_NOT_INSTALLED;
return false;
}
bool IMM32Manager::IsCtrlShiftPressed(base::i18n::TextDirection* direction) {
uint8_t keystate[256];
if (!::GetKeyboardState(&keystate[0]))
return false;
const int kKeyDownMask = 0x80;
if ((keystate[VK_CONTROL] & kKeyDownMask) == 0)
return false;
if (keystate[VK_RSHIFT] & kKeyDownMask) {
keystate[VK_RSHIFT] = 0;
*direction = base::i18n::RIGHT_TO_LEFT;
} else if (keystate[VK_LSHIFT] & kKeyDownMask) {
keystate[VK_LSHIFT] = 0;
*direction = base::i18n::LEFT_TO_RIGHT;
} else {
return false;
}
keystate[VK_SHIFT] = 0;
keystate[VK_CONTROL] = 0;
keystate[VK_RCONTROL] = 0;
keystate[VK_LCONTROL] = 0;
keystate[VK_F22] = 0;
for (int i = 0; i <= VK_PACKET; ++i) {
if (keystate[i] & kKeyDownMask)
return false;
}
return true;
}
void IMM32Manager::ConvertInputModeToImmFlags(TextInputMode input_mode,
DWORD initial_conversion_mode,
BOOL* open,
DWORD* new_conversion_mode) {
*open = TRUE;
*new_conversion_mode = initial_conversion_mode;
switch (input_mode) {
case ui::TEXT_INPUT_MODE_FULL_WIDTH_LATIN:
*new_conversion_mode |= IME_CMODE_FULLSHAPE;
*new_conversion_mode &= ~(IME_CMODE_NATIVE
| IME_CMODE_KATAKANA);
break;
case ui::TEXT_INPUT_MODE_KANA:
*new_conversion_mode |= (IME_CMODE_NATIVE
| IME_CMODE_FULLSHAPE);
*new_conversion_mode &= ~IME_CMODE_KATAKANA;
break;
case ui::TEXT_INPUT_MODE_KATAKANA:
*new_conversion_mode |= (IME_CMODE_NATIVE
| IME_CMODE_KATAKANA
| IME_CMODE_FULLSHAPE);
break;
default:
*open = FALSE;
break;
}
}
}