1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt3Support 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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef Q3TEXTEDIT_H
43 #define Q3TEXTEDIT_H
44 
45 #include <Qt3Support/q3scrollview.h>
46 #include <Qt3Support/q3stylesheet.h>
47 #include <Qt3Support/q3mimefactory.h>
48 #include <QtCore/qmap.h>
49 
50 QT_BEGIN_HEADER
51 
52 QT_BEGIN_NAMESPACE
53 
54 QT_MODULE(Qt3SupportLight)
55 
56 #ifndef QT_NO_TEXTEDIT
57 // uncomment below to enable optimization mode - also uncomment the
58 // optimDoAutoScroll() private slot since moc ignores #ifdefs..
59 #define QT_TEXTEDIT_OPTIMIZATION
60 
61 class QPainter;
62 class Q3TextDocument;
63 class Q3TextCursor;
64 class QKeyEvent;
65 class QResizeEvent;
66 class QMouseEvent;
67 class QTimer;
68 class Q3TextString;
69 class QTextCommand;
70 class Q3TextParagraph;
71 class Q3TextFormat;
72 class QFont;
73 class QColor;
74 class Q3TextEdit;
75 class QTextBrowser;
76 class Q3TextString;
77 struct QUndoRedoInfoPrivate;
78 class Q3PopupMenu;
79 class Q3TextEditPrivate;
80 class Q3SyntaxHighlighter;
81 class Q3TextDrag;
82 
83 #ifdef QT_TEXTEDIT_OPTIMIZATION
84 class Q3TextEditOptimPrivate
85 {
86 public:
87     // Note: no left-tag has any value for leftTag or parent, and
88     // no right-tag has any formatting flags set.
89     enum TagType { Color = 0, Format = 1 };
90     struct Tag {
91         TagType type:2;
92         bool bold:1;
93         bool italic:1;
94         bool underline:1;
95         int line;
96         int index;
97         Tag * leftTag; // ptr to left-tag in a left-right tag pair
98         Tag * parent;  // ptr to parent left-tag in a nested tag
99         Tag * prev;
100         Tag * next;
101         QString tag;
102     };
Q3TextEditOptimPrivate()103     Q3TextEditOptimPrivate()
104     {
105         len = numLines = maxLineWidth = 0;
106         selStart.line = selStart.index = -1;
107         selEnd.line = selEnd.index = -1;
108         search.line = search.index = 0;
109         tags = lastTag = 0;
110     }
clearTags()111     void clearTags()
112     {
113         Tag * itr = tags;
114         while (tags) {
115             itr  = tags;
116             tags = tags->next;
117             delete itr;
118         }
119         tags = lastTag = 0;
120         tagIndex.clear();
121     }
~Q3TextEditOptimPrivate()122     ~Q3TextEditOptimPrivate()
123     {
124         clearTags();
125     }
126     int len;
127     int numLines;
128     int maxLineWidth;
129     struct Selection {
130         int line;
131         int index;
132     };
133     Selection selStart, selEnd, search;
134     Tag * tags, * lastTag;
135     QMap<int, QString> lines;
136     QMap<int, Tag *> tagIndex;
137 };
138 #endif
139 
140 class Q_COMPAT_EXPORT Q3TextEdit : public Q3ScrollView
141 {
142     friend class Q3TextBrowser;
143     friend class Q3SyntaxHighlighter;
144 
145     Q_OBJECT
146     Q_ENUMS(WordWrap WrapPolicy)
147     Q_FLAGS(AutoFormattingFlag)
148     Q_PROPERTY(Qt::TextFormat textFormat READ textFormat WRITE setTextFormat)
149     Q_PROPERTY(QString text READ text WRITE setText)
150     Q_PROPERTY(QBrush paper READ paper WRITE setPaper)
151     Q_PROPERTY(bool linkUnderline READ linkUnderline WRITE setLinkUnderline)
152     Q_PROPERTY(QString documentTitle READ documentTitle)
153     Q_PROPERTY(int length READ length)
154     Q_PROPERTY(WordWrap wordWrap READ wordWrap WRITE setWordWrap)
155     Q_PROPERTY(int wrapColumnOrWidth READ wrapColumnOrWidth WRITE setWrapColumnOrWidth)
156     Q_PROPERTY(WrapPolicy wrapPolicy READ wrapPolicy WRITE setWrapPolicy)
157     Q_PROPERTY(bool hasSelectedText READ hasSelectedText)
158     Q_PROPERTY(QString selectedText READ selectedText)
159     Q_PROPERTY(int undoDepth READ undoDepth WRITE setUndoDepth)
160     Q_PROPERTY(bool overwriteMode READ isOverwriteMode WRITE setOverwriteMode)
161     Q_PROPERTY(bool modified READ isModified WRITE setModified DESIGNABLE false)
162     Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
163     Q_PROPERTY(bool undoRedoEnabled READ isUndoRedoEnabled WRITE setUndoRedoEnabled)
164     Q_PROPERTY(int tabStopWidth READ tabStopWidth WRITE setTabStopWidth)
165     Q_PROPERTY(bool tabChangesFocus READ tabChangesFocus WRITE setTabChangesFocus)
166     Q_PROPERTY(AutoFormattingFlag autoFormatting READ autoFormatting WRITE setAutoFormatting)
167 
168 public:
169     enum WordWrap {
170         NoWrap,
171         WidgetWidth,
172         FixedPixelWidth,
173         FixedColumnWidth
174     };
175 
176     enum WrapPolicy {
177         AtWordBoundary,
178         AtWhiteSpace = AtWordBoundary, // AtWhiteSpace is deprecated
179         Anywhere,
180         AtWordOrDocumentBoundary
181     };
182 
183     enum AutoFormattingFlag {
184         AutoNone = 0,
185         AutoBulletList = 0x00000001,
186         AutoAll = 0xffffffff
187     };
188 
189     Q_DECLARE_FLAGS(AutoFormatting, AutoFormattingFlag)
190 
191     enum KeyboardAction {
192         ActionBackspace,
193         ActionDelete,
194         ActionReturn,
195         ActionKill,
196         ActionWordBackspace,
197         ActionWordDelete
198     };
199 
200     enum CursorAction {
201         MoveBackward,
202         MoveForward,
203         MoveWordBackward,
204         MoveWordForward,
205         MoveUp,
206         MoveDown,
207         MoveLineStart,
208         MoveLineEnd,
209         MoveHome,
210         MoveEnd,
211         MovePgUp,
212         MovePgDown
213     };
214 
215     enum VerticalAlignment {
216         AlignNormal,
217         AlignSuperScript,
218         AlignSubScript
219     };
220 
221     enum TextInsertionFlags {
222         RedoIndentation = 0x0001,
223         CheckNewLines = 0x0002,
224         RemoveSelected = 0x0004
225     };
226 
227     Q3TextEdit(const QString& text, const QString& context = QString(),
228                QWidget* parent=0, const char* name=0);
229     Q3TextEdit(QWidget* parent=0, const char* name=0);
230     virtual ~Q3TextEdit();
231 
232     QString text() const;
233     QString text(int para) const;
234     Qt::TextFormat textFormat() const;
235     QString context() const;
236     QString documentTitle() const;
237 
238     void getSelection(int *paraFrom, int *indexFrom,
239                     int *paraTo, int *indexTo, int selNum = 0) const;
240     virtual bool find(const QString &expr, bool cs, bool wo, bool forward = true,
241                        int *para = 0, int *index = 0);
242 
243     int paragraphs() const;
244     int lines() const;
245     int linesOfParagraph(int para) const;
246     int lineOfChar(int para, int chr);
247     int length() const;
248     QRect paragraphRect(int para) const;
249     int paragraphAt(const QPoint &pos) const;
250     int charAt(const QPoint &pos, int *para) const;
251     int paragraphLength(int para) const;
252 
253     Q3StyleSheet* styleSheet() const;
254 #ifndef QT_NO_MIME
255     Q3MimeSourceFactory* mimeSourceFactory() const;
256 #endif
257     QBrush paper() const;
258     bool linkUnderline() const;
259 
260     int heightForWidth(int w) const;
261 
262     bool hasSelectedText() const;
263     QString selectedText() const;
264     bool isUndoAvailable() const;
265     bool isRedoAvailable() const;
266 
267     WordWrap wordWrap() const;
268     int wrapColumnOrWidth() const;
269     WrapPolicy wrapPolicy() const;
270 
271     int tabStopWidth() const;
272 
273     QString anchorAt(const QPoint& pos, Qt::AnchorAttribute a = Qt::AnchorHref);
274 
275     QSize sizeHint() const;
276 
isReadOnly()277     bool isReadOnly() const { return readonly; }
278 
279     void getCursorPosition(int *parag, int *index) const;
280 
281     bool isModified() const;
282     bool italic() const;
283     bool bold() const;
284     bool underline() const;
285     QString family() const;
286     int pointSize() const;
287     QColor color() const;
288     QFont font() const;
289     QFont currentFont() const;
290     int alignment() const;
291     VerticalAlignment verticalAlignment() const;
292     int undoDepth() const;
293 
294     // do not use, will go away
295     virtual bool getFormat(int para, int index, QFont *font, QColor *color, VerticalAlignment *verticalAlignment);
296     // do not use, will go away
297     virtual bool getParagraphFormat(int para, QFont *font, QColor *color,
298                                      VerticalAlignment *verticalAlignment, int *alignment,
299                                      Q3StyleSheetItem::DisplayMode *displayMode,
300                                      Q3StyleSheetItem::ListStyle *listStyle,
301                                      int *listDepth);
302 
303 
isOverwriteMode()304     bool isOverwriteMode() const { return overWrite; }
305     QColor paragraphBackgroundColor(int para) const;
306 
307     bool isUndoRedoEnabled() const;
308     bool eventFilter(QObject *o, QEvent *e);
309     bool tabChangesFocus() const;
310 
311     void setAutoFormatting(AutoFormatting);
312     AutoFormatting autoFormatting() const;
313     Q3SyntaxHighlighter *syntaxHighlighter() const;
314 
315 public Q_SLOTS:
316 #ifndef QT_NO_MIME
317     virtual void setMimeSourceFactory(Q3MimeSourceFactory* factory);
318 #endif
319     virtual void setStyleSheet(Q3StyleSheet* styleSheet);
320     virtual void scrollToAnchor(const QString& name);
321     virtual void setPaper(const QBrush& pap);
322     virtual void setLinkUnderline(bool);
323 
324     virtual void setWordWrap(Q3TextEdit::WordWrap mode);
325     virtual void setWrapColumnOrWidth(int);
326     virtual void setWrapPolicy(Q3TextEdit::WrapPolicy policy);
327 
328     virtual void copy();
329     virtual void append(const QString& text);
330 
setText(const QString & txt)331     void setText(const QString &txt) { setText(txt, QString()); }
332     virtual void setText(const QString &txt, const QString &context);
333     virtual void setTextFormat(Qt::TextFormat f);
334 
335     virtual void selectAll(bool select = true);
336     virtual void setTabStopWidth(int ts);
337     virtual void zoomIn(int range);
zoomIn()338     virtual void zoomIn() { zoomIn(1); }
339     virtual void zoomOut(int range);
zoomOut()340     virtual void zoomOut() { zoomOut(1); }
341     virtual void zoomTo(int size);
342 
343     virtual void sync();
344     virtual void setReadOnly(bool b);
345 
346     virtual void undo();
347     virtual void redo();
348     virtual void cut();
349     virtual void paste();
350 #ifndef QT_NO_CLIPBOARD
351     virtual void pasteSubType(const QByteArray &subtype);
352 #endif
353     virtual void clear();
354     virtual void del();
355     virtual void indent();
356     virtual void setItalic(bool b);
357     virtual void setBold(bool b);
358     virtual void setUnderline(bool b);
359     virtual void setFamily(const QString &f);
360     virtual void setPointSize(int s);
361     virtual void setColor(const QColor &c);
362     virtual void setVerticalAlignment(Q3TextEdit::VerticalAlignment a);
363     virtual void setAlignment(int a);
364 
365     // do not use, will go away
366     virtual void setParagType(Q3StyleSheetItem::DisplayMode dm, Q3StyleSheetItem::ListStyle listStyle);
367 
368     virtual void setCursorPosition(int parag, int index);
369     virtual void setSelection(int parag_from, int index_from, int parag_to, int index_to, int selNum = 0);
370     virtual void setSelectionAttributes(int selNum, const QColor &back, bool invertText);
371     virtual void setModified(bool m);
372     virtual void resetFormat();
373     virtual void setUndoDepth(int d);
374     virtual void setFormat(Q3TextFormat *f, int flags);
375     virtual void ensureCursorVisible();
376     virtual void placeCursor(const QPoint &pos, Q3TextCursor *c = 0);
377     virtual void moveCursor(Q3TextEdit::CursorAction action, bool select);
378     virtual void doKeyboardAction(Q3TextEdit::KeyboardAction action);
379     virtual void removeSelectedText(int selNum = 0);
380     virtual void removeSelection(int selNum = 0);
381     virtual void setCurrentFont(const QFont &f);
setOverwriteMode(bool b)382     virtual void setOverwriteMode(bool b) { overWrite = b; }
383 
384     virtual void scrollToBottom();
385 
386     virtual void insert(const QString &text, uint insertionFlags = CheckNewLines | RemoveSelected);
387 
388     // obsolete
389     virtual void insert(const QString &text, bool, bool = true, bool = true);
390 
391     virtual void insertAt(const QString &text, int para, int index);
392     virtual void removeParagraph(int para);
393     virtual void insertParagraph(const QString &text, int para);
394 
395     virtual void setParagraphBackgroundColor(int para, const QColor &bg);
396     virtual void clearParagraphBackground(int para);
397 
398     virtual void setUndoRedoEnabled(bool b);
399     virtual void setTabChangesFocus(bool b);
400 
401 #ifdef QT_TEXTEDIT_OPTIMIZATION
402     void polishEvent(QEvent*);
403     void setMaxLogLines(int numLines);
404     int maxLogLines() const;
405 #endif
406 
407 Q_SIGNALS:
408     void textChanged();
409     void selectionChanged();
410     void copyAvailable(bool);
411     void undoAvailable(bool yes);
412     void redoAvailable(bool yes);
413     void currentFontChanged(const QFont &f);
414     void currentColorChanged(const QColor &c);
415     void currentAlignmentChanged(int a);
416     void currentVerticalAlignmentChanged(Q3TextEdit::VerticalAlignment a);
417     void cursorPositionChanged(Q3TextCursor *c);
418     void cursorPositionChanged(int para, int pos);
419     void returnPressed();
420     void modificationChanged(bool m);
421     void clicked(int parag, int index);
422     void doubleClicked(int parag, int index);
423 
424 protected:
425     void repaintChanged();
426     void updateStyles();
427     void drawContents(QPainter *p, int cx, int cy, int cw, int ch);
428     bool event(QEvent *e);
429     void changeEvent(QEvent *);
430     void keyPressEvent(QKeyEvent *e);
431     void resizeEvent(QResizeEvent *e);
432     void viewportResizeEvent(QResizeEvent*);
433     void contentsMousePressEvent(QMouseEvent *e);
434     void contentsMouseMoveEvent(QMouseEvent *e);
435     void contentsMouseReleaseEvent(QMouseEvent *e);
436     void contentsMouseDoubleClickEvent(QMouseEvent *e);
437 #ifndef QT_NO_WHEELEVENT
438     void contentsWheelEvent(QWheelEvent *e);
439 #endif
440     void inputMethodEvent(QInputMethodEvent *);
441 #ifndef QT_NO_DRAGANDDROP
442     void contentsDragEnterEvent(QDragEnterEvent *e);
443     void contentsDragMoveEvent(QDragMoveEvent *e);
444     void contentsDragLeaveEvent(QDragLeaveEvent *e);
445     void contentsDropEvent(QDropEvent *e);
446 #endif
447     void contentsContextMenuEvent(QContextMenuEvent *e);
448     bool focusNextPrevChild(bool next);
449     Q3TextDocument *document() const;
450     Q3TextCursor *textCursor() const;
451     void setDocument(Q3TextDocument *doc);
452     virtual Q3PopupMenu *createPopupMenu(const QPoint& pos);
453     virtual Q3PopupMenu *createPopupMenu();
454     void drawCursor(bool visible);
455 
456 protected Q_SLOTS:
457     virtual void doChangeInterval();
458     virtual void sliderReleased();
459 
460 private Q_SLOTS:
461     void formatMore();
462     void doResize();
463     void autoScrollTimerDone();
464     void blinkCursor();
465     void setModified();
466     void startDrag();
467     void documentWidthChanged(int w);
468     void clipboardChanged();
469 
470 private:
471     struct Q_COMPAT_EXPORT UndoRedoInfo {
472         enum Type { Invalid, Insert, Delete, Backspace, Return, RemoveSelected, Format, Style, IME };
473 
474         UndoRedoInfo(Q3TextDocument *dc);
475         ~UndoRedoInfo();
476         void clear();
477         bool valid() const;
478 
479         QUndoRedoInfoPrivate *d;
480         int id;
481         int index;
482         int eid;
483         int eindex;
484         Q3TextFormat *format;
485         int flags;
486         Type type;
487         Q3TextDocument *doc;
488         QByteArray styleInformation;
489     };
490 
491 private:
492     void updateCursor(const QPoint & pos);
493     void handleMouseMove(const QPoint& pos);
494     void drawContents(QPainter *);
linksEnabled()495     virtual bool linksEnabled() const { return false; }
496     void init();
497     void checkUndoRedoInfo(UndoRedoInfo::Type t);
498     void updateCurrentFormat();
499     bool handleReadOnlyKeyEvent(QKeyEvent *e);
500     void makeParagVisible(Q3TextParagraph *p);
501     void normalCopy();
502     void copyToClipboard();
503 #ifndef QT_NO_MIME
504     QByteArray pickSpecial(QMimeSource* ms, bool always_ask, const QPoint&);
505     Q3TextDrag *dragObject(QWidget *parent = 0) const;
506 #endif
507 #ifndef QT_NO_MIMECLIPBOARD
508     void pasteSpecial(const QPoint&);
509 #endif
510     void setFontInternal(const QFont &f);
511 
emitHighlighted(const QString &)512     virtual void emitHighlighted(const QString &) {}
emitLinkClicked(const QString &)513     virtual void emitLinkClicked(const QString &) {}
514 
515     void readFormats(Q3TextCursor &c1, Q3TextCursor &c2, Q3TextString &text, bool fillStyles = false);
516     void clearUndoRedo();
517     void paintDocument(bool drawAll, QPainter *p, int cx = -1, int cy = -1, int cw = -1, int ch = -1);
518     void moveCursor(CursorAction action);
519     void ensureFormatted(Q3TextParagraph *p);
520     void placeCursor(const QPoint &pos, Q3TextCursor *c, bool link);
521     QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
522 
523 #ifdef QT_TEXTEDIT_OPTIMIZATION
524     bool checkOptimMode();
525     QString optimText() const;
526     void optimSetText(const QString &str);
527     void optimAppend(const QString &str);
528     void optimInsert(const QString &str, int line, int index);
529     void optimDrawContents(QPainter * p, int cx, int cy, int cw, int ch);
530     void optimMousePressEvent(QMouseEvent * e);
531     void optimMouseReleaseEvent(QMouseEvent * e);
532     void optimMouseMoveEvent(QMouseEvent * e);
533     int  optimCharIndex(const QString &str, int mx) const;
534     void optimSelectAll();
535     void optimRemoveSelection();
536     void optimSetSelection(int startLine, int startIdx, int endLine,
537                             int endIdx);
538     bool optimHasSelection() const;
539     QString optimSelectedText() const;
540     bool optimFind(const QString & str, bool, bool, bool, int *, int *);
541     void optimParseTags(QString * str, int lineNo = -1, int indexOffset = 0);
542     Q3TextEditOptimPrivate::Tag * optimPreviousLeftTag(int line);
543     void optimSetTextFormat(Q3TextDocument *, Q3TextCursor *, Q3TextFormat * f,
544                              int, int, Q3TextEditOptimPrivate::Tag * t);
545     Q3TextEditOptimPrivate::Tag * optimAppendTag(int index, const QString & tag);
546     Q3TextEditOptimPrivate::Tag * optimInsertTag(int line, int index, const QString & tag);
547     void optimCheckLimit(const QString& str);
548     bool optimHasBoldMetrics(int line);
549 
550 private Q_SLOTS:
551     void optimDoAutoScroll();
552 #endif // QT_TEXTEDIT_OPTIMIZATION
553 
554 private:
555 #ifndef QT_NO_CLIPBOARD
556     void pasteSubType(const QByteArray &subtype, QMimeSource *m);
557 #endif
558 
559 private:
560     Q_DISABLE_COPY(Q3TextEdit)
561 
562     Q3TextDocument *doc;
563     Q3TextCursor *cursor;
564     QTimer *formatTimer, *scrollTimer, *changeIntervalTimer, *blinkTimer, *dragStartTimer;
565     Q3TextParagraph *lastFormatted;
566     int interval;
567     UndoRedoInfo undoRedoInfo;
568     Q3TextFormat *currentFormat;
569     int currentAlignment;
570     QPoint oldMousePos, mousePos;
571     QPoint dragStartPos;
572     QString onLink;
573     WordWrap wrapMode;
574     WrapPolicy wPolicy;
575     int wrapWidth;
576     QString pressedLink;
577     Q3TextEditPrivate *d;
578     bool inDoubleClick : 1;
579     bool mousePressed : 1;
580     bool cursorVisible : 1;
581     bool blinkCursorVisible : 1;
582     bool modified : 1;
583     bool mightStartDrag : 1;
584     bool inDnD : 1;
585     bool readonly : 1;
586     bool undoEnabled : 1;
587     bool overWrite : 1;
588 };
589 
Q_DECLARE_OPERATORS_FOR_FLAGS(Q3TextEdit::AutoFormatting)590 Q_DECLARE_OPERATORS_FOR_FLAGS(Q3TextEdit::AutoFormatting)
591 
592 inline Q3TextDocument *Q3TextEdit::document() const
593 {
594     return doc;
595 }
596 
textCursor()597 inline Q3TextCursor *Q3TextEdit::textCursor() const
598 {
599     return cursor;
600 }
601 
setCurrentFont(const QFont & f)602 inline void Q3TextEdit::setCurrentFont(const QFont &f)
603 {
604     Q3TextEdit::setFontInternal(f);
605 }
606 
607 #endif // QT_NO_TEXTEDIT
608 
609 QT_END_NAMESPACE
610 
611 QT_END_HEADER
612 
613 #endif // Q3TEXTEDIT_H
614