lok: avoid ILibreOfficeKitNotifier null ptr de-reference on shutdown.
[LibreOffice.git] / sd / inc / Outliner.hxx
blob27ec4d80127d64341127417f08b05925021579a2
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 .
20 #ifndef INCLUDED_SD_INC_OUTLINER_HXX
21 #define INCLUDED_SD_INC_OUTLINER_HXX
23 #include <svx/svdoutl.hxx>
24 #include "pres.hxx"
25 #include "OutlinerIterator.hxx"
26 #include <editeng/SpellPortions.hxx>
27 #include <memory>
29 class SdrObject;
30 class SdrTextObj;
31 class SdDrawDocument;
33 namespace weld {
34 class Window;
37 namespace sd {
39 class View;
40 class ViewShell;
41 class Window;
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.
47 int const m_nPage;
48 /**
49 * List of selection rectangles in twips -- multiple rectangles only in
50 * case the selection spans over more layout lines.
52 OString const m_aRectangles;
54 SearchSelection(int nPage, const OString& rRectangles);
56 bool operator==(const SearchSelection& rOther) const
58 return m_nPage == rOther.m_nPage && m_aRectangles == rOther.m_aRectangles;
62 } // end of namespace sd
65 /** The main purpose of this class is searching and replacing as well as
66 spelling of impress documents. The main part of both tasks lies in
67 iterating over the pages and view modes of a document and apply the
68 respective function to all objects containing text on those pages.
70 <p>Relevant objects: There are two sets of objects to search/spell
71 check. One is the set of all selected objects. The other consists of
72 all objects on all pages in draw-, notes-, and handout view as well as
73 slide- and background view (draw pages and master pages).</p>
75 <p>Iteration: Search/replace and spelling functions operate on shapes
76 containing text. To cover all relevant objects an order has to be
77 defined on the objects. For the set of all selected objects this order
78 is simply the order in which they can be retrieved from the selection
79 object.<br>
80 When there is no selection the order is nested. The three modes of the
81 draw view are on the outer level: draw mode, notes mode, handout mode.
82 The inner level switches between draw pages and master pages. This
83 leads to the following order:
84 <ol>
85 <li>draw pages of draw mode</li>
86 <li>master pages of draw mode</li>
87 <li>draw pages of notes mode</li>
88 <li>master pages of notes mode</li>
89 <li>draw pages of handout mode</li>
90 <li>master pages of handout mode</li>
91 </ol>
92 Iteration starts at the top of the current page. When reaching the end
93 of the document, i.e. the last master page of the handout mode, it jumps
94 to the first draw page of draw mode. In backward searches this order is
95 reversed. When doing a <em>replace all</em> then the whole document is
96 searched for matches starting at the first page of the draw/slide view
97 (or last page of handout/background view even though search
98 direction).</p>
100 <p>The start position is restored after finishing spell checking or
101 replacing all matches in a document.</p>
103 <p>Some related pieces of information:
104 The search dialog (<type>SvxSearchDialog</type>) can be controlled in
105 more than one way:
106 <ul><li>A set of option flags returned by the slot call
107 SID_SEARCH_OPTIONS handled by the
108 <member>SdDrawDocument::GetState()</member> method.</li>
109 <li>The contents of the search item of type
110 <type>SvxSearchItem</type>.</li>
111 <li>The <member>HasSelection()</member> view shell method that returns
112 whether or not a selection exists. However, it is called from the
113 search dialog with an argument so that only text selections are
114 queried. This is only sufficient for searching the outline view.
115 </p>
117 class SdOutliner
118 : public SdrOutliner
120 public:
121 friend class ::sd::outliner::OutlinerContainer;
123 /** Create a new sd outliner object.
124 @param pDoc
125 The draw document from which to take the content.
126 @param nMode
127 The valid values <const>OutlinerMode::DontKnow</const>,
128 <const>OutlinerMode::TextObject</const>,
129 <const>OutlinerMode::TitleObject</const>,
130 <const>OutlinerMode::OutlineObject</const>, and
131 <const>OutlinerMode::OutlineView</const> are defined in
132 editeng/outliner.hxx.
134 SdOutliner( SdDrawDocument* pDoc, OutlinerMode nMode );
135 virtual ~SdOutliner() override;
136 /// Forbid copy construction and copy assignment
137 SdOutliner(const Outliner&) = delete;
138 SdOutliner& operator=(const Outliner&) = delete;
140 /** Despite the name this method is called prior to spell checking *and*
141 searching and replacing. The position of current view
142 mode/page/object/caret position is remembered and, depending on the
143 search mode, may be restored after finishing searching/spell
144 checking.
146 void PrepareSpelling();
148 /** Initialize a spell check but do not start it yet. This method
149 is a better candidate for the name PrepareSpelling.
151 void StartSpelling();
153 /** Initiate a find and/or replace on the next relevant text object.
154 @return
155 Returns </sal_True> when the search/replace is finished (as
156 indicated by user input to the search dialog). A </sal_False> value
157 indicates that another call to this method is required.
159 bool StartSearchAndReplace (const SvxSearchItem* pSearchItem);
161 /** Iterate over the sentences in all text shapes and stop at the
162 next sentence with spelling errors. While doing so the view
163 mode may be changed and text shapes are set into edit mode.
165 svx::SpellPortions GetNextSpellSentence();
167 /** Release all resources that have been created during the find&replace
168 or spell check.
170 void EndSpelling();
172 /** callback for textconversion */
173 bool ConvertNextDocument() override;
175 /** Starts the text conversion (hangul/hanja or Chinese simplified/traditional)
176 for the current viewshell */
177 void StartConversion( LanguageType nSourceLanguage, LanguageType nTargetLanguage,
178 const vcl::Font *pTargetFont, sal_Int32 nOptions, bool bIsInteractive );
180 /** This is called internally when text conversion is started.
181 The position of current view mode/page/object/caret position
182 is remembered and will be restored after conversion.
184 void BeginConversion();
186 /** Release all resources that have been created during the conversion */
187 void EndConversion();
189 int GetIgnoreCurrentPageChangesLevel() const { return mnIgnoreCurrentPageChangesLevel; };
190 void IncreIgnoreCurrentPageChangesLevel() { mnIgnoreCurrentPageChangesLevel++; };
191 void DecreIgnoreCurrentPageChangesLevel() { mnIgnoreCurrentPageChangesLevel--; };
192 SdDrawDocument* GetDoc() const { return mpDrawDocument; }
194 private:
195 class Implementation;
196 ::std::unique_ptr<Implementation> mpImpl;
198 /// Specifies whether to search and replace, to spell check or to do a
199 /// text conversion.
200 enum mode {SEARCH, SPELL, TEXT_CONVERSION} meMode;
202 /// The view which displays the searched objects.
203 ::sd::View* mpView;
204 /** The view shell containing the view. It is held as weak
205 pointer to avoid keeping it alive when the view is changed
206 during searching.
208 std::weak_ptr<::sd::ViewShell> mpWeakViewShell;
209 /// This window contains the view.
210 VclPtr< ::sd::Window> mpWindow;
211 /// The document on whose objects and pages this class operates.
212 SdDrawDocument* mpDrawDocument;
214 /** this is the language that is used for current text conversion.
215 Only valid if meMode is TEXT_CONVERSION.
217 LanguageType mnConversionLanguage;
219 /** While the value of this flag is greater than 0 changes of the current page
220 do not lead to selecting the corresponding text in the outliner.
222 int mnIgnoreCurrentPageChangesLevel;
224 /// Specifies whether the search string has been found so far.
225 bool mbStringFound;
227 /** This flag indicates whether there may exist a match of the search
228 string before/after the current position in the document. It can be
229 set to </sal_False> only when starting from the beginning/end of the
230 document. When reaching the end/beginning with it still be set to
231 </sal_False> then there exists no match and the search can be terminated.
233 bool mbMatchMayExist;
235 /// The number of pages in the current view.
236 sal_uInt16 mnPageCount;
238 /** A <TRUE/> value indicates that the end of the find&replace or spell
239 check has been reached.
241 bool mbEndOfSearch;
243 /** Set to <TRUE/> when an object has been prepared successfully for
244 searching/spell checking. This flag directs the internal iteration
245 which stops when set to </sal_True>.
247 bool mbFoundObject;
249 /** This flag indicates whether to search forward or backwards.
251 bool mbDirectionIsForward;
253 /** This flag indicates that only the selected objects are to be
254 searched.
256 bool mbRestrictSearchToSelection;
258 /** When the search is restricted to the current selection then
259 this list contains pointers to all the objects of the
260 selection. This copy is necessary because during the search
261 process the mark list is modified.
263 ::std::vector<tools::WeakReference<SdrObject>> maMarkListCopy;
265 /** Current object that may be a text object. The object pointer to
266 corresponds to <member>mnObjIndex</member>. While iterating over the
267 objects on a page <member>mpObj</member> will point to every object
268 while <member>mpTextObj</member> will be set only to valid text
269 objects.
271 SdrObject* mpObj;
273 /** this stores the first object that is used for text conversion.
274 Conversion automatically wraps around the document and stops when it
275 finds this object again.
277 SdrObject* mpFirstObj;
279 /// Candidate for being searched/spell checked.
280 SdrTextObj* mpSearchSpellTextObj;
282 /// Current text to be searched/spelled inside the current text object
283 sal_Int32 mnText;
285 /// Paragraph object of <member>mpTextObj</member>.
286 OutlinerParaObject* mpParaObj;
288 /// The view mode that was active when starting to search/spell check.
289 PageKind meStartViewMode;
291 /// The master page mode that was active when starting to search/spell check.
292 EditMode meStartEditMode;
294 /// The current page index on starting to search/spell check.
295 sal_uInt16 mnStartPageIndex;
297 /// The object in edit mode when searching /spell checking was started
298 /// (if any).
299 SdrObject* mpStartEditedObject;
301 /// The position of the caret when searching /spell checking was started.
302 ESelection maStartSelection;
304 /** The search item contains various attributes that define the type of
305 search. It is set every time the
306 <member>SearchAndReplaceAll</member> method is called.
308 const SvxSearchItem* mpSearchItem;
310 /// The actual object iterator.
311 ::sd::outliner::Iterator maObjectIterator;
312 /// The current position of the object iterator.
313 ::sd::outliner::IteratorPosition maCurrentPosition;
314 /// The position when the search started. Corresponds largely to the
315 /// m?Start* members.
316 ::sd::outliner::Iterator maSearchStartPosition;
317 /** The last valid position describes where the last text object has been
318 found. This position is restored when some dialogs are shown. The
319 position is initially set to the where the search begins.
321 ::sd::outliner::IteratorPosition maLastValidPosition;
323 /** When this flag is true then a PrepareSpelling() is executed when
324 StartSearchAndReplace() is called the next time.
326 bool mbPrepareSpellingPending;
328 /** Initialize the object iterator. Call this method after being
329 invoked from the search or spellcheck dialog. It creates a new
330 iterator pointing at the current object when this has not been done
331 before. It reverses the direction of iteration if the given flag
332 differs from the current direction.
333 @param bDirectionIsForward
334 This flag specifies in which direction to iterator over the
335 objects. If it differs from the current direction the iterator
336 is reversed.
338 void Initialize (bool bDirectionIsForward);
340 /** Do search and replace for whole document.
342 bool SearchAndReplaceAll();
344 /** Do search and replace for next match.
345 @param pSelections
346 When tiled rendering and not 0, then don't emit LOK events, instead
347 assume the caller will do so.
348 @return
349 The return value specifies whether the search ended (</sal_True>) or
350 another call to this method is required (</sal_False>).
352 bool SearchAndReplaceOnce(std::vector<::sd::SearchSelection>* pSelections = nullptr);
354 /** Detect changes of the document or view and react accordingly. Such
355 changes may occur because different calls to
356 <member>SearchAndReplace()</member> there usually is user
357 interaction. This is at least the press of the search or replace
358 button but may include any other action some of which affect the
359 search.
361 void DetectChange();
363 /** Detect whether the selection has changed.
364 @return
365 Return <TRUE/> when the selection has been changed since the
366 last call to this method.
368 bool DetectSelectionChange();
370 /** Remember the current edited object/caret position/page/view mode
371 when starting to search/spell check so that it can be restored on
372 termination.
374 void RememberStartPosition();
376 /** Restore the position stored in the last call of
377 <member>RememberStartPositiony</member>.
379 void RestoreStartPosition();
381 /** Provide next object to search or spell check as text object in edit
382 mode on the current page. This skips all objects that do not
383 match or are no text object.
385 void ProvideNextTextObject();
387 /** Handle the situation that the iterator has reached the last object.
388 This may result in setting the <member>mbEndOfSearch</member> flag
389 back to </sal_False>. This method may show either the end-of-search
390 dialog or the wrap-around dialog.
392 void EndOfSearch();
394 /** Show a dialog that tells the user that the search has ended either
395 because there are no more matches after finding at least one or that
396 no match has been found at all.
398 void ShowEndOfSearchDialog();
400 /** Show a dialog that asks the user whether to wrap around to the
401 beginning/end of the document and continue with the search/spell
402 check.
404 bool ShowWrapArroundDialog();
406 /** Check whether the object pointed to by the iterator is a valid text
407 object.
408 @param aPosition
409 The object for which to test whether it is a valid text object.
411 static bool IsValidTextObject (const ::sd::outliner::IteratorPosition& rPosition);
413 /** Put text of current text object into outliner so that the text can
414 be searched/spell checked.
416 void PutTextIntoOutliner();
418 /** Prepare to do spell checking on the current text object. This
419 includes putting it into edit mode. Under certain conditions this
420 method sets <member>mbEndOfSearch</member> to <TRUE/>.
422 void PrepareSpellCheck();
424 /** Prepare to search and replace on the current text object. This
425 includes putting it into edit mode.
427 void PrepareSearchAndReplace();
429 /** Prepare to do a text conversion on the current text
430 object. This includes putting it into edit mode.
432 void PrepareConversion();
434 /** Switch to a new view mode. Try to restore the original edit mode
435 before doing so.
436 @param ePageKind
437 Specifies the new view mode.
439 void SetViewMode (PageKind ePageKind);
441 /** Switch to the page or master page specified by the
442 <member>mnPage</member> index. Master page mode is specified by
443 <member>meEditMode</member>.
444 @param eEditMode
445 The new edit mode.
446 @param nPageIndex
447 The new page index.
449 void SetPage (EditMode eEditMode, sal_uInt16 nPageIndex);
451 /** Switch on edit mode for the currently selected text object.
453 void EnterEditMode (bool bGrabFocus);
455 /** Return the position at which a new search is started with respect to
456 the search direction as specified by the argument.
457 @return
458 The position mentioned above in form of a selection with start
459 equals end.
461 ESelection GetSearchStartPosition() const;
463 /** Detect whether there exists a previous match. Note that only the
464 absence of such a match can be detected reliably. An existing match
465 is assumed when the search started not at the beginning/end of the
466 presentation. This does not have to be true. The user can have set
467 the cursor at the middle of the text without a prior search.
468 @return
469 Returns </True> when there is no previous match and </False>
470 when there may be one.
472 bool HasNoPreviousMatch();
474 /** Handle a failed search (with or without replace) for the outline
475 mode. Show message boxes when the search failed completely,
476 i.e. there is no match in the whole presentation, or when no further
477 match exists.
478 @return
479 The returned value indicates whether another (wrapped around)
480 search shall take place. If that is so, then it is the caller's
481 responsibility to set the cursor position accordingly.
483 bool HandleFailedSearch();
485 /** Take a position as returned by an object iterator and switch to the
486 view and page on which the object specified by this position is
487 located.
488 @param rPosition
489 This position points to a <type>SdrObject</type> object and
490 contains the view and page where it is located.
491 @return
492 Return a pointer to the <type>SdrObject</type>.
494 SdrObject* SetObject (const ::sd::outliner::IteratorPosition& rPosition);
496 /** Use this method when the view shell in which to search has changed.
497 It handles i.e. registering at the associated view as selection
498 change listener.
500 void SetViewShell (const std::shared_ptr<::sd::ViewShell>& rpViewShell);
502 /** Activate or deactivate the search in the current selection. Call
503 this method whenever the selection has changed. This method creates
504 a copy of the current selection and reassigns the object iterator to
505 the current() iterator.
507 void HandleChangedSelection();
509 /** Initiate the spell check of the next relevant text object.
510 When the outline view is active then this method is called
511 after a wrap around to continue at the beginning of the document.
512 @return
513 Returns <TRUE/> to indicate that another call to this method is
514 required. When all text objects have been processed then
515 <FALSE/> is returned.
517 virtual bool SpellNextDocument() override;
519 /** Find the right parent to use for a message. This function makes sure
520 that the otherwise non-modal search or spell dialogs, if visible, are
521 locked, too.
523 weld::Window* GetMessageBoxParent();
526 #endif
528 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */