#include <msctf.h>
#include <windowsx.h>
#include "include/base/cef_build.h"
#include "tests/cefclient/browser/osr_ime_handler_win.h"
#include "tests/cefclient/browser/resource.h"
#include "tests/shared/browser/geometry_util.h"
#include "tests/shared/browser/main_message_loop.h"
#include "tests/shared/browser/util_win.h"
#define ColorUNDERLINE \
0xFF000000
#define ColorBKCOLOR \
0x00000000
namespace client {
namespace {
bool IsSelectionAttribute(char attribute) {
return (attribute == ATTR_TARGET_CONVERTED ||
attribute == ATTR_TARGET_NOTCONVERTED);
}
void GetCompositionSelectionRange(HIMC imc,
int* target_start,
int* target_end) {
int attribute_size = ::ImmGetCompositionString(imc, GCS_COMPATTR, nullptr, 0);
if (attribute_size > 0) {
int start = 0;
int end = 0;
std::vector<char> attribute_data(attribute_size);
::ImmGetCompositionString(imc, GCS_COMPATTR, &attribute_data[0],
attribute_size);
for (start = 0; start < attribute_size; ++start) {
if (IsSelectionAttribute(attribute_data[start]))
break;
}
for (end = start; end < attribute_size; ++end) {
if (!IsSelectionAttribute(attribute_data[end]))
break;
}
*target_start = start;
*target_end = end;
}
}
void GetCompositionUnderlines(
HIMC imc,
int target_start,
int target_end,
std::vector<CefCompositionUnderline>& underlines) {
int clause_size = ::ImmGetCompositionString(imc, GCS_COMPCLAUSE, nullptr, 0);
int clause_length = clause_size / sizeof(uint32);
if (clause_length) {
std::vector<uint32> clause_data(clause_length);
::ImmGetCompositionString(imc, GCS_COMPCLAUSE, &clause_data[0],
clause_size);
for (int i = 0; i < clause_length - 1; ++i) {
cef_composition_underline_t underline;
underline.range.from = clause_data[i];
underline.range.to = clause_data[i + 1];
underline.color = ColorUNDERLINE;
underline.background_color = ColorBKCOLOR;
underline.thick = 0;
if (underline.range.from >= target_start &&
underline.range.to <= target_end) {
underline.thick = 1;
}
underlines.push_back(underline);
}
}
}
}
OsrImeHandlerWin::OsrImeHandlerWin(HWND hwnd)
: is_composing_(false),
input_language_id_(LANG_USER_DEFAULT),
system_caret_(false),
cursor_index_(-1),
hwnd_(hwnd) {
ime_rect_ = {-1, -1, 0, 0};
}
OsrImeHandlerWin::~OsrImeHandlerWin() {
DestroyImeWindow();
}
void OsrImeHandlerWin::SetInputLanguage() {
WCHAR keyboard_layout[KL_NAMELENGTH];
if (::GetKeyboardLayoutNameW(keyboard_layout)) {
input_language_id_ =
static_cast<LANGID>(_wtoi(&keyboard_layout[KL_NAMELENGTH >> 1]));
} else {
input_language_id_ = 0x0409;
}
}
void OsrImeHandlerWin::CreateImeWindow() {
if (PRIMARYLANGID(input_language_id_) == LANG_CHINESE ||
PRIMARYLANGID(input_language_id_) == LANG_JAPANESE) {
if (!system_caret_) {
if (::CreateCaret(hwnd_, nullptr, 1, 1))
system_caret_ = true;
}
}
}
void OsrImeHandlerWin::DestroyImeWindow() {
if (system_caret_) {
::DestroyCaret();
system_caret_ = false;
}
}
void OsrImeHandlerWin::MoveImeWindow() {
if (GetFocus() != hwnd_)
return;
CefRect rc = ime_rect_;
int location = cursor_index_;
if (location == -1)
location = composition_range_.from;
if (location >= composition_range_.from)
location -= composition_range_.from;
if (location < static_cast<int>(composition_bounds_.size()))
rc = composition_bounds_[location];
else
return;
HIMC imc = ::ImmGetContext(hwnd_);
if (imc) {
const int kCaretMargin = 1;
if (PRIMARYLANGID(input_language_id_) == LANG_CHINESE) {
CANDIDATEFORM candidate_position = {
0, CFS_CANDIDATEPOS, {rc.x, rc.y}, {0, 0, 0, 0}};
::ImmSetCandidateWindow(imc, &candidate_position);
}
if (system_caret_) {
switch (PRIMARYLANGID(input_language_id_)) {
case LANG_JAPANESE:
::SetCaretPos(rc.x, rc.y + rc.height);
break;
default:
::SetCaretPos(rc.x, rc.y);
break;
}
}
if (PRIMARYLANGID(input_language_id_) == LANG_KOREAN) {
rc.y += kCaretMargin;
}
CANDIDATEFORM exclude_rectangle = {
0,
CFS_EXCLUDE,
{rc.x, rc.y},
{rc.x, rc.y, rc.x + rc.width, rc.y + rc.height}};
::ImmSetCandidateWindow(imc, &exclude_rectangle);
::ImmReleaseContext(hwnd_, imc);
}
}
void OsrImeHandlerWin::CleanupComposition() {
if (is_composing_) {
HIMC imc = ::ImmGetContext(hwnd_);
if (imc) {
::ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
::ImmReleaseContext(hwnd_, imc);
}
ResetComposition();
}
}
void OsrImeHandlerWin::ResetComposition() {
is_composing_ = false;
cursor_index_ = -1;
}
void OsrImeHandlerWin::GetCompositionInfo(
HIMC imc,
LPARAM lparam,
CefString& composition_text,
std::vector<CefCompositionUnderline>& underlines,
int& composition_start) {
underlines.clear();
int length = static_cast<int>(composition_text.length());
int target_start = length;
int target_end = length;
if (lparam & GCS_COMPATTR)
GetCompositionSelectionRange(imc, &target_start, &target_end);
if (!(lparam & CS_NOMOVECARET) && (lparam & GCS_CURSORPOS)) {
int cursor = ::ImmGetCompositionString(imc, GCS_CURSORPOS, nullptr, 0);
composition_start = cursor;
} else {
composition_start = 0;
}
if (lparam & GCS_COMPCLAUSE)
GetCompositionUnderlines(imc, target_start, target_end, underlines);
if (!underlines.size()) {
CefCompositionUnderline underline;
underline.color = ColorUNDERLINE;
underline.background_color = ColorBKCOLOR;
if (target_start > 0) {
underline.range.from = 0;
underline.range.to = target_start;
underline.thick = 0;
underlines.push_back(underline);
}
if (target_end > target_start) {
underline.range.from = target_start;
underline.range.to = target_end;
underline.thick = 1;
underlines.push_back(underline);
}
if (target_end < length) {
underline.range.from = target_end;
underline.range.to = length;
underline.thick = 0;
underlines.push_back(underline);
}
}
}
bool OsrImeHandlerWin::GetString(HIMC imc,
WPARAM lparam,
int type,
CefString& result) {
if (!(lparam & type))
return false;
LONG string_size = ::ImmGetCompositionString(imc, type, nullptr, 0);
if (string_size <= 0)
return false;
string_size += sizeof(WCHAR);
std::vector<wchar_t> buffer(string_size);
::ImmGetCompositionString(imc, type, &buffer[0], string_size);
result.FromWString(&buffer[0]);
return true;
}
bool OsrImeHandlerWin::GetResult(LPARAM lparam, CefString& result) {
bool ret = false;
HIMC imc = ::ImmGetContext(hwnd_);
if (imc) {
ret = GetString(imc, lparam, GCS_RESULTSTR, result);
::ImmReleaseContext(hwnd_, imc);
}
return ret;
}
bool OsrImeHandlerWin::GetComposition(
LPARAM lparam,
CefString& composition_text,
std::vector<CefCompositionUnderline>& underlines,
int& composition_start) {
bool ret = false;
HIMC imc = ::ImmGetContext(hwnd_);
if (imc) {
ret = GetString(imc, lparam, GCS_COMPSTR, composition_text);
if (ret) {
GetCompositionInfo(imc, lparam, composition_text, underlines,
composition_start);
is_composing_ = true;
}
::ImmReleaseContext(hwnd_, imc);
}
return ret;
}
void OsrImeHandlerWin::DisableIME() {
CleanupComposition();
::ImmAssociateContextEx(hwnd_, nullptr, 0);
}
void OsrImeHandlerWin::CancelIME() {
if (is_composing_) {
HIMC imc = ::ImmGetContext(hwnd_);
if (imc) {
::ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
::ImmReleaseContext(hwnd_, imc);
}
ResetComposition();
}
}
void OsrImeHandlerWin::EnableIME() {
::ImmAssociateContextEx(hwnd_, nullptr, IACE_DEFAULT);
}
void OsrImeHandlerWin::UpdateCaretPosition(int index) {
cursor_index_ = index;
MoveImeWindow();
}
void OsrImeHandlerWin::ChangeCompositionRange(
const CefRange& selection_range,
const std::vector<CefRect>& bounds) {
composition_range_ = selection_range;
composition_bounds_ = bounds;
MoveImeWindow();
}
}