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