]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkim-win32.c
9567ce19707ff3813a1a4fb5ff0e0dadf94a6228
[~andy/gtk] / gdk / win32 / gdkim-win32.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 <stdlib.h>
30 #include <string.h>
31 #include <locale.h>
32
33 #include "gdkim.h"
34 #include "gdkpixmap.h"
35 #include "gdkinternals.h"
36 #include "gdki18n.h"
37 #include "gdkwin32.h"
38
39 /*
40  *--------------------------------------------------------------
41  * gdk_set_locale
42  *
43  * Arguments:
44  *
45  * Results:
46  *
47  * Side effects:
48  *
49  *--------------------------------------------------------------
50  */
51
52 gchar*
53 gdk_set_locale (void)
54 {
55   if (!setlocale (LC_ALL, ""))
56     g_warning ("locale not supported by C library");
57   
58   return g_win32_getlocale ();
59 }
60
61 void 
62 gdk_im_begin (GdkIC *ic, GdkWindow* window)
63 {
64 }
65
66 void 
67 gdk_im_end (void)
68 {
69 }
70
71 GdkIMStyle
72 gdk_im_decide_style (GdkIMStyle supported_style)
73 {
74   return GDK_IM_PREEDIT_NONE | GDK_IM_STATUS_NONE;
75 }
76
77 GdkIMStyle
78 gdk_im_set_best_style (GdkIMStyle style)
79 {
80   return GDK_IM_PREEDIT_NONE | GDK_IM_STATUS_NONE;
81 }
82
83 gint 
84 gdk_im_ready (void)
85 {
86   return FALSE;
87 }
88
89 GdkIC * 
90 gdk_ic_new (GdkICAttr *attr, GdkICAttributesType mask)
91 {
92   return NULL;
93 }
94
95 void 
96 gdk_ic_destroy (GdkIC *ic)
97 {
98 }
99
100 GdkIMStyle
101 gdk_ic_get_style (GdkIC *ic)
102 {
103   return GDK_IM_PREEDIT_NONE | GDK_IM_STATUS_NONE;
104 }
105
106 GdkICAttr*
107 gdk_ic_attr_new (void)
108 {
109   return NULL;
110 }
111
112 void
113 gdk_ic_attr_destroy (GdkICAttr *attr)
114 {
115 }
116
117 void 
118 gdk_ic_set_values (GdkIC *ic, ...)
119 {
120 }
121
122 void 
123 gdk_ic_get_values (GdkIC *ic, ...)
124 {
125 }
126
127 GdkICAttributesType 
128 gdk_ic_set_attr (GdkIC *ic, GdkICAttr *attr, GdkICAttributesType mask)
129 {
130   return 0;
131 }
132
133 GdkICAttributesType 
134 gdk_ic_get_attr (GdkIC *ic, GdkICAttr *attr, GdkICAttributesType mask)
135 {
136   return 0;
137 }
138
139 GdkEventMask 
140 gdk_ic_get_events (GdkIC *ic)
141 {
142   return 0;
143 }
144
145 /*
146  * gdk_wcstombs 
147  *
148  * Returns a multi-byte string converted from the specified array
149  * of wide characters. The string is newly allocated. The array of
150  * wide characters must be null-terminated. If the conversion is
151  * failed, it returns NULL.
152  *
153  * On Win32, we always use UTF-8.
154  */
155 gchar *
156 gdk_wcstombs (const GdkWChar *src)
157 {
158   gint len;
159   const GdkWChar *wcp;
160   guchar *mbstr, *bp;
161
162   wcp = src;
163   len = 0;
164   while (*wcp)
165     {
166       const GdkWChar c = *wcp++;
167
168       if (c < 0x80)
169         len += 1;
170       else if (c < 0x800)
171         len += 2;
172       else if (c < 0x10000)
173         len += 3;
174       else if (c < 0x200000)
175         len += 4;
176       else if (c < 0x4000000)
177         len += 5;
178       else
179         len += 6;
180     }
181
182   mbstr = g_malloc (len + 1);
183   
184   wcp = src;
185   bp = mbstr;
186   while (*wcp)
187     {
188       int first;
189       GdkWChar c = *wcp++;
190
191       if (c < 0x80)
192         {
193           first = 0;
194           len = 1;
195         }
196       else if (c < 0x800)
197         {
198           first = 0xc0;
199           len = 2;
200         }
201       else if (c < 0x10000)
202         {
203           first = 0xe0;
204           len = 3;
205         }
206       else if (c < 0x200000)
207         {
208           first = 0xf0;
209           len = 4;
210         }
211       else if (c < 0x4000000)
212         {
213           first = 0xf8;
214           len = 5;
215         }
216       else
217         {
218           first = 0xfc;
219           len = 6;
220         }
221       
222       /* Woo-hoo! */
223       switch (len)
224         {
225         case 6: bp[5] = (c & 0x3f) | 0x80; c >>= 6; /* Fall through */
226         case 5: bp[4] = (c & 0x3f) | 0x80; c >>= 6; /* Fall through */
227         case 4: bp[3] = (c & 0x3f) | 0x80; c >>= 6; /* Fall through */
228         case 3: bp[2] = (c & 0x3f) | 0x80; c >>= 6; /* Fall through */
229         case 2: bp[1] = (c & 0x3f) | 0x80; c >>= 6; /* Fall through */
230         case 1: bp[0] = c | first;
231         }
232
233       bp += len;
234     }
235   *bp = 0;
236   return mbstr;
237 }
238
239   
240 /*
241  * gdk_mbstowcs
242  *
243  * Converts the specified string into GDK wide characters, and,
244  * returns the number of wide characters written. The string 'src'
245  * must be null-terminated. If the conversion is failed, it returns
246  * -1.
247  *
248  * On Win32, the string is assumed to be in UTF-8.  Also note that
249  * GdkWChar is 32 bits, while wchar_t, and the wide characters the
250  * Windows API uses, are 16 bits!
251  */
252
253 /* First a helper function for not zero-terminated strings */
254 gint
255 gdk_nmbstowcs (GdkWChar    *dest,
256                const gchar *src,
257                gint         src_len,
258                gint         dest_max)
259 {
260   guchar *cp, *end;
261   gint n;
262   
263   cp = (guchar *) src;
264   end = cp + src_len;
265   n = 0;
266   while (cp != end && dest != dest + dest_max)
267     {
268       gint i, mask = 0, len;
269       guchar c = *cp;
270
271       if (c < 0x80)
272         {
273           len = 1;
274           mask = 0x7f;
275         }
276       else if ((c & 0xe0) == 0xc0)
277         {
278           len = 2;
279           mask = 0x1f;
280         }
281       else if ((c & 0xf0) == 0xe0)
282         {
283           len = 3;
284           mask = 0x0f;
285         }
286       else if ((c & 0xf8) == 0xf0)
287         {
288           len = 4;
289           mask = 0x07;
290         }
291       else if ((c & 0xfc) == 0xf8)
292         {
293           len = 5;
294           mask = 0x03;
295         }
296       else if ((c & 0xfc) == 0xfc)
297         {
298           len = 6;
299           mask = 0x01;
300         }
301       else
302         return -1;
303
304       if (cp + len > end)
305         return -1;
306
307       *dest = (cp[0] & mask);
308       for (i = 1; i < len; i++)
309         {
310           if ((cp[i] & 0xc0) != 0x80)
311             return -1;
312           *dest <<= 6;
313           *dest |= (cp[i] & 0x3f);
314         }
315       if (*dest == -1)
316         return -1;
317
318       cp += len;
319       dest++;
320       n++;
321     }
322   if (cp != end)
323     return -1;
324
325   return n;
326 }
327
328 gint
329 gdk_mbstowcs (GdkWChar    *dest,
330               const gchar *src,
331               gint         dest_max)
332 {
333   return gdk_nmbstowcs (dest, src, strlen (src), dest_max);
334 }
335
336
337 /* A version that converts to wchar_t wide chars */
338
339 gint
340 gdk_nmbstowchar_ts (wchar_t     *dest,
341                     const gchar *src,
342                     gint         src_len,
343                     gint         dest_max)
344 {
345   wchar_t *wcp;
346   guchar *cp, *end;
347   gint n;
348   
349   wcp = dest;
350   cp = (guchar *) src;
351   end = cp + src_len;
352   n = 0;
353   while (cp != end && wcp != dest + dest_max)
354     {
355       gint i, mask = 0, len;
356       guchar c = *cp;
357
358       if (c < 0x80)
359         {
360           len = 1;
361           mask = 0x7f;
362         }
363       else if ((c & 0xe0) == 0xc0)
364         {
365           len = 2;
366           mask = 0x1f;
367         }
368       else if ((c & 0xf0) == 0xe0)
369         {
370           len = 3;
371           mask = 0x0f;
372         }
373       else /* Other lengths are not possible with 16-bit wchar_t! */
374         return -1;
375
376       if (cp + len > end)
377         return -1;
378
379       *wcp = (cp[0] & mask);
380       for (i = 1; i < len; i++)
381         {
382           if ((cp[i] & 0xc0) != 0x80)
383             return -1;
384           *wcp <<= 6;
385           *wcp |= (cp[i] & 0x3f);
386         }
387       if (*wcp == 0xFFFF)
388         return -1;
389
390       cp += len;
391       wcp++;
392       n++;
393     }
394   if (cp != end)
395     return -1;
396
397   return n;
398 }