1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Copyright (C) 1998-2002 Tor Lillqvist
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, 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/.
34 #include "gdkpixmap.h"
35 #include "gdkinternals.h"
40 *--------------------------------------------------------------
49 *--------------------------------------------------------------
55 if (!setlocale (LC_ALL, ""))
56 g_warning ("locale not supported by C library");
58 return g_win32_getlocale ();
64 * Returns a multi-byte string converted from the specified array
65 * of wide characters. The string is newly allocated. The array of
66 * wide characters must be null-terminated. If the conversion is
67 * failed, it returns NULL.
69 * On Win32, we always use UTF-8.
72 gdk_wcstombs (const GdkWChar *src)
82 const GdkWChar c = *wcp++;
90 else if (c < 0x200000)
92 else if (c < 0x4000000)
98 mbstr = g_malloc (len + 1);
117 else if (c < 0x10000)
122 else if (c < 0x200000)
127 else if (c < 0x4000000)
141 case 6: bp[5] = (c & 0x3f) | 0x80; c >>= 6; /* Fall through */
142 case 5: bp[4] = (c & 0x3f) | 0x80; c >>= 6; /* Fall through */
143 case 4: bp[3] = (c & 0x3f) | 0x80; c >>= 6; /* Fall through */
144 case 3: bp[2] = (c & 0x3f) | 0x80; c >>= 6; /* Fall through */
145 case 2: bp[1] = (c & 0x3f) | 0x80; c >>= 6; /* Fall through */
146 case 1: bp[0] = c | first;
155 /* A vesion that converts from wchar_t strings to UTF-8 */
158 _gdk_ucs2_to_utf8 (const wchar_t *src,
167 while (wcp < src + src_len)
169 const wchar_t c = *wcp++;
179 mbstr = g_malloc (len + 1);
183 while (wcp < src + src_len)
207 case 3: bp[2] = (c & 0x3f) | 0x80; c >>= 6; /* Fall through */
208 case 2: bp[1] = (c & 0x3f) | 0x80; c >>= 6; /* Fall through */
209 case 1: bp[0] = c | first;
219 /* Convert from UTF-8 to GdkWChar */
222 _gdk_utf8_to_wcs (GdkWChar *dest,
233 while (cp != end && dest != dest + dest_max)
235 gint i, mask = 0, len;
243 else if ((c & 0xe0) == 0xc0)
248 else if ((c & 0xf0) == 0xe0)
253 else if ((c & 0xf8) == 0xf0)
258 else if ((c & 0xfc) == 0xf8)
263 else if ((c & 0xfc) == 0xfc)
274 *dest = (cp[0] & mask);
275 for (i = 1; i < len; i++)
277 if ((cp[i] & 0xc0) != 0x80)
280 *dest |= (cp[i] & 0x3f);
298 * Converts the specified string into GDK wide characters, and,
299 * returns the number of wide characters written. The string 'src'
300 * must be null-terminated. If the conversion is failed, it returns
303 * On Win32, the string is assumed to be in UTF-8. Also note that
304 * GdkWChar is 32 bits, while wchar_t, and the wide characters the
305 * Windows API uses, are 16 bits!
309 gdk_mbstowcs (GdkWChar *dest,
313 return _gdk_utf8_to_wcs (dest, src, strlen (src), dest_max);
317 /* A version that converts to a wchar_t string */
320 _gdk_utf8_to_ucs2 (wchar_t *dest,
333 while (cp != end && wcp != dest + dest_max)
335 gint i, mask = 0, len;
343 else if ((c & 0xe0) == 0xc0)
348 else if ((c & 0xf0) == 0xe0)
353 else /* Other lengths are not possible with 16-bit wchar_t! */
359 *wcp = (cp[0] & mask);
360 for (i = 1; i < len; i++)
362 if ((cp[i] & 0xc0) != 0x80)
365 *wcp |= (cp[i] & 0x3f);