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 String& rNewText)
46 : {
47 0 : MultiLineEdit::SetText(rNewText);
48 0 : UpdateData();
49 0 : }
50 :
51 0 : void MultiLineEditSyntaxHighlight::DoBracketHilight(sal_uInt16 aKey)
52 : {
53 0 : TextSelection aCurrentPos = GetTextView()->GetSelection();
54 0 : xub_StrLen aStartPos = aCurrentPos.GetStart().GetIndex();
55 0 : sal_uLong nStartPara = aCurrentPos.GetStart().GetPara();
56 0 : sal_uInt16 aCount = 0;
57 0 : int aChar = -1;
58 :
59 0 : switch (aKey)
60 : {
61 : case '\'': // no break
62 : case '"':
63 : {
64 0 : aChar = aKey;
65 0 : break;
66 : }
67 : case '}' :
68 : {
69 0 : aChar = '{';
70 0 : break;
71 : }
72 : case ')':
73 : {
74 0 : aChar = '(';
75 0 : break;
76 : }
77 : case ']':
78 : {
79 0 : aChar = '[';
80 0 : break;
81 : }
82 : }
83 :
84 0 : if (aChar != -1)
85 : {
86 0 : for (long aPara =nStartPara; aPara>=0;--aPara)
87 : {
88 0 : if ( aStartPos == 0 )
89 0 : continue;
90 :
91 0 : String aLine( GetTextEngine()->GetText( aPara ) );
92 0 : for (sal_uInt16 i = ((unsigned long)aPara==nStartPara) ? aStartPos-1 : (sal_uInt16)(aLine.Len()-1); i>0; --i)
93 : {
94 0 : if (aLine.GetChar(i)==aChar)
95 : {
96 0 : if (!aCount)
97 : {
98 0 : GetTextEngine()->SetAttrib( TextAttribFontWeight( WEIGHT_ULTRABOLD ), aPara, i, i+1, sal_True );
99 0 : GetTextEngine()->SetAttrib( TextAttribFontColor( Color(0,0,0) ), aPara, i, i+1, sal_True );
100 0 : GetTextEngine()->SetAttrib( TextAttribFontWeight( WEIGHT_ULTRABOLD ), nStartPara, aStartPos, aStartPos, sal_True );
101 0 : GetTextEngine()->SetAttrib( TextAttribFontColor( Color(0,0,0) ), nStartPara, aStartPos, aStartPos, sal_True );
102 0 : return;
103 : }
104 : else
105 0 : aCount--;
106 : }
107 0 : if (aLine.GetChar(i)==aKey)
108 0 : aCount++;
109 : }
110 0 : }
111 : }
112 : }
113 :
114 0 : long MultiLineEditSyntaxHighlight::PreNotify( NotifyEvent& rNEvt )
115 : {
116 0 : if ( mbDoBracketHilight && (rNEvt.GetType() == EVENT_KEYINPUT) )
117 0 : DoBracketHilight(rNEvt.GetKeyEvent()->GetCharCode());
118 :
119 0 : return MultiLineEdit::PreNotify(rNEvt);
120 : }
121 :
122 0 : Color MultiLineEditSyntaxHighlight::GetColorValue(TokenTypes aToken)
123 : {
124 0 : Color aColor;
125 0 : switch (aHighlighter.GetLanguage())
126 : {
127 : case HIGHLIGHT_SQL:
128 : {
129 0 : switch (aToken)
130 : {
131 0 : case TT_IDENTIFIER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLIDENTIFIER).nColor; break;
132 0 : case TT_NUMBER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLNUMBER).nColor; break;
133 0 : case TT_STRING: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLSTRING).nColor; break;
134 0 : case TT_OPERATOR: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLOPERATOR).nColor; break;
135 0 : case TT_KEYWORDS: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLKEYWORD).nColor; break;
136 0 : case TT_PARAMETER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLPARAMETER).nColor; break;
137 0 : case TT_COMMENT: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLCOMMENT).nColor; break;
138 0 : default: aColor = Color(0,0,0);
139 : }
140 0 : break;
141 : }
142 : case HIGHLIGHT_BASIC:
143 : {
144 0 : switch (aToken)
145 : {
146 0 : case TT_IDENTIFIER: aColor = Color(255,0,0); break;
147 0 : case TT_COMMENT: aColor = Color(0,0,45); break;
148 0 : case TT_NUMBER: aColor = Color(204,102,204); break;
149 0 : case TT_STRING: aColor = Color(0,255,45); break;
150 0 : case TT_OPERATOR: aColor = Color(0,0,100); break;
151 0 : case TT_KEYWORDS: aColor = Color(0,0,255); break;
152 0 : case TT_ERROR : aColor = Color(0,255,255); break;
153 0 : default: aColor = Color(0,0,0);
154 : }
155 0 : break;
156 : }
157 0 : default: aColor = Color(0,0,0);
158 :
159 : }
160 0 : return aColor;
161 : }
162 :
163 0 : void MultiLineEditSyntaxHighlight::UpdateData()
164 : {
165 : // syntax highlighting
166 : // this must be possible improved by using notifychange correctly
167 0 : sal_Bool bTempModified = GetTextEngine()->IsModified();
168 0 : for (unsigned int nLine=0; nLine < GetTextEngine()->GetParagraphCount(); nLine++)
169 : {
170 0 : String aLine( GetTextEngine()->GetText( nLine ) );
171 0 : aHighlighter.notifyChange( nLine, 0, &aLine, 1 );
172 :
173 0 : GetTextEngine()->RemoveAttribs( nLine, sal_True );
174 0 : HighlightPortions aPortions;
175 0 : aHighlighter.getHighlightPortions( nLine, aLine, aPortions );
176 0 : for ( size_t i = 0; i < aPortions.size(); i++ )
177 : {
178 0 : HighlightPortion& r = aPortions[i];
179 0 : GetTextEngine()->SetAttrib( TextAttribFontColor( GetColorValue(r.tokenType) ), nLine, r.nBegin, r.nEnd, sal_True );
180 : }
181 0 : }
182 0 : GetTextView()->ShowCursor( false, true );
183 0 : GetTextEngine()->SetModified(bTempModified);
184 0 : }
185 :
186 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|