1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * Themes added by The Rasterman <raster@redhat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
36 #include <pango/pango-utils.h>
38 #include "gtkimmodule.h"
39 #include "gtkimcontextsimple.h"
43 /* Do *not* include "gtkprivate.h" in this file. If you do, the
44 * correct_libdir_prefix() function below will have to move somewhere
48 #define SIMPLE_ID "gtk-im-context-simple"
50 typedef struct _GtkIMModule GtkIMModule;
51 typedef struct _GtkIMModuleClass GtkIMModuleClass;
53 #define GTK_TYPE_IM_MODULE (gtk_im_module_get_type ())
54 #define GTK_IM_MODULE(im_module) (G_TYPE_CHECK_INSTANCE_CAST ((im_module), GTK_TYPE_IM_MODULE, GtkIMModule))
55 #define GTK_IS_IM_MODULE(im_module) (G_TYPE_CHECK_INSTANCE_TYPE ((im_module), GTK_TYPE_IM_MODULE))
59 GTypeModule parent_instance;
63 void (*list) (const GtkIMContextInfo ***contexts,
65 void (*init) (GTypeModule *module);
67 GtkIMContext *(*create) (const gchar *context_id);
69 GtkIMContextInfo **contexts;
75 struct _GtkIMModuleClass
77 GTypeModuleClass parent_class;
80 GType gtk_im_module_get_type (void);
82 static gint n_loaded_contexts = 0;
83 static GHashTable *contexts_hash = NULL;
84 static GSList *modules_list = NULL;
86 static GObjectClass *parent_class = NULL;
89 gtk_im_module_load (GTypeModule *module)
91 GtkIMModule *im_module = GTK_IM_MODULE (module);
93 im_module->library = g_module_open (im_module->path, 0);
94 if (!im_module->library)
96 g_warning (g_module_error());
100 /* extract symbols from the lib */
101 if (!g_module_symbol (im_module->library, "im_module_init",
102 (gpointer *)&im_module->init) ||
103 !g_module_symbol (im_module->library, "im_module_exit",
104 (gpointer *)&im_module->exit) ||
105 !g_module_symbol (im_module->library, "im_module_list",
106 (gpointer *)&im_module->list) ||
107 !g_module_symbol (im_module->library, "im_module_create",
108 (gpointer *)&im_module->create))
110 g_warning (g_module_error());
111 g_module_close (im_module->library);
116 /* call the theme's init (theme_init) function to let it */
117 /* setup anything it needs to set up. */
118 im_module->init (module);
124 gtk_im_module_unload (GTypeModule *module)
126 GtkIMModule *im_module = GTK_IM_MODULE (module);
130 g_module_close (im_module->library);
131 im_module->library = NULL;
133 im_module->init = NULL;
134 im_module->exit = NULL;
135 im_module->list = NULL;
136 im_module->create = NULL;
139 /* This only will ever be called if an error occurs during
143 gtk_im_module_finalize (GObject *object)
145 GtkIMModule *module = GTK_IM_MODULE (object);
147 g_free (module->path);
149 parent_class->finalize (object);
153 gtk_im_module_class_init (GtkIMModuleClass *class)
155 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
156 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
158 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (class));
160 module_class->load = gtk_im_module_load;
161 module_class->unload = gtk_im_module_unload;
163 gobject_class->finalize = gtk_im_module_finalize;
167 gtk_im_module_get_type (void)
169 static GType im_module_type = 0;
173 static const GTypeInfo im_module_info = {
174 sizeof (GtkIMModuleClass),
175 NULL, /* base_init */
176 NULL, /* base_finalize */
177 (GClassInitFunc) gtk_im_module_class_init,
178 NULL, /* class_finalize */
179 NULL, /* class_data */
180 sizeof (GtkIMModule),
182 NULL, /* instance_init */
186 g_type_register_static (G_TYPE_TYPE_MODULE, "GtkIMModule",
190 return im_module_type;
194 free_info (GtkIMContextInfo *info)
196 g_free ((char *)info->context_id);
197 g_free ((char *)info->context_name);
198 g_free ((char *)info->domain);
199 g_free ((char *)info->domain_dirname);
200 g_free ((char *)info->default_locales);
205 add_module (GtkIMModule *module, GSList *infos)
207 GSList *tmp_list = infos;
209 gint n = g_slist_length (infos);
210 module->contexts = g_new (GtkIMContextInfo *, n);
214 GtkIMContextInfo *info = tmp_list->data;
216 if (g_hash_table_lookup (contexts_hash, info->context_id))
218 free_info (info); /* Duplicate */
222 g_hash_table_insert (contexts_hash, (char *)info->context_id, module);
223 module->contexts[i++] = tmp_list->data;
227 tmp_list = tmp_list->next;
229 g_slist_free (infos);
230 module->n_contexts = i;
232 modules_list = g_slist_prepend (modules_list, module);
235 #if defined (G_OS_WIN32) && defined (GTK_LIBDIR)
236 /* This is needes on Win32, but not wanted when compiling with MSVC,
237 * as the makefile.msc doesn't define any GTK_LIBDIR value.
240 #define DO_CORRECT_LIBDIR_PREFIX /* Flag to check below whether to call this */
243 correct_libdir_prefix (gchar **path)
245 /* GTK_LIBDIR here is supposed to still have the definition from
246 * Makefile.am, i.e. the build-time value. Do *not* include gtkprivate.h
249 if (strncmp (*path, GTK_LIBDIR, strlen (GTK_LIBDIR)) == 0)
251 /* This is an entry put there by make install on the
252 * packager's system. On Windows a prebuilt GTK+
253 * package can be installed in a random
254 * location. The gtk.immodules file distributed in
255 * such a package contains paths from the package
256 * builder's machine. Replace the path with the real
257 * one on this machine.
259 extern const gchar *_gtk_get_libdir ();
261 *path = g_strconcat (_gtk_get_libdir (), tem + strlen (GTK_LIBDIR), NULL);
269 gtk_im_module_init ()
271 GString *line_buf = g_string_new (NULL);
272 GString *tmp_buf = g_string_new (NULL);
273 gchar *filename = gtk_rc_get_im_module_file();
275 gboolean have_error = FALSE;
277 GtkIMModule *module = NULL;
278 GSList *infos = NULL;
280 contexts_hash = g_hash_table_new (g_str_hash, g_str_equal);
282 file = fopen (filename, "r");
285 /* In case someone wants only the default input method,
286 * we allow no file at all.
288 g_string_free (line_buf, TRUE);
289 g_string_free (tmp_buf, TRUE);
294 while (!have_error && pango_read_line (file, line_buf))
300 if (!pango_skip_space (&p))
302 /* Blank line marking the end of a module
304 if (module && *p != '#')
306 add_module (module, infos);
316 /* Read a module location
318 module = g_object_new (GTK_TYPE_IM_MODULE, NULL);
320 if (!pango_scan_string (&p, tmp_buf) ||
321 pango_skip_space (&p))
323 g_warning ("Error parsing context info in '%s'\n %s",
324 filename, line_buf->str);
328 module->path = g_strdup (tmp_buf->str);
329 #ifdef DO_CORRECT_LIBDIR_PREFIX
330 correct_libdir_prefix (&module->path);
332 g_type_module_set_name (G_TYPE_MODULE (module), module->path);
336 GtkIMContextInfo *info = g_new0 (GtkIMContextInfo, 1);
338 /* Read information about a context type
340 if (!pango_scan_string (&p, tmp_buf))
342 info->context_id = g_strdup (tmp_buf->str);
344 if (!pango_scan_string (&p, tmp_buf))
346 info->context_name = g_strdup (tmp_buf->str);
348 if (!pango_scan_string (&p, tmp_buf))
350 info->domain = g_strdup (tmp_buf->str);
352 if (!pango_scan_string (&p, tmp_buf))
354 info->domain_dirname = g_strdup (tmp_buf->str);
355 #ifdef DO_CORRECT_LIBDIR_PREFIX
356 correct_libdir_prefix (&info->domain_dirname);
359 if (!pango_scan_string (&p, tmp_buf))
361 info->default_locales = g_strdup (tmp_buf->str);
363 if (pango_skip_space (&p))
366 infos = g_slist_prepend (infos, info);
370 g_warning ("Error parsing context info in '%s'\n %s",
371 filename, line_buf->str);
378 GSList *tmp_list = infos;
381 free_info (tmp_list->data);
382 tmp_list = tmp_list->next;
384 g_slist_free (infos);
386 g_object_unref (module);
389 add_module (module, infos);
392 g_string_free (line_buf, TRUE);
393 g_string_free (tmp_buf, TRUE);
398 compare_gtkimcontextinfo_name(const GtkIMContextInfo **a,
399 const GtkIMContextInfo **b)
401 return g_utf8_collate ((*a)->context_name, (*b)->context_name);
405 * _gtk_im_module_list:
406 * @contexts: location to store an array of pointers to #GtkIMContextInfo
407 * this array should be freed with g_free() when you are finished.
408 * The structures it points are statically allocated and should
409 * not be modified or freed.
410 * @n_contexts: the length of the array stored in @contexts
412 * List all available types of input method context
415 _gtk_im_module_list (const GtkIMContextInfo ***contexts,
420 static const GtkIMContextInfo simple_context_info = {
433 gtk_im_module_init ();
436 *n_contexts = (n_loaded_contexts + 1);
443 *contexts = g_new (const GtkIMContextInfo *, n_loaded_contexts + 1);
445 (*contexts)[n++] = &simple_context_info;
447 tmp_list = modules_list;
450 GtkIMModule *module = tmp_list->data;
452 for (i=0; i<module->n_contexts; i++)
453 (*contexts)[n++] = module->contexts[i];
455 tmp_list = tmp_list->next;
458 /* fisrt element (Default) should always be at top */
459 qsort ((*contexts)+1, n-1, sizeof (GtkIMContextInfo *), (GCompareFunc)compare_gtkimcontextinfo_name);
464 * _gtk_im_module_create:
465 * @context_id: the context ID for the context type to create
467 * Create an IM context of a type specified by the string
470 * Return value: a newly created input context of or @context_id, or
471 * if that could not be created, a newly created GtkIMContextSimple.
474 _gtk_im_module_create (const gchar *context_id)
476 GtkIMModule *im_module;
477 GtkIMContext *context = NULL;
480 gtk_im_module_init ();
482 if (strcmp (context_id, SIMPLE_ID) != 0)
484 im_module = g_hash_table_lookup (contexts_hash, context_id);
487 g_warning ("Attempt to load unknown IM context type '%s'", context_id);
491 if (g_type_module_use (G_TYPE_MODULE (im_module)))
493 context = im_module->create (context_id);
494 g_type_module_unuse (G_TYPE_MODULE (im_module));
498 g_warning ("Loading IM context type '%s' failed", context_id);
503 return gtk_im_context_simple_new ();
508 /* Match @locale against @against.
510 * 'en_US' against 'en_US' => 4
511 * 'en_US' against 'en' => 3
512 * 'en', 'en_UK' against 'en_US' => 2
513 * all locales, against '*' => 1
516 match_locale (const gchar *locale,
517 const gchar *against,
520 if (strcmp (against, "*") == 0)
523 if (g_ascii_strcasecmp (locale, against) == 0)
526 if (g_ascii_strncasecmp (locale, against, 2) == 0)
527 return (against_len == 2) ? 3 : 2;
533 * _gtk_im_module_get_default_context_id:
534 * @locale: a locale id in the form 'en_US'
536 * Return the context_id of the best IM context type
537 * for the given locale ID.
539 * Return value: the context ID (will never be %NULL)
540 * the value is newly allocated and must be freed
544 _gtk_im_module_get_default_context_id (const gchar *locale)
547 const gchar *context_id = NULL;
548 gint best_goodness = 0;
550 gchar *tmp_locale, *tmp;
554 gtk_im_module_init ();
556 envvar = g_getenv ("GTK_IM_MODULE");
558 (strcmp (envvar, SIMPLE_ID) == 0 ||
559 g_hash_table_lookup (contexts_hash, envvar)))
560 return g_strdup (envvar);
562 /* Strip the locale code down to the essentials
564 tmp_locale = g_strdup (locale);
565 tmp = strchr (tmp_locale, '.');
568 tmp = strchr (tmp_locale, '@');
572 tmp_list = modules_list;
575 GtkIMModule *module = tmp_list->data;
577 for (i=0; i<module->n_contexts; i++)
579 const gchar *p = module->contexts[i]->default_locales;
582 const gchar *q = strchr (p, ':');
583 gint goodness = match_locale (tmp_locale, p, q ? q - p : strlen (p));
585 if (goodness > best_goodness)
587 context_id = module->contexts[i]->context_id;
588 best_goodness = goodness;
591 p = q ? q + 1 : NULL;
595 tmp_list = tmp_list->next;
600 return g_strdup (context_id ? context_id : SIMPLE_ID);