Update ooo320-m1
[ooovba.git] / svtools / source / edit / editsyntaxhighlighter.cxx
blob4319fd5864328f1e3258d136b8d2d9ed4ae6f7a8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: editsyntaxhighlighter.cxx,v $
10 * $Revision: 1.44.108.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #include <svtools/svmedit.hxx>
35 #include <svtools/xtextedt.hxx>
36 #include <svtools/editsyntaxhighlighter.hxx>
37 #include "../../inc/txtattr.hxx"
40 MultiLineEditSyntaxHighlight::MultiLineEditSyntaxHighlight( Window* pParent, WinBits nWinStyle,
41 HighlighterLanguage aLanguage): MultiLineEdit(pParent,nWinStyle), mbDoBracketHilight(true)
43 EnableUpdateData(300);
44 aHighlighter.initialize( aLanguage );
47 MultiLineEditSyntaxHighlight::MultiLineEditSyntaxHighlight( Window* pParent, const ResId& rResId ,
48 HighlighterLanguage aLanguage): MultiLineEdit(pParent,rResId), mbDoBracketHilight(true)
50 EnableUpdateData(300);
51 aHighlighter.initialize( aLanguage );
54 MultiLineEditSyntaxHighlight::~MultiLineEditSyntaxHighlight()
58 void MultiLineEditSyntaxHighlight::EnableBracketHilight(bool aHilight)
60 mbDoBracketHilight = aHilight;
63 bool MultiLineEditSyntaxHighlight::IsBracketHilight()
65 return mbDoBracketHilight;
68 void MultiLineEditSyntaxHighlight::SetText(const String& rNewText)
70 MultiLineEdit::SetText(rNewText);
71 UpdateData();
74 void MultiLineEditSyntaxHighlight::DoBracketHilight(USHORT aKey)
76 TextSelection aCurrentPos = GetTextView()->GetSelection();
77 xub_StrLen aStartPos = aCurrentPos.GetStart().GetIndex();
78 ULONG nStartPara = aCurrentPos.GetStart().GetPara();
79 USHORT aCount = 0;
80 int aChar = -1;
82 switch (aKey)
84 case '\'': // no break
85 case '"':
87 aChar = aKey;
88 break;
90 case '}' :
92 aChar = '{';
93 break;
95 case ')':
97 aChar = '(';
98 break;
100 case ']':
102 aChar = '[';
103 break;
107 if (aChar != -1)
109 for (long aPara =nStartPara; aPara>=0;--aPara)
111 if ( aStartPos == 0 )
112 continue;
114 String aLine( GetTextEngine()->GetText( aPara ) );
115 for (USHORT i = ((unsigned long)aPara==nStartPara) ? aStartPos-1 : (USHORT)(aLine.Len()-1); i>0; --i)
117 if (aLine.GetChar(i)==aChar)
119 if (!aCount)
121 GetTextEngine()->SetAttrib( TextAttribFontWeight( WEIGHT_ULTRABOLD ), aPara, i, i+1, TRUE );
122 GetTextEngine()->SetAttrib( TextAttribFontColor( Color(0,0,0) ), aPara, i, i+1, TRUE );
123 GetTextEngine()->SetAttrib( TextAttribFontWeight( WEIGHT_ULTRABOLD ), nStartPara, aStartPos, aStartPos, TRUE );
124 GetTextEngine()->SetAttrib( TextAttribFontColor( Color(0,0,0) ), nStartPara, aStartPos, aStartPos, TRUE );
125 return;
127 else
128 aCount--;
130 if (aLine.GetChar(i)==aKey)
131 aCount++;
137 long MultiLineEditSyntaxHighlight::PreNotify( NotifyEvent& rNEvt )
139 if ( mbDoBracketHilight && (rNEvt.GetType() == EVENT_KEYINPUT) )
140 DoBracketHilight(rNEvt.GetKeyEvent()->GetCharCode());
142 return MultiLineEdit::PreNotify(rNEvt);
145 Color MultiLineEditSyntaxHighlight::GetColorValue(TokenTypes aToken)
147 Color aColor;
148 switch (aHighlighter.GetLanguage())
150 case HIGHLIGHT_SQL:
152 switch (aToken)
154 case TT_IDENTIFIER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLIDENTIFIER).nColor; break;
155 case TT_NUMBER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLNUMBER).nColor; break;
156 case TT_STRING: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLSTRING).nColor; break;
157 case TT_OPERATOR: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLOPERATOR).nColor; break;
158 case TT_KEYWORDS: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLKEYWORD).nColor; break;
159 case TT_PARAMETER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLPARAMETER).nColor; break;
160 case TT_COMMENT: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLCOMMENT).nColor; break;
161 default: aColor = Color(0,0,0);
163 break;
165 case HIGHLIGHT_BASIC:
167 switch (aToken)
169 case TT_IDENTIFIER: aColor = Color(255,0,0); break;
170 case TT_COMMENT: aColor = Color(0,0,45); break;
171 case TT_NUMBER: aColor = Color(204,102,204); break;
172 case TT_STRING: aColor = Color(0,255,45); break;
173 case TT_OPERATOR: aColor = Color(0,0,100); break;
174 case TT_KEYWORDS: aColor = Color(0,0,255); break;
175 case TT_ERROR : aColor = Color(0,255,255); break;
176 default: aColor = Color(0,0,0);
178 break;
180 default: aColor = Color(0,0,0);
183 return aColor;
186 void MultiLineEditSyntaxHighlight::UpdateData()
188 // syntax highlighting
189 // this must be possible improved by using notifychange correctly
190 BOOL bTempModified = GetTextEngine()->IsModified();
191 for (unsigned int nLine=0; nLine < GetTextEngine()->GetParagraphCount(); nLine++)
193 String aLine( GetTextEngine()->GetText( nLine ) );
194 Range aChanges = aHighlighter.notifyChange( nLine, 0, &aLine, 1 );
196 GetTextEngine()->RemoveAttribs( nLine, TRUE );
197 HighlightPortions aPortions;
198 aHighlighter.getHighlightPortions( nLine, aLine, aPortions );
199 for ( USHORT i = 0; i < aPortions.Count(); i++ )
201 HighlightPortion& r = aPortions[i];
202 GetTextEngine()->SetAttrib( TextAttribFontColor( GetColorValue(r.tokenType) ), nLine, r.nBegin, r.nEnd, TRUE );
205 GetTextView()->ShowCursor( false, true );
206 GetTextEngine()->SetModified(bTempModified);