This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
forked from ryancheung/MonoGame.IMEHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IMMCompositionResultHandler.cs
107 lines (88 loc) · 2.61 KB
/
IMMCompositionResultHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#if WINDOWSDX || XNA
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.Generic;
namespace MonoGame.IMEHelper
{
internal abstract class IMMCompositionResultHandler
{
internal IntPtr IMEHandle { get; set; }
public int Flag { get; private set; }
internal IMMCompositionResultHandler(int flag)
{
this.Flag = flag;
this.IMEHandle = IntPtr.Zero;
}
internal virtual void Update() { }
internal bool Update(int lParam)
{
if ((lParam & Flag) == Flag)
{
Update();
return true;
}
return false;
}
}
internal class IMMCompositionString : IMMCompositionResultHandler, IEnumerable<byte>
{
private byte[] _values;
public int Length { get; private set; }
public byte[] Values { get { return _values; } }
public byte this[int index] { get { return _values[index]; } }
internal IMMCompositionString(int flag) : base(flag)
{
Clear();
}
public IEnumerator<byte> GetEnumerator()
{
foreach (byte b in _values)
yield return b;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public override string ToString()
{
if (Length <= 0) return String.Empty;
return Encoding.Unicode.GetString(_values, 0, Length);
}
internal void Clear()
{
_values = new byte[0];
Length = 0;
}
internal override void Update()
{
Length = IMM.ImmGetCompositionString(IMEHandle, Flag, IntPtr.Zero, 0);
IntPtr pointer = Marshal.AllocHGlobal(Length);
try
{
IMM.ImmGetCompositionString(IMEHandle, Flag, pointer, Length);
_values = new byte[Length];
Marshal.Copy(pointer, _values, 0, Length);
}
finally
{
Marshal.FreeHGlobal(pointer);
}
}
}
internal class IMMCompositionInt : IMMCompositionResultHandler
{
public int Value { get; private set; }
internal IMMCompositionInt(int flag) : base(flag) { }
public override string ToString()
{
return Value.ToString();
}
internal override void Update()
{
Value = IMM.ImmGetCompositionString(IMEHandle, Flag, IntPtr.Zero, 0);
}
}
}
#endif