Blender's GPU Accelerated UI, Decoupled from it's original codebase. Supports Rust, Python, JS, React, C & C++. Twice as good as GPUI
ime support
Preetham Pemmasanifbe814fee07d
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 86d69d7..aff4c5e 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -90,6 +90,21 @@ if(BUI_BUILD_TESTS)set_tests_properties(bui_gpu_start_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60FAIL_REGULAR_EXPRESSION "failed to create drawable")# themes: Blender's Dark / Light presets (BK_set_theme), dark stays pixel-identical+ # input methods: composition events drive the text fields (Blender's IME handling)+ add_executable(bui_ime_test tests/ime_test.c)+ target_link_libraries(bui_ime_test PRIVATE bui::kit)+ add_test(NAME bui_ime_test COMMAND bui_ime_test)+ # ... and the Windows host's port of GHOST_ImeWin32 against a real window+ add_executable(bui_ime_win32_test tests/ime_win32_test.c)+ target_link_libraries(bui_ime_win32_test PRIVATE bui::kit)+ add_test(NAME bui_ime_win32_test COMMAND bui_ime_win32_test)+ set_tests_properties(bui_ime_win32_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 30)+ # what the IME support costs (bench/ime_bench.c); BUI_BENCH_NO_IME builds it for a kit without it+ add_executable(bui_ime_bench bench/ime_bench.c)+ target_link_libraries(bui_ime_bench PRIVATE bui::kit)+ if(BUI_BENCH_NO_IME)+ target_compile_definitions(bui_ime_bench PRIVATE BENCH_NO_IME)+ endif()add_executable(bui_theme_test tests/theme_test.c)target_link_libraries(bui_theme_test PRIVATE bui::kit)add_test(NAME bui_theme_test COMMAND bui_theme_test)diff --git a/bench/ime_bench.c b/bench/ime_bench.cnew file mode 100644index 0000000..cfa9658--- /dev/null+++ b/bench/ime_bench.c@@ -0,0 +1,126 @@+/* What the IME support costs the kit: a keystroke plus its repaint in a text field, an idle+ * frame, the host's per-frame BK_ime_state poll, and a frame while an IME composes.+ *+ * Build against the kit (CMake: target bui::kit), run, compare with a build without the IME+ * code (define BENCH_NO_IME to leave out the IME calls). Each figure is the median of 5 runs.+ *+ * cl /O2 /DBENCH_NO_IME bench\ime_bench.c ... (against an older kit) or without the define.+ *+ * SPDX-License-Identifier: GPL-2.0-or-later */++#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "BUI_kit.h"+#include "GPU_raster.h"++#ifdef _WIN32+# define WIN32_LEAN_AND_MEAN+# include <windows.h>+static double now_ns(void)+{+ LARGE_INTEGER f, c;+ QueryPerformanceFrequency(&f);+ QueryPerformanceCounter(&c);+ return (double)c.QuadPart * 1e9 / (double)f.QuadPart;+}+#else+# include <time.h>+static double now_ns(void)+{+ struct timespec t;+ clock_gettime(CLOCK_MONOTONIC, &t);+ return (double)t.tv_sec * 1e9 + (double)t.tv_nsec;+}+#endif++enum { W = 480, H = 240, RUNS = 5 };++static BKContext *g_ctx;+static BuiRaster *g_raster;++static int cmp(const void *a, const void *b)+{+ const double x = *(const double *)a, y = *(const double *)b;+ return (x > y) - (x < y);+}++static double median(double *v, int n)+{+ qsort(v, (size_t)n, sizeof(double), cmp);+ return v[n / 2];+}++#define BENCH(label, iters, body) \+ do { \+ double t[RUNS]; \+ for (int run = 0; run < RUNS; run++) { \+ const double t0 = now_ns(); \+ for (int i = 0; i < (iters); i++) { \+ body; \+ } \+ t[run] = (now_ns() - t0) / (iters); \+ } \+ printf("%-46s %10.1f ns\n", label, median(t, RUNS)); \+ } while (0)++#ifndef BENCH_NO_IME+/* Japanese hiragana, 10 and 11 characters (30 and 33 bytes), alternating */+static void compose_step(int tick)+{+ static const char *forms[2] = {+ "\xE3\x81\xAB\xE3\x81\xBB\xE3\x82\x93\xE3\x81\x94\xE3\x81\xAB\xE3\x81\xBB\xE3\x82\x93\xE3\x81\x94\xE3\x81\xAB\xE3\x81\xBB",+ "\xE3\x81\xAB\xE3\x81\xBB\xE3\x82\x93\xE3\x81\x94\xE3\x81\xAB\xE3\x81\xBB\xE3\x82\x93\xE3\x81\x94\xE3\x81\xAB\xE3\x81\xBB\xE3\x82\x93"};+ const BKImeEvent e = {"", forms[tick & 1], -1, 0, 6};+ BK_ime_event(g_ctx, BK_IME_COMPOSITION, &e);+ BK_render(g_ctx, g_raster, W, H, 0);+}+#endif++int main(void)+{+ BKNode *col = BK_node_new("column");+ BKNode *text = BK_node_new("text");+ BK_set_str(text, "value", "The quick brown fox");+ BK_node_add(col, text, -1);+ for (int i = 0; i < 6; i++) {+ BKNode *b = BK_node_new("button");+ BK_set_str(b, "text", "Button");+ BK_node_add(col, b, -1);+ }+ g_ctx = BK_context_new(col);+ g_raster = BUI_raster_create(W, H);+ BK_render(g_ctx, g_raster, W, H, 1);++ int r[4];+ BK_node_rect(text, r);+ BK_press(g_ctx, r[0] + r[2] - 4, r[1] + r[3] / 2, 0);+ BK_release(g_ctx, r[0] + r[2] - 4, r[1] + r[3] / 2, 0);+ BK_render(g_ctx, g_raster, W, H, 1);++ /* a keystroke in the focused text field and the repaint it causes */+ BENCH("keystroke + repaint (editing a text field)", 4000, {+ BK_key(g_ctx, "a", "a", 0);+ BK_key(g_ctx, "", "BackSpace", 0);+ BK_render(g_ctx, g_raster, W, H, 0);+ });+ /* nothing changed: the idle frame */+ BENCH("idle frame (nothing damaged)", 200000, BK_render(g_ctx, g_raster, W, H, 0));+ /* a full repaint of the window */+ BENCH("full repaint, editing", 2000, BK_render(g_ctx, g_raster, W, H, 1));++#ifndef BENCH_NO_IME+ BKImeState s;+ BENCH("host poll: BK_ime_state (unchanged)", 2000000, BK_ime_state(g_ctx, &s));++ BKImeEvent start = {"", "", -1, -1, -1};+ BK_ime_event(g_ctx, BK_IME_COMPOSITION_START, &start);+ int tick = 0;+ BENCH("composition update + repaint (10-11 chars)", 4000, compose_step(tick++));+ BK_ime_event(g_ctx, BK_IME_COMPOSITION_END, &start);+#endif+ BUI_raster_free(g_raster);+ BK_context_free(g_ctx);+ return 0;+}diff --git a/bindings/rust/blenderui-sys/build.rs b/bindings/rust/blenderui-sys/build.rsindex 60ba768..a9ba46e 100644--- a/bindings/rust/blenderui-sys/build.rs+++ b/bindings/rust/blenderui-sys/build.rs@@ -89,7 +89,7 @@ fn main() {println!("cargo:rustc-link-lib={}", if target == "macos" { "c++" } else { "stdc++" });}if target == "windows" {- for lib in ["user32", "gdi32"] {+ for lib in ["user32", "gdi32", "imm32"] {println!("cargo:rustc-link-lib={lib}");}} else {diff --git a/kit/CMakeLists.txt b/kit/CMakeLists.txtindex 265902e..38c6c0d 100644--- a/kit/CMakeLists.txt+++ b/kit/CMakeLists.txt@@ -15,6 +15,8 @@ add_library(bui_kit STATICintern/kit_color.cintern/kit_frame.cintern/kit_textarea.c+ intern/kit_ime.c+ intern/kit_ime_win32.cintern/kit_window_win32.cintern/kit_window_x11.cintern/kit_window_null.c@@ -130,7 +132,7 @@ if(NOT MSVC)target_link_libraries(bui_kit PRIVATE m)endif()if(WIN32)- target_link_libraries(bui_kit PRIVATE user32 gdi32)+ target_link_libraries(bui_kit PRIVATE user32 gdi32 imm32)elseif(APPLE)# Native windows on macOS: an AppKit host after Blender's GHOST Cocoa backend.# Objective-C (enable_language(OBJC) is in the top-level CMakeLists.txt) withdiff --git a/kit/include/BUI_kit.h b/kit/include/BUI_kit.hindex 1776309..1e6e215 100644--- a/kit/include/BUI_kit.h+++ b/kit/include/BUI_kit.h@@ -589,6 +589,34 @@ int BK_wheel(BKContext *ctx, int x, int y, int delta, int mods);int BK_scroll(BKContext *ctx, int x, int y, float dx, float dy, int mods);int BK_key(BKContext *ctx, const char *text, const char *keysym, int mods);int BK_context_menu(BKContext *ctx, int x, int y, int mods);++/* -- Input methods (IME) ------------------------------------------------------+ * Blender's model (GHOST_ImeWin32, wmIMEData, the text button's IME handling), one API for every+ * window host and binding. The IME is off until a text field or text area is being edited and+ * on only while it is, so shortcuts keep working with a CJK keyboard.+ *+ * Host side: forward the platform's composition events to BK_ime_event, then after each render+ * ask BK_ime_state whether the IME must be (re)started or moved, and tell the platform.+ * The kit draws the composition (underlined, at the caret) and inserts the result. */+typedef struct BKImeEvent {+ const char *result; /* text the IME committed (UTF-8), "" = none */+ const char *composite; /* text still being composed, "" = none */+ int cursor_pos; /* the IME's caret as a byte offset in composite, -1 = none */+ int sel_start, sel_end; /* the clause being converted, bytes in composite, -1 = none */+} BKImeEvent;+enum { BK_IME_COMPOSITION_START = 0, BK_IME_COMPOSITION, BK_IME_COMPOSITION_END };+int BK_ime_event(BKContext *ctx, int type, const BKImeEvent *e);++typedef struct BKImeState {+ int enabled; /* a field takes text: the IME should be on */+ int restart; /* another field began being edited: end any composition, then begin */+ int x, y; /* candidate window position in window pixels (top left origin) */+} BKImeState;+/* Fills `state`; returns non-zero when enabled, restart or the position changed since the last+ * call, which is the only time the host has anything to tell the platform. */+int BK_ime_state(BKContext *ctx, BKImeState *state);+/* Non-zero while a composition is in progress (a host should not treat keys as shortcuts). */+int BK_ime_composing(BKContext *ctx);/* Raw-only input for host bindings (Tk's <KeyRelease>, <Button-2>, <ButtonRelease-3>):* forwarded to "events" nodes, no widget behavior. */int BK_key_release(BKContext *ctx, const char *keysym, int mods);diff --git a/kit/intern/kit_core.c b/kit/intern/kit_core.cindex 1dbe6a5..bc3f09a 100644--- a/kit/intern/kit_core.c+++ b/kit/intern/kit_core.c@@ -677,6 +677,7 @@ void bk_begin_edit(BKNode *n, const char *text, bool select_all){BKContext *ctx = bk_ctx(n);if (ctx && ctx->focus && ctx->focus != n) {+ bk_ime_commit(ctx); /* another field takes over: the composition stays in this one */BKNode *f = ctx->focus;ctx->focus = NULL;if (f->cls->finish_edit) {@@ -695,6 +696,9 @@ void bk_begin_edit(BKNode *n, const char *text, bool select_all)static void end_edit(BKContext *ctx, bool confirm){+ if (confirm) {+ bk_ime_commit(ctx); /* Blender: a click outside inserts the text being composed */+ }BKNode *f = ctx->focus;ctx->focus = NULL;if (f) {@@ -2765,6 +2769,9 @@ static int undo_key(BKContext *ctx, const char *keysym, int mods)static int in_BK_key(BKContext *ctx, const char *text, const char *keysym, int mods){hide_tooltip(ctx);+ if (bk_ime_swallow_key(ctx, text, keysym)) {+ return 1; /* typed text and Escape belong to the IME while it composes */+ }BKEvent e = make_event(ctx, NULL, ctx->pointer_x, ctx->pointer_y, mods);e.text = text ? text : "";e.keysym = keysym ? keysym : "";@@ -2991,6 +2998,7 @@ void bk_te_free(BKTextEdit *te){if (te) {bk_textedit_undo_stack_destroy(te->undo);+ bk_ime_comp_free(&te->ime);free(te->text);free(te);}@@ -3014,7 +3022,7 @@ static void te_delete(BKTextEdit *te, int a, int b)te->anchor = -1;}-static bool te_delete_selection(BKTextEdit *te)+bool bk_te_delete_selection(BKTextEdit *te){int a, b;if (!te_selection(te, &a, &b)) {@@ -3023,6 +3031,7 @@ static bool te_delete_selection(BKTextEdit *te)te_delete(te, a, b);return true;}+#define te_delete_selection bk_te_delete_selectionstatic void te_insert(BKTextEdit *te, const char *s){@@ -3239,7 +3248,11 @@ void bk_te_draw(BKTextEdit *te, const BKRect *rect, const uchar color[4]){const int p = bk_pad();const float inner = BK_MAX(1.0f, (float)(bk_rw(rect) - 2 * p));- const float caret_px = bk_masked_width(te->text, te->caret, te->mask, false);+ const BKImeComp *ime = &te->ime;+ /* an IME composition sits at the caret and the caret moves into it (widget_draw_text) */+ const float comp_w = ime->len ? bk_text_width(ime->text, ime->len, false) : 0.0f;+ const float text_px = bk_masked_width(te->text, te->caret, te->mask, false);+ const float caret_px = text_px + bk_ime_caret_offset(ime, false);if (caret_px - te->scroll > inner) {te->scroll = caret_px - inner;}@@ -3257,7 +3270,15 @@ void bk_te_draw(BKTextEdit *te, const BKRect *rect, const uchar color[4])const float m = 0.15f * h;bk_fill(sx, rect->ymin + m, sw, h - 2 * m, bk_theme.selection);}- if (te->mask[0]) {+ if (ime->len) { /* text before the caret, the composite, the text after it */+ const float cx = tx + text_px;+ bk_text(tx, bk_text_baseline(rect), te->text, te->caret, color, false);+ bk_text(cx, bk_text_baseline(rect), ime->text, ime->len, color, false);+ bk_text(cx + comp_w, bk_text_baseline(rect), te->text + te->caret, te->len - te->caret, color,+ false);+ bk_ime_draw_underline(ime, cx, (float)rect->ymax, (float)bk_rw(rect), color, false);+ }+ else if (te->mask[0]) {char buf[1024];bk_text(tx, bk_text_baseline(rect), buf, bk_mask_text(te->text, te->len, te->mask, buf,sizeof(buf)),@@ -3270,6 +3291,7 @@ void bk_te_draw(BKTextEdit *te, const BKRect *rect, const uchar color[4])bk_fill(floorf(tx + caret_px), rect->ymin + m, BK_MAX(1.0f, floorf(0.1f * bk_unit() + 0.5f)),h - 2 * m, bk_theme.cursor);bk_clip_pop();+ bk_ime_set_candidate(&te->ime, (int)floorf(tx + caret_px) + 5, rect->ymax + 1);}/** \} */diff --git a/kit/intern/kit_ime.c b/kit/intern/kit_ime.cnew file mode 100644index 0000000..f2567af--- /dev/null+++ b/kit/intern/kit_ime.c@@ -0,0 +1,208 @@+/* Input methods (IME), the platform independent half.+ *+ * Blender: the text button's IME handling (interface_handlers.cc textedit_ime_*, WM_IME_COMPOSITE_*),+ * its drawing (interface_widgets.cc widget_draw_text_ime_underline) and the window manager's+ * IME owner (wm_window.cc WM_window_IME_*). A window host (kit_ime_win32.c) turns the platform's+ * composition messages into BK_ime_event calls and asks BK_ime_state what to tell the platform.+ *+ * Nothing here runs unless a host calls BK_ime_event / BK_ime_state, so hosts without an IME+ * (X11, Cocoa for now, headless) are unaffected.+ *+ * SPDX-License-Identifier: GPL-2.0-or-later */++#include <math.h>+#include <stdlib.h>+#include <string.h>++#include "kit_intern.h"++/* -------------------------------------------------------------------- */+/** \name Composition state shared by every editable node+ * \{ */++void bk_ime_comp_free(BKImeComp *c)+{+ free(c->text);+ memset(c, 0, sizeof(*c));+}++void bk_ime_comp_set(BKImeComp *c, const BKImeEvent *e)+{+ const char *s = (e && e->composite) ? e->composite : "";+ const int n = (int)strlen(s);+ if (n + 1 > c->cap) { /* grown, never shrunk: a composition does not allocate per event */+ c->cap = n + 64;+ c->text = (char *)realloc(c->text, (size_t)c->cap);+ }+ if (c->cap) {+ memcpy(c->text, s, (size_t)n + 1);+ }+ c->len = n;+ c->cursor = (n && e) ? e->cursor_pos : -1;+ c->sel_start = (n && e) ? e->sel_start : -1;+ c->sel_end = (n && e) ? e->sel_end : -1;+}++float bk_ime_caret_offset(const BKImeComp *c, bool bold)+{+ /* Blender: the cursor moves by `cursor_pos` into the inserted composite */+ if (c->len == 0 || c->cursor < 0) {+ return 0.0f;+ }+ return bk_text_width(c->text, BK_MIN(c->cursor, c->len), bold);+}++void bk_ime_draw_underline(const BKImeComp *c, float x, float bottom, float clip_w,+ const uchar color[4], bool bold)+{+ if (c->len == 0) {+ return;+ }+ const float y = bottom - 6.0f; /* rect->ymin + 6 * U.pixelsize, measured from the top here */+ const float w = BK_MIN(bk_text_width(c->text, c->len, bold), clip_w - 2.0f);+ bk_fill(floorf(x), y - 1.0f, w, 1.0f, color);+ if (c->sel_start >= 0 && c->sel_end > c->sel_start && c->sel_end <= c->len) {+ /* the thick line: the clause being converted */+ const float sx = x + bk_text_width(c->text, c->sel_start, bold);+ const float sw = bk_text_width(c->text + c->sel_start, c->sel_end - c->sel_start, bold);+ bk_fill(floorf(sx), y - 2.0f, BK_MIN(sw, clip_w - 2.0f), 2.0f, color);+ }+}++void bk_ime_set_candidate(BKImeComp *c, int x, int y)+{+ c->cand_x = x;+ c->cand_y = y;+ c->cand_valid = true;+}++/** \} */++/* -------------------------------------------------------------------- */+/** \name The node being edited+ * \{ */++BKImeComp *bk_ime_comp(BKNode *n)+{+ if (!n || !NODE_ENABLED(n)) {+ return NULL;+ }+ if (n->edit) {+ /* Blender's textedit_ime_begin skips number buttons; a password must not show the+ * composition in clear, and a hotkey field takes key codes, not text. */+ if (n->edit->mask[0] || n->cls == &bk_cls_number || n->cls == &bk_cls_slider ||+ n->cls == &bk_cls_hotkey)+ {+ return NULL;+ }+ return &n->edit->ime;+ }+ if (n->cls == &bk_cls_textarea) {+ return bk_textarea_ime(n);+ }+ return NULL;+}++/** \} */++/* -------------------------------------------------------------------- */+/** \name Events from the window host (WM_IME_COMPOSITE_*)+ * \{ */++int BK_ime_event(BKContext *ctx, int type, const BKImeEvent *e)+{+ BKNode *f = ctx->focus;+ BKImeComp *c = bk_ime_comp(f);+ if (!c) {+ return 0;+ }+ static const BKImeEvent none = {"", "", -1, -1, -1};+ switch (type) {+ case BK_IME_COMPOSITION_START:+ c->composing = true;+ bk_ime_comp_set(c, &none);+ /* the composition replaces the selection (textedit_delete_selection) */+ if (f->edit) {+ bk_te_delete_selection(f->edit);+ }+ else {+ bk_textarea_delete_selection(f);+ }+ break;+ case BK_IME_COMPOSITION:+ bk_ime_comp_set(c, e);+ if (e && e->result && e->result[0]) {+ /* committed text goes in as typed text: the node's own key path inserts it, undoes it+ * and reports a live change like every other character */+ const bool was = c->composing;+ c->composing = false;+ BK_key(ctx, e->result, e->result, 0);+ c->composing = was;+ }+ break;+ case BK_IME_COMPOSITION_END:+ c->composing = false;+ bk_ime_comp_set(c, &none);+ break;+ default:+ return 0;+ }+ bk_invalidate(f);+ return 1;+}++void bk_ime_commit(BKContext *ctx)+{+ BKNode *f = ctx->focus;+ BKImeComp *c = bk_ime_comp(f);+ if (c && c->composing && c->len > 0) {+ char *s = (char *)malloc((size_t)c->len + 1);+ memcpy(s, c->text, (size_t)c->len + 1);+ c->composing = false;+ bk_ime_comp_set(c, &(BKImeEvent){"", "", -1, -1, -1});+ BK_key(ctx, s, s, 0);+ free(s);+ }+}++bool bk_ime_swallow_key(BKContext *ctx, const char *text, const char *keysym)+{+ const BKImeComp *c = bk_ime_comp(ctx->focus);+ return c && c->composing && ((text && text[0]) || bk_keysym_is(keysym, "Escape"));+}++int BK_ime_composing(BKContext *ctx)+{+ const BKImeComp *c = bk_ime_comp(ctx->focus);+ return c && c->composing;+}++/** \} */++/* -------------------------------------------------------------------- */+/** \name What the host tells the platform+ * \{ */++int BK_ime_state(BKContext *ctx, BKImeState *state)+{+ BKNode *f = ctx->focus;+ const BKImeComp *c = bk_ime_comp(f);+ BKImeState s = {0, 0, 0, 0};+ if (c) {+ s.enabled = 1;+ s.restart = f != ctx->ime.node;+ /* before the first draw of a field: just below it */+ s.x = c->cand_valid ? c->cand_x : f->x;+ s.y = c->cand_valid ? c->cand_y : f->y + f->h + 1;+ }+ const int changed = s.enabled != ctx->ime.enabled || s.restart || s.x != ctx->ime.x ||+ s.y != ctx->ime.y;+ ctx->ime.node = c ? f : NULL;+ ctx->ime.enabled = s.enabled;+ ctx->ime.x = s.x;+ ctx->ime.y = s.y;+ *state = s;+ return changed;+}++/** \} */diff --git a/kit/intern/kit_ime_win32.c b/kit/intern/kit_ime_win32.cnew file mode 100644index 0000000..46a6d71--- /dev/null+++ b/kit/intern/kit_ime_win32.c@@ -0,0 +1,472 @@+/* Input methods on Win32: a C port of Blender's GHOST_ImeWin32 (intern/ghost/intern/+ * GHOST_ImeWin32.cc, which is Chromium's ImeInput). The function names follow GHOST's methods;+ * see kit_ime_win32.h for how the window procedure uses them.+ *+ * SPDX-FileCopyrightText: 2010 The Chromium Authors. All rights reserved.+ * SPDX-License-Identifier: GPL-2.0-or-later */++#ifdef _WIN32++# include <stdlib.h>+# include <string.h>++# include <windows.h>++# include <imm.h>++# include "kit_ime_win32.h"++# ifdef _MSC_VER+# pragma comment(lib, "imm32")+# endif++/* ISO_639-1 2-Letter Abbreviations. */+# define IMELANG_ENGLISH "en"+# define IMELANG_CHINESE "zh"+# define IMELANG_JAPANESE "ja"+# define IMELANG_KOREAN "ko"++void bk_ime_win32_init(BKImeWin32 *ime)+{+ memset(ime, 0, sizeof(*ime));+ strcpy(ime->language, IMELANG_ENGLISH);+ ime->conversion_modes = IME_CMODE_ALPHANUMERIC;+ ime->sentence_mode = IME_SMODE_NONE;+ ime->caret_l = ime->caret_t = -1;+ ime->is_first = true;+ ime->is_enable = true;+ ime->cursor_pos = ime->target_start = ime->target_end = -1;+}++void bk_ime_win32_free(BKImeWin32 *ime)+{+ free(ime->result.string);+ free(ime->comp.string);+ free(ime->result_utf8);+ free(ime->composite_utf8);+ free(ime->clauses);+ memset(ime, 0, sizeof(*ime));+}++static bool is_language(const BKImeWin32 *ime, const char *name)+{+ return strcmp(name, ime->language) == 0;+}++void bk_ime_win32_update_input_language(BKImeWin32 *ime)+{+ /* Get the current input locale full name. */+ WCHAR locale[LOCALE_NAME_MAX_LENGTH];+ LCIDToLocaleName(MAKELCID(LOWORD(GetKeyboardLayout(0)), SORT_DEFAULT), locale,+ LOCALE_NAME_MAX_LENGTH, 0);+ /* Get the 2-letter ISO-63901 abbreviation of the input locale name. */+ WCHAR language_u16[BK_IME_ISO639_LEN];+ GetLocaleInfoEx(locale, LOCALE_SISO639LANGNAME, language_u16, BK_IME_ISO639_LEN);+ /* Store this as a UTF8 string. */+ WideCharToMultiByte(CP_UTF8, 0, language_u16, BK_IME_ISO639_LEN, ime->language,+ BK_IME_ISO639_LEN, NULL, NULL);+ ime->language[BK_IME_ISO639_LEN - 1] = '\0';+}++void bk_ime_win32_update_conversion_status(BKImeWin32 *ime, HWND hwnd)+{+ HIMC imm_context = ImmGetContext(hwnd);+ if (imm_context) {+ if (ImmGetOpenStatus(imm_context)) {+ ImmGetConversionStatus(imm_context, &ime->conversion_modes, &ime->sentence_mode);+ }+ else {+ ime->conversion_modes = IME_CMODE_ALPHANUMERIC;+ ime->sentence_mode = IME_SMODE_NONE;+ }+ ImmReleaseContext(hwnd, imm_context);+ }+ else {+ ime->conversion_modes = IME_CMODE_ALPHANUMERIC;+ ime->sentence_mode = IME_SMODE_NONE;+ }+}++bool bk_ime_win32_is_english_mode(const BKImeWin32 *ime)+{+ return (ime->conversion_modes & IME_CMODE_NOCONVERSION) ||+ !(ime->conversion_modes & (IME_CMODE_NATIVE | IME_CMODE_FULLSHAPE));+}++bool bk_ime_win32_is_ime_key_event(const BKImeWin32 *ime, char ascii, bool numpad_period)+{+ if (!bk_ime_win32_is_english_mode(ime)) {+ /* In Chinese, Japanese, Korean, all alpha keys are processed by IME. */+ if ((ascii >= 'A' && ascii <= 'Z') || (ascii >= 'a' && ascii <= 'z')) {+ return true;+ }+ if (is_language(ime, IMELANG_JAPANESE) && (ascii >= ' ' && ascii <= '~')) {+ return true;+ }+ if (is_language(ime, IMELANG_CHINESE)) {+ if (ascii && strchr("!\"$'(),.:;<>?[\\]^_`/", ascii) && !numpad_period) {+ return true;+ }+ if ((ime->conversion_modes & IME_CMODE_FULLSHAPE) && (ascii >= '0' && ascii <= '9')) {+ /* When in Full Width mode the number keys are also converted. */+ return true;+ }+ }+ }+ return false;+}++static void move_window(BKImeWin32 *ime, HIMC imm_context)+{+ int x = ime->caret_l;+ int y = ime->caret_t;+ const int caret_margin = 1;+ /* Chinese IMEs ignore ImmSetCandidateWindow when TSF and CUAS are disabled and read the+ * system caret instead; with them enabled they ignore the caret and use CFS_CANDIDATEPOS.+ * So set both. */+ CANDIDATEFORM candidate_position = {0, CFS_CANDIDATEPOS, {x, y}, {0, 0, 0, 0}};+ ImmSetCandidateWindow(imm_context, &candidate_position);+ if (ime->system_caret) {+ SetCaretPos(x, y);+ }+ if (is_language(ime, IMELANG_KOREAN)) {+ /* Chinese and Japanese IMEs want the upper-left corner of the caret to move their candidate+ * windows, Korean IMEs the lower-left one. */+ y += caret_margin;+ }+ /* Japanese and Korean IMEs also use CFS_EXCLUDE to place their candidate windows when TSF and+ * CUAS are disabled. */+ CANDIDATEFORM exclude_rectangle = {+ 0, CFS_EXCLUDE, {x, y},+ {x, y, x + (ime->caret_r - ime->caret_l), y + (ime->caret_b - ime->caret_t)}};+ ImmSetCandidateWindow(imm_context, &exclude_rectangle);+}++void bk_ime_win32_update_window(BKImeWin32 *ime, HWND hwnd)+{+ /* Just move the IME window attached to the given window. */+ if (ime->caret_l >= 0 && ime->caret_t >= 0) {+ HIMC imm_context = ImmGetContext(hwnd);+ if (imm_context) {+ move_window(ime, imm_context);+ ImmReleaseContext(hwnd, imm_context);+ }+ }+}++void bk_ime_win32_create_window(BKImeWin32 *ime, HWND hwnd)+{+ /* With TSF and CUAS disabled, Chinese IMEs ignore ImmSetCandidateWindow and use ::GetCaretPos,+ * and some Japanese IMEs do too: create a temporary system caret for this input context. */+ if (!ime->system_caret && (is_language(ime, IMELANG_CHINESE) || is_language(ime, IMELANG_JAPANESE))) {+ ime->system_caret = CreateCaret(hwnd, NULL, 1, 1) != 0;+ }+ /* Restore the positions of the IME windows. */+ bk_ime_win32_update_window(ime, hwnd);+}++void bk_ime_win32_destroy_window(BKImeWin32 *ime, HWND hwnd)+{+ (void)hwnd;+ /* Destroy the system caret if we have created for this IME input context. */+ if (ime->system_caret) {+ DestroyCaret();+ ime->system_caret = false;+ }+}++void bk_ime_win32_reset_composition(BKImeWin32 *ime)+{+ /* Currently, just reset the composition status. */+ ime->is_composing = false;+}++void bk_ime_win32_cleanup_composition(BKImeWin32 *ime, HWND hwnd)+{+ /* Complete the ongoing composition (the window was de-activated while composing and is+ * re-activated), and reset the composition status. */+ if (ime->is_composing) {+ HIMC imm_context = ImmGetContext(hwnd);+ if (imm_context) {+ ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);+ ImmReleaseContext(hwnd, imm_context);+ }+ bk_ime_win32_reset_composition(ime);+ }+}++static void complete_composition(BKImeWin32 *ime, HIMC imm_context)+{+ /* Only complete a composition that exists: some IMEs get confused otherwise. */+ if (ime->is_composing) {+ ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);+ bk_ime_win32_reset_composition(ime);+ }+}++void bk_ime_win32_end(BKImeWin32 *ime, HWND hwnd)+{+ /* The input focus moved to something that takes no IME (or another field) while composing:+ * cancel the composition and clean up BEFORE disabling the IME. */+ if (!ime->is_enable) {+ return;+ }+ ime->is_enable = false;+ if (ime->is_composing) {+ HIMC imm_context = ImmGetContext(hwnd);+ if (imm_context) {+ /* Cancel composition */+ ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_CANCEL, 0);+ ImmNotifyIME(imm_context, NI_CLOSECANDIDATE, 0, 0);+ ImmReleaseContext(hwnd, imm_context);+ }+ bk_ime_win32_reset_composition(ime);+ }+ ImmAssociateContextEx(hwnd, NULL, 0);+ if (ime->composite_utf8) {+ ime->composite_utf8[0] = '\0';+ }+}++void bk_ime_win32_check_first(BKImeWin32 *ime, HWND hwnd)+{+ /* The IME starts disabled: it is on only while a field takes text. */+ if (ime->is_first) {+ bk_ime_win32_end(ime, hwnd);+ ime->is_first = false;+ }+}++void bk_ime_win32_begin(BKImeWin32 *ime, HWND hwnd, int l, int t, int r, int b, bool complete)+{+ if (ime->is_enable && complete) {+ return;+ }+ ime->is_enable = true;+ /* Load the default IME context. IMM ignores this call if the context is loaded. */+ ImmAssociateContextEx(hwnd, NULL, IACE_DEFAULT);+ /* Complete the ongoing composition and move the IME windows. */+ HIMC imm_context = ImmGetContext(hwnd);+ if (imm_context) {+ if (complete) {+ /* The focus moved to another edit control while composing: complete the composition and+ * hide the IME windows BEFORE MOVING THEM. */+ complete_composition(ime, imm_context);+ }+ /* Save the caret position, and update the position of the IME window. */+ if (l >= 0 && t >= 0) {+ ime->caret_l = l;+ ime->caret_t = t;+ ime->caret_r = r;+ ime->caret_b = b;+ move_window(ime, imm_context);+ }+ ImmReleaseContext(hwnd, imm_context);+ }+}++/* -------------------------------------------------------------------- */+/* Reading the strings */++static bool is_target_attribute(char attribute)+{+ return attribute == ATTR_TARGET_CONVERTED || attribute == ATTR_TARGET_NOTCONVERTED;+}++static void *grow(void *p, int *cap, int need, size_t elem)+{+ if (need > *cap) {+ *cap = need + 64;+ p = realloc(p, (size_t)*cap * elem);+ }+ return p;+}++/* Retrieve a string from the IMM. */+static bool get_string(HIMC imm_context, LPARAM lparam, int type, BKImeComposition *c)+{+ c->length = 0;+ if (lparam & type) {+ const int string_size = ImmGetCompositionStringW(imm_context, type, NULL, 0);+ if (string_size > 0) {+ const int string_length = string_size / (int)sizeof(wchar_t);+ c->string = (wchar_t *)grow(c->string, &c->cap, string_length + 1, sizeof(wchar_t));+ ImmGetCompositionStringW(imm_context, type, c->string, (DWORD)string_size);+ c->string[string_length] = L'\0';+ c->length = string_length;+ return true;+ }+ }+ return false;+}++/* The target area: language dependent, as the caret's style is. */+static void get_caret(BKImeWin32 *ime, HIMC imm_context, LPARAM lparam, BKImeComposition *c)+{+ int target_start = -1;+ int target_end = -1;+ if (is_language(ime, IMELANG_KOREAN)) {+ /* Korean IMEs: the caret is a blinking block (one hangul character). */+ if (lparam & CS_NOMOVECARET) {+ target_start = 0;+ target_end = 1;+ }+ }+ else if (is_language(ime, IMELANG_CHINESE)) {+ /* Chinese IMEs: the caret is a blinking line, no target selection needed... */+ int clause_size = ImmGetCompositionStringW(imm_context, GCS_COMPCLAUSE, NULL, 0);+ if (clause_size) {+ clause_size = clause_size / (int)sizeof(DWORD);+ ime->clauses = (DWORD *)grow(ime->clauses, &ime->clauses_cap, clause_size, sizeof(DWORD));+ ImmGetCompositionStringW(imm_context, GCS_COMPCLAUSE, ime->clauses,+ (DWORD)(sizeof(DWORD) * (size_t)clause_size));+ if (clause_size >= 2 && c->cursor_position == c->length) {+ target_start = (int)ime->clauses[clause_size - 2];+ target_end = (int)ime->clauses[clause_size - 1];+ }+ else {+ for (int i = 0; i < clause_size - 1; i++) {+ if ((int)ime->clauses[i] == c->cursor_position) {+ target_start = (int)ime->clauses[i];+ target_end = (int)ime->clauses[i + 1];+ break;+ }+ }+ }+ }+ else if (c->cursor_position != -1) {+ target_start = c->cursor_position;+ target_end = c->length;+ }+ }+ else if (is_language(ime, IMELANG_JAPANESE)) {+ /* Japanese IMEs: the caret is a selection (or underlined) block. The robustest way to find+ * it is scanning the attribute of the latest composition string for the target clause, the+ * one being converted. */+ if (lparam & GCS_COMPATTR) {+ const int attribute_size = ImmGetCompositionStringW(imm_context, GCS_COMPATTR, NULL, 0);+ if (attribute_size > 0) {+ char *attribute_data = (char *)malloc((size_t)attribute_size);+ if (attribute_data) {+ ImmGetCompositionStringW(imm_context, GCS_COMPATTR, attribute_data,+ (DWORD)attribute_size);+ for (target_start = 0; target_start < attribute_size; ++target_start) {+ if (is_target_attribute(attribute_data[target_start])) {+ break;+ }+ }+ for (target_end = target_start; target_end < attribute_size; ++target_end) {+ if (!is_target_attribute(attribute_data[target_end])) {+ break;+ }+ }+ if (target_start == attribute_size) {+ /* No target clause: this is an input clause, treat the whole of it as the target. */+ target_end = target_start;+ target_start = 0;+ }+ if (target_start != -1 && target_start < attribute_size &&+ attribute_data[target_start] == ATTR_TARGET_NOTCONVERTED)+ {+ c->cursor_position = target_start;+ }+ free(attribute_data);+ }+ }+ }+ }+ c->target_start = target_start;+ c->target_end = target_end;+}++static bool get_result(BKImeWin32 *ime, HWND hwnd, LPARAM lparam, BKImeComposition *c)+{+ bool result = false;+ HIMC imm_context = ImmGetContext(hwnd);+ if (imm_context) {+ /* Copy the result string; it has no composition attributes, so reset the others. */+ result = get_string(imm_context, lparam, GCS_RESULTSTR, c);+ c->cursor_position = -1;+ c->target_start = -1;+ c->target_end = -1;+ ImmReleaseContext(hwnd, imm_context);+ }+ (void)ime;+ return result;+}++static bool get_composition(BKImeWin32 *ime, HWND hwnd, LPARAM lparam, BKImeComposition *c)+{+ bool result = false;+ HIMC imm_context = ImmGetContext(hwnd);+ if (imm_context) {+ /* Copy the composition string. */+ result = get_string(imm_context, lparam, GCS_COMPSTR, c);+ /* Retrieve the cursor position in the IME composition. */+ c->cursor_position = ImmGetCompositionStringW(imm_context, GCS_CURSORPOS, NULL, 0);+ c->target_start = -1;+ c->target_end = -1;+ /* Retrieve the target selection. */+ get_caret(ime, imm_context, lparam, c);+ /* Mark that there is an ongoing composition. */+ ime->is_composing = true;+ ImmReleaseContext(hwnd, imm_context);+ }+ return result;+}++/* UTF-16 index -> UTF-8 byte offset (convert_utf16_to_utf8_len), -1 when out of range. */+static int utf8_offset(const BKImeComposition *c, int len)+{+ if (len >= 0 && len <= c->length) {+ return len ? WideCharToMultiByte(CP_UTF8, 0, c->string, len, NULL, 0, NULL, NULL) : 0;+ }+ return -1;+}++static void to_utf8(const BKImeComposition *c, char **buf, int *cap)+{+ const int n = c->length ? WideCharToMultiByte(CP_UTF8, 0, c->string, c->length, NULL, 0, NULL,+ NULL) :+ 0;+ *buf = (char *)grow(*buf, cap, n + 1, 1);+ if (n) {+ WideCharToMultiByte(CP_UTF8, 0, c->string, c->length, *buf, n, NULL, NULL);+ }+ (*buf)[n] = '\0';+}++void bk_ime_win32_empty_event(BKImeWin32 *ime, BKImeEvent *event)+{+ ime->result_utf8 = (char *)grow(ime->result_utf8, &ime->result_cap, 1, 1);+ ime->composite_utf8 = (char *)grow(ime->composite_utf8, &ime->composite_cap, 1, 1);+ ime->result_utf8[0] = ime->composite_utf8[0] = '\0';+ ime->cursor_pos = ime->target_start = ime->target_end = -1;+ event->result = ime->result_utf8;+ event->composite = ime->composite_utf8;+ event->cursor_pos = event->sel_start = event->sel_end = -1;+}++void bk_ime_win32_update_info(BKImeWin32 *ime, HWND hwnd, BKImeEvent *event)+{+ const bool res = get_result(ime, hwnd, GCS_RESULTSTR, &ime->result);+ const bool comp = get_composition(ime, hwnd, GCS_COMPSTR | GCS_COMPATTR, &ime->comp);+ bk_ime_win32_empty_event(ime, event);+ /* Convert wchar to UTF8. */+ if (res) {+ to_utf8(&ime->result, &ime->result_utf8, &ime->result_cap);+ }+ if (comp) {+ to_utf8(&ime->comp, &ime->composite_utf8, &ime->composite_cap);+ ime->cursor_pos = utf8_offset(&ime->comp, ime->comp.cursor_position);+ ime->target_start = utf8_offset(&ime->comp, ime->comp.target_start);+ ime->target_end = utf8_offset(&ime->comp, ime->comp.target_end);+ }+ event->result = ime->result_utf8;+ event->composite = ime->composite_utf8;+ event->cursor_pos = ime->cursor_pos;+ event->sel_start = ime->target_start;+ event->sel_end = ime->target_end;+}++#endif /* _WIN32 */diff --git a/kit/intern/kit_ime_win32.h b/kit/intern/kit_ime_win32.hnew file mode 100644index 0000000..2030843--- /dev/null+++ b/kit/intern/kit_ime_win32.h@@ -0,0 +1,87 @@+/* Input methods on Win32: a C port of Blender's GHOST_ImeWin32 (intern/ghost/intern/+ * GHOST_ImeWin32.cc, from Chromium's ImeInput): the IMM32 calls that enable an IME on a window,+ * place its candidate window, and read the composition and result strings.+ *+ * Message handling in the window procedure follows GHOST_SystemWin32.cc:+ * WM_INPUTLANGCHANGE bk_ime_win32_update_input_language + update_conversion_status+ * WM_IME_NOTIFY update_conversion_status (open status / conversion mode changes)+ * WM_IME_SETCONTEXT update_input_language, update_conversion_status, create_window,+ * cleanup_composition, check_first; then DefWindowProc+ * WM_IME_STARTCOMPOSITION create_window, reset_composition; handled (no DefWindowProc)+ * WM_IME_COMPOSITION update_window, update_info; handled+ * WM_IME_ENDCOMPOSITION reset_composition, destroy_window; handled+ * and the kit asks for begin/end (wm_window.cc WM_window_IME_begin / _end) through+ * BK_ime_state.+ *+ * SPDX-FileCopyrightText: 2010 The Chromium Authors. All rights reserved.+ * SPDX-License-Identifier: GPL-2.0-or-later */++#pragma once++#ifdef _WIN32++# include <stdbool.h>+# include <windows.h>++# include "BUI_kit.h"++/* MSDN LOCALE_SISO639LANGNAME: at most 9 characters including the terminator. */+# define BK_IME_ISO639_LEN 9++/* ImeComposition: the status of an ongoing composition, as UTF-16 straight from IMM32. */+typedef struct BKImeComposition {+ int cursor_position; /* the IME caret in the composition string */+ int target_start; /* the clause being converted: start and end */+ int target_end;+ wchar_t *string; /* NUL terminated */+ int length, cap;+} BKImeComposition;++typedef struct BKImeWin32 {+ bool is_composing;+ char language[BK_IME_ISO639_LEN]; /* "en", "zh", "ja", "ko", ... */+ DWORD conversion_modes;+ DWORD sentence_mode;+ bool system_caret; /* a temporary system caret exists, for IMEs that read ::GetCaretPos */+ int caret_l, caret_t, caret_r, caret_b;+ bool is_first, is_enable;++ BKImeComposition result, comp;+ /* GHOST_TEventImeData: what the last composition event carried, as UTF-8 */+ char *result_utf8, *composite_utf8;+ int result_cap, composite_cap;+ int cursor_pos, target_start, target_end;+ DWORD *clauses; /* GCS_COMPCLAUSE scratch */+ int clauses_cap;+} BKImeWin32;++void bk_ime_win32_init(BKImeWin32 *ime);+void bk_ime_win32_free(BKImeWin32 *ime);++void bk_ime_win32_update_input_language(BKImeWin32 *ime);+void bk_ime_win32_update_conversion_status(BKImeWin32 *ime, HWND hwnd);+bool bk_ime_win32_is_english_mode(const BKImeWin32 *ime);+/* Whether an IME handles this character key itself (`ascii` as typed, `numpad_period` for the+ * numpad's decimal key), so the window must not also type it. */+bool bk_ime_win32_is_ime_key_event(const BKImeWin32 *ime, char ascii, bool numpad_period);++void bk_ime_win32_create_window(BKImeWin32 *ime, HWND hwnd);+void bk_ime_win32_destroy_window(BKImeWin32 *ime, HWND hwnd);+void bk_ime_win32_update_window(BKImeWin32 *ime, HWND hwnd);+void bk_ime_win32_cleanup_composition(BKImeWin32 *ime, HWND hwnd);+void bk_ime_win32_reset_composition(BKImeWin32 *ime);+void bk_ime_win32_check_first(BKImeWin32 *ime, HWND hwnd);++/* Enables the IME (the default input context) for the window and moves its candidate window to+ * the caret rectangle; `complete` also completes an ongoing composition first, as GHOST's+ * beginIME does when another field takes over. */+void bk_ime_win32_begin(BKImeWin32 *ime, HWND hwnd, int l, int t, int r, int b, bool complete);+/* Disables the IME for the window, cancelling an ongoing composition. */+void bk_ime_win32_end(BKImeWin32 *ime, HWND hwnd);++/* Reads the result and composition strings into `event` (UTF-8; valid until the next call). */+void bk_ime_win32_update_info(BKImeWin32 *ime, HWND hwnd, BKImeEvent *event);+/* The state of "no composition": empty strings, no caret, no clause. */+void bk_ime_win32_empty_event(BKImeWin32 *ime, BKImeEvent *event);++#endif /* _WIN32 */diff --git a/kit/intern/kit_intern.h b/kit/intern/kit_intern.hindex a8ef667..6542312 100644--- a/kit/intern/kit_intern.h+++ b/kit/intern/kit_intern.h@@ -206,6 +206,19 @@ void bk_icons_draw(int id, float x, float y, float s, const float color[4]);/* -------------------------------------------------------------------- *//* Text editing (Blender's text button behavior) */+/* Input method composition (Blender's wmIMEData): the text an IME is composing at the+ * caret, drawn underlined and not yet part of the field. Embedded in every editable+ * node's state (BKTextEdit, the textarea) so one set of functions serves all of them. */+typedef struct BKImeComp {+ char *text; /* composite string (UTF-8), "" = none */+ int len, cap;+ int cursor; /* IME caret as a byte offset in text, -1 = none */+ int sel_start, sel_end; /* the clause being converted, bytes in text, -1 = none */+ bool composing; /* between WM_IME_STARTCOMPOSITION and ..END */+ int cand_x, cand_y; /* where the last draw wants the candidate window (window px) */+ bool cand_valid;+} BKImeComp;+typedef struct BKTextEdit {char *text;int len, cap;@@ -213,6 +226,7 @@ typedef struct BKTextEdit {float scroll;char mask[8]; /* shown instead of each character (password fields), "" = none */struct BKTextUndo *undo; /* Blender's interface_undo.cc text stack (Ctrl+Z while typing) */+ BKImeComp ime;} BKTextEdit;enum { TE_NONE = 0, TE_CHANGED, TE_CONFIRM, TE_CANCEL, TE_NEXT, TE_PREV };@@ -225,6 +239,30 @@ void bk_te_click(BKTextEdit *te, float px, bool extend);void bk_te_drag(BKTextEdit *te, float px);void bk_te_select_word(BKTextEdit *te, int index);void bk_te_draw(BKTextEdit *te, const BKRect *rect, const uchar color[4]);+bool bk_te_delete_selection(BKTextEdit *te);++/* -------------------------------------------------------------------- */+/* Input methods (kit_ime.c). Blender: GHOST_ImeWin32 + the wm/ui IME handlers. */++void bk_ime_comp_free(BKImeComp *c);+/* Stores a composition event's text, cursor and target clause. */+void bk_ime_comp_set(BKImeComp *c, const BKImeEvent *e);+/* The caret's x in the text the draw code lays out, composite included: `before` is the width+ * of the field text left of the caret, `to_caret` the composite's width up to the IME caret. */+float bk_ime_caret_offset(const BKImeComp *c, bool bold);+/* Blender's widget_draw_text_ime_underline: a 1px underline under the composite from `x`, a 2px+ * one under the clause being converted; `bottom` is the widget's lower edge, `clip_w` its width. */+void bk_ime_draw_underline(const BKImeComp *c, float x, float bottom, float clip_w,+ const uchar color[4], bool bold);+/* Records where the candidate window goes, in window pixels (the draw code calls it every frame,+ * like button_ime_reposition). A text field: 5px right of the caret, 1px below the field. */+void bk_ime_set_candidate(BKImeComp *c, int x, int y);+/* The composition of the node being edited, NULL when it takes no IME (number fields, passwords). */+BKImeComp *bk_ime_comp(BKNode *n);+/* An edit that ends while composing keeps the composite (Blender inserts it on a click outside). */+void bk_ime_commit(BKContext *ctx);+/* While composing, typed text and Escape belong to the IME. */+bool bk_ime_swallow_key(BKContext *ctx, const char *text, const char *keysym);/* -------------------------------------------------------------------- *//* Nodes */@@ -499,6 +537,11 @@ struct BKContext {BKNode *root, *header;BKHost host;BKNode *hover, *grab, *focus, *modal;+ /* what BK_ime_state last reported: the edited node and the candidate window position */+ struct {+ BKNode *node;+ int enabled, x, y;+ } ime;BKNode *popups[BK_MAX_POPUPS];int npopups;/* tooltip */@@ -639,6 +682,8 @@ float bk_hpx(float css);void bk_html_te_draw(BKTextEdit *te, const BKRect *content, const uchar color[4]);extern const BKClass bk_cls_meter;extern const BKClass bk_cls_filebrowser; /* kit_filebrowser.c */+BKImeComp *bk_textarea_ime(BKNode *n); /* kit_textarea.c */+bool bk_textarea_delete_selection(BKNode *n);bool bk_textarea_html_measure(BKNode *n, int *r_w, int *r_h); /* kit_textarea.c */void bk_textarea_html_draw(BKNode *n);diff --git a/kit/intern/kit_textarea.c b/kit/intern/kit_textarea.cindex 0238169..3d73e0e 100644--- a/kit/intern/kit_textarea.c+++ b/kit/intern/kit_textarea.c@@ -34,6 +34,7 @@ typedef struct TAData {Span *spans;int nspans;bool rows_set; /* "rows" was given (the browser look defaults to 2) */+ BKImeComp ime; /* the IME composition at the caret */} TAData;#define TA(n) ((TAData *)(n)->ext)@@ -117,12 +118,18 @@ static void ta_init(BKNode *n)static void ta_free(BKNode *n){TAData *t = TA(n);+ bk_ime_comp_free(&t->ime);free(t->text);free(t->lines);free(t->spans);free(t);}+BKImeComp *bk_textarea_ime(BKNode *n)+{+ return &TA(n)->ime;+}+static void ta_changed(BKNode *n){ta_reindex(TA(n));@@ -351,10 +358,20 @@ static bool delete_selection(BKNode *n)return true;}+bool bk_textarea_delete_selection(BKNode *n)+{+ const bool deleted = delete_selection(n);+ if (deleted) {+ ta_reindex(TA(n));+ }+ return deleted;+}+static int ta_press(BKNode *n, BKEvent *e){BKContext *ctx = bk_ctx(n);if (ctx && ctx->focus != n) {+ bk_ime_commit(ctx); /* the composition stays in the field being left */if (ctx->focus && ctx->focus->cls->finish_edit) {ctx->focus->cls->finish_edit(ctx->focus, true);}@@ -541,6 +558,8 @@ static void ta_measure(BKNode *n, int *rw, int *rh)static void draw_line_spans(TAData *t, int a, int b, float x, const BKRect *line,const uchar def[4]);+static void draw_caret_line(BKNode *n, int a, int b, float x, const BKRect *line,+ const uchar def[4]);/* Handlers that measure text, at the size it is drawn with. */#define TA_SCALED(name, impl) \@@ -615,7 +634,12 @@ void bk_textarea_html_draw(BKNode *n)}const int la = t->lines[line], lb = line_end(t, line);const BKRect lr = bk_rect(n->x, y, n->w, lh);- draw_line_spans(t, la, lb, x0, &lr, tc);+ if (focused && t->ime.len && t->caret >= la && t->caret <= lb) {+ draw_caret_line(n, la, lb, x0, &lr, tc);+ }+ else {+ draw_line_spans(t, la, lb, x0, &lr, tc);+ }if (sel && sa < lb + 1 && sb > la) { /* #3390ff behind white text */const int s0 = BK_MAX(sa, la), s1 = BK_MIN(sb, lb);const float sx = x0 + bk_text_width(t->text + la, s0 - la, false);@@ -631,9 +655,13 @@ void bk_textarea_html_draw(BKNode *n)bk_clip_pop();}if (focused && !sel && t->caret >= la && t->caret <= lb) {- const float cx = x0 + bk_text_width(t->text + la, t->caret - la, false);+ const float cx = x0 + bk_text_width(t->text + la, t->caret - la, false) ++ bk_ime_caret_offset(&t->ime, false);bk_fill(floorf(cx), (float)y, bw, (float)lh, tc);}+ if (focused && t->caret >= la && t->caret <= lb) {+ bk_ime_set_candidate(&t->ime, n->x, y + lh + 1);+ }}bk_clip_pop();}@@ -679,6 +707,20 @@ static void draw_line_spans(TAData *t, int a, int b, float x, const BKRect *line}}+/* The caret's line with an IME composition: the text before the caret, the composite underlined,+ * then the rest (spans keep colouring the text on either side). */+static void draw_caret_line(BKNode *n, int a, int b, float x, const BKRect *line,+ const uchar def[4])+{+ TAData *t = TA(n);+ const BKImeComp *c = &t->ime;+ const float cx = x + bk_text_width(t->text + a, t->caret - a, false);+ draw_line_spans(t, a, t->caret, x, line, def);+ bk_text(cx, bk_text_baseline(line), c->text, c->len, def, false);+ draw_line_spans(t, t->caret, b, cx + bk_text_width(c->text, c->len, false), line, def);+ bk_ime_draw_underline(c, cx, (float)line->ymax, (float)bk_rw(line), def, false);+}+static void ta_draw(BKNode *n){TAData *t = TA(n);@@ -714,11 +756,18 @@ static void ta_draw(BKNode *n)bk_fill(sx, (float)y, sw, (float)lh, bk_theme.selection);}const BKRect lr = bk_rect(n->x, y, n->w, lh);- draw_line_spans(t, la, lb, x0, &lr, w.text);+ if (focused && t->ime.len && t->caret >= la && t->caret <= lb) {+ draw_caret_line(n, la, lb, x0, &lr, w.text);+ }+ else {+ draw_line_spans(t, la, lb, x0, &lr, w.text);+ }if (focused && t->caret >= la && t->caret <= lb) {- const float cx = x0 + bk_text_width(t->text + la, t->caret - la, false);+ const float cx = x0 + bk_text_width(t->text + la, t->caret - la, false) ++ bk_ime_caret_offset(&t->ime, false);bk_fill(floorf(cx), (float)y + 1, BK_MAX(1.0f, floorf(0.1f * bk_unit() + 0.5f)),(float)lh - 2, bk_theme.cursor);+ bk_ime_set_candidate(&t->ime, n->x, y + lh + 1); /* the caret line's lower edge */}}bk_clip_pop();diff --git a/kit/intern/kit_widgets.c b/kit/intern/kit_widgets.cindex 737f2ab..441d478 100644--- a/kit/intern/kit_widgets.c+++ b/kit/intern/kit_widgets.c@@ -3454,7 +3454,9 @@ static void html_text(const BKRect *cr, const char *s, int len, const uchar c[4]void bk_html_te_draw(BKTextEdit *te, const BKRect *cr, const uchar color[4]){const float inner = BK_MAX(1.0f, (float)bk_rw(cr) - 1.0f);- const float caret_px = bk_masked_width(te->text, te->caret, te->mask, false);+ const BKImeComp *ime = &te->ime;+ const float text_px = bk_masked_width(te->text, te->caret, te->mask, false);+ const float caret_px = text_px + bk_ime_caret_offset(ime, false);if (caret_px - te->scroll > inner) {te->scroll = caret_px - inner;}@@ -3472,7 +3474,17 @@ void bk_html_te_draw(BKTextEdit *te, const BKRect *cr, const uchar color[4])}const float base = bk_text_baseline(cr);bk_clip_push(cr->xmin, cr->ymin - m, bk_rw(cr) + 1, h + 2 * m);- bk_text(tx, base, s, len, color, false);+ if (ime->len) { /* an IME composition: before the caret, the composite, after it */+ const float cx = tx + text_px;+ bk_text(tx, base, s, te->caret, color, false);+ bk_text(cx, base, ime->text, ime->len, color, false);+ bk_text(cx + bk_text_width(ime->text, ime->len, false), base, s + te->caret,+ len - te->caret, color, false);+ bk_ime_draw_underline(ime, cx, (float)cr->ymax, (float)bk_rw(cr), color, false);+ }+ else {+ bk_text(tx, base, s, len, color, false);+ }if (te->anchor >= 0 && te->anchor != te->caret) {const int a = BK_MIN(te->anchor, te->caret), b = BK_MAX(te->anchor, te->caret);const float sx = tx + bk_masked_width(te->text, a, te->mask, false);@@ -3490,6 +3502,7 @@ void bk_html_te_draw(BKTextEdit *te, const BKRect *cr, const uchar color[4])bk_fill(floorf(tx + caret_px), (float)cr->ymin, html_bw(), (float)h, color);}bk_clip_pop();+ bk_ime_set_candidate(&te->ime, (int)floorf(tx + caret_px) + 5, cr->ymax + 1);}/* -- buttons (push buttons, toggles, color input frame) -- */diff --git a/kit/intern/kit_window_win32.c b/kit/intern/kit_window_win32.cindex 03e0b78..4bf7986 100644--- a/kit/intern/kit_window_win32.c+++ b/kit/intern/kit_window_win32.c@@ -24,6 +24,8 @@# include <windows.h># include <windowsx.h>+# include "kit_ime_win32.h"+# define WM_BK_RENDER (WM_APP + 1)# define ID_TIP 1# define ID_ANIM 2@@ -45,6 +47,8 @@ struct BKWindow {int fixed_w, fixed_h; /* axes the user may not resize (Tk's resizable) */int user_sizing; /* inside a user move/resize loop */DWORD style; /* framed style restored by "override" 0 */+ BKImeWin32 ime; /* the IME (GHOST_ImeWin32) */+ WPARAM last_vk; /* the virtual key of the last WM_KEYDOWN */};static int g_open = 0;@@ -341,6 +345,25 @@ static double frame_ms(const BKWindow *win)return (double)(now.QuadPart - win->frame_start.QuadPart) * 1000.0 / (double)freq.QuadPart;}+/* Tells the platform what the kit's IME state needs (wm_window.cc WM_window_IME_begin / _reposition /+ * _end): on while a field takes text, restarted when another field takes over, the candidate+ * window following the caret. BK_ime_state reports only changes, so this costs nothing otherwise. */+static void ime_sync(BKWindow *win)+{+ BKImeState s;+ if (!BK_ime_state(win->ctx, &s)) {+ return;+ }+ if (!s.enabled) {+ bk_ime_win32_end(&win->ime, win->hwnd);+ return;+ }+ if (s.restart) {+ bk_ime_win32_end(&win->ime, win->hwnd); /* textedit_ime_begin ends first, then begins */+ }+ bk_ime_win32_begin(&win->ime, win->hwnd, s.x, s.y, s.x, s.y, s.restart != 0);+}+static void do_render(BKWindow *win){RECT rc;@@ -360,6 +383,7 @@ static void do_render(BKWindow *win)win->full = 1; /* the renderer changed: its frame starts empty */}const int regions = BK_render(win->ctx, win->raster, w, h, win->full);+ ime_sync(win); /* the draw placed the caret */QueryPerformanceCounter(&t1);win->full = 0;if (regions || resized) {@@ -589,8 +613,49 @@ static LRESULT CALLBACK wndproc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)mods_now(0, 0));return 0;}+ /* -- input methods: GHOST_SystemWin32.cc's IME cases, see kit_ime_win32.h -- */+ case WM_INPUTLANGCHANGE:+ bk_ime_win32_update_input_language(&win->ime);+ bk_ime_win32_update_conversion_status(&win->ime, hwnd);+ break;+ case WM_IME_NOTIFY: /* conversion status when the IME or its input mode changes */+ if (wp == IMN_SETOPENSTATUS || wp == IMN_SETCONVERSIONMODE) {+ bk_ime_win32_update_conversion_status(&win->ime, hwnd);+ }+ break;+ case WM_IME_SETCONTEXT:+ bk_ime_win32_update_input_language(&win->ime);+ bk_ime_win32_update_conversion_status(&win->ime, hwnd);+ bk_ime_win32_create_window(&win->ime, hwnd);+ bk_ime_win32_cleanup_composition(&win->ime, hwnd);+ bk_ime_win32_check_first(&win->ime, hwnd);+ break;+ case WM_IME_STARTCOMPOSITION: {+ BKImeEvent e;+ bk_ime_win32_create_window(&win->ime, hwnd);+ bk_ime_win32_reset_composition(&win->ime);+ bk_ime_win32_empty_event(&win->ime, &e);+ BK_ime_event(ctx, BK_IME_COMPOSITION_START, &e);+ return 0; /* no DefWindowProc: the kit draws the composition, not the system */+ }+ case WM_IME_COMPOSITION: {+ BKImeEvent e;+ bk_ime_win32_update_window(&win->ime, hwnd);+ bk_ime_win32_update_info(&win->ime, hwnd, &e);+ BK_ime_event(ctx, BK_IME_COMPOSITION, &e);+ return 0;+ }+ case WM_IME_ENDCOMPOSITION: {+ BKImeEvent e;+ bk_ime_win32_reset_composition(&win->ime);+ bk_ime_win32_destroy_window(&win->ime, hwnd);+ bk_ime_win32_empty_event(&win->ime, &e);+ BK_ime_event(ctx, BK_IME_COMPOSITION_END, &e);+ return 0;+ }case WM_KEYDOWN:case WM_SYSKEYDOWN: {+ win->last_vk = wp;char buf[8];const int mods = mods_now(0, 0);const char *ks = vk_keysym(wp, buf);@@ -622,6 +687,12 @@ static LRESULT CALLBACK wndproc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)if (wc[0] < 32 || wc[0] == 127 || (GetKeyState(VK_CONTROL) < 0 && GetKeyState(VK_MENU) >= 0)) {return 0; /* control characters: handled as keys */}+ /* a character key the IME handles itself must not also be typed (IsImeKeyEvent) */+ if (wc[0] < 0x80 &&+ bk_ime_win32_is_ime_key_event(&win->ime, (char)wc[0], win->last_vk == VK_DECIMAL))+ {+ return 0;+ }char *text = wide_to_utf8(wc, -1);const char *keysym = (text[0] == ' ' && !text[1]) ? "space" :(text[0] == '-' && !text[1]) ? "minus" :@@ -691,6 +762,7 @@ BKWindow *BK_window_new(BKContext *ctx, const char *title, int width, int heightregistered = 1;}BKWindow *win = (BKWindow *)calloc(1, sizeof(BKWindow));+ bk_ime_win32_init(&win->ime);win->ctx = ctx;win->prev = ctx->host;win->raster = BUI_raster_create(1, 1);@@ -745,6 +817,7 @@ void BK_window_free(BKWindow *win)BK_context_set_host(win->ctx, &win->prev);}BUI_raster_free(win->raster);+ bk_ime_win32_free(&win->ime);free(win);}diff --git a/tests/ime_test.c b/tests/ime_test.cnew file mode 100644index 0000000..1366343--- /dev/null+++ b/tests/ime_test.c@@ -0,0 +1,149 @@+/* Input methods: the kit's IME model (Blender's wmIMEData handling for text buttons) driven by+ * synthetic composition events, the way a window host feeds it. No real IME is needed.+ *+ * SPDX-License-Identifier: GPL-2.0-or-later */++#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "BUI_kit.h"+#include "GPU_raster.h"++static int g_fail = 0;+#define CHECK(c) \+ do { \+ if (!(c)) { \+ printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); \+ g_fail = 1; \+ } \+ } while (0)++enum { W = 320, H = 260 };++static BKContext *g_ctx;+static BuiRaster *g_raster;++static void draw(void)+{+ BK_render(g_ctx, g_raster, W, H, 1);+}++static void click(BKNode *n, int fx)+{+ int r[4];+ BK_node_rect(n, r);+ const int x = r[0] + r[2] * fx / 100, y = r[1] + r[3] / 2;+ BK_press(g_ctx, x, y, 0);+ BK_release(g_ctx, x, y, 0);+ draw();+}++static void ime(int type, const char *result, const char *composite, int cursor, int s0, int s1)+{+ BKImeEvent e = {result, composite, cursor, s0, s1};+ BK_ime_event(g_ctx, type, &e);+ draw();+}++static uint32_t checksum(void)+{+ const uint32_t *p = BUI_raster_pixels(g_raster);+ uint32_t h = 2166136261u;+ for (int i = 0; i < W * H; i++) {+ h = (h ^ p[i]) * 16777619u;+ }+ return h;+}++int main(void)+{+ BKNode *col = BK_node_new("column");+ BKNode *text = BK_node_new("text");+ BK_set_str(text, "value", "ab");+ BKNode *num = BK_node_new("number");+ BKNode *pass = BK_node_new("text");+ BK_set_str(pass, "value", "secret");+ BK_set_str(pass, "mask", "*");+ BKNode *area = BK_node_new("textarea");+ BK_set_str(area, "value", "hello");+ BK_node_add(col, text, -1);+ BK_node_add(col, num, -1);+ BK_node_add(col, pass, -1);+ BK_node_add(col, area, -1);+ g_ctx = BK_context_new(col);+ g_raster = BUI_raster_create(W, H);+ draw();++ BKImeState s;+ /* nothing is being edited: the IME stays off and the host has nothing to tell the platform */+ CHECK(BK_ime_state(g_ctx, &s) == 0 && !s.enabled);++ /* -- a text field -------------------------------------------------------------------- */+ click(text, 95);+ CHECK(BK_ime_state(g_ctx, &s) == 1);+ CHECK(s.enabled && s.restart);+ int r[4];+ BK_node_rect(text, r);+ CHECK(s.y == r[1] + r[3] + 1); /* just below the field (Blender: y - 4) */+ CHECK(s.x > r[0] && s.x < r[0] + r[2]); /* at the caret, 5px to its right */+ CHECK(BK_ime_state(g_ctx, &s) == 0); /* unchanged until the caret moves */+ const int x0 = s.x;+ const uint32_t before = checksum();++ ime(BK_IME_COMPOSITION_START, "", "", -1, -1, -1);+ CHECK(BK_ime_composing(g_ctx) == 1);+ ime(BK_IME_COMPOSITION, "", "\xE3\x81\xAB\xE3\x81\xBB", 6, 0, 6); /* "にほ", all converting */+ CHECK(checksum() != before); /* the composite is drawn, underlined */+ CHECK(BK_ime_state(g_ctx, &s) == 1 && s.enabled && !s.restart);+ CHECK(s.x > x0); /* the candidate window follows the caret */++ /* typed text and Escape belong to the IME while it composes */+ BK_key(g_ctx, "z", "z", 0);+ BK_key(g_ctx, "", "Escape", 0);+ CHECK(BK_ime_composing(g_ctx) == 1);+ CHECK(BK_ime_state(g_ctx, &s) == 0 && s.enabled);++ /* the result is typed in, the composite is gone */+ ime(BK_IME_COMPOSITION, "\xE6\x97\xA5\xE6\x9C\xAC", "", -1, -1, -1); /* "日本" */+ ime(BK_IME_COMPOSITION_END, "", "", -1, -1, -1);+ CHECK(BK_ime_composing(g_ctx) == 0);+ BK_key(g_ctx, "", "Return", 0);+ CHECK(strcmp(BK_get_str(text, "value"), "ab\xE6\x97\xA5\xE6\x9C\xAC") == 0);+ CHECK(BK_ime_state(g_ctx, &s) == 1 && !s.enabled); /* editing ended: the IME is off again */++ /* -- a composition that is still going when the edit ends is kept ----------------------- */+ click(text, 95);+ BK_ime_state(g_ctx, &s);+ ime(BK_IME_COMPOSITION_START, "", "", -1, -1, -1);+ ime(BK_IME_COMPOSITION, "", "\xE3\x81\x82", 3, -1, -1); /* "あ" */+ click(area, 50); /* a click outside */+ CHECK(strcmp(BK_get_str(text, "value"), "ab\xE6\x97\xA5\xE6\x9C\xAC\xE3\x81\x82") == 0);++ /* -- a text area ------------------------------------------------------------------------ */+ CHECK(BK_ime_state(g_ctx, &s) == 1 && s.enabled && s.restart); /* another field took over */+ BK_textarea_set_selection(area, 0, 2); /* a selection is replaced when composing starts */+ ime(BK_IME_COMPOSITION_START, "", "", -1, -1, -1);+ CHECK(strcmp(BK_get_str(area, "value"), "llo") == 0);+ ime(BK_IME_COMPOSITION, "\xE7\xAA\x93", "", -1, -1, -1); /* "窓" */+ ime(BK_IME_COMPOSITION_END, "", "", -1, -1, -1);+ CHECK(strstr(BK_get_str(area, "value"), "\xE7\xAA\x93") != NULL);++ /* -- fields that take no IME ------------------------------------------------------------- */+ click(num, 50); /* Blender's textedit_ime_begin skips number buttons */+ BK_ime_state(g_ctx, &s);+ CHECK(!s.enabled);+ BK_key(g_ctx, "", "Escape", 0);+ click(pass, 50); /* a password never shows its composition */+ BK_ime_state(g_ctx, &s);+ CHECK(!s.enabled);+ ime(BK_IME_COMPOSITION_START, "", "", -1, -1, -1); /* ignored */+ CHECK(BK_ime_composing(g_ctx) == 0);++ BUI_raster_free(g_raster);+ BK_context_free(g_ctx);+ if (!g_fail) {+ printf("ime ok\n");+ }+ return g_fail;+}diff --git a/tests/ime_win32_test.c b/tests/ime_win32_test.cnew file mode 100644index 0000000..fa5ccba--- /dev/null+++ b/tests/ime_win32_test.c@@ -0,0 +1,131 @@+/* Input methods on Win32: the window host's port of GHOST_ImeWin32 against a real window.+ *+ * The IME is off until a field is edited and on only while it is (BeginIME / EndIME), the+ * composition messages reach the kit, and a composition IMM32 can be driven to (it needs an+ * installed IME, so that part is skipped without one) arrives as drawn text and a result.+ * Exit 77 (skipped) off Windows.+ *+ * SPDX-License-Identifier: GPL-2.0-or-later */++#include <stdio.h>+#include <string.h>++#include "BUI_kit.h"++#ifndef _WIN32+int main(void)+{+ printf("not Windows: skipped\n");+ return 77;+}+#else++# define WIN32_LEAN_AND_MEAN+# include <windows.h>++# include <imm.h>++static int g_fail = 0;+# define CHECK(c) \+ do { \+ if (!(c)) { \+ printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); \+ g_fail = 1; \+ } \+ } while (0)++static void pump(void)+{+ for (int i = 0; i < 5; i++) {+ BK_window_pump();+ Sleep(5);+ }+}++static void click(HWND h, BKNode *n)+{+ int r[4];+ BK_node_rect(n, r);+ const LPARAM at = MAKELPARAM(r[0] + r[2] * 9 / 10, r[1] + r[3] / 2);+ SendMessageW(h, WM_LBUTTONDOWN, MK_LBUTTON, at);+ SendMessageW(h, WM_LBUTTONUP, 0, at);+ pump();+}++int main(void)+{+ if (!BK_window_supported()) {+ printf("no display: skipped\n");+ return 77;+ }+ BKNode *col = BK_node_new("column");+ BKNode *text = BK_node_new("text");+ BK_set_str(text, "value", "ab");+ BKNode *num = BK_node_new("number");+ BK_node_add(col, text, -1);+ BK_node_add(col, num, -1);+ BKContext *ctx = BK_context_new(col);+ BKWindow *win = BK_window_new(ctx, "BUI IME", 320, 200);+ if (!win) {+ printf("window failed\n");+ return 1;+ }+ BK_window_show(win);+ pump();+ BK_window_render_now(win);+ HWND h = (HWND)BK_window_handle(win);++ /* editing a text field turns the IME on (WM_window_IME_begin), ending the edit turns it off */+ click(h, text);+ CHECK(ImmGetContext(h) != NULL);+ SendMessageW(h, WM_KEYDOWN, VK_ESCAPE, 0);+ pump();+ CHECK(ImmGetContext(h) == NULL);++ /* number fields take no IME */+ click(h, num);+ CHECK(ImmGetContext(h) == NULL);+ SendMessageW(h, WM_KEYDOWN, VK_ESCAPE, 0);+ pump();++ /* the composition messages reach the kit: start composing, end composing */+ click(h, text);+ SendMessageW(h, WM_IME_STARTCOMPOSITION, 0, 0);+ CHECK(BK_ime_composing(ctx) == 1);+ SendMessageW(h, WM_IME_ENDCOMPOSITION, 0, 0);+ CHECK(BK_ime_composing(ctx) == 0);++ /* with an IME installed, drive a real composition through IMM32 and read it back */+ HIMC imc = ImmGetContext(h);+ if (imc) {+ const wchar_t comp[] = L"\x306B\x307B"; /* "にほ" */+ ImmSetOpenStatus(imc, TRUE);+ SendMessageW(h, WM_IME_STARTCOMPOSITION, 0, 0);+ if (ImmSetCompositionStringW(imc, SCS_SETSTR, (LPVOID)comp, sizeof(comp) - sizeof(wchar_t), NULL,+ 0))+ {+ pump();+ BK_window_render_now(win);+ CHECK(BK_ime_composing(ctx) == 1);+ ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);+ pump();+ printf("composition through IMM32: ok, value \"%s\"\n", BK_get_str(text, "value"));+ }+ else {+ printf("no IME to drive: composition through IMM32 skipped\n");+ }+ ImmReleaseContext(h, imc);+ SendMessageW(h, WM_IME_ENDCOMPOSITION, 0, 0);+ }+ SendMessageW(h, WM_KEYDOWN, VK_ESCAPE, 0);+ pump();++ BK_window_free(win);+ BK_context_free(ctx);+ if (!g_fail) {+ printf("ime win32 ok\n");+ }+ return g_fail;+}++#endif