Fix build
[LibreOffice.git] / sc / inc / validat.hxx
blob4afd9e2ba8f2c654c597e75796355615012a8f1c
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 "conditio.hxx"
23 #include "scdllapi.h"
25 namespace weld { class Window; }
27 namespace sc {
29 struct RefUpdateContext;
33 namespace vcl { class ILibreOfficeKitNotifier; }
35 class ScPatternAttr;
36 class ScTokenArray;
37 class ScTypedStrData;
38 struct ScValidationDataIsNumeric;
40 enum ScValidationMode
42 SC_VALID_ANY,
43 SC_VALID_WHOLE,
44 SC_VALID_DECIMAL,
45 SC_VALID_DATE,
46 SC_VALID_TIME,
47 SC_VALID_TEXTLEN,
48 SC_VALID_LIST,
49 SC_VALID_CUSTOM
52 enum ScValidErrorStyle
54 SC_VALERR_STOP,
55 SC_VALERR_WARNING,
56 SC_VALERR_INFO,
57 SC_VALERR_MACRO
60 // Entry for validation (only one condition exists)
62 class SAL_DLLPUBLIC_RTTI ScValidationData final : public ScConditionEntry
64 private:
65 sal_uInt32 nKey; // index in attributes
67 ScValidationMode eDataMode;
68 bool bShowInput;
69 bool bShowError;
70 ScValidErrorStyle eErrorStyle;
71 sal_Int16 mnListType; // selection list type: none, unsorted, sorted.
72 OUString aInputTitle;
73 OUString aInputMessage;
74 OUString aErrorTitle;
75 OUString aErrorMessage;
77 bool DoMacro( const ScAddress& rPos, const OUString& rInput,
78 ScFormulaCell* pCell, weld::Window* pParent ) const;
80 bool DoScript( const ScAddress& rPos, const OUString& rInput,
81 ScFormulaCell* pCell, weld::Window* pParent ) const;
83 using ScConditionEntry::operator==;
85 DECL_STATIC_LINK(ScValidationData, InstallLOKNotifierHdl, void*, vcl::ILibreOfficeKitNotifier*);
87 public:
88 SC_DLLPUBLIC ScValidationData( ScValidationMode eMode, ScConditionMode eOper,
89 const OUString& rExpr1, const OUString& rExpr2,
90 ScDocument& rDocument, const ScAddress& rPos,
91 const OUString& rExprNmsp1 = OUString(), const OUString& rExprNmsp2 = OUString(),
92 formula::FormulaGrammar::Grammar eGrammar1 = formula::FormulaGrammar::GRAM_DEFAULT,
93 formula::FormulaGrammar::Grammar eGrammar2 = formula::FormulaGrammar::GRAM_DEFAULT );
94 SC_DLLPUBLIC ScValidationData( ScValidationMode eMode, ScConditionMode eOper,
95 const ScTokenArray* pArr1, const ScTokenArray* pArr2,
96 ScDocument& rDocument, const ScAddress& rPos );
97 SC_DLLPUBLIC ScValidationData( const ScValidationData& r );
98 ScValidationData( ScDocument& rDocument, const ScValidationData& r );
99 SC_DLLPUBLIC virtual ~ScValidationData() override;
101 ScValidationData* Clone() const // real copy
102 { return new ScValidationData( *GetDocument(), *this ); }
103 ScValidationData* Clone(ScDocument* pNew) const override
104 { return new ScValidationData( *pNew, *this ); }
106 SC_DLLPUBLIC void ResetInput();
107 SC_DLLPUBLIC void ResetError();
108 SC_DLLPUBLIC void SetInput( const OUString& rTitle, const OUString& rMsg );
109 SC_DLLPUBLIC void SetError( const OUString& rTitle, const OUString& rMsg,
110 ScValidErrorStyle eStyle );
112 bool GetInput( OUString& rTitle, OUString& rMsg ) const
113 { rTitle = aInputTitle; rMsg = aInputMessage; return bShowInput; }
114 SC_DLLPUBLIC bool GetErrMsg( OUString& rTitle, OUString& rMsg, ScValidErrorStyle& rStyle ) const;
116 ScValidationMode GetDataMode() const { return eDataMode; }
118 sal_Int16 GetListType() const { return mnListType; }
119 void SetListType( sal_Int16 nListType ) { mnListType = nListType; }
121 /** Returns true, if the validation cell will show a selection list.
122 @descr Use this instead of GetListType() which returns the raw property
123 regardless of the validation type. */
124 bool HasSelectionList() const;
125 /** Tries to fill the passed collection with list validation entries.
126 @descr Fills the list only, if this is a list validation and IsShowList() is enabled.
127 @param rStrings (out-param) The string list to fill with list validation entries.
128 @return true = rStrings has been filled with at least one entry. */
129 SC_DLLPUBLIC bool FillSelectionList(std::vector<ScTypedStrData>& rStrings, const ScAddress& rPos) const;
131 // with string: during input, with cell: for detective / RC_FORCED
132 bool IsDataValid(
133 const OUString& rTest, const ScPatternAttr& rPattern, const ScAddress& rPos ) const;
135 // Custom validations (SC_VALID_CUSTOM) should be validated using this specific method.
136 // Take care that internally this method commits to the to be validated cell the new input,
137 // in order to be able to interpret the validating boolean formula on the new input.
138 // After the formula has been evaluated the original cell content is restored.
139 // At present is only used in ScInputHandler::EnterHandler: handling this case in the
140 // regular IsDataValid method would have been unsafe since it can be invoked
141 // by ScFormulaCell::InterpretTail.
143 struct CustomValidationPrivateAccess
145 // so IsDataValidCustom can be invoked only by ScInputHandler methods
146 friend class ScInputHandler;
147 private:
148 CustomValidationPrivateAccess() {}
151 bool IsDataValidCustom(
152 const OUString& rTest, const ScPatternAttr& rPattern,
153 const ScAddress& rPos, const CustomValidationPrivateAccess& ) const;
155 SC_DLLPUBLIC bool IsDataValid( ScRefCellValue& rCell, const ScAddress& rPos ) const;
157 /** Test, if formula is valid. */
158 bool isFormulaResultsValidatable(const OUString& rTest, const ScAddress& rPos, SvNumberFormatter* pFormatter,
159 OUString& rStrResult, double& nVal, sal_uInt32& nFormat, bool& bIsVal) const;
161 // TRUE -> break
162 bool DoError(weld::Window* pParent, const OUString& rInput, const ScAddress& rPos) const;
163 void DoCalcError( ScFormulaCell* pCell ) const;
165 bool IsEmpty() const;
166 sal_uInt32 GetKey() const { return nKey; }
167 void SetKey(sal_uInt32 nNew) { nKey = nNew; } // only if not inserted!
169 SC_DLLPUBLIC bool EqualEntries( const ScValidationData& r ) const; // for undo
171 // sort (using std::set) by index
172 bool operator < ( const ScValidationData& r ) const { return nKey < r.nKey; }
174 private:
175 /** Tries to fill the passed collection with list validation entries.
176 @descr Fills the list only if it is non-NULL,
177 @param pStrings (out-param) Optionally NULL, string list to fill with list validation entries.
178 @param pCell can be NULL if it is not necessary to which element in the list is selected.
179 @param rPos the base address for relative references.
180 @param rTokArr Formula token array.
181 @param rMatch (out-param) the index of the first item that matched, -1 if nothing matched.
182 @return true = Cell range found, rRange is valid, or an error entry stuffed into the list if pCell==NULL. */
183 bool GetSelectionFromFormula(
184 std::vector<ScTypedStrData>* pStrings, ScRefCellValue& rCell, const ScAddress& rPos,
185 const ScTokenArray& rTokArr, int& rMatch) const;
187 /** Tests, if pCell is equal to what the passed token array represents. */
188 bool IsEqualToTokenArray( ScRefCellValue& rCell, const ScAddress& rPos, const ScTokenArray& rTokArr ) const;
190 /** Tests, if contents of pCell occur in cell range referenced by own formula, or in a string list. */
191 bool IsListValid( ScRefCellValue& rCell, const ScAddress& rPos ) const;
193 /** Tests, if string or numeric data has valid text length.
194 @param pDataNumeric
195 nullptr if string data to be tested, else for numeric data a
196 properly initialized ScValidationDataIsNumeric struct, see
197 implementation.
199 bool IsDataValidTextLen( std::u16string_view rTest, const ScAddress& rPos,
200 ScValidationDataIsNumeric* pDataNumeric ) const;
203 // list of conditions:
205 struct CompareScValidationDataPtr
207 bool operator()( std::unique_ptr<ScValidationData> const& lhs, std::unique_ptr<ScValidationData> const& rhs ) const { return (*lhs)<(*rhs); }
210 class ScValidationDataList
212 private:
213 typedef std::set<std::unique_ptr<ScValidationData>, CompareScValidationDataPtr> ScValidationDataListDataType;
214 ScValidationDataListDataType maData;
216 public:
217 ScValidationDataList() {}
218 ScValidationDataList(const ScValidationDataList& rList);
219 ScValidationDataList(ScDocument& rNewDoc, const ScValidationDataList& rList);
221 typedef ScValidationDataListDataType::iterator iterator;
222 typedef ScValidationDataListDataType::const_iterator const_iterator;
224 iterator begin();
225 const_iterator begin() const;
226 iterator end();
227 const_iterator end() const;
229 void InsertNew( std::unique_ptr<ScValidationData> pNew )
230 { maData.insert(std::move(pNew)); }
232 ScValidationData* GetData( sal_uInt32 nKey );
234 void CompileXML();
235 void UpdateReference( sc::RefUpdateContext& rCxt );
236 void UpdateInsertTab( sc::RefUpdateInsertTabContext& rCxt );
237 void UpdateDeleteTab( sc::RefUpdateDeleteTabContext& rCxt );
238 void UpdateMoveTab( sc::RefUpdateMoveTabContext& rCxt );
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */