1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svtools.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <svtools/svmedit.hxx>
32*cdf0e10cSrcweir #include <svtools/xtextedt.hxx>
33*cdf0e10cSrcweir #include <svtools/editsyntaxhighlighter.hxx>
34*cdf0e10cSrcweir #include <svtools/txtattr.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir MultiLineEditSyntaxHighlight::MultiLineEditSyntaxHighlight( Window* pParent, WinBits nWinStyle,
38*cdf0e10cSrcweir 	HighlighterLanguage aLanguage): MultiLineEdit(pParent,nWinStyle), mbDoBracketHilight(true)
39*cdf0e10cSrcweir {
40*cdf0e10cSrcweir 	EnableUpdateData(300);
41*cdf0e10cSrcweir 	aHighlighter.initialize( aLanguage );
42*cdf0e10cSrcweir }
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir MultiLineEditSyntaxHighlight::MultiLineEditSyntaxHighlight( Window* pParent, const ResId& rResId ,
45*cdf0e10cSrcweir 	HighlighterLanguage aLanguage): MultiLineEdit(pParent,rResId), mbDoBracketHilight(true)
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir 	EnableUpdateData(300);
48*cdf0e10cSrcweir 	aHighlighter.initialize( aLanguage );
49*cdf0e10cSrcweir }
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir MultiLineEditSyntaxHighlight::~MultiLineEditSyntaxHighlight()
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir }
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir void MultiLineEditSyntaxHighlight::EnableBracketHilight(bool aHilight)
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir 	mbDoBracketHilight = aHilight;
58*cdf0e10cSrcweir }
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir bool MultiLineEditSyntaxHighlight::IsBracketHilight()
61*cdf0e10cSrcweir {
62*cdf0e10cSrcweir 	return mbDoBracketHilight;
63*cdf0e10cSrcweir }
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir void MultiLineEditSyntaxHighlight::SetText(const String& rNewText)
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir 	MultiLineEdit::SetText(rNewText);
68*cdf0e10cSrcweir 	UpdateData();
69*cdf0e10cSrcweir }
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir void MultiLineEditSyntaxHighlight::DoBracketHilight(sal_uInt16 aKey)
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir 	TextSelection aCurrentPos = GetTextView()->GetSelection();
74*cdf0e10cSrcweir 	xub_StrLen aStartPos  = aCurrentPos.GetStart().GetIndex();
75*cdf0e10cSrcweir 	sal_uLong nStartPara = aCurrentPos.GetStart().GetPara();
76*cdf0e10cSrcweir 	sal_uInt16 aCount = 0;
77*cdf0e10cSrcweir 	int aChar = -1;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 	switch (aKey)
80*cdf0e10cSrcweir 	{
81*cdf0e10cSrcweir 		case '\'':	// no break
82*cdf0e10cSrcweir 		case '"':
83*cdf0e10cSrcweir 		{
84*cdf0e10cSrcweir 			aChar = aKey;
85*cdf0e10cSrcweir 			break;
86*cdf0e10cSrcweir 		}
87*cdf0e10cSrcweir 		case '}' :
88*cdf0e10cSrcweir 		{
89*cdf0e10cSrcweir 			aChar = '{';
90*cdf0e10cSrcweir 			break;
91*cdf0e10cSrcweir 		}
92*cdf0e10cSrcweir 		case ')':
93*cdf0e10cSrcweir 		{
94*cdf0e10cSrcweir 			aChar = '(';
95*cdf0e10cSrcweir 			break;
96*cdf0e10cSrcweir 		}
97*cdf0e10cSrcweir 		case ']':
98*cdf0e10cSrcweir 		{
99*cdf0e10cSrcweir 			aChar = '[';
100*cdf0e10cSrcweir 			break;
101*cdf0e10cSrcweir 		}
102*cdf0e10cSrcweir 	}
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	if (aChar != -1)
105*cdf0e10cSrcweir 	{
106*cdf0e10cSrcweir 		for (long aPara =nStartPara; aPara>=0;--aPara)
107*cdf0e10cSrcweir 		{
108*cdf0e10cSrcweir             if ( aStartPos == 0 )
109*cdf0e10cSrcweir                 continue;
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 			String aLine( GetTextEngine()->GetText( aPara ) );
112*cdf0e10cSrcweir 			for (sal_uInt16 i = ((unsigned long)aPara==nStartPara) ? aStartPos-1 : (sal_uInt16)(aLine.Len()-1); i>0; --i)
113*cdf0e10cSrcweir 			{
114*cdf0e10cSrcweir 				if (aLine.GetChar(i)==aChar)
115*cdf0e10cSrcweir 				{
116*cdf0e10cSrcweir 					if (!aCount)
117*cdf0e10cSrcweir 					{
118*cdf0e10cSrcweir 						GetTextEngine()->SetAttrib( TextAttribFontWeight( WEIGHT_ULTRABOLD ), aPara, i, i+1, sal_True );
119*cdf0e10cSrcweir 						GetTextEngine()->SetAttrib( TextAttribFontColor( Color(0,0,0) ), aPara, i, i+1, sal_True );
120*cdf0e10cSrcweir 						GetTextEngine()->SetAttrib( TextAttribFontWeight( WEIGHT_ULTRABOLD ), nStartPara, aStartPos, aStartPos, sal_True );
121*cdf0e10cSrcweir 						GetTextEngine()->SetAttrib( TextAttribFontColor( Color(0,0,0) ), nStartPara, aStartPos, aStartPos, sal_True );
122*cdf0e10cSrcweir 						return;
123*cdf0e10cSrcweir 					}
124*cdf0e10cSrcweir 					else
125*cdf0e10cSrcweir 						aCount--;
126*cdf0e10cSrcweir 				}
127*cdf0e10cSrcweir 				if (aLine.GetChar(i)==aKey)
128*cdf0e10cSrcweir 					aCount++;
129*cdf0e10cSrcweir 			}
130*cdf0e10cSrcweir 		}
131*cdf0e10cSrcweir 	}
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir long MultiLineEditSyntaxHighlight::PreNotify( NotifyEvent& rNEvt )
135*cdf0e10cSrcweir {
136*cdf0e10cSrcweir 	if ( mbDoBracketHilight && (rNEvt.GetType() == EVENT_KEYINPUT) )
137*cdf0e10cSrcweir 		DoBracketHilight(rNEvt.GetKeyEvent()->GetCharCode());
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 	return MultiLineEdit::PreNotify(rNEvt);
140*cdf0e10cSrcweir }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir Color MultiLineEditSyntaxHighlight::GetColorValue(TokenTypes aToken)
143*cdf0e10cSrcweir {
144*cdf0e10cSrcweir 	Color aColor;
145*cdf0e10cSrcweir 	switch (aHighlighter.GetLanguage())
146*cdf0e10cSrcweir 	{
147*cdf0e10cSrcweir 		case HIGHLIGHT_SQL:
148*cdf0e10cSrcweir 		{
149*cdf0e10cSrcweir 			switch (aToken)
150*cdf0e10cSrcweir 			{
151*cdf0e10cSrcweir 				case TT_IDENTIFIER:	aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLIDENTIFIER).nColor; break;
152*cdf0e10cSrcweir 				case TT_NUMBER:		aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLNUMBER).nColor; break;
153*cdf0e10cSrcweir 				case TT_STRING:		aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLSTRING).nColor; break;
154*cdf0e10cSrcweir 				case TT_OPERATOR:	aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLOPERATOR).nColor; break;
155*cdf0e10cSrcweir 				case TT_KEYWORDS:	aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLKEYWORD).nColor; break;
156*cdf0e10cSrcweir 				case TT_PARAMETER:  aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLPARAMETER).nColor; break;
157*cdf0e10cSrcweir 				case TT_COMMENT:	aColor = (ColorData)m_aColorConfig.GetColorValue(svtools::SQLCOMMENT).nColor; break;
158*cdf0e10cSrcweir 				default:			aColor = Color(0,0,0);
159*cdf0e10cSrcweir 			}
160*cdf0e10cSrcweir 			break;
161*cdf0e10cSrcweir 		}
162*cdf0e10cSrcweir 		case HIGHLIGHT_BASIC:
163*cdf0e10cSrcweir 		{
164*cdf0e10cSrcweir 			switch (aToken)
165*cdf0e10cSrcweir 			{
166*cdf0e10cSrcweir 				case TT_IDENTIFIER:	aColor = Color(255,0,0); break;
167*cdf0e10cSrcweir 				case TT_COMMENT:	aColor = Color(0,0,45); break;
168*cdf0e10cSrcweir 				case TT_NUMBER:		aColor = Color(204,102,204); break;
169*cdf0e10cSrcweir 				case TT_STRING:		aColor = Color(0,255,45); break;
170*cdf0e10cSrcweir 				case TT_OPERATOR:	aColor = Color(0,0,100); break;
171*cdf0e10cSrcweir 				case TT_KEYWORDS:	aColor = Color(0,0,255); break;
172*cdf0e10cSrcweir 				case TT_ERROR :		aColor = Color(0,255,255); break;
173*cdf0e10cSrcweir 				default:			aColor = Color(0,0,0);
174*cdf0e10cSrcweir 			}
175*cdf0e10cSrcweir 			break;
176*cdf0e10cSrcweir 		}
177*cdf0e10cSrcweir 		default: aColor = Color(0,0,0);
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 	}
180*cdf0e10cSrcweir 	return aColor;
181*cdf0e10cSrcweir }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir void MultiLineEditSyntaxHighlight::UpdateData()
184*cdf0e10cSrcweir {
185*cdf0e10cSrcweir 	// syntax highlighting
186*cdf0e10cSrcweir 	// this must be possible improved by using notifychange correctly
187*cdf0e10cSrcweir 	sal_Bool bTempModified = GetTextEngine()->IsModified();
188*cdf0e10cSrcweir 	for (unsigned int nLine=0; nLine < GetTextEngine()->GetParagraphCount(); nLine++)
189*cdf0e10cSrcweir 	{
190*cdf0e10cSrcweir 		String aLine( GetTextEngine()->GetText( nLine ) );
191*cdf0e10cSrcweir 		Range aChanges = aHighlighter.notifyChange( nLine, 0, &aLine, 1 );
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 		GetTextEngine()->RemoveAttribs( nLine, sal_True );
194*cdf0e10cSrcweir 		HighlightPortions aPortions;
195*cdf0e10cSrcweir 		aHighlighter.getHighlightPortions( nLine, aLine, aPortions );
196*cdf0e10cSrcweir 		for ( size_t i = 0; i < aPortions.size(); i++ )
197*cdf0e10cSrcweir 		{
198*cdf0e10cSrcweir 			HighlightPortion& r = aPortions[i];
199*cdf0e10cSrcweir 			GetTextEngine()->SetAttrib( TextAttribFontColor( GetColorValue(r.tokenType) ), nLine, r.nBegin, r.nEnd, sal_True );
200*cdf0e10cSrcweir 		}
201*cdf0e10cSrcweir 	}
202*cdf0e10cSrcweir 	GetTextView()->ShowCursor( false, true );
203*cdf0e10cSrcweir 	GetTextEngine()->SetModified(bTempModified);
204*cdf0e10cSrcweir }
205