summaryrefslogtreecommitdiffstats
path: root/src/qt3support/text/q3textedit.h
blob: 924b3bd04ab63e3983fd108f11afe211d0b7d242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt3Support module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef Q3TEXTEDIT_H
#define Q3TEXTEDIT_H

#include <Qt3Support/q3scrollview.h>
#include <Qt3Support/q3stylesheet.h>
#include <Qt3Support/q3mimefactory.h>
#include <QtCore/qmap.h>

QT_BEGIN_HEADER

QT_BEGIN_NAMESPACE

QT_MODULE(Qt3SupportLight)

#ifndef QT_NO_TEXTEDIT
// uncomment below to enable optimization mode - also uncomment the
// optimDoAutoScroll() private slot since moc ignores #ifdefs..
#define QT_TEXTEDIT_OPTIMIZATION

class QPainter;
class Q3TextDocument;
class Q3TextCursor;
class QKeyEvent;
class QResizeEvent;
class QMouseEvent;
class QTimer;
class Q3TextString;
class QTextCommand;
class Q3TextParagraph;
class Q3TextFormat;
class QFont;
class QColor;
class Q3TextEdit;
class QTextBrowser;
class Q3TextString;
struct QUndoRedoInfoPrivate;
class Q3PopupMenu;
class Q3TextEditPrivate;
class Q3SyntaxHighlighter;
class Q3TextDrag;

#ifdef QT_TEXTEDIT_OPTIMIZATION
class Q3TextEditOptimPrivate
{
public:
    // Note: no left-tag has any value for leftTag or parent, and
    // no right-tag has any formatting flags set.
    enum TagType { Color = 0, Format = 1 };
    struct Tag {
        TagType type:2;
        bool bold:1;
        bool italic:1;
        bool underline:1;
        int line;
        int index;
        Tag * leftTag; // ptr to left-tag in a left-right tag pair
        Tag * parent;  // ptr to parent left-tag in a nested tag
        Tag * prev;
        Tag * next;
        QString tag;
    };
    Q3TextEditOptimPrivate()
    {
        len = numLines = maxLineWidth = 0;
        selStart.line = selStart.index = -1;
        selEnd.line = selEnd.index = -1;
        search.line = search.index = 0;
        tags = lastTag = 0;
    }
    void clearTags()
    {
        Tag * itr = tags;
        while (tags) {
            itr  = tags;
            tags = tags->next;
            delete itr;
        }
        tags = lastTag = 0;
        tagIndex.clear();
    }
    ~Q3TextEditOptimPrivate()
    {
        clearTags();
    }
    int len;
    int numLines;
    int maxLineWidth;
    struct Selection {
        int line;
        int index;
    };
    Selection selStart, selEnd, search;
    Tag * tags, * lastTag;
    QMap<int, QString> lines;
    QMap<int, Tag *> tagIndex;
};
#endif

class Q_COMPAT_EXPORT Q3TextEdit : public Q3ScrollView
{
    friend class Q3TextBrowser;
    friend class Q3SyntaxHighlighter;

    Q_OBJECT
    Q_ENUMS(WordWrap WrapPolicy)
    Q_FLAGS(AutoFormattingFlag)
    Q_PROPERTY(Qt::TextFormat textFormat READ textFormat WRITE setTextFormat)
    Q_PROPERTY(QString text READ text WRITE setText)
    Q_PROPERTY(QBrush paper READ paper WRITE setPaper)
    Q_PROPERTY(bool linkUnderline READ linkUnderline WRITE setLinkUnderline)
    Q_PROPERTY(QString documentTitle READ documentTitle)
    Q_PROPERTY(int length READ length)
    Q_PROPERTY(WordWrap wordWrap READ wordWrap WRITE setWordWrap)
    Q_PROPERTY(int wrapColumnOrWidth READ wrapColumnOrWidth WRITE setWrapColumnOrWidth)
    Q_PROPERTY(WrapPolicy wrapPolicy READ wrapPolicy WRITE setWrapPolicy)
    Q_PROPERTY(bool hasSelectedText READ hasSelectedText)
    Q_PROPERTY(QString selectedText READ selectedText)
    Q_PROPERTY(int undoDepth READ undoDepth WRITE setUndoDepth)
    Q_PROPERTY(bool overwriteMode READ isOverwriteMode WRITE setOverwriteMode)
    Q_PROPERTY(bool modified READ isModified WRITE setModified DESIGNABLE false)
    Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
    Q_PROPERTY(bool undoRedoEnabled READ isUndoRedoEnabled WRITE setUndoRedoEnabled)
    Q_PROPERTY(int tabStopWidth READ tabStopWidth WRITE setTabStopWidth)
    Q_PROPERTY(bool tabChangesFocus READ tabChangesFocus WRITE setTabChangesFocus)
    Q_PROPERTY(AutoFormattingFlag autoFormatting READ autoFormatting WRITE setAutoFormatting)

public:
    enum WordWrap {
        NoWrap,
        WidgetWidth,
        FixedPixelWidth,
        FixedColumnWidth
    };

    enum WrapPolicy {
        AtWordBoundary,
        AtWhiteSpace = AtWordBoundary, // AtWhiteSpace is deprecated
        Anywhere,
        AtWordOrDocumentBoundary
    };

    enum AutoFormattingFlag {
        AutoNone = 0,
        AutoBulletList = 0x00000001,
        AutoAll = 0xffffffff
    };

    Q_DECLARE_FLAGS(AutoFormatting, AutoFormattingFlag)

    enum KeyboardAction {
        ActionBackspace,
        ActionDelete,
        ActionReturn,
        ActionKill,
        ActionWordBackspace,
        ActionWordDelete
    };

    enum CursorAction {
        MoveBackward,
        MoveForward,
        MoveWordBackward,
        MoveWordForward,
        MoveUp,
        MoveDown,
        MoveLineStart,
        MoveLineEnd,
        MoveHome,
        MoveEnd,
        MovePgUp,
        MovePgDown
    };

    enum VerticalAlignment {
        AlignNormal,
        AlignSuperScript,
        AlignSubScript
    };

    enum TextInsertionFlags {
        RedoIndentation = 0x0001,
        CheckNewLines = 0x0002,
        RemoveSelected = 0x0004
    };

    Q3TextEdit(const QString& text, const QString& context = QString(),
               QWidget* parent=0, const char* name=0);
    Q3TextEdit(QWidget* parent=0, const char* name=0);
    virtual ~Q3TextEdit();

    QString text() const;
    QString text(int para) const;
    Qt::TextFormat textFormat() const;
    QString context() const;
    QString documentTitle() const;

    void getSelection(int *paraFrom, int *indexFrom,
                    int *paraTo, int *indexTo, int selNum = 0) const;
    virtual bool find(const QString &expr, bool cs, bool wo, bool forward = true,
                       int *para = 0, int *index = 0);

    int paragraphs() const;
    int lines() const;
    int linesOfParagraph(int para) const;
    int lineOfChar(int para, int chr);
    int length() const;
    QRect paragraphRect(int para) const;
    int paragraphAt(const QPoint &pos) const;
    int charAt(const QPoint &pos, int *para) const;
    int paragraphLength(int para) const;

    Q3StyleSheet* styleSheet() const;
#ifndef QT_NO_MIME
    Q3MimeSourceFactory* mimeSourceFactory() const;
#endif
    QBrush paper() const;
    bool linkUnderline() const;

    int heightForWidth(int w) const;

    bool hasSelectedText() const;
    QString selectedText() const;
    bool isUndoAvailable() const;
    bool isRedoAvailable() const;

    WordWrap wordWrap() const;
    int wrapColumnOrWidth() const;
    WrapPolicy wrapPolicy() const;

    int tabStopWidth() const;

    QString anchorAt(const QPoint& pos, Qt::AnchorAttribute a = Qt::AnchorHref);

    QSize sizeHint() const;

    bool isReadOnly() const { return readonly; }

    void getCursorPosition(int *parag, int *index) const;

    bool isModified() const;
    bool italic() const;
    bool bold() const;
    bool underline() const;
    QString family() const;
    int pointSize() const;
    QColor color() const;
    QFont font() const;
    QFont currentFont() const;
    int alignment() const;
    VerticalAlignment verticalAlignment() const;
    int undoDepth() const;

    // do not use, will go away
    virtual bool getFormat(int para, int index, QFont *font, QColor *color, VerticalAlignment *verticalAlignment);
    // do not use, will go away
    virtual bool getParagraphFormat(int para, QFont *font, QColor *color,
                                     VerticalAlignment *verticalAlignment, int *alignment,
                                     Q3StyleSheetItem::DisplayMode *displayMode,
                                     Q3StyleSheetItem::ListStyle *listStyle,
                                     int *listDepth);


    bool isOverwriteMode() const { return overWrite; }
    QColor paragraphBackgroundColor(int para) const;

    bool isUndoRedoEnabled() const;
    bool eventFilter(QObject *o, QEvent *e);
    bool tabChangesFocus() const;

    void setAutoFormatting(AutoFormatting);
    AutoFormatting autoFormatting() const;
    Q3SyntaxHighlighter *syntaxHighlighter() const;

public Q_SLOTS:
#ifndef QT_NO_MIME
    virtual void setMimeSourceFactory(Q3MimeSourceFactory* factory);
#endif
    virtual void setStyleSheet(Q3StyleSheet* styleSheet);
    virtual void scrollToAnchor(const QString& name);
    virtual void setPaper(const QBrush& pap);
    virtual void setLinkUnderline(bool);

    virtual void setWordWrap(Q3TextEdit::WordWrap mode);
    virtual void setWrapColumnOrWidth(int);
    virtual void setWrapPolicy(Q3TextEdit::WrapPolicy policy);

    virtual void copy();
    virtual void append(const QString& text);

    void setText(const QString &txt) { setText(txt, QString()); }
    virtual void setText(const QString &txt, const QString &context);
    virtual void setTextFormat(Qt::TextFormat f);

    virtual void selectAll(bool select = true);
    virtual void setTabStopWidth(int ts);
    virtual void zoomIn(int range);
    virtual void zoomIn() { zoomIn(1); }
    virtual void zoomOut(int range);
    virtual void zoomOut() { zoomOut(1); }
    virtual void zoomTo(int size);

    virtual void sync();
    virtual void setReadOnly(bool b);

    virtual void undo();
    virtual void redo();
    virtual void cut();
    virtual void paste();
#ifndef QT_NO_CLIPBOARD
    virtual void pasteSubType(const QByteArray &subtype);
#endif
    virtual void clear();
    virtual void del();
    virtual void indent();
    virtual void setItalic(bool b);
    virtual void setBold(bool b);
    virtual void setUnderline(bool b);
    virtual void setFamily(const QString &f);
    virtual void setPointSize(int s);
    virtual void setColor(const QColor &c);
    virtual void setVerticalAlignment(Q3TextEdit::VerticalAlignment a);
    virtual void setAlignment(int a);

    // do not use, will go away
    virtual void setParagType(Q3StyleSheetItem::DisplayMode dm, Q3StyleSheetItem::ListStyle listStyle);

    virtual void setCursorPosition(int parag, int index);
    virtual void setSelection(int parag_from, int index_from, int parag_to, int index_to, int selNum = 0);
    virtual void setSelectionAttributes(int selNum, const QColor &back, bool invertText);
    virtual void setModified(bool m);
    virtual void resetFormat();
    virtual void setUndoDepth(int d);
    virtual void setFormat(Q3TextFormat *f, int flags);
    virtual void ensureCursorVisible();
    virtual void placeCursor(const QPoint &pos, Q3TextCursor *c = 0);
    virtual void moveCursor(Q3TextEdit::CursorAction action, bool select);
    virtual void doKeyboardAction(Q3TextEdit::KeyboardAction action);
    virtual void removeSelectedText(int selNum = 0);
    virtual void removeSelection(int selNum = 0);
    virtual void setCurrentFont(const QFont &f);
    virtual void setOverwriteMode(bool b) { overWrite = b; }

    virtual void scrollToBottom();

    virtual void insert(const QString &text, uint insertionFlags = CheckNewLines | RemoveSelected);

    // obsolete
    virtual void insert(const QString &text, bool, bool = true, bool = true);

    virtual void insertAt(const QString &text, int para, int index);
    virtual void removeParagraph(int para);
    virtual void insertParagraph(const QString &text, int para);

    virtual void setParagraphBackgroundColor(int para, const QColor &bg);
    virtual void clearParagraphBackground(int para);

    virtual void setUndoRedoEnabled(bool b);
    virtual void setTabChangesFocus(bool b);

#ifdef QT_TEXTEDIT_OPTIMIZATION
    void polishEvent(QEvent*);
    void setMaxLogLines(int numLines);
    int maxLogLines() const;
#endif

Q_SIGNALS:
    void textChanged();
    void selectionChanged();
    void copyAvailable(bool);
    void undoAvailable(bool yes);
    void redoAvailable(bool yes);
    void currentFontChanged(const QFont &f);
    void currentColorChanged(const QColor &c);
    void currentAlignmentChanged(int a);
    void currentVerticalAlignmentChanged(Q3TextEdit::VerticalAlignment a);
    void cursorPositionChanged(Q3TextCursor *c);
    void cursorPositionChanged(int para, int pos);
    void returnPressed();
    void modificationChanged(bool m);
    void clicked(int parag, int index);
    void doubleClicked(int parag, int index);

protected:
    void repaintChanged();
    void updateStyles();
    void drawContents(QPainter *p, int cx, int cy, int cw, int ch);
    bool event(QEvent *e);
    void changeEvent(QEvent *);
    void keyPressEvent(QKeyEvent *e);
    void resizeEvent(QResizeEvent *e);
    void viewportResizeEvent(QResizeEvent*);
    void contentsMousePressEvent(QMouseEvent *e);
    void contentsMouseMoveEvent(QMouseEvent *e);
    void contentsMouseReleaseEvent(QMouseEvent *e);
    void contentsMouseDoubleClickEvent(QMouseEvent *e);
#ifndef QT_NO_WHEELEVENT
    void contentsWheelEvent(QWheelEvent *e);
#endif
    void inputMethodEvent(QInputMethodEvent *);
#ifndef QT_NO_DRAGANDDROP
    void contentsDragEnterEvent(QDragEnterEvent *e);
    void contentsDragMoveEvent(QDragMoveEvent *e);
    void contentsDragLeaveEvent(QDragLeaveEvent *e);
    void contentsDropEvent(QDropEvent *e);
#endif
    void contentsContextMenuEvent(QContextMenuEvent *e);
    bool focusNextPrevChild(bool next);
    Q3TextDocument *document() const;
    Q3TextCursor *textCursor() const;
    void setDocument(Q3TextDocument *doc);
    virtual Q3PopupMenu *createPopupMenu(const QPoint& pos);
    virtual Q3PopupMenu *createPopupMenu();
    void drawCursor(bool visible);

protected Q_SLOTS:
    virtual void doChangeInterval();
    virtual void sliderReleased();

private Q_SLOTS:
    void formatMore();
    void doResize();
    void autoScrollTimerDone();
    void blinkCursor();
    void setModified();
    void startDrag();
    void documentWidthChanged(int w);
    void clipboardChanged();

private:
    struct Q_COMPAT_EXPORT UndoRedoInfo {
        enum Type { Invalid, Insert, Delete, Backspace, Return, RemoveSelected, Format, Style, IME };

        UndoRedoInfo(Q3TextDocument *dc);
        ~UndoRedoInfo();
        void clear();
        bool valid() const;

        QUndoRedoInfoPrivate *d;
        int id;
        int index;
        int eid;
        int eindex;
        Q3TextFormat *format;
        int flags;
        Type type;
        Q3TextDocument *doc;
        QByteArray styleInformation;
    };

private:
    void updateCursor(const QPoint & pos);
    void handleMouseMove(const QPoint& pos);
    void drawContents(QPainter *);
    virtual bool linksEnabled() const { return false; }
    void init();
    void checkUndoRedoInfo(UndoRedoInfo::Type t);
    void updateCurrentFormat();
    bool handleReadOnlyKeyEvent(QKeyEvent *e);
    void makeParagVisible(Q3TextParagraph *p);
    void normalCopy();
    void copyToClipboard();
#ifndef QT_NO_MIME
    QByteArray pickSpecial(QMimeSource* ms, bool always_ask, const QPoint&);
    Q3TextDrag *dragObject(QWidget *parent = 0) const;
#endif
#ifndef QT_NO_MIMECLIPBOARD
    void pasteSpecial(const QPoint&);
#endif
    void setFontInternal(const QFont &f);

    virtual void emitHighlighted(const QString &) {}
    virtual void emitLinkClicked(const QString &) {}

    void readFormats(Q3TextCursor &c1, Q3TextCursor &c2, Q3TextString &text, bool fillStyles = false);
    void clearUndoRedo();
    void paintDocument(bool drawAll, QPainter *p, int cx = -1, int cy = -1, int cw = -1, int ch = -1);
    void moveCursor(CursorAction action);
    void ensureFormatted(Q3TextParagraph *p);
    void placeCursor(const QPoint &pos, Q3TextCursor *c, bool link);
    QVariant inputMethodQuery(Qt::InputMethodQuery query) const;

#ifdef QT_TEXTEDIT_OPTIMIZATION
    bool checkOptimMode();
    QString optimText() const;
    void optimSetText(const QString &str);
    void optimAppend(const QString &str);
    void optimInsert(const QString &str, int line, int index);
    void optimDrawContents(QPainter * p, int cx, int cy, int cw, int ch);
    void optimMousePressEvent(QMouseEvent * e);
    void optimMouseReleaseEvent(QMouseEvent * e);
    void optimMouseMoveEvent(QMouseEvent * e);
    int  optimCharIndex(const QString &str, int mx) const;
    void optimSelectAll();
    void optimRemoveSelection();
    void optimSetSelection(int startLine, int startIdx, int endLine,
                            int endIdx);
    bool optimHasSelection() const;
    QString optimSelectedText() const;
    bool optimFind(const QString & str, bool, bool, bool, int *, int *);
    void optimParseTags(QString * str, int lineNo = -1, int indexOffset = 0);
    Q3TextEditOptimPrivate::Tag * optimPreviousLeftTag(int line);
    void optimSetTextFormat(Q3TextDocument *, Q3TextCursor *, Q3TextFormat * f,
                             int, int, Q3TextEditOptimPrivate::Tag * t);
    Q3TextEditOptimPrivate::Tag * optimAppendTag(int index, const QString & tag);
    Q3TextEditOptimPrivate::Tag * optimInsertTag(int line, int index, const QString & tag);
    void optimCheckLimit(const QString& str);
    bool optimHasBoldMetrics(int line);

private Q_SLOTS:
    void optimDoAutoScroll();
#endif // QT_TEXTEDIT_OPTIMIZATION

private:
#ifndef QT_NO_CLIPBOARD
    void pasteSubType(const QByteArray &subtype, QMimeSource *m);
#endif

private:
    Q_DISABLE_COPY(Q3TextEdit)

    Q3TextDocument *doc;
    Q3TextCursor *cursor;
    QTimer *formatTimer, *scrollTimer, *changeIntervalTimer, *blinkTimer, *dragStartTimer;
    Q3TextParagraph *lastFormatted;
    int interval;
    UndoRedoInfo undoRedoInfo;
    Q3TextFormat *currentFormat;
    int currentAlignment;
    QPoint oldMousePos, mousePos;
    QPoint dragStartPos;
    QString onLink;
    WordWrap wrapMode;
    WrapPolicy wPolicy;
    int wrapWidth;
    QString pressedLink;
    Q3TextEditPrivate *d;
    bool inDoubleClick : 1;
    bool mousePressed : 1;
    bool cursorVisible : 1;
    bool blinkCursorVisible : 1;
    bool modified : 1;
    bool mightStartDrag : 1;
    bool inDnD : 1;
    bool readonly : 1;
    bool undoEnabled : 1;
    bool overWrite : 1;
};

Q_DECLARE_OPERATORS_FOR_FLAGS(Q3TextEdit::AutoFormatting)

inline Q3TextDocument *Q3TextEdit::document() const
{
    return doc;
}

inline Q3TextCursor *Q3TextEdit::textCursor() const
{
    return cursor;
}

inline void Q3TextEdit::setCurrentFont(const QFont &f)
{
    Q3TextEdit::setFontInternal(f);
}

#endif // QT_NO_TEXTEDIT

QT_END_NAMESPACE

QT_END_HEADER

#endif // Q3TEXTEDIT_H