]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkim-x11.c
moved and renamed the gdk_settings_names and gdk_settings_map.
[~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 "gdkx.h"
34 #include "gdk.h"                /* For gdk_flush() */
35 #include "gdkx.h"
36 #include "gdkpixmap.h"
37 #include "gdkinternals.h"
38 #include "gdkdisplay-x11.h"
39 #include "gdkalias.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     XSetLocaleModifiers ("");
67
68   if ((strcmp (current_locale, "C")) && (strcmp (current_locale, "POSIX")))
69     {
70       gdk_use_mb = TRUE;
71
72 #ifndef X_LOCALE
73       /* Detect ancient GNU libc, where mb == UTF8. Not useful unless it's
74        * really a UTF8 locale. The below still probably will
75        * screw up on Greek, Cyrillic, etc, encoded as UTF8.
76        */
77       
78       if ((MB_CUR_MAX == 2) &&
79           (mbstowcs (&result, "\xdd\xa5", 1) > 0) &&
80           result == 0x765)
81         {
82           if ((strlen (current_locale) < 4) ||
83               g_ascii_strcasecmp (current_locale + strlen(current_locale) - 4,
84                                   "utf8"))
85             gdk_use_mb = FALSE;
86         }
87 #endif /* X_LOCALE */
88     }
89
90   GDK_NOTE (XIM,
91             g_message ("%s multi-byte string functions.", 
92                        gdk_use_mb ? "Using" : "Not using"));
93   
94   return;
95 }
96
97 gchar*
98 gdk_set_locale (void)
99 {
100   if (!setlocale (LC_ALL,""))
101     g_warning ("locale not supported by C library");
102
103   _gdk_x11_initialize_locale ();
104   
105   return setlocale (LC_ALL, NULL);
106 }
107
108 static GdkDisplay *
109 find_a_display (void)
110 {
111   GdkDisplay *display = gdk_display_get_default ();
112
113   if (!display)
114     display = _gdk_displays->data;
115
116   return display;
117 }
118
119 /**
120  * gdk_wcstombs:
121  * @src: a wide character string.
122  * 
123  * Converts a wide character string to a multi-byte string.
124  * (The function name comes from an acronym of 'Wide Character String TO
125  * Multi-Byte String').
126  * 
127  * Return value: the multi-byte string corresponding to @src, or %NULL if the
128  * conversion failed. The returned string should be freed with g_free() when no
129  * longer needed.
130  **/
131 gchar *
132 gdk_wcstombs (const GdkWChar *src)
133 {
134   gchar *mbstr;
135
136   if (gdk_use_mb)
137     {
138       GdkDisplay *display = find_a_display ();
139       Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
140       XTextProperty tpr;
141
142       if (sizeof(wchar_t) != sizeof(GdkWChar))
143         {
144           gint i;
145           wchar_t *src_alt;
146           for (i=0; src[i]; i++);
147           src_alt = g_new (wchar_t, i+1);
148           for (; i>=0; i--)
149             src_alt[i] = src[i];
150           if (XwcTextListToTextProperty (xdisplay, &src_alt, 1, XTextStyle, &tpr)
151               != Success)
152             {
153               g_free (src_alt);
154               return NULL;
155             }
156           g_free (src_alt);
157         }
158       else
159         {
160           wchar_t *tmp;
161           
162           if (XwcTextListToTextProperty (xdisplay, &tmp, 1,
163                                          XTextStyle, &tpr) != Success)
164             {
165               return NULL;
166             }
167           
168           src = (GdkWChar *)tmp;
169         }
170       /*
171        * We must copy the string into an area allocated by glib, because
172        * the string 'tpr.value' must be freed by XFree().
173        */
174       mbstr = g_strdup(tpr.value);
175       XFree (tpr.value);
176     }
177   else
178     {
179       gint length = 0;
180       gint i;
181
182       while (src[length] != 0)
183         length++;
184       
185       mbstr = g_new (gchar, length + 1);
186
187       for (i=0; i<length+1; i++)
188         mbstr[i] = src[i];
189     }
190
191   return mbstr;
192 }
193 /**
194  * gdk_mbstowcs:
195  * @dest: the space to place the converted wide character string into.
196  * @src: the multi-byte string to convert, which must be nul-terminated.
197  * @dest_max: the maximum number of wide characters to place in @dest.
198  * 
199  * Converts a multi-byte string to a wide character string.
200  * (The function name comes from an acronym of 'Multi-Byte String TO Wide
201  * Character String').
202  * 
203  * Return value: the number of wide characters written into @dest, or -1 if 
204  *   the conversion failed.
205  **/
206   
207 gint
208 gdk_mbstowcs (GdkWChar *dest, const gchar *src, gint dest_max)
209 {
210   if (gdk_use_mb)
211     {
212       GdkDisplay *display = find_a_display ();
213       Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
214       XTextProperty tpr;
215       wchar_t **wstrs, *wstr_src;
216       gint num_wstrs;
217       gint len_cpy;
218       if (XmbTextListToTextProperty (xdisplay, (char **)&src, 1, XTextStyle,
219                                      &tpr)
220           != Success)
221         {
222           /* NoMem or LocaleNotSupp */
223           return -1;
224         }
225       if (XwcTextPropertyToTextList (xdisplay, &tpr, &wstrs, &num_wstrs)
226           != Success)
227         {
228           /* InvalidChar */
229           XFree(tpr.value);
230           return -1;
231         }
232       XFree(tpr.value);
233       if (num_wstrs == 0)
234         return 0;
235       wstr_src = wstrs[0];
236       for (len_cpy=0; len_cpy<dest_max && wstr_src[len_cpy]; len_cpy++)
237         dest[len_cpy] = wstr_src[len_cpy];
238       XwcFreeStringList (wstrs);
239       return len_cpy;
240     }
241   else
242     {
243       gint i;
244
245       for (i=0; i<dest_max && src[i]; i++)
246         dest[i] = src[i];
247
248       return i;
249     }
250 }
251
252 #define __GDK_IM_X11_C__
253 #include "gdkaliasdef.c"