1 | // Copyright (C) 2020 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 | |
4 | #ifndef QWIDGETLINECONTROL_P_H |
5 | #define QWIDGETLINECONTROL_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtWidgets/private/qtwidgetsglobal_p.h> |
19 | |
20 | #include "private/qwidget_p.h" |
21 | #include "QtWidgets/qlineedit.h" |
22 | #include "QtGui/qtextlayout.h" |
23 | #include "QtWidgets/qstyleoption.h" |
24 | #include "QtCore/qpointer.h" |
25 | #include "QtGui/qclipboard.h" |
26 | #include "QtGui/qinputmethod.h" |
27 | #include "QtCore/qpoint.h" |
28 | #if QT_CONFIG(completer) |
29 | #include "QtWidgets/qcompleter.h" |
30 | #endif |
31 | #include "QtCore/qthread.h" |
32 | #include "QtGui/private/qinputcontrol_p.h" |
33 | |
34 | #include "qplatformdefs.h" |
35 | |
36 | #include <vector> |
37 | #include <memory> |
38 | |
39 | #ifdef Q_OS_WIN |
40 | # include <qt_windows.h> |
41 | #endif |
42 | #ifdef DrawText |
43 | # undef DrawText |
44 | #endif |
45 | |
46 | QT_REQUIRE_CONFIG(lineedit); |
47 | |
48 | QT_BEGIN_NAMESPACE |
49 | |
50 | class Q_WIDGETS_EXPORT QWidgetLineControl : public QInputControl |
51 | { |
52 | Q_OBJECT |
53 | |
54 | public: |
55 | QWidgetLineControl(const QString &txt = QString()) |
56 | : QInputControl(LineEdit) |
57 | , m_cursor(0), m_preeditCursor(0), m_cursorWidth(0), m_layoutDirection(Qt::LayoutDirectionAuto), |
58 | m_hideCursor(false), m_separator(0), m_readOnly(0), |
59 | m_dragEnabled(0), m_echoMode(0), m_textDirty(0), m_selDirty(0), |
60 | m_validInput(1), m_blinkStatus(0), m_blinkEnabled(false), m_blinkTimer(0), m_deleteAllTimer(0), |
61 | m_ascent(0), m_maxLength(32767), m_lastCursorPos(-1), |
62 | m_tripleClickTimer(0), m_maskData(nullptr), m_modifiedState(0), m_undoState(0), |
63 | m_selstart(0), m_selend(0), m_passwordEchoEditing(false) |
64 | , m_passwordEchoTimer(0) |
65 | , m_passwordMaskDelay(-1) |
66 | #if defined(QT_BUILD_INTERNAL) |
67 | , m_passwordMaskDelayOverride(-1) |
68 | #endif |
69 | , m_keyboardScheme(0) |
70 | , m_accessibleObject(nullptr) |
71 | { |
72 | init(txt); |
73 | } |
74 | |
75 | ~QWidgetLineControl() |
76 | { |
77 | // If this control is used for password input, we don't want the |
78 | // password data to stay in the process memory, therefore we need |
79 | // to zero it out |
80 | if (m_echoMode != QLineEdit::Normal) |
81 | m_text.fill(c: u'\0'); |
82 | } |
83 | |
84 | void setAccessibleObject(QObject *object) |
85 | { |
86 | Q_ASSERT(object); |
87 | m_accessibleObject = object; |
88 | } |
89 | |
90 | QObject *accessibleObject() |
91 | { |
92 | if (m_accessibleObject) |
93 | return m_accessibleObject; |
94 | return parent(); |
95 | } |
96 | |
97 | int nextMaskBlank(int pos) |
98 | { |
99 | int c = findInMask(pos, forward: true, findSeparator: false); |
100 | m_separator |= (c != pos); |
101 | return (c != -1 ? c : m_maxLength); |
102 | } |
103 | |
104 | int prevMaskBlank(int pos) |
105 | { |
106 | int c = findInMask(pos, forward: false, findSeparator: false); |
107 | m_separator |= (c != pos); |
108 | return (c != -1 ? c : 0); |
109 | } |
110 | |
111 | bool isUndoAvailable() const; |
112 | bool isRedoAvailable() const; |
113 | void clearUndo() { m_history.clear(); m_modifiedState = m_undoState = 0; } |
114 | |
115 | bool isModified() const { return m_modifiedState != m_undoState; } |
116 | void setModified(bool modified) { m_modifiedState = modified ? -1 : m_undoState; } |
117 | |
118 | bool allSelected() const { return !m_text.isEmpty() && m_selstart == 0 && m_selend == (int)m_text.size(); } |
119 | bool hasSelectedText() const { return !m_text.isEmpty() && m_selend > m_selstart; } |
120 | |
121 | int width() const { return qRound(d: m_textLayout.lineAt(i: 0).width()) + 1; } |
122 | int height() const { return qRound(d: m_textLayout.lineAt(i: 0).height()) + 1; } |
123 | int ascent() const { return m_ascent; } |
124 | qreal naturalTextWidth() const { return m_textLayout.lineAt(i: 0).naturalTextWidth(); } |
125 | |
126 | void setSelection(int start, int length); |
127 | |
128 | inline QString selectedText() const { return hasSelectedText() ? m_text.mid(position: m_selstart, n: m_selend - m_selstart) : QString(); } |
129 | QString textBeforeSelection() const { return hasSelectedText() ? m_text.left(n: m_selstart) : QString(); } |
130 | QString textAfterSelection() const { return hasSelectedText() ? m_text.mid(position: m_selend) : QString(); } |
131 | |
132 | int selectionStart() const { return hasSelectedText() ? m_selstart : -1; } |
133 | int selectionEnd() const { return hasSelectedText() ? m_selend : -1; } |
134 | #if defined (Q_OS_ANDROID) |
135 | bool isSelectableByMouse() const { return true; } |
136 | #endif |
137 | bool inSelection(int x) const |
138 | { |
139 | if (m_selstart >= m_selend) |
140 | return false; |
141 | int pos = xToPos(x, QTextLine::CursorOnCharacter); |
142 | return pos >= m_selstart && pos < m_selend; |
143 | } |
144 | |
145 | void removeSelection() |
146 | { |
147 | int priorState = m_undoState; |
148 | removeSelectedText(); |
149 | finishChange(validateFromState: priorState); |
150 | } |
151 | |
152 | int start() const { return 0; } |
153 | int end() const { return m_text.size(); } |
154 | |
155 | #ifndef QT_NO_CLIPBOARD |
156 | void copy(QClipboard::Mode mode = QClipboard::Clipboard) const; |
157 | void paste(QClipboard::Mode mode = QClipboard::Clipboard); |
158 | #endif |
159 | |
160 | int cursor() const{ return m_cursor; } |
161 | int preeditCursor() const { return m_preeditCursor; } |
162 | |
163 | int cursorWidth() const { return m_cursorWidth; } |
164 | void setCursorWidth(int value) { m_cursorWidth = value; } |
165 | |
166 | Qt::CursorMoveStyle cursorMoveStyle() const { return m_textLayout.cursorMoveStyle(); } |
167 | void setCursorMoveStyle(Qt::CursorMoveStyle style) { m_textLayout.setCursorMoveStyle(style); } |
168 | |
169 | void moveCursor(int pos, bool mark = false); |
170 | void cursorForward(bool mark, int steps) |
171 | { |
172 | int c = m_cursor; |
173 | if (steps > 0) { |
174 | while (steps--) |
175 | c = cursorMoveStyle() == Qt::VisualMoveStyle ? m_textLayout.rightCursorPosition(oldPos: c) |
176 | : m_textLayout.nextCursorPosition(oldPos: c); |
177 | } else if (steps < 0) { |
178 | while (steps++) |
179 | c = cursorMoveStyle() == Qt::VisualMoveStyle ? m_textLayout.leftCursorPosition(oldPos: c) |
180 | : m_textLayout.previousCursorPosition(oldPos: c); |
181 | } |
182 | moveCursor(pos: c, mark); |
183 | } |
184 | |
185 | void cursorWordForward(bool mark) { moveCursor(pos: m_textLayout.nextCursorPosition(oldPos: m_cursor, mode: QTextLayout::SkipWords), mark); } |
186 | void cursorWordBackward(bool mark) { moveCursor(pos: m_textLayout.previousCursorPosition(oldPos: m_cursor, mode: QTextLayout::SkipWords), mark); } |
187 | |
188 | void home(bool mark) { moveCursor(pos: 0, mark); } |
189 | void end(bool mark) { moveCursor(pos: m_text.size(), mark); } |
190 | |
191 | int xToPos(int x, QTextLine::CursorPosition = QTextLine::CursorBetweenCharacters) const; |
192 | QRect rectForPos(int pos) const; |
193 | QRect cursorRect() const; |
194 | QRect anchorRect() const; |
195 | |
196 | qreal cursorToX(int cursor) const { return m_textLayout.lineAt(i: 0).cursorToX(cursorPos: cursor); } |
197 | qreal cursorToX() const |
198 | { |
199 | int cursor = m_cursor; |
200 | if (m_preeditCursor != -1) |
201 | cursor += m_preeditCursor; |
202 | return cursorToX(cursor); |
203 | } |
204 | |
205 | bool isReadOnly() const { return m_readOnly; } |
206 | void setReadOnly(bool enable); |
207 | |
208 | QString text() const |
209 | { |
210 | QString content = m_text; |
211 | QString res = m_maskData ? stripString(str: content) : content; |
212 | return (res.isNull() ? QString::fromLatin1(ba: "" ) : res); |
213 | } |
214 | void setText(const QString &txt) |
215 | { |
216 | #ifndef QT_NO_IM |
217 | if (composeMode()) |
218 | QGuiApplication::inputMethod()->reset(); |
219 | #endif |
220 | internalSetText(txt, pos: -1, edited: false); |
221 | } |
222 | void commitPreedit(); |
223 | |
224 | QString displayText() const { return m_textLayout.text(); } |
225 | |
226 | QString surroundingText() const |
227 | { |
228 | return m_text.isNull() ? QString::fromLatin1(ba: "" ) : m_text; |
229 | } |
230 | |
231 | void backspace(); |
232 | void del(); |
233 | void deselect() { internalDeselect(); finishChange(); } |
234 | void selectAll() { m_selstart = m_selend = m_cursor = 0; moveCursor(pos: m_text.size(), mark: true); } |
235 | |
236 | void insert(const QString &); |
237 | void clear(); |
238 | void undo(); |
239 | void redo() { internalRedo(); finishChange(); } |
240 | void selectWordAtPos(int); |
241 | |
242 | uint echoMode() const { return m_echoMode; } |
243 | void setEchoMode(uint mode) |
244 | { |
245 | cancelPasswordEchoTimer(); |
246 | m_echoMode = mode; |
247 | m_passwordEchoEditing = false; |
248 | |
249 | // If this control is used for password input, we want to minimize |
250 | // the possibility of string reallocation not to leak (parts of) |
251 | // the password. |
252 | if (m_echoMode != QLineEdit::Normal) |
253 | m_text.reserve(asize: 30); |
254 | |
255 | updateDisplayText(); |
256 | } |
257 | |
258 | int maxLength() const { return m_maxLength; } |
259 | void setMaxLength(int maxLength) |
260 | { |
261 | if (m_maskData) |
262 | return; |
263 | m_maxLength = maxLength; |
264 | setText(m_text); |
265 | } |
266 | |
267 | #ifndef QT_NO_VALIDATOR |
268 | const QValidator *validator() const { return m_validator; } |
269 | void setValidator(const QValidator *v) { m_validator = const_cast<QValidator*>(v); } |
270 | #endif |
271 | |
272 | #if QT_CONFIG(completer) |
273 | QCompleter *completer() const { return m_completer; } |
274 | /* Note that you must set the widget for the completer separately */ |
275 | void setCompleter(const QCompleter *c) { m_completer = const_cast<QCompleter*>(c); } |
276 | void complete(int key); |
277 | #endif |
278 | |
279 | int cursorPosition() const { return m_cursor; } |
280 | void setCursorPosition(int pos) { if (pos <= m_text.size()) moveCursor(pos: qMax(a: 0, b: pos)); } |
281 | |
282 | bool hasAcceptableInput() const { return hasAcceptableInput(text: m_text); } |
283 | bool fixup(); |
284 | |
285 | QString inputMask() const |
286 | { |
287 | QString mask; |
288 | if (m_maskData) { |
289 | mask = m_inputMask; |
290 | if (m_blank != u' ') { |
291 | mask += u';'; |
292 | mask += m_blank; |
293 | } |
294 | } |
295 | return mask; |
296 | } |
297 | void setInputMask(const QString &mask) |
298 | { |
299 | parseInputMask(maskFields: mask); |
300 | if (m_maskData) |
301 | moveCursor(pos: nextMaskBlank(pos: 0)); |
302 | } |
303 | |
304 | // input methods |
305 | #ifndef QT_NO_IM |
306 | bool composeMode() const { return !m_textLayout.preeditAreaText().isEmpty(); } |
307 | void setPreeditArea(int cursor, const QString &text) { m_textLayout.setPreeditArea(position: cursor, text); } |
308 | #endif |
309 | |
310 | QString preeditAreaText() const { return m_textLayout.preeditAreaText(); } |
311 | |
312 | void updatePasswordEchoEditing(bool editing); |
313 | bool passwordEchoEditing() const { |
314 | if (m_passwordEchoTimer != 0) |
315 | return true; |
316 | return m_passwordEchoEditing ; |
317 | } |
318 | |
319 | QChar passwordCharacter() const { return m_passwordCharacter; } |
320 | void setPasswordCharacter(QChar character) { m_passwordCharacter = character; updateDisplayText(); } |
321 | |
322 | int passwordMaskDelay() const { return m_passwordMaskDelay; } |
323 | void setPasswordMaskDelay(int delay) { m_passwordMaskDelay = delay; } |
324 | |
325 | Qt::LayoutDirection layoutDirection() const { |
326 | if (m_layoutDirection == Qt::LayoutDirectionAuto && !m_text.isEmpty()) |
327 | return m_text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight; |
328 | return m_layoutDirection; |
329 | } |
330 | void setLayoutDirection(Qt::LayoutDirection direction) |
331 | { |
332 | if (direction != m_layoutDirection) { |
333 | m_layoutDirection = direction; |
334 | updateDisplayText(); |
335 | } |
336 | } |
337 | |
338 | void setFont(const QFont &font) { m_textLayout.setFont(font); updateDisplayText(); } |
339 | |
340 | void processInputMethodEvent(QInputMethodEvent *event); |
341 | void processKeyEvent(QKeyEvent* ev); |
342 | |
343 | void setBlinkingCursorEnabled(bool enable); |
344 | void updateCursorBlinking(); |
345 | void resetCursorBlinkTimer(); |
346 | |
347 | bool cursorBlinkStatus() const { return m_blinkStatus; } |
348 | |
349 | QString cancelText() const { return m_cancelText; } |
350 | void setCancelText(const QString &text) { m_cancelText = text; } |
351 | |
352 | const QPalette &palette() const { return m_palette; } |
353 | void setPalette(const QPalette &p) { m_palette = p; } |
354 | |
355 | enum DrawFlags { |
356 | DrawText = 0x01, |
357 | DrawSelections = 0x02, |
358 | DrawCursor = 0x04, |
359 | DrawAll = DrawText | DrawSelections | DrawCursor |
360 | }; |
361 | void draw(QPainter *, const QPoint &, const QRect &, int flags = DrawAll); |
362 | |
363 | #ifndef QT_NO_SHORTCUT |
364 | void processShortcutOverrideEvent(QKeyEvent *ke); |
365 | #endif |
366 | |
367 | QTextLayout *textLayout() const |
368 | { |
369 | return &m_textLayout; |
370 | } |
371 | |
372 | private: |
373 | void init(const QString &txt); |
374 | void removeSelectedText(); |
375 | void internalSetText(const QString &txt, int pos = -1, bool edited = true); |
376 | void updateDisplayText(bool forceUpdate = false); |
377 | |
378 | void internalInsert(const QString &s); |
379 | void internalDelete(bool wasBackspace = false); |
380 | void internalRemove(int pos); |
381 | |
382 | inline void internalDeselect() |
383 | { |
384 | m_selDirty |= (m_selend > m_selstart); |
385 | m_selstart = m_selend = 0; |
386 | } |
387 | |
388 | void internalUndo(int until = -1); |
389 | void internalRedo(); |
390 | |
391 | QString m_text; |
392 | QPalette m_palette; |
393 | int m_cursor; |
394 | int m_preeditCursor; |
395 | int m_cursorWidth; |
396 | Qt::LayoutDirection m_layoutDirection; |
397 | uint m_hideCursor : 1; // used to hide the m_cursor inside preedit areas |
398 | uint m_separator : 1; |
399 | uint m_readOnly : 1; |
400 | uint m_dragEnabled : 1; |
401 | uint m_echoMode : 2; |
402 | uint m_textDirty : 1; |
403 | uint m_selDirty : 1; |
404 | uint m_validInput : 1; |
405 | uint m_blinkStatus : 1; |
406 | uint m_blinkEnabled : 1; |
407 | int m_blinkTimer; |
408 | int m_deleteAllTimer; |
409 | int m_ascent; |
410 | int m_maxLength; |
411 | int m_lastCursorPos; |
412 | QList<int> m_transactions; |
413 | QPoint m_tripleClick; |
414 | int m_tripleClickTimer; |
415 | QString m_cancelText; |
416 | |
417 | void emitCursorPositionChanged(); |
418 | |
419 | bool finishChange(int validateFromState = -1, bool update = false, bool edited = true); |
420 | |
421 | #ifndef QT_NO_VALIDATOR |
422 | QPointer<QValidator> m_validator; |
423 | #endif |
424 | QPointer<QCompleter> m_completer; |
425 | #if QT_CONFIG(completer) |
426 | bool advanceToEnabledItem(int dir); |
427 | #endif |
428 | |
429 | struct MaskInputData { |
430 | enum Casemode { NoCaseMode, Upper, Lower }; |
431 | QChar maskChar; // either the separator char or the inputmask |
432 | bool separator; |
433 | Casemode caseMode; |
434 | }; |
435 | QString m_inputMask; |
436 | QChar m_blank; |
437 | std::unique_ptr<MaskInputData[]> m_maskData; |
438 | |
439 | // undo/redo handling |
440 | enum CommandType { Separator, Insert, Remove, Delete, RemoveSelection, DeleteSelection, SetSelection }; |
441 | struct Command { |
442 | inline Command(CommandType t, int p, QChar c, int ss, int se) : type(t),uc(c),pos(p),selStart(ss),selEnd(se) {} |
443 | uint type : 4; |
444 | QChar uc; |
445 | int pos, selStart, selEnd; |
446 | }; |
447 | int m_modifiedState; |
448 | int m_undoState; |
449 | std::vector<Command> m_history; |
450 | void addCommand(const Command& cmd); |
451 | |
452 | inline void separate() { m_separator = true; } |
453 | |
454 | // selection |
455 | int m_selstart; |
456 | int m_selend; |
457 | |
458 | // masking |
459 | void parseInputMask(const QString &maskFields); |
460 | bool isValidInput(QChar key, QChar mask) const; |
461 | bool hasAcceptableInput(const QString &text) const; |
462 | QString maskString(int pos, const QString &str, bool clear = false) const; |
463 | QString clearString(int pos, int len) const; |
464 | QString stripString(const QString &str) const; |
465 | int findInMask(int pos, bool forward, bool findSeparator, QChar searchChar = QChar()) const; |
466 | |
467 | // complex text layout (must be mutable so it can be reshaped at will) |
468 | mutable QTextLayout m_textLayout; |
469 | |
470 | bool m_passwordEchoEditing; |
471 | QChar m_passwordCharacter; |
472 | int m_passwordEchoTimer; |
473 | int m_passwordMaskDelay; |
474 | void cancelPasswordEchoTimer() |
475 | { |
476 | if (m_passwordEchoTimer != 0) { |
477 | killTimer(id: m_passwordEchoTimer); |
478 | m_passwordEchoTimer = 0; |
479 | } |
480 | } |
481 | |
482 | int redoTextLayout() const; |
483 | |
484 | public: |
485 | #if defined(QT_BUILD_INTERNAL) |
486 | int m_passwordMaskDelayOverride; |
487 | #endif |
488 | |
489 | Q_SIGNALS: |
490 | void cursorPositionChanged(int, int); |
491 | void selectionChanged(); |
492 | |
493 | void displayTextChanged(const QString &); |
494 | void textChanged(const QString &); |
495 | void textEdited(const QString &); |
496 | |
497 | void resetInputContext(); |
498 | void updateMicroFocus(); |
499 | |
500 | void accepted(); |
501 | void editingFinished(); |
502 | void updateNeeded(const QRect &); |
503 | void inputRejected(); |
504 | |
505 | #ifdef QT_KEYPAD_NAVIGATION |
506 | void editFocusChange(bool); |
507 | #endif |
508 | protected: |
509 | virtual void timerEvent(QTimerEvent *event) override; |
510 | |
511 | private Q_SLOTS: |
512 | void _q_deleteSelected(); |
513 | |
514 | private: |
515 | int m_keyboardScheme; |
516 | |
517 | // accessibility events are sent for this object |
518 | QObject *m_accessibleObject; |
519 | }; |
520 | |
521 | QT_END_NAMESPACE |
522 | |
523 | #endif // QWIDGETLINECONTROL_P_H |
524 | |