Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / uibase / sidebar / PageSizeControl.cxx
blob211ebece9e5eacaafc7efc9405accaa397a1fa1d
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 <svx/pageitem.hxx>
25 #include <svx/sidebar/ValueSetWithTextControl.hxx>
27 #include <unotools/localedatawrapper.hxx>
28 #include <rtl/character.hxx>
29 #include <editeng/paperinf.hxx>
30 #include <sfx2/app.hxx>
31 #include <sfx2/bindings.hxx>
32 #include <sfx2/dispatch.hxx>
33 #include <sfx2/module.hxx>
34 #include <sfx2/viewfrm.hxx>
36 #include <vcl/settings.hxx>
37 #include <svl/itempool.hxx>
38 #include <svl/intitem.hxx>
39 #include <svtools/unitconv.hxx>
40 #include <editeng/sizeitem.hxx>
42 #include <PageSizePopup.hxx>
44 namespace
46 FieldUnit lcl_GetFieldUnit()
48 const SfxUInt16Item* pItem = nullptr;
49 if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
51 SfxItemState eState = pViewFrm->GetBindings().GetDispatcher()->QueryState(SID_ATTR_METRIC, pItem);
52 if (pItem && eState >= SfxItemState::DEFAULT)
53 return static_cast<FieldUnit>(pItem->GetValue());
55 return SfxModule::GetCurrentFieldUnit();
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::sidebar {
68 PageSizeControl::PageSizeControl(PageSizePopup* pControl, weld::Widget* pParent)
69 : WeldToolbarPopup(pControl->getFrameInterface(), pParent, "modules/swriter/ui/pagesizecontrol.ui", "PageSizeControl")
70 , mxMoreButton(m_xBuilder->weld_button("moreoptions"))
71 , mxWidthHeightField(m_xBuilder->weld_metric_spin_button("metric", FieldUnit::CM))
72 , mxSizeValueSet(new svx::sidebar::ValueSetWithTextControl)
73 , mxSizeValueSetWin(new weld::CustomWeld(*m_xBuilder, "pagesizevalueset", *mxSizeValueSet))
74 , mxControl(pControl)
76 mxWidthHeightField->set_unit(FieldUnit::CM);
77 mxWidthHeightField->set_range(0, 9999, FieldUnit::NONE);
78 mxWidthHeightField->set_digits(2);
79 mxWidthHeightField->set_increments(10, 100, FieldUnit::NONE);
80 SetFieldUnit( *mxWidthHeightField, lcl_GetFieldUnit() );
82 maPaperList.push_back( PAPER_A3 );
83 maPaperList.push_back( PAPER_A4 );
84 maPaperList.push_back( PAPER_A5 );
85 maPaperList.push_back( PAPER_B4_ISO );
86 maPaperList.push_back( PAPER_B5_ISO );
87 maPaperList.push_back( PAPER_ENV_C5 );
88 maPaperList.push_back( PAPER_LETTER );
89 maPaperList.push_back( PAPER_LEGAL );
91 mxSizeValueSet->SetStyle( mxSizeValueSet->GetStyle() | WB_3DLOOK | WB_NO_DIRECTSELECT );
92 mxSizeValueSet->SetColor( Application::GetSettings().GetStyleSettings().GetMenuColor() );
94 sal_uInt16 nSelectedItem = 0;
96 OUString aMetricStr;
98 const OUString aText = mxWidthHeightField->get_text();
99 for (short i = aText.getLength() - 1; i >= 0; i--)
101 sal_Unicode c = aText[i];
102 if ( rtl::isAsciiAlpha(c) || (c == '\'') || (c == '\"') || (c == '%') )
104 aMetricStr = OUStringChar(c) + aMetricStr;
106 else
108 if (!aMetricStr.isEmpty())
110 break;
116 bool bLandscape = false;
117 const SvxSizeItem* pSize = nullptr;
118 if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
120 const SvxPageItem* pPageItem;
121 pViewFrm->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE, pPageItem );
122 bLandscape = pPageItem->IsLandscape();
123 pViewFrm->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE_SIZE, pSize );
126 const LocaleDataWrapper& localeDataWrapper = Application::GetSettings().GetLocaleDataWrapper();
127 OUString aWidthStr;
128 OUString aHeightStr;
129 OUString aItemText2;
130 for ( std::vector< Paper >::size_type nPaperIdx = 0;
131 nPaperIdx < maPaperList.size();
132 ++nPaperIdx )
134 Size aPaperSize = SvxPaperInfo::GetPaperSize( maPaperList[ nPaperIdx ] );
135 if ( bLandscape )
137 Swap( aPaperSize );
140 mxWidthHeightField->set_value( mxWidthHeightField->normalize( aPaperSize.Width() ), FieldUnit::TWIP );
141 aWidthStr = localeDataWrapper.getNum(
142 mxWidthHeightField->get_value(FieldUnit::NONE),
143 mxWidthHeightField->get_digits(),
144 true,
145 true );
147 mxWidthHeightField->set_value( mxWidthHeightField->normalize( aPaperSize.Height() ), FieldUnit::TWIP);
148 aHeightStr = localeDataWrapper.getNum(
149 mxWidthHeightField->get_value(FieldUnit::NONE),
150 mxWidthHeightField->get_digits(),
151 true,
152 true );
154 aItemText2 = aWidthStr + " x " + aHeightStr + " " + aMetricStr;
156 mxSizeValueSet->AddItem(
157 SvxPaperInfo::GetName( maPaperList[ nPaperIdx ] ),
158 aItemText2 );
160 if ( pSize && aPaperSize == pSize->GetSize() )
162 nSelectedItem = nPaperIdx + 1;
166 mxSizeValueSet->SetNoSelection();
167 mxSizeValueSet->SetSelectHdl( LINK(this, PageSizeControl, ImplSizeHdl ) );
168 mxSizeValueSet->Show();
169 mxSizeValueSet->Resize();
171 mxSizeValueSet->SelectItem( nSelectedItem );
172 mxSizeValueSet->SetFormat();
173 mxSizeValueSet->Invalidate();
175 mxMoreButton->connect_clicked( LINK( this, PageSizeControl, MoreButtonClickHdl_Impl ) );
176 mxMoreButton->grab_focus();
179 void PageSizeControl::GrabFocus()
181 mxSizeValueSet->GrabFocus();
184 PageSizeControl::~PageSizeControl()
188 void PageSizeControl::ExecuteSizeChange( const Paper ePaper )
190 bool bLandscape = false;
191 const SvxPageItem *pItem;
192 MapUnit eUnit = lcl_GetUnit();
193 SfxViewFrame* pViewFrm = SfxViewFrame::Current();
194 if (!pViewFrm)
195 return;
197 pViewFrm->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE, pItem );
198 bLandscape = pItem->IsLandscape();
200 SvxSizeItem aPageSizeItem(SID_ATTR_PAGE_SIZE);
201 Size aPageSize = SvxPaperInfo::GetPaperSize( ePaper, eUnit );
202 if ( bLandscape )
204 Swap( aPageSize );
206 aPageSizeItem.SetSize( aPageSize );
208 pViewFrm->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE,
209 SfxCallMode::RECORD, { &aPageSizeItem });
213 IMPL_LINK_NOARG(PageSizeControl, ImplSizeHdl, ValueSet*, void)
215 mxSizeValueSet->SetNoSelection();
216 const sal_uInt16 nSelectedPaper = mxSizeValueSet->GetSelectedItemId();
217 const Paper ePaper = maPaperList[nSelectedPaper - 1];
218 ExecuteSizeChange( ePaper );
220 mxControl->EndPopupMode();
223 IMPL_LINK_NOARG(PageSizeControl, MoreButtonClickHdl_Impl, weld::Button&, void)
225 if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
226 pViewFrm->GetDispatcher()->Execute( FN_FORMAT_PAGE_SETTING_DLG, SfxCallMode::ASYNCHRON );
227 mxControl->EndPopupMode();
230 } // end of namespace sw::sidebar
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */