GDK Reference Manual
<<< Previous Page | Home | Up | Next Page >>>

# Input Methods

## Name

Input Methods -- support for internationalized text input.

## Synopsis

    #include <gdk/gdk.h>

    enum        GdkIMStyle;
    gint        gdk_im_ready                    (void);
    GdkIMStyle  gdk_im_decide_style             (GdkIMStyle supported_style);
    GdkIMStyle  gdk_im_set_best_style           (GdkIMStyle best_allowed_style);

    void        gdk_im_begin                    (GdkIC *ic,
                                                 GdkWindow *window);
    void        gdk_im_end                      (void);

## Description

Input Methods provide a way for complex character sets to be used in GTK+. Languages such as Chinese, Japanese, and Korean (often abbreviated to CJK) use a large number of ideographs, making it impossible to support all characters with a simple keyboard. Instead, text is usually pre-edited using a phonetic alphabet and then composed to form the ideographs.

GTK+ makes use of the input method mechanism provided by the X Windows platform. When a GTK+ application is started, it opens a connection to the input method appropriate for the current locale (if any).

Widgets which handle textual input, such as GtkEntry, need to do a number of things to support internationalized text input:

When the widget is realized:

  Check if an input method is being used with gdk_im_ready(). If it is, create a new Input Context using gdk_ic_new(). Find out which events the Input Context needs to receive with gdk_ic_get_events(), and make sure that the widget's window receives these events using gdk_window_set_events().

When the widget's size, state or cursor position changes:

  Update the appropriate Input Context attributes using gdk_ic_set_attr().

When the keyboard focus enters or leaves the widget:

  Call gdk_im_begin() or gdk_im_end() to start or finish editing the text.

When the widget receives a key_press event:

  The `string` and `length` fields of the GdkEventKey struct should be used to insert the composed text into the widget.

When the widget is unrealized:

  Destroy the Input Context.

See the XLib reference manual for more detailed information on input methods, and the GtkEntry and GtkText widgets for some example code.

## Details

### enum GdkIMStyle

    typedef enum			/*< flags >*/
    {
      GDK_IM_PREEDIT_AREA	   = 0x0001,
      GDK_IM_PREEDIT_CALLBACKS = 0x0002,
      GDK_IM_PREEDIT_POSITION  = 0x0004,
      GDK_IM_PREEDIT_NOTHING   = 0x0008,
      GDK_IM_PREEDIT_NONE	   = 0x0010,
      GDK_IM_PREEDIT_MASK      = 0x001f,

      GDK_IM_STATUS_AREA	   = 0x0100,
      GDK_IM_STATUS_CALLBACKS  = 0x0200,
      GDK_IM_STATUS_NOTHING	   = 0x0400,
      GDK_IM_STATUS_NONE	   = 0x0800,
      GDK_IM_STATUS_MASK	   = 0x0f00
    } GdkIMStyle;

A set of bit-flags used to specify the input method styles which are supported or which are currently in use. The flags can be divided into 2 groups, the pre-edit flags and the status flags.

The pre-edit flags specify how pre-editing data is displayed. For example, this could display the text being typed in the phonetic alphabet before it is composed and inserted as an ideograph.

The status flags specify how status information is displayed. The status information can be thought of as an extension of the standard keyboard mode indicators, such as the Caps Lock indicator.

> Note: The `GDK_IM_PREEDIT_CALLBACKS` and `GDK_IM_STATUS_CALLBACKS` styles are not currently supported in GTK+.

`GDK_IM_PREEDIT_AREA` | The application provides the input method with an area in which to perform off-the-spot pre-editing.
`GDK_IM_PREEDIT_CALLBACKS` | The application registers a number of callback functions which are used to display pre-editing data.
`GDK_IM_PREEDIT_POSITION` | The application provides the input method with the position of the insertion cursor, for over-the-spot pre-editing. The input method creates its own window over the widget to display the pre-editing data.
`GDK_IM_PREEDIT_NOTHING` | The input method uses the root X window to perform pre-editing, so the application does not need to do anything.
`GDK_IM_PREEDIT_NONE` | No pre-editing is done by the input method, or no pre-editing data needs to be displayed.
`GDK_IM_PREEDIT_MASK` | A bit-mask containing all the pre-edit flags.
`GDK_IM_STATUS_AREA` | The application provides the input method with an area in which to display status information.
`GDK_IM_STATUS_CALLBACKS` | The applications registers a number of callback functions which are used to display status information.
`GDK_IM_STATUS_NOTHING` | The input method uses the root X window to display status information, so the application does not need to do anything.
`GDK_IM_STATUS_NONE` | The input method does not display status information.
`GDK_IM_STATUS_MASK` | A bit-mask containing all the status flags.

### gdk_im_ready ()

    gint        gdk_im_ready                    (void);

Checks if an input method is to be used for the current locale. If GTK+ has been compiled without support for input methods, or the current locale doesn't need an input method, then this will return FALSE.

Returns : | TRUE if an input method is available and should be used.

### gdk_im_decide_style ()

    GdkIMStyle  gdk_im_decide_style             (GdkIMStyle supported_style);

Decides which input method style should be used, by comparing the styles given in `supported_style` with those of the available input method.

`supported_style` : | styles which are supported by the widget.
Returns : | the best style in `supported_style` that is also supported by the available input method.

### gdk_im_set_best_style ()

    GdkIMStyle  gdk_im_set_best_style           (GdkIMStyle best_allowed_style);

Sets the best pre-edit and/or status style which should be used. This will affect the style chosen in gdk_im_decide_style().

The order of the pre-edit styles is (from worst to best): `GDK_IM_PREEDIT_NONE`, `GDK_IM_PREEDIT_NOTHING`, `GDK_IM_PREEDIT_AREA`, `GDK_IM_PREEDIT_POSITION`, `GDK_IM_PREEDIT_CALLBACKS`. The order of the status styles is: `GDK_IM_STATUS_NONE`, `GDK_IM_STATUS_NOTHING`, `GDK_IM_STATUS_AREA`, `GDK_IM_STATUS_CALLBACKS`.

So, for example, to set the best allowed pre-edit style to `GDK_IM_PREEDIT_AREA` you would do this:

      gdk_im_set_best_style (GDK_IM_PREEDIT_AREA);

Or to set the best allowed pre-edit style to `GDK_IM_PREEDIT_POSITION` and the best allowed status style to `GDK_IM_STATUS_NOTHING` you can do this:

      gdk_im_set_best_style (GDK_IM_PREEDIT_POSITION | GDK_IM_STATUS_NOTHING);

`best_allowed_style` : | a bit-mask with the best pre-edit style and/or the best status style to use. If 0 is used, then the current bit-mask of all allowed styles is returned.
Returns : | a bit-mask of all the styles allowed.

### gdk_im_begin ()

    void        gdk_im_begin                    (GdkIC *ic,
                                                 GdkWindow *window);

Starts editing, using the given input context and GdkWindow. This should be called when the widget receives the input focus, typically in the widget's focus_in_event method.

`ic` : | a GdkIC.
`window` : | the GdkWindow which will be receiving the key press events.

### gdk_im_end ()

    void        gdk_im_end                      (void);

Stops editing using the input method. This should be called when the widget loses the input focus, typically in the widget's focus_out_event method.

## See Also

Input Contexts

  Used for each widget that handles internationalized text input using the global input method.

<<< Previous Page | Home | Up | Next Page >>>
Key Values | Input Contexts