Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qiostextresponder.mm
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include "qiosglobal.h"
9#include "quiview.h"
10
11#include <QtCore/qscopedvaluerollback.h>
12
13#include <QtGui/qevent.h>
14#include <QtGui/qtextformat.h>
15#include <QtGui/private/qguiapplication_p.h>
16#include <QtGui/qpa/qplatformwindow.h>
17
18// -------------------------------------------------------------------------
19
20@interface QUITextPosition : UITextPosition
21
22@property (nonatomic) NSUInteger index;
23+ (instancetype)positionWithIndex:(NSUInteger)index;
24
25@end
26
27@implementation QUITextPosition
28
29+ (instancetype)positionWithIndex:(NSUInteger)index
30{
31 QUITextPosition *pos = [[QUITextPosition alloc] init];
32 pos.index = index;
33 return [pos autorelease];
34}
35
36@end
37
38// -------------------------------------------------------------------------
39
40@interface QUITextRange : UITextRange
41
42@property (nonatomic) NSRange range;
43+ (instancetype)rangeWithNSRange:(NSRange)range;
44
45@end
46
47@implementation QUITextRange
48
49+ (instancetype)rangeWithNSRange:(NSRange)nsrange
50{
51 QUITextRange *range = [[self alloc] init];
52 range.range = nsrange;
53 return [range autorelease];
54}
55
56- (UITextPosition *)start
57{
58 return [QUITextPosition positionWithIndex:self.range.location];
59}
60
61- (UITextPosition *)end
62{
63 return [QUITextPosition positionWithIndex:(self.range.location + self.range.length)];
64}
65
66- (NSRange)range
67{
68 return _range;
69}
70
71- (BOOL)isEmpty
72{
73 return (self.range.length == 0);
74}
75
76@end
77
78// -------------------------------------------------------------------------
79
80@interface WrapperView : UIView
81@end
82
83@implementation WrapperView
84
85- (instancetype)initWithView:(UIView *)view
86{
87 if (self = [self init]) {
88 [self addSubview:view];
89
90 self.autoresizingMask = view.autoresizingMask;
91
92 [self sizeToFit];
93 }
94
95 return self;
96}
97
98- (void)layoutSubviews
99{
100 UIView *view = [self.subviews firstObject];
101 view.frame = self.bounds;
102
103 // FIXME: During orientation changes the size and position
104 // of the view is not respected by the host view, even if
105 // we call sizeToFit or setNeedsLayout on the superview.
106}
107
108- (CGSize)sizeThatFits:(CGSize)size
109{
110 return [[self.subviews firstObject] sizeThatFits:size];
111}
112
113// By keeping the responder (QIOSTextInputResponder in this case)
114// retained, we ensure that all messages sent to the view during
115// its lifetime in a window hierarchy will be able to traverse the
116// responder chain.
117- (void)willMoveToWindow:(UIWindow *)window
118{
119 if (window)
120 [[self nextResponder] retain];
121 else
122 [[self nextResponder] autorelease];
123}
124
125@end
126
127// -------------------------------------------------------------------------
128
129@implementation QIOSTextResponder {
130 @public
131 QT_PREPEND_NAMESPACE(QIOSInputContext) *m_inputContext;
132 QT_PREPEND_NAMESPACE(QInputMethodQueryEvent) *m_configuredImeState;
133 BOOL m_inSendEventToFocusObject;
134}
135
136- (instancetype)initWithInputContext:(QT_PREPEND_NAMESPACE(QIOSInputContext) *)inputContext
137{
138 if (!(self = [self init]))
139 return self;
140
141 m_inputContext = inputContext;
142 m_configuredImeState = static_cast<QInputMethodQueryEvent*>(m_inputContext->imeState().currentState.clone());
143 m_inSendEventToFocusObject = NO;
144
145 return self;
146}
147
148- (void)dealloc
149{
150 delete m_configuredImeState;
151 [super dealloc];
152}
153
154- (QVariant)currentImeState:(Qt::InputMethodQuery)query
155{
156 return m_inputContext->imeState().currentState.value(query);
157}
158
159- (BOOL)canBecomeFirstResponder
160{
161 return YES;
162}
163
164- (BOOL)becomeFirstResponder
165{
166 FirstResponderCandidate firstResponderCandidate(self);
167
168 qImDebug() << "self:" << self << "first:" << [UIResponder qt_currentFirstResponder];
169
170 if (![super becomeFirstResponder]) {
171 qImDebug() << self << "was not allowed to become first responder";
172 return NO;
173 }
174
175 qImDebug() << self << "became first responder";
176
177 return YES;
178}
179
180- (BOOL)resignFirstResponder
181{
182 qImDebug() << "self:" << self << "first:" << [UIResponder qt_currentFirstResponder];
183
184 // Don't allow activation events of the window that we're doing text on behalf on
185 // to steal responder.
186 if (FirstResponderCandidate::currentCandidate() == [self nextResponder]) {
187 qImDebug("not allowing parent window to steal responder");
188 return NO;
189 }
190
191 if (![super resignFirstResponder])
192 return NO;
193
194 qImDebug() << self << "resigned first responder";
195
196 // Dismissing the keyboard will trigger resignFirstResponder, but so will
197 // a regular responder transfer to another window. In the former case, iOS
198 // will set the new first-responder to our next-responder, and in the latter
199 // case we'll have an active responder candidate.
200 if (![UIResponder qt_currentFirstResponder] && !FirstResponderCandidate::currentCandidate()) {
201 // No first responder set anymore, sync this with Qt by clearing the
202 // focus object.
203 m_inputContext->clearCurrentFocusObject();
204 } else if ([UIResponder qt_currentFirstResponder] == [self nextResponder]) {
205 // We have resigned the keyboard, and transferred first responder back to the parent view
206 Q_ASSERT(!FirstResponderCandidate::currentCandidate());
207 if ([self currentImeState:Qt::ImEnabled].toBool()) {
208 // The current focus object expects text input, but there
209 // is no keyboard to get input from. So we clear focus.
210 qImDebug("no keyboard available, clearing focus object");
211 m_inputContext->clearCurrentFocusObject();
212 }
213 } else {
214 // We've lost responder status because another Qt window was made active,
215 // another QIOSTextResponder was made first-responder, another UIView was
216 // made first-responder, or the first-responder was cleared globally. In
217 // either of these cases we don't have to do anything.
218 qImDebug("lost first responder, but not clearing focus object");
219 }
220
221 return YES;
222}
223
224- (UIResponder*)nextResponder
225{
226 // Make sure we have a handle/platform window before getting the winId().
227 // In the dtor of QIOSWindow the platform window is set to null before calling
228 // removeFromSuperview which will end up calling nextResponder. That means it's
229 // possible that we can get here while the window is being torn down.
230 return (qApp->focusWindow() && qApp->focusWindow()->handle()) ?
231 reinterpret_cast<QUIView *>(qApp->focusWindow()->handle()->winId()) : 0;
232}
233
234// -------------------------------------------------------------------------
235
236- (void)notifyInputDelegate:(Qt::InputMethodQueries)updatedProperties
237{
238 Q_UNUSED(updatedProperties);
239}
240
241- (BOOL)needsKeyboardReconfigure:(Qt::InputMethodQueries)updatedProperties
242{
243 if (updatedProperties & Qt::ImEnabled) {
244 qImDebug() << "Qt::ImEnabled has changed since text responder was configured, need reconfigure";
245 return YES;
246 }
247
248 if (updatedProperties & Qt::ImReadOnly) {
249 qImDebug() << "Qt::ImReadOnly has changed since text responder was configured, need reconfigure";
250 return YES;
251 }
252
253 return NO;
254}
255
256- (void)reset
257{
258 // Nothing to reset for read-only text fields
259}
260
261- (void)commit
262{
263 // Nothing to commit for read-only text fields
264}
265
266// -------------------------------------------------------------------------
267
268#ifndef QT_NO_SHORTCUT
269
270- (void)sendKeyPressRelease:(Qt::Key)key modifiers:(Qt::KeyboardModifiers)modifiers
271{
272 QScopedValueRollback<BOOL> rollback(m_inSendEventToFocusObject, true);
273 QWindowSystemInterface::handleKeyEvent(qApp->focusWindow(), QEvent::KeyPress, key, modifiers);
274 QWindowSystemInterface::handleKeyEvent(qApp->focusWindow(), QEvent::KeyRelease, key, modifiers);
275}
276
277- (void)sendShortcut:(QKeySequence::StandardKey)standardKey
278{
279 const QKeyCombination combination = QKeySequence(standardKey)[0];
280 [self sendKeyPressRelease:combination.key() modifiers:combination.keyboardModifiers()];
281}
282
283- (BOOL)hasSelection
284{
285 QInputMethodQueryEvent query(Qt::ImAnchorPosition | Qt::ImCursorPosition);
286 QGuiApplication::sendEvent(QGuiApplication::focusObject(), &query);
287 int anchorPos = query.value(Qt::ImAnchorPosition).toInt();
288 int cursorPos = query.value(Qt::ImCursorPosition).toInt();
289 return anchorPos != cursorPos;
290}
291
292- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
293{
294 const bool isSelectAction =
295 action == @selector(select:) ||
296 action == @selector(selectAll:);
297
298 const bool isReadAction = action == @selector(copy:);
299
300 if (!isSelectAction && !isReadAction)
301 return [super canPerformAction:action withSender:sender];
302
303 const bool hasSelection = [self hasSelection];
304 return (!hasSelection && isSelectAction) || (hasSelection && isReadAction);
305}
306
307- (void)copy:(id)sender
308{
309 Q_UNUSED(sender);
310 [self sendShortcut:QKeySequence::Copy];
311}
312
313- (void)select:(id)sender
314{
315 Q_UNUSED(sender);
316 [self sendShortcut:QKeySequence::MoveToPreviousWord];
317 [self sendShortcut:QKeySequence::SelectNextWord];
318}
319
320- (void)selectAll:(id)sender
321{
322 Q_UNUSED(sender);
323 [self sendShortcut:QKeySequence::SelectAll];
324}
325
326#endif // QT_NO_SHORTCUT
327
328@end
329
330// -------------------------------------------------------------------------
331
332@implementation QIOSTextInputResponder {
333 QString m_markedText;
334 BOOL m_inSelectionChange;
335}
336
337- (instancetype)initWithInputContext:(QT_PREPEND_NAMESPACE(QIOSInputContext) *)inputContext
338{
339 if (!(self = [super initWithInputContext:inputContext]))
340 return self;
341
342 m_inSelectionChange = NO;
343
344 QVariantMap platformData = m_configuredImeState->value(Qt::ImPlatformData).toMap();
345 Qt::InputMethodHints hints = Qt::InputMethodHints(m_configuredImeState->value(Qt::ImHints).toUInt());
346 Qt::EnterKeyType enterKeyType = Qt::EnterKeyType(m_configuredImeState->value(Qt::ImEnterKeyType).toUInt());
347
348 switch (enterKeyType) {
349 case Qt::EnterKeyReturn:
350 self.returnKeyType = UIReturnKeyDefault;
351 break;
352 case Qt::EnterKeyDone:
353 self.returnKeyType = UIReturnKeyDone;
354 break;
355 case Qt::EnterKeyGo:
356 self.returnKeyType = UIReturnKeyGo;
357 break;
358 case Qt::EnterKeySend:
359 self.returnKeyType = UIReturnKeySend;
360 break;
361 case Qt::EnterKeySearch:
362 self.returnKeyType = UIReturnKeySearch;
363 break;
364 case Qt::EnterKeyNext:
365 self.returnKeyType = UIReturnKeyNext;
366 break;
367 default:
368 self.returnKeyType = (hints & Qt::ImhMultiLine) ? UIReturnKeyDefault : UIReturnKeyDone;
369 break;
370 }
371
372 self.secureTextEntry = BOOL(hints & Qt::ImhHiddenText);
373 self.autocorrectionType = (hints & Qt::ImhNoPredictiveText) ?
374 UITextAutocorrectionTypeNo : UITextAutocorrectionTypeDefault;
375 self.spellCheckingType = (hints & Qt::ImhNoPredictiveText) ?
376 UITextSpellCheckingTypeNo : UITextSpellCheckingTypeDefault;
377
378 if (hints & Qt::ImhUppercaseOnly)
379 self.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
380 else if (hints & Qt::ImhNoAutoUppercase)
381 self.autocapitalizationType = UITextAutocapitalizationTypeNone;
382 else
383 self.autocapitalizationType = UITextAutocapitalizationTypeSentences;
384
385 if (hints & Qt::ImhUrlCharactersOnly)
386 self.keyboardType = UIKeyboardTypeURL;
387 else if (hints & Qt::ImhEmailCharactersOnly)
388 self.keyboardType = UIKeyboardTypeEmailAddress;
389 else if (hints & Qt::ImhDigitsOnly)
390 self.keyboardType = UIKeyboardTypeNumberPad;
391 else if (hints & Qt::ImhDialableCharactersOnly)
392 self.keyboardType = UIKeyboardTypePhonePad;
393 else if (hints & Qt::ImhLatinOnly)
394 self.keyboardType = UIKeyboardTypeASCIICapable;
395 else if (hints & (Qt::ImhPreferNumbers | Qt::ImhFormattedNumbersOnly))
396 self.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
397 else
398 self.keyboardType = UIKeyboardTypeDefault;
399
400 if (UIView *inputView = static_cast<UIView *>(platformData.value(kImePlatformDataInputView).value<void *>()))
401 self.inputView = [[[WrapperView alloc] initWithView:inputView] autorelease];
402 if (UIView *accessoryView = static_cast<UIView *>(platformData.value(kImePlatformDataInputAccessoryView).value<void *>()))
403 self.inputAccessoryView = [[[WrapperView alloc] initWithView:accessoryView] autorelease];
404
405#if !defined(Q_OS_TVOS) && !defined(Q_OS_VISIONOS)
406 if (platformData.value(kImePlatformDataHideShortcutsBar).toBool()) {
407 // According to the docs, leadingBarButtonGroups/trailingBarButtonGroups should be set to nil to hide the shortcuts bar.
408 // However, starting with iOS 10, the API has been surrounded with NS_ASSUME_NONNULL, which contradicts this and causes
409 // compiler warnings. Still it is the way to go to really hide the space reserved for that.
410#pragma clang diagnostic push
411#pragma clang diagnostic ignored "-Wnonnull"
412 self.inputAssistantItem.leadingBarButtonGroups = nil;
413 self.inputAssistantItem.trailingBarButtonGroups = nil;
414#pragma clang diagnostic pop
415 }
416#endif
417
418 self.undoManager.groupsByEvent = NO;
419 [self rebuildUndoStack];
420
421 return self;
422}
423
424- (void)dealloc
425{
426 self.inputView = 0;
427 self.inputAccessoryView = 0;
428 [self.undoManager removeAllActions];
429
430 [super dealloc];
431}
432
433- (BOOL)needsKeyboardReconfigure:(Qt::InputMethodQueries)updatedProperties
434{
435 Qt::InputMethodQueries relevantProperties = updatedProperties;
436 if ((relevantProperties & Qt::ImEnabled)) {
437 // When switching on input-methods we need to consider hints and platform data
438 // as well, as the IM state that we were based on may have been invalidated when
439 // IM was switched off.
440
441 qImDebug("IM was turned on, we need to check hints and platform data as well");
442 relevantProperties |= (Qt::ImHints | Qt::ImPlatformData);
443 }
444
445 // Based on what we set up in initWithInputContext above
446 relevantProperties &= (Qt::ImHints | Qt::ImEnterKeyType | Qt::ImPlatformData);
447
448 if (!relevantProperties)
449 return [super needsKeyboardReconfigure:updatedProperties];
450
451 for (uint i = 0; i < (sizeof(Qt::ImQueryAll) * CHAR_BIT); ++i) {
452 if (Qt::InputMethodQuery property = Qt::InputMethodQuery(int(updatedProperties & (1 << i)))) {
453 if ([self currentImeState:property] != m_configuredImeState->value(property)) {
454 qImDebug() << property << "has changed since text responder was configured, need reconfigure";
455 return YES;
456 }
457 }
458 }
459
460 return [super needsKeyboardReconfigure:updatedProperties];
461}
462
463- (void)reset
464{
465 [self setMarkedText:@"" selectedRange:NSMakeRange(0, 0)];
466 [self notifyInputDelegate:Qt::ImSurroundingText];
467}
468
469- (void)commit
470{
471 [self unmarkText];
472 [self notifyInputDelegate:Qt::ImSurroundingText];
473}
474
475// -------------------------------------------------------------------------
476
477#ifndef QT_NO_SHORTCUT
478
479- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
480{
481 bool isEditAction = (action == @selector(cut:)
482 || action == @selector(copy:)
483 || action == @selector(paste:)
484 || action == @selector(delete:)
485 || action == @selector(toggleBoldface:)
486 || action == @selector(toggleItalics:)
487 || action == @selector(toggleUnderline:)
488 || action == @selector(undo)
489 || action == @selector(redo));
490
491 bool isSelectAction = (action == @selector(select:)
492 || action == @selector(selectAll:)
493 || action == @selector(paste:)
494 || action == @selector(undo)
495 || action == @selector(redo));
496
497 const bool unknownAction = !isEditAction && !isSelectAction;
498 const bool hasSelection = [self hasSelection];
499
500 if (unknownAction)
501 return [super canPerformAction:action withSender:sender];
502
503 QObject *focusObject = QGuiApplication::focusObject();
504 if (focusObject && focusObject->property("qt_im_readonly").toBool()) {
505 // exceptional menu items for read-only views: do include Copy, do not include Paste etc.
506 if (action == @selector(cut:)
507 || action == @selector(paste:)
508 || action == @selector(delete:))
509 return NO;
510 if (action == @selector(copy:))
511 return YES;
512 }
513 return (hasSelection && isEditAction) || (!hasSelection && isSelectAction);
514}
515
516- (void)cut:(id)sender
517{
518 Q_UNUSED(sender);
519 [self sendShortcut:QKeySequence::Cut];
520}
521
522- (void)paste:(id)sender
523{
524 Q_UNUSED(sender);
525 [self sendShortcut:QKeySequence::Paste];
526}
527
528- (void)delete:(id)sender
529{
530 Q_UNUSED(sender);
531 [self sendShortcut:QKeySequence::Delete];
532}
533
534- (void)toggleBoldface:(id)sender
535{
536 Q_UNUSED(sender);
537 [self sendShortcut:QKeySequence::Bold];
538}
539
540- (void)toggleItalics:(id)sender
541{
542 Q_UNUSED(sender);
543 [self sendShortcut:QKeySequence::Italic];
544}
545
546- (void)toggleUnderline:(id)sender
547{
548 Q_UNUSED(sender);
549 [self sendShortcut:QKeySequence::Underline];
550}
551
552// -------------------------------------------------------------------------
553
554- (void)undo
555{
556 [self sendShortcut:QKeySequence::Undo];
557 [self rebuildUndoStack];
558}
559
560- (void)redo
561{
562 [self sendShortcut:QKeySequence::Redo];
563 [self rebuildUndoStack];
564}
565
566- (void)registerRedo
567{
568 NSUndoManager *undoMgr = self.undoManager;
569 [undoMgr beginUndoGrouping];
570 [undoMgr registerUndoWithTarget:self selector:@selector(redo) object:nil];
571 [undoMgr endUndoGrouping];
572}
573
574- (void)rebuildUndoStack
575{
576 dispatch_async(dispatch_get_main_queue (), ^{
577 // Register dummy undo/redo operations to enable Cmd-Z and Cmd-Shift-Z
578 // Ensure we do this outside any undo/redo callback since NSUndoManager
579 // will treat registerUndoWithTarget as registering a redo when called
580 // from within a undo callback.
581 NSUndoManager *undoMgr = self.undoManager;
582 [undoMgr removeAllActions];
583
584 [undoMgr beginUndoGrouping];
585 [undoMgr registerUndoWithTarget:self selector:@selector(undo) object:nil];
586 [undoMgr endUndoGrouping];
587 [undoMgr beginUndoGrouping];
588 [undoMgr registerUndoWithTarget:self selector:@selector(undo) object:nil];
589 [undoMgr endUndoGrouping];
590
591 // Schedule operations that we immediately pop off to be able to schedule redos
592 [undoMgr beginUndoGrouping];
593 [undoMgr registerUndoWithTarget:self selector:@selector(registerRedo) object:nil];
594 [undoMgr endUndoGrouping];
595 [undoMgr beginUndoGrouping];
596 [undoMgr registerUndoWithTarget:self selector:@selector(registerRedo) object:nil];
597 [undoMgr endUndoGrouping];
598 [undoMgr undo];
599 [undoMgr undo];
600
601 // Note that, perhaps because of a bug in UIKit, the buttons on the shortcuts bar ends up
602 // disabled if a undo/redo callback doesn't lead to a [UITextInputDelegate textDidChange].
603 // And we only call that method if Qt made changes to the text. The effect is that the buttons
604 // become disabled when there is nothing more to undo (Qt didn't change anything upon receiving
605 // an undo request). This seems to be OK behavior, so we let it stay like that unless it shows
606 // to cause problems.
607
608 // QTBUG-63393: Having two operations on the rebuilt undo stack keeps the undo/redo widgets
609 // always enabled on the shortcut bar. This workaround was found by experimenting with
610 // removing the removeAllActions call, and is related to the unknown internal implementation
611 // details of how the shortcut bar updates the dimming of its buttons.
612 });
613}
614
615// -------------------------------------------------------------------------
616
617- (void)keyCommandTriggered:(UIKeyCommand *)keyCommand
618{
619 Qt::Key key = Qt::Key_unknown;
620 Qt::KeyboardModifiers modifiers = Qt::NoModifier;
621
622 if (keyCommand.input == UIKeyInputLeftArrow)
623 key = Qt::Key_Left;
624 else if (keyCommand.input == UIKeyInputRightArrow)
625 key = Qt::Key_Right;
626 else if (keyCommand.input == UIKeyInputUpArrow)
627 key = Qt::Key_Up;
628 else if (keyCommand.input == UIKeyInputDownArrow)
629 key = Qt::Key_Down;
630 else
631 Q_UNREACHABLE();
632
633 if (keyCommand.modifierFlags & UIKeyModifierAlternate)
634 modifiers |= Qt::AltModifier;
635 if (keyCommand.modifierFlags & UIKeyModifierShift)
636 modifiers |= Qt::ShiftModifier;
637 if (keyCommand.modifierFlags & UIKeyModifierCommand)
638 modifiers |= Qt::ControlModifier;
639
640 [self sendKeyPressRelease:key modifiers:modifiers];
641}
642
643- (void)addKeyCommandsToArray:(NSMutableArray<UIKeyCommand *> *)array key:(NSString *)key
644{
645 SEL s = @selector(keyCommandTriggered:);
646 [array addObject:[UIKeyCommand keyCommandWithInput:key modifierFlags:0 action:s]];
647 [array addObject:[UIKeyCommand keyCommandWithInput:key modifierFlags:UIKeyModifierShift action:s]];
648 [array addObject:[UIKeyCommand keyCommandWithInput:key modifierFlags:UIKeyModifierAlternate action:s]];
649 [array addObject:[UIKeyCommand keyCommandWithInput:key modifierFlags:UIKeyModifierAlternate|UIKeyModifierShift action:s]];
650 [array addObject:[UIKeyCommand keyCommandWithInput:key modifierFlags:UIKeyModifierCommand action:s]];
651 [array addObject:[UIKeyCommand keyCommandWithInput:key modifierFlags:UIKeyModifierCommand|UIKeyModifierShift action:s]];
652}
653
654- (NSArray<UIKeyCommand *> *)keyCommands
655{
656 // Since keyCommands is called for every key
657 // press/release, we cache the result
658 static dispatch_once_t once;
659 static NSMutableArray<UIKeyCommand *> *array;
660
661 dispatch_once(&once, ^{
662 // We let Qt move the cursor around when the arrow keys are being used. This
663 // is normally implemented through UITextInput, but since IM in Qt have poor
664 // support for moving the cursor vertically, and even less support for selecting
665 // text across multiple paragraphs, we do this through key events.
666 array = [NSMutableArray<UIKeyCommand *> new];
667 [self addKeyCommandsToArray:array key:UIKeyInputUpArrow];
668 [self addKeyCommandsToArray:array key:UIKeyInputDownArrow];
669 [self addKeyCommandsToArray:array key:UIKeyInputLeftArrow];
670 [self addKeyCommandsToArray:array key:UIKeyInputRightArrow];
671 });
672
673 return array;
674}
675
676#endif // QT_NO_SHORTCUT
677
678// -------------------------------------------------------------------------
679
680- (void)notifyInputDelegate:(Qt::InputMethodQueries)updatedProperties
681{
682 // As documented, we should not report textWillChange/textDidChange unless the text
683 // was changed externally. That will cause spell checking etc to fail. But we don't
684 // really know if the text/selection was changed by UITextInput or Qt/app when getting
685 // update calls from Qt. We therefore use a less ideal approach where we always assume
686 // that UITextView caused the change if we're currently processing an event sendt from it.
687 if (m_inSendEventToFocusObject)
688 return;
689
690 if (updatedProperties & (Qt::ImCursorPosition | Qt::ImAnchorPosition)) {
691 QScopedValueRollback<BOOL> rollback(m_inSelectionChange, true);
692 [self.inputDelegate selectionWillChange:self];
693 [self.inputDelegate selectionDidChange:self];
694 }
695
696 if (updatedProperties & Qt::ImSurroundingText) {
697 [self.inputDelegate textWillChange:self];
698 [self.inputDelegate textDidChange:self];
699 }
700}
701
702- (void)sendEventToFocusObject:(QEvent &)e
703{
704 QObject *focusObject = QGuiApplication::focusObject();
705 if (!focusObject)
706 return;
707
708 // While sending the event, we will receive back updateInputMethodWithQuery calls.
709 // Note that it would be more correct to post the event instead, but UITextInput expects
710 // callbacks to take effect immediately (it will query us for information after a callback).
711 QScopedValueRollback<BOOL> rollback(m_inSendEventToFocusObject);
712 m_inSendEventToFocusObject = YES;
713 QCoreApplication::sendEvent(focusObject, &e);
714}
715
716- (id<UITextInputTokenizer>)tokenizer
717{
718 return [[[UITextInputStringTokenizer alloc] initWithTextInput:self] autorelease];
719}
720
721- (UITextPosition *)beginningOfDocument
722{
723 return [QUITextPosition positionWithIndex:0];
724}
725
726- (UITextPosition *)endOfDocument
727{
728 QString surroundingText = [self currentImeState:Qt::ImSurroundingText].toString();
729 int endPosition = surroundingText.length() + m_markedText.length();
730 return [QUITextPosition positionWithIndex:endPosition];
731}
732
733- (void)setSelectedTextRange:(UITextRange *)range
734{
735 if (m_inSelectionChange) {
736 // After [UITextInputDelegate selectionWillChange], UIKit will cancel
737 // any ongoing auto correction (if enabled) and ask us to set an empty selection.
738 // This is contradictory to our current attempt to set a selection, so we ignore
739 // the callback. UIKit will be re-notified of the new selection after
740 // [UITextInputDelegate selectionDidChange].
741 return;
742 }
743
744 QUITextRange *r = static_cast<QUITextRange *>(range);
745 QList<QInputMethodEvent::Attribute> attrs;
746 attrs << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, r.range.location, r.range.length, 0);
747 QInputMethodEvent e(m_markedText, attrs);
748 [self sendEventToFocusObject:e];
749}
750
751- (UITextRange *)selectedTextRange
752{
753 int cursorPos = [self currentImeState:Qt::ImCursorPosition].toInt();
754 int anchorPos = [self currentImeState:Qt::ImAnchorPosition].toInt();
755 return [QUITextRange rangeWithNSRange:NSMakeRange(qMin(cursorPos, anchorPos), qAbs(anchorPos - cursorPos))];
756}
757
758- (NSString *)textInRange:(UITextRange *)range
759{
760 QString text = [self currentImeState:Qt::ImSurroundingText].toString();
761 if (!m_markedText.isEmpty()) {
762 // [UITextInput textInRange] is sparsely documented, but it turns out that unconfirmed
763 // marked text should be seen as a part of the text document. This is different from
764 // ImSurroundingText, which excludes it.
765 int cursorPos = [self currentImeState:Qt::ImCursorPosition].toInt();
766 text = text.left(cursorPos) + m_markedText + text.mid(cursorPos);
767 }
768
769 int s = static_cast<QUITextPosition *>([range start]).index;
770 int e = static_cast<QUITextPosition *>([range end]).index;
771 return text.mid(s, e - s).toNSString();
772}
773
774- (void)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRange
775{
776 Q_UNUSED(selectedRange);
777
778 m_markedText = markedText ? QString::fromNSString(markedText) : QString();
779
780 static QTextCharFormat markedTextFormat;
781 if (markedTextFormat.isEmpty()) {
782 // There seems to be no way to query how the preedit text
783 // should be drawn. So we need to hard-code the color.
784 markedTextFormat.setBackground(QColor(206, 221, 238));
785 }
786
787 QList<QInputMethodEvent::Attribute> attrs;
788 attrs << QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, 0, markedText.length, markedTextFormat);
789 QInputMethodEvent e(m_markedText, attrs);
790 [self sendEventToFocusObject:e];
791}
792
793- (void)unmarkText
794{
795 if (m_markedText.isEmpty())
796 return;
797
798 QInputMethodEvent e;
799 e.setCommitString(m_markedText);
800 [self sendEventToFocusObject:e];
801
802 m_markedText.clear();
803}
804
805- (NSComparisonResult)comparePosition:(UITextPosition *)position toPosition:(UITextPosition *)other
806{
807 int p = static_cast<QUITextPosition *>(position).index;
808 int o = static_cast<QUITextPosition *>(other).index;
809 if (p > o)
810 return NSOrderedAscending;
811 else if (p < o)
812 return NSOrderedDescending;
813 return NSOrderedSame;
814}
815
816- (UITextRange *)markedTextRange
817{
818 return m_markedText.isEmpty() ? nil : [QUITextRange rangeWithNSRange:NSMakeRange(0, m_markedText.length())];
819}
820
821- (UITextRange *)textRangeFromPosition:(UITextPosition *)fromPosition toPosition:(UITextPosition *)toPosition
822{
823 int f = static_cast<QUITextPosition *>(fromPosition).index;
824 int t = static_cast<QUITextPosition *>(toPosition).index;
825 return [QUITextRange rangeWithNSRange:NSMakeRange(f, t - f)];
826}
827
828- (UITextPosition *)positionFromPosition:(UITextPosition *)position offset:(NSInteger)offset
829{
830 int p = static_cast<QUITextPosition *>(position).index;
831 const int posWithIndex = p + offset;
832 const int textLength = [self currentImeState:Qt::ImSurroundingText].toString().length();
833 if (posWithIndex < 0 || posWithIndex > textLength)
834 return nil;
835 return [QUITextPosition positionWithIndex:posWithIndex];
836}
837
838- (UITextPosition *)positionFromPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction offset:(NSInteger)offset
839{
840 int p = static_cast<QUITextPosition *>(position).index;
841
842 switch (direction) {
843 case UITextLayoutDirectionLeft:
844 return [QUITextPosition positionWithIndex:p - offset];
845 case UITextLayoutDirectionRight:
846 return [QUITextPosition positionWithIndex:p + offset];
847 default:
848 // Qt doesn't support getting the position above or below the current position, so
849 // for those cases we just return the current position, making it a no-op.
850 return position;
851 }
852}
853
854- (UITextPosition *)positionWithinRange:(UITextRange *)range farthestInDirection:(UITextLayoutDirection)direction
855{
856 NSRange r = static_cast<QUITextRange *>(range).range;
857 if (direction == UITextLayoutDirectionRight)
858 return [QUITextPosition positionWithIndex:r.location + r.length];
859 return [QUITextPosition positionWithIndex:r.location];
860}
861
862- (NSInteger)offsetFromPosition:(UITextPosition *)fromPosition toPosition:(UITextPosition *)toPosition
863{
864 int f = static_cast<QUITextPosition *>(fromPosition).index;
865 int t = static_cast<QUITextPosition *>(toPosition).index;
866 return t - f;
867}
868
869- (UIView *)textInputView
870{
871 auto *focusWindow = QGuiApplication::focusWindow();
872 if (!focusWindow)
873 return nil;
874
875 // iOS expects rects we return from other UITextInput methods
876 // to be relative to the view this method returns.
877 // Since QInputMethod returns rects relative to the top level
878 // QWindow, that is also the view we need to return.
879 Q_ASSERT(focusWindow->handle());
880 QPlatformWindow *topLevel = focusWindow->handle();
881 while (QPlatformWindow *p = topLevel->parent())
882 topLevel = p;
883 return reinterpret_cast<UIView *>(topLevel->winId());
884}
885
886- (CGRect)firstRectForRange:(UITextRange *)range
887{
888 QObject *focusObject = QGuiApplication::focusObject();
889 if (!focusObject)
890 return CGRectZero;
891
892 // Using a work-around to get the current rect until
893 // a better API is in place:
894 if (!m_markedText.isEmpty())
895 return CGRectZero;
896
897 int cursorPos = [self currentImeState:Qt::ImCursorPosition].toInt();
898 int anchorPos = [self currentImeState:Qt::ImAnchorPosition].toInt();
899
900 NSRange r = static_cast<QUITextRange*>(range).range;
901 QList<QInputMethodEvent::Attribute> attrs;
902 attrs << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, r.location, 0, 0);
903 {
904 QInputMethodEvent e(m_markedText, attrs);
905 [self sendEventToFocusObject:e];
906 }
907 QRectF startRect = QPlatformInputContext::cursorRectangle();
908
909 attrs = QList<QInputMethodEvent::Attribute>();
910 attrs << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, r.location + r.length, 0, 0);
911 {
912 QInputMethodEvent e(m_markedText, attrs);
913 [self sendEventToFocusObject:e];
914 }
915 QRectF endRect = QPlatformInputContext::cursorRectangle();
916
917 if (cursorPos != int(r.location + r.length) || cursorPos != anchorPos) {
918 attrs = QList<QInputMethodEvent::Attribute>();
919 attrs << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, qMin(cursorPos, anchorPos), qAbs(cursorPos - anchorPos), 0);
920 QInputMethodEvent e(m_markedText, attrs);
921 [self sendEventToFocusObject:e];
922 }
923
924 return startRect.united(endRect).toCGRect();
925}
926
927- (NSArray<UITextSelectionRect *> *)selectionRectsForRange:(UITextRange *)range
928{
929 Q_UNUSED(range);
930 // This method is supposed to return a rectangle for each line with selection. Since we don't
931 // expose an API in Qt/IM for getting this information, and since we never seems to be getting
932 // a call from UIKit for this, we return an empty array until a need arise.
933 return [[NSArray<UITextSelectionRect *> new] autorelease];
934}
935
936- (CGRect)caretRectForPosition:(UITextPosition *)position
937{
938 Q_UNUSED(position);
939 // Assume for now that position is always the same as
940 // cursor index until a better API is in place:
941 return QPlatformInputContext::cursorRectangle().toCGRect();
942}
943
944- (void)replaceRange:(UITextRange *)range withText:(NSString *)text
945{
946 [self setSelectedTextRange:range];
947
948 QInputMethodEvent e;
949 e.setCommitString(QString::fromNSString(text));
950 [self sendEventToFocusObject:e];
951}
952
953- (void)setBaseWritingDirection:(NSWritingDirection)writingDirection forRange:(UITextRange *)range
954{
955 Q_UNUSED(writingDirection);
956 Q_UNUSED(range);
957 // Writing direction is handled by QLocale
958}
959
960- (NSWritingDirection)baseWritingDirectionForPosition:(UITextPosition *)position inDirection:(UITextStorageDirection)direction
961{
962 Q_UNUSED(position);
963 Q_UNUSED(direction);
964 if (QLocale::system().textDirection() == Qt::RightToLeft)
965 return NSWritingDirectionRightToLeft;
966 return NSWritingDirectionLeftToRight;
967}
968
969- (UITextRange *)characterRangeByExtendingPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction
970{
971 int p = static_cast<QUITextPosition *>(position).index;
972 if (direction == UITextLayoutDirectionLeft)
973 return [QUITextRange rangeWithNSRange:NSMakeRange(0, p)];
974 int l = [self currentImeState:Qt::ImSurroundingText].toString().length();
975 return [QUITextRange rangeWithNSRange:NSMakeRange(p, l - p)];
976}
977
978- (UITextPosition *)closestPositionToPoint:(CGPoint)point
979{
980 int textPos = QPlatformInputContext::queryFocusObject(Qt::ImCursorPosition, QPointF::fromCGPoint(point)).toInt();
981 return [QUITextPosition positionWithIndex:textPos];
982}
983
984- (UITextPosition *)closestPositionToPoint:(CGPoint)point withinRange:(UITextRange *)range
985{
986 // No API in Qt for determining this. Use sensible default instead:
987 Q_UNUSED(point);
988 Q_UNUSED(range);
989 return [QUITextPosition positionWithIndex:[self currentImeState:Qt::ImCursorPosition].toInt()];
990}
991
992- (UITextRange *)characterRangeAtPoint:(CGPoint)point
993{
994 // No API in Qt for determining this. Use sensible default instead:
995 Q_UNUSED(point);
996 return [QUITextRange rangeWithNSRange:NSMakeRange([self currentImeState:Qt::ImCursorPosition].toInt(), 0)];
997}
998
999- (void)setMarkedTextStyle:(NSDictionary *)style
1000{
1001 Q_UNUSED(style);
1002 // No-one is going to change our style. If UIKit itself did that
1003 // it would be very welcome, since then we knew how to style marked
1004 // text instead of just guessing...
1005}
1006
1007#ifndef Q_OS_TVOS
1008- (NSDictionary *)textStylingAtPosition:(UITextPosition *)position inDirection:(UITextStorageDirection)direction
1009{
1010 Q_UNUSED(position);
1011 Q_UNUSED(direction);
1012
1013 QObject *focusObject = QGuiApplication::focusObject();
1014 if (!focusObject)
1015 return @{};
1016
1017 // Assume position is the same as the cursor for now. QInputMethodQueryEvent with Qt::ImFont
1018 // needs to be extended to take an extra position argument before this can be fully correct.
1019 QInputMethodQueryEvent e(Qt::ImFont);
1020 QCoreApplication::sendEvent(focusObject, &e);
1021 QFont qfont = qvariant_cast<QFont>(e.value(Qt::ImFont));
1022 UIFont *uifont = [UIFont fontWithName:qfont.family().toNSString() size:qfont.pointSize()];
1023 if (!uifont)
1024 return @{};
1025 return @{NSFontAttributeName: uifont};
1026}
1027#endif
1028
1029- (NSDictionary *)markedTextStyle
1030{
1031 return [NSDictionary dictionary];
1032}
1033
1034- (BOOL)hasText
1035{
1036 return YES;
1037}
1038
1039- (void)insertText:(NSString *)text
1040{
1041 QObject *focusObject = QGuiApplication::focusObject();
1042 if (!focusObject)
1043 return;
1044
1045 if ([text isEqualToString:@"\n"]) {
1046 [self sendKeyPressRelease:Qt::Key_Return modifiers:Qt::NoModifier];
1047
1048 // An onEnter handler of a TextInput might move to the next input by calling
1049 // nextInput.forceActiveFocus() which changes the focusObject.
1050 // In that case we don't want to hide the VKB.
1051 if (focusObject != QGuiApplication::focusObject()) {
1052 qImDebug() << "focusObject already changed, not resigning first responder.";
1053 return;
1054 }
1055
1056 if (self.returnKeyType == UIReturnKeyDone || self.returnKeyType == UIReturnKeyGo
1057 || self.returnKeyType == UIReturnKeySend || self.returnKeyType == UIReturnKeySearch)
1058 [self resignFirstResponder];
1059
1060 return;
1061 }
1062
1063 QInputMethodEvent e;
1064 e.setCommitString(QString::fromNSString(text));
1065 [self sendEventToFocusObject:e];
1066}
1067
1068- (void)deleteBackward
1069{
1070 // UITextInput selects the text to be deleted before calling this method. To avoid
1071 // drawing the selection, we flush after posting the key press/release.
1072 [self sendKeyPressRelease:Qt::Key_Backspace modifiers:Qt::NoModifier];
1073}
1074
1075@end
\inmodule QtCore
Definition qvariant.h:66
long NSInteger
Q_FORWARD_DECLARE_OBJC_CLASS(NSString)
#define qImDebug
Definition qiosglobal.h:21