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/.
35 #include <glib/gstdio.h>
37 #include <pango/pango-utils.h>
38 #include "gtkimmodule.h"
39 #include "gtkimcontextsimple.h"
44 /* Do *not* include "gtkprivate.h" in this file. If you do, the
45 * correct_libdir_prefix() function below will have to move somewhere
49 #ifdef __GTK_PRIVATE_H__
50 #error gtkprivate.h should not be included in this file
53 #define SIMPLE_ID "gtk-im-context-simple"
55 typedef struct _GtkIMModule GtkIMModule;
56 typedef struct _GtkIMModuleClass GtkIMModuleClass;
58 #define GTK_TYPE_IM_MODULE (gtk_im_module_get_type ())
59 #define GTK_IM_MODULE(im_module) (G_TYPE_CHECK_INSTANCE_CAST ((im_module), GTK_TYPE_IM_MODULE, GtkIMModule))
60 #define GTK_IS_IM_MODULE(im_module) (G_TYPE_CHECK_INSTANCE_TYPE ((im_module), GTK_TYPE_IM_MODULE))
64 GTypeModule parent_instance;
68 void (*list) (const GtkIMContextInfo ***contexts,
70 void (*init) (GTypeModule *module);
72 GtkIMContext *(*create) (const gchar *context_id);
74 GtkIMContextInfo **contexts;
80 struct _GtkIMModuleClass
82 GTypeModuleClass parent_class;
85 static GType gtk_im_module_get_type (void);
87 static gint n_loaded_contexts = 0;
88 static GHashTable *contexts_hash = NULL;
89 static GSList *modules_list = NULL;
91 static GObjectClass *parent_class = NULL;
94 gtk_im_module_load (GTypeModule *module)
96 GtkIMModule *im_module = GTK_IM_MODULE (module);
98 im_module->library = g_module_open (im_module->path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
99 if (!im_module->library)
101 g_warning (g_module_error());
105 /* extract symbols from the lib */
106 if (!g_module_symbol (im_module->library, "im_module_init",
107 (gpointer *)&im_module->init) ||
108 !g_module_symbol (im_module->library, "im_module_exit",
109 (gpointer *)&im_module->exit) ||
110 !g_module_symbol (im_module->library, "im_module_list",
111 (gpointer *)&im_module->list) ||
112 !g_module_symbol (im_module->library, "im_module_create",
113 (gpointer *)&im_module->create))
115 g_warning (g_module_error());
116 g_module_close (im_module->library);
121 /* call the theme's init (theme_init) function to let it */
122 /* setup anything it needs to set up. */
123 im_module->init (module);
129 gtk_im_module_unload (GTypeModule *module)
131 GtkIMModule *im_module = GTK_IM_MODULE (module);
135 g_module_close (im_module->library);
136 im_module->library = NULL;
138 im_module->init = NULL;
139 im_module->exit = NULL;
140 im_module->list = NULL;
141 im_module->create = NULL;
144 /* This only will ever be called if an error occurs during
148 gtk_im_module_finalize (GObject *object)
150 GtkIMModule *module = GTK_IM_MODULE (object);
152 g_free (module->path);
154 parent_class->finalize (object);
157 G_DEFINE_TYPE (GtkIMModule, gtk_im_module, G_TYPE_TYPE_MODULE)
160 gtk_im_module_class_init (GtkIMModuleClass *class)
162 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
163 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
165 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (class));
167 module_class->load = gtk_im_module_load;
168 module_class->unload = gtk_im_module_unload;
170 gobject_class->finalize = gtk_im_module_finalize;
174 gtk_im_module_init (GtkIMModule* object)
179 free_info (GtkIMContextInfo *info)
181 g_free ((char *)info->context_id);
182 g_free ((char *)info->context_name);
183 g_free ((char *)info->domain);
184 g_free ((char *)info->domain_dirname);
185 g_free ((char *)info->default_locales);
190 add_module (GtkIMModule *module, GSList *infos)
192 GSList *tmp_list = infos;
194 gint n = g_slist_length (infos);
195 module->contexts = g_new (GtkIMContextInfo *, n);
199 GtkIMContextInfo *info = tmp_list->data;
201 if (g_hash_table_lookup (contexts_hash, info->context_id))
203 free_info (info); /* Duplicate */
207 g_hash_table_insert (contexts_hash, (char *)info->context_id, module);
208 module->contexts[i++] = tmp_list->data;
212 tmp_list = tmp_list->next;
214 g_slist_free (infos);
215 module->n_contexts = i;
217 modules_list = g_slist_prepend (modules_list, module);
220 #if defined (G_OS_WIN32) && defined (GTK_LIBDIR)
221 /* This is needes on Win32, but not wanted when compiling with MSVC,
222 * as the makefile.msc doesn't define any GTK_LIBDIR value.
225 #define DO_CORRECT_LIBDIR_PREFIX /* Flag to check below whether to call this */
228 correct_libdir_prefix (gchar **path)
230 /* GTK_LIBDIR here is supposed to still have the definition from
231 * Makefile.am, i.e. the build-time value. Do *not* include gtkprivate.h
234 if (strncmp (*path, GTK_LIBDIR, strlen (GTK_LIBDIR)) == 0)
236 /* This is an entry put there by make install on the
237 * packager's system. On Windows a prebuilt GTK+
238 * package can be installed in a random
239 * location. The gtk.immodules file distributed in
240 * such a package contains paths from the package
241 * builder's machine. Replace the path with the real
242 * one on this machine.
244 extern const gchar *_gtk_get_libdir ();
246 *path = g_strconcat (_gtk_get_libdir (), tem + strlen (GTK_LIBDIR), NULL);
254 gtk_im_module_initialize (void)
256 GString *line_buf = g_string_new (NULL);
257 GString *tmp_buf = g_string_new (NULL);
258 gchar *filename = gtk_rc_get_im_module_file();
260 gboolean have_error = FALSE;
262 GtkIMModule *module = NULL;
263 GSList *infos = NULL;
265 contexts_hash = g_hash_table_new (g_str_hash, g_str_equal);
267 file = g_fopen (filename, "r");
270 /* In case someone wants only the default input method,
271 * we allow no file at all.
273 g_string_free (line_buf, TRUE);
274 g_string_free (tmp_buf, TRUE);
279 while (!have_error && pango_read_line (file, line_buf))
285 if (!pango_skip_space (&p))
287 /* Blank line marking the end of a module
289 if (module && *p != '#')
291 add_module (module, infos);
301 /* Read a module location
303 module = g_object_new (GTK_TYPE_IM_MODULE, NULL);
305 if (!pango_scan_string (&p, tmp_buf) ||
306 pango_skip_space (&p))
308 g_warning ("Error parsing context info in '%s'\n %s",
309 filename, line_buf->str);
313 module->path = g_strdup (tmp_buf->str);
314 #ifdef DO_CORRECT_LIBDIR_PREFIX
315 correct_libdir_prefix (&module->path);
317 g_type_module_set_name (G_TYPE_MODULE (module), module->path);
321 GtkIMContextInfo *info = g_new0 (GtkIMContextInfo, 1);
323 /* Read information about a context type
325 if (!pango_scan_string (&p, tmp_buf))
327 info->context_id = g_strdup (tmp_buf->str);
329 if (!pango_scan_string (&p, tmp_buf))
331 info->context_name = g_strdup (tmp_buf->str);
333 if (!pango_scan_string (&p, tmp_buf))
335 info->domain = g_strdup (tmp_buf->str);
337 if (!pango_scan_string (&p, tmp_buf))
339 info->domain_dirname = g_strdup (tmp_buf->str);
340 #ifdef DO_CORRECT_LIBDIR_PREFIX
341 correct_libdir_prefix ((char **) &info->domain_dirname);
344 if (!pango_scan_string (&p, tmp_buf))
346 info->default_locales = g_strdup (tmp_buf->str);
348 if (pango_skip_space (&p))
351 infos = g_slist_prepend (infos, info);
355 g_warning ("Error parsing context info in '%s'\n %s",
356 filename, line_buf->str);
363 GSList *tmp_list = infos;
366 free_info (tmp_list->data);
367 tmp_list = tmp_list->next;
369 g_slist_free (infos);
371 g_object_unref (module);
374 add_module (module, infos);
377 g_string_free (line_buf, TRUE);
378 g_string_free (tmp_buf, TRUE);
383 compare_gtkimcontextinfo_name(const GtkIMContextInfo **a,
384 const GtkIMContextInfo **b)
386 return g_utf8_collate ((*a)->context_name, (*b)->context_name);
390 * _gtk_im_module_list:
391 * @contexts: location to store an array of pointers to #GtkIMContextInfo
392 * this array should be freed with g_free() when you are finished.
393 * The structures it points are statically allocated and should
394 * not be modified or freed.
395 * @n_contexts: the length of the array stored in @contexts
397 * List all available types of input method context
400 _gtk_im_module_list (const GtkIMContextInfo ***contexts,
405 static const GtkIMContextInfo simple_context_info = {
418 gtk_im_module_initialize ();
421 *n_contexts = (n_loaded_contexts + 1);
428 *contexts = g_new (const GtkIMContextInfo *, n_loaded_contexts + 1);
430 (*contexts)[n++] = &simple_context_info;
432 tmp_list = modules_list;
435 GtkIMModule *module = tmp_list->data;
437 for (i=0; i<module->n_contexts; i++)
438 (*contexts)[n++] = module->contexts[i];
440 tmp_list = tmp_list->next;
443 /* fisrt element (Default) should always be at top */
444 qsort ((*contexts)+1, n-1, sizeof (GtkIMContextInfo *), (GCompareFunc)compare_gtkimcontextinfo_name);
449 * _gtk_im_module_create:
450 * @context_id: the context ID for the context type to create
452 * Create an IM context of a type specified by the string
455 * Return value: a newly created input context of or @context_id, or
456 * if that could not be created, a newly created GtkIMContextSimple.
459 _gtk_im_module_create (const gchar *context_id)
461 GtkIMModule *im_module;
462 GtkIMContext *context = NULL;
465 gtk_im_module_initialize ();
467 if (strcmp (context_id, SIMPLE_ID) != 0)
469 im_module = g_hash_table_lookup (contexts_hash, context_id);
472 g_warning ("Attempt to load unknown IM context type '%s'", context_id);
476 if (g_type_module_use (G_TYPE_MODULE (im_module)))
478 context = im_module->create (context_id);
479 g_type_module_unuse (G_TYPE_MODULE (im_module));
483 g_warning ("Loading IM context type '%s' failed", context_id);
488 return gtk_im_context_simple_new ();
493 /* Match @locale against @against.
495 * 'en_US' against 'en_US' => 4
496 * 'en_US' against 'en' => 3
497 * 'en', 'en_UK' against 'en_US' => 2
498 * all locales, against '*' => 1
501 match_locale (const gchar *locale,
502 const gchar *against,
505 if (strcmp (against, "*") == 0)
508 if (g_ascii_strcasecmp (locale, against) == 0)
511 if (g_ascii_strncasecmp (locale, against, 2) == 0)
512 return (against_len == 2) ? 3 : 2;
518 * _gtk_im_module_get_default_context_id:
519 * @locale: a locale id in the form 'en_US'
521 * Return the context_id of the best IM context type
522 * for the given locale ID.
524 * Return value: the context ID (will never be %NULL)
525 * the value is newly allocated and must be freed
529 _gtk_im_module_get_default_context_id (const gchar *locale)
532 const gchar *context_id = NULL;
533 gint best_goodness = 0;
535 gchar *tmp_locale, *tmp;
539 gtk_im_module_initialize ();
541 envvar = g_getenv ("GTK_IM_MODULE");
543 (strcmp (envvar, SIMPLE_ID) == 0 ||
544 g_hash_table_lookup (contexts_hash, envvar)))
545 return g_strdup (envvar);
547 /* Strip the locale code down to the essentials
549 tmp_locale = g_strdup (locale);
550 tmp = strchr (tmp_locale, '.');
553 tmp = strchr (tmp_locale, '@');
557 tmp_list = modules_list;
560 GtkIMModule *module = tmp_list->data;
562 for (i=0; i<module->n_contexts; i++)
564 const gchar *p = module->contexts[i]->default_locales;
567 const gchar *q = strchr (p, ':');
568 gint goodness = match_locale (tmp_locale, p, q ? q - p : strlen (p));
570 if (goodness > best_goodness)
572 context_id = module->contexts[i]->context_id;
573 best_goodness = goodness;
576 p = q ? q + 1 : NULL;
580 tmp_list = tmp_list->next;
585 return g_strdup (context_id ? context_id : SIMPLE_ID);