source: trunk/src/gui/inputmethod/qcoefepinputcontext_s60.cpp@ 808

Last change on this file since 808 was 769, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.3 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 30.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QT_NO_IM
43
44#include "qcoefepinputcontext_p.h"
45#include <qapplication.h>
46#include <qtextformat.h>
47#include <private/qcore_symbian_p.h>
48
49#include <fepitfr.h>
50#include <hal.h>
51
52#include <limits.h>
53// You only find these enumerations on SDK 5 onwards, so we need to provide our own
54// to remain compatible with older releases. They won't be called by pre-5.0 SDKs.
55
56// MAknEdStateObserver::EAknCursorPositionChanged
57#define QT_EAknCursorPositionChanged MAknEdStateObserver::EAknEdwinStateEvent(6)
58// MAknEdStateObserver::EAknActivatePenInputRequest
59#define QT_EAknActivatePenInputRequest MAknEdStateObserver::EAknEdwinStateEvent(7)
60
61// EAknEditorFlagSelectionVisible is only valid from 3.2 onwards.
62// Sym^3 AVKON FEP manager expects that this flag is used for FEP-aware editors
63// that support text selection.
64#define QT_EAknEditorFlagSelectionVisible 0x100000
65
66QT_BEGIN_NAMESPACE
67
68QCoeFepInputContext::QCoeFepInputContext(QObject *parent)
69 : QInputContext(parent),
70 m_fepState(q_check_ptr(new CAknEdwinState)), // CBase derived object needs check on new
71 m_lastImHints(Qt::ImhNone),
72 m_textCapabilities(TCoeInputCapabilities::EAllText),
73 m_inDestruction(false),
74 m_pendingInputCapabilitiesChanged(false),
75 m_cursorVisibility(1),
76 m_inlinePosition(0),
77 m_formatRetriever(0),
78 m_pointerHandler(0),
79 m_cursorPos(0),
80 m_hasTempPreeditString(false)
81{
82 m_fepState->SetObjectProvider(this);
83 if (QSysInfo::s60Version() > QSysInfo::SV_S60_5_0)
84 m_fepState->SetFlags(EAknEditorFlagDefault | QT_EAknEditorFlagSelectionVisible);
85 else
86 m_fepState->SetFlags(EAknEditorFlagDefault);
87 m_fepState->SetDefaultInputMode( EAknEditorTextInputMode );
88 m_fepState->SetPermittedInputModes( EAknEditorAllInputModes );
89 m_fepState->SetDefaultCase( EAknEditorLowerCase );
90 m_fepState->SetPermittedCases( EAknEditorAllCaseModes );
91 m_fepState->SetSpecialCharacterTableResourceId(R_AVKON_SPECIAL_CHARACTER_TABLE_DIALOG);
92 m_fepState->SetNumericKeymap( EAknEditorStandardNumberModeKeymap );
93}
94
95QCoeFepInputContext::~QCoeFepInputContext()
96{
97 m_inDestruction = true;
98
99 // This is to make sure that the FEP manager "forgets" about us,
100 // otherwise we may get callbacks even after we're destroyed.
101 // The call below is essentially equivalent to InputCapabilitiesChanged(),
102 // but is synchronous, rather than asynchronous.
103 CCoeEnv::Static()->SyncNotifyFocusObserversOfChangeInFocus();
104
105 if (m_fepState)
106 delete m_fepState;
107}
108
109void QCoeFepInputContext::reset()
110{
111 commitCurrentString(true);
112}
113
114void QCoeFepInputContext::ReportAknEdStateEvent(MAknEdStateObserver::EAknEdwinStateEvent aEventType)
115{
116 QT_TRAP_THROWING(m_fepState->ReportAknEdStateEventL(aEventType));
117}
118
119void QCoeFepInputContext::update()
120{
121 updateHints(false);
122
123 // For pre-5.0 SDKs, we don't do text updates on S60 side.
124 if (QSysInfo::s60Version() < QSysInfo::SV_S60_5_0) {
125 return;
126 }
127
128 // Don't be fooled (as I was) by the name of this enumeration.
129 // What it really does is tell the virtual keyboard UI that the text has been
130 // updated and it should be reflected in the internal display of the VK.
131 ReportAknEdStateEvent(QT_EAknCursorPositionChanged);
132}
133
134void QCoeFepInputContext::setFocusWidget(QWidget *w)
135{
136 commitCurrentString(true);
137
138 QInputContext::setFocusWidget(w);
139
140 updateHints(true);
141}
142
143void QCoeFepInputContext::widgetDestroyed(QWidget *w)
144{
145 // Make sure that the input capabilities of whatever new widget got focused are queried.
146 CCoeControl *ctrl = w->effectiveWinId();
147 if (ctrl->IsFocused()) {
148 queueInputCapabilitiesChanged();
149 }
150}
151
152QString QCoeFepInputContext::language()
153{
154 TLanguage lang = m_fepState->LocalLanguage();
155 const QByteArray localeName = qt_symbianLocaleName(lang);
156 if (!localeName.isEmpty()) {
157 return QString::fromLatin1(localeName);
158 } else {
159 return QString::fromLatin1("C");
160 }
161}
162
163bool QCoeFepInputContext::needsInputPanel()
164{
165 switch (QSysInfo::s60Version()) {
166 case QSysInfo::SV_S60_3_1:
167 case QSysInfo::SV_S60_3_2:
168 // There are no touch phones for pre-5.0 SDKs.
169 return false;
170#ifdef Q_CC_NOKIAX86
171 default:
172 // For emulator we assume that we need an input panel, since we can't
173 // separate between phone types.
174 return true;
175#else
176 case QSysInfo::SV_S60_5_0: {
177 // For SDK == 5.0, we need phone specific detection, since the HAL API
178 // is no good on most phones. However, all phones at the time of writing use the
179 // input panel, except N97 in landscape mode, but in this mode it refuses to bring
180 // up the panel anyway, so we don't have to care.
181 return true;
182 }
183 default:
184 // For unknown/newer types, we try to use the HAL API.
185 int keyboardEnabled;
186 int keyboardType;
187 int err[2];
188 err[0] = HAL::Get(HAL::EKeyboard, keyboardType);
189 err[1] = HAL::Get(HAL::EKeyboardState, keyboardEnabled);
190 if (err[0] == KErrNone && err[1] == KErrNone
191 && keyboardType != 0 && keyboardEnabled)
192 // Means that we have some sort of keyboard.
193 return false;
194
195 // Fall back to using the input panel.
196 return true;
197#endif // !Q_CC_NOKIAX86
198 }
199}
200
201bool QCoeFepInputContext::filterEvent(const QEvent *event)
202{
203 // The CloseSoftwareInputPanel event is not handled here, because the VK will automatically
204 // close when it discovers that the underlying widget does not have input capabilities.
205
206 if (!focusWidget())
207 return false;
208
209 switch (event->type()) {
210 case QEvent::KeyPress:
211 commitTemporaryPreeditString();
212 // fall through intended
213 case QEvent::KeyRelease:
214 const QKeyEvent *keyEvent = static_cast<const QKeyEvent *>(event);
215 switch (keyEvent->key()) {
216 case Qt::Key_F20:
217 Q_ASSERT(m_lastImHints == focusWidget()->inputMethodHints());
218 if (m_lastImHints & Qt::ImhHiddenText) {
219 // Special case in Symbian. On editors with secret text, F20 is for some reason
220 // considered to be a backspace.
221 QKeyEvent modifiedEvent(keyEvent->type(), Qt::Key_Backspace, keyEvent->modifiers(),
222 keyEvent->text(), keyEvent->isAutoRepeat(), keyEvent->count());
223 QApplication::sendEvent(focusWidget(), &modifiedEvent);
224 return true;
225 }
226 break;
227 case Qt::Key_Select:
228 if (!m_preeditString.isEmpty()) {
229 commitCurrentString(true);
230 return true;
231 }
232 break;
233 default:
234 break;
235 }
236
237 if (keyEvent->type() == QEvent::KeyPress
238 && focusWidget()->inputMethodHints() & Qt::ImhHiddenText
239 && !keyEvent->text().isEmpty()) {
240 // Send some temporary preedit text in order to make text visible for a moment.
241 m_cursorPos = focusWidget()->inputMethodQuery(Qt::ImCursorPosition).toInt();
242 m_preeditString = keyEvent->text();
243 QList<QInputMethodEvent::Attribute> attributes;
244 QInputMethodEvent imEvent(m_preeditString, attributes);
245 sendEvent(imEvent);
246 m_tempPreeditStringTimeout.start(1000, this);
247 m_hasTempPreeditString = true;
248 update();
249 return true;
250 }
251 break;
252 }
253
254 if (!needsInputPanel())
255 return false;
256
257 if (event->type() == QEvent::RequestSoftwareInputPanel) {
258 // Notify S60 that we want the virtual keyboard to show up.
259 QSymbianControl *sControl;
260 sControl = focusWidget()->effectiveWinId()->MopGetObject(sControl);
261 Q_ASSERT(sControl);
262
263 // The FEP UI temporarily steals focus when it shows up the first time, causing
264 // all sorts of weird effects on the focused widgets. Since it will immediately give
265 // back focus to us, we temporarily disable focus handling until the job's done.
266 if (sControl) {
267 sControl->setIgnoreFocusChanged(true);
268 }
269
270 ensureInputCapabilitiesChanged();
271 m_fepState->ReportAknEdStateEventL(MAknEdStateObserver::QT_EAknActivatePenInputRequest);
272
273 if (sControl) {
274 sControl->setIgnoreFocusChanged(false);
275 }
276 return true;
277 }
278
279 return false;
280}
281
282void QCoeFepInputContext::timerEvent(QTimerEvent *timerEvent)
283{
284 if (timerEvent->timerId() == m_tempPreeditStringTimeout.timerId())
285 commitTemporaryPreeditString();
286}
287
288void QCoeFepInputContext::commitTemporaryPreeditString()
289{
290 if (m_tempPreeditStringTimeout.isActive())
291 m_tempPreeditStringTimeout.stop();
292
293 if (!m_hasTempPreeditString)
294 return;
295
296 commitCurrentString(false);
297}
298
299void QCoeFepInputContext::mouseHandler( int x, QMouseEvent *event)
300{
301 Q_ASSERT(focusWidget());
302
303 if (event->type() == QEvent::MouseButtonPress && event->button() == Qt::LeftButton) {
304 commitCurrentString(true);
305 int pos = focusWidget()->inputMethodQuery(Qt::ImCursorPosition).toInt();
306
307 QList<QInputMethodEvent::Attribute> attributes;
308 attributes << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, pos + x, 0, QVariant());
309 QInputMethodEvent event(QLatin1String(""), attributes);
310 sendEvent(event);
311 }
312}
313
314TCoeInputCapabilities QCoeFepInputContext::inputCapabilities()
315{
316 if (m_inDestruction || !focusWidget()) {
317 return TCoeInputCapabilities(TCoeInputCapabilities::ENone, 0, 0);
318 }
319
320 return TCoeInputCapabilities(m_textCapabilities, this, 0);
321}
322
323static QTextCharFormat qt_TCharFormat2QTextCharFormat(const TCharFormat &cFormat)
324{
325 QTextCharFormat qFormat;
326
327 QBrush foreground(QColor(cFormat.iFontPresentation.iTextColor.Internal()));
328 qFormat.setForeground(foreground);
329
330 qFormat.setFontStrikeOut(cFormat.iFontPresentation.iStrikethrough == EStrikethroughOn);
331 qFormat.setFontUnderline(cFormat.iFontPresentation.iUnderline == EUnderlineOn);
332
333 return qFormat;
334}
335
336void QCoeFepInputContext::updateHints(bool mustUpdateInputCapabilities)
337{
338 QWidget *w = focusWidget();
339 if (w) {
340 Qt::InputMethodHints hints = w->inputMethodHints();
341 if (hints != m_lastImHints) {
342 m_lastImHints = hints;
343 applyHints(hints);
344 } else if (!mustUpdateInputCapabilities) {
345 // Optimization. Return immediately if there was no change.
346 return;
347 }
348 }
349 queueInputCapabilitiesChanged();
350}
351
352void QCoeFepInputContext::applyHints(Qt::InputMethodHints hints)
353{
354 using namespace Qt;
355
356 commitTemporaryPreeditString();
357
358 bool numbersOnly = hints & ImhDigitsOnly || hints & ImhFormattedNumbersOnly
359 || hints & ImhDialableCharactersOnly;
360 bool noOnlys = !(numbersOnly || hints & ImhUppercaseOnly
361 || hints & ImhLowercaseOnly);
362 TInt flags;
363 Qt::InputMethodHints oldHints = hints;
364
365 // Some sanity checking. Make sure that only one preference is set.
366 InputMethodHints prefs = ImhPreferNumbers | ImhPreferUppercase | ImhPreferLowercase;
367 prefs &= hints;
368 if (prefs != ImhPreferNumbers && prefs != ImhPreferUppercase && prefs != ImhPreferLowercase) {
369 hints &= ~prefs;
370 }
371 if (!noOnlys) {
372 // Make sure that the preference is within the permitted set.
373 if (hints & ImhPreferNumbers && !(hints & ImhDigitsOnly || hints & ImhFormattedNumbersOnly
374 || hints & ImhDialableCharactersOnly)) {
375 hints &= ~ImhPreferNumbers;
376 } else if (hints & ImhPreferUppercase && !(hints & ImhUppercaseOnly)) {
377 hints &= ~ImhPreferUppercase;
378 } else if (hints & ImhPreferLowercase && !(hints & ImhLowercaseOnly)) {
379 hints &= ~ImhPreferLowercase;
380 }
381 // If there is no preference, set it to something within the permitted set.
382 if (!(hints & ImhPreferNumbers || hints & ImhPreferUppercase || hints & ImhPreferLowercase)) {
383 if (hints & ImhLowercaseOnly) {
384 hints |= ImhPreferLowercase;
385 } else if (hints & ImhUppercaseOnly) {
386 hints |= ImhPreferUppercase;
387 } else if (hints & ImhDigitsOnly || hints & ImhFormattedNumbersOnly
388 || hints & ImhDialableCharactersOnly) {
389 hints |= ImhPreferNumbers;
390 }
391 }
392 }
393
394 if (hints & ImhPreferNumbers) {
395 m_fepState->SetDefaultInputMode(EAknEditorNumericInputMode);
396 m_fepState->SetCurrentInputMode(EAknEditorNumericInputMode);
397 } else {
398 m_fepState->SetDefaultInputMode(EAknEditorTextInputMode);
399 m_fepState->SetCurrentInputMode(EAknEditorTextInputMode);
400 }
401 flags = 0;
402 if (numbersOnly) {
403 flags |= EAknEditorNumericInputMode;
404 }
405 if (hints & ImhUppercaseOnly || hints & ImhLowercaseOnly) {
406 flags |= EAknEditorTextInputMode;
407 }
408 if (flags == 0) {
409 flags = EAknEditorAllInputModes;
410 }
411 m_fepState->SetPermittedInputModes(flags);
412 ReportAknEdStateEvent(MAknEdStateObserver::EAknEdwinStateInputModeUpdate);
413
414 if (hints & ImhPreferLowercase) {
415 m_fepState->SetDefaultCase(EAknEditorLowerCase);
416 m_fepState->SetCurrentCase(EAknEditorLowerCase);
417 } else if (hints & ImhPreferUppercase) {
418 m_fepState->SetDefaultCase(EAknEditorUpperCase);
419 m_fepState->SetCurrentCase(EAknEditorUpperCase);
420 } else if (hints & ImhNoAutoUppercase) {
421 m_fepState->SetDefaultCase(EAknEditorLowerCase);
422 m_fepState->SetCurrentCase(EAknEditorLowerCase);
423 } else {
424 m_fepState->SetDefaultCase(EAknEditorTextCase);
425 m_fepState->SetCurrentCase(EAknEditorTextCase);
426 }
427 flags = 0;
428 if (hints & ImhUppercaseOnly) {
429 flags |= EAknEditorUpperCase;
430 }
431 if (hints & ImhLowercaseOnly) {
432 flags |= EAknEditorLowerCase;
433 }
434 if (flags == 0) {
435 flags = EAknEditorAllCaseModes;
436 if (hints & ImhNoAutoUppercase) {
437 flags &= ~EAknEditorTextCase;
438 }
439 }
440 m_fepState->SetPermittedCases(flags);
441 ReportAknEdStateEvent(MAknEdStateObserver::EAknEdwinStateCaseModeUpdate);
442
443 if (QSysInfo::s60Version() > QSysInfo::SV_S60_5_0)
444 flags = QT_EAknEditorFlagSelectionVisible;
445 else
446 flags = 0;
447 if (hints & ImhUppercaseOnly && !(hints & ImhLowercaseOnly)
448 || hints & ImhLowercaseOnly && !(hints & ImhUppercaseOnly)) {
449 flags |= EAknEditorFlagFixedCase;
450 }
451 // Using T9 and hidden text together may actually crash the FEP, so check for hidden text too.
452 if (hints & ImhNoPredictiveText || hints & ImhHiddenText) {
453 flags |= EAknEditorFlagNoT9;
454 }
455 m_fepState->SetFlags(flags);
456 ReportAknEdStateEvent(MAknEdStateObserver::EAknEdwinStateFlagsUpdate);
457
458 if (hints & ImhFormattedNumbersOnly) {
459 flags = EAknEditorCalculatorNumberModeKeymap;
460 } else if (hints & ImhDigitsOnly) {
461 flags = EAknEditorPlainNumberModeKeymap;
462 } else {
463 // ImhDialableCharactersOnly is the fallback as well, so we don't need to check for
464 // that flag.
465 flags = EAknEditorStandardNumberModeKeymap;
466 }
467 m_fepState->SetNumericKeymap(static_cast<TAknEditorNumericKeymap>(flags));
468
469 if (hints & ImhEmailCharactersOnly) {
470 m_fepState->SetSpecialCharacterTableResourceId(R_AVKON_EMAIL_ADDR_SPECIAL_CHARACTER_TABLE_DIALOG);
471 } else if (hints & ImhUrlCharactersOnly) {
472 m_fepState->SetSpecialCharacterTableResourceId(R_AVKON_URL_SPECIAL_CHARACTER_TABLE_DIALOG);
473 } else {
474 m_fepState->SetSpecialCharacterTableResourceId(R_AVKON_SPECIAL_CHARACTER_TABLE_DIALOG);
475 }
476
477 if (hints & ImhHiddenText) {
478 m_textCapabilities = TCoeInputCapabilities::EAllText | TCoeInputCapabilities::ESecretText;
479 } else {
480 m_textCapabilities = TCoeInputCapabilities::EAllText;
481 }
482}
483
484void QCoeFepInputContext::applyFormat(QList<QInputMethodEvent::Attribute> *attributes)
485{
486 TCharFormat cFormat;
487 const QColor styleTextColor = focusWidget() ? focusWidget()->palette().text().color() :
488 QApplication::palette("QLineEdit").text().color();
489 const TLogicalRgb fontColor(TRgb(styleTextColor.red(), styleTextColor.green(), styleTextColor.blue(), styleTextColor.alpha()));
490 cFormat.iFontPresentation.iTextColor = fontColor;
491
492 TInt numChars = 0;
493 TInt charPos = 0;
494 int oldSize = attributes->size();
495 while (m_formatRetriever) {
496 m_formatRetriever->GetFormatOfFepInlineText(cFormat, numChars, charPos);
497 if (numChars <= 0) {
498 // This shouldn't happen according to S60 docs, but apparently does sometimes.
499 break;
500 }
501 attributes->append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat,
502 charPos,
503 numChars,
504 QVariant(qt_TCharFormat2QTextCharFormat(cFormat))));
505 charPos += numChars;
506 if (charPos >= m_preeditString.size()) {
507 break;
508 }
509 }
510
511 if (attributes->size() == oldSize) {
512 // S60 didn't provide any format, so let's give our own instead.
513 attributes->append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat,
514 0,
515 m_preeditString.size(),
516 standardFormat(PreeditFormat)));
517 }
518}
519
520void QCoeFepInputContext::queueInputCapabilitiesChanged()
521{
522 if (m_pendingInputCapabilitiesChanged)
523 return;
524
525 // Call ensureInputCapabilitiesChanged asynchronously. This is done to improve performance
526 // by not updating input capabilities too often. The reason we don't call the Symbian
527 // asynchronous version of InputCapabilitiesChanged is because we need to ensure that it
528 // is synchronous in some specific cases. Those will call ensureInputCapabilitesChanged.
529 QMetaObject::invokeMethod(this, "ensureInputCapabilitiesChanged", Qt::QueuedConnection);
530 m_pendingInputCapabilitiesChanged = true;
531}
532
533void QCoeFepInputContext::ensureInputCapabilitiesChanged()
534{
535 if (!m_pendingInputCapabilitiesChanged)
536 return;
537
538 // The call below is essentially equivalent to InputCapabilitiesChanged(),
539 // but is synchronous, rather than asynchronous.
540 CCoeEnv::Static()->SyncNotifyFocusObserversOfChangeInFocus();
541 m_pendingInputCapabilitiesChanged = false;
542}
543
544void QCoeFepInputContext::StartFepInlineEditL(const TDesC& aInitialInlineText,
545 TInt aPositionOfInsertionPointInInlineText, TBool aCursorVisibility, const MFormCustomDraw* /*aCustomDraw*/,
546 MFepInlineTextFormatRetriever& aInlineTextFormatRetriever,
547 MFepPointerEventHandlerDuringInlineEdit& aPointerEventHandlerDuringInlineEdit)
548{
549 QWidget *w = focusWidget();
550 if (!w)
551 return;
552
553 commitTemporaryPreeditString();
554
555 m_cursorPos = w->inputMethodQuery(Qt::ImCursorPosition).toInt();
556
557 QList<QInputMethodEvent::Attribute> attributes;
558
559 m_cursorVisibility = aCursorVisibility ? 1 : 0;
560 m_inlinePosition = aPositionOfInsertionPointInInlineText;
561 m_preeditString = qt_TDesC2QString(aInitialInlineText);
562
563 m_formatRetriever = &aInlineTextFormatRetriever;
564 m_pointerHandler = &aPointerEventHandlerDuringInlineEdit;
565
566 // With T9 aInitialInlineText is typically empty when StartFepInlineEditL is called,
567 // but FEP requires that selected text is always removed at StartFepInlineEditL.
568 // Let's remove the selected text if aInitialInlineText is empty and there is selected text
569 if (m_preeditString.isEmpty()) {
570 int anchor = w->inputMethodQuery(Qt::ImAnchorPosition).toInt();
571 int replacementLength = qAbs(m_cursorPos-anchor);
572 if (replacementLength > 0) {
573 int replacementStart = m_cursorPos < anchor ? 0 : -replacementLength;
574 QList<QInputMethodEvent::Attribute> clearSelectionAttributes;
575 QInputMethodEvent clearSelectionEvent(QLatin1String(""), clearSelectionAttributes);
576 clearSelectionEvent.setCommitString(QLatin1String(""), replacementStart, replacementLength);
577 sendEvent(clearSelectionEvent);
578 }
579 }
580
581 applyFormat(&attributes);
582
583 attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::Cursor,
584 m_inlinePosition,
585 m_cursorVisibility,
586 QVariant()));
587 QInputMethodEvent event(m_preeditString, attributes);
588 sendEvent(event);
589}
590
591void QCoeFepInputContext::UpdateFepInlineTextL(const TDesC& aNewInlineText,
592 TInt aPositionOfInsertionPointInInlineText)
593{
594 QWidget *w = focusWidget();
595 if (!w)
596 return;
597
598 m_inlinePosition = aPositionOfInsertionPointInInlineText;
599
600 QList<QInputMethodEvent::Attribute> attributes;
601 applyFormat(&attributes);
602 attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::Cursor,
603 m_inlinePosition,
604 m_cursorVisibility,
605 QVariant()));
606 m_preeditString = qt_TDesC2QString(aNewInlineText);
607 QInputMethodEvent event(m_preeditString, attributes);
608 sendEvent(event);
609}
610
611void QCoeFepInputContext::SetInlineEditingCursorVisibilityL(TBool aCursorVisibility)
612{
613 QWidget *w = focusWidget();
614 if (!w)
615 return;
616
617 m_cursorVisibility = aCursorVisibility ? 1 : 0;
618
619 QList<QInputMethodEvent::Attribute> attributes;
620 attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::Cursor,
621 m_inlinePosition,
622 m_cursorVisibility,
623 QVariant()));
624 QInputMethodEvent event(m_preeditString, attributes);
625 sendEvent(event);
626}
627
628void QCoeFepInputContext::CancelFepInlineEdit()
629{
630 QList<QInputMethodEvent::Attribute> attributes;
631 QInputMethodEvent event(QLatin1String(""), attributes);
632 event.setCommitString(QLatin1String(""), 0, 0);
633 m_preeditString.clear();
634 sendEvent(event);
635}
636
637TInt QCoeFepInputContext::DocumentLengthForFep() const
638{
639 QWidget *w = focusWidget();
640 if (!w)
641 return 0;
642
643 QVariant variant = w->inputMethodQuery(Qt::ImSurroundingText);
644 return variant.value<QString>().size() + m_preeditString.size();
645}
646
647TInt QCoeFepInputContext::DocumentMaximumLengthForFep() const
648{
649 QWidget *w = focusWidget();
650 if (!w)
651 return 0;
652
653 QVariant variant = w->inputMethodQuery(Qt::ImMaximumTextLength);
654 int size;
655 if (variant.isValid()) {
656 size = variant.toInt();
657 } else {
658 size = INT_MAX; // Sensible default for S60.
659 }
660 return size;
661}
662
663void QCoeFepInputContext::SetCursorSelectionForFepL(const TCursorSelection& aCursorSelection)
664{
665 QWidget *w = focusWidget();
666 if (!w)
667 return;
668
669 commitTemporaryPreeditString();
670
671 int pos = aCursorSelection.iAnchorPos;
672 int length = aCursorSelection.iCursorPos - pos;
673
674 QList<QInputMethodEvent::Attribute> attributes;
675 attributes << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, pos, length, QVariant());
676 QInputMethodEvent event(m_preeditString, attributes);
677 sendEvent(event);
678}
679
680void QCoeFepInputContext::GetCursorSelectionForFep(TCursorSelection& aCursorSelection) const
681{
682 QWidget *w = focusWidget();
683 if (!w) {
684 aCursorSelection.SetSelection(0,0);
685 return;
686 }
687
688 int cursor = w->inputMethodQuery(Qt::ImCursorPosition).toInt() + m_preeditString.size();
689 int anchor = w->inputMethodQuery(Qt::ImAnchorPosition).toInt() + m_preeditString.size();
690 QString text = w->inputMethodQuery(Qt::ImSurroundingText).value<QString>();
691 int combinedSize = text.size() + m_preeditString.size();
692 if (combinedSize < anchor || combinedSize < cursor) {
693 // ### TODO! FIXME! QTBUG-5050
694 // This is a hack to prevent crashing in 4.6 with QLineEdits that use input masks.
695 // The root problem is that cursor position is relative to displayed text instead of the
696 // actual text we get.
697 //
698 // To properly fix this we would need to know the displayText of QLineEdits instead
699 // of just the text, which on itself should be a trivial change. The difficulties start
700 // when we need to commit the changes back to the QLineEdit, which would have to be somehow
701 // able to handle displayText, too.
702 //
703 // Until properly fixed, the cursor and anchor positions will not reflect correct positions
704 // for masked QLineEdits, unless all the masked positions are filled in order so that
705 // cursor position relative to the displayed text matches position relative to actual text.
706 aCursorSelection.iAnchorPos = combinedSize;
707 aCursorSelection.iCursorPos = combinedSize;
708 } else {
709 aCursorSelection.iAnchorPos = anchor;
710 aCursorSelection.iCursorPos = cursor;
711 }
712}
713
714void QCoeFepInputContext::GetEditorContentForFep(TDes& aEditorContent, TInt aDocumentPosition,
715 TInt aLengthToRetrieve) const
716{
717 QWidget *w = focusWidget();
718 if (!w) {
719 aEditorContent.FillZ(aLengthToRetrieve);
720 return;
721 }
722
723 QString text = w->inputMethodQuery(Qt::ImSurroundingText).value<QString>();
724 // FEP expects the preedit string to be part of the editor content, so let's mix it in.
725 int cursor = w->inputMethodQuery(Qt::ImCursorPosition).toInt();
726 text.insert(cursor, m_preeditString);
727 aEditorContent.Copy(qt_QString2TPtrC(text.mid(aDocumentPosition, aLengthToRetrieve)));
728}
729
730void QCoeFepInputContext::GetFormatForFep(TCharFormat& aFormat, TInt /* aDocumentPosition */) const
731{
732 QWidget *w = focusWidget();
733 if (!w) {
734 aFormat = TCharFormat();
735 return;
736 }
737
738 QFont font = w->inputMethodQuery(Qt::ImFont).value<QFont>();
739 QFontMetrics metrics(font);
740 //QString name = font.rawName();
741 QString name = font.defaultFamily(); // TODO! FIXME! Should be the above.
742 QHBufC hBufC(name);
743 aFormat = TCharFormat(hBufC->Des(), metrics.height());
744}
745
746void QCoeFepInputContext::GetScreenCoordinatesForFepL(TPoint& aLeftSideOfBaseLine, TInt& aHeight,
747 TInt& aAscent, TInt /* aDocumentPosition */) const
748{
749 QWidget *w = focusWidget();
750 if (!w) {
751 aLeftSideOfBaseLine = TPoint(0,0);
752 aHeight = 0;
753 aAscent = 0;
754 return;
755 }
756
757 QRect rect = w->inputMethodQuery(Qt::ImMicroFocus).value<QRect>();
758 aLeftSideOfBaseLine.iX = rect.left();
759 aLeftSideOfBaseLine.iY = rect.bottom();
760
761 QFont font = w->inputMethodQuery(Qt::ImFont).value<QFont>();
762 QFontMetrics metrics(font);
763 aHeight = metrics.height();
764 aAscent = metrics.ascent();
765}
766
767void QCoeFepInputContext::DoCommitFepInlineEditL()
768{
769 commitCurrentString(false);
770 if (QSysInfo::s60Version() > QSysInfo::SV_S60_5_0)
771 ReportAknEdStateEvent(QT_EAknCursorPositionChanged);
772
773}
774
775void QCoeFepInputContext::commitCurrentString(bool cancelFepTransaction)
776{
777 int longPress = 0;
778
779 if (m_preeditString.size() == 0) {
780 QWidget *w = focusWidget();
781 if (!cancelFepTransaction && w) {
782 // We must replace the last character only if the input box has already accepted one
783 if (w->inputMethodQuery(Qt::ImCursorPosition).toInt() != m_cursorPos)
784 longPress = 1;
785 }
786 }
787
788 QList<QInputMethodEvent::Attribute> attributes;
789 QInputMethodEvent event(QLatin1String(""), attributes);
790 event.setCommitString(m_preeditString, 0-longPress, longPress);
791 m_preeditString.clear();
792 sendEvent(event);
793
794 m_hasTempPreeditString = false;
795 longPress = 0;
796
797 if (cancelFepTransaction) {
798 CCoeFep* fep = CCoeEnv::Static()->Fep();
799 if (fep)
800 fep->CancelTransaction();
801 }
802}
803
804MCoeFepAwareTextEditor_Extension1* QCoeFepInputContext::Extension1(TBool& aSetToTrue)
805{
806 aSetToTrue = ETrue;
807 return this;
808}
809
810void QCoeFepInputContext::SetStateTransferingOwnershipL(MCoeFepAwareTextEditor_Extension1::CState* aState,
811 TUid /*aTypeSafetyUid*/)
812{
813 // Note: The S60 docs are wrong! See the State() function.
814 if (m_fepState)
815 delete m_fepState;
816 m_fepState = static_cast<CAknEdwinState *>(aState);
817}
818
819MCoeFepAwareTextEditor_Extension1::CState* QCoeFepInputContext::State(TUid /*aTypeSafetyUid*/)
820{
821 // Note: The S60 docs are horribly wrong when describing the
822 // SetStateTransferingOwnershipL function and this function. They say that the former
823 // sets a CState object identified by the TUid, and the latter retrieves it.
824 // In reality, the CState is expected to always be a CAknEdwinState (even if it was not
825 // previously set), and the TUid is ignored. All in all, there is a single CAknEdwinState
826 // per QCoeFepInputContext, which should be deleted if the SetStateTransferingOwnershipL
827 // function is used to set a new one.
828 return m_fepState;
829}
830
831TTypeUid::Ptr QCoeFepInputContext::MopSupplyObject(TTypeUid /*id*/)
832{
833 return TTypeUid::Null();
834}
835
836MObjectProvider *QCoeFepInputContext::MopNext()
837{
838 QWidget *w = focusWidget();
839 if (w)
840 return w->effectiveWinId();
841 return 0;
842}
843
844QT_END_NAMESPACE
845
846#endif // QT_NO_IM
Note: See TracBrowser for help on using the repository browser.