Skip to content

Commit 1107f91

Browse files
ArcticLampyridlc-soft
authored andcommitted
fix(ime): composition window position problem (#36, #175)
1 parent 9be4b7e commit 1107f91

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

include/LCUI/ime.h

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ typedef struct LCUI_IMEHandlerRec_ {
3838
void (*totext)(int);
3939
LCUI_BOOL (*open)(void);
4040
LCUI_BOOL (*close)(void);
41+
void (*setcaret)(int, int);
4142
} LCUI_IMEHandlerRec, *LCUI_IMEHandler;
4243

4344
/** 注册一个输入法 */
@@ -72,6 +73,8 @@ int LCUI_RegisterWin32IME(void);
7273
int LCUI_RegisterLinuxIME(void);
7374
#endif
7475

76+
LCUI_API void LCUIIME_SetCaret(int x, int y);
77+
7578
LCUI_END_HEADER
7679

7780
#endif

src/gui/widget/textcaret.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <LCUI/gui/widget.h>
3535
#include <LCUI/gui/widget/textcaret.h>
3636
#include <LCUI/gui/css_parser.h>
37+
#include <LCUI/ime.h>
3738

3839
/** 文本插入符相关数据 */
3940
typedef struct LCUI_TextCaretRec_ {
@@ -59,11 +60,16 @@ textcaret {
5960

6061
void TextCaret_Refresh(LCUI_Widget widget)
6162
{
63+
float x, y;
64+
6265
LCUI_TextCaret caret = Widget_GetData(widget, prototype);
66+
6367
if (!caret->visible) {
6468
return;
6569
}
6670
LCUITimer_Reset(caret->timer_id, caret->blink_interval);
71+
Widget_GetOffset(widget, LCUIWidget_GetRoot(), &x, &y);
72+
LCUIIME_SetCaret((int)x, (int)y);
6773
Widget_Show(widget);
6874
}
6975

@@ -101,7 +107,7 @@ static void TextCaret_OnInit(LCUI_Widget widget)
101107
caret->blink_interval = 500;
102108
caret->visible = FALSE;
103109
caret->timer_id = LCUI_SetInterval(caret->blink_interval,
104-
TextCaret_OnBlink, widget);
110+
TextCaret_OnBlink, widget);
105111
}
106112

107113
void TextCaret_SetBlinkTime(LCUI_Widget widget, unsigned int n_ms)

src/ime.c

+7
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,13 @@ void LCUI_InitIME(void)
270270
#endif
271271
}
272272

273+
void LCUIIME_SetCaret(int x, int y)
274+
{
275+
if (self.ime->handler.setcaret) {
276+
self.ime->handler.setcaret(x, y);
277+
}
278+
}
279+
273280
void LCUI_FreeIME(void)
274281
{
275282
self.active = FALSE;

src/platform/linux/linux_ime.c

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ int LCUI_RegisterLinuxIME(void)
8888
handler.totext = X11IME_ToText;
8989
handler.close = X11IME_Close;
9090
handler.open = X11IME_Open;
91+
handler.setcaret = NULL;
9192
return LCUIIME_Register("LCUI X11 Input Method", &handler);
9293
}
9394
#endif

src/platform/windows/windows_ime.c

+17-1
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@
3232
#include <string.h>
3333
#include <stdlib.h>
3434
#include <LCUI_Build.h>
35-
3635
#ifdef LCUI_BUILD_IN_WIN32
3736
#include <LCUI/LCUI.h>
3837
#include <LCUI/input.h>
3938
#include <LCUI/ime.h>
4039
#include <LCUI/platform.h>
4140
#include LCUI_EVENTS_H
4241

42+
#pragma comment(lib, "Imm32.lib")
43+
4344
static LCUI_BOOL IME_ProcessKey(int key, int key_state)
4445
{
4546
return FALSE;
@@ -60,6 +61,20 @@ static void WinIME_OnChar(LCUI_Event e, void *arg)
6061
LCUIIME_Commit(text, 2);
6162
}
6263

64+
static void IME_SetCaret(int x, int y)
65+
{
66+
HWND hwnd = GetActiveWindow();
67+
HIMC himc = ImmGetContext(hwnd);
68+
if (himc) {
69+
COMPOSITIONFORM composition;
70+
composition.dwStyle = CFS_POINT;
71+
composition.ptCurrentPos.x = x;
72+
composition.ptCurrentPos.y = y;
73+
ImmSetCompositionWindow(himc, &composition);
74+
ImmReleaseContext(hwnd, himc);
75+
}
76+
}
77+
6378
static LCUI_BOOL IME_Open(void)
6479
{
6580
LCUI_BindSysEvent(WM_CHAR, WinIME_OnChar, NULL, NULL);
@@ -79,6 +94,7 @@ int LCUI_RegisterWin32IME(void)
7994
handler.totext = IME_ToText;
8095
handler.close = IME_Close;
8196
handler.open = IME_Open;
97+
handler.setcaret = IME_SetCaret;
8298
return LCUIIME_Register("LCUI Input Method", &handler);
8399
}
84100

0 commit comments

Comments
 (0)