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
22 #include "ibus-input-context.h"
23 #include "ibus-client.h"
25 #include <QInputMethodEvent>
26 #include <QTextCharFormat>
28 typedef QInputMethodEvent::Attribute QAttribute
;
30 IBusInputContext::IBusInputContext (QObject
*parent
, IBusClient
*client
, QString
&ic
)
31 : QInputContext (parent
), client (client
), ic (ic
), preedit_visible (false)
35 IBusInputContext::~IBusInputContext ()
37 client
->releaseInputContext (this);
41 IBusInputContext::filterEvent (const QEvent
*event
)
44 if (client
->filterEvent (this, event
))
46 return QInputContext::filterEvent (event
);
48 return QInputContext::filterEvent (event
);
53 IBusInputContext::font () const
55 return QInputContext::font ();
59 IBusInputContext::identifierName ()
61 return QString ("ibus");
65 IBusInputContext::language()
71 IBusInputContext::mouseHandler (int x
, QMouseEvent
*event
)
73 client
->mouseHandler (this, x
, event
);
74 QInputContext::mouseHandler (x
, event
);
78 IBusInputContext::reset()
84 IBusInputContext::update ()
88 if ((widget
= focusWidget ()) == NULL
)
91 QRect rect
= widget
->inputMethodQuery(Qt::ImMicroFocus
).toRect ();
94 QFont font
= widget
->inputMethodQuery(Qt::ImFont
).value
<QFont
> ();
95 qDebug () << rect
<< preedit_string
<< preedit_cursor_pos
;
97 QFontMetrics
fm(font
);
98 int textWidth
= fm
.width (preedit_string
.left (preedit_cursor_pos
));
99 rect
.translate (textWidth
, 0);
102 QPoint topleft
= widget
->mapToGlobal(QPoint(0,0));
103 rect
.translate (topleft
);
104 if (cursor_location
!= rect
) {
105 client
->setCursorLocation (this, rect
);
106 cursor_location
= rect
;
111 value
= widget
->inputMethodQuery(Qt::ImMicroFocus
);
113 value
= widget
->inputMethodQuery(Qt::ImFont
);
115 value
= widget
->inputMethodQuery(Qt::ImCursorPosition
);
117 value
= widget
->inputMethodQuery(Qt::ImSurroundingText
);
119 value
= widget
->inputMethodQuery(Qt::ImCurrentSelection
);
125 IBusInputContext::isComposing() const
127 return (!preedit_string
.isEmpty ()) && preedit_visible
;
131 IBusInputContext::setFocusWidget (QWidget
*widget
)
133 // qDebug () << "setFocusWidget (" << widget << ")";
134 QInputContext::setFocusWidget (widget
);
139 IBusInputContext::widgetDestroyed (QWidget
*widget
)
141 QInputContext::widgetDestroyed (widget
);
147 IBusInputContext::x11FilterEvent (QWidget
*keywidget
, XEvent
*xevent
)
149 if (client
->x11FilterEvent (this, keywidget
, xevent
))
151 return QInputContext::x11FilterEvent (keywidget
, xevent
);
156 IBusInputContext::setIC (QString ic
)
162 IBusInputContext::getIC ()
168 IBusInputContext::commitString (QString text
)
170 QInputMethodEvent event
;
171 event
.setCommitString (text
);
177 IBusInputContext::updatePreedit (QString text
, QList
<QList
<quint32
> > attr_list
, int cursor_pos
, bool show
)
179 // qDebug () << text << cursor_pos << show;
180 QList
<QAttribute
> qattrs
;
184 qattrs
.append (QAttribute (QInputMethodEvent::Cursor
, cursor_pos
, true, 0));
187 for (QList
<QList
<quint32
> >::iterator it
= attr_list
.begin (); it
!= attr_list
.end(); ++ it
) {
189 QList
<quint32
> attr
= *it
;
190 QTextCharFormat format
;
194 format
.setUnderlineStyle (QTextCharFormat::SingleUnderline
);
196 case 2: // foreground
197 format
.setForeground (QBrush (QColor (attr
[1])));
199 case 3: // background
200 format
.setBackground (QBrush (QColor (attr
[1])));
206 qattrs
.append (QAttribute (QInputMethodEvent::TextFormat
, attr
[2], attr
[3] - attr
[2], QVariant (format
)));
207 // qDebug () << attr[0] << attr[2] << attr[3] - attr[2];
211 qattrs
.append (QAttribute (QInputMethodEvent::Cursor
, 0, true, 0));
216 preedit_string
= text
;
217 preedit_visible
= show
;
218 preedit_cursor_pos
= cursor_pos
;
220 QInputMethodEvent
event (text
, qattrs
);