Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / sw / source / uibase / sidebar / PageFormatPanel.cxx
blobad245f714c8706410af492605c0b912b48f2cfb2
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 <swtypes.hxx>
21 #include <svl/intitem.hxx>
22 #include <editeng/sizeitem.hxx>
23 #include <editeng/paperinf.hxx>
24 #include <svx/svxids.hrc>
25 #include <svx/dlgutil.hxx>
26 #include <svx/rulritem.hxx>
27 #include "PageFormatPanel.hxx"
28 #include "PageMarginUtils.hxx"
29 #include <sfx2/sidebar/ControlFactory.hxx>
30 #include <sfx2/dispatch.hxx>
31 #include <sfx2/bindings.hxx>
32 #include <sfx2/viewsh.hxx>
33 #include <sfx2/objsh.hxx>
34 #include "cmdid.h"
35 #include <svtools/unitconv.hxx>
36 #include <svtools/optionsdrawinglayer.hxx>
38 namespace sw { namespace sidebar{
40 VclPtr<vcl::Window> PageFormatPanel::Create(
41 vcl::Window* pParent,
42 const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rxFrame,
43 SfxBindings* pBindings)
45 if( pParent == nullptr )
46 throw ::com::sun::star::lang::IllegalArgumentException("no parent window given to PageFormatPanel::Create", nullptr, 0);
47 if( !rxFrame.is() )
48 throw ::com::sun::star::lang::IllegalArgumentException("no XFrame given to PageFormatPanel::Create", nullptr, 0);
50 return VclPtr<PageFormatPanel>::Create(pParent, rxFrame, pBindings);
53 PageFormatPanel::PageFormatPanel(
54 vcl::Window* pParent,
55 const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rxFrame,
56 SfxBindings* pBindings) :
57 PanelLayout(pParent, "PageFormatPanel", "modules/swriter/ui/pageformatpanel.ui", rxFrame),
58 mpBindings( pBindings ),
59 maPaperSizeController(SID_ATTR_PAGE_SIZE, *pBindings, *this),
60 maPaperOrientationController(SID_ATTR_PAGE, *pBindings, *this),
61 maMetricController(SID_ATTR_METRIC, *pBindings,*this),
62 maSwPageLRControl(SID_ATTR_PAGE_LRSPACE, *pBindings, *this),
63 maSwPageULControl(SID_ATTR_PAGE_ULSPACE, *pBindings, *this),
64 mpPageItem( new SvxPageItem(SID_ATTR_PAGE) ),
65 mpPageLRMarginItem( new SvxLongLRSpaceItem( 0, 0, SID_ATTR_PAGE_LRSPACE ) ),
66 mpPageULMarginItem( new SvxLongULSpaceItem( 0, 0, SID_ATTR_PAGE_ULSPACE ) ),
67 meFUnit(GetModuleFieldUnit()),
68 meLastFUnit(GetModuleFieldUnit()),
69 meUnit(),
70 aCustomEntry()
72 get(mpPaperSizeBox, "papersize");
73 get(mpPaperWidth, "paperwidth");
74 get(mpPaperHeight, "paperheight");
75 get(mpPaperOrientation, "paperorientation");
76 get(mpMarginSelectBox, "marginLB");
77 get(mpCustomEntry, "customlabel");
78 Initialize();
81 PageFormatPanel::~PageFormatPanel()
83 disposeOnce();
86 void PageFormatPanel::dispose()
88 mpPaperSizeBox.disposeAndClear();
89 mpPaperWidth.disposeAndClear();
90 mpPaperHeight.disposeAndClear();
91 mpPaperOrientation.disposeAndClear();
92 mpMarginSelectBox.disposeAndClear();
93 mpCustomEntry.clear();
95 maMetricController.dispose();
96 maPaperOrientationController.dispose();
97 maPaperSizeController.dispose();
98 maSwPageLRControl.dispose();
99 maSwPageULControl.dispose();
100 mpPageULMarginItem.reset();
101 mpPageLRMarginItem.reset();
102 mpPageItem.reset();
104 PanelLayout::dispose();
107 void PageFormatPanel::Initialize()
109 mpPaperSizeBox->FillPaperSizeEntries( PaperSizeApp::Std );
110 mpPaperSizeBox->SetDropDownLineCount(6);
111 meUnit = maPaperSizeController.GetCoreMetric();
112 SetFieldUnit( *mpPaperWidth, meFUnit );
113 SetFieldUnit( *mpPaperHeight, meFUnit );
114 aCustomEntry = mpCustomEntry->GetText();
116 const SvtOptionsDrawinglayer aDrawinglayerOpt;
117 mpPaperWidth->SetMax(mpPaperWidth->Normalize(aDrawinglayerOpt.GetMaximumPaperWidth()), FUNIT_CM);
118 mpPaperWidth->SetLast(mpPaperWidth->Normalize(aDrawinglayerOpt.GetMaximumPaperWidth()), FUNIT_CM);
119 mpPaperHeight->SetMax(mpPaperHeight->Normalize(aDrawinglayerOpt.GetMaximumPaperHeight()), FUNIT_CM);
120 mpPaperHeight->SetLast(mpPaperHeight->Normalize(aDrawinglayerOpt.GetMaximumPaperHeight()), FUNIT_CM);
122 mpPaperSizeBox->SetSelectHdl( LINK(this, PageFormatPanel, PaperFormatModifyHdl ));
123 mpPaperOrientation->SetSelectHdl( LINK(this, PageFormatPanel, PaperFormatModifyHdl ));
124 mpPaperHeight->SetModifyHdl( LINK(this, PageFormatPanel, PaperSizeModifyHdl ));
125 mpPaperWidth->SetModifyHdl( LINK(this, PageFormatPanel, PaperSizeModifyHdl ));
126 mpMarginSelectBox->SetSelectHdl( LINK(this, PageFormatPanel, PaperModifyMarginHdl));
128 mpBindings->Update(SID_ATTR_METRIC);
129 mpBindings->Update(SID_ATTR_PAGE);
130 mpBindings->Update(SID_ATTR_PAGE_SIZE);
131 mpBindings->Update( SID_ATTR_PAGE_LRSPACE );
132 mpBindings->Update( SID_ATTR_PAGE_ULSPACE );
134 UpdateMarginBox();
137 void PageFormatPanel::NotifyItemUpdate(
138 const sal_uInt16 nSId,
139 const SfxItemState eState,
140 const SfxPoolItem* pState,
141 const bool bIsEnabled)
143 (void)bIsEnabled;
145 switch(nSId)
147 case SID_ATTR_PAGE_SIZE:
149 const SvxSizeItem* pSizeItem = nullptr;
150 if (eState >= SfxItemState::DEFAULT)
151 pSizeItem = dynamic_cast< const SvxSizeItem* >(pState);
152 if (pSizeItem)
154 Size aPaperSize = pSizeItem->GetSize();
156 mpPaperWidth->SetValue( mpPaperWidth->Normalize( aPaperSize.Width() ), FUNIT_TWIP );
157 mpPaperHeight->SetValue( mpPaperHeight->Normalize( aPaperSize.Height() ), FUNIT_TWIP );
159 if(mpPaperOrientation->GetSelectEntryPos() == 1)
160 Swap(aPaperSize);
162 Paper ePaper = SvxPaperInfo::GetSvxPaper(aPaperSize, meUnit,true);
163 mpPaperSizeBox->SetSelection( ePaper );
166 break;
167 case SID_ATTR_METRIC:
169 meUnit = maPaperSizeController.GetCoreMetric();
170 meFUnit = GetCurrentUnit(eState, pState);
171 if(meFUnit != meLastFUnit)
173 SetFieldUnit( *mpPaperHeight, meFUnit );
174 SetFieldUnit( *mpPaperWidth, meFUnit );
176 meLastFUnit = meFUnit;
178 break;
179 case SID_ATTR_PAGE:
181 if ( eState >= SfxItemState::DEFAULT &&
182 pState && dynamic_cast< const SvxPageItem *>( pState ) != nullptr )
184 mpPageItem.reset( static_cast<SvxPageItem*>(pState->Clone()) );
185 if ( mpPageItem->IsLandscape() )
186 mpPaperOrientation->SelectEntryPos(1);
187 else
188 mpPaperOrientation->SelectEntryPos(0);
191 break;
192 case SID_ATTR_PAGE_LRSPACE:
194 if ( eState >= SfxItemState::DEFAULT &&
195 pState && dynamic_cast< const SvxLongLRSpaceItem *>( pState ) != nullptr )
197 mpPageLRMarginItem.reset( static_cast<SvxLongLRSpaceItem*>(pState->Clone()) );
198 UpdateMarginBox();
201 break;
202 case SID_ATTR_PAGE_ULSPACE:
204 if ( eState >= SfxItemState::DEFAULT &&
205 pState && dynamic_cast< const SvxLongULSpaceItem *>( pState ) != nullptr )
207 mpPageULMarginItem.reset( static_cast<SvxLongULSpaceItem*>(pState->Clone()) );
208 UpdateMarginBox();
211 break;
212 default:
213 break;
217 IMPL_LINK_NOARG(PageFormatPanel, PaperFormatModifyHdl, ListBox&, void)
219 Paper ePaper = mpPaperSizeBox->GetSelection();
220 Size aSize(SvxPaperInfo::GetPaperSize(ePaper, meUnit));
222 if(mpPaperOrientation->GetSelectEntryPos() == 1)
223 Swap(aSize);
225 mpPageItem->SetLandscape(mpPaperOrientation->GetSelectEntryPos() == 1);
226 SvxSizeItem aSizeItem(SID_ATTR_PAGE_SIZE, aSize);
227 mpBindings->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE, SfxCallMode::RECORD, { &aSizeItem, mpPageItem.get() });
230 IMPL_LINK_NOARG(PageFormatPanel, PaperSizeModifyHdl, Edit&, void)
232 Size aSize( GetCoreValue( *mpPaperWidth, meUnit ), GetCoreValue( *mpPaperHeight, meUnit));
233 SvxSizeItem aSizeItem(SID_ATTR_PAGE_SIZE, aSize);
234 mpBindings->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE, SfxCallMode::RECORD, { &aSizeItem });
237 IMPL_LINK_NOARG(PageFormatPanel, PaperModifyMarginHdl, ListBox&, void)
239 bool bMirrored = false;
240 bool bApplyNewPageMargins = true;
241 switch ( mpMarginSelectBox->GetSelectEntryPos() )
243 case 0:
244 SetNone(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
245 break;
246 case 1:
247 SetNarrow(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
248 break;
249 case 2:
250 SetModerate(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
251 break;
252 case 3:
253 SetNormal075(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
254 break;
255 case 4:
256 SetNormal100(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
257 break;
258 case 5:
259 SetNormal125(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
260 break;
261 case 6:
262 SetWide(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
263 break;
264 case 7:
265 SetMirrored(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored);
266 break;
267 default:
268 bApplyNewPageMargins = false;
269 break;
272 if(bApplyNewPageMargins)
274 ExecuteMarginLRChange( mnPageLeftMargin, mnPageRightMargin );
275 ExecuteMarginULChange( mnPageTopMargin, mnPageBottomMargin );
276 if(bMirrored != (mpPageItem->GetPageUsage() == SvxPageUsage::Mirror))
278 mpPageItem->SetPageUsage( bMirrored ? SvxPageUsage::Mirror : SvxPageUsage::All );
279 mpBindings->GetDispatcher()->ExecuteList(SID_ATTR_PAGE,
280 SfxCallMode::RECORD, { mpPageItem.get() });
285 FieldUnit PageFormatPanel::GetCurrentUnit( SfxItemState eState, const SfxPoolItem* pState )
287 FieldUnit eUnit = FUNIT_NONE;
289 if ( pState && eState >= SfxItemState::DEFAULT )
290 eUnit = (FieldUnit) static_cast<const SfxUInt16Item*>(pState)->GetValue();
291 else
293 SfxViewFrame* pFrame = SfxViewFrame::Current();
294 SfxObjectShell* pSh = nullptr;
295 if ( pFrame )
296 pSh = pFrame->GetObjectShell();
297 if ( pSh )
299 SfxModule* pModule = pSh->GetModule();
300 if ( pModule )
302 const SfxPoolItem* pItem = pModule->GetItem( SID_ATTR_METRIC );
303 if ( pItem )
304 eUnit = (FieldUnit) static_cast<const SfxUInt16Item*>(pItem)->GetValue();
306 else
308 SAL_WARN("sw.ui", "GetModuleFieldUnit(): no module found");
313 return eUnit;
316 void PageFormatPanel::ExecuteMarginLRChange( const long nPageLeftMargin, const long nPageRightMargin )
318 mpPageLRMarginItem->SetLeft( nPageLeftMargin );
319 mpPageLRMarginItem->SetRight( nPageRightMargin );
320 mpBindings->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_LRSPACE, SfxCallMode::RECORD, { mpPageLRMarginItem.get() });
323 void PageFormatPanel::ExecuteMarginULChange(const long nPageTopMargin, const long nPageBottomMargin)
325 mpPageULMarginItem->SetUpper( nPageTopMargin );
326 mpPageULMarginItem->SetLower( nPageBottomMargin );
327 mpBindings->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_ULSPACE, SfxCallMode::RECORD, { mpPageULMarginItem.get() });
330 void PageFormatPanel::UpdateMarginBox()
332 mnPageLeftMargin = mpPageLRMarginItem->GetLeft();
333 mnPageRightMargin = mpPageLRMarginItem->GetRight();
334 mnPageTopMargin = mpPageULMarginItem->GetUpper();
335 mnPageBottomMargin = mpPageULMarginItem->GetLower();
337 bool bMirrored = (mpPageItem->GetPageUsage() == SvxPageUsage::Mirror);
338 if( IsNone(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored) )
340 mpMarginSelectBox->SelectEntryPos(0);
341 mpMarginSelectBox->RemoveEntry(aCustomEntry);
343 else if( IsNarrow(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored) )
345 mpMarginSelectBox->SelectEntryPos(1);
346 mpMarginSelectBox->RemoveEntry(aCustomEntry);
348 else if( IsModerate(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored) )
350 mpMarginSelectBox->SelectEntryPos(2);
351 mpMarginSelectBox->RemoveEntry(aCustomEntry);
353 else if( IsNormal075(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored) )
355 mpMarginSelectBox->SelectEntryPos(3);
356 mpMarginSelectBox->RemoveEntry(aCustomEntry);
358 else if( IsNormal100(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored) )
360 mpMarginSelectBox->SelectEntryPos(4);
361 mpMarginSelectBox->RemoveEntry(aCustomEntry);
363 else if( IsNormal125(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored) )
365 mpMarginSelectBox->SelectEntryPos(5);
366 mpMarginSelectBox->RemoveEntry(aCustomEntry);
368 else if( IsWide(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored) )
370 mpMarginSelectBox->SelectEntryPos(6);
371 mpMarginSelectBox->RemoveEntry(aCustomEntry);
373 else if( IsMirrored(mnPageLeftMargin, mnPageRightMargin, mnPageTopMargin, mnPageBottomMargin, bMirrored) )
375 mpMarginSelectBox->SelectEntryPos(7);
376 mpMarginSelectBox->RemoveEntry(aCustomEntry);
378 else
380 if(mpMarginSelectBox->GetEntryPos(aCustomEntry) == LISTBOX_ENTRY_NOTFOUND)
381 mpMarginSelectBox->InsertEntry(aCustomEntry);
382 mpMarginSelectBox->SelectEntry(aCustomEntry);
388 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */