Gtk.IMContext

g GObject.Object GObject.Object Gtk.IMContext Gtk.IMContext GObject.Object->Gtk.IMContext

Subclasses:

Gtk.IMContextSimple, Gtk.IMMulticontext

Methods

Inherited:

GObject.Object (37)

Structs:

GObject.ObjectClass (5)

delete_surrounding (offset, n_chars)

filter_keypress (event)

focus_in ()

focus_out ()

get_preedit_string ()

get_surrounding ()

reset ()

set_client_window (window)

set_cursor_location (area)

set_surrounding (text, len, cursor_index)

set_use_preedit (use_preedit)

Virtual Methods

Inherited:

GObject.Object (7)

do_commit (str)

do_delete_surrounding (offset, n_chars)

do_filter_keypress (event)

do_focus_in ()

do_focus_out ()

do_get_preedit_string ()

do_get_surrounding ()

do_preedit_changed ()

do_preedit_end ()

do_preedit_start ()

do_reset ()

do_retrieve_surrounding ()

do_set_client_window (window)

do_set_cursor_location (area)

do_set_surrounding (text, len, cursor_index)

do_set_use_preedit (use_preedit)

Properties

Name

Type

Flags

Short Description

input-hints

Gtk.InputHints

r/w/en

Hints for the text field behaviour

input-purpose

Gtk.InputPurpose

r/w/en

Purpose of the text field

Signals

Inherited:

GObject.Object (1)

Name

Short Description

commit

The ::commit signal is emitted when a complete input sequence has been entered by the user.

delete-surrounding

The ::delete-surrounding signal is emitted when the input method needs to delete all or part of the context surrounding the cursor.

preedit-changed

The ::preedit-changed signal is emitted whenever the preedit sequence currently being entered has changed.

preedit-end

The ::preedit-end signal is emitted when a preediting sequence has been completed or canceled.

preedit-start

The ::preedit-start signal is emitted when a new preediting sequence starts.

retrieve-surrounding

The ::retrieve-surrounding signal is emitted when the input method requires the context surrounding the cursor.

Fields

Inherited:

GObject.Object (1)

Name

Type

Access

Description

parent_instance

GObject.Object

r

Class Details

class Gtk.IMContext(**kwargs)
Bases:

GObject.Object

Abstract:

Yes

Structure:

Gtk.IMContextClass

Gtk.IMContext defines the interface for GTK+ input methods. An input method is used by GTK+ text input widgets like Gtk.Entry to map from key events to Unicode character strings.

The default input method can be set programmatically via the Gtk.Settings :gtk-im-module Gtk.Settings property. Alternatively, you may set the GTK_IM_MODULE environment variable as documented in Running GTK+ Applications.

The Gtk.Entry Gtk.Entry :im-module and Gtk.TextView Gtk.TextView :im-module properties may also be used to set input methods for specific widget instances. For instance, a certain entry widget might be expected to contain certain characters which would be easier to input with a certain input method.

An input method may consume multiple key events in sequence and finally output the composed result. This is called preediting, and an input method may provide feedback about this process by displaying the intermediate composition states as preedit text. For instance, the default GTK+ input method implements the input of arbitrary Unicode code points by holding down the Control and Shift keys and then typing “U” followed by the hexadecimal digits of the code point. When releasing the Control and Shift keys, preediting ends and the character is inserted as text. Ctrl+Shift+u20AC for example results in the € sign.

Additional input methods can be made available for use by GTK+ widgets as loadable modules. An input method module is a small shared library which implements a subclass of Gtk.IMContext or Gtk.IMContextSimple and exports these four functions:

void im_module_init(GTypeModule *module);

This function should register the GObject.GType of the Gtk.IMContext subclass which implements the input method by means of GObject.TypeModule.register_type(). Note that GObject.type_register_static() cannot be used as the type needs to be registered dynamically.

void im_module_exit(void);

Here goes any cleanup code your input method might require on module unload.

void im_module_list(const GtkIMContextInfo ***contexts, int *n_contexts)
{
  *contexts = info_list;
  *n_contexts = G_N_ELEMENTS (info_list);
}

This function returns the list of input methods provided by the module. The example implementation above shows a common solution and simply returns a pointer to statically defined array of Gtk.IMContextInfo items for each provided input method.

GtkIMContext * im_module_create(const gchar *context_id);

This function should return a pointer to a newly created instance of the Gtk.IMContext subclass identified by context_id. The context ID is the same as specified in the Gtk.IMContextInfo array returned by im_module_list().

After a new loadable input method module has been installed on the system, the configuration file gtk.immodules needs to be regenerated by ‘gtk-query-immodules-3.0 [gtk-query-immodules-3.0]’, in order for the new input method to become available to GTK+ applications.

delete_surrounding(offset, n_chars)[source]
Parameters:
  • offset (int) – offset from cursor position in chars; a negative value means start before the cursor.

  • n_chars (int) – number of characters to delete.

Returns:

True if the signal was handled.

Return type:

bool

Asks the widget that the input context is attached to to delete characters around the cursor position by emitting the GtkIMContext::delete_surrounding signal. Note that offset and n_chars are in characters not in bytes which differs from the usage other places in Gtk.IMContext.

In order to use this function, you should first call Gtk.IMContext.get_surrounding() to get the current context, and call this function immediately afterwards to make sure that you know what you are deleting. You should also account for the fact that even if the signal was handled, the input context might not have deleted all the characters that were requested to be deleted.

This function is used by an input method that wants to make subsitutions in the existing text in response to new input. It is not useful for applications.

filter_keypress(event)[source]
Parameters:

event (Gdk.EventKey) – the key event

Returns:

True if the input method handled the key event.

Return type:

bool

Allow an input method to internally handle key press and release events. If this function returns True, then no further processing should be done for this key event.

focus_in()[source]

Notify the input method that the widget to which this input context corresponds has gained focus. The input method may, for example, change the displayed feedback to reflect this change.

focus_out()[source]

Notify the input method that the widget to which this input context corresponds has lost focus. The input method may, for example, change the displayed feedback or reset the contexts state to reflect this change.

get_preedit_string()[source]
Returns:

str:

location to store the retrieved string. The string retrieved must be freed with GLib.free().

attrs:

location to store the retrieved attribute list. When you are done with this list, you must unreference it with Pango.AttrList.unref().

cursor_pos:

location to store position of cursor (in characters) within the preedit string.

Return type:

(str: str, attrs: Pango.AttrList, cursor_pos: int)

Retrieve the current preedit string for the input context, and a list of attributes to apply to the string. This string should be displayed inserted at the insertion point.

get_surrounding()[source]
Returns:

None if no surrounding text was provided; otherwise a tuple containing:

text:

text holding context around the insertion point.

cursor_index:

byte index of the insertion cursor within text

Return type:

(text: str, cursor_index: int) or None

Retrieves context around the insertion point. Input methods typically want context in order to constrain input text based on existing text; this is important for languages such as Thai where only some sequences of characters are allowed.

This function is implemented by emitting the GtkIMContext::retrieve_surrounding signal on the input method; in response to this signal, a widget should provide as much context as is available, up to an entire paragraph, by calling Gtk.IMContext.set_surrounding(). Note that there is no obligation for a widget to respond to the ::retrieve_surrounding signal, so input methods must be prepared to function without context.

reset()[source]

Notify the input method that a change such as a change in cursor position has been made. This will typically cause the input method to clear the preedit state.

set_client_window(window)[source]
Parameters:

window (Gdk.Window or None) – the client window. This may be None to indicate that the previous client window no longer exists.

Set the client window for the input context; this is the Gdk.Window in which the input appears. This window is used in order to correctly position status windows, and may also be used for purposes internal to the input method.

set_cursor_location(area)[source]
Parameters:

area (Gdk.Rectangle) – new location

Notify the input method that a change in cursor position has been made. The location is relative to the client window.

set_surrounding(text, len, cursor_index)[source]
Parameters:
  • text (str) – text surrounding the insertion point, as UTF-8. the preedit string should not be included within text.

  • len (int) – the length of text, or -1 if text is nul-terminated

  • cursor_index (int) – the byte index of the insertion cursor within text.

Sets surrounding context around the insertion point and preedit string. This function is expected to be called in response to the GtkIMContext::retrieve_surrounding signal, and will likely have no effect if called at other times.

set_use_preedit(use_preedit)[source]
Parameters:

use_preedit (bool) – whether the IM context should use the preedit string.

Sets whether the IM context should use the preedit string to display feedback. If use_preedit is False (default is True), then the IM context may use some other method to display feedback, such as displaying it in a child of the root window.

do_commit(str) virtual
Parameters:

str (str) –

Default handler of the Gtk.IMContext ::commit signal.

do_delete_surrounding(offset, n_chars) virtual
Parameters:
  • offset (int) – offset from cursor position in chars; a negative value means start before the cursor.

  • n_chars (int) – number of characters to delete.

Returns:

True if the signal was handled.

Return type:

bool

Asks the widget that the input context is attached to to delete characters around the cursor position by emitting the GtkIMContext::delete_surrounding signal. Note that offset and n_chars are in characters not in bytes which differs from the usage other places in Gtk.IMContext.

In order to use this function, you should first call Gtk.IMContext.get_surrounding() to get the current context, and call this function immediately afterwards to make sure that you know what you are deleting. You should also account for the fact that even if the signal was handled, the input context might not have deleted all the characters that were requested to be deleted.

This function is used by an input method that wants to make subsitutions in the existing text in response to new input. It is not useful for applications.

do_filter_keypress(event) virtual
Parameters:

event (Gdk.EventKey) – the key event

Returns:

True if the input method handled the key event.

Return type:

bool

Allow an input method to internally handle key press and release events. If this function returns True, then no further processing should be done for this key event.

do_focus_in() virtual

Notify the input method that the widget to which this input context corresponds has gained focus. The input method may, for example, change the displayed feedback to reflect this change.

do_focus_out() virtual

Notify the input method that the widget to which this input context corresponds has lost focus. The input method may, for example, change the displayed feedback or reset the contexts state to reflect this change.

do_get_preedit_string() virtual
Returns:

str:

location to store the retrieved string. The string retrieved must be freed with GLib.free().

attrs:

location to store the retrieved attribute list. When you are done with this list, you must unreference it with Pango.AttrList.unref().

cursor_pos:

location to store position of cursor (in characters) within the preedit string.

Return type:

(str: str, attrs: Pango.AttrList, cursor_pos: int)

Retrieve the current preedit string for the input context, and a list of attributes to apply to the string. This string should be displayed inserted at the insertion point.

do_get_surrounding() virtual
Returns:

True if surrounding text was provided; in this case you must free the result stored in *text.

text:

location to store a UTF-8 encoded string of text holding context around the insertion point. If the function returns True, then you must free the result stored in this location with GLib.free().

cursor_index:

location to store byte index of the insertion cursor within text.

Return type:

(bool, text: str, cursor_index: int)

Retrieves context around the insertion point. Input methods typically want context in order to constrain input text based on existing text; this is important for languages such as Thai where only some sequences of characters are allowed.

This function is implemented by emitting the GtkIMContext::retrieve_surrounding signal on the input method; in response to this signal, a widget should provide as much context as is available, up to an entire paragraph, by calling Gtk.IMContext.set_surrounding(). Note that there is no obligation for a widget to respond to the ::retrieve_surrounding signal, so input methods must be prepared to function without context.

do_preedit_changed() virtual

Default handler of the Gtk.IMContext ::preedit-changed signal.

do_preedit_end() virtual

Default handler of the Gtk.IMContext ::preedit-end signal.

do_preedit_start() virtual

Default handler of the Gtk.IMContext ::preedit-start signal.

do_reset() virtual

Notify the input method that a change such as a change in cursor position has been made. This will typically cause the input method to clear the preedit state.

do_retrieve_surrounding() virtual
Return type:

bool

Default handler of the Gtk.IMContext ::retrieve-surrounding signal.

do_set_client_window(window) virtual
Parameters:

window (Gdk.Window or None) – the client window. This may be None to indicate that the previous client window no longer exists.

Set the client window for the input context; this is the Gdk.Window in which the input appears. This window is used in order to correctly position status windows, and may also be used for purposes internal to the input method.

do_set_cursor_location(area) virtual
Parameters:

area (Gdk.Rectangle) – new location

Notify the input method that a change in cursor position has been made. The location is relative to the client window.

do_set_surrounding(text, len, cursor_index) virtual
Parameters:
  • text (str) – text surrounding the insertion point, as UTF-8. the preedit string should not be included within text.

  • len (int) – the length of text, or -1 if text is nul-terminated

  • cursor_index (int) – the byte index of the insertion cursor within text.

Sets surrounding context around the insertion point and preedit string. This function is expected to be called in response to the GtkIMContext::retrieve_surrounding signal, and will likely have no effect if called at other times.

do_set_use_preedit(use_preedit) virtual
Parameters:

use_preedit (bool) – whether the IM context should use the preedit string.

Sets whether the IM context should use the preedit string to display feedback. If use_preedit is False (default is True), then the IM context may use some other method to display feedback, such as displaying it in a child of the root window.

Signal Details

Gtk.IMContext.signals.commit(i_m_context, str)
Signal Name:

commit

Flags:

RUN_LAST

Parameters:
  • i_m_context (Gtk.IMContext) – The object which received the signal

  • str (str) – the completed character(s) entered by the user

The ::commit signal is emitted when a complete input sequence has been entered by the user. This can be a single character immediately after a key press or the final result of preediting.

Gtk.IMContext.signals.delete_surrounding(i_m_context, offset, n_chars)
Signal Name:

delete-surrounding

Flags:

RUN_LAST

Parameters:
  • i_m_context (Gtk.IMContext) – The object which received the signal

  • offset (int) – the character offset from the cursor position of the text to be deleted. A negative value indicates a position before the cursor.

  • n_chars (int) – the number of characters to be deleted

Returns:

True if the signal was handled.

Return type:

bool

The ::delete-surrounding signal is emitted when the input method needs to delete all or part of the context surrounding the cursor.

Gtk.IMContext.signals.preedit_changed(i_m_context)
Signal Name:

preedit-changed

Flags:

RUN_LAST

Parameters:

i_m_context (Gtk.IMContext) – The object which received the signal

The ::preedit-changed signal is emitted whenever the preedit sequence currently being entered has changed. It is also emitted at the end of a preedit sequence, in which case Gtk.IMContext.get_preedit_string() returns the empty string.

Gtk.IMContext.signals.preedit_end(i_m_context)
Signal Name:

preedit-end

Flags:

RUN_LAST

Parameters:

i_m_context (Gtk.IMContext) – The object which received the signal

The ::preedit-end signal is emitted when a preediting sequence has been completed or canceled.

Gtk.IMContext.signals.preedit_start(i_m_context)
Signal Name:

preedit-start

Flags:

RUN_LAST

Parameters:

i_m_context (Gtk.IMContext) – The object which received the signal

The ::preedit-start signal is emitted when a new preediting sequence starts.

Gtk.IMContext.signals.retrieve_surrounding(i_m_context)
Signal Name:

retrieve-surrounding

Flags:

RUN_LAST

Parameters:

i_m_context (Gtk.IMContext) – The object which received the signal

Returns:

True if the signal was handled.

Return type:

bool

The ::retrieve-surrounding signal is emitted when the input method requires the context surrounding the cursor. The callback should set the input method surrounding context by calling the Gtk.IMContext.set_surrounding() method.

Property Details

Gtk.IMContext.props.input_hints
Name:

input-hints

Type:

Gtk.InputHints

Default Value:

Gtk.InputHints.NONE

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Hints for the text field behaviour

Gtk.IMContext.props.input_purpose
Name:

input-purpose

Type:

Gtk.InputPurpose

Default Value:

Gtk.InputPurpose.FREE_FORM

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Purpose of the text field