]> Pileus Git - ~andy/gtk/blob - gtk/gtkimcontext.c
fix up some docs
[~andy/gtk] / gtk / gtkimcontext.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2000 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "gtkimcontext.h"
21 #include "gtksignal.h"
22
23 enum {
24   PREEDIT_START,
25   PREEDIT_END,
26   PREEDIT_CHANGED,
27   COMMIT,
28   LAST_SIGNAL
29 };
30
31 static guint im_context_signals[LAST_SIGNAL] = { 0 };
32
33 static void gtk_im_context_class_init (GtkIMContextClass *class);
34 static void gtk_im_context_init (GtkIMContext *im_context);
35
36 static void     gtk_im_context_real_get_preedit_string (GtkIMContext       *context,
37                                                         gchar             **str,
38                                                         PangoAttrList     **attrs,
39                                                         gint               *cursor_pos);
40 static gboolean gtk_im_context_real_filter_keypress    (GtkIMContext       *context,
41                                                         GdkEventKey        *event);
42
43 GtkType
44 gtk_im_context_get_type (void)
45 {
46   static GtkType im_context_type = 0;
47
48   if (!im_context_type)
49     {
50       static const GtkTypeInfo im_context_info =
51       {
52         "GtkIMContext",
53         sizeof (GtkIMContext),
54         sizeof (GtkIMContextClass),
55         (GtkClassInitFunc) gtk_im_context_class_init,
56         (GtkObjectInitFunc) gtk_im_context_init,
57         /* reserved_1 */ NULL,
58         /* reserved_2 */ NULL,
59         (GtkClassInitFunc) NULL,
60       };
61
62       im_context_type = gtk_type_unique (GTK_TYPE_OBJECT, &im_context_info);
63     }
64
65   return im_context_type;
66 }
67
68 static void
69 gtk_im_context_class_init (GtkIMContextClass *klass)
70 {
71   GtkObjectClass *object_class;
72   
73   object_class = (GtkObjectClass*) klass;
74
75   klass->get_preedit_string = gtk_im_context_real_get_preedit_string;
76   klass->filter_keypress = gtk_im_context_real_filter_keypress;
77
78   im_context_signals[PREEDIT_START] =
79     gtk_signal_new ("preedit_start",
80                     GTK_RUN_LAST,
81                     GTK_CLASS_TYPE (object_class),
82                     GTK_SIGNAL_OFFSET (GtkIMContextClass, preedit_start),
83                     gtk_marshal_VOID__VOID,
84                     GTK_TYPE_NONE, 0);
85   
86   im_context_signals[PREEDIT_END] =
87     gtk_signal_new ("preedit_end",
88                     GTK_RUN_LAST,
89                     GTK_CLASS_TYPE (object_class),
90                     GTK_SIGNAL_OFFSET (GtkIMContextClass, preedit_end),
91                     gtk_marshal_VOID__VOID,
92                     GTK_TYPE_NONE, 0);
93   
94   im_context_signals[PREEDIT_CHANGED] =
95     gtk_signal_new ("preedit_changed",
96                     GTK_RUN_LAST,
97                     GTK_CLASS_TYPE (object_class),
98                     GTK_SIGNAL_OFFSET (GtkIMContextClass, preedit_changed),
99                     gtk_marshal_VOID__VOID,
100                     GTK_TYPE_NONE, 0);
101   
102   im_context_signals[COMMIT] =
103     gtk_signal_new ("commit",
104                     GTK_RUN_LAST,
105                     GTK_CLASS_TYPE (object_class),
106                     GTK_SIGNAL_OFFSET (GtkIMContextClass, commit),
107                     gtk_marshal_VOID__STRING,
108                     GTK_TYPE_NONE, 1,
109                     GTK_TYPE_STRING);
110 }
111
112 static void
113 gtk_im_context_init (GtkIMContext *im_context)
114 {
115 }
116
117 static void
118 gtk_im_context_real_get_preedit_string (GtkIMContext       *context,
119                                         gchar             **str,
120                                         PangoAttrList     **attrs,
121                                         gint               *cursor_pos)
122 {
123   if (str)
124     *str = g_strdup ("");
125   if (attrs)
126     *attrs = pango_attr_list_new ();
127   if (cursor_pos)
128     *cursor_pos = 0;
129 }
130
131 static gboolean
132 gtk_im_context_real_filter_keypress (GtkIMContext       *context,
133                                      GdkEventKey        *event)
134 {
135   return FALSE;
136 }
137
138 /**
139  * gtk_im_context_set_client_window:
140  * @context: a #GtkIMContext
141  * @window:  the client window. This may be %NULL to indicate
142  *           that the previous client window no longer exists.
143  * 
144  * Set the client window for the input context; this is the
145  * #GdkWindow in which the input appears. This window is
146  * used in order to correctly position status windows, and may
147  * also be used for purposes internal to the input method.
148  **/
149 void
150 gtk_im_context_set_client_window (GtkIMContext *context,
151                                   GdkWindow    *window)
152 {
153   GtkIMContextClass *klass;
154   
155   g_return_if_fail (context != NULL);
156   g_return_if_fail (GTK_IS_IM_CONTEXT (context));
157
158   klass = GTK_IM_CONTEXT_GET_CLASS (context);
159   if (klass->set_client_window)
160     klass->set_client_window (context, window);
161 }
162
163 /**
164  * gtk_im_context_get_preedit_string:
165  * @context:    a #GtkIMContext
166  * @str:        location to store the retrieved string. The
167  *              string retrieved must be freed with g_free ().
168  * @attrs:      location to store the retrieved attribute list.
169  *              When you are done with this list, you must
170  *              unreference it with pango_attr_list_unref().
171  * @cursor_pos: location to store position of cursor (in bytes)
172  *              within the preedit string.  
173  * 
174  * Retrieve the current preedit string for the input context,
175  * and a list of attributes to apply to the string.
176  * This string should be displayed inserted at the insertion
177  * point.
178  **/
179 void
180 gtk_im_context_get_preedit_string (GtkIMContext   *context,
181                                    gchar         **str,
182                                    PangoAttrList **attrs,
183                                    gint           *cursor_pos)
184 {
185   GtkIMContextClass *klass;
186   
187   g_return_if_fail (context != NULL);
188   g_return_if_fail (GTK_IS_IM_CONTEXT (context));
189   
190   klass = GTK_IM_CONTEXT_GET_CLASS (context);
191   klass->get_preedit_string (context, str, attrs, cursor_pos);
192   g_return_if_fail (str == NULL || g_utf8_validate (*str, -1, NULL));
193 }
194
195 /**
196  * gtk_im_context_filter_keypress:
197  * @context: a #GtkIMContext
198  * @event: the key event
199  * 
200  * Allow an input method to internally handle a key press event.
201  * if this function returns %TRUE, then no further processing
202  * should be done for this keystroke.
203  * 
204  * Return value: %TRUE if the input method handled the keystroke.
205  *
206  **/
207 gboolean
208 gtk_im_context_filter_keypress (GtkIMContext *context,
209                                 GdkEventKey  *key)
210 {
211   GtkIMContextClass *klass;
212   
213   g_return_val_if_fail (context != NULL, FALSE);
214   g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
215   g_return_val_if_fail (key != NULL, FALSE);
216
217   klass = GTK_IM_CONTEXT_GET_CLASS (context);
218   return klass->filter_keypress (context, key);
219 }
220
221 /**
222  * gtk_im_context_focus_in:
223  * @context: a #GtkIMContext
224  *
225  * Notify the input method that the widget to which this
226  * input context corresponds has lost gained. The input method
227  * may, for example, change the displayed feedback to reflect
228  * this change.
229  **/
230 void
231 gtk_im_context_focus_in (GtkIMContext   *context)
232 {
233   GtkIMContextClass *klass;
234   
235   g_return_if_fail (context != NULL);
236   g_return_if_fail (GTK_IS_IM_CONTEXT (context));
237   
238   klass = GTK_IM_CONTEXT_GET_CLASS (context);
239   if (klass->focus_in)
240     klass->focus_in (context);
241 }
242
243 /**
244  * gtk_im_context_focus_out:
245  * @context: a #GtkIMContext
246  *
247  * Notify the input method that the widget to which this
248  * input context corresponds has lost focus. The input method
249  * may, for example, change the displayed feedback or reset the contexts
250  * state to reflect this change.
251  **/
252 void
253 gtk_im_context_focus_out (GtkIMContext   *context)
254 {
255   GtkIMContextClass *klass;
256   
257   g_return_if_fail (context != NULL);
258   g_return_if_fail (GTK_IS_IM_CONTEXT (context));
259
260   klass = GTK_IM_CONTEXT_GET_CLASS (context);
261   if (klass->focus_out)
262     klass->focus_out (context);
263 }
264
265 /**
266  * gtk_im_context_reset:
267  * @context: a #GtkIMContext
268  *
269  * Notify the input method that a change such as a change in cursor
270  * position has been made. This will typically cause the input
271  * method to clear the preedit state.
272  **/
273 void
274 gtk_im_context_reset (GtkIMContext   *context)
275 {
276   GtkIMContextClass *klass;
277   
278   g_return_if_fail (context != NULL);
279   g_return_if_fail (GTK_IS_IM_CONTEXT (context));
280
281   klass = GTK_IM_CONTEXT_GET_CLASS (context);
282   if (klass->reset)
283     klass->reset (context);
284 }
285
286
287 /**
288  * gtk_im_context_set_cursor_location:
289  * @context: a #GtkIMContext
290  * @area: new location
291  *
292  * Notify the input method that a change in cursor 
293  * position has been made.
294  **/
295 void
296 gtk_im_context_set_cursor_location (GtkIMContext *context,
297                                     GdkRectangle *area)
298 {
299   GtkIMContextClass *klass;
300   
301   g_return_if_fail (context != NULL);
302   g_return_if_fail (GTK_IS_IM_CONTEXT (context));
303
304   klass = GTK_IM_CONTEXT_GET_CLASS (context);
305   if (klass->set_cursor_location)
306     klass->set_cursor_location (context, area);
307 }
308