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  std::vector<int> aWidths;
65  aWidths.push_back(m_xOptionsLB->get_checkbox_column_width());
66  m_xOptionsLB->set_column_fixed_widths(aWidths);
67  m_xGlobalOptionsCLB->set_column_fixed_widths(aWidths);
68 
69  int nPos = 0;
70  for ( int i = static_cast<int>(SvtCompatibilityEntry::Index::Module) + 1; i < static_cast<int>(SvtCompatibilityEntry::Index::INVALID); ++i )
71  {
72  int nCoptIdx = i - 2; /* Do not consider "Name" & "Module" indexes */
73 
74  const OUString sEntry = m_xFormattingLB->get_text(nCoptIdx);
75  m_xOptionsLB->append();
76  m_xOptionsLB->set_toggle(nPos, TRISTATE_FALSE, 0);
77  m_xOptionsLB->set_text(nPos, sEntry, 1);
78  ++nPos;
79  }
80 
81  m_sUserEntry = m_xFormattingLB->get_text(m_xFormattingLB->get_count() - 1);
82 
83  m_xFormattingLB->clear();
84 
85  // Set MSOCompatibleFormsMenu entry attributes
86  const bool bReadOnly = officecfg::Office::Compatibility::View::MSCompatibleFormsMenu::isReadOnly();
87  m_xGlobalOptionsCLB->set_sensitive(!bReadOnly);
88 
89  m_xGlobalOptionsCLB->append();
90  const bool bChecked = m_aViewConfigItem.HasMSOCompatibleFormsMenu();
91  m_xGlobalOptionsCLB->set_toggle(0, bChecked ? TRISTATE_TRUE : TRISTATE_FALSE, 0);
92  m_xGlobalOptionsCLB->set_text(0, m_xGlobalOptionsLB->get_text(0), 1);
93 
94  m_xGlobalOptionsLB->clear();
95 
96  // tdf#125799, we let only the doc options grow/shrink but give this one more than its bare
97  // min request height because there's only one row in it and that looks somewhat abrupt
98  m_xGlobalOptionsCLB->set_size_request(-1, m_xGlobalOptionsCLB->get_preferred_size().Height() * 2);
99 
100  InitControls( rSet );
101 
102  // set handler
103  m_xFormattingLB->connect_changed( LINK( this, SwCompatibilityOptPage, SelectHdl ) );
104  m_xDefaultPB->connect_clicked( LINK( this, SwCompatibilityOptPage, UseAsDefaultHdl ) );
105 }
106 
108 {
109 }
110 
111 static sal_uInt32 convertBools2Ulong_Impl
112 (
113  bool _bUsePrtMetrics,
114  bool _bAddSpacing,
115  bool _bAddSpacingAtPages,
116  bool _bUseOurTabStops,
117  bool _bNoExtLeading,
118  bool _bUseLineSpacing,
119  bool _bAddTableSpacing,
120  bool _bUseObjPos,
121  bool _bUseOurTextWrapping,
122  bool _bConsiderWrappingStyle,
123  bool _bExpandWordSpace,
124  bool _bProtectForm,
125  bool _bMsWordCompTrailingBlanks,
126  bool bSubtractFlysAnchoredAtFlys,
127  bool bEmptyDbFieldHidesPara
128 )
129 {
130  sal_uInt32 nRet = 0;
131  sal_uInt32 nSetBit = 1;
132 
133  if ( _bUsePrtMetrics )
134  nRet |= nSetBit;
135  nSetBit = nSetBit << 1;
136  if ( _bAddSpacing )
137  nRet |= nSetBit;
138  nSetBit = nSetBit << 1;
139  if ( _bAddSpacingAtPages )
140  nRet |= nSetBit;
141  nSetBit = nSetBit << 1;
142  if ( _bUseOurTabStops )
143  nRet |= nSetBit;
144  nSetBit = nSetBit << 1;
145  if ( _bNoExtLeading )
146  nRet |= nSetBit;
147  nSetBit = nSetBit << 1;
148  if ( _bUseLineSpacing )
149  nRet |= nSetBit;
150  nSetBit = nSetBit << 1;
151  if ( _bAddTableSpacing )
152  nRet |= nSetBit;
153  nSetBit = nSetBit << 1;
154  if ( _bUseObjPos )
155  nRet |= nSetBit;
156  nSetBit = nSetBit << 1;
157  if ( _bUseOurTextWrapping )
158  nRet |= nSetBit;
159  nSetBit = nSetBit << 1;
160  if ( _bConsiderWrappingStyle )
161  nRet |= nSetBit;
162  nSetBit = nSetBit << 1;
163  if ( _bExpandWordSpace )
164  nRet |= nSetBit;
165  nSetBit = nSetBit << 1;
166  if ( _bProtectForm )
167  nRet |= nSetBit;
168  nSetBit = nSetBit << 1;
169  if ( _bMsWordCompTrailingBlanks )
170  nRet |= nSetBit;
171  nSetBit = nSetBit << 1;
172  if (bSubtractFlysAnchoredAtFlys)
173  nRet |= nSetBit;
174  nSetBit = nSetBit << 1;
175  if (bEmptyDbFieldHidesPara)
176  nRet |= nSetBit;
177 
178  return nRet;
179 }
180 
182 {
183  // init objectshell and detect document name
184  OUString sDocTitle;
185  const SfxPoolItem* pItem = nullptr;
186  SfxObjectShell* pObjShell = nullptr;
187  if ( SfxItemState::SET == rSet.GetItemState( FN_PARAM_WRTSHELL, false, &pItem ) )
188  m_pWrtShell = static_cast<SwWrtShell*>(static_cast<const SwPtrItem*>(pItem)->GetValue());
189  if ( m_pWrtShell )
190  {
191  pObjShell = m_pWrtShell->GetView().GetDocShell();
192  if ( pObjShell )
193  sDocTitle = pObjShell->GetTitle();
194  }
195  else
196  {
197  m_xMain->set_sensitive(false);
198  m_xGlobalOptionsFrame->set_sensitive(false);
199  }
200  const OUString& rText = m_xMain->get_label();
201  m_xMain->set_label(rText.replaceAll("%DOCNAME", sDocTitle));
202 
203  // loading file formats
204  const Sequence< Sequence< PropertyValue > > aList = m_aConfigItem.GetList();
205 
206  SvtCompatibilityEntry aEntry;
208 
209  for ( const Sequence< PropertyValue >& rEntry : aList )
210  {
211  for ( const PropertyValue& aValue : rEntry )
212  {
213  aEntry.setValue( SvtCompatibilityEntry::getIndex(aValue.Name), aValue.Value );
214  }
215 
216  const OUString sEntryName = aEntry.getValue<OUString>( SvtCompatibilityEntry::Index::Name );
217 
218  const bool bIsUserEntry = ( sEntryName == SvtCompatibilityEntry::getUserEntryName() );
219  const bool bIsDefaultEntry = ( sEntryName == SvtCompatibilityEntry::getDefaultEntryName() );
220 
221  aEntry.setDefaultEntry( bIsDefaultEntry );
222 
223  m_pImpl->m_aList.push_back( aEntry );
224 
225  if ( aEntry.isDefaultEntry() )
226  continue;
227 
228  OUString sNewEntry;
229  if ( bIsUserEntry )
230  sNewEntry = m_sUserEntry;
231 
232  else if ( pObjShell && !sEntryName.isEmpty() )
233  {
234  SfxFilterContainer* pFacCont = pObjShell->GetFactory().GetFilterContainer();
235  std::shared_ptr<const SfxFilter> pFilter = pFacCont->GetFilter4FilterName( sEntryName );
236  if ( pFilter )
237  sNewEntry = pFilter->GetUIName();
238  }
239 
240  if ( sNewEntry.isEmpty() )
241  sNewEntry = sEntryName;
242 
243  sal_uInt32 nOptions = convertBools2Ulong_Impl(
254  aEntry.getValue<bool>( SvtCompatibilityEntry::Index::ExpandWordSpace ),
259  m_xFormattingLB->append(OUString::number(nOptions), sNewEntry);
260  }
261 }
262 
264 {
265  sal_uInt32 nOptions = m_xFormattingLB->get_active_id().toUInt32();
266  SetCurrentOptions(nOptions);
267 }
268 
270 {
271  std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(GetFrameWeld(), "modules/swriter/ui/querydefaultcompatdialog.ui"));
272  std::unique_ptr<weld::MessageDialog> xQueryBox(xBuilder->weld_message_dialog("QueryDefaultCompatDialog"));
273  if (xQueryBox->run() == RET_YES)
274  {
275  auto pItem = std::find_if(m_pImpl->m_aList.begin(), m_pImpl->m_aList.end(),
276  [](const SvtCompatibilityEntry& rItem) { return rItem.isDefaultEntry(); });
277  if (pItem != m_pImpl->m_aList.end())
278  {
279  const sal_Int32 nCount = m_xOptionsLB->n_children();
280  for ( sal_Int32 i = 0; i < nCount; ++i )
281  {
282  bool bChecked = m_xOptionsLB->get_toggle(i, 0);
283 
284  int nCoptIdx = i + 2; /* Consider "Name" & "Module" indexes */
285  pItem->setValue<bool>( SvtCompatibilityEntry::Index(nCoptIdx), bChecked );
286  }
287  }
288 
289  WriteOptions();
290  }
291 }
292 
293 void SwCompatibilityOptPage::SetCurrentOptions( sal_uInt32 nOptions )
294 {
295  const int nCount = m_xOptionsLB->n_children();
296  OSL_ENSURE( nCount <= 32, "SwCompatibilityOptPage::Reset(): entry overflow" );
297  for (int i = 0; i < nCount; ++i)
298  {
299  bool bChecked = ( ( nOptions & 0x00000001 ) == 0x00000001 );
300  m_xOptionsLB->set_toggle(i, bChecked ? TRISTATE_TRUE : TRISTATE_FALSE, 0);
301  nOptions = nOptions >> 1;
302  }
303 }
304 
306 {
307  sal_uInt32 nRet = 0;
308  if ( m_pWrtShell )
309  {
310  const IDocumentSettingAccess& rIDocumentSettingAccess = m_pWrtShell->getIDocumentSettingAccess();
312  !rIDocumentSettingAccess.get( DocumentSettingId::USE_VIRTUAL_DEVICE ),
313  rIDocumentSettingAccess.get( DocumentSettingId::PARA_SPACE_MAX ),
314  rIDocumentSettingAccess.get( DocumentSettingId::PARA_SPACE_MAX_AT_PAGES ),
315  !rIDocumentSettingAccess.get( DocumentSettingId::TAB_COMPAT ),
316  !rIDocumentSettingAccess.get( DocumentSettingId::ADD_EXT_LEADING ),
317  rIDocumentSettingAccess.get( DocumentSettingId::OLD_LINE_SPACING ),
318  rIDocumentSettingAccess.get( DocumentSettingId::ADD_PARA_SPACING_TO_TABLE_CELLS ),
319  rIDocumentSettingAccess.get( DocumentSettingId::USE_FORMER_OBJECT_POS ),
320  rIDocumentSettingAccess.get( DocumentSettingId::USE_FORMER_TEXT_WRAPPING ),
321  rIDocumentSettingAccess.get( DocumentSettingId::CONSIDER_WRAP_ON_OBJECT_POSITION ),
323  rIDocumentSettingAccess.get( DocumentSettingId::PROTECT_FORM ),
324  rIDocumentSettingAccess.get( DocumentSettingId::MS_WORD_COMP_TRAILING_BLANKS ),
325  rIDocumentSettingAccess.get( DocumentSettingId::SUBTRACT_FLYS ),
326  rIDocumentSettingAccess.get( DocumentSettingId::EMPTY_DB_FIELD_HIDES_PARA ) );
327  }
328  return nRet;
329 }
330 
332 {
334  for ( const auto& rItem : m_pImpl->m_aList )
335  m_aConfigItem.AppendItem(rItem);
336 }
337 
338 std::unique_ptr<SfxTabPage> SwCompatibilityOptPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrSet)
339 {
340  return std::make_unique<SwCompatibilityOptPage>(pPage, pController, *rAttrSet);
341 }
342 
344 {
345  bool bModified = false;
346  if ( m_pWrtShell )
347  {
348  sal_uInt32 nSavedOptions = m_nSavedOptions;
349  const int nCount = m_xOptionsLB->n_children();
350  OSL_ENSURE( nCount <= 32, "SwCompatibilityOptPage::Reset(): entry overflow" );
351 
352  for (int i = 0; i < nCount; ++i)
353  {
354  bool bChecked = m_xOptionsLB->get_toggle(i, 0) == TRISTATE_TRUE;
355  bool bSavedChecked = ( ( nSavedOptions & 0x00000001 ) == 0x00000001 );
356  if ( bChecked != bSavedChecked )
357  {
358  int nCoptIdx = i + 2; /* Consider "Name" & "Module" indexes */
359  switch ( SvtCompatibilityEntry::Index(nCoptIdx) )
360  {
362  m_pWrtShell->SetUseVirDev( !bChecked );
363  break;
364 
366  m_pWrtShell->SetParaSpaceMax( bChecked );
367  break;
368 
370  m_pWrtShell->SetParaSpaceMaxAtPages( bChecked );
371  break;
372 
374  m_pWrtShell->SetTabCompat( !bChecked );
375  break;
376 
378  m_pWrtShell->SetAddExtLeading( !bChecked );
379  break;
380 
383  break;
384 
387  break;
388 
391  break;
392 
395  break;
396 
399  break;
400 
403  break;
404 
406  m_pWrtShell->SetProtectForm( bChecked );
407  break;
408 
411  break;
412 
415  break;
416 
419  break;
420 
421  default:
422  break;
423  }
424  bModified = true;
425  }
426 
427  nSavedOptions = nSavedOptions >> 1;
428  }
429  }
430 
431  if ( bModified )
432  WriteOptions();
433 
434  bool bNewMSFormsMenuOption = m_xGlobalOptionsCLB->get_toggle(0, 0);
435  if (m_bSavedMSFormsMenuOption != bNewMSFormsMenuOption)
436  {
437  m_aViewConfigItem.SetMSOCompatibleFormsMenu(bNewMSFormsMenuOption);
438  m_bSavedMSFormsMenuOption = bNewMSFormsMenuOption;
439  bModified = true;
440 
441  // Show a message about that the option needs a restart to be applied
442  {
443  SolarMutexGuard aGuard;
446  {
447  GetDialogController()->response(RET_OK);
448  }
449  }
450  }
451 
452  return bModified;
453 }
454 
456 {
457  m_xOptionsLB->select(0);
458 
459  sal_uInt32 nOptions = GetDocumentOptions();
460  SetCurrentOptions( nOptions );
461  m_nSavedOptions = nOptions;
462 
465 }
466 
467 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetMSOCompatibleFormsMenu(bool bSet)
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:35
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:52
static Index getIndex(const OUString &rName)
const IDocumentSettingAccess & getIDocumentSettingAccess() const
Provides access to the document setting interface.
Definition: viewsh.cxx:2585
static OUString getUserEntryName()
IMPL_LINK_NOARG(SwCompatibilityOptPage, SelectHdl, weld::ComboBox &, void)
Definition: optcomp.cxx:263
bool bReadOnly
TRISTATE_TRUE
std::unique_ptr< weld::TreeView > m_xGlobalOptionsCLB
Definition: optcomp.hxx:54
virtual ~SwCompatibilityOptPage() override
Definition: optcomp.cxx:107
virtual void Reset(const SfxItemSet *rSet) override
Definition: optcomp.cxx:455
void SetUseFormerTextWrapping(bool _bUseFormerTextWrapping)
Definition: viewsh.cxx:879
void SetCurrentOptions(sal_uInt32 nOptions)
Definition: optcomp.cxx:293
const SwView & GetView() const
Definition: wrtsh.hxx:428
RET_YES
std::unique_ptr< weld::Frame > m_xGlobalOptionsFrame
Definition: optcomp.hxx:50
int nCount
void SetEmptyDbFieldHidesPara(bool bEmptyDbFieldHidesPara)
Definition: viewsh.cxx:928
std::vector< SvtCompatibilityEntry > m_aList
Definition: optcomp.cxx:47
std::unique_ptr< weld::Button > m_xDefaultPB
Definition: optcomp.hxx:55
bool HasMSOCompatibleFormsMenu() const
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:112
#define FN_PARAM_WRTSHELL
Definition: cmdid.h:786
TRISTATE_FALSE
bool isDefaultEntry() const
std::unique_ptr< weld::Frame > m_xMain
Definition: optcomp.hxx:49
void setValue(const Index rIdx, css::uno::Any const &rValue)
Provides access to settings of a document.
bool m_bSavedMSFormsMenuOption
Definition: optcomp.hxx:46
static OUString getDefaultEntryName()
SwWrtShell * m_pWrtShell
Definition: optcomp.hxx:41
std::unique_ptr< weld::ComboBox > m_xFormattingLB
Definition: optcomp.hxx:51
void InitControls(const SfxItemSet &rSet)
Definition: optcomp.cxx:181
void AppendItem(const SvtCompatibilityEntry &aItem)
SwDocShell * GetDocShell()
Definition: view.cxx:1107
sal_uInt32 m_nSavedOptions
Definition: optcomp.hxx:45
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:43
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:305
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: optcomp.cxx:338
std::unique_ptr< weld::TreeView > m_xOptionsLB
Definition: optcomp.hxx:53
static weld::Builder * CreateBuilder(weld::Widget *pParent, const OUString &rUIFile)
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:343
SvtCompatibilityViewOptions m_aViewConfigItem
Definition: optcomp.hxx:37
::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