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 27 28 29 // INCLUDE --------------------------------------------------------------- 30 31 #include "scitems.hxx" 32 #include <sfx2/app.hxx> 33 #include <sfx2/bindings.hxx> 34 #include <vcl/msgbox.hxx> 35 36 #include <com/sun/star/sdbc/XResultSet.hpp> 37 38 #include "dbfunc.hxx" 39 #include "docsh.hxx" 40 #include "attrib.hxx" 41 #include "sc.hrc" 42 #include "undodat.hxx" 43 #include "dbcolect.hxx" 44 #include "globstr.hrc" 45 #include "global.hxx" 46 #include "dbdocfun.hxx" 47 #include "editable.hxx" 48 49 //================================================================== 50 51 ScDBFunc::ScDBFunc( Window* pParent, ScDocShell& rDocSh, ScTabViewShell* pViewShell ) : 52 ScViewFunc( pParent, rDocSh, pViewShell ) 53 { 54 } 55 56 //UNUSED2008-05 ScDBFunc::ScDBFunc( Window* pParent, const ScDBFunc& rDBFunc, ScTabViewShell* pViewShell ) : 57 //UNUSED2008-05 ScViewFunc( pParent, rDBFunc, pViewShell ) 58 //UNUSED2008-05 { 59 //UNUSED2008-05 } 60 61 ScDBFunc::~ScDBFunc() 62 { 63 } 64 65 // 66 // Hilfsfunktionen 67 // 68 69 void ScDBFunc::GotoDBArea( const String& rDBName ) 70 { 71 ScDocument* pDoc = GetViewData()->GetDocument(); 72 ScDBCollection* pDBCol = pDoc->GetDBCollection(); 73 74 sal_uInt16 nFoundAt = 0; 75 if ( pDBCol->SearchName( rDBName, nFoundAt ) ) 76 { 77 ScDBData* pData = (*pDBCol)[nFoundAt]; 78 DBG_ASSERT( pData, "GotoDBArea: Datenbankbereich nicht gefunden!" ); 79 80 if ( pData ) 81 { 82 SCTAB nTab = 0; 83 SCCOL nStartCol = 0; 84 SCROW nStartRow = 0; 85 SCCOL nEndCol = 0; 86 SCROW nEndRow = 0; 87 88 pData->GetArea( nTab, nStartCol, nStartRow, nEndCol, nEndRow ); 89 SetTabNo( nTab ); 90 91 MoveCursorAbs( nStartCol, nStartRow, ScFollowMode( SC_FOLLOW_JUMP ), 92 sal_False, sal_False ); // bShift,bControl 93 DoneBlockMode(); 94 InitBlockMode( nStartCol, nStartRow, nTab ); 95 MarkCursor( nEndCol, nEndRow, nTab ); 96 SelectionChanged(); 97 } 98 } 99 } 100 101 // aktuellen Datenbereich fuer Sortieren / Filtern suchen 102 103 ScDBData* ScDBFunc::GetDBData( sal_Bool bMark, ScGetDBMode eMode, ScGetDBSelection eSel ) 104 { 105 ScDocShell* pDocSh = GetViewData()->GetDocShell(); 106 ScDBData* pData = NULL; 107 ScRange aRange; 108 ScMarkType eMarkType = GetViewData()->GetSimpleArea(aRange); 109 if ( eMarkType == SC_MARK_SIMPLE || eMarkType == SC_MARK_SIMPLE_FILTERED ) 110 { 111 bool bShrinkColumnsOnly = false; 112 if (eSel == SC_DBSEL_ROW_DOWN) 113 { 114 // Don't alter row range, additional rows may have been selected on 115 // purpose to append data, or to have a fake header row. 116 bShrinkColumnsOnly = true; 117 // Select further rows only if only one row or a portion thereof is 118 // selected. 119 if (aRange.aStart.Row() != aRange.aEnd.Row()) 120 { 121 // If an area is selected shrink that to the actual used 122 // columns, don't draw filter buttons for empty columns. 123 eSel = SC_DBSEL_SHRINK_TO_USED_DATA; 124 } 125 else if (aRange.aStart.Col() == aRange.aEnd.Col()) 126 { 127 // One cell only, if it is not marked obtain entire used data 128 // area. 129 const ScMarkData& rMarkData = GetViewData()->GetMarkData(); 130 if (!(rMarkData.IsMarked() || rMarkData.IsMultiMarked())) 131 eSel = SC_DBSEL_KEEP; 132 } 133 } 134 switch (eSel) 135 { 136 case SC_DBSEL_SHRINK_TO_SHEET_DATA: 137 { 138 // Shrink the selection to sheet data area. 139 ScDocument* pDoc = pDocSh->GetDocument(); 140 SCCOL nCol1 = aRange.aStart.Col(), nCol2 = aRange.aEnd.Col(); 141 SCROW nRow1 = aRange.aStart.Row(), nRow2 = aRange.aEnd.Row(); 142 if (pDoc->ShrinkToDataArea( aRange.aStart.Tab(), nCol1, nRow1, nCol2, nRow2)) 143 { 144 aRange.aStart.SetCol(nCol1); 145 aRange.aEnd.SetCol(nCol2); 146 aRange.aStart.SetRow(nRow1); 147 aRange.aEnd.SetRow(nRow2); 148 } 149 } 150 break; 151 case SC_DBSEL_SHRINK_TO_USED_DATA: 152 case SC_DBSEL_ROW_DOWN: 153 { 154 // Shrink the selection to actual used area. 155 ScDocument* pDoc = pDocSh->GetDocument(); 156 SCCOL nCol1 = aRange.aStart.Col(), nCol2 = aRange.aEnd.Col(); 157 SCROW nRow1 = aRange.aStart.Row(), nRow2 = aRange.aEnd.Row(); 158 bool bShrunk; 159 pDoc->ShrinkToUsedDataArea( bShrunk, aRange.aStart.Tab(), 160 nCol1, nRow1, nCol2, nRow2, bShrinkColumnsOnly); 161 if (bShrunk) 162 { 163 aRange.aStart.SetCol(nCol1); 164 aRange.aEnd.SetCol(nCol2); 165 aRange.aStart.SetRow(nRow1); 166 aRange.aEnd.SetRow(nRow2); 167 } 168 } 169 break; 170 default: 171 ; // nothing 172 } 173 pData = pDocSh->GetDBData( aRange, eMode, eSel ); 174 } 175 else if ( eMode != SC_DB_OLD ) 176 pData = pDocSh->GetDBData( 177 ScRange( GetViewData()->GetCurX(), GetViewData()->GetCurY(), 178 GetViewData()->GetTabNo() ), 179 eMode, SC_DBSEL_KEEP ); 180 181 if ( pData && bMark ) 182 { 183 ScRange aFound; 184 pData->GetArea(aFound); 185 MarkRange( aFound, sal_False ); 186 } 187 return pData; 188 } 189 190 // Datenbankbereiche aendern (Dialog) 191 192 void ScDBFunc::NotifyCloseDbNameDlg( const ScDBCollection& rNewColl, const List& rDelAreaList ) 193 { 194 195 ScDocShell* pDocShell = GetViewData()->GetDocShell(); 196 ScDocShellModificator aModificator( *pDocShell ); 197 ScDocument* pDoc = pDocShell->GetDocument(); 198 ScDBCollection* pOldColl = pDoc->GetDBCollection(); 199 ScDBCollection* pUndoColl = NULL; 200 ScDBCollection* pRedoColl = NULL; 201 const sal_Bool bRecord (pDoc->IsUndoEnabled()); 202 203 long nDelCount = rDelAreaList.Count(); 204 for (long nDelPos=0; nDelPos<nDelCount; nDelPos++) 205 { 206 ScRange* pEntry = (ScRange*) rDelAreaList.GetObject(nDelPos); 207 208 if ( pEntry ) 209 { 210 ScAddress& rStart = pEntry->aStart; 211 ScAddress& rEnd = pEntry->aEnd; 212 pDocShell->DBAreaDeleted( rStart.Tab(), 213 rStart.Col(), rStart.Row(), 214 rEnd.Col(), rEnd.Row() ); 215 216 // Targets am SBA abmelden nicht mehr noetig 217 } 218 } 219 220 if (bRecord) 221 pUndoColl = new ScDBCollection( *pOldColl ); 222 223 // neue Targets am SBA anmelden nicht mehr noetig 224 225 pDoc->CompileDBFormula( sal_True ); // CreateFormulaString 226 pDoc->SetDBCollection( new ScDBCollection( rNewColl ) ); 227 pDoc->CompileDBFormula( sal_False ); // CompileFormulaString 228 pOldColl = NULL; 229 pDocShell->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID ); 230 aModificator.SetDocumentModified(); 231 SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_DBAREAS_CHANGED ) ); 232 233 if (bRecord) 234 { 235 pRedoColl = new ScDBCollection( rNewColl ); 236 pDocShell->GetUndoManager()->AddUndoAction( 237 new ScUndoDBData( pDocShell, pUndoColl, pRedoColl ) ); 238 } 239 } 240 241 // 242 // wirkliche Funktionen 243 // 244 245 // Sortieren 246 247 void ScDBFunc::UISort( const ScSortParam& rSortParam, sal_Bool bRecord ) 248 { 249 ScDocShell* pDocSh = GetViewData()->GetDocShell(); 250 ScDocument* pDoc = pDocSh->GetDocument(); 251 SCTAB nTab = GetViewData()->GetTabNo(); 252 ScDBData* pDBData = pDoc->GetDBAtArea( nTab, rSortParam.nCol1, rSortParam.nRow1, 253 rSortParam.nCol2, rSortParam.nRow2 ); 254 if (!pDBData) 255 { 256 DBG_ERROR( "Sort: keine DBData" ); 257 return; 258 } 259 260 ScSubTotalParam aSubTotalParam; 261 pDBData->GetSubTotalParam( aSubTotalParam ); 262 if (aSubTotalParam.bGroupActive[0] && !aSubTotalParam.bRemoveOnly) 263 { 264 // Subtotals wiederholen, mit neuer Sortierung 265 266 DoSubTotals( aSubTotalParam, bRecord, &rSortParam ); 267 } 268 else 269 { 270 Sort( rSortParam, bRecord ); // nur sortieren 271 } 272 } 273 274 void ScDBFunc::Sort( const ScSortParam& rSortParam, sal_Bool bRecord, sal_Bool bPaint ) 275 { 276 ScDocShell* pDocSh = GetViewData()->GetDocShell(); 277 SCTAB nTab = GetViewData()->GetTabNo(); 278 ScDBDocFunc aDBDocFunc( *pDocSh ); 279 sal_Bool bSuccess = aDBDocFunc.Sort( nTab, rSortParam, bRecord, bPaint, sal_False ); 280 if ( bSuccess && !rSortParam.bInplace ) 281 { 282 // Ziel markieren 283 ScRange aDestRange( rSortParam.nDestCol, rSortParam.nDestRow, rSortParam.nDestTab, 284 rSortParam.nDestCol + rSortParam.nCol2 - rSortParam.nCol1, 285 rSortParam.nDestRow + rSortParam.nRow2 - rSortParam.nRow1, 286 rSortParam.nDestTab ); 287 MarkRange( aDestRange ); 288 } 289 } 290 291 // Filtern 292 293 void ScDBFunc::Query( const ScQueryParam& rQueryParam, const ScRange* pAdvSource, sal_Bool bRecord ) 294 { 295 ScDocShell* pDocSh = GetViewData()->GetDocShell(); 296 SCTAB nTab = GetViewData()->GetTabNo(); 297 ScDBDocFunc aDBDocFunc( *pDocSh ); 298 sal_Bool bSuccess = aDBDocFunc.Query( nTab, rQueryParam, pAdvSource, bRecord, sal_False ); 299 300 if (bSuccess) 301 { 302 sal_Bool bCopy = !rQueryParam.bInplace; 303 if (bCopy) 304 { 305 // Zielbereich markieren (DB-Bereich wurde ggf. angelegt) 306 ScDocument* pDoc = pDocSh->GetDocument(); 307 ScDBData* pDestData = pDoc->GetDBAtCursor( 308 rQueryParam.nDestCol, rQueryParam.nDestRow, 309 rQueryParam.nDestTab, sal_True ); 310 if (pDestData) 311 { 312 ScRange aDestRange; 313 pDestData->GetArea(aDestRange); 314 MarkRange( aDestRange ); 315 } 316 } 317 318 if (!bCopy) 319 { 320 UpdateScrollBars(); 321 SelectionChanged(); // for attribute states (filtered rows are ignored) 322 } 323 324 GetViewData()->GetBindings().Invalidate( SID_UNFILTER ); 325 } 326 } 327 328 // Autofilter-Knoepfe ein-/ausblenden 329 330 void ScDBFunc::ToggleAutoFilter() 331 { 332 ScDocShell* pDocSh = GetViewData()->GetDocShell(); 333 ScDocShellModificator aModificator( *pDocSh ); 334 335 ScQueryParam aParam; 336 ScDocument* pDoc = GetViewData()->GetDocument(); 337 ScDBData* pDBData = GetDBData( sal_False, SC_DB_MAKE, SC_DBSEL_ROW_DOWN ); 338 339 pDBData->SetByRow( sal_True ); //! Undo, vorher abfragen ?? 340 pDBData->GetQueryParam( aParam ); 341 342 343 SCCOL nCol; 344 SCROW nRow = aParam.nRow1; 345 SCTAB nTab = GetViewData()->GetTabNo(); 346 sal_Int16 nFlag; 347 sal_Bool bHasAuto = sal_True; 348 sal_Bool bHeader = pDBData->HasHeader(); 349 sal_Bool bPaint = sal_False; 350 351 //! stattdessen aus DB-Bereich abfragen? 352 353 for (nCol=aParam.nCol1; nCol<=aParam.nCol2 && bHasAuto; nCol++) 354 { 355 nFlag = ((ScMergeFlagAttr*) pDoc-> 356 GetAttr( nCol, nRow, nTab, ATTR_MERGE_FLAG ))->GetValue(); 357 358 if ( (nFlag & SC_MF_AUTO) == 0 ) 359 bHasAuto = sal_False; 360 } 361 362 if (bHasAuto) // aufheben 363 { 364 // Filterknoepfe ausblenden 365 366 for (nCol=aParam.nCol1; nCol<=aParam.nCol2; nCol++) 367 { 368 nFlag = ((ScMergeFlagAttr*) pDoc-> 369 GetAttr( nCol, nRow, nTab, ATTR_MERGE_FLAG ))->GetValue(); 370 pDoc->ApplyAttr( nCol, nRow, nTab, ScMergeFlagAttr( nFlag & ~SC_MF_AUTO ) ); 371 } 372 373 // use a list action for the AutoFilter buttons (ScUndoAutoFilter) and the filter operation 374 375 String aUndo = ScGlobal::GetRscString( STR_UNDO_QUERY ); 376 pDocSh->GetUndoManager()->EnterListAction( aUndo, aUndo ); 377 378 ScRange aRange; 379 pDBData->GetArea( aRange ); 380 pDocSh->GetUndoManager()->AddUndoAction( 381 new ScUndoAutoFilter( pDocSh, aRange, pDBData->GetName(), sal_False ) ); 382 383 pDBData->SetAutoFilter(sal_False); 384 385 // Filter aufheben (incl. Paint / Undo) 386 387 SCSIZE nEC = aParam.GetEntryCount(); 388 for (SCSIZE i=0; i<nEC; i++) 389 aParam.GetEntry(i).bDoQuery = sal_False; 390 aParam.bDuplicate = sal_True; 391 Query( aParam, NULL, sal_True ); 392 393 pDocSh->GetUndoManager()->LeaveListAction(); 394 395 bPaint = sal_True; 396 } 397 else // Filterknoepfe einblenden 398 { 399 if ( !pDoc->IsBlockEmpty( nTab, 400 aParam.nCol1, aParam.nRow1, 401 aParam.nCol2, aParam.nRow2 ) ) 402 { 403 if (!bHeader) 404 { 405 if ( MessBox( GetViewData()->GetDialogParent(), WinBits(WB_YES_NO | WB_DEF_YES), 406 ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_0 ), // "StarCalc" 407 ScGlobal::GetRscString( STR_MSSG_MAKEAUTOFILTER_0 ) // Koepfe aus erster Zeile? 408 ).Execute() == RET_YES ) 409 { 410 pDBData->SetHeader( sal_True ); //! Undo ?? 411 bHeader = sal_True; 412 } 413 } 414 415 ScRange aRange; 416 pDBData->GetArea( aRange ); 417 pDocSh->GetUndoManager()->AddUndoAction( 418 new ScUndoAutoFilter( pDocSh, aRange, pDBData->GetName(), sal_True ) ); 419 420 pDBData->SetAutoFilter(sal_True); 421 422 for (nCol=aParam.nCol1; nCol<=aParam.nCol2; nCol++) 423 { 424 nFlag = ((ScMergeFlagAttr*) pDoc-> 425 GetAttr( nCol, nRow, nTab, ATTR_MERGE_FLAG ))->GetValue(); 426 pDoc->ApplyAttr( nCol, nRow, nTab, ScMergeFlagAttr( nFlag | SC_MF_AUTO ) ); 427 } 428 pDocSh->PostPaint( aParam.nCol1, nRow, nTab, aParam.nCol2, nRow, nTab, 429 PAINT_GRID ); 430 bPaint = sal_True; 431 } 432 else 433 { 434 ErrorBox aErrorBox( GetViewData()->GetDialogParent(), WinBits( WB_OK | WB_DEF_OK ), 435 ScGlobal::GetRscString( STR_ERR_AUTOFILTER ) ); 436 aErrorBox.Execute(); 437 } 438 } 439 440 if ( bPaint ) 441 { 442 aModificator.SetDocumentModified(); 443 444 SfxBindings& rBindings = GetViewData()->GetBindings(); 445 rBindings.Invalidate( SID_AUTO_FILTER ); 446 rBindings.Invalidate( SID_AUTOFILTER_HIDE ); 447 } 448 } 449 450 // nur ausblenden, keine Daten veraendern 451 452 void ScDBFunc::HideAutoFilter() 453 { 454 ScDocShell* pDocSh = GetViewData()->GetDocShell(); 455 ScDocShellModificator aModificator( *pDocSh ); 456 457 ScDocument* pDoc = pDocSh->GetDocument(); 458 459 ScQueryParam aParam; 460 ScDBData* pDBData = GetDBData( sal_False ); 461 462 SCTAB nTab; 463 SCCOL nCol1, nCol2; 464 SCROW nRow1, nRow2; 465 pDBData->GetArea(nTab, nCol1, nRow1, nCol2, nRow2); 466 467 for (SCCOL nCol=nCol1; nCol<=nCol2; nCol++) 468 { 469 sal_Int16 nFlag = ((ScMergeFlagAttr*) pDoc-> 470 GetAttr( nCol, nRow1, nTab, ATTR_MERGE_FLAG ))->GetValue(); 471 pDoc->ApplyAttr( nCol, nRow1, nTab, ScMergeFlagAttr( nFlag & ~SC_MF_AUTO ) ); 472 } 473 474 ScRange aRange; 475 pDBData->GetArea( aRange ); 476 pDocSh->GetUndoManager()->AddUndoAction( 477 new ScUndoAutoFilter( pDocSh, aRange, pDBData->GetName(), sal_False ) ); 478 479 pDBData->SetAutoFilter(sal_False); 480 481 pDocSh->PostPaint( nCol1,nRow1,nTab, nCol2,nRow1,nTab, PAINT_GRID ); 482 aModificator.SetDocumentModified(); 483 484 SfxBindings& rBindings = GetViewData()->GetBindings(); 485 rBindings.Invalidate( SID_AUTO_FILTER ); 486 rBindings.Invalidate( SID_AUTOFILTER_HIDE ); 487 } 488 489 // Re-Import 490 491 sal_Bool ScDBFunc::ImportData( const ScImportParam& rParam, sal_Bool bRecord ) 492 { 493 ScDocument* pDoc = GetViewData()->GetDocument(); 494 ScEditableTester aTester( pDoc, GetViewData()->GetTabNo(), rParam.nCol1,rParam.nRow1, 495 rParam.nCol2,rParam.nRow2 ); 496 if ( !aTester.IsEditable() ) 497 { 498 ErrorMessage(aTester.GetMessageId()); 499 return sal_False; 500 } 501 502 ScDBDocFunc aDBDocFunc( *GetViewData()->GetDocShell() ); 503 return aDBDocFunc.DoImport( GetViewData()->GetTabNo(), rParam, NULL, bRecord ); 504 } 505 506 507 508