1 xtk_ime
liquibyte edited this page 2026-11-19 16:23:20 -06:00

xtk_ime.h

Input method support, so text widgets can take Chinese, Japanese and Korean input.

Without an input method, the text widgets read keys with XLookupString(), which gives Latin-1 characters and keysyms. Accented Latin letters typed with dead keys or a compose key (é, ü, ñ) still work, because the X server composes them before the key event arrives. Real input methods (fcitx, ibus, or any other XIM server with its own composition window) need an input context per text window and Xutf8LookupString() to receive the finished UTF-8 text. This header gives xtk_entry.h, xtk_textview.h and xtk_richtext.h that.

The support is opt-in. Nothing here runs until the program calls xtk_ime_open(); a program that never does sees no change.

Setup. Call setlocale(LC_ALL, "") before XOpenDisplay(). The input method checks the locale that is active when xtk_ime_open() runs; without it, no input method opens (or, with some Xlib versions, one opens that never composes anything). Then open the input method once and give it to each text widget that should take composed input, with xtk_entry_set_ime(), xtk_textview_set_ime() or xtk_richtext_set_ime(). Passing NULL to one of those turns its input method off again. The widget makes its own input context with xtk_ime_create_input_context() and destroys it itself.

Events. The program's event loop must pass every event to XFilterEvent() before it gives the event to any widget, and skip the event when that returns True. The input method uses those events to build the text; without this step, composed text never arrives, or arrives mixed with raw key presses.

Teardown. Call xtk_ime_close() once, after every widget that uses the input method has been destroyed.

Example.

setlocale(LC_ALL, "");
Display *display = XOpenDisplay(NULL);
XIM im = xtk_ime_open(display);           // NULL if there is no input method
xtk_entry_set_ime(entry, im);             // NULL is accepted: plain input

for (;;) {
    XEvent event;
    XNextEvent(display, &event);
    if (XFilterEvent(&event, None)) continue;
    xtk_entry_handle_event(entry, &event);
}

// At the end, after the entry is destroyed:
xtk_ime_close(im);
 * 

Note: The input context uses the style XIMPreeditNothing and XIMStatusNothing: the input method draws its composition text in its own window. The widgets do not draw the text being composed in place.

#include "xtk_ime.h"

Functions: xtk_ime_open, xtk_ime_close, xtk_ime_create_input_context, xtk_ime_destroy_input_context

Functions

xtk_ime_open

XIM xtk_ime_open(Display *display);

Open the user's input method.

Checks that the current locale is supported (XSupportsLocale()), sets the locale modifiers from the XMODIFIERS environment variable and opens the input method with XOpenIM().

Parameter Description
display The X connection

Returns: The input method, or NULL if display is NULL, the locale is not supported (usually because setlocale() was not called first) or no input method is running. Every xtk_*_set_ime() call accepts NULL and keeps plain input.

xtk_ime_close

void xtk_ime_close(XIM input_method);

Close an input method opened by xtk_ime_open().

Call it after every widget that uses the input method has been destroyed. Does nothing when input_method is NULL.

Parameter Description
input_method The input method from xtk_ime_open(), or NULL

xtk_ime_create_input_context

XIC xtk_ime_create_input_context(XIM input_method, Window window);

Create an input context for one text window.

The text widgets call this themselves from their *_set_ime() call; a program that uses those widgets does not need it. A program with a text window of its own can use it, then read keys with Xutf8LookupString() and the returned context.

Parameter Description
input_method An input method from xtk_ime_open()
window The text window. It becomes both the client window and the focus window of the context.

Returns: The new input context, or NULL if input_method is NULL, window is None or the input method refused to make one

xtk_ime_destroy_input_context

void xtk_ime_destroy_input_context(XIC ic);

Destroy an input context made by xtk_ime_create_input_context().

Does nothing when ic is NULL.

Parameter Description
ic The input context, or NULL