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 <string>
22 :
23 : #include <boost/scoped_ptr.hpp>
24 :
25 : #include <hintids.hxx>
26 : #include <com/sun/star/util/SearchOptions.hpp>
27 : #include <svl/cjkoptions.hxx>
28 : #include <svl/ctloptions.hxx>
29 : #include <svx/pageitem.hxx>
30 : #include <svl/whiter.hxx>
31 : #include <sfx2/dispatch.hxx>
32 : #include <svl/stritem.hxx>
33 : #include <unotools/textsearch.hxx>
34 : #include <svl/itempool.hxx>
35 : #include <svl/eitem.hxx>
36 : #include <svl/srchitem.hxx>
37 : #include <sal/macros.h>
38 : #include <sfx2/request.hxx>
39 : #include <svx/srchdlg.hxx>
40 : #include <svx/dialmgr.hxx>
41 : #include <svx/dialogs.hrc>
42 : #include <vcl/msgbox.hxx>
43 : #include <vcl/wrkwin.hxx>
44 : #include "editeng/unolingu.hxx"
45 : #include <swmodule.hxx>
46 : #include <swwait.hxx>
47 : #include <workctrl.hxx>
48 : #include <view.hxx>
49 : #include <wrtsh.hxx>
50 : #include <swundo.hxx> // fuer Undo-Ids
51 : #include <uitool.hxx>
52 : #include <cmdid.h>
53 : #include <docsh.hxx>
54 : #include <doc.hxx>
55 : #include <unocrsr.hxx>
56 :
57 : #include <view.hrc>
58 : #include <SwRewriter.hxx>
59 : #include <comcore.hrc>
60 :
61 : #include "PostItMgr.hxx"
62 :
63 : using namespace com::sun::star;
64 : using namespace ::com::sun::star::i18n;
65 : using namespace ::com::sun::star::lang;
66 : using namespace ::com::sun::star::util;
67 :
68 : /*--------------------------------------------------------------------
69 : Beschreibung: Search Parameter
70 : --------------------------------------------------------------------*/
71 :
72 : struct SwSearchOptions
73 : {
74 : SwDocPositions eStart, eEnd;
75 : sal_Bool bDontWrap;
76 :
77 : SwSearchOptions( SwWrtShell* pSh, sal_Bool bBackward );
78 : };
79 :
80 :
81 0 : inline Window* GetParentWindow( SvxSearchDialog* m_pSrchDlg )
82 : {
83 : Window* pWin;
84 0 : if( m_pSrchDlg && m_pSrchDlg->IsVisible() )
85 0 : pWin = m_pSrchDlg;
86 : else
87 0 : pWin = 0;
88 0 : return pWin;
89 : }
90 :
91 0 : inline void ShowNotFoundInfoBox( SvxSearchDialog* m_pSrchDlg )
92 : {
93 0 : Window* pParentWindow = GetParentWindow( m_pSrchDlg );
94 0 : InfoBox aBox( pParentWindow, SW_RES(MSG_NOT_FOUND));
95 0 : if (pParentWindow)
96 : {
97 0 : aBox.SetText(pParentWindow->GetText());
98 : }
99 : else
100 : {
101 0 : aBox.SetText(SVX_RESSTR(RID_SVXSTR_FINDBAR_FIND));
102 : }
103 0 : aBox.Execute();
104 0 : }
105 :
106 0 : void SwView::ExecSearch(SfxRequest& rReq, sal_Bool bNoMessage)
107 : {
108 0 : const SfxItemSet* pArgs = rReq.GetArgs();
109 0 : const SfxPoolItem* pItem = 0;
110 0 : const sal_uInt16 nId = SvxSearchDialogWrapper::GetChildWindowId();
111 0 : SvxSearchDialogWrapper *pWrp = (SvxSearchDialogWrapper*)GetViewFrame()->GetChildWindow(nId);
112 0 : sal_Bool bQuiet = sal_False;
113 0 : if(pArgs && SFX_ITEM_SET == pArgs->GetItemState(SID_SEARCH_QUIET, sal_False, &pItem))
114 0 : bQuiet = ((const SfxBoolItem*) pItem)->GetValue();
115 :
116 0 : sal_Bool bApi = bQuiet | bNoMessage;
117 :
118 0 : sal_uInt16 nSlot = rReq.GetSlot();
119 0 : if (nSlot == FN_REPEAT_SEARCH && !m_pSrchItem)
120 : {
121 0 : if(bApi)
122 : {
123 0 : rReq.SetReturnValue(SfxBoolItem(nSlot, sal_False));
124 0 : nSlot = 0;
125 : }
126 : }
127 0 : if( m_pWrtShell->IsBlockMode() )
128 0 : m_pWrtShell->LeaveBlockMode();
129 0 : switch (nSlot)
130 : {
131 : // erstmal Nichts tun
132 : case SID_SEARCH_ITEM:
133 : {
134 0 : delete m_pSrchItem;
135 0 : m_pSrchItem = (SvxSearchItem*) pArgs->Get(SID_SEARCH_ITEM).Clone();
136 : }
137 0 : break;
138 :
139 : case FID_SEARCH_ON:
140 0 : m_bJustOpened = true;
141 0 : GetViewFrame()->GetBindings().Invalidate(SID_SEARCH_ITEM);
142 0 : break;
143 :
144 : case FID_SEARCH_OFF:
145 0 : if(pArgs)
146 : {
147 : // Dialog abmelden
148 0 : delete m_pSrchItem;
149 0 : m_pSrchItem = (SvxSearchItem*) pArgs->Get(SID_SEARCH_ITEM).Clone();
150 :
151 0 : DELETEZ( m_pSrchList );
152 0 : DELETEZ( m_pReplList );
153 :
154 0 : if ( pWrp )
155 : {
156 0 : m_pSrchDlg = static_cast <SvxSearchDialog*> (pWrp->getDialog ());
157 : // die Search / Replace -Items merken wir uns
158 0 : const SearchAttrItemList* pList = m_pSrchDlg->GetSearchItemList();
159 0 : if( pList && pList->Count() )
160 0 : m_pSrchList = new SearchAttrItemList( *pList );
161 :
162 0 : if( 0 != (pList = m_pSrchDlg->GetReplaceItemList() ) &&
163 0 : pList->Count() )
164 0 : m_pReplList = new SearchAttrItemList( *pList );
165 : }
166 : }
167 0 : break;
168 :
169 : case FN_REPEAT_SEARCH:
170 : case FID_SEARCH_NOW:
171 : {
172 : {
173 0 : if(FID_SEARCH_NOW == nSlot && !rReq.IsAPI())
174 0 : SwView::SetMoveType(NID_SRCH_REP);
175 0 : if ( pWrp )
176 : {
177 0 : m_pSrchDlg = static_cast <SvxSearchDialog*> (pWrp->getDialog ());
178 : }
179 : else
180 0 : m_pSrchDlg = 0;
181 : }
182 :
183 0 : if (m_pSrchDlg)
184 : {
185 0 : DELETEZ( m_pSrchList );
186 0 : DELETEZ( m_pReplList );
187 :
188 0 : const SearchAttrItemList* pList = m_pSrchDlg->GetSearchItemList();
189 0 : if( pList && pList->Count() )
190 0 : m_pSrchList = new SearchAttrItemList( *pList );
191 :
192 0 : if( 0 != (pList = m_pSrchDlg->GetReplaceItemList() ) &&
193 0 : pList->Count() )
194 0 : m_pReplList = new SearchAttrItemList( *pList );
195 : }
196 :
197 0 : if (nSlot == FN_REPEAT_SEARCH)
198 : {
199 : OSL_ENSURE(m_pSrchItem, "SearchItem missing");
200 0 : if( !m_pSrchItem )
201 0 : m_pSrchItem = new SvxSearchItem(SID_SEARCH_ITEM);
202 : }
203 : else
204 : {
205 : // SearchItem aus Request besorgen
206 : OSL_ENSURE(pArgs, "Args missing");
207 0 : if ( pArgs )
208 : {
209 0 : delete m_pSrchItem;
210 0 : m_pSrchItem = (SvxSearchItem*) pArgs->Get(SID_SEARCH_ITEM).Clone();
211 : }
212 : }
213 0 : switch (m_pSrchItem->GetCommand())
214 : {
215 : case SVX_SEARCHCMD_FIND:
216 : {
217 0 : sal_Bool bRet = SearchAndWrap(bApi);
218 0 : if( bRet )
219 0 : Scroll(m_pWrtShell->GetCharRect().SVRect());
220 0 : rReq.SetReturnValue(SfxBoolItem(nSlot, bRet));
221 : }
222 0 : break;
223 : case SVX_SEARCHCMD_FIND_ALL:
224 : {
225 0 : sal_Bool bRet = SearchAll();
226 0 : if( !bRet )
227 : {
228 0 : if( !bApi )
229 : {
230 0 : ShowNotFoundInfoBox( m_pSrchDlg );
231 : }
232 0 : m_bFound = sal_False;
233 : }
234 0 : rReq.SetReturnValue(SfxBoolItem(nSlot, bRet));
235 : }
236 0 : break;
237 : case SVX_SEARCHCMD_REPLACE:
238 : {
239 :
240 : // 1) Selektion ersetzen (nicht. wenn nur Attribute ersetzt
241 : // werden sollen)
242 : //JP 27.04.95: warum ?
243 : // was ist, wenn man das gefundene nur attributieren will??
244 :
245 0 : sal_uInt16 nCmd = SVX_SEARCHCMD_FIND;
246 0 : if( !m_pSrchItem->GetReplaceString().isEmpty() ||
247 0 : !m_pReplList )
248 : {
249 : // Verhindern, dass - falls der Suchstring im
250 : // Ersetzungsstring enthalten ist - der ersetzte String
251 : // noch einmal gefunden wird.
252 :
253 0 : sal_Bool bBack = m_pSrchItem->GetBackward();
254 0 : if (bBack)
255 0 : m_pWrtShell->Push();
256 0 : String aReplace( m_pSrchItem->GetReplaceString() );
257 0 : SearchOptions aTmp( m_pSrchItem->GetSearchOptions() );
258 0 : String *pBackRef = ReplaceBackReferences( aTmp, m_pWrtShell->GetCrsr() );
259 0 : if( pBackRef )
260 0 : m_pSrchItem->SetReplaceString( *pBackRef );
261 0 : Replace();
262 0 : if( pBackRef )
263 : {
264 0 : m_pSrchItem->SetReplaceString( aReplace );
265 0 : delete pBackRef;
266 : }
267 0 : if (bBack)
268 : {
269 0 : m_pWrtShell->Pop();
270 0 : m_pWrtShell->SwapPam();
271 0 : }
272 : }
273 0 : else if( m_pReplList )
274 0 : nCmd = SVX_SEARCHCMD_REPLACE;
275 :
276 : // 2) Weiter suchen (ohne zu ersetzen!)
277 :
278 0 : sal_uInt16 nOldCmd = m_pSrchItem->GetCommand();
279 0 : m_pSrchItem->SetCommand( nCmd );
280 0 : sal_Bool bRet = SearchAndWrap(bApi);
281 0 : if( bRet )
282 0 : Scroll( m_pWrtShell->GetCharRect().SVRect());
283 0 : m_pSrchItem->SetCommand( nOldCmd );
284 0 : rReq.SetReturnValue(SfxBoolItem(nSlot, bRet));
285 : }
286 0 : break;
287 :
288 : case SVX_SEARCHCMD_REPLACE_ALL:
289 : {
290 0 : SwSearchOptions aOpts( m_pWrtShell, m_pSrchItem->GetBackward() );
291 0 : m_bExtra = sal_False;
292 : sal_uLong nFound;
293 :
294 : { //Scope for SwWait-Object
295 0 : SwWait aWait( *GetDocShell(), sal_True );
296 0 : m_pWrtShell->StartAllAction();
297 0 : if (!m_pSrchItem->GetSelection())
298 : {
299 : // if we don't want to search in the selection...
300 0 : m_pWrtShell->KillSelection(0, false);
301 : // i#8288 "replace all" should not change cursor
302 : // position, so save current cursor
303 0 : m_pWrtShell->Push();
304 0 : if (DOCPOS_START == aOpts.eEnd)
305 : {
306 0 : m_pWrtShell->EndDoc();
307 : }
308 : else
309 : {
310 0 : m_pWrtShell->SttDoc();
311 : }
312 : }
313 0 : nFound = FUNC_Search( aOpts );
314 0 : if (!m_pSrchItem->GetSelection())
315 : {
316 : // create it just to overwrite it with stack cursor
317 0 : m_pWrtShell->CreateCrsr();
318 : // i#8288 restore the original cursor position
319 0 : m_pWrtShell->Pop(false);
320 : }
321 0 : m_pWrtShell->EndAllAction();
322 : }
323 :
324 0 : rReq.SetReturnValue(SfxBoolItem(nSlot, nFound != 0 && ULONG_MAX != nFound));
325 0 : if( !nFound )
326 : {
327 0 : if( !bApi )
328 : {
329 0 : ShowNotFoundInfoBox( m_pSrchDlg );
330 : }
331 0 : m_bFound = sal_False;
332 0 : return;
333 : }
334 :
335 0 : if( !bApi && ULONG_MAX != nFound)
336 : {
337 0 : String aText( SW_RES( STR_NB_REPLACED ) );
338 0 : const xub_StrLen nPos = aText.Search( OUString("XX") );
339 0 : aText.Erase( nPos, 2 );
340 0 : aText.Insert( OUString::number( nFound ), nPos );
341 0 : Window* pParentWindow = GetParentWindow( m_pSrchDlg );
342 0 : InfoBox( pParentWindow, aText ).Execute();
343 : }
344 : }
345 0 : break;
346 : }
347 :
348 : uno::Reference< frame::XDispatchRecorder > xRecorder =
349 0 : GetViewFrame()->GetBindings().GetRecorder();
350 : //prevent additional dialogs in recorded macros
351 0 : if ( xRecorder.is() )
352 0 : rReq.AppendItem(SfxBoolItem(SID_SEARCH_QUIET, sal_True));
353 :
354 0 : rReq.Done();
355 : }
356 0 : break;
357 : case FID_SEARCH_SEARCHSET:
358 : case FID_SEARCH_REPLACESET:
359 : {
360 : static const sal_uInt16 aNormalAttr[] =
361 : {
362 : /* 0 */ RES_CHRATR_CASEMAP, RES_CHRATR_CASEMAP,
363 : /* 2 */ RES_CHRATR_COLOR, RES_CHRATR_POSTURE,
364 : /* 4 */ RES_CHRATR_SHADOWED, RES_CHRATR_WORDLINEMODE,
365 : /* 6 */ RES_CHRATR_BLINK, RES_CHRATR_BLINK,
366 : /* 8 */ RES_CHRATR_BACKGROUND, RES_CHRATR_BACKGROUND,
367 : /*10 */ RES_CHRATR_ROTATE, RES_CHRATR_ROTATE,
368 : /*12 */ RES_CHRATR_SCALEW, RES_CHRATR_RELIEF,
369 : // insert position for CJK/CTL attributes!
370 : /*14 */ RES_PARATR_LINESPACING, RES_PARATR_HYPHENZONE,
371 : /*16 */ RES_PARATR_REGISTER, RES_PARATR_REGISTER,
372 : /*18 */ RES_PARATR_VERTALIGN, RES_PARATR_VERTALIGN,
373 : /*20 */ RES_LR_SPACE, RES_UL_SPACE,
374 : /*22 */ SID_ATTR_PARA_MODEL, SID_ATTR_PARA_KEEP,
375 : /*24 */ 0
376 : };
377 :
378 : static const sal_uInt16 aCJKAttr[] =
379 : {
380 : RES_CHRATR_CJK_FONT, RES_CHRATR_CJK_WEIGHT,
381 : RES_CHRATR_EMPHASIS_MARK, RES_CHRATR_TWO_LINES,
382 : RES_PARATR_SCRIPTSPACE, RES_PARATR_FORBIDDEN_RULES
383 : };
384 : static const sal_uInt16 aCTLAttr[] =
385 : {
386 : RES_CHRATR_CTL_FONT, RES_CHRATR_CTL_WEIGHT
387 : };
388 :
389 0 : std::vector<sal_uInt16> aArr;
390 : aArr.insert( aArr.begin(), aNormalAttr,
391 0 : aNormalAttr + SAL_N_ELEMENTS( aNormalAttr ));
392 0 : if( SW_MOD()->GetCTLOptions().IsCTLFontEnabled() )
393 : {
394 : aArr.insert( aArr.begin() + 14, aCTLAttr,
395 0 : aCTLAttr + SAL_N_ELEMENTS( aCTLAttr ));
396 : }
397 0 : SvtCJKOptions aCJKOpt;
398 0 : if( aCJKOpt.IsAnyEnabled() )
399 : {
400 : aArr.insert( aArr.begin() + 14, aCJKAttr,
401 0 : aCJKAttr + SAL_N_ELEMENTS( aCJKAttr ));
402 : }
403 :
404 0 : SfxItemSet aSet( m_pWrtShell->GetAttrPool(), &aArr[0] );
405 0 : sal_uInt16 nWhich = SID_SEARCH_SEARCHSET;
406 :
407 0 : if ( FID_SEARCH_REPLACESET == nSlot )
408 : {
409 0 : nWhich = SID_SEARCH_REPLACESET;
410 :
411 0 : if ( m_pReplList )
412 : {
413 0 : m_pReplList->Get( aSet );
414 0 : DELETEZ( m_pReplList );
415 : }
416 : }
417 0 : else if ( m_pSrchList )
418 : {
419 0 : m_pSrchList->Get( aSet );
420 0 : DELETEZ( m_pSrchList );
421 : }
422 0 : rReq.SetReturnValue( SvxSetItem( nWhich, aSet ) );
423 : }
424 0 : break;
425 : default:
426 : #if OSL_DEBUG_LEVEL > 1
427 : if(nSlot)
428 : {
429 : OString sStr("nSlot: " + OString::number(nSlot) + " wrong Dispatcher (viewsrch.cxx)");
430 : OSL_FAIL(sStr.getStr());
431 : }
432 : #endif
433 0 : return;
434 : }
435 : }
436 :
437 :
438 0 : sal_Bool SwView::SearchAndWrap(sal_Bool bApi)
439 : {
440 0 : SwSearchOptions aOpts( m_pWrtShell, m_pSrchItem->GetBackward() );
441 :
442 : // Startposition der Suche fuer WrapAround merken
443 : // Start- / EndAction wegen vielleicht bestehender Selektionen
444 : // aus 'Suche alle'
445 0 : m_pWrtShell->StartAllAction();
446 0 : m_pWrtShell->Push();
447 :
448 : // fdo#65014 : Ensure that the point of the cursor is at the extremity of the
449 : // selection closest to the end being searched to as to exclude the selected
450 : // region from the search. (This doesn't work in the case of multiple
451 : // selected regions as the cursor doesn't mark the selection in that case.)
452 0 : m_pWrtShell->GetCrsr()->Normalize( m_pSrchItem->GetBackward() );
453 :
454 : // falls in selektierten Bereichen gesucht werden soll, duerfen sie
455 : // nicht aufgehoben werden
456 0 : if (!m_pSrchItem->GetSelection())
457 0 : m_pWrtShell->KillSelection(0, false);
458 :
459 0 : SwWait *pWait = new SwWait( *GetDocShell(), sal_True );
460 0 : if( FUNC_Search( aOpts ) )
461 : {
462 0 : m_bFound = sal_True;
463 0 : if(m_pWrtShell->IsSelFrmMode())
464 : {
465 0 : m_pWrtShell->UnSelectFrm();
466 0 : m_pWrtShell->LeaveSelFrmMode();
467 : }
468 0 : m_pWrtShell->Pop();
469 0 : m_pWrtShell->EndAllAction();
470 0 : delete pWait;
471 0 : return sal_True;
472 : }
473 0 : delete pWait, pWait = 0;
474 :
475 : // Suchen in den Sonderbereichen, wenn keine
476 : // Suche in Selektionen vorliegt. Bei Suche in Selektionen
477 : // wird ohnehin in diesen Sonderbereichen gesucht
478 0 : bool bHasSrchInOther = m_bExtra;
479 0 : if (!m_pSrchItem->GetSelection() && !m_bExtra )
480 : {
481 0 : m_bExtra = true;
482 0 : if( FUNC_Search( aOpts ) )
483 : {
484 0 : m_bFound = sal_True;
485 0 : m_pWrtShell->Pop();
486 0 : m_pWrtShell->EndAllAction();
487 0 : return sal_True;
488 : }
489 0 : m_bExtra = false;
490 : }
491 : else
492 0 : m_bExtra = !m_bExtra;
493 :
494 0 : const sal_uInt16 nId = SvxSearchDialogWrapper::GetChildWindowId();
495 0 : SvxSearchDialogWrapper *pWrp = (SvxSearchDialogWrapper*)GetViewFrame()->GetChildWindow(nId);
496 0 : m_pSrchDlg = pWrp ? static_cast <SvxSearchDialog*> (pWrp->getDialog ()) : 0;
497 :
498 : // falls Startposition am Dokumentende / -anfang
499 0 : if (aOpts.bDontWrap)
500 : {
501 0 : m_pWrtShell->EndAllAction();
502 0 : if( !bApi )
503 : {
504 0 : ShowNotFoundInfoBox( m_pSrchDlg );
505 : }
506 0 : m_bFound = sal_False;
507 0 : m_pWrtShell->Pop();
508 0 : return sal_False;
509 : }
510 0 : m_pWrtShell->EndAllAction();
511 : // noch mal mit WrapAround versuchen?
512 :
513 0 : if( bApi || RET_NO == QueryBox( GetParentWindow( m_pSrchDlg ),
514 0 : SW_RES( DOCPOS_START == aOpts.eEnd
515 : ? MSG_SEARCH_START
516 : : MSG_SEARCH_END )
517 0 : ).Execute() )
518 : {
519 0 : m_bFound = sal_False;
520 0 : m_pWrtShell->Pop();
521 0 : return sal_False;
522 : }
523 0 : m_pWrtShell->StartAllAction();
524 0 : m_pWrtShell->Pop(sal_False);
525 0 : pWait = new SwWait( *GetDocShell(), sal_True );
526 :
527 0 : bool bSrchBkwrd = DOCPOS_START == aOpts.eEnd;
528 :
529 0 : aOpts.eEnd = bSrchBkwrd ? DOCPOS_START : DOCPOS_END;
530 0 : aOpts.eStart = bSrchBkwrd ? DOCPOS_END : DOCPOS_START;
531 :
532 0 : if (bHasSrchInOther)
533 : {
534 0 : m_pWrtShell->ClearMark();
535 0 : if (bSrchBkwrd)
536 0 : m_pWrtShell->EndDoc();
537 : else
538 0 : m_pWrtShell->SttDoc();
539 : }
540 :
541 0 : m_bFound = 0 != FUNC_Search( aOpts );
542 0 : m_pWrtShell->EndAllAction();
543 0 : delete pWait;
544 0 : if ( m_bFound )
545 0 : return m_bFound;
546 0 : if(!bApi)
547 : {
548 0 : ShowNotFoundInfoBox( m_pSrchDlg );
549 : }
550 0 : return m_bFound = sal_False;
551 : }
552 :
553 :
554 0 : sal_Bool SwView::SearchAll(sal_uInt16* pFound)
555 : {
556 0 : SwWait aWait( *GetDocShell(), sal_True );
557 0 : m_pWrtShell->StartAllAction();
558 :
559 0 : SwSearchOptions aOpts( m_pWrtShell, m_pSrchItem->GetBackward() );
560 :
561 0 : if (!m_pSrchItem->GetSelection())
562 : {
563 : // bestehende Selektionen aufheben,
564 : // wenn nicht in selektierten Bereichen gesucht werden soll
565 0 : m_pWrtShell->KillSelection(0, false);
566 :
567 0 : if( DOCPOS_START == aOpts.eEnd )
568 0 : m_pWrtShell->EndDoc();
569 : else
570 0 : m_pWrtShell->SttDoc();
571 : }
572 0 : m_bExtra = false;
573 0 : sal_uInt16 nFound = (sal_uInt16)FUNC_Search( aOpts );
574 0 : if(pFound)
575 0 : *pFound = nFound;
576 0 : m_bFound = 0 != nFound;
577 :
578 0 : m_pWrtShell->EndAllAction();
579 0 : return m_bFound;
580 : }
581 :
582 :
583 0 : void SwView::Replace()
584 : {
585 0 : SwWait aWait( *GetDocShell(), sal_True );
586 :
587 0 : m_pWrtShell->StartAllAction();
588 :
589 0 : if( m_pSrchItem->GetPattern() ) // Vorlagen?
590 : {
591 0 : SwRewriter aRewriter;
592 0 : aRewriter.AddRule(UndoArg1, m_pSrchItem->GetSearchString());
593 0 : aRewriter.AddRule(UndoArg2, SW_RESSTR(STR_YIELDS));
594 0 : aRewriter.AddRule(UndoArg3, m_pSrchItem->GetReplaceString());
595 :
596 0 : m_pWrtShell->StartUndo(UNDO_UI_REPLACE_STYLE, &aRewriter);
597 :
598 : m_pWrtShell->SetTxtFmtColl( m_pWrtShell->GetParaStyle(
599 : m_pSrchItem->GetReplaceString(),
600 0 : SwWrtShell::GETSTYLE_CREATESOME ));
601 :
602 0 : m_pWrtShell->EndUndo();
603 : }
604 : else
605 : {
606 0 : if (GetPostItMgr()->HasActiveSidebarWin())
607 0 : GetPostItMgr()->Replace(m_pSrchItem);
608 :
609 0 : sal_Bool bReqReplace = true;
610 :
611 0 : if(m_pWrtShell->HasSelection())
612 : {
613 : /* check that the selection match the search string*/
614 : //save state
615 0 : SwPosition aStartPos = (* m_pWrtShell->GetSwCrsr()->Start());
616 0 : SwPosition aEndPos = (* m_pWrtShell->GetSwCrsr()->End());
617 0 : sal_Bool bHasSelection = m_pSrchItem->GetSelection();
618 0 : sal_uInt16 nOldCmd = m_pSrchItem->GetCommand();
619 :
620 : //set state for checking if current selection has a match
621 0 : m_pSrchItem->SetCommand( SVX_SEARCHCMD_FIND );
622 0 : m_pSrchItem->SetSelection(true);
623 :
624 : //check if it matchs
625 0 : SwSearchOptions aOpts( m_pWrtShell, m_pSrchItem->GetBackward() );
626 0 : if( ! FUNC_Search(aOpts) )
627 : {
628 :
629 : //no matching therefore should not replace selection
630 : // => remove selection
631 :
632 0 : if(! m_pSrchItem->GetBackward() )
633 : {
634 0 : (* m_pWrtShell->GetSwCrsr()->Start()) = aStartPos;
635 0 : (* m_pWrtShell->GetSwCrsr()->End()) = aEndPos;
636 : }
637 : else
638 : {
639 0 : (* m_pWrtShell->GetSwCrsr()->Start()) = aEndPos;
640 0 : (* m_pWrtShell->GetSwCrsr()->End()) = aStartPos;
641 : }
642 0 : bReqReplace = false;
643 : }
644 :
645 : //set back old search state
646 0 : m_pSrchItem->SetCommand( nOldCmd );
647 0 : m_pSrchItem->SetSelection(bHasSelection);
648 : }
649 : /*
650 : * remove current selection
651 : * otherwise it is always replaced
652 : * no matter if the search string exists or not in the selection
653 : * Now the selection is removed and the next matching string is selected
654 : */
655 :
656 0 : if( bReqReplace )
657 : {
658 :
659 : sal_Bool bReplaced = m_pWrtShell->SwEditShell::Replace( m_pSrchItem->GetReplaceString(),
660 0 : m_pSrchItem->GetRegExp());
661 0 : if( bReplaced && m_pReplList && m_pReplList->Count() && m_pWrtShell->HasSelection() )
662 : {
663 0 : SfxItemSet aReplSet( m_pWrtShell->GetAttrPool(),
664 0 : aTxtFmtCollSetRange );
665 0 : if( m_pReplList->Get( aReplSet ).Count() )
666 : {
667 0 : ::SfxToSwPageDescAttr( *m_pWrtShell, aReplSet );
668 0 : m_pWrtShell->SwEditShell::SetAttr( aReplSet );
669 0 : }
670 : }
671 : }
672 : }
673 :
674 0 : m_pWrtShell->EndAllAction();
675 0 : }
676 :
677 :
678 :
679 0 : SwSearchOptions::SwSearchOptions( SwWrtShell* pSh, sal_Bool bBackward )
680 : {
681 0 : eStart = DOCPOS_CURR;
682 0 : if( bBackward )
683 : {
684 0 : eEnd = DOCPOS_START;
685 0 : bDontWrap = pSh->IsEndOfDoc();
686 : }
687 : else
688 : {
689 0 : eEnd = DOCPOS_END;
690 0 : bDontWrap = pSh->IsStartOfDoc();
691 : }
692 0 : }
693 :
694 0 : sal_uLong SwView::FUNC_Search( const SwSearchOptions& rOptions )
695 : {
696 0 : sal_Bool bDoReplace = m_pSrchItem->GetCommand() == SVX_SEARCHCMD_REPLACE ||
697 0 : m_pSrchItem->GetCommand() == SVX_SEARCHCMD_REPLACE_ALL;
698 :
699 0 : int eRanges = m_pSrchItem->GetSelection() ?
700 0 : FND_IN_SEL : m_bExtra ? FND_IN_OTHER : FND_IN_BODY;
701 0 : if (m_pSrchItem->GetCommand() == SVX_SEARCHCMD_FIND_ALL ||
702 0 : m_pSrchItem->GetCommand() == SVX_SEARCHCMD_REPLACE_ALL)
703 0 : eRanges |= FND_IN_SELALL;
704 :
705 0 : m_pWrtShell->SttSelect();
706 :
707 : static sal_uInt16 aSearchAttrRange[] = {
708 : RES_FRMATR_BEGIN, RES_FRMATR_END-1,
709 : RES_CHRATR_BEGIN, RES_CHRATR_END-1,
710 : RES_PARATR_BEGIN, RES_PARATR_END-1,
711 : SID_ATTR_PARA_MODEL, SID_ATTR_PARA_KEEP,
712 : 0 };
713 :
714 0 : SfxItemSet aSrchSet( m_pWrtShell->GetAttrPool(), aSearchAttrRange);
715 0 : if( m_pSrchList && m_pSrchList->Count() )
716 : {
717 0 : m_pSrchList->Get( aSrchSet );
718 :
719 : /* -- Seitenumbruch mit Seitenvorlage */
720 0 : ::SfxToSwPageDescAttr( *m_pWrtShell, aSrchSet );
721 : }
722 :
723 0 : SfxItemSet* pReplSet = 0;
724 0 : if( bDoReplace && m_pReplList && m_pReplList->Count() )
725 : {
726 0 : pReplSet = new SfxItemSet( m_pWrtShell->GetAttrPool(),
727 0 : aSearchAttrRange );
728 0 : m_pReplList->Get( *pReplSet );
729 :
730 : /* -- Seitenumbruch mit Seitenvorlage */
731 0 : ::SfxToSwPageDescAttr( *m_pWrtShell, *pReplSet );
732 :
733 0 : if( !pReplSet->Count() ) // schade, die Attribute
734 0 : DELETEZ( pReplSet ); // kennen wir nicht
735 : }
736 :
737 : //
738 : // build SearchOptions to be used
739 : //
740 0 : SearchOptions aSearchOpt( m_pSrchItem->GetSearchOptions() );
741 0 : aSearchOpt.Locale = GetAppLanguageTag().getLocale();
742 0 : if( !bDoReplace )
743 0 : aSearchOpt.replaceString = aEmptyStr;
744 :
745 : sal_uLong nFound;
746 0 : if( aSrchSet.Count() || ( pReplSet && pReplSet->Count() ))
747 : {
748 : nFound = m_pWrtShell->SearchAttr(
749 : aSrchSet,
750 0 : !m_pSrchItem->GetPattern(),
751 : rOptions.eStart,
752 : rOptions.eEnd,
753 : FindRanges(eRanges),
754 0 : !m_pSrchItem->GetSearchString().isEmpty() ? &aSearchOpt : 0,
755 0 : pReplSet );
756 : }
757 0 : else if( m_pSrchItem->GetPattern() )
758 : {
759 : // Suchen (und ersetzen) von Vorlagen
760 0 : const String sRplStr( m_pSrchItem->GetReplaceString() );
761 : nFound = m_pWrtShell->SearchTempl( m_pSrchItem->GetSearchString(),
762 : rOptions.eStart,
763 : rOptions.eEnd,
764 : FindRanges(eRanges),
765 0 : bDoReplace ? &sRplStr : 0 );
766 : }
767 : else
768 : {
769 : // Normale Suche
770 0 : nFound = m_pWrtShell->SearchPattern(aSearchOpt, m_pSrchItem->GetNotes(),
771 : rOptions.eStart,
772 : rOptions.eEnd,
773 : FindRanges(eRanges),
774 0 : bDoReplace );
775 : }
776 0 : m_pWrtShell->EndSelect();
777 0 : return nFound;
778 : }
779 :
780 0 : Dialog* SwView::GetSearchDialog()
781 : {
782 0 : const sal_uInt16 nId = SvxSearchDialogWrapper::GetChildWindowId();
783 0 : SvxSearchDialogWrapper *pWrp = (SvxSearchDialogWrapper*)SfxViewFrame::Current()->GetChildWindow(nId);
784 0 : if ( pWrp )
785 0 : m_pSrchDlg = pWrp->getDialog ();
786 : else
787 0 : m_pSrchDlg = 0;
788 0 : return m_pSrchDlg;
789 : }
790 :
791 0 : void SwView::StateSearch(SfxItemSet &rSet)
792 : {
793 0 : SfxWhichIter aIter(rSet);
794 0 : sal_uInt16 nWhich = aIter.FirstWhich();
795 :
796 0 : while(nWhich)
797 : {
798 0 : switch(nWhich)
799 : {
800 : case SID_SEARCH_OPTIONS:
801 : {
802 0 : sal_uInt16 nOpt = 0xFFFF;
803 0 : if( GetDocShell()->IsReadOnly() )
804 : nOpt &= ~( SEARCH_OPTIONS_REPLACE |
805 0 : SEARCH_OPTIONS_REPLACE_ALL );
806 0 : rSet.Put( SfxUInt16Item( SID_SEARCH_OPTIONS, nOpt));
807 : }
808 0 : break;
809 : case SID_SEARCH_ITEM:
810 : {
811 0 : if ( !m_pSrchItem )
812 : {
813 0 : m_pSrchItem = new SvxSearchItem( SID_SEARCH_ITEM );
814 0 : m_pSrchItem->SetFamily(SFX_STYLE_FAMILY_PARA);
815 0 : m_pSrchItem->SetSearchString( m_pWrtShell->GetSelTxt() );
816 : }
817 :
818 0 : if( m_bJustOpened && m_pWrtShell->IsSelection() )
819 : {
820 0 : String aTxt;
821 0 : if( 1 == m_pWrtShell->GetCrsrCnt() &&
822 0 : ( aTxt = m_pWrtShell->SwCrsrShell::GetSelTxt() ).Len() )
823 : {
824 0 : m_pSrchItem->SetSearchString( aTxt );
825 0 : m_pSrchItem->SetSelection( sal_False );
826 : }
827 : else
828 0 : m_pSrchItem->SetSelection( sal_True );
829 : }
830 :
831 0 : m_bJustOpened = false;
832 0 : rSet.Put( *m_pSrchItem );
833 : }
834 0 : break;
835 : }
836 0 : nWhich = aIter.NextWhich();
837 0 : }
838 99 : }
839 :
840 :
841 :
842 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|