1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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>
37 #include <svtools/optionsdrawinglayer.hxx>
38 #include <unotools/localedatawrapper.hxx>
39 #include <vcl/settings.hxx>
40 #include <vcl/svapp.hxx>
42 #include <com/sun/star/lang/IllegalArgumentException.hpp>
44 namespace sw::sidebar
{
46 VclPtr
<vcl::Window
> PageFormatPanel::Create(
48 const ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XFrame
>& rxFrame
,
49 SfxBindings
* pBindings
)
51 if( pParent
== nullptr )
52 throw ::com::sun::star::lang::IllegalArgumentException("no parent window given to PageFormatPanel::Create", nullptr, 0);
54 throw ::com::sun::star::lang::IllegalArgumentException("no XFrame given to PageFormatPanel::Create", nullptr, 0);
56 return VclPtr
<PageFormatPanel
>::Create(pParent
, rxFrame
, pBindings
);
59 void PageFormatPanel::SetMarginFieldUnit()
61 auto nSelected
= mxMarginSelectBox
->get_active();
62 mxMarginSelectBox
->clear();
64 const LocaleDataWrapper
& rLocaleData
= Application::GetSettings().GetLocaleDataWrapper();
67 OUString sSuffix
= weld::MetricSpinButton::MetricToString(FieldUnit::INCH
);
68 for (size_t i
= 0; i
< SAL_N_ELEMENTS(RID_PAGEFORMATPANEL_MARGINS_INCH
); ++i
)
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
));
76 OUString sSuffix
= weld::MetricSpinButton::MetricToString(FieldUnit::CM
);
77 for (size_t i
= 0; i
< SAL_N_ELEMENTS(RID_PAGEFORMATPANEL_MARGINS_CM
); ++i
)
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
));
83 mxMarginSelectBox
->set_active(nSelected
);
86 PageFormatPanel::PageFormatPanel(
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()),
113 PageFormatPanel::~PageFormatPanel()
118 void PageFormatPanel::dispose()
120 mxPaperSizeBox
.reset();
121 mxPaperWidth
.reset();
122 mxPaperHeight
.reset();
123 mxPaperOrientation
.reset();
124 mxMarginSelectBox
.reset();
125 mxCustomEntry
.reset();
127 maMetricController
.dispose();
128 maPaperOrientationController
.dispose();
129 maPaperSizeController
.dispose();
130 maSwPageLRControl
.dispose();
131 maSwPageULControl
.dispose();
132 mpPageULMarginItem
.reset();
133 mpPageLRMarginItem
.reset();
136 PanelLayout::dispose();
139 void PageFormatPanel::Initialize()
141 mxPaperSizeBox
->FillPaperSizeEntries( PaperSizeApp::Std
);
142 maPaperSizeController
.SetFallbackCoreMetric(MapUnit::MapTwip
);
143 meUnit
= maPaperSizeController
.GetCoreMetric();
144 mxPaperWidth
->SetFieldUnit(meFUnit
);
145 mxPaperHeight
->SetFieldUnit(meFUnit
);
146 SetMarginFieldUnit();
147 aCustomEntry
= mxCustomEntry
->get_label();
149 const SvtOptionsDrawinglayer aDrawinglayerOpt
;
150 mxPaperWidth
->set_max(mxPaperWidth
->normalize(aDrawinglayerOpt
.GetMaximumPaperWidth()), FieldUnit::CM
);
151 mxPaperHeight
->set_max(mxPaperHeight
->normalize(aDrawinglayerOpt
.GetMaximumPaperHeight()), FieldUnit::CM
);
153 mxPaperSizeBox
->connect_changed( LINK(this, PageFormatPanel
, PaperFormatModifyHdl
));
154 mxPaperOrientation
->connect_changed( LINK(this, PageFormatPanel
, PaperFormatModifyHdl
));
155 mxPaperHeight
->connect_value_changed( LINK(this, PageFormatPanel
, PaperSizeModifyHdl
));
156 mxPaperWidth
->connect_value_changed( LINK(this, PageFormatPanel
, PaperSizeModifyHdl
));
157 mxMarginSelectBox
->connect_changed( LINK(this, PageFormatPanel
, PaperModifyMarginHdl
));
159 mpBindings
->Update(SID_ATTR_METRIC
);
160 mpBindings
->Update(SID_ATTR_PAGE
);
161 mpBindings
->Update(SID_ATTR_PAGE_SIZE
);
162 mpBindings
->Update(SID_ATTR_PAGE_LRSPACE
);
163 mpBindings
->Update(SID_ATTR_PAGE_ULSPACE
);
168 void PageFormatPanel::NotifyItemUpdate(
169 const sal_uInt16 nSId
,
170 const SfxItemState eState
,
171 const SfxPoolItem
* pState
)
175 case SID_ATTR_PAGE_SIZE
:
177 const SvxSizeItem
* pSizeItem
= nullptr;
178 if (eState
>= SfxItemState::DEFAULT
)
179 pSizeItem
= dynamic_cast< const SvxSizeItem
* >(pState
);
182 Size aPaperSize
= pSizeItem
->GetSize();
184 mxPaperWidth
->set_value(mxPaperWidth
->normalize(aPaperSize
.Width()), FieldUnit::TWIP
);
185 mxPaperHeight
->set_value(mxPaperHeight
->normalize(aPaperSize
.Height()), FieldUnit::TWIP
);
187 if(mxPaperOrientation
->get_active() == 1)
190 Paper ePaper
= SvxPaperInfo::GetSvxPaper(aPaperSize
, meUnit
);
191 mxPaperSizeBox
->set_active_id( ePaper
);
195 case SID_ATTR_METRIC
:
197 meUnit
= maPaperSizeController
.GetCoreMetric();
198 FieldUnit eFUnit
= GetCurrentUnit(eState
, pState
);
199 if (eFUnit
!= meFUnit
)
202 mxPaperHeight
->SetFieldUnit(meFUnit
);
203 mxPaperWidth
->SetFieldUnit(meFUnit
);
204 SetMarginFieldUnit();
211 if ( eState
>= SfxItemState::DEFAULT
&&
212 dynamic_cast< const SvxPageItem
*>( pState
) )
214 mpPageItem
.reset( static_cast<SvxPageItem
*>(pState
->Clone()) );
215 if ( mpPageItem
->IsLandscape() )
216 mxPaperOrientation
->set_active(1);
218 mxPaperOrientation
->set_active(0);
222 case SID_ATTR_PAGE_LRSPACE
:
224 if ( eState
>= SfxItemState::DEFAULT
&&
225 dynamic_cast< const SvxLongLRSpaceItem
*>( pState
) )
227 mpPageLRMarginItem
.reset( static_cast<SvxLongLRSpaceItem
*>(pState
->Clone()) );
232 case SID_ATTR_PAGE_ULSPACE
:
234 if ( eState
>= SfxItemState::DEFAULT
&&
235 dynamic_cast< const SvxLongULSpaceItem
*>( pState
) )
237 mpPageULMarginItem
.reset( static_cast<SvxLongULSpaceItem
*>(pState
->Clone()) );
247 IMPL_LINK_NOARG(PageFormatPanel
, PaperFormatModifyHdl
, weld::ComboBox
&, void)
249 Paper ePaper
= mxPaperSizeBox
->get_active_id();
252 if(ePaper
!=PAPER_USER
)
253 aSize
= SvxPaperInfo::GetPaperSize(ePaper
, meUnit
);
255 aSize
= Size(mxPaperWidth
->GetCoreValue(meUnit
), mxPaperHeight
->GetCoreValue(meUnit
));
257 if (mxPaperOrientation
->get_active() == 1 || ePaper
==PAPER_USER
)
260 mpPageItem
->SetLandscape(mxPaperOrientation
->get_active() == 1);
261 SvxSizeItem
aSizeItem(SID_ATTR_PAGE_SIZE
, aSize
);
262 mpBindings
->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE
, SfxCallMode::RECORD
, { &aSizeItem
, mpPageItem
.get() });
265 IMPL_LINK_NOARG(PageFormatPanel
, PaperSizeModifyHdl
, weld::MetricSpinButton
&, void)
267 Size
aSize(mxPaperWidth
->GetCoreValue(meUnit
), mxPaperHeight
->GetCoreValue(meUnit
));
268 SvxSizeItem
aSizeItem(SID_ATTR_PAGE_SIZE
, aSize
);
269 mpBindings
->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE
, SfxCallMode::RECORD
, { &aSizeItem
});
272 IMPL_LINK_NOARG(PageFormatPanel
, PaperModifyMarginHdl
, weld::ComboBox
&, void)
274 bool bMirrored
= false;
275 bool bApplyNewPageMargins
= true;
276 switch (mxMarginSelectBox
->get_active())
279 SetNone(mnPageLeftMargin
, mnPageRightMargin
, mnPageTopMargin
, mnPageBottomMargin
, bMirrored
);
282 SetNarrow(mnPageLeftMargin
, mnPageRightMargin
, mnPageTopMargin
, mnPageBottomMargin
, bMirrored
);
285 SetModerate(mnPageLeftMargin
, mnPageRightMargin
, mnPageTopMargin
, mnPageBottomMargin
, bMirrored
);
288 SetNormal075(mnPageLeftMargin
, mnPageRightMargin
, mnPageTopMargin
, mnPageBottomMargin
, bMirrored
);
291 SetNormal100(mnPageLeftMargin
, mnPageRightMargin
, mnPageTopMargin
, mnPageBottomMargin
, bMirrored
);
294 SetNormal125(mnPageLeftMargin
, mnPageRightMargin
, mnPageTopMargin
, mnPageBottomMargin
, bMirrored
);
297 SetWide(mnPageLeftMargin
, mnPageRightMargin
, mnPageTopMargin
, mnPageBottomMargin
, bMirrored
);
300 SetMirrored(mnPageLeftMargin
, mnPageRightMargin
, mnPageTopMargin
, mnPageBottomMargin
, bMirrored
);
303 bApplyNewPageMargins
= false;
307 if(bApplyNewPageMargins
)
309 ExecuteMarginLRChange( mnPageLeftMargin
, mnPageRightMargin
);
310 ExecuteMarginULChange( mnPageTopMargin
, mnPageBottomMargin
);
311 if(bMirrored
!= (mpPageItem
->GetPageUsage() == SvxPageUsage::Mirror
))
313 mpPageItem
->SetPageUsage( bMirrored
? SvxPageUsage::Mirror
: SvxPageUsage::All
);
314 mpBindings
->GetDispatcher()->ExecuteList(SID_ATTR_PAGE
,
315 SfxCallMode::RECORD
, { mpPageItem
.get() });
320 FieldUnit
PageFormatPanel::GetCurrentUnit( SfxItemState eState
, const SfxPoolItem
* pState
)
322 FieldUnit eUnit
= FieldUnit::NONE
;
324 if ( pState
&& eState
>= SfxItemState::DEFAULT
)
325 eUnit
= static_cast<FieldUnit
>(static_cast<const SfxUInt16Item
*>(pState
)->GetValue());
328 SfxViewFrame
* pFrame
= SfxViewFrame::Current();
329 SfxObjectShell
* pSh
= nullptr;
331 pSh
= pFrame
->GetObjectShell();
334 SfxModule
* pModule
= pSh
->GetModule();
337 const SfxPoolItem
* pItem
= pModule
->GetItem( SID_ATTR_METRIC
);
339 eUnit
= static_cast<FieldUnit
>(static_cast<const SfxUInt16Item
*>(pItem
)->GetValue());
343 SAL_WARN("sw.ui", "GetModuleFieldUnit(): no module found");
351 void PageFormatPanel::ExecuteMarginLRChange( const tools::Long nPageLeftMargin
, const tools::Long nPageRightMargin
)
353 mpPageLRMarginItem
->SetLeft( nPageLeftMargin
);
354 mpPageLRMarginItem
->SetRight( nPageRightMargin
);
355 mpBindings
->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_LRSPACE
, SfxCallMode::RECORD
, { mpPageLRMarginItem
.get() });
358 void PageFormatPanel::ExecuteMarginULChange(const tools::Long nPageTopMargin
, const tools::Long nPageBottomMargin
)
360 mpPageULMarginItem
->SetUpper( nPageTopMargin
);
361 mpPageULMarginItem
->SetLower( nPageBottomMargin
);
362 mpBindings
->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_ULSPACE
, SfxCallMode::RECORD
, { mpPageULMarginItem
.get() });
365 void PageFormatPanel::UpdateMarginBox()
367 mnPageLeftMargin
= mpPageLRMarginItem
->GetLeft();
368 mnPageRightMargin
= mpPageLRMarginItem
->GetRight();
369 mnPageTopMargin
= mpPageULMarginItem
->GetUpper();
370 mnPageBottomMargin
= mpPageULMarginItem
->GetLower();
372 int nCustomEntry
= mxMarginSelectBox
->find_text(aCustomEntry
);
374 bool bMirrored
= (mpPageItem
->GetPageUsage() == SvxPageUsage::Mirror
);
375 if( IsNone(mnPageLeftMargin
, mnPageRightMargin
, mnPageTopMargin
, mnPageBottomMargin
, bMirrored
) )
377 mxMarginSelectBox
->set_active(0);
378 if (nCustomEntry
!= -1)
379 mxMarginSelectBox
->remove(nCustomEntry
);
381 else if( IsNarrow(mnPageLeftMargin
, mnPageRightMargin
, mnPageTopMargin
, mnPageBottomMargin
, bMirrored
) )
383 mxMarginSelectBox
->set_active(1);
384 if (nCustomEntry
!= -1)
385 mxMarginSelectBox
->remove(nCustomEntry
);
387 else if( IsModerate(mnPageLeftMargin
, mnPageRightMargin
, mnPageTopMargin
, mnPageBottomMargin
, bMirrored
) )
389 mxMarginSelectBox
->set_active(2);
390 if (nCustomEntry
!= -1)
391 mxMarginSelectBox
->remove(nCustomEntry
);
393 else if( IsNormal075(mnPageLeftMargin
, mnPageRightMargin
, mnPageTopMargin
, mnPageBottomMargin
, bMirrored
) )
395 mxMarginSelectBox
->set_active(3);
396 if (nCustomEntry
!= -1)
397 mxMarginSelectBox
->remove(nCustomEntry
);
399 else if( IsNormal100(mnPageLeftMargin
, mnPageRightMargin
, mnPageTopMargin
, mnPageBottomMargin
, bMirrored
) )
401 mxMarginSelectBox
->set_active(4);
402 if (nCustomEntry
!= -1)
403 mxMarginSelectBox
->remove(nCustomEntry
);
405 else if( IsNormal125(mnPageLeftMargin
, mnPageRightMargin
, mnPageTopMargin
, mnPageBottomMargin
, bMirrored
) )
407 mxMarginSelectBox
->set_active(5);
408 if (nCustomEntry
!= -1)
409 mxMarginSelectBox
->remove(nCustomEntry
);
411 else if( IsWide(mnPageLeftMargin
, mnPageRightMargin
, mnPageTopMargin
, mnPageBottomMargin
, bMirrored
) )
413 mxMarginSelectBox
->set_active(6);
414 if (nCustomEntry
!= -1)
415 mxMarginSelectBox
->remove(nCustomEntry
);
417 else if( IsMirrored(mnPageLeftMargin
, mnPageRightMargin
, mnPageTopMargin
, mnPageBottomMargin
, bMirrored
) )
419 mxMarginSelectBox
->set_active(7);
420 if (nCustomEntry
!= -1)
421 mxMarginSelectBox
->remove(nCustomEntry
);
425 if (nCustomEntry
== -1)
426 mxMarginSelectBox
->append_text(aCustomEntry
);
427 mxMarginSelectBox
->set_active_text(aCustomEntry
);
433 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */