LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / dialog / printopt.cxx
blob8d16edfc0e3e1a50bee9b7d346f0f36d0cb68629
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 .
21 #include <sal/macros.h>
22 #include <officecfg/Office/Common.hxx>
23 #include <svtools/printoptions.hxx>
24 #include <svtools/restartdialog.hxx>
26 #include <comphelper/processfactory.hxx>
28 #include <sfx2/printopt.hxx>
30 static sal_uInt16 aDPIArray[] = { 72, 96, 150, 200, 300, 600 };
31 static bool bOutputForPrinter = true;
33 #define DPI_COUNT SAL_N_ELEMENTS(aDPIArray)
35 SfxCommonPrintOptionsTabPage::SfxCommonPrintOptionsTabPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rSet)
36 : SfxTabPage(pPage, pController, "sfx/ui/optprintpage.ui", "OptPrintPage", &rSet)
37 , m_xPrinterOutputRB(m_xBuilder->weld_radio_button("printer"))
38 , m_xPrintFileOutputRB(m_xBuilder->weld_radio_button("file"))
39 , m_xReduceTransparencyCB(m_xBuilder->weld_check_button("reducetrans"))
40 , m_xReduceTransparencyAutoRB(m_xBuilder->weld_radio_button("reducetransauto"))
41 , m_xReduceTransparencyNoneRB(m_xBuilder->weld_radio_button("reducetransnone"))
42 , m_xReduceGradientsCB(m_xBuilder->weld_check_button("reducegrad"))
43 , m_xReduceGradientsStripesRB(m_xBuilder->weld_radio_button("reducegradstripes"))
44 , m_xReduceGradientsColorRB(m_xBuilder->weld_radio_button("reducegradcolor"))
45 , m_xReduceGradientsStepCountNF(m_xBuilder->weld_spin_button("reducegradstep"))
46 , m_xReduceBitmapsCB(m_xBuilder->weld_check_button("reducebitmap"))
47 , m_xReduceBitmapsOptimalRB(m_xBuilder->weld_radio_button("reducebitmapoptimal"))
48 , m_xReduceBitmapsNormalRB(m_xBuilder->weld_radio_button("reducebitmapnormal"))
49 , m_xReduceBitmapsResolutionRB(m_xBuilder->weld_radio_button("reducebitmapresol"))
50 , m_xReduceBitmapsResolutionLB(m_xBuilder->weld_combo_box("reducebitmapdpi"))
51 , m_xReduceBitmapsTransparencyCB(m_xBuilder->weld_check_button("reducebitmaptrans"))
52 , m_xConvertToGreyscalesCB(m_xBuilder->weld_check_button("converttogray"))
53 , m_xPDFCB(m_xBuilder->weld_check_button("pdf"))
54 , m_xPaperSizeCB(m_xBuilder->weld_check_button("papersize"))
55 , m_xPaperOrientationCB(m_xBuilder->weld_check_button("paperorient"))
56 , m_xTransparencyCB(m_xBuilder->weld_check_button("trans"))
58 #ifndef ENABLE_CUPS
59 m_xPDFCB->hide();
60 #endif
62 if( bOutputForPrinter )
64 m_xPrinterOutputRB->set_active(true);
66 else
68 m_xPrintFileOutputRB->set_active(true);
69 m_xPDFCB->set_sensitive(false);
72 m_xPrinterOutputRB->connect_toggled( LINK( this, SfxCommonPrintOptionsTabPage, ToggleOutputPrinterRBHdl ) );
73 m_xPrintFileOutputRB->connect_toggled( LINK( this, SfxCommonPrintOptionsTabPage, ToggleOutputPrintFileRBHdl ) );
75 m_xReduceTransparencyCB->connect_toggled( LINK( this, SfxCommonPrintOptionsTabPage, ClickReduceTransparencyCBHdl ) );
76 m_xReduceGradientsCB->connect_toggled( LINK( this, SfxCommonPrintOptionsTabPage, ClickReduceGradientsCBHdl ) );
77 m_xReduceBitmapsCB->connect_toggled( LINK( this, SfxCommonPrintOptionsTabPage, ClickReduceBitmapsCBHdl ) );
79 m_xReduceGradientsStripesRB->connect_toggled( LINK( this, SfxCommonPrintOptionsTabPage, ToggleReduceGradientsStripesRBHdl ) );
80 m_xReduceBitmapsResolutionRB->connect_toggled( LINK( this, SfxCommonPrintOptionsTabPage, ToggleReduceBitmapsResolutionRBHdl ) );
83 SfxCommonPrintOptionsTabPage::~SfxCommonPrintOptionsTabPage()
87 std::unique_ptr<SfxTabPage> SfxCommonPrintOptionsTabPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrSet)
89 return std::make_unique<SfxCommonPrintOptionsTabPage>(pPage, pController, *rAttrSet);
92 bool SfxCommonPrintOptionsTabPage::FillItemSet( SfxItemSet* /*rSet*/ )
94 std::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create());
96 if( m_xPaperSizeCB->get_state_changed_from_saved())
97 officecfg::Office::Common::Print::Warning::PaperSize::set(m_xPaperSizeCB->get_active(), batch);
98 if( m_xPaperOrientationCB->get_state_changed_from_saved() )
99 officecfg::Office::Common::Print::Warning::PaperOrientation::set(m_xPaperOrientationCB->get_active(), batch);
100 if( m_xTransparencyCB->get_state_changed_from_saved() )
101 officecfg::Office::Common::Print::Warning::Transparency::set(m_xTransparencyCB->get_active(), batch);
103 batch->commit();
105 ImplSaveControls( m_xPrinterOutputRB->get_active() ? &maPrinterOptions : &maPrintFileOptions );
107 svtools::SetPrinterOptions(maPrinterOptions, /*bFile*/false);
108 svtools::SetPrinterOptions(maPrintFileOptions, /*bFile*/true);
110 return false;
113 void SfxCommonPrintOptionsTabPage::Reset( const SfxItemSet* /*rSet*/ )
115 m_xPaperSizeCB->set_active(officecfg::Office::Common::Print::Warning::PaperSize::get());
116 m_xPaperOrientationCB->set_active(officecfg::Office::Common::Print::Warning::PaperOrientation::get());
117 m_xTransparencyCB->set_active(officecfg::Office::Common::Print::Warning::Transparency::get());
119 m_xPaperSizeCB->save_state();
120 m_xPaperOrientationCB->save_state();
121 m_xTransparencyCB->save_state();
123 svtools::GetPrinterOptions( maPrinterOptions, /*bFile*/false );
124 svtools::GetPrinterOptions( maPrintFileOptions, /*bFile*/true );
125 if(m_xPrintFileOutputRB->get_active()){
126 m_xPrinterOutputRB->set_active(true);
129 ImplUpdateControls( m_xPrinterOutputRB->get_active() ? &maPrinterOptions : &maPrintFileOptions );
132 DeactivateRC SfxCommonPrintOptionsTabPage::DeactivatePage( SfxItemSet* pItemSet )
134 if( pItemSet )
135 FillItemSet( pItemSet );
137 return DeactivateRC::LeavePage;
140 void SfxCommonPrintOptionsTabPage::ImplUpdateControls( const vcl::printer::Options* pCurrentOptions )
142 m_xReduceTransparencyCB->set_active( pCurrentOptions->IsReduceTransparency() );
144 if( pCurrentOptions->GetReducedTransparencyMode() == vcl::printer::TransparencyMode::Auto )
145 m_xReduceTransparencyAutoRB->set_active(true);
146 else
147 m_xReduceTransparencyNoneRB->set_active(true);
149 m_xReduceGradientsCB->set_active( pCurrentOptions->IsReduceGradients() );
151 if( pCurrentOptions->GetReducedGradientMode() == vcl::printer::GradientMode::Stripes )
152 m_xReduceGradientsStripesRB->set_active(true);
153 else
154 m_xReduceGradientsColorRB->set_active(true);
156 m_xReduceGradientsStepCountNF->set_value(pCurrentOptions->GetReducedGradientStepCount());
158 m_xReduceBitmapsCB->set_active( pCurrentOptions->IsReduceBitmaps() );
160 if( pCurrentOptions->GetReducedBitmapMode() == vcl::printer::BitmapMode::Optimal )
161 m_xReduceBitmapsOptimalRB->set_active(true);
162 else if( pCurrentOptions->GetReducedBitmapMode() == vcl::printer::BitmapMode::Normal )
163 m_xReduceBitmapsNormalRB->set_active(true);
164 else
165 m_xReduceBitmapsResolutionRB->set_active(true);
167 const sal_uInt16 nDPI = pCurrentOptions->GetReducedBitmapResolution();
169 if( nDPI < aDPIArray[ 0 ] )
170 m_xReduceBitmapsResolutionLB->set_active(0);
171 else
173 for( int i = DPI_COUNT - 1; i >= 0; i-- )
175 if( nDPI >= aDPIArray[ i ] )
177 m_xReduceBitmapsResolutionLB->set_active(i);
178 i = -1;
183 m_xReduceBitmapsTransparencyCB->set_active( pCurrentOptions->IsReducedBitmapIncludesTransparency() );
184 m_xConvertToGreyscalesCB->set_active( pCurrentOptions->IsConvertToGreyscales() );
185 m_xPDFCB->set_active( pCurrentOptions->IsPDFAsStandardPrintJobFormat() );
187 ClickReduceTransparencyCBHdl(*m_xReduceTransparencyCB);
188 ClickReduceGradientsCBHdl(*m_xReduceGradientsCB);
189 ClickReduceBitmapsCBHdl(*m_xReduceBitmapsCB);
192 void SfxCommonPrintOptionsTabPage::ImplSaveControls( vcl::printer::Options* pCurrentOptions )
194 pCurrentOptions->SetReduceTransparency( m_xReduceTransparencyCB->get_active() );
195 pCurrentOptions->SetReducedTransparencyMode( m_xReduceTransparencyAutoRB->get_active() ? vcl::printer::TransparencyMode::Auto : vcl::printer::TransparencyMode::NONE );
196 pCurrentOptions->SetReduceGradients( m_xReduceGradientsCB->get_active() );
197 pCurrentOptions->SetReducedGradientMode( m_xReduceGradientsStripesRB->get_active() ? vcl::printer::GradientMode::Stripes : vcl::printer::GradientMode::Color );
198 pCurrentOptions->SetReducedGradientStepCount(m_xReduceGradientsStepCountNF->get_value());
199 pCurrentOptions->SetReduceBitmaps( m_xReduceBitmapsCB->get_active() );
200 pCurrentOptions->SetReducedBitmapMode( m_xReduceBitmapsOptimalRB->get_active() ? vcl::printer::BitmapMode::Optimal :
201 ( m_xReduceBitmapsNormalRB->get_active() ? vcl::printer::BitmapMode::Normal : vcl::printer::BitmapMode::Resolution ) );
202 pCurrentOptions->SetReducedBitmapResolution( aDPIArray[ std::min<sal_uInt16>( m_xReduceBitmapsResolutionLB->get_active(),
203 SAL_N_ELEMENTS(aDPIArray) - 1 ) ] );
204 pCurrentOptions->SetReducedBitmapIncludesTransparency( m_xReduceBitmapsTransparencyCB->get_active() );
205 pCurrentOptions->SetConvertToGreyscales( m_xConvertToGreyscalesCB->get_active() );
206 bool bOrigBackEnd = pCurrentOptions->IsPDFAsStandardPrintJobFormat();
207 if (bOrigBackEnd != m_xPDFCB->get_active())
209 pCurrentOptions->SetPDFAsStandardPrintJobFormat( m_xPDFCB->get_active() );
210 svtools::executeRestartDialog(
211 comphelper::getProcessComponentContext(), nullptr,
212 svtools::RESTART_REASON_PDF_AS_STANDARD_JOB_FORMAT);
216 IMPL_LINK_NOARG( SfxCommonPrintOptionsTabPage, ClickReduceTransparencyCBHdl, weld::Toggleable&, void )
218 const bool bReduceTransparency = m_xReduceTransparencyCB->get_active();
220 m_xReduceTransparencyAutoRB->set_sensitive( bReduceTransparency );
221 m_xReduceTransparencyNoneRB->set_sensitive( bReduceTransparency );
223 m_xTransparencyCB->set_sensitive( !bReduceTransparency );
226 IMPL_LINK_NOARG( SfxCommonPrintOptionsTabPage, ClickReduceGradientsCBHdl, weld::Toggleable&, void )
228 const bool bEnable = m_xReduceGradientsCB->get_active();
230 m_xReduceGradientsStripesRB->set_sensitive( bEnable );
231 m_xReduceGradientsColorRB->set_sensitive( bEnable );
232 m_xReduceGradientsStepCountNF->set_sensitive( bEnable );
234 ToggleReduceGradientsStripesRBHdl(*m_xReduceGradientsStripesRB);
237 IMPL_LINK_NOARG( SfxCommonPrintOptionsTabPage, ClickReduceBitmapsCBHdl, weld::Toggleable&, void )
239 const bool bEnable = m_xReduceBitmapsCB->get_active();
241 m_xReduceBitmapsOptimalRB->set_sensitive( bEnable );
242 m_xReduceBitmapsNormalRB->set_sensitive( bEnable );
243 m_xReduceBitmapsResolutionRB->set_sensitive( bEnable );
244 m_xReduceBitmapsTransparencyCB->set_sensitive( bEnable );
245 m_xReduceBitmapsResolutionLB->set_sensitive( bEnable );
247 ToggleReduceBitmapsResolutionRBHdl(*m_xReduceBitmapsResolutionRB);
250 IMPL_LINK_NOARG( SfxCommonPrintOptionsTabPage, ToggleReduceGradientsStripesRBHdl, weld::Toggleable&, void )
252 const bool bEnable = m_xReduceGradientsCB->get_active() && m_xReduceGradientsStripesRB->get_active();
254 m_xReduceGradientsStepCountNF->set_sensitive(bEnable);
257 IMPL_LINK_NOARG( SfxCommonPrintOptionsTabPage, ToggleReduceBitmapsResolutionRBHdl, weld::Toggleable&, void )
259 const bool bEnable = m_xReduceBitmapsCB->get_active() && m_xReduceBitmapsResolutionRB->get_active();
261 m_xReduceBitmapsResolutionLB->set_sensitive(bEnable);
264 IMPL_LINK( SfxCommonPrintOptionsTabPage, ToggleOutputPrinterRBHdl, weld::Toggleable&, rButton, void )
266 if (rButton.get_active())
268 ImplUpdateControls( &maPrinterOptions );
269 bOutputForPrinter = true;
271 else
272 ImplSaveControls( &maPrinterOptions );
275 IMPL_LINK( SfxCommonPrintOptionsTabPage, ToggleOutputPrintFileRBHdl, weld::Toggleable&, rButton, void )
277 if (rButton.get_active())
279 ImplUpdateControls( &maPrintFileOptions );
280 bOutputForPrinter = false;
281 m_xPDFCB->set_sensitive(false);
283 else
285 ImplSaveControls( &maPrintFileOptions );
286 m_xPDFCB->set_sensitive(true);
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */