]> git.lyx.org Git - lyx.git/blob - src/lyxlookup.C
3dc265bb5fa27d4bc293ca974ae62b6956a9d879
[lyx.git] / src / lyxlookup.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 1995 Matthias Ettrich 
7  *           Copyright 1995-2000 The LyX team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef HAVE_XOPENIM
14 // This part is the full blown Input Method manager for X11R5 and up.
15 // For the plain-and-old-X11R4 version, see later.
16
17 #include <X11/Xlib.h>
18 #include <X11/Xutil.h>
19 #include <X11/keysym.h>
20 #ifdef HAVE_LOCALE_H
21 #include <locale.h>
22 #endif
23 #include <clocale>
24
25 #include "debug.h"
26
27 static XIM xim;
28 static XIC xic;
29 XComposeStatus compose_status= {0, 0};
30
31 // This is called after the main LyX window has been created
32 void InitLyXLookup(Display * display, Window window) 
33 {
34         xic = 0;
35         
36         // This part could be done before opening display
37         setlocale(LC_CTYPE, "");
38         if (!XSupportsLocale()) {
39                 lyxerr[Debug::KEY]
40                         << "InitLyXLookup: X does not support this locale."
41                         << endl;
42                 return;
43         } 
44         if (!XSetLocaleModifiers("")) {
45                 lyxerr[Debug::KEY] << "InitLyXLookup: Could not set modifiers "
46                         "for this locale." << endl;
47                 return;
48         }
49         
50         // This part will have to be done for each frame
51         xim = XOpenIM (display, 0, 0, 0);
52         if (xim) {
53                 xic = XCreateIC(xim, XNInputStyle,
54                                 XIMPreeditNothing | XIMStatusNothing,
55                                 XNClientWindow, window,
56                                 XNFocusWindow, window, 
57                                 0);
58                 
59                 if (!xic) {
60                         lyxerr[Debug::KEY] << "InitLyXLookup: could not create "
61                                 "an input context" << endl;
62                         XCloseIM (xim);
63                         xim = 0;
64                 } 
65         }
66         else 
67                 lyxerr[Debug::KEY] << "InitLyXLookup: could not open "
68                         "an input method." << endl;
69 }
70
71
72 static
73 bool isDeadEvent(XEvent * event,
74                   char * buffer_return, int bytes_buffer,
75                   KeySym * keysym_return)
76 {
77         XLookupString(&event->xkey, buffer_return,
78                       bytes_buffer, keysym_return,
79                       0);
80         // Can this be done safely in any other way?
81         // This is all the dead keys I know of in X11R6.1
82         if (false
83 #ifdef XK_dead_grave
84             || *keysym_return == XK_dead_grave
85 #endif
86 #ifdef XK_dead_acute
87             || *keysym_return == XK_dead_acute
88 #endif
89 #ifdef XK_dead_circumflex
90             || *keysym_return == XK_dead_circumflex
91 #endif
92 #ifdef XK_dead_tilde
93             || *keysym_return == XK_dead_tilde
94 #endif
95 #ifdef XK_dead_macron
96             || *keysym_return == XK_dead_macron
97 #endif
98 #ifdef XK_dead_breve
99             || *keysym_return == XK_dead_breve
100 #endif
101 #ifdef XK_dead_abovedot
102             || *keysym_return == XK_dead_abovedot
103 #endif
104 #ifdef XK_dead_diaeresis
105             || *keysym_return == XK_dead_diaeresis
106 #endif
107 #ifdef XK_dead_abovering
108             || *keysym_return == XK_dead_abovering
109 #endif
110 #ifdef XK_dead_doubleacute
111             || *keysym_return == XK_dead_doubleacute
112 #endif
113 #ifdef XK_dead_caron
114             || *keysym_return == XK_dead_caron
115 #endif
116 #ifdef XK_dead_cedilla
117             || *keysym_return == XK_dead_cedilla
118 #endif
119 #ifdef XK_dead_ogonek
120             || *keysym_return == XK_dead_ogonek
121 #endif
122 #ifdef XK_dead_iota
123             || *keysym_return == XK_dead_iota
124 #endif
125 #ifdef XK_dead_voiced_sound
126             || *keysym_return == XK_dead_voiced_sound
127 #endif
128 #ifdef XK_dead_semivoiced_sound
129             || *keysym_return == XK_dead_semivoiced_sound
130 #endif
131 #ifdef XK_dead_belowdot
132             || *keysym_return == XK_dead_belowdot
133 #endif
134             )
135                 return true;
136         return false;
137 }
138
139
140 // This is called instead of XLookupString()
141 int LyXLookupString(XEvent * event,    
142                     char * buffer_return, int bytes_buffer,
143                     KeySym * keysym_return) 
144 {
145         int result = 0;
146         if (xic) {
147                 if (isDeadEvent(event, buffer_return, bytes_buffer,
148                                  keysym_return)) {
149                         return 0;
150                 }
151                 if (XFilterEvent (event, None)) {
152                         //lyxerr <<"XFilterEvent");
153                         *keysym_return = NoSymbol;
154                         return 0;
155                 }
156                 if (event->type != KeyPress)
157                         lyxerr << "LyXLookupString: wrong event type" 
158                                <<  event->type << endl;
159                 Status status_return;
160                 
161                 result =  XmbLookupString(xic, &event->xkey, buffer_return,
162                                        bytes_buffer, keysym_return,
163                                        &status_return);
164                 switch(status_return) {
165                 case XLookupBoth:
166                         //lyxerr <<"XLookupBoth");
167                         break;
168                 case XLookupChars:
169                         //lyxerr <<"XLookupChars");
170                         *keysym_return = NoSymbol;
171                         break;
172                 case XLookupKeySym:
173                         //lyxerr <<"XLookupKeySym");
174                         result = 0;
175                         break;
176                 default:
177                         //lyxerr <<"default");
178                         *keysym_return = NoSymbol;
179                         result = 0;
180                         break;
181                 }
182         } else {
183                 result = XLookupString(&event->xkey, buffer_return,
184                                   bytes_buffer, keysym_return,
185                                   &compose_status);
186         }
187         return result;
188 }
189
190 // This is called after the main window has been destroyed
191 void CloseLyXLookup() 
192 {
193         if (xic) {
194                 lyxerr[Debug::KEY] << "CloseLyXLookup: destroying input context"
195                                << endl;
196                 XDestroyIC(xic);
197                 xic = 0;
198                 XCloseIM(xim);
199         }
200 }
201
202
203 #else // We do not have XOpenIM, so we stick with normal XLookupString
204
205 #include <X11/Xlib.h>
206 #include <X11/Xutil.h>
207
208 XComposeStatus compose_status= {0, 0};
209
210 // This is called after the main LyX window has been created
211 void InitLyXLookup(Display *, Window ) 
212 {
213         //Nothing to do.
214 }
215
216 // This is called instead of XLookupString(). I this particular case,
217 // this *is* XLookupString...
218 int LyXLookupString(XEvent * event,    
219                     char * buffer_return, int bytes_buffer,
220                     KeySym * keysym_return) 
221 {
222         return XLookupString(&event->xkey, buffer_return,
223                                   bytes_buffer, keysym_return,
224                                   &compose_status);
225 }
226
227 // This is called after the main window has been destroyed
228 void CloseLyXLookup() 
229 {
230         // Nothing to do
231 }
232
233 #endif // HAVE_XOPENIM