1*5900e8ecSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*5900e8ecSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*5900e8ecSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*5900e8ecSAndrew Rist * distributed with this work for additional information
6*5900e8ecSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*5900e8ecSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*5900e8ecSAndrew Rist * "License"); you may not use this file except in compliance
9*5900e8ecSAndrew Rist * with the License. You may obtain a copy of the License at
10*5900e8ecSAndrew Rist *
11*5900e8ecSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*5900e8ecSAndrew Rist *
13*5900e8ecSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*5900e8ecSAndrew Rist * software distributed under the License is distributed on an
15*5900e8ecSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5900e8ecSAndrew Rist * KIND, either express or implied. See the License for the
17*5900e8ecSAndrew Rist * specific language governing permissions and limitations
18*5900e8ecSAndrew Rist * under the License.
19*5900e8ecSAndrew Rist *
20*5900e8ecSAndrew Rist *************************************************************/
21*5900e8ecSAndrew Rist
22*5900e8ecSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svtools.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <svtools/svmedit.hxx>
28cdf0e10cSrcweir #include <svtools/xtextedt.hxx>
29cdf0e10cSrcweir #include <svtools/editsyntaxhighlighter.hxx>
30cdf0e10cSrcweir #include <svtools/txtattr.hxx>
31cdf0e10cSrcweir
32cdf0e10cSrcweir
MultiLineEditSyntaxHighlight(Window * pParent,WinBits nWinStyle,HighlighterLanguage aLanguage)33cdf0e10cSrcweir MultiLineEditSyntaxHighlight::MultiLineEditSyntaxHighlight( Window* pParent, WinBits nWinStyle,
34cdf0e10cSrcweir HighlighterLanguage aLanguage): MultiLineEdit(pParent,nWinStyle), mbDoBracketHilight(true)
35cdf0e10cSrcweir {
36cdf0e10cSrcweir EnableUpdateData(300);
37cdf0e10cSrcweir aHighlighter.initialize( aLanguage );
38cdf0e10cSrcweir }
39cdf0e10cSrcweir
MultiLineEditSyntaxHighlight(Window * pParent,const ResId & rResId,HighlighterLanguage aLanguage)40cdf0e10cSrcweir MultiLineEditSyntaxHighlight::MultiLineEditSyntaxHighlight( Window* pParent, const ResId& rResId ,
41cdf0e10cSrcweir HighlighterLanguage aLanguage): MultiLineEdit(pParent,rResId), mbDoBracketHilight(true)
42cdf0e10cSrcweir {
43cdf0e10cSrcweir EnableUpdateData(300);
44cdf0e10cSrcweir aHighlighter.initialize( aLanguage );
45cdf0e10cSrcweir }
46cdf0e10cSrcweir
~MultiLineEditSyntaxHighlight()47cdf0e10cSrcweir MultiLineEditSyntaxHighlight::~MultiLineEditSyntaxHighlight()
48cdf0e10cSrcweir {
49cdf0e10cSrcweir }
50cdf0e10cSrcweir
EnableBracketHilight(bool aHilight)51cdf0e10cSrcweir void MultiLineEditSyntaxHighlight::EnableBracketHilight(bool aHilight)
52cdf0e10cSrcweir {
53cdf0e10cSrcweir mbDoBracketHilight = aHilight;
54cdf0e10cSrcweir }
55cdf0e10cSrcweir
IsBracketHilight()56cdf0e10cSrcweir bool MultiLineEditSyntaxHighlight::IsBracketHilight()
57cdf0e10cSrcweir {
58cdf0e10cSrcweir return mbDoBracketHilight;
59cdf0e10cSrcweir }
60cdf0e10cSrcweir
SetText(const String & rNewText)61cdf0e10cSrcweir void MultiLineEditSyntaxHighlight::SetText(const String& rNewText)
62cdf0e10cSrcweir {
63cdf0e10cSrcweir MultiLineEdit::SetText(rNewText);
64cdf0e10cSrcweir UpdateData();
65cdf0e10cSrcweir }
66cdf0e10cSrcweir
DoBracketHilight(sal_uInt16 aKey)67cdf0e10cSrcweir void MultiLineEditSyntaxHighlight::DoBracketHilight(sal_uInt16 aKey)
68cdf0e10cSrcweir {
69cdf0e10cSrcweir TextSelection aCurrentPos = GetTextView()->GetSelection();
70cdf0e10cSrcweir xub_StrLen aStartPos = aCurrentPos.GetStart().GetIndex();
71cdf0e10cSrcweir sal_uLong nStartPara = aCurrentPos.GetStart().GetPara();
72cdf0e10cSrcweir sal_uInt16 aCount = 0;
73cdf0e10cSrcweir int aChar = -1;
74cdf0e10cSrcweir
75cdf0e10cSrcweir switch (aKey)
76cdf0e10cSrcweir {
77cdf0e10cSrcweir case '\'': // no break
78cdf0e10cSrcweir case '"':
79cdf0e10cSrcweir {
80cdf0e10cSrcweir aChar = aKey;
81cdf0e10cSrcweir break;
82cdf0e10cSrcweir }
83cdf0e10cSrcweir case '}' :
84cdf0e10cSrcweir {
85cdf0e10cSrcweir aChar = '{';
86cdf0e10cSrcweir break;
87cdf0e10cSrcweir }
88cdf0e10cSrcweir case ')':
89cdf0e10cSrcweir {
90cdf0e10cSrcweir aChar = '(';
91cdf0e10cSrcweir break;
92cdf0e10cSrcweir }
93cdf0e10cSrcweir case ']':
94cdf0e10cSrcweir {
95cdf0e10cSrcweir aChar = '[';
96cdf0e10cSrcweir break;
97cdf0e10cSrcweir }
98cdf0e10cSrcweir }
99cdf0e10cSrcweir
100cdf0e10cSrcweir if (aChar != -1)
101cdf0e10cSrcweir {
102cdf0e10cSrcweir for (long aPara =nStartPara; aPara>=0;--aPara)
103cdf0e10cSrcweir {
104cdf0e10cSrcweir if ( aStartPos == 0 )
105cdf0e10cSrcweir continue;
106cdf0e10cSrcweir
107cdf0e10cSrcweir String aLine( GetTextEngine()->GetText( aPara ) );
108cdf0e10cSrcweir for (sal_uInt16 i = ((unsigned long)aPara==nStartPara) ? aStartPos-1 : (sal_uInt16)(aLine.Len()-1); i>0; --i)
109cdf0e10cSrcweir {
110cdf0e10cSrcweir if (aLine.GetChar(i)==aChar)
111cdf0e10cSrcweir {
112cdf0e10cSrcweir if (!aCount)
113cdf0e10cSrcweir {
114cdf0e10cSrcweir GetTextEngine()->SetAttrib( TextAttribFontWeight( WEIGHT_ULTRABOLD ), aPara, i, i+1, sal_True );
115cdf0e10cSrcweir GetTextEngine()->SetAttrib( TextAttribFontColor( Color(0,0,0) ), aPara, i, i+1, sal_True );
116cdf0e10cSrcweir GetTextEngine()->SetAttrib( TextAttribFontWeight( WEIGHT_ULTRABOLD ), nStartPara, aStartPos, aStartPos, sal_True );
117cdf0e10cSrcweir GetTextEngine()->SetAttrib( TextAttribFontColor( Color(0,0,0) ), nStartPara, aStartPos, aStartPos, sal_True );
118cdf0e10cSrcweir return;
119cdf0e10cSrcweir }
120cdf0e10cSrcweir else
121cdf0e10cSrcweir aCount--;
122cdf0e10cSrcweir }
123cdf0e10cSrcweir if (aLine.GetChar(i)==aKey)
124cdf0e10cSrcweir aCount++;
125cdf0e10cSrcweir }
126cdf0e10cSrcweir }
127cdf0e10cSrcweir }
128cdf0e10cSrcweir }
129cdf0e10cSrcweir
PreNotify(NotifyEvent & rNEvt)130cdf0e10cSrcweir long MultiLineEditSyntaxHighlight::PreNotify( NotifyEvent& rNEvt )
131cdf0e10cSrcweir {
132cdf0e10cSrcweir if ( mbDoBracketHilight && (rNEvt.GetType() == EVENT_KEYINPUT) )
133cdf0e10cSrcweir DoBracketHilight(rNEvt.GetKeyEvent()->GetCharCode());
134cdf0e10cSrcweir
135cdf0e10cSrcweir return MultiLineEdit::PreNotify(rNEvt);
136cdf0e10cSrcweir }
137cdf0e10cSrcweir
GetColorValue(TokenTypes aToken)138cdf0e10cSrcweir Color MultiLineEditSyntaxHighlight::GetColorValue(TokenTypes aToken)
139cdf0e10cSrcweir {
140cdf0e10cSrcweir Color aColor;
141cdf0e10cSrcweir switch (aHighlighter.GetLanguage())
142cdf0e10cSrcweir {
143cdf0e10cSrcweir case HIGHLIGHT_SQL:
144cdf0e10cSrcweir {
145cdf0e10cSrcweir switch (aToken)
146cdf0e10cSrcweir {
147cdf0e10cSrcweir case TT_IDENTIFIER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLIDENTIFIER).nColor; break;
148cdf0e10cSrcweir case TT_NUMBER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLNUMBER).nColor; break;
149cdf0e10cSrcweir case TT_STRING: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLSTRING).nColor; break;
150cdf0e10cSrcweir case TT_OPERATOR: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLOPERATOR).nColor; break;
151cdf0e10cSrcweir case TT_KEYWORDS: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLKEYWORD).nColor; break;
152cdf0e10cSrcweir case TT_PARAMETER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLPARAMETER).nColor; break;
153cdf0e10cSrcweir case TT_COMMENT: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLCOMMENT).nColor; break;
154cdf0e10cSrcweir default: aColor = Color(0,0,0);
155cdf0e10cSrcweir }
156cdf0e10cSrcweir break;
157cdf0e10cSrcweir }
158cdf0e10cSrcweir case HIGHLIGHT_BASIC:
159cdf0e10cSrcweir {
160cdf0e10cSrcweir switch (aToken)
161cdf0e10cSrcweir {
162cdf0e10cSrcweir case TT_IDENTIFIER: aColor = Color(255,0,0); break;
163cdf0e10cSrcweir case TT_COMMENT: aColor = Color(0,0,45); break;
164cdf0e10cSrcweir case TT_NUMBER: aColor = Color(204,102,204); break;
165cdf0e10cSrcweir case TT_STRING: aColor = Color(0,255,45); break;
166cdf0e10cSrcweir case TT_OPERATOR: aColor = Color(0,0,100); break;
167cdf0e10cSrcweir case TT_KEYWORDS: aColor = Color(0,0,255); break;
168cdf0e10cSrcweir case TT_ERROR : aColor = Color(0,255,255); break;
169cdf0e10cSrcweir default: aColor = Color(0,0,0);
170cdf0e10cSrcweir }
171cdf0e10cSrcweir break;
172cdf0e10cSrcweir }
173cdf0e10cSrcweir default: aColor = Color(0,0,0);
174cdf0e10cSrcweir
175cdf0e10cSrcweir }
176cdf0e10cSrcweir return aColor;
177cdf0e10cSrcweir }
178cdf0e10cSrcweir
UpdateData()179cdf0e10cSrcweir void MultiLineEditSyntaxHighlight::UpdateData()
180cdf0e10cSrcweir {
181cdf0e10cSrcweir // syntax highlighting
182cdf0e10cSrcweir // this must be possible improved by using notifychange correctly
183cdf0e10cSrcweir sal_Bool bTempModified = GetTextEngine()->IsModified();
184cdf0e10cSrcweir for (unsigned int nLine=0; nLine < GetTextEngine()->GetParagraphCount(); nLine++)
185cdf0e10cSrcweir {
186cdf0e10cSrcweir String aLine( GetTextEngine()->GetText( nLine ) );
187cdf0e10cSrcweir Range aChanges = aHighlighter.notifyChange( nLine, 0, &aLine, 1 );
188cdf0e10cSrcweir
189cdf0e10cSrcweir GetTextEngine()->RemoveAttribs( nLine, sal_True );
190cdf0e10cSrcweir HighlightPortions aPortions;
191cdf0e10cSrcweir aHighlighter.getHighlightPortions( nLine, aLine, aPortions );
192cdf0e10cSrcweir for ( size_t i = 0; i < aPortions.size(); i++ )
193cdf0e10cSrcweir {
194cdf0e10cSrcweir HighlightPortion& r = aPortions[i];
195cdf0e10cSrcweir GetTextEngine()->SetAttrib( TextAttribFontColor( GetColorValue(r.tokenType) ), nLine, r.nBegin, r.nEnd, sal_True );
196cdf0e10cSrcweir }
197cdf0e10cSrcweir }
198cdf0e10cSrcweir GetTextView()->ShowCursor( false, true );
199cdf0e10cSrcweir GetTextEngine()->SetModified(bTempModified);
200cdf0e10cSrcweir }
201