LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sc / inc / dociter.hxx
blob99ceb1e99972c5fe05d0a1eb6092bdcffb2d44d7
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 "address.hxx"
23 #include "formulagroup.hxx"
24 #include "global.hxx"
25 #include "scdllapi.h"
26 #include "cellvalue.hxx"
27 #include "mtvelements.hxx"
28 #include "queryparam.hxx"
29 #include <vcl/outdev.hxx>
30 #include <vcl/vclptr.hxx>
32 #include <memory>
33 #include <set>
34 #include <vector>
36 class ScDocument;
37 class ScPatternAttr;
38 class ScAttrArray;
39 class ScAttrIterator;
40 class ScFlatBoolRowSegments;
41 class ScMatrix;
42 struct ScDBQueryParamBase;
43 struct ScQueryParam;
44 struct ScDBQueryParamInternal;
45 struct ScDBQueryParamMatrix;
46 class ScFormulaCell;
47 class OutputDevice;
48 struct ScInterpreterContext;
49 enum class SvNumFormatType : sal_Int16;
51 class ScValueIterator // walk through all values in an area
53 typedef sc::CellStoreType::const_position_type PositionType;
55 ScDocument& mrDoc;
56 ScInterpreterContext* pContext;
57 const ScAttrArray* pAttrArray;
58 sal_uInt32 nNumFormat; // for CalcAsShown
59 sal_uInt32 nNumFmtIndex;
60 ScAddress maStartPos;
61 ScAddress maEndPos;
62 SCCOL mnCol;
63 SCTAB mnTab;
64 SCROW nAttrEndRow;
65 SubtotalFlags mnSubTotalFlags;
66 SvNumFormatType nNumFmtType;
67 bool bNumValid;
68 bool bCalcAsShown;
69 bool bTextAsZero;
71 const sc::CellStoreType* mpCells;
72 PositionType maCurPos;
74 SCROW GetRow() const;
75 void IncBlock();
76 void IncPos();
78 /**
79 * See if the cell at the current position is a non-empty cell. If not,
80 * move to the next non-empty cell position.
82 bool GetThis( double& rValue, FormulaError& rErr );
84 public:
86 ScValueIterator(
87 ScDocument& rDocument, const ScRange& rRange, SubtotalFlags nSubTotalFlags = SubtotalFlags::NONE,
88 bool bTextAsZero = false );
90 void GetCurNumFmtInfo( const ScInterpreterContext& rContext, SvNumFormatType& nType, sal_uInt32& nIndex );
92 /// Does NOT reset rValue if no value found!
93 bool GetFirst( double& rValue, FormulaError& rErr );
95 /// Does NOT reset rValue if no value found!
96 bool GetNext( double& rValue, FormulaError& rErr );
98 void SetInterpreterContext( ScInterpreterContext* context ) { pContext = context; }
101 class ScDBQueryDataIterator
103 public:
104 struct Value
106 OUString maString;
107 double mfValue;
108 FormulaError mnError;
109 bool mbIsNumber;
111 Value();
114 private:
115 static const sc::CellStoreType* GetColumnCellStore(ScDocument& rDoc, SCTAB nTab, SCCOL nCol);
116 static const ScAttrArray* GetAttrArrayByCol(ScDocument& rDoc, SCTAB nTab, SCCOL nCol);
117 static bool IsQueryValid(ScDocument& rDoc, const ScQueryParam& rParam, SCTAB nTab, SCROW nRow, const ScRefCellValue* pCell);
119 class DataAccess
121 public:
122 DataAccess();
123 virtual ~DataAccess() = 0;
124 virtual bool getCurrent(Value& rValue) = 0;
125 virtual bool getFirst(Value& rValue) = 0;
126 virtual bool getNext(Value& rValue) = 0;
129 class DataAccessInternal final : public DataAccess
131 typedef std::pair<sc::CellStoreType::const_iterator,size_t> PositionType;
132 public:
133 DataAccessInternal(ScDBQueryParamInternal* pParam, ScDocument& rDoc, const ScInterpreterContext& rContext);
134 virtual ~DataAccessInternal() override;
135 virtual bool getCurrent(Value& rValue) override;
136 virtual bool getFirst(Value& rValue) override;
137 virtual bool getNext(Value& rValue) override;
139 private:
140 void incBlock();
141 void incPos();
143 const sc::CellStoreType* mpCells;
144 PositionType maCurPos;
145 ScDBQueryParamInternal* mpParam;
146 ScDocument& mrDoc;
147 const ScInterpreterContext& mrContext;
148 const ScAttrArray* pAttrArray;
149 sal_uInt32 nNumFormat; // for CalcAsShown
150 sal_uInt32 nNumFmtIndex;
151 SCCOL nCol;
152 SCROW nRow;
153 SCROW nAttrEndRow;
154 SCTAB nTab;
155 SvNumFormatType nNumFmtType;
156 bool bCalcAsShown;
159 class DataAccessMatrix final : public DataAccess
161 public:
162 DataAccessMatrix(ScDBQueryParamMatrix* pParam);
163 virtual ~DataAccessMatrix() override;
164 virtual bool getCurrent(Value& rValue) override;
165 virtual bool getFirst(Value& rValue) override;
166 virtual bool getNext(Value& rValue) override;
168 private:
169 bool isValidQuery(SCROW mnRow, const ScMatrix& rMat) const;
171 ScDBQueryParamMatrix* mpParam;
172 SCROW mnCurRow;
173 SCROW mnRows;
176 ::std::unique_ptr<ScDBQueryParamBase> mpParam;
177 ::std::unique_ptr<DataAccess> mpData;
179 public:
180 ScDBQueryDataIterator(ScDocument& rDocument, const ScInterpreterContext& rContext, std::unique_ptr<ScDBQueryParamBase> pParam);
181 /// Does NOT reset rValue if no value found!
182 bool GetFirst(Value& rValue);
183 /// Does NOT reset rValue if no value found!
184 bool GetNext(Value& rValue);
187 class ScFormulaGroupIterator
189 private:
190 ScDocument& mrDoc;
191 SCTAB mnTab;
192 SCCOL mnCol;
193 bool mbNullCol;
194 size_t mnIndex;
195 std::vector<sc::FormulaGroupEntry> maEntries;
197 public:
198 ScFormulaGroupIterator( ScDocument& rDoc );
200 sc::FormulaGroupEntry* first();
201 sc::FormulaGroupEntry* next();
205 * Walk through all cells in an area. For SubTotal and Aggregate depending on mnSubTotalFlags.
207 class ScCellIterator
209 typedef std::pair<sc::CellStoreType::const_iterator, size_t> PositionType;
211 ScDocument& mrDoc;
212 ScAddress maStartPos;
213 ScAddress maEndPos;
214 ScAddress maCurPos;
216 PositionType maCurColPos;
217 SubtotalFlags mnSubTotalFlags;
219 ScRefCellValue maCurCell;
221 void incBlock();
222 void incPos();
223 void setPos(size_t nPos);
225 const ScColumn* getColumn() const;
227 void init();
228 bool getCurrent();
230 public:
231 ScCellIterator( ScDocument& rDoc, const ScRange& rRange, SubtotalFlags nSubTotalFlags = SubtotalFlags::NONE );
233 const ScAddress& GetPos() const { return maCurPos; }
235 CellType getType() const { return maCurCell.meType;}
236 OUString getString() const;
237 const EditTextObject* getEditText() const { return maCurCell.mpEditText;}
238 ScFormulaCell* getFormulaCell() { return maCurCell.mpFormula;}
239 const ScFormulaCell* getFormulaCell() const { return maCurCell.mpFormula;}
240 ScCellValue getCellValue() const;
241 const ScRefCellValue& getRefCellValue() const { return maCurCell;}
243 bool hasString() const;
244 bool isEmpty() const;
245 bool equalsWithoutFormat( const ScAddress& rPos ) const;
247 bool first();
248 bool next();
251 class ScQueryCellIterator // walk through all non-empty cells in an area
253 enum StopOnMismatchBits
255 nStopOnMismatchDisabled = 0x00,
256 nStopOnMismatchEnabled = 0x01,
257 nStopOnMismatchOccurred = 0x02,
258 nStopOnMismatchExecuted = nStopOnMismatchEnabled | nStopOnMismatchOccurred
261 enum TestEqualConditionBits
263 nTestEqualConditionDisabled = 0x00,
264 nTestEqualConditionEnabled = 0x01,
265 nTestEqualConditionMatched = 0x02,
266 nTestEqualConditionFulfilled = nTestEqualConditionEnabled | nTestEqualConditionMatched
269 typedef sc::CellStoreType::const_position_type PositionType;
270 PositionType maCurPos;
272 ScQueryParam maParam;
273 ScDocument& rDoc;
274 const ScInterpreterContext& mrContext;
275 SCTAB nTab;
276 SCCOL nCol;
277 SCROW nRow;
278 sal_uInt8 nStopOnMismatch;
279 sal_uInt8 nTestEqualCondition;
280 bool bAdvanceQuery;
281 bool bIgnoreMismatchOnLeadingStrings;
283 /** Initialize position for new column. */
284 void InitPos();
285 void IncPos();
286 void IncBlock();
287 bool GetThis();
289 /* Only works if no regular expression is involved, only
290 searches for rows in one column, and only the first
291 query entry is considered with simple conditions
292 SC_LESS_EQUAL (sorted ascending) or SC_GREATER_EQUAL
293 (sorted descending). Check these things before
294 invocation! Delivers a starting point, continue with
295 GetThis() and GetNext() afterwards. Introduced for
296 FindEqualOrSortedLastInRange()
298 bool BinarySearch();
300 public:
301 ScQueryCellIterator(ScDocument& rDocument, const ScInterpreterContext& rContext, SCTAB nTable,
302 const ScQueryParam& aParam, bool bMod);
303 // when !bMod, the QueryParam has to be filled
304 // (bIsString)
305 bool GetFirst();
306 bool GetNext();
307 SCCOL GetCol() const { return nCol; }
308 SCROW GetRow() const { return nRow; }
310 // increments all Entry.nField, if column
311 // changes, for ScInterpreter ScHLookup()
312 void SetAdvanceQueryParamEntryField( bool bVal )
313 { bAdvanceQuery = bVal; }
314 void AdvanceQueryParamEntryField();
316 /** If set, iterator stops on first non-matching cell
317 content. May be used in SC_LESS_EQUAL queries where a
318 cell range is assumed to be sorted; stops on first
319 value being greater than the queried value and
320 GetFirst()/GetNext() return NULL. StoppedOnMismatch()
321 returns true then.
322 However, the iterator's conditions are not set to end
323 all queries, GetCol() and GetRow() return values for
324 the non-matching cell, further GetNext() calls may be
325 executed. */
326 void SetStopOnMismatch( bool bVal )
328 nStopOnMismatch = sal::static_int_cast<sal_uInt8>(bVal ? nStopOnMismatchEnabled :
329 nStopOnMismatchDisabled);
331 bool StoppedOnMismatch() const
332 { return nStopOnMismatch == nStopOnMismatchExecuted; }
334 /** If set, an additional test for SC_EQUAL condition is
335 executed in ScTable::ValidQuery() if SC_LESS_EQUAL or
336 SC_GREATER_EQUAL conditions are to be tested. May be
337 used where a cell range is assumed to be sorted to stop
338 if an equal match is found. */
339 void SetTestEqualCondition( bool bVal )
341 nTestEqualCondition = sal::static_int_cast<sal_uInt8>(bVal ?
342 nTestEqualConditionEnabled :
343 nTestEqualConditionDisabled);
345 bool IsEqualConditionFulfilled() const
346 { return nTestEqualCondition == nTestEqualConditionFulfilled; }
348 /** In a range assumed to be sorted find either the last of
349 a sequence of equal entries or the last being less than
350 (or greater than) the queried value. Used by the
351 interpreter for [HV]?LOOKUP() and MATCH(). Column and
352 row position of the found entry are returned, otherwise
353 invalid.
355 The search does not stop when encountering a string and does not
356 assume that no values follow anymore.
357 If querying for a string a mismatch on the first
358 entry, e.g. column header, is ignored.
360 @ATTENTION! StopOnMismatch, TestEqualCondition and
361 the internal IgnoreMismatchOnLeadingStrings and query
362 params are in an undefined state upon return! The
363 iterator is not usable anymore except for obtaining the
364 number format!
366 bool FindEqualOrSortedLastInRange( SCCOL& nFoundCol, SCROW& nFoundRow );
369 // Used by ScInterpreter::ScCountIf.
370 // Walk through all non-empty cells in an area.
371 class ScCountIfCellIterator
373 typedef sc::CellStoreType::const_position_type PositionType;
374 PositionType maCurPos;
375 ScQueryParam maParam;
376 ScDocument& rDoc;
377 const ScInterpreterContext& mrContext;
378 SCTAB nTab;
379 SCCOL nCol;
380 SCROW nRow;
382 /** Initialize position for new column. */
383 void InitPos();
384 void IncPos();
385 void IncBlock();
386 void AdvanceQueryParamEntryField();
388 public:
389 ScCountIfCellIterator(ScDocument& rDocument, const ScInterpreterContext& rContext, SCTAB nTable,
390 const ScQueryParam& aParam);
391 int GetCount();
394 class ScDocAttrIterator // all attribute areas
396 private:
397 ScDocument& rDoc;
398 SCTAB nTab;
399 SCCOL nEndCol;
400 SCROW nStartRow;
401 SCROW nEndRow;
402 SCCOL nCol;
403 std::unique_ptr<ScAttrIterator>
404 pColIter;
406 public:
407 ScDocAttrIterator(ScDocument& rDocument, SCTAB nTable,
408 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2);
409 ~ScDocAttrIterator();
411 const ScPatternAttr* GetNext( SCCOL& rCol, SCROW& rRow1, SCROW& rRow2 );
414 class ScAttrRectIterator // all attribute areas, including areas stretching
415 // across more than one column
417 private:
418 ScDocument& rDoc;
419 SCTAB nTab;
420 SCCOL nEndCol;
421 SCROW nStartRow;
422 SCROW nEndRow;
423 SCCOL nIterStartCol;
424 SCCOL nIterEndCol;
425 std::unique_ptr<ScAttrIterator>
426 pColIter;
428 public:
429 ScAttrRectIterator(ScDocument& rDocument, SCTAB nTable,
430 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2);
431 ~ScAttrRectIterator();
433 void DataChanged();
434 const ScPatternAttr* GetNext( SCCOL& rCol1, SCCOL& rCol2, SCROW& rRow1, SCROW& rRow2 );
437 class ScHorizontalCellIterator // walk through all non empty cells in an area
438 { // row by row
439 struct ColParam
441 sc::CellStoreType::const_iterator maPos;
442 sc::CellStoreType::const_iterator maEnd;
443 SCCOL mnCol;
446 std::vector<ColParam>::iterator maColPos;
447 std::vector<ColParam> maColPositions;
449 ScDocument& rDoc;
450 SCTAB mnTab;
451 SCCOL nStartCol;
452 SCCOL nEndCol;
453 SCROW nStartRow;
454 SCROW nEndRow;
455 SCCOL mnCol;
456 SCROW mnRow;
457 ScRefCellValue maCurCell;
458 bool mbMore;
460 public:
461 ScHorizontalCellIterator(ScDocument& rDocument, SCTAB nTable,
462 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2);
463 ~ScHorizontalCellIterator();
465 ScRefCellValue* GetNext( SCCOL& rCol, SCROW& rRow );
466 bool GetPos( SCCOL& rCol, SCROW& rRow );
467 /// Set a(nother) sheet and (re)init.
468 void SetTab( SCTAB nTab );
470 private:
471 void Advance();
472 void SkipInvalid();
473 bool SkipInvalidInRow();
474 SCROW FindNextNonEmptyRow();
477 /** Row-wise value iterator. */
478 class ScHorizontalValueIterator
480 private:
481 ScDocument& rDoc;
482 const ScAttrArray* pAttrArray;
483 std::unique_ptr<ScHorizontalCellIterator>
484 pCellIter;
485 sal_uInt32 nNumFormat; // for CalcAsShown
486 SCTAB nEndTab;
487 SCCOL nCurCol;
488 SCROW nCurRow;
489 SCTAB nCurTab;
490 SCROW nAttrEndRow;
491 bool bCalcAsShown;
493 public:
495 ScHorizontalValueIterator( ScDocument& rDocument,
496 const ScRange& rRange );
497 ~ScHorizontalValueIterator();
498 /// Does NOT reset rValue if no value found!
499 bool GetNext( double& rValue, FormulaError& rErr );
502 class ScHorizontalAttrIterator
504 private:
505 ScDocument& rDoc;
506 SCTAB nTab;
507 SCCOL nStartCol;
508 SCROW nStartRow;
509 SCCOL nEndCol;
510 SCROW nEndRow;
512 std::unique_ptr<SCROW[]> pNextEnd;
513 std::unique_ptr<SCCOL[]> pHorizEnd;
514 std::unique_ptr<SCSIZE[]> pIndices;
515 std::unique_ptr<const ScPatternAttr*[]>
516 ppPatterns;
517 SCCOL nCol;
518 SCROW nRow;
519 SCROW nMinNextEnd;
521 void InitForNextRow(bool bInitialization);
523 public:
524 ScHorizontalAttrIterator( ScDocument& rDocument, SCTAB nTable,
525 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2 );
526 ~ScHorizontalAttrIterator();
528 const ScPatternAttr* GetNext( SCCOL& rCol1, SCCOL& rCol2, SCROW& rRow );
531 // returns non-empty cells and areas with formatting (horizontal)
533 class SC_DLLPUBLIC ScUsedAreaIterator
535 private:
536 ScHorizontalCellIterator aCellIter;
537 ScHorizontalAttrIterator aAttrIter;
539 SCCOL nNextCol;
540 SCROW nNextRow;
542 SCCOL nCellCol;
543 SCROW nCellRow;
544 ScRefCellValue* pCell;
545 SCCOL nAttrCol1;
546 SCCOL nAttrCol2;
547 SCROW nAttrRow;
548 const ScPatternAttr* pPattern;
550 SCCOL nFoundStartCol; // results after GetNext
551 SCCOL nFoundEndCol;
552 SCROW nFoundRow;
553 const ScPatternAttr* pFoundPattern;
555 ScRefCellValue maFoundCell;
557 public:
558 ScUsedAreaIterator( ScDocument& rDocument, SCTAB nTable,
559 SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2 );
560 ~ScUsedAreaIterator();
562 bool GetNext();
564 SCCOL GetStartCol() const { return nFoundStartCol; }
565 SCCOL GetEndCol() const { return nFoundEndCol; }
566 SCROW GetRow() const { return nFoundRow; }
567 const ScPatternAttr* GetPattern() const { return pFoundPattern; }
568 const ScRefCellValue& GetCell() const { return maFoundCell;}
571 class ScRowBreakIterator
573 public:
574 static constexpr SCROW NOT_FOUND = -1;
576 explicit ScRowBreakIterator(::std::set<SCROW>& rBreaks);
577 SCROW first();
578 SCROW next();
580 private:
581 ::std::set<SCROW>& mrBreaks;
582 ::std::set<SCROW>::const_iterator maItr;
583 ::std::set<SCROW>::const_iterator maEnd;
586 class ScDocRowHeightUpdater
588 public:
589 struct TabRanges
591 SCTAB mnTab;
592 ScFlatBoolRowSegments maRanges;
594 TabRanges(SCTAB nTab, SCROW nMaxRow);
598 * Passing a NULL pointer to pTabRangesArray forces the heights of all
599 * rows in all tables to be updated.
601 explicit ScDocRowHeightUpdater(
602 ScDocument& rDoc, OutputDevice* pOutDev, double fPPTX, double fPPTY,
603 const ::std::vector<TabRanges>* pTabRangesArray);
605 void update();
607 private:
608 void updateAll();
610 private:
611 ScDocument& mrDoc;
612 VclPtr<OutputDevice> mpOutDev;
613 double mfPPTX;
614 double mfPPTY;
615 const ::std::vector<TabRanges>* mpTabRangesArray;
618 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */