| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: qsyntaxhighlighter.cpp 2 2005-11-16 15:49:26Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Implementation of the QSyntaxHighlighter class
|
|---|
| 5 | **
|
|---|
| 6 | ** Created : 990101
|
|---|
| 7 | **
|
|---|
| 8 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
|---|
| 9 | **
|
|---|
| 10 | ** This file is part of the widgets module of the Qt GUI Toolkit.
|
|---|
| 11 | **
|
|---|
| 12 | ** This file may be distributed under the terms of the Q Public License
|
|---|
| 13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
|---|
| 14 | ** LICENSE.QPL included in the packaging of this file.
|
|---|
| 15 | **
|
|---|
| 16 | ** This file may be distributed and/or modified under the terms of the
|
|---|
| 17 | ** GNU General Public License version 2 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 19 | ** packaging of this file.
|
|---|
| 20 | **
|
|---|
| 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
|---|
| 22 | ** licenses may use this file in accordance with the Qt Commercial License
|
|---|
| 23 | ** Agreement provided with the Software.
|
|---|
| 24 | **
|
|---|
| 25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|---|
| 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 27 | **
|
|---|
| 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
|---|
| 29 | ** information about Qt Commercial License Agreements.
|
|---|
| 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
|---|
| 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
|---|
| 32 | **
|
|---|
| 33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
|---|
| 34 | ** not clear to you.
|
|---|
| 35 | **
|
|---|
| 36 | **********************************************************************/
|
|---|
| 37 |
|
|---|
| 38 | #include "qsyntaxhighlighter.h"
|
|---|
| 39 | #include "private/qsyntaxhighlighter_p.h"
|
|---|
| 40 |
|
|---|
| 41 | #ifndef QT_NO_SYNTAXHIGHLIGHTER
|
|---|
| 42 | #include "../kernel/qrichtext_p.h"
|
|---|
| 43 | #include "qtextedit.h"
|
|---|
| 44 | #include "qtimer.h"
|
|---|
| 45 |
|
|---|
| 46 | /*!
|
|---|
| 47 | \class QSyntaxHighlighter qsyntaxhighlighter.h
|
|---|
| 48 | \brief The QSyntaxHighlighter class is a base class for
|
|---|
| 49 | implementing QTextEdit syntax highlighters.
|
|---|
| 50 |
|
|---|
| 51 | \ingroup basic
|
|---|
| 52 | \ingroup text
|
|---|
| 53 |
|
|---|
| 54 | A syntax highligher automatically highlights parts of the text in
|
|---|
| 55 | a QTextEdit. Syntax highlighters are often used when the user is
|
|---|
| 56 | entering text in a specific format (for example, source code) and
|
|---|
| 57 | help the user to read the text and identify syntax errors.
|
|---|
| 58 |
|
|---|
| 59 | To provide your own syntax highlighting for QTextEdit, you must
|
|---|
| 60 | subclass QSyntaxHighlighter and reimplement highlightParagraph().
|
|---|
| 61 |
|
|---|
| 62 | When you create an instance of your QSyntaxHighlighter subclass,
|
|---|
| 63 | pass it the QTextEdit that you want the syntax highlighting to be
|
|---|
| 64 | applied to. After this your highlightParagraph() function will be
|
|---|
| 65 | called automatically whenever necessary. Use your
|
|---|
| 66 | highlightParagraph() function to apply formatting (e.g. setting
|
|---|
| 67 | the font and color) to the text that is passed to it.
|
|---|
| 68 | */
|
|---|
| 69 |
|
|---|
| 70 | /*!
|
|---|
| 71 | Constructs the QSyntaxHighlighter and installs it on \a textEdit.
|
|---|
| 72 | Ownership of the QSyntaxHighlighter is transferred to the \a
|
|---|
| 73 | textEdit
|
|---|
| 74 | */
|
|---|
| 75 |
|
|---|
| 76 | QSyntaxHighlighter::QSyntaxHighlighter( QTextEdit *textEdit )
|
|---|
| 77 | : para( 0 ), edit( textEdit ), d( new QSyntaxHighlighterPrivate )
|
|---|
| 78 | {
|
|---|
| 79 | textEdit->document()->setPreProcessor( new QSyntaxHighlighterInternal( this ) );
|
|---|
| 80 | textEdit->document()->invalidate();
|
|---|
| 81 | QTimer::singleShot( 0, textEdit->viewport(), SLOT( update() ) );
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /*!
|
|---|
| 85 | Destructor. Uninstalls this syntax highlighter from the textEdit()
|
|---|
| 86 | */
|
|---|
| 87 |
|
|---|
| 88 | QSyntaxHighlighter::~QSyntaxHighlighter()
|
|---|
| 89 | {
|
|---|
| 90 | delete d;
|
|---|
| 91 | textEdit()->document()->setPreProcessor( 0 );
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /*!
|
|---|
| 95 | \fn int QSyntaxHighlighter::highlightParagraph( const QString &text, int endStateOfLastPara )
|
|---|
| 96 |
|
|---|
| 97 | This function is called when necessary by the rich text engine,
|
|---|
| 98 | i.e. on paragraphs which have changed.
|
|---|
| 99 |
|
|---|
| 100 | In your reimplementation you should parse the paragraph's \a text
|
|---|
| 101 | and call setFormat() as often as necessary to apply any font and
|
|---|
| 102 | color changes that you require. Your function must return a value
|
|---|
| 103 | which indicates the paragraph's end state: see below.
|
|---|
| 104 |
|
|---|
| 105 | Some syntaxes can have constructs that span paragraphs. For
|
|---|
| 106 | example, a C++ syntax highlighter should be able to cope with
|
|---|
| 107 | \c{/}\c{*...*}\c{/} comments that span paragraphs. To deal
|
|---|
| 108 | with these cases it is necessary to know the end state of the
|
|---|
| 109 | previous paragraph (e.g. "in comment").
|
|---|
| 110 |
|
|---|
| 111 | If your syntax does not have paragraph spanning constructs, simply
|
|---|
| 112 | ignore the \a endStateOfLastPara parameter and always return 0.
|
|---|
| 113 |
|
|---|
| 114 | Whenever highlightParagraph() is called it is passed a value for
|
|---|
| 115 | \a endStateOfLastPara. For the very first paragraph this value is
|
|---|
| 116 | always -2. For any other paragraph the value is the value returned
|
|---|
| 117 | by the most recent highlightParagraph() call that applied to the
|
|---|
| 118 | preceding paragraph.
|
|---|
| 119 |
|
|---|
| 120 | The value you return is up to you. We recommend only returning 0
|
|---|
| 121 | (to signify that this paragraph's syntax highlighting does not
|
|---|
| 122 | affect the following paragraph), or a positive integer (to signify
|
|---|
| 123 | that this paragraph has ended in the middle of a paragraph
|
|---|
| 124 | spanning construct).
|
|---|
| 125 |
|
|---|
| 126 | To find out which paragraph is highlighted, call
|
|---|
| 127 | currentParagraph().
|
|---|
| 128 |
|
|---|
| 129 | For example, if you're writing a simple C++ syntax highlighter,
|
|---|
| 130 | you might designate 1 to signify "in comment". For a paragraph
|
|---|
| 131 | that ended in the middle of a comment you'd return 1, and for
|
|---|
| 132 | other paragraphs you'd return 0. In your parsing code if \a
|
|---|
| 133 | endStateOfLastPara was 1, you would highlight the text as a C++
|
|---|
| 134 | comment until you reached the closing \c{*}\c{/}.
|
|---|
| 135 | */
|
|---|
| 136 |
|
|---|
| 137 | /*!
|
|---|
| 138 | This function is applied to the syntax highlighter's current
|
|---|
| 139 | paragraph (the text of which is passed to the highlightParagraph()
|
|---|
| 140 | function).
|
|---|
| 141 |
|
|---|
| 142 | The specified \a font and \a color are applied to the text from
|
|---|
| 143 | position \a start for \a count characters. (If \a count is 0,
|
|---|
| 144 | nothing is done.)
|
|---|
| 145 | */
|
|---|
| 146 |
|
|---|
| 147 | void QSyntaxHighlighter::setFormat( int start, int count, const QFont &font, const QColor &color )
|
|---|
| 148 | {
|
|---|
| 149 | if ( !para || count <= 0 )
|
|---|
| 150 | return;
|
|---|
| 151 | QTextFormat *f = 0;
|
|---|
| 152 | f = para->document()->formatCollection()->format( font, color );
|
|---|
| 153 | para->setFormat( start, count, f );
|
|---|
| 154 | f->removeRef();
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /*! \overload */
|
|---|
| 158 |
|
|---|
| 159 | void QSyntaxHighlighter::setFormat( int start, int count, const QColor &color )
|
|---|
| 160 | {
|
|---|
| 161 | if ( !para || count <= 0 )
|
|---|
| 162 | return;
|
|---|
| 163 | QTextFormat *f = 0;
|
|---|
| 164 | QFont fnt = textEdit()->QWidget::font();
|
|---|
| 165 | f = para->document()->formatCollection()->format( fnt, color );
|
|---|
| 166 | para->setFormat( start, count, f );
|
|---|
| 167 | f->removeRef();
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | /*! \overload */
|
|---|
| 171 |
|
|---|
| 172 | void QSyntaxHighlighter::setFormat( int start, int count, const QFont &font )
|
|---|
| 173 | {
|
|---|
| 174 | if ( !para || count <= 0 )
|
|---|
| 175 | return;
|
|---|
| 176 | QTextFormat *f = 0;
|
|---|
| 177 | QColor c = textEdit()->viewport()->paletteForegroundColor();
|
|---|
| 178 | f = para->document()->formatCollection()->format( font, c );
|
|---|
| 179 | para->setFormat( start, count, f );
|
|---|
| 180 | f->removeRef();
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | /*!
|
|---|
| 184 | \fn QTextEdit *QSyntaxHighlighter::textEdit() const
|
|---|
| 185 |
|
|---|
| 186 | Returns the QTextEdit on which this syntax highlighter is
|
|---|
| 187 | installed
|
|---|
| 188 | */
|
|---|
| 189 |
|
|---|
| 190 | /*! Redoes the highlighting of the whole document.
|
|---|
| 191 | */
|
|---|
| 192 |
|
|---|
| 193 | void QSyntaxHighlighter::rehighlight()
|
|---|
| 194 | {
|
|---|
| 195 | QTextParagraph *s = edit->document()->firstParagraph();
|
|---|
| 196 | while ( s ) {
|
|---|
| 197 | s->invalidate( 0 );
|
|---|
| 198 | s->state = -1;
|
|---|
| 199 | s->needPreProcess = TRUE;
|
|---|
| 200 | s = s->next();
|
|---|
| 201 | }
|
|---|
| 202 | edit->repaintContents( FALSE );
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /*!
|
|---|
| 206 | Returns the id of the paragraph which is highlighted, or -1 of no
|
|---|
| 207 | paragraph is currently highlighted.
|
|---|
| 208 |
|
|---|
| 209 | Usually this function is called from within highlightParagraph().
|
|---|
| 210 | */
|
|---|
| 211 |
|
|---|
| 212 | int QSyntaxHighlighter::currentParagraph() const
|
|---|
| 213 | {
|
|---|
| 214 | return d->currentParagraph;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | #endif
|
|---|