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 if (hints & Qt::ImhDecimaNumbersOnly)
398 self.keyboardType = UIKeyboardTypeDecimalPad;
399 else
400 self.keyboardType = UIKeyboardTypeDefault;
401
402 if (UIView *inputView = static_cast<UIView *>(platformData.value(kImePlatformDataInputView).value<void *>()))
403 self.inputView = [[[WrapperView alloc] initWithView:inputView] autorelease];
404 if (UIView *accessoryView = static_cast<UIView *>(platformData.value(kImePlatformDataInputAccessoryView).value<void *>()))
405 self.inputAccessoryView = [[[WrapperView alloc] initWithView:accessoryView] autorelease];
406
407#if !defined(Q_OS_TVOS) && !defined(Q_OS_VISIONOS)
408 if (platformData.value(kImePlatformDataHideShortcutsBar).toBool()) {
409 // According to the docs, leadingBarButtonGroups/trailingBarButtonGroups should be set to nil to hide the shortcuts bar.
410 // However, starting with iOS 10, the API has been surrounded with NS_ASSUME_NONNULL, which contradicts this and causes
411 // compiler warnings. Still it is the way to go to really hide the space reserved for that.
412#pragma clang diagnostic push
413#pragma clang diagnostic ignored "-Wnonnull"
414 self.inputAssistantItem.leadingBarButtonGroups = nil;
415 self.inputAssistantItem.trailingBarButtonGroups = nil;
416#pragma clang diagnostic pop
417 }
418#endif
419
420 self.undoManager.groupsByEvent = NO;
421 [self rebuildUndoStack];
422
423 return self;
424}
425
426- (void)dealloc
427{
428 self.inputView = 0;
429 self.inputAccessoryView = 0;
430 [self.undoManager removeAllActions];
431
432 [super dealloc];
433}
434
435- (BOOL)needsKeyboardReconfigure:(Qt::InputMethodQueries)updatedProperties
436{
437 Qt::InputMethodQueries relevantProperties = updatedProperties;
438 if ((relevantProperties & Qt::ImEnabled)) {
439 // When switching on input-methods we need to consider hints and platform data
440 // as well, as the IM state that we were based on may have been invalidated when
441 // IM was switched off.
442
443 qImDebug("IM was turned on, we need to check hints and platform data as well");
444 relevantProperties |= (Qt::ImHints | Qt::ImPlatformData);
445 }
446
447 // Based on what we set up in initWithInputContext above
448 relevantProperties &= (Qt::ImHints | Qt::ImEnterKeyType | Qt::ImPlatformData);
449
450 if (!relevantProperties)
451 return [super needsKeyboardReconfigure:updatedProperties];
452
453 for (uint i = 0; i < (sizeof(Qt::ImQueryAll) * CHAR_BIT); ++i) {
454 if (Qt::InputMethodQuery property = Qt::InputMethodQuery(int(updatedProperties & (1 << i)))) {
455 if ([self currentImeState:property] != m_configuredImeState->value(property)) {
456 qImDebug() << property << "has changed since text responder was configured, need reconfigure";
457 return YES;
458 }
459 }
460 }
461
462 return [super needsKeyboardReconfigure:updatedProperties];
463}
464
465- (void)reset
466{
467 [self setMarkedText:@"" selectedRange:NSMakeRange(0, 0)];
468 [self notifyInputDelegate:Qt::ImSurroundingText];
469}
470
471- (void)commit
472{
473 [self unmarkText];
474 [self notifyInputDelegate:Qt::ImSurroundingText];
475}
476
477// -------------------------------------------------------------------------
478
479#ifndef QT_NO_SHORTCUT
480
481- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
482{
483 bool isEditAction = (action == @selector(cut:)
484 || action == @selector(copy:)
485 || action == @selector(paste:)
486 || action == @selector(delete:)
487 || action == @selector(toggleBoldface:)
488 || action == @selector(toggleItalics:)
489 || action == @selector(toggleUnderline:)
490 || action == @selector(undo)
491 || action == @selector(redo));
492
493 bool isSelectAction = (action == @selector(select:)
494 || action == @selector(selectAll:)
495 || action == @selector(paste:)
496 || action == @selector(undo)
497 || action == @selector(redo));
498
499 const bool unknownAction = !isEditAction && !isSelectAction;
500 const bool hasSelection = [self hasSelection];
501
502 if (unknownAction)
503 return [super canPerformAction:action withSender:sender];
504
505 QObject *focusObject = QGuiApplication::focusObject();
506 if (focusObject && focusObject->property("qt_im_readonly").toBool()) {
507 // exceptional menu items for read-only views: do include Copy, do not include Paste etc.
508 if (action == @selector(cut:)
509 || action == @selector(paste:)
510 || action == @selector(delete:))
511 return NO;
512 if (action == @selector(copy:))
513 return YES;
514 }
515 return (hasSelection && isEditAction) || (!hasSelection && isSelectAction);
516}
517
518- (void)cut:(id)sender
519{
520 Q_UNUSED(sender);
521 [self sendShortcut:QKeySequence::Cut];
522}
523
524- (void)paste:(id)sender
525{
526 Q_UNUSED(sender);
527 [self sendShortcut:QKeySequence::Paste];
528}
529
530- (void)delete:(id)sender
531{
532 Q_UNUSED(sender);
533 [self sendShortcut:QKeySequence::Delete];
534}
535
536- (void)toggleBoldface:(id)sender
537{
538 Q_UNUSED(sender);
539 [self sendShortcut:QKeySequence::Bold];
540}
541
542- (void)toggleItalics:(id)sender
543{
544 Q_UNUSED(sender);
545 [self sendShortcut:QKeySequence::Italic];
546}
547
548- (void)toggleUnderline:(id)sender
549{
550 Q_UNUSED(sender);
551 [self sendShortcut:QKeySequence::Underline];
552}
553
554// -------------------------------------------------------------------------
555
556- (void)undo
557{
558 [self sendShortcut:QKeySequence::Undo];
559 [self rebuildUndoStack];
560}
561
562- (void)redo
563{
564 [self sendShortcut:QKeySequence::Redo];
565 [self rebuildUndoStack];
566}
567
568- (void)registerRedo
569{
570 NSUndoManager *undoMgr = self.undoManager;
571 [undoMgr beginUndoGrouping];
572 [undoMgr registerUndoWithTarget:self selector:@selector(redo) object:nil];
573 [undoMgr endUndoGrouping];
574}
575
576- (void)rebuildUndoStack
577{
578 dispatch_async(dispatch_get_main_queue (), ^{
579 // Register dummy undo/redo operations to enable Cmd-Z and Cmd-Shift-Z
580 // Ensure we do this outside any undo/redo callback since NSUndoManager
581 // will treat registerUndoWithTarget as registering a redo when called
582 // from within a undo callback.
583 NSUndoManager *undoMgr = self.undoManager;
584 [undoMgr removeAllActions];
585
586 [undoMgr beginUndoGrouping];
587 [undoMgr registerUndoWithTarget:self selector:@selector(undo) object:nil];
588 [undoMgr endUndoGrouping];
589 [undoMgr beginUndoGrouping];
590 [undoMgr registerUndoWithTarget:self selector:@selector(undo) object:nil];
591 [undoMgr endUndoGrouping];
592
593 // Schedule operations that we immediately pop off to be able to schedule redos
594 [undoMgr beginUndoGrouping];
595 [undoMgr registerUndoWithTarget:self selector:@selector(registerRedo) object:nil];
596 [undoMgr endUndoGrouping];
597 [undoMgr beginUndoGrouping];
598 [undoMgr registerUndoWithTarget:self selector:@selector(registerRedo) object:nil];
599 [undoMgr endUndoGrouping];
600 [undoMgr undo];
601 [undoMgr undo];
602
603 // Note that, perhaps because of a bug in UIKit, the buttons on the shortcuts bar ends up
604 // disabled if a undo/redo callback doesn't lead to a [UITextInputDelegate textDidChange].
605 // And we only call that method if Qt made changes to the text. The effect is that the buttons
606 // become disabled when there is nothing more to undo (Qt didn't change anything upon receiving
607 // an undo request). This seems to be OK behavior, so we let it stay like that unless it shows
608 // to cause problems.
609
610 // QTBUG-63393: Having two operations on the rebuilt undo stack keeps the undo/redo widgets
611 // always enabled on the shortcut bar. This workaround was found by experimenting with
612 // removing the removeAllActions call, and is related to the unknown internal implementation
613 // details of how the shortcut bar updates the dimming of its buttons.
614 });
615}
616
617// -------------------------------------------------------------------------
618
619- (void)keyCommandTriggered:(UIKeyCommand *)keyCommand
620{
621 Qt::Key key = Qt::Key_unknown;
622 Qt::KeyboardModifiers modifiers = Qt::NoModifier;
623
624 if (keyCommand.input == UIKeyInputLeftArrow)
625 key = Qt::Key_Left;
626 else if (keyCommand.input == UIKeyInputRightArrow)
627 key = Qt::Key_Right;
628 else if (keyCommand.input == UIKeyInputUpArrow)
629 key = Qt::Key_Up;
630 else if (keyCommand.input == UIKeyInputDownArrow)
631 key = Qt::Key_Down;
632 else
633 Q_UNREACHABLE();
634
635 if (keyCommand.modifierFlags & UIKeyModifierAlternate)
636 modifiers |= Qt::AltModifier;
637 if (keyCommand.modifierFlags & UIKeyModifierShift)
638 modifiers |= Qt::ShiftModifier;
639 if (keyCommand.modifierFlags & UIKeyModifierCommand)
640 modifiers |= Qt::ControlModifier;
641
642 [self sendKeyPressRelease:key modifiers:modifiers];
643}
644
645- (void)addKeyCommandsToArray:(NSMutableArray<UIKeyCommand *> *)array key:(NSString *)key
646{
647 SEL s = @selector(keyCommandTriggered:);
648 [array addObject:[UIKeyCommand keyCommandWithInput:key modifierFlags:0 action:s]];
649 [array addObject:[UIKeyCommand keyCommandWithInput:key modifierFlags:UIKeyModifierShift action:s]];
650 [array addObject:[UIKeyCommand keyCommandWithInput:key modifierFlags:UIKeyModifierAlternate action:s]];
651 [array addObject:[UIKeyCommand keyCommandWithInput:key modifierFlags:UIKeyModifierAlternate|UIKeyModifierShift action:s]];
652 [array addObject:[UIKeyCommand keyCommandWithInput:key modifierFlags:UIKeyModifierCommand action:s]];
653 [array addObject:[UIKeyCommand keyCommandWithInput:key modifierFlags:UIKeyModifierCommand|UIKeyModifierShift action:s]];
654}
655
656- (NSArray<UIKeyCommand *> *)keyCommands
657{
658 // Since keyCommands is called for every key
659 // press/release, we cache the result
660 static dispatch_once_t once;
661 static NSMutableArray<UIKeyCommand *> *array;
662
663 dispatch_once(&once, ^{
664 // We let Qt move the cursor around when the arrow keys are being used. This
665 // is normally implemented through UITextInput, but since IM in Qt have poor
666 // support for moving the cursor vertically, and even less support for selecting
667 // text across multiple paragraphs, we do this through key events.
668 array = [NSMutableArray<UIKeyCommand *> new];
669 [self addKeyCommandsToArray:array key:UIKeyInputUpArrow];
670 [self addKeyCommandsToArray:array key:UIKeyInputDownArrow];
671 [self addKeyCommandsToArray:array key:UIKeyInputLeftArrow];
672 [self addKeyCommandsToArray:array key:UIKeyInputRightArrow];
673 });
674
675 return array;
676}
677
678#endif // QT_NO_SHORTCUT
679
680// -------------------------------------------------------------------------
681
682- (void)notifyInputDelegate:(Qt::InputMethodQueries)updatedProperties
683{
684 // As documented, we should not report textWillChange/textDidChange unless the text
685 // was changed externally. That will cause spell checking etc to fail. But we don't
686 // really know if the text/selection was changed by UITextInput or Qt/app when getting
687 // update calls from Qt. We therefore use a less ideal approach where we always assume
688 // that UITextView caused the change if we're currently processing an event sendt from it.
689 if (m_inSendEventToFocusObject)
690 return;
691
692 if (updatedProperties & (Qt::ImCursorPosition | Qt::ImAnchorPosition)) {
693 QScopedValueRollback<BOOL> rollback(m_inSelectionChange, true);
694 [self.inputDelegate selectionWillChange:self];
695 [self.inputDelegate selectionDidChange:self];
696 }
697
698 if (updatedProperties & Qt::ImSurroundingText) {
699 [self.inputDelegate textWillChange:self];
700 [self.inputDelegate textDidChange:self];
701 }
702}
703
704- (void)sendEventToFocusObject:(QEvent &)e
705{
706 QObject *focusObject = QGuiApplication::focusObject();
707 if (!focusObject)
708 return;
709
710 // While sending the event, we will receive back updateInputMethodWithQuery calls.
711 // Note that it would be more correct to post the event instead, but UITextInput expects
712 // callbacks to take effect immediately (it will query us for information after a callback).
713 QScopedValueRollback<BOOL> rollback(m_inSendEventToFocusObject);
714 m_inSendEventToFocusObject = YES;
715 QCoreApplication::sendEvent(focusObject, &e);
716}
717
718- (id<UITextInputTokenizer>)tokenizer
719{
720 return [[[UITextInputStringTokenizer alloc] initWithTextInput:self] autorelease];
721}
722
723- (UITextPosition *)beginningOfDocument
724{
725 return [QUITextPosition positionWithIndex:0];
726}
727
728- (UITextPosition *)endOfDocument
729{
730 QString surroundingText = [self currentImeState:Qt::ImSurroundingText].toString();
731 int endPosition = surroundingText.length() + m_markedText.length();
732 return [QUITextPosition positionWithIndex:endPosition];
733}
734
735- (void)setSelectedTextRange:(UITextRange *)range
736{
737 if (m_inSelectionChange) {
738 // After [UITextInputDelegate selectionWillChange], UIKit will cancel
739 // any ongoing auto correction (if enabled) and ask us to set an empty selection.
740 // This is contradictory to our current attempt to set a selection, so we ignore
741 // the callback. UIKit will be re-notified of the new selection after
742 // [UITextInputDelegate selectionDidChange].
743 return;
744 }
745
746 QUITextRange *r = static_cast<QUITextRange *>(range);
747 QList<QInputMethodEvent::Attribute> attrs;
748 attrs << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, r.range.location, r.range.length, 0);
749 QInputMethodEvent e(m_markedText, attrs);
750 [self sendEventToFocusObject:e];
751}
752
753- (UITextRange *)selectedTextRange
754{
755 int cursorPos = [self currentImeState:Qt::ImCursorPosition].toInt();
756 int anchorPos = [self currentImeState:Qt::ImAnchorPosition].toInt();
757 return [QUITextRange rangeWithNSRange:NSMakeRange(qMin(cursorPos, anchorPos), qAbs(anchorPos - cursorPos))];
758}
759
760- (NSString *)textInRange:(UITextRange *)range
761{
762 QString text = [self currentImeState:Qt::ImSurroundingText].toString();
763 if (!m_markedText.isEmpty()) {
764 // [UITextInput textInRange] is sparsely documented, but it turns out that unconfirmed
765 // marked text should be seen as a part of the text document. This is different from
766 // ImSurroundingText, which excludes it.
767 int cursorPos = [self currentImeState:Qt::ImCursorPosition].toInt();
768 text = text.left(cursorPos) + m_markedText + text.mid(cursorPos);
769 }
770
771 int s = static_cast<QUITextPosition *>([range start]).index;
772 int e = static_cast<QUITextPosition *>([range end]).index;
773 return text.mid(s, e - s).toNSString();
774}
775
776- (void)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRange
777{
778 Q_UNUSED(selectedRange);
779
780 m_markedText = markedText ? QString::fromNSString(markedText) : QString();
781
782 static QTextCharFormat markedTextFormat;
783 if (markedTextFormat.isEmpty()) {
784 // There seems to be no way to query how the preedit text
785 // should be drawn. So we need to hard-code the color.
786 markedTextFormat.setBackground(QColor(206, 221, 238));
787 }
788
789 QList<QInputMethodEvent::Attribute> attrs;
790 attrs << QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, 0, markedText.length, markedTextFormat);
791 QInputMethodEvent e(m_markedText, attrs);
792 [self sendEventToFocusObject:e];
793}
794
795- (void)unmarkText
796{
797 if (m_markedText.isEmpty())
798 return;
799
800 QInputMethodEvent e;
801 e.setCommitString(m_markedText);
802 [self sendEventToFocusObject:e];
803
804 m_markedText.clear();
805}
806
807- (NSComparisonResult)comparePosition:(UITextPosition *)position toPosition:(UITextPosition *)other
808{
809 int p = static_cast<QUITextPosition *>(position).index;
810 int o = static_cast<QUITextPosition *>(other).index;
811 if (p > o)
812 return NSOrderedAscending;
813 else if (p < o)
814 return NSOrderedDescending;
815 return NSOrderedSame;
816}
817
818- (UITextRange *)markedTextRange
819{
820 return m_markedText.isEmpty() ? nil : [QUITextRange rangeWithNSRange:NSMakeRange(0, m_markedText.length())];
821}
822
823- (UITextRange *)textRangeFromPosition:(UITextPosition *)fromPosition toPosition:(UITextPosition *)toPosition
824{
825 int f = static_cast<QUITextPosition *>(fromPosition).index;
826 int t = static_cast<QUITextPosition *>(toPosition).index;
827 return [QUITextRange rangeWithNSRange:NSMakeRange(f, t - f)];
828}
829
830- (UITextPosition *)positionFromPosition:(UITextPosition *)position offset:(NSInteger)offset
831{
832 int p = static_cast<QUITextPosition *>(position).index;
833 const int posWithIndex = p + offset;
834 const int textLength = [self currentImeState:Qt::ImSurroundingText].toString().length();
835 if (posWithIndex < 0 || posWithIndex > textLength)
836 return nil;
837 return [QUITextPosition positionWithIndex:posWithIndex];
838}
839
840- (UITextPosition *)positionFromPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction offset:(NSInteger)offset
841{
842 int p = static_cast<QUITextPosition *>(position).index;
843
844 switch (direction) {
845 case UITextLayoutDirectionLeft:
846 return [QUITextPosition positionWithIndex:p - offset];
847 case UITextLayoutDirectionRight:
848 return [QUITextPosition positionWithIndex:p + offset];
849 default:
850 // Qt doesn't support getting the position above or below the current position, so
851 // for those cases we just return the current position, making it a no-op.
852 return position;
853 }
854}
855
856- (UITextPosition *)positionWithinRange:(UITextRange *)range farthestInDirection:(UITextLayoutDirection)direction
857{
858 NSRange r = static_cast<QUITextRange *>(range).range;
859 if (direction == UITextLayoutDirectionRight)
860 return [QUITextPosition positionWithIndex:r.location + r.length];
861 return [QUITextPosition positionWithIndex:r.location];
862}
863
864- (NSInteger)offsetFromPosition:(UITextPosition *)fromPosition toPosition:(UITextPosition *)toPosition
865{
866 int f = static_cast<QUITextPosition *>(fromPosition).index;
867 int t = static_cast<QUITextPosition *>(toPosition).index;
868 return t - f;
869}
870
871- (UIView *)textInputView
872{
873 auto *focusWindow = QGuiApplication::focusWindow();
874 if (!focusWindow)
875 return nil;
876
877 // iOS expects rects we return from other UITextInput methods
878 // to be relative to the view this method returns.
879 // Since QInputMethod returns rects relative to the top level
880 // QWindow, that is also the view we need to return.
881 Q_ASSERT(focusWindow->handle());
882 QPlatformWindow *topLevel = focusWindow->handle();
883 while (QPlatformWindow *p = topLevel->parent())
884 topLevel = p;
885 return reinterpret_cast<UIView *>(topLevel->winId());
886}
887
888- (CGRect)firstRectForRange:(UITextRange *)range
889{
890 QObject *focusObject = QGuiApplication::focusObject();
891 if (!focusObject)
892 return CGRectZero;
893
894 // Using a work-around to get the current rect until
895 // a better API is in place:
896 if (!m_markedText.isEmpty())
897 return CGRectZero;
898
899 int cursorPos = [self currentImeState:Qt::ImCursorPosition].toInt();
900 int anchorPos = [self currentImeState:Qt::ImAnchorPosition].toInt();
901
902 NSRange r = static_cast<QUITextRange*>(range).range;
903 QList<QInputMethodEvent::Attribute> attrs;
904 attrs << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, r.location, 0, 0);
905 {
906 QInputMethodEvent e(m_markedText, attrs);
907 [self sendEventToFocusObject:e];
908 }
909 QRectF startRect = QPlatformInputContext::cursorRectangle();
910
911 attrs = QList<QInputMethodEvent::Attribute>();
912 attrs << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, r.location + r.length, 0, 0);
913 {
914 QInputMethodEvent e(m_markedText, attrs);
915 [self sendEventToFocusObject:e];
916 }
917 QRectF endRect = QPlatformInputContext::cursorRectangle();
918
919 if (cursorPos != int(r.location + r.length) || cursorPos != anchorPos) {
920 attrs = QList<QInputMethodEvent::Attribute>();
921 attrs << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, qMin(cursorPos, anchorPos), qAbs(cursorPos - anchorPos), 0);
922 QInputMethodEvent e(m_markedText, attrs);
923 [self sendEventToFocusObject:e];
924 }
925
926 return startRect.united(endRect).toCGRect();
927}
928
929- (NSArray<UITextSelectionRect *> *)selectionRectsForRange:(UITextRange *)range
930{
931 Q_UNUSED(range);
932 // This method is supposed to return a rectangle for each line with selection. Since we don't
933 // expose an API in Qt/IM for getting this information, and since we never seems to be getting
934 // a call from UIKit for this, we return an empty array until a need arise.
935 return [[NSArray<UITextSelectionRect *> new] autorelease];
936}
937
938- (CGRect)caretRectForPosition:(UITextPosition *)position
939{
940 Q_UNUSED(position);
941 // Assume for now that position is always the same as
942 // cursor index until a better API is in place:
943 return QPlatformInputContext::cursorRectangle().toCGRect();
944}
945
946- (void)replaceRange:(UITextRange *)range withText:(NSString *)text
947{
948 [self setSelectedTextRange:range];
949
950 QInputMethodEvent e;
951 e.setCommitString(QString::fromNSString(text));
952 [self sendEventToFocusObject:e];
953}
954
955- (void)setBaseWritingDirection:(NSWritingDirection)writingDirection forRange:(UITextRange *)range
956{
957 Q_UNUSED(writingDirection);
958 Q_UNUSED(range);
959 // Writing direction is handled by QLocale
960}
961
962- (NSWritingDirection)baseWritingDirectionForPosition:(UITextPosition *)position inDirection:(UITextStorageDirection)direction
963{
964 Q_UNUSED(position);
965 Q_UNUSED(direction);
966 if (QLocale::system().textDirection() == Qt::RightToLeft)
967 return NSWritingDirectionRightToLeft;
968 return NSWritingDirectionLeftToRight;
969}
970
971- (UITextRange *)characterRangeByExtendingPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction
972{
973 int p = static_cast<QUITextPosition *>(position).index;
974 if (direction == UITextLayoutDirectionLeft)
975 return [QUITextRange rangeWithNSRange:NSMakeRange(0, p)];
976 int l = [self currentImeState:Qt::ImSurroundingText].toString().length();
977 return [QUITextRange rangeWithNSRange:NSMakeRange(p, l - p)];
978}
979
980- (UITextPosition *)closestPositionToPoint:(CGPoint)point
981{
982 int textPos = QPlatformInputContext::queryFocusObject(Qt::ImCursorPosition, QPointF::fromCGPoint(point)).toInt();
983 return [QUITextPosition positionWithIndex:textPos];
984}
985
986- (UITextPosition *)closestPositionToPoint:(CGPoint)point withinRange:(UITextRange *)range
987{
988 // No API in Qt for determining this. Use sensible default instead:
989 Q_UNUSED(point);
990 Q_UNUSED(range);
991 return [QUITextPosition positionWithIndex:[self currentImeState:Qt::ImCursorPosition].toInt()];
992}
993
994- (UITextRange *)characterRangeAtPoint:(CGPoint)point
995{
996 // No API in Qt for determining this. Use sensible default instead:
997 Q_UNUSED(point);
998 return [QUITextRange rangeWithNSRange:NSMakeRange([self currentImeState:Qt::ImCursorPosition].toInt(), 0)];
999}
1000
1001- (void)setMarkedTextStyle:(NSDictionary *)style
1002{
1003 Q_UNUSED(style);
1004 // No-one is going to change our style. If UIKit itself did that
1005 // it would be very welcome, since then we knew how to style marked
1006 // text instead of just guessing...
1007}
1008
1009#ifndef Q_OS_TVOS
1010- (NSDictionary *)textStylingAtPosition:(UITextPosition *)position inDirection:(UITextStorageDirection)direction
1011{
1012 Q_UNUSED(position);
1013 Q_UNUSED(direction);
1014
1015 QObject *focusObject = QGuiApplication::focusObject();
1016 if (!focusObject)
1017 return @{};
1018
1019 // Assume position is the same as the cursor for now. QInputMethodQueryEvent with Qt::ImFont
1020 // needs to be extended to take an extra position argument before this can be fully correct.
1021 QInputMethodQueryEvent e(Qt::ImFont);
1022 QCoreApplication::sendEvent(focusObject, &e);
1023 QFont qfont = qvariant_cast<QFont>(e.value(Qt::ImFont));
1024 UIFont *uifont = [UIFont fontWithName:qfont.family().toNSString() size:qfont.pointSize()];
1025 if (!uifont)
1026 return @{};
1027 return @{NSFontAttributeName: uifont};
1028}
1029#endif
1030
1031- (NSDictionary *)markedTextStyle
1032{
1033 return [NSDictionary dictionary];
1034}
1035
1036- (BOOL)hasText
1037{
1038 return YES;
1039}
1040
1041- (void)insertText:(NSString *)text
1042{
1043 QObject *focusObject = QGuiApplication::focusObject();
1044 if (!focusObject)
1045 return;
1046
1047 if ([text isEqualToString:@"\n"]) {
1048 [self sendKeyPressRelease:Qt::Key_Return modifiers:Qt::NoModifier];
1049
1050 // An onEnter handler of a TextInput might move to the next input by calling
1051 // nextInput.forceActiveFocus() which changes the focusObject.
1052 // In that case we don't want to hide the VKB.
1053 if (focusObject != QGuiApplication::focusObject()) {
1054 qImDebug() << "focusObject already changed, not resigning first responder.";
1055 return;
1056 }
1057
1058 if (self.returnKeyType == UIReturnKeyDone || self.returnKeyType == UIReturnKeyGo
1059 || self.returnKeyType == UIReturnKeySend || self.returnKeyType == UIReturnKeySearch)
1060 [self resignFirstResponder];
1061
1062 return;
1063 }
1064
1065 QInputMethodEvent e;
1066 e.setCommitString(QString::fromNSString(text));
1067 [self sendEventToFocusObject:e];
1068}
1069
1070- (void)deleteBackward
1071{
1072 // UITextInput selects the text to be deleted before calling this method. To avoid
1073 // drawing the selection, we flush after posting the key press/release.
1074 [self sendKeyPressRelease:Qt::Key_Backspace modifiers:Qt::NoModifier];
1075}
1076
1077@end
\inmodule QtCore
Definition qvariant.h:68
long NSInteger
Q_FORWARD_DECLARE_OBJC_CLASS(NSString)
#define qImDebug
Definition qiosglobal.h:21