1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sfx2/printer.hxx>
21 #include <sal/log.hxx>
22 #include <doc.hxx>
23 #include <IDocumentDrawModelAccess.hxx>
24 #include <IDocumentUndoRedo.hxx>
25 #include <DocumentSettingManager.hxx>
26 #include <IDocumentDeviceAccess.hxx>
27 #include <IDocumentLayoutAccess.hxx>
28 #include <IDocumentFieldsAccess.hxx>
29 #include <IDocumentState.hxx>
30 #include <docsh.hxx>
31 #include <viewsh.hxx>
32 #include <rootfrm.hxx>
33 #include <viewimp.hxx>
34 #include <viewopt.hxx>
35 #include <txtfrm.hxx>
36 #include <notxtfrm.hxx>
37 #include <fntcache.hxx>
38 #include <docufld.hxx>
39 #include <ptqueue.hxx>
40 #include <dview.hxx>
41 #include <ndgrf.hxx>
42 #include <ndindex.hxx>
43 #include <accessibilityoptions.hxx>
44 
Init(const SwViewOption * pNewOpt)45 void SwViewShell::Init( const SwViewOption *pNewOpt )
46 {
47     mbDocSizeChgd = false;
48 
49     // We play it safe: Remove old font information whenever the printer
50     // resolution or the zoom factor changes. For that, Init() and Reformat()
51     // are the most secure places.
52     pFntCache->Flush( );
53 
54     // ViewOptions are created dynamically
55 
56     if( !mpOpt )
57     {
58         mpOpt.reset(new SwViewOption);
59 
60         // ApplyViewOptions() does not need to be called
61         if( pNewOpt )
62         {
63             *mpOpt = *pNewOpt;
64             // Zoom factor needs to be set because there is no call to ApplyViewOptions() during
65             // CTOR for performance reasons.
66             if( GetWin() && 100 != mpOpt->GetZoom() )
67             {
68                 MapMode aMode( mpWin->GetMapMode() );
69                 const Fraction aNewFactor( mpOpt->GetZoom(), 100 );
70                 aMode.SetScaleX( aNewFactor );
71                 aMode.SetScaleY( aNewFactor );
72                 mpWin->SetMapMode( aMode );
73             }
74         }
75     }
76 
77     SwDocShell* pDShell = mxDoc->GetDocShell();
78     mxDoc->GetDocumentSettingManager().set(DocumentSettingId::HTML_MODE, 0 != ::GetHtmlMode( pDShell ) );
79     // set readonly flag at ViewOptions before creating layout. Otherwise,
80     // one would have to reformat again.
81 
82     if( pDShell && pDShell->IsReadOnly() )
83         mpOpt->SetReadonly( true );
84 
85     SAL_INFO( "sw.core", "View::Init - before InitPrt" );
86     OutputDevice* pPDFOut = nullptr;
87 
88     if (mpOut && (OUTDEV_PDF == mpOut->GetOutDevType()))
89         pPDFOut = mpOut;
90 
91     // Only setup the printer if we need one:
92     const bool bBrowseMode = mpOpt->getBrowseMode();
93     if( pPDFOut )
94         InitPrt( pPDFOut );
95 
96     // i#44963 Good occasion to check if page sizes in
97     // page descriptions are still set to (LONG_MAX, LONG_MAX) (html import)
98     if ( !bBrowseMode )
99     {
100         mxDoc->CheckDefaultPageFormat();
101     }
102 
103     SAL_INFO( "sw.core", "View::Init - after InitPrt" );
104     if( GetWin() )
105     {
106         SwViewOption::Init( GetWin() );
107         GetWin()->SetFillColor();
108         GetWin()->SetBackground();
109         GetWin()->SetLineColor();
110     }
111 
112     // Create a new layout, if there is no one available
113     if( !mpLayout )
114     {
115         // Here's the code which disables the usage of "multiple" layouts at the moment
116         // If the problems with controls and groups objects are solved,
117         // this code can be removed...
118         SwViewShell *pCurrShell = GetDoc()->getIDocumentLayoutAccess().GetCurrentViewShell();
119         if( pCurrShell )
120             mpLayout = pCurrShell->mpLayout;
121         // end of "disable multiple layouts"
122         if( !mpLayout )
123         {
124             // switched to two step construction because creating the layout in SwRootFrame needs a valid pLayout set
125             mpLayout = SwRootFramePtr(new SwRootFrame(mxDoc->GetDfltFrameFormat(), this),
126                                     &SwFrame::DestroyFrame);
127             mpLayout->Init( mxDoc->GetDfltFrameFormat() );
128         }
129     }
130     SizeChgNotify();
131 
132     // XForms mode: initialize XForms mode, based on design mode (draw view)
133     //   MakeDrawView() requires layout
134     if( GetDoc()->isXForms() )
135     {
136         if( ! HasDrawView() )
137             MakeDrawView();
138         mpOpt->SetFormView( ! GetDrawView()->IsDesignMode() );
139     }
140 }
141 
142 /// CTor for the first Shell.
SwViewShell(SwDoc & rDocument,vcl::Window * pWindow,const SwViewOption * pNewOpt,OutputDevice * pOutput,long nFlags)143 SwViewShell::SwViewShell( SwDoc& rDocument, vcl::Window *pWindow,
144                         const SwViewOption *pNewOpt, OutputDevice *pOutput,
145                         long nFlags )
146     :
147     maBrowseBorder(),
148     mpSfxViewShell( nullptr ),
149     mpImp( new SwViewShellImp( this ) ),
150     mpWin( pWindow ),
151     mpOut( pOutput ? pOutput
152                   : pWindow ? static_cast<OutputDevice*>(pWindow)
153                             : static_cast<OutputDevice*>(rDocument.getIDocumentDeviceAccess().getPrinter( true ))),
154     mpAccOptions( new SwAccessibilityOptions ),
155     mbShowHeaderSeparator( false ),
156     mbShowFooterSeparator( false ),
157     mbHeaderFooterEdit( false ),
158     mpTargetPaintWindow(nullptr),
159     mpBufferedOut(nullptr),
160     mxDoc( &rDocument ),
161     mnStartAction( 0 ),
162     mnLockPaint( 0 ),
163     mbSelectAll(false),
164     mbOutputToWindow(false),
165     mpPrePostOutDev(nullptr),
166     maPrePostMapMode()
167 {
168     // in order to suppress event handling in
169     // <SwDrawContact::Changed> during construction of <SwViewShell> instance
170     mbInConstructor = true;
171 
172     mbPaintInProgress = mbViewLocked = mbInEndAction = mbFrameView =
173     mbEndActionByVirDev = false;
174     mbPaintWorks = mbEnableSmooth = true;
175     mbPreview = 0 !=( VSHELLFLAG_ISPREVIEW & nFlags );
176 
177     // i#38810 Do not reset modified state of document,
178     // if it's already been modified.
179     const bool bIsDocModified( mxDoc->getIDocumentState().IsModified() );
180     OutputDevice* pOrigOut = mpOut;
181     Init( pNewOpt );    // may change the Outdev (InitPrt())
182     mpOut = pOrigOut;
183 
184     // initialize print preview layout after layout
185     // is created in <SwViewShell::Init(..)> - called above.
186     if ( mbPreview )
187     {
188         // init page preview layout
189         mpImp->InitPagePreviewLayout();
190     }
191 
192     SET_CURR_SHELL( this );
193 
194     static_cast<SwHiddenTextFieldType*>(mxDoc->getIDocumentFieldsAccess().GetSysFieldType( SwFieldIds::HiddenText ))->
195         SetHiddenFlag( !mpOpt->IsShowHiddenField() );
196 
197     // In Init a standard FrameFormat is created.
198     if (   !mxDoc->GetIDocumentUndoRedo().IsUndoNoResetModified()
199         && !bIsDocModified )
200     {
201         mxDoc->getIDocumentState().ResetModified();
202     }
203 
204     // extend format cache.
205     if ( SwTextFrame::GetTextCache()->GetCurMax() < 2550 )
206         SwTextFrame::GetTextCache()->IncreaseMax( 100 );
207     if( mpOpt->IsGridVisible() || getIDocumentDrawModelAccess().GetDrawModel() )
208         Imp()->MakeDrawView();
209 
210     mbInConstructor = false;
211 }
212 
213 /// CTor for further Shells on a document.
SwViewShell(SwViewShell & rShell,vcl::Window * pWindow,OutputDevice * pOutput,long const nFlags)214 SwViewShell::SwViewShell( SwViewShell& rShell, vcl::Window *pWindow,
215                         OutputDevice * pOutput, long const nFlags)
216     : Ring( &rShell ) ,
217     maBrowseBorder( rShell.maBrowseBorder ),
218     mpSfxViewShell( nullptr ),
219     mpImp( new SwViewShellImp( this ) ),
220     mpWin( pWindow ),
221     mpOut( pOutput ? pOutput
222                   : pWindow ? static_cast<OutputDevice*>(pWindow)
223                             : static_cast<OutputDevice*>(rShell.GetDoc()->getIDocumentDeviceAccess().getPrinter( true ))),
224     mpAccOptions( new SwAccessibilityOptions ),
225     mbShowHeaderSeparator( false ),
226     mbShowFooterSeparator( false ),
227     mbHeaderFooterEdit( false ),
228     mpTargetPaintWindow(nullptr),
229     mpBufferedOut(nullptr),
230     mxDoc( rShell.GetDoc() ),
231     mnStartAction( 0 ),
232     mnLockPaint( 0 ),
233     mbSelectAll(false),
234     mbOutputToWindow(false),
235     mpPrePostOutDev(nullptr),
236     maPrePostMapMode()
237 {
238     // in order to suppress event handling in
239     // <SwDrawContact::Changed> during construction of <SwViewShell> instance
240     mbInConstructor = true;
241 
242     mbPaintWorks = mbEnableSmooth = true;
243     mbPaintInProgress = mbViewLocked = mbInEndAction = mbFrameView =
244     mbEndActionByVirDev = false;
245     mbPreview = 0 !=( VSHELLFLAG_ISPREVIEW & nFlags );
246 
247     if( nFlags & VSHELLFLAG_SHARELAYOUT )
248         mpLayout = rShell.mpLayout;
249 
250     SET_CURR_SHELL( this );
251 
252     bool bModified = mxDoc->getIDocumentState().IsModified();
253 
254     OutputDevice* pOrigOut = mpOut;
255     Init( rShell.GetViewOptions() ); // might change Outdev (InitPrt())
256     mpOut = pOrigOut;
257 
258     if ( mbPreview )
259         mpImp->InitPagePreviewLayout();
260 
261     static_cast<SwHiddenTextFieldType*>(mxDoc->getIDocumentFieldsAccess().GetSysFieldType( SwFieldIds::HiddenText ))->
262             SetHiddenFlag( !mpOpt->IsShowHiddenField() );
263 
264     // In Init a standard FrameFormat is created.
265     if( !bModified && !mxDoc->GetIDocumentUndoRedo().IsUndoNoResetModified() )
266     {
267         mxDoc->getIDocumentState().ResetModified();
268     }
269 
270     // extend format cache.
271     if ( SwTextFrame::GetTextCache()->GetCurMax() < 2550 )
272         SwTextFrame::GetTextCache()->IncreaseMax( 100 );
273     if( mpOpt->IsGridVisible() || getIDocumentDrawModelAccess().GetDrawModel() )
274         Imp()->MakeDrawView();
275 
276     mbInConstructor = false;
277 
278 }
279 
~SwViewShell()280 SwViewShell::~SwViewShell()
281 {
282     IDocumentLayoutAccess* const pLayoutAccess
283         = mxDoc ? &mxDoc->getIDocumentLayoutAccess() : nullptr;
284 
285     {
286         SET_CURR_SHELL( this );
287         mbPaintWorks = false;
288 
289         // i#9684 Stopping the animated graphics is not
290         // necessary during printing or pdf export, because the animation
291         // has not been started in this case.
292         if( mxDoc.get() && GetWin() )
293         {
294             SwNodes& rNds = mxDoc->GetNodes();
295 
296             SwStartNode *pStNd;
297             SwNodeIndex aIdx( *rNds.GetEndOfAutotext().StartOfSectionNode(), 1 );
298             while ( nullptr != (pStNd = aIdx.GetNode().GetStartNode()) )
299             {
300                 ++aIdx;
301                 SwGrfNode *pGNd = aIdx.GetNode().GetGrfNode();
302                 if ( nullptr != pGNd )
303                 {
304                     if( pGNd->IsAnimated() )
305                     {
306                         SwIterator<SwFrame,SwGrfNode> aIter( *pGNd );
307                         for( SwFrame* pFrame = aIter.First(); pFrame; pFrame = aIter.Next() )
308                         {
309                             OSL_ENSURE( pFrame->IsNoTextFrame(), "GraphicNode with Text?" );
310                             static_cast<SwNoTextFrame*>(pFrame)->StopAnimation( mpOut );
311                         }
312                     }
313                 }
314                 aIdx.Assign( *pStNd->EndOfSectionNode(), +1 );
315             }
316 
317             GetDoc()->StopNumRuleAnimations( mpOut );
318         }
319 
320         mpImp.reset();
321 
322         if (mxDoc)
323         {
324             if( mxDoc->getReferenceCount() > 1 )
325                 GetLayout()->ResetNewLayout();
326         }
327 
328         mpOpt.reset();
329 
330         // resize format cache.
331         if ( SwTextFrame::GetTextCache()->GetCurMax() > 250 )
332             SwTextFrame::GetTextCache()->DecreaseMax( 100 );
333 
334         // Remove from PaintQueue if necessary
335         SwPaintQueue::Remove( this );
336 
337         OSL_ENSURE( !mnStartAction, "EndAction() pending." );
338     }
339 
340     if ( pLayoutAccess )
341     {
342         GetLayout()->DeRegisterShell( this );
343         if(pLayoutAccess->GetCurrentViewShell()==this)
344         {
345             pLayoutAccess->SetCurrentViewShell(nullptr);
346             for(SwViewShell& rShell : GetRingContainer())
347             {
348                 if(&rShell != this)
349                 {
350                     pLayoutAccess->SetCurrentViewShell(&rShell);
351                     break;
352                 }
353             }
354         }
355     }
356 
357     mpAccOptions.reset();
358 }
359 
HasDrawView() const360 bool SwViewShell::HasDrawView() const
361 {
362     return Imp() && Imp()->HasDrawView();
363 }
364 
MakeDrawView()365 void SwViewShell::MakeDrawView()
366 {
367     Imp()->MakeDrawView( );
368 }
369 
HasDrawViewDrag() const370 bool SwViewShell::HasDrawViewDrag() const
371 {
372     return Imp()->HasDrawView() && Imp()->GetDrawView()->IsDragObj();
373 }
374 
GetDrawView()375 SdrView* SwViewShell::GetDrawView()
376 {
377     return Imp()->GetDrawView();
378 }
379 
GetDrawViewWithValidMarkList()380 SdrView* SwViewShell::GetDrawViewWithValidMarkList()
381 {
382     SwDrawView* pDView = Imp()->GetDrawView();
383     pDView->ValidateMarkList();
384     return pDView;
385 }
386 
387 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
388