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
44namespace
45{
46 FieldUnit lcl_GetFieldUnit()
47 {
48 FieldUnit eUnit = FieldUnit::INCH;
49 const SfxUInt16Item* 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>(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
71namespace 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 SvxPageItem* pPageItem;
126 SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE, pPageItem );
127 bLandscape = pPageItem->IsLandscape();
128 SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE_SIZE, pSize );
129 }
130
132 OUString aWidthStr;
133 OUString aHeightStr;
134 OUString aItemText2;
135 for ( std::vector< Paper >::size_type nPaperIdx = 0;
136 nPaperIdx < maPaperList.size();
137 ++nPaperIdx )
138 {
139 Size aPaperSize = SvxPaperInfo::GetPaperSize( maPaperList[ nPaperIdx ] );
140 if ( bLandscape )
141 {
142 Swap( aPaperSize );
143 }
144
145 mxWidthHeightField->set_value( mxWidthHeightField->normalize( aPaperSize.Width() ), FieldUnit::TWIP );
146 aWidthStr = localeDataWrapper.getNum(
147 mxWidthHeightField->get_value(FieldUnit::NONE),
148 mxWidthHeightField->get_digits(),
149 true,
150 true );
151
152 mxWidthHeightField->set_value( mxWidthHeightField->normalize( aPaperSize.Height() ), FieldUnit::TWIP);
153 aHeightStr = localeDataWrapper.getNum(
154 mxWidthHeightField->get_value(FieldUnit::NONE),
155 mxWidthHeightField->get_digits(),
156 true,
157 true );
158
159 aItemText2 = aWidthStr + " x " + aHeightStr + " " + aMetricStr;
160
161 mxSizeValueSet->AddItem(
162 SvxPaperInfo::GetName( maPaperList[ nPaperIdx ] ),
163 aItemText2 );
164
165 if ( pSize && aPaperSize == pSize->GetSize() )
166 {
167 nSelectedItem = nPaperIdx + 1;
168 }
169 }
170 }
171 mxSizeValueSet->SetNoSelection();
172 mxSizeValueSet->SetSelectHdl( LINK(this, PageSizeControl, ImplSizeHdl ) );
173 mxSizeValueSet->Show();
174 mxSizeValueSet->Resize();
175
176 mxSizeValueSet->SelectItem( nSelectedItem );
177 mxSizeValueSet->SetFormat();
178 mxSizeValueSet->Invalidate();
179
180 mxMoreButton->connect_clicked( LINK( this, PageSizeControl, MoreButtonClickHdl_Impl ) );
181 mxMoreButton->grab_focus();
182}
183
185{
186 mxSizeValueSet->GrabFocus();
187}
188
190{
191}
192
194{
195 bool bLandscape = false;
196 const SvxPageItem *pItem;
197 MapUnit eUnit = lcl_GetUnit();
198 if ( !SfxViewFrame::Current() )
199 return;
200
201 SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE, pItem );
202 bLandscape = pItem->IsLandscape();
203
204 SvxSizeItem aPageSizeItem(SID_ATTR_PAGE_SIZE);
205 Size aPageSize = SvxPaperInfo::GetPaperSize( ePaper, eUnit );
206 if ( bLandscape )
207 {
208 Swap( aPageSize );
209 }
210 aPageSizeItem.SetSize( aPageSize );
211
212 SfxViewFrame::Current()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE,
213 SfxCallMode::RECORD, { &aPageSizeItem });
214}
215
216
218{
219 mxSizeValueSet->SetNoSelection();
220 const sal_uInt16 nSelectedPaper = mxSizeValueSet->GetSelectedItemId();
221 const Paper ePaper = maPaperList[nSelectedPaper - 1];
222 ExecuteSizeChange( ePaper );
223
224 mxControl->EndPopupMode();
225}
226
227IMPL_LINK_NOARG(PageSizeControl, MoreButtonClickHdl_Impl, weld::Button&, void)
228{
229 if ( SfxViewFrame::Current() )
231 mxControl->EndPopupMode();
232}
233
234} // end of namespace sw::sidebar
235
236/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SfxApplication * SfxGetpApp()
const LocaleDataWrapper & GetLocaleDataWrapper() const
static const AllSettings & GetSettings()
sal_uInt16 GetValue() const
OUString getNum(sal_Int64 nNumber, sal_uInt16 nDecimals, bool bUseThousandSep=true, bool bTrailingZeros=true) const
SfxDispatcher * GetDispatcher() const
const SfxPoolItem * Execute(sal_uInt16 nSlot, SfxCallMode nCall=SfxCallMode::SLOT, const SfxPoolItem **pArgs=nullptr, sal_uInt16 nModi=0, const SfxPoolItem **pInternalArgs=nullptr)
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 * >())
SfxItemState QueryState(sal_uInt16 nSID, const SfxPoolItem *&rpState)
sal_uInt16 GetWhich(sal_uInt16 nSlot, bool bDeep=true) const
virtual MapUnit GetMetric(sal_uInt16 nWhich) const
static FieldUnit GetCurrentFieldUnit()
SfxItemPool & GetPool() const
static SfxViewFrame * Current()
SfxBindings & GetBindings()
SfxDispatcher * GetDispatcher()
constexpr tools::Long Height() const
constexpr tools::Long Width() const
bool IsLandscape() const
static OUString GetName(Paper ePaper)
static Size GetPaperSize(Paper ePaper, MapUnit eUnit=MapUnit::MapTwip)
const Size & GetSize() const
void SetSize(const Size &rSize)
std::unique_ptr< weld::MetricSpinButton > mxWidthHeightField
std::unique_ptr< weld::Button > mxMoreButton
std::vector< Paper > maPaperList
std::unique_ptr< svx::sidebar::ValueSetWithTextControl > mxSizeValueSet
static void ExecuteSizeChange(const Paper ePaper)
virtual ~PageSizeControl() override
PageSizeControl(PageSizePopup *pControl, weld::Widget *pParent)
virtual void GrabFocus() override
#define FN_FORMAT_PAGE_SETTING_DLG
Definition: cmdid.h:325
FieldUnit
MapUnit
int i
IMPL_LINK_NOARG(PageColumnControl, MoreButtonClickHdl_Impl, weld::Button &, void)
Paper
PAPER_LETTER
PAPER_A5
PAPER_A4
PAPER_B5_ISO
PAPER_A3
PAPER_LEGAL
PAPER_B4_ISO
PAPER_ENV_C5
Size & Swap(Size &rSize)
SfxItemState
sal_uInt16 sal_Unicode
SVT_DLLPUBLIC void SetFieldUnit(weld::MetricSpinButton &rCtrl, FieldUnit eUnit, bool bAll=false)
#define WB_NO_DIRECTSELECT
WinBits const WB_3DLOOK