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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sc.hxx"
// INCLUDE ---------------------------------------------------------------
#include "scitems.hxx"
#include <sfx2/app.hxx>
#include <sfx2/bindings.hxx>
#include <vcl/msgbox.hxx>
#include <com/sun/star/sdbc/XResultSet.hpp>
#include "dbfunc.hxx"
#include "docsh.hxx"
#include "attrib.hxx"
#include "sc.hrc"
#include "undodat.hxx"
#include "dbdata.hxx"
#include "globstr.hrc"
#include "global.hxx"
#include "dbdocfun.hxx"
#include "editable.hxx"
#include "queryentry.hxx"
//==================================================================
ScDBFunc::ScDBFunc( Window* pParent, ScDocShell& rDocSh, ScTabViewShell* pViewShell ) :
ScViewFunc( pParent, rDocSh, pViewShell )
{
}
ScDBFunc::~ScDBFunc()
{
}
//
// auxiliary functions
//
void ScDBFunc::GotoDBArea( const ::rtl::OUString& rDBName )
{
ScDocument* pDoc = GetViewData()->GetDocument();
ScDBCollection* pDBCol = pDoc->GetDBCollection();
ScDBData* pData = pDBCol->getNamedDBs().findByName(rDBName);
if (pData)
{
SCTAB nTab = 0;
SCCOL nStartCol = 0;
SCROW nStartRow = 0;
SCCOL nEndCol = 0;
SCROW nEndRow = 0;
pData->GetArea( nTab, nStartCol, nStartRow, nEndCol, nEndRow );
SetTabNo( nTab );
MoveCursorAbs( nStartCol, nStartRow, ScFollowMode( SC_FOLLOW_JUMP ),
false, false ); // bShift,bControl
DoneBlockMode();
InitBlockMode( nStartCol, nStartRow, nTab );
MarkCursor( nEndCol, nEndRow, nTab );
SelectionChanged();
}
}
// get actual datarange for sort / filter search
ScDBData* ScDBFunc::GetDBData( bool bMark, ScGetDBMode eMode, ScGetDBSelection eSel )
{
ScDocShell* pDocSh = GetViewData()->GetDocShell();
ScDBData* pData = NULL;
ScRange aRange;
ScMarkType eMarkType = GetViewData()->GetSimpleArea(aRange);
if ( eMarkType == SC_MARK_SIMPLE || eMarkType == SC_MARK_SIMPLE_FILTERED )
{
bool bShrinkColumnsOnly = false;
if (eSel == SC_DBSEL_ROW_DOWN)
{
// Don't alter row range, additional rows may have been selected on
// purpose to append data, or to have a fake header row.
bShrinkColumnsOnly = true;
// Select further rows only if only one row or a portion thereof is
// selected.
if (aRange.aStart.Row() != aRange.aEnd.Row())
{
// If an area is selected shrink that to the actual used
// columns, don't draw filter buttons for empty columns.
eSel = SC_DBSEL_SHRINK_TO_USED_DATA;
}
else if (aRange.aStart.Col() == aRange.aEnd.Col())
{
// One cell only, if it is not marked obtain entire used data
// area.
const ScMarkData& rMarkData = GetViewData()->GetMarkData();
if (!(rMarkData.IsMarked() || rMarkData.IsMultiMarked()))
eSel = SC_DBSEL_KEEP;
}
}
switch (eSel)
{
case SC_DBSEL_SHRINK_TO_SHEET_DATA:
{
// Shrink the selection to sheet data area.
ScDocument* pDoc = pDocSh->GetDocument();
SCCOL nCol1 = aRange.aStart.Col(), nCol2 = aRange.aEnd.Col();
SCROW nRow1 = aRange.aStart.Row(), nRow2 = aRange.aEnd.Row();
if (pDoc->ShrinkToDataArea( aRange.aStart.Tab(), nCol1, nRow1, nCol2, nRow2))
{
aRange.aStart.SetCol(nCol1);
aRange.aEnd.SetCol(nCol2);
aRange.aStart.SetRow(nRow1);
aRange.aEnd.SetRow(nRow2);
}
}
break;
case SC_DBSEL_SHRINK_TO_USED_DATA:
case SC_DBSEL_ROW_DOWN:
{
// Shrink the selection to actual used area.
ScDocument* pDoc = pDocSh->GetDocument();
SCCOL nCol1 = aRange.aStart.Col(), nCol2 = aRange.aEnd.Col();
SCROW nRow1 = aRange.aStart.Row(), nRow2 = aRange.aEnd.Row();
bool bShrunk;
pDoc->ShrinkToUsedDataArea( bShrunk, aRange.aStart.Tab(),
nCol1, nRow1, nCol2, nRow2, bShrinkColumnsOnly);
if (bShrunk)
{
aRange.aStart.SetCol(nCol1);
aRange.aEnd.SetCol(nCol2);
aRange.aStart.SetRow(nRow1);
aRange.aEnd.SetRow(nRow2);
}
}
break;
default:
; // nothing
}
pData = pDocSh->GetDBData( aRange, eMode, eSel );
}
else if ( eMode != SC_DB_OLD )
pData = pDocSh->GetDBData(
ScRange( GetViewData()->GetCurX(), GetViewData()->GetCurY(),
GetViewData()->GetTabNo() ),
eMode, SC_DBSEL_KEEP );
if (!pData)
return NULL;
if (bMark)
{
ScRange aFound;
pData->GetArea(aFound);
MarkRange( aFound, false );
}
return pData;
}
ScDBData* ScDBFunc::GetAnonymousDBData()
{
ScDocShell* pDocSh = GetViewData()->GetDocShell();
ScRange aRange;
ScMarkType eMarkType = GetViewData()->GetSimpleArea(aRange);
if (eMarkType != SC_MARK_SIMPLE && eMarkType != SC_MARK_SIMPLE_FILTERED)
return NULL;
// Expand to used data area if not explicitly marked.
const ScMarkData& rMarkData = GetViewData()->GetMarkData();
if (!rMarkData.IsMarked() && !rMarkData.IsMultiMarked())
{
SCCOL nCol1 = aRange.aStart.Col();
SCCOL nCol2 = aRange.aEnd.Col();
SCROW nRow1 = aRange.aStart.Row();
SCROW nRow2 = aRange.aEnd.Row();
pDocSh->GetDocument()->GetDataArea(aRange.aStart.Tab(), nCol1, nRow1, nCol2, nRow2, false, false);
aRange.aStart.SetCol(nCol1);
aRange.aStart.SetRow(nRow1);
aRange.aEnd.SetCol(nCol2);
aRange.aEnd.SetRow(nRow2);
}
return pDocSh->GetAnonymousDBData(aRange);
}
// change database range (dialog)
void ScDBFunc::NotifyCloseDbNameDlg( const ScDBCollection& rNewColl, const std::vector<ScRange> &rDelAreaList )
{
ScDocShell* pDocShell = GetViewData()->GetDocShell();
ScDocShellModificator aModificator( *pDocShell );
ScDocument* pDoc = pDocShell->GetDocument();
ScDBCollection* pOldColl = pDoc->GetDBCollection();
ScDBCollection* pUndoColl = NULL;
ScDBCollection* pRedoColl = NULL;
const sal_Bool bRecord (pDoc->IsUndoEnabled());
std::vector<ScRange>::const_iterator iter;
for (iter = rDelAreaList.begin(); iter != rDelAreaList.end(); ++iter)
{
// registering target in SBA no longer necessary
const ScAddress& rStart = iter->aStart;
const ScAddress& rEnd = iter->aEnd;
pDocShell->DBAreaDeleted( rStart.Tab(),
rStart.Col(), rStart.Row(),
rEnd.Col(), rEnd.Row() );
}
if (bRecord)
pUndoColl = new ScDBCollection( *pOldColl );
// register target in SBA no longer necessary
pDoc->CompileDBFormula( sal_True ); // CreateFormulaString
pDoc->SetDBCollection( new ScDBCollection( rNewColl ) );
pDoc->CompileDBFormula( false ); // CompileFormulaString
pOldColl = NULL;
pDocShell->PostPaint(ScRange(0, 0, 0, MAXCOL, MAXROW, MAXTAB), PAINT_GRID);
aModificator.SetDocumentModified();
SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_DBAREAS_CHANGED ) );
if (bRecord)
{
pRedoColl = new ScDBCollection( rNewColl );
pDocShell->GetUndoManager()->AddUndoAction(
new ScUndoDBData( pDocShell, pUndoColl, pRedoColl ) );
}
}
//
// main functions
//
// Sort
void ScDBFunc::UISort( const ScSortParam& rSortParam, sal_Bool bRecord )
{
ScDocShell* pDocSh = GetViewData()->GetDocShell();
ScDocument* pDoc = pDocSh->GetDocument();
SCTAB nTab = GetViewData()->GetTabNo();
ScDBData* pDBData = pDoc->GetDBAtArea( nTab, rSortParam.nCol1, rSortParam.nRow1,
rSortParam.nCol2, rSortParam.nRow2 );
if (!pDBData)
{
OSL_FAIL( "Sort: no DBData" );
return;
}
ScSubTotalParam aSubTotalParam;
pDBData->GetSubTotalParam( aSubTotalParam );
if (aSubTotalParam.bGroupActive[0] && !aSubTotalParam.bRemoveOnly)
{
// repeat subtotals, with new sortorder
DoSubTotals( aSubTotalParam, bRecord, &rSortParam );
}
else
{
Sort( rSortParam, bRecord ); // just sort
}
}
void ScDBFunc::Sort( const ScSortParam& rSortParam, sal_Bool bRecord, sal_Bool bPaint )
{
ScDocShell* pDocSh = GetViewData()->GetDocShell();
SCTAB nTab = GetViewData()->GetTabNo();
ScDBDocFunc aDBDocFunc( *pDocSh );
sal_Bool bSuccess = aDBDocFunc.Sort( nTab, rSortParam, bRecord, bPaint, false );
if ( bSuccess && !rSortParam.bInplace )
{
// mark target
ScRange aDestRange( rSortParam.nDestCol, rSortParam.nDestRow, rSortParam.nDestTab,
rSortParam.nDestCol + rSortParam.nCol2 - rSortParam.nCol1,
rSortParam.nDestRow + rSortParam.nRow2 - rSortParam.nRow1,
rSortParam.nDestTab );
MarkRange( aDestRange );
}
}
// filters
void ScDBFunc::Query( const ScQueryParam& rQueryParam, const ScRange* pAdvSource, sal_Bool bRecord )
{
ScDocShell* pDocSh = GetViewData()->GetDocShell();
SCTAB nTab = GetViewData()->GetTabNo();
ScDBDocFunc aDBDocFunc( *pDocSh );
sal_Bool bSuccess = aDBDocFunc.Query( nTab, rQueryParam, pAdvSource, bRecord, false );
if (bSuccess)
{
sal_Bool bCopy = !rQueryParam.bInplace;
if (bCopy)
{
// mark target range (data base range has been set up if applicable)
ScDocument* pDoc = pDocSh->GetDocument();
ScDBData* pDestData = pDoc->GetDBAtCursor(
rQueryParam.nDestCol, rQueryParam.nDestRow,
rQueryParam.nDestTab, sal_True );
if (pDestData)
{
ScRange aDestRange;
pDestData->GetArea(aDestRange);
MarkRange( aDestRange );
}
}
if (!bCopy)
{
UpdateScrollBars();
SelectionChanged(); // for attribute states (filtered rows are ignored)
}
GetViewData()->GetBindings().Invalidate( SID_UNFILTER );
}
}
// autofilter-buttons show / hide
void ScDBFunc::ToggleAutoFilter()
{
ScDocShell* pDocSh = GetViewData()->GetDocShell();
ScDocShellModificator aModificator( *pDocSh );
ScQueryParam aParam;
ScDocument* pDoc = GetViewData()->GetDocument();
ScDBData* pDBData = GetDBData(false, SC_DB_MAKE, SC_DBSEL_ROW_DOWN);
pDBData->SetByRow( sal_True ); //! undo, retrieve beforehand ??
pDBData->GetQueryParam( aParam );
SCCOL nCol;
SCROW nRow = aParam.nRow1;
SCTAB nTab = GetViewData()->GetTabNo();
sal_Int16 nFlag;
sal_Bool bHasAuto = sal_True;
sal_Bool bHeader = pDBData->HasHeader();
sal_Bool bPaint = false;
//! instead retrieve from DB-range?
for (nCol=aParam.nCol1; nCol<=aParam.nCol2 && bHasAuto; nCol++)
{
nFlag = ((ScMergeFlagAttr*) pDoc->
GetAttr( nCol, nRow, nTab, ATTR_MERGE_FLAG ))->GetValue();
if ( (nFlag & SC_MF_AUTO) == 0 )
bHasAuto = false;
}
if (bHasAuto) // remove
{
// hide filter buttons
for (nCol=aParam.nCol1; nCol<=aParam.nCol2; nCol++)
{
nFlag = ((ScMergeFlagAttr*) pDoc->
GetAttr( nCol, nRow, nTab, ATTR_MERGE_FLAG ))->GetValue();
pDoc->ApplyAttr( nCol, nRow, nTab, ScMergeFlagAttr( nFlag & ~SC_MF_AUTO ) );
}
// use a list action for the AutoFilter buttons (ScUndoAutoFilter) and the filter operation
String aUndo = ScGlobal::GetRscString( STR_UNDO_QUERY );
pDocSh->GetUndoManager()->EnterListAction( aUndo, aUndo );
ScRange aRange;
pDBData->GetArea( aRange );
pDocSh->GetUndoManager()->AddUndoAction(
new ScUndoAutoFilter( pDocSh, aRange, pDBData->GetName(), false ) );
pDBData->SetAutoFilter(false);
// remove filter (incl. Paint / Undo)
SCSIZE nEC = aParam.GetEntryCount();
for (SCSIZE i=0; i<nEC; i++)
aParam.GetEntry(i).bDoQuery = false;
aParam.bDuplicate = sal_True;
Query( aParam, NULL, sal_True );
pDocSh->GetUndoManager()->LeaveListAction();
bPaint = sal_True;
}
else // show filter buttons
{
if ( !pDoc->IsBlockEmpty( nTab,
aParam.nCol1, aParam.nRow1,
aParam.nCol2, aParam.nRow2 ) )
{
if (!bHeader)
{
if ( MessBox( GetViewData()->GetDialogParent(), WinBits(WB_YES_NO | WB_DEF_YES),
ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_0 ), // "StarCalc"
ScGlobal::GetRscString( STR_MSSG_MAKEAUTOFILTER_0 ) // header from first cell?
).Execute() == RET_YES )
{
pDBData->SetHeader( sal_True ); //! Undo ??
bHeader = sal_True;
}
}
ScRange aRange;
pDBData->GetArea( aRange );
pDocSh->GetUndoManager()->AddUndoAction(
new ScUndoAutoFilter( pDocSh, aRange, pDBData->GetName(), sal_True ) );
pDBData->SetAutoFilter(sal_True);
for (nCol=aParam.nCol1; nCol<=aParam.nCol2; nCol++)
{
nFlag = ((ScMergeFlagAttr*) pDoc->
GetAttr( nCol, nRow, nTab, ATTR_MERGE_FLAG ))->GetValue();
pDoc->ApplyAttr( nCol, nRow, nTab, ScMergeFlagAttr( nFlag | SC_MF_AUTO ) );
}
pDocSh->PostPaint(ScRange(aParam.nCol1, nRow, nTab, aParam.nCol2, nRow, nTab),
PAINT_GRID);
bPaint = sal_True;
}
else
{
ErrorBox aErrorBox( GetViewData()->GetDialogParent(), WinBits( WB_OK | WB_DEF_OK ),
ScGlobal::GetRscString( STR_ERR_AUTOFILTER ) );
aErrorBox.Execute();
}
}
if ( bPaint )
{
aModificator.SetDocumentModified();
SfxBindings& rBindings = GetViewData()->GetBindings();
rBindings.Invalidate( SID_AUTO_FILTER );
rBindings.Invalidate( SID_AUTOFILTER_HIDE );
}
}
// just hide, no data change
void ScDBFunc::HideAutoFilter()
{
ScDocShell* pDocSh = GetViewData()->GetDocShell();
ScDocShellModificator aModificator( *pDocSh );
ScDocument* pDoc = pDocSh->GetDocument();
ScQueryParam aParam;
ScDBData* pDBData = GetDBData( false );
SCTAB nTab;
SCCOL nCol1, nCol2;
SCROW nRow1, nRow2;
pDBData->GetArea(nTab, nCol1, nRow1, nCol2, nRow2);
for (SCCOL nCol=nCol1; nCol<=nCol2; nCol++)
{
sal_Int16 nFlag = ((ScMergeFlagAttr*) pDoc->
GetAttr( nCol, nRow1, nTab, ATTR_MERGE_FLAG ))->GetValue();
pDoc->ApplyAttr( nCol, nRow1, nTab, ScMergeFlagAttr( nFlag & ~SC_MF_AUTO ) );
}
ScRange aRange;
pDBData->GetArea( aRange );
pDocSh->GetUndoManager()->AddUndoAction(
new ScUndoAutoFilter( pDocSh, aRange, pDBData->GetName(), false ) );
pDBData->SetAutoFilter(false);
pDocSh->PostPaint(ScRange(nCol1, nRow1, nTab, nCol2, nRow1, nTab), PAINT_GRID );
aModificator.SetDocumentModified();
SfxBindings& rBindings = GetViewData()->GetBindings();
rBindings.Invalidate( SID_AUTO_FILTER );
rBindings.Invalidate( SID_AUTOFILTER_HIDE );
}
// Re-Import
sal_Bool ScDBFunc::ImportData( const ScImportParam& rParam, sal_Bool bRecord )
{
ScDocument* pDoc = GetViewData()->GetDocument();
ScEditableTester aTester( pDoc, GetViewData()->GetTabNo(), rParam.nCol1,rParam.nRow1,
rParam.nCol2,rParam.nRow2 );
if ( !aTester.IsEditable() )
{
ErrorMessage(aTester.GetMessageId());
return false;
}
ScDBDocFunc aDBDocFunc( *GetViewData()->GetDocShell() );
::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet > xResultSet;
return aDBDocFunc.DoImport( GetViewData()->GetTabNo(), rParam, xResultSet, NULL, bRecord );
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|