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