bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / ui / uiview / viewsrch.cxx
blob7e07a36abff905bcd8b87b5bfa0b7414e625f55e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <string>
23 #include <boost/scoped_ptr.hpp>
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>
57 #include <view.hrc>
58 #include <SwRewriter.hxx>
59 #include <comcore.hrc>
61 #include "PostItMgr.hxx"
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;
68 /*--------------------------------------------------------------------
69 Beschreibung: Search Parameter
70 --------------------------------------------------------------------*/
72 struct SwSearchOptions
74 SwDocPositions eStart, eEnd;
75 sal_Bool bDontWrap;
77 SwSearchOptions( SwWrtShell* pSh, sal_Bool bBackward );
81 inline Window* GetParentWindow( SvxSearchDialog* m_pSrchDlg )
83 Window* pWin;
84 if( m_pSrchDlg && m_pSrchDlg->IsVisible() )
85 pWin = m_pSrchDlg;
86 else
87 pWin = 0;
88 return pWin;
91 inline void ShowNotFoundInfoBox( SvxSearchDialog* m_pSrchDlg )
93 Window* pParentWindow = GetParentWindow( m_pSrchDlg );
94 InfoBox aBox( pParentWindow, SW_RES(MSG_NOT_FOUND));
95 if (pParentWindow)
97 aBox.SetText(pParentWindow->GetText());
99 else
101 aBox.SetText(SVX_RESSTR(RID_SVXSTR_FINDBAR_FIND));
103 aBox.Execute();
106 void SwView::ExecSearch(SfxRequest& rReq, sal_Bool bNoMessage)
108 const SfxItemSet* pArgs = rReq.GetArgs();
109 const SfxPoolItem* pItem = 0;
110 const sal_uInt16 nId = SvxSearchDialogWrapper::GetChildWindowId();
111 SvxSearchDialogWrapper *pWrp = (SvxSearchDialogWrapper*)GetViewFrame()->GetChildWindow(nId);
112 sal_Bool bQuiet = sal_False;
113 if(pArgs && SFX_ITEM_SET == pArgs->GetItemState(SID_SEARCH_QUIET, sal_False, &pItem))
114 bQuiet = ((const SfxBoolItem*) pItem)->GetValue();
116 sal_Bool bApi = bQuiet | bNoMessage;
118 sal_uInt16 nSlot = rReq.GetSlot();
119 if (nSlot == FN_REPEAT_SEARCH && !m_pSrchItem)
121 if(bApi)
123 rReq.SetReturnValue(SfxBoolItem(nSlot, sal_False));
124 nSlot = 0;
127 if( m_pWrtShell->IsBlockMode() )
128 m_pWrtShell->LeaveBlockMode();
129 switch (nSlot)
131 // erstmal Nichts tun
132 case SID_SEARCH_ITEM:
134 delete m_pSrchItem;
135 m_pSrchItem = (SvxSearchItem*) pArgs->Get(SID_SEARCH_ITEM).Clone();
137 break;
139 case FID_SEARCH_ON:
140 m_bJustOpened = true;
141 GetViewFrame()->GetBindings().Invalidate(SID_SEARCH_ITEM);
142 break;
144 case FID_SEARCH_OFF:
145 if(pArgs)
147 // Dialog abmelden
148 delete m_pSrchItem;
149 m_pSrchItem = (SvxSearchItem*) pArgs->Get(SID_SEARCH_ITEM).Clone();
151 DELETEZ( m_pSrchList );
152 DELETEZ( m_pReplList );
154 if ( pWrp )
156 m_pSrchDlg = static_cast <SvxSearchDialog*> (pWrp->getDialog ());
157 // die Search / Replace -Items merken wir uns
158 const SearchAttrItemList* pList = m_pSrchDlg->GetSearchItemList();
159 if( pList && pList->Count() )
160 m_pSrchList = new SearchAttrItemList( *pList );
162 if( 0 != (pList = m_pSrchDlg->GetReplaceItemList() ) &&
163 pList->Count() )
164 m_pReplList = new SearchAttrItemList( *pList );
167 break;
169 case FN_REPEAT_SEARCH:
170 case FID_SEARCH_NOW:
173 if(FID_SEARCH_NOW == nSlot && !rReq.IsAPI())
174 SwView::SetMoveType(NID_SRCH_REP);
175 if ( pWrp )
177 m_pSrchDlg = static_cast <SvxSearchDialog*> (pWrp->getDialog ());
179 else
180 m_pSrchDlg = 0;
183 if (m_pSrchDlg)
185 DELETEZ( m_pSrchList );
186 DELETEZ( m_pReplList );
188 const SearchAttrItemList* pList = m_pSrchDlg->GetSearchItemList();
189 if( pList && pList->Count() )
190 m_pSrchList = new SearchAttrItemList( *pList );
192 if( 0 != (pList = m_pSrchDlg->GetReplaceItemList() ) &&
193 pList->Count() )
194 m_pReplList = new SearchAttrItemList( *pList );
197 if (nSlot == FN_REPEAT_SEARCH)
199 OSL_ENSURE(m_pSrchItem, "SearchItem missing");
200 if( !m_pSrchItem )
201 m_pSrchItem = new SvxSearchItem(SID_SEARCH_ITEM);
203 else
205 // SearchItem aus Request besorgen
206 OSL_ENSURE(pArgs, "Args missing");
207 if ( pArgs )
209 delete m_pSrchItem;
210 m_pSrchItem = (SvxSearchItem*) pArgs->Get(SID_SEARCH_ITEM).Clone();
213 switch (m_pSrchItem->GetCommand())
215 case SVX_SEARCHCMD_FIND:
217 sal_Bool bRet = SearchAndWrap(bApi);
218 if( bRet )
219 Scroll(m_pWrtShell->GetCharRect().SVRect());
220 rReq.SetReturnValue(SfxBoolItem(nSlot, bRet));
222 break;
223 case SVX_SEARCHCMD_FIND_ALL:
225 sal_Bool bRet = SearchAll();
226 if( !bRet )
228 if( !bApi )
230 ShowNotFoundInfoBox( m_pSrchDlg );
232 m_bFound = sal_False;
234 rReq.SetReturnValue(SfxBoolItem(nSlot, bRet));
236 break;
237 case SVX_SEARCHCMD_REPLACE:
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??
245 sal_uInt16 nCmd = SVX_SEARCHCMD_FIND;
246 if( m_pSrchItem->GetReplaceString().Len() ||
247 !m_pReplList )
249 // Verhindern, dass - falls der Suchstring im
250 // Ersetzungsstring enthalten ist - der ersetzte String
251 // noch einmal gefunden wird.
253 sal_Bool bBack = m_pSrchItem->GetBackward();
254 if (bBack)
255 m_pWrtShell->Push();
256 String aReplace( m_pSrchItem->GetReplaceString() );
257 SearchOptions aTmp( m_pSrchItem->GetSearchOptions() );
258 String *pBackRef = ReplaceBackReferences( aTmp, m_pWrtShell->GetCrsr() );
259 if( pBackRef )
260 m_pSrchItem->SetReplaceString( *pBackRef );
261 Replace();
262 if( pBackRef )
264 m_pSrchItem->SetReplaceString( aReplace );
265 delete pBackRef;
267 if (bBack)
269 m_pWrtShell->Pop();
270 m_pWrtShell->SwapPam();
273 else if( m_pReplList )
274 nCmd = SVX_SEARCHCMD_REPLACE;
276 // 2) Weiter suchen (ohne zu ersetzen!)
278 sal_uInt16 nOldCmd = m_pSrchItem->GetCommand();
279 m_pSrchItem->SetCommand( nCmd );
280 sal_Bool bRet = SearchAndWrap(bApi);
281 if( bRet )
282 Scroll( m_pWrtShell->GetCharRect().SVRect());
283 m_pSrchItem->SetCommand( nOldCmd );
284 rReq.SetReturnValue(SfxBoolItem(nSlot, bRet));
286 break;
288 case SVX_SEARCHCMD_REPLACE_ALL:
290 SwSearchOptions aOpts( m_pWrtShell, m_pSrchItem->GetBackward() );
291 m_bExtra = sal_False;
292 sal_uLong nFound;
294 { //Scope for SwWait-Object
295 SwWait aWait( *GetDocShell(), sal_True );
296 m_pWrtShell->StartAllAction();
297 if (!m_pSrchItem->GetSelection())
299 // if we don't want to search in the selection...
300 m_pWrtShell->KillSelection(0, false);
301 // i#8288 "replace all" should not change cursor
302 // position, so save current cursor
303 m_pWrtShell->Push();
304 if (DOCPOS_START == aOpts.eEnd)
306 m_pWrtShell->EndDoc();
308 else
310 m_pWrtShell->SttDoc();
313 nFound = FUNC_Search( aOpts );
314 if (!m_pSrchItem->GetSelection())
316 // create it just to overwrite it with stack cursor
317 m_pWrtShell->CreateCrsr();
318 // i#8288 restore the original cursor position
319 m_pWrtShell->Pop(false);
321 m_pWrtShell->EndAllAction();
324 rReq.SetReturnValue(SfxBoolItem(nSlot, nFound != 0 && ULONG_MAX != nFound));
325 if( !nFound )
327 if( !bApi )
329 ShowNotFoundInfoBox( m_pSrchDlg );
331 m_bFound = sal_False;
332 return;
335 if( !bApi && ULONG_MAX != nFound)
337 String aText( SW_RES( STR_NB_REPLACED ) );
338 const xub_StrLen nPos = aText.Search( OUString("XX") );
339 aText.Erase( nPos, 2 );
340 aText.Insert( OUString::number( nFound ), nPos );
341 Window* pParentWindow = GetParentWindow( m_pSrchDlg );
342 InfoBox( pParentWindow, aText ).Execute();
345 break;
348 uno::Reference< frame::XDispatchRecorder > xRecorder =
349 GetViewFrame()->GetBindings().GetRecorder();
350 //prevent additional dialogs in recorded macros
351 if ( xRecorder.is() )
352 rReq.AppendItem(SfxBoolItem(SID_SEARCH_QUIET, sal_True));
354 rReq.Done();
356 break;
357 case FID_SEARCH_SEARCHSET:
358 case FID_SEARCH_REPLACESET:
360 static const sal_uInt16 aNormalAttr[] =
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
378 static const sal_uInt16 aCJKAttr[] =
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
384 static const sal_uInt16 aCTLAttr[] =
386 RES_CHRATR_CTL_FONT, RES_CHRATR_CTL_WEIGHT
389 std::vector<sal_uInt16> aArr;
390 aArr.insert( aArr.begin(), aNormalAttr,
391 aNormalAttr + SAL_N_ELEMENTS( aNormalAttr ));
392 if( SW_MOD()->GetCTLOptions().IsCTLFontEnabled() )
394 aArr.insert( aArr.begin() + 14, aCTLAttr,
395 aCTLAttr + SAL_N_ELEMENTS( aCTLAttr ));
397 SvtCJKOptions aCJKOpt;
398 if( aCJKOpt.IsAnyEnabled() )
400 aArr.insert( aArr.begin() + 14, aCJKAttr,
401 aCJKAttr + SAL_N_ELEMENTS( aCJKAttr ));
404 SfxItemSet aSet( m_pWrtShell->GetAttrPool(), &aArr[0] );
405 sal_uInt16 nWhich = SID_SEARCH_SEARCHSET;
407 if ( FID_SEARCH_REPLACESET == nSlot )
409 nWhich = SID_SEARCH_REPLACESET;
411 if ( m_pReplList )
413 m_pReplList->Get( aSet );
414 DELETEZ( m_pReplList );
417 else if ( m_pSrchList )
419 m_pSrchList->Get( aSet );
420 DELETEZ( m_pSrchList );
422 rReq.SetReturnValue( SvxSetItem( nWhich, aSet ) );
424 break;
425 default:
426 #if OSL_DEBUG_LEVEL > 1
427 if(nSlot)
429 OString sStr("nSlot: " + OString::number(nSlot) + " wrong Dispatcher (viewsrch.cxx)");
430 OSL_FAIL(sStr.getStr());
432 #endif
433 return;
438 sal_Bool SwView::SearchAndWrap(sal_Bool bApi)
440 SwSearchOptions aOpts( m_pWrtShell, m_pSrchItem->GetBackward() );
442 // Startposition der Suche fuer WrapAround merken
443 // Start- / EndAction wegen vielleicht bestehender Selektionen
444 // aus 'Suche alle'
445 m_pWrtShell->StartAllAction();
446 m_pWrtShell->Push();
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 m_pWrtShell->GetCrsr()->Normalize( m_pSrchItem->GetBackward() );
454 // falls in selektierten Bereichen gesucht werden soll, duerfen sie
455 // nicht aufgehoben werden
456 if (!m_pSrchItem->GetSelection())
457 m_pWrtShell->KillSelection(0, false);
459 SwWait *pWait = new SwWait( *GetDocShell(), sal_True );
460 if( FUNC_Search( aOpts ) )
462 m_bFound = sal_True;
463 if(m_pWrtShell->IsSelFrmMode())
465 m_pWrtShell->UnSelectFrm();
466 m_pWrtShell->LeaveSelFrmMode();
468 m_pWrtShell->Pop();
469 m_pWrtShell->EndAllAction();
470 delete pWait;
471 return sal_True;
473 delete pWait, pWait = 0;
475 // Suchen in den Sonderbereichen, wenn keine
476 // Suche in Selektionen vorliegt. Bei Suche in Selektionen
477 // wird ohnehin in diesen Sonderbereichen gesucht
478 bool bHasSrchInOther = m_bExtra;
479 if (!m_pSrchItem->GetSelection() && !m_bExtra )
481 m_bExtra = true;
482 if( FUNC_Search( aOpts ) )
484 m_bFound = sal_True;
485 m_pWrtShell->Pop();
486 m_pWrtShell->EndAllAction();
487 return sal_True;
489 m_bExtra = false;
491 else
492 m_bExtra = !m_bExtra;
494 const sal_uInt16 nId = SvxSearchDialogWrapper::GetChildWindowId();
495 SvxSearchDialogWrapper *pWrp = (SvxSearchDialogWrapper*)GetViewFrame()->GetChildWindow(nId);
496 m_pSrchDlg = pWrp ? static_cast <SvxSearchDialog*> (pWrp->getDialog ()) : 0;
498 // falls Startposition am Dokumentende / -anfang
499 if (aOpts.bDontWrap)
501 m_pWrtShell->EndAllAction();
502 if( !bApi )
504 ShowNotFoundInfoBox( m_pSrchDlg );
506 m_bFound = sal_False;
507 m_pWrtShell->Pop();
508 return sal_False;
510 m_pWrtShell->EndAllAction();
511 // noch mal mit WrapAround versuchen?
513 if( bApi || RET_NO == QueryBox( GetParentWindow( m_pSrchDlg ),
514 SW_RES( DOCPOS_START == aOpts.eEnd
515 ? MSG_SEARCH_START
516 : MSG_SEARCH_END )
517 ).Execute() )
519 m_bFound = sal_False;
520 m_pWrtShell->Pop();
521 return sal_False;
523 m_pWrtShell->StartAllAction();
524 m_pWrtShell->Pop(sal_False);
525 pWait = new SwWait( *GetDocShell(), sal_True );
527 bool bSrchBkwrd = DOCPOS_START == aOpts.eEnd;
529 aOpts.eEnd = bSrchBkwrd ? DOCPOS_START : DOCPOS_END;
530 aOpts.eStart = bSrchBkwrd ? DOCPOS_END : DOCPOS_START;
532 if (bHasSrchInOther)
534 m_pWrtShell->ClearMark();
535 if (bSrchBkwrd)
536 m_pWrtShell->EndDoc();
537 else
538 m_pWrtShell->SttDoc();
541 m_bFound = 0 != FUNC_Search( aOpts );
542 m_pWrtShell->EndAllAction();
543 delete pWait;
544 if ( m_bFound )
545 return m_bFound;
546 if(!bApi)
548 ShowNotFoundInfoBox( m_pSrchDlg );
550 return m_bFound = sal_False;
554 sal_Bool SwView::SearchAll(sal_uInt16* pFound)
556 SwWait aWait( *GetDocShell(), sal_True );
557 m_pWrtShell->StartAllAction();
559 SwSearchOptions aOpts( m_pWrtShell, m_pSrchItem->GetBackward() );
561 if (!m_pSrchItem->GetSelection())
563 // bestehende Selektionen aufheben,
564 // wenn nicht in selektierten Bereichen gesucht werden soll
565 m_pWrtShell->KillSelection(0, false);
567 if( DOCPOS_START == aOpts.eEnd )
568 m_pWrtShell->EndDoc();
569 else
570 m_pWrtShell->SttDoc();
572 m_bExtra = false;
573 sal_uInt16 nFound = (sal_uInt16)FUNC_Search( aOpts );
574 if(pFound)
575 *pFound = nFound;
576 m_bFound = 0 != nFound;
578 m_pWrtShell->EndAllAction();
579 return m_bFound;
583 void SwView::Replace()
585 SwWait aWait( *GetDocShell(), sal_True );
587 m_pWrtShell->StartAllAction();
589 if( m_pSrchItem->GetPattern() ) // Vorlagen?
591 SwRewriter aRewriter;
592 aRewriter.AddRule(UndoArg1, m_pSrchItem->GetSearchString());
593 aRewriter.AddRule(UndoArg2, SW_RESSTR(STR_YIELDS));
594 aRewriter.AddRule(UndoArg3, m_pSrchItem->GetReplaceString());
596 m_pWrtShell->StartUndo(UNDO_UI_REPLACE_STYLE, &aRewriter);
598 m_pWrtShell->SetTxtFmtColl( m_pWrtShell->GetParaStyle(
599 m_pSrchItem->GetReplaceString(),
600 SwWrtShell::GETSTYLE_CREATESOME ));
602 m_pWrtShell->EndUndo();
604 else
606 if (GetPostItMgr()->HasActiveSidebarWin())
607 GetPostItMgr()->Replace(m_pSrchItem);
609 sal_Bool bReqReplace = true;
611 if(m_pWrtShell->HasSelection())
613 /* check that the selection match the search string*/
614 //save state
615 SwPosition aStartPos = (* m_pWrtShell->GetSwCrsr()->Start());
616 SwPosition aEndPos = (* m_pWrtShell->GetSwCrsr()->End());
617 sal_Bool bHasSelection = m_pSrchItem->GetSelection();
618 sal_uInt16 nOldCmd = m_pSrchItem->GetCommand();
620 //set state for checking if current selection has a match
621 m_pSrchItem->SetCommand( SVX_SEARCHCMD_FIND );
622 m_pSrchItem->SetSelection(true);
624 //check if it matchs
625 SwSearchOptions aOpts( m_pWrtShell, m_pSrchItem->GetBackward() );
626 if( ! FUNC_Search(aOpts) )
629 //no matching therefore should not replace selection
630 // => remove selection
632 if(! m_pSrchItem->GetBackward() )
634 (* m_pWrtShell->GetSwCrsr()->Start()) = aStartPos;
635 (* m_pWrtShell->GetSwCrsr()->End()) = aEndPos;
637 else
639 (* m_pWrtShell->GetSwCrsr()->Start()) = aEndPos;
640 (* m_pWrtShell->GetSwCrsr()->End()) = aStartPos;
642 bReqReplace = false;
645 //set back old search state
646 m_pSrchItem->SetCommand( nOldCmd );
647 m_pSrchItem->SetSelection(bHasSelection);
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
656 if( bReqReplace )
659 sal_Bool bReplaced = m_pWrtShell->SwEditShell::Replace( m_pSrchItem->GetReplaceString(),
660 m_pSrchItem->GetRegExp());
661 if( bReplaced && m_pReplList && m_pReplList->Count() && m_pWrtShell->HasSelection() )
663 SfxItemSet aReplSet( m_pWrtShell->GetAttrPool(),
664 aTxtFmtCollSetRange );
665 if( m_pReplList->Get( aReplSet ).Count() )
667 ::SfxToSwPageDescAttr( *m_pWrtShell, aReplSet );
668 m_pWrtShell->SwEditShell::SetAttr( aReplSet );
674 m_pWrtShell->EndAllAction();
679 SwSearchOptions::SwSearchOptions( SwWrtShell* pSh, sal_Bool bBackward )
681 eStart = DOCPOS_CURR;
682 if( bBackward )
684 eEnd = DOCPOS_START;
685 bDontWrap = pSh->IsEndOfDoc();
687 else
689 eEnd = DOCPOS_END;
690 bDontWrap = pSh->IsStartOfDoc();
694 sal_uLong SwView::FUNC_Search( const SwSearchOptions& rOptions )
696 sal_Bool bDoReplace = m_pSrchItem->GetCommand() == SVX_SEARCHCMD_REPLACE ||
697 m_pSrchItem->GetCommand() == SVX_SEARCHCMD_REPLACE_ALL;
699 int eRanges = m_pSrchItem->GetSelection() ?
700 FND_IN_SEL : m_bExtra ? FND_IN_OTHER : FND_IN_BODY;
701 if (m_pSrchItem->GetCommand() == SVX_SEARCHCMD_FIND_ALL ||
702 m_pSrchItem->GetCommand() == SVX_SEARCHCMD_REPLACE_ALL)
703 eRanges |= FND_IN_SELALL;
705 m_pWrtShell->SttSelect();
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 };
714 SfxItemSet aSrchSet( m_pWrtShell->GetAttrPool(), aSearchAttrRange);
715 if( m_pSrchList && m_pSrchList->Count() )
717 m_pSrchList->Get( aSrchSet );
719 /* -- Seitenumbruch mit Seitenvorlage */
720 ::SfxToSwPageDescAttr( *m_pWrtShell, aSrchSet );
723 SfxItemSet* pReplSet = 0;
724 if( bDoReplace && m_pReplList && m_pReplList->Count() )
726 pReplSet = new SfxItemSet( m_pWrtShell->GetAttrPool(),
727 aSearchAttrRange );
728 m_pReplList->Get( *pReplSet );
730 /* -- Seitenumbruch mit Seitenvorlage */
731 ::SfxToSwPageDescAttr( *m_pWrtShell, *pReplSet );
733 if( !pReplSet->Count() ) // schade, die Attribute
734 DELETEZ( pReplSet ); // kennen wir nicht
738 // build SearchOptions to be used
740 SearchOptions aSearchOpt( m_pSrchItem->GetSearchOptions() );
741 aSearchOpt.Locale = GetAppLanguageTag().getLocale();
742 if( !bDoReplace )
743 aSearchOpt.replaceString = aEmptyStr;
745 sal_uLong nFound;
746 if( aSrchSet.Count() || ( pReplSet && pReplSet->Count() ))
748 nFound = m_pWrtShell->SearchAttr(
749 aSrchSet,
750 !m_pSrchItem->GetPattern(),
751 rOptions.eStart,
752 rOptions.eEnd,
753 FindRanges(eRanges),
754 m_pSrchItem->GetSearchString().Len() ? &aSearchOpt : 0,
755 pReplSet );
757 else if( m_pSrchItem->GetPattern() )
759 // Suchen (und ersetzen) von Vorlagen
760 const String sRplStr( m_pSrchItem->GetReplaceString() );
761 nFound = m_pWrtShell->SearchTempl( m_pSrchItem->GetSearchString(),
762 rOptions.eStart,
763 rOptions.eEnd,
764 FindRanges(eRanges),
765 bDoReplace ? &sRplStr : 0 );
767 else
769 // Normale Suche
770 nFound = m_pWrtShell->SearchPattern(aSearchOpt, m_pSrchItem->GetNotes(),
771 rOptions.eStart,
772 rOptions.eEnd,
773 FindRanges(eRanges),
774 bDoReplace );
776 m_pWrtShell->EndSelect();
777 return nFound;
780 Dialog* SwView::GetSearchDialog()
782 const sal_uInt16 nId = SvxSearchDialogWrapper::GetChildWindowId();
783 SvxSearchDialogWrapper *pWrp = (SvxSearchDialogWrapper*)SfxViewFrame::Current()->GetChildWindow(nId);
784 if ( pWrp )
785 m_pSrchDlg = pWrp->getDialog ();
786 else
787 m_pSrchDlg = 0;
788 return m_pSrchDlg;
791 void SwView::StateSearch(SfxItemSet &rSet)
793 SfxWhichIter aIter(rSet);
794 sal_uInt16 nWhich = aIter.FirstWhich();
796 while(nWhich)
798 switch(nWhich)
800 case SID_SEARCH_OPTIONS:
802 sal_uInt16 nOpt = 0xFFFF;
803 if( GetDocShell()->IsReadOnly() )
804 nOpt &= ~( SEARCH_OPTIONS_REPLACE |
805 SEARCH_OPTIONS_REPLACE_ALL );
806 rSet.Put( SfxUInt16Item( SID_SEARCH_OPTIONS, nOpt));
808 break;
809 case SID_SEARCH_ITEM:
811 if ( !m_pSrchItem )
813 m_pSrchItem = new SvxSearchItem( SID_SEARCH_ITEM );
814 m_pSrchItem->SetFamily(SFX_STYLE_FAMILY_PARA);
815 m_pSrchItem->SetSearchString( m_pWrtShell->GetSelTxt() );
818 if( m_bJustOpened && m_pWrtShell->IsSelection() )
820 String aTxt;
821 if( 1 == m_pWrtShell->GetCrsrCnt() &&
822 ( aTxt = m_pWrtShell->SwCrsrShell::GetSelTxt() ).Len() )
824 m_pSrchItem->SetSearchString( aTxt );
825 m_pSrchItem->SetSelection( sal_False );
827 else
828 m_pSrchItem->SetSelection( sal_True );
831 m_bJustOpened = false;
832 rSet.Put( *m_pSrchItem );
834 break;
836 nWhich = aIter.NextWhich();
842 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */