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>
37 #include "gtkimmodule.h"
38 #include "gtkimcontextsimple.h"
42 /* Do *not* include "gtkprivate.h" in this file. If you do, the
43 * correct_libdir_prefix() function below will have to move somewhere
47 #define SIMPLE_ID "gtk-im-context-simple"
49 typedef struct _GtkIMModule GtkIMModule;
50 typedef struct _GtkIMModuleClass GtkIMModuleClass;
52 #define GTK_TYPE_IM_MODULE (gtk_im_module_get_type ())
53 #define GTK_IM_MODULE(im_module) (G_TYPE_CHECK_INSTANCE_CAST ((im_module), GTK_TYPE_IM_MODULE, GtkIMModule))
54 #define GTK_IS_IM_MODULE(im_module) (G_TYPE_CHECK_INSTANCE_TYPE ((im_module), GTK_TYPE_IM_MODULE))
58 GTypeModule parent_instance;
62 void (*list) (const GtkIMContextInfo ***contexts,
64 void (*init) (GTypeModule *module);
66 GtkIMContext *(*create) (const gchar *context_id);
68 GtkIMContextInfo **contexts;
74 struct _GtkIMModuleClass
76 GTypeModuleClass parent_class;
79 GType gtk_im_module_get_type (void);
81 static gint n_loaded_contexts = 0;
82 static GHashTable *contexts_hash = NULL;
83 static GSList *modules_list = NULL;
85 static GObjectClass *parent_class = NULL;
88 gtk_im_module_load (GTypeModule *module)
90 GtkIMModule *im_module = GTK_IM_MODULE (module);
92 im_module->library = g_module_open (im_module->path, 0);
93 if (!im_module->library)
95 g_warning (g_module_error());
99 /* extract symbols from the lib */
100 if (!g_module_symbol (im_module->library, "im_module_init",
101 (gpointer *)&im_module->init) ||
102 !g_module_symbol (im_module->library, "im_module_exit",
103 (gpointer *)&im_module->exit) ||
104 !g_module_symbol (im_module->library, "im_module_list",
105 (gpointer *)&im_module->list) ||
106 !g_module_symbol (im_module->library, "im_module_create",
107 (gpointer *)&im_module->create))
109 g_warning (g_module_error());
110 g_module_close (im_module->library);
115 /* call the theme's init (theme_init) function to let it */
116 /* setup anything it needs to set up. */
117 im_module->init (module);
123 gtk_im_module_unload (GTypeModule *module)
125 GtkIMModule *im_module = GTK_IM_MODULE (module);
129 g_module_close (im_module->library);
130 im_module->library = NULL;
132 im_module->init = NULL;
133 im_module->exit = NULL;
134 im_module->list = NULL;
135 im_module->create = NULL;
138 /* This only will ever be called if an error occurs during
142 gtk_im_module_finalize (GObject *object)
144 GtkIMModule *module = GTK_IM_MODULE (object);
146 g_free (module->path);
148 parent_class->finalize (object);
152 gtk_im_module_class_init (GtkIMModuleClass *class)
154 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
155 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
157 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (class));
159 module_class->load = gtk_im_module_load;
160 module_class->unload = gtk_im_module_unload;
162 gobject_class->finalize = gtk_im_module_finalize;
166 gtk_im_module_get_type (void)
168 static GType im_module_type = 0;
172 static const GTypeInfo im_module_info = {
173 sizeof (GtkIMModuleClass),
174 NULL, /* base_init */
175 NULL, /* base_finalize */
176 (GClassInitFunc) gtk_im_module_class_init,
177 NULL, /* class_finalize */
178 NULL, /* class_data */
179 sizeof (GtkIMModule),
181 NULL, /* instance_init */
185 g_type_register_static (G_TYPE_TYPE_MODULE, "GtkIMModule",
189 return im_module_type;
193 free_info (GtkIMContextInfo *info)
195 g_free ((char *)info->context_id);
196 g_free ((char *)info->context_name);
197 g_free ((char *)info->domain);
198 g_free ((char *)info->domain_dirname);
199 g_free ((char *)info->default_locales);
204 add_module (GtkIMModule *module, GSList *infos)
206 GSList *tmp_list = infos;
208 gint n = g_slist_length (infos);
209 module->contexts = g_new (GtkIMContextInfo *, n);
213 GtkIMContextInfo *info = tmp_list->data;
215 if (g_hash_table_lookup (contexts_hash, info->context_id))
217 free_info (info); /* Duplicate */
221 g_hash_table_insert (contexts_hash, (char *)info->context_id, module);
222 module->contexts[i++] = tmp_list->data;
226 tmp_list = tmp_list->next;
228 g_slist_free (infos);
229 module->n_contexts = i;
231 modules_list = g_slist_prepend (modules_list, module);
234 #if defined (G_OS_WIN32) && defined (GTK_LIBDIR)
235 /* This is needes on Win32, but not wanted when compiling with MSVC,
236 * as the makefile.msc doesn't define any GTK_LIBDIR value.
239 #define DO_CORRECT_LIBDIR_PREFIX /* Flag to check below whether to call this */
242 correct_libdir_prefix (gchar **path)
244 /* GTK_LIBDIR here is supposed to still have the definition from
245 * Makefile.am, i.e. the build-time value. Do *not* include gtkprivate.h
248 if (strncmp (*path, GTK_LIBDIR, strlen (GTK_LIBDIR)) == 0)
250 /* This is an entry put there by make install on the
251 * packager's system. On Windows a prebuilt GTK+
252 * package can be installed in a random
253 * location. The gtk.immodules file distributed in
254 * such a package contains paths from the package
255 * builder's machine. Replace the path with the real
256 * one on this machine.
258 extern const gchar *_gtk_get_libdir ();
260 *path = g_strconcat (_gtk_get_libdir (), tem + strlen (GTK_LIBDIR), NULL);
268 gtk_im_module_init ()
270 GString *line_buf = g_string_new (NULL);
271 GString *tmp_buf = g_string_new (NULL);
272 gchar *filename = gtk_rc_get_im_module_file();
274 gboolean have_error = FALSE;
276 GtkIMModule *module = NULL;
277 GSList *infos = NULL;
279 contexts_hash = g_hash_table_new (g_str_hash, g_str_equal);
281 file = fopen (filename, "r");
284 /* In case someone wants only the default input method,
285 * we allow no file at all.
287 g_string_free (line_buf, TRUE);
288 g_string_free (tmp_buf, TRUE);
293 while (!have_error && pango_read_line (file, line_buf))
299 if (!pango_skip_space (&p))
301 /* Blank line marking the end of a module
303 if (module && *p != '#')
305 add_module (module, infos);
315 /* Read a module location
317 module = g_object_new (GTK_TYPE_IM_MODULE, NULL);
319 if (!pango_scan_string (&p, tmp_buf) ||
320 pango_skip_space (&p))
322 g_warning ("Error parsing context info in '%s'\n %s",
323 filename, line_buf->str);
327 module->path = g_strdup (tmp_buf->str);
328 #ifdef DO_CORRECT_LIBDIR_PREFIX
329 correct_libdir_prefix (&module->path);
331 g_type_module_set_name (G_TYPE_MODULE (module), module->path);
335 GtkIMContextInfo *info = g_new0 (GtkIMContextInfo, 1);
337 /* Read information about a context type
339 if (!pango_scan_string (&p, tmp_buf))
341 info->context_id = g_strdup (tmp_buf->str);
343 if (!pango_scan_string (&p, tmp_buf))
345 info->context_name = g_strdup (tmp_buf->str);
347 if (!pango_scan_string (&p, tmp_buf))
349 info->domain = g_strdup (tmp_buf->str);
351 if (!pango_scan_string (&p, tmp_buf))
353 info->domain_dirname = g_strdup (tmp_buf->str);
354 #ifdef DO_CORRECT_LIBDIR_PREFIX
355 correct_libdir_prefix (&info->domain_dirname);
358 if (!pango_scan_string (&p, tmp_buf))
360 info->default_locales = g_strdup (tmp_buf->str);
362 if (pango_skip_space (&p))
365 infos = g_slist_prepend (infos, info);
369 g_warning ("Error parsing context info in '%s'\n %s",
370 filename, line_buf->str);
377 GSList *tmp_list = infos;
380 free_info (tmp_list->data);
381 tmp_list = tmp_list->next;
383 g_slist_free (infos);
385 g_object_unref (module);
388 add_module (module, infos);
391 g_string_free (line_buf, TRUE);
392 g_string_free (tmp_buf, TRUE);
397 compare_gtkimcontextinfo_name(const GtkIMContextInfo **a,
398 const GtkIMContextInfo **b)
400 return g_utf8_collate ((*a)->context_name, (*b)->context_name);
404 * _gtk_im_module_list:
405 * @contexts: location to store an array of pointers to #GtkIMContextInfo
406 * this array should be freed with g_free() when you are finished.
407 * The structures it points are statically allocated and should
408 * not be modified or freed.
409 * @n_contexts: the length of the array stored in @contexts
411 * List all available types of input method context
414 _gtk_im_module_list (const GtkIMContextInfo ***contexts,
419 static const GtkIMContextInfo simple_context_info = {
432 gtk_im_module_init ();
435 *n_contexts = (n_loaded_contexts + 1);
442 *contexts = g_new (const GtkIMContextInfo *, n_loaded_contexts + 1);
444 (*contexts)[n++] = &simple_context_info;
446 tmp_list = modules_list;
449 GtkIMModule *module = tmp_list->data;
451 for (i=0; i<module->n_contexts; i++)
452 (*contexts)[n++] = module->contexts[i];
454 tmp_list = tmp_list->next;
457 /* fisrt element (Default) should always be at top */
458 qsort ((*contexts)+1, n-1, sizeof (GtkIMContextInfo *), (GCompareFunc)compare_gtkimcontextinfo_name);
463 * _gtk_im_module_create:
464 * @context_id: the context ID for the context type to create
466 * Create an IM context of a type specified by the string
469 * Return value: a newly created input context of or @context_id, or
470 * if that could not be created, a newly created GtkIMContextSimple.
473 _gtk_im_module_create (const gchar *context_id)
475 GtkIMModule *im_module;
476 GtkIMContext *context = NULL;
479 gtk_im_module_init ();
481 if (strcmp (context_id, SIMPLE_ID) != 0)
483 im_module = g_hash_table_lookup (contexts_hash, context_id);
486 g_warning ("Attempt to load unknown IM context type '%s'", context_id);
490 if (g_type_module_use (G_TYPE_MODULE (im_module)))
492 context = im_module->create (context_id);
493 g_type_module_unuse (G_TYPE_MODULE (im_module));
497 g_warning ("Loading IM context type '%s' failed", context_id);
502 return gtk_im_context_simple_new ();
507 /* Match @locale against @against.
509 * 'en_US' against 'en_US' => 4
510 * 'en_US' against 'en' => 3
511 * 'en', 'en_UK' against 'en_US' => 2
512 * all locales, against '*' => 1
515 match_locale (const gchar *locale,
516 const gchar *against,
519 if (strcmp (against, "*") == 0)
522 if (g_ascii_strcasecmp (locale, against) == 0)
525 if (g_ascii_strncasecmp (locale, against, 2) == 0)
526 return (against_len == 2) ? 3 : 2;
532 * _gtk_im_module_get_default_context_id:
533 * @locale: a locale id in the form 'en_US'
535 * Return the context_id of the best IM context type
536 * for the given locale ID.
538 * Return value: the context ID (will never be %NULL)
539 * the value is newly allocated and must be freed
543 _gtk_im_module_get_default_context_id (const gchar *locale)
546 const gchar *context_id = NULL;
547 gint best_goodness = 0;
549 gchar *tmp_locale, *tmp;
553 gtk_im_module_init ();
555 envvar = g_getenv ("GTK_IM_MODULE");
557 (strcmp (envvar, SIMPLE_ID) == 0 ||
558 g_hash_table_lookup (contexts_hash, envvar)))
559 return g_strdup (envvar);
561 /* Strip the locale code down to the essentials
563 tmp_locale = g_strdup (locale);
564 tmp = strchr (tmp_locale, '.');
567 tmp = strchr (tmp_locale, '@');
571 tmp_list = modules_list;
574 GtkIMModule *module = tmp_list->data;
576 for (i=0; i<module->n_contexts; i++)
578 const gchar *p = module->contexts[i]->default_locales;
581 const gchar *q = strchr (p, ':');
582 gint goodness = match_locale (tmp_locale, p, q ? q - p : strlen (p));
584 if (goodness > best_goodness)
586 context_id = module->contexts[i]->context_id;
587 best_goodness = goodness;
590 p = q ? q + 1 : NULL;
594 tmp_list = tmp_list->next;
599 return g_strdup (context_id ? context_id : SIMPLE_ID);