Add Marathi autocorrect
[LibreOffice.git] / sw / source / uibase / sidebar / PageFormatPanel.cxx
blobaecefa8030809a46718384d0d88207b48c8590d5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
36 #include <cmdid.h>
37 #include <unotools/localedatawrapper.hxx>
38 #include <vcl/settings.hxx>
39 #include <vcl/svapp.hxx>
40 #include <officecfg/Office/Common.hxx>
42 #include <com/sun/star/lang/IllegalArgumentException.hpp>
44 namespace sw::sidebar{
46 std::unique_ptr<PanelLayout> PageFormatPanel::Create(
47 weld::Widget* pParent,
48 SfxBindings* pBindings)
50 if( pParent == nullptr )
51 throw css::lang::IllegalArgumentException(u"no parent window given to PageFormatPanel::Create"_ustr, nullptr, 0);
53 return std::make_unique<PageFormatPanel>(pParent, pBindings);
56 void PageFormatPanel::SetMarginFieldUnit()
58 auto nSelected = mxMarginSelectBox->get_active();
59 mxMarginSelectBox->clear();
61 const LocaleDataWrapper& rLocaleData = Application::GetSettings().GetLocaleDataWrapper();
62 if (IsInch(meFUnit))
64 OUString sSuffix = weld::MetricSpinButton::MetricToString(FieldUnit::INCH);
65 for (auto const& [aName, nSize] : RID_PAGEFORMATPANEL_MARGINS_INCH)
67 OUString sStr = rLocaleData.getNum(nSize, 2, true, false) + sSuffix;
68 mxMarginSelectBox->append_text(SwResId(aName).replaceFirst("%1", sStr));
71 else
73 OUString sSuffix = weld::MetricSpinButton::MetricToString(FieldUnit::CM);
74 for (auto const& [aName, nSize] : RID_PAGEFORMATPANEL_MARGINS_CM)
76 OUString sStr = rLocaleData.getNum(nSize, 2, true, false) + " " + sSuffix;
77 mxMarginSelectBox->append_text(SwResId(aName).replaceFirst("%1", sStr));
80 mxMarginSelectBox->set_active(nSelected);
83 PageFormatPanel::PageFormatPanel(weld::Widget* pParent, SfxBindings* pBindings) :
84 PanelLayout(pParent, u"PageFormatPanel"_ustr, u"modules/swriter/ui/pageformatpanel.ui"_ustr),
85 mpBindings( pBindings ),
86 mxPaperSizeBox(new SvxPaperSizeListBox(m_xBuilder->weld_combo_box(u"papersize"_ustr))),
87 mxPaperWidth(new SvxRelativeField(m_xBuilder->weld_metric_spin_button(u"paperwidth"_ustr, FieldUnit::CM))),
88 mxPaperHeight(new SvxRelativeField(m_xBuilder->weld_metric_spin_button(u"paperheight"_ustr, FieldUnit::CM))),
89 mxPaperOrientation(m_xBuilder->weld_combo_box(u"paperorientation"_ustr)),
90 mxMarginSelectBox(m_xBuilder->weld_combo_box(u"marginLB"_ustr)),
91 mxCustomEntry(m_xBuilder->weld_label(u"customlabel"_ustr)),
92 maPaperSizeController(SID_ATTR_PAGE_SIZE, *pBindings, *this),
93 maPaperOrientationController(SID_ATTR_PAGE, *pBindings, *this),
94 maMetricController(SID_ATTR_METRIC, *pBindings,*this),
95 maSwPageLRControl(SID_ATTR_PAGE_LRSPACE, *pBindings, *this),
96 maSwPageULControl(SID_ATTR_PAGE_ULSPACE, *pBindings, *this),
97 mpPageItem( new SvxPageItem(SID_ATTR_PAGE) ),
98 mpPageLRMarginItem( new SvxLongLRSpaceItem( 0, 0, SID_ATTR_PAGE_LRSPACE ) ),
99 mpPageULMarginItem( new SvxLongULSpaceItem( 0, 0, SID_ATTR_PAGE_ULSPACE ) ),
100 meFUnit(GetModuleFieldUnit()),
101 meUnit()
103 Initialize();
106 PageFormatPanel::~PageFormatPanel()
108 mxPaperSizeBox.reset();
109 mxPaperWidth.reset();
110 mxPaperHeight.reset();
111 mxPaperOrientation.reset();
112 mxMarginSelectBox.reset();
113 mxCustomEntry.reset();
115 maMetricController.dispose();
116 maPaperOrientationController.dispose();
117 maPaperSizeController.dispose();
118 maSwPageLRControl.dispose();
119 maSwPageULControl.dispose();
120 mpPageULMarginItem.reset();
121 mpPageLRMarginItem.reset();
122 mpPageItem.reset();
125 void PageFormatPanel::Initialize()
127 mxPaperSizeBox->FillPaperSizeEntries( PaperSizeApp::Std );
128 maPaperSizeController.SetFallbackCoreMetric(MapUnit::MapTwip);
129 meUnit = maPaperSizeController.GetCoreMetric();
130 mxPaperWidth->SetFieldUnit(meFUnit);
131 mxPaperHeight->SetFieldUnit(meFUnit);
132 SetMarginFieldUnit();
133 m_aCustomEntry = mxCustomEntry->get_label();
135 mxPaperWidth->set_max(mxPaperWidth->normalize(officecfg::Office::Common::Drawinglayer::MaximumPaperWidth::get()), FieldUnit::CM);
136 mxPaperHeight->set_max(mxPaperHeight->normalize(officecfg::Office::Common::Drawinglayer::MaximumPaperHeight::get()), FieldUnit::CM);
138 mxPaperSizeBox->connect_changed( LINK(this, PageFormatPanel, PaperFormatModifyHdl ));
139 mxPaperOrientation->connect_changed( LINK(this, PageFormatPanel, PaperFormatModifyHdl ));
140 mxPaperHeight->connect_value_changed( LINK(this, PageFormatPanel, PaperSizeModifyHdl ));
141 mxPaperWidth->connect_value_changed( LINK(this, PageFormatPanel, PaperSizeModifyHdl ));
142 mxMarginSelectBox->connect_changed( LINK(this, PageFormatPanel, PaperModifyMarginHdl));
144 mpBindings->Update(SID_ATTR_METRIC);
145 mpBindings->Update(SID_ATTR_PAGE);
146 mpBindings->Update(SID_ATTR_PAGE_SIZE);
147 mpBindings->Update(SID_ATTR_PAGE_LRSPACE);
148 mpBindings->Update(SID_ATTR_PAGE_ULSPACE);
150 UpdateMarginBox();
153 void PageFormatPanel::NotifyItemUpdate(
154 const sal_uInt16 nSId,
155 const SfxItemState eState,
156 const SfxPoolItem* pState)
158 switch(nSId)
160 case SID_ATTR_PAGE_SIZE:
162 const SvxSizeItem* pSizeItem = nullptr;
163 if (eState >= SfxItemState::DEFAULT)
164 pSizeItem = dynamic_cast< const SvxSizeItem* >(pState);
165 if (pSizeItem)
167 Size aPaperSize = pSizeItem->GetSize();
169 mxPaperWidth->set_value(mxPaperWidth->normalize(aPaperSize.Width()), FieldUnit::TWIP);
170 mxPaperHeight->set_value(mxPaperHeight->normalize(aPaperSize.Height()), FieldUnit::TWIP);
172 if(mxPaperOrientation->get_active() == 1)
173 Swap(aPaperSize);
175 Paper ePaper = SvxPaperInfo::GetSvxPaper(aPaperSize, meUnit);
176 mxPaperSizeBox->set_active_id( ePaper );
179 break;
180 case SID_ATTR_METRIC:
182 meUnit = maPaperSizeController.GetCoreMetric();
183 FieldUnit eFUnit = GetCurrentUnit(eState, pState);
184 if (eFUnit != meFUnit)
186 meFUnit = eFUnit;
187 mxPaperHeight->SetFieldUnit(meFUnit);
188 mxPaperWidth->SetFieldUnit(meFUnit);
189 SetMarginFieldUnit();
190 UpdateMarginBox();
193 break;
194 case SID_ATTR_PAGE:
196 if ( eState >= SfxItemState::DEFAULT &&
197 dynamic_cast< const SvxPageItem *>( pState ) )
199 mpPageItem.reset( static_cast<SvxPageItem*>(pState->Clone()) );
200 if ( mpPageItem->IsLandscape() )
201 mxPaperOrientation->set_active(1);
202 else
203 mxPaperOrientation->set_active(0);
206 break;
207 case SID_ATTR_PAGE_LRSPACE:
209 if ( eState >= SfxItemState::DEFAULT &&
210 dynamic_cast< const SvxLongLRSpaceItem *>( pState ) )
212 mpPageLRMarginItem.reset( static_cast<SvxLongLRSpaceItem*>(pState->Clone()) );
213 UpdateMarginBox();
216 break;
217 case SID_ATTR_PAGE_ULSPACE:
219 if ( eState >= SfxItemState::DEFAULT &&
220 dynamic_cast< const SvxLongULSpaceItem *>( pState ) )
222 mpPageULMarginItem.reset( static_cast<SvxLongULSpaceItem*>(pState->Clone()) );
223 UpdateMarginBox();
226 break;
227 default:
228 break;
232 IMPL_LINK_NOARG(PageFormatPanel, PaperFormatModifyHdl, weld::ComboBox&, void)
234 Paper ePaper = mxPaperSizeBox->get_active_id();
235 Size aSize;
237 if(ePaper!=PAPER_USER)
238 aSize = SvxPaperInfo::GetPaperSize(ePaper, meUnit);
239 else
240 aSize = Size(mxPaperWidth->GetCoreValue(meUnit), mxPaperHeight->GetCoreValue(meUnit));
242 if (mxPaperOrientation->get_active() == 1 || ePaper==PAPER_USER)
243 Swap(aSize);
245 mpPageItem->SetLandscape(mxPaperOrientation->get_active() == 1);
246 SvxSizeItem aSizeItem(SID_ATTR_PAGE_SIZE, aSize);
247 mpBindings->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE, SfxCallMode::RECORD, { &aSizeItem, mpPageItem.get() });
250 IMPL_LINK_NOARG(PageFormatPanel, PaperSizeModifyHdl, weld::MetricSpinButton&, void)
252 Size aSize(mxPaperWidth->GetCoreValue(meUnit), mxPaperHeight->GetCoreValue(meUnit));
253 SvxSizeItem aSizeItem(SID_ATTR_PAGE_SIZE, aSize);
254 mpBindings->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE, SfxCallMode::RECORD, { &aSizeItem });
257 IMPL_LINK_NOARG(PageFormatPanel, PaperModifyMarginHdl, weld::ComboBox&, void)
259 bool bMirrored = false;
260 bool bApplyNewPageMargins = true;
261 switch (mxMarginSelectBox->get_active())
263 case 0:
264 SetNone(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
265 break;
266 case 1:
267 SetNarrow(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
268 break;
269 case 2:
270 SetModerate(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
271 break;
272 case 3:
273 SetNormal075(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
274 break;
275 case 4:
276 SetNormal100(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
277 break;
278 case 5:
279 SetNormal125(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
280 break;
281 case 6:
282 SetWide(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
283 break;
284 case 7:
285 SetMirrored(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
286 break;
287 default:
288 bApplyNewPageMargins = false;
289 break;
292 if(bApplyNewPageMargins)
294 ExecuteMarginLRChange( mnPageLeftMargin, mnPageRightMargin );
295 ExecuteMarginULChange( mnPageTopMargin, mnPageBottomMargin );
296 if(bMirrored != (mpPageItem->GetPageUsage() == SvxPageUsage::Mirror))
298 mpPageItem->SetPageUsage( bMirrored ? SvxPageUsage::Mirror : SvxPageUsage::All );
299 mpBindings->GetDispatcher()->ExecuteList(SID_ATTR_PAGE,
300 SfxCallMode::RECORD, { mpPageItem.get() });
305 FieldUnit PageFormatPanel::GetCurrentUnit( SfxItemState eState, const SfxPoolItem* pState )
307 FieldUnit eUnit = FieldUnit::NONE;
309 if ( pState && eState >= SfxItemState::DEFAULT )
310 eUnit = static_cast<FieldUnit>(static_cast<const SfxUInt16Item*>(pState)->GetValue());
311 else
313 SfxViewFrame* pFrame = SfxViewFrame::Current();
314 SfxObjectShell* pSh = nullptr;
315 if ( pFrame )
316 pSh = pFrame->GetObjectShell();
317 if ( pSh )
319 SfxModule* pModule = pSh->GetModule();
320 if ( pModule )
322 eUnit = pModule->GetFieldUnit();
324 else
326 SAL_WARN("sw.ui", "GetModuleFieldUnit(): no module found");
331 return eUnit;
334 void PageFormatPanel::ExecuteMarginLRChange( const tools::Long nPageLeftMargin, const tools::Long nPageRightMargin )
336 mpPageLRMarginItem->SetLeft( nPageLeftMargin );
337 mpPageLRMarginItem->SetRight( nPageRightMargin );
338 mpBindings->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_LRSPACE, SfxCallMode::RECORD, { mpPageLRMarginItem.get() });
341 void PageFormatPanel::ExecuteMarginULChange(const tools::Long nPageTopMargin, const tools::Long nPageBottomMargin)
343 mpPageULMarginItem->SetUpper( nPageTopMargin );
344 mpPageULMarginItem->SetLower( nPageBottomMargin );
345 mpBindings->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_ULSPACE, SfxCallMode::RECORD, { mpPageULMarginItem.get() });
348 void PageFormatPanel::UpdateMarginBox()
350 mnPageLeftMargin = mpPageLRMarginItem->GetLeft();
351 mnPageRightMargin = mpPageLRMarginItem->GetRight();
352 mnPageTopMargin = mpPageULMarginItem->GetUpper();
353 mnPageBottomMargin = mpPageULMarginItem->GetLower();
355 int nCustomEntry = mxMarginSelectBox->find_text(m_aCustomEntry);
357 bool bMirrored = (mpPageItem->GetPageUsage() == SvxPageUsage::Mirror);
358 if( IsNone(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored) )
360 mxMarginSelectBox->set_active(0);
361 if (nCustomEntry != -1)
362 mxMarginSelectBox->remove(nCustomEntry);
364 else if( IsNarrow(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored) )
366 mxMarginSelectBox->set_active(1);
367 if (nCustomEntry != -1)
368 mxMarginSelectBox->remove(nCustomEntry);
370 else if( IsModerate(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored) )
372 mxMarginSelectBox->set_active(2);
373 if (nCustomEntry != -1)
374 mxMarginSelectBox->remove(nCustomEntry);
376 else if( IsNormal075(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored) )
378 mxMarginSelectBox->set_active(3);
379 if (nCustomEntry != -1)
380 mxMarginSelectBox->remove(nCustomEntry);
382 else if( IsNormal100(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored) )
384 mxMarginSelectBox->set_active(4);
385 if (nCustomEntry != -1)
386 mxMarginSelectBox->remove(nCustomEntry);
388 else if( IsNormal125(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored) )
390 mxMarginSelectBox->set_active(5);
391 if (nCustomEntry != -1)
392 mxMarginSelectBox->remove(nCustomEntry);
394 else if( IsWide(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored) )
396 mxMarginSelectBox->set_active(6);
397 if (nCustomEntry != -1)
398 mxMarginSelectBox->remove(nCustomEntry);
400 else if( IsMirrored(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored) )
402 mxMarginSelectBox->set_active(7);
403 if (nCustomEntry != -1)
404 mxMarginSelectBox->remove(nCustomEntry);
406 else
408 if (nCustomEntry == -1)
409 mxMarginSelectBox->append_text(m_aCustomEntry);
410 mxMarginSelectBox->set_active_text(m_aCustomEntry);
416 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */