diff --git a/CefSharp.Core/CefSharp.Core.vcxproj b/CefSharp.Core/CefSharp.Core.vcxproj
index 351a07f962..45a4088eca 100644
--- a/CefSharp.Core/CefSharp.Core.vcxproj
+++ b/CefSharp.Core/CefSharp.Core.vcxproj
@@ -238,6 +238,8 @@
+
+
Create
@@ -279,6 +281,8 @@
+
+
diff --git a/CefSharp.Core/CefSharp.Core.vcxproj.filters b/CefSharp.Core/CefSharp.Core.vcxproj.filters
index 5fe799855f..b5f6361b2b 100644
--- a/CefSharp.Core/CefSharp.Core.vcxproj.filters
+++ b/CefSharp.Core/CefSharp.Core.vcxproj.filters
@@ -71,6 +71,12 @@
Source Files
+
+ Source Files
+
+
+ Source Files
+
@@ -271,6 +277,12 @@
Header Files
+
+ Header Files
+
+
+ Header Files
+
diff --git a/CefSharp.Core/Internals/CefBrowserHostWrapper.cpp b/CefSharp.Core/Internals/CefBrowserHostWrapper.cpp
index 089d35e87c..1280c95c14 100644
--- a/CefSharp.Core/Internals/CefBrowserHostWrapper.cpp
+++ b/CefSharp.Core/Internals/CefBrowserHostWrapper.cpp
@@ -336,12 +336,30 @@ void CefBrowserHostWrapper::ImeSetComposition(String^ text, cli::arrayImeSetComposition(StringUtils::ToNative(text), underlinesVector, CefRange(), range);
}
+void CefBrowserHostWrapper::ImeSetComposition(
+ const CefString& text,
+ const std::vector& underlines,
+ const CefRange& replacement_range,
+ const CefRange& selection_range) {
+
+ ThrowIfDisposed();
+ _browserHost->ImeSetComposition(text, underlines, replacement_range, selection_range);
+}
+
void CefBrowserHostWrapper::ImeCommitText(String^ text)
{
- ThrowIfDisposed();
+ ThrowIfDisposed();
+
+ //Range and cursor position are Mac OSX only
+ _browserHost->ImeCommitText(StringUtils::ToNative(text), CefRange(), NULL);
+}
+
+void CefBrowserHostWrapper::ImeCommitText(const CefString& text,
+ const CefRange& replacement_range,
+ int relative_cursor_pos) {
- //Range and cursor position are Mac OSX only
- _browserHost->ImeCommitText(StringUtils::ToNative(text), CefRange(), NULL);
+ ThrowIfDisposed();
+ _browserHost->ImeCommitText(text, replacement_range, relative_cursor_pos);
}
void CefBrowserHostWrapper::ImeFinishComposingText(bool keepSelection)
diff --git a/CefSharp.Core/Internals/CefBrowserHostWrapper.h b/CefSharp.Core/Internals/CefBrowserHostWrapper.h
index 7c46b9cd2b..42a2a2d55f 100644
--- a/CefSharp.Core/Internals/CefBrowserHostWrapper.h
+++ b/CefSharp.Core/Internals/CefBrowserHostWrapper.h
@@ -1,4 +1,4 @@
-// Copyright � 2010-2017 The CefSharp Authors. All rights reserved.
+// Copyright ?2010-2017 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
@@ -83,7 +83,9 @@ namespace CefSharp
virtual void Invalidate(PaintElementType type);
virtual void ImeSetComposition(String^ text, cli::array^ underlines, Nullable selectionRange);
- virtual void ImeCommitText(String^ text);
+ virtual void ImeSetComposition(const CefString& text, const std::vector& underlines, const CefRange& replacement_range, const CefRange& selection_range);
+ virtual void ImeCommitText(String^ text);
+ virtual void ImeCommitText(const CefString& text, const CefRange& replacement_range, int relative_cursor_pos);
virtual void ImeFinishComposingText(bool keepSelection);
virtual void ImeCancelComposition();
diff --git a/CefSharp.Core/OsrImeHandler.cpp b/CefSharp.Core/OsrImeHandler.cpp
new file mode 100644
index 0000000000..a188a7d241
--- /dev/null
+++ b/CefSharp.Core/OsrImeHandler.cpp
@@ -0,0 +1,406 @@
+// Copyright 2016 The Chromium Embedded Framework Authors. Portions copyright
+// 2013 The Chromium Authors. All rights reserved. Use of this source code is
+// governed by a BSD-style license that can be found in the LICENSE file.
+
+// Implementation based on ui/base/ime/win/imm32_manager.cc from Chromium.
+
+#include "Stdafx.h"
+//#include
+//#include
+
+#include "include/base/cef_build.h"
+#include "OsrImeHandler.h"
+
+#pragma comment(lib, "imm32.lib")
+
+#define ColorUNDERLINE 0xFF000000 // Black SkColor value for underline,
+// same as Blink.
+#define ColorBKCOLOR 0x00000000 // White SkColor value for background,
+// same as Blink.
+
+namespace CefSharp {
+
+ namespace {
+
+ // Determines whether or not the given attribute represents a selection
+ bool IsSelectionAttribute(char attribute) {
+ return (attribute == ATTR_TARGET_CONVERTED ||
+ attribute == ATTR_TARGET_NOTCONVERTED);
+ }
+
+ // Helper function for OsrImeHandler::GetCompositionInfo() method,
+ // to get the target range that's selected by the user in the current
+ // composition string.
+ void GetCompositionSelectionRange(HIMC imc, int* target_start,
+ int* target_end) {
+ int attribute_size = ::ImmGetCompositionString(imc, GCS_COMPATTR, NULL, 0);
+ if (attribute_size > 0) {
+ int start = 0;
+ int end = 0;
+ std::vector 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;
+ }
+ }
+
+ // Helper function for OsrImeHandler::GetCompositionInfo() method, to get
+ // underlines information of the current composition string.
+ void GetCompositionUnderlines(
+ HIMC imc,
+ int target_start,
+ int target_end,
+ std::vector &underlines) {
+ int clause_size = ::ImmGetCompositionString(imc, GCS_COMPCLAUSE, NULL, 0);
+ int clause_length = clause_size / sizeof(uint32);
+ if (clause_length) {
+ std::vector 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;
+
+ // Use thick underline for the target clause.
+ if (underline.range.from >= target_start &&
+ underline.range.to <= target_end) {
+ underline.thick = 1;
+ }
+ underlines.push_back(underline);
+ }
+ }
+ }
+
+ } // namespace
+
+ OsrImeHandler::OsrImeHandler(HWND hwnd)
+ : ime_status_(false),
+ hwnd_(hwnd),
+ input_language_id_(LANG_USER_DEFAULT),
+ is_composing_(false),
+ cursor_index_(-1),
+ system_caret_(false) {
+ ime_rect_ = { -1, -1, 0, 0 };
+ }
+
+ OsrImeHandler::~OsrImeHandler() {
+ DestroyImeWindow();
+ }
+
+ void OsrImeHandler::SetInputLanguage() {
+ // Retrieve the current input language from the system's keyboard layout.
+ // Using GetKeyboardLayoutName instead of GetKeyboardLayout, because
+ // the language from GetKeyboardLayout is the language under where the
+ // keyboard layout is installed. And the language from GetKeyboardLayoutName
+ // indicates the language of the keyboard layout itself.
+ // See crbug.com/344834.
+ WCHAR keyboard_layout[KL_NAMELENGTH];
+ if (::GetKeyboardLayoutNameW(keyboard_layout)) {
+ input_language_id_ =
+ static_cast(_wtoi(&keyboard_layout[KL_NAMELENGTH >> 1]));
+ } else {
+ input_language_id_ = 0x0409; // Fallback to en-US.
+ }
+ }
+
+ void OsrImeHandler::CreateImeWindow() {
+ // Chinese/Japanese IMEs somehow ignore function calls to
+ // ::ImmSetCandidateWindow(), and use the position of the current system
+ // caret instead -::GetCaretPos().
+ // Therefore, we create a temporary system caret for Chinese IMEs and use
+ // it during this input context.
+ // Since some third-party Japanese IME also uses ::GetCaretPos() to determine
+ // their window position, we also create a caret for Japanese IMEs.
+ if (PRIMARYLANGID(input_language_id_) == LANG_CHINESE ||
+ PRIMARYLANGID(input_language_id_) == LANG_JAPANESE) {
+ if (!system_caret_) {
+ if (::CreateCaret(hwnd_, NULL, 1, 1))
+ system_caret_ = true;
+ }
+ }
+ }
+
+ void OsrImeHandler::DestroyImeWindow() {
+ // Destroy the system caret if we have created for this IME input context.
+ if (system_caret_) {
+ ::DestroyCaret();
+ system_caret_ = false;
+ }
+ }
+
+ void OsrImeHandler::MoveImeWindow() {
+ // Does nothing when the target window has no input focus.
+ if (GetFocus() != hwnd_)
+ return;
+
+ CefRect rc = ime_rect_;
+ int location = cursor_index_;
+
+ // If location is not specified fall back to the composition range start.
+ if (location == -1)
+ location = composition_range_.from;
+
+ // Offset location by the composition range start if required.
+ if (location >= composition_range_.from)
+ location -= composition_range_.from;
+
+ if (location < static_cast(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) {
+ // Chinese IMEs ignore function calls to ::ImmSetCandidateWindow()
+ // when a user disables TSF (Text Service Framework) and CUAS (Cicero
+ // Unaware Application Support).
+ // On the other hand, when a user enables TSF and CUAS, Chinese IMEs
+ // ignore the position of the current system caret and use the
+ // parameters given to ::ImmSetCandidateWindow() with its 'dwStyle'
+ // parameter CFS_CANDIDATEPOS.
+ // Therefore, we do not only call ::ImmSetCandidateWindow() but also
+ // set the positions of the temporary system caret if it exists.
+ /*CANDIDATEFORM candidate_position = {
+ 0, CFS_CANDIDATEPOS, { rc.x, rc.y }, { 0, 0, 0, 0 }
+ };
+ ::ImmSetCandidateWindow(imc, &candidate_position);*/
+
+ COMPOSITIONFORM form = {
+ CFS_POINT,{ rc.x, rc.y },{ rc.x, rc.y, rc.x + rc.width, rc.y + rc.height }
+ };
+
+ auto ret = ::ImmSetCompositionWindow(imc, &form);
+ }
+
+ 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) {
+ // Korean IMEs require the lower-left corner of the caret to move their
+ // candidate windows.
+ rc.y += kCaretMargin;
+ }
+
+ COMPOSITIONFORM form = {
+ CFS_RECT,{ rc.x, rc.y },{ rc.x, rc.y, rc.x + rc.width, rc.y + rc.height }
+ };
+
+ auto ret = ::ImmSetCompositionWindow(imc, &form);
+ Debug::Assert(ret != 0);
+ //Debug::WriteLine("=======ImmSetCompositionWindow:{0}, Rc:[{1},{2}]", ret != 0, rc.x, rc.y);
+
+ // Japanese IMEs and Korean IMEs also use the rectangle given to
+ // ::ImmSetCandidateWindow() with its 'dwStyle' parameter CFS_EXCLUDE
+ // Therefore, we also set this parameter here.
+ //CANDIDATEFORM exclude_rectangle = {
+ // 0, CFS_EXCLUDE, { rc.x, rc.y },
+ // { rc.x, rc.y, rc.x + rc.width, rc.y + rc.height }
+ //};
+
+ //auto ret = ::ImmSetCandidateWindow(imc, &exclude_rectangle);
+ //Debug::WriteLine("=======ImmSetCandidateWindow:{0}, Rc:[{1},{2}]", ret != 0, rc.x, rc.y);
+ ::ImmReleaseContext(hwnd_, imc);
+ }
+ }
+
+ void OsrImeHandler::CleanupComposition() {
+ // Notify the IMM attached to the given window to complete the ongoing
+ // composition (when given window is de-activated while composing and
+ // re-activated) and reset the composition status.
+ if (is_composing_) {
+ HIMC imc = ::ImmGetContext(hwnd_);
+ if (imc) {
+ ::ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
+ ::ImmReleaseContext(hwnd_, imc);
+ }
+ ResetComposition();
+ }
+ }
+
+ void OsrImeHandler::ResetComposition() {
+ // Reset the composition status.
+ is_composing_ = false;
+ cursor_index_ = -1;
+ }
+
+
+ void OsrImeHandler::GetCompositionInfo(
+ HIMC imc,
+ LPARAM lparam,
+ CefString &composition_text,
+ std::vector &underlines,
+ int& composition_start) {
+ // We only care about GCS_COMPATTR, GCS_COMPCLAUSE and GCS_CURSORPOS, and
+ // convert them into underlines and selection range respectively.
+ underlines.clear();
+
+ int length = static_cast(composition_text.length());
+
+ // Find out the range selected by the user.
+ int target_start = length;
+ int target_end = length;
+ if (lparam & GCS_COMPATTR)
+ GetCompositionSelectionRange(imc, &target_start, &target_end);
+
+ // Retrieve the selection range information. If CS_NOMOVECARET is specified
+ // it means the cursor should not be moved and we therefore place the caret at
+ // the beginning of the composition string. Otherwise we should honour the
+ // GCS_CURSORPOS value if it's available.
+ // TODO(suzhe): Due to a bug in WebKit we currently can't use selection range
+ // with composition string.
+ // See: https://bugs.webkit.org/show_bug.cgi?id=40805
+ if (!(lparam & CS_NOMOVECARET) && (lparam & GCS_CURSORPOS)) {
+ // IMM32 does not support non-zero-width selection in a composition. So
+ // always use the caret position as selection range.
+ int cursor = ::ImmGetCompositionString(imc, GCS_CURSORPOS, NULL, 0);
+ composition_start = cursor;
+ } else {
+ composition_start = 0;
+ }
+
+ // Retrieve the clause segmentations and convert them to underlines.
+ if (lparam & GCS_COMPCLAUSE)
+ GetCompositionUnderlines(imc, target_start, target_end, underlines);
+
+ // Set default underlines in case there is no clause information.
+ 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 OsrImeHandler::GetString(HIMC imc, WPARAM lparam, int type,
+ CefString& result) {
+ if (!(lparam & type))
+ return false;
+ LONG string_size = ::ImmGetCompositionString(imc, type, NULL, 0);
+ if (string_size <= 0)
+ return false;
+
+ // For trailing NULL - ImmGetCompositionString excludes that.
+ string_size += sizeof(WCHAR);
+
+ std::vector buffer(string_size);
+ ::ImmGetCompositionString(imc, type, &buffer[0], string_size);
+ result.FromWString(&buffer[0]);
+ return true;
+ }
+
+ bool OsrImeHandler::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 OsrImeHandler::GetComposition(
+ LPARAM lparam,
+ CefString &composition_text,
+ std::vector &underlines,
+ int& composition_start) {
+ bool ret = false;
+ HIMC imc = ::ImmGetContext(hwnd_);
+ if (imc) {
+ // Copy the composition string to the CompositionText object.
+ ret = GetString(imc, lparam, GCS_COMPSTR, composition_text);
+
+ if (ret) {
+ // Retrieve the composition underlines and selection range information.
+ GetCompositionInfo(imc, lparam, composition_text, underlines,
+ composition_start);
+
+ // Mark that there is an ongoing composition.
+ is_composing_ = true;
+ }
+
+ ::ImmReleaseContext(hwnd_, imc);
+ }
+ return ret;
+ }
+
+ void OsrImeHandler::DisableIME() {
+ CleanupComposition();
+ ::ImmAssociateContextEx(hwnd_, NULL, 0);
+ }
+
+ void OsrImeHandler::CancelIME() {
+ if (is_composing_) {
+ HIMC imc = ::ImmGetContext(hwnd_);
+ if (imc) {
+ ::ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
+ ::ImmReleaseContext(hwnd_, imc);
+ }
+ ResetComposition();
+ }
+ }
+
+ void OsrImeHandler::EnableIME() {
+ // Load the default IME context.
+ ::ImmAssociateContextEx(hwnd_, NULL, IACE_DEFAULT);
+ }
+
+ void OsrImeHandler::UpdateCaretPosition(int index) {
+ // Save the caret position.
+ cursor_index_ = index;
+ // Move the IME window.
+ MoveImeWindow();
+ }
+
+ void OsrImeHandler::ChangeCompositionRange(
+ const CefRange& selection_range,
+ const std::vector& bounds) {
+ composition_range_ = selection_range;
+ composition_bounds_ = bounds;
+ MoveImeWindow();
+ }
+
+} // namespace CefSharp
\ No newline at end of file
diff --git a/CefSharp.Core/OsrImeHandler.h b/CefSharp.Core/OsrImeHandler.h
new file mode 100644
index 0000000000..eb01817fd1
--- /dev/null
+++ b/CefSharp.Core/OsrImeHandler.h
@@ -0,0 +1,117 @@
+// Copyright 2016 The Chromium Embedded Framework Authors. Portions copyright
+// 2013 The Chromium Authors. All rights reserved. Use of this source code is
+// governed by a BSD-style license that can be found in the LICENSE file.
+
+#ifndef CEF_TESTS_CEFCLIENT_BROWSER_OSR_IME_HANDLER_WIN_H_
+#define CEF_TESTS_CEFCLIENT_BROWSER_OSR_IME_HANDLER_WIN_H_
+#pragma once
+
+#include "Stdafx.h"
+#include
+//#include
+
+//#include "include/internal/cef_types_wrappers.h"
+
+namespace CefSharp {
+
+ // Handles IME for the native parent window that hosts an off-screen browser.
+ // This object is only accessed on the CEF UI thread.
+ class OsrImeHandler {
+ public:
+ explicit OsrImeHandler(HWND hwnd);
+ virtual ~OsrImeHandler();
+
+ // Retrieves whether or not there is an ongoing composition.
+ bool is_composing() const { return is_composing_; }
+
+ // Retrieves the input language from Windows and update it.
+ void SetInputLanguage();
+
+ // Creates the IME caret windows if required.
+ void CreateImeWindow();
+
+ // Destroys the IME caret windows.
+ void DestroyImeWindow();
+
+ // Cleans up the all resources attached to the given IMM32Manager object, and
+ // reset its composition status.
+ void CleanupComposition();
+
+ // Resets the composition status and cancels the ongoing composition.
+ void ResetComposition();
+
+ // Retrieves a composition result of the ongoing composition if it exists.
+ bool GetResult(LPARAM lparam, CefString& result);
+
+ // Retrieves the current composition status of the ongoing composition.
+ // Includes composition text, underline information and selection range in the
+ // composition text. IMM32 does not support char selection.
+ bool GetComposition(LPARAM lparam, CefString &composition_text,
+ std::vector &underlines,
+ int& composition_start);
+
+ // Enables the IME attached to the given window.
+ virtual void EnableIME();
+
+ // Disables the IME attached to the given window.
+ virtual void DisableIME();
+
+ // Cancels an ongoing composition of the IME.
+ virtual void CancelIME();
+
+ // Updates the IME caret position of the given window.
+ void UpdateCaretPosition(int index);
+
+ // Updates the composition range. |selected_range| is the range of characters
+ // that have been selected. |character_bounds| is the bounds of each character
+ // in view device coordinates.
+ void ChangeCompositionRange(const CefRange& selection_range,
+ const std::vector& character_bounds);
+
+ // Updates the position of the IME windows.
+ void MoveImeWindow();
+
+ private:
+ // Retrieves the composition information.
+ void GetCompositionInfo(HIMC imm_context, LPARAM lparam,
+ CefString &composition_text,
+ std::vector& underlines,
+ int& composition_start);
+
+ // Retrieves a string from the IMM.
+ bool GetString(HIMC imm_context, WPARAM lparam, int type, CefString& result);
+
+ // Represents whether or not there is an ongoing composition.
+ bool is_composing_;
+
+ // The current composition character range and its bounds.
+ std::vector composition_bounds_;
+
+ // This value represents whether or not the current input context has IMEs.
+ bool ime_status_;
+
+ // The current input Language ID retrieved from Windows -
+ // used for processing language-specific operations in IME.
+ LANGID input_language_id_;
+
+ // Represents whether or not the current input context has created a system
+ // caret to set the position of its IME candidate window.
+ bool system_caret_;
+
+ // The rectangle of the input caret retrieved from a renderer process.
+ CefRect ime_rect_;
+
+ // The current cursor index in composition string.
+ int cursor_index_;
+
+ // The composition range in the string. This may be used to determine the
+ // offset in composition bounds.
+ CefRange composition_range_;
+
+ // Hwnd associated with this instance.
+ HWND hwnd_;
+ };
+
+} // namespace client
+
+#endif // CEF_TESTS_CEFCLIENT_BROWSER_OSR_IME_HANDLER_WIN_H_
\ No newline at end of file
diff --git a/CefSharp.Core/OsrImeWin.cpp b/CefSharp.Core/OsrImeWin.cpp
new file mode 100644
index 0000000000..a1ad49b57e
--- /dev/null
+++ b/CefSharp.Core/OsrImeWin.cpp
@@ -0,0 +1,154 @@
+#include "Stdafx.h"
+#include "OsrImeWin.h"
+#include "Internals\CefBrowserHostWrapper.h"
+
+namespace CefSharp {
+ OsrImeWin::OsrImeWin(IntPtr ownerHWnd, IBrowser^ browser) {
+ _ownerHWnd = ownerHWnd;
+ _browser = browser;
+ _wndProcHandler = gcnew WndProcDelegate(this, &OsrImeWin::WndProc);
+
+ _imeHandler = new OsrImeHandler(static_cast(_ownerHWnd.ToPointer()));
+ }
+
+ OsrImeWin::~OsrImeWin() {
+ _wndProcHandler = nullptr;
+ _browser = nullptr;
+ if (_imeHandler) {
+ delete _imeHandler;
+ }
+ }
+
+ void OsrImeWin::OnIMESetContext(UINT message, WPARAM wParam, LPARAM lParam) {
+ // We handle the IME Composition Window ourselves (but let the IME Candidates
+ // Window be handled by IME through DefWindowProc()), so clear the
+ // ISC_SHOWUICOMPOSITIONWINDOW flag:
+ lParam &= ~ISC_SHOWUICOMPOSITIONWINDOW;
+ ::DefWindowProc(static_cast(_ownerHWnd.ToPointer()), message, wParam, lParam);
+
+ // Create Caret Window if required
+ if (_imeHandler) {
+ _imeHandler->CreateImeWindow();
+ _imeHandler->MoveImeWindow();
+ }
+ }
+
+ void OsrImeWin::OnIMEStartComposition() {
+ if (_imeHandler) {
+ _imeHandler->CreateImeWindow();
+ _imeHandler->MoveImeWindow();
+ _imeHandler->ResetComposition();
+ }
+ }
+
+ void OsrImeWin::OnIMEComposition(UINT message, WPARAM wParam, LPARAM lParam) {
+ if (_browser && _imeHandler) {
+ CefString cTextStr;
+ if (_imeHandler->GetResult(lParam, cTextStr)) {
+ // Send the text to the browser. The |replacement_range| and
+ // |relative_cursor_pos| params are not used on Windows, so provide
+ // default invalid values.
+
+ auto host = safe_cast(_browser->GetHost());
+ //host->ImeCommitText(cTextStr)
+ host->ImeCommitText(cTextStr,
+ CefRange(UINT32_MAX, UINT32_MAX), 0);
+ _imeHandler->ResetComposition();
+ // Continue reading the composition string - Japanese IMEs send both
+ // GCS_RESULTSTR and GCS_COMPSTR.
+ }
+
+ std::vector underlines;
+ int composition_start = 0;
+
+ if (_imeHandler->GetComposition(lParam, cTextStr, underlines,
+ composition_start)) {
+ // Send the composition string to the browser. The |replacement_range|
+ // param is not used on Windows, so provide a default invalid value.
+
+ auto host = safe_cast(_browser->GetHost());
+ host->ImeSetComposition(cTextStr, underlines,
+ CefRange(UINT32_MAX, UINT32_MAX),
+ CefRange(composition_start,
+ static_cast(composition_start + cTextStr.length())));
+
+ // Update the Candidate Window position. The cursor is at the end so
+ // subtract 1. This is safe because IMM32 does not support non-zero-width
+ // in a composition. Also, negative values are safely ignored in
+ // MoveImeWindow
+ _imeHandler->UpdateCaretPosition(composition_start - 1);
+ } else {
+ OnIMECancelCompositionEvent();
+ }
+ }
+ }
+
+ void OsrImeWin::OnIMECancelCompositionEvent() {
+ if (_browser && _imeHandler) {
+ _browser->GetHost()->ImeCancelComposition();
+ _imeHandler->ResetComposition();
+ _imeHandler->DestroyImeWindow();
+ }
+ }
+
+ void OsrImeWin::OnImeCompositionRangeChanged(Range selectedRange, array^ characterBounds) {
+ if (_imeHandler) {
+ // Convert from view coordinates to device coordinates.
+ /*RectList device_bounds;
+ CefRenderHandler::RectList::const_iterator it = character_bounds.begin();
+ for (; it != character_bounds.end(); ++it) {
+ device_bounds.push_back(LogicalToDevice(*it, device_scale_factor_));
+ }*/
+
+ std::vector device_bounds;
+ for each(Rect rect in characterBounds) {
+ CefRect rc(rect.X, rect.Y, rect.Width, rect.Height);
+ device_bounds.push_back(rc);
+ }
+
+ CefRange selection_range(selectedRange.From, selectedRange.To);
+ _imeHandler->ChangeCompositionRange(selection_range, device_bounds);
+ }
+ }
+
+ void OsrImeWin::OnKeyEvent(int message, int wParam, int lParam) {
+ if (!_browser)
+ return;
+
+ auto host = safe_cast(_browser->GetHost());
+ host->SendKeyEvent(message, wParam, lParam);
+ }
+
+ IntPtr OsrImeWin::WndProc(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam) {
+ WPARAM pwParam;
+ switch (message) {
+ case WM_IME_SETCONTEXT:
+ OnIMESetContext((UINT)message,
+ reinterpret_cast(wParam.ToPointer()),
+ reinterpret_cast(lParam.ToPointer()));
+ return IntPtr::Zero;
+ case WM_IME_STARTCOMPOSITION:
+ OnIMEStartComposition();
+ return IntPtr::Zero;
+ case WM_IME_COMPOSITION:
+ OnIMEComposition((UINT)message,
+ reinterpret_cast(wParam.ToPointer()),
+ reinterpret_cast(lParam.ToPointer()));
+ return IntPtr::Zero;
+ case WM_IME_ENDCOMPOSITION:
+ OnIMECancelCompositionEvent();
+ // Let WTL call::DefWindowProc() and release its resources.
+ break;
+ case WM_SYSCHAR:
+ case WM_SYSKEYDOWN:
+ case WM_SYSKEYUP:
+ case WM_KEYDOWN:
+ case WM_KEYUP:
+ case WM_CHAR:
+ OnKeyEvent(message, reinterpret_cast(wParam.ToPointer()), reinterpret_cast(lParam.ToPointer()));
+ break;
+ }
+
+ return IntPtr(1);
+ }
+}
\ No newline at end of file
diff --git a/CefSharp.Core/OsrImeWin.h b/CefSharp.Core/OsrImeWin.h
new file mode 100644
index 0000000000..829e2cf573
--- /dev/null
+++ b/CefSharp.Core/OsrImeWin.h
@@ -0,0 +1,34 @@
+#pragma once
+
+#include "OsrImeHandler.h"
+
+namespace CefSharp {
+
+ public ref class OsrImeWin {
+ public:
+ delegate IntPtr WndProcDelegate(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam);
+ OsrImeWin(IntPtr ownerHWnd, IBrowser^ browser);
+ ~OsrImeWin();
+ property WndProcDelegate^ WndProcHandler {
+ WndProcDelegate^ get() {
+ return _wndProcHandler;
+ }
+ }
+
+ void OnImeCompositionRangeChanged(Range selectedRange, array^ characterBounds);
+
+ protected:
+ void OnIMESetContext(UINT message, WPARAM wParam, LPARAM lParam);
+ void OnIMEStartComposition();
+ void OnIMEComposition(UINT message, WPARAM wParam, LPARAM lParam);
+ void OnIMECancelCompositionEvent();
+ void OnKeyEvent(int message, int wParam, int lParam);
+
+ IntPtr WndProc(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam);
+
+ IntPtr _ownerHWnd;
+ WndProcDelegate^ _wndProcHandler;
+ IBrowser^ _browser;
+ OsrImeHandler* _imeHandler;
+ };
+}
diff --git a/CefSharp.Wpf.Example/App.xaml.cs b/CefSharp.Wpf.Example/App.xaml.cs
index 08e4a0037b..6e90491b76 100644
--- a/CefSharp.Wpf.Example/App.xaml.cs
+++ b/CefSharp.Wpf.Example/App.xaml.cs
@@ -22,7 +22,7 @@ protected override void OnStartup(StartupEventArgs e)
}
#endif
- const bool multiThreadedMessageLoop = true;
+ const bool multiThreadedMessageLoop = false;
IBrowserProcessHandler browserProcessHandler;
diff --git a/CefSharp.Wpf.Example/CefSharp.Wpf.Example.csproj b/CefSharp.Wpf.Example/CefSharp.Wpf.Example.csproj
index e19e0ee7bb..7a654e3f4d 100644
--- a/CefSharp.Wpf.Example/CefSharp.Wpf.Example.csproj
+++ b/CefSharp.Wpf.Example/CefSharp.Wpf.Example.csproj
@@ -157,7 +157,9 @@
-
+
+ Designer
+
diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs
index ec32d5aa05..7b6a9a92e8 100644
--- a/CefSharp.Wpf/ChromiumWebBrowser.cs
+++ b/CefSharp.Wpf/ChromiumWebBrowser.cs
@@ -19,6 +19,9 @@
using System.Windows.Threading;
using CefSharp.ModelBinding;
using System.Runtime.CompilerServices;
+using System.Collections.Generic;
+using System.Linq;
+using System.Diagnostics;
namespace CefSharp.Wpf
{
@@ -96,6 +99,8 @@ public class ChromiumWebBrowser : ContentControl, IRenderWebBrowser, IWpfWebBrow
///
private IRequestContext requestContext;
+ private OsrImeWin _imeWin;
+
///
/// A flag that indicates whether or not the designer is active
/// NOTE: Needs to be static for OnApplicationExit
@@ -386,6 +391,26 @@ static ChromiumWebBrowser()
app.Exit += OnApplicationExit;
}
}
+
+ InputMethod.IsInputMethodEnabledProperty.OverrideMetadata(
+ typeof(ChromiumWebBrowser),
+ new FrameworkPropertyMetadata(
+ true,
+ FrameworkPropertyMetadataOptions.Inherits,
+ (obj, e) =>
+ {
+ var browser = obj as ChromiumWebBrowser;
+ if ((bool)e.NewValue && browser.browser != null && Keyboard.FocusedElement == browser)
+ {
+ browser.browser.GetHost().SendFocusEvent(true);
+ InputMethod.SetIsInputMethodSuspended(browser, true);
+ }
+ }));
+ InputMethod.IsInputMethodSuspendedProperty.OverrideMetadata(
+ typeof(ChromiumWebBrowser),
+ new FrameworkPropertyMetadata(
+ true,
+ FrameworkPropertyMetadataOptions.Inherits));
}
///
@@ -471,7 +496,7 @@ private void NoInliningConstructor()
PresentationSource.AddSourceChangedHandler(this, PresentationSourceChangedHandler);
- RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
+ RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.NearestNeighbor);
}
///
@@ -572,6 +597,12 @@ protected virtual void Dispose(bool isDisposing)
{
SetCurrentValue(IsBrowserInitializedProperty, false);
WebBrowser = null;
+
+ if (_imeWin != null)
+ {
+ _imeWin.Dispose();
+ _imeWin = null;
+ }
});
}
@@ -828,6 +859,24 @@ void IRenderWebBrowser.OnImeCompositionRangeChanged(Range selectedRange, Rect[]
protected virtual void OnImeCompositionRangeChanged(Range selectedRange, Rect[] characterBounds)
{
//TODO: Implement this
+ if (_imeWin != null)
+ {
+ UiThreadRunAsync(() =>
+ {
+ IList rects = new List();
+ foreach (var item in characterBounds)
+ {
+ GeneralTransform generalTransform1 = TransformToAncestor(this);
+
+ if (CleanupElement != null)
+ {
+ Point point = this.TransformToAncestor(CleanupElement).Transform(new Point(0, 0));
+ rects.Add(new Rect((int)(point.X + item.X), (int)(point.Y + item.Y), item.Width, item.Height));
+ }
+ }
+ _imeWin.OnImeCompositionRangeChanged(selectedRange, rects.ToArray());
+ });
+ }
}
///
@@ -988,6 +1037,17 @@ void IWebBrowserInternal.OnAfterBrowserCreated(IBrowser browser)
{
if (!IsDisposed)
{
+ if (_imeWin != null)
+ {
+ _imeWin.Dispose();
+ }
+
+ if (source != null)
+ {
+ _imeWin = new OsrImeWin(source.Handle, this.browser);
+ //Debug.WriteLine($"=== IME ===: imewin init, OnAfterBrowserCreated: browserId - {browser.Identifier} )");
+ }
+
SetCurrentValue(IsBrowserInitializedProperty, true);
// If Address was previously set, only now can we actually do the load
@@ -1581,6 +1641,12 @@ private void PresentationSourceChangedHandler(object sender, SourceChangedEventA
{
RemoveSourceHook();
+ if (_imeWin != null)
+ {
+ _imeWin.Dispose();
+ _imeWin = null;
+ }
+
var window = args.OldSource.RootVisual as Window;
if (window != null)
{
@@ -1757,6 +1823,35 @@ private static void CefShutdown()
Cef.Shutdown();
}
+ protected override void OnGotFocus(RoutedEventArgs e)
+ {
+ base.OnGotFocus(e);
+
+ if (_imeWin == null && browser != null && source != null)
+ {
+ _imeWin = new OsrImeWin(source.Handle, browser);
+ }
+
+ InputMethod.SetIsInputMethodEnabled(this, true);
+ InputMethod.SetIsInputMethodSuspended(this, true);
+ //Debug.WriteLine($"=== IME ===: IsInputMethodEnabled:{InputMethod.GetIsInputMethodEnabled(this)}, IsInputMethodSuspended:{InputMethod.GetIsInputMethodSuspended(this)}, OnGotFocus: browserId - {browser?.Identifier} )");
+ }
+
+ protected override void OnLostFocus(RoutedEventArgs e)
+ {
+ base.OnLostFocus(e);
+
+ InputMethod.SetIsInputMethodEnabled(this, false);
+ InputMethod.SetIsInputMethodSuspended(this, false);
+ //Debug.WriteLine($"=== IME ===: IsInputMethodEnabled:{InputMethod.GetIsInputMethodEnabled(this)}, IsInputMethodSuspended:{InputMethod.GetIsInputMethodSuspended(this)}, OnLostFocus: browserId - {browser?.Identifier} )");
+
+ if (_imeWin != null)
+ {
+ _imeWin.Dispose();
+ _imeWin = null;
+ }
+ }
+
///
/// Handles the event.
///
@@ -1859,37 +1954,15 @@ protected virtual IntPtr SourceHook(IntPtr hWnd, int message, IntPtr wParam, Int
return IntPtr.Zero;
}
- switch ((WM)message)
+ if (!IsDisposed && _imeWin != null && IsKeyboardFocused && browser != null)
{
- case WM.SYSCHAR:
- case WM.SYSKEYDOWN:
- case WM.SYSKEYUP:
- case WM.KEYDOWN:
- case WM.KEYUP:
- case WM.CHAR:
- case WM.IME_CHAR:
+ var ret = _imeWin.WndProcHandler(hWnd, message, wParam, lParam);
+ if (ret == IntPtr.Zero)
{
- if (!IsKeyboardFocused)
- {
- break;
- }
-
- if (message == (int)WM.SYSKEYDOWN &&
- wParam.ToInt32() == KeyInterop.VirtualKeyFromKey(Key.F4))
- {
- // We don't want CEF to receive this event (and mark it as handled), since that makes it impossible to
- // shut down a CefSharp-based app by pressing Alt-F4, which is kind of bad.
- return IntPtr.Zero;
- }
-
- if (browser != null)
- {
- browser.GetHost().SendKeyEvent(message, wParam.CastToInt32(), lParam.CastToInt32());
- handled = true;
- }
-
- break;
+ handled = true;
}
+
+ return ret;
}
return IntPtr.Zero;