1 // This module implements the QsciLexerCustom class.
2 //
3 // Copyright (c) 2021 Riverbank Computing Limited <info@riverbankcomputing.com>
4 //
5 // This file is part of QScintilla.
6 //
7 // This file may be used under the terms of the GNU General Public License
8 // version 3.0 as published by the Free Software Foundation and appearing in
9 // the file LICENSE included in the packaging of this file.  Please review the
10 // following information to ensure the GNU General Public License version 3.0
11 // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 //
13 // If you do not wish to use this file under the terms of the GPL version 3.0
14 // then you may purchase a commercial license.  For more information contact
15 // info@riverbankcomputing.com.
16 //
17 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 
20 
21 #include "Qsci/qscilexercustom.h"
22 
23 #include "Qsci/qsciscintilla.h"
24 #include "Qsci/qsciscintillabase.h"
25 #include "Qsci/qscistyle.h"
26 
27 
28 // The ctor.
QsciLexerCustom(QObject * parent)29 QsciLexerCustom::QsciLexerCustom(QObject *parent)
30     : QsciLexer(parent)
31 {
32 }
33 
34 
35 // The dtor.
~QsciLexerCustom()36 QsciLexerCustom::~QsciLexerCustom()
37 {
38 }
39 
40 
41 // Start styling.
startStyling(int start,int)42 void QsciLexerCustom::startStyling(int start, int)
43 {
44     if (!editor())
45         return;
46 
47     editor()->SendScintilla(QsciScintillaBase::SCI_STARTSTYLING, start);
48 }
49 
50 
51 // Set the style for a number of characters.
setStyling(int length,int style)52 void QsciLexerCustom::setStyling(int length, int style)
53 {
54     if (!editor())
55         return;
56 
57     editor()->SendScintilla(QsciScintillaBase::SCI_SETSTYLING, length, style);
58 }
59 
60 
61 // Set the style for a number of characters.
setStyling(int length,const QsciStyle & style)62 void QsciLexerCustom::setStyling(int length, const QsciStyle &style)
63 {
64     setStyling(length, style.style());
65 }
66 
67 
68 // Set the attached editor.
setEditor(QsciScintilla * new_editor)69 void QsciLexerCustom::setEditor(QsciScintilla *new_editor)
70 {
71     if (editor())
72         disconnect(editor(), SIGNAL(SCN_STYLENEEDED(int)), this,
73                 SLOT(handleStyleNeeded(int)));
74 
75     QsciLexer::setEditor(new_editor);
76 
77     if (editor())
78         connect(editor(), SIGNAL(SCN_STYLENEEDED(int)), this,
79                 SLOT(handleStyleNeeded(int)));
80 }
81 
82 
83 // Return the number of style bits needed by the lexer.
styleBitsNeeded() const84 int QsciLexerCustom::styleBitsNeeded() const
85 {
86     return 5;
87 }
88 
89 
90 // Handle a request to style some text.
handleStyleNeeded(int pos)91 void QsciLexerCustom::handleStyleNeeded(int pos)
92 {
93     int start = editor()->SendScintilla(QsciScintillaBase::SCI_GETENDSTYLED);
94     int line = editor()->SendScintilla(QsciScintillaBase::SCI_LINEFROMPOSITION,
95             start);
96     start = editor()->SendScintilla(QsciScintillaBase::SCI_POSITIONFROMLINE,
97             line);
98 
99     if (start != pos)
100         styleText(start, pos);
101 }
102