LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sc / inc / validat.hxx
blob9310a3804393eecf1ba34875ca45cc17a8e8b848
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 SC_DLLPUBLIC 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 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 ScValidationData( ScValidationMode eMode, ScConditionMode eOper,
95 const ScTokenArray* pArr1, const ScTokenArray* pArr2,
96 ScDocument& rDocument, const ScAddress& rPos );
97 ScValidationData( const ScValidationData& r );
98 ScValidationData( ScDocument& rDocument, const ScValidationData& r );
99 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 void ResetInput();
107 void ResetError();
108 void SetInput( const OUString& rTitle, const OUString& rMsg );
109 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 bool GetErrMsg( OUString& rTitle, OUString& rMsg, ScValidErrorStyle& rStyle ) const;
116 bool HasErrMsg() const { return bShowError; }
118 ScValidationMode GetDataMode() const { return eDataMode; }
120 sal_Int16 GetListType() const { return mnListType; }
121 void SetListType( sal_Int16 nListType ) { mnListType = nListType; }
123 /** Returns true, if the validation cell will show a selection list.
124 @descr Use this instead of GetListType() which returns the raw property
125 regardless of the validation type. */
126 bool HasSelectionList() const;
127 /** Tries to fill the passed collection with list validation entries.
128 @descr Fills the list only, if this is a list validation and IsShowList() is enabled.
129 @param rStrings (out-param) The string list to fill with list validation entries.
130 @return true = rStrings has been filled with at least one entry. */
131 bool FillSelectionList(std::vector<ScTypedStrData>& rStrings, const ScAddress& rPos) const;
133 // with string: during input, with cell: for detective / RC_FORCED
134 bool IsDataValid(
135 const OUString& rTest, const ScPatternAttr& rPattern, const ScAddress& rPos ) const;
137 // Custom validations (SC_VALID_CUSTOM) should be validated using this specific method.
138 // Take care that internally this method commits to the to be validated cell the new input,
139 // in order to be able to interpret the validating boolean formula on the new input.
140 // After the formula has been evaluated the original cell content is restored.
141 // At present is only used in ScInputHandler::EnterHandler: handling this case in the
142 // regular IsDataValid method would have been unsafe since it can be invoked
143 // by ScFormulaCell::InterpretTail.
145 struct CustomValidationPrivateAccess
147 // so IsDataValidCustom can be invoked only by ScInputHandler methods
148 friend class ScInputHandler;
149 private:
150 CustomValidationPrivateAccess() {}
153 bool IsDataValidCustom(
154 const OUString& rTest, const ScPatternAttr& rPattern,
155 const ScAddress& rPos, const CustomValidationPrivateAccess& ) const;
157 bool IsDataValid( ScRefCellValue& rCell, const ScAddress& rPos ) const;
159 // TRUE -> break
160 bool DoError(weld::Window* pParent, const OUString& rInput, const ScAddress& rPos) const;
161 void DoCalcError( ScFormulaCell* pCell ) const;
163 bool IsEmpty() const;
164 sal_uInt32 GetKey() const { return nKey; }
165 void SetKey(sal_uInt32 nNew) { nKey = nNew; } // only if not inserted!
167 bool EqualEntries( const ScValidationData& r ) const; // for undo
169 // sort (using std::set) by index
170 bool operator < ( const ScValidationData& r ) const { return nKey < r.nKey; }
172 private:
173 /** Tries to fill the passed collection with list validation entries.
174 @descr Fills the list only if it is non-NULL,
175 @param pStrings (out-param) Optionally NULL, string list to fill with list validation entries.
176 @param pCell can be NULL if it is not necessary to which element in the list is selected.
177 @param rPos the base address for relative references.
178 @param rTokArr Formula token array.
179 @param rMatch (out-param) the index of the first item that matched, -1 if nothing matched.
180 @return true = Cell range found, rRange is valid, or an error entry stuffed into the list if pCell==NULL. */
181 bool GetSelectionFromFormula(
182 std::vector<ScTypedStrData>* pStrings, ScRefCellValue& rCell, const ScAddress& rPos,
183 const ScTokenArray& rTokArr, int& rMatch) const;
185 /** Tests, if pCell is equal to what the passed token array represents. */
186 bool IsEqualToTokenArray( ScRefCellValue& rCell, const ScAddress& rPos, const ScTokenArray& rTokArr ) const;
188 /** Tests, if contents of pCell occur in cell range referenced by own formula, or in a string list. */
189 bool IsListValid( ScRefCellValue& rCell, const ScAddress& rPos ) const;
191 /** Tests, if string or numeric data has valid text length.
192 @param pDataNumeric
193 nullptr if string data to be tested, else for numeric data a
194 properly initialized ScValidationDataIsNumeric struct, see
195 implementation.
197 bool IsDataValidTextLen( const OUString& rTest, const ScAddress& rPos,
198 ScValidationDataIsNumeric* pDataNumeric ) const;
201 // list of conditions:
203 struct CompareScValidationDataPtr
205 bool operator()( std::unique_ptr<ScValidationData> const& lhs, std::unique_ptr<ScValidationData> const& rhs ) const { return (*lhs)<(*rhs); }
208 class ScValidationDataList
210 private:
211 typedef std::set<std::unique_ptr<ScValidationData>, CompareScValidationDataPtr> ScValidationDataListDataType;
212 ScValidationDataListDataType maData;
214 public:
215 ScValidationDataList() {}
216 ScValidationDataList(const ScValidationDataList& rList);
217 ScValidationDataList(ScDocument& rNewDoc, const ScValidationDataList& rList);
219 typedef ScValidationDataListDataType::iterator iterator;
220 typedef ScValidationDataListDataType::const_iterator const_iterator;
222 iterator begin();
223 const_iterator begin() const;
224 iterator end();
225 const_iterator end() const;
227 void InsertNew( std::unique_ptr<ScValidationData> pNew )
228 { maData.insert(std::move(pNew)); }
230 ScValidationData* GetData( sal_uInt32 nKey );
232 void CompileXML();
233 void UpdateReference( sc::RefUpdateContext& rCxt );
234 void UpdateInsertTab( sc::RefUpdateInsertTabContext& rCxt );
235 void UpdateDeleteTab( sc::RefUpdateDeleteTabContext& rCxt );
236 void UpdateMoveTab( sc::RefUpdateMoveTabContext& rCxt );
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */