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 : #include "vbafind.hxx"
20 : #include <vbahelper/vbahelper.hxx>
21 : #include <tools/diagnose_ex.h>
22 : #include "vbareplacement.hxx"
23 : #include <ooo/vba/word/WdFindWrap.hpp>
24 : #include <ooo/vba/word/WdReplace.hpp>
25 : #include <com/sun/star/text/XTextRangeCompare.hpp>
26 : #include "wordvbahelper.hxx"
27 :
28 : using namespace ::ooo::vba;
29 : using namespace ::com::sun::star;
30 :
31 0 : SwVbaFind::SwVbaFind( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, const uno::Reference< frame::XModel >& xModel, const uno::Reference< text::XTextRange >& xTextRange ) throw ( uno::RuntimeException ) :
32 0 : SwVbaFind_BASE( rParent, rContext ), mxModel( xModel ), mxTextRange( xTextRange ), mbReplace( sal_False ), mnReplaceType( word::WdReplace::wdReplaceOne ), mnWrap( word::WdFindWrap::wdFindStop )
33 : {
34 0 : mxReplaceable.set( mxModel, uno::UNO_QUERY_THROW );
35 0 : mxPropertyReplace.set( mxReplaceable->createReplaceDescriptor(), uno::UNO_QUERY_THROW );
36 0 : mxTVC = word::getXTextViewCursor( mxModel );
37 0 : mxSelSupp.set( mxModel->getCurrentController(), uno::UNO_QUERY_THROW );
38 0 : }
39 :
40 0 : SwVbaFind::~SwVbaFind()
41 : {
42 0 : }
43 :
44 0 : sal_Bool SwVbaFind::InRange( const uno::Reference< text::XTextRange >& xCurrentRange ) throw ( uno::RuntimeException )
45 : {
46 0 : uno::Reference< text::XTextRangeCompare > xTRC( mxTextRange->getText(), uno::UNO_QUERY_THROW );
47 0 : if( xTRC->compareRegionStarts( mxTextRange, xCurrentRange ) >= 0 && xTRC->compareRegionEnds( mxTextRange, xCurrentRange ) <= 0 )
48 0 : return sal_True;
49 0 : return sal_False;
50 : }
51 :
52 0 : sal_Bool SwVbaFind::InEqualRange( const uno::Reference< text::XTextRange >& xCurrentRange ) throw ( uno::RuntimeException )
53 : {
54 0 : uno::Reference< text::XTextRangeCompare > xTRC( mxTextRange->getText(), uno::UNO_QUERY_THROW );
55 0 : if( xTRC->compareRegionStarts( mxTextRange, xCurrentRange ) == 0 && xTRC->compareRegionEnds( mxTextRange, xCurrentRange ) == 0 )
56 0 : return sal_True;
57 0 : return sal_False;
58 : }
59 :
60 0 : void SwVbaFind::SetReplaceWith( const rtl::OUString& rText ) throw (uno::RuntimeException)
61 : {
62 0 : mxPropertyReplace->setReplaceString( rText );
63 0 : mbReplace = sal_True;
64 0 : }
65 :
66 0 : rtl::OUString SwVbaFind::GetReplaceWith() throw (uno::RuntimeException)
67 : {
68 0 : return mxPropertyReplace->getReplaceString();
69 : }
70 0 : void SwVbaFind::SetReplace( sal_Int32 type )
71 : {
72 0 : mnReplaceType = type;
73 0 : mbReplace = sal_True;
74 0 : }
75 0 : uno::Reference< text::XTextRange > SwVbaFind::FindOneElement() throw ( uno::RuntimeException )
76 : {
77 0 : uno::Reference< text::XTextRange > xFoundOne;
78 0 : if( !mxTVC->getString().isEmpty() )
79 : {
80 0 : if( getForward() )
81 : {
82 0 : xFoundOne.set( mxReplaceable->findNext( mxTextRange->getStart(), uno::Reference< util::XSearchDescriptor >( mxPropertyReplace, uno::UNO_QUERY_THROW ) ), uno::UNO_QUERY );
83 : }
84 : else
85 : {
86 0 : xFoundOne.set( mxReplaceable->findNext( mxTextRange->getEnd(), uno::Reference< util::XSearchDescriptor >( mxPropertyReplace, uno::UNO_QUERY_THROW ) ), uno::UNO_QUERY );
87 : }
88 :
89 0 : if( xFoundOne.is() && InEqualRange( xFoundOne ) )
90 : {
91 0 : xFoundOne.set( mxReplaceable->findNext( xFoundOne, uno::Reference< util::XSearchDescriptor >( mxPropertyReplace, uno::UNO_QUERY_THROW ) ), uno::UNO_QUERY );
92 : }
93 0 : else if( xFoundOne.is() && !InRange( xFoundOne ) )
94 : {
95 0 : xFoundOne = uno::Reference< text::XTextRange >();
96 : }
97 : }
98 : else
99 : {
100 0 : xFoundOne.set( mxReplaceable->findNext( mxTextRange, uno::Reference< util::XSearchDescriptor >( mxPropertyReplace, uno::UNO_QUERY_THROW ) ), uno::UNO_QUERY );
101 : }
102 :
103 0 : if( !xFoundOne.is() && ( getWrap() == word::WdFindWrap::wdFindContinue || getWrap() == word::WdFindWrap::wdFindAsk ) )
104 : {
105 0 : if( getForward() )
106 : {
107 0 : mxTVC->gotoStart(sal_False);
108 0 : xFoundOne.set( mxReplaceable->findNext( mxTextRange->getStart(), uno::Reference< util::XSearchDescriptor >( mxPropertyReplace, uno::UNO_QUERY_THROW ) ), uno::UNO_QUERY );
109 : }
110 : else
111 : {
112 0 : mxTVC->gotoEnd( sal_False );
113 0 : xFoundOne.set( mxReplaceable->findNext( mxTextRange->getEnd(), uno::Reference< util::XSearchDescriptor >( mxPropertyReplace, uno::UNO_QUERY_THROW ) ), uno::UNO_QUERY );
114 :
115 : }
116 : }
117 0 : return xFoundOne;
118 : }
119 :
120 0 : sal_Bool SwVbaFind::SearchReplace() throw (uno::RuntimeException)
121 : {
122 0 : sal_Bool result = sal_False;
123 :
124 : // TODO: map wildcards in area to OOo wildcards
125 :
126 0 : if( mbReplace )
127 : {
128 0 : switch( mnReplaceType )
129 : {
130 : case word::WdReplace::wdReplaceNone:
131 : {
132 0 : result = sal_True;
133 0 : break;
134 : }
135 : case word::WdReplace::wdReplaceOne:
136 : {
137 0 : uno::Reference< text::XTextRange > xFindOne = FindOneElement();
138 0 : if( xFindOne.is() )
139 : {
140 0 : xFindOne->setString( GetReplaceWith() );
141 0 : result = mxSelSupp->select( uno::makeAny( xFindOne ) );
142 : }
143 0 : break;
144 : }
145 : case word::WdReplace::wdReplaceAll:
146 : {
147 0 : uno::Reference< container::XIndexAccess > xIndexAccess = mxReplaceable->findAll( uno::Reference< util::XSearchDescriptor >( mxPropertyReplace, uno::UNO_QUERY_THROW ) );
148 0 : if( xIndexAccess->getCount() > 0 )
149 : {
150 0 : for( sal_Int32 i = 0; i < xIndexAccess->getCount(); i++ )
151 : {
152 0 : uno::Reference< text::XTextRange > xTextRange( xIndexAccess->getByIndex( i ), uno::UNO_QUERY_THROW );
153 0 : if( mnWrap == word::WdFindWrap::wdFindContinue || mnWrap == word::WdFindWrap::wdFindAsk || InRange( xTextRange ) )
154 : {
155 0 : xTextRange->setString( GetReplaceWith() );
156 0 : result = sal_True;
157 : }
158 0 : }
159 : }
160 0 : break;
161 : }
162 : default:
163 : {
164 0 : result = sal_False;
165 : }
166 : }
167 : }
168 : else
169 : {
170 0 : uno::Reference< text::XTextRange > xFindOne = FindOneElement();
171 0 : if( xFindOne.is() )
172 0 : result = mxSelSupp->select( uno::makeAny( xFindOne ) );
173 : }
174 :
175 0 : return result;
176 : }
177 :
178 0 : ::rtl::OUString SAL_CALL SwVbaFind::getText() throw (uno::RuntimeException)
179 : {
180 0 : return mxPropertyReplace->getSearchString();
181 : }
182 :
183 0 : void SAL_CALL SwVbaFind::setText( const ::rtl::OUString& _text ) throw (uno::RuntimeException)
184 : {
185 0 : mxPropertyReplace->setSearchString( _text );
186 0 : }
187 :
188 0 : uno::Any SAL_CALL SwVbaFind::getReplacement() throw (uno::RuntimeException)
189 : {
190 0 : return uno::makeAny( uno::Reference< word::XReplacement >( new SwVbaReplacement( this, mxContext, mxPropertyReplace ) ) );
191 : }
192 :
193 0 : void SAL_CALL SwVbaFind::setReplacement( const uno::Any& /*_replacement */ ) throw (uno::RuntimeException)
194 : {
195 0 : throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference< uno::XInterface >() );
196 : }
197 :
198 0 : ::sal_Bool SAL_CALL SwVbaFind::getForward() throw (uno::RuntimeException)
199 : {
200 0 : sal_Bool bBackward = sal_False;
201 0 : mxPropertyReplace->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("SearchBackwards") ) ) >>= bBackward;
202 0 : return !bBackward;
203 : }
204 :
205 0 : void SAL_CALL SwVbaFind::setForward( ::sal_Bool _forward ) throw (uno::RuntimeException)
206 : {
207 0 : sal_Bool bBackward = !_forward;
208 0 : mxPropertyReplace->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("SearchBackwards") ), uno::makeAny( bBackward ) );
209 0 : }
210 :
211 0 : ::sal_Int32 SAL_CALL SwVbaFind::getWrap() throw (uno::RuntimeException)
212 : {
213 : // seems not supported in Writer
214 0 : return mnWrap;
215 : }
216 :
217 0 : void SAL_CALL SwVbaFind::setWrap( ::sal_Int32 _wrap ) throw (uno::RuntimeException)
218 : {
219 : // seems not supported in Writer
220 0 : mnWrap = _wrap;
221 0 : }
222 :
223 0 : ::sal_Bool SAL_CALL SwVbaFind::getFormat() throw (uno::RuntimeException)
224 : {
225 0 : return mxPropertyReplace->getValueSearch();
226 : }
227 :
228 0 : void SAL_CALL SwVbaFind::setFormat( ::sal_Bool _format ) throw (uno::RuntimeException)
229 : {
230 0 : mxPropertyReplace->setValueSearch( _format );
231 0 : }
232 :
233 0 : ::sal_Bool SAL_CALL SwVbaFind::getMatchCase() throw (uno::RuntimeException)
234 : {
235 0 : sal_Bool value = sal_False;
236 0 : mxPropertyReplace->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("SearchCaseSensitive") ) ) >>= value;
237 0 : return value;
238 : }
239 :
240 0 : void SAL_CALL SwVbaFind::setMatchCase( ::sal_Bool _matchcase ) throw (uno::RuntimeException)
241 : {
242 0 : mxPropertyReplace->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("SearchCaseSensitive") ), uno::makeAny( _matchcase ) );
243 0 : }
244 :
245 0 : ::sal_Bool SAL_CALL SwVbaFind::getMatchWholeWord() throw (uno::RuntimeException)
246 : {
247 0 : sal_Bool value = sal_False;
248 0 : mxPropertyReplace->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("SearchWords") ) ) >>= value;
249 0 : return value;
250 : }
251 :
252 0 : void SAL_CALL SwVbaFind::setMatchWholeWord( ::sal_Bool _matchwholeword ) throw (uno::RuntimeException)
253 : {
254 0 : mxPropertyReplace->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("SearchWords") ), uno::makeAny( _matchwholeword ) );
255 0 : }
256 :
257 0 : ::sal_Bool SAL_CALL SwVbaFind::getMatchWildcards() throw (uno::RuntimeException)
258 : {
259 0 : sal_Bool value = sal_False;
260 0 : mxPropertyReplace->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("SearchRegularExpression") ) ) >>= value;
261 0 : return value;
262 : }
263 :
264 0 : void SAL_CALL SwVbaFind::setMatchWildcards( ::sal_Bool _matchwildcards ) throw (uno::RuntimeException)
265 : {
266 0 : mxPropertyReplace->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("SearchRegularExpression") ), uno::makeAny( _matchwildcards ) );
267 0 : }
268 :
269 0 : ::sal_Bool SAL_CALL SwVbaFind::getMatchSoundsLike() throw (uno::RuntimeException)
270 : {
271 0 : sal_Bool value = sal_False;
272 0 : mxPropertyReplace->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("SearchSimilarity") ) ) >>= value;
273 0 : return value;
274 : }
275 :
276 0 : void SAL_CALL SwVbaFind::setMatchSoundsLike( ::sal_Bool _matchsoundslike ) throw (uno::RuntimeException)
277 : {
278 : // seems not accurate
279 0 : mxPropertyReplace->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("SearchSimilarity") ), uno::makeAny( _matchsoundslike ) );
280 0 : }
281 :
282 0 : ::sal_Bool SAL_CALL SwVbaFind::getMatchAllWordForms() throw (uno::RuntimeException)
283 : {
284 0 : sal_Bool value = sal_False;
285 0 : mxPropertyReplace->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("SearchSimilarity") ) ) >>= value;
286 0 : if( value )
287 0 : mxPropertyReplace->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("SearchSimilarityRelax") ) ) >>= value;
288 0 : return value;
289 : }
290 :
291 0 : void SAL_CALL SwVbaFind::setMatchAllWordForms( ::sal_Bool _matchallwordforms ) throw (uno::RuntimeException)
292 : {
293 : // seems not accurate
294 0 : mxPropertyReplace->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("SearchSimilarity") ), uno::makeAny( _matchallwordforms ) );
295 0 : mxPropertyReplace->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("SearchSimilarityRelax") ), uno::makeAny( _matchallwordforms ) );
296 0 : }
297 :
298 0 : uno::Any SAL_CALL SwVbaFind::getStyle() throw (uno::RuntimeException)
299 : {
300 0 : throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference< uno::XInterface >() );
301 : }
302 :
303 0 : void SAL_CALL SwVbaFind::setStyle( const uno::Any& /*_style */ ) throw (uno::RuntimeException)
304 : {
305 0 : throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference< uno::XInterface >() );
306 : }
307 :
308 : sal_Bool SAL_CALL
309 0 : SwVbaFind::Execute( const uno::Any& FindText, const uno::Any& MatchCase, const uno::Any& MatchWholeWord, const uno::Any& MatchWildcards, const uno::Any& MatchSoundsLike, const uno::Any& MatchAllWordForms, const uno::Any& Forward, const uno::Any& Wrap, const uno::Any& Format, const uno::Any& ReplaceWith, const uno::Any& Replace, const uno::Any& /*MatchKashida*/, const uno::Any& /*MatchDiacritics*/, const uno::Any& /*MatchAlefHamza*/, const uno::Any& /*MatchControl*/, const uno::Any& /*MatchPrefix*/, const uno::Any& /*MatchSuffix*/, const uno::Any& /*MatchPhrase*/, const uno::Any& /*IgnoreSpace*/, const uno::Any& /*IgnorePunct*/ ) throw (uno::RuntimeException)
310 : {
311 0 : sal_Bool result = sal_False;
312 0 : if( FindText.hasValue() )
313 : {
314 0 : rtl::OUString sText;
315 0 : FindText >>= sText;
316 0 : setText( sText );
317 : }
318 :
319 0 : sal_Bool bValue = sal_False;
320 0 : if( MatchCase.hasValue() )
321 : {
322 0 : MatchCase >>= bValue;
323 0 : setMatchCase( bValue );
324 : }
325 :
326 0 : if( MatchWholeWord.hasValue() )
327 : {
328 0 : MatchWholeWord >>= bValue;
329 0 : setMatchWholeWord( bValue );
330 : }
331 :
332 0 : if( MatchWildcards.hasValue() )
333 : {
334 0 : MatchWildcards >>= bValue;
335 0 : setMatchWildcards( bValue );
336 : }
337 :
338 0 : if( MatchSoundsLike.hasValue() )
339 : {
340 0 : MatchSoundsLike >>= bValue;
341 0 : setMatchSoundsLike( bValue );
342 : }
343 :
344 0 : if( MatchAllWordForms.hasValue() )
345 : {
346 0 : MatchAllWordForms >>= bValue;
347 0 : setMatchAllWordForms( bValue );
348 : }
349 :
350 0 : if( Forward.hasValue() )
351 : {
352 0 : Forward >>= bValue;
353 0 : setForward( bValue );
354 : }
355 :
356 0 : if( Wrap.hasValue() )
357 : {
358 0 : sal_Int32 nWrapType = 0;
359 0 : Wrap >>= nWrapType;
360 0 : setWrap( nWrapType );
361 : }
362 :
363 0 : if( Format.hasValue() )
364 : {
365 0 : Format >>= bValue;
366 0 : setFormat( bValue );
367 : }
368 :
369 0 : if( ReplaceWith.hasValue() )
370 : {
371 0 : rtl::OUString sValue;
372 0 : ReplaceWith >>= sValue;
373 0 : SetReplaceWith( sValue );
374 : }
375 :
376 0 : if( Replace.hasValue() )
377 : {
378 0 : sal_Int32 nValue(0);
379 0 : Replace >>= nValue;
380 0 : SetReplace( nValue );
381 : }
382 :
383 0 : result = SearchReplace();
384 :
385 0 : return result;
386 : }
387 :
388 : void SAL_CALL
389 0 : SwVbaFind::ClearFormatting( ) throw (uno::RuntimeException)
390 : {
391 0 : uno::Sequence< beans::PropertyValue > aSearchAttribs;
392 0 : mxPropertyReplace->setSearchAttributes( aSearchAttribs );
393 0 : }
394 :
395 : rtl::OUString
396 0 : SwVbaFind::getServiceImplName()
397 : {
398 0 : return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SwVbaFind"));
399 : }
400 :
401 : uno::Sequence< rtl::OUString >
402 0 : SwVbaFind::getServiceNames()
403 : {
404 0 : static uno::Sequence< rtl::OUString > aServiceNames;
405 0 : if ( aServiceNames.getLength() == 0 )
406 : {
407 0 : aServiceNames.realloc( 1 );
408 0 : aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Find" ) );
409 : }
410 0 : return aServiceNames;
411 : }
412 :
413 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|