LibreOffice Module sw (master)  1
optcomp.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 
20 #include <optcomp.hxx>
21 
22 #include <cmdid.h>
23 #include <docsh.hxx>
24 #include <uiitems.hxx>
25 #include <view.hxx>
26 #include <wrtsh.hxx>
27 
28 #include <vcl/svapp.hxx>
29 #include <vcl/weld.hxx>
30 #include <sfx2/docfilt.hxx>
31 #include <sfx2/fcontnr.hxx>
33 #include <vector>
36 #include <officecfg/Office/Compatibility.hxx>
37 
38 #include <com/sun/star/beans/PropertyValue.hpp>
39 
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::document;
42 using namespace ::com::sun::star::uno;
43 using namespace ::std;
44 
46 {
47  std::vector< SvtCompatibilityEntry > m_aList;
48 };
49 
51  : SfxTabPage(pPage, pController, "modules/swriter/ui/optcompatpage.ui", "OptCompatPage", &rSet)
52  , m_pWrtShell(nullptr)
54  , m_nSavedOptions(0)
55  , m_bSavedMSFormsMenuOption(false)
56  , m_xMain(m_xBuilder->weld_frame("compatframe"))
57  , m_xGlobalOptionsFrame(m_xBuilder->weld_frame("globalcompatframe"))
58  , m_xFormattingLB(m_xBuilder->weld_combo_box("format"))
59  , m_xGlobalOptionsLB(m_xBuilder->weld_combo_box("globaloptions"))
60  , m_xOptionsLB(m_xBuilder->weld_tree_view("options"))
61  , m_xGlobalOptionsCLB(m_xBuilder->weld_tree_view("globaloptioncheckbox"))
62  , m_xDefaultPB(m_xBuilder->weld_button("default"))
63 {
64  m_xOptionsLB->enable_toggle_buttons(weld::ColumnToggleType::Check);
65  m_xGlobalOptionsCLB->enable_toggle_buttons(weld::ColumnToggleType::Check);
66 
67  int nPos = 0;
68  for ( int i = static_cast<int>(SvtCompatibilityEntry::Index::Module) + 1; i < static_cast<int>(SvtCompatibilityEntry::Index::INVALID); ++i )
69  {
70  int nCoptIdx = i - 2; /* Do not consider "Name" & "Module" indexes */
71 
72  const OUString sEntry = m_xFormattingLB->get_text(nCoptIdx);
73  m_xOptionsLB->append();
74  m_xOptionsLB->set_toggle(nPos, TRISTATE_FALSE);
75  m_xOptionsLB->set_text(nPos, sEntry, 0);
76  ++nPos;
77  }
78 
79  m_sUserEntry = m_xFormattingLB->get_text(m_xFormattingLB->get_count() - 1);
80 
81  m_xFormattingLB->clear();
82 
83  // Set MSOCompatibleFormsMenu entry attributes
84  const bool bReadOnly = officecfg::Office::Compatibility::View::MSCompatibleFormsMenu::isReadOnly();
85  m_xGlobalOptionsCLB->set_sensitive(!bReadOnly);
86 
87  m_xGlobalOptionsCLB->append();
89  m_xGlobalOptionsCLB->set_toggle(0, bChecked ? TRISTATE_TRUE : TRISTATE_FALSE);
90  m_xGlobalOptionsCLB->set_text(0, m_xGlobalOptionsLB->get_text(0), 0);
91 
92  m_xGlobalOptionsLB->clear();
93 
94  // tdf#125799, we let only the doc options grow/shrink but give this one more than its bare
95  // min request height because there's only one row in it and that looks somewhat abrupt
96  m_xGlobalOptionsCLB->set_size_request(-1, m_xGlobalOptionsCLB->get_preferred_size().Height() * 2);
97 
98  InitControls( rSet );
99 
100  // set handler
101  m_xFormattingLB->connect_changed( LINK( this, SwCompatibilityOptPage, SelectHdl ) );
102  m_xDefaultPB->connect_clicked( LINK( this, SwCompatibilityOptPage, UseAsDefaultHdl ) );
103 }
104 
106 {
107 }
108 
109 static sal_uInt32 convertBools2Ulong_Impl
110 (
111  bool _bUsePrtMetrics,
112  bool _bAddSpacing,
113  bool _bAddSpacingAtPages,
114  bool _bUseOurTabStops,
115  bool _bNoExtLeading,
116  bool _bUseLineSpacing,
117  bool _bAddTableSpacing,
118  bool _bUseObjPos,
119  bool _bUseOurTextWrapping,
120  bool _bConsiderWrappingStyle,
121  bool _bExpandWordSpace,
122  bool _bProtectForm,
123  bool _bMsWordCompTrailingBlanks,
124  bool bSubtractFlysAnchoredAtFlys,
125  bool bEmptyDbFieldHidesPara
126 )
127 {
128  sal_uInt32 nRet = 0;
129  sal_uInt32 nSetBit = 1;
130 
131  if ( _bUsePrtMetrics )
132  nRet |= nSetBit;
133  nSetBit = nSetBit << 1;
134  if ( _bAddSpacing )
135  nRet |= nSetBit;
136  nSetBit = nSetBit << 1;
137  if ( _bAddSpacingAtPages )
138  nRet |= nSetBit;
139  nSetBit = nSetBit << 1;
140  if ( _bUseOurTabStops )
141  nRet |= nSetBit;
142  nSetBit = nSetBit << 1;
143  if ( _bNoExtLeading )
144  nRet |= nSetBit;
145  nSetBit = nSetBit << 1;
146  if ( _bUseLineSpacing )
147  nRet |= nSetBit;
148  nSetBit = nSetBit << 1;
149  if ( _bAddTableSpacing )
150  nRet |= nSetBit;
151  nSetBit = nSetBit << 1;
152  if ( _bUseObjPos )
153  nRet |= nSetBit;
154  nSetBit = nSetBit << 1;
155  if ( _bUseOurTextWrapping )
156  nRet |= nSetBit;
157  nSetBit = nSetBit << 1;
158  if ( _bConsiderWrappingStyle )
159  nRet |= nSetBit;
160  nSetBit = nSetBit << 1;
161  if ( _bExpandWordSpace )
162  nRet |= nSetBit;
163  nSetBit = nSetBit << 1;
164  if ( _bProtectForm )
165  nRet |= nSetBit;
166  nSetBit = nSetBit << 1;
167  if ( _bMsWordCompTrailingBlanks )
168  nRet |= nSetBit;
169  nSetBit = nSetBit << 1;
170  if (bSubtractFlysAnchoredAtFlys)
171  nRet |= nSetBit;
172  nSetBit = nSetBit << 1;
173  if (bEmptyDbFieldHidesPara)
174  nRet |= nSetBit;
175 
176  return nRet;
177 }
178 
180 {
181  // init objectshell and detect document name
182  OUString sDocTitle;
183  const SfxPoolItem* pItem = nullptr;
184  SfxObjectShell* pObjShell = nullptr;
185  if ( SfxItemState::SET == rSet.GetItemState( FN_PARAM_WRTSHELL, false, &pItem ) )
186  m_pWrtShell = static_cast<SwWrtShell*>(static_cast<const SwPtrItem*>(pItem)->GetValue());
187  if ( m_pWrtShell )
188  {
189  pObjShell = m_pWrtShell->GetView().GetDocShell();
190  if ( pObjShell )
191  sDocTitle = pObjShell->GetTitle();
192  }
193  else
194  {
195  m_xMain->set_sensitive(false);
196  m_xGlobalOptionsFrame->set_sensitive(false);
197  }
198  const OUString& rText = m_xMain->get_label();
199  m_xMain->set_label(rText.replaceAll("%DOCNAME", sDocTitle));
200 
201  // loading file formats
202  const Sequence< Sequence< PropertyValue > > aList = m_aConfigItem.GetList();
203 
204  SvtCompatibilityEntry aEntry;
206 
207  for ( const Sequence< PropertyValue >& rEntry : aList )
208  {
209  for ( const PropertyValue& aValue : rEntry )
210  {
211  aEntry.setValue( SvtCompatibilityEntry::getIndex(aValue.Name), aValue.Value );
212  }
213 
214  const OUString sEntryName = aEntry.getValue<OUString>( SvtCompatibilityEntry::Index::Name );
215 
216  const bool bIsUserEntry = ( sEntryName == SvtCompatibilityEntry::getUserEntryName() );
217  const bool bIsDefaultEntry = ( sEntryName == SvtCompatibilityEntry::getDefaultEntryName() );
218 
219  aEntry.setDefaultEntry( bIsDefaultEntry );
220 
221  m_pImpl->m_aList.push_back( aEntry );
222 
223  if ( aEntry.isDefaultEntry() )
224  continue;
225 
226  OUString sNewEntry;
227  if ( bIsUserEntry )
228  sNewEntry = m_sUserEntry;
229 
230  else if ( pObjShell && !sEntryName.isEmpty() )
231  {
232  SfxFilterContainer* pFacCont = pObjShell->GetFactory().GetFilterContainer();
233  std::shared_ptr<const SfxFilter> pFilter = pFacCont->GetFilter4FilterName( sEntryName );
234  if ( pFilter )
235  sNewEntry = pFilter->GetUIName();
236  }
237 
238  if ( sNewEntry.isEmpty() )
239  sNewEntry = sEntryName;
240 
241  sal_uInt32 nOptions = convertBools2Ulong_Impl(
252  aEntry.getValue<bool>( SvtCompatibilityEntry::Index::ExpandWordSpace ),
257  m_xFormattingLB->append(OUString::number(nOptions), sNewEntry);
258  }
259 }
260 
262 {
263  sal_uInt32 nOptions = m_xFormattingLB->get_active_id().toUInt32();
264  SetCurrentOptions(nOptions);
265 }
266 
268 {
269  std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(GetFrameWeld(), "modules/swriter/ui/querydefaultcompatdialog.ui"));
270  std::unique_ptr<weld::MessageDialog> xQueryBox(xBuilder->weld_message_dialog("QueryDefaultCompatDialog"));
271  if (xQueryBox->run() != RET_YES)
272  return;
273 
274  auto pItem = std::find_if(m_pImpl->m_aList.begin(), m_pImpl->m_aList.end(),
275  [](const SvtCompatibilityEntry& rItem) { return rItem.isDefaultEntry(); });
276  if (pItem != m_pImpl->m_aList.end())
277  {
278  const sal_Int32 nCount = m_xOptionsLB->n_children();
279  for ( sal_Int32 i = 0; i < nCount; ++i )
280  {
281  bool bChecked = m_xOptionsLB->get_toggle(i);
282 
283  int nCoptIdx = i + 2; /* Consider "Name" & "Module" indexes */
284  pItem->setValue<bool>( SvtCompatibilityEntry::Index(nCoptIdx), bChecked );
285  }
286  }
287 
288  WriteOptions();
289 }
290 
291 void SwCompatibilityOptPage::SetCurrentOptions( sal_uInt32 nOptions )
292 {
293  const int nCount = m_xOptionsLB->n_children();
294  OSL_ENSURE( nCount <= 32, "SwCompatibilityOptPage::Reset(): entry overflow" );
295  for (int i = 0; i < nCount; ++i)
296  {
297  bool bChecked = ( ( nOptions & 0x00000001 ) == 0x00000001 );
298  m_xOptionsLB->set_toggle(i, bChecked ? TRISTATE_TRUE : TRISTATE_FALSE);
299  nOptions = nOptions >> 1;
300  }
301 }
302 
304 {
305  sal_uInt32 nRet = 0;
306  if ( m_pWrtShell )
307  {
308  const IDocumentSettingAccess& rIDocumentSettingAccess = m_pWrtShell->getIDocumentSettingAccess();
310  !rIDocumentSettingAccess.get( DocumentSettingId::USE_VIRTUAL_DEVICE ),
311  rIDocumentSettingAccess.get( DocumentSettingId::PARA_SPACE_MAX ),
312  rIDocumentSettingAccess.get( DocumentSettingId::PARA_SPACE_MAX_AT_PAGES ),
313  !rIDocumentSettingAccess.get( DocumentSettingId::TAB_COMPAT ),
314  !rIDocumentSettingAccess.get( DocumentSettingId::ADD_EXT_LEADING ),
315  rIDocumentSettingAccess.get( DocumentSettingId::OLD_LINE_SPACING ),
316  rIDocumentSettingAccess.get( DocumentSettingId::ADD_PARA_SPACING_TO_TABLE_CELLS ),
317  rIDocumentSettingAccess.get( DocumentSettingId::USE_FORMER_OBJECT_POS ),
318  rIDocumentSettingAccess.get( DocumentSettingId::USE_FORMER_TEXT_WRAPPING ),
319  rIDocumentSettingAccess.get( DocumentSettingId::CONSIDER_WRAP_ON_OBJECT_POSITION ),
321  rIDocumentSettingAccess.get( DocumentSettingId::PROTECT_FORM ),
322  rIDocumentSettingAccess.get( DocumentSettingId::MS_WORD_COMP_TRAILING_BLANKS ),
323  rIDocumentSettingAccess.get( DocumentSettingId::SUBTRACT_FLYS ),
324  rIDocumentSettingAccess.get( DocumentSettingId::EMPTY_DB_FIELD_HIDES_PARA ) );
325  }
326  return nRet;
327 }
328 
330 {
332  for ( const auto& rItem : m_pImpl->m_aList )
333  m_aConfigItem.AppendItem(rItem);
334 }
335 
336 std::unique_ptr<SfxTabPage> SwCompatibilityOptPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrSet)
337 {
338  return std::make_unique<SwCompatibilityOptPage>(pPage, pController, *rAttrSet);
339 }
340 
342 {
343  bool bModified = false;
344  if ( m_pWrtShell )
345  {
346  sal_uInt32 nSavedOptions = m_nSavedOptions;
347  const int nCount = m_xOptionsLB->n_children();
348  OSL_ENSURE( nCount <= 32, "SwCompatibilityOptPage::Reset(): entry overflow" );
349 
350  for (int i = 0; i < nCount; ++i)
351  {
352  bool bChecked = m_xOptionsLB->get_toggle(i) == TRISTATE_TRUE;
353  bool bSavedChecked = ( ( nSavedOptions & 0x00000001 ) == 0x00000001 );
354  if ( bChecked != bSavedChecked )
355  {
356  int nCoptIdx = i + 2; /* Consider "Name" & "Module" indexes */
357  switch ( SvtCompatibilityEntry::Index(nCoptIdx) )
358  {
360  m_pWrtShell->SetUseVirDev( !bChecked );
361  break;
362 
364  m_pWrtShell->SetParaSpaceMax( bChecked );
365  break;
366 
368  m_pWrtShell->SetParaSpaceMaxAtPages( bChecked );
369  break;
370 
372  m_pWrtShell->SetTabCompat( !bChecked );
373  break;
374 
376  m_pWrtShell->SetAddExtLeading( !bChecked );
377  break;
378 
381  break;
382 
385  break;
386 
389  break;
390 
393  break;
394 
397  break;
398 
401  break;
402 
404  m_pWrtShell->SetProtectForm( bChecked );
405  break;
406 
409  break;
410 
413  break;
414 
417  break;
418 
419  default:
420  break;
421  }
422  bModified = true;
423  }
424 
425  nSavedOptions = nSavedOptions >> 1;
426  }
427  }
428 
429  if ( bModified )
430  WriteOptions();
431 
432  bool bNewMSFormsMenuOption = m_xGlobalOptionsCLB->get_toggle(0);
433  if (m_bSavedMSFormsMenuOption != bNewMSFormsMenuOption)
434  {
435  std::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create());
436  officecfg::Office::Compatibility::View::MSCompatibleFormsMenu::set(bNewMSFormsMenuOption, batch);
437  batch->commit();
438 
439  m_bSavedMSFormsMenuOption = bNewMSFormsMenuOption;
440  bModified = true;
441 
442  // Show a message about that the option needs a restart to be applied
443  {
444  SolarMutexGuard aGuard;
447  {
448  GetDialogController()->response(RET_OK);
449  }
450  }
451  }
452 
453  return bModified;
454 }
455 
457 {
458  m_xOptionsLB->select(0);
459 
460  sal_uInt32 nOptions = GetDocumentOptions();
461  SetCurrentOptions( nOptions );
462  m_nSavedOptions = nOptions;
463 
466 }
467 
468 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
void SetAddExtLeading(bool bNew)
Definition: viewsh.cxx:791
virtual SfxObjectFactory & GetFactory() const =0
void SetMsWordCompTrailingBlanks(bool _bMsWordCompTrailingBlanks)
Definition: viewsh.cxx:910
SvtCompatibilityOptions m_aConfigItem
Definition: optcomp.hxx:34
OUString GetTitle(sal_uInt16 nMaxLen=0) const
void SetDoNotJustifyLinesWithManualBreak(bool _bDoNotJustifyLinesWithManualBreak)
Definition: viewsh.cxx:892
void SetParaSpaceMaxAtPages(bool bNew)
Definition: viewsh.cxx:767
void SetAddParaSpacingToTableCells(bool _bAddParaSpacingToTableCells)
Sets if paragraph and table spacing is added at bottom of table cells.
Definition: viewsh.cxx:822
css::uno::Sequence< css::uno::Sequence< css::beans::PropertyValue > > GetList() const
std::unique_ptr< weld::ComboBox > m_xGlobalOptionsLB
Definition: optcomp.hxx:49
static weld::Builder * CreateBuilder(weld::Widget *pParent, const OUString &rUIFile, bool bMobile=false)
static Index getIndex(const OUString &rName)
const IDocumentSettingAccess & getIDocumentSettingAccess() const
Provides access to the document setting interface.
Definition: viewsh.cxx:2583
static OUString getUserEntryName()
IMPL_LINK_NOARG(SwCompatibilityOptPage, SelectHdl, weld::ComboBox &, void)
Definition: optcomp.cxx:261
bool bReadOnly
TRISTATE_TRUE
std::unique_ptr< weld::TreeView > m_xGlobalOptionsCLB
Definition: optcomp.hxx:51
virtual ~SwCompatibilityOptPage() override
Definition: optcomp.cxx:105
virtual void Reset(const SfxItemSet *rSet) override
Definition: optcomp.cxx:456
void SetUseFormerTextWrapping(bool _bUseFormerTextWrapping)
Definition: viewsh.cxx:879
void SetCurrentOptions(sal_uInt32 nOptions)
Definition: optcomp.cxx:291
const SwView & GetView() const
Definition: wrtsh.hxx:428
RET_YES
std::unique_ptr< weld::Frame > m_xGlobalOptionsFrame
Definition: optcomp.hxx:47
int nCount
void SetEmptyDbFieldHidesPara(bool bEmptyDbFieldHidesPara)
Definition: viewsh.cxx:928
static std::shared_ptr< ConfigurationChanges > create(css::uno::Reference< css::uno::XComponentContext > const &context=comphelper::getProcessComponentContext())
std::vector< SvtCompatibilityEntry > m_aList
Definition: optcomp.cxx:47
std::unique_ptr< weld::Button > m_xDefaultPB
Definition: optcomp.hxx:52
weld::Window * GetFrameWeld(const SfxFrame *pFrame)
Definition: dialoghelp.cxx:20
SVT_DLLPUBLIC bool executeRestartDialog(css::uno::Reference< css::uno::XComponentContext > const &context, weld::Window *parent, RestartReason reason)
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
int i
SwCompatibilityOptPage(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet &rSet)
Definition: optcomp.cxx:50
static sal_uInt32 convertBools2Ulong_Impl(bool _bUsePrtMetrics, bool _bAddSpacing, bool _bAddSpacingAtPages, bool _bUseOurTabStops, bool _bNoExtLeading, bool _bUseLineSpacing, bool _bAddTableSpacing, bool _bUseObjPos, bool _bUseOurTextWrapping, bool _bConsiderWrappingStyle, bool _bExpandWordSpace, bool _bProtectForm, bool _bMsWordCompTrailingBlanks, bool bSubtractFlysAnchoredAtFlys, bool bEmptyDbFieldHidesPara)
Definition: optcomp.cxx:110
#define FN_PARAM_WRTSHELL
Definition: cmdid.h:791
TRISTATE_FALSE
bool isDefaultEntry() const
std::unique_ptr< weld::Frame > m_xMain
Definition: optcomp.hxx:46
void setValue(const Index rIdx, css::uno::Any const &rValue)
Provides access to settings of a document.
bool m_bSavedMSFormsMenuOption
Definition: optcomp.hxx:43
static OUString getDefaultEntryName()
SwWrtShell * m_pWrtShell
Definition: optcomp.hxx:38
std::unique_ptr< weld::ComboBox > m_xFormattingLB
Definition: optcomp.hxx:48
void InitControls(const SfxItemSet &rSet)
Definition: optcomp.cxx:179
void AppendItem(const SvtCompatibilityEntry &aItem)
SwDocShell * GetDocShell()
Definition: view.cxx:1108
sal_uInt32 m_nSavedOptions
Definition: optcomp.hxx:42
void SetConsiderWrapOnObjPos(bool _bConsiderWrapOnObjPos)
Definition: viewsh.cxx:868
weld::Window * GetFrameWeld() const
void SetUseFormerLineSpacing(bool _bUseFormerLineSpacing)
Sets if former formatting of text lines with proportional line spacing should used.
Definition: viewsh.cxx:839
std::unique_ptr< SwCompatibilityOptPage_Impl > m_pImpl
Definition: optcomp.hxx:40
void SetUseFormerObjectPositioning(bool _bUseFormerObjPos)
Sets IDocumentSettingAccess if former object positioning should be used.
Definition: viewsh.cxx:856
std::shared_ptr< const SfxFilter > GetFilter4FilterName(const OUString &rName, SfxFilterFlags nMust=SfxFilterFlags::NONE, SfxFilterFlags nDont=SFX_FILTER_NOTINSTALLED) const
RET_OK
Reference< XComponentContext > getProcessComponentContext()
void SetUseVirDev(bool bNew)
Definition: viewsh.cxx:806
SfxOkDialogController * GetDialogController() const
void SetParaSpaceMax(bool bNew)
Definition: viewsh.cxx:755
virtual bool get(DocumentSettingId id) const =0
Return the specified document setting.
sal_uInt32 GetDocumentOptions() const
Definition: optcomp.cxx:303
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: optcomp.cxx:336
std::unique_ptr< weld::TreeView > m_xOptionsLB
Definition: optcomp.hxx:50
void SetSubtractFlysAnchoredAtFlys(bool bSubtractFlysAnchoredAtFlys)
Definition: viewsh.cxx:922
css::uno::Any getValue(const Index rIdx) const
RESTART_REASON_MSCOMPATIBLE_FORMS_MENU
void setDefaultEntry(bool rValue)
virtual bool FillItemSet(SfxItemSet *rSet) override
Definition: optcomp.cxx:341
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl
SfxFilterContainer * GetFilterContainer() const
void SetProtectForm(bool _bProtectForm)
Definition: viewsh.cxx:904
void SetTabCompat(bool bNew)
Definition: viewsh.cxx:779
sal_uInt16 nPos