cd10312a9dbdf6db125b91d02cbcd293505154cc
[mim] / src / ui / mim-immodule.c
1 /*
2  * GTK+ MiM input module 
3  */
4
5 #ifdef HAVE_CONFIG_H
6 # include <config.h>
7 #endif
8
9 #include <gtk/gtkimcontext.h>
10 #include <gtk/gtkimmodule.h>
11 #include <gdk/gdkkeysyms.h>
12 #include <string.h>
13
14 #include "im-extra-intl.h"
15
16 GType type_mim = 0;
17
18 /* is this a character that could appear in a mim word */
19 static gboolean
20 is_mim_word_character(gunichar uc)
21 {
22     return (((uc >= 0x0590 && uc <= 0x5ff) || (uc >= 0xfb1d && uc <= 0xfb4f))
23             && g_unichar_isdefined(uc) && !g_unichar_ispunct(uc));
24
25 }
26
27 static gboolean
28 at_initial_position(GtkIMContext *context)
29 {
30     gchar *text;
31     gchar *prevp;
32     gint cursor_index;
33     gunichar uc;
34
35     if (!gtk_im_context_get_surrounding(context, &text, &cursor_index))
36         return FALSE;
37
38     prevp = g_utf8_find_prev_char(text, text + cursor_index);
39     if (prevp == NULL)
40         return TRUE;
41
42     uc = g_utf8_get_char_validated(prevp, text + cursor_index - prevp);
43     g_return_val_if_fail(uc != (gunichar)(-1) && uc != (gunichar)(-2), FALSE);
44
45     if (is_mim_word_character(uc))
46         return FALSE;
47     else
48         return TRUE;
49 }
50
51 static gboolean
52 mim_filter_keypress(GtkIMContext *context, GdkEventKey *event)
53 {
54     gboolean is_initial;
55     gint i;
56
57     if (event->type == GDK_KEY_RELEASE)
58         return FALSE;
59
60     /* don't filter key events with accelerator modifiers held down */
61     if (event->state & (gtk_accelerator_get_default_mod_mask()
62             & ~GDK_SHIFT_MASK))
63         return FALSE;
64
65     is_initial = at_initial_position(context);
66     return TRUE;
67 }
68
69 static void
70 mim_set_client_window(GtkIMContext *context,
71                       GdkWindow *window)
72 {
73     if (window == NULL) {
74         /* create new client window */
75         //mim_mui_create_window();
76     }
77 }
78
79 static void
80 mim_reset(GtkIMContext *context)
81 {
82 }
83
84 static void
85 mim_class_init(GtkIMContextClass *clazz)
86 {
87     clazz->filter_keypress = mim_filter_keypress;
88     clazz->reset = mim_reset;
89     clazz->set_client_window = mim_set_client_window;
90 }
91
92 static void
93 mim_init(GtkIMContext *im_context)
94 {
95 }
96
97 static void
98 mim_register_type(GTypeModule *module)
99 {
100     static const GTypeInfo object_info =
101     {
102         sizeof(GtkIMContextClass),
103         (GBaseInitFunc) NULL,
104         (GBaseFinalizeFunc) NULL,
105         (GClassInitFunc) mim_class_init,
106         NULL, /* class_finalize */
107         NULL, /* class_data */
108         sizeof(GtkIMContext),
109         0,
110         (GInstanceInitFunc) mim_init,
111     };
112
113     type_mim = g_type_module_register_type(module,
114                                            GTK_TYPE_IM_CONTEXT,
115                                            "GtkIMContextYiddishNoah",
116                                            &object_info,
117                                            0);
118 }
119
120 static const
121 GtkIMContextInfo mim_info =
122 {
123     "mim", /* ID */
124     N_("MiM"), /* Human readable name */
125     GETTEXT_PACKAGE, /* Translation domain */
126     LOCALEDIR, /* Dir for bindtextdomain */
127     "zh_CN", /* Languages for which this module is the default */
128 };
129
130 static const
131 GtkIMContextInfo *info_list[] =
132 {
133     &mim_info,
134 };
135
136 void
137 im_module_init(GTypeModule *module)
138 {
139     g_debug("mim-immodule imported!");
140     mim_register_type(module);
141 }
142
143 void
144 im_module_exit()
145 {
146     g_debug("mim-immodule released!");
147 }
148
149 void
150 im_module_list(const GtkIMContextInfo ***contexts,
151                gint *n_contexts)
152 {
153     *contexts = info_list;
154     *n_contexts = G_N_ELEMENTS(info_list);
155 }
156
157 GtkIMContext *
158 im_module_create(const gchar *context_id)
159 {
160     if (strcmp(context_id, "mim-n") == 0)
161         return GTK_IM_CONTEXT(g_object_new(type_mim, NULL));
162     else
163         return NULL;
164 }
165