Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <svtools/svmedit.hxx>
22 : #include <vcl/xtextedt.hxx>
23 : #include <svtools/editsyntaxhighlighter.hxx>
24 : #include <vcl/txtattr.hxx>
25 :
26 :
27 0 : MultiLineEditSyntaxHighlight::MultiLineEditSyntaxHighlight( Window* pParent, WinBits nWinStyle,
28 0 : HighlighterLanguage aLanguage): MultiLineEdit(pParent,nWinStyle), mbDoBracketHilight(true)
29 : {
30 0 : EnableUpdateData(300);
31 0 : aHighlighter.initialize( aLanguage );
32 0 : }
33 :
34 0 : MultiLineEditSyntaxHighlight::MultiLineEditSyntaxHighlight( Window* pParent, const ResId& rResId ,
35 0 : HighlighterLanguage aLanguage): MultiLineEdit(pParent,rResId), mbDoBracketHilight(true)
36 : {
37 0 : EnableUpdateData(300);
38 0 : aHighlighter.initialize( aLanguage );
39 0 : }
40 :
41 0 : MultiLineEditSyntaxHighlight::~MultiLineEditSyntaxHighlight()
42 : {
43 0 : }
44 :
45 0 : void MultiLineEditSyntaxHighlight::SetText(const OUString& rNewText)
46 : {
47 0 : MultiLineEdit::SetText(rNewText);
48 0 : UpdateData();
49 0 : }
50 :
51 0 : void MultiLineEditSyntaxHighlight::DoBracketHilight(sal_uInt16 nKey)
52 : {
53 0 : TextSelection aCurrentPos = GetTextView()->GetSelection();
54 0 : xub_StrLen nStartPos = aCurrentPos.GetStart().GetIndex();
55 0 : sal_uLong nStartPara = aCurrentPos.GetStart().GetPara();
56 0 : sal_uInt16 nCount = 0;
57 0 : int nChar = -1;
58 :
59 0 : switch (nKey)
60 : {
61 : case '\'': // no break
62 : case '"':
63 : {
64 0 : nChar = nKey;
65 0 : break;
66 : }
67 : case '}' :
68 : {
69 0 : nChar = '{';
70 0 : break;
71 : }
72 : case ')':
73 : {
74 0 : nChar = '(';
75 0 : break;
76 : }
77 : case ']':
78 : {
79 0 : nChar = '[';
80 0 : break;
81 : }
82 : }
83 :
84 0 : if (nChar != -1)
85 : {
86 0 : for (long nPara = nStartPara; nPara>=0; --nPara)
87 : {
88 0 : if (nStartPos == 0)
89 0 : continue;
90 :
91 0 : OUString aLine( GetTextEngine()->GetText( nPara ) );
92 :
93 0 : if (aLine.isEmpty())
94 0 : continue;
95 :
96 0 : for (sal_Int32 i = ((sal_uLong)nPara==nStartPara) ? nStartPos-1 : aLine.getLength()-1; i>0; --i)
97 : {
98 0 : if (aLine[i] == nChar)
99 : {
100 0 : if (!nCount)
101 : {
102 0 : GetTextEngine()->SetAttrib( TextAttribFontWeight( WEIGHT_ULTRABOLD ), nPara, i, i+1, sal_True );
103 0 : GetTextEngine()->SetAttrib( TextAttribFontColor( Color(0,0,0) ), nPara, i, i+1, sal_True );
104 0 : GetTextEngine()->SetAttrib( TextAttribFontWeight( WEIGHT_ULTRABOLD ), nStartPara, nStartPos, nStartPos, sal_True );
105 0 : GetTextEngine()->SetAttrib( TextAttribFontColor( Color(0,0,0) ), nStartPara, nStartPos, nStartPos, sal_True );
106 0 : return;
107 : }
108 : else
109 0 : --nCount;
110 : }
111 0 : if (aLine[i] == nKey)
112 0 : ++nCount;
113 : }
114 0 : }
115 : }
116 : }
117 :
118 0 : long MultiLineEditSyntaxHighlight::PreNotify( NotifyEvent& rNEvt )
119 : {
120 0 : if ( mbDoBracketHilight && (rNEvt.GetType() == EVENT_KEYINPUT) )
121 0 : DoBracketHilight(rNEvt.GetKeyEvent()->GetCharCode());
122 :
123 0 : return MultiLineEdit::PreNotify(rNEvt);
124 : }
125 :
126 0 : Color MultiLineEditSyntaxHighlight::GetColorValue(TokenTypes aToken)
127 : {
128 0 : Color aColor;
129 0 : switch (aHighlighter.GetLanguage())
130 : {
131 : case HIGHLIGHT_SQL:
132 : {
133 0 : switch (aToken)
134 : {
135 0 : case TT_IDENTIFIER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLIDENTIFIER).nColor; break;
136 0 : case TT_NUMBER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLNUMBER).nColor; break;
137 0 : case TT_STRING: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLSTRING).nColor; break;
138 0 : case TT_OPERATOR: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLOPERATOR).nColor; break;
139 0 : case TT_KEYWORDS: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLKEYWORD).nColor; break;
140 0 : case TT_PARAMETER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLPARAMETER).nColor; break;
141 0 : case TT_COMMENT: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLCOMMENT).nColor; break;
142 0 : default: aColor = Color(0,0,0);
143 : }
144 0 : break;
145 : }
146 : case HIGHLIGHT_BASIC:
147 : {
148 0 : switch (aToken)
149 : {
150 0 : case TT_IDENTIFIER: aColor = Color(255,0,0); break;
151 0 : case TT_COMMENT: aColor = Color(0,0,45); break;
152 0 : case TT_NUMBER: aColor = Color(204,102,204); break;
153 0 : case TT_STRING: aColor = Color(0,255,45); break;
154 0 : case TT_OPERATOR: aColor = Color(0,0,100); break;
155 0 : case TT_KEYWORDS: aColor = Color(0,0,255); break;
156 0 : case TT_ERROR : aColor = Color(0,255,255); break;
157 0 : default: aColor = Color(0,0,0);
158 : }
159 0 : break;
160 : }
161 0 : default: aColor = Color(0,0,0);
162 :
163 : }
164 0 : return aColor;
165 : }
166 :
167 0 : void MultiLineEditSyntaxHighlight::UpdateData()
168 : {
169 : // syntax highlighting
170 : // this must be possible improved by using notifychange correctly
171 0 : sal_Bool bTempModified = GetTextEngine()->IsModified();
172 0 : for (unsigned int nLine=0; nLine < GetTextEngine()->GetParagraphCount(); nLine++)
173 : {
174 0 : OUString aLine( GetTextEngine()->GetText( nLine ) );
175 0 : aHighlighter.notifyChange( nLine, 0, &aLine, 1 );
176 :
177 0 : GetTextEngine()->RemoveAttribs( nLine, sal_True );
178 0 : HighlightPortions aPortions;
179 0 : aHighlighter.getHighlightPortions( nLine, aLine, aPortions );
180 0 : for ( size_t i = 0; i < aPortions.size(); i++ )
181 : {
182 0 : HighlightPortion& r = aPortions[i];
183 0 : GetTextEngine()->SetAttrib( TextAttribFontColor( GetColorValue(r.tokenType) ), nLine, r.nBegin, r.nEnd, sal_True );
184 : }
185 0 : }
186 0 : GetTextView()->ShowCursor( false, true );
187 0 : GetTextEngine()->SetModified(bTempModified);
188 465 : }
189 :
190 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|