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 <cfgitems.hxx>
38 #include <cmdid.h>
39 #include <drawdoc.hxx>
40 #include <wdocsh.hxx>
41 #include <prtopt.hxx>
42 #include <viewsh.hxx>
43 #include <rootfrm.hxx>
44 #include <viewopt.hxx>
45 #include <swwait.hxx>
46 #include <fntcache.hxx>
47 
48 class SwDocShell;
49 class SwWait;
50 
51 namespace sw {
52 
53 DocumentDeviceManager::DocumentDeviceManager( SwDoc& i_rSwdoc ) : m_rDoc( i_rSwdoc ), mpPrt(nullptr), mpVirDev(nullptr) {}
54 
55 SfxPrinter* DocumentDeviceManager::getPrinter(/*[in]*/ bool bCreate ) const
56 {
57  SfxPrinter* pRet = nullptr;
58  if ( !bCreate || mpPrt )
59  pRet = mpPrt;
60  else
61  pRet = &CreatePrinter_();
62 
63  return pRet;
64 }
65 
66 void DocumentDeviceManager::setPrinter(/*[in]*/ SfxPrinter *pP,/*[in]*/ bool bDeleteOld,/*[in]*/ bool bCallPrtDataChanged )
67 {
68  assert ( !pP || !pP->isDisposed() );
69  if ( pP != mpPrt )
70  {
71  if ( bDeleteOld )
73  mpPrt = pP;
74 
75  // our printer should always use TWIP. Don't rely on this being set in SwViewShell::InitPrt, there
76  // are situations where this isn't called. #i108712#
77  if ( mpPrt )
78  {
79  MapMode aMapMode( mpPrt->GetMapMode() );
80  aMapMode.SetMapUnit( MapUnit::MapTwip );
81  mpPrt->SetMapMode( aMapMode );
82  }
83 
86  }
87 
88  if ( bCallPrtDataChanged &&
89  // #i41075# Do not call PrtDataChanged() if we do not
90  // use the printer for formatting:
93 }
94 
96 {
97  VirtualDevice* pRet = nullptr;
98  if ( !bCreate || mpVirDev )
99  pRet = mpVirDev;
100  else
101  pRet = &CreateVirtualDevice_();
102 
103  assert ( !pRet || !pRet->isDisposed() );
104 
105  return pRet;
106 }
107 
109 {
110  assert ( !pVd->isDisposed() );
111 
112  if ( mpVirDev.get() != pVd )
113  {
115  mpVirDev = pVd;
116 
119  }
120 }
121 
123 {
124  OutputDevice* pRet = nullptr;
126  {
127  pRet = getPrinter( bCreate );
128 
129  if ( bCreate && !mpPrt->IsValid() )
130  {
131  pRet = getVirtualDevice( true );
132  }
133  }
134  else
135  {
136  pRet = getVirtualDevice( bCreate );
137  }
138 
139  assert ( !pRet || !pRet->isDisposed() );
140 
141  return pRet;
142 }
143 
144 void DocumentDeviceManager::setReferenceDeviceType(/*[in]*/ bool bNewVirtual, /*[in]*/ bool bNewHiRes )
145 {
148  {
149  if ( bNewVirtual )
150  {
151  VirtualDevice* pMyVirDev = getVirtualDevice( true );
152  if ( !bNewHiRes )
154  else
156 
159  }
160  else
161  {
162  // #i41075#
163  // We have to take care that a printer exists before calling
164  // PrtDataChanged() in order to prevent that PrtDataChanged()
165  // triggers this funny situation:
166  // getReferenceDevice()->getPrinter()->CreatePrinter_()
167  // ->setPrinter()-> PrtDataChanged()
168  SfxPrinter* pPrinter = getPrinter( true );
171  }
172 
175  PrtDataChanged();
177  }
178 }
179 
181 {
182  return mpPrt ? &mpPrt->GetJobSetup() : nullptr;
183 }
184 
185 void DocumentDeviceManager::setJobsetup(/*[in]*/ const JobSetup &rJobSetup )
186 {
187  bool bCheckPageDescs = !mpPrt;
188  bool bDataChanged = false;
189 
190  if ( mpPrt )
191  {
192  if ( mpPrt->GetName() == rJobSetup.GetPrinterName() )
193  {
194  if ( mpPrt->GetJobSetup() != rJobSetup )
195  {
196  mpPrt->SetJobSetup( rJobSetup );
197  bDataChanged = true;
198  }
199  }
200  else
202  }
203 
204  if( !mpPrt )
205  {
206  //The ItemSet is deleted by Sfx!
207  auto pSet = std::make_unique<SfxItemSet>(
209  svl::Items<
210  SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
211  SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
212  SID_HTML_MODE, SID_HTML_MODE,
213  FN_PARAM_ADDPRINTER, FN_PARAM_ADDPRINTER>{});
214  VclPtr<SfxPrinter> p = VclPtr<SfxPrinter>::Create( std::move(pSet), rJobSetup );
215  if ( bCheckPageDescs )
216  setPrinter( p, true, true );
217  else
218  {
219  mpPrt = p;
220  bDataChanged = true;
221  }
222  }
224  PrtDataChanged();
225 }
226 
228 {
229  if(!mpPrtData)
230  {
231  DocumentDeviceManager * pThis = const_cast< DocumentDeviceManager * >(this);
232  pThis->mpPrtData.reset(new SwPrintData);
233 
234  // SwPrintData should be initialized from the configuration,
235  // the respective config item is implemented by SwPrintOptions which
236  // is also derived from SwPrintData
237  const SwDocShell *pDocSh = m_rDoc.GetDocShell();
238  OSL_ENSURE( pDocSh, "pDocSh is 0, can't determine if this is a WebDoc or not" );
239  bool bWeb = dynamic_cast< const SwWebDocShell * >(pDocSh) != nullptr;
240  SwPrintOptions aPrintOptions( bWeb );
241  *pThis->mpPrtData = aPrintOptions;
242  }
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::BITMASK);
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 #if OSL_DEBUG_LEVEL > 1
287  SAL_INFO("sw", "Printer will be created!" );
288 #endif
289 
290  // We create a default SfxPrinter.
291  // The ItemSet is deleted by Sfx!
292  auto pSet = std::make_unique<SfxItemSet>(
294  svl::Items<
295  SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
296  SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
297  SID_HTML_MODE, SID_HTML_MODE,
298  FN_PARAM_ADDPRINTER, FN_PARAM_ADDPRINTER>{});
299 
300  VclPtr<SfxPrinter> pNewPrt = VclPtr<SfxPrinter>::Create( std::move(pSet) );
301 
302  // assign PrintData to newly created printer
303  const SwPrintData& rPrtData = getPrintData();
304  SwAddPrinterItem aAddPrinterItem(rPrtData);
305  SfxItemSet aOptions(pNewPrt->GetOptions());
306  aOptions.Put(aAddPrinterItem);
307  pNewPrt->SetOptions(aOptions);
308 
309  const_cast<DocumentDeviceManager*>(this)->setPrinter( pNewPrt, true, true );
310  return *mpPrt;
311 }
312 
314 {
315 // If you change this, also modify InJobSetup in Sw3io if appropriate.
316 
317  // #i41075#
319  nullptr != getPrinter( false ), "PrtDataChanged will be called recursively!" );
321  std::unique_ptr<SwWait> pWait;
322  bool bEndAction = false;
323 
324  if( m_rDoc.GetDocShell() )
326 
327  bool bDraw = true;
328  if ( pTmpRoot )
329  {
331  if( pSh &&
332  (!pSh->GetViewOptions()->getBrowseMode() ||
333  pSh->GetViewOptions()->IsPrtFormat()) )
334  {
335  if ( m_rDoc.GetDocShell() )
336  pWait.reset(new SwWait( *m_rDoc.GetDocShell(), true ));
337 
338  pTmpRoot->StartAllAction();
339  bEndAction = true;
340 
341  bDraw = false;
343  {
346  }
347 
348  pFntCache->Flush();
349 
350  for(SwRootFrame* aLayout : m_rDoc.GetAllLayouts())
351  aLayout->InvalidateAllContent(SwInvalidateFlags::Size);
352 
353  for(SwViewShell& rShell : pSh->GetRingContainer())
354  rShell.InitPrt(getPrinter(false));
355  }
356  }
358  {
360  if ( bTmpAddExtLeading != m_rDoc.getIDocumentDrawModelAccess().GetDrawModel()->IsAddExtLeading() )
362 
363  OutputDevice* pOutDev = getReferenceDevice( false );
366  }
367 
368  m_rDoc.PrtOLENotify( true );
369 
370  if ( bEndAction )
371  pTmpRoot->EndAllAction();
372 }
373 
374 }
375 
376 /* 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:1348
void setPrinter(SfxPrinter *pP, bool bDeleteOld, bool bCallPrtDataChanged) override
Set the printer at the document.
void UpdateFontList()
Definition: docshini.cxx:409
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:186
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:753
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:444
void PrtOLENotify(bool bAll)
Definition: docdesc.cxx:686
void Flush()
Definition: fntcache.cxx:161
void setVirtualDevice(VirtualDevice *pVd) override
Sets the current virtual device.
o3tl::sorted_vector< SwRootFrame * > GetAllLayouts()
Definition: doclay.cxx:1665
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
OutputDevice * getReferenceDevice(bool bCreate) const override
Returns the current reference device.
ring_container GetRingContainer()
Definition: ring.hxx:240
PDFDocument & m_rDoc
SwFntCache * pFntCache
Definition: fntcache.cxx:64
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
void * p
bool IsPrtFormat() const
Definition: viewopt.hxx:513
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:1315