Class IMContext

All Implemented Interfaces:
Proxy
Direct Known Subclasses:
IMContext.IMContext$Impl, IMContextSimple, IMMulticontext, VimIMContext

@Generated("org.javagi.JavaGI") public abstract class IMContext extends GObject

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 Gtk.IMContext::preedit-start, Gtk.IMContext::preedit-changed and Gtk.IMContext::preedit-end signals.

For instance, the built-in GTK input method IMContextSimple 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. 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 IMMulticontext.

  • Constructor Details

    • IMContext

      public IMContext(MemorySegment address)
      Create a IMContext instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • IMContext

      public IMContext()
      Create a new IMContext.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the IMContext class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected IMContext asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class GObject
      Returns:
      the instance as if it were its parent type
    • activateOsk

      public boolean activateOsk(@Nullable Event event)

      Requests the platform to show an on-screen keyboard for user input.

      This method will return true if this request was actually performed to the platform, other environmental factors may result in an on-screen keyboard effectively not showing up.

      Parameters:
      event - a Event
      Returns:
      true if an on-screen keyboard could be requested to the platform.
      Since:
      4.14
    • deleteSurrounding

      public boolean deleteSurrounding(int offset, int nChars)

      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 nChars 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 getSurrounding(Out, Out) 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 substitutions 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.
      nChars - number of characters to delete.
      Returns:
      true if the signal was handled.
    • filterKey

      public boolean filterKey(boolean press, Surface surface, Device device, int time, int keycode, Set<ModifierType> 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.
    • filterKey

      public boolean filterKey(boolean press, Surface surface, Device device, int time, int keycode, ModifierType 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(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.

    • getPreeditString

      public void getPreeditString(Out<String> str, Out<AttrList> attrs, Out<Integer> cursorPos)

      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.

      Parameters:
      str - location to store the retrieved string. The string retrieved must be freed with g_free().
      attrs - location to store the retrieved attribute list. When you are done with this list, you must unreference it with AttrList#unref.
      cursorPos - location to store position of cursor (in characters) within the preedit string.
    • getSurrounding

      @Deprecated public boolean getSurrounding(Out<String> text, Out<Integer> cursorIndex)

      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 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 setSurrounding(String, int, int).

      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().
      cursorIndex - location to store byte index of the insertion cursor within text.
      Returns:
      TRUE if surrounding text was provided; in this case you must free the result stored in text.
    • getSurroundingWithSelection

      public boolean getSurroundingWithSelection(Out<String> text, Out<Integer> cursorIndex, Out<Integer> anchorIndex)

      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 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 setSurroundingWithSelection(String, int, int, int).

      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().
      cursorIndex - location to store byte index of the insertion cursor within text.
      anchorIndex - 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.
      Since:
      4.2
    • 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(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
    • setSurrounding

      @Deprecated public void setSurrounding(String text, int len, int cursorIndex)

      Sets surrounding context around the insertion point and preedit string.

      This function is expected to be called in response to the 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
      cursorIndex - the byte index of the insertion cursor within text.
    • setSurroundingWithSelection

      public void setSurroundingWithSelection(String text, int len, int cursorIndex, int anchorIndex)
      Sets surrounding context around the insertion point and preedit string. This function is expected to be called in response to the 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
      cursorIndex - the byte index of the insertion cursor within text
      anchorIndex - the byte index of the selection bound within text
      Since:
      4.2
    • setUsePreedit

      public void setUsePreedit(boolean usePreedit)

      Sets whether the IM context should use the preedit string to display feedback.

      If usePreedit 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:
      usePreedit - whether the IM context should use the preedit string.
    • activateOsk

      protected void activateOsk()
    • activateOskWithEvent

      protected boolean activateOskWithEvent(Event event)
    • commit

      protected void commit(String str)
      Default handler of the Gtk.IMContext::commit signal.
    • invalidComposition

      protected boolean invalidComposition(String str)
      Default handler of the Gtk.IMContext::invalid-composition signal. Since: 4.22
    • preeditChanged

      protected void preeditChanged()
      Default handler of the Gtk.IMContext::preedit-changed signal.
    • preeditEnd

      protected void preeditEnd()
      Default handler of the Gtk.IMContext::preedit-end signal.
    • preeditStart

      protected void preeditStart()
      Default handler of the Gtk.IMContext::preedit-start signal.
    • retrieveSurrounding

      protected boolean retrieveSurrounding()
      Default handler of the Gtk.IMContext::retrieve-surrounding signal.
    • onCommit

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

      If the commit comes after a preediting sequence, the ::commit signal is emitted after ::preedit-end.

      This can be a single character immediately after a key press or the final result of preediting.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitCommit

      public void emitCommit(String str)
      Emits the "commit" signal. See onCommit(IMContext.CommitCallback).
    • onDeleteSurrounding

      The ::delete-surrounding signal is emitted when the input method needs to delete all or part of the context surrounding the cursor.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitDeleteSurrounding

      public boolean emitDeleteSurrounding(int offset, int nChars)
      Emits the "delete-surrounding" signal. See onDeleteSurrounding(IMContext.DeleteSurroundingCallback).
    • onInvalidComposition

      Emitted when the filtered keys do not compose to a single valid character.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      Since:
      4.22
      See Also:
    • emitInvalidComposition

      public boolean emitInvalidComposition(String str)
      Emits the "invalid-composition" signal. See onInvalidComposition(IMContext.InvalidCompositionCallback).
    • onPreeditChanged

      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 getPreeditString(Out, Out, Out) returns the empty string.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitPreeditChanged

      public void emitPreeditChanged()
      Emits the "preedit-changed" signal. See onPreeditChanged(IMContext.PreeditChangedCallback).
    • onPreeditEnd

      The ::preedit-end signal is emitted when a preediting sequence has been completed or canceled.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitPreeditEnd

      public void emitPreeditEnd()
      Emits the "preedit-end" signal. See onPreeditEnd(IMContext.PreeditEndCallback).
    • onPreeditStart

      The ::preedit-start signal is emitted when a new preediting sequence starts.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitPreeditStart

      public void emitPreeditStart()
      Emits the "preedit-start" signal. See onPreeditStart(IMContext.PreeditStartCallback).
    • onRetrieveSurrounding

      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 setSurrounding(String, int, int) method.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitRetrieveSurrounding

      public boolean emitRetrieveSurrounding()
      Emits the "retrieve-surrounding" signal. See onRetrieveSurrounding(IMContext.RetrieveSurroundingCallback).