]> git.lyx.org Git - lyx.git/blob - src/lyxlookup.C
white-space changes, removed definitions.h several enum changes because of this,...
[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() << "InitLyXLookup: X does not support this locale." << endl;
39                 return;
40         } 
41         if (!XSetLocaleModifiers("")) {
42                 lyxerr.debug() << "InitLyXLookup: Could not set modifiers "
43                         "for this locale." << endl;
44                 return;
45         }
46
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" << endl;
59                         XCloseIM (xim);
60                         xim = 0;
61                 } 
62         }
63         else 
64                 lyxerr.debug() << "InitLyXLookup: could not open "
65                         "an input method." << endl;
66
67 }
68
69
70 static
71 bool isDeadEvent(XEvent *event,
72                   char *buffer_return, int bytes_buffer,
73                   KeySym *keysym_return)
74 {
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;
135 }
136
137
138 // This is called instead of XLookupString()
139 int LyXLookupString(XEvent *event,    
140                     char *buffer_return, int bytes_buffer,
141                     KeySym *keysym_return) 
142 {
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                                <<  event->type << endl;
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;
186 }
187
188 // This is called after the main window has been destroyed
189 void CloseLyXLookup() 
190 {
191         if (xic) {
192                 lyxerr.debug() << "CloseLyXLookup: destroying input context"
193                                << endl;
194                 XDestroyIC(xic);
195                 XCloseIM(xim);
196         }
197 }
198
199
200 #else // We do not have XOpenIM, so we stick with normal XLookupString
201
202 #include <X11/Xlib.h>
203 #include <X11/Xutil.h>
204
205 XComposeStatus compose_status= {0, 0};
206
207 // This is called after the main LyX window has been created
208 void InitLyXLookup(Display* , Window ) 
209 {
210         //Nothing to do.
211 }
212
213 // This is called instead of XLookupString(). I this particular case,
214 // this *is* XLookupString...
215 int LyXLookupString(XEvent *event,    
216                     char *buffer_return, int bytes_buffer,
217                     KeySym *keysym_return) 
218 {
219         return XLookupString(&event->xkey, buffer_return,
220                                   bytes_buffer, keysym_return,
221                                   &compose_status);
222 }
223
224 // This is called after the main window has been destroyed
225 void CloseLyXLookup() 
226 {
227         // Nothing to do
228 }
229
230
231 #endif // HAVE_XOPENIM