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/.
33 #include <pango/pango-utils.h>
34 #include "gtkimmodule.h"
35 #include "gtkimcontextsimple.h"
40 /* Do *not* include "gtkprivate.h" in this file. If you do, the
41 * correct_libdir_prefix() function below will have to move somewhere
45 #define SIMPLE_ID "gtk-im-context-simple"
47 typedef struct _GtkIMModule GtkIMModule;
48 typedef struct _GtkIMModuleClass GtkIMModuleClass;
50 #define GTK_TYPE_IM_MODULE (gtk_im_module_get_type ())
51 #define GTK_IM_MODULE(im_module) (G_TYPE_CHECK_INSTANCE_CAST ((im_module), GTK_TYPE_IM_MODULE, GtkIMModule))
52 #define GTK_IS_IM_MODULE(im_module) (G_TYPE_CHECK_INSTANCE_TYPE ((im_module), GTK_TYPE_IM_MODULE))
56 GTypeModule parent_instance;
60 void (*list) (const GtkIMContextInfo ***contexts,
62 void (*init) (GTypeModule *module);
64 GtkIMContext *(*create) (const gchar *context_id);
66 GtkIMContextInfo **contexts;
72 struct _GtkIMModuleClass
74 GTypeModuleClass parent_class;
77 GType gtk_im_module_get_type (void);
79 static gint n_loaded_contexts = 0;
80 static GHashTable *contexts_hash = NULL;
81 static GSList *modules_list = NULL;
83 static GObjectClass *parent_class = NULL;
86 gtk_im_module_load (GTypeModule *module)
88 GtkIMModule *im_module = GTK_IM_MODULE (module);
90 im_module->library = g_module_open (im_module->path, 0);
91 if (!im_module->library)
93 g_warning (g_module_error());
97 /* extract symbols from the lib */
98 if (!g_module_symbol (im_module->library, "im_module_init",
99 (gpointer *)&im_module->init) ||
100 !g_module_symbol (im_module->library, "im_module_exit",
101 (gpointer *)&im_module->exit) ||
102 !g_module_symbol (im_module->library, "im_module_list",
103 (gpointer *)&im_module->list) ||
104 !g_module_symbol (im_module->library, "im_module_create",
105 (gpointer *)&im_module->create))
107 g_warning (g_module_error());
108 g_module_close (im_module->library);
113 /* call the theme's init (theme_init) function to let it */
114 /* setup anything it needs to set up. */
115 im_module->init (module);
121 gtk_im_module_unload (GTypeModule *module)
123 GtkIMModule *im_module = GTK_IM_MODULE (module);
127 g_module_close (im_module->library);
128 im_module->library = NULL;
130 im_module->init = NULL;
131 im_module->exit = NULL;
132 im_module->list = NULL;
133 im_module->create = NULL;
136 /* This only will ever be called if an error occurs during
140 gtk_im_module_finalize (GObject *object)
142 GtkIMModule *module = GTK_IM_MODULE (object);
144 g_free (module->path);
146 parent_class->finalize (object);
150 gtk_im_module_class_init (GtkIMModuleClass *class)
152 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
153 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
155 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (class));
157 module_class->load = gtk_im_module_load;
158 module_class->unload = gtk_im_module_unload;
160 gobject_class->finalize = gtk_im_module_finalize;
164 gtk_im_module_get_type (void)
166 static GType im_module_type = 0;
170 static const GTypeInfo im_module_info = {
171 sizeof (GtkIMModuleClass),
172 NULL, /* base_init */
173 NULL, /* base_finalize */
174 (GClassInitFunc) gtk_im_module_class_init,
175 NULL, /* class_finalize */
176 NULL, /* class_data */
177 sizeof (GtkIMModule),
179 NULL, /* instance_init */
183 g_type_register_static (G_TYPE_TYPE_MODULE, "GtkIMModule",
187 return im_module_type;
191 free_info (GtkIMContextInfo *info)
193 g_free ((char *)info->context_id);
194 g_free ((char *)info->context_name);
195 g_free ((char *)info->domain);
196 g_free ((char *)info->domain_dirname);
197 g_free ((char *)info->default_locales);
202 add_module (GtkIMModule *module, GSList *infos)
204 GSList *tmp_list = infos;
206 gint n = g_slist_length (infos);
207 module->contexts = g_new (GtkIMContextInfo *, n);
211 GtkIMContextInfo *info = tmp_list->data;
213 if (g_hash_table_lookup (contexts_hash, info->context_id))
215 free_info (info); /* Duplicate */
219 g_hash_table_insert (contexts_hash, (char *)info->context_id, module);
220 module->contexts[i++] = tmp_list->data;
224 tmp_list = tmp_list->next;
226 g_slist_free (infos);
227 module->n_contexts = i;
229 modules_list = g_slist_prepend (modules_list, module);
232 #if defined (G_OS_WIN32) && defined (GTK_LIBDIR)
233 /* This is needes on Win32, but not wanted when compiling with MSVC,
234 * as the makefile.msc doesn't define any GTK_LIBDIR value.
237 #define DO_CORRECT_LIBDIR_PREFIX /* Flag to check below whether to call this */
240 correct_libdir_prefix (gchar **path)
242 /* GTK_LIBDIR here is supposed to still have the definition from
243 * Makefile.am, i.e. the build-time value. Do *not* include gtkprivate.h
246 if (strncmp (*path, GTK_LIBDIR, strlen (GTK_LIBDIR)) == 0)
248 /* This is an entry put there by make install on the
249 * packager's system. On Windows a prebuilt GTK+
250 * package can be installed in a random
251 * location. The gtk.immodules file distributed in
252 * such a package contains paths from the package
253 * builder's machine. Replace the path with the real
254 * one on this machine.
256 extern const gchar *_gtk_get_libdir ();
258 *path = g_strconcat (_gtk_get_libdir (), tem + strlen (GTK_LIBDIR), NULL);
266 gtk_im_module_init ()
268 GString *line_buf = g_string_new (NULL);
269 GString *tmp_buf = g_string_new (NULL);
270 gchar *filename = gtk_rc_get_im_module_file();
272 gboolean have_error = FALSE;
274 GtkIMModule *module = NULL;
275 GSList *infos = NULL;
277 contexts_hash = g_hash_table_new (g_str_hash, g_str_equal);
279 file = fopen (filename, "r");
282 g_warning ("Can not open Input Method module file '%s': %s",
283 filename, g_strerror (errno));
284 /* We are leaking all kinds of memory here. */
288 while (!have_error && pango_read_line (file, line_buf))
294 if (!pango_skip_space (&p))
296 /* Blank line marking the end of a module
298 if (module && *p != '#')
300 add_module (module, infos);
310 /* Read a module location
312 module = g_object_new (GTK_TYPE_IM_MODULE, NULL);
314 if (!pango_scan_string (&p, tmp_buf) ||
315 pango_skip_space (&p))
317 g_warning ("Error parsing context info in '%s'\n %s",
318 filename, line_buf->str);
322 module->path = g_strdup (tmp_buf->str);
323 #ifdef DO_CORRECT_LIBDIR_PREFIX
324 correct_libdir_prefix (&module->path);
326 g_type_module_set_name (G_TYPE_MODULE (module), module->path);
330 GtkIMContextInfo *info = g_new0 (GtkIMContextInfo, 1);
332 /* Read information about a context type
334 if (!pango_scan_string (&p, tmp_buf))
336 info->context_id = g_strdup (tmp_buf->str);
338 if (!pango_scan_string (&p, tmp_buf))
340 info->context_name = g_strdup (tmp_buf->str);
342 if (!pango_scan_string (&p, tmp_buf))
344 info->domain = g_strdup (tmp_buf->str);
346 if (!pango_scan_string (&p, tmp_buf))
348 info->domain_dirname = g_strdup (tmp_buf->str);
349 #ifdef DO_CORRECT_LIBDIR_PREFIX
350 correct_libdir_prefix (&info->domain_dirname);
353 if (!pango_scan_string (&p, tmp_buf))
355 info->default_locales = g_strdup (tmp_buf->str);
357 if (pango_skip_space (&p))
360 infos = g_slist_prepend (infos, info);
364 g_warning ("Error parsing context info in '%s'\n %s",
365 filename, line_buf->str);
372 GSList *tmp_list = infos;
375 free_info (tmp_list->data);
376 tmp_list = tmp_list->next;
378 g_slist_free (infos);
380 g_object_unref (module);
383 add_module (module, infos);
386 g_string_free (line_buf, TRUE);
387 g_string_free (tmp_buf, TRUE);
392 compare_gtkimcontextinfo_name(const GtkIMContextInfo **a,
393 const GtkIMContextInfo **b)
395 return g_utf8_collate ((*a)->context_name, (*b)->context_name);
399 * _gtk_im_module_list:
400 * @contexts: location to store an array of pointers to #GtkIMContextInfo
401 * this array should be freed with g_free() when you are finished.
402 * The structures it points are statically allocated and should
403 * not be modified or freed.
404 * @n_contexts: the length of the array stored in @contexts
406 * List all available types of input method context
409 _gtk_im_module_list (const GtkIMContextInfo ***contexts,
414 static const GtkIMContextInfo simple_context_info = {
423 gtk_im_module_init ();
426 *n_contexts = (n_loaded_contexts + 1);
433 *contexts = g_new (const GtkIMContextInfo *, n_loaded_contexts + 1);
435 (*contexts)[n++] = &simple_context_info;
437 tmp_list = modules_list;
440 GtkIMModule *module = tmp_list->data;
442 for (i=0; i<module->n_contexts; i++)
443 (*contexts)[n++] = module->contexts[i];
445 tmp_list = tmp_list->next;
448 /* fisrt element (Default) should always be at top */
449 qsort ((*contexts)+1, n-1, sizeof (GtkIMContextInfo *), (GCompareFunc)compare_gtkimcontextinfo_name);
454 * _gtk_im_module_create:
455 * @context_id: the context ID for the context type to create
457 * Create an IM context of a type specified by the string
460 * Return value: a newly created input context of or @context_id, or
461 * if that could not be created, a newly created GtkIMContextSimple.
464 _gtk_im_module_create (const gchar *context_id)
466 GtkIMModule *im_module;
467 GtkIMContext *context = NULL;
470 gtk_im_module_init ();
472 if (strcmp (context_id, SIMPLE_ID) != 0)
474 im_module = g_hash_table_lookup (contexts_hash, context_id);
477 g_warning ("Attempt to load unknown IM context type '%s'", context_id);
481 if (g_type_module_use (G_TYPE_MODULE (im_module)))
483 context = im_module->create (context_id);
484 g_type_module_unuse (G_TYPE_MODULE (im_module));
488 g_warning ("Loading IM context type '%s' failed", context_id);
493 return gtk_im_context_simple_new ();
498 /* Match @locale against @against.
500 * 'en_US' against 'en_US' => 4
501 * 'en_US' against 'en' => 3
502 * 'en', 'en_UK' against 'en_US' => 2
503 * all locales, against '*' => 1
506 match_locale (const gchar *locale,
507 const gchar *against,
510 if (strcmp (against, "*") == 0)
513 if (strcmp (locale, against) == 0)
516 if (strncmp (locale, against, 2) == 0)
517 return (against_len == 2) ? 3 : 2;
523 * _gtk_im_module_get_default_context_id:
524 * @locale: a locale id in the form 'en_US'
526 * Return the context_id of the best IM context type
527 * for the given locale ID.
529 * Return value: the context ID (will never be %NULL)
530 * the value is newly allocated and must be freed
534 _gtk_im_module_get_default_context_id (const gchar *locale)
537 const gchar *context_id = NULL;
538 gint best_goodness = 0;
540 gchar *tmp_locale, *tmp;
544 gtk_im_module_init ();
546 envvar = g_getenv ("GTK_IM_MODULE");
548 (strcmp (envvar, SIMPLE_ID) == 0 ||
549 g_hash_table_lookup (contexts_hash, envvar)))
550 return g_strdup (envvar);
552 /* Strip the locale code down to the essentials
554 tmp_locale = g_strdup (locale);
555 tmp = strchr (tmp_locale, '.');
558 tmp = strchr (tmp_locale, '@');
562 tmp_list = modules_list;
565 GtkIMModule *module = tmp_list->data;
567 for (i=0; i<module->n_contexts; i++)
569 const gchar *p = module->contexts[i]->default_locales;
572 const gchar *q = strchr (p, ':');
573 gint goodness = match_locale (tmp_locale, p, q ? q - p : strlen (p));
575 if (goodness > best_goodness)
577 context_id = module->contexts[i]->context_id;
578 best_goodness = goodness;
581 p = q ? q + 1 : NULL;
585 tmp_list = tmp_list->next;
590 return g_strdup (context_id ? context_id : SIMPLE_ID);