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