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");
81  maWidthHeightField->Hide();
82  maWidthHeightField->SetUnit(FieldUnit::CM);
83  maWidthHeightField->SetMax(9999);
84  maWidthHeightField->SetDecimalDigits(2);
85  maWidthHeightField->SetSpinSize(10);
86  maWidthHeightField->SetLast(9999);
87  SetFieldUnit( *maWidthHeightField, lcl_GetFieldUnit() );
88 
89  maPaperList.push_back( PAPER_A3 );
90  maPaperList.push_back( PAPER_A4 );
91  maPaperList.push_back( PAPER_A5 );
92  maPaperList.push_back( PAPER_B4_ISO );
93  maPaperList.push_back( PAPER_B5_ISO );
94  maPaperList.push_back( PAPER_ENV_C5 );
95  maPaperList.push_back( PAPER_LETTER );
96  maPaperList.push_back( PAPER_LEGAL );
97 
98  mpSizeValueSet->SetStyle( mpSizeValueSet->GetStyle() | WB_3DLOOK | WB_NO_DIRECTSELECT );
99  mpSizeValueSet->SetColor( GetSettings().GetStyleSettings().GetMenuColor() );
100 
101  sal_uInt16 nSelectedItem = 0;
102  {
103  OUString aMetricStr;
104  {
105  const OUString aText = maWidthHeightField->GetText();
106  for (short i = aText.getLength() - 1; i >= 0; i--)
107  {
108  sal_Unicode c = aText[i];
109  if ( rtl::isAsciiAlpha(c) || (c == '\'') || (c == '\"') || (c == '%') )
110  {
111  aMetricStr = OUStringLiteral1(c) + aMetricStr;
112  }
113  else
114  {
115  if (!aMetricStr.isEmpty())
116  {
117  break;
118  }
119  }
120  }
121  }
122 
123  bool bLandscape = false;
124  const SfxPoolItem* pItem;
125  const SvxSizeItem* pSize = nullptr;
126  if ( SfxViewFrame::Current() )
127  {
128  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE, pItem );
129  bLandscape = static_cast<const SvxPageItem*>(pItem)->IsLandscape();
130  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE_SIZE, pItem );
131  pSize = static_cast<const SvxSizeItem*>(pItem);
132  }
133 
134  const LocaleDataWrapper& localeDataWrapper = maWidthHeightField->GetLocaleDataWrapper();
135  OUString aWidthStr;
136  OUString aHeightStr;
137  OUString aItemText2;
138  for ( std::vector< Paper >::size_type nPaperIdx = 0;
139  nPaperIdx < maPaperList.size();
140  ++nPaperIdx )
141  {
142  Size aPaperSize = SvxPaperInfo::GetPaperSize( maPaperList[ nPaperIdx ] );
143  if ( bLandscape )
144  {
145  Swap( aPaperSize );
146  }
147 
148  maWidthHeightField->SetValue( maWidthHeightField->Normalize( aPaperSize.Width() ), FieldUnit::TWIP );
149  aWidthStr = localeDataWrapper.getNum(
150  maWidthHeightField->GetValue(),
151  maWidthHeightField->GetDecimalDigits(),
152  maWidthHeightField->IsUseThousandSep(),
153  maWidthHeightField->IsShowTrailingZeros() );
154 
155  maWidthHeightField->SetValue( maWidthHeightField->Normalize( aPaperSize.Height() ), FieldUnit::TWIP);
156  aHeightStr = localeDataWrapper.getNum(
157  maWidthHeightField->GetValue(),
158  maWidthHeightField->GetDecimalDigits(),
159  maWidthHeightField->IsUseThousandSep(),
160  maWidthHeightField->IsShowTrailingZeros() );
161 
162  aItemText2 = aWidthStr + " x " + aHeightStr + " " + aMetricStr;
163 
165  SvxPaperInfo::GetName( maPaperList[ nPaperIdx ] ),
166  aItemText2 );
167 
168  if ( pSize && aPaperSize == pSize->GetSize() )
169  {
170  nSelectedItem = nPaperIdx + 1;
171  }
172  }
173  }
174  mpSizeValueSet->SetNoSelection();
175  mpSizeValueSet->SetSelectHdl( LINK(this, PageSizeControl, ImplSizeHdl ) );
176  mpSizeValueSet->Show();
177  mpSizeValueSet->Resize();
178 
179  mpSizeValueSet->SelectItem( nSelectedItem );
180  mpSizeValueSet->SetFormat();
181  mpSizeValueSet->Invalidate();
182  mpSizeValueSet->StartSelection();
183 
184  maMoreButton->SetClickHdl( LINK( this, PageSizeControl, MoreButtonClickHdl_Impl ) );
186 }
187 
189 {
190  disposeOnce();
191 }
192 
194 {
200 }
201 
203 {
204  bool bLandscape = false;
205  const SfxPoolItem *pItem;
206  MapUnit eUnit = lcl_GetUnit();
207  if ( SfxViewFrame::Current() )
208  {
209  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE, pItem );
210  bLandscape = static_cast<const SvxPageItem*>(pItem)->IsLandscape();
211 
212  std::unique_ptr<SvxSizeItem> pPageSizeItem( new SvxSizeItem(SID_ATTR_PAGE_SIZE) );
213  Size aPageSize = SvxPaperInfo::GetPaperSize( ePaper, eUnit );
214  if ( bLandscape )
215  {
216  Swap( aPageSize );
217  }
218  pPageSizeItem->SetSize( aPageSize );
219 
220  SfxViewFrame::Current()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE,
221  SfxCallMode::RECORD, { pPageSizeItem.get() });
222  }
223 }
224 
225 
226 IMPL_LINK(PageSizeControl, ImplSizeHdl, ValueSet*, pControl, void)
227 {
228  mpSizeValueSet->SetNoSelection();
229  if ( pControl == mpSizeValueSet )
230  {
231  const sal_uInt16 nSelectedPaper = mpSizeValueSet->GetSelectedItemId();
232  const Paper ePaper = maPaperList[nSelectedPaper - 1];
233  ExecuteSizeChange( ePaper );
234  }
235 
236  EndPopupMode();
237 }
238 
239 IMPL_LINK_NOARG(PageSizeControl, MoreButtonClickHdl_Impl, Button*, void)
240 {
241  if ( SfxViewFrame::Current() )
243  EndPopupMode();
244 }
245 
246 } } // end of namespace sw::sidebar
247 
248 /* 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()
Dialog to specify the properties of date form field.
Definition: accfrmobj.cxx:40
PageSizeControl(sal_uInt16 nId, vcl::Window *pParent)
SfxApplication * SfxGetpApp()
virtual MapUnit GetMetric(sal_uInt16 nWhich) const
Paper
sal_uInt16 sal_Unicode
virtual void dispose() override
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:293
PAPER_B4_ISO
SfxBindings & GetBindings()
int i
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
WinBits const WB_3DLOOK
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
SfxDispatcher * GetDispatcher() const
VclVBox * get() 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)
IMPL_LINK(PageColumnControl, ColumnButtonClickHdl_Impl, Button *, pButton, void)
virtual void dispose() override
PAPER_LETTER
const Size & GetSize() const