Class IMContext

All Implemented Interfaces:
PointerInterface
Direct Known Subclasses:
IMContextSimple, IMMulticontext

public class IMContext extends Object
`GtkIMContext` defines the interface for GTK input methods.

`GtkIMContext` is used by GTK text input widgets like `GtkText`
to map from key events to Unicode character strings.

An input method may consume multiple key events in sequence before finally
outputting 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. To do so, the `GtkIMContext` will emit
[signal@Gtk.IMContext::preedit-start], [signal@Gtk.IMContext::preedit-changed]
and [signal@Gtk.IMContext::preedit-end] signals.

For instance, the built-in GTK input method [class@Gtk.IMContextSimple]
implements the input of arbitrary Unicode code points by holding down the
<kbd>Control</kbd> and <kbd>Shift</kbd> keys and then typing <kbd>u</kbd>
followed by the hexadecimal digits of the code point. When releasing the
<kbd>Control</kbd> and <kbd>Shift</kbd> keys, preediting ends and the
character is inserted as text. For example,

Ctrl+Shift+u 2 0 A C

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
provides a `GIOExtension` for the extension point named "gtk-im-module".

To connect a widget to the users preferred input method, you should use
[class@Gtk.IMMulticontext].

https://docs.gtk.org/gtk4/class.IMContext.html

  • Field Details

  • Constructor Details

  • Method Details

    • getClassHandler

      public static ClassHandler getClassHandler()
    • deleteSurrounding

      public boolean deleteSurrounding(int offset, int n_chars)
      Asks the widget that the input context is attached to delete
      characters around the cursor position by emitting the
      `::delete_surrounding` signal.

      Note that @offset and @n_chars are in characters not in bytes
      which differs from the usage other places in `GtkIMContext`.

      In order to use this function, you should first call
      [method@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.
      Parameters:
      offset - offset from cursor position in chars; a negative value means start before the cursor.
      n_chars - number of characters to delete.
      Returns:
      %TRUE if the signal was handled.
    • filterKey

      public boolean filterKey(boolean press, @Nonnull Surface surface, @Nonnull Device device, int time, int keycode, int state, int group)
      Allow an input method to forward key press and release events
      to another input method without necessarily having a `GdkEvent`
      available.
      Parameters:
      press - whether to forward a key press or release event
      surface - the surface the event is for
      device - the device that the event is for
      time - the timestamp for the event
      keycode - the keycode for the event
      state - modifier state for the event
      group - the active keyboard group for the event
      Returns:
      %TRUE if the input method handled the key event.
    • filterKeypress

      public boolean filterKeypress(@Nonnull Event event)
      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.
      Parameters:
      event - the key event
      Returns:
      %TRUE if the input method handled the key event.
    • focusIn

      public void focusIn()
      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.
    • focusOut

      public void focusOut()
      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.
    • getSurroundingWithSelection

      public boolean getSurroundingWithSelection(@Nonnull Strs text, @Nonnull Int cursor_index, @Nonnull Int anchor_index)
      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
      [signal@Gtk.IMContext::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
      [method@Gtk.IMContext.set_surrounding_with_selection].

      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.
      Parameters:
      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 g_free().
      cursor_index - location to store byte index of the insertion cursor within @text.
      anchor_index - location to store byte index of the selection bound within @text
      Returns:
      `TRUE` if surrounding text was provided; in this case you must free the result stored in `text`.
    • reset

      public void reset()
      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.
    • setClientWidget

      public void setClientWidget(@Nullable Widget widget)
      Set the client widget for the input context.

      This is the `GtkWidget` holding the input focus. This widget is
      used in order to correctly position status windows, and may
      also be used for purposes internal to the input method.
      Parameters:
      widget - the client widget. This may be %NULL to indicate that the previous client widget no longer exists.
    • setCursorLocation

      public void setCursorLocation(@Nonnull Rectangle area)
      Notify the input method that a change in cursor
      position has been made.

      The location is relative to the client widget.
      Parameters:
      area - new location
    • setSurroundingWithSelection

      public void setSurroundingWithSelection(@Nonnull Str text, int len, int cursor_index, int anchor_index)
      Sets surrounding context around the insertion point and preedit
      string. This function is expected to be called in response to the
      [signal@Gtk.IMContext::retrieve_surrounding] signal, and will likely
      have no effect if called at other times.
      Parameters:
      text - text surrounding the insertion point, as UTF-8. the preedit string should not be included within @text
      len - the length of @text, or -1 if @text is nul-terminated
      cursor_index - the byte index of the insertion cursor within @text
      anchor_index - the byte index of the selection bound within @text
    • setSurroundingWithSelection

      public void setSurroundingWithSelection(String text, int len, int cursor_index, int anchor_index)
      Sets surrounding context around the insertion point and preedit
      string. This function is expected to be called in response to the
      [signal@Gtk.IMContext::retrieve_surrounding] signal, and will likely
      have no effect if called at other times.
      Parameters:
      text - text surrounding the insertion point, as UTF-8. the preedit string should not be included within @text
      len - the length of @text, or -1 if @text is nul-terminated
      cursor_index - the byte index of the insertion cursor within @text
      anchor_index - the byte index of the selection bound within @text
    • setUsePreedit

      public void setUsePreedit(boolean use_preedit)
      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.
      Parameters:
      use_preedit - whether the IM context should use the preedit string.
    • onCommit

      public SignalHandler onCommit(IMContext.OnCommit signal)
      Connect to signal "commit".
      See IMContext.OnCommit.onCommit(ch.bailu.gtk.type.Str) for signal description.
      Field SIGNAL_ON_COMMIT contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onDeleteSurrounding

      public SignalHandler onDeleteSurrounding(IMContext.OnDeleteSurrounding signal)
      Connect to signal "delete-surrounding".
      See IMContext.OnDeleteSurrounding.onDeleteSurrounding(int, int) for signal description.
      Field SIGNAL_ON_DELETE_SURROUNDING contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onPreeditChanged

      public SignalHandler onPreeditChanged(IMContext.OnPreeditChanged signal)
      Connect to signal "preedit-changed".
      See IMContext.OnPreeditChanged.onPreeditChanged() for signal description.
      Field SIGNAL_ON_PREEDIT_CHANGED contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onPreeditEnd

      public SignalHandler onPreeditEnd(IMContext.OnPreeditEnd signal)
      Connect to signal "preedit-end".
      See IMContext.OnPreeditEnd.onPreeditEnd() for signal description.
      Field SIGNAL_ON_PREEDIT_END contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onPreeditStart

      public SignalHandler onPreeditStart(IMContext.OnPreeditStart signal)
      Connect to signal "preedit-start".
      See IMContext.OnPreeditStart.onPreeditStart() for signal description.
      Field SIGNAL_ON_PREEDIT_START contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onRetrieveSurrounding

      public SignalHandler onRetrieveSurrounding(IMContext.OnRetrieveSurrounding signal)
      Connect to signal "retrieve-surrounding".
      See IMContext.OnRetrieveSurrounding.onRetrieveSurrounding() for signal description.
      Field SIGNAL_ON_RETRIEVE_SURROUNDING contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • getTypeID

      public static long getTypeID()
    • getParentTypeID

      public static long getParentTypeID()
    • getTypeSize

      public static TypeSystem.TypeSize getTypeSize()
    • getParentTypeSize

      public static TypeSystem.TypeSize getParentTypeSize()
    • getInstanceSize

      public static int getInstanceSize()