LibreOffice Module sw (master)  1
viewsrch.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 
20 #include <string>
21 
22 #include <memory>
23 #include <boost/property_tree/json_parser.hpp>
24 
25 #include <hintids.hxx>
26 
27 #include <sal/log.hxx>
28 #include <svl/cjkoptions.hxx>
29 #include <svl/ctloptions.hxx>
30 #include <svx/pageitem.hxx>
31 #include <svl/whiter.hxx>
32 #include <sfx2/viewfrm.hxx>
33 #include <svl/eitem.hxx>
34 #include <svl/srchitem.hxx>
35 #include <sfx2/bindings.hxx>
36 #include <sfx2/request.hxx>
37 #include <svx/srchdlg.hxx>
38 #include <swmodule.hxx>
39 #include <swwait.hxx>
40 #include <workctrl.hxx>
41 #include <view.hxx>
42 #include <wrtsh.hxx>
43 #include <swundo.hxx>
44 #include <uitool.hxx>
45 #include <cmdid.h>
46 #include <docsh.hxx>
47 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
48 #include <comphelper/lok.hxx>
49 #include <comphelper/string.hxx>
50 
51 #include <strings.hrc>
52 #include <SwRewriter.hxx>
53 
54 #include <PostItMgr.hxx>
55 
56 using namespace com::sun::star;
57 using namespace ::com::sun::star::i18n;
58 using namespace ::com::sun::star::lang;
59 using namespace ::com::sun::star::util;
60 
61 //Search Parameter
62 
64 {
66  bool bDontWrap;
67 
68  SwSearchOptions( SwWrtShell const * pSh, bool bBackward );
69 };
70 
72 static void lcl_addContainerToJson(boost::property_tree::ptree& rTree, const OString& rKey, const std::vector<OString>& rMatches)
73 {
74  boost::property_tree::ptree aChildren;
75 
76  for (const OString& rMatch : rMatches)
77  {
78  boost::property_tree::ptree aChild;
79  aChild.put("part", "0");
80  aChild.put("rectangles", rMatch.getStr());
81  aChildren.push_back(std::make_pair("", aChild));
82  }
83 
84  rTree.add_child(rKey.getStr(), aChildren);
85 }
86 
88 static void lcl_emitSearchResultCallbacks(SvxSearchItem const * pSearchItem, SwWrtShell const * pWrtShell, bool bHighlightAll)
89 {
90  // Emit a callback also about the selection rectangles, grouped by matches.
91  SwPaM* pPaM = pWrtShell->GetCursor();
92  if (!pPaM)
93  return;
94 
95  std::vector<OString> aMatches;
96  for (SwPaM& rPaM : pPaM->GetRingContainer())
97  {
98  if (SwShellCursor* pShellCursor = dynamic_cast<SwShellCursor*>(&rPaM))
99  {
100  std::vector<OString> aSelectionRectangles;
101  pShellCursor->SwSelPaintRects::Show(&aSelectionRectangles);
102  std::vector<OString> aRect;
103  for (const OString & rSelectionRectangle : aSelectionRectangles)
104  {
105  if (rSelectionRectangle.isEmpty())
106  continue;
107  aRect.push_back(rSelectionRectangle);
108  }
109  OString sRect = comphelper::string::join("; ", aRect);
110  aMatches.push_back(sRect);
111  }
112  }
113  boost::property_tree::ptree aTree;
114  aTree.put("searchString", pSearchItem->GetSearchString().toUtf8().getStr());
115  aTree.put("highlightAll", bHighlightAll);
116  lcl_addContainerToJson(aTree, "searchResultSelection", aMatches);
117 
118  std::stringstream aStream;
119  boost::property_tree::write_json(aStream, aTree);
120  OString aPayload = aStream.str().c_str();
121 
122  pWrtShell->GetSfxViewShell()->libreOfficeKitViewCallback(LOK_CALLBACK_SEARCH_RESULT_SELECTION, aPayload.getStr());
123 }
124 
126 {
127  GetWrtShell().addCurrentPosition();
128 
129  const SfxItemSet* pArgs = rReq.GetArgs();
130  const SfxPoolItem* pItem = nullptr;
131  bool bQuiet = false;
132  if(pArgs && SfxItemState::SET == pArgs->GetItemState(SID_SEARCH_QUIET, false, &pItem))
133  bQuiet = static_cast<const SfxBoolItem*>( pItem)->GetValue();
134 
135  sal_uInt16 nSlot = rReq.GetSlot();
136  if (nSlot == FN_REPEAT_SEARCH && !s_pSrchItem)
137  {
138  if(bQuiet)
139  {
140  rReq.SetReturnValue(SfxBoolItem(nSlot, false));
141  nSlot = 0;
142  }
143  }
144  if( m_pWrtShell->IsBlockMode() )
145  m_pWrtShell->LeaveBlockMode();
146  switch (nSlot)
147  {
148  // for now do nothing
149  case SID_SEARCH_ITEM:
150  {
151  delete s_pSrchItem;
152  s_pSrchItem = pArgs->Get(SID_SEARCH_ITEM).Clone();
153  }
154  break;
155 
156  case FID_SEARCH_ON:
157  s_bJustOpened = true;
158  GetViewFrame()->GetBindings().Invalidate(SID_SEARCH_ITEM);
159  break;
160 
161  case FID_SEARCH_OFF:
162  if(pArgs)
163  {
164  // Unregister dialog
165  delete s_pSrchItem;
166  s_pSrchItem = pArgs->Get(SID_SEARCH_ITEM).Clone();
167 
168  s_xSearchList.reset();
169  s_xReplaceList.reset();
170 
171  SvxSearchDialog *const pSrchDlg(GetSearchDialog());
172  if (pSrchDlg)
173  {
174  // We will remember the search-/replace items.
175  const SearchAttrItemList* pList = pSrchDlg->GetSearchItemList();
176  if( nullptr != pList && pList->Count() )
177  s_xSearchList.reset(new SearchAttrItemList( *pList ));
178 
179  pList = pSrchDlg->GetReplaceItemList();
180  if (nullptr != pList && pList->Count())
181  s_xReplaceList.reset(new SearchAttrItemList( *pList ));
182  }
183  }
184  break;
185 
186  case FN_REPEAT_SEARCH:
187  case FID_SEARCH_NOW:
188  {
189  sal_uInt16 nMoveType = SwView::GetMoveType();
190  {
191  if(FID_SEARCH_NOW == nSlot && !rReq.IsAPI())
193  }
194 
195  SvxSearchDialog * pSrchDlg(GetSearchDialog());
196  if (pSrchDlg)
197  {
198  s_xSearchList.reset();
199  s_xReplaceList.reset();
200 
201  const SearchAttrItemList* pList = pSrchDlg->GetSearchItemList();
202  if( nullptr != pList && pList->Count() )
203  s_xSearchList.reset(new SearchAttrItemList( *pList ));
204 
205  pList = pSrchDlg->GetReplaceItemList();
206  if (nullptr != pList && pList->Count())
207  s_xReplaceList.reset(new SearchAttrItemList( *pList ));
208  }
209 
210  if (nSlot == FN_REPEAT_SEARCH)
211  {
212  OSL_ENSURE(s_pSrchItem, "SearchItem missing");
213  if( !s_pSrchItem )
214  s_pSrchItem = new SvxSearchItem(SID_SEARCH_ITEM);
215  }
216  else
217  {
218  // Get SearchItem from request
219  OSL_ENSURE(pArgs, "Args missing");
220  if ( pArgs )
221  {
222  delete s_pSrchItem;
223  s_pSrchItem = pArgs->Get(SID_SEARCH_ITEM).Clone();
224  }
225  }
226  SvxSearchCmd eCommand = s_pSrchItem->GetCommand();
227  switch (eCommand)
228  {
229  case SvxSearchCmd::FIND:
230  {
231  bool bRet = SearchAndWrap(bQuiet);
232  if( bRet )
233  {
234  Scroll(m_pWrtShell->GetCharRect().SVRect());
236  lcl_emitSearchResultCallbacks(s_pSrchItem, m_pWrtShell.get(), /* bHighlightAll = */ false);
237  }
238  rReq.SetReturnValue(SfxBoolItem(nSlot, bRet));
239 
240  GetDocShell()->Broadcast(SfxHint(SfxHintId::SwNavigatorUpdateTracking));
241  }
242  break;
243  case SvxSearchCmd::FIND_ALL:
244  {
245  // Disable LOK selection notifications during search.
246  m_pWrtShell->GetSfxViewShell()->setTiledSearching(true);
247  bool bRet = SearchAll();
248  m_pWrtShell->GetSfxViewShell()->setTiledSearching(false);
249 
250  GetDocShell()->Broadcast(
251  SfxHint(SfxHintId::SwNavigatorSelectOutlinesWithSelections));
252 
253  if( !bRet )
254  {
255 #if HAVE_FEATURE_DESKTOP
256  if( !bQuiet )
257  {
258  m_pWrtShell->GetSfxViewShell()->libreOfficeKitViewCallback(LOK_CALLBACK_SEARCH_NOT_FOUND, s_pSrchItem->GetSearchString().toUtf8().getStr());
259  SvxSearchDialogWrapper::SetSearchLabel(SearchLabel::NotFound);
260  }
261 #endif
262  s_bFound = false;
263  }
265  lcl_emitSearchResultCallbacks(s_pSrchItem, m_pWrtShell.get(), /* bHighlightAll = */ true);
266  rReq.SetReturnValue(SfxBoolItem(nSlot, bRet));
267  }
268  break;
269  case SvxSearchCmd::REPLACE:
270  {
271 
272  // 1) Replace selection (Not if only attributes should be replaced)
273 //JP 27.04.95: Why?
274 // what if you only want to assign attributes to the found??
275 
276  SvxSearchCmd nCmd = SvxSearchCmd::FIND;
277  if( !s_pSrchItem->GetReplaceString().isEmpty() ||
278  !s_xReplaceList )
279  {
280  // Prevent, that the replaced string will be found again
281  // if the replacement string is containing the search string.
282  bool bBack = s_pSrchItem->GetBackward();
283  if (bBack)
284  m_pWrtShell->Push();
285  OUString aReplace( s_pSrchItem->GetReplaceString() );
286  i18nutil::SearchOptions2 aTmp( s_pSrchItem->GetSearchOptions() );
287  std::optional<OUString> xBackRef = sw::ReplaceBackReferences(aTmp,
288  m_pWrtShell->GetCursor(), m_pWrtShell->GetLayout());
289  if( xBackRef )
290  s_pSrchItem->SetReplaceString( *xBackRef );
291  Replace();
292  if( xBackRef )
293  {
294  s_pSrchItem->SetReplaceString( aReplace );
295  }
296  if (bBack)
297  {
298  m_pWrtShell->Pop();
299  m_pWrtShell->SwapPam();
300  }
301  }
302  else if( s_xReplaceList )
303  nCmd = SvxSearchCmd::REPLACE;
304 
305  // 2) Search further (without replacing!)
306 
307  SvxSearchCmd nOldCmd = s_pSrchItem->GetCommand();
308  s_pSrchItem->SetCommand( nCmd );
309  bool bRet = SearchAndWrap(bQuiet);
310  if( bRet )
311  Scroll( m_pWrtShell->GetCharRect().SVRect());
312  s_pSrchItem->SetCommand( nOldCmd );
313  rReq.SetReturnValue(SfxBoolItem(nSlot, bRet));
314  }
315  break;
316 
317  case SvxSearchCmd::REPLACE_ALL:
318  {
319  SwSearchOptions aOpts( m_pWrtShell.get(), s_pSrchItem->GetBackward() );
320  s_bExtra = false;
321  sal_uLong nFound;
322 
323  { //Scope for SwWait-Object
324  SwWait aWait( *GetDocShell(), true );
325  m_pWrtShell->StartAllAction();
326 
327  // i#8288 "replace all" should not change cursor
328  // position, so save current cursor
329  m_pWrtShell->Push();
330 
331  if (!s_pSrchItem->GetSelection())
332  {
333  // if we don't want to search in the selection...
334  m_pWrtShell->KillSelection(nullptr, false);
335  if (SwDocPositions::Start == aOpts.eEnd)
336  {
337  m_pWrtShell->EndOfSection();
338  }
339  else
340  {
341  m_pWrtShell->StartOfSection();
342  }
343  }
344  nFound = FUNC_Search( aOpts );
345  // create it just to overwrite it with stack cursor
346  m_pWrtShell->CreateCursor();
347  // i#8288 restore the original cursor position
348  m_pWrtShell->Pop(SwCursorShell::PopMode::DeleteCurrent);
349  m_pWrtShell->EndAllAction();
350  }
351 
352  rReq.SetReturnValue(SfxBoolItem(nSlot, nFound != 0 && ULONG_MAX != nFound));
353  if( !nFound )
354  {
355 #if HAVE_FEATURE_DESKTOP
356  if( !bQuiet )
357  {
358  m_pWrtShell->GetSfxViewShell()->libreOfficeKitViewCallback(LOK_CALLBACK_SEARCH_NOT_FOUND, s_pSrchItem->GetSearchString().toUtf8().getStr());
359  SvxSearchDialogWrapper::SetSearchLabel(SearchLabel::NotFound);
360  }
361 #endif
362  s_bFound = false;
363  SwView::SetMoveType(nMoveType);
364  return;
365  }
366 
367  if( !bQuiet && ULONG_MAX != nFound)
368  {
369  OUString sText( SwResId( STR_NB_REPLACED ) );
370  sText = sText.replaceFirst("XX", OUString::number( nFound ));
372  }
373  }
374  break;
375  }
376 
377  uno::Reference< frame::XDispatchRecorder > xRecorder =
378  GetViewFrame()->GetBindings().GetRecorder();
379  //prevent additional dialogs in recorded macros
380  if ( xRecorder.is() )
381  rReq.AppendItem(SfxBoolItem(SID_SEARCH_QUIET, true));
382 
383  rReq.Done();
384  m_eLastSearchCommand = s_pSrchItem->GetCommand();
385  SwView::SetMoveType(nMoveType);
386  }
387  break;
388  case FID_SEARCH_SEARCHSET:
389  case FID_SEARCH_REPLACESET:
390  {
391  static const sal_uInt16 aNormalAttr[] =
392  {
404 /*22 */ RES_LR_SPACE, RES_UL_SPACE,
405 /*24 */ SID_ATTR_PARA_MODEL, SID_ATTR_PARA_KEEP,
406 /*26 */ 0
407  };
408 
409  SfxItemSet aSet(m_pWrtShell->GetAttrPool(), aNormalAttr);
410 
411  if( SW_MOD()->GetCTLOptions().IsCTLFontEnabled() )
412  {
413  aSet.MergeRange(RES_CHRATR_CTL_FONT, RES_CHRATR_CTL_WEIGHT);
414  }
415  SvtCJKOptions aCJKOpt;
416  if( aCJKOpt.IsAnyEnabled() )
417  {
418  aSet.MergeRange(RES_CHRATR_CJK_FONT, RES_CHRATR_CJK_WEIGHT);
421  }
422 
423  sal_uInt16 nWhich = SID_SEARCH_SEARCHSET;
424 
425  if ( FID_SEARCH_REPLACESET == nSlot )
426  {
427  nWhich = SID_SEARCH_REPLACESET;
428 
429  if ( s_xReplaceList )
430  {
431  s_xReplaceList->Get( aSet );
432  s_xReplaceList.reset();
433  }
434  }
435  else if ( s_xSearchList )
436  {
437  s_xSearchList->Get( aSet );
438  s_xSearchList.reset();
439  }
440  rReq.SetReturnValue( SvxSetItem( nWhich, aSet ) );
441  }
442  break;
443  default:
444  SAL_WARN_IF( nSlot, "sw", "nSlot: " << nSlot << " wrong Dispatcher (viewsrch.cxx)" );
445  return;
446  }
447 }
448 
449 bool SwView::SearchAndWrap(bool bApi)
450 {
451  SwSearchOptions aOpts( m_pWrtShell.get(), s_pSrchItem->GetBackward() );
452 
453  // Remember starting position of the search for wraparound
454  // Start- / EndAction perhaps because existing selections of 'search all'
455  m_pWrtShell->StartAllAction();
456  m_pWrtShell->Push();
457 
458  // After a search all action we place the cursor at the beginning of
459  // the document so that the single search selects the first matching
460  // occurrence in the document instead of the second.
461  if( m_eLastSearchCommand == SvxSearchCmd::FIND_ALL )
462  {
463  if( SwDocPositions::Start == aOpts.eEnd )
464  m_pWrtShell->EndOfSection();
465  else
466  m_pWrtShell->StartOfSection();
467  }
468 
469  // fdo#65014 : Ensure that the point of the cursor is at the extremity of the
470  // selection closest to the end being searched to as to exclude the selected
471  // region from the search. (This doesn't work in the case of multiple
472  // selected regions as the cursor doesn't mark the selection in that case.)
473  m_pWrtShell->GetCursor()->Normalize( s_pSrchItem->GetBackward() );
474 
475  if (!m_pWrtShell->HasSelection() && (s_pSrchItem->HasStartPoint()))
476  {
477  // No selection -> but we have a start point (top left corner of the
478  // current view), start searching from there, not from the current
479  // cursor position.
480  SwEditShell& rShell = GetWrtShell();
481  Point aPosition(s_pSrchItem->GetStartPointX(), s_pSrchItem->GetStartPointY());
482  rShell.SetCursor(aPosition);
483  }
484 
485  // If you want to search in selected areas, they must not be unselected.
486  if (!s_pSrchItem->GetSelection())
487  m_pWrtShell->KillSelection(nullptr, false);
488 
489  std::unique_ptr<SwWait> pWait(new SwWait( *GetDocShell(), true ));
490  if( FUNC_Search( aOpts ) )
491  {
492  s_bFound = true;
493  if(m_pWrtShell->IsSelFrameMode())
494  {
495  m_pWrtShell->UnSelectFrame();
496  m_pWrtShell->LeaveSelFrameMode();
497  }
498  m_pWrtShell->Pop();
499  m_pWrtShell->EndAllAction();
500  return true;
501  }
502  pWait.reset();
503 
504  // Search in the specialized areas when no search is present in selections.
505  // When searching selections will already searched in these special areas.
506  bool bHasSrchInOther = s_bExtra;
507  if (!s_pSrchItem->GetSelection() && !s_bExtra )
508  {
509  s_bExtra = true;
510  if( FUNC_Search( aOpts ) )
511  {
512  s_bFound = true;
513  m_pWrtShell->Pop();
514  m_pWrtShell->EndAllAction();
515  return true;
516  }
517  s_bExtra = false;
518  }
519  else
520  s_bExtra = !s_bExtra;
521 
522  // If starting position is at the end or beginning of the document.
523  if (aOpts.bDontWrap)
524  {
525  m_pWrtShell->EndAllAction();
526  if( !bApi )
527  {
528 #if HAVE_FEATURE_DESKTOP
529  m_pWrtShell->GetSfxViewShell()->libreOfficeKitViewCallback(LOK_CALLBACK_SEARCH_NOT_FOUND, s_pSrchItem->GetSearchString().toUtf8().getStr());
530  SvxSearchDialogWrapper::SetSearchLabel(SearchLabel::NotFound);
531 #endif
532  }
533  s_bFound = false;
534  m_pWrtShell->Pop();
535  return false;
536  }
537  m_pWrtShell->EndAllAction();
538  // Try again with WrapAround?
539 
540  m_pWrtShell->StartAllAction();
541  m_pWrtShell->Pop(SwCursorShell::PopMode::DeleteCurrent);
542  pWait.reset(new SwWait( *GetDocShell(), true ));
543 
544  bool bSrchBkwrd = SwDocPositions::Start == aOpts.eEnd;
545 
546  aOpts.eEnd = bSrchBkwrd ? SwDocPositions::Start : SwDocPositions::End;
547  aOpts.eStart = bSrchBkwrd ? SwDocPositions::End : SwDocPositions::Start;
548 
549  if (bHasSrchInOther)
550  {
551  m_pWrtShell->ClearMark();
552  // Select the start or the end of the entire document
553  if (bSrchBkwrd)
554  m_pWrtShell->SttEndDoc(false);
555  else
556  m_pWrtShell->SttEndDoc(true);
557  }
558 
559  s_bFound = bool(FUNC_Search( aOpts ));
560 
561  // If WrapAround found no matches in the body text, search in the special
562  // sections, too.
563  if (!s_bFound && !s_pSrchItem->GetSelection() && !s_bExtra)
564  {
565  s_bExtra = true;
566  if (FUNC_Search(aOpts))
567  s_bFound = true;
568  else
569  s_bExtra = false;
570  }
571 
572  m_pWrtShell->EndAllAction();
573  pWait.reset();
574 #if HAVE_FEATURE_DESKTOP
575  if (s_bFound)
576  {
577  if (!bSrchBkwrd)
578  SvxSearchDialogWrapper::SetSearchLabel(SearchLabel::End);
579  else
580  SvxSearchDialogWrapper::SetSearchLabel(SearchLabel::Start);
581  }
582  else if(!bApi)
583  {
584  m_pWrtShell->GetSfxViewShell()->libreOfficeKitViewCallback(LOK_CALLBACK_SEARCH_NOT_FOUND, s_pSrchItem->GetSearchString().toUtf8().getStr());
585  SvxSearchDialogWrapper::SetSearchLabel(SearchLabel::NotFound);
586  }
587 #endif
588  return s_bFound;
589 }
590 
592 {
593  SwWait aWait( *GetDocShell(), true );
594  m_pWrtShell->StartAllAction();
595 
596  SwSearchOptions aOpts( m_pWrtShell.get(), s_pSrchItem->GetBackward() );
597 
598  if (!s_pSrchItem->GetSelection())
599  {
600  // Cancel existing selections, if should not be sought in selected areas.
601  m_pWrtShell->KillSelection(nullptr, false);
602 
603  if( SwDocPositions::Start == aOpts.eEnd )
604  m_pWrtShell->EndOfSection();
605  else
606  m_pWrtShell->StartOfSection();
607  }
608  s_bExtra = false;
609  sal_uInt16 nFound = static_cast<sal_uInt16>(FUNC_Search( aOpts ));
610  s_bFound = 0 != nFound;
611 
612  m_pWrtShell->EndAllAction();
613  return s_bFound;
614 }
615 
617 {
618  SwWait aWait( *GetDocShell(), true );
619 
620  m_pWrtShell->StartAllAction();
621 
622  if( s_pSrchItem->GetPattern() ) // Templates?
623  {
624  SwRewriter aRewriter;
625  aRewriter.AddRule(UndoArg1, s_pSrchItem->GetSearchString());
626  aRewriter.AddRule(UndoArg2, SwResId(STR_YIELDS));
627  aRewriter.AddRule(UndoArg3, s_pSrchItem->GetReplaceString());
628 
629  m_pWrtShell->StartUndo(SwUndoId::UI_REPLACE_STYLE, &aRewriter);
630 
631  m_pWrtShell->SetTextFormatColl( m_pWrtShell->GetParaStyle(
632  s_pSrchItem->GetReplaceString(),
634 
635  m_pWrtShell->EndUndo();
636  }
637  else
638  {
639  if (GetPostItMgr()->HasActiveSidebarWin())
640  GetPostItMgr()->Replace(s_pSrchItem);
641 
642  bool bReqReplace = true;
643 
644  if(m_pWrtShell->HasSelection())
645  {
646  /* check that the selection match the search string*/
647  //save state
648  SwPosition aStartPos = * m_pWrtShell->GetSwCursor()->Start();
649  SwPosition aEndPos = * m_pWrtShell->GetSwCursor()->End();
650  bool bHasSelection = s_pSrchItem->GetSelection();
651  SvxSearchCmd nOldCmd = s_pSrchItem->GetCommand();
652 
653  //set state for checking if current selection has a match
654  s_pSrchItem->SetCommand( SvxSearchCmd::FIND );
655  s_pSrchItem->SetSelection(true);
656 
657  //check if it matches
658  SwSearchOptions aOpts( m_pWrtShell.get(), s_pSrchItem->GetBackward() );
659  if( ! FUNC_Search(aOpts) )
660  {
661 
662  //no matching therefore should not replace selection
663  // => remove selection
664 
665  if(! s_pSrchItem->GetBackward() )
666  {
667  (* m_pWrtShell->GetSwCursor()->Start()) = aStartPos;
668  (* m_pWrtShell->GetSwCursor()->End()) = aEndPos;
669  }
670  else
671  {
672  (* m_pWrtShell->GetSwCursor()->Start()) = aEndPos;
673  (* m_pWrtShell->GetSwCursor()->End()) = aStartPos;
674  }
675  bReqReplace = false;
676  }
677 
678  //set back old search state
679  s_pSrchItem->SetCommand( nOldCmd );
680  s_pSrchItem->SetSelection(bHasSelection);
681  }
682  /*
683  * remove current selection
684  * otherwise it is always replaced
685  * no matter if the search string exists or not in the selection
686  * Now the selection is removed and the next matching string is selected
687  */
688 
689  if( bReqReplace )
690  {
691 
692  bool bReplaced = m_pWrtShell->SwEditShell::Replace( s_pSrchItem->GetReplaceString(),
693  s_pSrchItem->GetRegExp());
694  if( bReplaced && s_xReplaceList && s_xReplaceList->Count() && m_pWrtShell->HasSelection() )
695  {
696  SfxItemSet aReplSet( m_pWrtShell->GetAttrPool(),
698  if( s_xReplaceList->Get( aReplSet ).Count() )
699  {
700  ::SfxToSwPageDescAttr( *m_pWrtShell, aReplSet );
701  m_pWrtShell->SwEditShell::SetAttrSet( aReplSet );
702  }
703  }
704  }
705  }
706 
707  m_pWrtShell->EndAllAction();
708 }
709 
710 SwSearchOptions::SwSearchOptions( SwWrtShell const * pSh, bool bBackward )
711  : eStart(SwDocPositions::Curr)
712 {
713  if( bBackward )
714  {
716  bDontWrap = pSh->IsEndOfDoc();
717  }
718  else
719  {
721  bDontWrap = pSh->IsStartOfDoc();
722  }
723 }
724 
726 {
727 #if HAVE_FEATURE_DESKTOP
728  SvxSearchDialogWrapper::SetSearchLabel(SearchLabel::Empty);
729 #endif
730  bool bDoReplace = s_pSrchItem->GetCommand() == SvxSearchCmd::REPLACE ||
731  s_pSrchItem->GetCommand() == SvxSearchCmd::REPLACE_ALL;
732 
733  FindRanges eRanges = s_pSrchItem->GetSelection()
735  : s_bExtra
737  if (s_pSrchItem->GetCommand() == SvxSearchCmd::FIND_ALL ||
738  s_pSrchItem->GetCommand() == SvxSearchCmd::REPLACE_ALL)
739  eRanges |= FindRanges::InSelAll;
740 
741  m_pWrtShell->SttSelect();
742 
743  static const sal_uInt16 aSearchAttrRange[] = {
747  SID_ATTR_PARA_MODEL, SID_ATTR_PARA_KEEP,
748  0 };
749 
750  SfxItemSet aSrchSet( m_pWrtShell->GetAttrPool(), aSearchAttrRange);
751  if( s_xSearchList && s_xSearchList->Count() )
752  {
753  s_xSearchList->Get( aSrchSet );
754 
755  // -- Page break with page template
756  ::SfxToSwPageDescAttr( *m_pWrtShell, aSrchSet );
757  }
758 
759  std::unique_ptr<SfxItemSet> pReplSet;
760  if( bDoReplace && s_xReplaceList && s_xReplaceList->Count() )
761  {
762  pReplSet.reset( new SfxItemSet( m_pWrtShell->GetAttrPool(),
763  aSearchAttrRange ) );
764  s_xReplaceList->Get( *pReplSet );
765 
766  // -- Page break with page template
767  ::SfxToSwPageDescAttr( *m_pWrtShell, *pReplSet );
768 
769  if( !pReplSet->Count() ) // too bad, we don't know
770  pReplSet.reset(); // the attributes
771  }
772 
773  // build SearchOptions to be used
774 
776  aSearchOpt.Locale = GetAppLanguageTag().getLocale();
777  if( !bDoReplace )
778  aSearchOpt.replaceString.clear();
779 
780  sal_uLong nFound;
781  if( aSrchSet.Count() || ( pReplSet && pReplSet->Count() ))
782  {
783  nFound = m_pWrtShell->SearchAttr(
784  aSrchSet,
786  rOptions.eStart,
787  rOptions.eEnd,
788  eRanges,
789  !s_pSrchItem->GetSearchString().isEmpty() ? &aSearchOpt : nullptr,
790  pReplSet.get() );
791  }
792  else if( s_pSrchItem->GetPattern() )
793  {
794  // Searching (and replacing) templates
795  const OUString& sRplStr( s_pSrchItem->GetReplaceString() );
796  nFound = m_pWrtShell->SearchTempl( s_pSrchItem->GetSearchString(),
797  rOptions.eStart,
798  rOptions.eEnd,
799  eRanges,
800  bDoReplace ? &sRplStr : nullptr );
801  }
802  else
803  {
804  // Normal search
805  nFound = m_pWrtShell->SearchPattern(aSearchOpt, s_pSrchItem->GetNotes(),
806  rOptions.eStart,
807  rOptions.eEnd,
808  eRanges,
809  bDoReplace );
810  }
811  m_pWrtShell->EndSelect();
812  return nFound;
813 }
814 
816 {
817 #if HAVE_FEATURE_DESKTOP
818  const sal_uInt16 nId = SvxSearchDialogWrapper::GetChildWindowId();
820  auto pSrchDlg = pWrp ? pWrp->getDialog() : nullptr;
821  return pSrchDlg;
822 #else
823  return nullptr;
824 #endif
825 }
826 
828 {
829  SfxWhichIter aIter(rSet);
830  sal_uInt16 nWhich = aIter.FirstWhich();
831 
832  while(nWhich)
833  {
834  switch(nWhich)
835  {
836  case SID_SEARCH_OPTIONS:
837  {
838  SearchOptionFlags nOpt = SearchOptionFlags::ALL;
839  if( GetDocShell()->IsReadOnly() )
840  nOpt &= ~SearchOptionFlags( SearchOptionFlags::REPLACE |
841  SearchOptionFlags::REPLACE_ALL );
842  rSet.Put( SfxUInt16Item( SID_SEARCH_OPTIONS, static_cast<sal_uInt16>(nOpt) ));
843  }
844  break;
845  case SID_SEARCH_ITEM:
846  {
847  if ( !s_pSrchItem )
848  {
849  s_pSrchItem = new SvxSearchItem( SID_SEARCH_ITEM );
850  s_pSrchItem->SetFamily(SfxStyleFamily::Para);
851  s_pSrchItem->SetSearchString( m_pWrtShell->GetSelText() );
852  }
853 
854  if( s_bJustOpened && m_pWrtShell->IsSelection() )
855  {
856  OUString aText;
857  if( 1 == m_pWrtShell->GetCursorCnt() &&
858  !( aText = m_pWrtShell->SwCursorShell::GetSelText() ).isEmpty() )
859  {
860  s_pSrchItem->SetSearchString( aText );
861  s_pSrchItem->SetSelection( false );
862  }
863  else
864  s_pSrchItem->SetSelection( true );
865  }
866 
867  s_bJustOpened = false;
868  rSet.Put( *s_pSrchItem );
869  }
870  break;
871  }
872  nWhich = aIter.NextWhich();
873  }
874 }
875 
876 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::optional< OUString > ReplaceBackReferences(const i18nutil::SearchOptions2 &rSearchOpt, SwPaM *const pPam, SwRootFrame const *const pLayout)
Helperfunction to resolve backward references in regular expressions.
Definition: findtxt.cxx:1118
SwDocPositions eStart
Definition: viewsrch.cxx:65
constexpr TypedWhichId< SvxBlinkItem > RES_CHRATR_BLINK(18)
static void lcl_addContainerToJson(boost::property_tree::ptree &rTree, const OString &rKey, const std::vector< OString > &rMatches)
Adds rMatches using rKey as a key to the rTree tree.
Definition: viewsrch.cxx:72
Marks a position in the document model.
Definition: pam.hxx:35
bool IsAnyEnabled() const
SfxChildWindow * GetChildWindow(sal_uInt16)
constexpr TypedWhichId< SvxFontItem > RES_CHRATR_CTL_FONT(27)
static sal_uInt16 GetMoveType()
Definition: viewmdi.cxx:691
SwPaM * GetCursor(bool bMakeTableCursor=true) const
Return pointer to the current shell cursor.
Definition: crsrsh.cxx:194
static void lcl_emitSearchResultCallbacks(SvxSearchItem const *pSearchItem, SwWrtShell const *pWrtShell, bool bHighlightAll)
Emits LOK callbacks (count, selection) for search results.
Definition: viewsrch.cxx:88
std::unique_ptr< SwWrtShell > m_pWrtShell
Definition: view.hxx:193
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const =0
void SetSearchString(const OUString &rNewString)
bool IsStartOfDoc() const
Definition: crsrsh.cxx:2747
sal_uIntPtr sal_uLong
Find "all" in Footer/Header/Fly...
constexpr TypedWhichId< SwRegisterItem > RES_PARATR_REGISTER(71)
SAL_DLLPRIVATE void Replace()
Definition: viewsrch.cxx:616
static SvxSearchDialog * GetSearchDialog()
Definition: viewsrch.cxx:815
constexpr sal_uInt16 RES_FRMATR_END(133)
sal_Int16 nId
sal_uInt16 FirstWhich()
static std::unique_ptr< SearchAttrItemList > s_xSearchList
Definition: view.hxx:173
void Done(bool bRemove=false)
static std::unique_ptr< SearchAttrItemList > s_xReplaceList
Definition: view.hxx:174
SfxViewShell * GetSfxViewShell() const
Definition: viewsh.hxx:441
void StateSearch(SfxItemSet &)
Definition: viewsrch.cxx:827
const SfxItemSet * GetArgs() const
void SfxToSwPageDescAttr(const SwWrtShell &rShell, SfxItemSet &rSet)
Definition: uitool.cxx:625
sal_uInt16 const aTextFormatCollSetRange[]
Definition: init.cxx:149
Find in selections.
constexpr TypedWhichId< SvxTwoLinesItem > RES_CHRATR_TWO_LINES(34)
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
sal_uInt16 NextWhich()
#define NID_SRCH_REP
Definition: workctrl.hxx:45
const i18nutil::SearchOptions2 & GetSearchOptions() const
SwDocPositions
Definition: cshtyp.hxx:103
Used by the UI to modify the document model.
Definition: wrtsh.hxx:91
constexpr TypedWhichId< SvxCaseMapItem > RES_CHRATR_CASEMAP(RES_CHRATR_BEGIN)
constexpr sal_uInt16 RES_PARATR_BEGIN(RES_TXTATR_END)
OString join(std::string_view rSeparator, const std::vector< OString > &rSequence)
constexpr OUStringLiteral IsReadOnly(u"IsReadOnly")
FindRanges
Definition: cshtyp.hxx:90
void libreOfficeKitViewCallback(int nType, const char *pPayload) const override
int SetCursor(const Point &rPt, bool bOnlyText=false, bool bBlock=true)
Definition: crsrsh.cxx:760
constexpr TypedWhichId< SvxCharReliefItem > RES_CHRATR_RELIEF(36)
static bool s_bJustOpened
Definition: view.hxx:171
bool GetPattern() const
constexpr TypedWhichId< SvxHyphenZoneItem > RES_PARATR_HYPHENZONE(69)
css::lang::Locale Locale
constexpr TypedWhichId< SvxParaVertAlignItem > RES_PARATR_VERTALIGN(76)
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
#define SW_MOD()
Definition: swmodule.hxx:256
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
SvxSearchCmd GetCommand() const
bool GetNotes() const
constexpr TypedWhichId< SvxShadowedItem > RES_CHRATR_SHADOWED(13)
const OUString & GetReplaceString() const
All (only in non-body and selections).
bool GetSelection() const
void AddRule(SwUndoArg eWhat, const OUString &rWith)
Definition: SwRewriter.cxx:27
sal_uInt16 Count() const
constexpr TypedWhichId< SvxLineSpacingItem > RES_PARATR_LINESPACING(RES_PARATR_BEGIN)
ring_container GetRingContainer()
Definition: ring.hxx:240
OUString SwResId(const char *pId)
Definition: swmodule.cxx:165
static void SetMoveType(sal_uInt16 nSet)
Definition: viewmdi.cxx:696
constexpr TypedWhichId< SvxOverlineItem > RES_CHRATR_OVERLINE(38)
constexpr TypedWhichId< SvxEmphasisMarkItem > RES_CHRATR_EMPHASIS_MARK(33)
static void SetSearchLabel(const SearchLabel &rSL)
void SetFamily(SfxStyleFamily eNewFamily)
SwDocShell * GetDocShell()
Definition: view.cxx:1126
constexpr TypedWhichId< SvxCharScaleWidthItem > RES_CHRATR_SCALEW(35)
SAL_DLLPRIVATE bool SearchAndWrap(bool bApi)
Definition: viewsrch.cxx:449
void SetReturnValue(const SfxPoolItem &)
const LanguageTag & GetAppLanguageTag()
Definition: init.cxx:739
constexpr TypedWhichId< SvxColorItem > RES_CHRATR_COLOR(3)
constexpr TypedWhichId< SvxScriptSpaceItem > RES_PARATR_SCRIPTSPACE(73)
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
constexpr TypedWhichId< SvxBrushItem > RES_CHRATR_BACKGROUND(21)
sal_uInt16 GetSlot() const
SAL_DLLPRIVATE sal_uLong FUNC_Search(const SwSearchOptions &rOptions)
Definition: viewsrch.cxx:725
static SfxViewFrame * Current()
constexpr sal_uInt16 RES_CHRATR_BEGIN(HINT_BEGIN)
constexpr TypedWhichId< SvxWeightItem > RES_CHRATR_CJK_WEIGHT(26)
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
#define SAL_WARN_IF(condition, area, stream)
constexpr TypedWhichId< SvxWordLineModeItem > RES_CHRATR_WORDLINEMODE(16)
constexpr TypedWhichId< SvxWeightItem > RES_CHRATR_CTL_WEIGHT(31)
constexpr TypedWhichId< SvxForbiddenRuleItem > RES_PARATR_FORBIDDEN_RULES(75)
void ExecSearch(SfxRequest &)
Definition: viewsrch.cxx:125
SvxSearchDialog * getDialog()
const OUString & GetSearchString() const
constexpr TypedWhichId< SvxFontItem > RES_CHRATR_CJK_FONT(22)
SwDocPositions eEnd
Definition: viewsrch.cxx:65
constexpr sal_uInt16 RES_PARATR_END(82)
void SetSelection(bool bNewSelection)
constexpr TypedWhichId< SvxLRSpaceItem > RES_LR_SPACE(91)
constexpr sal_uInt16 RES_CHRATR_END(46)
SvxSearchCmd
Find "one" only in body text.
bool IsEndOfDoc() const
Definition: crsrsh.cxx:2759
#define FN_REPEAT_SEARCH
Definition: cmdid.h:103
void AppendItem(const SfxPoolItem &)
const SearchAttrItemList * GetReplaceItemList() const
bool IsAPI() const
constexpr TypedWhichId< SvxCharRotateItem > RES_CHRATR_ROTATE(32)
SAL_DLLPRIVATE bool SearchAll()
Definition: viewsrch.cxx:591
static SvxSearchItem * s_pSrchItem
Definition: view.hxx:164
SearchOptionFlags
constexpr sal_uInt16 RES_FRMATR_BEGIN(RES_PARATR_LIST_END)
constexpr TypedWhichId< SvxULSpaceItem > RES_UL_SPACE(92)
SwSearchOptions(SwWrtShell const *pSh, bool bBackward)
Definition: viewsrch.cxx:710
constexpr TypedWhichId< SvxPostureItem > RES_CHRATR_POSTURE(11)
static bool s_bExtra
Definition: view.hxx:169
const SearchAttrItemList * GetSearchItemList() const