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 <osl/diagnose.h>
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 <cfgitems.hxx>
39 #include <cmdid.h>
40 #include <drawdoc.hxx>
41 #include <wdocsh.hxx>
42 #include <prtopt.hxx>
43 #include <viewsh.hxx>
44 #include <rootfrm.hxx>
45 #include <viewopt.hxx>
46 #include <swwait.hxx>
47 #include <fntcache.hxx>
48 
49 class SwDocShell;
50 class SwWait;
51 
52 namespace sw {
53 
54 DocumentDeviceManager::DocumentDeviceManager( SwDoc& i_rSwdoc ) : m_rDoc( i_rSwdoc ), mpPrt(nullptr), mpVirDev(nullptr) {}
55 
56 SfxPrinter* DocumentDeviceManager::getPrinter(/*[in]*/ bool bCreate ) const
57 {
58  SfxPrinter* pRet = nullptr;
59  if ( !bCreate || mpPrt )
60  pRet = mpPrt;
61  else
62  pRet = &CreatePrinter_();
63 
64  return pRet;
65 }
66 
67 void DocumentDeviceManager::setPrinter(/*[in]*/ SfxPrinter *pP,/*[in]*/ bool bDeleteOld,/*[in]*/ bool bCallPrtDataChanged )
68 {
69  assert ( !pP || !pP->isDisposed() );
70  if ( pP != mpPrt )
71  {
72  if ( bDeleteOld )
74  mpPrt = pP;
75 
76  // our printer should always use TWIP. Don't rely on this being set in SwViewShell::InitPrt, there
77  // are situations where this isn't called. #i108712#
78  if ( mpPrt )
79  {
80  MapMode aMapMode( mpPrt->GetMapMode() );
81  aMapMode.SetMapUnit( MapUnit::MapTwip );
82  mpPrt->SetMapMode( aMapMode );
83  }
84 
87  }
88 
89  if ( bCallPrtDataChanged &&
90  // #i41075# Do not call PrtDataChanged() if we do not
91  // use the printer for formatting:
94 }
95 
97 {
98  VirtualDevice* pRet = nullptr;
99  if ( !bCreate || mpVirDev )
100  pRet = mpVirDev;
101  else
102  pRet = &CreateVirtualDevice_();
103 
104  assert ( !pRet || !pRet->isDisposed() );
105 
106  return pRet;
107 }
108 
110 {
111  assert ( !pVd->isDisposed() );
112 
113  if ( mpVirDev.get() != pVd )
114  {
116  mpVirDev = pVd;
117 
120  }
121 }
122 
124 {
125  OutputDevice* pRet = nullptr;
127  {
128  pRet = getPrinter( bCreate );
129 
130  if ( bCreate && !mpPrt->IsValid() )
131  {
132  pRet = getVirtualDevice( true );
133  }
134  }
135  else
136  {
137  pRet = getVirtualDevice( bCreate );
138  }
139 
140  assert ( !pRet || !pRet->isDisposed() );
141 
142  return pRet;
143 }
144 
145 void DocumentDeviceManager::setReferenceDeviceType(/*[in]*/ bool bNewVirtual, /*[in]*/ bool bNewHiRes )
146 {
149  return;
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 
182 {
183  return mpPrt ? &mpPrt->GetJobSetup() : nullptr;
184 }
185 
186 void DocumentDeviceManager::setJobsetup(/*[in]*/ const JobSetup &rJobSetup )
187 {
188  bool bCheckPageDescs = !mpPrt;
189  bool bDataChanged = false;
190 
191  if ( mpPrt )
192  {
193  if ( mpPrt->GetName() == rJobSetup.GetPrinterName() )
194  {
195  if ( mpPrt->GetJobSetup() != rJobSetup )
196  {
197  mpPrt->SetJobSetup( rJobSetup );
198  bDataChanged = true;
199  }
200  }
201  else
203  }
204 
205  if( !mpPrt )
206  {
207  //The ItemSet is deleted by Sfx!
208  auto pSet = std::make_unique<SfxItemSetFixed<
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>>(m_rDoc.GetAttrPool());
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  assert(mpPrtData && "this will always be set by now");
243  return *mpPrtData;
244 }
245 
246 void DocumentDeviceManager::setPrintData(/*[in]*/ const SwPrintData& rPrtData )
247 {
248  if(!mpPrtData)
249  mpPrtData.reset(new SwPrintData);
250  *mpPrtData = rPrtData;
251 }
252 
254 {
255  mpPrtData.reset();
258 }
259 
261 {
262 #ifdef IOS
263  VclPtr<VirtualDevice> pNewVir = VclPtr<VirtualDevice>::Create(DeviceFormat::GRAYSCALE);
264 #else
265  VclPtr<VirtualDevice> pNewVir = VclPtr<VirtualDevice>::Create(DeviceFormat::DEFAULT);
266 #endif
267 
269 
270  // #i60945# External leading compatibility for unix systems.
272  pNewVir->Compat_ZeroExtleadBug();
273 
274  MapMode aMapMode( pNewVir->GetMapMode() );
275  aMapMode.SetMapUnit( MapUnit::MapTwip );
276  pNewVir->SetMapMode( aMapMode );
277 
278  const_cast<DocumentDeviceManager*>(this)->setVirtualDevice( pNewVir );
279  return *mpVirDev;
280 }
281 
283 {
284  OSL_ENSURE( ! mpPrt, "Do not call CreatePrinter_(), call getPrinter() instead" );
285 
286  // We create a default SfxPrinter.
287  // The ItemSet is deleted by Sfx!
288  auto pSet = std::make_unique<SfxItemSetFixed<
289  SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
290  SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
291  SID_HTML_MODE, SID_HTML_MODE,
292  FN_PARAM_ADDPRINTER, FN_PARAM_ADDPRINTER>>(m_rDoc.GetAttrPool());
293 
294  VclPtr<SfxPrinter> pNewPrt = VclPtr<SfxPrinter>::Create( std::move(pSet) );
295 
296  // assign PrintData to newly created printer
297  const SwPrintData& rPrtData = getPrintData();
298  SwAddPrinterItem aAddPrinterItem(rPrtData);
299  SfxItemSet aOptions(pNewPrt->GetOptions());
300  aOptions.Put(aAddPrinterItem);
301  pNewPrt->SetOptions(aOptions);
302 
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:1351
void setPrinter(SfxPrinter *pP, bool bDeleteOld, bool bCallPrtDataChanged) override
Set the printer at the document.
void UpdateFontList()
Definition: docshini.cxx:414
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:187
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:780
The root element of a Writer document layout.
Definition: rootfrm.hxx:81
void SetMapMode()
OutputDevice * GetRefDevice() const
IDocumentDrawModelAccess const & getIDocumentDrawModelAccess() const
Definition: doc.cxx:155
SwDoc & m_rDoc
Definition: docbm.cxx:1201
bool getBrowseMode() const
Definition: viewopt.hxx:472
void PrtOLENotify(bool bAll)
Definition: docdesc.cxx:806
void Flush()
Definition: fntcache.cxx:92
void setVirtualDevice(VirtualDevice *pVd) override
Sets the current virtual device.
o3tl::sorted_vector< SwRootFrame * > GetAllLayouts()
Definition: doclay.cxx:1668
VirtualDevice & CreateVirtualDevice_() const
const SwPrintData & getPrintData() const override
Returns the PrintData.
SfxPrinter & CreatePrinter_() const
VclPtr< VirtualDevice > mpVirDev
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:394
OutputDevice * getReferenceDevice(bool bCreate) const override
Returns the current reference device.
ring_container GetRingContainer()
Definition: ring.hxx:240
SwFntCache * pFntCache
Definition: fntcache.cxx:64
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:405
void PrtDataChanged()
Printer or JobSetup altered.
VclPtr< VirtualDevice > mpVirDev
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
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:428
std::unique_ptr< SwPrintData > mpPrtData
IDocumentSettingAccess const & getIDocumentSettingAccess() const
Definition: doc.cxx:176
static VclPtr< reference_type > Create(Arg &&...arg)
::sw::DocumentSettingManager & GetDocumentSettingManager()
Definition: doc.cxx:186
void * p
bool IsPrtFormat() const
Definition: viewopt.hxx:541
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:1318