LibreOffice Module sw (master)  1
PageFormatPanel.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 #include <sal/config.h>
20 #include <sal/log.hxx>
21 #include <swtypes.hxx>
22 #include <svl/intitem.hxx>
23 #include <editeng/sizeitem.hxx>
24 #include <editeng/paperinf.hxx>
25 #include <svx/dlgutil.hxx>
26 #include <svx/rulritem.hxx>
27 #include <svx/svdtrans.hxx>
28 #include "PageFormatPanel.hxx"
29 #include "PageMarginUtils.hxx"
30 #include <sfx2/dispatch.hxx>
31 #include <sfx2/bindings.hxx>
32 #include <sfx2/module.hxx>
33 #include <sfx2/objsh.hxx>
34 #include <sfx2/viewfrm.hxx>
35 #include <pageformatpanel.hrc>
36 #include <cmdid.h>
39 #include <vcl/settings.hxx>
40 #include <vcl/svapp.hxx>
41 
42 #include <com/sun/star/lang/IllegalArgumentException.hpp>
43 
44 namespace sw::sidebar{
45 
47  vcl::Window* pParent,
48  const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rxFrame,
49  SfxBindings* pBindings)
50 {
51  if( pParent == nullptr )
52  throw ::com::sun::star::lang::IllegalArgumentException("no parent window given to PageFormatPanel::Create", nullptr, 0);
53  if( !rxFrame.is() )
54  throw ::com::sun::star::lang::IllegalArgumentException("no XFrame given to PageFormatPanel::Create", nullptr, 0);
55 
56  return VclPtr<PageFormatPanel>::Create(pParent, rxFrame, pBindings);
57 }
58 
60 {
61  auto nSelected = mxMarginSelectBox->get_active();
62  mxMarginSelectBox->clear();
63 
65  if (IsInch(meFUnit))
66  {
67  OUString sSuffix = weld::MetricSpinButton::MetricToString(FieldUnit::INCH);
68  for (size_t i = 0; i < SAL_N_ELEMENTS(RID_PAGEFORMATPANEL_MARGINS_INCH); ++i)
69  {
70  OUString sStr = rLocaleData.getNum(RID_PAGEFORMATPANEL_MARGINS_INCH[i].second, 2, true, false) + sSuffix;
71  mxMarginSelectBox->append_text(SwResId(RID_PAGEFORMATPANEL_MARGINS_INCH[i].first).replaceFirst("%1", sStr));
72  }
73  }
74  else
75  {
76  OUString sSuffix = weld::MetricSpinButton::MetricToString(FieldUnit::CM);
77  for (size_t i = 0; i < SAL_N_ELEMENTS(RID_PAGEFORMATPANEL_MARGINS_CM); ++i)
78  {
79  OUString sStr = rLocaleData.getNum(RID_PAGEFORMATPANEL_MARGINS_CM[i].second, 2, true, false) + " " + sSuffix;
80  mxMarginSelectBox->append_text(SwResId(RID_PAGEFORMATPANEL_MARGINS_CM[i].first).replaceFirst("%1", sStr));
81  }
82  }
83  mxMarginSelectBox->set_active(nSelected);
84 }
85 
87  vcl::Window* pParent,
88  const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rxFrame,
89  SfxBindings* pBindings) :
90  PanelLayout(pParent, "PageFormatPanel", "modules/swriter/ui/pageformatpanel.ui", rxFrame),
91  mpBindings( pBindings ),
92  mxPaperSizeBox(new SvxPaperSizeListBox(m_xBuilder->weld_combo_box("papersize"))),
93  mxPaperWidth(new SvxRelativeField(m_xBuilder->weld_metric_spin_button("paperwidth", FieldUnit::CM))),
94  mxPaperHeight(new SvxRelativeField(m_xBuilder->weld_metric_spin_button("paperheight", FieldUnit::CM))),
95  mxPaperOrientation(m_xBuilder->weld_combo_box("paperorientation")),
96  mxMarginSelectBox(m_xBuilder->weld_combo_box("marginLB")),
97  mxCustomEntry(m_xBuilder->weld_label("customlabel")),
98  maPaperSizeController(SID_ATTR_PAGE_SIZE, *pBindings, *this),
99  maPaperOrientationController(SID_ATTR_PAGE, *pBindings, *this),
100  maMetricController(SID_ATTR_METRIC, *pBindings,*this),
101  maSwPageLRControl(SID_ATTR_PAGE_LRSPACE, *pBindings, *this),
102  maSwPageULControl(SID_ATTR_PAGE_ULSPACE, *pBindings, *this),
103  mpPageItem( new SvxPageItem(SID_ATTR_PAGE) ),
104  mpPageLRMarginItem( new SvxLongLRSpaceItem( 0, 0, SID_ATTR_PAGE_LRSPACE ) ),
105  mpPageULMarginItem( new SvxLongULSpaceItem( 0, 0, SID_ATTR_PAGE_ULSPACE ) ),
106  meFUnit(GetModuleFieldUnit()),
107  meUnit(),
108  aCustomEntry()
109 {
110  Initialize();
111 }
112 
114 {
115  disposeOnce();
116 }
117 
119 {
120  mxPaperSizeBox.reset();
121  mxPaperWidth.reset();
122  mxPaperHeight.reset();
123  mxPaperOrientation.reset();
124  mxMarginSelectBox.reset();
125  mxCustomEntry.reset();
126 
132  mpPageULMarginItem.reset();
133  mpPageLRMarginItem.reset();
134  mpPageItem.reset();
135 
137 }
138 
140 {
141  mxPaperSizeBox->FillPaperSizeEntries( PaperSizeApp::Std );
143  mxPaperWidth->SetFieldUnit(meFUnit);
144  mxPaperHeight->SetFieldUnit(meFUnit);
146  aCustomEntry = mxCustomEntry->get_label();
147 
148  const SvtOptionsDrawinglayer aDrawinglayerOpt;
149  mxPaperWidth->set_max(mxPaperWidth->normalize(aDrawinglayerOpt.GetMaximumPaperWidth()), FieldUnit::CM);
150  mxPaperHeight->set_max(mxPaperHeight->normalize(aDrawinglayerOpt.GetMaximumPaperHeight()), FieldUnit::CM);
151 
152  mxPaperSizeBox->connect_changed( LINK(this, PageFormatPanel, PaperFormatModifyHdl ));
153  mxPaperOrientation->connect_changed( LINK(this, PageFormatPanel, PaperFormatModifyHdl ));
154  mxPaperHeight->connect_value_changed( LINK(this, PageFormatPanel, PaperSizeModifyHdl ));
155  mxPaperWidth->connect_value_changed( LINK(this, PageFormatPanel, PaperSizeModifyHdl ));
156  mxMarginSelectBox->connect_changed( LINK(this, PageFormatPanel, PaperModifyMarginHdl));
157 
158  mpBindings->Update(SID_ATTR_METRIC);
159  mpBindings->Update(SID_ATTR_PAGE);
160  mpBindings->Update(SID_ATTR_PAGE_SIZE);
161  mpBindings->Update(SID_ATTR_PAGE_LRSPACE);
162  mpBindings->Update(SID_ATTR_PAGE_ULSPACE);
163 
164  UpdateMarginBox();
165 }
166 
168  const sal_uInt16 nSId,
169  const SfxItemState eState,
170  const SfxPoolItem* pState)
171 {
172  switch(nSId)
173  {
174  case SID_ATTR_PAGE_SIZE:
175  {
176  const SvxSizeItem* pSizeItem = nullptr;
177  if (eState >= SfxItemState::DEFAULT)
178  pSizeItem = dynamic_cast< const SvxSizeItem* >(pState);
179  if (pSizeItem)
180  {
181  Size aPaperSize = pSizeItem->GetSize();
182 
183  mxPaperWidth->set_value(mxPaperWidth->normalize(aPaperSize.Width()), FieldUnit::TWIP);
184  mxPaperHeight->set_value(mxPaperHeight->normalize(aPaperSize.Height()), FieldUnit::TWIP);
185 
186  if(mxPaperOrientation->get_active() == 1)
187  Swap(aPaperSize);
188 
189  Paper ePaper = SvxPaperInfo::GetSvxPaper(aPaperSize, meUnit);
190  mxPaperSizeBox->set_active_id( ePaper );
191  }
192  }
193  break;
194  case SID_ATTR_METRIC:
195  {
197  FieldUnit eFUnit = GetCurrentUnit(eState, pState);
198  if (eFUnit != meFUnit)
199  {
200  meFUnit = eFUnit;
201  mxPaperHeight->SetFieldUnit(meFUnit);
202  mxPaperWidth->SetFieldUnit(meFUnit);
204  UpdateMarginBox();
205  }
206  }
207  break;
208  case SID_ATTR_PAGE:
209  {
210  if ( eState >= SfxItemState::DEFAULT &&
211  dynamic_cast< const SvxPageItem *>( pState ) )
212  {
213  mpPageItem.reset( static_cast<SvxPageItem*>(pState->Clone()) );
214  if ( mpPageItem->IsLandscape() )
215  mxPaperOrientation->set_active(1);
216  else
217  mxPaperOrientation->set_active(0);
218  }
219  }
220  break;
221  case SID_ATTR_PAGE_LRSPACE:
222  {
223  if ( eState >= SfxItemState::DEFAULT &&
224  dynamic_cast< const SvxLongLRSpaceItem *>( pState ) )
225  {
226  mpPageLRMarginItem.reset( static_cast<SvxLongLRSpaceItem*>(pState->Clone()) );
227  UpdateMarginBox();
228  }
229  }
230  break;
231  case SID_ATTR_PAGE_ULSPACE:
232  {
233  if ( eState >= SfxItemState::DEFAULT &&
234  dynamic_cast< const SvxLongULSpaceItem *>( pState ) )
235  {
236  mpPageULMarginItem.reset( static_cast<SvxLongULSpaceItem*>(pState->Clone()) );
237  UpdateMarginBox();
238  }
239  }
240  break;
241  default:
242  break;
243  }
244 }
245 
246 IMPL_LINK_NOARG(PageFormatPanel, PaperFormatModifyHdl, weld::ComboBox&, void)
247 {
248  Paper ePaper = mxPaperSizeBox->get_active_id();
249  Size aSize;
250 
251  if(ePaper!=PAPER_USER)
252  aSize = SvxPaperInfo::GetPaperSize(ePaper, meUnit);
253  else
254  aSize = Size(mxPaperWidth->GetCoreValue(meUnit), mxPaperHeight->GetCoreValue(meUnit));
255 
256  if (mxPaperOrientation->get_active() == 1 || ePaper==PAPER_USER)
257  Swap(aSize);
258 
259  mpPageItem->SetLandscape(mxPaperOrientation->get_active() == 1);
260  SvxSizeItem aSizeItem(SID_ATTR_PAGE_SIZE, aSize);
261  mpBindings->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE, SfxCallMode::RECORD, { &aSizeItem, mpPageItem.get() });
262 }
263 
265 {
266  Size aSize(mxPaperWidth->GetCoreValue(meUnit), mxPaperHeight->GetCoreValue(meUnit));
267  SvxSizeItem aSizeItem(SID_ATTR_PAGE_SIZE, aSize);
268  mpBindings->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE, SfxCallMode::RECORD, { &aSizeItem });
269 }
270 
271 IMPL_LINK_NOARG(PageFormatPanel, PaperModifyMarginHdl, weld::ComboBox&, void)
272 {
273  bool bMirrored = false;
274  bool bApplyNewPageMargins = true;
275  switch (mxMarginSelectBox->get_active())
276  {
277  case 0:
278  SetNone(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
279  break;
280  case 1:
281  SetNarrow(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
282  break;
283  case 2:
284  SetModerate(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
285  break;
286  case 3:
287  SetNormal075(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
288  break;
289  case 4:
290  SetNormal100(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
291  break;
292  case 5:
293  SetNormal125(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
294  break;
295  case 6:
296  SetWide(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
297  break;
298  case 7:
299  SetMirrored(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
300  break;
301  default:
302  bApplyNewPageMargins = false;
303  break;
304  }
305 
306  if(bApplyNewPageMargins)
307  {
308  ExecuteMarginLRChange( mnPageLeftMargin, mnPageRightMargin );
309  ExecuteMarginULChange( mnPageTopMargin, mnPageBottomMargin );
310  if(bMirrored != (mpPageItem->GetPageUsage() == SvxPageUsage::Mirror))
311  {
312  mpPageItem->SetPageUsage( bMirrored ? SvxPageUsage::Mirror : SvxPageUsage::All );
313  mpBindings->GetDispatcher()->ExecuteList(SID_ATTR_PAGE,
314  SfxCallMode::RECORD, { mpPageItem.get() });
315  }
316  }
317 }
318 
320 {
321  FieldUnit eUnit = FieldUnit::NONE;
322 
323  if ( pState && eState >= SfxItemState::DEFAULT )
324  eUnit = static_cast<FieldUnit>(static_cast<const SfxUInt16Item*>(pState)->GetValue());
325  else
326  {
328  SfxObjectShell* pSh = nullptr;
329  if ( pFrame )
330  pSh = pFrame->GetObjectShell();
331  if ( pSh )
332  {
333  SfxModule* pModule = pSh->GetModule();
334  if ( pModule )
335  {
336  const SfxPoolItem* pItem = pModule->GetItem( SID_ATTR_METRIC );
337  if ( pItem )
338  eUnit = static_cast<FieldUnit>(static_cast<const SfxUInt16Item*>(pItem)->GetValue());
339  }
340  else
341  {
342  SAL_WARN("sw.ui", "GetModuleFieldUnit(): no module found");
343  }
344  }
345  }
346 
347  return eUnit;
348 }
349 
350 void PageFormatPanel::ExecuteMarginLRChange( const long nPageLeftMargin, const long nPageRightMargin )
351 {
352  mpPageLRMarginItem->SetLeft( nPageLeftMargin );
353  mpPageLRMarginItem->SetRight( nPageRightMargin );
354  mpBindings->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_LRSPACE, SfxCallMode::RECORD, { mpPageLRMarginItem.get() });
355 }
356 
357 void PageFormatPanel::ExecuteMarginULChange(const long nPageTopMargin, const long nPageBottomMargin)
358 {
359  mpPageULMarginItem->SetUpper( nPageTopMargin );
360  mpPageULMarginItem->SetLower( nPageBottomMargin );
361  mpBindings->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_ULSPACE, SfxCallMode::RECORD, { mpPageULMarginItem.get() });
362 }
363 
365 {
368  mnPageTopMargin = mpPageULMarginItem->GetUpper();
370 
371  int nCustomEntry = mxMarginSelectBox->find_text(aCustomEntry);
372 
373  bool bMirrored = (mpPageItem->GetPageUsage() == SvxPageUsage::Mirror);
375  {
376  mxMarginSelectBox->set_active(0);
377  if (nCustomEntry != -1)
378  mxMarginSelectBox->remove(nCustomEntry);
379  }
381  {
382  mxMarginSelectBox->set_active(1);
383  if (nCustomEntry != -1)
384  mxMarginSelectBox->remove(nCustomEntry);
385  }
387  {
388  mxMarginSelectBox->set_active(2);
389  if (nCustomEntry != -1)
390  mxMarginSelectBox->remove(nCustomEntry);
391  }
393  {
394  mxMarginSelectBox->set_active(3);
395  if (nCustomEntry != -1)
396  mxMarginSelectBox->remove(nCustomEntry);
397  }
399  {
400  mxMarginSelectBox->set_active(4);
401  if (nCustomEntry != -1)
402  mxMarginSelectBox->remove(nCustomEntry);
403  }
405  {
406  mxMarginSelectBox->set_active(5);
407  if (nCustomEntry != -1)
408  mxMarginSelectBox->remove(nCustomEntry);
409  }
411  {
412  mxMarginSelectBox->set_active(6);
413  if (nCustomEntry != -1)
414  mxMarginSelectBox->remove(nCustomEntry);
415  }
417  {
418  mxMarginSelectBox->set_active(7);
419  if (nCustomEntry != -1)
420  mxMarginSelectBox->remove(nCustomEntry);
421  }
422  else
423  {
424  if (nCustomEntry == -1)
425  mxMarginSelectBox->append_text(aCustomEntry);
426  mxMarginSelectBox->set_active_text(aCustomEntry);
427  }
428 }
429 
430 }
431 
432 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
::sfx2::sidebar::ControllerItem maPaperOrientationController
const SfxPoolItem * ExecuteList(sal_uInt16 nSlot, SfxCallMode nCall, std::initializer_list< SfxPoolItem const * > args, std::initializer_list< SfxPoolItem const * > internalargs=std::initializer_list< SfxPoolItem const * >())
FieldUnit
const LocaleDataWrapper & GetLocaleDataWrapper() const
std::unique_ptr< weld::ComboBox > mxMarginSelectBox
void SetNormal100(long &nPageLeftMargin, long &nPageRightMargin, long &nPageTopMargin, long &nPageBottomMargin, bool &bMirrored)
void ExecuteMarginULChange(const long nPageTopMargin, const long nPageBottomMargin)
Size & Swap(Size &rSize)
virtual void NotifyItemUpdate(const sal_uInt16 nSId, const SfxItemState eState, const SfxPoolItem *pState) override
std::string GetValue
::sfx2::sidebar::ControllerItem maSwPageLRControl
long Height() const
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const =0
static const AllSettings & GetSettings()
bool IsNarrow(const long nPageLeftMargin, const long nPageRightMargin, const long nPageTopMargin, const long nPageBottomMargin, bool bMirrored)
MapUnit GetCoreMetric() const
virtual SfxObjectShell * GetObjectShell() override
static OUString MetricToString(FieldUnit rUnit)
sal_uInt32 GetMaximumPaperWidth() const
FieldUnit GetModuleFieldUnit(const SfxItemSet &rSet)
bool IsNormal075(const long nPageLeftMargin, const long nPageRightMargin, const long nPageTopMargin, const long nPageBottomMargin, bool bMirrored)
SfxModule * GetModule() const
Paper
void SetNarrow(long &nPageLeftMargin, long &nPageRightMargin, long &nPageTopMargin, long &nPageBottomMargin, bool &bMirrored)
void ExecuteMarginLRChange(const long nPageLeftMargin, const long nPageRightMargin)
bool IsMirrored(const long nPageLeftMargin, const long nPageRightMargin, const long nPageTopMargin, const long nPageBottomMargin, bool bMirrored)
::sfx2::sidebar::ControllerItem maSwPageULControl
OUString sSuffix
static FieldUnit GetCurrentUnit(SfxItemState eState, const SfxPoolItem *pState)
void SetWide(long &nPageLeftMargin, long &nPageRightMargin, long &nPageTopMargin, long &nPageBottomMargin, bool &bMirrored)
#define SAL_N_ELEMENTS(arr)
std::unique_ptr< SvxRelativeField > mxPaperWidth
bool IsWide(const long nPageLeftMargin, const long nPageRightMargin, const long nPageTopMargin, const long nPageBottomMargin, bool bMirrored)
const SfxPoolItem * GetItem(sal_uInt16 nSlotId) const
int i
virtual void dispose()
std::unique_ptr< SvxRelativeField > mxPaperHeight
std::unique_ptr< SvxPageItem > mpPageItem
std::unique_ptr< SvxLongLRSpaceItem > mpPageLRMarginItem
static Size GetPaperSize(Paper ePaper, MapUnit eUnit=MapUnit::MapTwip)
OUString SwResId(const char *pId)
Definition: swmodule.cxx:165
IMPL_LINK_NOARG(PageColumnControl, MoreButtonClickHdl_Impl, weld::Button &, void)
::sfx2::sidebar::ControllerItem maMetricController
void SetNone(long &nPageLeftMargin, long &nPageRightMargin, long &nPageTopMargin, long &nPageBottomMargin, bool &bMirrored)
void Update(sal_uInt16 nId)
virtual void dispose() override
sal_uInt32 GetMaximumPaperHeight() const
void SetModerate(long &nPageLeftMargin, long &nPageRightMargin, long &nPageTopMargin, long &nPageBottomMargin, bool &bMirrored)
PageFormatPanel(vcl::Window *pParent, const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > &rxFrame, SfxBindings *pBindings)
static SfxViewFrame * Current()
SfxItemState
std::unique_ptr< SvxLongULSpaceItem > mpPageULMarginItem
std::unique_ptr< weld::ComboBox > mxPaperOrientation
virtual ~PageFormatPanel() override
static VclPtr< reference_type > Create(Arg &&...arg)
std::unique_ptr< SvxPaperSizeListBox > mxPaperSizeBox
void SetNormal125(long &nPageLeftMargin, long &nPageRightMargin, long &nPageTopMargin, long &nPageBottomMargin, bool &bMirrored)
bool IsInch(MapUnit eU)
SfxDispatcher * GetDispatcher() const
#define SAL_WARN(area, stream)
void SetMirrored(long &nPageLeftMargin, long &nPageRightMargin, long &nPageTopMargin, long &nPageBottomMargin, bool &bMirrored)
OUString getNum(sal_Int64 nNumber, sal_uInt16 nDecimals, bool bUseThousandSep=true, bool bTrailingZeros=true) const
bool IsNormal100(const long nPageLeftMargin, const long nPageRightMargin, const long nPageTopMargin, const long nPageBottomMargin, bool bMirrored)
::sfx2::sidebar::ControllerItem maPaperSizeController
bool IsNormal125(const long nPageLeftMargin, const long nPageRightMargin, const long nPageTopMargin, const long nPageBottomMargin, bool bMirrored)
std::unique_ptr< weld::Label > mxCustomEntry
virtual void dispose() override
PAPER_USER
bool IsNone(const long nPageLeftMargin, const long nPageRightMargin, const long nPageTopMargin, const long nPageBottomMargin, bool bMirrored)
void SetNormal075(long &nPageLeftMargin, long &nPageRightMargin, long &nPageTopMargin, long &nPageBottomMargin, bool &bMirrored)
bool IsModerate(const long nPageLeftMargin, const long nPageRightMargin, const long nPageTopMargin, const long nPageBottomMargin, bool bMirrored)
const Size & GetSize() const
const char first[]
static Paper GetSvxPaper(const Size &rSize, MapUnit eUnit)
static VclPtr< vcl::Window > Create(vcl::Window *pParent, const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > &rxFrame, SfxBindings *pBindings)