Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sc / source / filter / lotus / tool.cxx
blob8e867fd8e46942c6993a9db3024c07b9db920d9d
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 "scitems.hxx"
21 #include <svx/algitem.hxx>
22 #include <editeng/justifyitem.hxx>
23 #include <svl/zforlist.hxx>
25 #include "rangenam.hxx"
26 #include "compiler.hxx"
28 #include "tool.h"
29 #include "decl.h"
30 #include "root.hxx"
31 #include "lotrange.hxx"
32 #include "namebuff.hxx"
33 #include "ftools.hxx"
34 #include "stringutil.hxx"
35 #include "tokenarray.hxx"
36 #include "lotfilter.hxx"
38 #include <math.h>
40 void PutFormString(LotusContext& rContext, SCCOL nCol, SCROW nRow, SCTAB nTab, sal_Char* pString)
42 // evaluate Label-Format
43 OSL_ENSURE( pString != nullptr, "PutFormString(): pString == NULL" );
44 if (!pString)
45 return;
47 sal_Char cForm;
48 SvxHorJustifyItem* pJustify = nullptr;
50 cForm = *pString;
52 switch( cForm )
54 case '"': // align-right
55 pJustify = rContext.pAttrRight;
56 pString++;
57 break;
58 case '\'': // align-left
59 pJustify = rContext.pAttrLeft;
60 pString++;
61 break;
62 case '^': // centered
63 pJustify = rContext.pAttrCenter;
64 pString++;
65 break;
66 case '|': // printer command
67 pString = nullptr;
68 break;
69 case '\\': // repetition
70 pJustify = rContext.pAttrRepeat;
71 pString++;
72 break;
73 default: // undefined case!
74 pJustify = rContext.pAttrStandard;
77 if (!pString)
78 return;
80 nCol = SanitizeCol(nCol);
81 nRow = SanitizeRow(nRow);
82 nTab = SanitizeTab(nTab);
84 rContext.pDoc->ApplyAttr( nCol, nRow, nTab, *pJustify );
85 ScSetStringParam aParam;
86 aParam.setTextInput();
87 rContext.pDoc->SetString(ScAddress(nCol,nRow,nTab), OUString(pString, strlen(pString), rContext.pLotusRoot->eCharsetQ), &aParam);
90 void SetFormat(LotusContext& rContext, SCCOL nCol, SCROW nRow, SCTAB nTab, sal_uInt8 nFormat, sal_uInt8 nSt)
92 nCol = SanitizeCol(nCol);
93 nRow = SanitizeRow(nRow);
94 nTab = SanitizeTab(nTab);
96 // PREC: nSt = default number of decimal places
97 rContext.pDoc->ApplyAttr(nCol, nRow, nTab, *(rContext.pValueFormCache->GetAttr(nFormat, nSt)));
99 ScProtectionAttr aAttr;
101 aAttr.SetProtection( nFormat & 0x80 );
103 rContext.pDoc->ApplyAttr( nCol, nRow, nTab, aAttr );
106 double SnumToDouble( sal_Int16 nVal )
108 const double pFacts[ 8 ] = {
109 5000.0,
110 500.0,
111 0.05,
112 0.005,
113 0.0005,
114 0.00005,
115 0.0625,
116 0.015625 };
118 double fVal;
120 if( nVal & 0x0001 )
122 fVal = pFacts[ ( nVal >> 1 ) & 0x0007 ];
123 fVal *= ( sal_Int16 ) ( nVal >> 4 );
125 else
126 fVal = ( sal_Int16 ) ( nVal >> 1 );
128 return fVal;
131 double Snum32ToDouble( sal_uInt32 nValue )
133 double fValue, temp;
135 fValue = nValue >> 6;
136 temp = nValue & 0x0f;
137 if (temp)
139 if (nValue & 0x00000010)
140 fValue /= pow((double)10, temp);
141 else
142 fValue *= pow((double)10, temp);
145 if ((nValue & 0x00000020))
146 fValue = -fValue;
147 return fValue;
150 FormCache::FormCache( ScDocument* pDoc1, sal_uInt8 nNewDefaultFormat )
151 : nIndex(0)
152 { // Default format is 'Default'
153 nDefaultFormat = nNewDefaultFormat;
154 pFormTable = pDoc1->GetFormatTable();
155 for(bool & rb : bValid)
156 rb = false;
157 eLanguage = ScGlobal::eLnge;
160 FormCache::~FormCache()
162 for(FormIdent & rIdent : aIdents)
163 delete rIdent.GetAttr();
166 SfxUInt32Item* FormCache::NewAttr( sal_uInt8 nFormat, sal_uInt8 nSt )
168 // setup new Format
169 sal_uInt8 nL, nH; // Low-/High-Nibble
170 OUString aFormString;
171 sal_Int16 eType = css::util::NumberFormat::ALL;
172 sal_uInt32 nIndex1;
173 sal_uInt32 nHandle;
174 NfIndexTableOffset eIndexTableOffset = NF_NUMERIC_START;
175 bool bDefault = false;
177 // split into Low and High byte
178 nL = nFormat & 0x0F;
179 nH = ( nFormat & 0xF0 ) / 16;
181 nH &= 0x07; // extract bits 4-6
182 switch( nH )
184 case 0x00: // fixed-point number
185 //fStandard;nL;
186 nIndex1 = pFormTable->GetStandardFormat(
187 css::util::NumberFormat::NUMBER, eLanguage );
188 aFormString = pFormTable->GenerateFormat(nIndex1,
189 eLanguage, false, false, nL);
190 break;
191 case 0x01: // scientific notation
192 //fExponent;nL;
193 nIndex1 = pFormTable->GetStandardFormat(
194 css::util::NumberFormat::SCIENTIFIC, eLanguage );
195 aFormString = pFormTable->GenerateFormat(nIndex1,
196 eLanguage, false, false, nL);
197 break;
198 case 0x02: // currency
199 //fMoney;nL;
200 nIndex1 = pFormTable->GetStandardFormat(
201 css::util::NumberFormat::CURRENCY, eLanguage );
202 aFormString = pFormTable->GenerateFormat(nIndex1,
203 eLanguage, false, false, nL);
204 break;
205 case 0x03: // percentage
206 //fPercent;nL;
207 nIndex1 = pFormTable->GetStandardFormat(
208 css::util::NumberFormat::PERCENT, eLanguage );
209 aFormString = pFormTable->GenerateFormat(nIndex1,
210 eLanguage, false, false, nL);
211 break;
212 case 0x04: // Decimal
213 //fStandard;nL;
214 nIndex1 = pFormTable->GetStandardFormat(
215 css::util::NumberFormat::NUMBER, eLanguage );
216 aFormString = pFormTable->GenerateFormat(nIndex1,
217 eLanguage, true, false, nL);
218 break;
219 case 0x05: // unspecified
220 //fStandard;nL;
221 nIndex1 = pFormTable->GetStandardFormat(
222 css::util::NumberFormat::NUMBER, eLanguage );
223 aFormString = pFormTable->GenerateFormat(nIndex1,
224 eLanguage, false, false, nL);
225 break;
226 case 0x06: // unspecified
227 //fStandard;nL;
228 nIndex1 = pFormTable->GetStandardFormat(
229 css::util::NumberFormat::NUMBER, eLanguage );
230 aFormString = pFormTable->GenerateFormat(nIndex1,
231 eLanguage, false, false, nL);
232 break;
233 case 0x07: // Special format
234 switch( nL )
236 case 0x00: // +/-
237 //fStandard;nSt;
238 nIndex1 = pFormTable->GetStandardFormat(
239 css::util::NumberFormat::NUMBER, eLanguage );
240 aFormString = pFormTable->GenerateFormat(nIndex1,
241 eLanguage, false, true, nSt);
242 break;
243 case 0x01: // general Format
244 //fStandard;nSt;
245 nIndex1 = pFormTable->GetStandardFormat(
246 css::util::NumberFormat::NUMBER, eLanguage );
247 aFormString = pFormTable->GenerateFormat(nIndex1,
248 eLanguage, false, false, nSt);
249 break;
250 case 0x02: // Date: Day, Month, Year
251 //fDate;dfDayMonthYearLong;
252 eType = css::util::NumberFormat::DATE;
253 eIndexTableOffset = NF_DATE_SYS_DDMMYYYY;
254 break;
255 case 0x03: // Date: Day, Month
256 //fDate;dfDayMonthLong;
257 eType = css::util::NumberFormat::DATE;
258 aFormString = pFormTable->GetKeyword( eLanguage, NF_KEY_DD);
259 aFormString += pFormTable->GetDateSep(); // matches last eLanguage
260 aFormString += pFormTable->GetKeyword( eLanguage, NF_KEY_MMMM);
261 break;
262 case 0x04: // Date: Month, Year
263 //fDate;dfMonthYearLong;
264 eType = css::util::NumberFormat::DATE;
265 aFormString = pFormTable->GetKeyword( eLanguage, NF_KEY_MM);
266 aFormString += pFormTable->GetDateSep(); // matches last eLanguage
267 aFormString += pFormTable->GetKeyword( eLanguage, NF_KEY_YYYY);
268 break;
269 case 0x05: // Text formats
270 //fString;nSt;
271 eType = css::util::NumberFormat::TEXT;
272 eIndexTableOffset = NF_TEXT;
273 break;
274 case 0x06: // hidden
275 //wFlag |= paHideAll;bSetFormat = sal_False;
276 eType = css::util::NumberFormat::NUMBER;
277 aFormString = "\"\"";
278 break;
279 case 0x07: // Time: hour, min, sec
280 //fTime;tfHourMinSec24;
281 eType = css::util::NumberFormat::TIME;
282 eIndexTableOffset = NF_TIME_HHMMSS;
283 break;
284 case 0x08: // Time: hour, min
285 //fTime;tfHourMin24;
286 eType = css::util::NumberFormat::TIME;
287 eIndexTableOffset = NF_TIME_HHMM;
288 break;
289 case 0x09: // Date, intern sal_Int32 1
290 //fDate;dfDayMonthYearLong;
291 eType = css::util::NumberFormat::DATE;
292 eIndexTableOffset = NF_DATE_SYS_DDMMYYYY;
293 break;
294 case 0x0A: // Date, intern sal_Int32 2
295 //fDate;dfDayMonthYearLong;
296 eType = css::util::NumberFormat::DATE;
297 eIndexTableOffset = NF_DATE_SYS_DDMMYYYY;
298 break;
299 case 0x0B: // Time, intern sal_Int32 1
300 //fTime;tfHourMinSec24;
301 eType = css::util::NumberFormat::TIME;
302 eIndexTableOffset = NF_TIME_HHMMSS;
303 break;
304 case 0x0C: // Time, intern sal_Int32 2
305 //fTime;tfHourMinSec24;
306 eType = css::util::NumberFormat::TIME;
307 eIndexTableOffset = NF_TIME_HHMMSS;
308 break;
309 case 0x0F: // Default
310 //fStandard;nSt;
311 bDefault = true;
312 break;
313 default:
314 //fStandard;nSt;
315 bDefault = true;
316 break;
318 break;
321 // push Format into table
322 if( bDefault )
323 nHandle = 0;
324 else if (eIndexTableOffset != NF_NUMERIC_START)
325 nHandle = pFormTable->GetFormatIndex( eIndexTableOffset, eLanguage);
326 else
328 sal_Int32 nDummy;
329 pFormTable->PutEntry( aFormString, nDummy, eType, nHandle, eLanguage );
332 return new SfxUInt32Item( ATTR_VALUE_FORMAT, nHandle );
335 void LotusRange::MakeHash()
337 // 33222222222211111111110000000000
338 // 10987654321098765432109876543210
339 // ******** nColS
340 // ******** nColE
341 // **************** nRowS
342 // **************** nRowE
343 nHash = static_cast<sal_uInt32>(nColStart);
344 nHash += static_cast<sal_uInt32>(nColEnd) << 6;
345 nHash += static_cast<sal_uInt32>(nRowStart) << 12;
346 nHash += static_cast<sal_uInt32>(nRowEnd ) << 16;
349 LotusRange::LotusRange( SCCOL nCol, SCROW nRow )
351 nColStart = nColEnd = nCol;
352 nRowStart = nRowEnd = nRow;
353 nId = ID_FAIL;
354 MakeHash();
357 LotusRange::LotusRange( SCCOL nCS, SCROW nRS, SCCOL nCE, SCROW nRE )
359 nColStart = nCS;
360 nColEnd = nCE;
361 nRowStart = nRS;
362 nRowEnd = nRE;
363 nId = ID_FAIL;
364 MakeHash();
367 LotusRange::LotusRange( const LotusRange& rCpy )
369 Copy( rCpy );
372 LotusRangeList::LotusRangeList(LOTUS_ROOT* pLotRoot)
373 : m_pLotRoot(pLotRoot)
375 aComplRef.InitFlags();
377 ScSingleRefData* pSingRef;
378 nIdCnt = 1;
380 pSingRef = &aComplRef.Ref1;
381 pSingRef->SetRelTab(0);
382 pSingRef->SetColRel( false );
383 pSingRef->SetRowRel( false );
384 pSingRef->SetFlag3D( false );
386 pSingRef = &aComplRef.Ref2;
387 pSingRef->SetRelTab(0);
388 pSingRef->SetColRel( false );
389 pSingRef->SetRowRel( false );
390 pSingRef->SetFlag3D( false );
393 SCCOL LotusRangeList::nEingCol;
394 SCROW LotusRangeList::nEingRow;
396 LotusRangeList::~LotusRangeList ()
398 std::vector<LotusRange*>::iterator pIter;
399 for (pIter = maRanges.begin(); pIter != maRanges.end(); ++pIter)
400 delete (*pIter);
403 LR_ID LotusRangeList::GetIndex( const LotusRange &rRef )
405 std::vector<LotusRange*>::iterator pIter;
406 for (pIter = maRanges.begin(); pIter != maRanges.end(); ++pIter)
408 if (rRef == *(*pIter))
409 return (*pIter)->nId;
412 return ID_FAIL;
415 void LotusRangeList::Append( LotusRange* pLR, const OUString& rName )
417 OSL_ENSURE( pLR, "*LotusRangeList::Append(): no pointer!" );
418 maRanges.push_back(pLR);
420 ScTokenArray aTokArray;
422 ScSingleRefData* pSingRef = &aComplRef.Ref1;
424 pSingRef->SetAbsCol(pLR->nColStart);
425 pSingRef->SetAbsRow(pLR->nRowStart);
427 if( pLR->IsSingle() )
428 aTokArray.AddSingleReference( *pSingRef );
429 else
431 pSingRef = &aComplRef.Ref2;
432 pSingRef->SetAbsCol(pLR->nColEnd);
433 pSingRef->SetAbsRow(pLR->nRowEnd);
434 aTokArray.AddDoubleReference( aComplRef );
437 ScRangeData* pData = new ScRangeData(
438 m_pLotRoot->pDoc, rName, aTokArray );
440 m_pLotRoot->pScRangeName->insert( pData );
442 pLR->SetId( nIdCnt );
444 nIdCnt++;
447 RangeNameBufferWK3::RangeNameBufferWK3(LOTUS_ROOT* pLotRoot)
448 : m_pLotRoot(pLotRoot)
450 pScTokenArray = new ScTokenArray;
451 nIntCount = 1;
454 RangeNameBufferWK3::~RangeNameBufferWK3()
456 delete pScTokenArray;
459 void RangeNameBufferWK3::Add( const OUString& rOrgName, const ScComplexRefData& rCRD )
461 OUString aScName = ScfTools::ConvertToScDefinedName(rOrgName);
463 Entry aInsert( rOrgName, aScName, rCRD );
465 pScTokenArray->Clear();
467 const ScSingleRefData& rRef1 = rCRD.Ref1;
468 const ScSingleRefData& rRef2 = rCRD.Ref2;
469 ScAddress aAbs1 = rRef1.toAbs(ScAddress());
470 ScAddress aAbs2 = rRef2.toAbs(ScAddress());
471 if (aAbs1 == aAbs2)
473 pScTokenArray->AddSingleReference( rCRD.Ref1 );
474 aInsert.bSingleRef = true;
476 else
478 pScTokenArray->AddDoubleReference( rCRD );
479 aInsert.bSingleRef = false;
482 ScRangeData* pData = new ScRangeData( m_pLotRoot->pDoc, aScName, *pScTokenArray );
484 aInsert.nRelInd = nIntCount;
485 pData->SetIndex( nIntCount );
486 nIntCount++;
488 maEntries.push_back( aInsert );
489 m_pLotRoot->pScRangeName->insert( pData );
492 bool RangeNameBufferWK3::FindRel( const OUString& rRef, sal_uInt16& rIndex )
494 StringHashEntry aRef( rRef );
496 std::vector<Entry>::const_iterator itr;
497 for ( itr = maEntries.begin(); itr != maEntries.end(); ++itr )
499 if ( aRef == itr->aStrHashEntry )
501 rIndex = itr->nRelInd;
502 return true;
506 return false;
509 bool RangeNameBufferWK3::FindAbs( const OUString& rRef, sal_uInt16& rIndex )
511 OUString aTmp( rRef );
512 aTmp = aTmp.copy(1);
513 StringHashEntry aRef( aTmp ); // search w/o '$'!
515 std::vector<Entry>::iterator itr;
516 for ( itr = maEntries.begin(); itr != maEntries.end(); ++itr )
518 if ( aRef == itr->aStrHashEntry )
520 // setup new range if needed
521 if( itr->nAbsInd )
522 rIndex = itr->nAbsInd;
523 else
525 ScSingleRefData* pRef = &itr->aScComplexRefDataRel.Ref1;
526 pScTokenArray->Clear();
528 pRef->SetColRel( false );
529 pRef->SetRowRel( false );
530 pRef->SetTabRel( true );
532 if( itr->bSingleRef )
533 pScTokenArray->AddSingleReference( *pRef );
534 else
536 pRef = &itr->aScComplexRefDataRel.Ref2;
537 pRef->SetColRel( false );
538 pRef->SetRowRel( false );
539 pRef->SetTabRel( true );
540 pScTokenArray->AddDoubleReference( itr->aScComplexRefDataRel );
543 ScRangeData* pData = new ScRangeData( m_pLotRoot->pDoc, itr->aScAbsName, *pScTokenArray );
545 rIndex = itr->nAbsInd = nIntCount;
546 pData->SetIndex( rIndex );
547 nIntCount++;
549 m_pLotRoot->pScRangeName->insert( pData );
552 return true;
556 return false;
559 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */