Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / tool / cellform.cxx
blob6a23ba238f058d7d802f3859768fcc6fd50bdecd
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 #include "cellform.hxx"
22 #include <sfx2/objsh.hxx>
23 #include <svl/smplhint.hxx>
24 #include <svl/zforlist.hxx>
25 #include "svl/sharedstring.hxx"
27 #include "formulacell.hxx"
28 #include "document.hxx"
29 #include "cellvalue.hxx"
30 #include "formula/errorcodes.hxx"
31 #include "sc.hrc"
33 // STATIC DATA
34 // Err527 Workaround
35 const ScFormulaCell* pLastFormulaTreeTop = 0;
37 void ScCellFormat::GetString( ScRefCellValue& rCell, sal_uLong nFormat, OUString& rString,
38 Color** ppColor, SvNumberFormatter& rFormatter, const ScDocument* pDoc,
39 bool bNullVals, bool bFormula, ScForceTextFmt eForceTextFmt,
40 bool bUseStarFormat )
42 *ppColor = NULL;
43 if (&rFormatter==NULL)
45 rString = EMPTY_OUSTRING;
46 return;
49 switch (rCell.meType)
51 case CELLTYPE_STRING:
52 rFormatter.GetOutputString(rCell.mpString->getString(), nFormat, rString, ppColor, bUseStarFormat);
53 break;
54 case CELLTYPE_EDIT:
55 rFormatter.GetOutputString(rCell.getString(pDoc), nFormat, rString, ppColor );
56 break;
57 case CELLTYPE_VALUE:
59 double nValue = rCell.mfValue;
60 if (!bNullVals && nValue == 0.0)
61 rString = "";
62 else
64 if( eForceTextFmt == ftCheck )
66 if( nFormat && rFormatter.IsTextFormat( nFormat ) )
67 eForceTextFmt = ftForce;
69 if( eForceTextFmt == ftForce )
71 OUString aTemp;
72 rFormatter.GetOutputString( nValue, 0, aTemp, ppColor );
73 rFormatter.GetOutputString( aTemp, nFormat, rString, ppColor );
75 else
76 rFormatter.GetOutputString( nValue, nFormat, rString, ppColor, bUseStarFormat );
79 break;
80 case CELLTYPE_FORMULA:
82 ScFormulaCell* pFCell = rCell.mpFormula;
83 if ( bFormula )
85 pFCell->GetFormula( rString );
87 else
89 // A macro started from the interpreter, which has
90 // access to Formular Cells, becomes a CellText, even if
91 // that triggers further interpretation, except if those
92 // cells are already being interpreted.
93 // IdleCalc generally doesn't trigger futher interpretation,
94 // as not to get Err522 (circular).
95 if ( pFCell->GetDocument()->IsInInterpreter() &&
96 (!pFCell->GetDocument()->GetMacroInterpretLevel()
97 || pFCell->IsRunning()) )
99 rString = "...";
101 else
103 sal_uInt16 nErrCode = pFCell->GetErrCode();
105 if (nErrCode != 0)
106 rString = ScGlobal::GetErrorString(nErrCode);
107 else if ( pFCell->IsEmptyDisplayedAsString() )
108 rString = "";
109 else if ( pFCell->IsValue() )
111 double fValue = pFCell->GetValue();
112 if ( !bNullVals && fValue == 0.0 )
113 rString = "";
114 else if ( pFCell->IsHybridValueCell() )
115 rString = pFCell->GetString().getString();
116 else
117 rFormatter.GetOutputString( fValue, nFormat, rString, ppColor, bUseStarFormat );
119 else
121 OUString aCellString = pFCell->GetString().getString();
122 rFormatter.GetOutputString( aCellString, nFormat, rString, ppColor, bUseStarFormat );
127 break;
128 default:
129 rString = "";
130 break;
134 OUString ScCellFormat::GetString(
135 ScDocument& rDoc, const ScAddress& rPos, sal_uLong nFormat, Color** ppColor,
136 SvNumberFormatter& rFormatter, bool bNullVals, bool bFormula, ScForceTextFmt eForceTextFmt,
137 bool bUseStarFormat )
139 OUString aString;
140 *ppColor = NULL;
142 CellType eType = rDoc.GetCellType(rPos);
143 switch (eType)
145 case CELLTYPE_STRING:
147 ScRefCellValue aCell;
148 aCell.assign(rDoc, rPos);
149 rFormatter.GetOutputString(aCell.mpString->getString(), nFormat, aString, ppColor, bUseStarFormat);
151 break;
152 case CELLTYPE_EDIT:
154 ScRefCellValue aCell;
155 aCell.assign(rDoc, rPos);
156 rFormatter.GetOutputString(aCell.getString(&rDoc), nFormat, aString, ppColor);
158 break;
159 case CELLTYPE_VALUE:
161 double nValue = rDoc.GetValue(rPos);
162 if (!bNullVals && nValue == 0.0) aString = "";
163 else
165 if (eForceTextFmt == ftCheck)
167 if (nFormat && rFormatter.IsTextFormat(nFormat)) eForceTextFmt = ftForce;
169 if (eForceTextFmt == ftForce)
171 OUString aTemp;
172 rFormatter.GetOutputString(nValue, 0, aTemp, ppColor);
173 rFormatter.GetOutputString(aTemp, nFormat, aString, ppColor);
175 else rFormatter.GetOutputString(nValue, nFormat, aString, ppColor, bUseStarFormat);
178 break;
179 case CELLTYPE_FORMULA:
181 ScFormulaCell* pFCell = rDoc.GetFormulaCell(rPos);
182 if (bFormula)
184 pFCell->GetFormula(aString);
186 else
188 // A macro started from the interpreter, which has
189 // access to Formular Cells, becomes a CellText, even if
190 // that triggers further interpretation, except if those
191 // cells are already being interpreted.
192 // IdleCalc generally doesn't trigger futher interpretation,
193 // as not to get Err522 (circular).
194 if (pFCell->GetDocument()->IsInInterpreter() &&
195 (!pFCell->GetDocument()->GetMacroInterpretLevel()
196 || pFCell->IsRunning()))
198 aString = "...";
200 else
202 sal_uInt16 nErrCode = pFCell->GetErrCode();
204 if (nErrCode != 0) aString = ScGlobal::GetErrorString(nErrCode);
205 else if (pFCell->IsEmptyDisplayedAsString()) aString = "";
206 else if (pFCell->IsValue())
208 double fValue = pFCell->GetValue();
209 if (!bNullVals && fValue == 0.0) aString = "";
210 else if (pFCell->IsHybridValueCell()) aString = pFCell->GetString().getString();
211 else rFormatter.GetOutputString(fValue, nFormat, aString, ppColor, bUseStarFormat);
213 else
215 OUString aCellString = pFCell->GetString().getString();
216 rFormatter.GetOutputString(aCellString, nFormat, aString, ppColor, bUseStarFormat);
221 break;
222 default:
225 return aString;
228 void ScCellFormat::GetInputString(
229 ScRefCellValue& rCell, sal_uLong nFormat, OUString& rString, SvNumberFormatter& rFormatter, const ScDocument* pDoc )
231 if (&rFormatter == NULL)
233 rString = EMPTY_OUSTRING;
234 return;
237 OUString aString = rString;
238 switch (rCell.meType)
240 case CELLTYPE_STRING:
241 case CELLTYPE_EDIT:
242 aString = rCell.getString(pDoc);
243 break;
244 case CELLTYPE_VALUE:
245 rFormatter.GetInputLineString(rCell.mfValue, nFormat, aString );
246 break;
247 case CELLTYPE_FORMULA:
249 ScFormulaCell* pFC = rCell.mpFormula;
250 if (pFC->IsEmptyDisplayedAsString())
251 aString = EMPTY_OUSTRING;
252 else if (pFC->IsValue())
253 rFormatter.GetInputLineString(pFC->GetValue(), nFormat, aString);
254 else
255 aString = pFC->GetString().getString();
257 sal_uInt16 nErrCode = pFC->GetErrCode();
258 if (nErrCode != 0)
259 aString = EMPTY_OUSTRING;
261 break;
262 default:
263 aString = EMPTY_OUSTRING;
264 break;
266 rString = aString;
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */