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 <swtypes.hxx>
25 #include <svx/svxids.hrc>
26 #include <svx/pageitem.hxx>
28 
30 #include <rtl/character.hxx>
31 #include <editeng/paperinf.hxx>
32 #include <sfx2/app.hxx>
33 #include <sfx2/bindings.hxx>
34 #include <sfx2/dispatch.hxx>
35 #include <sfx2/module.hxx>
36 #include <sfx2/viewfrm.hxx>
37 
38 #include <vcl/settings.hxx>
39 #include <svl/itempool.hxx>
40 #include <svl/intitem.hxx>
41 #include <svtools/unitconv.hxx>
42 #include <editeng/sizeitem.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 { namespace sidebar {
72 
73 PageSizeControl::PageSizeControl( sal_uInt16 nId, vcl::Window* pParent )
74  : SfxPopupWindow( nId, pParent, "PageSizeControl", "modules/swriter/ui/pagesizecontrol.ui" )
75  , maPaperList()
76 {
77  get(maMoreButton, "moreoptions");
78  get(maContainer, "container");
80 
81  // Avoid flicker when hovering over the menu items.
82  if (!IsNativeControlSupported(ControlType::Pushbutton, ControlPart::Focus))
83  // If NWF renders the focus rects itself, that breaks double-buffering.
84  mpSizeValueSet->RequestDoubleBuffering(true);
85 
88  maWidthHeightField->SetUnit(FieldUnit::CM);
93  SetFieldUnit( *maWidthHeightField, lcl_GetFieldUnit() );
94 
95  maPaperList.push_back( PAPER_A3 );
96  maPaperList.push_back( PAPER_A4 );
97  maPaperList.push_back( PAPER_A5 );
98  maPaperList.push_back( PAPER_B4_ISO );
99  maPaperList.push_back( PAPER_B5_ISO );
100  maPaperList.push_back( PAPER_ENV_C5 );
101  maPaperList.push_back( PAPER_LETTER );
102  maPaperList.push_back( PAPER_LEGAL );
103 
104  mpSizeValueSet->SetStyle( mpSizeValueSet->GetStyle() | WB_3DLOOK | WB_NO_DIRECTSELECT );
105  mpSizeValueSet->SetColor( GetSettings().GetStyleSettings().GetMenuColor() );
106 
107  sal_uInt16 nSelectedItem = 0;
108  {
109  OUString aMetricStr;
110  {
111  const OUString aText = maWidthHeightField->GetText();
112  for (short i = aText.getLength() - 1; i >= 0; i--)
113  {
114  sal_Unicode c = aText[i];
115  if ( rtl::isAsciiAlpha(c) || (c == '\'') || (c == '\"') || (c == '%') )
116  {
117  aMetricStr = OUStringLiteral1(c) + aMetricStr;
118  }
119  else
120  {
121  if (!aMetricStr.isEmpty())
122  {
123  break;
124  }
125  }
126  }
127  }
128 
129  bool bLandscape = false;
130  const SfxPoolItem* pItem;
131  const SvxSizeItem* pSize = nullptr;
132  if ( SfxViewFrame::Current() )
133  {
134  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE, pItem );
135  bLandscape = static_cast<const SvxPageItem*>(pItem)->IsLandscape();
136  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE_SIZE, pItem );
137  pSize = static_cast<const SvxSizeItem*>(pItem);
138  }
139 
140  const LocaleDataWrapper& localeDataWrapper = maWidthHeightField->GetLocaleDataWrapper();
141  OUString aWidthStr;
142  OUString aHeightStr;
143  OUString aItemText2;
144  for ( std::vector< Paper >::size_type nPaperIdx = 0;
145  nPaperIdx < maPaperList.size();
146  ++nPaperIdx )
147  {
148  Size aPaperSize = SvxPaperInfo::GetPaperSize( maPaperList[ nPaperIdx ] );
149  if ( bLandscape )
150  {
151  Swap( aPaperSize );
152  }
153 
154  maWidthHeightField->SetValue( maWidthHeightField->Normalize( aPaperSize.Width() ), FieldUnit::TWIP );
155  aWidthStr = localeDataWrapper.getNum(
160 
161  maWidthHeightField->SetValue( maWidthHeightField->Normalize( aPaperSize.Height() ), FieldUnit::TWIP);
162  aHeightStr = localeDataWrapper.getNum(
167 
168  aItemText2 = aWidthStr + " x " + aHeightStr + " " + aMetricStr;
169 
171  SvxPaperInfo::GetName( maPaperList[ nPaperIdx ] ),
172  aItemText2 );
173 
174  if ( pSize && aPaperSize == pSize->GetSize() )
175  {
176  nSelectedItem = nPaperIdx + 1;
177  }
178  }
179  }
180  mpSizeValueSet->SetNoSelection();
181  mpSizeValueSet->SetSelectHdl( LINK(this, PageSizeControl, ImplSizeHdl ) );
182  mpSizeValueSet->Show();
183  mpSizeValueSet->Resize();
184 
185  mpSizeValueSet->SelectItem( nSelectedItem );
186  mpSizeValueSet->SetFormat();
187  mpSizeValueSet->Invalidate();
188  mpSizeValueSet->StartSelection();
189 
190  maMoreButton->SetClickHdl( LINK( this, PageSizeControl, MoreButtonClickHdl_Impl ) );
192 }
193 
195 {
196  disposeOnce();
197 }
198 
200 {
206 }
207 
209 {
210  bool bLandscape = false;
211  const SfxPoolItem *pItem;
212  MapUnit eUnit = lcl_GetUnit();
213  if ( SfxViewFrame::Current() )
214  {
215  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE, pItem );
216  bLandscape = static_cast<const SvxPageItem*>(pItem)->IsLandscape();
217 
218  std::unique_ptr<SvxSizeItem> pPageSizeItem( new SvxSizeItem(SID_ATTR_PAGE_SIZE) );
219  Size aPageSize = SvxPaperInfo::GetPaperSize( ePaper, eUnit );
220  if ( bLandscape )
221  {
222  Swap( aPageSize );
223  }
224  pPageSizeItem->SetSize( aPageSize );
225 
226  SfxViewFrame::Current()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE,
227  SfxCallMode::RECORD, { pPageSizeItem.get() });
228  }
229 }
230 
231 
232 IMPL_LINK(PageSizeControl, ImplSizeHdl, ValueSet*, pControl, void)
233 {
234  mpSizeValueSet->SetNoSelection();
235  if ( pControl == mpSizeValueSet )
236  {
237  const sal_uInt16 nSelectedPaper = mpSizeValueSet->GetSelectedItemId();
238  const Paper ePaper = maPaperList[nSelectedPaper - 1];
239  ExecuteSizeChange( ePaper );
240  }
241 
242  EndPopupMode();
243 }
244 
245 IMPL_LINK_NOARG(PageSizeControl, MoreButtonClickHdl_Impl, Button*, void)
246 {
247  if ( SfxViewFrame::Current() )
249  EndPopupMode();
250 }
251 
252 } } // end of namespace sw::sidebar
253 
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
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
Size & Swap(Size &rSize)
void SetClickHdl(const Link< Button *, void > &rLink)
std::string GetValue
long Height() const
PAPER_LEGAL
SfxDispatcher * GetDispatcher()
void disposeAndClear()
static FieldUnit GetCurrentFieldUnit()
void SetMax(sal_Int64 nNewMax, FieldUnit eInUnit)
Dialog to specify the properties of date form field.
PageSizeControl(sal_uInt16 nId, vcl::Window *pParent)
void SetSpinSize(sal_Int64 nNewSize)
SfxApplication * SfxGetpApp()
virtual MapUnit GetMetric(sal_uInt16 nWhich) const
Paper
sal_uInt16 sal_Unicode
virtual void dispose() override
sal_uInt16 GetDecimalDigits() const
PAPER_A4
VclPtr< MetricField > maWidthHeightField
SfxItemPool & GetPool() const
void AddItem(const OUString &rItemText, const OUString &rItemText2)
PAPER_A5
PAPER_ENV_C5
IMPL_LINK_NOARG(PageColumnControl, MoreButtonClickHdl_Impl, Button *, void)
#define FN_FORMAT_PAGE_SETTING_DLG
Definition: cmdid.h:295
PAPER_B4_ISO
sal_Int64 Normalize(sal_Int64 nValue) const
SfxBindings & GetBindings()
int i
virtual OUString GetText() const override
static OUString GetName(Paper ePaper)
static Size GetPaperSize(Paper ePaper, MapUnit eUnit=MapUnit::MapTwip)
SfxItemState QueryState(sal_uInt16 nSID, const SfxPoolItem *&rpState)
void GrabFocus()
VclPtr< svx::sidebar::ValueSetWithTextControl > mpSizeValueSet
void SetLast(sal_Int64 nNewLast, FieldUnit eInUnit)
bool IsShowTrailingZeros() const
WinBits const WB_3DLOOK
virtual void SetUnit(FieldUnit meUnit) override
static SfxViewFrame * Current()
SfxItemState
#define WB_NO_DIRECTSELECT
VclPtr< PushButton > maMoreButton
sal_uInt16 GetWhich(sal_uInt16 nSlot, bool bDeep=true) const
WinBits const WB_BORDER
static void ExecuteSizeChange(const Paper ePaper)
PAPER_A3
static VclPtr< reference_type > Create(Arg &&...arg)
PAPER_B5_ISO
void SetDecimalDigits(sal_uInt16 nDigits)
virtual void SetValue(sal_Int64 nNewValue, FieldUnit eInUnit)
SfxDispatcher * GetDispatcher() const
VclVBox * get() const
bool IsUseThousandSep() const
MapUnit
std::vector< Paper > maPaperList
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)
sal_Int64 GetValue(FieldUnit eOutUnit) const
IMPL_LINK(PageColumnControl, ColumnButtonClickHdl_Impl, Button *, pButton, void)
const LocaleDataWrapper & GetLocaleDataWrapper() const
virtual void dispose() override
PAPER_LETTER
const Size & GetSize() const