1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /****************************************************************************
|
---|
43 | **
|
---|
44 | ** Implementation of QInputContext 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 | ** [email protected] if any conditions of this licensing are
|
---|
52 | ** not clear to you.
|
---|
53 | **
|
---|
54 | ****************************************************************************/
|
---|
55 |
|
---|
56 | //#define QT_NO_IM_PREEDIT_RELOCATION
|
---|
57 |
|
---|
58 | #include "qinputcontext.h"
|
---|
59 | #include "qinputcontext_p.h"
|
---|
60 |
|
---|
61 | #ifndef QT_NO_IM
|
---|
62 |
|
---|
63 | #include "qplatformdefs.h"
|
---|
64 |
|
---|
65 | #include "qapplication.h"
|
---|
66 | #include "qmenu.h"
|
---|
67 | #include "qtextformat.h"
|
---|
68 | #include "qpalette.h"
|
---|
69 |
|
---|
70 | #include <stdlib.h>
|
---|
71 | #include <limits.h>
|
---|
72 |
|
---|
73 | QT_BEGIN_NAMESPACE
|
---|
74 |
|
---|
75 | /*!
|
---|
76 | \class QInputContext
|
---|
77 | \brief The QInputContext class abstracts the input method dependent data and composing state.
|
---|
78 |
|
---|
79 | \ingroup i18n
|
---|
80 |
|
---|
81 | An input method is responsible to input complex text that cannot
|
---|
82 | be inputted via simple keymap. It converts a sequence of input
|
---|
83 | events (typically key events) into a text string through the input
|
---|
84 | method specific converting process. The class of the processes are
|
---|
85 | widely ranging from simple finite state machine to complex text
|
---|
86 | translator that pools a whole paragraph of a text with text
|
---|
87 | editing capability to perform grammar and semantic analysis.
|
---|
88 |
|
---|
89 | To abstract such different input method specific intermediate
|
---|
90 | information, Qt offers the QInputContext as base class. The
|
---|
91 | concept is well known as 'input context' in the input method
|
---|
92 | domain. an input context is created for a text widget in response
|
---|
93 | to a demand. It is ensured that an input context is prepared for
|
---|
94 | an input method before input to a text widget.
|
---|
95 |
|
---|
96 | Multiple input contexts that is belonging to a single input method
|
---|
97 | may concurrently coexist. Suppose multi-window text editor. Each
|
---|
98 | text widget of window A and B holds different QInputContext
|
---|
99 | instance which contains different state information such as
|
---|
100 | partially composed text.
|
---|
101 |
|
---|
102 | \section1 Groups of Functions
|
---|
103 |
|
---|
104 | \table
|
---|
105 | \header \o Context \o Functions
|
---|
106 |
|
---|
107 | \row \o Receiving information \o
|
---|
108 | x11FilterEvent(),
|
---|
109 | filterEvent(),
|
---|
110 | mouseHandler()
|
---|
111 |
|
---|
112 | \row \o Sending back composed text \o
|
---|
113 | sendEvent()
|
---|
114 |
|
---|
115 | \row \o State change notification \o
|
---|
116 | setFocusWidget(),
|
---|
117 | reset()
|
---|
118 |
|
---|
119 | \row \o Context information \o
|
---|
120 | identifierName(),
|
---|
121 | language(),
|
---|
122 | font(),
|
---|
123 | isComposing()
|
---|
124 |
|
---|
125 | \endtable
|
---|
126 |
|
---|
127 | \legalese
|
---|
128 | Copyright (C) 2003-2004 immodule for Qt Project. All rights reserved.
|
---|
129 |
|
---|
130 | This file is written to contribute to Nokia Corporation and/or its subsidiary(-ies) under their own
|
---|
131 | license. You may use this file under your Qt license. Following
|
---|
132 | description is copied from their original file headers. Contact
|
---|
133 | [email protected] if any conditions of this licensing are
|
---|
134 | not clear to you.
|
---|
135 | \endlegalese
|
---|
136 |
|
---|
137 | \sa QInputContextPlugin, QInputContextFactory, QApplication::setInputContext()
|
---|
138 | */
|
---|
139 |
|
---|
140 | /*!
|
---|
141 | Constructs an input context with the given \a parent.
|
---|
142 | */
|
---|
143 | QInputContext::QInputContext(QObject* parent)
|
---|
144 | : QObject(*new QInputContextPrivate, parent)
|
---|
145 | {
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | /*!
|
---|
150 | Destroys the input context.
|
---|
151 | */
|
---|
152 | QInputContext::~QInputContext()
|
---|
153 | {
|
---|
154 | }
|
---|
155 |
|
---|
156 | /*!
|
---|
157 | \internal
|
---|
158 | Returns the widget that has an input focus for this input
|
---|
159 | context. Ordinary input methods should not call this function
|
---|
160 | directly to keep platform independence and flexible configuration
|
---|
161 | possibility.
|
---|
162 |
|
---|
163 | The return value may differ from holderWidget() if the input
|
---|
164 | context is shared between several text widgets.
|
---|
165 |
|
---|
166 | \sa setFocusWidget(), holderWidget()
|
---|
167 | */
|
---|
168 | QWidget *QInputContext::focusWidget() const
|
---|
169 | {
|
---|
170 | Q_D(const QInputContext);
|
---|
171 | return d->focusWidget;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | /*!
|
---|
176 | \internal
|
---|
177 | Sets the widget that has an input focus for this input
|
---|
178 | context. Ordinary input methods must not call this function
|
---|
179 | directly.
|
---|
180 |
|
---|
181 | \sa focusWidget()
|
---|
182 | */
|
---|
183 | void QInputContext::setFocusWidget(QWidget *widget)
|
---|
184 | {
|
---|
185 | Q_ASSERT(!widget || widget->testAttribute(Qt::WA_InputMethodEnabled));
|
---|
186 | Q_D(QInputContext);
|
---|
187 | d->focusWidget = widget;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /*!
|
---|
191 | \fn bool QInputContext::isComposing() const
|
---|
192 |
|
---|
193 | This function indicates whether InputMethodStart event had been
|
---|
194 | sent to the current focus widget. It is ensured that an input
|
---|
195 | context can send InputMethodCompose or InputMethodEnd event safely
|
---|
196 | if this function returned true.
|
---|
197 |
|
---|
198 | The state is automatically being tracked through sendEvent().
|
---|
199 |
|
---|
200 | \sa sendEvent()
|
---|
201 | */
|
---|
202 |
|
---|
203 | /*!
|
---|
204 | This function can be reimplemented in a subclass to filter input
|
---|
205 | events.
|
---|
206 |
|
---|
207 | Return true if the \a event has been consumed. Otherwise, the
|
---|
208 | unfiltered \a event will be forwarded to widgets as ordinary
|
---|
209 | way. Although the input events have accept() and ignore()
|
---|
210 | methods, leave it untouched.
|
---|
211 |
|
---|
212 | \a event is currently restricted to QKeyEvent. But some input
|
---|
213 | method related events such as QWheelEvent or QTabletEvent may be
|
---|
214 | added in future.
|
---|
215 |
|
---|
216 | The filtering opportunity is always given to the input context as
|
---|
217 | soon as possible. It has to be taken place before any other key
|
---|
218 | event consumers such as eventfilters and accelerators because some
|
---|
219 | input methods require quite various key combination and
|
---|
220 | sequences. It often conflicts with accelerators and so on, so we
|
---|
221 | must give the input context the filtering opportunity first to
|
---|
222 | ensure all input methods work properly regardless of application
|
---|
223 | design.
|
---|
224 |
|
---|
225 | Ordinary input methods require discrete key events to work
|
---|
226 | properly, so Qt's key compression is always disabled for any input
|
---|
227 | contexts.
|
---|
228 |
|
---|
229 | \sa QKeyEvent, x11FilterEvent()
|
---|
230 | */
|
---|
231 | bool QInputContext::filterEvent(const QEvent * /*event*/)
|
---|
232 | {
|
---|
233 | return false;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /*!
|
---|
237 | Sends an input method event specified by \a event to the current focus
|
---|
238 | widget. Implementations of QInputContext should call this method to
|
---|
239 | send the generated input method events and not
|
---|
240 | QApplication::sendEvent(), as the events might have to get dispatched
|
---|
241 | to a different application on some platforms.
|
---|
242 |
|
---|
243 | Some complex input methods route the handling to several child
|
---|
244 | contexts (e.g. to enable language switching). To account for this,
|
---|
245 | QInputContext will check if the parent object is a QInputContext. If
|
---|
246 | yes, it will call the parents sendEvent() implementation instead of
|
---|
247 | sending the event directly.
|
---|
248 |
|
---|
249 | \sa QInputMethodEvent
|
---|
250 | */
|
---|
251 | void QInputContext::sendEvent(const QInputMethodEvent &event)
|
---|
252 | {
|
---|
253 | // route events over input context parents to make chaining possible.
|
---|
254 | QInputContext *p = qobject_cast<QInputContext *>(parent());
|
---|
255 | if (p) {
|
---|
256 | p->sendEvent(event);
|
---|
257 | return;
|
---|
258 | }
|
---|
259 |
|
---|
260 | QWidget *focus = focusWidget();
|
---|
261 | if (!focus)
|
---|
262 | return;
|
---|
263 |
|
---|
264 | QInputMethodEvent e(event);
|
---|
265 | qApp->sendEvent(focus, &e);
|
---|
266 | }
|
---|
267 |
|
---|
268 |
|
---|
269 | /*!
|
---|
270 | This function can be reimplemented in a subclass to handle mouse
|
---|
271 | press, release, double-click, and move events within the preedit
|
---|
272 | text. You can use the function to implement mouse-oriented user
|
---|
273 | interface such as text selection or popup menu for candidate
|
---|
274 | selection.
|
---|
275 |
|
---|
276 | The \a x parameter is the offset within the string that was sent
|
---|
277 | with the InputMethodCompose event. The alteration boundary of \a
|
---|
278 | x is ensured as character boundary of preedit string accurately.
|
---|
279 |
|
---|
280 | The \a event parameter is the event that was sent to the editor
|
---|
281 | widget. The event type is QEvent::MouseButtonPress,
|
---|
282 | QEvent::MouseButtonRelease, QEvent::MouseButtonDblClick or
|
---|
283 | QEvent::MouseButtonMove. The event's button and state indicate
|
---|
284 | the kind of operation that was performed.
|
---|
285 | */
|
---|
286 | void QInputContext::mouseHandler(int /*x*/, QMouseEvent *event)
|
---|
287 | {
|
---|
288 | // Default behavior for simple ephemeral input contexts. Some
|
---|
289 | // complex input contexts should not be reset here.
|
---|
290 | if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonDblClick)
|
---|
291 | reset();
|
---|
292 | }
|
---|
293 |
|
---|
294 |
|
---|
295 | /*!
|
---|
296 | Returns the font of the current input widget
|
---|
297 | */
|
---|
298 | QFont QInputContext::font() const
|
---|
299 | {
|
---|
300 | Q_D(const QInputContext);
|
---|
301 | if (!d->focusWidget)
|
---|
302 | return QApplication::font();
|
---|
303 |
|
---|
304 | return qvariant_cast<QFont>(d->focusWidget->inputMethodQuery(Qt::ImFont));
|
---|
305 | }
|
---|
306 |
|
---|
307 | /*!
|
---|
308 | This virtual function is called when a state in the focus widget
|
---|
309 | has changed. QInputContext can then use
|
---|
310 | QWidget::inputMethodQuery() to query the new state of the widget.
|
---|
311 | */
|
---|
312 | void QInputContext::update()
|
---|
313 | {
|
---|
314 | }
|
---|
315 |
|
---|
316 | /*!
|
---|
317 | This virtual function is called when the specified \a widget is
|
---|
318 | destroyed. The \a widget is a widget on which this input context
|
---|
319 | is installed.
|
---|
320 | */
|
---|
321 | void QInputContext::widgetDestroyed(QWidget *widget)
|
---|
322 | {
|
---|
323 | Q_D(QInputContext);
|
---|
324 | if (widget == d->focusWidget)
|
---|
325 | setFocusWidget(0);
|
---|
326 | }
|
---|
327 |
|
---|
328 | /*!
|
---|
329 | \fn void QInputContext::reset()
|
---|
330 |
|
---|
331 | This function can be reimplemented in a subclass to reset the
|
---|
332 | state of the input method.
|
---|
333 |
|
---|
334 | This function is called by several widgets to reset input
|
---|
335 | state. For example, a text widget call this function before
|
---|
336 | inserting a text to make widget ready to accept a text.
|
---|
337 |
|
---|
338 | Default implementation is sufficient for simple input method. You
|
---|
339 | can override this function to reset external input method engines
|
---|
340 | in complex input method. In the case, call QInputContext::reset()
|
---|
341 | to ensure proper termination of inputting.
|
---|
342 |
|
---|
343 | You must not send any QInputMethodEvent except empty InputMethodEnd event using
|
---|
344 | QInputContext::reset() at reimplemented reset(). It will break
|
---|
345 | input state consistency.
|
---|
346 | */
|
---|
347 |
|
---|
348 |
|
---|
349 | /*!
|
---|
350 | \fn QString QInputContext::identifierName()
|
---|
351 |
|
---|
352 | This function must be implemented in any subclasses to return the
|
---|
353 | identifier name of the input method.
|
---|
354 |
|
---|
355 | Return value is the name to identify and specify input methods for
|
---|
356 | the input method switching mechanism and so on. The name has to be
|
---|
357 | consistent with QInputContextPlugin::keys(). The name has to
|
---|
358 | consist of ASCII characters only.
|
---|
359 |
|
---|
360 | There are two different names with different responsibility in the
|
---|
361 | input method domain. This function returns one of them. Another
|
---|
362 | name is called 'display name' that stands for the name for
|
---|
363 | endusers appeared in a menu and so on.
|
---|
364 |
|
---|
365 | \sa QInputContextPlugin::keys(), QInputContextPlugin::displayName()
|
---|
366 | */
|
---|
367 |
|
---|
368 |
|
---|
369 | /*!
|
---|
370 | \fn QString QInputContext::language()
|
---|
371 |
|
---|
372 | This function must be implemented in any subclasses to return a
|
---|
373 | language code (e.g. "zh_CN", "zh_TW", "zh_HK", "ja", "ko", ...)
|
---|
374 | of the input context. If the input context can handle multiple
|
---|
375 | languages, return the currently used one. The name has to be
|
---|
376 | consistent with QInputContextPlugin::language().
|
---|
377 |
|
---|
378 | This information will be used by language tagging feature in
|
---|
379 | QInputMethodEvent. It is required to distinguish unified han characters
|
---|
380 | correctly. It enables proper font and character code
|
---|
381 | handling. Suppose CJK-awared multilingual web browser
|
---|
382 | (that automatically modifies fonts in CJK-mixed text) and XML editor
|
---|
383 | (that automatically inserts lang attr).
|
---|
384 | */
|
---|
385 |
|
---|
386 |
|
---|
387 | /*!
|
---|
388 | This is a preliminary interface for Qt 4.
|
---|
389 | */
|
---|
390 | QList<QAction *> QInputContext::actions()
|
---|
391 | {
|
---|
392 | return QList<QAction *>();
|
---|
393 | }
|
---|
394 |
|
---|
395 | /*!
|
---|
396 | \enum QInputContext::StandardFormat
|
---|
397 |
|
---|
398 | \value PreeditFormat The preedit text.
|
---|
399 | \value SelectionFormat The selection text.
|
---|
400 |
|
---|
401 | \sa standardFormat()
|
---|
402 | */
|
---|
403 |
|
---|
404 | /*!
|
---|
405 | Returns a QTextFormat object that specifies the format for
|
---|
406 | component \a s.
|
---|
407 | */
|
---|
408 | QTextFormat QInputContext::standardFormat(StandardFormat s) const
|
---|
409 | {
|
---|
410 | QWidget *focus = focusWidget();
|
---|
411 | const QPalette &pal = focus ? focus->palette() : qApp->palette();
|
---|
412 |
|
---|
413 | QTextCharFormat fmt;
|
---|
414 | QColor bg;
|
---|
415 | switch (s) {
|
---|
416 | case QInputContext::PreeditFormat: {
|
---|
417 | fmt.setUnderlineStyle(QTextCharFormat::DashUnderline);
|
---|
418 | #ifndef Q_WS_WIN
|
---|
419 | int h1, s1, v1, h2, s2, v2;
|
---|
420 | pal.color(QPalette::Base).getHsv(&h1, &s1, &v1);
|
---|
421 | pal.color(QPalette::Background).getHsv(&h2, &s2, &v2);
|
---|
422 | bg.setHsv(h1, s1, (v1 + v2) / 2);
|
---|
423 | fmt.setBackground(QBrush(bg));
|
---|
424 | #endif
|
---|
425 | break;
|
---|
426 | }
|
---|
427 | case QInputContext::SelectionFormat: {
|
---|
428 | bg = pal.text().color();
|
---|
429 | fmt.setBackground(QBrush(bg));
|
---|
430 | fmt.setForeground(pal.background());
|
---|
431 | break;
|
---|
432 | }
|
---|
433 | }
|
---|
434 | return fmt;
|
---|
435 | }
|
---|
436 |
|
---|
437 | #ifdef Q_WS_X11
|
---|
438 | /*!
|
---|
439 | This function may be overridden only if input method is depending
|
---|
440 | on X11 and you need raw XEvent. Otherwise, this function must not.
|
---|
441 |
|
---|
442 | This function is designed to filter raw key events for XIM, but
|
---|
443 | other input methods may use this to implement some special
|
---|
444 | features such as distinguishing Shift_L and Shift_R.
|
---|
445 |
|
---|
446 | Return true if the \a event has been consumed. Otherwise, the
|
---|
447 | unfiltered \a event will be translated into QEvent and forwarded
|
---|
448 | to filterEvent(). Filtering at both x11FilterEvent() and
|
---|
449 | filterEvent() in single input method is allowed.
|
---|
450 |
|
---|
451 | \a keywidget is a client widget into which a text is inputted. \a
|
---|
452 | event is inputted XEvent.
|
---|
453 |
|
---|
454 | \sa filterEvent()
|
---|
455 | */
|
---|
456 | bool QInputContext::x11FilterEvent(QWidget * /*keywidget*/, XEvent * /*event*/)
|
---|
457 | {
|
---|
458 | return false;
|
---|
459 | }
|
---|
460 | #endif // Q_WS_X11
|
---|
461 |
|
---|
462 | QT_END_NAMESPACE
|
---|
463 |
|
---|
464 | #endif //Q_NO_IM
|
---|