| 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 | |
| 4 | #ifndef QPLAINTEXTEDIT_P_H |
| 5 | #define QPLAINTEXTEDIT_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 | #include "private/qabstractscrollarea_p.h" |
| 20 | #include "QtGui/qtextdocumentfragment.h" |
| 21 | #if QT_CONFIG(scrollbar) |
| 22 | #include "QtWidgets/qscrollbar.h" |
| 23 | #endif |
| 24 | #include "QtGui/qtextcursor.h" |
| 25 | #include "QtGui/qtextformat.h" |
| 26 | #if QT_CONFIG(menu) |
| 27 | #include "QtWidgets/qmenu.h" |
| 28 | #endif |
| 29 | #include "QtGui/qabstracttextdocumentlayout.h" |
| 30 | #include "QtCore/qbasictimer.h" |
| 31 | #include "qplaintextedit.h" |
| 32 | |
| 33 | #include "private/qwidgettextcontrol_p.h" |
| 34 | |
| 35 | #include <QtCore/qpointer.h> |
| 36 | |
| 37 | QT_REQUIRE_CONFIG(textedit); |
| 38 | |
| 39 | QT_BEGIN_NAMESPACE |
| 40 | |
| 41 | class QMimeData; |
| 42 | |
| 43 | class QPlainTextEdit; |
| 44 | class ; |
| 45 | |
| 46 | class QPlainTextEditControl : public QWidgetTextControl |
| 47 | { |
| 48 | Q_OBJECT |
| 49 | public: |
| 50 | QPlainTextEditControl(QPlainTextEdit *parent); |
| 51 | |
| 52 | |
| 53 | QMimeData *createMimeDataFromSelection() const override; |
| 54 | bool canInsertFromMimeData(const QMimeData *source) const override; |
| 55 | void insertFromMimeData(const QMimeData *source) override; |
| 56 | int hitTest(const QPointF &point, Qt::HitTestAccuracy = Qt::FuzzyHit) const override; |
| 57 | QRectF blockBoundingRect(const QTextBlock &block) const override; |
| 58 | QString anchorAt(const QPointF &pos) const override; |
| 59 | inline QRectF cursorRect(const QTextCursor &cursor) const { |
| 60 | QRectF r = QWidgetTextControl::cursorRect(cursor); |
| 61 | r.setLeft(qMax(a: r.left(), b: (qreal) 0.)); |
| 62 | return r; |
| 63 | } |
| 64 | inline QRectF cursorRect() { return cursorRect(cursor: textCursor()); } |
| 65 | void ensureCursorVisible() override { |
| 66 | textEdit->ensureCursorVisible(); |
| 67 | emit microFocusChanged(); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | QPlainTextEdit *textEdit; |
| 72 | int topBlock; |
| 73 | QTextBlock firstVisibleBlock() const; |
| 74 | |
| 75 | QVariant loadResource(int type, const QUrl &name) override { |
| 76 | return textEdit->loadResource(type, name); |
| 77 | } |
| 78 | |
| 79 | }; |
| 80 | |
| 81 | |
| 82 | class QPlainTextEditPrivate : public QAbstractScrollAreaPrivate |
| 83 | { |
| 84 | Q_DECLARE_PUBLIC(QPlainTextEdit) |
| 85 | public: |
| 86 | QPlainTextEditPrivate(); |
| 87 | |
| 88 | void init(const QString &txt = QString()); |
| 89 | void repaintContents(const QRectF &contentsRect); |
| 90 | void updatePlaceholderVisibility(); |
| 91 | |
| 92 | inline QPoint mapToContents(const QPoint &point) const |
| 93 | { return QPoint(point.x() + horizontalOffset(), point.y() + verticalOffset()); } |
| 94 | |
| 95 | void adjustScrollbars(); |
| 96 | void verticalScrollbarActionTriggered(int action); |
| 97 | void ensureViewportLayouted(); |
| 98 | void relayoutDocument(); |
| 99 | |
| 100 | void pageUpDown(QTextCursor::MoveOperation op, QTextCursor::MoveMode moveMode, bool moveCursor = true); |
| 101 | |
| 102 | inline int horizontalOffset() const |
| 103 | { return (q_func()->isRightToLeft() ? (hbar->maximum() - hbar->value()) : hbar->value()); } |
| 104 | qreal verticalOffset(int topBlock, int topLine) const; |
| 105 | qreal verticalOffset() const; |
| 106 | |
| 107 | inline void sendControlEvent(QEvent *e) |
| 108 | { control->processEvent(e, coordinateOffset: QPointF(horizontalOffset(), verticalOffset()), contextWidget: viewport); } |
| 109 | |
| 110 | void updateDefaultTextOption(); |
| 111 | |
| 112 | QBasicTimer autoScrollTimer; |
| 113 | #ifdef QT_KEYPAD_NAVIGATION |
| 114 | QBasicTimer deleteAllTimer; |
| 115 | #endif |
| 116 | QPoint autoScrollDragPos; |
| 117 | QString placeholderText; |
| 118 | |
| 119 | QPlainTextEditControl *control = nullptr; |
| 120 | qreal topLineFracture = 0; // for non-int sized fonts |
| 121 | qreal pageUpDownLastCursorY = 0; |
| 122 | QPlainTextEdit::LineWrapMode lineWrap = QPlainTextEdit::WidgetWidth; |
| 123 | QTextOption::WrapMode wordWrap = QTextOption::WrapAtWordBoundaryOrAnywhere; |
| 124 | int originalOffsetY = 0; |
| 125 | int topLine = 0; |
| 126 | |
| 127 | uint tabChangesFocus : 1; |
| 128 | uint showCursorOnInitialShow : 1; |
| 129 | uint backgroundVisible : 1; |
| 130 | uint centerOnScroll : 1; |
| 131 | uint inDrag : 1; |
| 132 | uint clickCausedFocus : 1; |
| 133 | uint pageUpDownLastCursorYIsValid : 1; |
| 134 | uint placeholderTextShown : 1; |
| 135 | |
| 136 | void setTopLine(int visualTopLine, int dx = 0); |
| 137 | void setTopBlock(int newTopBlock, int newTopLine, int dx = 0); |
| 138 | |
| 139 | void ensureVisible(int position, bool center, bool forceCenter = false); |
| 140 | void ensureCursorVisible(bool center = false); |
| 141 | void updateViewport(); |
| 142 | |
| 143 | QPointer<QPlainTextDocumentLayout> documentLayoutPtr; |
| 144 | |
| 145 | void append(const QString &text, Qt::TextFormat format = Qt::AutoText); |
| 146 | |
| 147 | void cursorPositionChanged(); |
| 148 | void modificationChanged(bool); |
| 149 | inline bool placeHolderTextToBeShown() const |
| 150 | { |
| 151 | Q_Q(const QPlainTextEdit); |
| 152 | return q->document()->isEmpty() && !q->placeholderText().isEmpty(); |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | QT_END_NAMESPACE |
| 157 | |
| 158 | #endif // QPLAINTEXTEDIT_P_H |
| 159 | |