xref: /AOO41X/main/sc/source/core/tool/cellform.cxx (revision f04bd1c41dbb33d4d3f057e7474795ed0a0da601)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 // INCLUDE ---------------------------------------------------------------
27 
28 #include <sfx2/objsh.hxx>
29 #include <svl/smplhint.hxx>
30 #include <svl/zforlist.hxx>
31 
32 #include "cellform.hxx"
33 #include "cell.hxx"
34 #include "document.hxx"
35 #include "formula/errorcodes.hxx"
36 #include "sc.hrc"
37 
38 // STATIC DATA -----------------------------------------------------------
39 
40 // Err527 Workaround
41 const ScFormulaCell* pLastFormulaTreeTop = 0;
42 
43 // -----------------------------------------------------------------------
44 
45 void ScCellFormat::GetString( ScBaseCell* pCell, sal_uLong nFormat, String& rString,
46 							  Color** ppColor, SvNumberFormatter& rFormatter,
47 							  sal_Bool bNullVals,
48 							  sal_Bool bFormula,
49 							  ScForceTextFmt eForceTextFmt )
50 {
51 	*ppColor = NULL;
52 	if (&rFormatter==NULL)
53 	{
54 		rString.Erase();
55 		return;
56 	}
57 
58 	CellType eType = pCell->GetCellType();
59 	switch(eType)
60 	{
61 		case CELLTYPE_STRING:
62 			{
63 				String aCellString;
64 				((ScStringCell*)pCell)->GetString( aCellString );
65 				rFormatter.GetOutputString( aCellString, nFormat, rString, ppColor );
66 			}
67 			break;
68 		case CELLTYPE_EDIT:
69 			{
70 				String aCellString;
71 				((ScEditCell*)pCell)->GetString( aCellString );
72 				rFormatter.GetOutputString( aCellString, nFormat, rString, ppColor );
73 			}
74 			break;
75 		case CELLTYPE_VALUE:
76 			{
77 				double nValue = ((ScValueCell*)pCell)->GetValue();
78 				if ( !bNullVals && nValue == 0.0 )
79 					rString.Erase();
80 				else
81 				{
82 					if( eForceTextFmt == ftCheck )
83 					{
84 						if( nFormat && rFormatter.IsTextFormat( nFormat ) )
85 							eForceTextFmt = ftForce;
86 					}
87 					if( eForceTextFmt == ftForce )
88 					{
89 						String aTemp;
90 						rFormatter.GetOutputString( nValue, 0, aTemp, ppColor );
91 						rFormatter.GetOutputString( aTemp, nFormat, rString, ppColor );
92 					}
93 					else
94 						rFormatter.GetOutputString( nValue, nFormat, rString, ppColor );
95 				}
96 			}
97 			break;
98 		case CELLTYPE_FORMULA:
99 			{
100 				ScFormulaCell*	pFCell = (ScFormulaCell*)pCell;
101 				if ( bFormula )
102 					pFCell->GetFormula( rString );
103 				else
104 				{
105 					// #62160# Ein via Interpreter gestartetes Makro, das hart
106 					// auf Formelzellen zugreift, bekommt einen CellText, auch
107 					// wenn dadurch ein weiterer Interpreter gestartet wird,
108 					// aber nicht wenn diese Zelle gerade interpretiert wird.
109 					// IdleCalc startet generell keine weiteren Interpreter,
110 					// um keine Err522 (zirkulaer) zu bekommen.
111 					if ( pFCell->GetDocument()->IsInInterpreter() &&
112 							(!pFCell->GetDocument()->GetMacroInterpretLevel()
113 							|| pFCell->IsRunning()) )
114 					{
115 						rString.AssignAscii( RTL_CONSTASCII_STRINGPARAM("...") );
116 					}
117 					else
118 					{
119 						sal_uInt16 nErrCode = pFCell->GetErrCode();
120 
121 						// erst nach dem Interpretieren (GetErrCode) das Zahlformat holen:
122 						if ( (nFormat % SV_COUNTRY_LANGUAGE_OFFSET) == 0 )
123 							nFormat = pFCell->GetStandardFormat( rFormatter,
124 								nFormat );
125 
126 						if (nErrCode != 0)
127 							rString = ScGlobal::GetErrorString(nErrCode);
128 						else if ( pFCell->IsEmptyDisplayedAsString() )
129                             rString.Erase();
130 						else if ( pFCell->IsValue() )
131 						{
132 							double fValue = pFCell->GetValue();
133 							if ( !bNullVals && fValue == 0.0 )
134 								rString.Erase();
135 							else
136 								rFormatter.GetOutputString( fValue, nFormat, rString, ppColor );
137 						}
138 						else
139 						{
140 							String aCellString;
141 							pFCell->GetString( aCellString );
142 							rFormatter.GetOutputString( aCellString, nFormat, rString, ppColor );
143 						}
144 					}
145 				}
146 			}
147 			break;
148 		default:
149 			rString.Erase();
150 			break;
151 	}
152 }
153 
154 void ScCellFormat::GetInputString( ScBaseCell* pCell, sal_uLong nFormat, String& rString,
155 									  SvNumberFormatter& rFormatter )
156 {
157 	if (&rFormatter==NULL)
158 	{
159 		rString.Erase();
160 		return;
161 	}
162 
163 	CellType eType = pCell->GetCellType();
164 	switch(eType)
165 	{
166 		case CELLTYPE_STRING:
167 			{
168 				((ScStringCell*)pCell)->GetString( rString );
169 			}
170 			break;
171 		case CELLTYPE_EDIT:
172 			{
173 				((ScEditCell*)pCell)->GetString( rString );
174 			}
175 			break;
176 		case CELLTYPE_VALUE:
177 			{
178 				double nValue = ((ScValueCell*)pCell)->GetValue();
179 				rFormatter.GetInputLineString( nValue, nFormat, rString );
180 			}
181 			break;
182 		case CELLTYPE_FORMULA:
183 			{
184 				if (((ScFormulaCell*)pCell)->IsEmptyDisplayedAsString())
185                 {
186                     rString.Erase();
187                 }
188                 else if (((ScFormulaCell*)pCell)->IsValue())
189 				{
190 					double nValue = ((ScFormulaCell*)pCell)->GetValue();
191 					rFormatter.GetInputLineString( nValue, nFormat, rString );
192 				}
193 				else
194 				{
195 					((ScFormulaCell*)pCell)->GetString( rString );
196 				}
197 
198 				sal_uInt16 nErrCode = ((ScFormulaCell*)pCell)->GetErrCode();
199 				if (nErrCode != 0)
200 				{
201 					rString.Erase();
202 				}
203 			}
204 			break;
205 		default:
206 			rString.Erase();
207 			break;
208 	}
209 }
210 
211 
212 
213