fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / docshell / docsh4.cxx
blob969c678e4ce166878dd237e87ed3748964d7a1dd
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 <config_features.h>
22 #include <com/sun/star/embed/XEmbeddedObject.hpp>
23 #include <com/sun/star/frame/Desktop.hpp>
24 #include <com/sun/star/frame/XComponentLoader.hpp>
26 using namespace ::com::sun::star;
28 #include <math.h>
30 #include "scitems.hxx"
31 #include <sfx2/fcontnr.hxx>
32 #include <sfx2/objface.hxx>
33 #include <sfx2/docfile.hxx>
34 #include <svtools/ehdl.hxx>
35 #include <basic/sbxcore.hxx>
36 #include <svtools/sfxecode.hxx>
37 #include <svx/ofaitem.hxx>
38 #include <svl/whiter.hxx>
39 #include <vcl/msgbox.hxx>
40 #include <vcl/waitobj.hxx>
41 #include <svx/dataaccessdescriptor.hxx>
42 #include <svx/drawitem.hxx>
43 #include <svx/fmshell.hxx>
44 #include <svtools/xwindowitem.hxx>
45 #include <sfx2/passwd.hxx>
46 #include <sfx2/filedlghelper.hxx>
47 #include <sfx2/dispatch.hxx>
48 #include <svl/PasswordHelper.hxx>
49 #include <svl/documentlockfile.hxx>
50 #include <svl/sharecontrolfile.hxx>
51 #include <unotools/securityoptions.hxx>
53 #include <comphelper/processfactory.hxx>
54 #include "docuno.hxx"
56 #include <com/sun/star/sdbc/XResultSet.hpp>
57 #include "docsh.hxx"
58 #include "docshimp.hxx"
59 #include "docfunc.hxx"
60 #include "sc.hrc"
61 #include "stlsheet.hxx"
62 #include "stlpool.hxx"
63 #include "appoptio.hxx"
64 #include "globstr.hrc"
65 #include "global.hxx"
66 #include "dbdocfun.hxx"
67 #include "printfun.hxx"
68 #include "viewdata.hxx"
69 #include "tabvwsh.hxx"
70 #include "impex.hxx"
71 #include "attrib.hxx"
72 #include "undodat.hxx"
73 #include "autostyl.hxx"
74 #include "undocell.hxx"
75 #include "undotab.hxx"
76 #include "inputhdl.hxx"
77 #include "dbdata.hxx"
78 #include "servobj.hxx"
79 #include "rangenam.hxx"
80 #include "scmod.hxx"
81 #include "chgviset.hxx"
82 #include "reffact.hxx"
83 #include "chartlis.hxx"
84 #include "chartpos.hxx"
85 #include "waitoff.hxx"
86 #include "tablink.hxx"
87 #include "drwlayer.hxx"
88 #include "docoptio.hxx"
89 #include "undostyl.hxx"
90 #include "rangeseq.hxx"
91 #include "chgtrack.hxx"
92 #include "printopt.hxx"
93 #include <com/sun/star/document/UpdateDocMode.hpp>
94 #include "scresid.hxx"
95 #include "scabstdlg.hxx"
96 #include "externalrefmgr.hxx"
97 #include "sharedocdlg.hxx"
98 #include "conditio.hxx"
99 #include "sheetevents.hxx"
100 #include <documentlinkmgr.hxx>
101 #include <boost/scoped_ptr.hpp>
103 #define IS_SHARE_HEADER(set) \
104 static_cast<const SfxBoolItem&>( \
105 static_cast<const SvxSetItem&>((set).Get(ATTR_PAGE_HEADERSET)).GetItemSet(). \
106 Get(ATTR_PAGE_SHARED)).GetValue()
108 #define IS_SHARE_FOOTER(set) \
109 static_cast<const SfxBoolItem&>( \
110 static_cast<const SvxSetItem&>((set).Get(ATTR_PAGE_FOOTERSET)).GetItemSet(). \
111 Get(ATTR_PAGE_SHARED)).GetValue()
113 void ScDocShell::Execute( SfxRequest& rReq )
115 // SID_SC_RANGE (Range),
116 // SID_SC_CELLTEXT (CellText),
117 // SID_SC_CELLS (Cells) - removed (old Basic)
119 const SfxItemSet* pReqArgs = rReq.GetArgs();
120 SfxBindings* pBindings = GetViewBindings();
121 bool bUndo (aDocument.IsUndoEnabled());
123 sal_uInt16 nSlot = rReq.GetSlot();
124 switch ( nSlot )
126 case SID_SC_SETTEXT:
128 const SfxPoolItem* pColItem;
129 const SfxPoolItem* pRowItem;
130 const SfxPoolItem* pTabItem;
131 const SfxPoolItem* pTextItem;
132 if( pReqArgs && pReqArgs->HasItem( FN_PARAM_1, &pColItem ) &&
133 pReqArgs->HasItem( FN_PARAM_2, &pRowItem ) &&
134 pReqArgs->HasItem( FN_PARAM_3, &pTabItem ) &&
135 pReqArgs->HasItem( SID_SC_SETTEXT, &pTextItem ) )
137 // Parameter sind 1-based !!!
138 SCCOL nCol = static_cast<const SfxInt16Item*>(pColItem)->GetValue() - 1;
139 SCROW nRow = static_cast<const SfxInt32Item*>(pRowItem)->GetValue() - 1;
140 SCTAB nTab = static_cast<const SfxInt16Item*>(pTabItem)->GetValue() - 1;
142 SCTAB nTabCount = aDocument.GetTableCount();
143 if ( ValidCol(nCol) && ValidRow(nRow) && ValidTab(nTab,nTabCount) )
145 if ( aDocument.IsBlockEditable( nTab, nCol,nRow, nCol, nRow ) )
147 OUString aVal = static_cast<const SfxStringItem*>(pTextItem)->GetValue();
148 aDocument.SetString( nCol, nRow, nTab, aVal );
150 PostPaintCell( nCol, nRow, nTab );
151 SetDocumentModified();
153 rReq.Done();
154 break;
156 else // geschuetzte Zelle
158 #if HAVE_FEATURE_SCRIPTING
159 SbxBase::SetError( SbxERR_BAD_PARAMETER ); //! welchen Fehler ?
160 #endif
161 break;
165 #if HAVE_FEATURE_SCRIPTING
166 SbxBase::SetError( SbxERR_NO_OBJECT );
167 #endif
169 break;
171 case SID_SBA_IMPORT:
173 if (pReqArgs)
175 const SfxPoolItem* pItem;
176 svx::ODataAccessDescriptor aDesc;
177 if ( pReqArgs->GetItemState( nSlot, true, &pItem ) == SfxItemState::SET )
179 uno::Any aAny = static_cast<const SfxUsrAnyItem*>(pItem)->GetValue();
180 uno::Sequence<beans::PropertyValue> aProperties;
181 if ( aAny >>= aProperties )
182 aDesc.initializeFrom( aProperties );
185 OUString sTarget;
186 if ( pReqArgs->GetItemState( FN_PARAM_1, true, &pItem ) == SfxItemState::SET )
187 sTarget = static_cast<const SfxStringItem*>(pItem)->GetValue();
189 bool bIsNewArea = true; // Default sal_True (keine Nachfrage)
190 if ( pReqArgs->GetItemState( FN_PARAM_2, true, &pItem ) == SfxItemState::SET )
191 bIsNewArea = static_cast<const SfxBoolItem*>(pItem)->GetValue();
193 // bei Bedarf neuen Datenbankbereich anlegen
194 bool bMakeArea = false;
195 if (bIsNewArea)
197 ScDBCollection* pDBColl = aDocument.GetDBCollection();
198 if ( !pDBColl || !pDBColl->getNamedDBs().findByUpperName(ScGlobal::pCharClass->uppercase(sTarget)) )
200 ScAddress aPos;
201 if ( aPos.Parse( sTarget, &aDocument, aDocument.GetAddressConvention() ) & SCA_VALID )
203 bMakeArea = true;
204 if (bUndo)
206 OUString aStrImport = ScGlobal::GetRscString( STR_UNDO_IMPORTDATA );
207 GetUndoManager()->EnterListAction( aStrImport, aStrImport );
210 ScDBData* pDBData = GetDBData( ScRange(aPos), SC_DB_IMPORT, SC_DBSEL_KEEP );
211 OSL_ENSURE(pDBData, "kann DB-Daten nicht anlegen");
212 sTarget = pDBData->GetName();
217 // nachfragen, bevor alter DB-Bereich ueberschrieben wird
218 bool bDo = true;
219 if (!bIsNewArea)
221 OUString aTemplate = ScGlobal::GetRscString( STR_IMPORT_REPLACE );
222 OUString aMessage = aTemplate.getToken( 0, '#' );
223 aMessage += sTarget;
224 aMessage += aTemplate.getToken( 1, '#' );
226 ScopedVclPtrInstance< QueryBox > aBox( nullptr, WinBits(WB_YES_NO | WB_DEF_YES), aMessage );
227 bDo = ( aBox->Execute() == RET_YES );
230 if (bDo)
232 ScDBDocFunc(*this).UpdateImport( sTarget, aDesc );
233 rReq.Done();
235 // UpdateImport aktualisiert auch die internen Operationen
237 else
238 rReq.Ignore();
240 if ( bMakeArea && bUndo)
241 GetUndoManager()->LeaveListAction();
243 else
245 OSL_FAIL( "arguments expected" );
248 break;
250 case SID_CHART_SOURCE:
251 case SID_CHART_ADDSOURCE:
252 if (pReqArgs)
254 ScDocument& rDoc = GetDocument();
255 const SfxPoolItem* pItem;
256 OUString aChartName, aRangeName;
258 ScRange aSingleRange;
259 ScRangeListRef aRangeListRef;
260 bool bMultiRange = false;
262 bool bColHeaders = true;
263 bool bRowHeaders = true;
264 bool bColInit = false;
265 bool bRowInit = false;
266 bool bAddRange = (nSlot == SID_CHART_ADDSOURCE);
268 if( pReqArgs->HasItem( SID_CHART_NAME, &pItem ) )
269 aChartName = static_cast<const SfxStringItem*>(pItem)->GetValue();
271 if( pReqArgs->HasItem( SID_CHART_SOURCE, &pItem ) )
272 aRangeName = static_cast<const SfxStringItem*>(pItem)->GetValue();
274 if( pReqArgs->HasItem( FN_PARAM_1, &pItem ) )
276 bColHeaders = static_cast<const SfxBoolItem*>(pItem)->GetValue();
277 bColInit = true;
279 if( pReqArgs->HasItem( FN_PARAM_2, &pItem ) )
281 bRowHeaders = static_cast<const SfxBoolItem*>(pItem)->GetValue();
282 bRowInit = true;
285 ScAddress::Details aDetails(rDoc.GetAddressConvention(), 0, 0);
286 bool bValid = ( aSingleRange.ParseAny( aRangeName, &rDoc, aDetails ) & SCA_VALID ) != 0;
287 if (!bValid)
289 aRangeListRef = new ScRangeList;
290 aRangeListRef->Parse( aRangeName, &rDoc );
291 if ( !aRangeListRef->empty() )
293 bMultiRange = true;
294 aSingleRange = *aRangeListRef->front(); // fuer Header
295 bValid = true;
297 else
298 aRangeListRef.Clear();
301 ScTabViewShell* pViewSh = ScTabViewShell::GetActiveViewShell();
302 if (pViewSh && bValid && !aChartName.isEmpty() )
304 vcl::Window* pParent = pViewSh->GetDialogParent();
306 SCCOL nCol1 = aSingleRange.aStart.Col();
307 SCROW nRow1 = aSingleRange.aStart.Row();
308 SCCOL nCol2 = aSingleRange.aEnd.Col();
309 SCROW nRow2 = aSingleRange.aEnd.Row();
310 SCTAB nTab = aSingleRange.aStart.Tab();
312 //! immer oder gar nicht begrenzen ???
313 if (!bMultiRange)
314 aDocument.LimitChartArea( nTab, nCol1,nRow1, nCol2,nRow2 );
316 // Dialog fuer Spalten/Zeilenkoepfe
317 bool bOk = true;
318 if ( !bAddRange && ( !bColInit || !bRowInit ) )
320 ScChartPositioner aChartPositioner( &aDocument, nTab, nCol1,nRow1, nCol2,nRow2 );
321 if (!bColInit)
322 bColHeaders = aChartPositioner.HasColHeaders();
323 if (!bRowInit)
324 bRowHeaders = aChartPositioner.HasRowHeaders();
326 ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
327 OSL_ENSURE(pFact, "ScAbstractFactory create fail!");
329 boost::scoped_ptr<AbstractScColRowLabelDlg> pDlg(pFact->CreateScColRowLabelDlg(pParent, bRowHeaders, bColHeaders));
330 OSL_ENSURE(pDlg, "Dialog create fail!");
331 if ( pDlg->Execute() == RET_OK )
333 bColHeaders = pDlg->IsRow();
334 bRowHeaders = pDlg->IsCol();
336 rReq.AppendItem(SfxBoolItem(FN_PARAM_1, bColHeaders));
337 rReq.AppendItem(SfxBoolItem(FN_PARAM_2, bRowHeaders));
339 else
340 bOk = false;
343 if (bOk) // ausfuehren
345 if (bMultiRange)
347 if (bUndo)
349 GetUndoManager()->AddUndoAction(
350 new ScUndoChartData( this, aChartName, aRangeListRef,
351 bColHeaders, bRowHeaders, bAddRange ) );
353 aDocument.UpdateChartArea( aChartName, aRangeListRef,
354 bColHeaders, bRowHeaders, bAddRange );
356 else
358 ScRange aNewRange( nCol1,nRow1,nTab, nCol2,nRow2,nTab );
359 if (bUndo)
361 GetUndoManager()->AddUndoAction(
362 new ScUndoChartData( this, aChartName, aNewRange,
363 bColHeaders, bRowHeaders, bAddRange ) );
365 aDocument.UpdateChartArea( aChartName, aNewRange,
366 bColHeaders, bRowHeaders, bAddRange );
370 else
372 OSL_FAIL("UpdateChartArea: keine ViewShell oder falsche Daten");
374 rReq.Done();
376 else
378 OSL_FAIL("SID_CHART_SOURCE ohne Argumente");
380 break;
382 case FID_AUTO_CALC:
384 bool bNewVal;
385 const SfxPoolItem* pItem;
386 if ( pReqArgs && SfxItemState::SET == pReqArgs->GetItemState( nSlot, true, &pItem ) )
387 bNewVal = static_cast<const SfxBoolItem*>(pItem)->GetValue();
388 else
389 bNewVal = !aDocument.GetAutoCalc(); // Toggle fuer Menue
390 aDocument.SetAutoCalc( bNewVal );
391 SetDocumentModified();
392 if (pBindings)
394 pBindings->Invalidate( FID_AUTO_CALC );
396 rReq.AppendItem( SfxBoolItem( FID_AUTO_CALC, bNewVal ) );
397 rReq.Done();
399 break;
400 case FID_RECALC:
401 DoRecalc( rReq.IsAPI() );
402 rReq.Done();
403 break;
404 case FID_HARD_RECALC:
405 DoHardRecalc( rReq.IsAPI() );
406 rReq.Done();
407 break;
408 case SID_UPDATETABLINKS:
410 ScDocument& rDoc = GetDocument();
412 ScLkUpdMode nSet = rDoc.GetLinkMode();
414 sal_uInt16 nDlgRet=RET_NO;
415 if(nSet==LM_UNKNOWN)
417 ScAppOptions aAppOptions=SC_MOD()->GetAppOptions();
418 nSet=aAppOptions.GetLinkMode();
421 if (nCanUpdate == com::sun::star::document::UpdateDocMode::NO_UPDATE)
422 nSet = LM_NEVER;
423 else if (nCanUpdate == com::sun::star::document::UpdateDocMode::FULL_UPDATE)
424 nSet = LM_ALWAYS;
426 if (nSet == LM_ALWAYS
427 && !(SvtSecurityOptions()
428 .isTrustedLocationUriForUpdatingLinks(
429 GetMedium() == nullptr
430 ? OUString() : GetMedium()->GetName())))
432 nSet = LM_ON_DEMAND;
434 if (nCanUpdate == css::document::UpdateDocMode::QUIET_UPDATE
435 && nSet == LM_ON_DEMAND)
437 nSet = LM_NEVER;
440 if(nSet==LM_ON_DEMAND)
442 ScopedVclPtrInstance<QueryBox> aBox( GetActiveDialogParent(), WinBits(WB_YES_NO | WB_DEF_YES),
443 ScGlobal::GetRscString(STR_RELOAD_TABLES) );
445 nDlgRet=aBox->Execute();
448 if (nDlgRet == RET_YES || nSet==LM_ALWAYS)
450 ReloadTabLinks();
451 aDocument.UpdateExternalRefLinks(GetActiveDialogParent());
453 bool bAny = aDocument.GetDocLinkManager().updateDdeLinks(GetActiveDialogParent());
455 if (bAny)
457 // Formeln berechnen und painten wie im TrackTimeHdl
458 aDocument.TrackFormulas();
459 Broadcast(SfxSimpleHint(FID_DATACHANGED));
461 // wenn FID_DATACHANGED irgendwann mal asynchron werden sollte
462 // (z.B. mit Invalidate am Window), muss hier ein Update erzwungen werden.
465 aDocument.UpdateAreaLinks();
467 //! Test, ob Fehler
468 rReq.Done();
470 else
471 rReq.Ignore();
473 break;
475 case SID_REIMPORT_AFTER_LOAD:
477 // wird nach dem Laden aufgerufen, wenn DB-Bereiche mit
478 // weggelassenen Daten enthalten sind
480 bool bDone = false;
481 ScDBCollection* pDBColl = aDocument.GetDBCollection();
483 if ((nCanUpdate != com::sun::star::document::UpdateDocMode::NO_UPDATE) &&
484 (nCanUpdate != com::sun::star::document::UpdateDocMode::QUIET_UPDATE))
486 ScRange aRange;
487 ScTabViewShell* pViewSh = GetBestViewShell();
488 OSL_ENSURE(pViewSh,"SID_REIMPORT_AFTER_LOAD: keine View");
489 if (pViewSh && pDBColl)
491 ScopedVclPtrInstance<QueryBox> aBox( GetActiveDialogParent(), WinBits(WB_YES_NO | WB_DEF_YES),
492 ScGlobal::GetRscString(STR_REIMPORT_AFTER_LOAD) );
493 if (aBox->Execute() == RET_YES)
495 ScDBCollection::NamedDBs& rDBs = pDBColl->getNamedDBs();
496 ScDBCollection::NamedDBs::iterator itr = rDBs.begin(), itrEnd = rDBs.end();
497 for (; itr != itrEnd; ++itr)
499 ScDBData& rDBData = *itr;
500 if ( rDBData.IsStripData() &&
501 rDBData.HasImportParam() && !rDBData.HasImportSelection() )
503 rDBData.GetArea(aRange);
504 pViewSh->MarkRange(aRange);
506 // Import und interne Operationen wie SID_REFRESH_DBAREA
507 // (Abfrage auf Import hier nicht noetig)
509 ScImportParam aImportParam;
510 rDBData.GetImportParam( aImportParam );
511 bool bContinue = pViewSh->ImportData( aImportParam );
512 rDBData.SetImportParam( aImportParam );
514 // markieren (Groesse kann sich geaendert haben)
515 rDBData.GetArea(aRange);
516 pViewSh->MarkRange(aRange);
518 if ( bContinue ) // Fehler beim Import -> Abbruch
520 // interne Operationen, wenn welche gespeichert
522 if ( rDBData.HasQueryParam() || rDBData.HasSortParam() ||
523 rDBData.HasSubTotalParam() )
524 pViewSh->RepeatDB();
526 // Pivottabellen die den Bereich als Quelldaten haben
528 RefreshPivotTables(aRange);
532 bDone = true;
537 if ( !bDone && pDBColl )
539 // wenn nicht, dann aber die abhaengigen Formeln updaten
540 //! auch fuer einzelne Bereiche, die nicht aktualisiert werden koennen
542 aDocument.CalcAll(); //! nur die abhaengigen
543 PostDataChanged();
546 if (bDone)
547 rReq.Done();
548 else
549 rReq.Ignore();
551 break;
553 case SID_AUTO_STYLE:
554 OSL_FAIL("use ScAutoStyleHint instead of SID_AUTO_STYLE");
555 break;
557 case SID_GET_COLORLIST:
559 const SvxColorListItem* pColItem = static_cast<const SvxColorListItem*>(GetItem(SID_COLOR_TABLE));
560 XColorListRef pList = pColItem->GetColorList();
561 rReq.SetReturnValue(OfaRefItem<XColorList>(SID_GET_COLORLIST, pList));
563 break;
565 case FID_CHG_RECORD:
567 ScDocument& rDoc = GetDocument();
568 // get argument (recorded macro)
569 SFX_REQUEST_ARG( rReq, pItem, SfxBoolItem, FID_CHG_RECORD, false );
570 bool bDo = true;
572 // xmlsec05/06:
573 // getting real parent window when called from Security-Options TP
574 vcl::Window* pParent = NULL;
575 const SfxPoolItem* pParentItem;
576 if( pReqArgs && SfxItemState::SET == pReqArgs->GetItemState( SID_ATTR_XWINDOW, false, &pParentItem ) )
577 pParent = static_cast<const XWindowItem*>( pParentItem )->GetWindowPtr();
579 // desired state
580 ScChangeTrack* pChangeTrack = rDoc.GetChangeTrack();
581 bool bActivateTracking = (pChangeTrack == 0); // toggle
582 if ( pItem )
583 bActivateTracking = pItem->GetValue(); // from argument
585 if ( !bActivateTracking )
587 if ( !pItem )
589 // no dialog on playing the macro
590 ScopedVclPtrInstance<WarningBox> aBox( pParent ? pParent : GetActiveDialogParent(),
591 WinBits(WB_YES_NO | WB_DEF_NO),
592 ScGlobal::GetRscString( STR_END_REDLINING ) );
593 bDo = ( aBox->Execute() == RET_YES );
596 if ( bDo )
598 if (pChangeTrack)
600 if ( pChangeTrack->IsProtected() )
601 bDo = ExecuteChangeProtectionDialog( NULL );
603 if ( bDo )
605 rDoc.EndChangeTracking();
606 PostPaintGridAll();
610 else
612 rDoc.StartChangeTracking();
613 ScChangeViewSettings aChangeViewSet;
614 aChangeViewSet.SetShowChanges(true);
615 rDoc.SetChangeViewSettings(aChangeViewSet);
618 if ( bDo )
620 UpdateAcceptChangesDialog();
622 // Slots invalidieren
623 if (pBindings)
624 pBindings->InvalidateAll(false);
625 if ( !pItem )
626 rReq.AppendItem( SfxBoolItem( FID_CHG_RECORD, bActivateTracking ) );
627 rReq.Done();
629 else
630 rReq.Ignore();
632 break;
634 case SID_CHG_PROTECT :
636 vcl::Window* pParent = NULL;
637 const SfxPoolItem* pParentItem;
638 if( pReqArgs && SfxItemState::SET == pReqArgs->GetItemState( SID_ATTR_XWINDOW, false, &pParentItem ) )
639 pParent = static_cast<const XWindowItem*>( pParentItem )->GetWindowPtr();
640 if ( ExecuteChangeProtectionDialog( pParent ) )
642 rReq.Done();
643 SetDocumentModified();
645 else
646 rReq.Ignore();
648 break;
650 case SID_DOCUMENT_MERGE:
651 case SID_DOCUMENT_COMPARE:
653 bool bDo = true;
654 ScChangeTrack* pChangeTrack = aDocument.GetChangeTrack();
655 if ( pChangeTrack && !pImpl->bIgnoreLostRedliningWarning )
657 if ( nSlot == SID_DOCUMENT_COMPARE )
658 { //! old changes trace will be lost
659 ScopedVclPtrInstance<WarningBox> aBox( GetActiveDialogParent(),
660 WinBits(WB_YES_NO | WB_DEF_NO),
661 ScGlobal::GetRscString( STR_END_REDLINING ) );
662 if( aBox->Execute() == RET_YES )
663 bDo = ExecuteChangeProtectionDialog( NULL, true );
664 else
665 bDo = false;
667 else // merge might reject some actions
668 bDo = ExecuteChangeProtectionDialog( NULL, true );
670 if ( !bDo )
672 rReq.Ignore();
673 break;
675 SfxApplication* pApp = SfxGetpApp();
676 const SfxPoolItem* pItem;
677 SfxMedium* pMed = NULL;
678 if ( pReqArgs &&
679 pReqArgs->GetItemState( SID_FILE_NAME, true, &pItem ) == SfxItemState::SET &&
680 pItem->ISA(SfxStringItem) )
682 OUString aFileName =
683 static_cast<const SfxStringItem*>(pItem)->GetValue();
685 OUString aFilterName;
686 if ( pReqArgs->GetItemState( SID_FILTER_NAME, true, &pItem ) == SfxItemState::SET &&
687 pItem->ISA(SfxStringItem) )
689 aFilterName = static_cast<const SfxStringItem*>(pItem)->GetValue();
691 OUString aOptions;
692 if ( pReqArgs->GetItemState( SID_FILE_FILTEROPTIONS, true, &pItem ) == SfxItemState::SET &&
693 pItem->ISA(SfxStringItem) )
695 aOptions = static_cast<const SfxStringItem*>(pItem)->GetValue();
697 short nVersion = 0;
698 if ( pReqArgs->GetItemState( SID_VERSION, true, &pItem ) == SfxItemState::SET &&
699 pItem->ISA(SfxInt16Item) )
701 nVersion = static_cast<const SfxInt16Item*>(pItem)->GetValue();
704 // kein Filter angegeben -> Detection
705 if (aFilterName.isEmpty())
706 ScDocumentLoader::GetFilterName( aFileName, aFilterName, aOptions, true, false );
708 // filter name from dialog contains application prefix,
709 // GetFilter needs name without the prefix.
710 ScDocumentLoader::RemoveAppPrefix( aFilterName );
712 const SfxFilter* pFilter = ScDocShell::Factory().GetFilterContainer()->GetFilter4FilterName( aFilterName );
713 SfxItemSet* pSet = new SfxAllItemSet( pApp->GetPool() );
714 if (!aOptions.isEmpty())
715 pSet->Put( SfxStringItem( SID_FILE_FILTEROPTIONS, aOptions ) );
716 if ( nVersion != 0 )
717 pSet->Put( SfxInt16Item( SID_VERSION, nVersion ) );
718 pMed = new SfxMedium( aFileName, STREAM_STD_READ, pFilter, pSet );
720 else
722 // start file dialog asynchronous
723 pImpl->bIgnoreLostRedliningWarning = true;
724 delete pImpl->pRequest;
725 pImpl->pRequest = new SfxRequest( rReq );
726 delete pImpl->pDocInserter;
727 pImpl->pDocInserter = new ::sfx2::DocumentInserter(
728 OUString::createFromAscii( ScDocShell::Factory().GetShortName() ), false );
729 pImpl->pDocInserter->StartExecuteModal( LINK( this, ScDocShell, DialogClosedHdl ) );
730 return ;
733 if ( pMed ) // nun wirklich ausfuehren...
735 SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, pMed->GetName() );
737 // pOtherDocSh->DoClose() will be called explicitly later, but it is still more safe to use SfxObjectShellLock here
738 ScDocShell* pOtherDocSh = new ScDocShell;
739 SfxObjectShellLock aDocShTablesRef = pOtherDocSh;
740 pOtherDocSh->DoLoad( pMed );
741 sal_uLong nErr = pOtherDocSh->GetErrorCode();
742 if (nErr)
743 ErrorHandler::HandleError( nErr ); // auch Warnings
745 if ( !pOtherDocSh->GetError() ) // nur Errors
747 bool bHadTrack = ( aDocument.GetChangeTrack() != NULL );
748 #if HAVE_FEATURE_MULTIUSER_ENVIRONMENT
749 sal_uLong nStart = 0;
750 if ( nSlot == SID_DOCUMENT_MERGE && pChangeTrack )
752 nStart = pChangeTrack->GetActionMax() + 1;
754 #endif
755 if ( nSlot == SID_DOCUMENT_COMPARE )
756 CompareDocument( pOtherDocSh->GetDocument() );
757 else
758 MergeDocument( pOtherDocSh->GetDocument() );
760 // show "accept changes" dialog
761 //! get view for this document!
762 if ( !IsDocShared() )
764 SfxViewFrame* pViewFrm = SfxViewFrame::Current();
765 if ( pViewFrm )
767 pViewFrm->ShowChildWindow( ScAcceptChgDlgWrapper::GetChildWindowId(), true ); //@51669
769 if ( pBindings )
771 pBindings->Invalidate( FID_CHG_ACCEPT );
775 rReq.SetReturnValue( SfxInt32Item( nSlot, 0 ) ); //! ???????
776 rReq.Done();
778 if (!bHadTrack) // neu eingeschaltet -> auch anzeigen
780 ScChangeViewSettings* pOldSet = aDocument.GetChangeViewSettings();
781 if ( !pOldSet || !pOldSet->ShowChanges() )
783 ScChangeViewSettings aChangeViewSet;
784 aChangeViewSet.SetShowChanges(true);
785 aDocument.SetChangeViewSettings(aChangeViewSet);
788 #if HAVE_FEATURE_MULTIUSER_ENVIRONMENT
789 else if ( nSlot == SID_DOCUMENT_MERGE && IsDocShared() && pChangeTrack )
791 sal_uLong nEnd = pChangeTrack->GetActionMax();
792 if ( nEnd >= nStart )
794 // only show changes from merged document
795 ScChangeViewSettings aChangeViewSet;
796 aChangeViewSet.SetShowChanges( true );
797 aChangeViewSet.SetShowAccepted( true );
798 aChangeViewSet.SetHasActionRange( true );
799 aChangeViewSet.SetTheActionRange( nStart, nEnd );
800 aDocument.SetChangeViewSettings( aChangeViewSet );
802 // update view
803 PostPaintExtras();
804 PostPaintGridAll();
807 #endif
809 pOtherDocSh->DoClose(); // delete passiert mit der Ref
812 break;
814 case SID_DELETE_SCENARIO:
815 if (pReqArgs)
817 const SfxPoolItem* pItem;
818 if ( pReqArgs->GetItemState( nSlot, true, &pItem ) == SfxItemState::SET )
820 if ( pItem->ISA(SfxStringItem) )
822 OUString aName = static_cast<const SfxStringItem*>(pItem)->GetValue();
823 SCTAB nTab;
824 if (aDocument.GetTable( aName, nTab ))
826 // DeleteTable von viewfunc nach docfunc verschieben!
828 ScTabViewShell* pSh = GetBestViewShell();
829 if ( pSh )
831 //! SetTabNo in DeleteTable weglassen?
832 SCTAB nDispTab = pSh->GetViewData().GetTabNo();
833 pSh->DeleteTable( nTab );
834 pSh->SetTabNo(nDispTab);
835 rReq.Done();
841 break;
843 case SID_EDIT_SCENARIO:
845 const SfxPoolItem* pItem;
846 if ( pReqArgs->GetItemState( nSlot, true, &pItem ) == SfxItemState::SET )
848 if ( pItem->ISA(SfxStringItem) )
850 OUString aName = static_cast<const SfxStringItem*>(pItem)->GetValue();
851 SCTAB nTab;
852 if (aDocument.GetTable( aName, nTab ))
854 if (aDocument.IsScenario(nTab))
856 OUString aComment;
857 Color aColor;
858 sal_uInt16 nFlags;
859 aDocument.GetScenarioData( nTab, aComment, aColor, nFlags );
861 // Determine if the Sheet that the Scenario was created on
862 // is protected. But first we need to find that Sheet.
863 // Rewind back to the actual sheet.
864 SCTAB nActualTab = nTab;
867 nActualTab--;
869 while(aDocument.IsScenario(nActualTab));
870 bool bSheetProtected = aDocument.IsTabProtected(nActualTab);
872 ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
873 OSL_ENSURE(pFact, "ScAbstractFactory create fail!");
875 boost::scoped_ptr<AbstractScNewScenarioDlg> pNewDlg(pFact->CreateScNewScenarioDlg(GetActiveDialogParent(), aName, true, bSheetProtected));
876 OSL_ENSURE(pNewDlg, "Dialog create fail!");
877 pNewDlg->SetScenarioData( aName, aComment, aColor, nFlags );
878 if ( pNewDlg->Execute() == RET_OK )
880 pNewDlg->GetScenarioData( aName, aComment, aColor, nFlags );
881 ModifyScenario( nTab, aName, aComment, aColor, nFlags );
882 rReq.Done();
889 break;
891 case SID_ATTR_YEAR2000 :
893 const SfxPoolItem* pItem;
894 if ( pReqArgs->GetItemState( nSlot, true, &pItem ) == SfxItemState::SET )
896 if ( pItem->ISA(SfxUInt16Item) )
898 sal_uInt16 nY2k = static_cast<const SfxUInt16Item*>(pItem)->GetValue();
899 // immer an den DocOptions setzen, damit das auch fuer SO50
900 // gespeichert wird (und alle Abfragen bisher auch darauf laufen).
901 // SetDocOptions propagiert das an den NumberFormatter
902 ScDocOptions aDocOpt( aDocument.GetDocOptions() );
903 aDocOpt.SetYear2000( nY2k );
904 aDocument.SetDocOptions( aDocOpt );
905 // die FormShell soll es mitbekommen
906 ScTabViewShell* pSh = GetBestViewShell();
907 if ( pSh )
909 FmFormShell* pFSh = pSh->GetFormShell();
910 if ( pFSh )
911 pFSh->SetY2KState( nY2k );
916 break;
918 #if HAVE_FEATURE_MULTIUSER_ENVIRONMENT
919 case SID_SHARE_DOC:
921 ScViewData* pViewData = GetViewData();
922 if ( !pViewData )
924 rReq.Ignore();
925 break;
928 ScopedVclPtrInstance< ScShareDocumentDlg > aDlg( GetActiveDialogParent(), pViewData );
929 if ( aDlg->Execute() == RET_OK )
931 bool bSetShared = aDlg->IsShareDocumentChecked();
932 if ( bSetShared != static_cast< bool >( IsDocShared() ) )
934 if ( bSetShared )
936 bool bContinue = true;
937 if ( HasName() )
939 ScopedVclPtrInstance<QueryBox> aBox(
940 GetActiveDialogParent(),
941 WinBits( WB_YES_NO | WB_DEF_YES ),
942 ScGlobal::GetRscString( STR_DOC_WILLBESAVED ) );
943 if ( aBox->Execute() == RET_NO )
945 bContinue = false;
948 if ( bContinue )
950 EnableSharedSettings( true );
952 SC_MOD()->SetInSharedDocSaving( true );
953 if ( !SwitchToShared( true, true ) )
955 // TODO/LATER: what should be done in case the switch has failed?
956 // for example in case the user has cancelled the saveAs operation
959 SC_MOD()->SetInSharedDocSaving( false );
961 InvalidateName();
962 GetUndoManager()->Clear();
964 ScTabView* pTabView = dynamic_cast< ScTabView* >( pViewData->GetView() );
965 if ( pTabView )
967 pTabView->UpdateLayerLocks();
971 else
973 uno::Reference< frame::XModel > xModel;
976 // load shared file
977 xModel.set( LoadSharedDocument(), uno::UNO_QUERY_THROW );
978 uno::Reference< util::XCloseable > xCloseable( xModel, uno::UNO_QUERY_THROW );
980 // check if shared flag is set in shared file
981 bool bShared = false;
982 ScModelObj* pDocObj = ScModelObj::getImplementation( xModel );
983 if ( pDocObj )
985 ScDocShell* pDocShell = dynamic_cast< ScDocShell* >( pDocObj->GetEmbeddedObject() );
986 if ( pDocShell )
988 bShared = pDocShell->HasSharedXMLFlagSet();
992 // #i87870# check if shared status was disabled and enabled again
993 bool bOwnEntry = false;
996 ::svt::ShareControlFile aControlFile( GetSharedFileURL() );
997 bOwnEntry = aControlFile.HasOwnEntry();
999 catch ( uno::Exception& )
1003 if ( bShared && bOwnEntry )
1005 uno::Reference< frame::XStorable > xStorable( xModel, uno::UNO_QUERY_THROW );
1006 if ( xStorable->isReadonly() )
1008 xCloseable->close( sal_True );
1010 OUString aUserName( ScGlobal::GetRscString( STR_UNKNOWN_USER ) );
1013 ::svt::DocumentLockFile aLockFile( GetSharedFileURL() );
1014 LockFileEntry aData = aLockFile.GetLockData();
1015 if ( !aData[LockFileComponent::OOOUSERNAME].isEmpty() )
1017 aUserName = aData[LockFileComponent::OOOUSERNAME];
1019 else if ( !aData[LockFileComponent::SYSUSERNAME].isEmpty() )
1021 aUserName = aData[LockFileComponent::SYSUSERNAME];
1024 catch ( uno::Exception& )
1027 OUString aMessage( ScGlobal::GetRscString( STR_FILE_LOCKED_TRY_LATER ) );
1028 aMessage = aMessage.replaceFirst( "%1", aUserName );
1030 ScopedVclPtrInstance< WarningBox > aBox( GetActiveDialogParent(), WinBits( WB_OK ), aMessage );
1031 aBox->Execute();
1033 else
1035 ScopedVclPtrInstance<WarningBox> aBox(
1036 GetActiveDialogParent(),
1037 WinBits( WB_YES_NO | WB_DEF_YES ),
1038 ScGlobal::GetRscString( STR_DOC_DISABLESHARED ) );
1039 if ( aBox->Execute() == RET_YES )
1041 xCloseable->close( sal_True );
1043 if ( !SwitchToShared( false, true ) )
1045 // TODO/LATER: what should be done in case the switch has failed?
1046 // for example in case the user has cancelled the saveAs operation
1049 EnableSharedSettings( false );
1051 if ( pBindings )
1053 pBindings->ExecuteSynchron( SID_SAVEDOC );
1056 ScTabView* pTabView = dynamic_cast< ScTabView* >( pViewData->GetView() );
1057 if ( pTabView )
1059 pTabView->UpdateLayerLocks();
1062 else
1064 xCloseable->close( sal_True );
1068 else
1070 xCloseable->close( sal_True );
1071 ScopedVclPtrInstance<WarningBox> aBox( GetActiveDialogParent(), WinBits( WB_OK ),
1072 ScGlobal::GetRscString( STR_DOC_NOLONGERSHARED ) );
1073 aBox->Execute();
1076 catch ( uno::Exception& )
1078 OSL_FAIL( "SID_SHARE_DOC: caught exception\n" );
1079 SC_MOD()->SetInSharedDocSaving( false );
1083 uno::Reference< util::XCloseable > xClose( xModel, uno::UNO_QUERY_THROW );
1084 xClose->close( sal_True );
1086 catch ( uno::Exception& )
1093 rReq.Done();
1095 break;
1096 #endif
1097 case SID_OPEN_CALC:
1099 SfxStringItem aApp(SID_DOC_SERVICE, OUString("com.sun.star.sheet.SpreadsheetDocument"));
1100 SfxStringItem aTarget(SID_TARGETNAME, OUString("_blank"));
1101 GetViewData()->GetDispatcher().Execute(
1102 SID_OPENDOC, SfxCallMode::API|SfxCallMode::SYNCHRON, &aApp, &aTarget, 0L);
1104 break;
1105 default:
1107 // kleiner (?) Hack -> forward der Slots an TabViewShell
1108 ScTabViewShell* pSh = GetBestViewShell();
1109 if ( pSh )
1110 pSh->Execute( rReq );
1111 #if HAVE_FEATURE_SCRIPTING
1112 else
1113 SbxBase::SetError( SbxERR_NO_ACTIVE_OBJECT );
1114 #endif
1119 void UpdateAcceptChangesDialog()
1121 // update "accept changes" dialog
1122 //! notify all views
1123 SfxViewFrame* pViewFrm = SfxViewFrame::Current();
1124 if ( pViewFrm && pViewFrm->HasChildWindow( FID_CHG_ACCEPT ) )
1126 SfxChildWindow* pChild = pViewFrm->GetChildWindow( FID_CHG_ACCEPT );
1127 if ( pChild )
1128 static_cast<ScAcceptChgDlgWrapper*>(pChild)->ReInitDlg();
1132 bool ScDocShell::ExecuteChangeProtectionDialog( vcl::Window* _pParent, bool bJustQueryIfProtected )
1134 bool bDone = false;
1135 ScChangeTrack* pChangeTrack = aDocument.GetChangeTrack();
1136 if ( pChangeTrack )
1138 bool bProtected = pChangeTrack->IsProtected();
1139 if ( bJustQueryIfProtected && !bProtected )
1140 return true;
1142 OUString aTitle( ScResId( bProtected ? SCSTR_CHG_UNPROTECT : SCSTR_CHG_PROTECT ) );
1143 OUString aText( ScResId( SCSTR_PASSWORD ) );
1144 OUString aPassword;
1146 ScopedVclPtrInstance<SfxPasswordDialog> pDlg(
1147 _pParent ? _pParent : GetActiveDialogParent(), &aText );
1148 pDlg->SetText( aTitle );
1149 pDlg->SetMinLen( 1 );
1150 pDlg->SetHelpId( GetStaticInterface()->GetSlot(SID_CHG_PROTECT)->GetCommand() );
1151 pDlg->SetEditHelpId( HID_CHG_PROTECT );
1152 if ( !bProtected )
1153 pDlg->ShowExtras( SfxShowExtras::CONFIRM );
1154 if ( pDlg->Execute() == RET_OK )
1155 aPassword = pDlg->GetPassword();
1156 pDlg.reset();
1158 if (!aPassword.isEmpty())
1160 if ( bProtected )
1162 if ( SvPasswordHelper::CompareHashPassword(pChangeTrack->GetProtection(), aPassword) )
1164 if ( bJustQueryIfProtected )
1165 bDone = true;
1166 else
1167 pChangeTrack->SetProtection(
1168 com::sun::star::uno::Sequence< sal_Int8 > (0) );
1170 else
1172 ScopedVclPtrInstance<InfoBox> aBox( GetActiveDialogParent(),
1173 OUString( ScResId( SCSTR_WRONGPASSWORD ) ) );
1174 aBox->Execute();
1177 else
1179 com::sun::star::uno::Sequence< sal_Int8 > aPass;
1180 SvPasswordHelper::GetHashPassword( aPass, aPassword );
1181 pChangeTrack->SetProtection( aPass );
1183 if ( bProtected != pChangeTrack->IsProtected() )
1185 UpdateAcceptChangesDialog();
1186 bDone = true;
1190 else if ( bJustQueryIfProtected )
1191 bDone = true;
1192 return bDone;
1195 void ScDocShell::DoRecalc( bool bApi )
1197 bool bDone = false;
1198 ScTabViewShell* pSh = GetBestViewShell();
1199 if ( pSh )
1201 ScInputHandler* pHdl = SC_MOD()->GetInputHdl(pSh);
1202 if ( pHdl && pHdl->IsInputMode() && pHdl->IsFormulaMode() && !bApi )
1204 pHdl->FormulaPreview(); // Teilergebnis als QuickHelp
1205 bDone = true;
1207 else
1209 ScTabView::UpdateInputLine(); // InputEnterHandler
1210 pSh->UpdateInputHandler();
1213 if (!bDone) // sonst Dokument neu berechnen
1215 WaitObject aWaitObj( GetActiveDialogParent() );
1216 aDocument.CalcFormulaTree();
1217 if ( pSh )
1218 pSh->UpdateCharts(true);
1220 aDocument.BroadcastUno( SfxSimpleHint( SFX_HINT_DATACHANGED ) );
1222 // Wenn es Charts gibt, dann alles painten, damit nicht
1223 // PostDataChanged und die Charts nacheinander kommen und Teile
1224 // doppelt gepainted werden.
1226 ScChartListenerCollection* pCharts = aDocument.GetChartListenerCollection();
1227 if ( pCharts && pCharts->hasListeners() )
1228 PostPaintGridAll();
1229 else
1230 PostDataChanged();
1234 void ScDocShell::DoHardRecalc( bool /* bApi */ )
1236 WaitObject aWaitObj( GetActiveDialogParent() );
1237 ScTabViewShell* pSh = GetBestViewShell();
1238 if ( pSh )
1240 ScTabView::UpdateInputLine(); // InputEnterHandler
1241 pSh->UpdateInputHandler();
1243 aDocument.CalcAll();
1244 GetDocFunc().DetectiveRefresh(); // erzeugt eigenes Undo
1245 if ( pSh )
1246 pSh->UpdateCharts(true);
1248 // set notification flags for "calculate" event (used in SFX_HINT_DATACHANGED broadcast)
1249 // (might check for the presence of any formulas on each sheet)
1250 SCTAB nTabCount = aDocument.GetTableCount();
1251 SCTAB nTab;
1252 if (aDocument.HasAnySheetEventScript( SC_SHEETEVENT_CALCULATE, true )) // search also for VBA hendler
1253 for (nTab=0; nTab<nTabCount; nTab++)
1254 aDocument.SetCalcNotification(nTab);
1256 // CalcAll doesn't broadcast value changes, so SC_HINT_CALCALL is broadcasted globally
1257 // in addition to SFX_HINT_DATACHANGED.
1258 aDocument.BroadcastUno( SfxSimpleHint( SC_HINT_CALCALL ) );
1259 aDocument.BroadcastUno( SfxSimpleHint( SFX_HINT_DATACHANGED ) );
1261 // use hard recalc also to disable stream-copying of all sheets
1262 // (somewhat consistent with charts)
1263 for (nTab=0; nTab<nTabCount; nTab++)
1264 if (aDocument.IsStreamValid(nTab))
1265 aDocument.SetStreamValid(nTab, false);
1267 PostPaintGridAll();
1270 void ScDocShell::DoAutoStyle( const ScRange& rRange, const OUString& rStyle )
1272 ScStyleSheetPool* pStylePool = aDocument.GetStyleSheetPool();
1273 ScStyleSheet* pStyleSheet =
1274 pStylePool->FindCaseIns( rStyle, SFX_STYLE_FAMILY_PARA );
1275 if (!pStyleSheet)
1276 pStyleSheet = static_cast<ScStyleSheet*>(
1277 pStylePool->Find( ScGlobal::GetRscString(STR_STYLENAME_STANDARD), SFX_STYLE_FAMILY_PARA ));
1278 if (pStyleSheet)
1280 OSL_ENSURE(rRange.aStart.Tab() == rRange.aEnd.Tab(),
1281 "DoAutoStyle mit mehreren Tabellen");
1282 SCTAB nTab = rRange.aStart.Tab();
1283 SCCOL nStartCol = rRange.aStart.Col();
1284 SCROW nStartRow = rRange.aStart.Row();
1285 SCCOL nEndCol = rRange.aEnd.Col();
1286 SCROW nEndRow = rRange.aEnd.Row();
1287 aDocument.ApplyStyleAreaTab( nStartCol, nStartRow, nEndCol, nEndRow, nTab, *pStyleSheet );
1288 aDocument.ExtendMerge( nStartCol, nStartRow, nEndCol, nEndRow, nTab );
1289 PostPaint( nStartCol, nStartRow, nTab, nEndCol, nEndRow, nTab, PAINT_GRID );
1293 void ScDocShell::NotifyStyle( const SfxStyleSheetHint& rHint )
1295 sal_uInt16 nId = rHint.GetHint();
1296 const SfxStyleSheetBase* pStyle = rHint.GetStyleSheet();
1297 if (!pStyle)
1298 return;
1300 if ( pStyle->GetFamily() == SFX_STYLE_FAMILY_PAGE )
1302 if ( nId == SfxStyleSheetHintId::MODIFIED )
1304 ScDocShellModificator aModificator( *this );
1306 OUString aNewName = pStyle->GetName();
1307 OUString aOldName = aNewName;
1308 const SfxStyleSheetHintExtended* pExtendedHint = dynamic_cast<const SfxStyleSheetHintExtended*>(&rHint); // Name geaendert?
1309 if (pExtendedHint)
1310 aOldName = pExtendedHint->GetOldName();
1312 if ( aNewName != aOldName )
1313 aDocument.RenamePageStyleInUse( aOldName, aNewName );
1315 SCTAB nTabCount = aDocument.GetTableCount();
1316 for (SCTAB nTab=0; nTab<nTabCount; nTab++)
1317 if (aDocument.GetPageStyle(nTab) == aNewName) // schon auf neu angepasst
1319 aDocument.PageStyleModified( nTab, aNewName );
1320 ScPrintFunc aPrintFunc( this, GetPrinter(), nTab );
1321 aPrintFunc.UpdatePages();
1324 aModificator.SetDocumentModified();
1326 if (pExtendedHint)
1328 SfxBindings* pBindings = GetViewBindings();
1329 if (pBindings)
1331 pBindings->Invalidate( SID_STATUS_PAGESTYLE );
1332 pBindings->Invalidate( SID_STYLE_FAMILY4 );
1333 pBindings->Invalidate( FID_RESET_PRINTZOOM );
1334 pBindings->Invalidate( SID_ATTR_PARA_LEFT_TO_RIGHT );
1335 pBindings->Invalidate( SID_ATTR_PARA_RIGHT_TO_LEFT );
1340 else if ( pStyle->GetFamily() == SFX_STYLE_FAMILY_PARA )
1342 if ( nId == SfxStyleSheetHintId::MODIFIED)
1344 OUString aNewName = pStyle->GetName();
1345 OUString aOldName = aNewName;
1346 const SfxStyleSheetHintExtended* pExtendedHint = dynamic_cast<const SfxStyleSheetHintExtended*>(&rHint);
1347 if (pExtendedHint)
1348 aOldName = pExtendedHint->GetOldName();
1349 if ( aNewName != aOldName )
1351 for(SCTAB i = 0; i < aDocument.GetTableCount(); ++i)
1353 ScConditionalFormatList* pList = aDocument.GetCondFormList(i);
1354 if (pList)
1355 pList->RenameCellStyle( aOldName,aNewName );
1361 // alles andere geht ueber Slots...
1364 // wie in printfun.cxx
1365 #define ZOOM_MIN 10
1367 void ScDocShell::SetPrintZoom( SCTAB nTab, sal_uInt16 nScale, sal_uInt16 nPages )
1369 bool bUndo(aDocument.IsUndoEnabled());
1370 OUString aStyleName = aDocument.GetPageStyle( nTab );
1371 ScStyleSheetPool* pStylePool = aDocument.GetStyleSheetPool();
1372 SfxStyleSheetBase* pStyleSheet = pStylePool->Find( aStyleName, SFX_STYLE_FAMILY_PAGE );
1373 OSL_ENSURE( pStyleSheet, "PageStyle not found" );
1374 if ( pStyleSheet )
1376 ScDocShellModificator aModificator( *this );
1378 SfxItemSet& rSet = pStyleSheet->GetItemSet();
1379 if (bUndo)
1381 sal_uInt16 nOldScale = static_cast<const SfxUInt16Item&>(rSet.Get(ATTR_PAGE_SCALE)).GetValue();
1382 sal_uInt16 nOldPages = static_cast<const SfxUInt16Item&>(rSet.Get(ATTR_PAGE_SCALETOPAGES)).GetValue();
1383 GetUndoManager()->AddUndoAction( new ScUndoPrintZoom(
1384 this, nTab, nOldScale, nOldPages, nScale, nPages ) );
1387 rSet.Put( SfxUInt16Item( ATTR_PAGE_SCALE, nScale ) );
1388 rSet.Put( SfxUInt16Item( ATTR_PAGE_SCALETOPAGES, nPages ) );
1390 ScPrintFunc aPrintFunc( this, GetPrinter(), nTab );
1391 aPrintFunc.UpdatePages();
1392 aModificator.SetDocumentModified();
1394 SfxBindings* pBindings = GetViewBindings();
1395 if (pBindings)
1396 pBindings->Invalidate( FID_RESET_PRINTZOOM );
1400 bool ScDocShell::AdjustPrintZoom( const ScRange& rRange )
1402 bool bChange = false;
1403 SCTAB nTab = rRange.aStart.Tab();
1405 OUString aStyleName = aDocument.GetPageStyle( nTab );
1406 ScStyleSheetPool* pStylePool = aDocument.GetStyleSheetPool();
1407 SfxStyleSheetBase* pStyleSheet = pStylePool->Find( aStyleName, SFX_STYLE_FAMILY_PAGE );
1408 OSL_ENSURE( pStyleSheet, "PageStyle not found" );
1409 if ( pStyleSheet )
1411 SfxItemSet& rSet = pStyleSheet->GetItemSet();
1412 bool bHeaders = static_cast<const SfxBoolItem&>(rSet.Get(ATTR_PAGE_HEADERS)).GetValue();
1413 sal_uInt16 nOldScale = static_cast<const SfxUInt16Item&>(rSet.Get(ATTR_PAGE_SCALE)).GetValue();
1414 sal_uInt16 nOldPages = static_cast<const SfxUInt16Item&>(rSet.Get(ATTR_PAGE_SCALETOPAGES)).GetValue();
1415 const ScRange* pRepeatCol = aDocument.GetRepeatColRange( nTab );
1416 const ScRange* pRepeatRow = aDocument.GetRepeatRowRange( nTab );
1418 // benoetigte Skalierung fuer Selektion ausrechnen
1420 sal_uInt16 nNewScale = nOldScale;
1422 long nBlkTwipsX = 0;
1423 if (bHeaders)
1424 nBlkTwipsX += (long) PRINT_HEADER_WIDTH;
1425 SCCOL nStartCol = rRange.aStart.Col();
1426 SCCOL nEndCol = rRange.aEnd.Col();
1427 if ( pRepeatCol && nStartCol >= pRepeatCol->aStart.Col() )
1429 for (SCCOL i=pRepeatCol->aStart.Col(); i<=pRepeatCol->aEnd.Col(); i++ )
1430 nBlkTwipsX += aDocument.GetColWidth( i, nTab );
1431 if ( nStartCol <= pRepeatCol->aEnd.Col() )
1432 nStartCol = pRepeatCol->aEnd.Col() + 1;
1434 // legacy compilers' own scope for i
1436 for ( SCCOL i=nStartCol; i<=nEndCol; i++ )
1437 nBlkTwipsX += aDocument.GetColWidth( i, nTab );
1440 long nBlkTwipsY = 0;
1441 if (bHeaders)
1442 nBlkTwipsY += (long) PRINT_HEADER_HEIGHT;
1443 SCROW nStartRow = rRange.aStart.Row();
1444 SCROW nEndRow = rRange.aEnd.Row();
1445 if ( pRepeatRow && nStartRow >= pRepeatRow->aStart.Row() )
1447 nBlkTwipsY += aDocument.GetRowHeight( pRepeatRow->aStart.Row(),
1448 pRepeatRow->aEnd.Row(), nTab );
1449 if ( nStartRow <= pRepeatRow->aEnd.Row() )
1450 nStartRow = pRepeatRow->aEnd.Row() + 1;
1452 nBlkTwipsY += aDocument.GetRowHeight( nStartRow, nEndRow, nTab );
1454 Size aPhysPage;
1455 long nHdr, nFtr;
1456 ScPrintFunc aOldPrFunc( this, GetPrinter(), nTab );
1457 aOldPrFunc.GetScaleData( aPhysPage, nHdr, nFtr );
1458 nBlkTwipsY += nHdr + nFtr;
1460 if ( nBlkTwipsX == 0 ) // hidden columns/rows may lead to 0
1461 nBlkTwipsX = 1;
1462 if ( nBlkTwipsY == 0 )
1463 nBlkTwipsY = 1;
1465 long nNeeded = std::min( aPhysPage.Width() * 100 / nBlkTwipsX,
1466 aPhysPage.Height() * 100 / nBlkTwipsY );
1467 if ( nNeeded < ZOOM_MIN )
1468 nNeeded = ZOOM_MIN; // Begrenzung
1469 if ( nNeeded < (long) nNewScale )
1470 nNewScale = (sal_uInt16) nNeeded;
1472 bChange = ( nNewScale != nOldScale || nOldPages != 0 );
1473 if ( bChange )
1474 SetPrintZoom( nTab, nNewScale, 0 );
1476 return bChange;
1479 void ScDocShell::PageStyleModified( const OUString& rStyleName, bool bApi )
1481 ScDocShellModificator aModificator( *this );
1483 SCTAB nTabCount = aDocument.GetTableCount();
1484 SCTAB nUseTab = MAXTAB+1;
1485 for (SCTAB nTab=0; nTab<nTabCount && nUseTab>MAXTAB; nTab++)
1486 if ( aDocument.GetPageStyle(nTab) == rStyleName &&
1487 ( !bApi || aDocument.GetPageSize(nTab).Width() ) )
1488 nUseTab = nTab;
1489 // bei bApi nur, wenn Umbrueche schon angezeigt
1491 if (ValidTab(nUseTab)) // nicht verwendet -> nichts zu tun
1493 bool bWarn = false;
1495 ScPrintFunc aPrintFunc( this, GetPrinter(), nUseTab ); //! ohne CountPages auskommen
1496 if (!aPrintFunc.UpdatePages()) // setzt Umbrueche auf allen Tabs
1497 bWarn = true;
1499 if (bWarn && !bApi)
1501 ScWaitCursorOff aWaitOff( GetActiveDialogParent() );
1502 ScopedVclPtrInstance<InfoBox> aInfoBox(GetActiveDialogParent(),
1503 ScGlobal::GetRscString(STR_PRINT_INVALID_AREA));
1504 aInfoBox->Execute();
1508 aModificator.SetDocumentModified();
1510 SfxBindings* pBindings = GetViewBindings();
1511 if (pBindings)
1513 pBindings->Invalidate( FID_RESET_PRINTZOOM );
1514 pBindings->Invalidate( SID_ATTR_PARA_LEFT_TO_RIGHT );
1515 pBindings->Invalidate( SID_ATTR_PARA_RIGHT_TO_LEFT );
1519 void ScDocShell::ExecutePageStyle( SfxViewShell& rCaller,
1520 SfxRequest& rReq,
1521 SCTAB nCurTab )
1523 const SfxItemSet* pReqArgs = rReq.GetArgs();
1525 switch ( rReq.GetSlot() )
1527 case SID_STATUS_PAGESTYLE: // Click auf StatusBar-Control
1528 case SID_FORMATPAGE:
1530 if ( pReqArgs != NULL )
1533 else if ( pReqArgs == NULL )
1535 bool bUndo(aDocument.IsUndoEnabled());
1536 OUString aOldName = aDocument.GetPageStyle( nCurTab );
1537 ScStyleSheetPool* pStylePool = aDocument.GetStyleSheetPool();
1538 SfxStyleSheetBase* pStyleSheet
1539 = pStylePool->Find( aOldName, SFX_STYLE_FAMILY_PAGE );
1541 OSL_ENSURE( pStyleSheet, "PageStyle not found! :-/" );
1543 if ( pStyleSheet )
1545 ScStyleSaveData aOldData;
1546 if (bUndo)
1547 aOldData.InitFromStyle( pStyleSheet );
1549 SfxItemSet& rStyleSet = pStyleSheet->GetItemSet();
1551 ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
1552 OSL_ENSURE(pFact, "ScAbstractFactory create fail!");
1554 boost::scoped_ptr<SfxAbstractTabDialog> pDlg(pFact->CreateScStyleDlg( GetActiveDialogParent(), *pStyleSheet, RID_SCDLG_STYLES_PAGE, RID_SCDLG_STYLES_PAGE ));
1555 OSL_ENSURE(pDlg, "Dialog create fail!");
1557 if ( pDlg->Execute() == RET_OK )
1559 const SfxItemSet* pOutSet = pDlg->GetOutputItemSet();
1561 WaitObject aWait( GetActiveDialogParent() );
1563 OUString aNewName = pStyleSheet->GetName();
1564 if ( aNewName != aOldName &&
1565 aDocument.RenamePageStyleInUse( aOldName, aNewName ) )
1567 SfxBindings* pBindings = GetViewBindings();
1568 if (pBindings)
1570 pBindings->Invalidate( SID_STATUS_PAGESTYLE );
1571 pBindings->Invalidate( FID_RESET_PRINTZOOM );
1575 if ( pOutSet )
1576 aDocument.ModifyStyleSheet( *pStyleSheet, *pOutSet );
1578 // merken fuer GetState():
1579 GetPageOnFromPageStyleSet( &rStyleSet, nCurTab, bHeaderOn, bFooterOn );
1580 rCaller.GetViewFrame()->GetBindings().Invalidate( SID_HFEDIT );
1582 ScStyleSaveData aNewData;
1583 aNewData.InitFromStyle( pStyleSheet );
1584 if (bUndo)
1586 GetUndoManager()->AddUndoAction(
1587 new ScUndoModifyStyle( this, SFX_STYLE_FAMILY_PAGE,
1588 aOldData, aNewData ) );
1591 PageStyleModified( aNewName, false );
1592 rReq.Done();
1594 pDlg.reset();
1596 rStyleSet.ClearItem( ATTR_PAGE_PAPERTRAY );
1600 break;
1602 case SID_HFEDIT:
1604 if ( pReqArgs != NULL )
1607 else if ( pReqArgs == NULL )
1609 OUString aStr( aDocument.GetPageStyle( nCurTab ) );
1611 ScStyleSheetPool* pStylePool
1612 = aDocument.GetStyleSheetPool();
1614 SfxStyleSheetBase* pStyleSheet
1615 = pStylePool->Find( aStr, SFX_STYLE_FAMILY_PAGE );
1617 OSL_ENSURE( pStyleSheet, "PageStyle not found! :-/" );
1619 if ( pStyleSheet )
1621 SfxItemSet& rStyleSet = pStyleSheet->GetItemSet();
1623 SvxPageUsage eUsage =
1624 SvxPageUsage( static_cast<const SvxPageItem&>(
1625 rStyleSet.Get( ATTR_PAGE )).
1626 GetPageUsage() );
1627 bool bShareHeader = IS_SHARE_HEADER(rStyleSet);
1628 bool bShareFooter = IS_SHARE_FOOTER(rStyleSet);
1629 sal_uInt16 nResId = 0;
1631 switch ( eUsage )
1633 case SVX_PAGE_LEFT:
1634 case SVX_PAGE_RIGHT:
1636 if ( bHeaderOn && bFooterOn )
1637 nResId = RID_SCDLG_HFEDIT;
1638 else if ( SVX_PAGE_RIGHT == eUsage )
1640 if ( !bHeaderOn && bFooterOn )
1641 nResId = RID_SCDLG_HFEDIT_RIGHTFOOTER;
1642 else if ( bHeaderOn && !bFooterOn )
1643 nResId = RID_SCDLG_HFEDIT_RIGHTHEADER;
1645 else
1647 // #69193a# respect "shared" setting
1648 if ( !bHeaderOn && bFooterOn )
1649 nResId = bShareFooter ?
1650 RID_SCDLG_HFEDIT_RIGHTFOOTER :
1651 RID_SCDLG_HFEDIT_LEFTFOOTER;
1652 else if ( bHeaderOn && !bFooterOn )
1653 nResId = bShareHeader ?
1654 RID_SCDLG_HFEDIT_RIGHTHEADER :
1655 RID_SCDLG_HFEDIT_LEFTHEADER;
1658 break;
1660 case SVX_PAGE_MIRROR:
1661 case SVX_PAGE_ALL:
1662 default:
1664 if ( !bShareHeader && !bShareFooter )
1666 if ( bHeaderOn && bFooterOn )
1667 nResId = RID_SCDLG_HFEDIT_ALL;
1668 else if ( !bHeaderOn && bFooterOn )
1669 nResId = RID_SCDLG_HFEDIT_FOOTER;
1670 else if ( bHeaderOn && !bFooterOn )
1671 nResId = RID_SCDLG_HFEDIT_HEADER;
1673 else if ( bShareHeader && bShareFooter )
1675 if ( bHeaderOn && bFooterOn )
1676 nResId = RID_SCDLG_HFEDIT;
1677 else
1679 if ( !bHeaderOn && bFooterOn )
1680 nResId = RID_SCDLG_HFEDIT_RIGHTFOOTER;
1681 else if ( bHeaderOn && !bFooterOn )
1682 nResId = RID_SCDLG_HFEDIT_RIGHTHEADER;
1685 else if ( !bShareHeader && bShareFooter )
1687 if ( bHeaderOn && bFooterOn )
1688 nResId = RID_SCDLG_HFEDIT_SFTR;
1689 else if ( !bHeaderOn && bFooterOn )
1690 nResId = RID_SCDLG_HFEDIT_RIGHTFOOTER;
1691 else if ( bHeaderOn && !bFooterOn )
1692 nResId = RID_SCDLG_HFEDIT_HEADER;
1694 else if ( bShareHeader && !bShareFooter )
1696 if ( bHeaderOn && bFooterOn )
1697 nResId = RID_SCDLG_HFEDIT_SHDR;
1698 else if ( !bHeaderOn && bFooterOn )
1699 nResId = RID_SCDLG_HFEDIT_FOOTER;
1700 else if ( bHeaderOn && !bFooterOn )
1701 nResId = RID_SCDLG_HFEDIT_RIGHTHEADER;
1706 ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
1707 OSL_ENSURE(pFact, "ScAbstractFactory create fail!");
1709 boost::scoped_ptr<SfxAbstractTabDialog> pDlg(pFact->CreateScHFEditDlg( SfxViewFrame::Current(),
1710 GetActiveDialogParent(),
1711 rStyleSet,
1712 aStr,
1713 nResId));
1714 OSL_ENSURE(pDlg, "Dialog create fail!");
1715 if ( pDlg->Execute() == RET_OK )
1717 const SfxItemSet* pOutSet = pDlg->GetOutputItemSet();
1719 if ( pOutSet )
1720 aDocument.ModifyStyleSheet( *pStyleSheet, *pOutSet );
1722 SetDocumentModified();
1723 rReq.Done();
1728 break;
1730 default:
1731 break;
1735 void ScDocShell::GetStatePageStyle( SfxViewShell& /* rCaller */,
1736 SfxItemSet& rSet,
1737 SCTAB nCurTab )
1739 SfxWhichIter aIter(rSet);
1740 sal_uInt16 nWhich = aIter.FirstWhich();
1741 while ( nWhich )
1743 switch (nWhich)
1745 case SID_STATUS_PAGESTYLE:
1746 rSet.Put( SfxStringItem( nWhich, aDocument.GetPageStyle( nCurTab ) ) );
1747 break;
1749 case SID_HFEDIT:
1751 OUString aStr = aDocument.GetPageStyle( nCurTab );
1752 ScStyleSheetPool* pStylePool = aDocument.GetStyleSheetPool();
1753 SfxStyleSheetBase* pStyleSheet = pStylePool->Find( aStr, SFX_STYLE_FAMILY_PAGE );
1755 OSL_ENSURE( pStyleSheet, "PageStyle not found! :-/" );
1757 if ( pStyleSheet )
1759 SfxItemSet& rStyleSet = pStyleSheet->GetItemSet();
1760 GetPageOnFromPageStyleSet( &rStyleSet, nCurTab, bHeaderOn, bFooterOn );
1762 if ( !bHeaderOn && !bFooterOn )
1763 rSet.DisableItem( nWhich );
1766 break;
1769 nWhich = aIter.NextWhich();
1773 void ScDocShell::GetState( SfxItemSet &rSet )
1775 bool bTabView = GetBestViewShell(true) != NULL;
1777 SfxWhichIter aIter(rSet);
1778 for (sal_uInt16 nWhich = aIter.FirstWhich(); nWhich; nWhich = aIter.NextWhich())
1780 if (!bTabView)
1782 rSet.DisableItem(nWhich);
1783 continue;
1786 switch (nWhich)
1788 case FID_AUTO_CALC:
1789 if ( aDocument.GetHardRecalcState() )
1790 rSet.DisableItem( nWhich );
1791 else
1792 rSet.Put( SfxBoolItem( nWhich, aDocument.GetAutoCalc() ) );
1793 break;
1795 case FID_CHG_RECORD:
1796 if ( IsDocShared() )
1797 rSet.DisableItem( nWhich );
1798 else
1799 rSet.Put( SfxBoolItem( nWhich,
1800 aDocument.GetChangeTrack() != NULL ) );
1801 break;
1803 case SID_CHG_PROTECT:
1805 ScChangeTrack* pChangeTrack = aDocument.GetChangeTrack();
1806 if ( pChangeTrack && !IsDocShared() )
1807 rSet.Put( SfxBoolItem( nWhich,
1808 pChangeTrack->IsProtected() ) );
1809 else
1810 rSet.DisableItem( nWhich );
1812 break;
1814 case SID_DOCUMENT_COMPARE:
1816 if ( IsDocShared() )
1818 rSet.DisableItem( nWhich );
1821 break;
1823 // Wenn eine Formel editiert wird, muss FID_RECALC auf jeden Fall enabled sein.
1824 // Recalc fuer das Doc war mal wegen eines Bugs disabled, wenn AutoCalc an war,
1825 // ist jetzt wegen eines anderen Bugs aber auch immer enabled.
1827 case SID_TABLES_COUNT:
1828 rSet.Put( SfxInt16Item( nWhich, aDocument.GetTableCount() ) );
1829 break;
1831 case SID_ATTR_YEAR2000 :
1832 rSet.Put( SfxUInt16Item( nWhich,
1833 aDocument.GetDocOptions().GetYear2000() ) );
1834 break;
1836 case SID_SHARE_DOC:
1838 if ( IsReadOnly() )
1840 rSet.DisableItem( nWhich );
1843 break;
1845 default:
1848 break;
1853 void ScDocShell::GetSbxState( SfxItemSet &rSet )
1855 // SID_SC_SELECTION (Selection),
1856 // SID_SC_ACTIVECELL (ActiveCell),
1857 // SID_SC_ACTIVETAB (ActiveTable),
1858 // SID_TABLES_GET (Tables),
1859 // SID_PIVOT_GET (DataPilotTables) - removed (old Basic)
1861 // Wenn hier Slots von der View-Shell executed werden, muss auch der
1862 // GetState weitergeleitet werden!
1864 ScTabViewShell* pVisibleSh = GetBestViewShell(); // sichtbare View
1865 if ( pVisibleSh )
1866 pVisibleSh->GetState( rSet );
1869 void ScDocShell::Draw( OutputDevice* pDev, const JobSetup & /* rSetup */, sal_uInt16 nAspect )
1872 SCTAB nVisTab = aDocument.GetVisibleTab();
1873 if (!aDocument.HasTable(nVisTab))
1874 return;
1876 ComplexTextLayoutMode nOldLayoutMode = pDev->GetLayoutMode();
1877 pDev->SetLayoutMode( TEXT_LAYOUT_DEFAULT ); // even if it's the same, to get the metafile action
1879 if ( nAspect == ASPECT_THUMBNAIL )
1881 Rectangle aBoundRect = GetVisArea( ASPECT_THUMBNAIL );
1882 ScViewData aTmpData( this, NULL );
1883 aTmpData.SetTabNo(nVisTab);
1884 SnapVisArea( aBoundRect );
1885 aTmpData.SetScreen( aBoundRect );
1886 ScPrintFunc::DrawToDev( &aDocument, pDev, 1.0, aBoundRect, &aTmpData, true );
1888 else
1890 Rectangle aBoundRect = SfxObjectShell::GetVisArea();
1891 ScViewData aTmpData( this, NULL );
1892 aTmpData.SetTabNo(nVisTab);
1893 SnapVisArea( aBoundRect );
1894 aTmpData.SetScreen( aBoundRect );
1895 ScPrintFunc::DrawToDev( &aDocument, pDev, 1.0, aBoundRect, &aTmpData, true );
1898 pDev->SetLayoutMode( nOldLayoutMode );
1901 Rectangle ScDocShell::GetVisArea( sal_uInt16 nAspect ) const
1903 SfxObjectCreateMode eShellMode = GetCreateMode();
1904 if ( eShellMode == SfxObjectCreateMode::ORGANIZER )
1906 // ohne Inhalte wissen wir auch nicht, wie gross die Inhalte sind
1907 // leeres Rechteck zurueckgeben, das wird dann nach dem Laden berechnet
1908 return Rectangle();
1911 if( nAspect == ASPECT_THUMBNAIL )
1913 SCTAB nVisTab = aDocument.GetVisibleTab();
1914 if (!aDocument.HasTable(nVisTab))
1916 nVisTab = 0;
1917 const_cast<ScDocShell*>(this)->aDocument.SetVisibleTab(nVisTab);
1919 Size aSize = aDocument.GetPageSize(nVisTab);
1920 const long SC_PREVIEW_SIZE_X = 10000;
1921 const long SC_PREVIEW_SIZE_Y = 12400;
1922 Rectangle aArea( 0,0, SC_PREVIEW_SIZE_X, SC_PREVIEW_SIZE_Y);
1923 if (aSize.Width() > aSize.Height())
1925 aArea.Right() = SC_PREVIEW_SIZE_Y;
1926 aArea.Bottom() = SC_PREVIEW_SIZE_X;
1929 bool bNegativePage = aDocument.IsNegativePage( aDocument.GetVisibleTab() );
1930 if ( bNegativePage )
1931 ScDrawLayer::MirrorRectRTL( aArea );
1932 SnapVisArea( aArea );
1933 return aArea;
1935 else if( nAspect == ASPECT_CONTENT && eShellMode != SfxObjectCreateMode::EMBEDDED )
1937 // Visarea holen wie nach Load
1939 SCTAB nVisTab = aDocument.GetVisibleTab();
1940 if (!aDocument.HasTable(nVisTab))
1942 nVisTab = 0;
1943 const_cast<ScDocShell*>(this)->aDocument.SetVisibleTab(nVisTab);
1945 SCCOL nStartCol;
1946 SCROW nStartRow;
1947 aDocument.GetDataStart( nVisTab, nStartCol, nStartRow );
1948 SCCOL nEndCol;
1949 SCROW nEndRow;
1950 aDocument.GetPrintArea( nVisTab, nEndCol, nEndRow );
1951 if (nStartCol>nEndCol)
1952 nStartCol = nEndCol;
1953 if (nStartRow>nEndRow)
1954 nStartRow = nEndRow;
1955 Rectangle aNewArea = ((ScDocument&)aDocument)
1956 .GetMMRect( nStartCol,nStartRow, nEndCol,nEndRow, nVisTab );
1957 //TODO/LATER: different methods for setting VisArea?!
1958 const_cast<ScDocShell*>(this)->SfxObjectShell::SetVisArea( aNewArea );
1959 return aNewArea;
1961 else
1962 return SfxObjectShell::GetVisArea( nAspect );
1965 namespace {
1967 void SnapHor( const ScDocument& rDoc, SCTAB nTab, long& rVal, SCCOL& rStartCol )
1969 SCCOL nCol = 0;
1970 long nTwips = (long) (rVal / HMM_PER_TWIPS);
1971 long nSnap = 0;
1972 while ( nCol<MAXCOL )
1974 long nAdd = rDoc.GetColWidth(nCol, nTab);
1975 if ( nSnap + nAdd/2 < nTwips || nCol < rStartCol )
1977 nSnap += nAdd;
1978 ++nCol;
1980 else
1981 break;
1983 rVal = (long) ( nSnap * HMM_PER_TWIPS );
1984 rStartCol = nCol;
1987 void SnapVer( const ScDocument& rDoc, SCTAB nTab, long& rVal, SCROW& rStartRow )
1989 SCROW nRow = 0;
1990 long nTwips = (long) (rVal / HMM_PER_TWIPS);
1991 long nSnap = 0;
1993 bool bFound = false;
1994 for (SCROW i = nRow; i <= MAXROW; ++i)
1996 SCROW nLastRow;
1997 if (rDoc.RowHidden(i, nTab, NULL, &nLastRow))
1999 i = nLastRow;
2000 continue;
2003 nRow = i;
2004 long nAdd = rDoc.GetRowHeight(i, nTab);
2005 if ( nSnap + nAdd/2 < nTwips || nRow < rStartRow )
2007 nSnap += nAdd;
2008 ++nRow;
2010 else
2012 bFound = true;
2013 break;
2016 if (!bFound)
2017 nRow = MAXROW; // all hidden down to the bottom
2019 rVal = (long) ( nSnap * HMM_PER_TWIPS );
2020 rStartRow = nRow;
2025 void ScDocShell::SnapVisArea( Rectangle& rRect ) const
2027 SCTAB nTab = aDocument.GetVisibleTab();
2028 bool bNegativePage = aDocument.IsNegativePage( nTab );
2029 if ( bNegativePage )
2030 ScDrawLayer::MirrorRectRTL( rRect ); // calculate with positive (LTR) values
2032 SCCOL nCol = 0;
2033 SnapHor( aDocument, nTab, rRect.Left(), nCol );
2034 ++nCol; // mindestens eine Spalte
2035 SnapHor( aDocument, nTab, rRect.Right(), nCol );
2037 SCROW nRow = 0;
2038 SnapVer( aDocument, nTab, rRect.Top(), nRow );
2039 ++nRow; // mindestens eine Zeile
2040 SnapVer( aDocument, nTab, rRect.Bottom(), nRow );
2042 if ( bNegativePage )
2043 ScDrawLayer::MirrorRectRTL( rRect ); // back to real rectangle
2046 void ScDocShell::GetPageOnFromPageStyleSet( const SfxItemSet* pStyleSet,
2047 SCTAB nCurTab,
2048 bool& rbHeader,
2049 bool& rbFooter )
2051 if ( !pStyleSet )
2053 ScStyleSheetPool* pStylePool = aDocument.GetStyleSheetPool();
2054 SfxStyleSheetBase* pStyleSheet = pStylePool->
2055 Find( aDocument.GetPageStyle( nCurTab ),
2056 SFX_STYLE_FAMILY_PAGE );
2058 OSL_ENSURE( pStyleSheet, "PageStyle not found! :-/" );
2060 if ( pStyleSheet )
2061 pStyleSet = &pStyleSheet->GetItemSet();
2062 else
2063 rbHeader = rbFooter = false;
2066 OSL_ENSURE( pStyleSet, "PageStyle-Set not found! :-(" );
2067 if (!pStyleSet)
2068 return;
2070 const SvxSetItem* pSetItem = NULL;
2071 const SfxItemSet* pSet = NULL;
2073 pSetItem = static_cast<const SvxSetItem*>( &pStyleSet->Get( ATTR_PAGE_HEADERSET ) );
2074 pSet = &pSetItem->GetItemSet();
2075 rbHeader = static_cast<const SfxBoolItem&>(pSet->Get(ATTR_PAGE_ON)).GetValue();
2077 pSetItem = static_cast<const SvxSetItem*>( &pStyleSet->Get( ATTR_PAGE_FOOTERSET ) );
2078 pSet = &pSetItem->GetItemSet();
2079 rbFooter = static_cast<const SfxBoolItem&>(pSet->Get(ATTR_PAGE_ON)).GetValue();
2082 #if defined WNT
2083 bool ScDocShell::DdeGetData( const OUString& rItem,
2084 const OUString& rMimeType,
2085 ::com::sun::star::uno::Any & rValue )
2087 if( SotClipboardFormatId::STRING == SotExchange::GetFormatIdFromMimeType( rMimeType ) )
2089 if( rItem.equalsIgnoreAsciiCase( "Format" ) )
2091 OString aFmtByte(OUStringToOString(aDdeTextFmt,
2092 osl_getThreadTextEncoding()));
2093 rValue <<= ::com::sun::star::uno::Sequence< sal_Int8 >(
2094 reinterpret_cast<const sal_Int8*>(aFmtByte.getStr()),
2095 aFmtByte.getLength() + 1 );
2096 return true;
2098 ScImportExport aObj( &aDocument, rItem );
2099 if ( !aObj.IsRef() )
2100 return false; // ungueltiger Bereich
2102 if( aDdeTextFmt[0] == 'F' )
2103 aObj.SetFormulas( true );
2104 if( aDdeTextFmt == "SYLK" ||
2105 aDdeTextFmt == "FSYLK" )
2107 OString aData;
2108 if( aObj.ExportByteString( aData, osl_getThreadTextEncoding(),
2109 SotClipboardFormatId::SYLK ) )
2111 rValue <<= ::com::sun::star::uno::Sequence< sal_Int8 >(
2112 reinterpret_cast<const sal_Int8*>(aData.getStr()),
2113 aData.getLength() + 1 );
2114 return true;
2116 else
2117 return false;
2119 if( aDdeTextFmt == "CSV" ||
2120 aDdeTextFmt == "FCSV" )
2121 aObj.SetSeparator( ',' );
2122 aObj.SetExportTextOptions( ScExportTextOptions( ScExportTextOptions::ToSpace, 0, false ) );
2123 return aObj.ExportData( rMimeType, rValue );
2126 ScImportExport aObj( &aDocument, rItem );
2127 aObj.SetExportTextOptions( ScExportTextOptions( ScExportTextOptions::ToSpace, 0, false ) );
2128 return aObj.IsRef() && aObj.ExportData( rMimeType, rValue );
2131 bool ScDocShell::DdeSetData( const OUString& rItem,
2132 const OUString& rMimeType,
2133 const ::com::sun::star::uno::Any & rValue )
2135 if( SotClipboardFormatId::STRING == SotExchange::GetFormatIdFromMimeType( rMimeType ))
2137 if( rItem.equalsIgnoreAsciiCase( "Format" ) )
2139 if ( ScByteSequenceToString::GetString( aDdeTextFmt, rValue, osl_getThreadTextEncoding() ) )
2141 aDdeTextFmt = aDdeTextFmt.toAsciiUpperCase();
2142 return true;
2144 return false;
2146 ScImportExport aObj( &aDocument, rItem );
2147 if( aDdeTextFmt[0] == 'F' )
2148 aObj.SetFormulas( true );
2149 if( aDdeTextFmt == "SYLK" ||
2150 aDdeTextFmt == "FSYLK" )
2152 OUString aData;
2153 if ( ScByteSequenceToString::GetString( aData, rValue, osl_getThreadTextEncoding() ) )
2155 return aObj.ImportString( aData, SotClipboardFormatId::SYLK );
2157 return false;
2159 if( aDdeTextFmt == "CSV" ||
2160 aDdeTextFmt == "FCSV" )
2161 aObj.SetSeparator( ',' );
2162 return aObj.ImportData( rMimeType, rValue );
2164 ScImportExport aObj( &aDocument, rItem );
2165 return aObj.IsRef() && aObj.ImportData( rMimeType, rValue );
2167 #endif
2169 ::sfx2::SvLinkSource* ScDocShell::DdeCreateLinkSource( const OUString& rItem )
2171 // only check for valid item string - range is parsed again in ScServerObject ctor
2173 // named range?
2174 OUString aPos = rItem;
2175 ScRangeName* pRange = aDocument.GetRangeName();
2176 if( pRange )
2178 const ScRangeData* pData = pRange->findByUpperName(ScGlobal::pCharClass->uppercase(aPos));
2179 if (pData)
2181 if( pData->HasType( RT_REFAREA )
2182 || pData->HasType( RT_ABSAREA )
2183 || pData->HasType( RT_ABSPOS ) )
2184 pData->GetSymbol( aPos ); // continue with the name's contents
2188 // Address in DDE function must be always parsed as CONV_OOO so that it
2189 // would always work regardless of current address convension. We do this
2190 // because the address item in a DDE entry is *not* normalized when saved
2191 // into ODF.
2192 ScRange aRange;
2193 bool bValid = ( (aRange.Parse(aPos, &aDocument, formula::FormulaGrammar::CONV_OOO ) & SCA_VALID) ||
2194 (aRange.aStart.Parse(aPos, &aDocument, formula::FormulaGrammar::CONV_OOO) & SCA_VALID) );
2196 ScServerObject* pObj = NULL; // NULL = error
2197 if ( bValid )
2198 pObj = new ScServerObject( this, rItem );
2200 // GetLinkManager()->InsertServer() is in the ScServerObject ctor
2202 return pObj;
2205 ScViewData* ScDocShell::GetViewData()
2207 SfxViewShell* pCur = SfxViewShell::Current();
2208 ScTabViewShell* pViewSh = PTR_CAST(ScTabViewShell,pCur);
2209 return pViewSh ? &pViewSh->GetViewData() : NULL;
2212 SCTAB ScDocShell::GetCurTab()
2214 //! this must be made non-static and use a ViewShell from this document!
2216 ScViewData* pViewData = GetViewData();
2218 return pViewData ? pViewData->GetTabNo() : static_cast<SCTAB>(0);
2221 ScTabViewShell* ScDocShell::GetBestViewShell( bool bOnlyVisible )
2223 ScTabViewShell* pViewSh = ScTabViewShell::GetActiveViewShell();
2224 // falsches Doc?
2225 if( pViewSh && pViewSh->GetViewData().GetDocShell() != this )
2226 pViewSh = NULL;
2227 if( !pViewSh )
2229 // 1. ViewShell suchen
2230 SfxViewFrame* pFrame = SfxViewFrame::GetFirst( this, bOnlyVisible );
2231 if( pFrame )
2233 SfxViewShell* p = pFrame->GetViewShell();
2234 pViewSh = PTR_CAST(ScTabViewShell,p);
2237 return pViewSh;
2240 SfxBindings* ScDocShell::GetViewBindings()
2242 // used to invalidate slots after changes to this document
2244 SfxViewShell* pViewSh = GetBestViewShell();
2245 if (pViewSh)
2246 return &pViewSh->GetViewFrame()->GetBindings();
2247 else
2248 return NULL;
2251 ScDocShell* ScDocShell::GetShellByNum( sal_uInt16 nDocNo ) // static
2253 ScDocShell* pFound = NULL;
2254 SfxObjectShell* pShell = SfxObjectShell::GetFirst();
2255 sal_uInt16 nShellCnt = 0;
2257 while ( pShell && !pFound )
2259 if ( pShell->Type() == TYPE(ScDocShell) )
2261 if ( nShellCnt == nDocNo )
2262 pFound = static_cast<ScDocShell*>(pShell);
2263 else
2264 ++nShellCnt;
2266 pShell = SfxObjectShell::GetNext( *pShell );
2269 return pFound;
2272 IMPL_LINK( ScDocShell, DialogClosedHdl, sfx2::FileDialogHelper*, _pFileDlg )
2274 OSL_ENSURE( _pFileDlg, "ScDocShell::DialogClosedHdl(): no file dialog" );
2275 OSL_ENSURE( pImpl->pDocInserter, "ScDocShell::DialogClosedHdl(): no document inserter" );
2277 if ( ERRCODE_NONE == _pFileDlg->GetError() )
2279 sal_uInt16 nSlot = pImpl->pRequest->GetSlot();
2280 SfxMedium* pMed = pImpl->pDocInserter->CreateMedium();
2281 // #i87094# If a .odt was selected pMed is NULL.
2282 if (pMed)
2284 pImpl->pRequest->AppendItem( SfxStringItem( SID_FILE_NAME, pMed->GetName() ) );
2285 if ( SID_DOCUMENT_COMPARE == nSlot )
2287 if ( pMed->GetFilter() )
2288 pImpl->pRequest->AppendItem(
2289 SfxStringItem( SID_FILTER_NAME, pMed->GetFilter()->GetFilterName() ) );
2290 OUString sOptions = ScDocumentLoader::GetOptions( *pMed );
2291 if ( !sOptions.isEmpty() )
2292 pImpl->pRequest->AppendItem( SfxStringItem( SID_FILE_FILTEROPTIONS, sOptions ) );
2294 const SfxPoolItem* pItem = NULL;
2295 SfxItemSet* pSet = pMed->GetItemSet();
2296 if ( pSet &&
2297 pSet->GetItemState( SID_VERSION, true, &pItem ) == SfxItemState::SET &&
2298 pItem->ISA( SfxInt16Item ) )
2300 pImpl->pRequest->AppendItem( *pItem );
2303 Execute( *(pImpl->pRequest) );
2307 pImpl->bIgnoreLostRedliningWarning = false;
2308 return 0;
2311 #if HAVE_FEATURE_MULTIUSER_ENVIRONMENT
2313 void ScDocShell::EnableSharedSettings( bool bEnable )
2315 SetDocumentModified();
2317 if ( bEnable )
2319 aDocument.EndChangeTracking();
2320 aDocument.StartChangeTracking();
2322 // hide accept or reject changes dialog
2323 sal_uInt16 nId = ScAcceptChgDlgWrapper::GetChildWindowId();
2324 SfxViewFrame* pViewFrame = SfxViewFrame::Current();
2325 if ( pViewFrame && pViewFrame->HasChildWindow( nId ) )
2327 pViewFrame->ToggleChildWindow( nId );
2328 SfxBindings* pBindings = GetViewBindings();
2329 if ( pBindings )
2331 pBindings->Invalidate( FID_CHG_ACCEPT );
2335 else
2337 aDocument.EndChangeTracking();
2340 ScChangeViewSettings aChangeViewSet;
2341 aChangeViewSet.SetShowChanges( false );
2342 aDocument.SetChangeViewSettings( aChangeViewSet );
2345 uno::Reference< frame::XModel > ScDocShell::LoadSharedDocument()
2347 uno::Reference< frame::XModel > xModel;
2350 SC_MOD()->SetInSharedDocLoading( true );
2351 uno::Reference< frame::XDesktop2 > xLoader = frame::Desktop::create( ::comphelper::getProcessComponentContext() );
2352 uno::Sequence < beans::PropertyValue > aArgs( 1 );
2353 aArgs[0].Name = "Hidden";
2354 aArgs[0].Value <<= sal_True;
2356 if ( GetMedium() )
2358 SFX_ITEMSET_ARG( GetMedium()->GetItemSet(), pPasswordItem, SfxStringItem, SID_PASSWORD, false);
2359 if ( pPasswordItem && !pPasswordItem->GetValue().isEmpty() )
2361 aArgs.realloc( 2 );
2362 aArgs[1].Name = "Password";
2363 aArgs[1].Value <<= pPasswordItem->GetValue();
2367 xModel.set(
2368 xLoader->loadComponentFromURL( GetSharedFileURL(), OUString( "_blank" ), 0, aArgs ),
2369 uno::UNO_QUERY_THROW );
2370 SC_MOD()->SetInSharedDocLoading( false );
2372 catch ( uno::Exception& )
2374 OSL_FAIL( "ScDocShell::LoadSharedDocument(): caught exception\n" );
2375 SC_MOD()->SetInSharedDocLoading( false );
2378 uno::Reference< util::XCloseable > xClose( xModel, uno::UNO_QUERY_THROW );
2379 xClose->close( sal_True );
2380 return uno::Reference< frame::XModel >();
2382 catch ( uno::Exception& )
2384 return uno::Reference< frame::XModel >();
2387 return xModel;
2390 #endif
2392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */