1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #ifndef BASETEXTDOCUMENTLAYOUT_H
34 #define BASETEXTDOCUMENTLAYOUT_H
36 #include "texteditor_global.h"
38 #include "itexteditor.h"
40 #include <QtGui/QTextBlockUserData>
41 #include <QtGui/QPlainTextDocumentLayout>
43 namespace TextEditor {
46 typedef QVector<Parenthesis> Parentheses;
48 struct TEXTEDITOR_EXPORT Parenthesis
50 enum Type { Opened, Closed };
52 inline Parenthesis() : type(Opened), pos(-1) {}
53 inline Parenthesis(Type t, QChar c, int position)
54 : type(t), chr(c), pos(position) {}
60 class TEXTEDITOR_EXPORT CodeFormatterData
63 virtual ~CodeFormatterData();
66 class TEXTEDITOR_EXPORT TextBlockUserData : public QTextBlockUserData
70 inline TextBlockUserData()
75 m_foldingStartIncluded(false),
76 m_foldingEndIncluded(false),
77 m_codeFormatterData(0)
81 inline TextMarks marks() const { return m_marks; }
82 void addMark(ITextMark *mark);
83 inline bool removeMark(ITextMark *mark) { return m_marks.removeAll(mark); }
84 inline bool hasMark(ITextMark *mark) const { return m_marks.contains(mark); }
85 inline void clearMarks() { m_marks.clear(); }
86 inline void documentClosing() { Q_FOREACH(ITextMark *tm, m_marks) { tm->documentClosing(); } m_marks.clear();}
88 inline void setFolded(bool b) { m_folded = b; }
89 inline bool folded() const { return m_folded; }
91 inline void setParentheses(const Parentheses &parentheses) { m_parentheses = parentheses; }
92 inline void clearParentheses() { m_parentheses.clear(); }
93 inline const Parentheses &parentheses() const { return m_parentheses; }
94 inline bool hasParentheses() const { return !m_parentheses.isEmpty(); }
95 int braceDepthDelta() const;
97 inline bool setIfdefedOut() { bool result = m_ifdefedOut; m_ifdefedOut = true; return !result; }
98 inline bool clearIfdefedOut() { bool result = m_ifdefedOut; m_ifdefedOut = false; return result;}
99 inline bool ifdefedOut() const { return m_ifdefedOut; }
102 enum MatchType { NoMatch, Match, Mismatch };
103 static MatchType checkOpenParenthesis(QTextCursor *cursor, QChar c);
104 static MatchType checkClosedParenthesis(QTextCursor *cursor, QChar c);
105 static MatchType matchCursorBackward(QTextCursor *cursor);
106 static MatchType matchCursorForward(QTextCursor *cursor);
107 static bool findPreviousOpenParenthesis(QTextCursor *cursor, bool select = false);
108 static bool findNextClosingParenthesis(QTextCursor *cursor, bool select = false);
110 static bool findPreviousBlockOpenParenthesis(QTextCursor *cursor, bool checkStartPosition = false);
111 static bool findNextBlockClosingParenthesis(QTextCursor *cursor);
113 // Get the code folding level
114 inline int foldingIndent() const { return m_foldingIndent; }
115 /* Set the code folding level.
117 * A code folding marker will appear the line *before* the one where the indention
118 * level increases. The code folding reagion will end in the last line that has the same
119 * indention level (or higher).
121 inline void setFoldingIndent(int indent) { m_foldingIndent = indent; }
122 // Set whether the first character of the folded region will show when the code is folded.
123 inline void setFoldingStartIncluded(bool included) { m_foldingStartIncluded = included; }
124 inline bool foldingStartIncluded() const { return m_foldingStartIncluded; }
125 // Set whether the last character of the folded region will show when the code is folded.
126 inline void setFoldingEndIncluded(bool included) { m_foldingEndIncluded = included; }
127 inline bool foldingEndIncluded() const { return m_foldingEndIncluded; }
128 inline int lexerState() const { return m_lexerState; }
129 inline void setLexerState(int state) {m_lexerState = state; }
132 CodeFormatterData *codeFormatterData() const { return m_codeFormatterData; }
133 void setCodeFormatterData(CodeFormatterData *data);
138 uint m_ifdefedOut : 1;
139 uint m_foldingIndent : 16;
140 uint m_lexerState : 4;
141 uint m_foldingStartIncluded : 1;
142 uint m_foldingEndIncluded : 1;
143 Parentheses m_parentheses;
144 CodeFormatterData *m_codeFormatterData;
148 class TEXTEDITOR_EXPORT BaseTextDocumentLayout : public QPlainTextDocumentLayout
153 BaseTextDocumentLayout(QTextDocument *doc);
154 ~BaseTextDocumentLayout();
156 static void setParentheses(const QTextBlock &block, const Parentheses &parentheses);
157 static void clearParentheses(const QTextBlock &block) { setParentheses(block, Parentheses());}
158 static Parentheses parentheses(const QTextBlock &block);
159 static bool hasParentheses(const QTextBlock &block);
160 static bool setIfdefedOut(const QTextBlock &block);
161 static bool clearIfdefedOut(const QTextBlock &block);
162 static bool ifdefedOut(const QTextBlock &block);
163 static int braceDepthDelta(const QTextBlock &block);
164 static int braceDepth(const QTextBlock &block);
165 static void setBraceDepth(QTextBlock &block, int depth);
166 static void changeBraceDepth(QTextBlock &block, int delta);
167 static void setFoldingIndent(const QTextBlock &block, int indent);
168 static int foldingIndent(const QTextBlock &block);
169 static void setLexerState(const QTextBlock &block, int state);
170 static int lexerState(const QTextBlock &block);
171 static void changeFoldingIndent(QTextBlock &block, int delta);
172 static bool canFold(const QTextBlock &block);
173 static void doFoldOrUnfold(const QTextBlock& block, bool unfold);
174 static bool isFolded(const QTextBlock &block);
175 static void setFolded(const QTextBlock &block, bool folded);
177 static TextBlockUserData *testUserData(const QTextBlock &block) {
178 return static_cast<TextBlockUserData*>(block.userData());
180 static TextBlockUserData *userData(const QTextBlock &block) {
181 TextBlockUserData *data = static_cast<TextBlockUserData*>(block.userData());
182 if (!data && block.isValid())
183 const_cast<QTextBlock &>(block).setUserData((data = new TextBlockUserData));
188 void emitDocumentSizeChanged() { emit documentSizeChanged(documentSize()); }
189 int lastSaveRevision;
193 void setRequiredWidth(int width);
195 QSizeF documentSize() const;
199 } // namespace TextEditor
201 #endif // BASETEXTDOCUMENTLAYOUT_H