1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtWidgets module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QPLAINTEXTEDIT_P_H
41 #define QPLAINTEXTEDIT_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists purely as an
48 // implementation detail.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtWidgets/private/qtwidgetsglobal_p.h>
55 #include "private/qabstractscrollarea_p.h"
56 #include "QtGui/qtextdocumentfragment.h"
57 #if QT_CONFIG(scrollbar)
58 #include "QtWidgets/qscrollbar.h"
59 #endif
60 #include "QtGui/qtextcursor.h"
61 #include "QtGui/qtextformat.h"
62 #if QT_CONFIG(menu)
63 #include "QtWidgets/qmenu.h"
64 #endif
65 #include "QtGui/qabstracttextdocumentlayout.h"
66 #include "QtCore/qbasictimer.h"
67 #include "qplaintextedit.h"
68 
69 #include "private/qwidgettextcontrol_p.h"
70 
71 QT_REQUIRE_CONFIG(textedit);
72 
73 QT_BEGIN_NAMESPACE
74 
75 class QMimeData;
76 
77 class QPlainTextEdit;
78 class ExtraArea;
79 
80 class QPlainTextEditControl : public QWidgetTextControl
81 {
82     Q_OBJECT
83 public:
84     QPlainTextEditControl(QPlainTextEdit *parent);
85 
86 
87     QMimeData *createMimeDataFromSelection() const override;
88     bool canInsertFromMimeData(const QMimeData *source) const override;
89     void insertFromMimeData(const QMimeData *source) override;
90     int hitTest(const QPointF &point, Qt::HitTestAccuracy = Qt::FuzzyHit) const override;
91     QRectF blockBoundingRect(const QTextBlock &block) const override;
92     QString anchorAt(const QPointF &pos) const override;
cursorRect(const QTextCursor & cursor)93     inline QRectF cursorRect(const QTextCursor &cursor) const {
94         QRectF r = QWidgetTextControl::cursorRect(cursor);
95         r.setLeft(qMax(r.left(), (qreal) 0.));
96         return r;
97     }
cursorRect()98     inline QRectF cursorRect() { return cursorRect(textCursor()); }
ensureCursorVisible()99     void ensureCursorVisible() override {
100         textEdit->ensureCursorVisible();
101         emit microFocusChanged();
102     }
103 
104 
105     QPlainTextEdit *textEdit;
106     int topBlock;
107     QTextBlock firstVisibleBlock() const;
108 
loadResource(int type,const QUrl & name)109     QVariant loadResource(int type, const QUrl &name) override {
110         return textEdit->loadResource(type, name);
111     }
112 
113 };
114 
115 
116 class QPlainTextEditPrivate : public QAbstractScrollAreaPrivate
117 {
118     Q_DECLARE_PUBLIC(QPlainTextEdit)
119 public:
120     QPlainTextEditPrivate();
121 
122     void init(const QString &txt = QString());
123     void _q_repaintContents(const QRectF &contentsRect);
124     void _q_textChanged();
125 
mapToContents(const QPoint & point)126     inline QPoint mapToContents(const QPoint &point) const
127         { return QPoint(point.x() + horizontalOffset(), point.y() + verticalOffset()); }
128 
129     void _q_adjustScrollbars();
130     void _q_verticalScrollbarActionTriggered(int action);
131     void ensureViewportLayouted();
132     void relayoutDocument();
133 
134     void pageUpDown(QTextCursor::MoveOperation op, QTextCursor::MoveMode moveMode, bool moveCursor = true);
135 
horizontalOffset()136     inline int horizontalOffset() const
137         { return (q_func()->isRightToLeft() ? (hbar->maximum() - hbar->value()) : hbar->value()); }
138     qreal verticalOffset(int topBlock, int topLine) const;
139     qreal verticalOffset() const;
140 
sendControlEvent(QEvent * e)141     inline void sendControlEvent(QEvent *e)
142         { control->processEvent(e, QPointF(horizontalOffset(), verticalOffset()), viewport); }
143 
144     void updateDefaultTextOption();
145 
146     QPlainTextEditControl *control;
147 
148     bool tabChangesFocus;
149 
150     QBasicTimer autoScrollTimer;
151     QPoint autoScrollDragPos;
152 
153     QPlainTextEdit::LineWrapMode lineWrap;
154     QTextOption::WrapMode wordWrap;
155 
156     uint showCursorOnInitialShow : 1;
157     uint backgroundVisible : 1;
158     uint centerOnScroll : 1;
159     uint inDrag : 1;
160     uint clickCausedFocus : 1;
161     uint placeholderVisible : 1;
162 
163     int topLine;
164     qreal topLineFracture; // for non-int sized fonts
165 
166     void setTopLine(int visualTopLine, int dx = 0);
167     void setTopBlock(int newTopBlock, int newTopLine, int dx = 0);
168 
169     void ensureVisible(int position, bool center, bool forceCenter = false);
170     void ensureCursorVisible(bool center = false);
171     void updateViewport();
172 
173     QPointer<QPlainTextDocumentLayout> documentLayoutPtr;
174 
175     void append(const QString &text, Qt::TextFormat format = Qt::AutoText);
176 
177     qreal pageUpDownLastCursorY;
178     bool pageUpDownLastCursorYIsValid;
179 
180 
181 #ifdef QT_KEYPAD_NAVIGATION
182     QBasicTimer deleteAllTimer;
183 #endif
184 
185     void _q_cursorPositionChanged();
186     void _q_modificationChanged(bool);
187 
188     int originalOffsetY;
189     QString placeholderText;
190 };
191 
192 QT_END_NAMESPACE
193 
194 #endif // QPLAINTEXTEDIT_P_H
195