1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "PageSizeControl.hxx"
24 #include <svx/pageitem.hxx>
25 #include <svx/sidebar/ValueSetWithTextControl.hxx>
27 #include <unotools/localedatawrapper.hxx>
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>
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>
42 #include <PageSizePopup.hxx>
46 FieldUnit
lcl_GetFieldUnit()
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
)
53 eUnit
= static_cast<FieldUnit
>(static_cast<const SfxUInt16Item
*>(pItem
)->GetValue());
57 return SfxModule::GetCurrentFieldUnit();
65 SfxItemPool
&rPool
= SfxGetpApp()->GetPool();
66 sal_uInt16 nWhich
= rPool
.GetWhich( SID_ATTR_PAGE_SIZE
);
67 return rPool
.GetMetric( nWhich
);
71 namespace sw::sidebar
{
73 PageSizeControl::PageSizeControl(PageSizePopup
* pControl
, weld::Widget
* pParent
)
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
))
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() );
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
);
97 mxSizeValueSet
->SetStyle( mxSizeValueSet
->GetStyle() | WB_3DLOOK
| WB_NO_DIRECTSELECT
);
98 mxSizeValueSet
->SetColor( Application::GetSettings().GetStyleSettings().GetMenuColor() );
100 sal_uInt16 nSelectedItem
= 0;
104 const OUString aText
= mxWidthHeightField
->get_text();
105 for (short i
= aText
.getLength() - 1; i
>= 0; i
--)
107 sal_Unicode c
= aText
[i
];
108 if ( rtl::isAsciiAlpha(c
) || (c
== '\'') || (c
== '\"') || (c
== '%') )
110 aMetricStr
= OUStringChar(c
) + aMetricStr
;
114 if (!aMetricStr
.isEmpty())
122 bool bLandscape
= false;
123 const SvxSizeItem
* pSize
= nullptr;
124 if ( SfxViewFrame::Current() )
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
);
133 const LocaleDataWrapper
& localeDataWrapper
= Application::GetSettings().GetLocaleDataWrapper();
137 for ( std::vector
< Paper
>::size_type nPaperIdx
= 0;
138 nPaperIdx
< maPaperList
.size();
141 Size aPaperSize
= SvxPaperInfo::GetPaperSize( maPaperList
[ nPaperIdx
] );
147 mxWidthHeightField
->set_value( mxWidthHeightField
->normalize( aPaperSize
.Width() ), FieldUnit::TWIP
);
148 aWidthStr
= localeDataWrapper
.getNum(
149 mxWidthHeightField
->get_value(FieldUnit::NONE
),
150 mxWidthHeightField
->get_digits(),
154 mxWidthHeightField
->set_value( mxWidthHeightField
->normalize( aPaperSize
.Height() ), FieldUnit::TWIP
);
155 aHeightStr
= localeDataWrapper
.getNum(
156 mxWidthHeightField
->get_value(FieldUnit::NONE
),
157 mxWidthHeightField
->get_digits(),
161 aItemText2
= aWidthStr
+ " x " + aHeightStr
+ " " + aMetricStr
;
163 mxSizeValueSet
->AddItem(
164 SvxPaperInfo::GetName( maPaperList
[ nPaperIdx
] ),
167 if ( pSize
&& aPaperSize
== pSize
->GetSize() )
169 nSelectedItem
= nPaperIdx
+ 1;
173 mxSizeValueSet
->SetNoSelection();
174 mxSizeValueSet
->SetSelectHdl( LINK(this, PageSizeControl
, ImplSizeHdl
) );
175 mxSizeValueSet
->Show();
176 mxSizeValueSet
->Resize();
178 mxSizeValueSet
->SelectItem( nSelectedItem
);
179 mxSizeValueSet
->SetFormat();
180 mxSizeValueSet
->Invalidate();
182 mxMoreButton
->connect_clicked( LINK( this, PageSizeControl
, MoreButtonClickHdl_Impl
) );
183 mxMoreButton
->grab_focus();
186 void PageSizeControl::GrabFocus()
188 mxSizeValueSet
->GrabFocus();
191 PageSizeControl::~PageSizeControl()
195 void PageSizeControl::ExecuteSizeChange( const Paper ePaper
)
197 bool bLandscape
= false;
198 const SfxPoolItem
*pItem
;
199 MapUnit eUnit
= lcl_GetUnit();
200 if ( !SfxViewFrame::Current() )
203 SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState( SID_ATTR_PAGE
, pItem
);
204 bLandscape
= static_cast<const SvxPageItem
*>(pItem
)->IsLandscape();
206 std::unique_ptr
<SvxSizeItem
> pPageSizeItem( new SvxSizeItem(SID_ATTR_PAGE_SIZE
) );
207 Size aPageSize
= SvxPaperInfo::GetPaperSize( ePaper
, eUnit
);
212 pPageSizeItem
->SetSize( aPageSize
);
214 SfxViewFrame::Current()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE
,
215 SfxCallMode::RECORD
, { pPageSizeItem
.get() });
219 IMPL_LINK_NOARG(PageSizeControl
, ImplSizeHdl
, ValueSet
*, void)
221 mxSizeValueSet
->SetNoSelection();
222 const sal_uInt16 nSelectedPaper
= mxSizeValueSet
->GetSelectedItemId();
223 const Paper ePaper
= maPaperList
[nSelectedPaper
- 1];
224 ExecuteSizeChange( ePaper
);
226 mxControl
->EndPopupMode();
229 IMPL_LINK_NOARG(PageSizeControl
, MoreButtonClickHdl_Impl
, weld::Button
&, void)
231 if ( SfxViewFrame::Current() )
232 SfxViewFrame::Current()->GetDispatcher()->Execute( FN_FORMAT_PAGE_SETTING_DLG
, SfxCallMode::ASYNCHRON
);
233 mxControl
->EndPopupMode();
236 } // end of namespace sw::sidebar
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */