Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "cellform.hxx"
21 :
22 : #include <sfx2/objsh.hxx>
23 : #include <svl/smplhint.hxx>
24 : #include <svl/zforlist.hxx>
25 : #include "svl/sharedstring.hxx"
26 :
27 : #include "formulacell.hxx"
28 : #include "document.hxx"
29 : #include "cellvalue.hxx"
30 : #include "formula/errorcodes.hxx"
31 : #include "sc.hrc"
32 :
33 : // STATIC DATA
34 : // Err527 Workaround
35 : const ScFormulaCell* pLastFormulaTreeTop = 0;
36 :
37 0 : 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 )
41 : {
42 0 : *ppColor = NULL;
43 0 : if (&rFormatter==NULL)
44 : {
45 0 : rString = EMPTY_OUSTRING;
46 0 : return;
47 : }
48 :
49 0 : switch (rCell.meType)
50 : {
51 : case CELLTYPE_STRING:
52 0 : rFormatter.GetOutputString(rCell.mpString->getString(), nFormat, rString, ppColor, bUseStarFormat);
53 0 : break;
54 : case CELLTYPE_EDIT:
55 0 : rFormatter.GetOutputString(rCell.getString(pDoc), nFormat, rString, ppColor );
56 0 : break;
57 : case CELLTYPE_VALUE:
58 : {
59 0 : double nValue = rCell.mfValue;
60 0 : if (!bNullVals && nValue == 0.0)
61 0 : rString = "";
62 : else
63 : {
64 0 : if( eForceTextFmt == ftCheck )
65 : {
66 0 : if( nFormat && rFormatter.IsTextFormat( nFormat ) )
67 0 : eForceTextFmt = ftForce;
68 : }
69 0 : if( eForceTextFmt == ftForce )
70 : {
71 0 : OUString aTemp;
72 0 : rFormatter.GetOutputString( nValue, 0, aTemp, ppColor );
73 0 : rFormatter.GetOutputString( aTemp, nFormat, rString, ppColor );
74 : }
75 : else
76 0 : rFormatter.GetOutputString( nValue, nFormat, rString, ppColor, bUseStarFormat );
77 : }
78 : }
79 0 : break;
80 : case CELLTYPE_FORMULA:
81 : {
82 0 : ScFormulaCell* pFCell = rCell.mpFormula;
83 0 : if ( bFormula )
84 : {
85 0 : pFCell->GetFormula( rString );
86 : }
87 : else
88 : {
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 0 : if ( pFCell->GetDocument()->IsInInterpreter() &&
96 0 : (!pFCell->GetDocument()->GetMacroInterpretLevel()
97 0 : || pFCell->IsRunning()) )
98 : {
99 0 : rString = "...";
100 : }
101 : else
102 : {
103 0 : sal_uInt16 nErrCode = pFCell->GetErrCode();
104 :
105 0 : if (nErrCode != 0)
106 0 : rString = ScGlobal::GetErrorString(nErrCode);
107 0 : else if ( pFCell->IsEmptyDisplayedAsString() )
108 0 : rString = "";
109 0 : else if ( pFCell->IsValue() )
110 : {
111 0 : double fValue = pFCell->GetValue();
112 0 : if ( !bNullVals && fValue == 0.0 )
113 0 : rString = "";
114 0 : else if ( pFCell->IsHybridValueCell() )
115 0 : rString = pFCell->GetString().getString();
116 : else
117 0 : rFormatter.GetOutputString( fValue, nFormat, rString, ppColor, bUseStarFormat );
118 : }
119 : else
120 : {
121 : rFormatter.GetOutputString( pFCell->GetString().getString(),
122 0 : nFormat, rString, ppColor, bUseStarFormat );
123 : }
124 : }
125 : }
126 : }
127 0 : break;
128 : default:
129 0 : rString = "";
130 0 : break;
131 : }
132 : }
133 :
134 0 : 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 )
138 : {
139 0 : OUString aString;
140 0 : *ppColor = NULL;
141 :
142 0 : CellType eType = rDoc.GetCellType(rPos);
143 0 : switch (eType)
144 : {
145 : case CELLTYPE_STRING:
146 : {
147 0 : ScRefCellValue aCell;
148 0 : aCell.assign(rDoc, rPos);
149 0 : rFormatter.GetOutputString(aCell.mpString->getString(), nFormat, aString, ppColor, bUseStarFormat);
150 : }
151 0 : break;
152 : case CELLTYPE_EDIT:
153 : {
154 0 : ScRefCellValue aCell;
155 0 : aCell.assign(rDoc, rPos);
156 0 : rFormatter.GetOutputString(aCell.getString(&rDoc), nFormat, aString, ppColor);
157 : }
158 0 : break;
159 : case CELLTYPE_VALUE:
160 : {
161 0 : double nValue = rDoc.GetValue(rPos);
162 0 : if (!bNullVals && nValue == 0.0) aString = "";
163 : else
164 : {
165 0 : if (eForceTextFmt == ftCheck)
166 : {
167 0 : if (nFormat && rFormatter.IsTextFormat(nFormat)) eForceTextFmt = ftForce;
168 : }
169 0 : if (eForceTextFmt == ftForce)
170 : {
171 0 : OUString aTemp;
172 0 : rFormatter.GetOutputString(nValue, 0, aTemp, ppColor);
173 0 : rFormatter.GetOutputString(aTemp, nFormat, aString, ppColor);
174 : }
175 0 : else rFormatter.GetOutputString(nValue, nFormat, aString, ppColor, bUseStarFormat);
176 : }
177 : }
178 0 : break;
179 : case CELLTYPE_FORMULA:
180 : {
181 0 : ScFormulaCell* pFCell = rDoc.GetFormulaCell(rPos);
182 0 : if (!pFCell)
183 0 : return aString;
184 0 : if (bFormula)
185 : {
186 0 : pFCell->GetFormula(aString);
187 : }
188 : else
189 : {
190 : // A macro started from the interpreter, which has
191 : // access to Formular Cells, becomes a CellText, even if
192 : // that triggers further interpretation, except if those
193 : // cells are already being interpreted.
194 : // IdleCalc generally doesn't trigger futher interpretation,
195 : // as not to get Err522 (circular).
196 0 : if (pFCell->GetDocument()->IsInInterpreter() &&
197 0 : (!pFCell->GetDocument()->GetMacroInterpretLevel()
198 0 : || pFCell->IsRunning()))
199 : {
200 0 : aString = "...";
201 : }
202 : else
203 : {
204 0 : sal_uInt16 nErrCode = pFCell->GetErrCode();
205 :
206 0 : if (nErrCode != 0) aString = ScGlobal::GetErrorString(nErrCode);
207 0 : else if (pFCell->IsEmptyDisplayedAsString()) aString = "";
208 0 : else if (pFCell->IsValue())
209 : {
210 0 : double fValue = pFCell->GetValue();
211 0 : if (!bNullVals && fValue == 0.0) aString = "";
212 0 : else if (pFCell->IsHybridValueCell()) aString = pFCell->GetString().getString();
213 0 : else rFormatter.GetOutputString(fValue, nFormat, aString, ppColor, bUseStarFormat);
214 : }
215 : else
216 : {
217 : rFormatter.GetOutputString(pFCell->GetString().getString(),
218 0 : nFormat, aString, ppColor, bUseStarFormat);
219 : }
220 : }
221 : }
222 : }
223 0 : break;
224 : default:
225 : ;
226 : }
227 0 : return aString;
228 : }
229 :
230 0 : void ScCellFormat::GetInputString(
231 : ScRefCellValue& rCell, sal_uLong nFormat, OUString& rString, SvNumberFormatter& rFormatter, const ScDocument* pDoc )
232 : {
233 0 : if (&rFormatter == NULL)
234 : {
235 0 : rString = EMPTY_OUSTRING;
236 0 : return;
237 : }
238 :
239 0 : OUString aString = rString;
240 0 : switch (rCell.meType)
241 : {
242 : case CELLTYPE_STRING:
243 : case CELLTYPE_EDIT:
244 0 : aString = rCell.getString(pDoc);
245 0 : break;
246 : case CELLTYPE_VALUE:
247 0 : rFormatter.GetInputLineString(rCell.mfValue, nFormat, aString );
248 0 : break;
249 : case CELLTYPE_FORMULA:
250 : {
251 0 : ScFormulaCell* pFC = rCell.mpFormula;
252 0 : if (pFC->IsEmptyDisplayedAsString())
253 0 : aString = EMPTY_OUSTRING;
254 0 : else if (pFC->IsValue())
255 0 : rFormatter.GetInputLineString(pFC->GetValue(), nFormat, aString);
256 : else
257 0 : aString = pFC->GetString().getString();
258 :
259 0 : sal_uInt16 nErrCode = pFC->GetErrCode();
260 0 : if (nErrCode != 0)
261 0 : aString = EMPTY_OUSTRING;
262 : }
263 0 : break;
264 : default:
265 0 : aString = EMPTY_OUSTRING;
266 0 : break;
267 : }
268 0 : rString = aString;
269 : }
270 :
271 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|