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 <vcl/xtextedt.hxx>
22 : #include <vcl/svapp.hxx> // International
23 : #include <unotools/textsearch.hxx>
24 : #include <com/sun/star/util/SearchOptions.hpp>
25 : #include <com/sun/star/util/SearchFlags.hpp>
26 :
27 : using namespace ::com::sun::star;
28 :
29 :
30 45 : ExtTextEngine::ExtTextEngine() : maGroupChars(OUString("(){}[]"))
31 : {
32 45 : }
33 :
34 90 : ExtTextEngine::~ExtTextEngine()
35 : {
36 90 : }
37 :
38 0 : TextSelection ExtTextEngine::MatchGroup( const TextPaM& rCursor ) const
39 : {
40 0 : TextSelection aSel( rCursor );
41 0 : sal_uInt16 nPos = rCursor.GetIndex();
42 0 : sal_uLong nPara = rCursor.GetPara();
43 0 : sal_uLong nParas = GetParagraphCount();
44 0 : if ( ( nPara < nParas ) && ( nPos < GetTextLen( nPara ) ) )
45 : {
46 0 : sal_uInt16 nMatchChar = maGroupChars.Search( GetText( rCursor.GetPara() ).GetChar( nPos ) );
47 0 : if ( nMatchChar != STRING_NOTFOUND )
48 : {
49 0 : if ( ( nMatchChar % 2 ) == 0 )
50 : {
51 : // search forwards
52 0 : sal_Unicode nSC = maGroupChars.GetChar( nMatchChar );
53 0 : sal_Unicode nEC = maGroupChars.GetChar( nMatchChar+1 );
54 :
55 0 : sal_uInt16 nCur = nPos+1;
56 0 : sal_uInt16 nLevel = 1;
57 0 : while ( nLevel && ( nPara < nParas ) )
58 : {
59 0 : XubString aStr = GetText( nPara );
60 0 : while ( nCur < aStr.Len() )
61 : {
62 0 : if ( aStr.GetChar( nCur ) == nSC )
63 0 : nLevel++;
64 0 : else if ( aStr.GetChar( nCur ) == nEC )
65 : {
66 0 : nLevel--;
67 0 : if ( !nLevel )
68 0 : break; // while nCur...
69 : }
70 0 : nCur++;
71 : }
72 :
73 0 : if ( nLevel )
74 : {
75 0 : nPara++;
76 0 : nCur = 0;
77 : }
78 0 : }
79 0 : if ( nLevel == 0 ) // found
80 : {
81 0 : aSel.GetStart() = rCursor;
82 0 : aSel.GetEnd() = TextPaM( nPara, nCur+1 );
83 : }
84 : }
85 : else
86 : {
87 : // search backwards
88 0 : sal_Unicode nEC = maGroupChars.GetChar( nMatchChar );
89 0 : sal_Unicode nSC = maGroupChars.GetChar( nMatchChar-1 );
90 :
91 0 : sal_uInt16 nCur = rCursor.GetIndex()-1;
92 0 : sal_uInt16 nLevel = 1;
93 0 : while ( nLevel )
94 : {
95 0 : if ( GetTextLen( nPara ) )
96 : {
97 0 : XubString aStr = GetText( nPara );
98 0 : while ( nCur )
99 : {
100 0 : if ( aStr.GetChar( nCur ) == nSC )
101 : {
102 0 : nLevel--;
103 0 : if ( !nLevel )
104 0 : break; // while nCur...
105 : }
106 0 : else if ( aStr.GetChar( nCur ) == nEC )
107 0 : nLevel++;
108 :
109 0 : nCur--;
110 0 : }
111 : }
112 :
113 0 : if ( nLevel )
114 : {
115 0 : if ( nPara )
116 : {
117 0 : nPara--;
118 0 : nCur = GetTextLen( nPara )-1; // no matter if negativ, as if Len()
119 : }
120 : else
121 0 : break;
122 : }
123 : }
124 :
125 0 : if ( nLevel == 0 ) // found
126 : {
127 0 : aSel.GetStart() = rCursor;
128 0 : aSel.GetStart().GetIndex()++; // behind the char
129 0 : aSel.GetEnd() = TextPaM( nPara, nCur );
130 : }
131 : }
132 : }
133 : }
134 0 : return aSel;
135 : }
136 :
137 0 : sal_Bool ExtTextEngine::Search( TextSelection& rSel, const util::SearchOptions& rSearchOptions, sal_Bool bForward )
138 : {
139 0 : TextSelection aSel( rSel );
140 0 : aSel.Justify();
141 :
142 0 : bool bSearchInSelection = (0 != (rSearchOptions.searchFlag & util::SearchFlags::REG_NOT_BEGINOFLINE) );
143 :
144 0 : TextPaM aStartPaM( aSel.GetEnd() );
145 0 : if ( aSel.HasRange() && ( ( bSearchInSelection && bForward ) || ( !bSearchInSelection && !bForward ) ) )
146 : {
147 0 : aStartPaM = aSel.GetStart();
148 : }
149 :
150 0 : bool bFound = false;
151 : sal_uLong nStartNode, nEndNode;
152 :
153 0 : if ( bSearchInSelection )
154 0 : nEndNode = bForward ? aSel.GetEnd().GetPara() : aSel.GetStart().GetPara();
155 : else
156 0 : nEndNode = bForward ? (GetParagraphCount()-1) : 0;
157 :
158 0 : nStartNode = aStartPaM.GetPara();
159 :
160 0 : util::SearchOptions aOptions( rSearchOptions );
161 0 : aOptions.Locale = Application::GetSettings().GetLanguageTag().getLocale();
162 0 : utl::TextSearch aSearcher( rSearchOptions );
163 :
164 : // iterate over the paragraphs
165 0 : for ( sal_uLong nNode = nStartNode;
166 : bForward ? ( nNode <= nEndNode) : ( nNode >= nEndNode );
167 : bForward ? nNode++ : nNode-- )
168 : {
169 0 : OUString aText = GetText( nNode );
170 0 : sal_Int32 nStartPos = 0;
171 0 : sal_Int32 nEndPos = aText.getLength();
172 0 : if ( nNode == nStartNode )
173 : {
174 0 : if ( bForward )
175 0 : nStartPos = aStartPaM.GetIndex();
176 : else
177 0 : nEndPos = aStartPaM.GetIndex();
178 : }
179 0 : if ( ( nNode == nEndNode ) && bSearchInSelection )
180 : {
181 0 : if ( bForward )
182 0 : nEndPos = aSel.GetEnd().GetIndex();
183 : else
184 0 : nStartPos = aSel.GetStart().GetIndex();
185 : }
186 :
187 0 : if ( bForward )
188 0 : bFound = aSearcher.SearchForward( aText, &nStartPos, &nEndPos );
189 : else
190 0 : bFound = aSearcher.SearchBackward( aText, &nEndPos, &nStartPos );
191 :
192 0 : if ( bFound )
193 : {
194 0 : rSel.GetStart().GetPara() = nNode;
195 0 : rSel.GetStart().GetIndex() = nStartPos;
196 0 : rSel.GetEnd().GetPara() = nNode;
197 0 : rSel.GetEnd().GetIndex() = nEndPos;
198 : // Select over the paragraph?
199 : // FIXME This should be max long...
200 0 : if( nEndPos == -1)
201 : {
202 0 : if ( (rSel.GetEnd().GetPara()+1) < GetParagraphCount() )
203 : {
204 0 : rSel.GetEnd().GetPara()++;
205 0 : rSel.GetEnd().GetIndex() = 0;
206 : }
207 : else
208 : {
209 0 : rSel.GetEnd().GetIndex() = nStartPos;
210 0 : bFound = false;
211 : }
212 : }
213 :
214 0 : break;
215 : }
216 :
217 0 : if ( !bForward && !nNode ) // if searching backwards, if nEndNode == 0:
218 0 : break;
219 0 : }
220 :
221 0 : return bFound;
222 : }
223 :
224 :
225 : // -------------------------------------------------------------------------
226 : // class ExtTextView
227 : // -------------------------------------------------------------------------
228 45 : ExtTextView::ExtTextView( ExtTextEngine* pEng, Window* pWindow )
229 45 : : TextView( pEng, pWindow )
230 : {
231 45 : }
232 :
233 90 : ExtTextView::~ExtTextView()
234 : {
235 90 : }
236 :
237 0 : sal_Bool ExtTextView::MatchGroup()
238 : {
239 0 : TextSelection aTmpSel( GetSelection() );
240 0 : aTmpSel.Justify();
241 0 : if ( ( aTmpSel.GetStart().GetPara() != aTmpSel.GetEnd().GetPara() ) ||
242 0 : ( ( aTmpSel.GetEnd().GetIndex() - aTmpSel.GetStart().GetIndex() ) > 1 ) )
243 : {
244 0 : return sal_False;
245 : }
246 :
247 0 : TextSelection aMatchSel = ((ExtTextEngine*)GetTextEngine())->MatchGroup( aTmpSel.GetStart() );
248 0 : if ( aMatchSel.HasRange() )
249 0 : SetSelection( aMatchSel );
250 :
251 0 : return aMatchSel.HasRange() ? sal_True : sal_False;
252 : }
253 :
254 0 : sal_Bool ExtTextView::Search( const util::SearchOptions& rSearchOptions, sal_Bool bForward )
255 : {
256 0 : sal_Bool bFound = sal_False;
257 0 : TextSelection aSel( GetSelection() );
258 0 : if ( ((ExtTextEngine*)GetTextEngine())->Search( aSel, rSearchOptions, bForward ) )
259 : {
260 0 : bFound = sal_True;
261 : // First add the beginning of the word to the selection,
262 : // so that the whole word is in the visible region.
263 0 : SetSelection( aSel.GetStart() );
264 0 : ShowCursor( sal_True, sal_False );
265 : }
266 : else
267 : {
268 0 : aSel = GetSelection().GetEnd();
269 : }
270 :
271 0 : SetSelection( aSel );
272 0 : ShowCursor();
273 :
274 0 : return bFound;
275 : }
276 :
277 0 : sal_uInt16 ExtTextView::Replace( const util::SearchOptions& rSearchOptions, sal_Bool bAll, sal_Bool bForward )
278 : {
279 0 : sal_uInt16 nFound = 0;
280 :
281 0 : if ( !bAll )
282 : {
283 0 : if ( GetSelection().HasRange() )
284 : {
285 0 : InsertText( rSearchOptions.replaceString );
286 0 : nFound = 1;
287 0 : Search( rSearchOptions, bForward ); // right away to the next
288 : }
289 : else
290 : {
291 0 : if( Search( rSearchOptions, bForward ) )
292 0 : nFound = 1;
293 : }
294 : }
295 : else
296 : {
297 : // the writer replaces all, from beginning to end
298 :
299 0 : ExtTextEngine* pTextEngine = (ExtTextEngine*)GetTextEngine();
300 :
301 : // HideSelection();
302 0 : TextSelection aSel;
303 :
304 0 : bool bSearchInSelection = (0 != (rSearchOptions.searchFlag & util::SearchFlags::REG_NOT_BEGINOFLINE) );
305 0 : if ( bSearchInSelection )
306 : {
307 0 : aSel = GetSelection();
308 0 : aSel.Justify();
309 : }
310 :
311 0 : TextSelection aSearchSel( aSel );
312 :
313 0 : sal_Bool bFound = pTextEngine->Search( aSel, rSearchOptions, sal_True );
314 0 : if ( bFound )
315 0 : pTextEngine->UndoActionStart();
316 0 : while ( bFound )
317 : {
318 0 : nFound++;
319 :
320 0 : TextPaM aNewStart = pTextEngine->ImpInsertText( aSel, rSearchOptions.replaceString );
321 0 : aSel = aSearchSel;
322 0 : aSel.GetStart() = aNewStart;
323 0 : bFound = pTextEngine->Search( aSel, rSearchOptions, sal_True );
324 : }
325 0 : if ( nFound )
326 : {
327 0 : SetSelection( aSel.GetStart() );
328 0 : pTextEngine->FormatAndUpdate( this );
329 0 : pTextEngine->UndoActionEnd();
330 : }
331 : }
332 0 : return nFound;
333 : }
334 :
335 0 : sal_Bool ExtTextView::ImpIndentBlock( sal_Bool bRight )
336 : {
337 0 : sal_Bool bDone = sal_False;
338 :
339 0 : TextSelection aSel = GetSelection();
340 0 : aSel.Justify();
341 :
342 0 : HideSelection();
343 0 : GetTextEngine()->UndoActionStart();
344 :
345 0 : sal_uLong nStartPara = aSel.GetStart().GetPara();
346 0 : sal_uLong nEndPara = aSel.GetEnd().GetPara();
347 0 : if ( aSel.HasRange() && !aSel.GetEnd().GetIndex() )
348 : {
349 0 : nEndPara--; // do not indent
350 : }
351 :
352 0 : for ( sal_uLong nPara = nStartPara; nPara <= nEndPara; nPara++ )
353 : {
354 0 : if ( bRight )
355 : {
356 : // add tabs
357 0 : GetTextEngine()->ImpInsertText( TextPaM( nPara, 0 ), '\t' );
358 0 : bDone = sal_True;
359 : }
360 : else
361 : {
362 : // remove Tabs/Blanks
363 0 : String aText = GetTextEngine()->GetText( nPara );
364 0 : if ( aText.Len() && (
365 0 : ( aText.GetChar( 0 ) == '\t' ) ||
366 0 : ( aText.GetChar( 0 ) == ' ' ) ) )
367 : {
368 0 : GetTextEngine()->ImpDeleteText( TextSelection( TextPaM( nPara, 0 ), TextPaM( nPara, 1 ) ) );
369 0 : bDone = sal_True;
370 0 : }
371 : }
372 : }
373 :
374 0 : GetTextEngine()->UndoActionEnd();
375 :
376 0 : sal_Bool bRange = aSel.HasRange();
377 0 : if ( bRight )
378 : {
379 0 : aSel.GetStart().GetIndex()++;
380 0 : if ( bRange && ( aSel.GetEnd().GetPara() == nEndPara ) )
381 0 : aSel.GetEnd().GetIndex()++;
382 : }
383 : else
384 : {
385 0 : if ( aSel.GetStart().GetIndex() )
386 0 : aSel.GetStart().GetIndex()--;
387 0 : if ( bRange && aSel.GetEnd().GetIndex() )
388 0 : aSel.GetEnd().GetIndex()--;
389 : }
390 :
391 0 : ImpSetSelection( aSel );
392 0 : GetTextEngine()->FormatAndUpdate( this );
393 :
394 0 : return bDone;
395 : }
396 :
397 0 : sal_Bool ExtTextView::IndentBlock()
398 : {
399 0 : return ImpIndentBlock( sal_True );
400 : }
401 :
402 0 : sal_Bool ExtTextView::UnindentBlock()
403 : {
404 0 : return ImpIndentBlock( sal_False );
405 465 : }
406 :
407 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|