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