Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / editeng / unoedsrc.hxx
blob1537f00a743c6f42e6af723e482d2934170c8141
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_EDITENG_UNOEDSRC_HXX
21 #define INCLUDED_EDITENG_UNOEDSRC_HXX
23 #include <i18nlangtag/lang.h>
24 #include <rtl/ustring.hxx>
25 #include <tools/gen.hxx>
26 #include <tools/fontenum.hxx>
27 #include <vcl/mapmod.hxx>
28 #include <svl/poolitem.hxx>
29 #include <editeng/editengdllapi.h>
30 #include <editeng/editeng.hxx>
32 #include <vector>
34 struct ESelection;
35 struct EFieldInfo;
36 struct EBulletInfo;
37 class Color;
38 class OutputDevice;
39 class SfxItemSet;
40 class SvxTextForwarder;
41 class SvxViewForwarder;
42 class SvxEditViewForwarder;
43 class SvxFieldItem;
44 class SfxBroadcaster;
45 class SvxUnoTextRangeBase;
47 typedef std::vector< SvxUnoTextRangeBase* > SvxUnoTextRangeBaseVec;
49 /** Wrapper class for unified EditEngine/Outliner access
51 This class wraps a textual object, which might or might not
52 contain an EditEngine/Outliner. Is used e.g. for filling an
53 EditEngine with the content of a cell, note page or page template.
55 class EDITENG_DLLPUBLIC SvxEditSource
57 public:
58 SvxEditSource() = default;
59 SvxEditSource(SvxEditSource const &) = default;
60 SvxEditSource(SvxEditSource &&) = default;
61 SvxEditSource & operator =(SvxEditSource const &) = default;
62 SvxEditSource & operator =(SvxEditSource &&) = default;
64 virtual ~SvxEditSource();
66 /// Returns a new reference to the same object. This is a shallow copy
67 virtual std::unique_ptr<SvxEditSource> Clone() const = 0;
69 /** Query the text forwarder
71 @return the text forwarder, or NULL if the underlying object is dead
73 virtual SvxTextForwarder* GetTextForwarder() = 0;
75 /** Query the view forwarder
77 @return the view forwarder, or NULL if the underlying object
78 is dead, or if no view is available
80 @derive default implementation provided, overriding is optional
82 virtual SvxViewForwarder* GetViewForwarder();
84 /** Query the edit view forwarder
86 @param bCreate
87 Determines whether an EditView should be created, if there is
88 none active. If set to sal_True, and the underlying object is
89 not in EditMode, the text forwarder changes and the object is
90 set to EditMode.
92 @return the edit view forwarder, or NULL if the underlying
93 object is dead, or if no view is available (if bCreate is
94 sal_False, NULL is also returned if the object is not in
95 EditMode)
97 @derive default implementation provided, overriding is optional
99 @attention If this method is called with bCreate equal to
100 sal_True, all previously returned text forwarder can become
101 invalid
103 virtual SvxEditViewForwarder* GetEditViewForwarder( bool bCreate = false );
105 /// Write back data to model
106 virtual void UpdateData() = 0;
108 /** Returns broadcaster the underlying edit engine's events are sent from
110 @derive default implementation provided, overriding is optional
112 virtual SfxBroadcaster& GetBroadcaster() const;
114 /** adds the given SvxUnoTextRangeBase to the text object
115 capsulated by this SvxEditSource. This allows the text
116 object to inform all created text ranges about changes
117 and also allows to re use already created instances.
118 All SvxUnoTextRangeBase must remove itself with
119 removeRange() before they are deleted. */
120 virtual void addRange( SvxUnoTextRangeBase* pNewRange );
122 /** removes the given SvxUnoTextRangeBase from the text
123 object capsulated by this SvxEditSource. This text range
124 will not be informed any longer of changes on the underlying
125 text and will also not re used anymore. */
126 virtual void removeRange( SvxUnoTextRangeBase* pOldRange );
128 /** returns a const list of all text ranges that are registered
129 for the underlying text object. */
130 virtual const SvxUnoTextRangeBaseVec& getRanges() const;
134 /** Contains an EditEngine or an Outliner and unifies access to them.
136 The EditEngine-UNO objects use this class only. To reflect changes
137 not only in the EditEngine, but also in the model data, call
138 SvxEditSource::UpdateData(). This copies back the data to the model's
139 EditTextObject/OutlinerParaObject.
141 class EDITENG_DLLPUBLIC SvxTextForwarder
143 public:
144 virtual ~SvxTextForwarder() COVERITY_NOEXCEPT_FALSE;
146 virtual sal_Int32 GetParagraphCount() const = 0;
147 virtual sal_Int32 GetTextLen( sal_Int32 nParagraph ) const = 0;
148 virtual OUString GetText( const ESelection& rSel ) const = 0;
149 virtual SfxItemSet GetAttribs( const ESelection& rSel, EditEngineAttribs nOnlyHardAttrib = EditEngineAttribs::All ) const = 0;
150 virtual SfxItemSet GetParaAttribs( sal_Int32 nPara ) const = 0;
151 virtual void SetParaAttribs( sal_Int32 nPara, const SfxItemSet& rSet ) = 0;
152 virtual void RemoveAttribs( const ESelection& rSelection ) = 0;
153 virtual void GetPortions( sal_Int32 nPara, std::vector<sal_Int32>& rList ) const = 0;
155 virtual OUString GetStyleSheet(sal_Int32 nPara) const = 0;
156 virtual void SetStyleSheet(sal_Int32 nPara, const OUString& rStyleName) = 0;
158 virtual SfxItemState GetItemState( const ESelection& rSel, sal_uInt16 nWhich ) const = 0;
159 virtual SfxItemState GetItemState( sal_Int32 nPara, sal_uInt16 nWhich ) const = 0;
161 virtual void QuickInsertText( const OUString& rText, const ESelection& rSel ) = 0;
162 virtual void QuickInsertField( const SvxFieldItem& rFld, const ESelection& rSel ) = 0;
163 virtual void QuickSetAttribs( const SfxItemSet& rSet, const ESelection& rSel ) = 0;
164 virtual void QuickInsertLineBreak( const ESelection& rSel ) = 0;
166 virtual OUString CalcFieldValue( const SvxFieldItem& rField, sal_Int32 nPara, sal_Int32 nPos, std::optional<Color>& rpTxtColor, std::optional<Color>& rpFldColor, std::optional<FontLineStyle>& rpFldLineStyle ) = 0;
167 virtual void FieldClicked( const SvxFieldItem& rField ) = 0;
169 virtual SfxItemPool* GetPool() const = 0;
171 virtual const SfxItemSet* GetEmptyItemSetPtr() = 0;
173 // implementation functions for XParagraphAppend and XTextPortionAppend
174 virtual void AppendParagraph() = 0;
175 virtual sal_Int32 AppendTextPortion( sal_Int32 nPara, const OUString &rText, const SfxItemSet &rSet ) = 0;
177 // XTextCopy
178 virtual void CopyText(const SvxTextForwarder& rSource) = 0;
180 /** Query state of forwarder
182 @return false, if no longer valid
184 virtual bool IsValid() const = 0;
186 /** Query language of character at given position on the underlying edit engine
188 @param nPara[0 .. n-1]
189 Index of paragraph to query language in
191 @param nIndex[0 .. m-1]
192 Index of character to query language of
194 virtual LanguageType GetLanguage( sal_Int32 nPara, sal_Int32 nIndex ) const = 0;
196 /** Query number of fields in the underlying edit engine
198 @param nPara[0 .. n-1]
199 Index of paragraph to query field number in
201 virtual sal_Int32 GetFieldCount( sal_Int32 nPara ) const = 0;
203 /** Query information for given field number in the underlying edit engine
205 @param nPara[0 .. n-1]
206 Index of paragraph to query field info in
208 @param nField[0 .. m-1]
209 Index of field to query information of
211 virtual EFieldInfo GetFieldInfo( sal_Int32 nPara, sal_uInt16 nField ) const = 0;
213 /** Query information regarding bullets for given paragraph on the underlying edit engine
215 @param nPara[0 .. n-1]
216 Index of paragraph to query bullet info on
218 virtual EBulletInfo GetBulletInfo( sal_Int32 nPara ) const = 0;
220 /** Query the bounding rectangle of the given character
222 @param nPara[0 .. n]
223 Index of paragraph to query the bounds in. <p>The virtual
224 character after the last character of the represented text,
225 i.e. the one at position n is a special case. Because it does
226 not represent an existing character its bounding box is
227 defined in relation to preceding characters. It should be
228 roughly equivalent to the bounding box of some character when
229 inserted at the end of the text. Its height typically being
230 the maximal height of all the characters in the text or the
231 height of the preceding character, its width being at least
232 one pixel so that the bounding box is not degenerate.<br>
234 @param nIndex[0 .. m-1]
235 Index of character to query the bounds of
237 @return rectangle in logical coordinates, relative to upper
238 left corner of text. The coordinates returned here are to be
239 interpreted in the map mode given by GetMapMode().
241 virtual tools::Rectangle GetCharBounds( sal_Int32 nPara, sal_Int32 nIndex ) const = 0;
243 /** Query the bounding rectangle of the given paragraph
245 @param nPara[0 .. n-1]
246 Index of paragraph to query the bounds of
248 @return rectangle in logical coordinates, relative to upper
249 left corner of text. The coordinates returned here are to be
250 interpreted in the map mode given by GetMapMode().
252 virtual tools::Rectangle GetParaBounds( sal_Int32 nPara ) const = 0;
254 /** Query the map mode of the underlying EditEngine/Outliner
256 @return the map mode used on the EditEngine/Outliner. The
257 values returned by GetParaBounds() and GetCharBounds() are to
258 be interpreted in this map mode, the point given to
259 GetIndexAtPoint() is interpreted in this map mode.
261 virtual MapMode GetMapMode() const = 0;
263 /** Query the reference output device of the underlying EditEngine/Outliner
265 @return the OutputDevice used from the EditEngine/Outliner to
266 format the text. It should be used when performing e.g. font
267 calculations, since this is usually a printer with fonts and
268 resolution different from the screen.
270 virtual OutputDevice* GetRefDevice() const = 0;
272 /** Query paragraph and character index of the character at the
273 given point. Returns sal_True on success, sal_False otherwise
275 @param rPoint
276 Point to query text position of. Is interpreted in logical
277 coordinates, relative to the upper left corner of the text, and
278 in the map mode given by GetMapMode()
280 @param rPara[0 .. n-1]
281 Index of paragraph the point is within
283 @param rIndex[0 .. m-1]
284 Index of character the point is over
286 @return true, if the point is over any text and both rPara and rIndex are valid
289 virtual bool GetIndexAtPoint( const Point& rPoint, sal_Int32& rPara, sal_Int32& rIndex ) const = 0;
291 /** Get the start and the end index of the word at the given index
293 An index value on a word leads from the first character of
294 that word up to and including the last space before the next
295 word. The index values returned do not contain any leading or
296 trailing white-space. If the input indices are invalid,
297 sal_False is returned.
299 @param nPara[0 .. n-1]
300 Index of paragraph to start the search in
302 @param nIndex[0 .. m-1]
303 Index of character to query the search on
305 @param rStart
306 Start index (in the same paragraph)
308 @param rEnd
309 End index (in the same paragraph), this point to the last
310 character still contained in the query
312 @return true, if the result is non-empty
314 virtual bool GetWordIndices( sal_Int32 nPara, sal_Int32 nIndex, sal_Int32& rStart, sal_Int32& rEnd ) const = 0;
316 /** Query range of similar attributes
318 Please note that the range returned is half-open: [nStartIndex,nEndIndex)
320 @param nStartIndex
321 Herein, the start index of the range of similar attributes is returned
323 @param nEndIndex
324 Herein, the end index (exclusive) of the range of similar attributes is returned
326 @param nIndex
327 The character index from which the range of similar attributed characters is requested
329 @return true, if the range has been successfully determined
331 virtual bool GetAttributeRun( sal_Int32& nStartIndex, sal_Int32& nEndIndex, sal_Int32 nPara, sal_Int32 nIndex, bool bInCell = false ) const = 0;
333 /** Query number of lines in the formatted paragraph
335 @param nPara[0 .. n-1]
336 Index of paragraph to query number of lines in
338 @return number of lines in given paragraph
341 virtual sal_Int32 GetLineCount( sal_Int32 nPara ) const = 0;
343 /** Query line length
345 @param nPara[0 .. n-1]
346 Index of paragraph to query line length in
348 @param nLine[0 .. m-1]
349 Index of line in paragraph to query line length of
352 virtual sal_Int32 GetLineLen( sal_Int32 nPara, sal_Int32 nLine ) const = 0;
354 /** Query bounds of line in paragraph
356 @param rStart [output param; 0 .. text_len]
357 The index in the paragraph text that belongs to the chara at the start of the line
359 @param rEnd [output param; 0 .. text_len]
360 The index in the paragraph text that follows the last chara in the line
362 @param nParagraph[0 .. n-1]
363 Index of paragraph to query line length in
365 @param nLine[0 .. m-1]
366 Index of line in paragraph to query line length of
369 virtual void GetLineBoundaries( /*out*/sal_Int32 &rStart, /*out*/sal_Int32 &rEnd, sal_Int32 nParagraph, sal_Int32 nLine ) const = 0;
371 /** Query the line number for an index in the paragraphs text
373 @param nPara[0 .. n-1]
374 Index of paragraph to query line length in
376 @param nIndex[0 .. m-1]
377 Index of the char in the paragraph text
379 @returns [0 .. k-1]
380 The line number of the char in the paragraph
382 virtual sal_Int32 GetLineNumberAtIndex( sal_Int32 nPara, sal_Int32 nIndex ) const = 0;
384 /** Delete given text range and reformat text
386 @param rSelection
387 The text range to be deleted
389 @return true if text has been successfully deleted
391 virtual bool Delete( const ESelection& rSelection ) = 0;
393 /** Insert/Replace given text in given range and reformat text
395 @param rText
396 Text to insert/replace
398 @param rSel
399 Selection where text should be replaced. The empty selection inserts
401 @return true if text has been successfully inserted
403 virtual bool InsertText( const OUString& rText, const ESelection& rSel ) = 0;
405 /** Updates the formatting
407 @see EditEngine::QuickFormatDoc() for details
409 @return true if text have been successfully reformatted
411 virtual bool QuickFormatDoc( bool bFull = false ) = 0;
413 /** Get the outline depth of given paragraph
415 @param nPara
416 Index of the paragraph to query the depth of
418 @return the outline level of the given paragraph. The range is
419 [0,n), where n is the maximal outline level.
421 virtual sal_Int16 GetDepth( sal_Int32 nPara ) const = 0;
423 /** Set the outline depth of given paragraph
425 @param nPara
426 Index of the paragraph to set the depth of
428 @param nNewDepth
429 The depth to set on the given paragraph. The range is
430 [0,n), where n is the maximal outline level.
432 @return true, if depth could be successfully set. Reasons for
433 failure are e.g. the text does not support outline level
434 (EditEngine), or the depth range is exceeded.
436 virtual bool SetDepth( sal_Int32 nPara, sal_Int16 nNewDepth ) = 0;
438 virtual sal_Int32 GetNumberingStartValue( sal_Int32 nPara );
439 virtual void SetNumberingStartValue( sal_Int32 nPara, sal_Int32 nNumberingStartValue );
441 virtual bool IsParaIsNumberingRestart( sal_Int32 nPara );
442 virtual void SetParaIsNumberingRestart( sal_Int32 nPara, bool bParaIsNumberingRestart );
445 /** Encapsulates the document view for the purpose of unified
446 EditEngine/Outliner access.
448 This one has to be different from the SvxEditViewForwarder, since
449 the latter is only valid in edit mode.
451 class EDITENG_DLLPUBLIC SvxViewForwarder
453 public:
454 virtual ~SvxViewForwarder();
456 /** Query state of forwarder
458 @return sal_False, if no longer valid
460 virtual bool IsValid() const = 0;
462 /** Convert from logical, EditEngine-relative coordinates to screen coordinates
464 @param rPoint
465 Point in logical, EditEngine-relative coordinates.
467 @param rMapMode
468 The map mode to interpret the coordinates in.
470 @return the point in screen coordinates
472 virtual Point LogicToPixel( const Point& rPoint, const MapMode& rMapMode ) const = 0;
474 /** Convert from screen to logical, EditEngine-relative coordinates
476 @param rPoint
477 Point in screen coordinates
479 @param rMapMode
480 The map mode to interpret the coordinates in.
482 @return the point in logical coordinates.
484 virtual Point PixelToLogic( const Point& rPoint, const MapMode& rMapMode ) const = 0;
489 /** Encapsulates EditView and OutlinerView for the purpose of unified
490 EditEngine/Outliner access
492 class SvxEditViewForwarder : public SvxViewForwarder
494 public:
496 /** Query current selection.
498 @param rSelection
499 Contains the current selection after method call
501 @return false, if there is no view or no selection (the empty selection _is_ a selection)
504 virtual bool GetSelection( ESelection& rSelection ) const = 0;
506 /** Set selection in view.
508 @param rSelection
509 The selection to set
511 @return false, if there is no view or selection is invalid
513 virtual bool SetSelection( const ESelection& rSelection ) = 0;
515 /** Copy current selection to clipboard.
517 @return false if no selection or no view (the empty selection _is_ a selection)
519 virtual bool Copy() = 0;
521 /** Cut current selection to clipboard.
523 @return false if no selection or no view (the empty selection _is_ a selection)
525 virtual bool Cut() = 0;
527 /** Paste clipboard into current selection.
529 @return false if no view or no selection (the empty selection _is_ a selection)
531 virtual bool Paste() = 0;
535 #endif
537 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */