1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QLINECONTROL_P_H
43 #define QLINECONTROL_P_H
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
56 #include "QtCore/qglobal.h"
58 #ifndef QT_NO_LINEEDIT
59 #include "private/qwidget_p.h"
60 #include "QtGui/qlineedit.h"
61 #include "QtGui/qtextlayout.h"
62 #include "QtGui/qstyleoption.h"
63 #include "QtCore/qpointer.h"
64 #include "QtGui/qclipboard.h"
65 #include "QtCore/qpoint.h"
66 #include "QtGui/qcompleter.h"
67 #include "QtGui/qaccessible.h"
68 #include "QtCore/qthread.h"
70 #include "qplatformdefs.h"
78 class Q_GUI_EXPORT QLineControl : public QObject
83 QLineControl(const QString &txt = QString())
84 : m_cursor(0), m_preeditCursor(0), m_cursorWidth(0), m_layoutDirection(Qt::LayoutDirectionAuto),
85 m_hideCursor(false), m_separator(0), m_readOnly(0),
86 m_dragEnabled(0), m_echoMode(0), m_textDirty(0), m_selDirty(0),
87 m_validInput(1), m_blinkStatus(0), m_blinkPeriod(0), m_blinkTimer(0), m_deleteAllTimer(0),
88 m_ascent(0), m_maxLength(32767), m_lastCursorPos(-1),
89 m_tripleClickTimer(0), m_maskData(0), m_modifiedState(0), m_undoState(0),
90 m_selstart(0), m_selend(0), m_passwordEchoEditing(false)
91 #ifdef QT_GUI_PASSWORD_ECHO_DELAY
92 , m_passwordEchoTimer(0)
95 , m_threadChecks(false)
96 , m_textLayoutThread(0)
104 delete [] m_maskData;
107 int nextMaskBlank(int pos)
109 int c = findInMask(pos, true, false);
110 m_separator |= (c != pos);
111 return (c != -1 ? c : m_maxLength);
114 int prevMaskBlank(int pos)
116 int c = findInMask(pos, false, false);
117 m_separator |= (c != pos);
118 return (c != -1 ? c : 0);
121 bool isUndoAvailable() const { return !m_readOnly && m_undoState; }
122 bool isRedoAvailable() const { return !m_readOnly && m_undoState < (int)m_history.size(); }
123 void clearUndo() { m_history.clear(); m_modifiedState = m_undoState = 0; }
125 bool isModified() const { return m_modifiedState != m_undoState; }
126 void setModified(bool modified) { m_modifiedState = modified ? -1 : m_undoState; }
128 bool allSelected() const { return !m_text.isEmpty() && m_selstart == 0 && m_selend == (int)m_text.length(); }
129 bool hasSelectedText() const { return !m_text.isEmpty() && m_selend > m_selstart; }
131 int width() const { return qRound(m_textLayout.lineAt(0).width()) + 1; }
132 int height() const { return qRound(m_textLayout.lineAt(0).height()) + 1; }
133 int ascent() const { return m_ascent; }
134 qreal naturalTextWidth() const { return m_textLayout.lineAt(0).naturalTextWidth(); }
136 void setSelection(int start, int length);
138 inline QString selectedText() const { return hasSelectedText() ? m_text.mid(m_selstart, m_selend - m_selstart) : QString(); }
139 QString textBeforeSelection() const { return hasSelectedText() ? m_text.left(m_selstart) : QString(); }
140 QString textAfterSelection() const { return hasSelectedText() ? m_text.mid(m_selend) : QString(); }
142 int selectionStart() const { return hasSelectedText() ? m_selstart : -1; }
143 int selectionEnd() const { return hasSelectedText() ? m_selend : -1; }
144 bool inSelection(int x) const
146 if (m_selstart >= m_selend)
148 int pos = xToPos(x, QTextLine::CursorOnCharacter);
149 return pos >= m_selstart && pos < m_selend;
152 void removeSelection()
154 int priorState = m_undoState;
155 removeSelectedText();
156 finishChange(priorState);
159 int start() const { return 0; }
160 int end() const { return m_text.length(); }
162 #ifndef QT_NO_CLIPBOARD
163 void copy(QClipboard::Mode mode = QClipboard::Clipboard) const;
164 void paste(QClipboard::Mode mode = QClipboard::Clipboard);
167 int cursor() const{ return m_cursor; }
168 int preeditCursor() const { return m_preeditCursor; }
170 int cursorWidth() const { return m_cursorWidth; }
171 void setCursorWidth(int value) { m_cursorWidth = value; }
173 Qt::CursorMoveStyle cursorMoveStyle() const { return m_textLayout.cursorMoveStyle(); }
174 void setCursorMoveStyle(Qt::CursorMoveStyle style) { m_textLayout.setCursorMoveStyle(style); }
176 void moveCursor(int pos, bool mark = false);
177 void cursorForward(bool mark, int steps)
182 c = cursorMoveStyle() == Qt::VisualMoveStyle ? m_textLayout.rightCursorPosition(c)
183 : m_textLayout.nextCursorPosition(c);
184 } else if (steps < 0) {
186 c = cursorMoveStyle() == Qt::VisualMoveStyle ? m_textLayout.leftCursorPosition(c)
187 : m_textLayout.previousCursorPosition(c);
192 void cursorWordForward(bool mark) { moveCursor(m_textLayout.nextCursorPosition(m_cursor, QTextLayout::SkipWords), mark); }
193 void cursorWordBackward(bool mark) { moveCursor(m_textLayout.previousCursorPosition(m_cursor, QTextLayout::SkipWords), mark); }
195 void home(bool mark) { moveCursor(0, mark); }
196 void end(bool mark) { moveCursor(text().length(), mark); }
198 int xToPos(int x, QTextLine::CursorPosition = QTextLine::CursorBetweenCharacters) const;
199 QRect cursorRect() const;
201 qreal cursorToX(int cursor) const { return m_textLayout.lineAt(0).cursorToX(cursor); }
202 qreal cursorToX() const
204 int cursor = m_cursor;
205 if (m_preeditCursor != -1)
206 cursor += m_preeditCursor;
207 return cursorToX(cursor);
210 bool isReadOnly() const { return m_readOnly; }
211 void setReadOnly(bool enable) { m_readOnly = enable; }
215 QString res = m_maskData ? stripString(m_text) : m_text;
216 return (res.isNull() ? QString::fromLatin1("") : res);
218 void setText(const QString &txt) { internalSetText(txt, -1, false); }
219 QString displayText() const { return m_textLayout.text(); }
223 void deselect() { internalDeselect(); finishChange(); }
224 void selectAll() { m_selstart = m_selend = m_cursor = 0; moveCursor(m_text.length(), true); }
226 void insert(const QString &);
228 void undo() { internalUndo(); finishChange(-1, true); }
229 void redo() { internalRedo(); finishChange(); }
230 void selectWordAtPos(int);
232 uint echoMode() const { return m_echoMode; }
233 void setEchoMode(uint mode)
235 cancelPasswordEchoTimer();
237 m_passwordEchoEditing = false;
241 int maxLength() const { return m_maxLength; }
242 void setMaxLength(int maxLength)
246 m_maxLength = maxLength;
250 #ifndef QT_NO_VALIDATOR
251 const QValidator *validator() const { return m_validator; }
252 void setValidator(const QValidator *v) { m_validator = const_cast<QValidator*>(v); }
255 #ifndef QT_NO_COMPLETER
256 QCompleter *completer() const { return m_completer; }
257 /* Note that you must set the widget for the completer separately */
258 void setCompleter(const QCompleter *c) { m_completer = const_cast<QCompleter*>(c); }
259 void complete(int key);
262 int cursorPosition() const { return m_cursor; }
263 void setCursorPosition(int pos) { if (pos <= m_text.length()) moveCursor(qMax(0, pos)); }
265 bool hasAcceptableInput() const { return hasAcceptableInput(m_text); }
268 QString inputMask() const { return m_maskData ? m_inputMask + QLatin1Char(';') + m_blank : QString(); }
269 void setInputMask(const QString &mask)
271 parseInputMask(mask);
273 moveCursor(nextMaskBlank(0));
278 bool composeMode() const { return !m_textLayout.preeditAreaText().isEmpty(); }
279 void setPreeditArea(int cursor, const QString &text) { m_textLayout.setPreeditArea(cursor, text); }
282 QString preeditAreaText() const { return m_textLayout.preeditAreaText(); }
284 void updatePasswordEchoEditing(bool editing);
285 bool passwordEchoEditing() const {
286 #ifdef QT_GUI_PASSWORD_ECHO_DELAY
287 if (m_passwordEchoTimer != 0)
290 return m_passwordEchoEditing ;
293 QChar passwordCharacter() const { return m_passwordCharacter; }
294 void setPasswordCharacter(const QChar &character) { m_passwordCharacter = character; updateDisplayText(); }
296 Qt::LayoutDirection layoutDirection() const {
297 if (m_layoutDirection == Qt::LayoutDirectionAuto) {
298 if (m_text.isEmpty())
299 return QApplication::keyboardInputDirection();
300 return m_text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight;
302 return m_layoutDirection;
304 void setLayoutDirection(Qt::LayoutDirection direction)
306 if (direction != m_layoutDirection) {
307 m_layoutDirection = direction;
312 void setFont(const QFont &font) { m_textLayout.setFont(font); updateDisplayText(); }
314 void processInputMethodEvent(QInputMethodEvent *event);
315 void processMouseEvent(QMouseEvent* ev);
316 void processKeyEvent(QKeyEvent* ev);
318 int cursorBlinkPeriod() const { return m_blinkPeriod; }
319 void setCursorBlinkPeriod(int msec);
320 void resetCursorBlinkTimer();
322 bool cursorBlinkStatus() const { return m_blinkStatus; }
324 QString cancelText() const { return m_cancelText; }
325 void setCancelText(const QString &text) { m_cancelText = text; }
327 const QPalette &palette() const { return m_palette; }
328 void setPalette(const QPalette &p) { m_palette = p; }
332 DrawSelections = 0x02,
334 DrawAll = DrawText | DrawSelections | DrawCursor
336 void draw(QPainter *, const QPoint &, const QRect &, int flags = DrawAll);
338 bool processEvent(QEvent *ev);
340 QTextLayout *textLayout() const
342 #if defined(Q_WS_MAC)
343 if (m_threadChecks && QThread::currentThread() != m_textLayoutThread)
346 return &m_textLayout;
349 #if defined(Q_WS_MAC)
350 void setThreadChecks(bool threadChecks)
352 m_threadChecks = threadChecks;
355 bool threadChecks() const
357 return m_threadChecks;
362 void init(const QString &txt);
363 void removeSelectedText();
364 void internalSetText(const QString &txt, int pos = -1, bool edited = true);
365 void updateDisplayText(bool forceUpdate = false);
367 void internalInsert(const QString &s);
368 void internalDelete(bool wasBackspace = false);
369 void internalRemove(int pos);
371 inline void internalDeselect()
373 m_selDirty |= (m_selend > m_selstart);
374 m_selstart = m_selend = 0;
377 void internalUndo(int until = -1);
385 Qt::LayoutDirection m_layoutDirection;
386 uint m_hideCursor : 1; // used to hide the m_cursor inside preedit areas
387 uint m_separator : 1;
389 uint m_dragEnabled : 1;
391 uint m_textDirty : 1;
393 uint m_validInput : 1;
394 uint m_blinkStatus : 1;
395 int m_blinkPeriod; // 0 for non-blinking cursor
397 int m_deleteAllTimer;
401 QList<int> m_transactions;
402 QPoint m_tripleClick;
403 int m_tripleClickTimer;
404 QString m_cancelText;
406 void emitCursorPositionChanged();
408 bool finishChange(int validateFromState = -1, bool update = false, bool edited = true);
410 #ifndef QT_NO_VALIDATOR
411 QPointer<QValidator> m_validator;
413 QPointer<QCompleter> m_completer;
414 #ifndef QT_NO_COMPLETER
415 bool advanceToEnabledItem(int dir);
418 struct MaskInputData {
419 enum Casemode { NoCaseMode, Upper, Lower };
420 QChar maskChar; // either the separator char or the inputmask
426 MaskInputData *m_maskData;
428 // undo/redo handling
429 enum CommandType { Separator, Insert, Remove, Delete, RemoveSelection, DeleteSelection, SetSelection };
432 inline Command(CommandType t, int p, QChar c, int ss, int se) : type(t),uc(c),pos(p),selStart(ss),selEnd(se) {}
435 int pos, selStart, selEnd;
439 QVector<Command> m_history;
440 void addCommand(const Command& cmd);
442 inline void separate() { m_separator = true; }
449 void parseInputMask(const QString &maskFields);
450 bool isValidInput(QChar key, QChar mask) const;
451 bool hasAcceptableInput(const QString &text) const;
452 QString maskString(uint pos, const QString &str, bool clear = false) const;
453 QString clearString(uint pos, uint len) const;
454 QString stripString(const QString &str) const;
455 int findInMask(int pos, bool forward, bool findSeparator, QChar searchChar = QChar()) const;
457 // complex text layout (must be mutable so it can be reshaped at will)
458 mutable QTextLayout m_textLayout;
460 bool m_passwordEchoEditing;
461 QChar m_passwordCharacter;
462 #ifdef QT_GUI_PASSWORD_ECHO_DELAY
463 int m_passwordEchoTimer;
465 void cancelPasswordEchoTimer()
467 #ifdef QT_GUI_PASSWORD_ECHO_DELAY
468 if (m_passwordEchoTimer != 0) {
469 killTimer(m_passwordEchoTimer);
470 m_passwordEchoTimer = 0;
475 int redoTextLayout() const;
476 #if defined(Q_WS_MAC)
478 mutable QThread *m_textLayoutThread;
482 void cursorPositionChanged(int, int);
483 void selectionChanged();
485 void displayTextChanged(const QString &);
486 void textChanged(const QString &);
487 void textEdited(const QString &);
489 void resetInputContext();
490 void updateMicroFocus();
493 void editingFinished();
494 void updateNeeded(const QRect &);
496 #ifdef QT_KEYPAD_NAVIGATION
497 void editFocusChange(bool);
500 virtual void timerEvent(QTimerEvent *event);
503 void _q_clipboardChanged();
504 void _q_deleteSelected();
512 #endif // QT_NO_LINEEDIT
514 #endif // QLINECONTROL_P_H