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 
25 #include <doc.hxx>
28 #include <IDocumentState.hxx>
30 #include <sfx2/printer.hxx>
31 #include <vcl/virdev.hxx>
32 #include <vcl/outdev.hxx>
33 #include <vcl/jobset.hxx>
34 #include <printdata.hxx>
35 #include <vcl/mapmod.hxx>
36 #include <svl/itemset.hxx>
37 #include <cmdid.h>
38 #include <drawdoc.hxx>
39 #include <wdocsh.hxx>
40 #include <prtopt.hxx>
41 #include <viewsh.hxx>
42 #include <rootfrm.hxx>
43 #include <viewopt.hxx>
44 #include <swwait.hxx>
45 #include <fntcache.hxx>
46 
47 class SwDocShell;
48 class SwWait;
49 
50 namespace sw {
51 
52 DocumentDeviceManager::DocumentDeviceManager( SwDoc& i_rSwdoc ) : m_rDoc( i_rSwdoc ), mpPrt(nullptr), mpVirDev(nullptr) {}
53 
54 SfxPrinter* DocumentDeviceManager::getPrinter(/*[in]*/ bool bCreate ) const
55 {
56  SfxPrinter* pRet = nullptr;
57  if ( !bCreate || mpPrt )
58  pRet = mpPrt;
59  else
60  pRet = &CreatePrinter_();
61 
62  return pRet;
63 }
64 
65 void DocumentDeviceManager::setPrinter(/*[in]*/ SfxPrinter *pP,/*[in]*/ bool bDeleteOld,/*[in]*/ bool bCallPrtDataChanged )
66 {
67  assert ( !pP || !pP->isDisposed() );
68  if ( pP != mpPrt )
69  {
70  if ( bDeleteOld )
72  mpPrt = pP;
73 
74  // our printer should always use TWIP. Don't rely on this being set in SwViewShell::InitPrt, there
75  // are situations where this isn't called. #i108712#
76  if ( mpPrt )
77  {
78  MapMode aMapMode( mpPrt->GetMapMode() );
79  aMapMode.SetMapUnit( MapUnit::MapTwip );
80  mpPrt->SetMapMode( aMapMode );
81  }
82 
85  }
86 
87  if ( bCallPrtDataChanged &&
88  // #i41075# Do not call PrtDataChanged() if we do not
89  // use the printer for formatting:
92 }
93 
95 {
96  VirtualDevice* pRet = nullptr;
97  if ( !bCreate || mpVirDev )
98  pRet = mpVirDev;
99  else
100  pRet = &CreateVirtualDevice_();
101 
102  assert ( !pRet || !pRet->isDisposed() );
103 
104  return pRet;
105 }
106 
108 {
109  assert ( !pVd->isDisposed() );
110 
111  if ( mpVirDev.get() != pVd )
112  {
114  mpVirDev = pVd;
115 
118  }
119 }
120 
122 {
123  OutputDevice* pRet = nullptr;
125  {
126  pRet = getPrinter( bCreate );
127 
128  if ( bCreate && !mpPrt->IsValid() )
129  {
130  pRet = getVirtualDevice( true );
131  }
132  }
133  else
134  {
135  pRet = getVirtualDevice( bCreate );
136  }
137 
138  assert ( !pRet || !pRet->isDisposed() );
139 
140  return pRet;
141 }
142 
143 void DocumentDeviceManager::setReferenceDeviceType(/*[in]*/ bool bNewVirtual, /*[in]*/ bool bNewHiRes )
144 {
147  {
148  if ( bNewVirtual )
149  {
150  VirtualDevice* pMyVirDev = getVirtualDevice( true );
151  if ( !bNewHiRes )
153  else
155 
158  }
159  else
160  {
161  // #i41075#
162  // We have to take care that a printer exists before calling
163  // PrtDataChanged() in order to prevent that PrtDataChanged()
164  // triggers this funny situation:
165  // getReferenceDevice()->getPrinter()->CreatePrinter_()
166  // ->setPrinter()-> PrtDataChanged()
167  SfxPrinter* pPrinter = getPrinter( true );
170  }
171 
174  PrtDataChanged();
176  }
177 }
178 
180 {
181  return mpPrt ? &mpPrt->GetJobSetup() : nullptr;
182 }
183 
184 void DocumentDeviceManager::setJobsetup(/*[in]*/ const JobSetup &rJobSetup )
185 {
186  bool bCheckPageDescs = !mpPrt;
187  bool bDataChanged = false;
188 
189  if ( mpPrt )
190  {
191  if ( mpPrt->GetName() == rJobSetup.GetPrinterName() )
192  {
193  if ( mpPrt->GetJobSetup() != rJobSetup )
194  {
195  mpPrt->SetJobSetup( rJobSetup );
196  bDataChanged = true;
197  }
198  }
199  else
201  }
202 
203  if( !mpPrt )
204  {
205  //The ItemSet is deleted by Sfx!
206  auto pSet = std::make_unique<SfxItemSet>(
208  svl::Items<
209  SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
210  SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
211  SID_HTML_MODE, SID_HTML_MODE,
212  FN_PARAM_ADDPRINTER, FN_PARAM_ADDPRINTER>{});
213  VclPtr<SfxPrinter> p = VclPtr<SfxPrinter>::Create( std::move(pSet), rJobSetup );
214  if ( bCheckPageDescs )
215  setPrinter( p, true, true );
216  else
217  {
218  mpPrt = p;
219  bDataChanged = true;
220  }
221  }
223  PrtDataChanged();
224 }
225 
227 {
228  if(!mpPrtData)
229  {
230  DocumentDeviceManager * pThis = const_cast< DocumentDeviceManager * >(this);
231  pThis->mpPrtData.reset(new SwPrintData);
232 
233  // SwPrintData should be initialized from the configuration,
234  // the respective config item is implemented by SwPrintOptions which
235  // is also derived from SwPrintData
236  const SwDocShell *pDocSh = m_rDoc.GetDocShell();
237  OSL_ENSURE( pDocSh, "pDocSh is 0, can't determine if this is a WebDoc or not" );
238  bool bWeb = dynamic_cast< const SwWebDocShell * >(pDocSh) != nullptr;
239  SwPrintOptions aPrintOptions( bWeb );
240  *pThis->mpPrtData = aPrintOptions;
241  }
242  return *mpPrtData;
243 }
244 
245 void DocumentDeviceManager::setPrintData(/*[in]*/ const SwPrintData& rPrtData )
246 {
247  if(!mpPrtData)
248  mpPrtData.reset(new SwPrintData);
249  *mpPrtData = rPrtData;
250 }
251 
253 {
254  mpPrtData.reset();
257 }
258 
260 {
261 #ifdef IOS
262  VclPtr<VirtualDevice> pNewVir = VclPtr<VirtualDevice>::Create(DeviceFormat::GRAYSCALE);
263 #else
264  VclPtr<VirtualDevice> pNewVir = VclPtr<VirtualDevice>::Create(DeviceFormat::BITMASK);
265 #endif
266 
268 
269  // #i60945# External leading compatibility for unix systems.
271  pNewVir->Compat_ZeroExtleadBug();
272 
273  MapMode aMapMode( pNewVir->GetMapMode() );
274  aMapMode.SetMapUnit( MapUnit::MapTwip );
275  pNewVir->SetMapMode( aMapMode );
276 
277  const_cast<DocumentDeviceManager*>(this)->setVirtualDevice( pNewVir );
278  return *mpVirDev;
279 }
280 
282 {
283  OSL_ENSURE( ! mpPrt, "Do not call CreatePrinter_(), call getPrinter() instead" );
284 
285 #if OSL_DEBUG_LEVEL > 1
286  SAL_INFO("sw", "Printer will be created!" );
287 #endif
288 
289  // We create a default SfxPrinter.
290  // The ItemSet is deleted by Sfx!
291  auto pSet = std::make_unique<SfxItemSet>(
293  svl::Items<
294  SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
295  SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
296  SID_HTML_MODE, SID_HTML_MODE,
297  FN_PARAM_ADDPRINTER, FN_PARAM_ADDPRINTER>{});
298 
299  VclPtr<SfxPrinter> pNewPrt = VclPtr<SfxPrinter>::Create( std::move(pSet) );
300  const_cast<DocumentDeviceManager*>(this)->setPrinter( pNewPrt, true, true );
301  return *mpPrt;
302 }
303 
305 {
306 // If you change this, also modify InJobSetup in Sw3io if appropriate.
307 
308  // #i41075#
310  nullptr != getPrinter( false ), "PrtDataChanged will be called recursively!" );
312  std::unique_ptr<SwWait> pWait;
313  bool bEndAction = false;
314 
315  if( m_rDoc.GetDocShell() )
317 
318  bool bDraw = true;
319  if ( pTmpRoot )
320  {
322  if( pSh &&
323  (!pSh->GetViewOptions()->getBrowseMode() ||
324  pSh->GetViewOptions()->IsPrtFormat()) )
325  {
326  if ( m_rDoc.GetDocShell() )
327  pWait.reset(new SwWait( *m_rDoc.GetDocShell(), true ));
328 
329  pTmpRoot->StartAllAction();
330  bEndAction = true;
331 
332  bDraw = false;
334  {
337  }
338 
339  pFntCache->Flush();
340 
341  for(SwRootFrame* aLayout : m_rDoc.GetAllLayouts())
342  aLayout->InvalidateAllContent(SwInvalidateFlags::Size);
343 
344  for(SwViewShell& rShell : pSh->GetRingContainer())
345  rShell.InitPrt(getPrinter(false));
346  }
347  }
349  {
351  if ( bTmpAddExtLeading != m_rDoc.getIDocumentDrawModelAccess().GetDrawModel()->IsAddExtLeading() )
353 
354  OutputDevice* pOutDev = getReferenceDevice( false );
357  }
358 
359  m_rDoc.PrtOLENotify( true );
360 
361  if ( bEndAction )
362  pTmpRoot->EndAllAction();
363 }
364 
365 }
366 
367 /* 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:1342
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.
#define FN_PARAM_ADDPRINTER
Definition: cmdid.h:743
The root element of a Writer document layout.
Definition: rootfrm.hxx:79
void SetMapMode()
OutputDevice * GetRefDevice() const
IDocumentDrawModelAccess const & getIDocumentDrawModelAccess() const
Definition: doc.cxx:154
bool getBrowseMode() const
Definition: viewopt.hxx:433
void PrtOLENotify(bool bAll)
Definition: docdesc.cxx:686
void Flush()
Definition: fntcache.cxx:165
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:393
std::set< SwRootFrame * > GetAllLayouts()
Definition: doclay.cxx:1660
OutputDevice * getReferenceDevice(bool bCreate) const override
Returns the current reference device.
ring_container GetRingContainer()
Definition: ring.hxx:240
SwFntCache * pFntCache
Definition: fntcache.cxx:68
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:404
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:175
static VclPtr< reference_type > Create(Arg &&...arg)
::sw::DocumentSettingManager & GetDocumentSettingManager()
Definition: doc.cxx:185
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:1309