mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-05 11:42:36 +00:00
WPF - Add IME Support (#2757)
* WPF - Add initial IME implementation * WPF - WpfImeKeyboardHandler handler code cleanup - Restructure ImeHandler to be a static class as it really didn't maintain any state, now hwnd is passed in as param - Renamed many of the methods to Ime for consistency (IME in all caps doesn't patch the naming convention used) - The WpfIMEKeyboardHandler implementation is pretty much self contained now, only a single reference in ChromiumWebBrowser also moved into Experimental folder/namespace
This commit is contained in:
@@ -81,12 +81,15 @@
|
||||
<Compile Include="CefSettings.cs" />
|
||||
<Compile Include="Experimental\ChromiumWebBrowserWithTouchSupport.cs" />
|
||||
<Compile Include="Internals\DragOperationMaskExtensions.cs" />
|
||||
<Compile Include="Internals\ImeHandler.cs" />
|
||||
<Compile Include="Internals\MonitorInfo.cs" />
|
||||
<Compile Include="Internals\MonitorInfoEx.cs" />
|
||||
<Compile Include="Internals\ImeNative.cs" />
|
||||
<Compile Include="Internals\RectStruct.cs" />
|
||||
<Compile Include="Experimental\WpfImeKeyboardHandler.cs" />
|
||||
<Compile Include="Internals\WpfKeyboardHandler.cs" />
|
||||
<Compile Include="IWpfKeyboardHandler.cs" />
|
||||
<Compile Include="Internals\VirtualKeys.cs" />
|
||||
<Compile Include="Internals\WpfKeyboardHandler.cs" />
|
||||
<Compile Include="Internals\WpfLegacyKeyboardHandler.cs" />
|
||||
<Compile Include="DelegateCommand.cs" />
|
||||
<Compile Include="IRenderHandler.cs" />
|
||||
|
||||
@@ -15,6 +15,7 @@ using System.Windows.Threading;
|
||||
using CefSharp.Enums;
|
||||
using CefSharp.Internals;
|
||||
using CefSharp.Structs;
|
||||
using CefSharp.Wpf.Experimental;
|
||||
using CefSharp.Wpf.Internals;
|
||||
using CefSharp.Wpf.Rendering;
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
@@ -973,7 +974,11 @@ namespace CefSharp.Wpf
|
||||
/// <param name="characterBounds">is the bounds of each character in view coordinates.</param>
|
||||
protected virtual void OnImeCompositionRangeChanged(Range selectedRange, Rect[] characterBounds)
|
||||
{
|
||||
//TODO: Implement this
|
||||
var imeKeyboardHandler = WpfKeyboardHandler as WpfImeKeyboardHandler;
|
||||
if (imeKeyboardHandler != null)
|
||||
{
|
||||
imeKeyboardHandler.ChangeCompositionRange(selectedRange, characterBounds);
|
||||
}
|
||||
}
|
||||
|
||||
void IRenderWebBrowser.OnVirtualKeyboardRequested(IBrowser browser, TextInputMode inputMode)
|
||||
@@ -1794,7 +1799,7 @@ namespace CefSharp.Wpf
|
||||
/// </summary>
|
||||
/// <param name="action">The action.</param>
|
||||
/// <param name="priority">The priority.</param>
|
||||
private void UiThreadRunAsync(Action action, DispatcherPriority priority = DispatcherPriority.DataBind)
|
||||
internal void UiThreadRunAsync(Action action, DispatcherPriority priority = DispatcherPriority.DataBind)
|
||||
{
|
||||
if (Dispatcher.CheckAccess())
|
||||
{
|
||||
|
||||
@@ -0,0 +1,348 @@
|
||||
// Copyright © 2018 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.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Interop;
|
||||
using CefSharp.Internals;
|
||||
using CefSharp.Structs;
|
||||
using CefSharp.Wpf.Internals;
|
||||
using Point = System.Windows.Point;
|
||||
using Rect = CefSharp.Structs.Rect;
|
||||
|
||||
namespace CefSharp.Wpf.Experimental
|
||||
{
|
||||
public class WpfImeKeyboardHandler : WpfKeyboardHandler
|
||||
{
|
||||
private int languageCodeId;
|
||||
private bool systemCaret;
|
||||
private bool isDisposed;
|
||||
private List<Rect> compositionBounds = new List<Rect>();
|
||||
private HwndSource source;
|
||||
private HwndSourceHook sourceHook;
|
||||
private bool hasImeComposition;
|
||||
private MouseButtonEventHandler mouseDownEventHandler;
|
||||
private bool isActive;
|
||||
|
||||
public WpfImeKeyboardHandler(ChromiumWebBrowser owner) : base(owner)
|
||||
{
|
||||
}
|
||||
|
||||
public void ChangeCompositionRange(Range selectionRange, Rect[] characterBounds)
|
||||
{
|
||||
if (!isActive)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var screenInfo = ((IRenderWebBrowser)owner).GetScreenInfo();
|
||||
var scaleFactor = screenInfo.HasValue ? screenInfo.Value.DeviceScaleFactor : 1.0f;
|
||||
|
||||
//This is called on the CEF UI thread, we need to invoke back onte main UI thread to
|
||||
//access the UI controls
|
||||
owner.UiThreadRunAsync(() =>
|
||||
{
|
||||
//TODO: Getting the root window for every composition range change seems expensive,
|
||||
//we should cache the position and update it on window move.
|
||||
var parentWindow = Window.GetWindow(owner);
|
||||
if (parentWindow != null)
|
||||
{
|
||||
//TODO: What are we calculating here exactly???
|
||||
var point = owner.TransformToAncestor(parentWindow).Transform(new Point(0, 0));
|
||||
|
||||
var rects = new List<Rect>();
|
||||
|
||||
foreach (var item in characterBounds)
|
||||
{
|
||||
rects.Add(new Rect(
|
||||
(int)((point.X + item.X) * scaleFactor),
|
||||
(int)((point.Y + item.Y) * scaleFactor),
|
||||
(int)(item.Width * scaleFactor),
|
||||
(int)(item.Height * scaleFactor)));
|
||||
}
|
||||
|
||||
compositionBounds = rects;
|
||||
MoveImeWindow(source.Handle);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public override void Setup(HwndSource source)
|
||||
{
|
||||
this.source = source;
|
||||
sourceHook = SourceHook;
|
||||
source.AddHook(SourceHook);
|
||||
|
||||
owner.GotFocus += OwnerGotFocus;
|
||||
owner.LostFocus += OwnerLostFocus;
|
||||
|
||||
mouseDownEventHandler = new MouseButtonEventHandler(OwnerMouseDown);
|
||||
|
||||
owner.AddHandler(UIElement.MouseDownEvent, mouseDownEventHandler, true);
|
||||
|
||||
owner.Focus();
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
if (isDisposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
isDisposed = true;
|
||||
|
||||
owner.GotFocus -= OwnerGotFocus;
|
||||
owner.LostFocus -= OwnerLostFocus;
|
||||
|
||||
owner.RemoveHandler(UIElement.MouseDownEvent, mouseDownEventHandler);
|
||||
|
||||
if (source != null && sourceHook != null)
|
||||
{
|
||||
source.RemoveHook(sourceHook);
|
||||
source = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void OwnerMouseDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
CloseImeComposition();
|
||||
}
|
||||
|
||||
private void OwnerLostFocus(object sender, RoutedEventArgs e)
|
||||
{
|
||||
isActive = false;
|
||||
|
||||
// These calls are needed in order for IME to function correctly.
|
||||
InputMethod.SetIsInputMethodEnabled(owner, false);
|
||||
InputMethod.SetIsInputMethodSuspended(owner, false);
|
||||
}
|
||||
|
||||
private void OwnerGotFocus(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// These calls are needed in order for IME to function correctly.
|
||||
InputMethod.SetIsInputMethodEnabled(owner, true);
|
||||
InputMethod.SetIsInputMethodSuspended(owner, true);
|
||||
|
||||
isActive = true;
|
||||
}
|
||||
|
||||
private IntPtr SourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
|
||||
{
|
||||
handled = false;
|
||||
|
||||
if (!isActive || isDisposed || owner == null || owner.IsDisposed || owner.GetBrowserHost() == null)
|
||||
{
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
|
||||
switch ((WM)msg)
|
||||
{
|
||||
case WM.IME_SETCONTEXT:
|
||||
{
|
||||
OnImeSetContext(hwnd, (uint)msg, wParam, lParam);
|
||||
handled = true;
|
||||
break;
|
||||
}
|
||||
case WM.IME_STARTCOMPOSITION:
|
||||
{
|
||||
OnIMEStartComposition(hwnd);
|
||||
hasImeComposition = true;
|
||||
handled = true;
|
||||
break;
|
||||
}
|
||||
case WM.IME_COMPOSITION:
|
||||
{
|
||||
OnImeComposition(hwnd, lParam.ToInt32());
|
||||
handled = true;
|
||||
break;
|
||||
}
|
||||
case WM.IME_ENDCOMPOSITION:
|
||||
{
|
||||
OnImeEndComposition(hwnd);
|
||||
hasImeComposition = false;
|
||||
handled = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return handled ? IntPtr.Zero : new IntPtr(1);
|
||||
}
|
||||
|
||||
private void CloseImeComposition()
|
||||
{
|
||||
if (hasImeComposition)
|
||||
{
|
||||
// Set focus to 0, which destroys IME suggestions window.
|
||||
ImeNative.SetFocus(IntPtr.Zero);
|
||||
// Restore focus.
|
||||
ImeNative.SetFocus(source.Handle);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnImeComposition(IntPtr hwnd, int lParam)
|
||||
{
|
||||
string text = string.Empty;
|
||||
|
||||
if (ImeHandler.GetResult(hwnd, (uint)lParam, out text))
|
||||
{
|
||||
owner.GetBrowserHost().ImeCommitText(text, new Range(int.MaxValue, int.MaxValue), 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
var underlines = new List<CompositionUnderline>();
|
||||
int compositionStart = 0;
|
||||
|
||||
if (ImeHandler.GetComposition(hwnd, (uint)lParam, underlines, ref compositionStart, out text))
|
||||
{
|
||||
owner.GetBrowserHost().ImeSetComposition(text, underlines.ToArray(), new Range(int.MaxValue, int.MaxValue), new Range(compositionStart, compositionStart));
|
||||
|
||||
UpdateCaretPosition(compositionStart - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
CancelComposition(hwnd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void CancelComposition(IntPtr hwnd)
|
||||
{
|
||||
owner.GetBrowserHost().ImeCancelComposition();
|
||||
DestroyImeWindow(hwnd);
|
||||
}
|
||||
|
||||
private void OnImeEndComposition(IntPtr hwnd)
|
||||
{
|
||||
owner.GetBrowserHost().ImeFinishComposingText(false);
|
||||
DestroyImeWindow(hwnd);
|
||||
}
|
||||
|
||||
private void OnImeSetContext(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr 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:
|
||||
ImeNative.DefWindowProc(hwnd, msg, wParam, (IntPtr)(lParam.ToInt64() & ~ImeNative.ISC_SHOWUICOMPOSITIONWINDOW));
|
||||
// TODO: should we call ImmNotifyIME?
|
||||
|
||||
CreateImeWindow(hwnd);
|
||||
MoveImeWindow(hwnd);
|
||||
}
|
||||
|
||||
private void OnIMEStartComposition(IntPtr hwnd)
|
||||
{
|
||||
CreateImeWindow(hwnd);
|
||||
MoveImeWindow(hwnd);
|
||||
}
|
||||
|
||||
private void CreateImeWindow(IntPtr hwnd)
|
||||
{
|
||||
// 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.
|
||||
languageCodeId = PrimaryLangId(InputLanguageManager.Current.CurrentInputLanguage.KeyboardLayoutId);
|
||||
|
||||
if (languageCodeId == ImeNative.LANG_JAPANESE || languageCodeId == ImeNative.LANG_CHINESE)
|
||||
{
|
||||
if (!systemCaret)
|
||||
{
|
||||
if (ImeNative.CreateCaret(hwnd, IntPtr.Zero, 1, 1))
|
||||
{
|
||||
systemCaret = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int PrimaryLangId(int lgid)
|
||||
{
|
||||
return lgid & 0x3ff;
|
||||
}
|
||||
|
||||
private void MoveImeWindow(IntPtr hwnd)
|
||||
{
|
||||
if (compositionBounds.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var hIMC = ImeNative.ImmGetContext(hwnd);
|
||||
|
||||
var rc = compositionBounds[0];
|
||||
|
||||
var x = rc.X + rc.Width;
|
||||
var y = rc.Y + rc.Height;
|
||||
|
||||
const int kCaretMargin = 1;
|
||||
// As written in a comment in ImeInput::CreateImeWindow(),
|
||||
// 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 uses 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.
|
||||
var candidatePosition = new ImeNative.CANDIDATEFORM
|
||||
{
|
||||
dwIndex = 0,
|
||||
dwStyle = (int)ImeNative.CFS_CANDIDATEPOS,
|
||||
ptCurrentPos = new ImeNative.POINT(x, y),
|
||||
rcArea = new ImeNative.RECT(0, 0, 0, 0)
|
||||
};
|
||||
ImeNative.ImmSetCandidateWindow(hIMC, ref candidatePosition);
|
||||
|
||||
if (systemCaret)
|
||||
{
|
||||
ImeNative.SetCaretPos(x, y);
|
||||
}
|
||||
|
||||
if (languageCodeId == ImeNative.LANG_KOREAN)
|
||||
{
|
||||
// Chinese IMEs and Japanese IMEs require the upper-left corner of
|
||||
// the caret to move the position of their candidate windows.
|
||||
// On the other hand, Korean IMEs require the lower-left corner of the
|
||||
// caret to move their candidate windows.
|
||||
y += kCaretMargin;
|
||||
}
|
||||
// Japanese IMEs and Korean IMEs also use the rectangle given to
|
||||
// ::ImmSetCandidateWindow() with its 'dwStyle' parameter CFS_EXCLUDE
|
||||
// to move their candidate windows when a user disables TSF and CUAS.
|
||||
// Therefore, we also set this parameter here.
|
||||
var excludeRectangle = new ImeNative.CANDIDATEFORM
|
||||
{
|
||||
dwIndex = 0,
|
||||
dwStyle = (int)ImeNative.CFS_EXCLUDE,
|
||||
ptCurrentPos = new ImeNative.POINT(x, y),
|
||||
rcArea = new ImeNative.RECT(rc.X, rc.Y, x, y + kCaretMargin)
|
||||
};
|
||||
ImeNative.ImmSetCandidateWindow(hIMC, ref excludeRectangle);
|
||||
|
||||
ImeNative.ImmReleaseContext(hwnd, hIMC);
|
||||
}
|
||||
|
||||
private void DestroyImeWindow(IntPtr hwnd)
|
||||
{
|
||||
if (systemCaret)
|
||||
{
|
||||
ImeNative.DestroyCaret();
|
||||
systemCaret = false;
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Should we remove this, it's only a single method
|
||||
private void UpdateCaretPosition(int index)
|
||||
{
|
||||
MoveImeWindow(source.Handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
// Copyright © 2018 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.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using CefSharp.Structs;
|
||||
|
||||
namespace CefSharp.Wpf.Internals
|
||||
{
|
||||
/// <summary>
|
||||
/// ImeHandler provides implementation when message WM_IME_COMPOSITION is received.
|
||||
/// </summary>
|
||||
public static class ImeHandler
|
||||
{
|
||||
// Black SkColor value for underline.
|
||||
internal const uint ColorUNDERLINE = 0xFF000000;
|
||||
// White SkColor value for background.
|
||||
internal const uint ColorBKCOLOR = 0xFFFFFFFF;
|
||||
|
||||
public static bool GetResult(IntPtr hwnd, uint lParam, out string text)
|
||||
{
|
||||
var hIMC = ImeNative.ImmGetContext(hwnd);
|
||||
|
||||
var ret = GetString(hIMC, lParam, ImeNative.GCS_RESULTSTR, out text);
|
||||
|
||||
ImeNative.ImmReleaseContext(hwnd, hIMC);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static bool GetComposition(IntPtr hwnd, uint lParam, List<CompositionUnderline> underlines, ref int compositionStart, out string text)
|
||||
{
|
||||
var hIMC = ImeNative.ImmGetContext(hwnd);
|
||||
|
||||
bool ret = GetString(hIMC, lParam, ImeNative.GCS_COMPSTR, out text);
|
||||
if (ret)
|
||||
{
|
||||
GetCompositionInfo(hwnd, lParam, text, underlines, ref compositionStart);
|
||||
}
|
||||
|
||||
ImeNative.ImmReleaseContext(hwnd, hIMC);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private static bool GetString(IntPtr hIMC, uint lParam, uint type, out string text)
|
||||
{
|
||||
text = string.Empty;
|
||||
|
||||
if (!IsParam(lParam, type))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var strLen = ImeNative.ImmGetCompositionString(hIMC, type, null, 0);
|
||||
if (strLen <= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// buffer contains char (2 bytes)
|
||||
byte[] buffer = new byte[strLen];
|
||||
ImeNative.ImmGetCompositionString(hIMC, type, buffer, strLen);
|
||||
text = Encoding.Unicode.GetString(buffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void GetCompositionInfo(IntPtr hwnd, uint lParam, string text, List<CompositionUnderline> underlines, ref int compositionStart)
|
||||
{
|
||||
var hIMC = ImeNative.ImmGetContext(hwnd);
|
||||
|
||||
underlines.Clear();
|
||||
|
||||
int targetStart = text.Length;
|
||||
int targetEnd = text.Length;
|
||||
if (IsParam(lParam, ImeNative.GCS_COMPATTR))
|
||||
{
|
||||
GetCompositionSelectionRange(hIMC, ref targetStart, ref targetEnd);
|
||||
}
|
||||
|
||||
// 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.
|
||||
if (!IsParam(lParam, ImeNative.CS_NOMOVECARET) && IsParam(lParam, ImeNative.GCS_CURSORPOS))
|
||||
{
|
||||
// IMM32 does not support non-zero-width selection in a composition. So
|
||||
// always use the caret position as selection range.
|
||||
int cursor = (int)ImeNative.ImmGetCompositionString(hIMC, ImeNative.GCS_CURSORPOS, null, 0);
|
||||
compositionStart = cursor;
|
||||
}
|
||||
else
|
||||
{
|
||||
compositionStart = 0;
|
||||
}
|
||||
|
||||
if (IsParam(lParam, ImeNative.GCS_COMPCLAUSE))
|
||||
{
|
||||
GetCompositionUnderlines(hIMC, targetStart, targetEnd, underlines);
|
||||
}
|
||||
|
||||
if (underlines.Count == 0)
|
||||
{
|
||||
var range = new Range();
|
||||
|
||||
bool thick = false;
|
||||
|
||||
if (targetStart > 0)
|
||||
{
|
||||
range = new Range(0, targetStart);
|
||||
}
|
||||
|
||||
if (targetEnd > targetStart)
|
||||
{
|
||||
range = new Range(targetStart, targetEnd);
|
||||
thick = true;
|
||||
}
|
||||
|
||||
if (targetEnd < text.Length)
|
||||
{
|
||||
range = new Range(targetEnd, text.Length);
|
||||
}
|
||||
|
||||
underlines.Add(new CompositionUnderline(range, ColorUNDERLINE, ColorBKCOLOR, thick));
|
||||
}
|
||||
|
||||
ImeNative.ImmReleaseContext(hwnd, hIMC);
|
||||
}
|
||||
|
||||
private static void GetCompositionUnderlines(IntPtr hIMC, int targetStart, int targetEnd, List<CompositionUnderline> underlines)
|
||||
{
|
||||
var clauseSize = ImeNative.ImmGetCompositionString(hIMC, ImeNative.GCS_COMPCLAUSE, null, 0);
|
||||
if (clauseSize <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int clauseLength = (int)clauseSize / sizeof(Int32);
|
||||
|
||||
// buffer contains 32 bytes (4 bytes) array
|
||||
var clauseData = new byte[(int)clauseSize];
|
||||
ImeNative.ImmGetCompositionString(hIMC, ImeNative.GCS_COMPCLAUSE, clauseData, clauseSize);
|
||||
|
||||
var clauseLength_1 = clauseLength - 1;
|
||||
for (int i = 0; i < clauseLength_1; i++)
|
||||
{
|
||||
int from = BitConverter.ToInt32(clauseData, i * sizeof(Int32));
|
||||
int to = BitConverter.ToInt32(clauseData, (i + 1) * sizeof(Int32));
|
||||
|
||||
var range = new Range(from, to);
|
||||
bool thick = (range.From >= targetStart && range.To <= targetEnd);
|
||||
|
||||
underlines.Add(new CompositionUnderline(range, ColorUNDERLINE, ColorBKCOLOR, thick));
|
||||
}
|
||||
}
|
||||
|
||||
private static void GetCompositionSelectionRange(IntPtr hIMC, ref int targetStart, ref int targetEnd)
|
||||
{
|
||||
var attributeSize = ImeNative.ImmGetCompositionString(hIMC, ImeNative.GCS_COMPATTR, null, 0);
|
||||
if (attributeSize <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int start = 0;
|
||||
int end = 0;
|
||||
|
||||
// Buffer contains 8bit array
|
||||
var attributeData = new byte[attributeSize];
|
||||
ImeNative.ImmGetCompositionString(hIMC, ImeNative.GCS_COMPATTR, attributeData, attributeSize);
|
||||
|
||||
for (start = 0; start < attributeSize; ++start)
|
||||
{
|
||||
if (IsSelectionAttribute(attributeData[start]))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (end = start; end < attributeSize; ++end)
|
||||
{
|
||||
if (!IsSelectionAttribute(attributeData[end]))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
targetStart = start;
|
||||
targetEnd = end;
|
||||
}
|
||||
|
||||
private static bool IsSelectionAttribute(byte attribute)
|
||||
{
|
||||
return (attribute == ImeNative.ATTR_TARGET_CONVERTED || attribute == ImeNative.ATTR_TARGET_NOTCONVERTED);
|
||||
}
|
||||
|
||||
private static bool IsParam(uint lParam, uint type)
|
||||
{
|
||||
return (lParam & type) == type;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
// Copyright © 2018 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.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CefSharp.Wpf.Internals
|
||||
{
|
||||
public static class ImeNative
|
||||
{
|
||||
internal const uint GCS_RESULTSTR = 0x0800;
|
||||
internal const uint GCS_COMPSTR = 0x0008;
|
||||
internal const uint GCS_COMPATTR = 0x0010;
|
||||
internal const uint GCS_CURSORPOS = 0x0080;
|
||||
internal const uint GCS_COMPCLAUSE = 0x0020;
|
||||
|
||||
internal const uint CPS_CANCEL = 0x0004;
|
||||
internal const uint CS_NOMOVECARET = 0x4000;
|
||||
internal const uint NI_COMPOSITIONSTR = 0x0015;
|
||||
|
||||
internal const uint ATTR_TARGET_CONVERTED = 0x01;
|
||||
internal const uint ATTR_TARGET_NOTCONVERTED = 0x03;
|
||||
|
||||
internal const uint ISC_SHOWUICOMPOSITIONWINDOW = 0x80000000;
|
||||
|
||||
internal const uint CFS_DEFAULT = 0x0000;
|
||||
internal const uint CFS_RECT = 0x0001;
|
||||
internal const uint CFS_POINT = 0x0002;
|
||||
internal const uint CFS_FORCE_POSITION = 0x0020;
|
||||
internal const uint CFS_CANDIDATEPOS = 0x0040;
|
||||
internal const uint CFS_EXCLUDE = 0x0080;
|
||||
|
||||
internal const uint LANG_JAPANESE = 0x11;
|
||||
internal const uint LANG_CHINESE = 0x04;
|
||||
internal const uint LANG_KOREAN = 0x12;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct POINT
|
||||
{
|
||||
public int X;
|
||||
public int Y;
|
||||
|
||||
public POINT(int x, int y)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct RECT
|
||||
{
|
||||
public int Left, Top, Right, Bottom;
|
||||
|
||||
public RECT(int left, int top, int right, int bottom)
|
||||
{
|
||||
Left = left;
|
||||
Top = top;
|
||||
Right = right;
|
||||
Bottom = bottom;
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct COMPOSITIONFORM
|
||||
{
|
||||
public int dwStyle;
|
||||
public POINT ptCurrentPos;
|
||||
public RECT rcArea;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct CANDIDATEFORM
|
||||
{
|
||||
public int dwIndex;
|
||||
public int dwStyle;
|
||||
public POINT ptCurrentPos;
|
||||
public RECT rcArea;
|
||||
}
|
||||
|
||||
[DllImport("imm32.dll")]
|
||||
internal static extern IntPtr ImmCreateContext();
|
||||
|
||||
[DllImport("imm32.dll")]
|
||||
internal static extern IntPtr ImmAssociateContext(IntPtr hWnd, IntPtr hIMC);
|
||||
|
||||
[DllImport("imm32.dll")]
|
||||
internal static extern bool ImmDestroyContext(IntPtr hIMC);
|
||||
|
||||
[DllImport("imm32.dll")]
|
||||
internal static extern IntPtr ImmGetContext(IntPtr hWnd);
|
||||
|
||||
[DllImport("Imm32.dll")]
|
||||
internal static extern bool ImmReleaseContext(IntPtr hWnd, IntPtr hIMC);
|
||||
|
||||
[DllImport("Imm32.dll")]
|
||||
internal static extern bool ImmNotifyIME(IntPtr hIMC, uint action, uint index, uint value);
|
||||
|
||||
[DllImport("imm32.dll", CharSet = CharSet.Unicode)]
|
||||
internal static extern uint ImmGetCompositionString(IntPtr hIMC, uint dwIndex, byte[] lpBuf, uint dwBufLen);
|
||||
|
||||
[DllImport("imm32.dll")]
|
||||
internal static extern int ImmSetCompositionWindow(IntPtr hIMC, ref COMPOSITIONFORM lpCompForm);
|
||||
|
||||
[DllImport("imm32.dll")]
|
||||
public static extern int ImmSetCandidateWindow(IntPtr hIMC, [MarshalAs(UnmanagedType.Struct)] ref CANDIDATEFORM lpCandidateForm);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
internal static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
internal static extern bool DestroyCaret();
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
internal static extern bool SetCaretPos(int x, int y);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
internal static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
internal static extern IntPtr GetForegroundWindow();
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
internal static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
internal static extern IntPtr GetKeyboardLayout(uint idThread);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
internal static extern IntPtr SetFocus(IntPtr hWnd);
|
||||
}
|
||||
}
|
||||
@@ -54,5 +54,27 @@ namespace CefSharp.Wpf
|
||||
/// its WindowProc function.
|
||||
/// </summary>
|
||||
IME_CHAR = 0x0286,
|
||||
|
||||
/// <summary>
|
||||
/// Sent immediately before the IME generates the composition string as a result of a keystroke. A window receives this
|
||||
/// message through its WindowProc function.
|
||||
/// </summary>
|
||||
IME_STARTCOMPOSITION = 0x10D,
|
||||
|
||||
/// <summary>
|
||||
/// Sent to an application when the IME ends composition. A window receives this message through its WindowProc function.
|
||||
/// </summary>
|
||||
IME_ENDCOMPOSITION = 0x10E,
|
||||
|
||||
/// <summary>
|
||||
/// Sent to an application when the IME changes composition status as a result of a keystroke. A window receives this
|
||||
/// message through its WindowProc function.
|
||||
/// </summary>
|
||||
IME_COMPOSITION = 0x10F,
|
||||
|
||||
/// <summary>
|
||||
/// Sent to an application when a window is activated. A window receives this message through its WindowProc function.
|
||||
/// </summary>
|
||||
IME_SETCONTEXT = 0x281,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user