]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkim-x11.c
isspace -> g_ascii_isspace Remove gdki8n.h include. Remove <ctype.h>
[~andy/gtk] / gdk / x11 / gdkim-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include <locale.h>
28 #include <stdlib.h>
29
30 #include "gdk.h"                /* For gdk_flush() */
31 #include "gdkpixmap.h"
32 #include "gdkinternals.h"
33 #include "gdkprivate-x11.h"
34
35 #if HAVE_CONFIG_H
36 #  include <config.h>
37 #  if STDC_HEADERS
38 #    include <string.h>
39 #  endif
40 #endif
41
42
43 /* If this variable is FALSE, it indicates that we should
44  * avoid trying to use multibyte conversion functions and
45  * assume everything is 1-byte per character
46  */
47 static gboolean gdk_use_mb;
48
49 void
50 _gdk_x11_initialize_locale (void)
51 {
52   wchar_t result;
53   gchar *current_locale;
54   static char *last_locale = NULL;
55
56   gdk_use_mb = FALSE;
57
58   current_locale = setlocale (LC_ALL, NULL);
59
60   if (last_locale && strcmp (last_locale, current_locale) == 0)
61     return;
62
63   g_free (last_locale);
64   last_locale = g_strdup (current_locale);
65
66   if (!XSupportsLocale ())
67     g_warning ("locale not supported by Xlib");
68   
69   if (!XSetLocaleModifiers (""))
70     g_warning ("can not set locale modifiers");
71
72   if ((strcmp (current_locale, "C")) && (strcmp (current_locale, "POSIX")))
73     {
74       gdk_use_mb = TRUE;
75
76 #ifndef X_LOCALE
77       /* Detect ancient GNU libc, where mb == UTF8. Not useful unless it's
78        * really a UTF8 locale. The below still probably will
79        * screw up on Greek, Cyrillic, etc, encoded as UTF8.
80        */
81       
82       if ((MB_CUR_MAX == 2) &&
83           (mbstowcs (&result, "\xdd\xa5", 1) > 0) &&
84           result == 0x765)
85         {
86           if ((strlen (current_locale) < 4) ||
87               g_strcasecmp (current_locale + strlen(current_locale) - 4, "utf8"))
88             gdk_use_mb = FALSE;
89         }
90 #endif /* X_LOCALE */
91     }
92
93   GDK_NOTE (XIM,
94             g_message ("%s multi-byte string functions.", 
95                        gdk_use_mb ? "Using" : "Not using"));
96   
97   return;
98 }
99
100 gchar*
101 gdk_set_locale (void)
102 {
103   if (!setlocale (LC_ALL,""))
104     g_warning ("locale not supported by C library");
105
106   _gdk_x11_initialize_locale ();
107   
108   return setlocale (LC_ALL, NULL);
109 }
110
111 /*
112  * gdk_wcstombs 
113  *
114  * Returns a multi-byte string converted from the specified array
115  * of wide characters. The string is newly allocated. The array of
116  * wide characters must be null-terminated. If the conversion is
117  * failed, it returns NULL.
118  */
119 gchar *
120 gdk_wcstombs (const GdkWChar *src)
121 {
122   gchar *mbstr;
123
124   if (gdk_use_mb)
125     {
126       XTextProperty tpr;
127
128       if (sizeof(wchar_t) != sizeof(GdkWChar))
129         {
130           gint i;
131           wchar_t *src_alt;
132           for (i=0; src[i]; i++);
133           src_alt = g_new (wchar_t, i+1);
134           for (; i>=0; i--)
135             src_alt[i] = src[i];
136           if (XwcTextListToTextProperty (gdk_display, &src_alt, 1, XTextStyle, &tpr)
137               != Success)
138             {
139               g_free (src_alt);
140               return NULL;
141             }
142           g_free (src_alt);
143         }
144       else
145         {
146           if (XwcTextListToTextProperty (gdk_display, (wchar_t**)&src, 1,
147                                          XTextStyle, &tpr) != Success)
148             {
149               return NULL;
150             }
151         }
152       /*
153        * We must copy the string into an area allocated by glib, because
154        * the string 'tpr.value' must be freed by XFree().
155        */
156       mbstr = g_strdup(tpr.value);
157       XFree (tpr.value);
158     }
159   else
160     {
161       gint length = 0;
162       gint i;
163
164       while (src[length] != 0)
165         length++;
166       
167       mbstr = g_new (gchar, length + 1);
168
169       for (i=0; i<length+1; i++)
170         mbstr[i] = src[i];
171     }
172
173   return mbstr;
174 }
175   
176 /*
177  * gdk_mbstowcs
178  *
179  * Converts the specified string into wide characters, and, returns the
180  * number of wide characters written. The string 'src' must be
181  * null-terminated. If the conversion is failed, it returns -1.
182  */
183 gint
184 gdk_mbstowcs (GdkWChar *dest, const gchar *src, gint dest_max)
185 {
186   if (gdk_use_mb)
187     {
188       XTextProperty tpr;
189       wchar_t **wstrs, *wstr_src;
190       gint num_wstrs;
191       gint len_cpy;
192       if (XmbTextListToTextProperty (gdk_display, (char **)&src, 1, XTextStyle,
193                                      &tpr)
194           != Success)
195         {
196           /* NoMem or LocaleNotSupp */
197           return -1;
198         }
199       if (XwcTextPropertyToTextList (gdk_display, &tpr, &wstrs, &num_wstrs)
200           != Success)
201         {
202           /* InvalidChar */
203           XFree(tpr.value);
204           return -1;
205         }
206       XFree(tpr.value);
207       if (num_wstrs == 0)
208         return 0;
209       wstr_src = wstrs[0];
210       for (len_cpy=0; len_cpy<dest_max && wstr_src[len_cpy]; len_cpy++)
211         dest[len_cpy] = wstr_src[len_cpy];
212       XwcFreeStringList (wstrs);
213       return len_cpy;
214     }
215   else
216     {
217       gint i;
218
219       for (i=0; i<dest_max && src[i]; i++)
220         dest[i] = src[i];
221
222       return i;
223     }
224 }