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