]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkim-x11.c
Fix many sparse warnings.
[~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 <config.h>
28
29 #include <locale.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "gdkalias.h"
34 #include "gdkx.h"
35 #include "gdk.h"                /* For gdk_flush() */
36 #include "gdkx.h"
37 #include "gdkpixmap.h"
38 #include "gdkinternals.h"
39 #include "gdkdisplay-x11.h"
40
41
42 /* If this variable is FALSE, it indicates that we should
43  * avoid trying to use multibyte conversion functions and
44  * assume everything is 1-byte per character
45  */
46 static gboolean gdk_use_mb;
47
48 void
49 _gdk_x11_initialize_locale (void)
50 {
51   wchar_t result;
52   gchar *current_locale;
53   static char *last_locale = NULL;
54
55   gdk_use_mb = FALSE;
56
57   current_locale = setlocale (LC_ALL, NULL);
58
59   if (last_locale && strcmp (last_locale, current_locale) == 0)
60     return;
61
62   g_free (last_locale);
63   last_locale = g_strdup (current_locale);
64
65   if (!XSupportsLocale ())
66     g_warning ("locale not supported by Xlib");
67   
68   if (!XSetLocaleModifiers (""))
69     g_warning ("cannot set locale modifiers");
70
71   if ((strcmp (current_locale, "C")) && (strcmp (current_locale, "POSIX")))
72     {
73       gdk_use_mb = TRUE;
74
75 #ifndef X_LOCALE
76       /* Detect ancient GNU libc, where mb == UTF8. Not useful unless it's
77        * really a UTF8 locale. The below still probably will
78        * screw up on Greek, Cyrillic, etc, encoded as UTF8.
79        */
80       
81       if ((MB_CUR_MAX == 2) &&
82           (mbstowcs (&result, "\xdd\xa5", 1) > 0) &&
83           result == 0x765)
84         {
85           if ((strlen (current_locale) < 4) ||
86               g_ascii_strcasecmp (current_locale + strlen(current_locale) - 4,
87                                   "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 static GdkDisplay *
112 find_a_display (void)
113 {
114   GdkDisplay *display = gdk_display_get_default ();
115
116   if (!display)
117     display = _gdk_displays->data;
118
119   return display;
120 }
121
122 /**
123  * gdk_wcstombs:
124  * @src: a wide character string.
125  * 
126  * Converts a wide character string to a multi-byte string.
127  * (The function name comes from an acronym of 'Wide Character String TO
128  * Multi-Byte String').
129  * 
130  * Return value: the multi-byte string corresponding to @src, or %NULL if the
131  * conversion failed. The returned string should be freed with g_free() when no
132  * longer needed.
133  **/
134 gchar *
135 gdk_wcstombs (const GdkWChar *src)
136 {
137   gchar *mbstr;
138
139   if (gdk_use_mb)
140     {
141       GdkDisplay *display = find_a_display ();
142       Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
143       XTextProperty tpr;
144
145       if (sizeof(wchar_t) != sizeof(GdkWChar))
146         {
147           gint i;
148           wchar_t *src_alt;
149           for (i=0; src[i]; i++);
150           src_alt = g_new (wchar_t, i+1);
151           for (; i>=0; i--)
152             src_alt[i] = src[i];
153           if (XwcTextListToTextProperty (xdisplay, &src_alt, 1, XTextStyle, &tpr)
154               != Success)
155             {
156               g_free (src_alt);
157               return NULL;
158             }
159           g_free (src_alt);
160         }
161       else
162         {
163           if (XwcTextListToTextProperty (xdisplay, (wchar_t**)&src, 1,
164                                          XTextStyle, &tpr) != Success)
165             {
166               return NULL;
167             }
168         }
169       /*
170        * We must copy the string into an area allocated by glib, because
171        * the string 'tpr.value' must be freed by XFree().
172        */
173       mbstr = g_strdup(tpr.value);
174       XFree (tpr.value);
175     }
176   else
177     {
178       gint length = 0;
179       gint i;
180
181       while (src[length] != 0)
182         length++;
183       
184       mbstr = g_new (gchar, length + 1);
185
186       for (i=0; i<length+1; i++)
187         mbstr[i] = src[i];
188     }
189
190   return mbstr;
191 }
192 /**
193  * gdk_mbstowcs:
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.
197  * 
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').
201  * 
202  * Return value: the number of wide characters written into @dest, or -1 if 
203  *   the conversion failed.
204  **/
205   
206 gint
207 gdk_mbstowcs (GdkWChar *dest, const gchar *src, gint dest_max)
208 {
209   if (gdk_use_mb)
210     {
211       GdkDisplay *display = find_a_display ();
212       Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
213       XTextProperty tpr;
214       wchar_t **wstrs, *wstr_src;
215       gint num_wstrs;
216       gint len_cpy;
217       if (XmbTextListToTextProperty (xdisplay, (char **)&src, 1, XTextStyle,
218                                      &tpr)
219           != Success)
220         {
221           /* NoMem or LocaleNotSupp */
222           return -1;
223         }
224       if (XwcTextPropertyToTextList (xdisplay, &tpr, &wstrs, &num_wstrs)
225           != Success)
226         {
227           /* InvalidChar */
228           XFree(tpr.value);
229           return -1;
230         }
231       XFree(tpr.value);
232       if (num_wstrs == 0)
233         return 0;
234       wstr_src = wstrs[0];
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);
238       return len_cpy;
239     }
240   else
241     {
242       gint i;
243
244       for (i=0; i<dest_max && src[i]; i++)
245         dest[i] = src[i];
246
247       return i;
248     }
249 }