1
/* vim:set noet ts=4: */
5
* Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this program; if not, write to the
19
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
* Boston, MA 02111-1307 USA
23
#include "ibus-input-context.h"
24
#include "ibus-client.h"
26
#include <QInputMethodEvent>
27
#include <QTextCharFormat>
29
typedef QInputMethodEvent::Attribute QAttribute;
31
IBusInputContext::IBusInputContext (QObject *parent, IBusClient *client, QString &ic)
32
: QInputContext (parent),
35
preedit_visible (false),
37
caps (IBUS_CAP_PREEDIT | IBUS_CAP_FOCUS)
41
IBusInputContext::~IBusInputContext ()
43
client->releaseInputContext (this);
47
IBusInputContext::filterEvent (const QEvent *event)
50
if (client->filterEvent (this, event)) {
53
return QInputContext::filterEvent (event);
55
return QInputContext::filterEvent (event);
60
IBusInputContext::font () const
62
return QInputContext::font ();
66
IBusInputContext::identifierName ()
68
return QString ("ibus");
72
IBusInputContext::language()
78
IBusInputContext::mouseHandler (int x, QMouseEvent *event)
80
client->mouseHandler (this, x, event);
81
QInputContext::mouseHandler (x, event);
85
IBusInputContext::reset()
91
IBusInputContext::update ()
93
QWidget *widget = focusWidget ();
99
QRect rect = widget->inputMethodQuery(Qt::ImMicroFocus).toRect ();
101
QPoint topleft = widget->mapToGlobal(QPoint(0,0));
102
rect.translate (topleft);
104
client->setCursorLocation (this, rect);
108
qDebug () << "== update == ";
109
value = widget->inputMethodQuery(Qt::ImMicroFocus);
110
qDebug () << "Qt::ImMicroFocus " << value;
111
value = widget->inputMethodQuery(Qt::ImFont);
112
qDebug () << "Qt::ImFont " <<value;
113
value = widget->inputMethodQuery(Qt::ImCursorPosition);
114
qDebug () << "Qt::ImCursorPosition " << value;
115
value = widget->inputMethodQuery(Qt::ImSurroundingText);
116
qDebug () << "Qt::ImSurroundingText " << value;
117
value = widget->inputMethodQuery(Qt::ImCurrentSelection);
118
qDebug () << "Qt::ImCurrentSelection " << value;
123
IBusInputContext::isComposing() const
125
return preedit_visible && !preedit_string.isEmpty ();
129
IBusInputContext::setFocusWidget (QWidget *widget)
131
QInputContext::setFocusWidget (widget);
133
if (widget == NULL) {
135
client->focusOut (this);
138
/* KateView can not support preedit well. */
139
if (widget->inherits("KateViewInternal")) {
140
caps &= ~IBUS_CAP_PREEDIT;
143
caps |= IBUS_CAP_PREEDIT;
145
client->setCapabilities (this, caps);
148
client->focusIn (this);
154
IBusInputContext::widgetDestroyed (QWidget *widget)
156
QInputContext::widgetDestroyed (widget);
159
setFocusWidget (NULL);
167
IBusInputContext::x11FilterEvent (QWidget *keywidget, XEvent *xevent)
169
if (client->x11FilterEvent (this, keywidget, xevent)) {
172
return QInputContext::x11FilterEvent (keywidget, xevent);
177
IBusInputContext::setIC (QString ic)
183
client->setCapabilities (this, caps);
185
client->focusIn (this);
190
IBusInputContext::getIC ()
196
IBusInputContext::commitString (QString text)
198
QInputMethodEvent event;
199
event.setCommitString (text);
205
IBusInputContext::updatePreedit (QString text, QList <QList <quint32> > attr_list, int cursor_pos, bool visible)
207
// qDebug () << text << cursor_pos << show;
208
QList <QAttribute> qattrs;
212
qattrs.append (QAttribute (QInputMethodEvent::Cursor, cursor_pos, true, 0));
215
for (QList <QList <quint32> >::iterator it = attr_list.begin (); it != attr_list.end(); ++ it) {
217
QList <quint32> attr = *it;
218
QTextCharFormat format;
222
format.setUnderlineStyle (QTextCharFormat::SingleUnderline);
224
case 2: // foreground
225
format.setForeground (QBrush (QColor (attr[1])));
227
case 3: // background
228
format.setBackground (QBrush (QColor (attr[1])));
234
qattrs.append (QAttribute (QInputMethodEvent::TextFormat, attr[2], attr[3] - attr[2], QVariant (format)));
235
// qDebug () << attr[0] << attr[2] << attr[3] - attr[2];
239
qattrs.append (QAttribute (QInputMethodEvent::Cursor, 0, true, 0));
244
preedit_string = text;
245
preedit_visible = visible;
246
preedit_attrs = attr_list;
247
preedit_cursor_pos = cursor_pos;
249
QInputMethodEvent event (text, qattrs);
255
IBusInputContext::showPreedit ()
260
updatePreedit (preedit_string, preedit_attrs, preedit_cursor_pos, TRUE);
264
IBusInputContext::hidePreedit ()
266
if (!preedit_visible)
269
updatePreedit (preedit_string, preedit_attrs, preedit_cursor_pos, FALSE);