summaryrefslogtreecommitdiff
path: root/binfilter/bf_sc/source/core/data/sc_documen4.cxx
blob: de6e817cd7593723967b7e45578c355db990fbca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifdef _MSC_VER
#pragma hdrstop
#endif


#include <bf_svtools/intitem.hxx>
#include <vcl/sound.hxx>

#include "globstr.hrc"
#include "subtotal.hxx"
#include "interpre.hxx"
#include "markdata.hxx"
#include "validat.hxx"
#include "scitems.hxx"
#include "stlpool.hxx"
#include "poolhelp.hxx"
#include "detdata.hxx"
namespace binfilter {

// -----------------------------------------------------------------------

// Nach der Regula Falsi Methode
/*N*/ BOOL ScDocument::Solver(USHORT nFCol, USHORT nFRow, USHORT nFTab,
/*N*/                       USHORT nVCol, USHORT nVRow, USHORT nVTab,
/*N*/                       const String& sValStr, double& nX)
/*N*/ {
/*N*/   BOOL bRet = FALSE;
/*N*/   nX = 0.0;
/*N*/   if (ValidColRow(nFCol, nFRow) && ValidColRow(nVCol, nVRow) &&
/*N*/       VALIDTAB(nFTab) && VALIDTAB(nVTab) && pTab[nFTab] && pTab[nVTab])
/*N*/   {
/*N*/       CellType eFType, eVType;
/*N*/       GetCellType(nFCol, nFRow, nFTab, eFType);
/*N*/       GetCellType(nVCol, nVRow, nVTab, eVType);
/*N*/       // CELLTYPE_NOTE: kein Value aber von Formel referiert
/*N*/       if (eFType == CELLTYPE_FORMULA && (eVType == CELLTYPE_VALUE
/*N*/               || eVType == CELLTYPE_NOTE) )
/*N*/       {
/*N*/           SingleRefData aRefData;
/*N*/           aRefData.InitFlags();
/*N*/           aRefData.nCol = nVCol;
/*N*/           aRefData.nRow = nVRow;
/*N*/           aRefData.nTab = nVTab;
/*N*/
/*N*/           ScTokenArray aArr;
/*N*/           aArr.AddOpCode( ocBackSolver );
/*N*/           aArr.AddOpCode( ocOpen );
/*N*/           aArr.AddSingleReference( aRefData );
/*N*/           aArr.AddOpCode( ocSep );
/*N*/
/*N*/           aRefData.nCol = nFCol;
/*N*/           aRefData.nRow = nFRow;
/*N*/           aRefData.nTab = nFTab;
/*N*/
/*N*/           aArr.AddSingleReference( aRefData );
/*N*/           aArr.AddOpCode( ocSep );
/*N*/           aArr.AddString( sValStr.GetBuffer() );
/*N*/           aArr.AddOpCode( ocClose );
/*N*/           aArr.AddOpCode( ocStop );
/*N*/
/*N*/           ScFormulaCell* pCell = new ScFormulaCell( this, ScAddress(), &aArr );
/*N*/
/*N*/           if (pCell)
/*N*/           {
/*N*/               pCell->Interpret();
/*N*/               USHORT nErrCode = pCell->GetErrCode();
/*N*/               nX = pCell->GetValueAlways();
/*N*/               if (nErrCode == 0)                  // kein fehler beim Rechnen
/*N*/                   bRet = TRUE;
/*N*/               delete pCell;
/*N*/           }
/*N*/       }
/*N*/   }
/*N*/   return bRet;
/*N*/ }

/*N*/ void ScDocument::InsertMatrixFormula(USHORT nCol1, USHORT nRow1,
/*N*/                                    USHORT nCol2, USHORT nRow2,
/*N*/                                    const ScMarkData& rMark,
/*N*/                                    const String& rFormula,
/*N*/                                    const ScTokenArray* pArr )
/*N*/ {
/*N*/   PutInOrder(nCol1, nCol2);
/*N*/   PutInOrder(nRow1, nRow2);
/*N*/   USHORT i, j, k, nTab1;
/*N*/   i = 0;
/*N*/   BOOL bStop = FALSE;
/*N*/   while (i <= MAXTAB && !bStop)               // erste markierte Tabelle finden
/*N*/   {
/*N*/       if (pTab[i] && rMark.GetTableSelect(i))
/*N*/           bStop = TRUE;
/*N*/       else
/*N*/           i++;
/*N*/   }
/*N*/   nTab1 = i;
/*N*/   if (i == MAXTAB + 1)
/*N*/   {
/*N*/       Sound::Beep();
/*N*/       OSL_FAIL("ScDocument::InsertMatrixFormula Keine Tabelle markiert");
/*N*/       return;
/*N*/   }
/*N*/
/*N*/   ScFormulaCell* pCell;
/*N*/   ScAddress aPos( nCol1, nRow1, nTab1 );
/*N*/   if (pArr)
/*N*/       pCell = new ScFormulaCell( this, aPos, pArr, MM_FORMULA );
/*N*/   else
/*N*/       pCell = new ScFormulaCell( this, aPos, rFormula, MM_FORMULA );
/*N*/   pCell->SetMatColsRows( nCol2 - nCol1 + 1, nRow2 - nRow1 + 1 );
/*N*/   for (i = 0; i <= MAXTAB; i++)
/*N*/   {
/*N*/       if (pTab[i] && rMark.GetTableSelect(i))
/*N*/       {
/*N*/           if (i == nTab1)
/*N*/               pTab[i]->PutCell(nCol1, nRow1, pCell);
/*N*/       }
/*N*/   }
/*N*/
/*N*/   SingleRefData aRefData;
/*N*/   aRefData.InitFlags();
/*N*/   aRefData.nCol = nCol1;
/*N*/   aRefData.nRow = nRow1;
/*N*/   aRefData.nTab = nTab1;
/*N*/   aRefData.SetColRel( TRUE );
/*N*/   aRefData.SetRowRel( TRUE );
/*N*/   aRefData.SetTabRel( TRUE );
/*N*/   aRefData.CalcRelFromAbs( ScAddress( nCol1, nRow1, nTab1 ) );
/*N*/
/*N*/   ScTokenArray aArr;
/*N*/   ScToken* t = aArr.AddSingleReference(aRefData);
/*N*/   t->NewOpCode( ocMatRef );
/*N*/
/*N*/   for (i = 0; i <= MAXTAB; i++)
/*N*/   {
/*N*/       if (pTab[i] && rMark.GetTableSelect(i))
/*N*/       {
/*N*/           pTab[i]->DoColResize( nCol1, nCol2, nRow2 - nRow1 + 1 );
/*N*/           if (i != nTab1)
/*N*/           {
/*?*/               aRefData.nTab = i;
/*?*/               aRefData.nRelTab = i - nTab1;
/*?*/               t->GetSingleRef() = aRefData;
/*N*/           }
/*N*/           for (j = nCol1; j <= nCol2; j++)
/*N*/           {
/*N*/               for (k = nRow1; k <= nRow2; k++)
/*N*/               {
/*N*/                   if (j != nCol1 || k != nRow1)       // nicht in der ersten Zelle
/*N*/                   {
/*N*/                       // Array muss geklont werden, damit jede
/*N*/                       // Zelle ein eigenes Array erhaelt!
/*N*/                       aPos = ScAddress( j, k, i );
/*N*/                       t->CalcRelFromAbs( aPos );
/*N*/                       pCell = new ScFormulaCell( this, aPos, aArr.Clone(), MM_REFERENCE );
/*N*/                       pTab[i]->PutCell(j, k, (ScBaseCell*) pCell);
/*N*/                   }
/*N*/               }
/*N*/           }
/*N*/       }
/*N*/   }
/*N*/ }




/*N*/ BOOL ScDocument::GetNextMarkedCell( USHORT& rCol, USHORT& rRow, USHORT nTab,
/*N*/                                       const ScMarkData& rMark )
/*N*/ {
/*N*/   if (nTab<=MAXTAB && pTab[nTab])
/*N*/       return pTab[nTab]->GetNextMarkedCell( rCol, rRow, rMark );
/*N*/   else
/*N*/       return FALSE;
/*N*/ }


/*N*/ void ScDocument::CompileDBFormula( BOOL bCreateFormulaString )
/*N*/ {
/*N*/   for (USHORT i=0; i<=MAXTAB; i++)
/*N*/   {
/*N*/       if (pTab[i]) pTab[i]->CompileDBFormula( bCreateFormulaString );
/*N*/   }
/*N*/ }

/*N*/ void ScDocument::CompileNameFormula( BOOL bCreateFormulaString )
/*N*/ {
/*N*/   for (USHORT i=0; i<=MAXTAB; i++)
/*N*/   {
/*N*/       if (pTab[i]) pTab[i]->CompileNameFormula( bCreateFormulaString );
/*N*/   }
/*N*/ }

/*N*/ void ScDocument::CompileColRowNameFormula()
/*N*/ {
/*N*/   for (USHORT i=0; i<=MAXTAB; i++)
/*N*/   {
/*N*/       if (pTab[i]) pTab[i]->CompileColRowNameFormula();
/*N*/   }
/*N*/ }

/*N*/ void ScDocument::DoColResize( USHORT nTab, USHORT nCol1, USHORT nCol2, USHORT nAdd )
/*N*/ {
/*N*/   if (nTab<=MAXTAB && pTab[nTab])
/*N*/       pTab[nTab]->DoColResize( nCol1, nCol2, nAdd );
/*N*/   else
/*N*/       OSL_FAIL("DoColResize: falsche Tabelle");
/*N*/ }

/*N*/ void ScDocument::InvalidateTableArea()
/*N*/ {
/*N*/   for (USHORT nTab=0; nTab<=MAXTAB && pTab[nTab]; nTab++)
/*N*/   {
/*N*/       pTab[nTab]->InvalidateTableArea();
/*N*/       if ( pTab[nTab]->IsScenario() )
/*N*/           pTab[nTab]->InvalidateScenarioRanges();
/*N*/   }
/*N*/ }



/*N*/ BOOL ScDocument::GetSelectionFunction( ScSubTotalFunc eFunc,
/*N*/                                       const ScAddress& rCursor, const ScMarkData& rMark,
/*N*/                                       double& rResult )
/*N*/ {
/*N*/   ScFunctionData aData(eFunc);
/*N*/
/*N*/   ScRange aSingle( rCursor );
/*N*/   if ( rMark.IsMarked() )
/*N*/       rMark.GetMarkArea(aSingle);
/*N*/
/*N*/   USHORT nStartCol = aSingle.aStart.Col();
/*N*/   USHORT nStartRow = aSingle.aStart.Row();
/*N*/   USHORT nEndCol = aSingle.aEnd.Col();
/*N*/   USHORT nEndRow = aSingle.aEnd.Row();
/*N*/
/*N*/   for (USHORT nTab=0; nTab<=MAXTAB && !aData.bError; nTab++)
/*N*/       if (pTab[nTab] && rMark.GetTableSelect(nTab))
/*N*/           pTab[nTab]->UpdateSelectionFunction( aData,
/*N*/                           nStartCol, nStartRow, nEndCol, nEndRow, rMark );
/*N*/
/*N*/           //! rMark an UpdateSelectionFunction uebergeben !!!!!
/*N*/
/*N*/   if (!aData.bError)
/*N*/       switch (eFunc)
/*N*/       {
/*N*/           case SUBTOTAL_FUNC_SUM:
/*N*/               rResult = aData.nVal;
/*N*/               break;
/*N*/           case SUBTOTAL_FUNC_CNT:
/*N*/           case SUBTOTAL_FUNC_CNT2:
/*N*/               rResult = aData.nCount;
/*N*/               break;
/*N*/           case SUBTOTAL_FUNC_AVE:
/*N*/               if (aData.nCount)
/*N*/                   rResult = aData.nVal / (double) aData.nCount;
/*N*/               else
/*N*/                   aData.bError = TRUE;
/*N*/               break;
/*N*/           case SUBTOTAL_FUNC_MAX:
/*N*/           case SUBTOTAL_FUNC_MIN:
/*N*/               if (aData.nCount)
/*N*/                   rResult = aData.nVal;
/*N*/               else
/*N*/                   aData.bError = TRUE;
/*N*/               break;
/*N*/           default:
/*N*/               break;
/*N*/       }
/*N*/
/*N*/   if (aData.bError)
/*N*/       rResult = 0.0;
/*N*/
/*N*/   return !aData.bError;
/*N*/ }

//
//          bedingte Formate und Gueltigkeitsbereiche
//

/*N*/ ULONG ScDocument::AddCondFormat( const ScConditionalFormat& rNew )
/*N*/ {
/*N*/   if (rNew.IsEmpty())
/*N*/       return 0;                   // leer ist immer 0
/*N*/
/*N*/   if (!pCondFormList)
/*N*/       pCondFormList = new ScConditionalFormatList;
/*N*/
/*N*/   ULONG nMax = 0;
/*N*/   USHORT nCount = pCondFormList->Count();
/*N*/   for (USHORT i=0; i<nCount; i++)
/*N*/   {
/*N*/       const ScConditionalFormat* pForm = (*pCondFormList)[i];
/*N*/       ULONG nKey = pForm->GetKey();
/*N*/       if ( pForm->EqualEntries( rNew ) )
/*N*/           return nKey;
/*N*/       if ( nKey > nMax )
/*N*/           nMax = nKey;
/*N*/   }
/*N*/
/*N*/   // Der Aufruf kann aus ScPatternAttr::PutInPool kommen, darum Clone (echte Kopie)
/*N*/
/*N*/   ULONG nNewKey = nMax + 1;
/*N*/   ScConditionalFormat* pInsert = rNew.Clone(this);
/*N*/   pInsert->SetKey( nNewKey );
/*N*/   pCondFormList->InsertNew( pInsert );
/*N*/   return nNewKey;
/*N*/ }

/*N*/ ULONG ScDocument::AddValidationEntry( const ScValidationData& rNew )
/*N*/ {
/*N*/   if (rNew.IsEmpty())
/*N*/       return 0;                   // leer ist immer 0
/*N*/
/*N*/   if (!pValidationList)
/*N*/       pValidationList = new ScValidationDataList;
/*N*/
/*N*/   ULONG nMax = 0;
/*N*/   USHORT nCount = pValidationList->Count();
/*N*/   for (USHORT i=0; i<nCount; i++)
/*N*/   {
/*N*/       const ScValidationData* pData = (*pValidationList)[i];
/*N*/       ULONG nKey = pData->GetKey();
/*N*/       if ( pData->EqualEntries( rNew ) )
/*N*/           return nKey;
/*N*/       if ( nKey > nMax )
/*N*/           nMax = nKey;
/*N*/   }
/*N*/
/*N*/   // Der Aufruf kann aus ScPatternAttr::PutInPool kommen, darum Clone (echte Kopie)
/*N*/
/*N*/   ULONG nNewKey = nMax + 1;
/*N*/   ScValidationData* pInsert = rNew.Clone(this);
/*N*/   pInsert->SetKey( nNewKey );
/*N*/   pValidationList->InsertNew( pInsert );
/*N*/   return nNewKey;
/*N*/ }


/*N*/ const SfxItemSet* ScDocument::GetCondResult( USHORT nCol, USHORT nRow, USHORT nTab ) const
/*N*/ {
/*N*/   const ScConditionalFormat* pForm = GetCondFormat( nCol, nRow, nTab );
/*N*/   if ( pForm )
/*N*/   {
/*N*/       ScBaseCell* pCell = ((ScDocument*)this)->GetCell(ScAddress(nCol,nRow,nTab));
/*N*/       String aStyle = pForm->GetCellStyle( pCell, ScAddress(nCol, nRow, nTab) );
/*N*/       if (aStyle.Len())
/*N*/       {
/*N*/           SfxStyleSheetBase* pStyleSheet = xPoolHelper->GetStylePool()->Find( aStyle, SFX_STYLE_FAMILY_PARA );
/*N*/           if ( pStyleSheet )
/*N*/               return &pStyleSheet->GetItemSet();
/*N*/           // if style is not there, treat like no condition
/*N*/       }
/*N*/   }
/*N*/   return NULL;
/*N*/ }

/*N*/ const ScConditionalFormat* ScDocument::GetCondFormat(
/*N*/                           USHORT nCol, USHORT nRow, USHORT nTab ) const
/*N*/ {
/*N*/   ULONG nIndex = ((const SfxUInt32Item*)GetAttr(nCol,nRow,nTab,ATTR_CONDITIONAL))->GetValue();
/*N*/   if (nIndex)
/*N*/   {
/*N*/       if (pCondFormList)
/*N*/           return pCondFormList->GetFormat( nIndex );
/*N*/       else
/*N*/           OSL_FAIL("pCondFormList ist 0");
/*N*/   }
/*N*/
/*N*/   return NULL;
/*N*/ }

/*N*/ const ScValidationData*   ScDocument::GetValidationEntry( ULONG nIndex ) const
/*N*/ {
/*N*/   if ( pValidationList )
/*N*/       return pValidationList->GetData( nIndex );
/*N*/   else
/*N*/       return NULL;
/*N*/ }

/*N*/ void ScDocument::FindConditionalFormat( ULONG nKey, ScRangeList& rRanges )
/*N*/ {
/*N*/   for (USHORT i=0; i<=MAXTAB && pTab[i]; i++)
/*N*/       pTab[i]->FindConditionalFormat( nKey, rRanges );
/*N*/ }


/*N*/ void ScDocument::ConditionalChanged( ULONG nKey )
/*N*/ {
/*N*/   if ( nKey && pCondFormList && !bIsClip && !bIsUndo )        // nKey==0 -> noop
/*N*/   {
/*N*/       ScConditionalFormat* pForm = pCondFormList->GetFormat( nKey );
/*N*/       if (pForm)
/*N*/           pForm->InvalidateArea();
/*N*/   }
/*N*/ }

//------------------------------------------------------------------------

/*N*/ void ScDocument::AddDetectiveOperation( const ScDetOpData& rData )
/*N*/ {
/*N*/   if (!pDetOpList)
/*N*/       pDetOpList = new ScDetOpList;
/*N*/
/*N*/   pDetOpList->Append( new ScDetOpData( rData ) );
/*N*/ }

/*N*/ void ScDocument::ClearDetectiveOperations()
/*N*/ {
/*N*/   delete pDetOpList;      // loescht auch die Eintraege
/*N*/   pDetOpList = NULL;
/*N*/ }


//------------------------------------------------------------------------
//
//      Vergleich von Dokumenten
//
//------------------------------------------------------------------------

//  Pfriemel-Faktoren
#define SC_DOCCOMP_MAXDIFF  256
#define SC_DOCCOMP_MINGOOD  128
#define SC_DOCCOMP_COLUMNS  10
#define SC_DOCCOMP_ROWS     100

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */