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