]> Pileus Git - ~andy/gtk/blob - gdk/gdkkeys.c
add gdkkeys.[hc]
[~andy/gtk] / gdk / gdkkeys.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2000 Red Hat, Inc.
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 "gdkkeys.h"
28 #include <config.h>
29
30 static void gdk_keymap_init       (GdkKeymap      *keymap);
31 static void gdk_keymap_class_init (GdkKeymapClass *klass);
32 static void gdk_keymap_finalize   (GObject              *object);
33
34 static gpointer parent_class = NULL;
35
36 GType
37 gdk_keymap_get_type (void)
38 {
39   static GType object_type = 0;
40
41   if (!object_type)
42     {
43       static const GTypeInfo object_info =
44       {
45         sizeof (GdkKeymapClass),
46         (GBaseInitFunc) NULL,
47         (GBaseFinalizeFunc) NULL,
48         (GClassInitFunc) gdk_keymap_class_init,
49         NULL,           /* class_finalize */
50         NULL,           /* class_data */
51         sizeof (GdkKeymap),
52         0,              /* n_preallocs */
53         (GInstanceInitFunc) gdk_keymap_init,
54       };
55       
56       object_type = g_type_register_static (G_TYPE_OBJECT,
57                                             "GdkKeymap",
58                                             &object_info, 0);
59     }
60   
61   return object_type;
62 }
63
64 static void
65 gdk_keymap_init (GdkKeymap *keymap)
66 {
67
68 }
69
70 static void
71 gdk_keymap_class_init (GdkKeymapClass *klass)
72 {
73   GObjectClass *object_class = G_OBJECT_CLASS (klass);
74
75   parent_class = g_type_class_peek_parent (klass);
76
77   object_class->finalize = gdk_keymap_finalize;
78 }
79
80 static void
81 gdk_keymap_finalize (GObject *object)
82 {
83   GdkKeymap *keymap = GDK_KEYMAP (object);
84   
85   G_OBJECT_CLASS (parent_class)->finalize (object);
86 }
87
88 GdkKeymap*
89 gdk_keymap_get_default (void)
90 {
91   static GdkKeymap *keymap = NULL;
92
93   if (keymap == NULL)
94     keymap = g_object_new (gdk_keymap_get_type (), NULL);
95
96   return keymap;
97 }
98
99
100
101 /* Other key-handling stuff
102  */
103
104 #ifndef HAVE_XCONVERTCASE
105 /* compatibility function from X11R6.3, since XConvertCase is not
106  * supplied by X11R5.
107  */
108 void
109 gdk_keyval_convert_case (guint symbol,
110                          guint *lower,
111                          guint *upper)
112 {
113   guint xlower = symbol;
114   guint xupper = symbol;
115
116   switch (symbol >> 8)
117     {
118 #if     defined (GDK_A) && defined (GDK_Ooblique)
119     case 0: /* Latin 1 */
120       if ((symbol >= GDK_A) && (symbol <= GDK_Z))
121         xlower += (GDK_a - GDK_A);
122       else if ((symbol >= GDK_a) && (symbol <= GDK_z))
123         xupper -= (GDK_a - GDK_A);
124       else if ((symbol >= GDK_Agrave) && (symbol <= GDK_Odiaeresis))
125         xlower += (GDK_agrave - GDK_Agrave);
126       else if ((symbol >= GDK_agrave) && (symbol <= GDK_odiaeresis))
127         xupper -= (GDK_agrave - GDK_Agrave);
128       else if ((symbol >= GDK_Ooblique) && (symbol <= GDK_Thorn))
129         xlower += (GDK_oslash - GDK_Ooblique);
130       else if ((symbol >= GDK_oslash) && (symbol <= GDK_thorn))
131         xupper -= (GDK_oslash - GDK_Ooblique);
132       break;
133 #endif  /* LATIN1 */
134       
135 #if     defined (GDK_Aogonek) && defined (GDK_tcedilla)
136     case 1: /* Latin 2 */
137       /* Assume the KeySym is a legal value (ignore discontinuities) */
138       if (symbol == GDK_Aogonek)
139         xlower = GDK_aogonek;
140       else if (symbol >= GDK_Lstroke && symbol <= GDK_Sacute)
141         xlower += (GDK_lstroke - GDK_Lstroke);
142       else if (symbol >= GDK_Scaron && symbol <= GDK_Zacute)
143         xlower += (GDK_scaron - GDK_Scaron);
144       else if (symbol >= GDK_Zcaron && symbol <= GDK_Zabovedot)
145         xlower += (GDK_zcaron - GDK_Zcaron);
146       else if (symbol == GDK_aogonek)
147         xupper = GDK_Aogonek;
148       else if (symbol >= GDK_lstroke && symbol <= GDK_sacute)
149         xupper -= (GDK_lstroke - GDK_Lstroke);
150       else if (symbol >= GDK_scaron && symbol <= GDK_zacute)
151         xupper -= (GDK_scaron - GDK_Scaron);
152       else if (symbol >= GDK_zcaron && symbol <= GDK_zabovedot)
153         xupper -= (GDK_zcaron - GDK_Zcaron);
154       else if (symbol >= GDK_Racute && symbol <= GDK_Tcedilla)
155         xlower += (GDK_racute - GDK_Racute);
156       else if (symbol >= GDK_racute && symbol <= GDK_tcedilla)
157         xupper -= (GDK_racute - GDK_Racute);
158       break;
159 #endif  /* LATIN2 */
160       
161 #if     defined (GDK_Hstroke) && defined (GDK_Cabovedot)
162     case 2: /* Latin 3 */
163       /* Assume the KeySym is a legal value (ignore discontinuities) */
164       if (symbol >= GDK_Hstroke && symbol <= GDK_Hcircumflex)
165         xlower += (GDK_hstroke - GDK_Hstroke);
166       else if (symbol >= GDK_Gbreve && symbol <= GDK_Jcircumflex)
167         xlower += (GDK_gbreve - GDK_Gbreve);
168       else if (symbol >= GDK_hstroke && symbol <= GDK_hcircumflex)
169         xupper -= (GDK_hstroke - GDK_Hstroke);
170       else if (symbol >= GDK_gbreve && symbol <= GDK_jcircumflex)
171         xupper -= (GDK_gbreve - GDK_Gbreve);
172       else if (symbol >= GDK_Cabovedot && symbol <= GDK_Scircumflex)
173         xlower += (GDK_cabovedot - GDK_Cabovedot);
174       else if (symbol >= GDK_cabovedot && symbol <= GDK_scircumflex)
175         xupper -= (GDK_cabovedot - GDK_Cabovedot);
176       break;
177 #endif  /* LATIN3 */
178       
179 #if     defined (GDK_Rcedilla) && defined (GDK_Amacron)
180     case 3: /* Latin 4 */
181       /* Assume the KeySym is a legal value (ignore discontinuities) */
182       if (symbol >= GDK_Rcedilla && symbol <= GDK_Tslash)
183         xlower += (GDK_rcedilla - GDK_Rcedilla);
184       else if (symbol >= GDK_rcedilla && symbol <= GDK_tslash)
185         xupper -= (GDK_rcedilla - GDK_Rcedilla);
186       else if (symbol == GDK_ENG)
187         xlower = GDK_eng;
188       else if (symbol == GDK_eng)
189         xupper = GDK_ENG;
190       else if (symbol >= GDK_Amacron && symbol <= GDK_Umacron)
191         xlower += (GDK_amacron - GDK_Amacron);
192       else if (symbol >= GDK_amacron && symbol <= GDK_umacron)
193         xupper -= (GDK_amacron - GDK_Amacron);
194       break;
195 #endif  /* LATIN4 */
196       
197 #if     defined (GDK_Serbian_DJE) && defined (GDK_Cyrillic_yu)
198     case 6: /* Cyrillic */
199       /* Assume the KeySym is a legal value (ignore discontinuities) */
200       if (symbol >= GDK_Serbian_DJE && symbol <= GDK_Serbian_DZE)
201         xlower -= (GDK_Serbian_DJE - GDK_Serbian_dje);
202       else if (symbol >= GDK_Serbian_dje && symbol <= GDK_Serbian_dze)
203         xupper += (GDK_Serbian_DJE - GDK_Serbian_dje);
204       else if (symbol >= GDK_Cyrillic_YU && symbol <= GDK_Cyrillic_HARDSIGN)
205         xlower -= (GDK_Cyrillic_YU - GDK_Cyrillic_yu);
206       else if (symbol >= GDK_Cyrillic_yu && symbol <= GDK_Cyrillic_hardsign)
207         xupper += (GDK_Cyrillic_YU - GDK_Cyrillic_yu);
208       break;
209 #endif  /* CYRILLIC */
210       
211 #if     defined (GDK_Greek_ALPHAaccent) && defined (GDK_Greek_finalsmallsigma)
212     case 7: /* Greek */
213       /* Assume the KeySym is a legal value (ignore discontinuities) */
214       if (symbol >= GDK_Greek_ALPHAaccent && symbol <= GDK_Greek_OMEGAaccent)
215         xlower += (GDK_Greek_alphaaccent - GDK_Greek_ALPHAaccent);
216       else if (symbol >= GDK_Greek_alphaaccent && symbol <= GDK_Greek_omegaaccent &&
217                symbol != GDK_Greek_iotaaccentdieresis &&
218                symbol != GDK_Greek_upsilonaccentdieresis)
219         xupper -= (GDK_Greek_alphaaccent - GDK_Greek_ALPHAaccent);
220       else if (symbol >= GDK_Greek_ALPHA && symbol <= GDK_Greek_OMEGA)
221         xlower += (GDK_Greek_alpha - GDK_Greek_ALPHA);
222       else if (symbol >= GDK_Greek_alpha && symbol <= GDK_Greek_omega &&
223                symbol != GDK_Greek_finalsmallsigma)
224         xupper -= (GDK_Greek_alpha - GDK_Greek_ALPHA);
225       break;
226 #endif  /* GREEK */
227     }
228
229   if (lower)
230     *lower = xlower;
231   if (upper)
232     *upper = xupper;
233 }
234 #endif
235
236 guint
237 gdk_keyval_to_upper (guint keyval)
238 {
239   guint result;
240   
241   gdk_keyval_convert_case (keyval, NULL, &result);
242
243   return result;
244 }
245
246 guint
247 gdk_keyval_to_lower (guint keyval)
248 {
249   guint result;
250   
251   gdk_keyval_convert_case (keyval, &result, NULL);
252
253   return result;
254 }
255
256 gboolean
257 gdk_keyval_is_upper (guint keyval)
258 {
259   if (keyval)
260     {
261       guint upper_val = 0;
262       
263       gdk_keyval_convert_case (keyval, NULL, &upper_val);
264       return upper_val == keyval;
265     }
266   return FALSE;
267 }
268
269 gboolean
270 gdk_keyval_is_lower (guint keyval)
271 {
272   if (keyval)
273     {
274       guint lower_val = 0;
275       
276       gdk_keyval_convert_case (keyval, &lower_val, NULL);
277       return lower_val == keyval;
278     }
279   return FALSE;
280 }