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 const SfxUInt16Item* pItem = nullptr;
49 if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
50 {
51 SfxItemState eState = pViewFrm->GetBindings().GetDispatcher()->QueryState(SID_ATTR_METRIC, pItem);
52 if (pItem && eState >= SfxItemState::DEFAULT)
53 return static_cast<FieldUnit>(pItem->GetValue());
54 }
56 }
57
58 MapUnit lcl_GetUnit()
59 {
60 SfxItemPool &rPool = SfxGetpApp()->GetPool();
61 sal_uInt16 nWhich = rPool.GetWhich( SID_ATTR_PAGE_SIZE );
62 return rPool.GetMetric( nWhich );
63 }
64}
65
66namespace sw::sidebar {
67
69 : WeldToolbarPopup(pControl->getFrameInterface(), pParent, "modules/swriter/ui/pagesizecontrol.ui", "PageSizeControl")
70 , mxMoreButton(m_xBuilder->weld_button("moreoptions"))
71 , mxWidthHeightField(m_xBuilder->weld_metric_spin_button("metric", FieldUnit::CM))
72 , mxSizeValueSet(new svx::sidebar::ValueSetWithTextControl)
73 , mxSizeValueSetWin(new weld::CustomWeld(*m_xBuilder, "pagesizevalueset", *mxSizeValueSet))
74 , mxControl(pControl)
75{
76 mxWidthHeightField->set_unit(FieldUnit::CM);
77 mxWidthHeightField->set_range(0, 9999, FieldUnit::NONE);
78 mxWidthHeightField->set_digits(2);
79 mxWidthHeightField->set_increments(10, 100, FieldUnit::NONE);
80 SetFieldUnit( *mxWidthHeightField, lcl_GetFieldUnit() );
81
82 maPaperList.push_back( PAPER_A3 );
83 maPaperList.push_back( PAPER_A4 );
84 maPaperList.push_back( PAPER_A5 );
85 maPaperList.push_back( PAPER_B4_ISO );
86 maPaperList.push_back( PAPER_B5_ISO );
87 maPaperList.push_back( PAPER_ENV_C5 );
88 maPaperList.push_back( PAPER_LETTER );
89 maPaperList.push_back( PAPER_LEGAL );
90
91 mxSizeValueSet->SetStyle( mxSizeValueSet->GetStyle() | WB_3DLOOK | WB_NO_DIRECTSELECT );
92 mxSizeValueSet->SetColor( Application::GetSettings().GetStyleSettings().GetMenuColor() );
93
94 sal_uInt16 nSelectedItem = 0;
95 {
96 OUString aMetricStr;
97 {
98 const OUString aText = mxWidthHeightField->get_text();
99 for (short i = aText.getLength() - 1; i >= 0; i--)
100 {
101 sal_Unicode c = aText[i];
102 if ( rtl::isAsciiAlpha(c) || (c == '\'') || (c == '\"') || (c == '%') )
103 {
104 aMetricStr = OUStringChar(c) + aMetricStr;
105 }
106 else
107 {
108 if (!aMetricStr.isEmpty())
109 {
110 break;
111 }
112 }
113 }
114 }
115
116 bool bLandscape = false;
117 const SvxSizeItem* pSize = nullptr;
118 if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
119 {
120 const SvxPageItem* pPageItem;
121 pViewFrm->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE, pPageItem );
122 bLandscape = pPageItem->IsLandscape();
123 pViewFrm->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE_SIZE, pSize );
124 }
125
127 OUString aWidthStr;
128 OUString aHeightStr;
129 OUString aItemText2;
130 for ( std::vector< Paper >::size_type nPaperIdx = 0;
131 nPaperIdx < maPaperList.size();
132 ++nPaperIdx )
133 {
134 Size aPaperSize = SvxPaperInfo::GetPaperSize( maPaperList[ nPaperIdx ] );
135 if ( bLandscape )
136 {
137 Swap( aPaperSize );
138 }
139
140 mxWidthHeightField->set_value( mxWidthHeightField->normalize( aPaperSize.Width() ), FieldUnit::TWIP );
141 aWidthStr = localeDataWrapper.getNum(
142 mxWidthHeightField->get_value(FieldUnit::NONE),
143 mxWidthHeightField->get_digits(),
144 true,
145 true );
146
147 mxWidthHeightField->set_value( mxWidthHeightField->normalize( aPaperSize.Height() ), FieldUnit::TWIP);
148 aHeightStr = localeDataWrapper.getNum(
149 mxWidthHeightField->get_value(FieldUnit::NONE),
150 mxWidthHeightField->get_digits(),
151 true,
152 true );
153
154 aItemText2 = aWidthStr + " x " + aHeightStr + " " + aMetricStr;
155
156 mxSizeValueSet->AddItem(
157 SvxPaperInfo::GetName( maPaperList[ nPaperIdx ] ),
158 aItemText2 );
159
160 if ( pSize && aPaperSize == pSize->GetSize() )
161 {
162 nSelectedItem = nPaperIdx + 1;
163 }
164 }
165 }
166 mxSizeValueSet->SetNoSelection();
167 mxSizeValueSet->SetSelectHdl( LINK(this, PageSizeControl, ImplSizeHdl ) );
168 mxSizeValueSet->SetOptimalDrawingAreaHeight();
169 mxSizeValueSet->Show();
170 mxSizeValueSet->Resize();
171
172 mxSizeValueSet->SelectItem( nSelectedItem );
173 mxSizeValueSet->SetFormat();
174 mxSizeValueSet->Invalidate();
175
176 mxMoreButton->connect_clicked( LINK( this, PageSizeControl, MoreButtonClickHdl_Impl ) );
177 mxMoreButton->grab_focus();
178}
179
181{
182 mxSizeValueSet->GrabFocus();
183}
184
186{
187}
188
190{
191 bool bLandscape = false;
192 const SvxPageItem *pItem;
193 MapUnit eUnit = lcl_GetUnit();
195 if (!pViewFrm)
196 return;
197
198 pViewFrm->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE, pItem );
199 bLandscape = pItem->IsLandscape();
200
201 SvxSizeItem aPageSizeItem(SID_ATTR_PAGE_SIZE);
202 Size aPageSize = SvxPaperInfo::GetPaperSize( ePaper, eUnit );
203 if ( bLandscape )
204 {
205 Swap( aPageSize );
206 }
207 aPageSizeItem.SetSize( aPageSize );
208
209 pViewFrm->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE,
210 SfxCallMode::RECORD, { &aPageSizeItem });
211}
212
213
215{
216 mxSizeValueSet->SetNoSelection();
217 const sal_uInt16 nSelectedPaper = mxSizeValueSet->GetSelectedItemId();
218 const Paper ePaper = maPaperList[nSelectedPaper - 1];
219 ExecuteSizeChange( ePaper );
220
221 mxControl->EndPopupMode();
222}
223
224IMPL_LINK_NOARG(PageSizeControl, MoreButtonClickHdl_Impl, weld::Button&, void)
225{
226 if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
227 pViewFrm->GetDispatcher()->Execute( FN_FORMAT_PAGE_SETTING_DLG, SfxCallMode::ASYNCHRON );
228 mxControl->EndPopupMode();
229}
230
231} // end of namespace sw::sidebar
232
233/* 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 * 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 SAL_WARN_UNUSED_RESULT 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:345
FieldUnit
MapUnit
int i
IMPL_LINK_NOARG(AccessibilityCheckEntry, GotoButtonClicked, weld::LinkButton &, bool)
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