|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt3Support module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "q3syntaxhighlighter.h" |
|
43 #include "q3syntaxhighlighter_p.h" |
|
44 |
|
45 #ifndef QT_NO_SYNTAXHIGHLIGHTER |
|
46 #include "q3textedit.h" |
|
47 #include "qtimer.h" |
|
48 |
|
49 QT_BEGIN_NAMESPACE |
|
50 |
|
51 /*! |
|
52 \class Q3SyntaxHighlighter |
|
53 \brief The Q3SyntaxHighlighter class is a base class for |
|
54 implementing Q3TextEdit syntax highlighters. |
|
55 |
|
56 \compat |
|
57 |
|
58 A syntax highligher automatically highlights parts of the text in |
|
59 a Q3TextEdit. Syntax highlighters are often used when the user is |
|
60 entering text in a specific format (for example, source code) and |
|
61 help the user to read the text and identify syntax errors. |
|
62 |
|
63 To provide your own syntax highlighting for Q3TextEdit, you must |
|
64 subclass Q3SyntaxHighlighter and reimplement highlightParagraph(). |
|
65 |
|
66 When you create an instance of your Q3SyntaxHighlighter subclass, |
|
67 pass it the Q3TextEdit that you want the syntax highlighting to be |
|
68 applied to. After this your highlightParagraph() function will be |
|
69 called automatically whenever necessary. Use your |
|
70 highlightParagraph() function to apply formatting (e.g. setting |
|
71 the font and color) to the text that is passed to it. |
|
72 */ |
|
73 |
|
74 /*! |
|
75 Constructs the Q3SyntaxHighlighter and installs it on \a textEdit. |
|
76 Ownership of the Q3SyntaxHighlighter is transferred to the \a |
|
77 textEdit |
|
78 */ |
|
79 |
|
80 Q3SyntaxHighlighter::Q3SyntaxHighlighter(Q3TextEdit *textEdit) |
|
81 : para(0), edit(textEdit), d(new Q3SyntaxHighlighterPrivate) |
|
82 { |
|
83 textEdit->document()->setPreProcessor(new Q3SyntaxHighlighterInternal(this)); |
|
84 textEdit->document()->invalidate(); |
|
85 QTimer::singleShot(0, textEdit->viewport(), SLOT(update())); |
|
86 } |
|
87 |
|
88 /*! |
|
89 Destructor. Uninstalls this syntax highlighter from the textEdit() |
|
90 */ |
|
91 |
|
92 Q3SyntaxHighlighter::~Q3SyntaxHighlighter() |
|
93 { |
|
94 delete d; |
|
95 textEdit()->document()->setPreProcessor(0); |
|
96 } |
|
97 |
|
98 /*! |
|
99 \fn int Q3SyntaxHighlighter::highlightParagraph(const QString &text, int endStateOfLastPara) |
|
100 |
|
101 This function is called when necessary by the rich text engine, |
|
102 i.e. on paragraphs which have changed. |
|
103 |
|
104 In your reimplementation you should parse the paragraph's \a text |
|
105 and call setFormat() as often as necessary to apply any font and |
|
106 color changes that you require. Your function must return a value |
|
107 which indicates the paragraph's end state: see below. |
|
108 |
|
109 Some syntaxes can have constructs that span paragraphs. For |
|
110 example, a C++ syntax highlighter should be able to cope with |
|
111 \c{/}\c{*...*}\c{/} comments that span paragraphs. To deal |
|
112 with these cases it is necessary to know the end state of the |
|
113 previous paragraph (e.g. "in comment"). |
|
114 |
|
115 If your syntax does not have paragraph spanning constructs, simply |
|
116 ignore the \a endStateOfLastPara parameter and always return 0. |
|
117 |
|
118 Whenever highlightParagraph() is called it is passed a value for |
|
119 \a endStateOfLastPara. For the very first paragraph this value is |
|
120 always -2. For any other paragraph the value is the value returned |
|
121 by the most recent highlightParagraph() call that applied to the |
|
122 preceding paragraph. |
|
123 |
|
124 The value you return is up to you. We recommend only returning 0 |
|
125 (to signify that this paragraph's syntax highlighting does not |
|
126 affect the following paragraph), or a positive integer (to signify |
|
127 that this paragraph has ended in the middle of a paragraph |
|
128 spanning construct). |
|
129 |
|
130 To find out which paragraph is highlighted, call |
|
131 currentParagraph(). |
|
132 |
|
133 For example, if you're writing a simple C++ syntax highlighter, |
|
134 you might designate 1 to signify "in comment". For a paragraph |
|
135 that ended in the middle of a comment you'd return 1, and for |
|
136 other paragraphs you'd return 0. In your parsing code if \a |
|
137 endStateOfLastPara was 1, you would highlight the text as a C++ |
|
138 comment until you reached the closing \c{*}\c{/}. |
|
139 */ |
|
140 |
|
141 /*! |
|
142 This function is applied to the syntax highlighter's current |
|
143 paragraph (the text of which is passed to the highlightParagraph() |
|
144 function). |
|
145 |
|
146 The specified \a font and \a color are applied to the text from |
|
147 position \a start for \a count characters. (If \a count is 0, |
|
148 nothing is done.) |
|
149 */ |
|
150 |
|
151 void Q3SyntaxHighlighter::setFormat(int start, int count, const QFont &font, const QColor &color) |
|
152 { |
|
153 if (!para || count <= 0) |
|
154 return; |
|
155 Q3TextFormat *f = 0; |
|
156 f = para->document()->formatCollection()->format(font, color); |
|
157 para->setFormat(start, count, f); |
|
158 f->removeRef(); |
|
159 } |
|
160 |
|
161 /*! \overload */ |
|
162 |
|
163 void Q3SyntaxHighlighter::setFormat(int start, int count, const QColor &color) |
|
164 { |
|
165 if (!para || count <= 0) |
|
166 return; |
|
167 Q3TextFormat *f = 0; |
|
168 QFont fnt = textEdit()->QWidget::font(); |
|
169 f = para->document()->formatCollection()->format(fnt, color); |
|
170 para->setFormat(start, count, f); |
|
171 f->removeRef(); |
|
172 } |
|
173 |
|
174 /*! \overload */ |
|
175 |
|
176 void Q3SyntaxHighlighter::setFormat(int start, int count, const QFont &font) |
|
177 { |
|
178 if (!para || count <= 0) |
|
179 return; |
|
180 Q3TextFormat *f = 0; |
|
181 QColor c = textEdit()->viewport()->palette().color(textEdit()->viewport()->foregroundRole()); |
|
182 f = para->document()->formatCollection()->format(font, c); |
|
183 para->setFormat(start, count, f); |
|
184 f->removeRef(); |
|
185 } |
|
186 |
|
187 /*! |
|
188 \fn Q3TextEdit *Q3SyntaxHighlighter::textEdit() const |
|
189 |
|
190 Returns the Q3TextEdit on which this syntax highlighter is |
|
191 installed |
|
192 */ |
|
193 |
|
194 /*! Redoes the highlighting of the whole document. |
|
195 */ |
|
196 |
|
197 void Q3SyntaxHighlighter::rehighlight() |
|
198 { |
|
199 Q3TextParagraph *s = edit->document()->firstParagraph(); |
|
200 while (s) { |
|
201 s->invalidate(0); |
|
202 s->state = -1; |
|
203 s->needPreProcess = true; |
|
204 s = s->next(); |
|
205 } |
|
206 edit->repaintContents(); |
|
207 } |
|
208 |
|
209 /*! |
|
210 Returns the id of the paragraph which is highlighted, or -1 of no |
|
211 paragraph is currently highlighted. |
|
212 |
|
213 Usually this function is called from within highlightParagraph(). |
|
214 */ |
|
215 |
|
216 int Q3SyntaxHighlighter::currentParagraph() const |
|
217 { |
|
218 return d->currentParagraph; |
|
219 } |
|
220 |
|
221 QT_END_NAMESPACE |
|
222 |
|
223 #endif |