1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <DocumentDrawModelManager.hxx>
23 #include <IDocumentUndoRedo.hxx>
24 #include <IDocumentSettingAccess.hxx>
25 #include <IDocumentDeviceAccess.hxx>
26 #include <IDocumentLinksAdministration.hxx>
27 #include <IDocumentLayoutAccess.hxx>
30 #include <swtypes.hxx>
31 #include <svl/hint.hxx>
34 #include <drawdoc.hxx>
35 #include <rootfrm.hxx>
36 #include <fmtanchr.hxx>
37 #include <editeng/eeitem.hxx>
38 #include <editeng/fhgtitem.hxx>
39 #include <svx/svdlayer.hxx>
40 #include <svx/svdoutl.hxx>
41 #include <svx/svdpage.hxx>
42 #include <svx/svdpagv.hxx>
43 #include <svx/svdotext.hxx>
44 #include <svx/svdview.hxx>
45 #include <svl/srchitem.hxx>
46 #include <unotools/configmgr.hxx>
47 #include <sal/log.hxx>
48 #include <osl/diagnose.h>
55 DocumentDrawModelManager::DocumentDrawModelManager(SwDoc
& i_rSwdoc
)
60 , mnInvisibleHeaven(0)
62 , mnInvisibleControls(0)
66 // Is also called by the Sw3 Reader, if there was an error when reading the
67 // drawing layer. If it is called by the Sw3 Reader the layer is rebuilt
69 void DocumentDrawModelManager::InitDrawModel()
71 // !! Attention: there is similar code in the Sw3 Reader (sw3imp.cxx) that
72 // also has to be maintained!!
76 // set FontHeight pool defaults without changing static SdrEngineDefaults
77 m_rDoc
.GetAttrPool().SetPoolDefaultItem(SvxFontHeightItem( 240, 100, EE_CHAR_FONTHEIGHT
));
79 SAL_INFO( "sw.doc", "before create DrawDocument" );
80 // The document owns the SwDrawModel. We always have two layers and one page.
81 mpDrawModel
.reset(new SwDrawModel(m_rDoc
));
83 mpDrawModel
->EnableUndo( m_rDoc
.GetIDocumentUndoRedo().DoesUndo() );
87 mnHell
= mpDrawModel
->GetLayerAdmin().NewLayer( sLayerNm
)->GetID();
90 mnHeaven
= mpDrawModel
->GetLayerAdmin().NewLayer( sLayerNm
)->GetID();
92 sLayerNm
= "Controls";
93 mnControls
= mpDrawModel
->GetLayerAdmin().NewLayer( sLayerNm
)->GetID();
94 mpDrawModel
->GetLayerAdmin().SetControlLayerName(sLayerNm
);
96 // add invisible layers corresponding to the visible ones.
98 sLayerNm
= "InvisibleHell";
99 mnInvisibleHell
= mpDrawModel
->GetLayerAdmin().NewLayer( sLayerNm
)->GetID();
101 sLayerNm
= "InvisibleHeaven";
102 mnInvisibleHeaven
= mpDrawModel
->GetLayerAdmin().NewLayer( sLayerNm
)->GetID();
104 sLayerNm
= "InvisibleControls";
105 mnInvisibleControls
= mpDrawModel
->GetLayerAdmin().NewLayer( sLayerNm
)->GetID();
108 rtl::Reference
<SdrPage
> pMasterPage
= mpDrawModel
->AllocPage( false );
109 mpDrawModel
->InsertPage( pMasterPage
.get() );
110 SAL_INFO( "sw.doc", "after create DrawDocument" );
111 SdrOutliner
& rOutliner
= mpDrawModel
->GetDrawOutliner();
112 if (!utl::ConfigManager::IsFuzzing())
114 SAL_INFO( "sw.doc", "before create Spellchecker/Hyphenator" );
115 css::uno::Reference
< css::linguistic2::XSpellChecker1
> xSpell
= ::GetSpellChecker();
116 rOutliner
.SetSpeller( xSpell
);
117 css::uno::Reference
< css::linguistic2::XHyphenator
> xHyphenator( ::GetHyphenator() );
118 rOutliner
.SetHyphenator( xHyphenator
);
119 SAL_INFO( "sw.doc", "after create Spellchecker/Hyphenator" );
121 m_rDoc
.SetCalcFieldValueHdl(&rOutliner
);
122 m_rDoc
.SetCalcFieldValueHdl(&mpDrawModel
->GetHitTestOutliner());
124 // Set the LinkManager in the model so that linked graphics can be inserted.
125 // The WinWord import needs it too.
126 mpDrawModel
->SetLinkManager( & m_rDoc
.getIDocumentLinksAdministration().GetLinkManager() );
127 mpDrawModel
->SetAddExtLeading( m_rDoc
.getIDocumentSettingAccess().get(DocumentSettingId::ADD_EXT_LEADING
) );
129 OutputDevice
* pRefDev
= m_rDoc
.getIDocumentDeviceAccess().getReferenceDevice( false );
131 mpDrawModel
->SetRefDevice( pRefDev
);
133 mpDrawModel
->SetNotifyUndoActionHdl( std::bind( &SwDoc::AddDrawUndo
, &m_rDoc
, std::placeholders::_1
));
134 SwViewShell
* const pSh
= m_rDoc
.getIDocumentLayoutAccess().GetCurrentViewShell();
138 for(const SwViewShell
& rViewSh
: pSh
->GetRingContainer())
140 SwRootFrame
* pRoot
= rViewSh
.GetLayout();
141 if( pRoot
&& !pRoot
->GetDrawPage() )
143 // Disable "multiple layout" for the moment:
144 // use pMasterPage instead of a new created SdrPage
145 // mpDrawModel->AllocPage( FALSE );
146 // mpDrawModel->InsertPage( pDrawPage );
147 SdrPage
* pDrawPage
= pMasterPage
.get();
148 pRoot
->SetDrawPage( pDrawPage
);
149 pDrawPage
->SetSize( pRoot
->getFrameArea().SSize() );
155 void DocumentDrawModelManager::ReleaseDrawModel()
157 // !! Also maintain the code in the sw3io for inserting documents!!
162 const SwDrawModel
* DocumentDrawModelManager::GetDrawModel() const
164 return mpDrawModel
.get();
167 SwDrawModel
* DocumentDrawModelManager::GetDrawModel()
169 return mpDrawModel
.get();
172 SwDrawModel
* DocumentDrawModelManager::MakeDrawModel_()
174 OSL_ENSURE( !mpDrawModel
, "MakeDrawModel_: Why?" );
176 SwViewShell
* const pSh
= m_rDoc
.getIDocumentLayoutAccess().GetCurrentViewShell();
179 for(SwViewShell
& rViewSh
: pSh
->GetRingContainer())
180 rViewSh
.MakeDrawView();
182 // Broadcast, so that the FormShell can be connected to the DrawView
183 if( m_rDoc
.GetDocShell() )
185 SfxHint
aHint( SfxHintId::SwDrawViewsCreated
);
186 m_rDoc
.GetDocShell()->Broadcast( aHint
);
189 return mpDrawModel
.get();
192 SwDrawModel
* DocumentDrawModelManager::GetOrCreateDrawModel()
194 return GetDrawModel() ? GetDrawModel() : MakeDrawModel_();
197 SdrLayerID
DocumentDrawModelManager::GetHeavenId() const
202 SdrLayerID
DocumentDrawModelManager::GetHellId() const
207 SdrLayerID
DocumentDrawModelManager::GetControlsId() const
212 SdrLayerID
DocumentDrawModelManager::GetInvisibleHeavenId() const
214 return mnInvisibleHeaven
;
217 SdrLayerID
DocumentDrawModelManager::GetInvisibleHellId() const
219 return mnInvisibleHell
;
222 SdrLayerID
DocumentDrawModelManager::GetInvisibleControlsId() const
224 return mnInvisibleControls
;
227 void DocumentDrawModelManager::NotifyInvisibleLayers( SdrPageView
& _rSdrPageView
)
230 sLayerNm
= "InvisibleHell";
231 _rSdrPageView
.SetLayerVisible( sLayerNm
, false );
233 sLayerNm
= "InvisibleHeaven";
234 _rSdrPageView
.SetLayerVisible( sLayerNm
, false );
236 sLayerNm
= "InvisibleControls";
237 _rSdrPageView
.SetLayerVisible( sLayerNm
, false );
240 bool DocumentDrawModelManager::IsVisibleLayerId( SdrLayerID _nLayerId
) const
244 if ( _nLayerId
== GetHeavenId() ||
245 _nLayerId
== GetHellId() ||
246 _nLayerId
== GetControlsId() )
250 else if ( _nLayerId
== GetInvisibleHeavenId() ||
251 _nLayerId
== GetInvisibleHellId() ||
252 _nLayerId
== GetInvisibleControlsId() )
258 OSL_FAIL( "<SwDoc::IsVisibleLayerId(..)> - unknown layer ID." );
265 SdrLayerID
DocumentDrawModelManager::GetInvisibleLayerIdByVisibleOne( SdrLayerID _nVisibleLayerId
)
267 SdrLayerID nInvisibleLayerId
;
269 if ( _nVisibleLayerId
== GetHeavenId() )
271 nInvisibleLayerId
= GetInvisibleHeavenId();
273 else if ( _nVisibleLayerId
== GetHellId() )
275 nInvisibleLayerId
= GetInvisibleHellId();
277 else if ( _nVisibleLayerId
== GetControlsId() )
279 nInvisibleLayerId
= GetInvisibleControlsId();
281 else if ( _nVisibleLayerId
== GetInvisibleHeavenId() ||
282 _nVisibleLayerId
== GetInvisibleHellId() ||
283 _nVisibleLayerId
== GetInvisibleControlsId() )
285 OSL_FAIL( "<SwDoc::GetInvisibleLayerIdByVisibleOne(..)> - given layer ID already an invisible one." );
286 nInvisibleLayerId
= _nVisibleLayerId
;
290 OSL_FAIL( "<SwDoc::GetInvisibleLayerIdByVisibleOne(..)> - given layer ID is unknown." );
291 nInvisibleLayerId
= _nVisibleLayerId
;
294 return nInvisibleLayerId
;
297 bool DocumentDrawModelManager::Search(const SwPaM
& rPaM
, const SvxSearchItem
& rSearchItem
)
299 SwPosFlyFrames aFrames
= m_rDoc
.GetAllFlyFormats(&rPaM
, /*bDrawAlso=*/true);
301 for (const SwPosFlyFrame
& rPosFlyFrame
: aFrames
)
303 // Filter for at-paragraph anchored draw frames.
304 const SwFrameFormat
& rFrameFormat
= rPosFlyFrame
.GetFormat();
305 const SwFormatAnchor
& rAnchor
= rFrameFormat
.GetAnchor();
306 if (rAnchor
.GetAnchorId() != RndStdIds::FLY_AT_PARA
|| rFrameFormat
.Which() != RES_DRAWFRMFMT
)
309 // Does the shape have matching text?
310 SdrOutliner
& rOutliner
= GetDrawModel()->GetDrawOutliner();
311 SdrObject
* pObject
= const_cast<SdrObject
*>(rFrameFormat
.FindSdrObject());
312 SdrTextObj
* pTextObj
= DynCastSdrTextObj(pObject
);
315 const OutlinerParaObject
* pParaObj
= pTextObj
->GetOutlinerParaObject();
318 rOutliner
.SetText(*pParaObj
);
319 SwDocShell
* pDocShell
= m_rDoc
.GetDocShell();
322 SwWrtShell
* pWrtShell
= pDocShell
->GetWrtShell();
325 if (!rOutliner
.HasText(rSearchItem
))
328 // If so, then select highlight the search result.
329 pWrtShell
->SelectObj(Point(), 0, pObject
);
330 SwView
* pView
= pDocShell
->GetView();
333 if (!pView
->EnterShapeDrawTextMode(pObject
))
335 SdrView
* pSdrView
= pWrtShell
->GetDrawView();
338 OutlinerView
* pOutlinerView
= pSdrView
->GetTextEditOutlinerView();
339 if (!rSearchItem
.GetBackward())
340 pOutlinerView
->SetSelection(ESelection(0, 0, 0, 0));
342 pOutlinerView
->SetSelection(ESelection(EE_PARA_MAX_COUNT
, EE_TEXTPOS_MAX_COUNT
, EE_PARA_MAX_COUNT
, EE_TEXTPOS_MAX_COUNT
));
343 pOutlinerView
->StartSearchAndReplace(rSearchItem
);
350 void DocumentDrawModelManager::DrawNotifyUndoHdl()
352 mpDrawModel
->SetNotifyUndoActionHdl( nullptr );
357 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */