Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / sw / source / uibase / sidebar / PageSizeControl.cxx
blob6dfb0867a9f19e0cc73d05cb7672a9a23a8da827
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 .
20 #include <memory>
21 #include "PageSizeControl.hxx"
23 #include <cmdid.h>
24 #include <swtypes.hxx>
25 #include <svx/svxids.hrc>
26 #include <svx/pageitem.hxx>
27 #include <svx/sidebar/ValueSetWithTextControl.hxx>
29 #include <rtl/character.hxx>
30 #include <editeng/paperinf.hxx>
31 #include <sfx2/bindings.hxx>
32 #include <sfx2/dispatch.hxx>
34 #include <vcl/settings.hxx>
35 #include <svl/itempool.hxx>
36 #include <svl/intitem.hxx>
37 #include <editeng/sizeitem.hxx>
39 namespace
41 FieldUnit lcl_GetFieldUnit()
43 FieldUnit eUnit = FUNIT_INCH;
44 const SfxPoolItem* pItem = nullptr;
45 SfxItemState eState = SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_METRIC, pItem );
46 if ( pItem && eState >= SfxItemState::DEFAULT )
48 eUnit = (FieldUnit)static_cast<const SfxUInt16Item*>(pItem)->GetValue();
50 else
52 return SfxModule::GetCurrentFieldUnit();
55 return eUnit;
58 MapUnit lcl_GetUnit()
60 SfxItemPool &rPool = SfxGetpApp()->GetPool();
61 sal_uInt16 nWhich = rPool.GetWhich( SID_ATTR_PAGE_SIZE );
62 return rPool.GetMetric( nWhich );
66 namespace sw { namespace sidebar {
68 PageSizeControl::PageSizeControl( sal_uInt16 nId )
69 : SfxPopupWindow( nId, "PageSizeControl", "modules/swriter/ui/pagesizecontrol.ui" )
70 , maPaperList()
72 get(maMoreButton, "moreoptions");
73 get(maContainer, "container");
74 mpSizeValueSet = VclPtr<svx::sidebar::ValueSetWithTextControl>::Create( maContainer.get(), WB_BORDER );
75 maWidthHeightField = VclPtr<MetricField>::Create( maContainer.get(), 0 );
76 maWidthHeightField->Hide();
77 maWidthHeightField->SetUnit(FUNIT_CM);
78 maWidthHeightField->SetMax(9999);
79 maWidthHeightField->SetDecimalDigits(2);
80 maWidthHeightField->SetSpinSize(10);
81 maWidthHeightField->SetLast(9999);
82 SetFieldUnit( *maWidthHeightField.get(), lcl_GetFieldUnit() );
84 maPaperList.push_back( PAPER_A3 );
85 maPaperList.push_back( PAPER_A4 );
86 maPaperList.push_back( PAPER_A5 );
87 maPaperList.push_back( PAPER_B4_ISO );
88 maPaperList.push_back( PAPER_B5_ISO );
89 maPaperList.push_back( PAPER_ENV_C5 );
90 maPaperList.push_back( PAPER_LETTER );
91 maPaperList.push_back( PAPER_LEGAL );
93 mpSizeValueSet->SetStyle( mpSizeValueSet->GetStyle() | WB_3DLOOK | WB_NO_DIRECTSELECT );
94 mpSizeValueSet->SetColor( GetSettings().GetStyleSettings().GetMenuColor() );
96 sal_uInt16 nSelectedItem = 0;
98 OUString aMetricStr;
100 const OUString aText = maWidthHeightField->GetText();
101 for (short i = aText.getLength() - 1; i >= 0; i--)
103 sal_Unicode c = aText[i];
104 if ( rtl::isAsciiAlpha(c) || (c == '\'') || (c == '\"') || (c == '%') )
106 aMetricStr = OUStringLiteral1(c) + aMetricStr;
108 else
110 if (!aMetricStr.isEmpty())
112 break;
118 bool bLandscape = false;
119 const SfxPoolItem* pItem;
120 const SvxSizeItem* pSize = nullptr;
121 if ( SfxViewFrame::Current() )
123 SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE, pItem );
124 bLandscape = static_cast<const SvxPageItem*>(pItem)->IsLandscape();
125 SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE_SIZE, pItem );
126 pSize = static_cast<const SvxSizeItem*>(pItem);
129 const LocaleDataWrapper& localeDataWrapper = maWidthHeightField->GetLocaleDataWrapper();
130 OUString aWidthStr;
131 OUString aHeightStr;
132 OUString aItemText2;
133 for ( std::vector< Paper >::size_type nPaperIdx = 0;
134 nPaperIdx < maPaperList.size();
135 ++nPaperIdx )
137 Size aPaperSize = SvxPaperInfo::GetPaperSize( maPaperList[ nPaperIdx ] );
138 if ( bLandscape )
140 Swap( aPaperSize );
143 maWidthHeightField->SetValue( maWidthHeightField->Normalize( aPaperSize.Width() ), FUNIT_TWIP );
144 aWidthStr = localeDataWrapper.getNum(
145 maWidthHeightField->GetValue(),
146 maWidthHeightField->GetDecimalDigits(),
147 maWidthHeightField->IsUseThousandSep(),
148 maWidthHeightField->IsShowTrailingZeros() );
150 maWidthHeightField->SetValue( maWidthHeightField->Normalize( aPaperSize.Height() ), FUNIT_TWIP);
151 aHeightStr = localeDataWrapper.getNum(
152 maWidthHeightField->GetValue(),
153 maWidthHeightField->GetDecimalDigits(),
154 maWidthHeightField->IsUseThousandSep(),
155 maWidthHeightField->IsShowTrailingZeros() );
157 aItemText2 = aWidthStr + " x " + aHeightStr + " " + aMetricStr;
159 mpSizeValueSet->AddItem(
160 SvxPaperInfo::GetName( maPaperList[ nPaperIdx ] ),
161 aItemText2 );
163 if ( pSize && aPaperSize == pSize->GetSize() )
165 nSelectedItem = nPaperIdx + 1;
169 mpSizeValueSet->SetNoSelection();
170 mpSizeValueSet->SetSelectHdl( LINK(this, PageSizeControl, ImplSizeHdl ) );
171 mpSizeValueSet->Show();
172 mpSizeValueSet->Resize();
174 mpSizeValueSet->SelectItem( nSelectedItem );
175 mpSizeValueSet->SetFormat();
176 mpSizeValueSet->Invalidate();
177 mpSizeValueSet->StartSelection();
179 maMoreButton->SetClickHdl( LINK( this, PageSizeControl, MoreButtonClickHdl_Impl ) );
180 maMoreButton->GrabFocus();
183 PageSizeControl::~PageSizeControl()
185 disposeOnce();
188 void PageSizeControl::dispose()
190 mpSizeValueSet.disposeAndClear();
191 maMoreButton.disposeAndClear();
192 maWidthHeightField.disposeAndClear();
193 maContainer.disposeAndClear();
194 SfxPopupWindow::dispose();
197 void PageSizeControl::ExecuteSizeChange( const Paper ePaper )
199 bool bLandscape = false;
200 const SfxPoolItem *pItem;
201 MapUnit eUnit = lcl_GetUnit();
202 if ( SfxViewFrame::Current() )
204 SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE, pItem );
205 bLandscape = static_cast<const SvxPageItem*>(pItem)->IsLandscape();
207 std::unique_ptr<SvxSizeItem> pPageSizeItem( new SvxSizeItem(SID_ATTR_PAGE_SIZE) );
208 Size aPageSize = SvxPaperInfo::GetPaperSize( ePaper, eUnit );
209 if ( bLandscape )
211 Swap( aPageSize );
213 pPageSizeItem->SetSize( aPageSize );
215 SfxViewFrame::Current()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE,
216 SfxCallMode::RECORD, { pPageSizeItem.get() });
221 IMPL_LINK(PageSizeControl, ImplSizeHdl, ValueSet*, pControl, void)
223 mpSizeValueSet->SetNoSelection();
224 if ( pControl == mpSizeValueSet )
226 const sal_uInt16 nSelectedPaper = mpSizeValueSet->GetSelectItemId();
227 const Paper ePaper = maPaperList[nSelectedPaper - 1];
228 ExecuteSizeChange( ePaper );
231 EndPopupMode();
234 IMPL_LINK_NOARG(PageSizeControl, MoreButtonClickHdl_Impl, Button*, void)
236 if ( SfxViewFrame::Current() )
237 SfxViewFrame::Current()->GetDispatcher()->Execute( FN_FORMAT_PAGE_SETTING_DLG, SfxCallMode::ASYNCHRON );
238 EndPopupMode();
241 } } // end of namespace sw::sidebar
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */