Add a comment to clarify what kind of inputs the class handles
[LibreOffice.git] / sw / source / uibase / uiview / viewprt.cxx
blobd6a2f579fc89e1ecf1710522a5f92ff6abf2bb5b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <libxml/xmlwriter.h>
21 #include <cmdid.h>
22 #include <officecfg/Office/Common.hxx>
23 #include <sfx2/docfile.hxx>
24 #include <sfx2/request.hxx>
25 #include <sfx2/viewfrm.hxx>
26 #include <vcl/svapp.hxx>
27 #include <vcl/stdtext.hxx>
28 #include <vcl/weld.hxx>
29 #include <sfx2/printer.hxx>
30 #include <editeng/paperinf.hxx>
31 #include <sfx2/dispatch.hxx>
32 #include <svx/dialmgr.hxx>
33 #include <svx/strings.hrc>
34 #include <svl/eitem.hxx>
35 #include <svl/stritem.hxx>
36 #include <svl/intitem.hxx>
37 #include <svl/flagitem.hxx>
38 #include <sfx2/linkmgr.hxx>
39 #include <osl/diagnose.h>
40 #include <svtools/colorcfg.hxx>
41 #include <o3tl/unreachable.hxx>
43 #include <modcfg.hxx>
44 #include <edtwin.hxx>
45 #include <view.hxx>
46 #include <wrtsh.hxx>
47 #include <viewopt.hxx>
48 #include <prtopt.hxx>
49 #include <cfgitems.hxx>
50 #include "viewfunc.hxx"
51 #include <swmodule.hxx>
52 #include <wview.hxx>
53 #include <IDocumentDeviceAccess.hxx>
55 #include <globals.hrc>
56 #include <strings.hrc>
57 #include <swabstdlg.hxx>
59 #include <uivwimp.hxx>
61 using namespace ::com::sun::star;
63 // Hand over the printer to Sfx
65 SfxPrinter* SwView::GetPrinter( bool bCreate )
67 const IDocumentDeviceAccess& rIDDA = GetWrtShell().getIDocumentDeviceAccess();
68 SfxPrinter *pOld = rIDDA.getPrinter( false );
69 SfxPrinter *pPrt = rIDDA.getPrinter( bCreate );
70 if ( pOld != pPrt )
72 bool bWeb = dynamic_cast<SwWebView*>(this) != nullptr;
73 ::SetAppPrintOptions( &GetWrtShell(), bWeb );
75 return pPrt;
78 // Propagate printer change
80 void SetPrinter( IDocumentDeviceAccess* pIDDA, SfxPrinter const * pNew, bool bWeb )
82 SwPrintOptions* pOpt = SwModule::get()->GetPrtOptions(bWeb);
83 if( !pOpt)
84 return;
86 // Reading Application own printing options from SfxPrinter
87 const SfxItemSet& rSet = pNew->GetOptions();
89 const SwAddPrinterItem* pAddPrinterAttr =
90 rSet.GetItemIfSet( FN_PARAM_ADDPRINTER, false );
91 if( pAddPrinterAttr )
93 if( pIDDA )
94 pIDDA->setPrintData( *pAddPrinterAttr );
95 if( !pAddPrinterAttr->GetFaxName().isEmpty() )
96 pOpt->SetFaxName(pAddPrinterAttr->GetFaxName());
100 sal_uInt16 SwView::SetPrinter(SfxPrinter* pNew, SfxPrinterChangeFlags nDiffFlags )
102 SwWrtShell &rSh = GetWrtShell();
103 SfxPrinter* pOld = rSh.getIDocumentDeviceAccess().getPrinter( false );
104 if ( pOld && pOld->IsPrinting() )
105 return SFX_PRINTERROR_BUSY;
107 if ( (SfxPrinterChangeFlags::JOBSETUP | SfxPrinterChangeFlags::PRINTER) & nDiffFlags )
109 rSh.getIDocumentDeviceAccess().setPrinter( pNew, true, true );
110 if ( nDiffFlags & SfxPrinterChangeFlags::PRINTER )
111 rSh.SetModified();
113 bool bWeb = dynamic_cast< const SwWebView *>( this ) != nullptr;
114 if ( nDiffFlags & SfxPrinterChangeFlags::OPTIONS )
115 ::SetPrinter( &rSh.getIDocumentDeviceAccess(), pNew, bWeb );
117 const bool bChgOri = bool(nDiffFlags & SfxPrinterChangeFlags::CHG_ORIENTATION);
118 const bool bChgSize = bool(nDiffFlags & SfxPrinterChangeFlags::CHG_SIZE);
119 if ( bChgOri || bChgSize )
121 rSh.StartAllAction();
122 if ( bChgOri )
123 rSh.ChgAllPageOrientation( pNew->GetOrientation() );
124 if ( bChgSize )
126 Size aSz( SvxPaperInfo::GetPaperSize( pNew ) );
127 rSh.ChgAllPageSize( aSz );
129 rSh.SetModified();
130 rSh.EndAllAction();
131 InvalidateRulerPos();
133 return 0;
136 bool SwView::HasPrintOptionsPage() const
138 return true;
141 namespace
143 class SvxPrtQryBox
145 private:
146 std::unique_ptr<weld::MessageDialog> m_xQueryBox;
147 public:
148 SvxPrtQryBox(weld::Window* pParent)
149 : m_xQueryBox(Application::CreateMessageDialog(pParent, VclMessageType::Question, VclButtonsType::NONE, SvxResId(RID_SVXSTR_QRY_PRINT_MSG)))
151 m_xQueryBox->set_title(SvxResId(RID_SVXSTR_QRY_PRINT_TITLE));
153 m_xQueryBox->add_button(SvxResId(RID_SVXSTR_QRY_PRINT_SELECTION), RET_OK);
154 m_xQueryBox->add_button(SvxResId(RID_SVXSTR_QRY_PRINT_ALL), 2);
155 m_xQueryBox->add_button(GetStandardText(StandardButtonType::Cancel), RET_CANCEL);
156 m_xQueryBox->set_default_response(RET_OK);
158 short run() { return m_xQueryBox->run(); }
162 // TabPage for application-specific print options
164 std::unique_ptr<SfxTabPage> SwView::CreatePrintOptionsPage(weld::Container* pPage, weld::DialogController* pController,
165 const SfxItemSet& rSet)
167 return ::CreatePrintOptionsPage(pPage, pController, rSet, false);
170 // Print dispatcher
172 void SwView::ExecutePrint(SfxRequest& rReq)
174 bool bWeb = dynamic_cast<SwWebView*>( this ) != nullptr;
175 ::SetAppPrintOptions( &GetWrtShell(), bWeb );
176 switch (rReq.GetSlot())
178 case FN_FAX:
180 SwPrintOptions* pPrintOptions = SwModule::get()->GetPrtOptions(bWeb);
181 const OUString& sFaxName(pPrintOptions->GetFaxName());
182 if (!sFaxName.isEmpty())
184 SfxStringItem aPrinterName(SID_PRINTER_NAME, sFaxName);
185 SfxBoolItem aSilent( SID_SILENT, true );
186 GetViewFrame().GetDispatcher()->ExecuteList(SID_PRINTDOC,
187 SfxCallMode::SYNCHRON|SfxCallMode::RECORD,
188 { &aPrinterName, &aSilent });
190 else
192 std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(GetEditWin().GetFrameWeld(),
193 VclMessageType::Info, VclButtonsType::Ok,
194 SwResId(STR_ERR_NO_FAX)));
195 TranslateId pResId = bWeb ? STR_WEBOPTIONS : STR_TEXTOPTIONS;
196 xInfoBox->set_primary_text(xInfoBox->get_primary_text().replaceFirst("%1", SwResId(pResId)));
197 xInfoBox->run();
198 SfxUInt16Item aDefPage(SID_SW_EDITOPTIONS, TP_OPTPRINT_PAGE);
199 GetViewFrame().GetDispatcher()->ExecuteList(SID_SW_EDITOPTIONS,
200 SfxCallMode::SYNCHRON|SfxCallMode::RECORD,
201 { &aDefPage });
204 break;
205 case SID_PRINTDOC:
206 case SID_PRINTDOCDIRECT:
208 SwWrtShell* pSh = &GetWrtShell();
209 const SfxBoolItem* pSilentItem = rReq.GetArg<SfxBoolItem>(SID_SILENT);
210 bool bSilent = pSilentItem && pSilentItem->GetValue();
211 const SfxBoolItem* pPrintFromMergeItem = rReq.GetArg<SfxBoolItem>(FN_QRY_MERGE);
212 if(pPrintFromMergeItem)
213 rReq.RemoveItem(FN_QRY_MERGE);
214 bool bFromMerge = pPrintFromMergeItem && pPrintFromMergeItem->GetValue();
215 bool bPrintSelection = false;
216 if(!bSilent && !bFromMerge &&
217 SwModule::get()->GetModuleConfig()->IsAskForMailMerge() && pSh->IsAnyDatabaseFieldInDoc())
219 std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(GetEditWin().GetFrameWeld(), u"modules/swriter/ui/printmergedialog.ui"_ustr));
220 std::unique_ptr<weld::MessageDialog> xBox(xBuilder->weld_message_dialog(u"PrintMergeDialog"_ustr));
221 short nRet = xBox->run();
222 if(RET_NO != nRet)
224 if(RET_YES == nRet)
226 SfxBoolItem aBool(FN_QRY_MERGE, true);
227 GetViewFrame().GetDispatcher()->ExecuteList(
228 FN_QRY_MERGE, SfxCallMode::ASYNCHRON,
229 { &aBool });
230 rReq.Ignore();
232 return;
235 else if( rReq.GetSlot() == SID_PRINTDOCDIRECT && ! bSilent )
237 if( pSh->IsSelection() || pSh->IsFrameSelected() || pSh->GetSelectedObjCount() )
239 SvxPrtQryBox aBox(GetEditWin().GetFrameWeld());
240 short nBtn = aBox.run();
241 if( RET_CANCEL == nBtn )
242 return;
244 if( RET_OK == nBtn )
245 bPrintSelection = true;
249 //#i61455# if master documents are printed silently without loaded links then update the links now
250 if( bSilent && pSh->IsGlobalDoc() && !pSh->IsGlblDocSaveLinks() )
252 SfxMedium * medium = nullptr;
253 if (auto const sh = pSh->GetDoc()->GetDocShell()) {
254 medium = sh->GetMedium();
256 pSh->GetLinkManager().UpdateAllLinks(
257 false, false, nullptr, medium == nullptr ? OUString() : medium->GetName() );
259 SfxRequest aReq( rReq );
260 SfxBoolItem aBool(SID_SELECTION, bPrintSelection);
261 aReq.AppendItem( aBool );
262 SfxViewShell::ExecuteSlot( aReq, SfxViewShell::GetInterface() );
263 return;
265 default:
266 OSL_ENSURE(false, "wrong dispatcher");
267 return;
271 int SwView::getPart() const
273 return 0;
276 void SwView::dumpAsXml(xmlTextWriterPtr pWriter) const
278 (void)xmlTextWriterStartElement(pWriter, BAD_CAST("SwView"));
279 SfxViewShell::dumpAsXml(pWriter);
280 if (m_pWrtShell)
281 m_pWrtShell->dumpAsXml(pWriter);
282 (void)xmlTextWriterEndElement(pWriter);
285 void SwView::SetRedlineAuthor(const OUString& rAuthor)
287 m_pViewImpl->m_sRedlineAuthor = rAuthor;
290 const OUString& SwView::GetRedlineAuthor() const
292 return m_pViewImpl->m_sRedlineAuthor;
295 void SwView::NotifyCursor(SfxViewShell* pViewShell) const
297 m_pWrtShell->NotifyCursor(pViewShell);
300 ::Color SwView::GetColorConfigColor(svtools::ColorConfigEntry nColorType) const
302 if (const SwViewOption* pViewOptions = GetWrtShell().GetViewOptions())
304 switch (nColorType)
306 case svtools::ColorConfigEntry::DOCCOLOR:
308 return pViewOptions->GetDocColor();
310 // Should never be called for an unimplemented color type
311 default:
313 O3TL_UNREACHABLE;
317 else
319 SAL_WARN("sw", "GetViewOptions() returned nullptr");
322 return {};
325 // Create page printer/additions for SwView and SwPagePreview
327 std::unique_ptr<SfxTabPage> CreatePrintOptionsPage(weld::Container* pPage, weld::DialogController* pController,
328 const SfxItemSet &rOptions,
329 bool bPreview)
331 SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
333 ::CreateTabPage fnCreatePage = pFact->GetTabPageCreatorFunc(TP_OPTPRINT_PAGE);
334 OSL_ENSURE(pFact, "No Page Creator");
335 if (!fnCreatePage)
336 return nullptr;
338 std::unique_ptr<SfxTabPage> xSfxPage = fnCreatePage(pPage, pController, &rOptions);
339 OSL_ENSURE(xSfxPage, "No page");
340 if (!xSfxPage)
341 return nullptr;
343 SfxAllItemSet aSet(*(rOptions.GetPool()));
344 aSet.Put(SfxBoolItem(SID_PREVIEWFLAG_TYPE, bPreview));
345 aSet.Put(SfxBoolItem(SID_FAX_LIST, true));
346 xSfxPage->PageCreated(aSet);
347 return xSfxPage;
350 void SetAppPrintOptions( SwViewShell* pSh, bool bWeb )
352 const IDocumentDeviceAccess& rIDDA = pSh->getIDocumentDeviceAccess();
353 const SwPrintData& aPrtData = rIDDA.getPrintData();
355 if( !rIDDA.getPrinter( false ) )
356 return;
358 // Close application own printing options in SfxPrinter.
359 SwAddPrinterItem aAddPrinterItem(aPrtData);
360 SfxItemSetFixed<
361 SID_PRINTER_NOTFOUND_WARN, SID_PRINTER_NOTFOUND_WARN,
362 SID_PRINTER_CHANGESTODOC, SID_PRINTER_CHANGESTODOC,
363 SID_HTML_MODE, SID_HTML_MODE,
364 FN_PARAM_ADDPRINTER, FN_PARAM_ADDPRINTER> aSet( pSh->GetAttrPool() );
366 if(bWeb)
367 aSet.Put(SfxUInt16Item(SID_HTML_MODE,
368 ::GetHtmlMode(static_cast<SwWrtShell*>(pSh)->GetView().GetDocShell())));
369 aSet.Put(SfxBoolItem(SID_PRINTER_NOTFOUND_WARN,
370 officecfg::Office::Common::Print::Warning::NotFound::get() ));
371 aSet.Put(aAddPrinterItem);
372 aSet.Put( SfxFlagItem( SID_PRINTER_CHANGESTODOC,
373 static_cast<int>(officecfg::Office::Common::Print::Warning::PaperSize::get()
374 ? SfxPrinterChangeFlags::CHG_SIZE : SfxPrinterChangeFlags::NONE) |
375 static_cast<int>(officecfg::Office::Common::Print::Warning::PaperOrientation::get()
376 ? SfxPrinterChangeFlags::CHG_ORIENTATION : SfxPrinterChangeFlags::NONE )));
378 rIDDA.getPrinter( true )->SetOptions( aSet );
382 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */