Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sc / inc / sharedformula.hxx
blobf4c8ab01853284d8021a7a3157dfd3f2acba2165
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/.
8 */
10 #ifndef INCLUDED_SC_INC_SHAREDFORMULA_HXX
11 #define INCLUDED_SC_INC_SHAREDFORMULA_HXX
13 #include "formulacell.hxx"
14 #include "mtvelements.hxx"
16 #include <vector>
18 #define USE_FORMULA_GROUP_LISTENER 1
20 namespace sc {
22 class StartListeningContext;
24 class SharedFormulaUtil
26 public:
28 /**
29 * Group formula cells stored in the passed container. The formula cells
30 * in the container are assumed to be all <b>non-shared</b>.
32 template<typename Iter>
33 static void groupFormulaCells(const Iter& itBeg, const Iter& itEnd)
35 Iter it = itBeg;
36 ScFormulaCell* pPrev = *it;
37 ScFormulaCell* pCur = nullptr;
38 for (++it; it != itEnd; ++it, pPrev = pCur)
40 pCur = *it;
41 ScFormulaCell::CompareState eState = pCur->CompareByTokenArray(*pPrev);
42 if (eState == ScFormulaCell::NotEqual)
43 continue;
45 ScFormulaCellGroupRef xGroup = pPrev->GetCellGroup();
46 if (xGroup)
48 // Extend the group.
49 ++xGroup->mnLength;
50 pCur->SetCellGroup(xGroup);
51 continue;
54 // Create a new group.
55 xGroup = pPrev->CreateCellGroup(2, eState == ScFormulaCell::EqualInvariant);
56 pCur->SetCellGroup(xGroup);
60 /**
61 * Split existing shared formula range at specified position. The cell at
62 * specified position becomes the top cell of the lower shared formula
63 * range after this call. This method does nothing if the cell at
64 * specified position is not a formula cell.
66 * @param aPos position of cell to examine.
67 * @param pCxt context to be used, if any, may be nullptr.
69 static void splitFormulaCellGroup(const CellStoreType::position_type& aPos, sc::EndListeningContext* pCxt);
71 /**
72 * Split existing shared formula ranges at specified row positions.
74 * @param rCells cell storage container
75 * @param rBounds row positions at which to split existing shared formula
76 * ranges. Note that this method will directly modify this
77 * parameter to sort and remove duplicates.
79 static void splitFormulaCellGroups(CellStoreType& rCells, std::vector<SCROW>& rBounds);
81 /**
82 * See if two specified adjacent formula cells can be merged, and if they
83 * can, merge them into the same group.
85 * @param rPos position object of the first cell
86 * @param rCell1 first cell
87 * @param rCell2 second cell located immediately below the first cell.
89 * @return true if the cells are merged, false otherwise. If the two
90 * cells already belong to the same group, it returns false.
92 static bool joinFormulaCells(
93 const CellStoreType::position_type& rPos, ScFormulaCell& rCell1, ScFormulaCell& rCell2 );
94 /**
95 * Merge with an existing formula group (if any) located immediately above
96 * if the cell at specified position is a formula cell, and its formula
97 * tokens are identical to that of the above formula group.
99 * @param aPos position of cell to examine.
101 * @return true if the cells are merged, false otherwise. If the two
102 * cells already belong to the same group, it returns false.
104 static bool joinFormulaCellAbove( const CellStoreType::position_type& aPos );
107 * Turn a shared formula cell into a non-shared one, and split it off from
108 * the adjacent formula cell groups.
110 * @param aPos position of cell to examine
111 * @param rCell formula cell instance
113 static void unshareFormulaCell(const CellStoreType::position_type& aPos, ScFormulaCell& rCell);
116 * Make specified formula cells non-shared ones, and split them off from
117 * their respective adjacent formula cell groups.
119 * @param rCells cell storage container
120 * @param rRows row positions at which to unshare formula cells.
122 static void unshareFormulaCells(CellStoreType& rCells, std::vector<SCROW>& rRows);
125 * Have all formula cells belonging to a group start listening to their
126 * references.
128 * @param rCxt context object.
129 * @param ppSharedTop memory position of the pointer of the topmost
130 * formula cell instance in the cell storage. The
131 * caller is responsible for ensuring that it is indeed
132 * the topmost cell of a shared formula group.
134 static void startListeningAsGroup( StartListeningContext& rCxt, ScFormulaCell** ppSharedTop );
139 #endif
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */