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<SfxItemSet>(
210  svl::Items<
211  SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
212  SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
213  SID_HTML_MODE, SID_HTML_MODE,
214  FN_PARAM_ADDPRINTER, FN_PARAM_ADDPRINTER>{});
215  VclPtr<SfxPrinter> p = VclPtr<SfxPrinter>::Create( std::move(pSet), rJobSetup );
216  if ( bCheckPageDescs )
217  setPrinter( p, true, true );
218  else
219  {
220  mpPrt = p;
221  bDataChanged = true;
222  }
223  }
225  PrtDataChanged();
226 }
227 
229 {
230  if(!mpPrtData)
231  {
232  DocumentDeviceManager * pThis = const_cast< DocumentDeviceManager * >(this);
233  pThis->mpPrtData.reset(new SwPrintData);
234 
235  // SwPrintData should be initialized from the configuration,
236  // the respective config item is implemented by SwPrintOptions which
237  // is also derived from SwPrintData
238  const SwDocShell *pDocSh = m_rDoc.GetDocShell();
239  OSL_ENSURE( pDocSh, "pDocSh is 0, can't determine if this is a WebDoc or not" );
240  bool bWeb = dynamic_cast< const SwWebDocShell * >(pDocSh) != nullptr;
241  SwPrintOptions aPrintOptions( bWeb );
242  *pThis->mpPrtData = aPrintOptions;
243  }
244  return *mpPrtData;
245 }
246 
247 void DocumentDeviceManager::setPrintData(/*[in]*/ const SwPrintData& rPrtData )
248 {
249  if(!mpPrtData)
250  mpPrtData.reset(new SwPrintData);
251  *mpPrtData = rPrtData;
252 }
253 
255 {
256  mpPrtData.reset();
259 }
260 
262 {
263 #ifdef IOS
264  VclPtr<VirtualDevice> pNewVir = VclPtr<VirtualDevice>::Create(DeviceFormat::GRAYSCALE);
265 #else
266  VclPtr<VirtualDevice> pNewVir = VclPtr<VirtualDevice>::Create(DeviceFormat::BITMASK);
267 #endif
268 
270 
271  // #i60945# External leading compatibility for unix systems.
273  pNewVir->Compat_ZeroExtleadBug();
274 
275  MapMode aMapMode( pNewVir->GetMapMode() );
276  aMapMode.SetMapUnit( MapUnit::MapTwip );
277  pNewVir->SetMapMode( aMapMode );
278 
279  const_cast<DocumentDeviceManager*>(this)->setVirtualDevice( pNewVir );
280  return *mpVirDev;
281 }
282 
284 {
285  OSL_ENSURE( ! mpPrt, "Do not call CreatePrinter_(), call getPrinter() instead" );
286 
287 #if OSL_DEBUG_LEVEL > 1
288  SAL_INFO("sw", "Printer will be created!" );
289 #endif
290 
291  // We create a default SfxPrinter.
292  // The ItemSet is deleted by Sfx!
293  auto pSet = std::make_unique<SfxItemSet>(
295  svl::Items<
296  SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
297  SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
298  SID_HTML_MODE, SID_HTML_MODE,
299  FN_PARAM_ADDPRINTER, FN_PARAM_ADDPRINTER>{});
300 
301  VclPtr<SfxPrinter> pNewPrt = VclPtr<SfxPrinter>::Create( std::move(pSet) );
302 
303  // assign PrintData to newly created printer
304  const SwPrintData& rPrtData = getPrintData();
305  SwAddPrinterItem aAddPrinterItem(rPrtData);
306  SfxItemSet aOptions(pNewPrt->GetOptions());
307  aOptions.Put(aAddPrinterItem);
308  pNewPrt->SetOptions(aOptions);
309 
310  const_cast<DocumentDeviceManager*>(this)->setPrinter( pNewPrt, true, true );
311  return *mpPrt;
312 }
313 
315 {
316 // If you change this, also modify InJobSetup in Sw3io if appropriate.
317 
318  // #i41075#
320  nullptr != getPrinter( false ), "PrtDataChanged will be called recursively!" );
322  std::unique_ptr<SwWait> pWait;
323  bool bEndAction = false;
324 
325  if( m_rDoc.GetDocShell() )
327 
328  bool bDraw = true;
329  if ( pTmpRoot )
330  {
332  if( pSh &&
333  (!pSh->GetViewOptions()->getBrowseMode() ||
334  pSh->GetViewOptions()->IsPrtFormat()) )
335  {
336  if ( m_rDoc.GetDocShell() )
337  pWait.reset(new SwWait( *m_rDoc.GetDocShell(), true ));
338 
339  pTmpRoot->StartAllAction();
340  bEndAction = true;
341 
342  bDraw = false;
344  {
347  }
348 
349  pFntCache->Flush();
350 
351  for(SwRootFrame* aLayout : m_rDoc.GetAllLayouts())
352  aLayout->InvalidateAllContent(SwInvalidateFlags::Size);
353 
354  for(SwViewShell& rShell : pSh->GetRingContainer())
355  rShell.InitPrt(getPrinter(false));
356  }
357  }
359  {
361  if ( bTmpAddExtLeading != m_rDoc.getIDocumentDrawModelAccess().GetDrawModel()->IsAddExtLeading() )
363 
364  OutputDevice* pOutDev = getReferenceDevice( false );
367  }
368 
369  m_rDoc.PrtOLENotify( true );
370 
371  if ( bEndAction )
372  pTmpRoot->EndAllAction();
373 }
374 
375 }
376 
377 /* 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:1354
void setPrinter(SfxPrinter *pP, bool bDeleteOld, bool bCallPrtDataChanged) override
Set the printer at the document.
void UpdateFontList()
Definition: docshini.cxx:410
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:765
The root element of a Writer document layout.
Definition: rootfrm.hxx:82
void SetMapMode()
OutputDevice * GetRefDevice() const
IDocumentDrawModelAccess const & getIDocumentDrawModelAccess() const
Definition: doc.cxx:155
SwDoc & m_rDoc
Definition: docbm.cxx:1206
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
bool getBrowseMode() const
Definition: viewopt.hxx:473
void PrtOLENotify(bool bAll)
Definition: docdesc.cxx:688
void Flush()
Definition: fntcache.cxx:161
void setVirtualDevice(VirtualDevice *pVd) override
Sets the current virtual device.
o3tl::sorted_vector< SwRootFrame * > GetAllLayouts()
Definition: doclay.cxx:1669
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
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:423
#define SAL_INFO(area, stream)
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:542
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:1321