Linux x86 build fix
[LibreOffice.git] / svtools / source / edit / editsyntaxhighlighter.cxx
blob6e6965890d4e94154d76aee3ea888995199c4ca3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <svtools/svmedit.hxx>
22 #include <vcl/xtextedt.hxx>
23 #include <vcl/builderfactory.hxx>
24 #include <svtools/editsyntaxhighlighter.hxx>
25 #include <vcl/txtattr.hxx>
28 MultiLineEditSyntaxHighlight::MultiLineEditSyntaxHighlight( vcl::Window* pParent, WinBits nWinStyle,
29 HighlighterLanguage aLanguage): MultiLineEdit(pParent,nWinStyle), mbDoBracketHilight(true), aHighlighter(aLanguage)
31 EnableUpdateData(300);
34 VCL_BUILDER_FACTORY(MultiLineEditSyntaxHighlight)
36 void MultiLineEditSyntaxHighlight::SetText(const OUString& rNewText)
38 MultiLineEdit::SetText(rNewText);
39 UpdateData();
42 void MultiLineEditSyntaxHighlight::DoBracketHilight(sal_uInt16 nKey)
44 TextSelection aCurrentPos = GetTextView()->GetSelection();
45 sal_Int32 nStartPos = aCurrentPos.GetStart().GetIndex();
46 sal_Int32 nStartPara = aCurrentPos.GetStart().GetPara();
47 sal_uInt16 nCount = 0;
48 int nChar = -1;
50 switch (nKey)
52 case '\'': // no break
53 case '"':
55 nChar = nKey;
56 break;
58 case '}' :
60 nChar = '{';
61 break;
63 case ')':
65 nChar = '(';
66 break;
68 case ']':
70 nChar = '[';
71 break;
75 if (nChar != -1)
77 for (sal_Int32 nPara = nStartPara; nPara >= 0; --nPara)
79 if (nStartPos == 0)
80 continue;
82 OUString aLine( GetTextEngine()->GetText( nPara ) );
84 if (aLine.isEmpty())
85 continue;
87 for (sal_Int32 i = (nPara==nStartPara) ? nStartPos-1 : aLine.getLength()-1; i>0; --i)
89 if (aLine[i] == nChar)
91 if (!nCount)
93 GetTextEngine()->SetAttrib( TextAttribFontWeight( WEIGHT_ULTRABOLD ), nPara, i, i+1, true );
94 GetTextEngine()->SetAttrib( TextAttribFontColor( Color(0,0,0) ), nPara, i, i+1, true );
95 GetTextEngine()->SetAttrib( TextAttribFontWeight( WEIGHT_ULTRABOLD ), nStartPara, nStartPos, nStartPos, true );
96 GetTextEngine()->SetAttrib( TextAttribFontColor( Color(0,0,0) ), nStartPara, nStartPos, nStartPos, true );
97 return;
99 else
100 --nCount;
102 if (aLine[i] == nKey)
103 ++nCount;
109 bool MultiLineEditSyntaxHighlight::PreNotify( NotifyEvent& rNEvt )
111 if ( mbDoBracketHilight && (rNEvt.GetType() == MouseNotifyEvent::KEYINPUT) )
112 DoBracketHilight(rNEvt.GetKeyEvent()->GetCharCode());
114 return MultiLineEdit::PreNotify(rNEvt);
117 Color MultiLineEditSyntaxHighlight::GetColorValue(TokenTypes aToken)
119 Color aColor;
120 switch (aHighlighter.GetLanguage())
122 case HIGHLIGHT_SQL:
124 switch (aToken)
126 case TT_IDENTIFIER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLIDENTIFIER).nColor; break;
127 case TT_NUMBER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLNUMBER).nColor; break;
128 case TT_STRING: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLSTRING).nColor; break;
129 case TT_OPERATOR: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLOPERATOR).nColor; break;
130 case TT_KEYWORDS: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLKEYWORD).nColor; break;
131 case TT_PARAMETER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLPARAMETER).nColor; break;
132 case TT_COMMENT: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLCOMMENT).nColor; break;
133 default: aColor = Color(0,0,0);
135 break;
137 case HIGHLIGHT_BASIC:
139 switch (aToken)
141 case TT_IDENTIFIER: aColor = Color(255,0,0); break;
142 case TT_COMMENT: aColor = Color(0,0,45); break;
143 case TT_NUMBER: aColor = Color(204,102,204); break;
144 case TT_STRING: aColor = Color(0,255,45); break;
145 case TT_OPERATOR: aColor = Color(0,0,100); break;
146 case TT_KEYWORDS: aColor = Color(0,0,255); break;
147 case TT_ERROR : aColor = Color(0,255,255); break;
148 default: aColor = Color(0,0,0);
150 break;
152 default: aColor = Color(0,0,0);
155 return aColor;
158 void MultiLineEditSyntaxHighlight::UpdateData()
160 // syntax highlighting
161 // this must be possible improved by using notifychange correctly
162 bool bTempModified = GetTextEngine()->IsModified();
163 for (unsigned int nLine=0; nLine < GetTextEngine()->GetParagraphCount(); nLine++)
165 OUString aLine( GetTextEngine()->GetText( nLine ) );
166 GetTextEngine()->RemoveAttribs( nLine, true );
167 std::vector<HighlightPortion> aPortions;
168 aHighlighter.getHighlightPortions( aLine, aPortions );
169 for (std::vector<HighlightPortion>::iterator i(aPortions.begin());
170 i != aPortions.end(); ++i)
172 GetTextEngine()->SetAttrib( TextAttribFontColor( GetColorValue(i->tokenType) ), nLine, i->nBegin, i->nEnd, true );
175 GetTextView()->ShowCursor( false, true );
176 GetTextEngine()->SetModified(bTempModified);
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */