0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the QtGui module of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
/****************************************************************************
|
|
43 |
**
|
|
44 |
** Implementation of QXIMInputContext class
|
|
45 |
**
|
|
46 |
** Copyright (C) 2003-2004 immodule for Qt Project. All rights reserved.
|
|
47 |
**
|
|
48 |
** This file is written to contribute to Nokia Corporation and/or its subsidiary(-ies) under their own
|
|
49 |
** license. You may use this file under your Qt license. Following
|
|
50 |
** description is copied from their original file headers. Contact
|
|
51 |
** immodule-qt@freedesktop.org if any conditions of this licensing are
|
|
52 |
** not clear to you.
|
|
53 |
**
|
|
54 |
****************************************************************************/
|
|
55 |
|
|
56 |
#include "qplatformdefs.h"
|
|
57 |
#include "qdebug.h"
|
|
58 |
#include "qximinputcontext_p.h"
|
|
59 |
|
|
60 |
#if !defined(QT_NO_IM)
|
|
61 |
|
|
62 |
QT_BEGIN_NAMESPACE
|
|
63 |
|
|
64 |
#if !defined(QT_NO_XIM)
|
|
65 |
|
|
66 |
QT_BEGIN_INCLUDE_NAMESPACE
|
|
67 |
#include "qplatformdefs.h"
|
|
68 |
|
|
69 |
#include "qapplication.h"
|
|
70 |
#include "qwidget.h"
|
|
71 |
#include "qstring.h"
|
|
72 |
#include "qlist.h"
|
|
73 |
#include "qtextcodec.h"
|
|
74 |
#include "qevent.h"
|
|
75 |
#include "qtextformat.h"
|
|
76 |
|
|
77 |
#include "qx11info_x11.h"
|
|
78 |
|
|
79 |
#include <stdlib.h>
|
|
80 |
#include <limits.h>
|
|
81 |
QT_END_INCLUDE_NAMESPACE
|
|
82 |
|
|
83 |
// #define QT_XIM_DEBUG
|
|
84 |
#ifdef QT_XIM_DEBUG
|
|
85 |
#define XIM_DEBUG qDebug
|
|
86 |
#else
|
|
87 |
#define XIM_DEBUG if (0) qDebug
|
|
88 |
#endif
|
|
89 |
|
|
90 |
// from qapplication_x11.cpp
|
|
91 |
// #### move to X11 struct
|
|
92 |
extern XIMStyle qt_xim_preferred_style;
|
|
93 |
extern char *qt_ximServer;
|
|
94 |
extern int qt_ximComposingKeycode;
|
|
95 |
extern QTextCodec * qt_input_mapper;
|
|
96 |
|
|
97 |
XIMStyle QXIMInputContext::xim_style = 0;
|
|
98 |
// moved from qapplication_x11.cpp
|
|
99 |
static const XIMStyle xim_default_style = XIMPreeditCallbacks | XIMStatusNothing;
|
|
100 |
|
|
101 |
|
|
102 |
extern "C" {
|
|
103 |
#ifdef USE_X11R6_XIM
|
|
104 |
static void xim_create_callback(XIM /*im*/,
|
|
105 |
XPointer client_data,
|
|
106 |
XPointer /*call_data*/)
|
|
107 |
{
|
|
108 |
QXIMInputContext *qic = reinterpret_cast<QXIMInputContext *>(client_data);
|
|
109 |
// qDebug("xim_create_callback");
|
|
110 |
qic->create_xim();
|
|
111 |
}
|
|
112 |
|
|
113 |
static void xim_destroy_callback(XIM /*im*/,
|
|
114 |
XPointer client_data,
|
|
115 |
XPointer /*call_data*/)
|
|
116 |
{
|
|
117 |
QXIMInputContext *qic = reinterpret_cast<QXIMInputContext *>(client_data);
|
|
118 |
// qDebug("xim_destroy_callback");
|
|
119 |
qic->close_xim();
|
|
120 |
XRegisterIMInstantiateCallback(X11->display, 0, 0, 0,
|
|
121 |
(XIMProc) xim_create_callback, reinterpret_cast<char *>(qic));
|
|
122 |
}
|
|
123 |
#endif // USE_X11R6_XIM
|
|
124 |
|
|
125 |
static int xic_start_callback(XIC, XPointer client_data, XPointer) {
|
|
126 |
QXIMInputContext *qic = (QXIMInputContext *) client_data;
|
|
127 |
if (!qic) {
|
|
128 |
XIM_DEBUG("xic_start_callback: no qic");
|
|
129 |
return 0;
|
|
130 |
}
|
|
131 |
QXIMInputContext::ICData *data = qic->icData();
|
|
132 |
if (!data) {
|
|
133 |
XIM_DEBUG("xic_start_callback: no ic data");
|
|
134 |
return 0;
|
|
135 |
}
|
|
136 |
XIM_DEBUG("xic_start_callback");
|
|
137 |
|
|
138 |
data->clear();
|
|
139 |
data->composing = true;
|
|
140 |
|
|
141 |
return 0;
|
|
142 |
}
|
|
143 |
|
|
144 |
static int xic_draw_callback(XIC, XPointer client_data, XPointer call_data) {
|
|
145 |
QXIMInputContext *qic = (QXIMInputContext *) client_data;
|
|
146 |
if (!qic) {
|
|
147 |
XIM_DEBUG("xic_draw_callback: no qic");
|
|
148 |
return 0;
|
|
149 |
}
|
|
150 |
QXIMInputContext::ICData *data = qic->icData();
|
|
151 |
if (!data) {
|
|
152 |
XIM_DEBUG("xic_draw_callback: no ic data");
|
|
153 |
return 0;
|
|
154 |
}
|
|
155 |
XIM_DEBUG("xic_draw_callback");
|
|
156 |
|
|
157 |
|
|
158 |
if(!data->composing) {
|
|
159 |
data->clear();
|
|
160 |
data->composing = true;
|
|
161 |
}
|
|
162 |
|
|
163 |
XIMPreeditDrawCallbackStruct *drawstruct = (XIMPreeditDrawCallbackStruct *) call_data;
|
|
164 |
XIMText *text = (XIMText *) drawstruct->text;
|
|
165 |
int cursor = drawstruct->caret, sellen = 0, selstart = 0;
|
|
166 |
|
|
167 |
if (!drawstruct->caret && !drawstruct->chg_first && !drawstruct->chg_length && !text) {
|
|
168 |
if(data->text.isEmpty()) {
|
|
169 |
XIM_DEBUG("compose emptied");
|
|
170 |
// if the composition string has been emptied, we need
|
|
171 |
// to send an InputMethodEnd event
|
|
172 |
QInputMethodEvent e;
|
|
173 |
qic->sendEvent(e);
|
|
174 |
data->clear();
|
|
175 |
|
|
176 |
// if the commit string has coming after here, InputMethodStart
|
|
177 |
// will be sent dynamically
|
|
178 |
}
|
|
179 |
return 0;
|
|
180 |
}
|
|
181 |
|
|
182 |
if (text) {
|
|
183 |
char *str = 0;
|
|
184 |
if (text->encoding_is_wchar) {
|
|
185 |
int l = wcstombs(NULL, text->string.wide_char, text->length);
|
|
186 |
if (l != -1) {
|
|
187 |
str = new char[l + 1];
|
|
188 |
wcstombs(str, text->string.wide_char, l);
|
|
189 |
str[l] = 0;
|
|
190 |
}
|
|
191 |
} else
|
|
192 |
str = text->string.multi_byte;
|
|
193 |
|
|
194 |
if (!str)
|
|
195 |
return 0;
|
|
196 |
|
|
197 |
QString s = QString::fromLocal8Bit(str);
|
|
198 |
|
|
199 |
if (text->encoding_is_wchar)
|
|
200 |
delete [] str;
|
|
201 |
|
|
202 |
if (drawstruct->chg_length < 0)
|
|
203 |
data->text.replace(drawstruct->chg_first, INT_MAX, s);
|
|
204 |
else
|
|
205 |
data->text.replace(drawstruct->chg_first, drawstruct->chg_length, s);
|
|
206 |
|
|
207 |
if (data->selectedChars.size() < data->text.length()) {
|
|
208 |
// expand the selectedChars array if the compose string is longer
|
|
209 |
int from = data->selectedChars.size();
|
|
210 |
data->selectedChars.resize(data->text.length());
|
|
211 |
for (int x = from; x < data->selectedChars.size(); ++x)
|
|
212 |
data->selectedChars.clearBit(x);
|
|
213 |
}
|
|
214 |
|
|
215 |
// determine if the changed chars are selected based on text->feedback
|
|
216 |
for (int x = 0; x < text->length; ++x)
|
|
217 |
data->selectedChars.setBit(x + drawstruct->chg_first,
|
|
218 |
(text->feedback ? (text->feedback[x] & XIMReverse) : 0));
|
|
219 |
|
|
220 |
// figure out where the selection starts, and how long it is
|
|
221 |
bool started = false;
|
|
222 |
for (int x = 0; x < qMin(data->selectedChars.size(), data->text.length()); ++x) {
|
|
223 |
if (started) {
|
|
224 |
if (data->selectedChars.testBit(x)) ++sellen;
|
|
225 |
else break;
|
|
226 |
} else {
|
|
227 |
if (data->selectedChars.testBit(x)) {
|
|
228 |
selstart = x;
|
|
229 |
started = true;
|
|
230 |
sellen = 1;
|
|
231 |
}
|
|
232 |
}
|
|
233 |
}
|
|
234 |
} else {
|
|
235 |
if (drawstruct->chg_length == 0)
|
|
236 |
drawstruct->chg_length = -1;
|
|
237 |
|
|
238 |
data->text.remove(drawstruct->chg_first, drawstruct->chg_length);
|
|
239 |
bool qt_compose_emptied = data->text.isEmpty();
|
|
240 |
if (qt_compose_emptied) {
|
|
241 |
XIM_DEBUG("compose emptied 2 text=%s", data->text.toUtf8().constData());
|
|
242 |
// if the composition string has been emptied, we need
|
|
243 |
// to send an InputMethodEnd event
|
|
244 |
QInputMethodEvent e;
|
|
245 |
qic->sendEvent(e);
|
|
246 |
data->clear();
|
|
247 |
// if the commit string has coming after here, InputMethodStart
|
|
248 |
// will be sent dynamically
|
|
249 |
return 0;
|
|
250 |
}
|
|
251 |
}
|
|
252 |
|
|
253 |
XIM_DEBUG("sending compose: '%s', cursor=%d, sellen=%d",
|
|
254 |
data->text.toUtf8().constData(), cursor, sellen);
|
|
255 |
QList<QInputMethodEvent::Attribute> attrs;
|
|
256 |
if (selstart > 0)
|
|
257 |
attrs << QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, 0, selstart,
|
|
258 |
qic->standardFormat(QInputContext::PreeditFormat));
|
|
259 |
if (sellen)
|
|
260 |
attrs << QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, selstart, sellen,
|
|
261 |
qic->standardFormat(QInputContext::SelectionFormat));
|
|
262 |
if (selstart + sellen < data->text.length())
|
|
263 |
attrs << QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat,
|
|
264 |
selstart + sellen, data->text.length() - selstart - sellen,
|
|
265 |
qic->standardFormat(QInputContext::PreeditFormat));
|
|
266 |
attrs << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, cursor, sellen ? 0 : 1, QVariant());
|
|
267 |
QInputMethodEvent e(data->text, attrs);
|
|
268 |
data->preeditEmpty = data->text.isEmpty();
|
|
269 |
qic->sendEvent(e);
|
|
270 |
|
|
271 |
return 0;
|
|
272 |
}
|
|
273 |
|
|
274 |
static int xic_done_callback(XIC, XPointer client_data, XPointer) {
|
|
275 |
QXIMInputContext *qic = (QXIMInputContext *) client_data;
|
|
276 |
if (!qic)
|
|
277 |
return 0;
|
|
278 |
|
|
279 |
XIM_DEBUG("xic_done_callback");
|
|
280 |
// Don't send InputMethodEnd here. QXIMInputContext::x11FilterEvent()
|
|
281 |
// handles InputMethodEnd with commit string.
|
|
282 |
return 0;
|
|
283 |
}
|
|
284 |
}
|
|
285 |
|
|
286 |
void QXIMInputContext::ICData::clear()
|
|
287 |
{
|
|
288 |
text = QString();
|
|
289 |
selectedChars.clear();
|
|
290 |
composing = false;
|
|
291 |
preeditEmpty = true;
|
|
292 |
}
|
|
293 |
|
|
294 |
QXIMInputContext::ICData *QXIMInputContext::icData() const
|
|
295 |
{
|
|
296 |
if (QWidget *w = focusWidget())
|
|
297 |
return ximData.value(w->effectiveWinId());
|
|
298 |
return 0;
|
|
299 |
}
|
|
300 |
/* The cache here is needed, as X11 leaks a few kb for every
|
|
301 |
XFreeFontSet call, so we avoid creating and deletion of fontsets as
|
|
302 |
much as possible
|
|
303 |
*/
|
|
304 |
static XFontSet fontsetCache[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
305 |
static int fontsetRefCount = 0;
|
|
306 |
|
|
307 |
static const char * const fontsetnames[] = {
|
|
308 |
"-*-fixed-medium-r-*-*-16-*,-*-*-medium-r-*-*-16-*",
|
|
309 |
"-*-fixed-medium-i-*-*-16-*,-*-*-medium-i-*-*-16-*",
|
|
310 |
"-*-fixed-bold-r-*-*-16-*,-*-*-bold-r-*-*-16-*",
|
|
311 |
"-*-fixed-bold-i-*-*-16-*,-*-*-bold-i-*-*-16-*",
|
|
312 |
"-*-fixed-medium-r-*-*-24-*,-*-*-medium-r-*-*-24-*",
|
|
313 |
"-*-fixed-medium-i-*-*-24-*,-*-*-medium-i-*-*-24-*",
|
|
314 |
"-*-fixed-bold-r-*-*-24-*,-*-*-bold-r-*-*-24-*",
|
|
315 |
"-*-fixed-bold-i-*-*-24-*,-*-*-bold-i-*-*-24-*"
|
|
316 |
};
|
|
317 |
|
|
318 |
static XFontSet getFontSet(const QFont &f)
|
|
319 |
{
|
|
320 |
int i = 0;
|
|
321 |
if (f.italic())
|
|
322 |
i |= 1;
|
|
323 |
if (f.bold())
|
|
324 |
i |= 2;
|
|
325 |
|
|
326 |
if (f.pointSize() > 20)
|
|
327 |
i += 4;
|
|
328 |
|
|
329 |
if (!fontsetCache[i]) {
|
|
330 |
Display* dpy = X11->display;
|
|
331 |
int missCount;
|
|
332 |
char** missList;
|
|
333 |
fontsetCache[i] = XCreateFontSet(dpy, fontsetnames[i], &missList, &missCount, 0);
|
|
334 |
if(missCount > 0)
|
|
335 |
XFreeStringList(missList);
|
|
336 |
if (!fontsetCache[i]) {
|
|
337 |
fontsetCache[i] = XCreateFontSet(dpy, "-*-fixed-*-*-*-*-16-*", &missList, &missCount, 0);
|
|
338 |
if(missCount > 0)
|
|
339 |
XFreeStringList(missList);
|
|
340 |
if (!fontsetCache[i])
|
|
341 |
fontsetCache[i] = (XFontSet)-1;
|
|
342 |
}
|
|
343 |
}
|
|
344 |
return (fontsetCache[i] == (XFontSet)-1) ? 0 : fontsetCache[i];
|
|
345 |
}
|
|
346 |
|
|
347 |
|
|
348 |
|
|
349 |
QXIMInputContext::QXIMInputContext()
|
|
350 |
{
|
|
351 |
if (!qt_xim_preferred_style) // no configured input style, use the default
|
|
352 |
qt_xim_preferred_style = xim_default_style;
|
|
353 |
|
|
354 |
xim = 0;
|
|
355 |
QByteArray ximServerName(qt_ximServer);
|
|
356 |
if (qt_ximServer)
|
|
357 |
ximServerName.prepend("@im=");
|
|
358 |
else
|
|
359 |
ximServerName = "";
|
|
360 |
|
|
361 |
if (!XSupportsLocale())
|
|
362 |
#ifndef QT_NO_DEBUG
|
|
363 |
qWarning("Qt: Locale not supported on X server")
|
|
364 |
#endif
|
|
365 |
;
|
|
366 |
#ifdef USE_X11R6_XIM
|
|
367 |
else if (XSetLocaleModifiers (ximServerName.constData()) == 0)
|
|
368 |
qWarning("Qt: Cannot set locale modifiers: %s", ximServerName.constData());
|
|
369 |
else
|
|
370 |
XRegisterIMInstantiateCallback(X11->display, 0, 0, 0,
|
|
371 |
(XIMProc) xim_create_callback, reinterpret_cast<char *>(this));
|
|
372 |
#else // !USE_X11R6_XIM
|
|
373 |
else if (XSetLocaleModifiers ("") == 0)
|
|
374 |
qWarning("Qt: Cannot set locale modifiers");
|
|
375 |
else
|
|
376 |
QXIMInputContext::create_xim();
|
|
377 |
#endif // USE_X11R6_XIM
|
|
378 |
}
|
|
379 |
|
|
380 |
|
|
381 |
/*!\internal
|
|
382 |
Creates the application input method.
|
|
383 |
*/
|
|
384 |
void QXIMInputContext::create_xim()
|
|
385 |
{
|
|
386 |
++fontsetRefCount;
|
|
387 |
#ifndef QT_NO_XIM
|
|
388 |
xim = XOpenIM(X11->display, 0, 0, 0);
|
|
389 |
if (xim) {
|
|
390 |
|
|
391 |
#ifdef USE_X11R6_XIM
|
|
392 |
XIMCallback destroy;
|
|
393 |
destroy.callback = (XIMProc) xim_destroy_callback;
|
|
394 |
destroy.client_data = XPointer(this);
|
|
395 |
if (XSetIMValues(xim, XNDestroyCallback, &destroy, (char *) 0) != 0)
|
|
396 |
qWarning("Xlib doesn't support destroy callback");
|
|
397 |
#endif // USE_X11R6_XIM
|
|
398 |
|
|
399 |
XIMStyles *styles = 0;
|
|
400 |
XGetIMValues(xim, XNQueryInputStyle, &styles, (char *) 0, (char *) 0);
|
|
401 |
if (styles) {
|
|
402 |
int i;
|
|
403 |
for (i = 0; !xim_style && i < styles->count_styles; i++) {
|
|
404 |
if (styles->supported_styles[i] == qt_xim_preferred_style) {
|
|
405 |
xim_style = qt_xim_preferred_style;
|
|
406 |
break;
|
|
407 |
}
|
|
408 |
}
|
|
409 |
// if the preferred input style couldn't be found, look for
|
|
410 |
// Nothing
|
|
411 |
for (i = 0; !xim_style && i < styles->count_styles; i++) {
|
|
412 |
if (styles->supported_styles[i] == (XIMPreeditNothing | XIMStatusNothing)) {
|
|
413 |
xim_style = XIMPreeditNothing | XIMStatusNothing;
|
|
414 |
break;
|
|
415 |
}
|
|
416 |
}
|
|
417 |
// ... and failing that, None.
|
|
418 |
for (i = 0; !xim_style && i < styles->count_styles; i++) {
|
|
419 |
if (styles->supported_styles[i] == (XIMPreeditNone |
|
|
420 |
XIMStatusNone)) {
|
|
421 |
xim_style = XIMPreeditNone | XIMStatusNone;
|
|
422 |
break;
|
|
423 |
}
|
|
424 |
}
|
|
425 |
|
|
426 |
// qDebug("QApplication: using im style %lx", xim_style);
|
|
427 |
XFree((char *)styles);
|
|
428 |
}
|
|
429 |
|
|
430 |
if (xim_style) {
|
|
431 |
|
|
432 |
#ifdef USE_X11R6_XIM
|
|
433 |
XUnregisterIMInstantiateCallback(X11->display, 0, 0, 0,
|
|
434 |
(XIMProc) xim_create_callback, reinterpret_cast<char *>(this));
|
|
435 |
#endif // USE_X11R6_XIM
|
|
436 |
|
|
437 |
if (QWidget *focusWidget = QApplication::focusWidget()) {
|
|
438 |
// reinitialize input context after the input method
|
|
439 |
// server (like SCIM) has been launched without
|
|
440 |
// requiring the user to manually switch focus.
|
|
441 |
if (focusWidget->testAttribute(Qt::WA_InputMethodEnabled)
|
|
442 |
&& focusWidget->testAttribute(Qt::WA_WState_Created)
|
|
443 |
&& focusWidget->isEnabled())
|
|
444 |
setFocusWidget(focusWidget);
|
|
445 |
}
|
|
446 |
// following code fragment is not required for immodule
|
|
447 |
// version of XIM
|
|
448 |
#if 0
|
|
449 |
QWidgetList list = qApp->topLevelWidgets();
|
|
450 |
for (int i = 0; i < list.size(); ++i) {
|
|
451 |
QWidget *w = list.at(i);
|
|
452 |
w->d->createTLSysExtra();
|
|
453 |
}
|
|
454 |
#endif
|
|
455 |
} else {
|
|
456 |
// Give up
|
|
457 |
qWarning("No supported input style found."
|
|
458 |
" See InputMethod documentation.");
|
|
459 |
close_xim();
|
|
460 |
}
|
|
461 |
}
|
|
462 |
#endif // QT_NO_XIM
|
|
463 |
}
|
|
464 |
|
|
465 |
/*!\internal
|
|
466 |
Closes the application input method.
|
|
467 |
*/
|
|
468 |
void QXIMInputContext::close_xim()
|
|
469 |
{
|
|
470 |
for(QHash<WId, ICData *>::const_iterator i = ximData.constBegin(),
|
|
471 |
e = ximData.constEnd(); i != e; ++i) {
|
|
472 |
ICData *data = i.value();
|
|
473 |
if (data->ic)
|
|
474 |
XDestroyIC(data->ic);
|
|
475 |
delete data;
|
|
476 |
}
|
|
477 |
ximData.clear();
|
|
478 |
|
|
479 |
if ( --fontsetRefCount == 0 ) {
|
|
480 |
Display *dpy = X11->display;
|
|
481 |
for ( int i = 0; i < 8; i++ ) {
|
|
482 |
if ( fontsetCache[i] && fontsetCache[i] != (XFontSet)-1 ) {
|
|
483 |
XFreeFontSet(dpy, fontsetCache[i]);
|
|
484 |
fontsetCache[i] = 0;
|
|
485 |
}
|
|
486 |
}
|
|
487 |
}
|
|
488 |
|
|
489 |
setFocusWidget(0);
|
|
490 |
xim = 0;
|
|
491 |
}
|
|
492 |
|
|
493 |
|
|
494 |
|
|
495 |
QXIMInputContext::~QXIMInputContext()
|
|
496 |
{
|
|
497 |
XIM old_xim = xim; // close_xim clears xim pointer.
|
|
498 |
close_xim();
|
|
499 |
if (old_xim)
|
|
500 |
XCloseIM(old_xim);
|
|
501 |
}
|
|
502 |
|
|
503 |
|
|
504 |
QString QXIMInputContext::identifierName()
|
|
505 |
{
|
|
506 |
// the name should be "xim" rather than "XIM" to be consistent
|
|
507 |
// with corresponding immodule of GTK+
|
|
508 |
return QLatin1String("xim");
|
|
509 |
}
|
|
510 |
|
|
511 |
|
|
512 |
QString QXIMInputContext::language()
|
|
513 |
{
|
|
514 |
QString language;
|
|
515 |
if (xim) {
|
|
516 |
QByteArray locale(XLocaleOfIM(xim));
|
|
517 |
|
|
518 |
if (locale.startsWith("zh")) {
|
|
519 |
// Chinese language should be formed as "zh_CN", "zh_TW", "zh_HK"
|
|
520 |
language = QLatin1String(locale.left(5));
|
|
521 |
} else {
|
|
522 |
// other languages should be two-letter ISO 639 language code
|
|
523 |
language = QLatin1String(locale.left(2));
|
|
524 |
}
|
|
525 |
}
|
|
526 |
return language;
|
|
527 |
}
|
|
528 |
|
|
529 |
void QXIMInputContext::reset()
|
|
530 |
{
|
|
531 |
QWidget *w = focusWidget();
|
|
532 |
if (!w)
|
|
533 |
return;
|
|
534 |
|
|
535 |
ICData *data = ximData.value(w->effectiveWinId());
|
|
536 |
if (!data)
|
|
537 |
return;
|
|
538 |
|
|
539 |
if (data->ic) {
|
|
540 |
char *mb = XmbResetIC(data->ic);
|
|
541 |
QInputMethodEvent e;
|
|
542 |
if (mb) {
|
|
543 |
e.setCommitString(QString::fromLocal8Bit(mb));
|
|
544 |
XFree(mb);
|
|
545 |
data->preeditEmpty = false; // force sending an event
|
|
546 |
}
|
|
547 |
if (!data->preeditEmpty) {
|
|
548 |
sendEvent(e);
|
|
549 |
update();
|
|
550 |
}
|
|
551 |
}
|
|
552 |
data->clear();
|
|
553 |
}
|
|
554 |
|
|
555 |
void QXIMInputContext::widgetDestroyed(QWidget *w)
|
|
556 |
{
|
|
557 |
QInputContext::widgetDestroyed(w);
|
|
558 |
ICData *data = ximData.take(w->effectiveWinId());
|
|
559 |
if (!data)
|
|
560 |
return;
|
|
561 |
|
|
562 |
data->clear();
|
|
563 |
if (data->ic)
|
|
564 |
XDestroyIC(data->ic);
|
|
565 |
delete data;
|
|
566 |
}
|
|
567 |
|
|
568 |
void QXIMInputContext::mouseHandler(int pos, QMouseEvent *e)
|
|
569 |
{
|
|
570 |
if(e->type() != QEvent::MouseButtonPress)
|
|
571 |
return;
|
|
572 |
|
|
573 |
XIM_DEBUG("QXIMInputContext::mouseHandler pos=%d", pos);
|
|
574 |
if (QWidget *w = focusWidget()) {
|
|
575 |
ICData *data = ximData.value(w->effectiveWinId());
|
|
576 |
if (!data)
|
|
577 |
return;
|
|
578 |
if (pos < 0 || pos > data->text.length())
|
|
579 |
reset();
|
|
580 |
// ##### handle mouse position
|
|
581 |
}
|
|
582 |
}
|
|
583 |
|
|
584 |
bool QXIMInputContext::isComposing() const
|
|
585 |
{
|
|
586 |
QWidget *w = focusWidget();
|
|
587 |
if (!w)
|
|
588 |
return false;
|
|
589 |
|
|
590 |
ICData *data = ximData.value(w->effectiveWinId());
|
|
591 |
if (!data)
|
|
592 |
return false;
|
|
593 |
return data->composing;
|
|
594 |
}
|
|
595 |
|
|
596 |
void QXIMInputContext::setFocusWidget(QWidget *w)
|
|
597 |
{
|
|
598 |
if (!xim)
|
|
599 |
return;
|
|
600 |
QWidget *oldFocus = focusWidget();
|
|
601 |
if (oldFocus == w)
|
|
602 |
return;
|
|
603 |
|
|
604 |
if (language() != QLatin1String("ja"))
|
|
605 |
reset();
|
|
606 |
|
|
607 |
if (oldFocus) {
|
|
608 |
ICData *data = ximData.value(oldFocus->effectiveWinId());
|
|
609 |
if (data && data->ic)
|
|
610 |
XUnsetICFocus(data->ic);
|
|
611 |
}
|
|
612 |
|
|
613 |
QInputContext::setFocusWidget(w);
|
|
614 |
|
|
615 |
if (!w || w->inputMethodHints() & (Qt::ImhExclusiveInputMask | Qt::ImhHiddenText))
|
|
616 |
return;
|
|
617 |
|
|
618 |
ICData *data = ximData.value(w->effectiveWinId());
|
|
619 |
if (!data)
|
|
620 |
data = createICData(w);
|
|
621 |
|
|
622 |
if (data->ic)
|
|
623 |
XSetICFocus(data->ic);
|
|
624 |
|
|
625 |
update();
|
|
626 |
}
|
|
627 |
|
|
628 |
|
|
629 |
bool QXIMInputContext::x11FilterEvent(QWidget *keywidget, XEvent *event)
|
|
630 |
{
|
|
631 |
int xkey_keycode = event->xkey.keycode;
|
|
632 |
if (!keywidget->testAttribute(Qt::WA_WState_Created))
|
|
633 |
return false;
|
|
634 |
if (XFilterEvent(event, keywidget->effectiveWinId())) {
|
|
635 |
qt_ximComposingKeycode = xkey_keycode; // ### not documented in xlib
|
|
636 |
|
|
637 |
update();
|
|
638 |
|
|
639 |
return true;
|
|
640 |
}
|
|
641 |
if (event->type != XKeyPress || event->xkey.keycode != 0)
|
|
642 |
return false;
|
|
643 |
|
|
644 |
QWidget *w = focusWidget();
|
|
645 |
if (keywidget != w)
|
|
646 |
return false;
|
|
647 |
ICData *data = ximData.value(w->effectiveWinId());
|
|
648 |
if (!data)
|
|
649 |
return false;
|
|
650 |
|
|
651 |
// input method has sent us a commit string
|
|
652 |
QByteArray string;
|
|
653 |
string.resize(513);
|
|
654 |
KeySym key; // unused
|
|
655 |
Status status; // unused
|
|
656 |
QString text;
|
|
657 |
int count = XmbLookupString(data->ic, &event->xkey, string.data(), string.size(),
|
|
658 |
&key, &status);
|
|
659 |
|
|
660 |
if (status == XBufferOverflow) {
|
|
661 |
string.resize(count + 1);
|
|
662 |
count = XmbLookupString(data->ic, &event->xkey, string.data(), string.size(),
|
|
663 |
&key, &status);
|
|
664 |
}
|
|
665 |
if (count > 0) {
|
|
666 |
// XmbLookupString() gave us some text, convert it to unicode
|
|
667 |
text = qt_input_mapper->toUnicode(string.constData() , count);
|
|
668 |
if (text.isEmpty()) {
|
|
669 |
// codec couldn't convert to unicode? this can happen when running in the
|
|
670 |
// C locale (or with no LANG set). try converting from latin-1
|
|
671 |
text = QString::fromLatin1(string.constData(), count);
|
|
672 |
}
|
|
673 |
}
|
|
674 |
|
|
675 |
#if 0
|
|
676 |
if (!(xim_style & XIMPreeditCallbacks) || !isComposing()) {
|
|
677 |
// ############### send a regular key event here!
|
|
678 |
;
|
|
679 |
}
|
|
680 |
#endif
|
|
681 |
|
|
682 |
QInputMethodEvent e;
|
|
683 |
e.setCommitString(text);
|
|
684 |
sendEvent(e);
|
|
685 |
data->clear();
|
|
686 |
|
|
687 |
update();
|
|
688 |
|
|
689 |
return true;
|
|
690 |
}
|
|
691 |
|
|
692 |
|
|
693 |
QXIMInputContext::ICData *QXIMInputContext::createICData(QWidget *w)
|
|
694 |
{
|
|
695 |
ICData *data = new ICData;
|
|
696 |
data->widget = w;
|
|
697 |
data->preeditEmpty = true;
|
|
698 |
|
|
699 |
XVaNestedList preedit_attr = 0;
|
|
700 |
XIMCallback startcallback, drawcallback, donecallback;
|
|
701 |
|
|
702 |
QFont font = w->font();
|
|
703 |
data->fontset = getFontSet(font);
|
|
704 |
|
|
705 |
if (xim_style & XIMPreeditArea) {
|
|
706 |
XRectangle rect;
|
|
707 |
rect.x = 0;
|
|
708 |
rect.y = 0;
|
|
709 |
rect.width = w->width();
|
|
710 |
rect.height = w->height();
|
|
711 |
|
|
712 |
preedit_attr = XVaCreateNestedList(0,
|
|
713 |
XNArea, &rect,
|
|
714 |
XNFontSet, data->fontset,
|
|
715 |
(char *) 0);
|
|
716 |
} else if (xim_style & XIMPreeditPosition) {
|
|
717 |
XPoint spot;
|
|
718 |
spot.x = 1;
|
|
719 |
spot.y = 1;
|
|
720 |
|
|
721 |
preedit_attr = XVaCreateNestedList(0,
|
|
722 |
XNSpotLocation, &spot,
|
|
723 |
XNFontSet, data->fontset,
|
|
724 |
(char *) 0);
|
|
725 |
} else if (xim_style & XIMPreeditCallbacks) {
|
|
726 |
startcallback.client_data = (XPointer) this;
|
|
727 |
startcallback.callback = (XIMProc) xic_start_callback;
|
|
728 |
drawcallback.client_data = (XPointer) this;
|
|
729 |
drawcallback.callback = (XIMProc)xic_draw_callback;
|
|
730 |
donecallback.client_data = (XPointer) this;
|
|
731 |
donecallback.callback = (XIMProc) xic_done_callback;
|
|
732 |
|
|
733 |
preedit_attr = XVaCreateNestedList(0,
|
|
734 |
XNPreeditStartCallback, &startcallback,
|
|
735 |
XNPreeditDrawCallback, &drawcallback,
|
|
736 |
XNPreeditDoneCallback, &donecallback,
|
|
737 |
(char *) 0);
|
|
738 |
}
|
|
739 |
|
|
740 |
if (preedit_attr) {
|
|
741 |
data->ic = XCreateIC(xim,
|
|
742 |
XNInputStyle, xim_style,
|
|
743 |
XNClientWindow, w->effectiveWinId(),
|
|
744 |
XNPreeditAttributes, preedit_attr,
|
|
745 |
(char *) 0);
|
|
746 |
XFree(preedit_attr);
|
|
747 |
} else {
|
|
748 |
data->ic = XCreateIC(xim,
|
|
749 |
XNInputStyle, xim_style,
|
|
750 |
XNClientWindow, w->effectiveWinId(),
|
|
751 |
(char *) 0);
|
|
752 |
}
|
|
753 |
|
|
754 |
if (data->ic) {
|
|
755 |
// when resetting the input context, preserve the input state
|
|
756 |
(void) XSetICValues(data->ic, XNResetState, XIMPreserveState, (char *) 0);
|
|
757 |
} else {
|
|
758 |
qWarning("Failed to create XIC");
|
|
759 |
}
|
|
760 |
|
|
761 |
ximData[w->effectiveWinId()] = data;
|
|
762 |
return data;
|
|
763 |
}
|
|
764 |
|
|
765 |
void QXIMInputContext::update()
|
|
766 |
{
|
|
767 |
QWidget *w = focusWidget();
|
|
768 |
if (!w)
|
|
769 |
return;
|
|
770 |
|
|
771 |
ICData *data = ximData.value(w->effectiveWinId());
|
|
772 |
if (!data || !data->ic)
|
|
773 |
return;
|
|
774 |
|
|
775 |
QRect r = w->inputMethodQuery(Qt::ImMicroFocus).toRect();
|
|
776 |
QPoint p;
|
|
777 |
if (w->nativeParentWidget())
|
|
778 |
p = w->mapTo(w->nativeParentWidget(), QPoint((r.left() + r.right() + 1)/2, r.bottom()));
|
|
779 |
else
|
|
780 |
p = QPoint((r.left() + r.right() + 1)/2, r.bottom());
|
|
781 |
XPoint spot;
|
|
782 |
spot.x = p.x();
|
|
783 |
spot.y = p.y();
|
|
784 |
|
|
785 |
r = w->rect();
|
|
786 |
XRectangle area;
|
|
787 |
area.x = r.x();
|
|
788 |
area.y = r.y();
|
|
789 |
area.width = r.width();
|
|
790 |
area.height = r.height();
|
|
791 |
|
|
792 |
XFontSet fontset = getFontSet(qvariant_cast<QFont>(w->inputMethodQuery(Qt::ImFont)));
|
|
793 |
if (data->fontset == fontset)
|
|
794 |
fontset = 0;
|
|
795 |
else
|
|
796 |
data->fontset = fontset;
|
|
797 |
|
|
798 |
XVaNestedList preedit_attr;
|
|
799 |
if (fontset)
|
|
800 |
preedit_attr = XVaCreateNestedList(0,
|
|
801 |
XNSpotLocation, &spot,
|
|
802 |
XNArea, &area,
|
|
803 |
XNFontSet, fontset,
|
|
804 |
(char *) 0);
|
|
805 |
else
|
|
806 |
preedit_attr = XVaCreateNestedList(0,
|
|
807 |
XNSpotLocation, &spot,
|
|
808 |
XNArea, &area,
|
|
809 |
(char *) 0);
|
|
810 |
|
|
811 |
XSetICValues(data->ic, XNPreeditAttributes, preedit_attr, (char *) 0);
|
|
812 |
XFree(preedit_attr);
|
|
813 |
}
|
|
814 |
|
|
815 |
|
|
816 |
#else
|
|
817 |
/*
|
|
818 |
When QT_NO_XIM is defined, we provide a dummy implementation for
|
|
819 |
this class. The reason for this is that the header file is moc'ed
|
|
820 |
regardless of QT_NO_XIM. The best would be to remove the file
|
|
821 |
completely from the pri file is QT_NO_XIM was defined, or for moc
|
|
822 |
to understand this preprocessor directive. Since the header does
|
|
823 |
not declare this class when QT_NO_XIM is defined, this is dead
|
|
824 |
code.
|
|
825 |
*/
|
|
826 |
bool QXIMInputContext::isComposing() const { return false; }
|
|
827 |
QString QXIMInputContext::identifierName() { return QString(); }
|
|
828 |
void QXIMInputContext::mouseHandler(int, QMouseEvent *) {}
|
|
829 |
void QXIMInputContext::setFocusWidget(QWidget *) {}
|
|
830 |
void QXIMInputContext::reset() {}
|
|
831 |
void QXIMInputContext::update() {}
|
|
832 |
QXIMInputContext::~QXIMInputContext() {}
|
|
833 |
void QXIMInputContext::widgetDestroyed(QWidget *) {}
|
|
834 |
QString QXIMInputContext::language() { return QString(); }
|
|
835 |
bool QXIMInputContext::x11FilterEvent(QWidget *, XEvent *) { return true; }
|
|
836 |
|
|
837 |
#endif //QT_NO_XIM
|
|
838 |
|
|
839 |
QT_END_NAMESPACE
|
|
840 |
|
|
841 |
#endif //QT_NO_IM
|