1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <svx/svdoutl.hxx>
24 #include "OutlinerIterator.hxx"
25 #include <editeng/SpellPortions.hxx>
43 /// Describes a single search hit: a set of rectangles on a given page.
44 struct SearchSelection
46 /// 0-based index of the page that has the selection.
50 * List of selection rectangles in twips -- multiple rectangles only in
51 * case the selection spans over more layout lines.
53 OString m_aRectangles
;
55 SearchSelection(int nPage
, const OString
& rRectangles
)
57 , m_aRectangles(rRectangles
)
61 bool operator==(const SearchSelection
& rOther
) const
63 return m_nPage
== rOther
.m_nPage
&& m_aRectangles
== rOther
.m_aRectangles
;
67 } // end of namespace sd
69 /** The main purpose of this class is searching and replacing as well as
70 spelling of impress documents. The main part of both tasks lies in
71 iterating over the pages and view modes of a document and apply the
72 respective function to all objects containing text on those pages.
74 <p>Relevant objects: There are two sets of objects to search/spell
75 check. One is the set of all selected objects. The other consists of
76 all objects on all pages in draw-, notes-, and handout view as well as
77 slide- and background view (draw pages and master pages).</p>
79 <p>Iteration: Search/replace and spelling functions operate on shapes
80 containing text. To cover all relevant objects an order has to be
81 defined on the objects. For the set of all selected objects this order
82 is simply the order in which they can be retrieved from the selection
84 When there is no selection the order is nested. The three modes of the
85 draw view are on the outer level: draw mode, notes mode, handout mode.
86 The inner level switches between draw pages and master pages. This
87 leads to the following order:
89 <li>draw pages of draw mode</li>
90 <li>master pages of draw mode</li>
91 <li>draw pages of notes mode</li>
92 <li>master pages of notes mode</li>
93 <li>draw pages of handout mode</li>
94 <li>master pages of handout mode</li>
96 Iteration starts at the top of the current page. When reaching the end
97 of the document, i.e. the last master page of the handout mode, it jumps
98 to the first draw page of draw mode. In backward searches this order is
99 reversed. When doing a <em>replace all</em> then the whole document is
100 searched for matches starting at the first page of the draw/slide view
101 (or last page of handout/background view even though search
104 <p>The start position is restored after finishing spell checking or
105 replacing all matches in a document.</p>
107 <p>Some related pieces of information:
108 The search dialog (<type>SvxSearchDialog</type>) can be controlled in
110 <ul><li>A set of option flags returned by the slot call
111 SID_SEARCH_OPTIONS handled by the
112 <member>SdDrawDocument::GetState()</member> method.</li>
113 <li>The contents of the search item of type
114 <type>SvxSearchItem</type>.</li>
115 <li>The <member>HasSelection()</member> view shell method that returns
116 whether or not a selection exists. However, it is called from the
117 search dialog with an argument so that only text selections are
118 queried. This is only sufficient for searching the outline view.
121 class SdOutliner
: public SdrOutliner
124 friend class ::sd::outliner::OutlinerContainer
;
126 /** Create a new sd outliner object.
128 The draw document from which to take the content.
130 The valid values <const>OutlinerMode::DontKnow</const>,
131 <const>OutlinerMode::TextObject</const>,
132 <const>OutlinerMode::TitleObject</const>,
133 <const>OutlinerMode::OutlineObject</const>, and
134 <const>OutlinerMode::OutlineView</const> are defined in
135 editeng/outliner.hxx.
137 SdOutliner(SdDrawDocument
* pDoc
, OutlinerMode nMode
);
138 virtual ~SdOutliner() override
;
139 /// Forbid copy construction and copy assignment
140 SdOutliner(const Outliner
&) = delete;
141 SdOutliner
& operator=(const Outliner
&) = delete;
143 /** Despite the name this method is called prior to spell checking *and*
144 searching and replacing. The position of current view
145 mode/page/object/caret position is remembered and, depending on the
146 search mode, may be restored after finishing searching/spell
149 void PrepareSpelling();
151 /** Initialize a spell check but do not start it yet. This method
152 is a better candidate for the name PrepareSpelling.
154 void StartSpelling();
156 /** Initiate a find and/or replace on the next relevant text object.
158 Returns </sal_True> when the search/replace is finished (as
159 indicated by user input to the search dialog). A </sal_False> value
160 indicates that another call to this method is required.
162 bool StartSearchAndReplace(const SvxSearchItem
* pSearchItem
);
164 /** Iterate over the sentences in all text shapes and stop at the
165 next sentence with spelling errors. While doing so the view
166 mode may be changed and text shapes are set into edit mode.
168 svx::SpellPortions
GetNextSpellSentence();
170 /** Release all resources that have been created during the find&replace
175 /** callback for textconversion */
176 bool ConvertNextDocument() override
;
178 /** Starts the text conversion (hangul/hanja or Chinese simplified/traditional)
179 for the current viewshell */
180 void StartConversion(LanguageType nSourceLanguage
, LanguageType nTargetLanguage
,
181 const vcl::Font
* pTargetFont
, sal_Int32 nOptions
, bool bIsInteractive
);
183 /** This is called internally when text conversion is started.
184 The position of current view mode/page/object/caret position
185 is remembered and will be restored after conversion.
187 void BeginConversion();
189 /** Release all resources that have been created during the conversion */
190 void EndConversion();
192 int GetIgnoreCurrentPageChangesLevel() const { return mnIgnoreCurrentPageChangesLevel
; };
193 void IncreIgnoreCurrentPageChangesLevel() { mnIgnoreCurrentPageChangesLevel
++; };
194 void DecreIgnoreCurrentPageChangesLevel() { mnIgnoreCurrentPageChangesLevel
--; };
195 SdDrawDocument
* GetDoc() const { return mpDrawDocument
; }
198 class Implementation
;
199 ::std::unique_ptr
<Implementation
> mpImpl
;
201 /// Returns the current outline view
202 OutlinerView
* getOutlinerView();
204 /// Specifies whether to search and replace, to spell check or to do a
213 /// The view which displays the searched objects.
215 /** The view shell containing the view. It is held as weak
216 pointer to avoid keeping it alive when the view is changed
219 std::weak_ptr
<::sd::ViewShell
> mpWeakViewShell
;
220 /// This window contains the view.
221 VclPtr
<::sd::Window
> mpWindow
;
222 /// The document on whose objects and pages this class operates.
223 SdDrawDocument
* mpDrawDocument
;
225 /** this is the language that is used for current text conversion.
226 Only valid if meMode is TEXT_CONVERSION.
228 LanguageType mnConversionLanguage
;
230 /** While the value of this flag is greater than 0 changes of the current page
231 do not lead to selecting the corresponding text in the outliner.
233 int mnIgnoreCurrentPageChangesLevel
;
235 /// Specifies whether the search string has been found so far.
238 /** This flag indicates whether there may exist a match of the search
239 string before/after the current position in the document. It can be
240 set to </sal_False> only when starting from the beginning/end of the
241 document. When reaching the end/beginning with it still be set to
242 </sal_False> then there exists no match and the search can be terminated.
244 bool mbMatchMayExist
;
246 /// The number of pages in the current view.
247 sal_uInt16 mnPageCount
;
249 /** A <TRUE/> value indicates that the end of the find&replace or spell
250 check has been reached.
254 /** Set to <TRUE/> when an object has been prepared successfully for
255 searching/spell checking. This flag directs the internal iteration
256 which stops when set to </sal_True>.
260 /** This flag indicates whether to search forward or backwards.
262 bool mbDirectionIsForward
;
264 /** This flag indicates that only the selected objects are to be
267 bool mbRestrictSearchToSelection
;
269 /** When the search is restricted to the current selection then
270 this list contains pointers to all the objects of the
271 selection. This copy is necessary because during the search
272 process the mark list is modified.
274 ::std::vector
<tools::WeakReference
<SdrObject
>> maMarkListCopy
;
276 /** Current object that may be a text object. The object pointer to
277 corresponds to <member>mnObjIndex</member>. While iterating over the
278 objects on a page <member>mpObj</member> will point to every object
279 while <member>mpTextObj</member> will be set only to valid text
284 /** this stores the first object that is used for text conversion.
285 Conversion automatically wraps around the document and stops when it
286 finds this object again.
288 SdrObject
* mpFirstObj
;
290 /// Candidate for being searched/spell checked.
291 SdrTextObj
* mpSearchSpellTextObj
;
293 /// Current text to be searched/spelled inside the current text object
296 /// Paragraph object of <member>mpTextObj</member>.
297 OutlinerParaObject
* mpParaObj
;
299 /// The view mode that was active when starting to search/spell check.
300 PageKind meStartViewMode
;
302 /// The master page mode that was active when starting to search/spell check.
303 EditMode meStartEditMode
;
305 /// The current page index on starting to search/spell check.
306 sal_uInt16 mnStartPageIndex
;
308 /// The object in edit mode when searching /spell checking was started
310 SdrObject
* mpStartEditedObject
;
312 /// The position of the caret when searching /spell checking was started.
313 ESelection maStartSelection
;
315 /** The search item contains various attributes that define the type of
316 search. It is set every time the
317 <member>SearchAndReplaceAll</member> method is called.
319 const SvxSearchItem
* mpSearchItem
;
321 /// The actual object iterator.
322 ::sd::outliner::Iterator maObjectIterator
;
323 /// The current position of the object iterator.
324 ::sd::outliner::IteratorPosition maCurrentPosition
;
325 /// The position when the search started. Corresponds largely to the
326 /// m?Start* members.
327 ::sd::outliner::Iterator maSearchStartPosition
;
328 /** The last valid position describes where the last text object has been
329 found. This position is restored when some dialogs are shown. The
330 position is initially set to the where the search begins.
332 ::sd::outliner::IteratorPosition maLastValidPosition
;
334 /** When this flag is true then a PrepareSpelling() is executed when
335 StartSearchAndReplace() is called the next time.
337 bool mbPrepareSpellingPending
;
339 /** Initialize the object iterator. Call this method after being
340 invoked from the search or spellcheck dialog. It creates a new
341 iterator pointing at the current object when this has not been done
342 before. It reverses the direction of iteration if the given flag
343 differs from the current direction.
344 @param bDirectionIsForward
345 This flag specifies in which direction to iterator over the
346 objects. If it differs from the current direction the iterator
349 void Initialize(bool bDirectionIsForward
);
351 /** Do search and replace for whole document.
353 bool SearchAndReplaceAll();
355 /** Do search and replace for next match.
357 When tiled rendering and not 0, then don't emit LOK events, instead
358 assume the caller will do so.
360 The return value specifies whether the search ended (</sal_True>) or
361 another call to this method is required (</sal_False>).
363 bool SearchAndReplaceOnce(std::vector
<::sd::SearchSelection
>* pSelections
= nullptr);
365 void sendLOKSearchResultCallback(const std::shared_ptr
<sd::ViewShell
>& pViewShell
,
366 const OutlinerView
* pOutlinerView
,
367 std::vector
<sd::SearchSelection
>* pSelections
);
369 /** Detect changes of the document or view and react accordingly. Such
370 changes may occur because different calls to
371 <member>SearchAndReplace()</member> there usually is user
372 interaction. This is at least the press of the search or replace
373 button but may include any other action some of which affect the
378 /** Detect whether the selection has changed.
380 Return <TRUE/> when the selection has been changed since the
381 last call to this method.
383 bool DetectSelectionChange();
385 /** Remember the current edited object/caret position/page/view mode
386 when starting to search/spell check so that it can be restored on
389 void RememberStartPosition();
391 /** Restore the position stored in the last call of
392 <member>RememberStartPositiony</member>.
394 void RestoreStartPosition();
396 /** Provide next object to search or spell check as text object in edit
397 mode on the current page. This skips all objects that do not
398 match or are no text object.
400 void ProvideNextTextObject();
402 /** Handle the situation that the iterator has reached the last object.
403 This may result in setting the <member>mbEndOfSearch</member> flag
404 back to </sal_False>. This method may show either the end-of-search
405 dialog or the wrap-around dialog.
409 /** Show a dialog that tells the user that the search has ended either
410 because there are no more matches after finding at least one or that
411 no match has been found at all.
413 void ShowEndOfSearchDialog();
415 /** Show a dialog that asks the user whether to wrap around to the
416 beginning/end of the document and continue with the search/spell
419 bool ShowWrapAroundDialog();
421 /** Put text of current text object into outliner so that the text can
422 be searched/spell checked.
424 void PutTextIntoOutliner();
426 /** Prepare to do spell checking on the current text object. This
427 includes putting it into edit mode. Under certain conditions this
428 method sets <member>mbEndOfSearch</member> to <TRUE/>.
430 void PrepareSpellCheck();
432 /** Prepare to search and replace on the current text object. This
433 includes putting it into edit mode.
435 void PrepareSearchAndReplace();
437 /** Prepare to do a text conversion on the current text
438 object. This includes putting it into edit mode.
440 void PrepareConversion();
442 /** Switch to a new view mode. Try to restore the original edit mode
445 Specifies the new view mode.
447 void SetViewMode(PageKind ePageKind
);
449 /** Switch to the page or master page specified by the
450 <member>mnPage</member> index. Master page mode is specified by
451 <member>meEditMode</member>.
457 void SetPage(EditMode eEditMode
, sal_uInt16 nPageIndex
);
459 /** Switch on edit mode for the currently selected text object.
461 void EnterEditMode(bool bGrabFocus
);
463 /** Return the position at which a new search is started with respect to
464 the search direction as specified by the argument.
466 The position mentioned above in form of a selection with start
469 ESelection
GetSearchStartPosition() const;
471 /** Detect whether there exists a previous match. Note that only the
472 absence of such a match can be detected reliably. An existing match
473 is assumed when the search started not at the beginning/end of the
474 presentation. This does not have to be true. The user can have set
475 the cursor at the middle of the text without a prior search.
477 Returns </True> when there is no previous match and </False>
478 when there may be one.
480 bool HasNoPreviousMatch();
482 /** Handle a failed search (with or without replace) for the outline
483 mode. Show message boxes when the search failed completely,
484 i.e. there is no match in the whole presentation, or when no further
487 The returned value indicates whether another (wrapped around)
488 search shall take place. If that is so, then it is the caller's
489 responsibility to set the cursor position accordingly.
491 bool HandleFailedSearch();
493 /** Take a position as returned by an object iterator and switch to the
494 view and page on which the object specified by this position is
497 This position points to a <type>SdrObject</type> object and
498 contains the view and page where it is located.
500 Return a pointer to the <type>SdrObject</type>.
502 SdrObject
* SetObject(const ::sd::outliner::IteratorPosition
& rPosition
);
504 /** Use this method when the view shell in which to search has changed.
505 It handles i.e. registering at the associated view as selection
508 void SetViewShell(const std::shared_ptr
<::sd::ViewShell
>& rpViewShell
);
510 /** Activate or deactivate the search in the current selection. Call
511 this method whenever the selection has changed. This method creates
512 a copy of the current selection and reassigns the object iterator to
513 the current() iterator.
515 void HandleChangedSelection();
517 /** Initiate the spell check of the next relevant text object.
518 When the outline view is active then this method is called
519 after a wrap around to continue at the beginning of the document.
521 Returns <TRUE/> to indicate that another call to this method is
522 required. When all text objects have been processed then
523 <FALSE/> is returned.
525 virtual bool SpellNextDocument() override
;
527 /** Find the right parent to use for a message. This function makes sure
528 that the otherwise non-modal search or spell dialogs, if visible, are
531 weld::Window
* GetMessageBoxParent();
534 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */