LibreOffice Module sw (master)  1
DocumentDeviceManager.cxx
Go to the documentation of this file.
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 
21 
22 #include <memory>
23 #include <utility>
24 
26 #include <doc.hxx>
29 #include <IDocumentState.hxx>
31 #include <sfx2/printer.hxx>
32 #include <vcl/virdev.hxx>
33 #include <vcl/outdev.hxx>
34 #include <vcl/jobset.hxx>
35 #include <printdata.hxx>
36 #include <vcl/mapmod.hxx>
37 #include <svl/itemset.hxx>
38 #include <svx/svdmodel.hxx>
39 #include <sal/log.hxx>
40 #include <cmdid.h>
41 #include <drawdoc.hxx>
42 #include <wdocsh.hxx>
43 #include <prtopt.hxx>
44 #include <viewsh.hxx>
45 #include <rootfrm.hxx>
46 #include <viewopt.hxx>
47 #include <swwait.hxx>
48 #include <fntcache.hxx>
49 
50 class SwDocShell;
51 class SwWait;
52 
53 namespace sw {
54 
55 DocumentDeviceManager::DocumentDeviceManager( SwDoc& i_rSwdoc ) : m_rDoc( i_rSwdoc ), mpPrt(nullptr), mpVirDev(nullptr) {}
56 
57 SfxPrinter* DocumentDeviceManager::getPrinter(/*[in]*/ bool bCreate ) const
58 {
59  SfxPrinter* pRet = nullptr;
60  if ( !bCreate || mpPrt )
61  pRet = mpPrt;
62  else
63  pRet = &CreatePrinter_();
64 
65  return pRet;
66 }
67 
68 void DocumentDeviceManager::setPrinter(/*[in]*/ SfxPrinter *pP,/*[in]*/ bool bDeleteOld,/*[in]*/ bool bCallPrtDataChanged )
69 {
70  assert ( !pP || !pP->isDisposed() );
71  if ( pP != mpPrt )
72  {
73  if ( bDeleteOld )
75  mpPrt = pP;
76 
77  // our printer should always use TWIP. Don't rely on this being set in SwViewShell::InitPrt, there
78  // are situations where this isn't called. #i108712#
79  if ( mpPrt )
80  {
81  MapMode aMapMode( mpPrt->GetMapMode() );
82  aMapMode.SetMapUnit( MapUnit::MapTwip );
83  mpPrt->SetMapMode( aMapMode );
84  }
85 
88  }
89 
90  if ( bCallPrtDataChanged &&
91  // #i41075# Do not call PrtDataChanged() if we do not
92  // use the printer for formatting:
95 }
96 
98 {
99  VirtualDevice* pRet = nullptr;
100  if ( !bCreate || mpVirDev )
101  pRet = mpVirDev;
102  else
103  pRet = &CreateVirtualDevice_();
104 
105  assert ( !pRet || !pRet->isDisposed() );
106 
107  return pRet;
108 }
109 
111 {
112  assert ( !pVd->isDisposed() );
113 
114  if ( mpVirDev.get() != pVd )
115  {
117  mpVirDev = pVd;
118 
121  }
122 }
123 
125 {
126  OutputDevice* pRet = nullptr;
128  {
129  pRet = getPrinter( bCreate );
130 
131  if ( bCreate && !mpPrt->IsValid() )
132  {
133  pRet = getVirtualDevice( true );
134  }
135  }
136  else
137  {
138  pRet = getVirtualDevice( bCreate );
139  }
140 
141  assert ( !pRet || !pRet->isDisposed() );
142 
143  return pRet;
144 }
145 
146 void DocumentDeviceManager::setReferenceDeviceType(/*[in]*/ bool bNewVirtual, /*[in]*/ bool bNewHiRes )
147 {
150  {
151  if ( bNewVirtual )
152  {
153  VirtualDevice* pMyVirDev = getVirtualDevice( true );
154  if ( !bNewHiRes )
156  else
158 
161  }
162  else
163  {
164  // #i41075#
165  // We have to take care that a printer exists before calling
166  // PrtDataChanged() in order to prevent that PrtDataChanged()
167  // triggers this funny situation:
168  // getReferenceDevice()->getPrinter()->CreatePrinter_()
169  // ->setPrinter()-> PrtDataChanged()
170  SfxPrinter* pPrinter = getPrinter( true );
173  }
174 
177  PrtDataChanged();
179  }
180 }
181 
183 {
184  return mpPrt ? &mpPrt->GetJobSetup() : nullptr;
185 }
186 
187 void DocumentDeviceManager::setJobsetup(/*[in]*/ const JobSetup &rJobSetup )
188 {
189  bool bCheckPageDescs = !mpPrt;
190  bool bDataChanged = false;
191 
192  if ( mpPrt )
193  {
194  if ( mpPrt->GetName() == rJobSetup.GetPrinterName() )
195  {
196  if ( mpPrt->GetJobSetup() != rJobSetup )
197  {
198  mpPrt->SetJobSetup( rJobSetup );
199  bDataChanged = true;
200  }
201  }
202  else
204  }
205 
206  if( !mpPrt )
207  {
208  //The ItemSet is deleted by Sfx!
209  auto pSet = std::make_unique<SfxItemSet>(
211  svl::Items<
212  SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
213  SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
214  SID_HTML_MODE, SID_HTML_MODE,
215  FN_PARAM_ADDPRINTER, FN_PARAM_ADDPRINTER>{});
216  VclPtr<SfxPrinter> p = VclPtr<SfxPrinter>::Create( std::move(pSet), rJobSetup );
217  if ( bCheckPageDescs )
218  setPrinter( p, true, true );
219  else
220  {
221  mpPrt = p;
222  bDataChanged = true;
223  }
224  }
226  PrtDataChanged();
227 }
228 
230 {
231  if(!mpPrtData)
232  {
233  DocumentDeviceManager * pThis = const_cast< DocumentDeviceManager * >(this);
234  pThis->mpPrtData.reset(new SwPrintData);
235 
236  // SwPrintData should be initialized from the configuration,
237  // the respective config item is implemented by SwPrintOptions which
238  // is also derived from SwPrintData
239  const SwDocShell *pDocSh = m_rDoc.GetDocShell();
240  OSL_ENSURE( pDocSh, "pDocSh is 0, can't determine if this is a WebDoc or not" );
241  bool bWeb = dynamic_cast< const SwWebDocShell * >(pDocSh) != nullptr;
242  SwPrintOptions aPrintOptions( bWeb );
243  *pThis->mpPrtData = aPrintOptions;
244  }
245  return *mpPrtData;
246 }
247 
248 void DocumentDeviceManager::setPrintData(/*[in]*/ const SwPrintData& rPrtData )
249 {
250  if(!mpPrtData)
251  mpPrtData.reset(new SwPrintData);
252  *mpPrtData = rPrtData;
253 }
254 
256 {
257  mpPrtData.reset();
260 }
261 
263 {
264 #ifdef IOS
265  VclPtr<VirtualDevice> pNewVir = VclPtr<VirtualDevice>::Create(DeviceFormat::GRAYSCALE);
266 #else
267  VclPtr<VirtualDevice> pNewVir = VclPtr<VirtualDevice>::Create(DeviceFormat::BITMASK);
268 #endif
269 
271 
272  // #i60945# External leading compatibility for unix systems.
274  pNewVir->Compat_ZeroExtleadBug();
275 
276  MapMode aMapMode( pNewVir->GetMapMode() );
277  aMapMode.SetMapUnit( MapUnit::MapTwip );
278  pNewVir->SetMapMode( aMapMode );
279 
280  const_cast<DocumentDeviceManager*>(this)->setVirtualDevice( pNewVir );
281  return *mpVirDev;
282 }
283 
285 {
286  OSL_ENSURE( ! mpPrt, "Do not call CreatePrinter_(), call getPrinter() instead" );
287 
288 #if OSL_DEBUG_LEVEL > 1
289  SAL_INFO("sw", "Printer will be created!" );
290 #endif
291 
292  // We create a default SfxPrinter.
293  // The ItemSet is deleted by Sfx!
294  auto pSet = std::make_unique<SfxItemSet>(
296  svl::Items<
297  SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
298  SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
299  SID_HTML_MODE, SID_HTML_MODE,
300  FN_PARAM_ADDPRINTER, FN_PARAM_ADDPRINTER>{});
301 
302  VclPtr<SfxPrinter> pNewPrt = VclPtr<SfxPrinter>::Create( std::move(pSet) );
303  const_cast<DocumentDeviceManager*>(this)->setPrinter( pNewPrt, true, true );
304  return *mpPrt;
305 }
306 
308 {
309 // If you change this, also modify InJobSetup in Sw3io if appropriate.
310 
311  // #i41075#
313  nullptr != getPrinter( false ), "PrtDataChanged will be called recursively!" );
315  std::unique_ptr<SwWait> pWait;
316  bool bEndAction = false;
317 
318  if( m_rDoc.GetDocShell() )
320 
321  bool bDraw = true;
322  if ( pTmpRoot )
323  {
325  if( pSh &&
326  (!pSh->GetViewOptions()->getBrowseMode() ||
327  pSh->GetViewOptions()->IsPrtFormat()) )
328  {
329  if ( m_rDoc.GetDocShell() )
330  pWait.reset(new SwWait( *m_rDoc.GetDocShell(), true ));
331 
332  pTmpRoot->StartAllAction();
333  bEndAction = true;
334 
335  bDraw = false;
337  {
340  }
341 
342  pFntCache->Flush();
343 
344  for(SwRootFrame* aLayout : m_rDoc.GetAllLayouts())
345  aLayout->InvalidateAllContent(SwInvalidateFlags::Size);
346 
347  for(SwViewShell& rShell : pSh->GetRingContainer())
348  rShell.InitPrt(getPrinter(false));
349  }
350  }
352  {
354  if ( bTmpAddExtLeading != m_rDoc.getIDocumentDrawModelAccess().GetDrawModel()->IsAddExtLeading() )
356 
357  OutputDevice* pOutDev = getReferenceDevice( false );
360  }
361 
362  m_rDoc.PrtOLENotify( true );
363 
364  if ( bEndAction )
365  pTmpRoot->EndAllAction();
366 }
367 
368 }
369 
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SfxPrinter * getPrinter(bool bCreate) const override
Return the printer set at the document.
void Compat_ZeroExtleadBug()
void SetReferenceDevice(RefDevMode)
virtual const SwRootFrame * GetCurrentLayout() const =0
virtual bool get(DocumentSettingId id) const override
Return the specified document setting.
SwDocShell * GetDocShell()
Definition: doc.hxx:1340
void setPrinter(SfxPrinter *pP, bool bDeleteOld, bool bCallPrtDataChanged) override
Set the printer at the document.
void UpdateFontList()
Definition: docshini.cxx:428
void setPrintData(const SwPrintData &rPrtData) override
Sets the PrintData.
VirtualDevice * getVirtualDevice(bool bCreate) const override
Return the virtual device set at the document.
virtual void SetModified()=0
Must be called manually at changes of format.
virtual ~DocumentDeviceManager() override
void disposeAndClear()
Definition: doc.hxx:185
virtual void set(DocumentSettingId id, bool value) override
Set the specified document setting.
const JobSetup * getJobsetup() const override
Returns the Jobsetup.
const MapMode & GetMapMode() const
void setReferenceDeviceType(bool bNewVirtual, bool bNewHiRes) override
Sets the type of the reference device used for formatting the document.
Dialog to specify the properties of date form field.
Definition: accfrmobj.cxx:40
#define FN_PARAM_ADDPRINTER
Definition: cmdid.h:738
The root element of a Writer document layout.
Definition: rootfrm.hxx:79
void SetMapMode()
OutputDevice * GetRefDevice() const
IDocumentDrawModelAccess const & getIDocumentDrawModelAccess() const
Definition: doc.cxx:187
bool getBrowseMode() const
Definition: viewopt.hxx:433
void PrtOLENotify(bool bAll)
Definition: docdesc.cxx:693
void Flush()
Definition: fntcache.cxx:166
void setVirtualDevice(VirtualDevice *pVd) override
Sets the current virtual device.
VirtualDevice & CreateVirtualDevice_() const
const SwPrintData & getPrintData() const override
Returns the PrintData.
SfxPrinter & CreatePrinter_() const
bool IsAddExtLeading() const
void SetRefDevice(OutputDevice *pDev)
virtual const SwDrawModel * GetDrawModel() const =0
Draw Model and id accessors.
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:426
std::set< SwRootFrame * > GetAllLayouts()
Definition: doclay.cxx:1673
OutputDevice * getReferenceDevice(bool bCreate) const override
Returns the current reference device.
ring_container GetRingContainer()
Definition: ring.hxx:240
SwFntCache * pFntCache
Definition: fntcache.cxx:67
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:437
void PrtDataChanged()
Printer or JobSetup altered.
VclPtr< VirtualDevice > mpVirDev
OUString const & GetPrinterName() const
void SetAddExtLeading(bool bEnabled)
virtual const SwViewShell * GetCurrentViewShell() const =0
Returns the layout set at the document.
const SwViewOption * GetViewOptions() const
Definition: viewsh.hxx:426
#define SAL_INFO(area, stream)
std::unique_ptr< SwPrintData > mpPrtData
IDocumentSettingAccess const & getIDocumentSettingAccess() const
Definition: doc.cxx:208
static VclPtr< reference_type > Create(Arg &&...arg)
::sw::DocumentSettingManager & GetDocumentSettingManager()
Definition: doc.cxx:218
bool IsPrtFormat() const
Definition: viewopt.hxx:502
void setJobsetup(const JobSetup &rJobSetup) override
Sets the Jobsetup.
virtual bool get(DocumentSettingId id) const =0
Return the specified document setting.
VirtualDevice * get() const
bool isDisposed() const
const SwAttrPool & GetAttrPool() const
Definition: doc.hxx:1307