basetexteditor_p.h 7.56 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11 12 13 14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30 31 32 33
#ifndef BASETEXTEDITOR_P_H
#define BASETEXTEDITOR_P_H

#include "basetexteditor.h"
mae's avatar
mae committed
34
#include <texteditor/fontsettings.h>
con's avatar
con committed
35 36 37

#include <QtCore/QBasicTimer>
#include <QtCore/QSharedData>
38
#include <QtCore/QPointer>
con's avatar
con committed
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

#include <QtGui/QTextEdit>
#include <QtGui/QPixmap>

namespace TextEditor {

class BaseTextDocument;

namespace Internal {

//========== Pointers with reference count ==========

template <class T> class QRefCountData : public QSharedData
{
public:
    QRefCountData(T *data) { m_data = data; }

    ~QRefCountData() { delete m_data; }

    T *m_data;
};

/* MOSTLY COPIED FROM QSHAREDDATA(-POINTER) */
template <class T> class QRefCountPointer
{
public:
    inline T &operator*() { return d ? *(d->m_data) : 0; }
    inline const T &operator*() const { return d ? *(d->m_data) : 0; }
    inline T *operator->() { return d ? d->m_data : 0; }
    inline const T *operator->() const { return d ? d->m_data : 0; }
    inline operator T *() { return d ? d->m_data : 0; }
    inline operator const T *() const { return d ? d->m_data : 0; }

    inline bool operator==(const QRefCountPointer<T> &other) const { return d == other.d; }
    inline bool operator!=(const QRefCountPointer<T> &other) const { return d != other.d; }

    inline QRefCountPointer() { d = 0; }
    inline ~QRefCountPointer() { if (d && !d->ref.deref()) delete d; }

    explicit QRefCountPointer(T *data) {
        if (data) {
            d = new QRefCountData<T>(data);
            d->ref.ref();
        }
        else {
            d = 0;
        }
    }
    inline QRefCountPointer(const QRefCountPointer<T> &o) : d(o.d) { if (d) d->ref.ref(); }
    inline QRefCountPointer<T> & operator=(const QRefCountPointer<T> &o) {
        if (o.d != d) {
            if (d && !d->ref.deref())
                delete d;
            //todo: atomic assign of pointers
            d = o.d;
            if (d)
                d->ref.ref();
        }
        return *this;
    }
    inline QRefCountPointer &operator=(T *o) {
        if (d == 0 || d->m_data != o) {
            if (d && !d->ref.deref())
                delete d;
            d = new QRefCountData<T>(o);
            if (d)
                d->ref.ref();
        }
        return *this;
    }

    inline bool operator!() const { return !d; }

private:
    QRefCountData<T> *d;
};

//================BaseTextEditorPrivate==============

118 119 120 121 122
struct BaseTextEditorPrivateHighlightBlocks
{
    QList<int> open;
    QList<int> close;
    QList<int> visualIndent;
mae's avatar
mae committed
123
    inline int count() const { return visualIndent.size(); }
124
    inline bool isEmpty() const { return open.isEmpty() || close.isEmpty() || visualIndent.isEmpty(); }
125 126 127 128 129 130
    inline bool operator==(const BaseTextEditorPrivateHighlightBlocks &o) const {
        return (open == o.open && close == o.close && visualIndent == o.visualIndent);
    }
    inline bool operator!=(const BaseTextEditorPrivateHighlightBlocks &o) const { return !(*this == o); }
};

131

con's avatar
con committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
class BaseTextEditorPrivate
{
    BaseTextEditorPrivate(const BaseTextEditorPrivate &);
    BaseTextEditorPrivate &operator=(const BaseTextEditorPrivate &);

public:
    BaseTextEditorPrivate();
    ~BaseTextEditorPrivate();

#ifndef TEXTEDITOR_STANDALONE
    void setupBasicEditActions(TextEditorActionHandler *actionHandler);
#endif
    void setupDocumentSignals(BaseTextDocument *document);
    void updateLineSelectionColor();

    void print(QPrinter *printer);

    QTextBlock m_firstVisible;
    int m_lastScrollPos;
    int m_lineNumber;

    BaseTextEditor *q;
    bool m_contentsChanged;
155
    bool m_lastCursorChangeWasInteresting;
con's avatar
con committed
156 157 158 159 160 161

    QList<QTextEdit::ExtraSelection> m_syntaxHighlighterSelections;
    QTextEdit::ExtraSelection m_lineSelection;

    QRefCountPointer<BaseTextDocument> m_document;
    QByteArray m_tempState;
162
    QByteArray m_tempNavigationState;
con's avatar
con committed
163 164 165

    QString m_displayName;
    bool m_parenthesesMatchingEnabled;
166
    bool m_autoParenthesesEnabled;
con's avatar
con committed
167 168 169 170 171 172 173 174 175 176 177 178
    QTimer *m_updateTimer;

    // parentheses matcher
    bool m_formatRange;
    QTextCharFormat m_matchFormat;
    QTextCharFormat m_mismatchFormat;
    QTextCharFormat m_rangeFormat;
    QTimer *m_parenthesesMatchingTimer;
    // end parentheses matcher

    QWidget *m_extraArea;
    DisplaySettings m_displaySettings;
mae's avatar
mae committed
179
    TextEditor::FontSettings m_fontSettings;
con's avatar
con committed
180 181 182 183

    int extraAreaSelectionAnchorBlockNumber;
    int extraAreaToggleMarkBlockNumber;
    int extraAreaHighlightCollapseBlockNumber;
184
    int extraAreaHighlightCollapseColumn;
con's avatar
con committed
185 186 187 188 189

    QBasicTimer collapsedBlockTimer;
    int visibleCollapsedBlockNumber;
    int suggestedVisibleCollapsedBlockNumber;
    void clearVisibleCollapsedBlock();
190
    bool m_mouseOnCollapsedMarker;
con's avatar
con committed
191 192 193 194 195 196

    QBasicTimer autoScrollTimer;
    void updateMarksLineNumber();
    void updateMarksBlock(const QTextBlock &block);
    uint m_marksVisible : 1;
    uint m_codeFoldingVisible : 1;
197
    uint m_codeFoldingSupported : 1;
198
    uint m_mouseNavigationEnabled : 1;
con's avatar
con committed
199 200 201 202 203 204 205
    uint m_revisionsVisible : 1;
    uint m_lineNumbersVisible : 1;
    uint m_highlightCurrentLine : 1;
    uint m_requestMarkEnabled : 1;
    uint m_lineSeparatorsAllowed : 1;
    int m_visibleWrapColumn;

206
    QTextCharFormat m_linkFormat;
207 208
    BaseTextEditor::Link m_currentLink;
    bool m_linkPressed;
209

con's avatar
con committed
210 211 212
    QTextCharFormat m_ifdefedOutFormat;

    QRegExp m_searchExpr;
213
    Find::IFindSupport::FindFlags m_findFlags;
con's avatar
con committed
214 215 216
    QTextCharFormat m_searchResultFormat;
    QTextCharFormat m_searchScopeFormat;
    QTextCharFormat m_currentLineFormat;
217
    QTextCharFormat m_currentLineNumberFormat;
con's avatar
con committed
218 219 220 221 222 223 224
    void highlightSearchResults(const QTextBlock &block,
                                QVector<QTextLayout::FormatRange> *selections);

    BaseTextEditorEditable *m_editable;

    QObject *m_actionHack;

225
    QList<QTextEdit::ExtraSelection> m_extraSelections[BaseTextEditor::NExtraSelectionKinds];
con's avatar
con committed
226 227 228 229 230 231 232 233

    // block selection mode
    bool m_inBlockSelectionMode;
    bool m_lastEventWasBlockSelectionEvent;
    int m_blockSelectionExtraX;
    void clearBlockSelection();
    QString copyBlockSelection();
    void removeBlockSelection(const QString &text = QString());
234
    bool m_moveLineUndoHack;
235

con's avatar
con committed
236
    QTextCursor m_findScope;
237
    QTextCursor m_selectBlockAnchor;
con's avatar
con committed
238

239
    void moveCursorVisible(bool ensureVisible = true);
240 241 242 243 244

    int visualIndent(const QTextBlock &block) const;
    BaseTextEditorPrivateHighlightBlocks m_highlightBlocksInfo;
    QTimer *m_highlightBlocksTimer;

245 246
    QPointer<BaseTextEditorAnimator> m_animator;

con's avatar
con committed
247 248 249 250 251 252
};

} // namespace Internal
} // namespace TextEditor

#endif // BASETEXTEDITOR_P_H