Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / svx / source / dialog / optgrid.cxx
blob598aacb420e99ec8c1e972fb2084324e632f52e0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <svl/intitem.hxx>
21 #include <svtools/unitconv.hxx>
23 #include <svx/svxids.hrc>
24 #include <svx/optgrid.hxx>
25 #include <svx/dlgutil.hxx>
27 // local functions
28 static void lcl_GetMinMax(weld::MetricSpinButton const& rField, int& nMin, int& nMax)
30 rField.get_range(nMin, nMax, FieldUnit::TWIP);
31 nMin = rField.denormalize(nMin);
32 nMax = rField.denormalize(nMax);
35 static void lcl_SetMinMax(weld::MetricSpinButton& rField, int nMin, int nMax)
37 rField.set_range(rField.normalize(nMin), rField.normalize(nMax), FieldUnit::TWIP);
40 SvxOptionsGrid::SvxOptionsGrid() :
41 nFldDrawX ( 100 ),
42 nFldDivisionX ( 0 ),
43 nFldDrawY ( 100 ),
44 nFldDivisionY ( 0 ),
45 nFldSnapX ( 100 ),
46 nFldSnapY ( 100 ),
47 bUseGridsnap ( false ),
48 bSynchronize ( true ),
49 bGridVisible ( false ),
50 bEqualGrid ( true )
54 SvxGridItem* SvxGridItem::Clone( SfxItemPool* ) const
56 return new SvxGridItem( *this );
59 bool SvxGridItem::operator==( const SfxPoolItem& rAttr ) const
61 assert(SfxPoolItem::operator==(rAttr));
63 const SvxGridItem& rItem = static_cast<const SvxGridItem&>(rAttr);
65 return ( bUseGridsnap == rItem.bUseGridsnap &&
66 bSynchronize == rItem.bSynchronize &&
67 bGridVisible == rItem.bGridVisible &&
68 bEqualGrid == rItem.bEqualGrid &&
69 nFldDrawX == rItem.nFldDrawX &&
70 nFldDivisionX== rItem.nFldDivisionX&&
71 nFldDrawY == rItem.nFldDrawY &&
72 nFldDivisionY== rItem.nFldDivisionY&&
73 nFldSnapX == rItem.nFldSnapX &&
74 nFldSnapY == rItem.nFldSnapY );
77 bool SvxGridItem::GetPresentation
79 SfxItemPresentation /*ePres*/,
80 MapUnit /*eCoreUnit*/,
81 MapUnit /*ePresUnit*/,
82 OUString& rText, const IntlWrapper&
83 ) const
85 rText = "SvxGridItem";
86 return true;
89 // TabPage Screen Settings
90 SvxGridTabPage::SvxGridTabPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rCoreSet)
91 : SfxTabPage(pPage, pController, "svx/ui/optgridpage.ui", "OptGridPage", &rCoreSet)
92 , bAttrModified(false)
93 , m_xCbxUseGridsnap(m_xBuilder->weld_check_button("usegridsnap"))
94 , m_xCbxGridVisible(m_xBuilder->weld_check_button("gridvisible"))
95 , m_xMtrFldDrawX(m_xBuilder->weld_metric_spin_button("mtrflddrawx", FieldUnit::CM))
96 , m_xMtrFldDrawY(m_xBuilder->weld_metric_spin_button("mtrflddrawy", FieldUnit::CM))
97 , m_xNumFldDivisionX(m_xBuilder->weld_spin_button("numflddivisionx"))
98 , m_xNumFldDivisionY(m_xBuilder->weld_spin_button("numflddivisiony"))
99 , m_xCbxSynchronize(m_xBuilder->weld_check_button("synchronize"))
100 , m_xSnapFrames(m_xBuilder->weld_widget("snapframes"))
101 , m_xCbxSnapHelplines(m_xBuilder->weld_check_button("snaphelplines"))
102 , m_xCbxSnapBorder(m_xBuilder->weld_check_button("snapborder"))
103 , m_xCbxSnapFrame(m_xBuilder->weld_check_button("snapframe"))
104 , m_xCbxSnapPoints(m_xBuilder->weld_check_button("snappoints"))
105 , m_xMtrFldSnapArea(m_xBuilder->weld_metric_spin_button("mtrfldsnaparea", FieldUnit::PIXEL))
106 , m_xCbxOrtho(m_xBuilder->weld_check_button("ortho"))
107 , m_xCbxBigOrtho(m_xBuilder->weld_check_button("bigortho"))
108 , m_xCbxRotate(m_xBuilder->weld_check_button("rotate"))
109 , m_xMtrFldAngle(m_xBuilder->weld_metric_spin_button("mtrfldangle", FieldUnit::DEGREE))
110 , m_xMtrFldBezAngle(m_xBuilder->weld_metric_spin_button("mtrfldbezangle", FieldUnit::DEGREE))
112 // This page requires exchange Support
113 SetExchangeSupport();
115 // Set Metrics
116 FieldUnit eFUnit = GetModuleFieldUnit( rCoreSet );
117 int nMin, nMax;
119 lcl_GetMinMax(*m_xMtrFldDrawX, nMin, nMax);
120 SetFieldUnit( *m_xMtrFldDrawX, eFUnit, true );
121 lcl_SetMinMax(*m_xMtrFldDrawX, nMin, nMax);
123 lcl_GetMinMax(*m_xMtrFldDrawY, nMin, nMax);
124 SetFieldUnit( *m_xMtrFldDrawY, eFUnit, true );
125 lcl_SetMinMax(*m_xMtrFldDrawY, nMin, nMax);
128 m_xCbxRotate->connect_toggled(LINK(this, SvxGridTabPage, ClickRotateHdl_Impl));
129 Link<weld::ToggleButton&,void> aLink = LINK(this, SvxGridTabPage, ChangeGridsnapHdl_Impl);
130 m_xCbxUseGridsnap->connect_toggled(aLink);
131 m_xCbxSynchronize->connect_toggled(aLink);
132 m_xCbxGridVisible->connect_toggled(aLink);
133 m_xMtrFldDrawX->connect_value_changed(
134 LINK( this, SvxGridTabPage, ChangeDrawHdl_Impl ) );
135 m_xMtrFldDrawY->connect_value_changed(
136 LINK( this, SvxGridTabPage, ChangeDrawHdl_Impl ) );
137 m_xNumFldDivisionX->connect_value_changed(
138 LINK( this, SvxGridTabPage, ChangeDivisionHdl_Impl ) );
139 m_xNumFldDivisionY->connect_value_changed(
140 LINK( this, SvxGridTabPage, ChangeDivisionHdl_Impl ) );
143 SvxGridTabPage::~SvxGridTabPage()
147 std::unique_ptr<SfxTabPage> SvxGridTabPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rAttrSet)
149 return std::make_unique<SvxGridTabPage>(pPage, pController, rAttrSet);
152 bool SvxGridTabPage::FillItemSet( SfxItemSet* rCoreSet )
154 if ( bAttrModified )
156 SvxGridItem aGridItem( SID_ATTR_GRID_OPTIONS );
158 aGridItem.bUseGridsnap = m_xCbxUseGridsnap->get_active();
159 aGridItem.bSynchronize = m_xCbxSynchronize->get_active();
160 aGridItem.bGridVisible = m_xCbxGridVisible->get_active();
162 MapUnit eUnit =
163 rCoreSet->GetPool()->GetMetric( GetWhich( SID_ATTR_GRID_OPTIONS ) );
164 tools::Long nX = GetCoreValue( *m_xMtrFldDrawX, eUnit );
165 tools::Long nY = GetCoreValue( *m_xMtrFldDrawY, eUnit );
167 aGridItem.nFldDrawX = static_cast<sal_uInt32>(nX);
168 aGridItem.nFldDrawY = static_cast<sal_uInt32>(nY);
169 aGridItem.nFldDivisionX = static_cast<tools::Long>(m_xNumFldDivisionX->get_value() - 1);
170 aGridItem.nFldDivisionY = static_cast<tools::Long>(m_xNumFldDivisionY->get_value() - 1);
172 rCoreSet->Put( aGridItem );
174 return bAttrModified;
177 void SvxGridTabPage::Reset( const SfxItemSet* rSet )
179 const SfxPoolItem* pAttr = nullptr;
181 if( SfxItemState::SET == rSet->GetItemState( SID_ATTR_GRID_OPTIONS , false,
182 &pAttr ))
184 const SvxGridItem* pGridAttr = static_cast<const SvxGridItem*>(pAttr);
185 m_xCbxUseGridsnap->set_active(pGridAttr->bUseGridsnap);
186 m_xCbxSynchronize->set_active(pGridAttr->bSynchronize);
187 m_xCbxGridVisible->set_active(pGridAttr->bGridVisible);
189 MapUnit eUnit =
190 rSet->GetPool()->GetMetric( GetWhich( SID_ATTR_GRID_OPTIONS ) );
191 SetMetricValue( *m_xMtrFldDrawX , pGridAttr->nFldDrawX, eUnit );
192 SetMetricValue( *m_xMtrFldDrawY , pGridAttr->nFldDrawY, eUnit );
194 m_xNumFldDivisionX->set_value(pGridAttr->nFldDivisionX + 1);
195 m_xNumFldDivisionY->set_value(pGridAttr->nFldDivisionY + 1);
198 ChangeGridsnapHdl_Impl(*m_xCbxUseGridsnap);
199 bAttrModified = false;
202 void SvxGridTabPage::ActivatePage( const SfxItemSet& rSet )
204 const SfxPoolItem* pAttr = nullptr;
205 if( SfxItemState::SET == rSet.GetItemState( SID_ATTR_GRID_OPTIONS , false,
206 &pAttr ))
208 const SvxGridItem* pGridAttr = static_cast<const SvxGridItem*>(pAttr);
209 m_xCbxUseGridsnap->set_active(pGridAttr->bUseGridsnap);
211 ChangeGridsnapHdl_Impl(*m_xCbxUseGridsnap);
214 // Metric Change if necessary (as TabPage is in the dialog, where the
215 // metric can be set
216 if( SfxItemState::SET != rSet.GetItemState( SID_ATTR_METRIC , false,
217 &pAttr ))
218 return;
220 const SfxUInt16Item* pItem = static_cast<const SfxUInt16Item*>(pAttr);
222 FieldUnit eFUnit = static_cast<FieldUnit>(static_cast<tools::Long>(pItem->GetValue()));
224 if (eFUnit == m_xMtrFldDrawX->get_unit())
225 return;
227 // Set Metrics
228 int nMin, nMax;
229 int nVal = m_xMtrFldDrawX->denormalize(m_xMtrFldDrawX->get_value(FieldUnit::TWIP));
231 lcl_GetMinMax(*m_xMtrFldDrawX, nMin, nMax);
232 SetFieldUnit(*m_xMtrFldDrawX, eFUnit, true);
233 lcl_SetMinMax(*m_xMtrFldDrawX, nMin, nMax);
235 m_xMtrFldDrawX->set_value(m_xMtrFldDrawX->normalize(nVal), FieldUnit::TWIP);
237 nVal = m_xMtrFldDrawY->denormalize(m_xMtrFldDrawY->get_value(FieldUnit::TWIP));
238 lcl_GetMinMax(*m_xMtrFldDrawY, nMin, nMax);
239 SetFieldUnit(*m_xMtrFldDrawY, eFUnit, true);
240 lcl_SetMinMax(*m_xMtrFldDrawY, nMin, nMax);
241 m_xMtrFldDrawY->set_value(m_xMtrFldDrawY->normalize(nVal), FieldUnit::TWIP);
244 DeactivateRC SvxGridTabPage::DeactivatePage( SfxItemSet* _pSet )
246 if ( _pSet )
247 FillItemSet( _pSet );
248 return DeactivateRC::LeavePage;
251 IMPL_LINK(SvxGridTabPage, ChangeDrawHdl_Impl, weld::MetricSpinButton&, rField, void)
253 bAttrModified = true;
254 if (m_xCbxSynchronize->get_active())
256 if (&rField == m_xMtrFldDrawX.get())
257 m_xMtrFldDrawY->set_value(m_xMtrFldDrawX->get_value(FieldUnit::NONE), FieldUnit::NONE);
258 else
259 m_xMtrFldDrawX->set_value(m_xMtrFldDrawY->get_value(FieldUnit::NONE), FieldUnit::NONE);
263 IMPL_LINK_NOARG(SvxGridTabPage, ClickRotateHdl_Impl, weld::ToggleButton&, void)
265 if (m_xCbxRotate->get_active())
266 m_xMtrFldAngle->set_sensitive(true);
267 else
268 m_xMtrFldAngle->set_sensitive(false);
271 IMPL_LINK(SvxGridTabPage, ChangeDivisionHdl_Impl, weld::SpinButton&, rField, void)
273 bAttrModified = true;
274 if (m_xCbxSynchronize->get_active())
276 if (m_xNumFldDivisionX.get() == &rField)
277 m_xNumFldDivisionY->set_value(m_xNumFldDivisionX->get_value());
278 else
279 m_xNumFldDivisionX->set_value(m_xNumFldDivisionY->get_value());
283 IMPL_LINK_NOARG(SvxGridTabPage, ChangeGridsnapHdl_Impl, weld::ToggleButton&, void)
285 bAttrModified = true;
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */