LibreOffice Module sw (master)  1
PageSizeControl.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <memory>
21 #include "PageSizeControl.hxx"
22 
23 #include <cmdid.h>
24 #include <svx/pageitem.hxx>
26 
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>
35 
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>
41 
42 #include <PageSizePopup.hxx>
43 
44 namespace
45 {
46  FieldUnit lcl_GetFieldUnit()
47  {
48  FieldUnit eUnit = FieldUnit::INCH;
49  const SfxPoolItem* pItem = nullptr;
50  SfxItemState eState = SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_METRIC, pItem );
51  if ( pItem && eState >= SfxItemState::DEFAULT )
52  {
53  eUnit = static_cast<FieldUnit>(static_cast<const SfxUInt16Item*>(pItem)->GetValue());
54  }
55  else
56  {
58  }
59 
60  return eUnit;
61  }
62 
63  MapUnit lcl_GetUnit()
64  {
65  SfxItemPool &rPool = SfxGetpApp()->GetPool();
66  sal_uInt16 nWhich = rPool.GetWhich( SID_ATTR_PAGE_SIZE );
67  return rPool.GetMetric( nWhich );
68  }
69 }
70 
71 namespace sw::sidebar {
72 
74  : WeldToolbarPopup(pControl->getFrameInterface(), pParent, "modules/swriter/ui/pagesizecontrol.ui", "PageSizeControl")
75  , mxMoreButton(m_xBuilder->weld_button("moreoptions"))
76  , mxWidthHeightField(m_xBuilder->weld_metric_spin_button("metric", FieldUnit::CM))
77  , mxSizeValueSet(new svx::sidebar::ValueSetWithTextControl)
78  , mxSizeValueSetWin(new weld::CustomWeld(*m_xBuilder, "pagesizevalueset", *mxSizeValueSet))
79  , mxControl(pControl)
80 {
81  mxWidthHeightField->set_unit(FieldUnit::CM);
82  mxWidthHeightField->set_range(0, 9999, FieldUnit::NONE);
83  mxWidthHeightField->set_digits(2);
84  mxWidthHeightField->set_increments(10, 100, FieldUnit::NONE);
85  SetFieldUnit( *mxWidthHeightField, lcl_GetFieldUnit() );
86 
87  maPaperList.push_back( PAPER_A3 );
88  maPaperList.push_back( PAPER_A4 );
89  maPaperList.push_back( PAPER_A5 );
90  maPaperList.push_back( PAPER_B4_ISO );
91  maPaperList.push_back( PAPER_B5_ISO );
92  maPaperList.push_back( PAPER_ENV_C5 );
93  maPaperList.push_back( PAPER_LETTER );
94  maPaperList.push_back( PAPER_LEGAL );
95 
96  mxSizeValueSet->SetStyle( mxSizeValueSet->GetStyle() | WB_3DLOOK | WB_NO_DIRECTSELECT );
97  mxSizeValueSet->SetColor( Application::GetSettings().GetStyleSettings().GetMenuColor() );
98 
99  sal_uInt16 nSelectedItem = 0;
100  {
101  OUString aMetricStr;
102  {
103  const OUString aText = mxWidthHeightField->get_text();
104  for (short i = aText.getLength() - 1; i >= 0; i--)
105  {
106  sal_Unicode c = aText[i];
107  if ( rtl::isAsciiAlpha(c) || (c == '\'') || (c == '\"') || (c == '%') )
108  {
109  aMetricStr = OUStringChar(c) + aMetricStr;
110  }
111  else
112  {
113  if (!aMetricStr.isEmpty())
114  {
115  break;
116  }
117  }
118  }
119  }
120 
121  bool bLandscape = false;
122  const SvxSizeItem* pSize = nullptr;
123  if ( SfxViewFrame::Current() )
124  {
125  const SfxPoolItem* pItem;
126  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE, pItem );
127  bLandscape = static_cast<const SvxPageItem*>(pItem)->IsLandscape();
128  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE_SIZE, pItem );
129  pSize = static_cast<const SvxSizeItem*>(pItem);
130  }
131 
132  const LocaleDataWrapper& localeDataWrapper = Application::GetSettings().GetLocaleDataWrapper();
133  OUString aWidthStr;
134  OUString aHeightStr;
135  OUString aItemText2;
136  for ( std::vector< Paper >::size_type nPaperIdx = 0;
137  nPaperIdx < maPaperList.size();
138  ++nPaperIdx )
139  {
140  Size aPaperSize = SvxPaperInfo::GetPaperSize( maPaperList[ nPaperIdx ] );
141  if ( bLandscape )
142  {
143  Swap( aPaperSize );
144  }
145 
146  mxWidthHeightField->set_value( mxWidthHeightField->normalize( aPaperSize.Width() ), FieldUnit::TWIP );
147  aWidthStr = localeDataWrapper.getNum(
148  mxWidthHeightField->get_value(FieldUnit::NONE),
149  mxWidthHeightField->get_digits(),
150  true,
151  true );
152 
153  mxWidthHeightField->set_value( mxWidthHeightField->normalize( aPaperSize.Height() ), FieldUnit::TWIP);
154  aHeightStr = localeDataWrapper.getNum(
155  mxWidthHeightField->get_value(FieldUnit::NONE),
156  mxWidthHeightField->get_digits(),
157  true,
158  true );
159 
160  aItemText2 = aWidthStr + " x " + aHeightStr + " " + aMetricStr;
161 
162  mxSizeValueSet->AddItem(
163  SvxPaperInfo::GetName( maPaperList[ nPaperIdx ] ),
164  aItemText2 );
165 
166  if ( pSize && aPaperSize == pSize->GetSize() )
167  {
168  nSelectedItem = nPaperIdx + 1;
169  }
170  }
171  }
172  mxSizeValueSet->SetNoSelection();
173  mxSizeValueSet->SetSelectHdl( LINK(this, PageSizeControl, ImplSizeHdl ) );
174  mxSizeValueSet->Show();
175  mxSizeValueSet->Resize();
176 
177  mxSizeValueSet->SelectItem( nSelectedItem );
178  mxSizeValueSet->SetFormat();
179  mxSizeValueSet->Invalidate();
180 
181  mxMoreButton->connect_clicked( LINK( this, PageSizeControl, MoreButtonClickHdl_Impl ) );
182  mxMoreButton->grab_focus();
183 }
184 
186 {
187  mxSizeValueSet->GrabFocus();
188 }
189 
191 {
192 }
193 
195 {
196  bool bLandscape = false;
197  const SfxPoolItem *pItem;
198  MapUnit eUnit = lcl_GetUnit();
199  if ( !SfxViewFrame::Current() )
200  return;
201 
202  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE, pItem );
203  bLandscape = static_cast<const SvxPageItem*>(pItem)->IsLandscape();
204 
205  SvxSizeItem aPageSizeItem(SID_ATTR_PAGE_SIZE);
206  Size aPageSize = SvxPaperInfo::GetPaperSize( ePaper, eUnit );
207  if ( bLandscape )
208  {
209  Swap( aPageSize );
210  }
211  aPageSizeItem.SetSize( aPageSize );
212 
213  SfxViewFrame::Current()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE,
214  SfxCallMode::RECORD, { &aPageSizeItem });
215 }
216 
217 
219 {
220  mxSizeValueSet->SetNoSelection();
221  const sal_uInt16 nSelectedPaper = mxSizeValueSet->GetSelectedItemId();
222  const Paper ePaper = maPaperList[nSelectedPaper - 1];
223  ExecuteSizeChange( ePaper );
224 
225  mxControl->EndPopupMode();
226 }
227 
228 IMPL_LINK_NOARG(PageSizeControl, MoreButtonClickHdl_Impl, weld::Button&, void)
229 {
230  if ( SfxViewFrame::Current() )
232  mxControl->EndPopupMode();
233 }
234 
235 } // end of namespace sw::sidebar
236 
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual ~PageSizeControl() override
void SetFieldUnit(weld::MetricSpinButton &rField, FieldUnit eUnit, bool bAll)
const SfxPoolItem * ExecuteList(sal_uInt16 nSlot, SfxCallMode nCall, std::initializer_list< SfxPoolItem const * > args, std::initializer_list< SfxPoolItem const * > internalargs=std::initializer_list< SfxPoolItem const * >())
FieldUnit
const LocaleDataWrapper & GetLocaleDataWrapper() const
Size & Swap(Size &rSize)
std::unique_ptr< weld::MetricSpinButton > mxWidthHeightField
std::string GetValue
PAPER_ENV_C5
PAPER_B5_ISO
SfxDispatcher * GetDispatcher()
static const AllSettings & GetSettings()
static FieldUnit GetCurrentFieldUnit()
SfxApplication * SfxGetpApp()
constexpr tools::Long Width() const
virtual MapUnit GetMetric(sal_uInt16 nWhich) const
sal_uInt16 sal_Unicode
std::unique_ptr< weld::Button > mxMoreButton
PAPER_LEGAL
std::vector< Paper > maPaperList
SfxItemPool & GetPool() const
int i
virtual void GrabFocus() override
#define FN_FORMAT_PAGE_SETTING_DLG
Definition: cmdid.h:312
SfxBindings & GetBindings()
static OUString GetName(Paper ePaper)
Paper
static Size GetPaperSize(Paper ePaper, MapUnit eUnit=MapUnit::MapTwip)
IMPL_LINK_NOARG(PageColumnControl, MoreButtonClickHdl_Impl, weld::Button &, void)
SfxItemState QueryState(sal_uInt16 nSID, const SfxPoolItem *&rpState)
WinBits const WB_3DLOOK
static SfxViewFrame * Current()
SfxItemState
#define WB_NO_DIRECTSELECT
sal_uInt16 GetWhich(sal_uInt16 nSlot, bool bDeep=true) const
constexpr tools::Long Height() const
static void ExecuteSizeChange(const Paper ePaper)
void SetSize(const Size &rSize)
PAPER_B4_ISO
PAPER_A3
PAPER_LETTER
SfxDispatcher * GetDispatcher() const
MapUnit
OUString getNum(sal_Int64 nNumber, sal_uInt16 nDecimals, bool bUseThousandSep=true, bool bTrailingZeros=true) const
const SfxPoolItem * Execute(sal_uInt16 nSlot, SfxCallMode nCall=SfxCallMode::SLOT, const SfxPoolItem **pArgs=nullptr, sal_uInt16 nModi=0, const SfxPoolItem **pInternalArgs=nullptr)
PageSizeControl(PageSizePopup *pControl, weld::Widget *pParent)
PAPER_A5
PAPER_A4
const Size & GetSize() const
std::unique_ptr< svx::sidebar::ValueSetWithTextControl > mxSizeValueSet