tdf#142635: use the same ScDocument for clipboard and GlobalDrawPersist
[LibreOffice.git] / sc / source / ui / inc / docsh.hxx
blob95bd297efadbddd5b8ddc73ae3ea8e9d483dd0fc
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 #pragma once
22 #include <sfx2/objsh.hxx>
23 #include <sfx2/docfac.hxx>
24 #include <sfx2/sfxmodelfactory.hxx>
25 #include <sfx2/viewsh.hxx>
26 #include <o3tl/deleter.hxx>
27 #include <comphelper/servicehelper.hxx>
29 #include <scdllapi.h>
30 #include <document.hxx>
31 #include <shellids.hxx>
32 #include <optutil.hxx>
33 #include <docuno.hxx>
35 #include <memory>
36 #include <string_view>
37 #include <unordered_map>
38 #include <map>
40 class ScRefreshTimerProtector;
41 class ScEditEngineDefaulter;
42 class SfxStyleSheetBasePool;
43 class SfxStyleSheetHint;
44 class INetURLObject;
46 class ScViewData;
47 class ScDocFunc;
48 class ScDrawLayer;
49 class ScTabViewShell;
50 class ScAutoStyleList;
51 class ScMarkData;
52 class ScPaintLockData;
53 class ScChangeAction;
54 class ScImportOptions;
55 class ScDocShellModificator;
56 class ScOptSolverSave;
57 class ScSheetSaveData;
58 class ScFlatBoolRowSegments;
59 struct ScColWidthParam;
60 class ScFormulaOptions;
61 namespace com::sun::star::script::vba { class XVBAScriptListener; }
62 namespace ooo::vba::excel { class XWorkbook; }
63 namespace com::sun::star::datatransfer { class XTransferable2; }
64 namespace sfx2 { class FileDialogHelper; }
65 struct DocShell_Impl;
67 typedef std::unordered_map< sal_uLong, sal_uLong > ScChangeActionMergeMap;
69 //enum ScDBFormat { SC_FORMAT_SDF, SC_FORMAT_DBF };
71 enum class LOKCommentNotificationType { Add, Modify, Remove };
73 extern "C" SAL_DLLPUBLIC_EXPORT bool TestImportDBF(SvStream &rStream);
75 // Extra flags for Repaint
76 #define SC_PF_LINES 1
77 #define SC_PF_TESTMERGE 2
78 #define SC_PF_WHOLEROWS 4
80 class SC_DLLPUBLIC ScDocShell final: public SfxObjectShell, public SfxListener
82 std::shared_ptr<ScDocument> m_pDocument;
84 OUString m_aDdeTextFmt;
86 double m_nPrtToScreenFactor;
87 std::unique_ptr<DocShell_Impl> m_pImpl;
88 std::unique_ptr<ScDocFunc> m_pDocFunc;
90 bool m_bHeaderOn;
91 bool m_bFooterOn;
92 bool m_bIsInplace:1; // Is set by the View
93 bool m_bIsEmpty:1;
94 bool m_bIsInUndo:1;
95 bool m_bDocumentModifiedPending:1;
96 bool m_bUpdateEnabled:1;
97 bool m_bUcalcTest:1; // avoid loading the styles in the ucalc test
98 bool m_bAreasChangedNeedBroadcast:1;
99 sal_uInt16 m_nDocumentLock;
100 sal_Int16 m_nCanUpdate; // stores the UpdateDocMode from loading a document till update links
102 std::unique_ptr<ScDBData> m_pOldAutoDBRange;
104 std::unique_ptr<ScAutoStyleList> m_pAutoStyleList;
105 std::unique_ptr<ScPaintLockData> m_pPaintLockData;
106 std::unique_ptr<ScOptSolverSave> m_pSolverSaveData;
107 std::unique_ptr<ScSheetSaveData> m_pSheetSaveData;
108 std::unique_ptr<ScFormatSaveData> m_pFormatSaveData;
110 std::unique_ptr<ScDocShellModificator, o3tl::default_delete<ScDocShellModificator>> m_pModificator; // #109979#; is used to load XML (created in BeforeXMLLoading and destroyed in AfterXMLLoading)
112 css::uno::Reference< ooo::vba::excel::XWorkbook> mxAutomationWorkbookObject;
114 // Only used by Vba helper functions
115 css::uno::Reference<css::script::vba::XVBAScriptListener> m_xVBAListener;
116 css::uno::Reference<css::datatransfer::XTransferable2> m_xClipData;
118 SAL_DLLPRIVATE void InitItems();
119 SAL_DLLPRIVATE void DoEnterHandler();
120 SAL_DLLPRIVATE void InitOptions(bool bForLoading);
121 SAL_DLLPRIVATE void ResetDrawObjectShell();
123 /** Do things that need to be done before saving to our own format and
124 necessary clean ups in dtor. */
125 class SAL_DLLPRIVATE PrepareSaveGuard
127 public:
128 explicit PrepareSaveGuard( ScDocShell & rDocShell );
129 ~PrepareSaveGuard() COVERITY_NOEXCEPT_FALSE;
130 private:
131 ScDocShell & mrDocShell;
134 SAL_DLLPRIVATE bool LoadXML( SfxMedium* pMedium, const css::uno::Reference< css::embed::XStorage >& );
135 SAL_DLLPRIVATE bool SaveXML( SfxMedium* pMedium, const css::uno::Reference< css::embed::XStorage >& );
136 SAL_DLLPRIVATE SCTAB GetSaveTab();
138 friend bool TestImportDBF(SvStream &rStream);
140 SAL_DLLPRIVATE ErrCode DBaseImport( const OUString& rFullFileName, rtl_TextEncoding eCharSet,
141 std::map<SCCOL, ScColWidthParam>& aColWidthParam, ScFlatBoolRowSegments& rRowHeightsRecalc );
142 SAL_DLLPRIVATE ErrCode DBaseExport(
143 const OUString& rFullFileName, rtl_TextEncoding eCharSet, bool& bHasMemo );
145 SAL_DLLPRIVATE static bool MoveFile( const INetURLObject& rSource, const INetURLObject& rDest );
146 SAL_DLLPRIVATE static bool KillFile( const INetURLObject& rURL );
147 SAL_DLLPRIVATE static bool IsDocument( const INetURLObject& rURL );
149 SAL_DLLPRIVATE void LockPaint_Impl(bool bDoc);
150 SAL_DLLPRIVATE void UnlockPaint_Impl(bool bDoc);
151 SAL_DLLPRIVATE void LockDocument_Impl(sal_uInt16 nNew);
152 SAL_DLLPRIVATE void UnlockDocument_Impl(sal_uInt16 nNew);
154 SAL_DLLPRIVATE void EnableSharedSettings( bool bEnable );
155 SAL_DLLPRIVATE css::uno::Reference< css::frame::XModel > LoadSharedDocument();
157 SAL_DLLPRIVATE void UseSheetSaveEntries();
159 SAL_DLLPRIVATE std::unique_ptr<ScDocFunc> CreateDocFunc();
161 virtual void Notify( SfxBroadcaster& rBC, const SfxHint& rHint ) override;
163 public:
164 SFX_DECL_INTERFACE(SCID_DOC_SHELL)
165 SFX_DECL_OBJECTFACTORY();
167 private:
168 /// SfxInterface initializer.
169 static void InitInterface_Impl();
171 public:
172 explicit ScDocShell( const ScDocShell& rDocShell ) = delete;
173 explicit ScDocShell( const SfxModelFlags i_nSfxCreationFlags = SfxModelFlags::EMBEDDED_OBJECT, const std::shared_ptr<ScDocument>& pDoc = {} );
174 virtual ~ScDocShell() override;
176 virtual SfxUndoManager*
177 GetUndoManager() override;
179 virtual void FillClass( SvGlobalName * pClassName,
180 SotClipboardFormatId * pFormat,
181 OUString * pFullTypeName,
182 sal_Int32 nFileFormat,
183 bool bTemplate = false ) const override;
185 virtual std::set<Color> GetDocColors() override;
187 virtual bool InitNew( const css::uno::Reference< css::embed::XStorage >& ) override;
188 virtual bool Load( SfxMedium& rMedium ) override;
189 virtual bool LoadFrom( SfxMedium& rMedium ) override;
190 virtual bool ConvertFrom( SfxMedium &rMedium ) override;
191 virtual bool LoadExternal( SfxMedium& rMedium ) override;
192 virtual bool Save() override;
193 virtual bool SaveAs( SfxMedium& rMedium ) override;
194 virtual bool ConvertTo( SfxMedium &rMedium ) override;
195 virtual bool PrepareClose( bool bUI = true ) override;
196 virtual void LoadStyles( SfxObjectShell &rSource ) override;
198 virtual bool DoSaveCompleted( SfxMedium * pNewStor=nullptr, bool bRegisterRecent=true ) override; // SfxObjectShell
199 virtual bool QuerySlotExecutable( sal_uInt16 nSlotId ) override;
201 virtual void Draw( OutputDevice *, const JobSetup & rSetup, sal_uInt16 nAspect ) override;
203 virtual void SetVisArea( const tools::Rectangle & rVisArea ) override;
205 virtual void TerminateEditing() override;
207 using SfxObjectShell::GetVisArea;
208 virtual tools::Rectangle GetVisArea( sal_uInt16 nAspect ) const override;
210 virtual Printer* GetDocumentPrinter() override;
212 virtual void SetModified( bool = true ) override;
214 void SetVisAreaOrSize( const tools::Rectangle& rVisArea );
216 virtual std::shared_ptr<SfxDocumentInfoDialog> CreateDocumentInfoDialog(weld::Window* pParent, const SfxItemSet &rSet) override;
218 void GetDocStat( ScDocStat& rDocStat );
220 const ScDocument& GetDocument() const { return *m_pDocument; }
221 ScDocument& GetDocument() { return *m_pDocument; }
222 ScDocFunc& GetDocFunc() { return *m_pDocFunc; }
224 css::uno::Reference<css::datatransfer::XTransferable2> const & GetClipData() const { return m_xClipData; }
225 void SetClipData(const css::uno::Reference<css::datatransfer::XTransferable2>& xTransferable) { m_xClipData = xTransferable; }
227 SfxPrinter* GetPrinter( bool bCreateIfNotExist = true );
228 sal_uInt16 SetPrinter( VclPtr<SfxPrinter> const & pNewPrinter, SfxPrinterChangeFlags nDiffFlags = SFX_PRINTER_ALL );
230 void UpdateFontList();
232 ScDrawLayer* MakeDrawLayer();
234 void AsciiSave( SvStream& rStream, const ScImportOptions& rOpt, SCTAB nTab );
236 void Execute( SfxRequest& rReq );
237 void GetState( SfxItemSet &rSet );
238 void ExecutePageStyle ( const SfxViewShell& rCaller, SfxRequest& rReq, SCTAB nCurTab );
239 void GetStatePageStyle( SfxItemSet& rSet, SCTAB nCurTab );
241 void CompareDocument( ScDocument& rOtherDoc );
242 void MergeDocument( ScDocument& rOtherDoc, bool bShared = false, bool bCheckDuplicates = false, sal_uLong nOffset = 0, ScChangeActionMergeMap* pMergeMap = nullptr, bool bInverseMap = false );
243 bool MergeSharedDocument( ScDocShell* pSharedDocShell );
245 ScChangeAction* GetChangeAction( const ScAddress& rPos );
246 void SetChangeComment( ScChangeAction* pAction, const OUString& rComment );
247 void ExecuteChangeCommentDialog( ScChangeAction* pAction, weld::Window* pParent, bool bPrevNext = true );
248 /// Protect/unprotect ChangeTrack and return <TRUE/> if
249 /// protection was successfully changed.
250 /// If bJustQueryIfProtected==sal_True protection is not
251 /// changed and <TRUE/> is returned if not protected or
252 /// password was entered correctly.
253 bool ExecuteChangeProtectionDialog( bool bJustQueryIfProtected = false );
255 void SetPrintZoom( SCTAB nTab, sal_uInt16 nScale, sal_uInt16 nPages );
256 bool AdjustPrintZoom( const ScRange& rRange );
258 void LoadStylesArgs( ScDocShell& rSource, bool bReplace, bool bCellStyles, bool bPageStyles );
260 void PageStyleModified( std::u16string_view rStyleName, bool bApi );
262 void NotifyStyle( const SfxStyleSheetHint& rHint );
263 void DoAutoStyle( const ScRange& rRange, const OUString& rStyle );
265 static weld::Window* GetActiveDialogParent();
266 void ErrorMessage(TranslateId pGlobStrId);
267 bool IsEditable() const;
269 bool AdjustRowHeight( SCROW nStartRow, SCROW nEndRow, SCTAB nTab );
270 void UpdateAllRowHeights( const ScMarkData* pTabMark = nullptr );
271 void UpdatePendingRowHeights( SCTAB nUpdateTab, bool bBefore = false );
273 void RefreshPivotTables( const ScRange& rSource );
274 void DoConsolidate( const ScConsolidateParam& rParam, bool bRecord = true );
275 void UseScenario( SCTAB nTab, const OUString& rName, bool bRecord = true );
276 SCTAB MakeScenario(SCTAB nTab, const OUString& rName, const OUString& rComment,
277 const Color& rColor, ScScenarioFlags nFlags,
278 ScMarkData& rMark, bool bRecord = true);
279 void ModifyScenario(SCTAB nTab, const OUString& rName, const OUString& rComment,
280 const Color& rColor, ScScenarioFlags nFlags);
281 sal_uLong TransferTab( ScDocShell& rSrcDocShell, SCTAB nSrcPos,
282 SCTAB nDestPos, bool bInsertNew,
283 bool bNotifyAndPaint );
285 bool MoveTable( SCTAB nSrcTab, SCTAB nDestTab, bool bCopy, bool bRecord );
287 void DoRecalc( bool bApi );
288 void DoHardRecalc();
290 void UpdateOle(const ScViewData& rViewData, bool bSnapSize = false);
291 bool IsOle() const;
293 void DBAreaDeleted( SCTAB nTab, SCCOL nX1, SCROW nY1, SCCOL nX2 );
294 ScDBData* GetDBData( const ScRange& rMarked, ScGetDBMode eMode, ScGetDBSelection eSel );
295 ScDBData* GetAnonymousDBData(const ScRange& rRange);
296 std::unique_ptr<ScDBData> GetOldAutoDBRange();
297 void CancelAutoDBRange(); // called when dialog is cancelled
299 virtual void ReconnectDdeLink(SfxObjectShell& rServer) override;
300 void UpdateLinks() override;
301 void SetInitialLinkUpdate( const SfxMedium* pMedium );
302 void AllowLinkUpdate();
303 void ReloadAllLinks();
304 void ReloadTabLinks();
305 ScLkUpdMode GetLinkUpdateModeState() const;
307 void SetFormulaOptions( const ScFormulaOptions& rOpt, bool bForLoading = false );
309 * Called when the Options dialog is dismissed with the OK button, to
310 * handle potentially conflicting option settings.
312 void CheckConfigOptions();
314 void PostEditView( ScEditEngineDefaulter* pEditEngine, const ScAddress& rCursorPos );
316 void PostPaint( SCCOL nStartCol, SCROW nStartRow, SCTAB nStartTab,
317 SCCOL nEndCol, SCROW nEndRow, SCTAB nEndTab, PaintPartFlags nPart,
318 sal_uInt16 nExtFlags = 0 );
319 void PostPaint( const ScRangeList& rRanges, PaintPartFlags nPart, sal_uInt16 nExtFlags = 0 );
321 void PostPaintCell( SCCOL nCol, SCROW nRow, SCTAB nTab );
322 void PostPaintCell( const ScAddress& rPos );
323 void PostPaintGridAll();
324 void PostPaintExtras();
326 bool IsPaintLocked() const { return m_pPaintLockData != nullptr; }
328 void PostDataChanged();
330 void UpdatePaintExt( sal_uInt16& rExtFlags, SCCOL nStartCol, SCROW nStartRow, SCTAB nStartTab,
331 SCCOL nEndCol, SCROW nEndRow, SCTAB nEndTab );
332 void UpdatePaintExt( sal_uInt16& rExtFlags, const ScRange& rRange );
334 void SetDocumentModified();
335 void SetDrawModified();
337 void LockPaint();
338 void UnlockPaint();
339 sal_uInt16 GetLockCount() const { return m_nDocumentLock;}
340 void SetLockCount(sal_uInt16 nNew);
342 void LockDocument();
343 void UnlockDocument();
345 DECL_DLLPRIVATE_LINK( DialogClosedHdl, sfx2::FileDialogHelper*, void );
346 DECL_DLLPRIVATE_LINK( ReloadAllLinksHdl, weld::Button&, void );
348 virtual SfxStyleSheetBasePool* GetStyleSheetPool() override;
350 void SetInplace( bool bInplace );
351 bool IsEmpty() const { return m_bIsEmpty; }
352 void SetEmpty(bool bSet);
354 bool IsInUndo() const { return m_bIsInUndo; }
355 void SetInUndo(bool bSet);
357 void CalcOutputFactor();
358 double GetOutputFactor() const { return m_nPrtToScreenFactor;}
359 void GetPageOnFromPageStyleSet( const SfxItemSet* pStyleSet,
360 SCTAB nCurTab,
361 bool& rbHeader,
362 bool& rbFooter );
364 #if defined(_WIN32)
365 virtual bool DdeGetData( const OUString& rItem, const OUString& rMimeType,
366 css::uno::Any & rValue ) override;
367 virtual bool DdeSetData( const OUString& rItem, const OUString& rMimeType,
368 const css::uno::Any & rValue ) override;
369 #endif
371 virtual ::sfx2::SvLinkSource* DdeCreateLinkSource( const OUString& rItem ) override;
373 const OUString& GetDdeTextFmt() const { return m_aDdeTextFmt; }
375 SfxBindings* GetViewBindings();
377 ScTabViewShell* GetBestViewShell( bool bOnlyVisible = true );
379 void SetDocumentModifiedPending( bool bVal )
380 { m_bDocumentModifiedPending = bVal; }
381 bool IsDocumentModifiedPending() const
382 { return m_bDocumentModifiedPending; }
384 bool IsUpdateEnabled() const
385 { return m_bUpdateEnabled; }
386 void SetUpdateEnabled(bool bValue)
387 { m_bUpdateEnabled = bValue; }
389 void SetAreasChangedNeedBroadcast()
390 { m_bAreasChangedNeedBroadcast = true; }
392 OutputDevice* GetRefDevice(); // WYSIWYG: Printer, otherwise VirtualDevice...
394 static ScViewData* GetViewData();
395 static SCTAB GetCurTab();
397 static ScDocShell* GetShellByNum( sal_uInt16 nDocNo );
398 static OUString GetOwnFilterName();
399 static OUString GetHtmlFilterName();
400 static OUString GetWebQueryFilterName();
401 static OUString GetAsciiFilterName();
402 static OUString GetLotusFilterName();
403 static OUString GetDBaseFilterName();
404 static OUString GetDifFilterName();
405 static bool HasAutomaticTableName( std::u16string_view rFilter );
406 static void LOKCommentNotify(LOKCommentNotificationType nType, const ScDocument* pDocument, const ScAddress& rPos, const ScPostIt* pNote);
408 DECL_DLLPRIVATE_LINK( RefreshDBDataHdl, Timer*, void );
410 void BeforeXMLLoading();
411 void AfterXMLLoading(bool bRet);
413 virtual HiddenInformation GetHiddenInformationState( HiddenInformation nStates ) override;
415 const ScOptSolverSave* GetSolverSaveData() const { return m_pSolverSaveData.get(); } // may be null
416 void SetSolverSaveData( std::unique_ptr<ScOptSolverSave> pData );
417 ScSheetSaveData* GetSheetSaveData();
418 ScFormatSaveData* GetFormatSaveData();
420 static void ResetKeyBindings( ScOptionsUtil::KeyBindingType eType );
422 // password protection for Calc (derived from SfxObjectShell)
423 // see also: FID_CHG_RECORD, SID_CHG_PROTECT
424 virtual bool IsChangeRecording() const override;
425 virtual bool HasChangeRecordProtection() const override;
426 virtual void SetChangeRecording( bool bActivate, bool bLockAllViews = false ) override;
427 virtual void SetProtectionPassword( const OUString &rPassword ) override;
428 virtual bool GetProtectionHash( /*out*/ css::uno::Sequence< sal_Int8 > &rPasswordHash ) override;
430 void SnapVisArea( tools::Rectangle& rRect ) const;
432 void SetIsInUcalc();
434 void RegisterAutomationWorkbookObject(css::uno::Reference< ooo::vba::excel::XWorkbook > const& xWorkbook);
437 void UpdateAcceptChangesDialog();
439 typedef tools::SvRef<ScDocShell> ScDocShellRef;
441 /** Create before modifications of the document and destroy thereafter.
442 Call SetDocumentModified() at an instance of this class instead of at
443 ScDocShell.
445 Remembers in the ctor ScDocument's AutoCalcShellDisabled and IdleDisabled,
446 switches them off and restores them in the dtor, AutoCalcShellDisabled
447 also before a ScDocShell::SetDocumentModified() call if necessary.
448 In the dtor, if ScDocShell's bDocumentModifiedPending is set and
449 bAutoCalcShellDisabled is not set, then ScDocShell::SetDocumentModified()
450 is called.
452 Several instances can be used in nested calls to ScDocFunc or ScDocShell
453 methods to avoid multiple modified status changes, only the last instance
454 destroyed calls ScDocShell::SetDocumentModified().
456 class ScDocShellModificator
458 ScDocShell& rDocShell;
459 std::unique_ptr<ScRefreshTimerProtector> mpProtector;
460 bool bAutoCalcShellDisabled;
461 bool bIdleEnabled;
463 ScDocShellModificator( const ScDocShellModificator& ) = delete;
464 ScDocShellModificator& operator=( const ScDocShellModificator& ) = delete;
466 public:
467 explicit ScDocShellModificator( ScDocShell& );
468 ~ScDocShellModificator() COVERITY_NOEXCEPT_FALSE;
469 void SetDocumentModified();
472 //#i97876# Spreadsheet data changes are not notified
473 namespace HelperNotifyChanges
475 inline ScModelObj* getMustPropagateChangesModel(const ScDocShell &rDocShell)
477 ScModelObj* pModelObj = comphelper::getFromUnoTunnel<ScModelObj>(rDocShell.GetModel());
478 if (pModelObj && pModelObj->HasChangesListeners())
479 return pModelObj;
480 return nullptr;
483 inline void Notify(ScModelObj &rModelObj, const ScRangeList &rChangeRanges,
484 const OUString &rType = OUString("cell-change"),
485 const css::uno::Sequence< css::beans::PropertyValue >& rProperties =
486 css::uno::Sequence< css::beans::PropertyValue >())
488 rModelObj.NotifyChanges(rType, rChangeRanges, rProperties);
491 inline void NotifyIfChangesListeners(const ScDocShell &rDocShell, const ScRange &rRange,
492 const OUString &rType = OUString("cell-change"))
494 if (ScModelObj* pModelObj = getMustPropagateChangesModel(rDocShell))
496 ScRangeList aChangeRanges(rRange);
497 Notify(*pModelObj, aChangeRanges, rType);
502 void VBA_InsertModule( ScDocument& rDoc, SCTAB nTab, const OUString& sModuleSource );
504 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */