Debian Codegraph @

extras/immodules/client-gtk/gtk4/scim-bridge-client-imcontext-gtk.c

raw symbols SCIP
1 /*
2 * SCIM Bridge
3 *
4 * Copyright (c) 2006 Ryo Dairiki <[email protected]>
5 *
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation and
10 * appearing in the file LICENSE.LGPL included in the package of this file.
11 * You can also redistribute it and/or modify it under the terms of
12 * the GNU General Public License as published by the Free Software Foundation and
13 * appearing in the file LICENSE.GPL included in the package of this file.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 */
19
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <gtk/gtk.h>
27
28 #include <gdk/gdk.h>
29
30 #ifdef GDK_WINDOWING_X11
31 #include <gdk/x11/gdkx.h>
32 #include <X11/Xlib.h>
33 #endif
34
35 #include "scim-bridge-attribute.h"
36 #include "scim-bridge-client.h"
37 #include "scim-bridge-client-imcontext-gtk.h"
38 #include "scim-bridge-client-key-event-utility-gtk.h"
39 #include "scim-bridge-imcontext.h"
40 #include "scim-bridge-messenger.h"
41 #include "scim-bridge-output.h"
42 #include "scim-bridge-string.h"
43
44 /* Typedef */
45 struct _ScimBridgeClientIMContext
46 {
47 GtkIMContext parent;
48 GtkIMContext *slave;
49 boolean slave_preedit;
50
51 scim_bridge_imcontext_id_t id;
52
53 char *preedit_string;
54 size_t preedit_string_capacity;
55
56 PangoAttrList *preedit_attributes;
57
58 unsigned int preedit_cursor_position;
59 boolean preedit_cursor_flicking;
60
61 boolean preedit_shown;
62 boolean preedit_started;
63
64 char *commit_string;
65 size_t commit_string_capacity;
66
67 boolean enabled;
68
69 GtkWidget *client_widget;
70
71 int cursor_x;
72 int cursor_y;
73 int window_x;
74 int window_y;
75 };
76
77
78 typedef struct {
79 guint16 red;
80 guint16 green;
81 guint16 blue;
82 } Color;
83
84
85 /* Private variables */
86 static Color preedit_normal_background;
87 static Color preedit_normal_foreground;
88 static Color preedit_active_background;
89 static Color preedit_active_foreground;
90
91 static GType class_type = 0;
92 static GObjectClass *root_klass = NULL;
93
94 static ScimBridgeClientIMContext *focused_imcontext = NULL;
95 static GtkWidget *focused_widget = NULL;
96
97 /* Class functions */
98 static void scim_bridge_client_imcontext_class_initialize (ScimBridgeClientIMContextClass *klass, gpointer *klass_data);
99
100 static void scim_bridge_client_imcontext_initialize (ScimBridgeClientIMContext *context, ScimBridgeClientIMContextClass *klass);
101 static void scim_bridge_client_imcontext_finalize (GObject *object);
102
103 static gboolean scim_bridge_client_imcontext_filter_key_event (GtkIMContext *context, GdkEvent *event);
104 static void scim_bridge_client_imcontext_reset (GtkIMContext *context);
105 static void scim_bridge_client_imcontext_get_preedit_string (GtkIMContext *context, gchar **str, PangoAttrList **attrs, gint *cursor_pos);
106 static void scim_bridge_client_imcontext_set_preedit_enabled (GtkIMContext *context, gboolean enabled);
107
108 static void scim_bridge_client_imcontext_set_client_widget (GtkIMContext *context, GtkWidget *window);
109 static void scim_bridge_client_imcontext_focus_in (GtkIMContext *context);
110 static void scim_bridge_client_imcontext_focus_out (GtkIMContext *context);
111 static void scim_bridge_client_imcontext_set_cursor_location (GtkIMContext *context, GdkRectangle *area);
112
113 /* slave callbacks */
114 static void gtk_im_slave_commit_cb (GtkIMContext *context, const char *str, ScimBridgeClientIMContext *imcontext);
115 static void gtk_im_slave_preedit_changed_cb (GtkIMContext *context, ScimBridgeClientIMContext *imcontext);
116 static void gtk_im_slave_preedit_start_cb (GtkIMContext *context, ScimBridgeClientIMContext *imcontext);
117 static void gtk_im_slave_preedit_end_cb (GtkIMContext *context, ScimBridgeClientIMContext *imcontext);
118
119 static retval_t filter_key_event (ScimBridgeClientIMContext *imcontext, GdkEvent *event, boolean *consumed)
120 {
121 scim_bridge_pdebugln (5, "filter_key_event ()");
122
123 if (focused_imcontext != imcontext)
124 scim_bridge_client_imcontext_focus_in (GTK_IM_CONTEXT (imcontext));
125 //focused_widget = gtk_get_event_widget (event);
126 focused_widget = imcontext->client_widget;
127
128 if (scim_bridge_client_is_messenger_opened ()) {
129 ScimBridgeKeyEvent *bridge_key_event = scim_bridge_alloc_key_event ();
130 scim_bridge_key_event_gdk_to_bridge (bridge_key_event, imcontext->client_widget, event);
131
132 *consumed = FALSE;
133 const retval_t retval_error = scim_bridge_client_handle_key_event (imcontext, bridge_key_event, consumed);
134 scim_bridge_free_key_event (bridge_key_event);
135
136 if (retval_error) {
137 scim_bridge_perrorln ("An IOException at filter_key_event ()");
138 } else {
139 return RETVAL_SUCCEEDED;
140 }
141 }
142
143 return RETVAL_FAILED;
144 }
145
146
147 static retval_t set_cursor_location (ScimBridgeClientIMContext *imcontext, int window_x, int window_y, int cursor_x, int cursor_y)
148 {
149 scim_bridge_pdebugln (5, "set_cursor_location ()");
150
151 if (imcontext->window_x == window_x && imcontext->window_y == window_y && imcontext->cursor_x == cursor_x && imcontext->cursor_y == cursor_y) {
152 return RETVAL_SUCCEEDED;
153 } else {
154 imcontext->cursor_x = cursor_x;
155 imcontext->cursor_y = cursor_y;
156 imcontext->window_x = window_x;
157 imcontext->window_y = window_y;
158
159 scim_bridge_pdebugln (3, "The cursor location is changed: x = %d + %d\ty = %d + %d", imcontext->window_x, imcontext->cursor_x, imcontext->window_y, imcontext->cursor_y);
160
161 if (scim_bridge_client_is_messenger_opened ()) {
162 if (scim_bridge_client_set_cursor_location (imcontext, imcontext->window_x + imcontext->cursor_x, imcontext->window_y + imcontext->cursor_y)) {
163 scim_bridge_perrorln ("An IOException occurred at set_cursor_location ()");
164 return RETVAL_FAILED;
165 } else {
166 return RETVAL_SUCCEEDED;
167 }
168 }
169 }
170
171 return RETVAL_FAILED;
172 }
173
174
175 static void widget_get_origin(GtkWidget *widget, int *x, int *y)
176 {
177 double widget_x, widget_y;
178 GtkRoot *root = gtk_widget_get_root(widget);
179 gtk_widget_translate_coordinates(widget, GTK_WIDGET(root), 0, 0, &widget_x, &widget_y);
180
181 GdkSurface *surface = gtk_native_get_surface (GTK_NATIVE(root));
182 Window wiget_win = gdk_x11_surface_get_xid (surface);
183 XWindowAttributes attr;
184 Display *display = gdk_x11_display_get_xdisplay (gdk_surface_get_display (surface));
185 XGetWindowAttributes(display, wiget_win, &attr);
186
187 Window child_win = None;
188 XTranslateCoordinates(display, wiget_win, attr.root, widget_x, widget_y, x, y, &child_win);
189 }
190
191
192 static gboolean key_snooper (GtkWidget *widget, GdkEvent *event, gpointer data)
193 {
194 scim_bridge_pdebugln (7, "key_snooper ()");
195
196 GdkEventType type = gdk_event_get_event_type(event);
197 if (focused_imcontext && scim_bridge_client_is_messenger_opened () &&
198 (type == GDK_KEY_PRESS || type == GDK_KEY_RELEASE)) { //&&
199 //!(event->send_event & SEND_EVENT_MASK)) {
200 if (focused_imcontext->client_widget) {
201 // TODO get cliet_wiget's origin
202 int new_widget_x = 0;
203 int new_widget_y = 0;
204 widget_get_origin (focused_imcontext->client_widget, &new_widget_x, &new_widget_y);
205
206 if (focused_imcontext->window_x != new_widget_x || focused_imcontext->window_y != new_widget_y) {
207 scim_bridge_pdebugln (1,
208 "The cursor location is changed: x = %d + %d\ty = %d + %d",
209 new_widget_x, focused_imcontext->cursor_x, new_widget_y, focused_imcontext->cursor_y);
210
211 if (set_cursor_location (focused_imcontext, new_widget_x, new_widget_y, focused_imcontext->cursor_x, focused_imcontext->cursor_y)) {
212 scim_bridge_perrorln ("An IOException at key_snooper ()");
213 return FALSE;
214 }
215 }
216 }
217
218 boolean consumed = FALSE;
219 if (filter_key_event (focused_imcontext, event, &consumed)) {
220 scim_bridge_perrorln ("An IOException at key_snooper ()");
221 return FALSE;
222 } else {
223 if (consumed) {
224 g_signal_emit_by_name (focused_imcontext, "preedit-changed");
225 return TRUE;
226 }
227 }
228 }
229
230 return FALSE;
231 }
232
233
234 static boolean is_precise_cursor_enabled ()
235 {
236 static boolean first_time = TRUE;
237 static boolean precise_cursor_enabled = FALSE;
238
239 if (first_time) {
240 char *env_precise_cursor_enabled = getenv ("SCIM_BRIDGE_PRECISE_CURSOR_ENABLED");
241 if (env_precise_cursor_enabled != NULL) scim_bridge_string_to_boolean (&precise_cursor_enabled, env_precise_cursor_enabled);
242 first_time = FALSE;
243 }
244
245 return precise_cursor_enabled;
246 }
247
248 /* Bindings */
249 void scim_bridge_client_imcontext_set_id (ScimBridgeClientIMContext *imcontext, scim_bridge_imcontext_id_t new_id)
250 {
251 imcontext->id = new_id;
252 }
253
254
255 scim_bridge_imcontext_id_t scim_bridge_client_imcontext_get_id (const ScimBridgeClientIMContext *imcontext)
256 {
257 return imcontext->id;
258 }
259
260
261 void scim_bridge_client_imcontext_set_preedit_string (ScimBridgeClientIMContext *imcontext, const char *preedit_string)
262 {
263 if (imcontext->preedit_string != NULL && preedit_string != NULL && !strcmp (imcontext->preedit_string, preedit_string))
264 return;
265
266 size_t preedit_string_length;
267 if (preedit_string != NULL) {
268 preedit_string_length = strlen (preedit_string);
269 } else {
270 preedit_string_length = 0;
271 }
272 if (imcontext->preedit_string_capacity <= preedit_string_length) {
273 imcontext->preedit_string_capacity = preedit_string_length;
274 free (imcontext->preedit_string);
275 imcontext->preedit_string = malloc (sizeof (char) * (imcontext->preedit_string_capacity + 1));
276 }
277 if (preedit_string_length > 0) {
278 strcpy (imcontext->preedit_string, preedit_string);
279 } else {
280 imcontext->preedit_string[0] = '\0';
281 }
282 }
283
284
285 void scim_bridge_client_imcontext_set_preedit_shown (ScimBridgeClientIMContext *imcontext, boolean preedit_shown)
286 {
287 imcontext->preedit_shown = preedit_shown;
288 if (!preedit_shown) {
289 free (imcontext->preedit_string);
290 imcontext->preedit_string = malloc (sizeof (char));
291 imcontext->preedit_string[0] = '\0';
292 imcontext->preedit_string_capacity = 0;
293 imcontext->preedit_cursor_position = 0;
294 if (imcontext->preedit_attributes != NULL) {
295 pango_attr_list_unref (imcontext->preedit_attributes);
296 imcontext->preedit_attributes = NULL;
297 }
298 }
299 }
300
301 void scim_bridge_client_imcontext_set_preedit_cursor_position (ScimBridgeClientIMContext *imcontext, int cursor_position)
302 {
303 imcontext->preedit_cursor_position = cursor_position;
304 }
305
306
307 void scim_bridge_client_imcontext_set_preedit_attributes (ScimBridgeClientIMContext *imcontext, ScimBridgeAttribute** const preedit_attributes, int attribute_count)
308 {
309 if (imcontext->preedit_attributes != NULL)
310 pango_attr_list_unref (imcontext->preedit_attributes);
311
312 imcontext->preedit_attributes = pango_attr_list_new ();
313
314 int preedit_string_length = 0;
315 int preedit_wstring_length = 0;
316 if (imcontext->preedit_string != NULL) {
317 preedit_string_length = strlen (imcontext->preedit_string);
318 preedit_wstring_length = g_utf8_strlen (imcontext->preedit_string, -1);
319 }
320
321 boolean *has_attribute = alloca (sizeof (boolean) * preedit_string_length);
322 int i;
323 for (i = 0; i < preedit_string_length; ++i) {
324 has_attribute[i] = FALSE;
325 }
326
327 for (i = 0; i < attribute_count; ++i) {
328 const ScimBridgeAttribute *attr = preedit_attributes[i];
329 const int begin_pos = scim_bridge_attribute_get_begin (attr);
330 const int end_pos = scim_bridge_attribute_get_end (attr);
331
332 if (begin_pos <= end_pos && 0 <= begin_pos && end_pos <= preedit_wstring_length) {
333 const int start_index = g_utf8_offset_to_pointer (imcontext->preedit_string, begin_pos) - imcontext->preedit_string;
334 const int end_index = g_utf8_offset_to_pointer (imcontext->preedit_string, end_pos) - imcontext->preedit_string;
335
336 const scim_bridge_attribute_type_t attr_type = scim_bridge_attribute_get_type (attr);
337 const scim_bridge_attribute_value_t attr_value = scim_bridge_attribute_get_value (attr);
338
339 boolean valid_attribute = FALSE;
340 if (attr_type == ATTRIBUTE_DECORATE) {
341 if (attr_value == SCIM_BRIDGE_ATTRIBUTE_DECORATE_UNDERLINE) {
342 valid_attribute = TRUE;
343
344 PangoAttribute *pango_attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
345 pango_attr->start_index = start_index;
346 pango_attr->end_index = end_index;
347 pango_attr_list_insert (imcontext->preedit_attributes, pango_attr);
348 } else if (attr_value == SCIM_BRIDGE_ATTRIBUTE_DECORATE_REVERSE) {
349 valid_attribute = TRUE;
350
351 PangoAttribute *pango_attr0 = pango_attr_foreground_new (preedit_normal_background.red, preedit_normal_background.green, preedit_normal_background.blue);
352 pango_attr0->start_index = start_index;
353 pango_attr0->end_index = end_index;
354 pango_attr_list_insert (imcontext->preedit_attributes, pango_attr0);
355
356 PangoAttribute *pango_attr1 = pango_attr_background_new (preedit_normal_foreground.red, preedit_normal_foreground.green, preedit_normal_foreground.blue);
357 pango_attr1->start_index = start_index;
358 pango_attr1->end_index = end_index;
359 pango_attr_list_insert (imcontext->preedit_attributes, pango_attr1);
360 } else if (attr_value == SCIM_BRIDGE_ATTRIBUTE_DECORATE_HIGHLIGHT) {
361 valid_attribute = TRUE;
362
363 PangoAttribute *pango_attr0 = pango_attr_foreground_new (preedit_active_foreground.red, preedit_active_foreground.green, preedit_active_foreground.blue);
364 pango_attr0->start_index = start_index;
365 pango_attr0->end_index = end_index;
366 pango_attr_list_insert (imcontext->preedit_attributes, pango_attr0);
367
368 PangoAttribute *pango_attr1 = pango_attr_background_new (preedit_active_background.red, preedit_active_background.green, preedit_active_background.blue);
369 pango_attr1->start_index = start_index;
370 pango_attr1->end_index = end_index;
371 pango_attr_list_insert (imcontext->preedit_attributes, pango_attr1);
372 } else {
373 scim_bridge_perrorln ("Unknown preedit decoration!");
374 }
375 } else if (attr_type == ATTRIBUTE_FOREGROUND) {
376 valid_attribute = TRUE;
377
378 const unsigned int red = scim_bridge_attribute_get_red (attr) * 256;
379 const unsigned int green = scim_bridge_attribute_get_green (attr) * 256;
380 const unsigned int blue = scim_bridge_attribute_get_blue (attr) * 256;
381
382 PangoAttribute *pango_attr = pango_attr_foreground_new (red, green, blue);
383 pango_attr->start_index = start_index;
384 pango_attr->end_index = end_index;
385 pango_attr_list_insert (imcontext->preedit_attributes, pango_attr);
386 } else if (attr_type == ATTRIBUTE_BACKGROUND) {
387 valid_attribute = TRUE;
388
389 const unsigned int red = scim_bridge_attribute_get_red (attr) * 256;
390 const unsigned int green = scim_bridge_attribute_get_green (attr) * 256;
391 const unsigned int blue = scim_bridge_attribute_get_blue (attr) * 256;
392
393 PangoAttribute *pango_attr = pango_attr_background_new (red, green, blue);
394 pango_attr->start_index = start_index;
395 pango_attr->end_index = end_index;
396 pango_attr_list_insert (imcontext->preedit_attributes, pango_attr);
397 }
398
399 if (valid_attribute) {
400 int j;
401 for (j = start_index; j < end_index; ++j) {
402 has_attribute[j] = TRUE;
403 }
404 }
405 }
406
407 }
408
409 // Add underlines for the all characters without attributes.
410 for (i = 0; i < preedit_string_length; ++i) {
411 if (has_attribute[i] == FALSE) {
412 PangoAttribute *pango_attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
413 pango_attr->start_index = i;
414 for (; i < preedit_string_length && has_attribute[i] == FALSE; ++i);
415 pango_attr->end_index = i;
416 pango_attr_list_insert (imcontext->preedit_attributes, pango_attr);
417 }
418 }
419 }
420
421
422 void scim_bridge_client_imcontext_update_preedit (ScimBridgeClientIMContext *imcontext)
423 {
424 if (imcontext->preedit_shown && !imcontext->preedit_started) {
425 g_signal_emit_by_name ((ScimBridgeClientIMContext*) imcontext, "preedit-start");
426 imcontext->preedit_started = TRUE;
427 }
428
429 if (is_precise_cursor_enabled ()) {
430 const size_t old_cursor_position = imcontext->preedit_cursor_position;
431 imcontext->preedit_cursor_position = 0;
432 if (imcontext->preedit_string != NULL)
433 imcontext->preedit_cursor_position = g_utf8_strlen (imcontext->preedit_string, -1);
434
435 imcontext->preedit_cursor_flicking = TRUE;
436 g_signal_emit_by_name ((ScimBridgeClientIMContext*) imcontext, "preedit-changed");
437
438 imcontext->preedit_cursor_position = old_cursor_position;
439 imcontext->preedit_cursor_flicking = FALSE;
440 }
441
442 g_signal_emit_by_name ((ScimBridgeClientIMContext*) imcontext, "preedit-changed");
443
444 if (!imcontext->preedit_shown && imcontext->preedit_started) {
445 g_signal_emit_by_name ((ScimBridgeClientIMContext*) imcontext, "preedit-end");
446 imcontext->preedit_started = FALSE;
447 }
448 }
449
450
451 void scim_bridge_client_imcontext_set_commit_string (ScimBridgeClientIMContext *imcontext, const char *commit_string)
452 {
453 size_t commit_string_length;
454 if (commit_string != NULL) {
455 commit_string_length = strlen (commit_string);
456 } else {
457 commit_string_length = 0;
458 }
459
460 if (imcontext->commit_string_capacity <= commit_string_length) {
461 imcontext->commit_string_capacity = commit_string_length;
462 free (imcontext->commit_string);
463 imcontext->commit_string = malloc (sizeof (char) * (imcontext->commit_string_capacity + 1));
464 }
465 if (commit_string_length > 0) {
466 strcpy (imcontext->commit_string, commit_string);
467 } else {
468 imcontext->commit_string[0] = '\0';
469 }
470 }
471
472
473 void scim_bridge_client_imcontext_commit (ScimBridgeClientIMContext *imcontext)
474 {
475 g_signal_emit_by_name ((ScimBridgeClientIMContext*) imcontext, "commit", imcontext->commit_string);
476 }
477
478
479 void scim_bridge_client_imcontext_beep (ScimBridgeClientIMContext *imcontext)
480 {
481 gtk_widget_error_bell(imcontext->client_widget);
482 }
483
484
485 boolean scim_bridge_client_imcontext_get_surrounding_text (ScimBridgeClientIMContext *imcontext, int before_max, int after_max, char **string, int *cursor_position)
486 {
487 char *str;
488 int cur_pos_in_utf8, an_pos_in_utf8;
489
490 if (gtk_im_context_get_surrounding_with_selection (GTK_IM_CONTEXT (imcontext),
491 &str,
492 &cur_pos_in_utf8,
493 &an_pos_in_utf8)) {
494 const size_t fetch_wstr_length = g_utf8_strlen (str, -1);
495 const size_t after_wstr_length = g_utf8_strlen (str + cur_pos_in_utf8, an_pos_in_utf8);
496 const size_t before_wstr_length = fetch_wstr_length - after_wstr_length;
497
498 size_t before_copy_wstr_length;
499 size_t after_copy_wstr_length;
500 if (after_wstr_length > after_max) {
501 after_copy_wstr_length = after_max;
502 } else {
503 after_copy_wstr_length = after_wstr_length;
504 }
505 if (before_wstr_length > before_max) {
506 before_copy_wstr_length = before_max;
507 } else {
508 before_copy_wstr_length = before_wstr_length;
509 }
510
511 const size_t begin_wstr_index = before_wstr_length - before_copy_wstr_length;
512 const size_t end_wstr_index = fetch_wstr_length - (after_wstr_length - after_copy_wstr_length);
513
514 char* begin_str_ptr = g_utf8_offset_to_pointer (str, begin_wstr_index);
515 char* end_str_ptr = g_utf8_offset_to_pointer (str, end_wstr_index);
516 size_t str_length = end_str_ptr - begin_str_ptr;
517
518 *string = malloc (sizeof (char) * (str_length + 1));
519 strncpy (*string, begin_str_ptr, str_length);
520 (*string)[str_length] = '\0';
521 *cursor_position = before_copy_wstr_length;
522
523 g_free (str);
524 return TRUE;
525 } else {
526 *string = NULL;
527
528 return FALSE;
529 }
530 }
531
532
533 boolean scim_bridge_client_imcontext_delete_surrounding_text (ScimBridgeClientIMContext *imcontext, int offset, int length)
534 {
535 boolean retval = gtk_im_context_delete_surrounding (GTK_IM_CONTEXT (imcontext), offset, length);
536 return retval;
537 }
538
539
540 boolean scim_bridge_client_imcontext_replace_surrounding_text (ScimBridgeClientIMContext *imcontext, int cursor_position, const char *string)
541 {
542 gtk_im_context_set_surrounding_with_selection (GTK_IM_CONTEXT (imcontext),
543 string,
544 -1,
545 cursor_position,
546 cursor_position);
547 return TRUE;
548 }
549
550 void scim_bridge_client_imcontext_forward_key_event (ScimBridgeClientIMContext *imcontext, const ScimBridgeKeyEvent *key_event)
551 {
552 scim_bridge_pdebugln (7, "scim_bridge_client_imcontext_forward_key_event: NOT IMPLEMENTED YET");
553 }
554
555
556 void scim_bridge_client_imcontext_imengine_status_changed (ScimBridgeClientIMContext *imcontext, boolean enabled)
557 {
558 if (imcontext->preedit_shown) {
559 if (imcontext->enabled) {
560 scim_bridge_client_imcontext_set_preedit_shown (imcontext, FALSE);
561 scim_bridge_client_imcontext_update_preedit (imcontext);
562 }
563 }
564 imcontext->enabled = enabled;
565 }
566
567
568 /* Class Implementations */
569 void scim_bridge_client_imcontext_static_initialize ()
570 {
571 GdkRGBA color;
572 g_warn_if_fail(gdk_rgba_parse (&color, "gray92"));
573 preedit_normal_background = (Color){color.red * 65535, color.green * 65535, color.blue * 65535};
574 g_warn_if_fail(gdk_rgba_parse (&color, "black"));
575 preedit_normal_foreground = (Color){color.red * 65535, color.green * 65535, color.blue * 65535};
576 g_warn_if_fail(gdk_rgba_parse (&color, "light blue"));
577 preedit_active_background = (Color){color.red * 65535, color.green * 65535, color.blue * 65535};
578 g_warn_if_fail(gdk_rgba_parse (&color, "black"));
579 preedit_active_foreground = (Color){color.red * 65535, color.green * 65535, color.blue * 65535};
580
581 focused_imcontext = NULL;
582 }
583
584
585 void scim_bridge_client_imcontext_static_finalize ()
586 {
587 focused_imcontext = NULL;
588 }
589
590
591 void scim_bridge_client_imcontext_connection_opened ()
592 {
593 }
594
595
596 void scim_bridge_client_imcontext_connection_closed ()
597 {
598 if (focused_imcontext != NULL) scim_bridge_client_imcontext_set_preedit_shown (focused_imcontext, FALSE);
599 }
600
601
602 void scim_bridge_client_imcontext_class_initialize (ScimBridgeClientIMContextClass *klass, gpointer *klass_data)
603 {
604 root_klass = (GObjectClass *) g_type_class_peek_parent (klass);
605
606 GtkIMContextClass *gtk_im_klass = GTK_IM_CONTEXT_CLASS (klass);
607 gtk_im_klass->set_client_widget = scim_bridge_client_imcontext_set_client_widget;
608 gtk_im_klass->filter_keypress = scim_bridge_client_imcontext_filter_key_event;
609 gtk_im_klass->reset = scim_bridge_client_imcontext_reset;
610 gtk_im_klass->get_preedit_string = scim_bridge_client_imcontext_get_preedit_string;
611 gtk_im_klass->focus_in = scim_bridge_client_imcontext_focus_in;
612 gtk_im_klass->focus_out = scim_bridge_client_imcontext_focus_out;
613 gtk_im_klass->set_cursor_location = scim_bridge_client_imcontext_set_cursor_location;
614 gtk_im_klass->set_use_preedit = scim_bridge_client_imcontext_set_preedit_enabled;
615
616 GObjectClass *gobject_klass = G_OBJECT_CLASS (klass);
617 gobject_klass->finalize = scim_bridge_client_imcontext_finalize;
618 }
619
620
621 GType scim_bridge_client_imcontext_get_type ()
622 {
623 return class_type;
624 }
625
626
627 void scim_bridge_client_imcontext_register_type (GIOModule *io_module)
628 {
629 scim_bridge_pdebugln (2, "scim_bridge_client_imcontext_register_type ()");
630
631 static const GTypeInfo klass_info = {
632 sizeof (ScimBridgeClientIMContextClass),
633 /* no base class initializer */
634 NULL,
635 /* no base class finalizer */
636 NULL,
637 /* class initializer */
638 (GClassInitFunc) scim_bridge_client_imcontext_class_initialize,
639 /* no class finalizer */
640 NULL,
641 /* no class data */
642 NULL,
643 sizeof (ScimBridgeClientIMContext),
644 0,
645 /* object initizlier */
646 (GInstanceInitFunc) scim_bridge_client_imcontext_initialize,
647 0
648 };
649
650 class_type = g_type_module_register_type (G_TYPE_MODULE(io_module),
651 GTK_TYPE_IM_CONTEXT,
652 "ScimBridgeClientIMContext",
653 &klass_info,
654 0);
655 }
656
657
658 GtkIMContext *scim_bridge_client_imcontext_new ()
659 {
660 scim_bridge_pdebugln (4, "scim_bridge_client_imcontext_new ()");
661
662 ScimBridgeClientIMContext *ic = SCIM_BRIDGE_CLIENT_IMCONTEXT (g_object_new (GTK_TYPE_SCIM_CLIENT_IMCONTEXT, NULL));
663 return GTK_IM_CONTEXT (ic);
664 }
665
666
667 void scim_bridge_client_imcontext_initialize (ScimBridgeClientIMContext *imcontext, ScimBridgeClientIMContextClass *klass)
668 {
669 scim_bridge_pdebugln (5, "scim_bridge_client_imcontext_initialize ()");
670
671 /* slave exists for using gtk+'s table based input method */
672 imcontext->slave_preedit = FALSE;
673 imcontext->slave = gtk_im_context_simple_new ();
674 g_signal_connect(G_OBJECT(imcontext->slave),
675 "commit",
676 G_CALLBACK(gtk_im_slave_commit_cb),
677 imcontext);
678
679 g_signal_connect(G_OBJECT(imcontext->slave),
680 "preedit-changed",
681 G_CALLBACK(gtk_im_slave_preedit_changed_cb),
682 imcontext);
683
684 g_signal_connect(G_OBJECT(imcontext->slave),
685 "preedit-start",
686 G_CALLBACK(gtk_im_slave_preedit_start_cb),
687 imcontext);
688
689 g_signal_connect(G_OBJECT(imcontext->slave),
690 "preedit-end",
691 G_CALLBACK(gtk_im_slave_preedit_end_cb),
692 imcontext);
693
694 imcontext->preedit_shown = FALSE;
695 imcontext->preedit_started = FALSE;
696
697 imcontext->preedit_cursor_position = 0;
698 imcontext->preedit_cursor_flicking = FALSE;
699
700 imcontext->preedit_string = malloc (sizeof (char));
701 imcontext->preedit_string[0] = '\0';
702 imcontext->preedit_string_capacity = 0;
703
704 imcontext->preedit_attributes = NULL;
705
706 imcontext->commit_string = malloc (sizeof (char));
707 imcontext->commit_string[0] = '\0';
708 imcontext->commit_string_capacity = 0;
709
710 imcontext->enabled = FALSE;
711
712 imcontext->client_widget = NULL;
713
714 imcontext->id = -1;
715
716 if (!scim_bridge_client_is_messenger_opened ()) {
717 scim_bridge_perrorln ("The messenger is now down");
718 } else if (scim_bridge_client_register_imcontext (imcontext)) {
719 scim_bridge_perrorln ("Failed to register the IMContext");
720 } else {
721 scim_bridge_pdebugln (1, "IMContext registered: id = %d", imcontext->id);
722 }
723 }
724
725
726 void scim_bridge_client_imcontext_finalize (GObject *object)
727 {
728 scim_bridge_pdebugln (5, "scim_bridge_client_imcontext_finalize ()");
729
730 ScimBridgeClientIMContext *imcontext = SCIM_BRIDGE_CLIENT_IMCONTEXT (object);
731
732 if (imcontext == focused_imcontext) scim_bridge_client_imcontext_focus_out (GTK_IM_CONTEXT (imcontext));
733
734 if (!scim_bridge_client_is_messenger_opened ()) {
735 scim_bridge_perrorln ("The messenger is now down");
736 } else if (scim_bridge_client_deregister_imcontext (imcontext)) {
737 scim_bridge_perrorln ("Failed to deregister an IMContext");
738 } else {
739 scim_bridge_pdebugln (3, "IMContext deregistered: id = %d", imcontext->id);
740 }
741
742 if (imcontext->client_widget) g_object_unref (imcontext->client_widget);
743
744 free (imcontext->preedit_string);
745 free (imcontext->commit_string);
746
747 if (imcontext->preedit_attributes != NULL)
748 pango_attr_list_unref (imcontext->preedit_attributes);
749
750 imcontext->preedit_attributes = NULL;
751
752 g_signal_handlers_disconnect_by_func(imcontext->slave,
753 (void *)gtk_im_slave_commit_cb,
754 (void *)imcontext);
755 g_signal_handlers_disconnect_by_func(imcontext->slave,
756 (void *)gtk_im_slave_preedit_changed_cb,
757 (void *)imcontext);
758 g_signal_handlers_disconnect_by_func(imcontext->slave,
759 (void *)gtk_im_slave_preedit_start_cb,
760 (void *)imcontext);
761 g_signal_handlers_disconnect_by_func(imcontext->slave,
762 (void *)gtk_im_slave_preedit_end_cb,
763 (void *)imcontext);
764 g_object_unref(imcontext->slave);
765
766 root_klass->finalize (object);
767 }
768
769
770 /* Class functions */
771 gboolean scim_bridge_client_imcontext_filter_key_event (GtkIMContext *context, GdkEvent *event)
772 {
773 scim_bridge_pdebugln (8, "scim_bridge_client_imcontext_filter_key_event ()");
774
775 ScimBridgeClientIMContext *imcontext = SCIM_BRIDGE_CLIENT_IMCONTEXT (context);
776
777 boolean ret = FALSE;
778 if (imcontext) {
779 ret = key_snooper(0, event, 0);
780
781 if (imcontext->slave) {
782 if (!ret) {
783 ret = gtk_im_context_filter_keypress (imcontext->slave, event);
784 } else if (imcontext->slave_preedit) {
785 imcontext->slave_preedit = FALSE;
786 gtk_im_context_reset (imcontext->slave);
787 }
788 }
789
790 }
791
792 return ret;
793 }
794
795
796 void scim_bridge_client_imcontext_reset (GtkIMContext *context)
797 {
798 scim_bridge_pdebugln (8, "scim_bridge_client_imcontext_reset ()");
799
800 ScimBridgeClientIMContext *imcontext = SCIM_BRIDGE_CLIENT_IMCONTEXT (context);
801
802 if (imcontext != focused_imcontext) return;
803
804 if (scim_bridge_client_is_messenger_opened () && imcontext != NULL) {
805 if (scim_bridge_client_reset_imcontext (imcontext)) {
806 scim_bridge_perrorln ("An IOException occurred at scim_bridge_client_imcontext_reset ()");
807 }
808 }
809 }
810
811
812 void scim_bridge_client_imcontext_get_preedit_string (GtkIMContext *context, gchar **str, PangoAttrList **pango_attrs, gint *cursor_pos)
813 {
814 scim_bridge_pdebugln (4, "scim_bridge_client_imcontext_get_preedit_string ()");
815
816 ScimBridgeClientIMContext *imcontext = SCIM_BRIDGE_CLIENT_IMCONTEXT (context);
817
818 if (imcontext->slave_preedit) {
819 gtk_im_context_get_preedit_string (imcontext->slave, str, pango_attrs, cursor_pos);
820 return;
821 }
822
823 if (scim_bridge_client_is_messenger_opened () && imcontext != NULL && imcontext->preedit_shown) {
824 const size_t preedit_string_length = strlen (imcontext->preedit_string);
825 const size_t preedit_wstring_length = g_utf8_strlen (imcontext->preedit_string, -1);
826
827 if (str) {
828 if (preedit_string_length > 0) {
829 *str = g_strdup (imcontext->preedit_string);
830 } else {
831 *str = g_strdup ("");
832 }
833 }
834
835 if (cursor_pos) {
836 if (imcontext->preedit_cursor_position > preedit_wstring_length) {
837 *cursor_pos = preedit_wstring_length;
838 } else {
839 *cursor_pos = imcontext->preedit_cursor_position;
840 }
841 }
842
843 if (pango_attrs) {
844 *pango_attrs = imcontext->preedit_attributes;
845 pango_attr_list_ref (imcontext->preedit_attributes);
846 }
847 } else {
848 if (str) *str = g_strdup ("");
849 if (cursor_pos) *cursor_pos = 0;
850 if (pango_attrs) *pango_attrs = pango_attr_list_new ();
851 }
852 }
853
854
855 void scim_bridge_client_imcontext_focus_in (GtkIMContext *context)
856 {
857 scim_bridge_pdebugln (8, "scim_bridge_client_imcontext_focus_in ()");
858
859 ScimBridgeClientIMContext *imcontext = SCIM_BRIDGE_CLIENT_IMCONTEXT (context);
860
861 if (focused_imcontext != NULL && focused_imcontext != imcontext) {
862 scim_bridge_client_imcontext_focus_out (GTK_IM_CONTEXT (focused_imcontext));
863 }
864 focused_imcontext = imcontext;
865
866 if (!scim_bridge_client_is_messenger_opened () && scim_bridge_client_is_reconnection_enabled ()) {
867 scim_bridge_pdebugln (7, "Trying to open the connection again...");
868 scim_bridge_client_open_messenger ();
869 }
870
871 if (scim_bridge_client_is_messenger_opened () && imcontext != NULL) {
872 if (scim_bridge_client_change_focus (imcontext, TRUE)) {
873 scim_bridge_perrorln ("An IOException occurred at scim_bridge_client_imcontext_focus_in ()");
874 }
875 }
876 }
877
878
879 void scim_bridge_client_imcontext_focus_out (GtkIMContext *context)
880 {
881 scim_bridge_pdebugln (8, "scim_bridge_client_imcontext_focus_out ()");
882
883 ScimBridgeClientIMContext *imcontext = SCIM_BRIDGE_CLIENT_IMCONTEXT (context);
884 focused_widget = NULL;
885
886 focused_imcontext = imcontext;
887 if (imcontext->preedit_shown) {
888 if (imcontext->enabled) {
889 scim_bridge_client_imcontext_set_preedit_shown (imcontext, FALSE);
890 scim_bridge_client_imcontext_update_preedit (imcontext);
891 }
892 }
893 if (scim_bridge_client_is_messenger_opened () && imcontext != NULL) {
894 if (scim_bridge_client_change_focus (imcontext, FALSE)) {
895 scim_bridge_perrorln ("An IOException occurred at scim_bridge_client_imcontext_focus_out ()");
896 }
897 }
898
899 focused_imcontext = NULL;
900 }
901
902
903 void scim_bridge_client_imcontext_set_client_widget (GtkIMContext *context, GtkWidget *new_widget)
904 {
905 scim_bridge_pdebugln (7, "scim_bridge_client_imcontext_set_client_widget ()");
906
907 ScimBridgeClientIMContext *imcontext = SCIM_BRIDGE_CLIENT_IMCONTEXT (context);
908 if (!imcontext) {
909 return;
910 }
911
912 if (imcontext->client_widget) {
913 g_object_unref(imcontext->client_widget);
914 }
915
916 imcontext->client_widget = new_widget;
917 if (!new_widget) {
918 return;
919 }
920
921 g_object_ref (new_widget);
922 widget_get_origin (imcontext->client_widget, &imcontext->window_x, &imcontext->window_y);
923 }
924
925 void scim_bridge_client_imcontext_set_cursor_location (GtkIMContext *context, GdkRectangle *area)
926 {
927 scim_bridge_pdebugln (4, "scim_bridge_client_imcontext_set_cursor_location ()");
928
929 ScimBridgeClientIMContext *imcontext = SCIM_BRIDGE_CLIENT_IMCONTEXT (context);
930 if (imcontext->preedit_cursor_flicking)
931 return;
932
933 if (imcontext != NULL && imcontext->client_widget != NULL) {
934 const int new_cursor_x = area->x + area->width;
935 const int new_cursor_y = area->y + area->height + 8;
936
937 int widget_x, widget_y;
938 widget_get_origin(imcontext->client_widget, &widget_x, &widget_y);
939
940 if (set_cursor_location (imcontext, widget_x, widget_y, new_cursor_x, new_cursor_y)) {
941 scim_bridge_perrorln ("An IOException occurred at scim_bridge_client_imcontext_set_cursor_location ()");
942 }
943 }
944 }
945
946
947 void scim_bridge_client_imcontext_set_preedit_enabled (GtkIMContext *context, gboolean enabled)
948 {
949 scim_bridge_pdebugln (8, "scim_bridge_client_imcontext_set_preedit_enabled ()");
950
951 ScimBridgeClientIMContext *imcontext = SCIM_BRIDGE_CLIENT_IMCONTEXT (context);
952
953 if (imcontext != NULL) {
954 if (scim_bridge_client_is_messenger_opened ()) {
955 if (scim_bridge_client_set_preedit_mode (imcontext, enabled ? PREEDIT_EMBEDDED:PREEDIT_ANY)) {
956 scim_bridge_perrorln ("An IOException occurred at scim_bridge_client_imcontext_set_preedit_enabled ()");
957 }
958 }
959 }
960 }
961
962 static void
963 gtk_im_slave_commit_cb (
964 GtkIMContext *context,
965 const char *str,
966 ScimBridgeClientIMContext *imcontext
967 ) {
968 g_return_if_fail(str);
969 g_signal_emit_by_name(imcontext, "commit", str);
970 }
971
972 static void
973 gtk_im_slave_preedit_changed_cb (
974 GtkIMContext *context,
975 ScimBridgeClientIMContext *imcontext
976 ) {
977 imcontext->slave_preedit = TRUE;
978 g_signal_emit_by_name(imcontext, "preedit-changed");
979 }
980
981 static void
982 gtk_im_slave_preedit_start_cb (
983 GtkIMContext *context,
984 ScimBridgeClientIMContext *imcontext
985 ) {
986 imcontext->slave_preedit = TRUE;
987 g_signal_emit_by_name(imcontext, "preedit-start");
988 }
989
990 static void
991 gtk_im_slave_preedit_end_cb (
992 GtkIMContext *context,
993 ScimBridgeClientIMContext *imcontext
994 ) {
995 imcontext->slave_preedit = FALSE;
996 g_signal_emit_by_name(imcontext, "preedit-end");
997 }