Browse Source

improve IME support

pull/24/head
Siegfried Pammer 13 years ago
parent
commit
67b43f86f3
  1. 18
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/ImeNativeWrapper.cs
  2. 29
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/ImeSupport.cs
  3. 2
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/TextEditorOptions.cs

18
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/ImeNativeWrapper.cs

@ -16,6 +16,7 @@ using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Document; using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Rendering; using ICSharpCode.AvalonEdit.Rendering;
using ICSharpCode.AvalonEdit.Utils; using ICSharpCode.AvalonEdit.Utils;
using Draw = System.Drawing;
namespace ICSharpCode.AvalonEdit.Editing namespace ICSharpCode.AvalonEdit.Editing
{ {
@ -70,7 +71,9 @@ namespace ICSharpCode.AvalonEdit.Editing
const int CPS_CANCEL = 0x4; const int CPS_CANCEL = 0x4;
const int NI_COMPOSITIONSTR = 0x15; const int NI_COMPOSITIONSTR = 0x15;
const int GCS_COMPSTR = 0x0008; const int GCS_COMPSTR = 0x0008;
public const int WM_IME_COMPOSITION = 0x10F; public const int WM_IME_COMPOSITION = 0x10F;
public const int WM_INPUTLANGCHANGE = 0x51;
[DllImport("imm32.dll")] [DllImport("imm32.dll")]
static extern IntPtr ImmAssociateContext(IntPtr hWnd, IntPtr hIMC); static extern IntPtr ImmAssociateContext(IntPtr hWnd, IntPtr hIMC);
@ -85,6 +88,12 @@ namespace ICSharpCode.AvalonEdit.Editing
[DllImport("imm32.dll")] [DllImport("imm32.dll")]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
static extern bool ImmSetCompositionWindow(IntPtr hIMC, ref CompositionForm form); static extern bool ImmSetCompositionWindow(IntPtr hIMC, ref CompositionForm form);
[DllImport("imm32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ImmSetCompositionFont(IntPtr hIMC, ref LOGFONT font);
[DllImport("imm32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ImmGetCompositionFont(IntPtr hIMC, out LOGFONT font);
public static IntPtr AssociateContext(HwndSource source, IntPtr hIMC) public static IntPtr AssociateContext(HwndSource source, IntPtr hIMC)
{ {
@ -132,6 +141,15 @@ namespace ICSharpCode.AvalonEdit.Editing
return ImmSetCompositionWindow(hIMC, ref form); return ImmSetCompositionWindow(hIMC, ref form);
} }
public static bool SetCompositionFont(HwndSource source, IntPtr hIMC, TextArea textArea)
{
if (textArea == null)
throw new ArgumentNullException("textArea");
// LOGFONT font = new LOGFONT();
// ImmGetCompositionFont(hIMC, out font);
return false;
}
static Rect GetBounds(this TextView textView) static Rect GetBounds(this TextView textView)
{ {
Point location = textView.TranslatePoint(new Point(0,0), textView); Point location = textView.TranslatePoint(new Point(0,0), textView);

29
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/ImeSupport.cs

@ -34,9 +34,17 @@ namespace ICSharpCode.AvalonEdit.Editing
InputMethod.SetIsInputMethodSuspended(this.textArea, true); InputMethod.SetIsInputMethodSuspended(this.textArea, true);
textArea.GotKeyboardFocus += TextAreaGotKeyboardFocus; textArea.GotKeyboardFocus += TextAreaGotKeyboardFocus;
textArea.LostKeyboardFocus += TextAreaLostKeyboardFocus; textArea.LostKeyboardFocus += TextAreaLostKeyboardFocus;
textArea.OptionChanged += TextAreaOptionChanged;
currentContext = IntPtr.Zero; currentContext = IntPtr.Zero;
previousContext = IntPtr.Zero; previousContext = IntPtr.Zero;
} }
void TextAreaOptionChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "EnableImeSupport" && textArea.IsKeyboardFocusWithin) {
CreateContext();
}
}
public void Dispose() public void Dispose()
{ {
@ -66,10 +74,20 @@ namespace ICSharpCode.AvalonEdit.Editing
return; return;
if (e.OriginalSource != this.textArea) if (e.OriginalSource != this.textArea)
return; return;
CreateContext();
}
void CreateContext()
{
if (this.textArea == null)
return;
if (!textArea.Options.EnableImeSupport)
return;
hwndSource = (HwndSource)PresentationSource.FromVisual(this.textArea); hwndSource = (HwndSource)PresentationSource.FromVisual(this.textArea);
if (hwndSource != null) { if (hwndSource != null) {
currentContext = ImeNativeWrapper.GetContext(hwndSource); currentContext = ImeNativeWrapper.GetContext(hwndSource);
previousContext = ImeNativeWrapper.AssociateContext(hwndSource, currentContext); previousContext = ImeNativeWrapper.AssociateContext(hwndSource, currentContext);
// ImeNativeWrapper.SetCompositionFont(hwndSource, currentContext, textArea);
hwndSource.AddHook(WndProc); hwndSource.AddHook(WndProc);
} }
} }
@ -85,8 +103,15 @@ namespace ICSharpCode.AvalonEdit.Editing
IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{ {
if (msg == ImeNativeWrapper.WM_IME_COMPOSITION) switch (msg) {
UpdateCompositionWindow(); case ImeNativeWrapper.WM_INPUTLANGCHANGE:
ClearContext();
CreateContext();
break;
case ImeNativeWrapper.WM_IME_COMPOSITION:
UpdateCompositionWindow();
break;
}
return IntPtr.Zero; return IntPtr.Zero;
} }

2
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/TextEditorOptions.cs

@ -382,7 +382,7 @@ namespace ICSharpCode.AvalonEdit
} }
} }
bool enableImeSupport; bool enableImeSupport = true;
/// <summary> /// <summary>
/// Gets/Sets whether the support for Input Method Editors (IME) /// Gets/Sets whether the support for Input Method Editors (IME)

Loading…
Cancel
Save