1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
34 #include "gdk.h" /* For gdk_flush() */
35 #include "gdkpixmap.h"
36 #include "gdkinternals.h"
37 #include "gdkdisplay-x11.h"
41 /* If this variable is FALSE, it indicates that we should
42 * avoid trying to use multibyte conversion functions and
43 * assume everything is 1-byte per character
45 static gboolean gdk_use_mb;
48 _gdk_x11_initialize_locale (void)
51 gchar *current_locale;
52 static char *last_locale = NULL;
56 current_locale = setlocale (LC_ALL, NULL);
58 if (last_locale && strcmp (last_locale, current_locale) == 0)
62 last_locale = g_strdup (current_locale);
64 if (XSupportsLocale ())
65 XSetLocaleModifiers ("");
67 if ((strcmp (current_locale, "C")) && (strcmp (current_locale, "POSIX")))
72 /* Detect ancient GNU libc, where mb == UTF8. Not useful unless it's
73 * really a UTF8 locale. The below still probably will
74 * screw up on Greek, Cyrillic, etc, encoded as UTF8.
77 if ((MB_CUR_MAX == 2) &&
78 (mbstowcs (&result, "\xdd\xa5", 1) > 0) &&
81 if ((strlen (current_locale) < 4) ||
82 g_ascii_strcasecmp (current_locale + strlen(current_locale) - 4,
90 g_message ("%s multi-byte string functions.",
91 gdk_use_mb ? "Using" : "Not using"));
99 if (!setlocale (LC_ALL,""))
100 g_warning ("locale not supported by C library");
102 _gdk_x11_initialize_locale ();
104 return setlocale (LC_ALL, NULL);
108 find_a_display (void)
110 GdkDisplay *display = gdk_display_get_default ();
113 display = _gdk_displays->data;
120 * @src: a wide character string.
122 * Converts a wide character string to a multi-byte string.
123 * (The function name comes from an acronym of 'Wide Character String TO
124 * Multi-Byte String').
126 * Return value: the multi-byte string corresponding to @src, or %NULL if the
127 * conversion failed. The returned string should be freed with g_free() when no
131 gdk_wcstombs (const GdkWChar *src)
137 GdkDisplay *display = find_a_display ();
138 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
141 if (sizeof(wchar_t) != sizeof(GdkWChar))
145 for (i=0; src[i]; i++);
146 src_alt = g_new (wchar_t, i+1);
149 if (XwcTextListToTextProperty (xdisplay, &src_alt, 1, XTextStyle, &tpr)
161 if (XwcTextListToTextProperty (xdisplay, &tmp, 1,
162 XTextStyle, &tpr) != Success)
167 src = (GdkWChar *)tmp;
170 * We must copy the string into an area allocated by glib, because
171 * the string 'tpr.value' must be freed by XFree().
173 mbstr = g_strdup(tpr.value);
181 while (src[length] != 0)
184 mbstr = g_new (gchar, length + 1);
186 for (i=0; i<length+1; i++)
194 * @dest: the space to place the converted wide character string into.
195 * @src: the multi-byte string to convert, which must be nul-terminated.
196 * @dest_max: the maximum number of wide characters to place in @dest.
198 * Converts a multi-byte string to a wide character string.
199 * (The function name comes from an acronym of 'Multi-Byte String TO Wide
200 * Character String').
202 * Return value: the number of wide characters written into @dest, or -1 if
203 * the conversion failed.
207 gdk_mbstowcs (GdkWChar *dest, const gchar *src, gint dest_max)
211 GdkDisplay *display = find_a_display ();
212 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
214 wchar_t **wstrs, *wstr_src;
217 if (XmbTextListToTextProperty (xdisplay, (char **)&src, 1, XTextStyle,
221 /* NoMem or LocaleNotSupp */
224 if (XwcTextPropertyToTextList (xdisplay, &tpr, &wstrs, &num_wstrs)
235 for (len_cpy=0; len_cpy<dest_max && wstr_src[len_cpy]; len_cpy++)
236 dest[len_cpy] = wstr_src[len_cpy];
237 XwcFreeStringList (wstrs);
244 for (i=0; i<dest_max && src[i]; i++)
251 #define __GDK_IM_X11_C__
252 #include "gdkaliasdef.c"