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 <vcl/builder.hxx>
24 : #include <svtools/editsyntaxhighlighter.hxx>
25 : #include <vcl/txtattr.hxx>
26 :
27 :
28 0 : MultiLineEditSyntaxHighlight::MultiLineEditSyntaxHighlight( vcl::Window* pParent, WinBits nWinStyle,
29 0 : HighlighterLanguage aLanguage): MultiLineEdit(pParent,nWinStyle), mbDoBracketHilight(true), aHighlighter(aLanguage)
30 : {
31 0 : EnableUpdateData(300);
32 0 : }
33 :
34 0 : extern "C" SAL_DLLPUBLIC_EXPORT vcl::Window* SAL_CALL makeMultiLineEditSyntaxHighlight(vcl::Window *pParent, VclBuilder::stringmap &)
35 : {
36 0 : return new MultiLineEditSyntaxHighlight(pParent);
37 : }
38 :
39 0 : MultiLineEditSyntaxHighlight::~MultiLineEditSyntaxHighlight()
40 : {
41 0 : }
42 :
43 0 : void MultiLineEditSyntaxHighlight::SetText(const OUString& rNewText)
44 : {
45 0 : MultiLineEdit::SetText(rNewText);
46 0 : UpdateData();
47 0 : }
48 :
49 0 : void MultiLineEditSyntaxHighlight::DoBracketHilight(sal_uInt16 nKey)
50 : {
51 0 : TextSelection aCurrentPos = GetTextView()->GetSelection();
52 0 : sal_Int32 nStartPos = aCurrentPos.GetStart().GetIndex();
53 0 : sal_Int32 nStartPara = aCurrentPos.GetStart().GetPara();
54 0 : sal_uInt16 nCount = 0;
55 0 : int nChar = -1;
56 :
57 0 : switch (nKey)
58 : {
59 : case '\'': // no break
60 : case '"':
61 : {
62 0 : nChar = nKey;
63 0 : break;
64 : }
65 : case '}' :
66 : {
67 0 : nChar = '{';
68 0 : break;
69 : }
70 : case ')':
71 : {
72 0 : nChar = '(';
73 0 : break;
74 : }
75 : case ']':
76 : {
77 0 : nChar = '[';
78 0 : break;
79 : }
80 : }
81 :
82 0 : if (nChar != -1)
83 : {
84 0 : for (sal_Int32 nPara = nStartPara; nPara >= 0; --nPara)
85 : {
86 0 : if (nStartPos == 0)
87 0 : continue;
88 :
89 0 : OUString aLine( GetTextEngine()->GetText( nPara ) );
90 :
91 0 : if (aLine.isEmpty())
92 0 : continue;
93 :
94 0 : for (sal_Int32 i = (nPara==nStartPara) ? nStartPos-1 : aLine.getLength()-1; i>0; --i)
95 : {
96 0 : if (aLine[i] == nChar)
97 : {
98 0 : if (!nCount)
99 : {
100 0 : GetTextEngine()->SetAttrib( TextAttribFontWeight( WEIGHT_ULTRABOLD ), nPara, i, i+1, true );
101 0 : GetTextEngine()->SetAttrib( TextAttribFontColor( Color(0,0,0) ), nPara, i, i+1, true );
102 0 : GetTextEngine()->SetAttrib( TextAttribFontWeight( WEIGHT_ULTRABOLD ), nStartPara, nStartPos, nStartPos, true );
103 0 : GetTextEngine()->SetAttrib( TextAttribFontColor( Color(0,0,0) ), nStartPara, nStartPos, nStartPos, true );
104 0 : return;
105 : }
106 : else
107 0 : --nCount;
108 : }
109 0 : if (aLine[i] == nKey)
110 0 : ++nCount;
111 : }
112 0 : }
113 : }
114 : }
115 :
116 0 : bool MultiLineEditSyntaxHighlight::PreNotify( NotifyEvent& rNEvt )
117 : {
118 0 : if ( mbDoBracketHilight && (rNEvt.GetType() == EVENT_KEYINPUT) )
119 0 : DoBracketHilight(rNEvt.GetKeyEvent()->GetCharCode());
120 :
121 0 : return MultiLineEdit::PreNotify(rNEvt);
122 : }
123 :
124 0 : Color MultiLineEditSyntaxHighlight::GetColorValue(TokenTypes aToken)
125 : {
126 0 : Color aColor;
127 0 : switch (aHighlighter.GetLanguage())
128 : {
129 : case HIGHLIGHT_SQL:
130 : {
131 0 : switch (aToken)
132 : {
133 0 : case TT_IDENTIFIER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLIDENTIFIER).nColor; break;
134 0 : case TT_NUMBER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLNUMBER).nColor; break;
135 0 : case TT_STRING: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLSTRING).nColor; break;
136 0 : case TT_OPERATOR: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLOPERATOR).nColor; break;
137 0 : case TT_KEYWORDS: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLKEYWORD).nColor; break;
138 0 : case TT_PARAMETER: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLPARAMETER).nColor; break;
139 0 : case TT_COMMENT: aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLCOMMENT).nColor; break;
140 0 : default: aColor = Color(0,0,0);
141 : }
142 0 : break;
143 : }
144 : case HIGHLIGHT_BASIC:
145 : {
146 0 : switch (aToken)
147 : {
148 0 : case TT_IDENTIFIER: aColor = Color(255,0,0); break;
149 0 : case TT_COMMENT: aColor = Color(0,0,45); break;
150 0 : case TT_NUMBER: aColor = Color(204,102,204); break;
151 0 : case TT_STRING: aColor = Color(0,255,45); break;
152 0 : case TT_OPERATOR: aColor = Color(0,0,100); break;
153 0 : case TT_KEYWORDS: aColor = Color(0,0,255); break;
154 0 : case TT_ERROR : aColor = Color(0,255,255); break;
155 0 : default: aColor = Color(0,0,0);
156 : }
157 0 : break;
158 : }
159 0 : default: aColor = Color(0,0,0);
160 :
161 : }
162 0 : return aColor;
163 : }
164 :
165 0 : void MultiLineEditSyntaxHighlight::UpdateData()
166 : {
167 : // syntax highlighting
168 : // this must be possible improved by using notifychange correctly
169 0 : bool bTempModified = GetTextEngine()->IsModified();
170 0 : for (unsigned int nLine=0; nLine < GetTextEngine()->GetParagraphCount(); nLine++)
171 : {
172 0 : OUString aLine( GetTextEngine()->GetText( nLine ) );
173 0 : GetTextEngine()->RemoveAttribs( nLine, true );
174 0 : std::vector<HighlightPortion> aPortions;
175 0 : aHighlighter.getHighlightPortions( aLine, aPortions );
176 0 : for (std::vector<HighlightPortion>::iterator i(aPortions.begin());
177 0 : i != aPortions.end(); ++i)
178 : {
179 0 : GetTextEngine()->SetAttrib( TextAttribFontColor( GetColorValue(i->tokenType) ), nLine, i->nBegin, i->nEnd, true );
180 : }
181 0 : }
182 0 : GetTextView()->ShowCursor( false, true );
183 0 : GetTextEngine()->SetModified(bTempModified);
184 1227 : }
185 :
186 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|