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, "valueset", *mxSizeValueSet))
79  , mxControl(pControl)
80  , maPaperList()
81 {
82  mxWidthHeightField->set_unit(FieldUnit::CM);
83  mxWidthHeightField->set_range(0, 9999, FieldUnit::NONE);
84  mxWidthHeightField->set_digits(2);
85  mxWidthHeightField->set_increments(10, 100, FieldUnit::NONE);
86  SetFieldUnit( *mxWidthHeightField, lcl_GetFieldUnit() );
87 
88  maPaperList.push_back( PAPER_A3 );
89  maPaperList.push_back( PAPER_A4 );
90  maPaperList.push_back( PAPER_A5 );
91  maPaperList.push_back( PAPER_B4_ISO );
92  maPaperList.push_back( PAPER_B5_ISO );
93  maPaperList.push_back( PAPER_ENV_C5 );
94  maPaperList.push_back( PAPER_LETTER );
95  maPaperList.push_back( PAPER_LEGAL );
96 
97  mxSizeValueSet->SetStyle( mxSizeValueSet->GetStyle() | WB_3DLOOK | WB_NO_DIRECTSELECT );
98  mxSizeValueSet->SetColor( Application::GetSettings().GetStyleSettings().GetMenuColor() );
99 
100  sal_uInt16 nSelectedItem = 0;
101  {
102  OUString aMetricStr;
103  {
104  const OUString aText = mxWidthHeightField->get_text();
105  for (short i = aText.getLength() - 1; i >= 0; i--)
106  {
107  sal_Unicode c = aText[i];
108  if ( rtl::isAsciiAlpha(c) || (c == '\'') || (c == '\"') || (c == '%') )
109  {
110  aMetricStr = OUStringChar(c) + aMetricStr;
111  }
112  else
113  {
114  if (!aMetricStr.isEmpty())
115  {
116  break;
117  }
118  }
119  }
120  }
121 
122  bool bLandscape = false;
123  const SvxSizeItem* pSize = nullptr;
124  if ( SfxViewFrame::Current() )
125  {
126  const SfxPoolItem* pItem;
127  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE, pItem );
128  bLandscape = static_cast<const SvxPageItem*>(pItem)->IsLandscape();
129  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE_SIZE, pItem );
130  pSize = static_cast<const SvxSizeItem*>(pItem);
131  }
132 
133  const LocaleDataWrapper& localeDataWrapper = Application::GetSettings().GetLocaleDataWrapper();
134  OUString aWidthStr;
135  OUString aHeightStr;
136  OUString aItemText2;
137  for ( std::vector< Paper >::size_type nPaperIdx = 0;
138  nPaperIdx < maPaperList.size();
139  ++nPaperIdx )
140  {
141  Size aPaperSize = SvxPaperInfo::GetPaperSize( maPaperList[ nPaperIdx ] );
142  if ( bLandscape )
143  {
144  Swap( aPaperSize );
145  }
146 
147  mxWidthHeightField->set_value( mxWidthHeightField->normalize( aPaperSize.Width() ), FieldUnit::TWIP );
148  aWidthStr = localeDataWrapper.getNum(
149  mxWidthHeightField->get_value(FieldUnit::NONE),
150  mxWidthHeightField->get_digits(),
151  true,
152  true );
153 
154  mxWidthHeightField->set_value( mxWidthHeightField->normalize( aPaperSize.Height() ), FieldUnit::TWIP);
155  aHeightStr = localeDataWrapper.getNum(
156  mxWidthHeightField->get_value(FieldUnit::NONE),
157  mxWidthHeightField->get_digits(),
158  true,
159  true );
160 
161  aItemText2 = aWidthStr + " x " + aHeightStr + " " + aMetricStr;
162 
163  mxSizeValueSet->AddItem(
164  SvxPaperInfo::GetName( maPaperList[ nPaperIdx ] ),
165  aItemText2 );
166 
167  if ( pSize && aPaperSize == pSize->GetSize() )
168  {
169  nSelectedItem = nPaperIdx + 1;
170  }
171  }
172  }
173  mxSizeValueSet->SetNoSelection();
174  mxSizeValueSet->SetSelectHdl( LINK(this, PageSizeControl, ImplSizeHdl ) );
175  mxSizeValueSet->Show();
176  mxSizeValueSet->Resize();
177 
178  mxSizeValueSet->SelectItem( nSelectedItem );
179  mxSizeValueSet->SetFormat();
180  mxSizeValueSet->Invalidate();
181 
182  mxMoreButton->connect_clicked( LINK( this, PageSizeControl, MoreButtonClickHdl_Impl ) );
183  mxMoreButton->grab_focus();
184 }
185 
187 {
188  mxSizeValueSet->GrabFocus();
189 }
190 
192 {
193 }
194 
196 {
197  bool bLandscape = false;
198  const SfxPoolItem *pItem;
199  MapUnit eUnit = lcl_GetUnit();
200  if ( !SfxViewFrame::Current() )
201  return;
202 
203  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE, pItem );
204  bLandscape = static_cast<const SvxPageItem*>(pItem)->IsLandscape();
205 
206  std::unique_ptr<SvxSizeItem> pPageSizeItem( new SvxSizeItem(SID_ATTR_PAGE_SIZE) );
207  Size aPageSize = SvxPaperInfo::GetPaperSize( ePaper, eUnit );
208  if ( bLandscape )
209  {
210  Swap( aPageSize );
211  }
212  pPageSizeItem->SetSize( aPageSize );
213 
214  SfxViewFrame::Current()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE,
215  SfxCallMode::RECORD, { pPageSizeItem.get() });
216 }
217 
218 
220 {
221  mxSizeValueSet->SetNoSelection();
222  const sal_uInt16 nSelectedPaper = mxSizeValueSet->GetSelectedItemId();
223  const Paper ePaper = maPaperList[nSelectedPaper - 1];
224  ExecuteSizeChange( ePaper );
225 
226  mxControl->EndPopupMode();
227 }
228 
229 IMPL_LINK_NOARG(PageSizeControl, MoreButtonClickHdl_Impl, weld::Button&, void)
230 {
231  if ( SfxViewFrame::Current() )
233  mxControl->EndPopupMode();
234 }
235 
236 } // end of namespace sw::sidebar
237 
238 /* 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()
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:310
SfxBindings & GetBindings()
tools::Long Width() const
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
static void ExecuteSizeChange(const Paper ePaper)
tools::Long Height() const
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