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