Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / inc / editutil.hxx
blobce102885ae4f6410c788263256d97aa0363f0585
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 #pragma once
22 #include <memory>
23 #include "scdllapi.h"
24 #include "types.hxx"
25 #include <editeng/editeng.hxx>
26 #include <svx/pageitem.hxx>
27 #include <tools/datetime.hxx>
28 #include <tools/gen.hxx>
29 #include <tools/fract.hxx>
30 #include <vcl/outdev.hxx>
32 class ScDocument;
33 class ScPatternAttr;
34 class ScEditEngineDefaulter;
36 class ScEditUtil
38 ScDocument* pDoc;
39 SCCOL nCol;
40 SCROW nRow;
41 SCTAB nTab;
42 Point aCellPos;
43 VclPtr<OutputDevice> pDev; // MapMode has to be set
44 double nPPTX;
45 double nPPTY;
46 Fraction aZoomX;
47 Fraction aZoomY;
48 bool bInPrintTwips;
50 public:
51 static OUString ModifyDelimiters( const OUString& rOld );
53 /// Retrieves string with paragraphs delimited by spaces
54 static OUString GetSpaceDelimitedString( const EditEngine& rEngine );
56 /// Retrieves string with paragraphs delimited by new lines ('\n').
57 static OUString GetMultilineString( const EditEngine& rEngine );
58 static OUString GetMultilineString( const EditTextObject& rEdit );
60 /** Retrieves string with paragraphs delimited by new lines ('\n').
62 @param pDoc
63 If not NULL, use pDoc->GetEditEngine() to retrieve field content.
64 If NULL, a static mutex-guarded ScFieldEditEngine is used that
65 is not capable of resolving document specific fields; avoid.
67 SC_DLLPUBLIC static OUString GetString( const EditTextObject& rEditText, const ScDocument* pDoc );
69 static std::unique_ptr<EditTextObject> CreateURLObjectFromURL(
70 ScDocument& rDoc, const OUString& rURL, const OUString& rText );
72 static void RemoveCharAttribs( EditTextObject& rEditText, const ScPatternAttr& rAttr );
74 static std::unique_ptr<EditTextObject> Clone( const EditTextObject& rSrc, ScDocument& rDestDoc );
76 static OUString GetCellFieldValue(
77 const SvxFieldData& rFieldData, const ScDocument* pDoc, std::optional<Color>* ppTextColor, std::optional<FontLineStyle>* ppFldLineStyle );
79 public:
80 ScEditUtil( ScDocument* pDocument, SCCOL nX, SCROW nY, SCTAB nZ,
81 const Point& rCellPos,
82 OutputDevice* pDevice, double nScaleX, double nScaleY,
83 const Fraction& rX, const Fraction& rY, bool bPrintTwips = false );
85 tools::Rectangle GetEditArea( const ScPatternAttr* pPattern, bool bForceToTop );
86 tools::Long GetIndent(const ScPatternAttr* pPattern) const;
87 void GetMargins(const ScPatternAttr* pPattern, tools::Long& nLeftMargin, tools::Long& nTopMargin,
88 tools::Long& nRightMargin, tools::Long& BottomMargin) const;
92 class ScEditAttrTester
94 std::unique_ptr<SfxItemSet> pEditAttrs;
95 bool bNeedsObject;
96 bool bNeedsCellAttr;
98 public:
99 ScEditAttrTester( ScEditEngineDefaulter* pEng );
100 ~ScEditAttrTester();
102 bool NeedsObject() const { return bNeedsObject; }
103 bool NeedsCellAttr() const { return bNeedsCellAttr; }
104 const SfxItemSet& GetAttribs() const { return *pEditAttrs; }
107 // construct pool before constructing EditEngine, destroy pool after EditEngine
108 class ScEnginePoolHelper
110 protected:
111 rtl::Reference<SfxItemPool> pEnginePool;
112 SfxItemSet* pDefaults;
113 bool bDeleteEnginePool;
114 bool bDeleteDefaults;
116 ScEnginePoolHelper( SfxItemPool* pEnginePool, bool bDeleteEnginePool );
117 ScEnginePoolHelper( const ScEnginePoolHelper& rOrg );
118 virtual ~ScEnginePoolHelper();
121 class SC_DLLPUBLIC ScEditEngineDefaulter : public ScEnginePoolHelper, public EditEngine
123 public:
124 using EditEngine::SetText;
125 /// bDeleteEnginePool: Engine becomes the owner of the pool
126 /// and deletes it on destruction
127 ScEditEngineDefaulter( SfxItemPool* pEnginePool,
128 bool bDeleteEnginePool = false );
129 /// If rOrg.bDeleteEnginePool: pool gets cloned and will be
130 /// deleted on destruction. Defaults are not set.
131 ScEditEngineDefaulter( const ScEditEngineDefaulter& rOrg );
132 virtual ~ScEditEngineDefaulter() override;
134 /// Creates a copy of SfxItemSet if bRememberCopy set
135 void SetDefaults( const SfxItemSet& rDefaults, bool bRememberCopy = true );
137 /// Becomes the owner of the SfxItemSet
138 void SetDefaults( std::unique_ptr<SfxItemSet> pDefaults );
140 /// Set the item in the default ItemSet which is created
141 /// if it doesn't exist yet.
142 /// The default ItemSet is then applied to each paragraph.
143 void SetDefaultItem( const SfxPoolItem& rItem );
145 /// Returns the stored defaults, used to find non-default character attributes
146 const SfxItemSet& GetDefaults();
148 /// SetText and apply defaults already set
149 void SetTextCurrentDefaults( const EditTextObject& rTextObject );
150 /// Current defaults are not applied, new defaults are applied
151 void SetTextNewDefaults( const EditTextObject& rTextObject,
152 const SfxItemSet& rDefaults, bool bRememberCopy = true );
154 /// SetText and apply defaults already set
155 void SetTextCurrentDefaults( const OUString& rText );
156 /// Current defaults are not applied, new defaults are applied
157 void SetTextNewDefaults( const OUString& rText,
158 const SfxItemSet& rDefaults );
160 /// Paragraph attributes that are not defaults are copied to
161 /// character attributes and all paragraph attributes reset
162 void RemoveParaAttribs();
164 /// Re-apply existing defaults if set, same as in SetText,
165 /// but without EnableUndo/SetUpdateMode.
166 void RepeatDefaults();
169 // for field commands (or just fields?) in a table
170 class SC_DLLPUBLIC ScFieldEditEngine : public ScEditEngineDefaulter
172 private:
173 ScDocument* mpDoc;
174 bool bExecuteURL;
176 public:
177 ScFieldEditEngine(
178 ScDocument* pDoc, SfxItemPool* pEnginePool, SfxItemPool* pTextObjectPool = nullptr,
179 bool bDeleteEnginePool = false);
181 void SetExecuteURL(bool bSet) { bExecuteURL = bSet; }
183 virtual bool FieldClicked( const SvxFieldItem& rField ) override;
184 virtual OUString CalcFieldValue( const SvxFieldItem& rField, sal_Int32 nPara, sal_Int32 nPos, std::optional<Color>& rTxtColor, std::optional<Color>& rFldColor, std::optional<FontLineStyle>& rFldLineStyle ) override;
187 // 1/100 mm
188 class SC_DLLPUBLIC ScTabEditEngine final : public ScFieldEditEngine
190 private:
191 void Init(const ScPatternAttr& rPattern);
192 public:
193 ScTabEditEngine( ScDocument* pDoc ); // Default
194 ScTabEditEngine(const ScPatternAttr& rPattern,
195 SfxItemPool *pEngineItemPool, ScDocument *pDoc,
196 SfxItemPool* pTextObjectPool = nullptr );
199 struct ScHeaderFieldData
201 OUString aTitle; // title or file name (if no title)
202 OUString aLongDocName; // path and file name
203 OUString aShortDocName; // pure file name
204 OUString aTabName;
205 DateTime aDateTime;
206 tools::Long nPageNo;
207 tools::Long nTotalPages;
208 SvxNumType eNumType;
210 ScHeaderFieldData();
213 // for headers/footers with fields
214 class SC_DLLPUBLIC ScHeaderEditEngine final : public ScEditEngineDefaulter
216 private:
217 ScHeaderFieldData aData;
219 public:
220 ScHeaderEditEngine( SfxItemPool* pEnginePool );
221 virtual OUString CalcFieldValue( const SvxFieldItem& rField, sal_Int32 nPara, sal_Int32 nPos, std::optional<Color>& rTxtColor, std::optional<Color>& rFldColor, std::optional<FontLineStyle>& rFldLineStyle ) override;
223 void SetNumType(SvxNumType eNew) { aData.eNumType = eNew; }
224 void SetData(const ScHeaderFieldData& rNew) { aData = rNew; }
227 // for Note text objects.
228 class ScNoteEditEngine final : public ScEditEngineDefaulter
231 public:
232 ScNoteEditEngine( SfxItemPool* pEnginePool, SfxItemPool* pTextObjectPool );
236 // SvxFieldData derivations were moved to Svx (comment can be deleted?)
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */