Update git submodules
[LibreOffice.git] / sd / source / ui / docshell / docshell.cxx
blobfd564bcc8f4ae992743923f76966fbfd60cddc94
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 <DrawDocShell.hxx>
22 #include <officecfg/Office/Common.hxx>
23 #include <unotools/configmgr.hxx>
25 #include <sfx2/docfac.hxx>
26 #include <sfx2/objface.hxx>
27 #include <sfx2/viewfrm.hxx>
28 #include <svx/svxids.hrc>
29 #include <svl/srchitem.hxx>
30 #include <svx/srchdlg.hxx>
31 #include <svx/svdoutl.hxx>
32 #include <svx/svditer.hxx>
33 #include <editeng/flstitem.hxx>
34 #include <editeng/eeitem.hxx>
35 #include <svl/eitem.hxx>
36 #include <svl/intitem.hxx>
37 #include <sfx2/printer.hxx>
38 #include <svx/drawitem.hxx>
39 #include <sfx2/dispatch.hxx>
40 #include <svl/whiter.hxx>
41 #include <svl/itempool.hxx>
42 #include <svl/stritem.hxx>
43 #include <svtools/ctrltool.hxx>
44 #include <svtools/langtab.hxx>
45 #include <comphelper/classids.hxx>
46 #include <svl/cjkoptions.hxx>
47 #include <svl/visitem.hxx>
49 #include <app.hrc>
50 #include <sdmod.hxx>
51 #include <View.hxx>
52 #include <drawdoc.hxx>
54 #include <ViewShell.hxx>
55 #include <unomodel.hxx>
56 #include <undo/undomanager.hxx>
57 #include <undo/undofactory.hxx>
58 #include <OutlineView.hxx>
59 #include <ViewShellBase.hxx>
60 #include <sfx2/notebookbar/SfxNotebookBar.hxx>
61 #include <comphelper/lok.hxx>
62 #include <DrawViewShell.hxx>
63 #include <sdpage.hxx>
64 #include <docmodel/theme/Theme.hxx>
66 using namespace sd;
67 #define ShellClass_DrawDocShell
68 #include <sdslots.hxx>
70 SFX_IMPL_SUPERCLASS_INTERFACE(DrawDocShell, SfxObjectShell);
72 void DrawDocShell::InitInterface_Impl()
74 GetStaticInterface()->RegisterChildWindow(SvxSearchDialogWrapper::GetChildWindowId());
77 namespace sd {
79 /**
80 * slotmaps and definitions of SFX
83 SFX_IMPL_OBJECTFACTORY(
84 DrawDocShell,
85 SvGlobalName(SO3_SIMPRESS_CLASSID),
86 "simpress" )
88 void DrawDocShell::Construct( bool bClipboard )
90 mbInDestruction = false;
91 SetSlotFilter(); // resets the filter
93 mbOwnDocument = mpDoc == nullptr;
94 if( mbOwnDocument )
95 mpDoc = new SdDrawDocument(meDocType, this);
97 // The document has been created so we can call UpdateRefDevice() to set
98 // the document's ref device.
99 UpdateRefDevice();
101 SetBaseModel( new SdXImpressDocument( this, bClipboard ) );
102 SetPool( &mpDoc->GetItemPool() );
103 std::unique_ptr<sd::UndoManager> pUndoManager(new sd::UndoManager);
104 pUndoManager->SetDocShell(this);
105 mpUndoManager = std::move(pUndoManager);
107 if (!utl::ConfigManager::IsFuzzing()
108 && officecfg::Office::Common::Undo::Steps::get() < 1)
110 mpUndoManager->EnableUndo(false); // tdf#108863 disable if 0 steps
112 mpDoc->SetSdrUndoManager( mpUndoManager.get() );
113 mpDoc->SetSdrUndoFactory( new sd::UndoFactory );
114 UpdateTablePointers();
115 SetStyleFamily(SfxStyleFamily::Pseudo);
118 DrawDocShell::DrawDocShell(SfxObjectCreateMode eMode,
119 bool bDataObject,
120 DocumentType eDocumentType) :
121 SfxObjectShell( eMode == SfxObjectCreateMode::INTERNAL ? SfxObjectCreateMode::EMBEDDED : eMode),
122 mpDoc(nullptr),
123 mpPrinter(nullptr),
124 mpViewShell(nullptr),
125 meDocType(eDocumentType),
126 mbSdDataObj(bDataObject),
127 mbOwnPrinter(false)
129 Construct( eMode == SfxObjectCreateMode::INTERNAL );
132 DrawDocShell::DrawDocShell( SfxModelFlags nModelCreationFlags, bool bDataObject, DocumentType eDocumentType ) :
133 SfxObjectShell( nModelCreationFlags ),
134 mpDoc(nullptr),
135 mpPrinter(nullptr),
136 mpViewShell(nullptr),
137 meDocType(eDocumentType),
138 mbSdDataObj(bDataObject),
139 mbOwnPrinter(false)
141 Construct( false );
144 DrawDocShell::DrawDocShell(SdDrawDocument* pDoc, SfxObjectCreateMode eMode,
145 bool bDataObject,
146 DocumentType eDocumentType) :
147 SfxObjectShell(eMode == SfxObjectCreateMode::INTERNAL ? SfxObjectCreateMode::EMBEDDED : eMode),
148 mpDoc(pDoc),
149 mpPrinter(nullptr),
150 mpViewShell(nullptr),
151 meDocType(eDocumentType),
152 mbSdDataObj(bDataObject),
153 mbOwnPrinter(false)
155 Construct( eMode == SfxObjectCreateMode::INTERNAL );
158 DrawDocShell::~DrawDocShell()
160 // Tell all listeners that the doc shell is about to be
161 // destroyed. This has been introduced for the PreviewRenderer to
162 // free its view (that uses the item poll of the doc shell) but
163 // may be useful in other places as well.
164 Broadcast(SfxHint(SfxHintId::Dying));
166 mbInDestruction = true;
168 if (mpViewShell)
170 auto* pView = mpViewShell->GetView();
171 if (pView)
173 auto & pSearchContext = pView->getSearchContext();
174 pSearchContext.resetSearchFunction();
178 mpFontList.reset();
180 if( mpDoc )
181 mpDoc->SetSdrUndoManager( nullptr );
182 mpUndoManager.reset();
184 if (mbOwnPrinter)
185 mpPrinter.disposeAndClear();
187 if( mbOwnDocument )
188 delete mpDoc;
190 // that the navigator get informed about the disappearance of the document
191 SfxBoolItem aItem(SID_NAVIGATOR_INIT, true);
192 SfxViewFrame* pFrame = mpViewShell ? mpViewShell->GetFrame() : GetFrame();
194 if( !pFrame )
195 pFrame = SfxViewFrame::GetFirst( this );
197 if( pFrame )
199 pFrame->GetDispatcher()->ExecuteList(
200 SID_NAVIGATOR_INIT, SfxCallMode::ASYNCHRON | SfxCallMode::RECORD,
201 { &aItem });
205 void DrawDocShell::GetState(SfxItemSet &rSet)
208 SfxWhichIter aIter( rSet );
209 sal_uInt16 nWhich = aIter.FirstWhich();
211 while ( nWhich )
213 sal_uInt16 nSlotId = SfxItemPool::IsWhich(nWhich)
214 ? GetPool().GetSlotId(nWhich)
215 : nWhich;
217 switch ( nSlotId )
219 case SID_ATTR_CHAR_FONTLIST:
220 rSet.Put( SvxFontListItem( mpFontList.get(), nSlotId ) );
221 break;
223 case SID_SEARCH_ITEM:
225 rSet.Put( *SD_MOD()->GetSearchItem() );
227 break;
229 case SID_CLOSEDOC:
230 GetSlotState(SID_CLOSEDOC, SfxObjectShell::GetInterface(), &rSet);
231 break;
233 case SID_SEARCH_OPTIONS:
235 SearchOptionFlags nOpt = SearchOptionFlags::SEARCH |
236 SearchOptionFlags::WHOLE_WORDS |
237 SearchOptionFlags::BACKWARDS |
238 SearchOptionFlags::REG_EXP |
239 SearchOptionFlags::EXACT |
240 SearchOptionFlags::SIMILARITY |
241 SearchOptionFlags::SELECTION;
243 if (!IsReadOnly())
245 nOpt |= SearchOptionFlags::REPLACE;
246 nOpt |= SearchOptionFlags::REPLACE_ALL;
249 rSet.Put(SfxUInt16Item(nWhich, static_cast<sal_uInt16>(nOpt)));
251 break;
253 case SID_VERSION:
255 GetSlotState( SID_VERSION, SfxObjectShell::GetInterface(), &rSet );
257 break;
259 case SID_CHINESE_CONVERSION:
260 case SID_HANGUL_HANJA_CONVERSION:
262 rSet.Put(SfxVisibilityItem(nWhich, SvtCJKOptions::IsAnyEnabled()));
264 break;
265 case SID_LANGUAGE_STATUS:
267 SdrObject* pObj = nullptr;
268 bool bLanguageFound = false;
269 OutlinerParaObject* pParaObj = nullptr;
270 LanguageType eLanguage( LANGUAGE_DONTKNOW );
271 sal_uInt16 nCount = mpDoc->GetPageCount();
272 for ( sal_uInt16 itPage = 0; itPage < nCount && !bLanguageFound; itPage++ )
274 SdrObjListIter aListIter(mpDoc->GetPage(itPage), SdrIterMode::DeepWithGroups);
275 while ( aListIter.IsMore() && !bLanguageFound )
277 pObj = aListIter.Next();
278 if ( pObj )
280 pParaObj = pObj->GetOutlinerParaObject();
281 if ( pParaObj )
283 SdrOutliner aOutliner(&mpDoc->GetPool(), OutlinerMode::TextObject);
284 aOutliner.SetText(*pParaObj);
285 eLanguage = aOutliner.GetLanguage(0, 0);
286 bLanguageFound = eLanguage != LANGUAGE_DONTKNOW;
292 if ( eLanguage == LANGUAGE_DONTKNOW )
294 eLanguage = mpDoc->GetLanguage( EE_CHAR_LANGUAGE );
297 OUString aLanguage = SvtLanguageTable::GetLanguageString(eLanguage);
298 if (comphelper::LibreOfficeKit::isActive())
300 if (eLanguage == LANGUAGE_DONTKNOW)
302 aLanguage += ";-";
304 else
306 aLanguage += ";" + LanguageTag(eLanguage).getBcp47(false);
309 rSet.Put(SfxStringItem(nWhich, aLanguage));
311 break;
313 case SID_NOTEBOOKBAR:
315 if (mpViewShell)
317 bool bImpress = mpDoc->GetDocumentType() == DocumentType::Impress;
318 bool bVisible = false;
319 if(bImpress)
321 bVisible = sfx2::SfxNotebookBar::StateMethod(mpViewShell->GetFrame()->GetBindings(),
322 u"modules/simpress/ui/");
324 else
326 bVisible = sfx2::SfxNotebookBar::StateMethod(mpViewShell->GetFrame()->GetBindings(),
327 u"modules/sdraw/ui/");
329 rSet.Put( SfxBoolItem( SID_NOTEBOOKBAR, bVisible ) );
332 break;
334 default:
335 break;
337 nWhich = aIter.NextWhich();
340 if (SfxViewFrame* pFrame = SfxViewFrame::Current())
342 if (rSet.GetItemState(SID_RELOAD) != SfxItemState::UNKNOWN)
344 pFrame->GetSlotState(SID_RELOAD,
345 pFrame->GetInterface(), &rSet);
350 void DrawDocShell::Activate( bool bMDI)
352 if (bMDI)
354 ApplySlotFilter();
355 mpDoc->StartOnlineSpelling();
359 void DrawDocShell::Deactivate( bool )
363 SfxUndoManager* DrawDocShell::GetUndoManager()
365 return mpUndoManager.get();
368 void DrawDocShell::UpdateTablePointers()
370 PutItem( SvxColorListItem( mpDoc->GetColorList(), SID_COLOR_TABLE ) );
371 PutItem( SvxGradientListItem( mpDoc->GetGradientList(), SID_GRADIENT_LIST ) );
372 PutItem( SvxHatchListItem( mpDoc->GetHatchList(), SID_HATCH_LIST ) );
373 PutItem( SvxBitmapListItem( mpDoc->GetBitmapList(), SID_BITMAP_LIST ) );
374 PutItem( SvxPatternListItem( mpDoc->GetPatternList(), SID_PATTERN_LIST ) );
375 PutItem( SvxDashListItem( mpDoc->GetDashList(), SID_DASH_LIST ) );
376 PutItem( SvxLineEndListItem( mpDoc->GetLineEndList(), SID_LINEEND_LIST ) );
378 UpdateFontList();
381 void DrawDocShell::CancelSearching()
383 if (mpViewShell)
385 auto* pView = mpViewShell->GetView();
386 if (pView)
388 auto & pSearchContext = pView->getSearchContext();
389 pSearchContext.resetSearchFunction();
395 * apply configured slot filters
397 void DrawDocShell::ApplySlotFilter() const
399 SfxViewShell* pTestViewShell = SfxViewShell::GetFirst();
401 while( pTestViewShell )
403 if( pTestViewShell->GetObjectShell() == this
404 && pTestViewShell->GetViewFrame().GetDispatcher() )
406 SfxDispatcher* pDispatcher = pTestViewShell->GetViewFrame().GetDispatcher();
408 if( !mpFilterSIDs.empty() )
409 pDispatcher->SetSlotFilter( mbFilterEnable ? SfxSlotFilterState::ENABLED : SfxSlotFilterState::DISABLED, mpFilterSIDs );
410 else
411 pDispatcher->SetSlotFilter();
413 if( pDispatcher->GetBindings() )
414 pDispatcher->GetBindings()->InvalidateAll( true );
417 pTestViewShell = SfxViewShell::GetNext( *pTestViewShell );
421 void DrawDocShell::SetModified( bool bSet /* = true */ )
423 SfxObjectShell::SetModified( bSet );
425 // change model state, too
426 // only set the changed state if modification is enabled
427 if( IsEnableSetModified() )
429 if ( mpDoc )
430 mpDoc->NbcSetChanged( bSet );
432 Broadcast( SfxHint( SfxHintId::DocChanged ) );
437 * Callback for ExecuteSpellPopup()
439 // ExecuteSpellPopup now handled by DrawDocShell. This is necessary
440 // to get hands on the outliner and the text object.
441 IMPL_LINK(DrawDocShell, OnlineSpellCallback, SpellCallbackInfo&, rInfo, void)
443 SdrObject* pObj = nullptr;
444 SdrOutliner* pOutl = nullptr;
446 if(GetViewShell())
448 pOutl = GetViewShell()->GetView()->GetTextEditOutliner();
449 pObj = GetViewShell()->GetView()->GetTextEditObject();
452 mpDoc->ImpOnlineSpellCallback(&rInfo, pObj, pOutl);
455 void DrawDocShell::ClearUndoBuffer()
457 // clear possible undo buffers of outliners
458 SfxViewFrame* pSfxViewFrame = SfxViewFrame::GetFirst(this, false);
459 while(pSfxViewFrame)
461 ViewShellBase* pViewShellBase = dynamic_cast< ViewShellBase* >( pSfxViewFrame->GetViewShell() );
462 if( pViewShellBase )
464 std::shared_ptr<ViewShell> pViewSh( pViewShellBase->GetMainViewShell() );
465 if( pViewSh )
467 ::sd::View* pView = pViewSh->GetView();
468 if( pView )
470 pView->SdrEndTextEdit();
471 sd::OutlineView* pOutlView = dynamic_cast< sd::OutlineView* >( pView );
472 if( pOutlView )
474 pOutlView->GetOutliner().GetUndoManager().Clear();
479 pSfxViewFrame = SfxViewFrame::GetNext(*pSfxViewFrame, this, false);
482 SfxUndoManager* pUndoManager = GetUndoManager();
483 if(pUndoManager && pUndoManager->GetUndoActionCount())
484 pUndoManager->Clear();
487 std::vector<Color> DrawDocShell::GetThemeColors()
489 auto pViewShell = dynamic_cast<sd::DrawViewShell*>(GetViewShell());
490 if (!pViewShell)
492 return {};
495 SdPage* pPage = pViewShell->getCurrentPage();
496 model::Theme* pTheme = pPage->getSdrPageProperties().GetTheme().get();
497 if (!pPage->IsMasterPage())
499 pTheme = pPage->TRG_GetMasterPage().getSdrPageProperties().GetTheme().get();
502 if (!pTheme)
504 return {};
507 return pTheme->GetColors();
510 } // end of namespace sd
512 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */