Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <vcl/xtextedt.hxx>
31 : : #include <vcl/svapp.hxx> // International
32 : : #include <unotools/textsearch.hxx>
33 : : #include <com/sun/star/util/SearchOptions.hpp>
34 : : #include <com/sun/star/util/SearchFlags.hpp>
35 : :
36 : : using namespace ::com::sun::star;
37 : :
38 : :
39 : :
40 : : // -------------------------------------------------------------------------
41 : : // class ExtTextEngine
42 : : // -------------------------------------------------------------------------
43 [ + - ]: 90 : ExtTextEngine::ExtTextEngine() : maGroupChars(rtl::OUString("(){}[]"))
44 : : {
45 : 90 : }
46 : :
47 [ + - ]: 90 : ExtTextEngine::~ExtTextEngine()
48 : : {
49 [ - + ]: 180 : }
50 : :
51 : 0 : TextSelection ExtTextEngine::MatchGroup( const TextPaM& rCursor ) const
52 : : {
53 : 0 : TextSelection aSel( rCursor );
54 : 0 : sal_uInt16 nPos = rCursor.GetIndex();
55 : 0 : sal_uLong nPara = rCursor.GetPara();
56 : 0 : sal_uLong nParas = GetParagraphCount();
57 [ # # ][ # # ]: 0 : if ( ( nPara < nParas ) && ( nPos < GetTextLen( nPara ) ) )
[ # # ]
58 : : {
59 [ # # ]: 0 : sal_uInt16 nMatchChar = maGroupChars.Search( GetText( rCursor.GetPara() ).GetChar( nPos ) );
60 [ # # ]: 0 : if ( nMatchChar != STRING_NOTFOUND )
61 : : {
62 [ # # ]: 0 : if ( ( nMatchChar % 2 ) == 0 )
63 : : {
64 : : // Vorwaerts suchen...
65 : 0 : sal_Unicode nSC = maGroupChars.GetChar( nMatchChar );
66 : 0 : sal_Unicode nEC = maGroupChars.GetChar( nMatchChar+1 );
67 : :
68 : 0 : sal_uInt16 nCur = nPos+1;
69 : 0 : sal_uInt16 nLevel = 1;
70 [ # # ][ # # ]: 0 : while ( nLevel && ( nPara < nParas ) )
[ # # ]
71 : : {
72 [ # # ]: 0 : XubString aStr = GetText( nPara );
73 [ # # ]: 0 : while ( nCur < aStr.Len() )
74 : : {
75 [ # # ]: 0 : if ( aStr.GetChar( nCur ) == nSC )
76 : 0 : nLevel++;
77 [ # # ]: 0 : else if ( aStr.GetChar( nCur ) == nEC )
78 : : {
79 : 0 : nLevel--;
80 [ # # ]: 0 : if ( !nLevel )
81 : 0 : break; // while nCur...
82 : : }
83 : 0 : nCur++;
84 : : }
85 : :
86 [ # # ]: 0 : if ( nLevel )
87 : : {
88 : 0 : nPara++;
89 : 0 : nCur = 0;
90 : : }
91 [ # # ]: 0 : }
92 [ # # ]: 0 : if ( nLevel == 0 ) // gefunden
93 : : {
94 : 0 : aSel.GetStart() = rCursor;
95 : 0 : aSel.GetEnd() = TextPaM( nPara, nCur+1 );
96 : : }
97 : : }
98 : : else
99 : : {
100 : : // Rueckwaerts suchen...
101 : 0 : xub_Unicode nEC = maGroupChars.GetChar( nMatchChar );
102 : 0 : xub_Unicode nSC = maGroupChars.GetChar( nMatchChar-1 );
103 : :
104 : 0 : sal_uInt16 nCur = rCursor.GetIndex()-1;
105 : 0 : sal_uInt16 nLevel = 1;
106 [ # # ]: 0 : while ( nLevel )
107 : : {
108 [ # # ]: 0 : if ( GetTextLen( nPara ) )
109 : : {
110 [ # # ]: 0 : XubString aStr = GetText( nPara );
111 [ # # ]: 0 : while ( nCur )
112 : : {
113 [ # # ]: 0 : if ( aStr.GetChar( nCur ) == nSC )
114 : : {
115 : 0 : nLevel--;
116 [ # # ]: 0 : if ( !nLevel )
117 : 0 : break; // while nCur...
118 : : }
119 [ # # ]: 0 : else if ( aStr.GetChar( nCur ) == nEC )
120 : 0 : nLevel++;
121 : :
122 : 0 : nCur--;
123 [ # # ]: 0 : }
124 : : }
125 : :
126 [ # # ]: 0 : if ( nLevel )
127 : : {
128 [ # # ]: 0 : if ( nPara )
129 : : {
130 : 0 : nPara--;
131 : 0 : nCur = GetTextLen( nPara )-1; // egal ob negativ, weil if Len()
132 : : }
133 : : else
134 : 0 : break;
135 : : }
136 : : }
137 : :
138 [ # # ]: 0 : if ( nLevel == 0 ) // gefunden
139 : : {
140 : 0 : aSel.GetStart() = rCursor;
141 : 0 : aSel.GetStart().GetIndex()++; // hinter das Zeichen
142 : 0 : aSel.GetEnd() = TextPaM( nPara, nCur );
143 : : }
144 : : }
145 : : }
146 : : }
147 : 0 : return aSel;
148 : : }
149 : :
150 : 0 : sal_Bool ExtTextEngine::Search( TextSelection& rSel, const util::SearchOptions& rSearchOptions, sal_Bool bForward )
151 : : {
152 : 0 : TextSelection aSel( rSel );
153 [ # # ]: 0 : aSel.Justify();
154 : :
155 : 0 : sal_Bool bSearchInSelection = (0 != (rSearchOptions.searchFlag & util::SearchFlags::REG_NOT_BEGINOFLINE) );
156 : :
157 : 0 : TextPaM aStartPaM( aSel.GetEnd() );
158 [ # # ][ # # ]: 0 : if ( aSel.HasRange() && ( ( bSearchInSelection && bForward ) || ( !bSearchInSelection && !bForward ) ) )
[ # # ][ # # ]
[ # # ][ # # ]
159 : : {
160 : 0 : aStartPaM = aSel.GetStart();
161 : : }
162 : :
163 : 0 : bool bFound = false;
164 : : sal_uLong nStartNode, nEndNode;
165 : :
166 [ # # ]: 0 : if ( bSearchInSelection )
167 [ # # ]: 0 : nEndNode = bForward ? aSel.GetEnd().GetPara() : aSel.GetStart().GetPara();
168 : : else
169 [ # # ][ # # ]: 0 : nEndNode = bForward ? (GetParagraphCount()-1) : 0;
170 : :
171 : 0 : nStartNode = aStartPaM.GetPara();
172 : :
173 : 0 : util::SearchOptions aOptions( rSearchOptions );
174 [ # # ][ # # ]: 0 : aOptions.Locale = Application::GetSettings().GetLocale();
175 [ # # ]: 0 : utl::TextSearch aSearcher( rSearchOptions );
176 : :
177 : : // ueber die Absaetze iterieren...
178 [ # # ][ # # ]: 0 : for ( sal_uLong nNode = nStartNode;
[ # # ]
179 : : bForward ? ( nNode <= nEndNode) : ( nNode >= nEndNode );
180 : : bForward ? nNode++ : nNode-- )
181 : : {
182 [ # # ]: 0 : String aText = GetText( nNode );
183 : 0 : sal_uInt16 nStartPos = 0;
184 : 0 : sal_uInt16 nEndPos = aText.Len();
185 [ # # ]: 0 : if ( nNode == nStartNode )
186 : : {
187 [ # # ]: 0 : if ( bForward )
188 : 0 : nStartPos = aStartPaM.GetIndex();
189 : : else
190 : 0 : nEndPos = aStartPaM.GetIndex();
191 : : }
192 [ # # ][ # # ]: 0 : if ( ( nNode == nEndNode ) && bSearchInSelection )
193 : : {
194 [ # # ]: 0 : if ( bForward )
195 : 0 : nEndPos = aSel.GetEnd().GetIndex();
196 : : else
197 : 0 : nStartPos = aSel.GetStart().GetIndex();
198 : : }
199 : :
200 [ # # ]: 0 : if ( bForward )
201 [ # # ]: 0 : bFound = aSearcher.SearchFrwrd( aText, &nStartPos, &nEndPos );
202 : : else
203 [ # # ]: 0 : bFound = aSearcher.SearchBkwrd( aText, &nEndPos, &nStartPos );
204 : :
205 [ # # ]: 0 : if ( bFound )
206 : : {
207 : 0 : rSel.GetStart().GetPara() = nNode;
208 : 0 : rSel.GetStart().GetIndex() = nStartPos;
209 : 0 : rSel.GetEnd().GetPara() = nNode;
210 : 0 : rSel.GetEnd().GetIndex() = nEndPos;
211 : : // Ueber den Absatz selektieren?
212 : : // Select over the paragraph?
213 : : // FIXME This should be max long...
214 [ # # ]: 0 : if( nEndPos == sal::static_int_cast<sal_uInt16>(-1) ) // sal_uInt16 for 0 and -1 !
215 : : {
216 [ # # ][ # # ]: 0 : if ( (rSel.GetEnd().GetPara()+1) < GetParagraphCount() )
217 : : {
218 : 0 : rSel.GetEnd().GetPara()++;
219 : 0 : rSel.GetEnd().GetIndex() = 0;
220 : : }
221 : : else
222 : : {
223 : 0 : rSel.GetEnd().GetIndex() = nStartPos;
224 : 0 : bFound = false;
225 : : }
226 : : }
227 : :
228 : : break;
229 : : }
230 : :
231 [ # # ][ # # ]: 0 : if ( !bForward && !nNode ) // Bei rueckwaertsuche, wenn nEndNode = 0:
232 : : break;
233 [ # # ][ # # ]: 0 : }
234 : :
235 [ # # ]: 0 : return bFound;
236 : : }
237 : :
238 : :
239 : : // -------------------------------------------------------------------------
240 : : // class ExtTextView
241 : : // -------------------------------------------------------------------------
242 : 90 : ExtTextView::ExtTextView( ExtTextEngine* pEng, Window* pWindow )
243 : 90 : : TextView( pEng, pWindow )
244 : : {
245 : 90 : }
246 : :
247 : 90 : ExtTextView::~ExtTextView()
248 : : {
249 [ - + ]: 180 : }
250 : :
251 : 0 : sal_Bool ExtTextView::MatchGroup()
252 : : {
253 [ # # ]: 0 : TextSelection aTmpSel( GetSelection() );
254 [ # # ]: 0 : aTmpSel.Justify();
255 [ # # # # ]: 0 : if ( ( aTmpSel.GetStart().GetPara() != aTmpSel.GetEnd().GetPara() ) ||
[ # # ]
256 : 0 : ( ( aTmpSel.GetEnd().GetIndex() - aTmpSel.GetStart().GetIndex() ) > 1 ) )
257 : : {
258 : 0 : return sal_False;
259 : : }
260 : :
261 [ # # ][ # # ]: 0 : TextSelection aMatchSel = ((ExtTextEngine*)GetTextEngine())->MatchGroup( aTmpSel.GetStart() );
262 [ # # ]: 0 : if ( aMatchSel.HasRange() )
263 [ # # ]: 0 : SetSelection( aMatchSel );
264 : :
265 [ # # ]: 0 : return aMatchSel.HasRange() ? sal_True : sal_False;
266 : : }
267 : :
268 : 0 : sal_Bool ExtTextView::Search( const util::SearchOptions& rSearchOptions, sal_Bool bForward )
269 : : {
270 : 0 : sal_Bool bFound = sal_False;
271 [ # # ]: 0 : TextSelection aSel( GetSelection() );
272 [ # # ][ # # ]: 0 : if ( ((ExtTextEngine*)GetTextEngine())->Search( aSel, rSearchOptions, bForward ) )
[ # # ]
273 : : {
274 : 0 : bFound = sal_True;
275 : : // Erstmal den Anfang des Wortes als Selektion einstellen,
276 : : // damit das ganze Wort in den sichtbaren Bereich kommt.
277 [ # # ][ # # ]: 0 : SetSelection( aSel.GetStart() );
278 [ # # ]: 0 : ShowCursor( sal_True, sal_False );
279 : : }
280 : : else
281 : : {
282 [ # # ][ # # ]: 0 : aSel = GetSelection().GetEnd();
283 : : }
284 : :
285 [ # # ]: 0 : SetSelection( aSel );
286 [ # # ]: 0 : ShowCursor();
287 : :
288 : 0 : return bFound;
289 : : }
290 : :
291 : 0 : sal_uInt16 ExtTextView::Replace( const util::SearchOptions& rSearchOptions, sal_Bool bAll, sal_Bool bForward )
292 : : {
293 : 0 : sal_uInt16 nFound = 0;
294 : :
295 [ # # ]: 0 : if ( !bAll )
296 : : {
297 [ # # ]: 0 : if ( GetSelection().HasRange() )
298 : : {
299 [ # # ]: 0 : InsertText( rSearchOptions.replaceString );
300 : 0 : nFound = 1;
301 : 0 : Search( rSearchOptions, bForward ); // gleich zum naechsten
302 : : }
303 : : else
304 : : {
305 [ # # ]: 0 : if( Search( rSearchOptions, bForward ) )
306 : 0 : nFound = 1;
307 : : }
308 : : }
309 : : else
310 : : {
311 : : // Der Writer ersetzt alle, vom Anfang bis Ende...
312 : :
313 [ # # ]: 0 : ExtTextEngine* pTextEngine = (ExtTextEngine*)GetTextEngine();
314 : :
315 : : // HideSelection();
316 [ # # ]: 0 : TextSelection aSel;
317 : :
318 : 0 : sal_Bool bSearchInSelection = (0 != (rSearchOptions.searchFlag & util::SearchFlags::REG_NOT_BEGINOFLINE) );
319 [ # # ]: 0 : if ( bSearchInSelection )
320 : : {
321 [ # # ]: 0 : aSel = GetSelection();
322 [ # # ]: 0 : aSel.Justify();
323 : : }
324 : :
325 : 0 : TextSelection aSearchSel( aSel );
326 : :
327 [ # # ]: 0 : sal_Bool bFound = pTextEngine->Search( aSel, rSearchOptions, sal_True );
328 [ # # ]: 0 : if ( bFound )
329 [ # # ]: 0 : pTextEngine->UndoActionStart();
330 [ # # ]: 0 : while ( bFound )
331 : : {
332 : 0 : nFound++;
333 : :
334 [ # # ][ # # ]: 0 : TextPaM aNewStart = pTextEngine->ImpInsertText( aSel, rSearchOptions.replaceString );
[ # # ]
335 : 0 : aSel = aSearchSel;
336 : 0 : aSel.GetStart() = aNewStart;
337 [ # # ]: 0 : bFound = pTextEngine->Search( aSel, rSearchOptions, sal_True );
338 : : }
339 [ # # ]: 0 : if ( nFound )
340 : : {
341 [ # # ][ # # ]: 0 : SetSelection( aSel.GetStart() );
342 [ # # ]: 0 : pTextEngine->FormatAndUpdate( this );
343 [ # # ]: 0 : pTextEngine->UndoActionEnd();
344 : : }
345 : : }
346 : 0 : return nFound;
347 : : }
348 : :
349 : 0 : sal_Bool ExtTextView::ImpIndentBlock( sal_Bool bRight )
350 : : {
351 : 0 : sal_Bool bDone = sal_False;
352 : :
353 [ # # ]: 0 : TextSelection aSel = GetSelection();
354 [ # # ]: 0 : aSel.Justify();
355 : :
356 [ # # ]: 0 : HideSelection();
357 [ # # ][ # # ]: 0 : GetTextEngine()->UndoActionStart();
358 : :
359 : 0 : sal_uLong nStartPara = aSel.GetStart().GetPara();
360 : 0 : sal_uLong nEndPara = aSel.GetEnd().GetPara();
361 [ # # ][ # # ]: 0 : if ( aSel.HasRange() && !aSel.GetEnd().GetIndex() )
[ # # ]
362 : : {
363 : 0 : nEndPara--; // den dann nicht einruecken...
364 : : }
365 : :
366 [ # # ]: 0 : for ( sal_uLong nPara = nStartPara; nPara <= nEndPara; nPara++ )
367 : : {
368 [ # # ]: 0 : if ( bRight )
369 : : {
370 : : // Tabs hinzufuegen
371 [ # # ][ # # ]: 0 : GetTextEngine()->ImpInsertText( TextPaM( nPara, 0 ), '\t' );
[ # # ]
372 : 0 : bDone = sal_True;
373 : : }
374 : : else
375 : : {
376 : : // Tabs/Blanks entfernen
377 [ # # ][ # # ]: 0 : String aText = GetTextEngine()->GetText( nPara );
378 [ # # # # : 0 : if ( aText.Len() && (
# # ][ # # ]
379 : 0 : ( aText.GetChar( 0 ) == '\t' ) ||
380 : 0 : ( aText.GetChar( 0 ) == ' ' ) ) )
381 : : {
382 [ # # ][ # # ]: 0 : GetTextEngine()->ImpDeleteText( TextSelection( TextPaM( nPara, 0 ), TextPaM( nPara, 1 ) ) );
[ # # ]
383 : 0 : bDone = sal_True;
384 [ # # ]: 0 : }
385 : : }
386 : : }
387 : :
388 [ # # ][ # # ]: 0 : GetTextEngine()->UndoActionEnd();
389 : :
390 : 0 : sal_Bool bRange = aSel.HasRange();
391 [ # # ]: 0 : if ( bRight )
392 : : {
393 : 0 : aSel.GetStart().GetIndex()++;
394 [ # # ][ # # ]: 0 : if ( bRange && ( aSel.GetEnd().GetPara() == nEndPara ) )
[ # # ]
395 : 0 : aSel.GetEnd().GetIndex()++;
396 : : }
397 : : else
398 : : {
399 [ # # ]: 0 : if ( aSel.GetStart().GetIndex() )
400 : 0 : aSel.GetStart().GetIndex()--;
401 [ # # ][ # # ]: 0 : if ( bRange && aSel.GetEnd().GetIndex() )
[ # # ]
402 : 0 : aSel.GetEnd().GetIndex()--;
403 : : }
404 : :
405 [ # # ]: 0 : ImpSetSelection( aSel );
406 [ # # ][ # # ]: 0 : GetTextEngine()->FormatAndUpdate( this );
407 : :
408 : 0 : return bDone;
409 : : }
410 : :
411 : 0 : sal_Bool ExtTextView::IndentBlock()
412 : : {
413 : 0 : return ImpIndentBlock( sal_True );
414 : : }
415 : :
416 : 0 : sal_Bool ExtTextView::UnindentBlock()
417 : : {
418 : 0 : return ImpIndentBlock( sal_False );
419 : : }
420 : :
421 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|