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